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