Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Crypto/AesGcmEncryptingStream.php

raw symbols SCIP
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 * @internal Represents a stream of data to be gcm encrypted.
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 * @var StreamInterface
31 */
32 private $stream;
33
34 /**
35 * Same as non-static 'getAesName' method, allowing calls in a static
36 * context.
37 *
38 * @return string
39 */
40 public static function getStaticAesName()
41 {
42 return 'AES/GCM/NoPadding';
43 }
44
45 /**
46 * @param StreamInterface $plaintext
47 * @param string $key
48 * @param string $initializationVector
49 * @param string $aad
50 * @param int $tagLength
51 * @param int $keySize
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 // unsetting the property forces the first access to go through
69 // __get().
70 unset($this->stream);
71 }
72
73 public function getOpenSslName()
74 {
75 return "aes-{$this->keySize}-gcm";
76 }
77
78 /**
79 * Same as static method and retained for backwards compatibility
80 *
81 * @return string
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 * @return string
109 */
110 public function getTag()
111 {
112 return $this->tag;
113 }
114
115 public function isWritable(): bool
116 {
117 return false;
118 }
119 }