| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7; |
| 5 |
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 6 |
use Psr\Http\Message\StreamInterface; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class AesGcmEncryptingStream implements AesStreamInterface, AesStreamInterfaceV2 |
| 12 |
{ |
| 13 |
use StreamDecoratorTrait; |
| 14 |
|
| 15 |
private $aad; |
| 16 |
|
| 17 |
private $initializationVector; |
| 18 |
|
| 19 |
private $key; |
| 20 |
|
| 21 |
private $keySize; |
| 22 |
|
| 23 |
private $plaintext; |
| 24 |
|
| 25 |
private $tag = ''; |
| 26 |
|
| 27 |
private $tagLength; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $stream; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static function getStaticAesName() |
| 41 |
{ |
| 42 |
return 'AES/GCM/NoPadding'; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( |
| 54 |
StreamInterface $plaintext, |
| 55 |
$key, |
| 56 |
$initializationVector, |
| 57 |
$aad = '', |
| 58 |
$tagLength = 16, |
| 59 |
$keySize = 256 |
| 60 |
) { |
| 61 |
|
| 62 |
$this->plaintext = $plaintext; |
| 63 |
$this->key = $key; |
| 64 |
$this->initializationVector = $initializationVector; |
| 65 |
$this->aad = $aad; |
| 66 |
$this->tagLength = $tagLength; |
| 67 |
$this->keySize = $keySize; |
| 68 |
|
| 69 |
|
| 70 |
unset($this->stream); |
| 71 |
} |
| 72 |
|
| 73 |
public function getOpenSslName() |
| 74 |
{ |
| 75 |
return "aes-{$this->keySize}-gcm"; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getAesName() |
| 84 |
{ |
| 85 |
return self::getStaticAesName(); |
| 86 |
} |
| 87 |
|
| 88 |
public function getCurrentIv() |
| 89 |
{ |
| 90 |
return $this->initializationVector; |
| 91 |
} |
| 92 |
|
| 93 |
public function createStream() |
| 94 |
{ |
| 95 |
return Psr7\Utils::streamFor(\openssl_encrypt( |
| 96 |
(string)$this->plaintext, |
| 97 |
$this->getOpenSslName(), |
| 98 |
$this->key, |
| 99 |
OPENSSL_RAW_DATA, |
| 100 |
$this->initializationVector, |
| 101 |
$this->tag, |
| 102 |
$this->aad, |
| 103 |
$this->tagLength |
| 104 |
)); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function getTag() |
| 111 |
{ |
| 112 |
return $this->tag; |
| 113 |
} |
| 114 |
|
| 115 |
public function isWritable(): bool |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |