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