| 1 |
<?php |
| 2 |
namespace Aws\Crypto\Cipher; |
| 3 |
|
| 4 |
use \InvalidArgumentException; |
| 5 |
use \LogicException; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class Cbc implements CipherMethod |
| 18 |
{ |
| 19 |
const BLOCK_SIZE = 16; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private $baseIv; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private $iv; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private $keySize; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
} |