AesGcmEncryptingStream.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * Same as non-static 'getAesName' method, allowing calls in a static
  24. * context.
  25. *
  26. * @return string
  27. */
  28. public static function getStaticAesName()
  29. {
  30. return 'AES/GCM/NoPadding';
  31. }
  32. /**
  33. * @param StreamInterface $plaintext
  34. * @param string $key
  35. * @param string $initializationVector
  36. * @param string $aad
  37. * @param int $tagLength
  38. * @param int $keySize
  39. */
  40. public function __construct(
  41. StreamInterface $plaintext,
  42. $key,
  43. $initializationVector,
  44. $aad = '',
  45. $tagLength = 16,
  46. $keySize = 256
  47. ) {
  48. $this->plaintext = $plaintext;
  49. $this->key = $key;
  50. $this->initializationVector = $initializationVector;
  51. $this->aad = $aad;
  52. $this->tagLength = $tagLength;
  53. $this->keySize = $keySize;
  54. }
  55. public function getOpenSslName()
  56. {
  57. return "aes-{$this->keySize}-gcm";
  58. }
  59. /**
  60. * Same as static method and retained for backwards compatibility
  61. *
  62. * @return string
  63. */
  64. public function getAesName()
  65. {
  66. return self::getStaticAesName();
  67. }
  68. public function getCurrentIv()
  69. {
  70. return $this->initializationVector;
  71. }
  72. public function createStream()
  73. {
  74. if (version_compare(PHP_VERSION, '7.1', '<')) {
  75. return Psr7\stream_for(AesGcm::encrypt(
  76. (string) $this->plaintext,
  77. $this->initializationVector,
  78. new Key($this->key),
  79. $this->aad,
  80. $this->tag,
  81. $this->keySize
  82. ));
  83. } else {
  84. return Psr7\stream_for(\openssl_encrypt(
  85. (string)$this->plaintext,
  86. $this->getOpenSslName(),
  87. $this->key,
  88. OPENSSL_RAW_DATA,
  89. $this->initializationVector,
  90. $this->tag,
  91. $this->aad,
  92. $this->tagLength
  93. ));
  94. }
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getTag()
  100. {
  101. return $this->tag;
  102. }
  103. public function isWritable()
  104. {
  105. return false;
  106. }
  107. }