| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Crypto\Cipher; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LogicException; |
| 7 |
/** |
| 8 |
* An implementation of the CBC cipher for use with an AesEncryptingStream or |
| 9 |
* AesDecrypting stream. |
| 10 |
* |
| 11 |
* This cipher method is deprecated and in maintenance mode - no new updates will be |
| 12 |
* released. Please see https://docs.aws.amazon.com/general/latest/gr/aws_sdk_cryptography.html |
| 13 |
* for more information. |
| 14 |
* |
| 15 |
* @deprecated |
| 16 |
*/ |
| 17 |
class Cbc implements CipherMethod |
| 18 |
{ |
| 19 |
const BLOCK_SIZE = 16; |
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $baseIv; |
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $iv; |
| 28 |
/** |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
private $keySize; |
| 32 |
/** |
| 33 |
* @param string $iv Base Initialization Vector for the cipher. |
| 34 |
* @param int $keySize Size of the encryption key, in bits, that will be |
| 35 |
* used. |
| 36 |
* |
| 37 |
* @throws InvalidArgumentException Thrown if the passed iv does not match |
| 38 |
* the iv length required by the cipher. |
| 39 |
*/ |
| 40 |
public function __construct($iv, $keySize = 256) |
| 41 |
{ |
| 42 |
$this->baseIv = $this->iv = $iv; |
| 43 |
$this->keySize = $keySize; |
| 44 |
if (\strlen($iv) !== \openssl_cipher_iv_length($this->getOpenSslName())) { |
| 45 |
throw new InvalidArgumentException('Invalid initialization vector'); |
| 46 |
} |
| 47 |
} |
| 48 |
public function getOpenSslName() |
| 49 |
{ |
| 50 |
return "aes-{$this->keySize}-cbc"; |
| 51 |
} |
| 52 |
public function getAesName() |
| 53 |
{ |
| 54 |
return 'AES/CBC/PKCS5Padding'; |
| 55 |
} |
| 56 |
public function getCurrentIv() |
| 57 |
{ |
| 58 |
return $this->iv; |
| 59 |
} |
| 60 |
public function requiresPadding() |
| 61 |
{ |
| 62 |
return \true; |
| 63 |
} |
| 64 |
public function seek($offset, $whence = \SEEK_SET) |
| 65 |
{ |
| 66 |
if ($offset === 0 && $whence === \SEEK_SET) { |
| 67 |
$this->iv = $this->baseIv; |
| 68 |
} else { |
| 69 |
throw new LogicException('CBC initialization only support being' . ' rewound, not arbitrary seeking.'); |
| 70 |
} |
| 71 |
} |
| 72 |
public function update($cipherTextBlock) |
| 73 |
{ |
| 74 |
$this->iv = \substr($cipherTextBlock, self::BLOCK_SIZE * -1); |
| 75 |
} |
| 76 |
} |
| 77 |
|