@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\Exception; |
6 | 6 |
@@ -12,44 +12,44 @@ |
||
12 | 12 | */ |
13 | 13 | interface ContentEncryptionAlgorithm extends EncryptionAlgorithmParameterValue, HeaderParameters |
14 | 14 | { |
15 | - /** |
|
16 | - * Encrypt plaintext. |
|
17 | - * |
|
18 | - * @param string $plaintext Data to encrypt |
|
19 | - * @param string $key Encryption key |
|
20 | - * @param string $iv Initialization vector |
|
21 | - * @param string $aad Additional authenticated data |
|
22 | - * |
|
23 | - * @return array Tuple of ciphertext and authentication tag |
|
24 | - */ |
|
25 | - public function encrypt(string $plaintext, string $key, string $iv, |
|
26 | - string $aad): array; |
|
15 | + /** |
|
16 | + * Encrypt plaintext. |
|
17 | + * |
|
18 | + * @param string $plaintext Data to encrypt |
|
19 | + * @param string $key Encryption key |
|
20 | + * @param string $iv Initialization vector |
|
21 | + * @param string $aad Additional authenticated data |
|
22 | + * |
|
23 | + * @return array Tuple of ciphertext and authentication tag |
|
24 | + */ |
|
25 | + public function encrypt(string $plaintext, string $key, string $iv, |
|
26 | + string $aad): array; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Decrypt ciphertext. |
|
30 | - * |
|
31 | - * @param string $ciphertext Data to decrypt |
|
32 | - * @param string $key Encryption key |
|
33 | - * @param string $iv Initialization vector |
|
34 | - * @param string $aad Additional authenticated data |
|
35 | - * @param string $auth_tag Authentication tag to compare |
|
36 | - * |
|
37 | - * @return string Plaintext |
|
38 | - */ |
|
39 | - public function decrypt(string $ciphertext, string $key, string $iv, |
|
40 | - string $aad, string $auth_tag): string; |
|
28 | + /** |
|
29 | + * Decrypt ciphertext. |
|
30 | + * |
|
31 | + * @param string $ciphertext Data to decrypt |
|
32 | + * @param string $key Encryption key |
|
33 | + * @param string $iv Initialization vector |
|
34 | + * @param string $aad Additional authenticated data |
|
35 | + * @param string $auth_tag Authentication tag to compare |
|
36 | + * |
|
37 | + * @return string Plaintext |
|
38 | + */ |
|
39 | + public function decrypt(string $ciphertext, string $key, string $iv, |
|
40 | + string $aad, string $auth_tag): string; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Get the required key size in bytes. |
|
44 | - * |
|
45 | - * @return int |
|
46 | - */ |
|
47 | - public function keySize(): int; |
|
42 | + /** |
|
43 | + * Get the required key size in bytes. |
|
44 | + * |
|
45 | + * @return int |
|
46 | + */ |
|
47 | + public function keySize(): int; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Get the required IV size in bytes. |
|
51 | - * |
|
52 | - * @return int |
|
53 | - */ |
|
54 | - public function ivSize(): int; |
|
49 | + /** |
|
50 | + * Get the required IV size in bytes. |
|
51 | + * |
|
52 | + * @return int |
|
53 | + */ |
|
54 | + public function ivSize(): int; |
|
55 | 55 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE; |
6 | 6 |
@@ -22,199 +22,199 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class PBES2Algorithm extends KeyManagementAlgorithm |
24 | 24 | { |
25 | - use RandomCEK; |
|
26 | - |
|
27 | - /** |
|
28 | - * Mapping from algorithm name to class name. |
|
29 | - * |
|
30 | - * @internal |
|
31 | - * |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - const MAP_ALGO_TO_CLASS = [ |
|
35 | - JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
36 | - JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
37 | - JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
|
38 | - ]; |
|
39 | - |
|
40 | - /** |
|
41 | - * Password. |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $_password; |
|
46 | - |
|
47 | - /** |
|
48 | - * Salt input. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $_saltInput; |
|
53 | - |
|
54 | - /** |
|
55 | - * Iteration count. |
|
56 | - * |
|
57 | - * @var int |
|
58 | - */ |
|
59 | - protected $_count; |
|
60 | - |
|
61 | - /** |
|
62 | - * Derived key. |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - private $_derivedKey; |
|
67 | - |
|
68 | - /** |
|
69 | - * Constructor. |
|
70 | - * |
|
71 | - * @param string $password Password |
|
72 | - * @param string $salt_input Salt input |
|
73 | - * @param int $count Iteration count |
|
74 | - */ |
|
75 | - public function __construct(string $password, string $salt_input, int $count) |
|
76 | - { |
|
77 | - $this->_password = $password; |
|
78 | - $this->_saltInput = $salt_input; |
|
79 | - $this->_count = $count; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Initialize from JWK. |
|
84 | - * |
|
85 | - * @param JWK $jwk |
|
86 | - * @param Header $header |
|
87 | - * |
|
88 | - * @throws \UnexpectedValueException |
|
89 | - * |
|
90 | - * @return self |
|
91 | - */ |
|
92 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
93 | - { |
|
94 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
95 | - if (!$header->hasPBES2SaltInput()) { |
|
96 | - throw new \UnexpectedValueException('No salt input.'); |
|
97 | - } |
|
98 | - $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
99 | - if (!$header->hasPBES2Count()) { |
|
100 | - throw new \UnexpectedValueException('No iteration count.'); |
|
101 | - } |
|
102 | - $count = $header->PBES2Count()->value(); |
|
103 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
104 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
105 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
106 | - } |
|
107 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
108 | - return new $cls($jwk->key(), $salt_input, $count); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Initialize from a password with random salt and default iteration count. |
|
113 | - * |
|
114 | - * @param string $password Password |
|
115 | - * @param int $count Optional user defined iteration count |
|
116 | - * @param int $salt_bytes Optional user defined salt length |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public static function fromPassword(string $password, int $count = 64000, |
|
121 | - int $salt_bytes = 8): self |
|
122 | - { |
|
123 | - $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
124 | - return new static($password, $salt_input, $count); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Get salt input. |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public function saltInput(): string |
|
133 | - { |
|
134 | - return $this->_saltInput; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Get computed salt. |
|
139 | - * |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function salt(): string |
|
143 | - { |
|
144 | - return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
145 | - AlgorithmParameter::fromAlgorithm($this)); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Get iteration count. |
|
150 | - * |
|
151 | - * @return int |
|
152 | - */ |
|
153 | - public function iterationCount(): int |
|
154 | - { |
|
155 | - return $this->_count; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * {@inheritdoc} |
|
160 | - */ |
|
161 | - public function headerParameters(): array |
|
162 | - { |
|
163 | - return array_merge(parent::headerParameters(), |
|
164 | - [AlgorithmParameter::fromAlgorithm($this), |
|
165 | - PBES2SaltInputParameter::fromString($this->_saltInput), |
|
166 | - new PBES2CountParameter($this->_count), ]); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get hash algorithm for hash_pbkdf2. |
|
171 | - * |
|
172 | - * @return string |
|
173 | - */ |
|
174 | - abstract protected function _hashAlgo(): string; |
|
175 | - |
|
176 | - /** |
|
177 | - * Get derived key length. |
|
178 | - * |
|
179 | - * @return int |
|
180 | - */ |
|
181 | - abstract protected function _keyLength(): int; |
|
182 | - |
|
183 | - /** |
|
184 | - * Get key wrapping algoritym. |
|
185 | - * |
|
186 | - * @return AESKeyWrapAlgorithm |
|
187 | - */ |
|
188 | - abstract protected function _kwAlgo(): AESKeyWrapAlgorithm; |
|
189 | - |
|
190 | - /** |
|
191 | - * Get derived key. |
|
192 | - * |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - protected function _derivedKey(): string |
|
196 | - { |
|
197 | - if (!isset($this->_derivedKey)) { |
|
198 | - $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
199 | - $this->_password, $this->salt(), $this->_count, |
|
200 | - $this->_keyLength(), true); |
|
201 | - } |
|
202 | - return $this->_derivedKey; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * {@inheritdoc} |
|
207 | - */ |
|
208 | - protected function _encryptKey(string $key, Header &$header): string |
|
209 | - { |
|
210 | - return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * {@inheritdoc} |
|
215 | - */ |
|
216 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
217 | - { |
|
218 | - return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
219 | - } |
|
25 | + use RandomCEK; |
|
26 | + |
|
27 | + /** |
|
28 | + * Mapping from algorithm name to class name. |
|
29 | + * |
|
30 | + * @internal |
|
31 | + * |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + const MAP_ALGO_TO_CLASS = [ |
|
35 | + JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
36 | + JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
37 | + JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
|
38 | + ]; |
|
39 | + |
|
40 | + /** |
|
41 | + * Password. |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $_password; |
|
46 | + |
|
47 | + /** |
|
48 | + * Salt input. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $_saltInput; |
|
53 | + |
|
54 | + /** |
|
55 | + * Iteration count. |
|
56 | + * |
|
57 | + * @var int |
|
58 | + */ |
|
59 | + protected $_count; |
|
60 | + |
|
61 | + /** |
|
62 | + * Derived key. |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + private $_derivedKey; |
|
67 | + |
|
68 | + /** |
|
69 | + * Constructor. |
|
70 | + * |
|
71 | + * @param string $password Password |
|
72 | + * @param string $salt_input Salt input |
|
73 | + * @param int $count Iteration count |
|
74 | + */ |
|
75 | + public function __construct(string $password, string $salt_input, int $count) |
|
76 | + { |
|
77 | + $this->_password = $password; |
|
78 | + $this->_saltInput = $salt_input; |
|
79 | + $this->_count = $count; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Initialize from JWK. |
|
84 | + * |
|
85 | + * @param JWK $jwk |
|
86 | + * @param Header $header |
|
87 | + * |
|
88 | + * @throws \UnexpectedValueException |
|
89 | + * |
|
90 | + * @return self |
|
91 | + */ |
|
92 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
93 | + { |
|
94 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
95 | + if (!$header->hasPBES2SaltInput()) { |
|
96 | + throw new \UnexpectedValueException('No salt input.'); |
|
97 | + } |
|
98 | + $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
99 | + if (!$header->hasPBES2Count()) { |
|
100 | + throw new \UnexpectedValueException('No iteration count.'); |
|
101 | + } |
|
102 | + $count = $header->PBES2Count()->value(); |
|
103 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
104 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
105 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
106 | + } |
|
107 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
108 | + return new $cls($jwk->key(), $salt_input, $count); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Initialize from a password with random salt and default iteration count. |
|
113 | + * |
|
114 | + * @param string $password Password |
|
115 | + * @param int $count Optional user defined iteration count |
|
116 | + * @param int $salt_bytes Optional user defined salt length |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public static function fromPassword(string $password, int $count = 64000, |
|
121 | + int $salt_bytes = 8): self |
|
122 | + { |
|
123 | + $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
124 | + return new static($password, $salt_input, $count); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Get salt input. |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public function saltInput(): string |
|
133 | + { |
|
134 | + return $this->_saltInput; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Get computed salt. |
|
139 | + * |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function salt(): string |
|
143 | + { |
|
144 | + return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
145 | + AlgorithmParameter::fromAlgorithm($this)); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Get iteration count. |
|
150 | + * |
|
151 | + * @return int |
|
152 | + */ |
|
153 | + public function iterationCount(): int |
|
154 | + { |
|
155 | + return $this->_count; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * {@inheritdoc} |
|
160 | + */ |
|
161 | + public function headerParameters(): array |
|
162 | + { |
|
163 | + return array_merge(parent::headerParameters(), |
|
164 | + [AlgorithmParameter::fromAlgorithm($this), |
|
165 | + PBES2SaltInputParameter::fromString($this->_saltInput), |
|
166 | + new PBES2CountParameter($this->_count), ]); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get hash algorithm for hash_pbkdf2. |
|
171 | + * |
|
172 | + * @return string |
|
173 | + */ |
|
174 | + abstract protected function _hashAlgo(): string; |
|
175 | + |
|
176 | + /** |
|
177 | + * Get derived key length. |
|
178 | + * |
|
179 | + * @return int |
|
180 | + */ |
|
181 | + abstract protected function _keyLength(): int; |
|
182 | + |
|
183 | + /** |
|
184 | + * Get key wrapping algoritym. |
|
185 | + * |
|
186 | + * @return AESKeyWrapAlgorithm |
|
187 | + */ |
|
188 | + abstract protected function _kwAlgo(): AESKeyWrapAlgorithm; |
|
189 | + |
|
190 | + /** |
|
191 | + * Get derived key. |
|
192 | + * |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + protected function _derivedKey(): string |
|
196 | + { |
|
197 | + if (!isset($this->_derivedKey)) { |
|
198 | + $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
199 | + $this->_password, $this->salt(), $this->_count, |
|
200 | + $this->_keyLength(), true); |
|
201 | + } |
|
202 | + return $this->_derivedKey; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * {@inheritdoc} |
|
207 | + */ |
|
208 | + protected function _encryptKey(string $key, Header &$header): string |
|
209 | + { |
|
210 | + return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * {@inheritdoc} |
|
215 | + */ |
|
216 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
217 | + { |
|
218 | + return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
219 | + } |
|
220 | 220 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -15,35 +15,35 @@ |
||
15 | 15 | */ |
16 | 16 | class PBES2HS256A128KWAlgorithm extends PBES2Algorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - public function algorithmParamValue(): string |
|
22 | - { |
|
23 | - return JWA::ALGO_PBES2_HS256_A128KW; |
|
24 | - } |
|
18 | + /** |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + public function algorithmParamValue(): string |
|
22 | + { |
|
23 | + return JWA::ALGO_PBES2_HS256_A128KW; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - protected function _hashAlgo(): string |
|
30 | - { |
|
31 | - return 'sha256'; |
|
32 | - } |
|
26 | + /** |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + protected function _hashAlgo(): string |
|
30 | + { |
|
31 | + return 'sha256'; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _keyLength(): int |
|
38 | - { |
|
39 | - return 16; |
|
40 | - } |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _keyLength(): int |
|
38 | + { |
|
39 | + return 16; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - protected function _kwAlgo(): AESKeyWrapAlgorithm |
|
46 | - { |
|
47 | - return new AESKW128(); |
|
48 | - } |
|
42 | + /** |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + protected function _kwAlgo(): AESKeyWrapAlgorithm |
|
46 | + { |
|
47 | + return new AESKW128(); |
|
48 | + } |
|
49 | 49 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -9,21 +9,21 @@ |
||
9 | 9 | */ |
10 | 10 | trait RandomCEK |
11 | 11 | { |
12 | - /** |
|
13 | - * Generate a random content encryption key. |
|
14 | - * |
|
15 | - * @param int $length Key length in bytes |
|
16 | - * |
|
17 | - * @throws \RuntimeException |
|
18 | - * |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - public function cekForEncryption(int $length): string |
|
22 | - { |
|
23 | - $ret = openssl_random_pseudo_bytes($length); |
|
24 | - if (false === $ret) { |
|
25 | - throw new \RuntimeException('openssl_random_pseudo_bytes() failed.'); |
|
26 | - } |
|
27 | - return $ret; |
|
28 | - } |
|
12 | + /** |
|
13 | + * Generate a random content encryption key. |
|
14 | + * |
|
15 | + * @param int $length Key length in bytes |
|
16 | + * |
|
17 | + * @throws \RuntimeException |
|
18 | + * |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + public function cekForEncryption(int $length): string |
|
22 | + { |
|
23 | + $ret = openssl_random_pseudo_bytes($length); |
|
24 | + if (false === $ret) { |
|
25 | + throw new \RuntimeException('openssl_random_pseudo_bytes() failed.'); |
|
26 | + } |
|
27 | + return $ret; |
|
28 | + } |
|
29 | 29 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm\Feature; |
6 | 6 |
@@ -15,27 +15,27 @@ |
||
15 | 15 | */ |
16 | 16 | class A128KWAlgorithm extends AESKWAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - public function algorithmParamValue(): string |
|
22 | - { |
|
23 | - return JWA::ALGO_A128KW; |
|
24 | - } |
|
18 | + /** |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + public function algorithmParamValue(): string |
|
22 | + { |
|
23 | + return JWA::ALGO_A128KW; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - protected function _kekSize(): int |
|
30 | - { |
|
31 | - return 16; |
|
32 | - } |
|
26 | + /** |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + protected function _kekSize(): int |
|
30 | + { |
|
31 | + return 16; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
38 | - { |
|
39 | - return new AESKW128(); |
|
40 | - } |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
38 | + { |
|
39 | + return new AESKW128(); |
|
40 | + } |
|
41 | 41 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -20,121 +20,121 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class AESKWAlgorithm extends KeyManagementAlgorithm |
22 | 22 | { |
23 | - use RandomCEK; |
|
23 | + use RandomCEK; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Mapping from algorithm name to class name. |
|
27 | - * |
|
28 | - * @internal |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - const MAP_ALGO_TO_CLASS = [ |
|
33 | - JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
34 | - JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
35 | - JWA::ALGO_A256KW => A256KWAlgorithm::class, |
|
36 | - ]; |
|
25 | + /** |
|
26 | + * Mapping from algorithm name to class name. |
|
27 | + * |
|
28 | + * @internal |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + const MAP_ALGO_TO_CLASS = [ |
|
33 | + JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
34 | + JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
35 | + JWA::ALGO_A256KW => A256KWAlgorithm::class, |
|
36 | + ]; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Key encryption key. |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $_kek; |
|
38 | + /** |
|
39 | + * Key encryption key. |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $_kek; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Key wrapping algorithm. |
|
47 | - * |
|
48 | - * Lazily initialized. |
|
49 | - * |
|
50 | - * @var null|AESKeyWrapAlgorithm |
|
51 | - */ |
|
52 | - protected $_kw; |
|
45 | + /** |
|
46 | + * Key wrapping algorithm. |
|
47 | + * |
|
48 | + * Lazily initialized. |
|
49 | + * |
|
50 | + * @var null|AESKeyWrapAlgorithm |
|
51 | + */ |
|
52 | + protected $_kw; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Constructor. |
|
56 | - * |
|
57 | - * @param string $kek Key encryption key |
|
58 | - */ |
|
59 | - public function __construct(string $kek) |
|
60 | - { |
|
61 | - if (strlen($kek) !== $this->_kekSize()) { |
|
62 | - throw new \LengthException( |
|
63 | - 'Key encryption key must be ' . $this->_kekSize() . ' bytes.'); |
|
64 | - } |
|
65 | - $this->_kek = $kek; |
|
66 | - } |
|
54 | + /** |
|
55 | + * Constructor. |
|
56 | + * |
|
57 | + * @param string $kek Key encryption key |
|
58 | + */ |
|
59 | + public function __construct(string $kek) |
|
60 | + { |
|
61 | + if (strlen($kek) !== $this->_kekSize()) { |
|
62 | + throw new \LengthException( |
|
63 | + 'Key encryption key must be ' . $this->_kekSize() . ' bytes.'); |
|
64 | + } |
|
65 | + $this->_kek = $kek; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Initialize from JWK. |
|
70 | - * |
|
71 | - * @param JWK $jwk |
|
72 | - * @param Header $header |
|
73 | - * |
|
74 | - * @throws \UnexpectedValueException |
|
75 | - * |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
79 | - { |
|
80 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
81 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
82 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
83 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
84 | - } |
|
85 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
86 | - return new $cls($jwk->key()); |
|
87 | - } |
|
68 | + /** |
|
69 | + * Initialize from JWK. |
|
70 | + * |
|
71 | + * @param JWK $jwk |
|
72 | + * @param Header $header |
|
73 | + * |
|
74 | + * @throws \UnexpectedValueException |
|
75 | + * |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
79 | + { |
|
80 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
81 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
82 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
83 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
84 | + } |
|
85 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
86 | + return new $cls($jwk->key()); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * {@inheritdoc} |
|
91 | - */ |
|
92 | - public function headerParameters(): array |
|
93 | - { |
|
94 | - return array_merge(parent::headerParameters(), |
|
95 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
96 | - } |
|
89 | + /** |
|
90 | + * {@inheritdoc} |
|
91 | + */ |
|
92 | + public function headerParameters(): array |
|
93 | + { |
|
94 | + return array_merge(parent::headerParameters(), |
|
95 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Get the size of the key encryption key in bytes. |
|
100 | - * |
|
101 | - * @return int |
|
102 | - */ |
|
103 | - abstract protected function _kekSize(): int; |
|
98 | + /** |
|
99 | + * Get the size of the key encryption key in bytes. |
|
100 | + * |
|
101 | + * @return int |
|
102 | + */ |
|
103 | + abstract protected function _kekSize(): int; |
|
104 | 104 | |
105 | - /** |
|
106 | - * Get key wrapping algorithm instance. |
|
107 | - * |
|
108 | - * @return AESKeyWrapAlgorithm |
|
109 | - */ |
|
110 | - abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm; |
|
105 | + /** |
|
106 | + * Get key wrapping algorithm instance. |
|
107 | + * |
|
108 | + * @return AESKeyWrapAlgorithm |
|
109 | + */ |
|
110 | + abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm; |
|
111 | 111 | |
112 | - /** |
|
113 | - * Get key wrapping algorithm. |
|
114 | - * |
|
115 | - * @return AESKeyWrapAlgorithm |
|
116 | - */ |
|
117 | - protected function _kw(): AESKeyWrapAlgorithm |
|
118 | - { |
|
119 | - if (!isset($this->_kw)) { |
|
120 | - $this->_kw = $this->_AESKWAlgo(); |
|
121 | - } |
|
122 | - return $this->_kw; |
|
123 | - } |
|
112 | + /** |
|
113 | + * Get key wrapping algorithm. |
|
114 | + * |
|
115 | + * @return AESKeyWrapAlgorithm |
|
116 | + */ |
|
117 | + protected function _kw(): AESKeyWrapAlgorithm |
|
118 | + { |
|
119 | + if (!isset($this->_kw)) { |
|
120 | + $this->_kw = $this->_AESKWAlgo(); |
|
121 | + } |
|
122 | + return $this->_kw; |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * {@inheritdoc} |
|
127 | - */ |
|
128 | - protected function _encryptKey(string $key, Header &$header): string |
|
129 | - { |
|
130 | - return $this->_kw()->wrap($key, $this->_kek); |
|
131 | - } |
|
125 | + /** |
|
126 | + * {@inheritdoc} |
|
127 | + */ |
|
128 | + protected function _encryptKey(string $key, Header &$header): string |
|
129 | + { |
|
130 | + return $this->_kw()->wrap($key, $this->_kek); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * {@inheritdoc} |
|
135 | - */ |
|
136 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
137 | - { |
|
138 | - return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
139 | - } |
|
133 | + /** |
|
134 | + * {@inheritdoc} |
|
135 | + */ |
|
136 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
137 | + { |
|
138 | + return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
139 | + } |
|
140 | 140 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -15,27 +15,27 @@ |
||
15 | 15 | */ |
16 | 16 | class A192KWAlgorithm extends AESKWAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * {@inheritdoc} |
|
20 | - */ |
|
21 | - public function algorithmParamValue(): string |
|
22 | - { |
|
23 | - return JWA::ALGO_A192KW; |
|
24 | - } |
|
18 | + /** |
|
19 | + * {@inheritdoc} |
|
20 | + */ |
|
21 | + public function algorithmParamValue(): string |
|
22 | + { |
|
23 | + return JWA::ALGO_A192KW; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - protected function _kekSize(): int |
|
30 | - { |
|
31 | - return 24; |
|
32 | - } |
|
26 | + /** |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + protected function _kekSize(): int |
|
30 | + { |
|
31 | + return 24; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
38 | - { |
|
39 | - return new AESKW192(); |
|
40 | - } |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
38 | + { |
|
39 | + return new AESKW192(); |
|
40 | + } |
|
41 | 41 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |
@@ -18,101 +18,101 @@ |
||
18 | 18 | */ |
19 | 19 | class DirectCEKAlgorithm extends KeyManagementAlgorithm |
20 | 20 | { |
21 | - /** |
|
22 | - * Content encryption key. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $_cek; |
|
21 | + /** |
|
22 | + * Content encryption key. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $_cek; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param string $cek Content encryption key |
|
32 | - */ |
|
33 | - public function __construct(string $cek) |
|
34 | - { |
|
35 | - $this->_cek = $cek; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param string $cek Content encryption key |
|
32 | + */ |
|
33 | + public function __construct(string $cek) |
|
34 | + { |
|
35 | + $this->_cek = $cek; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Initialize from JWK. |
|
40 | - * |
|
41 | - * @param JWK $jwk |
|
42 | - * @param Header $header |
|
43 | - * |
|
44 | - * @throws \UnexpectedValueException |
|
45 | - * |
|
46 | - * @return self |
|
47 | - */ |
|
48 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
49 | - { |
|
50 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
51 | - $alg = JWA::deriveAlgorithmName($header); |
|
52 | - if (JWA::ALGO_DIR !== $alg) { |
|
53 | - throw new \UnexpectedValueException("Invalid algorithm '{$alg}'."); |
|
54 | - } |
|
55 | - return new self($jwk->key()); |
|
56 | - } |
|
38 | + /** |
|
39 | + * Initialize from JWK. |
|
40 | + * |
|
41 | + * @param JWK $jwk |
|
42 | + * @param Header $header |
|
43 | + * |
|
44 | + * @throws \UnexpectedValueException |
|
45 | + * |
|
46 | + * @return self |
|
47 | + */ |
|
48 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
49 | + { |
|
50 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
51 | + $alg = JWA::deriveAlgorithmName($header); |
|
52 | + if (JWA::ALGO_DIR !== $alg) { |
|
53 | + throw new \UnexpectedValueException("Invalid algorithm '{$alg}'."); |
|
54 | + } |
|
55 | + return new self($jwk->key()); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get content encryption key. |
|
60 | - * |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function cek(): string |
|
64 | - { |
|
65 | - return $this->_cek; |
|
66 | - } |
|
58 | + /** |
|
59 | + * Get content encryption key. |
|
60 | + * |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function cek(): string |
|
64 | + { |
|
65 | + return $this->_cek; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - public function cekForEncryption(int $length): string |
|
72 | - { |
|
73 | - if (strlen($this->_cek) !== $length) { |
|
74 | - throw new \UnexpectedValueException('Invalid key length.'); |
|
75 | - } |
|
76 | - return $this->_cek; |
|
77 | - } |
|
68 | + /** |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + public function cekForEncryption(int $length): string |
|
72 | + { |
|
73 | + if (strlen($this->_cek) !== $length) { |
|
74 | + throw new \UnexpectedValueException('Invalid key length.'); |
|
75 | + } |
|
76 | + return $this->_cek; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * {@inheritdoc} |
|
81 | - */ |
|
82 | - public function algorithmParamValue(): string |
|
83 | - { |
|
84 | - return JWA::ALGO_DIR; |
|
85 | - } |
|
79 | + /** |
|
80 | + * {@inheritdoc} |
|
81 | + */ |
|
82 | + public function algorithmParamValue(): string |
|
83 | + { |
|
84 | + return JWA::ALGO_DIR; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * {@inheritdoc} |
|
89 | - */ |
|
90 | - public function headerParameters(): array |
|
91 | - { |
|
92 | - return array_merge(parent::headerParameters(), |
|
93 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
94 | - } |
|
87 | + /** |
|
88 | + * {@inheritdoc} |
|
89 | + */ |
|
90 | + public function headerParameters(): array |
|
91 | + { |
|
92 | + return array_merge(parent::headerParameters(), |
|
93 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - protected function _encryptKey(string $key, Header &$header): string |
|
100 | - { |
|
101 | - if ($key !== $this->_cek) { |
|
102 | - throw new \LogicException("Content encryption key doesn't match."); |
|
103 | - } |
|
104 | - return ''; |
|
105 | - } |
|
96 | + /** |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + protected function _encryptKey(string $key, Header &$header): string |
|
100 | + { |
|
101 | + if ($key !== $this->_cek) { |
|
102 | + throw new \LogicException("Content encryption key doesn't match."); |
|
103 | + } |
|
104 | + return ''; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * {@inheritdoc} |
|
109 | - */ |
|
110 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
111 | - { |
|
112 | - if ('' !== $ciphertext) { |
|
113 | - throw new \UnexpectedValueException( |
|
114 | - 'Encrypted key must be an empty octet sequence.'); |
|
115 | - } |
|
116 | - return $this->_cek; |
|
117 | - } |
|
107 | + /** |
|
108 | + * {@inheritdoc} |
|
109 | + */ |
|
110 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
111 | + { |
|
112 | + if ('' !== $ciphertext) { |
|
113 | + throw new \UnexpectedValueException( |
|
114 | + 'Encrypted key must be an empty octet sequence.'); |
|
115 | + } |
|
116 | + return $this->_cek; |
|
117 | + } |
|
118 | 118 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\JWX\JWE\KeyAlgorithm; |
6 | 6 |