Cipher
11 months ago
Polyfill
11 months ago
AbstractCryptoClient.php
11 months ago
AbstractCryptoClientV2.php
11 months ago
AesDecryptingStream.php
11 months ago
AesEncryptingStream.php
11 months ago
AesGcmDecryptingStream.php
11 months ago
AesGcmEncryptingStream.php
11 months ago
AesStreamInterface.php
11 months ago
AesStreamInterfaceV2.php
11 months ago
DecryptionTrait.php
11 months ago
DecryptionTraitV2.php
11 months ago
EncryptionTrait.php
11 months ago
EncryptionTraitV2.php
11 months ago
KmsMaterialsProvider.php
11 months ago
KmsMaterialsProviderV2.php
11 months ago
MaterialsProvider.php
11 months ago
MaterialsProviderInterface.php
11 months ago
MaterialsProviderInterfaceV2.php
11 months ago
MaterialsProviderV2.php
11 months ago
MetadataEnvelope.php
11 months ago
MetadataStrategyInterface.php
11 months ago
AesGcmDecryptingStream.php
108 lines
| 1 | <?php |
| 2 | namespace Aws\Crypto; |
| 3 | |
| 4 | use Aws\Exception\CryptoException; |
| 5 | use GuzzleHttp\Psr7; |
| 6 | use GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 7 | use Psr\Http\Message\StreamInterface; |
| 8 | use Aws\Crypto\Polyfill\AesGcm; |
| 9 | use Aws\Crypto\Polyfill\Key; |
| 10 | |
| 11 | /** |
| 12 | * @internal Represents a stream of data to be gcm decrypted. |
| 13 | */ |
| 14 | class AesGcmDecryptingStream implements AesStreamInterface |
| 15 | { |
| 16 | use StreamDecoratorTrait; |
| 17 | |
| 18 | private $aad; |
| 19 | |
| 20 | private $initializationVector; |
| 21 | |
| 22 | private $key; |
| 23 | |
| 24 | private $keySize; |
| 25 | |
| 26 | private $cipherText; |
| 27 | |
| 28 | private $tag; |
| 29 | |
| 30 | private $tagLength; |
| 31 | |
| 32 | /** |
| 33 | * @param StreamInterface $cipherText |
| 34 | * @param string $key |
| 35 | * @param string $initializationVector |
| 36 | * @param string $tag |
| 37 | * @param string $aad |
| 38 | * @param int $tagLength |
| 39 | * @param int $keySize |
| 40 | */ |
| 41 | public function __construct( |
| 42 | StreamInterface $cipherText, |
| 43 | $key, |
| 44 | $initializationVector, |
| 45 | $tag, |
| 46 | $aad = '', |
| 47 | $tagLength = 128, |
| 48 | $keySize = 256 |
| 49 | ) { |
| 50 | $this->cipherText = $cipherText; |
| 51 | $this->key = $key; |
| 52 | $this->initializationVector = $initializationVector; |
| 53 | $this->tag = $tag; |
| 54 | $this->aad = $aad; |
| 55 | $this->tagLength = $tagLength; |
| 56 | $this->keySize = $keySize; |
| 57 | } |
| 58 | |
| 59 | public function getOpenSslName() |
| 60 | { |
| 61 | return "aes-{$this->keySize}-gcm"; |
| 62 | } |
| 63 | |
| 64 | public function getAesName() |
| 65 | { |
| 66 | return 'AES/GCM/NoPadding'; |
| 67 | } |
| 68 | |
| 69 | public function getCurrentIv() |
| 70 | { |
| 71 | return $this->initializationVector; |
| 72 | } |
| 73 | |
| 74 | public function createStream() |
| 75 | { |
| 76 | if (version_compare(PHP_VERSION, '7.1', '<')) { |
| 77 | return Psr7\Utils::streamFor(AesGcm::decrypt( |
| 78 | (string) $this->cipherText, |
| 79 | $this->initializationVector, |
| 80 | new Key($this->key), |
| 81 | $this->aad, |
| 82 | $this->tag, |
| 83 | $this->keySize |
| 84 | )); |
| 85 | } else { |
| 86 | $result = \openssl_decrypt( |
| 87 | (string)$this->cipherText, |
| 88 | $this->getOpenSslName(), |
| 89 | $this->key, |
| 90 | OPENSSL_RAW_DATA, |
| 91 | $this->initializationVector, |
| 92 | $this->tag, |
| 93 | $this->aad |
| 94 | ); |
| 95 | if ($result === false) { |
| 96 | throw new CryptoException('The requested object could not be' |
| 97 | . ' decrypted due to an invalid authentication tag.'); |
| 98 | } |
| 99 | return Psr7\Utils::streamFor($result); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public function isWritable(): bool |
| 104 | { |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 |