@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | // let the ciphertext be: |
149 | 149 | // C[0] | C[1] = ENC(K, A | P[1]). |
150 | 150 | if (8 == strlen($key)) { |
151 | - return $this->_encrypt($kek, $aiv . $key); |
|
151 | + return $this->_encrypt($kek, $aiv.$key); |
|
152 | 152 | } |
153 | 153 | // build plaintext blocks and apply normal wrapping with AIV as an |
154 | 154 | // initial value |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | // For i = 1 to n |
231 | 231 | for ($i = 1; $i <= $n; ++$i) { |
232 | 232 | // B = AES(K, A | R[i]) |
233 | - $B = $this->_encrypt($kek, $A . $R[$i]); |
|
233 | + $B = $this->_encrypt($kek, $A.$R[$i]); |
|
234 | 234 | // A = MSB(64, B) ^ t where t = (n*j)+i |
235 | 235 | $t = $n * $j + $i; |
236 | 236 | $A = $this->_msb64($B) ^ $this->_uint64($t); |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | // if key consists of only one block, recover AIV and padded key as: |
265 | 265 | // A | P[1] = DEC(K, C[0] | C[1]) |
266 | 266 | if ($n == 1) { |
267 | - $P = str_split($this->_decrypt($kek, $C[0] . $C[1]), 8); |
|
267 | + $P = str_split($this->_decrypt($kek, $C[0].$C[1]), 8); |
|
268 | 268 | $A = $P[0]; |
269 | 269 | unset($P[0]); |
270 | 270 | } else { |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | for ($i = $n; $i >= 1; --$i) { |
309 | 309 | // B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i |
310 | 310 | $t = $n * $j + $i; |
311 | - $B = $this->_decrypt($kek, ($A ^ $this->_uint64($t)) . $R[$i]); |
|
311 | + $B = $this->_decrypt($kek, ($A ^ $this->_uint64($t)).$R[$i]); |
|
312 | 312 | // A = MSB(64, B) |
313 | 313 | $A = $this->_msb64($B); |
314 | 314 | // R[i] = LSB(64, B) |
@@ -333,7 +333,7 @@ discard block |
||
333 | 333 | } |
334 | 334 | // compute AIV |
335 | 335 | $mli = pack("N", $len); |
336 | - $aiv = self::AIV_HI . $mli; |
|
336 | + $aiv = self::AIV_HI.$mli; |
|
337 | 337 | return [$key, $aiv]; |
338 | 338 | } |
339 | 339 | |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
398 | 398 | if (false === $str) { |
399 | 399 | throw new \RuntimeException( |
400 | - "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
400 | + "openssl_encrypt() failed: ".$this->_getLastOpenSSLError()); |
|
401 | 401 | } |
402 | 402 | return $str; |
403 | 403 | } |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
417 | 417 | if (false === $str) { |
418 | 418 | throw new \RuntimeException( |
419 | - "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
419 | + "openssl_decrypt() failed: ".$this->_getLastOpenSSLError()); |
|
420 | 420 | } |
421 | 421 | return $str; |
422 | 422 | } |
@@ -468,7 +468,7 @@ discard block |
||
468 | 468 | { |
469 | 469 | // truncate on 32 bit hosts |
470 | 470 | if (PHP_INT_SIZE < 8) { |
471 | - return "\0\0\0\0" . pack("N", $num); |
|
471 | + return "\0\0\0\0".pack("N", $num); |
|
472 | 472 | } |
473 | 473 | return pack("J", $num); |
474 | 474 | } |
@@ -11,467 +11,467 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class Algorithm implements AESKeyWrapAlgorithm |
13 | 13 | { |
14 | - /** |
|
15 | - * Default initial value. |
|
16 | - * |
|
17 | - * @link https://tools.ietf.org/html/rfc3394#section-2.2.3.1 |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - const DEFAULT_IV = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6"; |
|
14 | + /** |
|
15 | + * Default initial value. |
|
16 | + * |
|
17 | + * @link https://tools.ietf.org/html/rfc3394#section-2.2.3.1 |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + const DEFAULT_IV = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6"; |
|
21 | 21 | |
22 | - /** |
|
23 | - * High order bytes of the alternative initial value for padding. |
|
24 | - * |
|
25 | - * @link https://tools.ietf.org/html/rfc5649#section-3 |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - const AIV_HI = "\xA6\x59\x59\xA6"; |
|
22 | + /** |
|
23 | + * High order bytes of the alternative initial value for padding. |
|
24 | + * |
|
25 | + * @link https://tools.ietf.org/html/rfc5649#section-3 |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + const AIV_HI = "\xA6\x59\x59\xA6"; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Initial value. |
|
32 | - * |
|
33 | - * @var string $_iv |
|
34 | - */ |
|
35 | - protected $_iv; |
|
30 | + /** |
|
31 | + * Initial value. |
|
32 | + * |
|
33 | + * @var string $_iv |
|
34 | + */ |
|
35 | + protected $_iv; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get OpenSSL cipher method. |
|
39 | - * |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - abstract protected function _cipherMethod(): string; |
|
37 | + /** |
|
38 | + * Get OpenSSL cipher method. |
|
39 | + * |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + abstract protected function _cipherMethod(): string; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get key encryption key size. |
|
46 | - * |
|
47 | - * @return int |
|
48 | - */ |
|
49 | - abstract protected function _keySize(): int; |
|
44 | + /** |
|
45 | + * Get key encryption key size. |
|
46 | + * |
|
47 | + * @return int |
|
48 | + */ |
|
49 | + abstract protected function _keySize(): int; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Constructor. |
|
53 | - * |
|
54 | - * @param string $iv Initial value |
|
55 | - */ |
|
56 | - public function __construct(string $iv = self::DEFAULT_IV) |
|
57 | - { |
|
58 | - if (strlen($iv) != 8) { |
|
59 | - throw new \UnexpectedValueException("IV size must be 64 bits."); |
|
60 | - } |
|
61 | - $this->_iv = $iv; |
|
62 | - } |
|
51 | + /** |
|
52 | + * Constructor. |
|
53 | + * |
|
54 | + * @param string $iv Initial value |
|
55 | + */ |
|
56 | + public function __construct(string $iv = self::DEFAULT_IV) |
|
57 | + { |
|
58 | + if (strlen($iv) != 8) { |
|
59 | + throw new \UnexpectedValueException("IV size must be 64 bits."); |
|
60 | + } |
|
61 | + $this->_iv = $iv; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Wrap a key using given key encryption key. |
|
66 | - * |
|
67 | - * Key length must be at least 64 bits (8 octets) and a multiple |
|
68 | - * of 64 bits (8 octets). |
|
69 | - * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
|
70 | - * |
|
71 | - * Key encryption key must have a size of underlying AES algorithm, |
|
72 | - * ie. 128, 196 or 256 bits. |
|
73 | - * |
|
74 | - * @param string $key Key to wrap |
|
75 | - * @param string $kek Key encryption key |
|
76 | - * @throws \UnexpectedValueException If the key length is invalid |
|
77 | - * @return string Ciphertext |
|
78 | - */ |
|
79 | - public function wrap(string $key, string $kek): string |
|
80 | - { |
|
81 | - $key_len = strlen($key); |
|
82 | - // rfc3394 dictates n to be at least 2 |
|
83 | - if ($key_len < 16) { |
|
84 | - throw new \UnexpectedValueException( |
|
85 | - "Key length must be at least 16 octets."); |
|
86 | - } |
|
87 | - if (0 !== $key_len % 8) { |
|
88 | - throw new \UnexpectedValueException( |
|
89 | - "Key length must be a multiple of 64 bits."); |
|
90 | - } |
|
91 | - $this->_checkKEKSize($kek); |
|
92 | - // P = plaintext as 64 bit blocks |
|
93 | - $P = []; |
|
94 | - $i = 1; |
|
95 | - foreach (str_split($key, 8) as $val) { |
|
96 | - $P[$i++] = $val; |
|
97 | - } |
|
98 | - $C = $this->_wrapBlocks($P, $kek, $this->_iv); |
|
99 | - return implode("", $C); |
|
100 | - } |
|
64 | + /** |
|
65 | + * Wrap a key using given key encryption key. |
|
66 | + * |
|
67 | + * Key length must be at least 64 bits (8 octets) and a multiple |
|
68 | + * of 64 bits (8 octets). |
|
69 | + * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
|
70 | + * |
|
71 | + * Key encryption key must have a size of underlying AES algorithm, |
|
72 | + * ie. 128, 196 or 256 bits. |
|
73 | + * |
|
74 | + * @param string $key Key to wrap |
|
75 | + * @param string $kek Key encryption key |
|
76 | + * @throws \UnexpectedValueException If the key length is invalid |
|
77 | + * @return string Ciphertext |
|
78 | + */ |
|
79 | + public function wrap(string $key, string $kek): string |
|
80 | + { |
|
81 | + $key_len = strlen($key); |
|
82 | + // rfc3394 dictates n to be at least 2 |
|
83 | + if ($key_len < 16) { |
|
84 | + throw new \UnexpectedValueException( |
|
85 | + "Key length must be at least 16 octets."); |
|
86 | + } |
|
87 | + if (0 !== $key_len % 8) { |
|
88 | + throw new \UnexpectedValueException( |
|
89 | + "Key length must be a multiple of 64 bits."); |
|
90 | + } |
|
91 | + $this->_checkKEKSize($kek); |
|
92 | + // P = plaintext as 64 bit blocks |
|
93 | + $P = []; |
|
94 | + $i = 1; |
|
95 | + foreach (str_split($key, 8) as $val) { |
|
96 | + $P[$i++] = $val; |
|
97 | + } |
|
98 | + $C = $this->_wrapBlocks($P, $kek, $this->_iv); |
|
99 | + return implode("", $C); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Unwrap a key from a ciphertext using given key encryption key. |
|
104 | - * |
|
105 | - * @param string $ciphertext Ciphertext of the wrapped key |
|
106 | - * @param string $kek Key encryption key |
|
107 | - * @throws \UnexpectedValueException If the ciphertext is invalid |
|
108 | - * @return string Unwrapped key |
|
109 | - */ |
|
110 | - public function unwrap(string $ciphertext, string $kek): string |
|
111 | - { |
|
112 | - if (0 !== strlen($ciphertext) % 8) { |
|
113 | - throw new \UnexpectedValueException( |
|
114 | - "Ciphertext length must be a multiple of 64 bits."); |
|
115 | - } |
|
116 | - $this->_checkKEKSize($kek); |
|
117 | - // C = ciphertext as 64 bit blocks with integrity check value prepended |
|
118 | - $C = str_split($ciphertext, 8); |
|
119 | - list($A, $R) = $this->_unwrapBlocks($C, $kek); |
|
120 | - // check integrity value |
|
121 | - if ($A != $this->_iv) { |
|
122 | - throw new \UnexpectedValueException("Integrity check failed."); |
|
123 | - } |
|
124 | - // output the plaintext |
|
125 | - $P = array_slice($R, 1, null, true); |
|
126 | - return implode("", $P); |
|
127 | - } |
|
102 | + /** |
|
103 | + * Unwrap a key from a ciphertext using given key encryption key. |
|
104 | + * |
|
105 | + * @param string $ciphertext Ciphertext of the wrapped key |
|
106 | + * @param string $kek Key encryption key |
|
107 | + * @throws \UnexpectedValueException If the ciphertext is invalid |
|
108 | + * @return string Unwrapped key |
|
109 | + */ |
|
110 | + public function unwrap(string $ciphertext, string $kek): string |
|
111 | + { |
|
112 | + if (0 !== strlen($ciphertext) % 8) { |
|
113 | + throw new \UnexpectedValueException( |
|
114 | + "Ciphertext length must be a multiple of 64 bits."); |
|
115 | + } |
|
116 | + $this->_checkKEKSize($kek); |
|
117 | + // C = ciphertext as 64 bit blocks with integrity check value prepended |
|
118 | + $C = str_split($ciphertext, 8); |
|
119 | + list($A, $R) = $this->_unwrapBlocks($C, $kek); |
|
120 | + // check integrity value |
|
121 | + if ($A != $this->_iv) { |
|
122 | + throw new \UnexpectedValueException("Integrity check failed."); |
|
123 | + } |
|
124 | + // output the plaintext |
|
125 | + $P = array_slice($R, 1, null, true); |
|
126 | + return implode("", $P); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * Wrap a key of arbitrary length using given key encryption key. |
|
131 | - * |
|
132 | - * This variant of wrapping does not place any restriction on key size. |
|
133 | - * |
|
134 | - * Key encryption key has the same restrictions as with <i>wrap</i> method. |
|
135 | - * |
|
136 | - * @param string $key Key to wrap |
|
137 | - * @param string $kek Key encryption key |
|
138 | - * @throws \UnexpectedValueException If the key length is invalid |
|
139 | - * @return string Ciphertext |
|
140 | - */ |
|
141 | - public function wrapPad(string $key, string $kek): string |
|
142 | - { |
|
143 | - if (!strlen($key)) { |
|
144 | - throw new \UnexpectedValueException( |
|
145 | - "Key must have at least one octet."); |
|
146 | - } |
|
147 | - $this->_checkKEKSize($kek); |
|
148 | - list($key, $aiv) = $this->_padKey($key); |
|
149 | - // If the padded key contains exactly eight octets, |
|
150 | - // let the ciphertext be: |
|
151 | - // C[0] | C[1] = ENC(K, A | P[1]). |
|
152 | - if (8 == strlen($key)) { |
|
153 | - return $this->_encrypt($kek, $aiv . $key); |
|
154 | - } |
|
155 | - // build plaintext blocks and apply normal wrapping with AIV as an |
|
156 | - // initial value |
|
157 | - $P = []; |
|
158 | - $i = 1; |
|
159 | - foreach (str_split($key, 8) as $val) { |
|
160 | - $P[$i++] = $val; |
|
161 | - } |
|
162 | - $C = $this->_wrapBlocks($P, $kek, $aiv); |
|
163 | - return implode("", $C); |
|
164 | - } |
|
129 | + /** |
|
130 | + * Wrap a key of arbitrary length using given key encryption key. |
|
131 | + * |
|
132 | + * This variant of wrapping does not place any restriction on key size. |
|
133 | + * |
|
134 | + * Key encryption key has the same restrictions as with <i>wrap</i> method. |
|
135 | + * |
|
136 | + * @param string $key Key to wrap |
|
137 | + * @param string $kek Key encryption key |
|
138 | + * @throws \UnexpectedValueException If the key length is invalid |
|
139 | + * @return string Ciphertext |
|
140 | + */ |
|
141 | + public function wrapPad(string $key, string $kek): string |
|
142 | + { |
|
143 | + if (!strlen($key)) { |
|
144 | + throw new \UnexpectedValueException( |
|
145 | + "Key must have at least one octet."); |
|
146 | + } |
|
147 | + $this->_checkKEKSize($kek); |
|
148 | + list($key, $aiv) = $this->_padKey($key); |
|
149 | + // If the padded key contains exactly eight octets, |
|
150 | + // let the ciphertext be: |
|
151 | + // C[0] | C[1] = ENC(K, A | P[1]). |
|
152 | + if (8 == strlen($key)) { |
|
153 | + return $this->_encrypt($kek, $aiv . $key); |
|
154 | + } |
|
155 | + // build plaintext blocks and apply normal wrapping with AIV as an |
|
156 | + // initial value |
|
157 | + $P = []; |
|
158 | + $i = 1; |
|
159 | + foreach (str_split($key, 8) as $val) { |
|
160 | + $P[$i++] = $val; |
|
161 | + } |
|
162 | + $C = $this->_wrapBlocks($P, $kek, $aiv); |
|
163 | + return implode("", $C); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Unwrap a key from a padded ciphertext using given key encryption key. |
|
168 | - * |
|
169 | - * This variant of unwrapping must be used if the key was wrapped using |
|
170 | - * <i>wrapPad</i>. |
|
171 | - * |
|
172 | - * @param string $ciphertext Ciphertext of the wrapped and padded key |
|
173 | - * @param string $kek Key encryption key |
|
174 | - * @throws \UnexpectedValueException If the ciphertext is invalid |
|
175 | - * @return string Unwrapped key |
|
176 | - */ |
|
177 | - public function unwrapPad(string $ciphertext, string $kek): string |
|
178 | - { |
|
179 | - if (0 !== strlen($ciphertext) % 8) { |
|
180 | - throw new \UnexpectedValueException( |
|
181 | - "Ciphertext length must be a multiple of 64 bits."); |
|
182 | - } |
|
183 | - $this->_checkKEKSize($kek); |
|
184 | - list($P, $A) = $this->_unwrapPaddedCiphertext($ciphertext, $kek); |
|
185 | - // check message integrity |
|
186 | - $this->_checkPaddedIntegrity($A); |
|
187 | - // verify padding |
|
188 | - $len = $this->_verifyPadding($P, $A); |
|
189 | - // remove padding and return unwrapped key |
|
190 | - return substr(implode("", $P), 0, $len); |
|
191 | - } |
|
166 | + /** |
|
167 | + * Unwrap a key from a padded ciphertext using given key encryption key. |
|
168 | + * |
|
169 | + * This variant of unwrapping must be used if the key was wrapped using |
|
170 | + * <i>wrapPad</i>. |
|
171 | + * |
|
172 | + * @param string $ciphertext Ciphertext of the wrapped and padded key |
|
173 | + * @param string $kek Key encryption key |
|
174 | + * @throws \UnexpectedValueException If the ciphertext is invalid |
|
175 | + * @return string Unwrapped key |
|
176 | + */ |
|
177 | + public function unwrapPad(string $ciphertext, string $kek): string |
|
178 | + { |
|
179 | + if (0 !== strlen($ciphertext) % 8) { |
|
180 | + throw new \UnexpectedValueException( |
|
181 | + "Ciphertext length must be a multiple of 64 bits."); |
|
182 | + } |
|
183 | + $this->_checkKEKSize($kek); |
|
184 | + list($P, $A) = $this->_unwrapPaddedCiphertext($ciphertext, $kek); |
|
185 | + // check message integrity |
|
186 | + $this->_checkPaddedIntegrity($A); |
|
187 | + // verify padding |
|
188 | + $len = $this->_verifyPadding($P, $A); |
|
189 | + // remove padding and return unwrapped key |
|
190 | + return substr(implode("", $P), 0, $len); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Check KEK size. |
|
195 | - * |
|
196 | - * @param string $kek |
|
197 | - * @throws \UnexpectedValueException |
|
198 | - * @return self |
|
199 | - */ |
|
200 | - protected function _checkKEKSize(string $kek): self |
|
201 | - { |
|
202 | - $len = $this->_keySize(); |
|
203 | - if (strlen($kek) != $len) { |
|
204 | - throw new \UnexpectedValueException("KEK size must be $len bytes."); |
|
205 | - } |
|
206 | - return $this; |
|
207 | - } |
|
193 | + /** |
|
194 | + * Check KEK size. |
|
195 | + * |
|
196 | + * @param string $kek |
|
197 | + * @throws \UnexpectedValueException |
|
198 | + * @return self |
|
199 | + */ |
|
200 | + protected function _checkKEKSize(string $kek): self |
|
201 | + { |
|
202 | + $len = $this->_keySize(); |
|
203 | + if (strlen($kek) != $len) { |
|
204 | + throw new \UnexpectedValueException("KEK size must be $len bytes."); |
|
205 | + } |
|
206 | + return $this; |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Apply Key Wrap to data blocks. |
|
211 | - * |
|
212 | - * Uses alternative version of the key wrap procedure described in the RFC. |
|
213 | - * |
|
214 | - * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
|
215 | - * @param string[] $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
|
216 | - * Pn}</code> |
|
217 | - * @param string $kek Key encryption key |
|
218 | - * @param string $iv Initial value |
|
219 | - * @return string[] Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
|
220 | - * Cn}</code> |
|
221 | - */ |
|
222 | - protected function _wrapBlocks(array $P, string $kek, string $iv): array |
|
223 | - { |
|
224 | - $n = count($P); |
|
225 | - // Set A = IV |
|
226 | - $A = $iv; |
|
227 | - // For i = 1 to n |
|
228 | - // R[i] = P[i] |
|
229 | - $R = $P; |
|
230 | - // For j = 0 to 5 |
|
231 | - for ($j = 0; $j <= 5; ++$j) { |
|
232 | - // For i = 1 to n |
|
233 | - for ($i = 1; $i <= $n; ++$i) { |
|
234 | - // B = AES(K, A | R[i]) |
|
235 | - $B = $this->_encrypt($kek, $A . $R[$i]); |
|
236 | - // A = MSB(64, B) ^ t where t = (n*j)+i |
|
237 | - $t = $n * $j + $i; |
|
238 | - $A = $this->_msb64($B) ^ $this->_uint64($t); |
|
239 | - // R[i] = LSB(64, B) |
|
240 | - $R[$i] = $this->_lsb64($B); |
|
241 | - } |
|
242 | - } |
|
243 | - // Set C[0] = A |
|
244 | - $C = [$A]; |
|
245 | - // For i = 1 to n |
|
246 | - for ($i = 1; $i <= $n; ++$i) { |
|
247 | - // C[i] = R[i] |
|
248 | - $C[$i] = $R[$i]; |
|
249 | - } |
|
250 | - return $C; |
|
251 | - } |
|
209 | + /** |
|
210 | + * Apply Key Wrap to data blocks. |
|
211 | + * |
|
212 | + * Uses alternative version of the key wrap procedure described in the RFC. |
|
213 | + * |
|
214 | + * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
|
215 | + * @param string[] $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
|
216 | + * Pn}</code> |
|
217 | + * @param string $kek Key encryption key |
|
218 | + * @param string $iv Initial value |
|
219 | + * @return string[] Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
|
220 | + * Cn}</code> |
|
221 | + */ |
|
222 | + protected function _wrapBlocks(array $P, string $kek, string $iv): array |
|
223 | + { |
|
224 | + $n = count($P); |
|
225 | + // Set A = IV |
|
226 | + $A = $iv; |
|
227 | + // For i = 1 to n |
|
228 | + // R[i] = P[i] |
|
229 | + $R = $P; |
|
230 | + // For j = 0 to 5 |
|
231 | + for ($j = 0; $j <= 5; ++$j) { |
|
232 | + // For i = 1 to n |
|
233 | + for ($i = 1; $i <= $n; ++$i) { |
|
234 | + // B = AES(K, A | R[i]) |
|
235 | + $B = $this->_encrypt($kek, $A . $R[$i]); |
|
236 | + // A = MSB(64, B) ^ t where t = (n*j)+i |
|
237 | + $t = $n * $j + $i; |
|
238 | + $A = $this->_msb64($B) ^ $this->_uint64($t); |
|
239 | + // R[i] = LSB(64, B) |
|
240 | + $R[$i] = $this->_lsb64($B); |
|
241 | + } |
|
242 | + } |
|
243 | + // Set C[0] = A |
|
244 | + $C = [$A]; |
|
245 | + // For i = 1 to n |
|
246 | + for ($i = 1; $i <= $n; ++$i) { |
|
247 | + // C[i] = R[i] |
|
248 | + $C[$i] = $R[$i]; |
|
249 | + } |
|
250 | + return $C; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Unwrap the padded ciphertext producing plaintext and integrity value. |
|
255 | - * |
|
256 | - * @param string $ciphertext Ciphertext |
|
257 | - * @param string $kek Encryption key |
|
258 | - * @return array Tuple of plaintext <code>{P1, P2, ..., Pn}</code> and |
|
259 | - * integrity value <code>A</code> |
|
260 | - */ |
|
261 | - protected function _unwrapPaddedCiphertext(string $ciphertext, string $kek): array |
|
262 | - { |
|
263 | - // split to blocks |
|
264 | - $C = str_split($ciphertext, 8); |
|
265 | - $n = count($C) - 1; |
|
266 | - // if key consists of only one block, recover AIV and padded key as: |
|
267 | - // A | P[1] = DEC(K, C[0] | C[1]) |
|
268 | - if ($n == 1) { |
|
269 | - $P = str_split($this->_decrypt($kek, $C[0] . $C[1]), 8); |
|
270 | - $A = $P[0]; |
|
271 | - unset($P[0]); |
|
272 | - } else { |
|
273 | - // apply normal unwrapping |
|
274 | - list($A, $R) = $this->_unwrapBlocks($C, $kek); |
|
275 | - $P = array_slice($R, 1, null, true); |
|
276 | - } |
|
277 | - return [$P, $A]; |
|
278 | - } |
|
253 | + /** |
|
254 | + * Unwrap the padded ciphertext producing plaintext and integrity value. |
|
255 | + * |
|
256 | + * @param string $ciphertext Ciphertext |
|
257 | + * @param string $kek Encryption key |
|
258 | + * @return array Tuple of plaintext <code>{P1, P2, ..., Pn}</code> and |
|
259 | + * integrity value <code>A</code> |
|
260 | + */ |
|
261 | + protected function _unwrapPaddedCiphertext(string $ciphertext, string $kek): array |
|
262 | + { |
|
263 | + // split to blocks |
|
264 | + $C = str_split($ciphertext, 8); |
|
265 | + $n = count($C) - 1; |
|
266 | + // if key consists of only one block, recover AIV and padded key as: |
|
267 | + // A | P[1] = DEC(K, C[0] | C[1]) |
|
268 | + if ($n == 1) { |
|
269 | + $P = str_split($this->_decrypt($kek, $C[0] . $C[1]), 8); |
|
270 | + $A = $P[0]; |
|
271 | + unset($P[0]); |
|
272 | + } else { |
|
273 | + // apply normal unwrapping |
|
274 | + list($A, $R) = $this->_unwrapBlocks($C, $kek); |
|
275 | + $P = array_slice($R, 1, null, true); |
|
276 | + } |
|
277 | + return [$P, $A]; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Apply Key Unwrap to data blocks. |
|
282 | - * |
|
283 | - * Uses the index based version of key unwrap procedure |
|
284 | - * described in the RFC. |
|
285 | - * |
|
286 | - * Does not compute step 3. |
|
287 | - * |
|
288 | - * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
|
289 | - * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
|
290 | - * Cn}</code> |
|
291 | - * @param string $kek Key encryption key |
|
292 | - * @throws \UnexpectedValueException |
|
293 | - * @return array Tuple of integrity value <code>A</code> and register |
|
294 | - * <code>R</code> |
|
295 | - */ |
|
296 | - protected function _unwrapBlocks(array $C, string $kek): array |
|
297 | - { |
|
298 | - $n = count($C) - 1; |
|
299 | - if (!$n) { |
|
300 | - throw new \UnexpectedValueException("No blocks."); |
|
301 | - } |
|
302 | - // Set A = C[0] |
|
303 | - $A = $C[0]; |
|
304 | - // For i = 1 to n |
|
305 | - // R[i] = C[i] |
|
306 | - $R = $C; |
|
307 | - // For j = 5 to 0 |
|
308 | - for ($j = 5; $j >= 0; --$j) { |
|
309 | - // For i = n to 1 |
|
310 | - for ($i = $n; $i >= 1; --$i) { |
|
311 | - // B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i |
|
312 | - $t = $n * $j + $i; |
|
313 | - $B = $this->_decrypt($kek, ($A ^ $this->_uint64($t)) . $R[$i]); |
|
314 | - // A = MSB(64, B) |
|
315 | - $A = $this->_msb64($B); |
|
316 | - // R[i] = LSB(64, B) |
|
317 | - $R[$i] = $this->_lsb64($B); |
|
318 | - } |
|
319 | - } |
|
320 | - return array($A, $R); |
|
321 | - } |
|
280 | + /** |
|
281 | + * Apply Key Unwrap to data blocks. |
|
282 | + * |
|
283 | + * Uses the index based version of key unwrap procedure |
|
284 | + * described in the RFC. |
|
285 | + * |
|
286 | + * Does not compute step 3. |
|
287 | + * |
|
288 | + * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
|
289 | + * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
|
290 | + * Cn}</code> |
|
291 | + * @param string $kek Key encryption key |
|
292 | + * @throws \UnexpectedValueException |
|
293 | + * @return array Tuple of integrity value <code>A</code> and register |
|
294 | + * <code>R</code> |
|
295 | + */ |
|
296 | + protected function _unwrapBlocks(array $C, string $kek): array |
|
297 | + { |
|
298 | + $n = count($C) - 1; |
|
299 | + if (!$n) { |
|
300 | + throw new \UnexpectedValueException("No blocks."); |
|
301 | + } |
|
302 | + // Set A = C[0] |
|
303 | + $A = $C[0]; |
|
304 | + // For i = 1 to n |
|
305 | + // R[i] = C[i] |
|
306 | + $R = $C; |
|
307 | + // For j = 5 to 0 |
|
308 | + for ($j = 5; $j >= 0; --$j) { |
|
309 | + // For i = n to 1 |
|
310 | + for ($i = $n; $i >= 1; --$i) { |
|
311 | + // B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i |
|
312 | + $t = $n * $j + $i; |
|
313 | + $B = $this->_decrypt($kek, ($A ^ $this->_uint64($t)) . $R[$i]); |
|
314 | + // A = MSB(64, B) |
|
315 | + $A = $this->_msb64($B); |
|
316 | + // R[i] = LSB(64, B) |
|
317 | + $R[$i] = $this->_lsb64($B); |
|
318 | + } |
|
319 | + } |
|
320 | + return array($A, $R); |
|
321 | + } |
|
322 | 322 | |
323 | - /** |
|
324 | - * Pad a key with zeroes and compute alternative initial value. |
|
325 | - * |
|
326 | - * @param string $key Key |
|
327 | - * @return array Tuple of padded key and AIV |
|
328 | - */ |
|
329 | - protected function _padKey(string $key): array |
|
330 | - { |
|
331 | - $len = strlen($key); |
|
332 | - // append padding |
|
333 | - if (0 != $len % 8) { |
|
334 | - $key .= str_repeat("\0", 8 - $len % 8); |
|
335 | - } |
|
336 | - // compute AIV |
|
337 | - $mli = pack("N", $len); |
|
338 | - $aiv = self::AIV_HI . $mli; |
|
339 | - return [$key, $aiv]; |
|
340 | - } |
|
323 | + /** |
|
324 | + * Pad a key with zeroes and compute alternative initial value. |
|
325 | + * |
|
326 | + * @param string $key Key |
|
327 | + * @return array Tuple of padded key and AIV |
|
328 | + */ |
|
329 | + protected function _padKey(string $key): array |
|
330 | + { |
|
331 | + $len = strlen($key); |
|
332 | + // append padding |
|
333 | + if (0 != $len % 8) { |
|
334 | + $key .= str_repeat("\0", 8 - $len % 8); |
|
335 | + } |
|
336 | + // compute AIV |
|
337 | + $mli = pack("N", $len); |
|
338 | + $aiv = self::AIV_HI . $mli; |
|
339 | + return [$key, $aiv]; |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * Check that the integrity check value of the padded key is correct. |
|
344 | - * |
|
345 | - * @param string $A |
|
346 | - * @throws \UnexpectedValueException |
|
347 | - */ |
|
348 | - protected function _checkPaddedIntegrity(string $A) |
|
349 | - { |
|
350 | - // check that MSB(32,A) = A65959A6 |
|
351 | - if (substr($A, 0, 4) != self::AIV_HI) { |
|
352 | - throw new \UnexpectedValueException("Integrity check failed."); |
|
353 | - } |
|
354 | - } |
|
342 | + /** |
|
343 | + * Check that the integrity check value of the padded key is correct. |
|
344 | + * |
|
345 | + * @param string $A |
|
346 | + * @throws \UnexpectedValueException |
|
347 | + */ |
|
348 | + protected function _checkPaddedIntegrity(string $A) |
|
349 | + { |
|
350 | + // check that MSB(32,A) = A65959A6 |
|
351 | + if (substr($A, 0, 4) != self::AIV_HI) { |
|
352 | + throw new \UnexpectedValueException("Integrity check failed."); |
|
353 | + } |
|
354 | + } |
|
355 | 355 | |
356 | - /** |
|
357 | - * Verify that the padding of the plaintext is valid. |
|
358 | - * |
|
359 | - * @param array $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
|
360 | - * Pn}</code> |
|
361 | - * @param string $A Integrity check value |
|
362 | - * @throws \UnexpectedValueException |
|
363 | - * @return int Message length without padding |
|
364 | - */ |
|
365 | - protected function _verifyPadding(array $P, string $A): int |
|
366 | - { |
|
367 | - // extract mli |
|
368 | - $mli = substr($A, -4); |
|
369 | - $len = unpack("N1", $mli)[1]; |
|
370 | - // check under and overflow |
|
371 | - $n = count($P); |
|
372 | - if (8 * ($n - 1) >= $len || $len > 8 * $n) { |
|
373 | - throw new \UnexpectedValueException("Invalid message length."); |
|
374 | - } |
|
375 | - // if key is padded |
|
376 | - $b = 8 - ($len % 8); |
|
377 | - if ($b < 8) { |
|
378 | - // last block (note that the first index in P is 1) |
|
379 | - $Pn = $P[$n]; |
|
380 | - // check that padding consists of zeroes |
|
381 | - if (substr($Pn, -$b) != str_repeat("\0", $b)) { |
|
382 | - throw new \UnexpectedValueException("Invalid padding."); |
|
383 | - } |
|
384 | - } |
|
385 | - return $len; |
|
386 | - } |
|
356 | + /** |
|
357 | + * Verify that the padding of the plaintext is valid. |
|
358 | + * |
|
359 | + * @param array $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
|
360 | + * Pn}</code> |
|
361 | + * @param string $A Integrity check value |
|
362 | + * @throws \UnexpectedValueException |
|
363 | + * @return int Message length without padding |
|
364 | + */ |
|
365 | + protected function _verifyPadding(array $P, string $A): int |
|
366 | + { |
|
367 | + // extract mli |
|
368 | + $mli = substr($A, -4); |
|
369 | + $len = unpack("N1", $mli)[1]; |
|
370 | + // check under and overflow |
|
371 | + $n = count($P); |
|
372 | + if (8 * ($n - 1) >= $len || $len > 8 * $n) { |
|
373 | + throw new \UnexpectedValueException("Invalid message length."); |
|
374 | + } |
|
375 | + // if key is padded |
|
376 | + $b = 8 - ($len % 8); |
|
377 | + if ($b < 8) { |
|
378 | + // last block (note that the first index in P is 1) |
|
379 | + $Pn = $P[$n]; |
|
380 | + // check that padding consists of zeroes |
|
381 | + if (substr($Pn, -$b) != str_repeat("\0", $b)) { |
|
382 | + throw new \UnexpectedValueException("Invalid padding."); |
|
383 | + } |
|
384 | + } |
|
385 | + return $len; |
|
386 | + } |
|
387 | 387 | |
388 | - /** |
|
389 | - * Apply AES(K, W) operation (encrypt) to 64 bit block. |
|
390 | - * |
|
391 | - * @param string $kek |
|
392 | - * @param string $block |
|
393 | - * @throws \RuntimeException If encrypt fails |
|
394 | - * @return string |
|
395 | - */ |
|
396 | - protected function _encrypt(string $kek, string $block): string |
|
397 | - { |
|
398 | - $str = openssl_encrypt($block, $this->_cipherMethod(), $kek, |
|
399 | - OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
400 | - if (false === $str) { |
|
401 | - throw new \RuntimeException( |
|
402 | - "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
403 | - } |
|
404 | - return $str; |
|
405 | - } |
|
388 | + /** |
|
389 | + * Apply AES(K, W) operation (encrypt) to 64 bit block. |
|
390 | + * |
|
391 | + * @param string $kek |
|
392 | + * @param string $block |
|
393 | + * @throws \RuntimeException If encrypt fails |
|
394 | + * @return string |
|
395 | + */ |
|
396 | + protected function _encrypt(string $kek, string $block): string |
|
397 | + { |
|
398 | + $str = openssl_encrypt($block, $this->_cipherMethod(), $kek, |
|
399 | + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
400 | + if (false === $str) { |
|
401 | + throw new \RuntimeException( |
|
402 | + "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
403 | + } |
|
404 | + return $str; |
|
405 | + } |
|
406 | 406 | |
407 | - /** |
|
408 | - * Apply AES-1(K, W) operation (decrypt) to 64 bit block. |
|
409 | - * |
|
410 | - * @param string $kek |
|
411 | - * @param string $block |
|
412 | - * @throws \RuntimeException If decrypt fails |
|
413 | - * @return string |
|
414 | - */ |
|
415 | - protected function _decrypt(string $kek, string $block): string |
|
416 | - { |
|
417 | - $str = openssl_decrypt($block, $this->_cipherMethod(), $kek, |
|
418 | - OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
419 | - if (false === $str) { |
|
420 | - throw new \RuntimeException( |
|
421 | - "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
422 | - } |
|
423 | - return $str; |
|
424 | - } |
|
407 | + /** |
|
408 | + * Apply AES-1(K, W) operation (decrypt) to 64 bit block. |
|
409 | + * |
|
410 | + * @param string $kek |
|
411 | + * @param string $block |
|
412 | + * @throws \RuntimeException If decrypt fails |
|
413 | + * @return string |
|
414 | + */ |
|
415 | + protected function _decrypt(string $kek, string $block): string |
|
416 | + { |
|
417 | + $str = openssl_decrypt($block, $this->_cipherMethod(), $kek, |
|
418 | + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); |
|
419 | + if (false === $str) { |
|
420 | + throw new \RuntimeException( |
|
421 | + "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
422 | + } |
|
423 | + return $str; |
|
424 | + } |
|
425 | 425 | |
426 | - /** |
|
427 | - * Get the latest OpenSSL error message. |
|
428 | - * |
|
429 | - * @return string |
|
430 | - */ |
|
431 | - protected function _getLastOpenSSLError(): string |
|
432 | - { |
|
433 | - $msg = ""; |
|
434 | - while (false !== ($err = openssl_error_string())) { |
|
435 | - $msg = $err; |
|
436 | - } |
|
437 | - return $msg; |
|
438 | - } |
|
426 | + /** |
|
427 | + * Get the latest OpenSSL error message. |
|
428 | + * |
|
429 | + * @return string |
|
430 | + */ |
|
431 | + protected function _getLastOpenSSLError(): string |
|
432 | + { |
|
433 | + $msg = ""; |
|
434 | + while (false !== ($err = openssl_error_string())) { |
|
435 | + $msg = $err; |
|
436 | + } |
|
437 | + return $msg; |
|
438 | + } |
|
439 | 439 | |
440 | - /** |
|
441 | - * Take 64 most significant bits from value. |
|
442 | - * |
|
443 | - * @param string $val |
|
444 | - * @return string |
|
445 | - */ |
|
446 | - protected function _msb64(string $val): string |
|
447 | - { |
|
448 | - return substr($val, 0, 8); |
|
449 | - } |
|
440 | + /** |
|
441 | + * Take 64 most significant bits from value. |
|
442 | + * |
|
443 | + * @param string $val |
|
444 | + * @return string |
|
445 | + */ |
|
446 | + protected function _msb64(string $val): string |
|
447 | + { |
|
448 | + return substr($val, 0, 8); |
|
449 | + } |
|
450 | 450 | |
451 | - /** |
|
452 | - * Take 64 least significant bits from value. |
|
453 | - * |
|
454 | - * @param string $val |
|
455 | - * @return string |
|
456 | - */ |
|
457 | - protected function _lsb64(string $val): string |
|
458 | - { |
|
459 | - return substr($val, -8); |
|
460 | - } |
|
451 | + /** |
|
452 | + * Take 64 least significant bits from value. |
|
453 | + * |
|
454 | + * @param string $val |
|
455 | + * @return string |
|
456 | + */ |
|
457 | + protected function _lsb64(string $val): string |
|
458 | + { |
|
459 | + return substr($val, -8); |
|
460 | + } |
|
461 | 461 | |
462 | - /** |
|
463 | - * Convert number to 64 bit unsigned integer octet string with |
|
464 | - * most significant bit first. |
|
465 | - * |
|
466 | - * @param int $num |
|
467 | - * @return string |
|
468 | - */ |
|
469 | - protected function _uint64(int $num): string |
|
470 | - { |
|
471 | - // truncate on 32 bit hosts |
|
472 | - if (PHP_INT_SIZE < 8) { |
|
473 | - return "\0\0\0\0" . pack("N", $num); |
|
474 | - } |
|
475 | - return pack("J", $num); |
|
476 | - } |
|
462 | + /** |
|
463 | + * Convert number to 64 bit unsigned integer octet string with |
|
464 | + * most significant bit first. |
|
465 | + * |
|
466 | + * @param int $num |
|
467 | + * @return string |
|
468 | + */ |
|
469 | + protected function _uint64(int $num): string |
|
470 | + { |
|
471 | + // truncate on 32 bit hosts |
|
472 | + if (PHP_INT_SIZE < 8) { |
|
473 | + return "\0\0\0\0" . pack("N", $num); |
|
474 | + } |
|
475 | + return pack("J", $num); |
|
476 | + } |
|
477 | 477 | } |
@@ -9,21 +9,21 @@ |
||
9 | 9 | */ |
10 | 10 | class AESKW128 extends Algorithm |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - protected function _cipherMethod(): string |
|
17 | - { |
|
18 | - return "AES-128-ECB"; |
|
19 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + protected function _cipherMethod(): string |
|
17 | + { |
|
18 | + return "AES-128-ECB"; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * |
|
23 | - * {@inheritdoc} |
|
24 | - */ |
|
25 | - protected function _keySize(): int |
|
26 | - { |
|
27 | - return 16; |
|
28 | - } |
|
21 | + /** |
|
22 | + * |
|
23 | + * {@inheritdoc} |
|
24 | + */ |
|
25 | + protected function _keySize(): int |
|
26 | + { |
|
27 | + return 16; |
|
28 | + } |
|
29 | 29 | } |
@@ -13,61 +13,61 @@ |
||
13 | 13 | */ |
14 | 14 | interface AESKeyWrapAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * Wrap a key using given key encryption key. |
|
18 | - * |
|
19 | - * Key length must be at least 64 bits (8 octets) and a multiple |
|
20 | - * of 64 bits (8 octets). |
|
21 | - * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
|
22 | - * |
|
23 | - * Key encryption key must have a size of underlying AES algorithm, |
|
24 | - * ie. 128, 196 or 256 bits. |
|
25 | - * |
|
26 | - * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
|
27 | - * @param string $key Key to wrap |
|
28 | - * @param string $kek Key encryption key |
|
29 | - * @throws \RuntimeException For invalid inputs |
|
30 | - * @return string Ciphertext |
|
31 | - */ |
|
32 | - public function wrap(string $key, string $kek): string; |
|
16 | + /** |
|
17 | + * Wrap a key using given key encryption key. |
|
18 | + * |
|
19 | + * Key length must be at least 64 bits (8 octets) and a multiple |
|
20 | + * of 64 bits (8 octets). |
|
21 | + * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
|
22 | + * |
|
23 | + * Key encryption key must have a size of underlying AES algorithm, |
|
24 | + * ie. 128, 196 or 256 bits. |
|
25 | + * |
|
26 | + * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
|
27 | + * @param string $key Key to wrap |
|
28 | + * @param string $kek Key encryption key |
|
29 | + * @throws \RuntimeException For invalid inputs |
|
30 | + * @return string Ciphertext |
|
31 | + */ |
|
32 | + public function wrap(string $key, string $kek): string; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Unwrap a key from a ciphertext using given key encryption key. |
|
36 | - * |
|
37 | - * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
|
38 | - * @param string $ciphertext Ciphertext of the wrapped key |
|
39 | - * @param string $kek Key encryption key |
|
40 | - * @throws \RuntimeException For invalid inputs |
|
41 | - * @return string Unwrapped key |
|
42 | - */ |
|
43 | - public function unwrap(string $ciphertext, string $kek): string; |
|
34 | + /** |
|
35 | + * Unwrap a key from a ciphertext using given key encryption key. |
|
36 | + * |
|
37 | + * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
|
38 | + * @param string $ciphertext Ciphertext of the wrapped key |
|
39 | + * @param string $kek Key encryption key |
|
40 | + * @throws \RuntimeException For invalid inputs |
|
41 | + * @return string Unwrapped key |
|
42 | + */ |
|
43 | + public function unwrap(string $ciphertext, string $kek): string; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Wrap a key of arbitrary length using given key encryption key. |
|
47 | - * |
|
48 | - * This variant of wrapping does not place any restriction on key size. |
|
49 | - * |
|
50 | - * Key encryption key has the same restrictions as with <i>wrap</i> method. |
|
51 | - * |
|
52 | - * @link https://tools.ietf.org/html/rfc5649#section-4.1 |
|
53 | - * @param string $key Key to wrap |
|
54 | - * @param string $kek Key encryption key |
|
55 | - * @throws \RuntimeException For invalid inputs |
|
56 | - * @return string Ciphertext |
|
57 | - */ |
|
58 | - public function wrapPad(string $key, string $kek): string; |
|
45 | + /** |
|
46 | + * Wrap a key of arbitrary length using given key encryption key. |
|
47 | + * |
|
48 | + * This variant of wrapping does not place any restriction on key size. |
|
49 | + * |
|
50 | + * Key encryption key has the same restrictions as with <i>wrap</i> method. |
|
51 | + * |
|
52 | + * @link https://tools.ietf.org/html/rfc5649#section-4.1 |
|
53 | + * @param string $key Key to wrap |
|
54 | + * @param string $kek Key encryption key |
|
55 | + * @throws \RuntimeException For invalid inputs |
|
56 | + * @return string Ciphertext |
|
57 | + */ |
|
58 | + public function wrapPad(string $key, string $kek): string; |
|
59 | 59 | |
60 | - /** |
|
61 | - * Unwrap a key from a padded ciphertext using given key encryption key. |
|
62 | - * |
|
63 | - * This variant of unwrapping must be used if the key was wrapped using |
|
64 | - * <i>wrapPad</i>. |
|
65 | - * |
|
66 | - * @link https://tools.ietf.org/html/rfc5649#section-4.2 |
|
67 | - * @param string $ciphertext Ciphertext of the wrapped and padded key |
|
68 | - * @param string $kek Key encryption key |
|
69 | - * @throws \RuntimeException For invalid inputs |
|
70 | - * @return string Unwrapped key |
|
71 | - */ |
|
72 | - public function unwrapPad(string $ciphertext, string $kek): string; |
|
60 | + /** |
|
61 | + * Unwrap a key from a padded ciphertext using given key encryption key. |
|
62 | + * |
|
63 | + * This variant of unwrapping must be used if the key was wrapped using |
|
64 | + * <i>wrapPad</i>. |
|
65 | + * |
|
66 | + * @link https://tools.ietf.org/html/rfc5649#section-4.2 |
|
67 | + * @param string $ciphertext Ciphertext of the wrapped and padded key |
|
68 | + * @param string $kek Key encryption key |
|
69 | + * @throws \RuntimeException For invalid inputs |
|
70 | + * @return string Unwrapped key |
|
71 | + */ |
|
72 | + public function unwrapPad(string $ciphertext, string $kek): string; |
|
73 | 73 | } |
@@ -9,21 +9,21 @@ |
||
9 | 9 | */ |
10 | 10 | class AESKW192 extends Algorithm |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - protected function _cipherMethod(): string |
|
17 | - { |
|
18 | - return "AES-192-ECB"; |
|
19 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + protected function _cipherMethod(): string |
|
17 | + { |
|
18 | + return "AES-192-ECB"; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * |
|
23 | - * {@inheritdoc} |
|
24 | - */ |
|
25 | - protected function _keySize(): int |
|
26 | - { |
|
27 | - return 24; |
|
28 | - } |
|
21 | + /** |
|
22 | + * |
|
23 | + * {@inheritdoc} |
|
24 | + */ |
|
25 | + protected function _keySize(): int |
|
26 | + { |
|
27 | + return 24; |
|
28 | + } |
|
29 | 29 | } |
@@ -9,21 +9,21 @@ |
||
9 | 9 | */ |
10 | 10 | class AESKW256 extends Algorithm |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - protected function _cipherMethod(): string |
|
17 | - { |
|
18 | - return "AES-256-ECB"; |
|
19 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + protected function _cipherMethod(): string |
|
17 | + { |
|
18 | + return "AES-256-ECB"; |
|
19 | + } |
|
20 | 20 | |
21 | - /** |
|
22 | - * |
|
23 | - * {@inheritdoc} |
|
24 | - */ |
|
25 | - protected function _keySize(): int |
|
26 | - { |
|
27 | - return 32; |
|
28 | - } |
|
21 | + /** |
|
22 | + * |
|
23 | + * {@inheritdoc} |
|
24 | + */ |
|
25 | + protected function _keySize(): int |
|
26 | + { |
|
27 | + return 32; |
|
28 | + } |
|
29 | 29 | } |