Cbc.php
89 lines
| 1 | <?php |
| 2 | namespace Aws\Crypto\Cipher; |
| 3 | |
| 4 | use \InvalidArgumentException; |
| 5 | use \LogicException; |
| 6 | |
| 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 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | private $baseIv; |
| 25 | |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $iv; |
| 30 | |
| 31 | /** |
| 32 | * @var int |
| 33 | */ |
| 34 | private $keySize; |
| 35 | |
| 36 | /** |
| 37 | * @param string $iv Base Initialization Vector for the cipher. |
| 38 | * @param int $keySize Size of the encryption key, in bits, that will be |
| 39 | * used. |
| 40 | * |
| 41 | * @throws InvalidArgumentException Thrown if the passed iv does not match |
| 42 | * the iv length required by the cipher. |
| 43 | */ |
| 44 | public function __construct($iv, $keySize = 256) |
| 45 | { |
| 46 | $this->baseIv = $this->iv = $iv; |
| 47 | $this->keySize = $keySize; |
| 48 | |
| 49 | if (strlen($iv) !== openssl_cipher_iv_length($this->getOpenSslName())) { |
| 50 | throw new InvalidArgumentException('Invalid initialization vector'); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function getOpenSslName() |
| 55 | { |
| 56 | return "aes-{$this->keySize}-cbc"; |
| 57 | } |
| 58 | |
| 59 | public function getAesName() |
| 60 | { |
| 61 | return 'AES/CBC/PKCS5Padding'; |
| 62 | } |
| 63 | |
| 64 | public function getCurrentIv() |
| 65 | { |
| 66 | return $this->iv; |
| 67 | } |
| 68 | |
| 69 | public function requiresPadding() |
| 70 | { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | public function seek($offset, $whence = SEEK_SET) |
| 75 | { |
| 76 | if ($offset === 0 && $whence === SEEK_SET) { |
| 77 | $this->iv = $this->baseIv; |
| 78 | } else { |
| 79 | throw new LogicException('CBC initialization only support being' |
| 80 | . ' rewound, not arbitrary seeking.'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function update($cipherTextBlock) |
| 85 | { |
| 86 | $this->iv = substr($cipherTextBlock, self::BLOCK_SIZE * -1); |
| 87 | } |
| 88 | } |
| 89 |