Cbc.php 2.0 KB

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