AesGcmEncryptingStream.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Aws\Crypto;
  3. use Aws\Crypto\Polyfill\AesGcm;
  4. use Aws\Crypto\Polyfill\Key;
  5. use GuzzleHttp\Psr7;
  6. use GuzzleHttp\Psr7\StreamDecoratorTrait;
  7. use Psr\Http\Message\StreamInterface;
  8. use \RuntimeException;
  9. /**
  10. * @internal Represents a stream of data to be gcm encrypted.
  11. */
  12. class AesGcmEncryptingStream implements AesStreamInterface, AesStreamInterfaceV2
  13. {
  14. use StreamDecoratorTrait;
  15. private $aad;
  16. private $initializationVector;
  17. private $key;
  18. private $keySize;
  19. private $plaintext;
  20. private $tag = '';
  21. private $tagLength;
  22. /**
  23. * @var StreamInterface
  24. */
  25. private $stream;
  26. /**
  27. * Same as non-static 'getAesName' method, allowing calls in a static
  28. * context.
  29. *
  30. * @return string
  31. */
  32. public static function getStaticAesName()
  33. {
  34. return 'AES/GCM/NoPadding';
  35. }
  36. /**
  37. * @param StreamInterface $plaintext
  38. * @param string $key
  39. * @param string $initializationVector
  40. * @param string $aad
  41. * @param int $tagLength
  42. * @param int $keySize
  43. */
  44. public function __construct(
  45. StreamInterface $plaintext,
  46. $key,
  47. $initializationVector,
  48. $aad = '',
  49. $tagLength = 16,
  50. $keySize = 256
  51. ) {
  52. $this->plaintext = $plaintext;
  53. $this->key = $key;
  54. $this->initializationVector = $initializationVector;
  55. $this->aad = $aad;
  56. $this->tagLength = $tagLength;
  57. $this->keySize = $keySize;
  58. // unsetting the property forces the first access to go through
  59. // __get().
  60. unset($this->stream);
  61. }
  62. public function getOpenSslName()
  63. {
  64. return "aes-{$this->keySize}-gcm";
  65. }
  66. /**
  67. * Same as static method and retained for backwards compatibility
  68. *
  69. * @return string
  70. */
  71. public function getAesName()
  72. {
  73. return self::getStaticAesName();
  74. }
  75. public function getCurrentIv()
  76. {
  77. return $this->initializationVector;
  78. }
  79. public function createStream()
  80. {
  81. if (version_compare(PHP_VERSION, '7.1', '<')) {
  82. return Psr7\Utils::streamFor(AesGcm::encrypt(
  83. (string) $this->plaintext,
  84. $this->initializationVector,
  85. new Key($this->key),
  86. $this->aad,
  87. $this->tag,
  88. $this->keySize
  89. ));
  90. } else {
  91. return Psr7\Utils::streamFor(\openssl_encrypt(
  92. (string)$this->plaintext,
  93. $this->getOpenSslName(),
  94. $this->key,
  95. OPENSSL_RAW_DATA,
  96. $this->initializationVector,
  97. $this->tag,
  98. $this->aad,
  99. $this->tagLength
  100. ));
  101. }
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getTag()
  107. {
  108. return $this->tag;
  109. }
  110. public function isWritable(): bool
  111. {
  112. return false;
  113. }
  114. }