@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_XSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,49 +9,49 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_XSalsa20 extends ParagonIE_Sodium_Core_HSalsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $len |
|
18 | - * @param string $nonce |
|
19 | - * @param string $key |
|
20 | - * @return string |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - */ |
|
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
26 | - $ret = self::salsa20( |
|
27 | - $len, |
|
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
30 | - ); |
|
31 | - return $ret; |
|
32 | - } |
|
12 | + /** |
|
13 | + * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $len |
|
18 | + * @param string $nonce |
|
19 | + * @param string $key |
|
20 | + * @return string |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + */ |
|
24 | + public static function xsalsa20($len, $nonce, $key) |
|
25 | + { |
|
26 | + $ret = self::salsa20( |
|
27 | + $len, |
|
28 | + self::substr($nonce, 16, 8), |
|
29 | + self::hsalsa20($nonce, $key) |
|
30 | + ); |
|
31 | + return $ret; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | - * |
|
37 | - * @internal You should not use this directly from another application |
|
38 | - * |
|
39 | - * @param string $message |
|
40 | - * @param string $nonce |
|
41 | - * @param string $key |
|
42 | - * @return string |
|
43 | - * @throws SodiumException |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
48 | - return self::xorStrings( |
|
49 | - $message, |
|
50 | - self::xsalsa20( |
|
51 | - self::strlen($message), |
|
52 | - $nonce, |
|
53 | - $key |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
34 | + /** |
|
35 | + * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | + * |
|
37 | + * @internal You should not use this directly from another application |
|
38 | + * |
|
39 | + * @param string $message |
|
40 | + * @param string $nonce |
|
41 | + * @param string $key |
|
42 | + * @return string |
|
43 | + * @throws SodiumException |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | + { |
|
48 | + return self::xorStrings( |
|
49 | + $message, |
|
50 | + self::xsalsa20( |
|
51 | + self::strlen($message), |
|
52 | + $nonce, |
|
53 | + $key |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,80 +9,80 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * Bitwise left rotation |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $v |
|
18 | - * @param int $n |
|
19 | - * @return int |
|
20 | - */ |
|
21 | - public static function rotate($v, $n) |
|
22 | - { |
|
23 | - $v &= 0xffffffff; |
|
24 | - $n &= 31; |
|
25 | - return (int) ( |
|
26 | - 0xffffffff & ( |
|
27 | - ($v << $n) |
|
28 | - | |
|
29 | - ($v >> (32 - $n)) |
|
30 | - ) |
|
31 | - ); |
|
32 | - } |
|
12 | + /** |
|
13 | + * Bitwise left rotation |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $v |
|
18 | + * @param int $n |
|
19 | + * @return int |
|
20 | + */ |
|
21 | + public static function rotate($v, $n) |
|
22 | + { |
|
23 | + $v &= 0xffffffff; |
|
24 | + $n &= 31; |
|
25 | + return (int) ( |
|
26 | + 0xffffffff & ( |
|
27 | + ($v << $n) |
|
28 | + | |
|
29 | + ($v >> (32 - $n)) |
|
30 | + ) |
|
31 | + ); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
36 | - * |
|
37 | - * @internal You should not use this directly from another application |
|
38 | - * |
|
39 | - * @param int $a |
|
40 | - * @param int $b |
|
41 | - * @param int $c |
|
42 | - * @param int $d |
|
43 | - * @return array<int, int> |
|
44 | - */ |
|
45 | - protected static function quarterRound($a, $b, $c, $d) |
|
46 | - { |
|
47 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
48 | - /** @var int $a */ |
|
49 | - $a = ($a + $b) & 0xffffffff; |
|
50 | - $d = self::rotate($d ^ $a, 16); |
|
34 | + /** |
|
35 | + * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
36 | + * |
|
37 | + * @internal You should not use this directly from another application |
|
38 | + * |
|
39 | + * @param int $a |
|
40 | + * @param int $b |
|
41 | + * @param int $c |
|
42 | + * @param int $d |
|
43 | + * @return array<int, int> |
|
44 | + */ |
|
45 | + protected static function quarterRound($a, $b, $c, $d) |
|
46 | + { |
|
47 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
48 | + /** @var int $a */ |
|
49 | + $a = ($a + $b) & 0xffffffff; |
|
50 | + $d = self::rotate($d ^ $a, 16); |
|
51 | 51 | |
52 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
53 | - /** @var int $c */ |
|
54 | - $c = ($c + $d) & 0xffffffff; |
|
55 | - $b = self::rotate($b ^ $c, 12); |
|
52 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
53 | + /** @var int $c */ |
|
54 | + $c = ($c + $d) & 0xffffffff; |
|
55 | + $b = self::rotate($b ^ $c, 12); |
|
56 | 56 | |
57 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
58 | - /** @var int $a */ |
|
59 | - $a = ($a + $b) & 0xffffffff; |
|
60 | - $d = self::rotate($d ^ $a, 8); |
|
57 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
58 | + /** @var int $a */ |
|
59 | + $a = ($a + $b) & 0xffffffff; |
|
60 | + $d = self::rotate($d ^ $a, 8); |
|
61 | 61 | |
62 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
63 | - /** @var int $c */ |
|
64 | - $c = ($c + $d) & 0xffffffff; |
|
65 | - $b = self::rotate($b ^ $c, 7); |
|
66 | - return array((int) $a, (int) $b, (int) $c, (int) $d); |
|
67 | - } |
|
62 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
63 | + /** @var int $c */ |
|
64 | + $c = ($c + $d) & 0xffffffff; |
|
65 | + $b = self::rotate($b ^ $c, 7); |
|
66 | + return array((int) $a, (int) $b, (int) $c, (int) $d); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @internal You should not use this directly from another application |
|
71 | - * |
|
72 | - * @param ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx |
|
73 | - * @param string $message |
|
74 | - * |
|
75 | - * @return string |
|
76 | - * @throws TypeError |
|
77 | - * @throws SodiumException |
|
78 | - */ |
|
79 | - public static function encryptBytes( |
|
80 | - ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx, |
|
81 | - $message = '' |
|
82 | - ) { |
|
83 | - $bytes = self::strlen($message); |
|
69 | + /** |
|
70 | + * @internal You should not use this directly from another application |
|
71 | + * |
|
72 | + * @param ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx |
|
73 | + * @param string $message |
|
74 | + * |
|
75 | + * @return string |
|
76 | + * @throws TypeError |
|
77 | + * @throws SodiumException |
|
78 | + */ |
|
79 | + public static function encryptBytes( |
|
80 | + ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx, |
|
81 | + $message = '' |
|
82 | + ) { |
|
83 | + $bytes = self::strlen($message); |
|
84 | 84 | |
85 | - /* |
|
85 | + /* |
|
86 | 86 | j0 = ctx->input[0]; |
87 | 87 | j1 = ctx->input[1]; |
88 | 88 | j2 = ctx->input[2]; |
@@ -100,73 +100,73 @@ discard block |
||
100 | 100 | j14 = ctx->input[14]; |
101 | 101 | j15 = ctx->input[15]; |
102 | 102 | */ |
103 | - $j0 = (int) $ctx[0]; |
|
104 | - $j1 = (int) $ctx[1]; |
|
105 | - $j2 = (int) $ctx[2]; |
|
106 | - $j3 = (int) $ctx[3]; |
|
107 | - $j4 = (int) $ctx[4]; |
|
108 | - $j5 = (int) $ctx[5]; |
|
109 | - $j6 = (int) $ctx[6]; |
|
110 | - $j7 = (int) $ctx[7]; |
|
111 | - $j8 = (int) $ctx[8]; |
|
112 | - $j9 = (int) $ctx[9]; |
|
113 | - $j10 = (int) $ctx[10]; |
|
114 | - $j11 = (int) $ctx[11]; |
|
115 | - $j12 = (int) $ctx[12]; |
|
116 | - $j13 = (int) $ctx[13]; |
|
117 | - $j14 = (int) $ctx[14]; |
|
118 | - $j15 = (int) $ctx[15]; |
|
103 | + $j0 = (int) $ctx[0]; |
|
104 | + $j1 = (int) $ctx[1]; |
|
105 | + $j2 = (int) $ctx[2]; |
|
106 | + $j3 = (int) $ctx[3]; |
|
107 | + $j4 = (int) $ctx[4]; |
|
108 | + $j5 = (int) $ctx[5]; |
|
109 | + $j6 = (int) $ctx[6]; |
|
110 | + $j7 = (int) $ctx[7]; |
|
111 | + $j8 = (int) $ctx[8]; |
|
112 | + $j9 = (int) $ctx[9]; |
|
113 | + $j10 = (int) $ctx[10]; |
|
114 | + $j11 = (int) $ctx[11]; |
|
115 | + $j12 = (int) $ctx[12]; |
|
116 | + $j13 = (int) $ctx[13]; |
|
117 | + $j14 = (int) $ctx[14]; |
|
118 | + $j15 = (int) $ctx[15]; |
|
119 | 119 | |
120 | - $c = ''; |
|
121 | - for (;;) { |
|
122 | - if ($bytes < 64) { |
|
123 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
124 | - } |
|
120 | + $c = ''; |
|
121 | + for (;;) { |
|
122 | + if ($bytes < 64) { |
|
123 | + $message .= str_repeat("\x00", 64 - $bytes); |
|
124 | + } |
|
125 | 125 | |
126 | - $x0 = (int) $j0; |
|
127 | - $x1 = (int) $j1; |
|
128 | - $x2 = (int) $j2; |
|
129 | - $x3 = (int) $j3; |
|
130 | - $x4 = (int) $j4; |
|
131 | - $x5 = (int) $j5; |
|
132 | - $x6 = (int) $j6; |
|
133 | - $x7 = (int) $j7; |
|
134 | - $x8 = (int) $j8; |
|
135 | - $x9 = (int) $j9; |
|
136 | - $x10 = (int) $j10; |
|
137 | - $x11 = (int) $j11; |
|
138 | - $x12 = (int) $j12; |
|
139 | - $x13 = (int) $j13; |
|
140 | - $x14 = (int) $j14; |
|
141 | - $x15 = (int) $j15; |
|
126 | + $x0 = (int) $j0; |
|
127 | + $x1 = (int) $j1; |
|
128 | + $x2 = (int) $j2; |
|
129 | + $x3 = (int) $j3; |
|
130 | + $x4 = (int) $j4; |
|
131 | + $x5 = (int) $j5; |
|
132 | + $x6 = (int) $j6; |
|
133 | + $x7 = (int) $j7; |
|
134 | + $x8 = (int) $j8; |
|
135 | + $x9 = (int) $j9; |
|
136 | + $x10 = (int) $j10; |
|
137 | + $x11 = (int) $j11; |
|
138 | + $x12 = (int) $j12; |
|
139 | + $x13 = (int) $j13; |
|
140 | + $x14 = (int) $j14; |
|
141 | + $x15 = (int) $j15; |
|
142 | 142 | |
143 | - # for (i = 20; i > 0; i -= 2) { |
|
144 | - for ($i = 20; $i > 0; $i -= 2) { |
|
145 | - # QUARTERROUND( x0, x4, x8, x12) |
|
146 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
143 | + # for (i = 20; i > 0; i -= 2) { |
|
144 | + for ($i = 20; $i > 0; $i -= 2) { |
|
145 | + # QUARTERROUND( x0, x4, x8, x12) |
|
146 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
147 | 147 | |
148 | - # QUARTERROUND( x1, x5, x9, x13) |
|
149 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
148 | + # QUARTERROUND( x1, x5, x9, x13) |
|
149 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
150 | 150 | |
151 | - # QUARTERROUND( x2, x6, x10, x14) |
|
152 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
151 | + # QUARTERROUND( x2, x6, x10, x14) |
|
152 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
153 | 153 | |
154 | - # QUARTERROUND( x3, x7, x11, x15) |
|
155 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
154 | + # QUARTERROUND( x3, x7, x11, x15) |
|
155 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
156 | 156 | |
157 | - # QUARTERROUND( x0, x5, x10, x15) |
|
158 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
157 | + # QUARTERROUND( x0, x5, x10, x15) |
|
158 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
159 | 159 | |
160 | - # QUARTERROUND( x1, x6, x11, x12) |
|
161 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
160 | + # QUARTERROUND( x1, x6, x11, x12) |
|
161 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
162 | 162 | |
163 | - # QUARTERROUND( x2, x7, x8, x13) |
|
164 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
163 | + # QUARTERROUND( x2, x7, x8, x13) |
|
164 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
165 | 165 | |
166 | - # QUARTERROUND( x3, x4, x9, x14) |
|
167 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
168 | - } |
|
169 | - /* |
|
166 | + # QUARTERROUND( x3, x4, x9, x14) |
|
167 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
168 | + } |
|
169 | + /* |
|
170 | 170 | x0 = PLUS(x0, j0); |
171 | 171 | x1 = PLUS(x1, j1); |
172 | 172 | x2 = PLUS(x2, j2); |
@@ -184,40 +184,40 @@ discard block |
||
184 | 184 | x14 = PLUS(x14, j14); |
185 | 185 | x15 = PLUS(x15, j15); |
186 | 186 | */ |
187 | - /** @var int $x0 */ |
|
188 | - $x0 = ($x0 & 0xffffffff) + $j0; |
|
189 | - /** @var int $x1 */ |
|
190 | - $x1 = ($x1 & 0xffffffff) + $j1; |
|
191 | - /** @var int $x2 */ |
|
192 | - $x2 = ($x2 & 0xffffffff) + $j2; |
|
193 | - /** @var int $x3 */ |
|
194 | - $x3 = ($x3 & 0xffffffff) + $j3; |
|
195 | - /** @var int $x4 */ |
|
196 | - $x4 = ($x4 & 0xffffffff) + $j4; |
|
197 | - /** @var int $x5 */ |
|
198 | - $x5 = ($x5 & 0xffffffff) + $j5; |
|
199 | - /** @var int $x6 */ |
|
200 | - $x6 = ($x6 & 0xffffffff) + $j6; |
|
201 | - /** @var int $x7 */ |
|
202 | - $x7 = ($x7 & 0xffffffff) + $j7; |
|
203 | - /** @var int $x8 */ |
|
204 | - $x8 = ($x8 & 0xffffffff) + $j8; |
|
205 | - /** @var int $x9 */ |
|
206 | - $x9 = ($x9 & 0xffffffff) + $j9; |
|
207 | - /** @var int $x10 */ |
|
208 | - $x10 = ($x10 & 0xffffffff) + $j10; |
|
209 | - /** @var int $x11 */ |
|
210 | - $x11 = ($x11 & 0xffffffff) + $j11; |
|
211 | - /** @var int $x12 */ |
|
212 | - $x12 = ($x12 & 0xffffffff) + $j12; |
|
213 | - /** @var int $x13 */ |
|
214 | - $x13 = ($x13 & 0xffffffff) + $j13; |
|
215 | - /** @var int $x14 */ |
|
216 | - $x14 = ($x14 & 0xffffffff) + $j14; |
|
217 | - /** @var int $x15 */ |
|
218 | - $x15 = ($x15 & 0xffffffff) + $j15; |
|
187 | + /** @var int $x0 */ |
|
188 | + $x0 = ($x0 & 0xffffffff) + $j0; |
|
189 | + /** @var int $x1 */ |
|
190 | + $x1 = ($x1 & 0xffffffff) + $j1; |
|
191 | + /** @var int $x2 */ |
|
192 | + $x2 = ($x2 & 0xffffffff) + $j2; |
|
193 | + /** @var int $x3 */ |
|
194 | + $x3 = ($x3 & 0xffffffff) + $j3; |
|
195 | + /** @var int $x4 */ |
|
196 | + $x4 = ($x4 & 0xffffffff) + $j4; |
|
197 | + /** @var int $x5 */ |
|
198 | + $x5 = ($x5 & 0xffffffff) + $j5; |
|
199 | + /** @var int $x6 */ |
|
200 | + $x6 = ($x6 & 0xffffffff) + $j6; |
|
201 | + /** @var int $x7 */ |
|
202 | + $x7 = ($x7 & 0xffffffff) + $j7; |
|
203 | + /** @var int $x8 */ |
|
204 | + $x8 = ($x8 & 0xffffffff) + $j8; |
|
205 | + /** @var int $x9 */ |
|
206 | + $x9 = ($x9 & 0xffffffff) + $j9; |
|
207 | + /** @var int $x10 */ |
|
208 | + $x10 = ($x10 & 0xffffffff) + $j10; |
|
209 | + /** @var int $x11 */ |
|
210 | + $x11 = ($x11 & 0xffffffff) + $j11; |
|
211 | + /** @var int $x12 */ |
|
212 | + $x12 = ($x12 & 0xffffffff) + $j12; |
|
213 | + /** @var int $x13 */ |
|
214 | + $x13 = ($x13 & 0xffffffff) + $j13; |
|
215 | + /** @var int $x14 */ |
|
216 | + $x14 = ($x14 & 0xffffffff) + $j14; |
|
217 | + /** @var int $x15 */ |
|
218 | + $x15 = ($x15 & 0xffffffff) + $j15; |
|
219 | 219 | |
220 | - /* |
|
220 | + /* |
|
221 | 221 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
222 | 222 | x1 = XOR(x1, LOAD32_LE(m + 4)); |
223 | 223 | x2 = XOR(x2, LOAD32_LE(m + 8)); |
@@ -235,35 +235,35 @@ discard block |
||
235 | 235 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
236 | 236 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
237 | 237 | */ |
238 | - $x0 ^= self::load_4(self::substr($message, 0, 4)); |
|
239 | - $x1 ^= self::load_4(self::substr($message, 4, 4)); |
|
240 | - $x2 ^= self::load_4(self::substr($message, 8, 4)); |
|
241 | - $x3 ^= self::load_4(self::substr($message, 12, 4)); |
|
242 | - $x4 ^= self::load_4(self::substr($message, 16, 4)); |
|
243 | - $x5 ^= self::load_4(self::substr($message, 20, 4)); |
|
244 | - $x6 ^= self::load_4(self::substr($message, 24, 4)); |
|
245 | - $x7 ^= self::load_4(self::substr($message, 28, 4)); |
|
246 | - $x8 ^= self::load_4(self::substr($message, 32, 4)); |
|
247 | - $x9 ^= self::load_4(self::substr($message, 36, 4)); |
|
248 | - $x10 ^= self::load_4(self::substr($message, 40, 4)); |
|
249 | - $x11 ^= self::load_4(self::substr($message, 44, 4)); |
|
250 | - $x12 ^= self::load_4(self::substr($message, 48, 4)); |
|
251 | - $x13 ^= self::load_4(self::substr($message, 52, 4)); |
|
252 | - $x14 ^= self::load_4(self::substr($message, 56, 4)); |
|
253 | - $x15 ^= self::load_4(self::substr($message, 60, 4)); |
|
238 | + $x0 ^= self::load_4(self::substr($message, 0, 4)); |
|
239 | + $x1 ^= self::load_4(self::substr($message, 4, 4)); |
|
240 | + $x2 ^= self::load_4(self::substr($message, 8, 4)); |
|
241 | + $x3 ^= self::load_4(self::substr($message, 12, 4)); |
|
242 | + $x4 ^= self::load_4(self::substr($message, 16, 4)); |
|
243 | + $x5 ^= self::load_4(self::substr($message, 20, 4)); |
|
244 | + $x6 ^= self::load_4(self::substr($message, 24, 4)); |
|
245 | + $x7 ^= self::load_4(self::substr($message, 28, 4)); |
|
246 | + $x8 ^= self::load_4(self::substr($message, 32, 4)); |
|
247 | + $x9 ^= self::load_4(self::substr($message, 36, 4)); |
|
248 | + $x10 ^= self::load_4(self::substr($message, 40, 4)); |
|
249 | + $x11 ^= self::load_4(self::substr($message, 44, 4)); |
|
250 | + $x12 ^= self::load_4(self::substr($message, 48, 4)); |
|
251 | + $x13 ^= self::load_4(self::substr($message, 52, 4)); |
|
252 | + $x14 ^= self::load_4(self::substr($message, 56, 4)); |
|
253 | + $x15 ^= self::load_4(self::substr($message, 60, 4)); |
|
254 | 254 | |
255 | - /* |
|
255 | + /* |
|
256 | 256 | j12 = PLUSONE(j12); |
257 | 257 | if (!j12) { |
258 | 258 | j13 = PLUSONE(j13); |
259 | 259 | } |
260 | 260 | */ |
261 | - ++$j12; |
|
262 | - if ($j12 & 0xf0000000) { |
|
263 | - throw new SodiumException('Overflow'); |
|
264 | - } |
|
261 | + ++$j12; |
|
262 | + if ($j12 & 0xf0000000) { |
|
263 | + throw new SodiumException('Overflow'); |
|
264 | + } |
|
265 | 265 | |
266 | - /* |
|
266 | + /* |
|
267 | 267 | STORE32_LE(c + 0, x0); |
268 | 268 | STORE32_LE(c + 4, x1); |
269 | 269 | STORE32_LE(c + 8, x2); |
@@ -281,115 +281,115 @@ discard block |
||
281 | 281 | STORE32_LE(c + 56, x14); |
282 | 282 | STORE32_LE(c + 60, x15); |
283 | 283 | */ |
284 | - $block = self::store32_le((int) ($x0 & 0xffffffff)) . |
|
285 | - self::store32_le((int) ($x1 & 0xffffffff)) . |
|
286 | - self::store32_le((int) ($x2 & 0xffffffff)) . |
|
287 | - self::store32_le((int) ($x3 & 0xffffffff)) . |
|
288 | - self::store32_le((int) ($x4 & 0xffffffff)) . |
|
289 | - self::store32_le((int) ($x5 & 0xffffffff)) . |
|
290 | - self::store32_le((int) ($x6 & 0xffffffff)) . |
|
291 | - self::store32_le((int) ($x7 & 0xffffffff)) . |
|
292 | - self::store32_le((int) ($x8 & 0xffffffff)) . |
|
293 | - self::store32_le((int) ($x9 & 0xffffffff)) . |
|
294 | - self::store32_le((int) ($x10 & 0xffffffff)) . |
|
295 | - self::store32_le((int) ($x11 & 0xffffffff)) . |
|
296 | - self::store32_le((int) ($x12 & 0xffffffff)) . |
|
297 | - self::store32_le((int) ($x13 & 0xffffffff)) . |
|
298 | - self::store32_le((int) ($x14 & 0xffffffff)) . |
|
299 | - self::store32_le((int) ($x15 & 0xffffffff)); |
|
284 | + $block = self::store32_le((int) ($x0 & 0xffffffff)) . |
|
285 | + self::store32_le((int) ($x1 & 0xffffffff)) . |
|
286 | + self::store32_le((int) ($x2 & 0xffffffff)) . |
|
287 | + self::store32_le((int) ($x3 & 0xffffffff)) . |
|
288 | + self::store32_le((int) ($x4 & 0xffffffff)) . |
|
289 | + self::store32_le((int) ($x5 & 0xffffffff)) . |
|
290 | + self::store32_le((int) ($x6 & 0xffffffff)) . |
|
291 | + self::store32_le((int) ($x7 & 0xffffffff)) . |
|
292 | + self::store32_le((int) ($x8 & 0xffffffff)) . |
|
293 | + self::store32_le((int) ($x9 & 0xffffffff)) . |
|
294 | + self::store32_le((int) ($x10 & 0xffffffff)) . |
|
295 | + self::store32_le((int) ($x11 & 0xffffffff)) . |
|
296 | + self::store32_le((int) ($x12 & 0xffffffff)) . |
|
297 | + self::store32_le((int) ($x13 & 0xffffffff)) . |
|
298 | + self::store32_le((int) ($x14 & 0xffffffff)) . |
|
299 | + self::store32_le((int) ($x15 & 0xffffffff)); |
|
300 | 300 | |
301 | - /* Partial block */ |
|
302 | - if ($bytes < 64) { |
|
303 | - $c .= self::substr($block, 0, $bytes); |
|
304 | - break; |
|
305 | - } |
|
301 | + /* Partial block */ |
|
302 | + if ($bytes < 64) { |
|
303 | + $c .= self::substr($block, 0, $bytes); |
|
304 | + break; |
|
305 | + } |
|
306 | 306 | |
307 | - /* Full block */ |
|
308 | - $c .= $block; |
|
309 | - $bytes -= 64; |
|
310 | - if ($bytes <= 0) { |
|
311 | - break; |
|
312 | - } |
|
313 | - $message = self::substr($message, 64); |
|
314 | - } |
|
315 | - /* end for(;;) loop */ |
|
307 | + /* Full block */ |
|
308 | + $c .= $block; |
|
309 | + $bytes -= 64; |
|
310 | + if ($bytes <= 0) { |
|
311 | + break; |
|
312 | + } |
|
313 | + $message = self::substr($message, 64); |
|
314 | + } |
|
315 | + /* end for(;;) loop */ |
|
316 | 316 | |
317 | - $ctx[12] = $j12; |
|
318 | - $ctx[13] = $j13; |
|
319 | - return $c; |
|
320 | - } |
|
317 | + $ctx[12] = $j12; |
|
318 | + $ctx[13] = $j13; |
|
319 | + return $c; |
|
320 | + } |
|
321 | 321 | |
322 | - /** |
|
323 | - * @internal You should not use this directly from another application |
|
324 | - * |
|
325 | - * @param int $len |
|
326 | - * @param string $nonce |
|
327 | - * @param string $key |
|
328 | - * @return string |
|
329 | - * @throws SodiumException |
|
330 | - * @throws TypeError |
|
331 | - */ |
|
332 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
333 | - { |
|
334 | - return self::encryptBytes( |
|
335 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), |
|
336 | - str_repeat("\x00", $len) |
|
337 | - ); |
|
338 | - } |
|
322 | + /** |
|
323 | + * @internal You should not use this directly from another application |
|
324 | + * |
|
325 | + * @param int $len |
|
326 | + * @param string $nonce |
|
327 | + * @param string $key |
|
328 | + * @return string |
|
329 | + * @throws SodiumException |
|
330 | + * @throws TypeError |
|
331 | + */ |
|
332 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
333 | + { |
|
334 | + return self::encryptBytes( |
|
335 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), |
|
336 | + str_repeat("\x00", $len) |
|
337 | + ); |
|
338 | + } |
|
339 | 339 | |
340 | - /** |
|
341 | - * @internal You should not use this directly from another application |
|
342 | - * |
|
343 | - * @param int $len |
|
344 | - * @param string $nonce |
|
345 | - * @param string $key |
|
346 | - * @return string |
|
347 | - * @throws SodiumException |
|
348 | - * @throws TypeError |
|
349 | - */ |
|
350 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
351 | - { |
|
352 | - return self::encryptBytes( |
|
353 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), |
|
354 | - str_repeat("\x00", $len) |
|
355 | - ); |
|
356 | - } |
|
340 | + /** |
|
341 | + * @internal You should not use this directly from another application |
|
342 | + * |
|
343 | + * @param int $len |
|
344 | + * @param string $nonce |
|
345 | + * @param string $key |
|
346 | + * @return string |
|
347 | + * @throws SodiumException |
|
348 | + * @throws TypeError |
|
349 | + */ |
|
350 | + public static function ietfStream($len, $nonce = '', $key = '') |
|
351 | + { |
|
352 | + return self::encryptBytes( |
|
353 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), |
|
354 | + str_repeat("\x00", $len) |
|
355 | + ); |
|
356 | + } |
|
357 | 357 | |
358 | - /** |
|
359 | - * @internal You should not use this directly from another application |
|
360 | - * |
|
361 | - * @param string $message |
|
362 | - * @param string $nonce |
|
363 | - * @param string $key |
|
364 | - * @param string $ic |
|
365 | - * @return string |
|
366 | - * @throws SodiumException |
|
367 | - * @throws TypeError |
|
368 | - */ |
|
369 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
370 | - { |
|
371 | - return self::encryptBytes( |
|
372 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
373 | - $message |
|
374 | - ); |
|
375 | - } |
|
358 | + /** |
|
359 | + * @internal You should not use this directly from another application |
|
360 | + * |
|
361 | + * @param string $message |
|
362 | + * @param string $nonce |
|
363 | + * @param string $key |
|
364 | + * @param string $ic |
|
365 | + * @return string |
|
366 | + * @throws SodiumException |
|
367 | + * @throws TypeError |
|
368 | + */ |
|
369 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
370 | + { |
|
371 | + return self::encryptBytes( |
|
372 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
373 | + $message |
|
374 | + ); |
|
375 | + } |
|
376 | 376 | |
377 | - /** |
|
378 | - * @internal You should not use this directly from another application |
|
379 | - * |
|
380 | - * @param string $message |
|
381 | - * @param string $nonce |
|
382 | - * @param string $key |
|
383 | - * @param string $ic |
|
384 | - * @return string |
|
385 | - * @throws SodiumException |
|
386 | - * @throws TypeError |
|
387 | - */ |
|
388 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
389 | - { |
|
390 | - return self::encryptBytes( |
|
391 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), |
|
392 | - $message |
|
393 | - ); |
|
394 | - } |
|
377 | + /** |
|
378 | + * @internal You should not use this directly from another application |
|
379 | + * |
|
380 | + * @param string $message |
|
381 | + * @param string $nonce |
|
382 | + * @param string $key |
|
383 | + * @param string $ic |
|
384 | + * @return string |
|
385 | + * @throws SodiumException |
|
386 | + * @throws TypeError |
|
387 | + */ |
|
388 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
389 | + { |
|
390 | + return self::encryptBytes( |
|
391 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), |
|
392 | + $message |
|
393 | + ); |
|
394 | + } |
|
395 | 395 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,100 +9,100 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20 |
11 | 11 | { |
12 | - /** |
|
13 | - * @param string $in |
|
14 | - * @param string $key |
|
15 | - * @param string|null $c |
|
16 | - * @return string |
|
17 | - * @throws TypeError |
|
18 | - */ |
|
19 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
20 | - { |
|
21 | - $ctx = array(); |
|
12 | + /** |
|
13 | + * @param string $in |
|
14 | + * @param string $key |
|
15 | + * @param string|null $c |
|
16 | + * @return string |
|
17 | + * @throws TypeError |
|
18 | + */ |
|
19 | + public static function hChaCha20($in = '', $key = '', $c = null) |
|
20 | + { |
|
21 | + $ctx = array(); |
|
22 | 22 | |
23 | - if ($c === null) { |
|
24 | - $ctx[0] = 0x61707865; |
|
25 | - $ctx[1] = 0x3320646e; |
|
26 | - $ctx[2] = 0x79622d32; |
|
27 | - $ctx[3] = 0x6b206574; |
|
28 | - } else { |
|
29 | - $ctx[0] = self::load_4(self::substr($c, 0, 4)); |
|
30 | - $ctx[1] = self::load_4(self::substr($c, 4, 4)); |
|
31 | - $ctx[2] = self::load_4(self::substr($c, 8, 4)); |
|
32 | - $ctx[3] = self::load_4(self::substr($c, 12, 4)); |
|
33 | - } |
|
34 | - $ctx[4] = self::load_4(self::substr($key, 0, 4)); |
|
35 | - $ctx[5] = self::load_4(self::substr($key, 4, 4)); |
|
36 | - $ctx[6] = self::load_4(self::substr($key, 8, 4)); |
|
37 | - $ctx[7] = self::load_4(self::substr($key, 12, 4)); |
|
38 | - $ctx[8] = self::load_4(self::substr($key, 16, 4)); |
|
39 | - $ctx[9] = self::load_4(self::substr($key, 20, 4)); |
|
40 | - $ctx[10] = self::load_4(self::substr($key, 24, 4)); |
|
41 | - $ctx[11] = self::load_4(self::substr($key, 28, 4)); |
|
42 | - $ctx[12] = self::load_4(self::substr($in, 0, 4)); |
|
43 | - $ctx[13] = self::load_4(self::substr($in, 4, 4)); |
|
44 | - $ctx[14] = self::load_4(self::substr($in, 8, 4)); |
|
45 | - $ctx[15] = self::load_4(self::substr($in, 12, 4)); |
|
46 | - return self::hChaCha20Bytes($ctx); |
|
47 | - } |
|
23 | + if ($c === null) { |
|
24 | + $ctx[0] = 0x61707865; |
|
25 | + $ctx[1] = 0x3320646e; |
|
26 | + $ctx[2] = 0x79622d32; |
|
27 | + $ctx[3] = 0x6b206574; |
|
28 | + } else { |
|
29 | + $ctx[0] = self::load_4(self::substr($c, 0, 4)); |
|
30 | + $ctx[1] = self::load_4(self::substr($c, 4, 4)); |
|
31 | + $ctx[2] = self::load_4(self::substr($c, 8, 4)); |
|
32 | + $ctx[3] = self::load_4(self::substr($c, 12, 4)); |
|
33 | + } |
|
34 | + $ctx[4] = self::load_4(self::substr($key, 0, 4)); |
|
35 | + $ctx[5] = self::load_4(self::substr($key, 4, 4)); |
|
36 | + $ctx[6] = self::load_4(self::substr($key, 8, 4)); |
|
37 | + $ctx[7] = self::load_4(self::substr($key, 12, 4)); |
|
38 | + $ctx[8] = self::load_4(self::substr($key, 16, 4)); |
|
39 | + $ctx[9] = self::load_4(self::substr($key, 20, 4)); |
|
40 | + $ctx[10] = self::load_4(self::substr($key, 24, 4)); |
|
41 | + $ctx[11] = self::load_4(self::substr($key, 28, 4)); |
|
42 | + $ctx[12] = self::load_4(self::substr($in, 0, 4)); |
|
43 | + $ctx[13] = self::load_4(self::substr($in, 4, 4)); |
|
44 | + $ctx[14] = self::load_4(self::substr($in, 8, 4)); |
|
45 | + $ctx[15] = self::load_4(self::substr($in, 12, 4)); |
|
46 | + return self::hChaCha20Bytes($ctx); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param array $ctx |
|
51 | - * @return string |
|
52 | - * @throws TypeError |
|
53 | - */ |
|
54 | - protected static function hChaCha20Bytes(array $ctx) |
|
55 | - { |
|
56 | - $x0 = (int) $ctx[0]; |
|
57 | - $x1 = (int) $ctx[1]; |
|
58 | - $x2 = (int) $ctx[2]; |
|
59 | - $x3 = (int) $ctx[3]; |
|
60 | - $x4 = (int) $ctx[4]; |
|
61 | - $x5 = (int) $ctx[5]; |
|
62 | - $x6 = (int) $ctx[6]; |
|
63 | - $x7 = (int) $ctx[7]; |
|
64 | - $x8 = (int) $ctx[8]; |
|
65 | - $x9 = (int) $ctx[9]; |
|
66 | - $x10 = (int) $ctx[10]; |
|
67 | - $x11 = (int) $ctx[11]; |
|
68 | - $x12 = (int) $ctx[12]; |
|
69 | - $x13 = (int) $ctx[13]; |
|
70 | - $x14 = (int) $ctx[14]; |
|
71 | - $x15 = (int) $ctx[15]; |
|
49 | + /** |
|
50 | + * @param array $ctx |
|
51 | + * @return string |
|
52 | + * @throws TypeError |
|
53 | + */ |
|
54 | + protected static function hChaCha20Bytes(array $ctx) |
|
55 | + { |
|
56 | + $x0 = (int) $ctx[0]; |
|
57 | + $x1 = (int) $ctx[1]; |
|
58 | + $x2 = (int) $ctx[2]; |
|
59 | + $x3 = (int) $ctx[3]; |
|
60 | + $x4 = (int) $ctx[4]; |
|
61 | + $x5 = (int) $ctx[5]; |
|
62 | + $x6 = (int) $ctx[6]; |
|
63 | + $x7 = (int) $ctx[7]; |
|
64 | + $x8 = (int) $ctx[8]; |
|
65 | + $x9 = (int) $ctx[9]; |
|
66 | + $x10 = (int) $ctx[10]; |
|
67 | + $x11 = (int) $ctx[11]; |
|
68 | + $x12 = (int) $ctx[12]; |
|
69 | + $x13 = (int) $ctx[13]; |
|
70 | + $x14 = (int) $ctx[14]; |
|
71 | + $x15 = (int) $ctx[15]; |
|
72 | 72 | |
73 | - for ($i = 0; $i < 10; ++$i) { |
|
74 | - # QUARTERROUND( x0, x4, x8, x12) |
|
75 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
73 | + for ($i = 0; $i < 10; ++$i) { |
|
74 | + # QUARTERROUND( x0, x4, x8, x12) |
|
75 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
76 | 76 | |
77 | - # QUARTERROUND( x1, x5, x9, x13) |
|
78 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
77 | + # QUARTERROUND( x1, x5, x9, x13) |
|
78 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
79 | 79 | |
80 | - # QUARTERROUND( x2, x6, x10, x14) |
|
81 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
80 | + # QUARTERROUND( x2, x6, x10, x14) |
|
81 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
82 | 82 | |
83 | - # QUARTERROUND( x3, x7, x11, x15) |
|
84 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
83 | + # QUARTERROUND( x3, x7, x11, x15) |
|
84 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
85 | 85 | |
86 | - # QUARTERROUND( x0, x5, x10, x15) |
|
87 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
86 | + # QUARTERROUND( x0, x5, x10, x15) |
|
87 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
88 | 88 | |
89 | - # QUARTERROUND( x1, x6, x11, x12) |
|
90 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
89 | + # QUARTERROUND( x1, x6, x11, x12) |
|
90 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
91 | 91 | |
92 | - # QUARTERROUND( x2, x7, x8, x13) |
|
93 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
92 | + # QUARTERROUND( x2, x7, x8, x13) |
|
93 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
94 | 94 | |
95 | - # QUARTERROUND( x3, x4, x9, x14) |
|
96 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
97 | - } |
|
95 | + # QUARTERROUND( x3, x4, x9, x14) |
|
96 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
97 | + } |
|
98 | 98 | |
99 | - return self::store32_le((int) ($x0 & 0xffffffff)) . |
|
100 | - self::store32_le((int) ($x1 & 0xffffffff)) . |
|
101 | - self::store32_le((int) ($x2 & 0xffffffff)) . |
|
102 | - self::store32_le((int) ($x3 & 0xffffffff)) . |
|
103 | - self::store32_le((int) ($x12 & 0xffffffff)) . |
|
104 | - self::store32_le((int) ($x13 & 0xffffffff)) . |
|
105 | - self::store32_le((int) ($x14 & 0xffffffff)) . |
|
106 | - self::store32_le((int) ($x15 & 0xffffffff)); |
|
107 | - } |
|
99 | + return self::store32_le((int) ($x0 & 0xffffffff)) . |
|
100 | + self::store32_le((int) ($x1 & 0xffffffff)) . |
|
101 | + self::store32_le((int) ($x2 & 0xffffffff)) . |
|
102 | + self::store32_le((int) ($x3 & 0xffffffff)) . |
|
103 | + self::store32_le((int) ($x12 & 0xffffffff)) . |
|
104 | + self::store32_le((int) ($x13 & 0xffffffff)) . |
|
105 | + self::store32_le((int) ($x14 & 0xffffffff)) . |
|
106 | + self::store32_le((int) ($x15 & 0xffffffff)); |
|
107 | + } |
|
108 | 108 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,88 +9,88 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Calculate an hsalsa20 hash of a single block |
|
14 | - * |
|
15 | - * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | - * one block (used to derive a subkey for xsalsa20). |
|
17 | - * |
|
18 | - * @internal You should not use this directly from another application |
|
19 | - * |
|
20 | - * @param string $in |
|
21 | - * @param string $k |
|
22 | - * @param string|null $c |
|
23 | - * @return string |
|
24 | - * @throws TypeError |
|
25 | - */ |
|
26 | - public static function hsalsa20($in, $k, $c = null) |
|
27 | - { |
|
28 | - if ($c === null) { |
|
29 | - $x0 = 0x61707865; |
|
30 | - $x5 = 0x3320646e; |
|
31 | - $x10 = 0x79622d32; |
|
32 | - $x15 = 0x6b206574; |
|
33 | - } else { |
|
34 | - $x0 = self::load_4(self::substr($c, 0, 4)); |
|
35 | - $x5 = self::load_4(self::substr($c, 4, 4)); |
|
36 | - $x10 = self::load_4(self::substr($c, 8, 4)); |
|
37 | - $x15 = self::load_4(self::substr($c, 12, 4)); |
|
38 | - } |
|
39 | - $x1 = self::load_4(self::substr($k, 0, 4)); |
|
40 | - $x2 = self::load_4(self::substr($k, 4, 4)); |
|
41 | - $x3 = self::load_4(self::substr($k, 8, 4)); |
|
42 | - $x4 = self::load_4(self::substr($k, 12, 4)); |
|
43 | - $x11 = self::load_4(self::substr($k, 16, 4)); |
|
44 | - $x12 = self::load_4(self::substr($k, 20, 4)); |
|
45 | - $x13 = self::load_4(self::substr($k, 24, 4)); |
|
46 | - $x14 = self::load_4(self::substr($k, 28, 4)); |
|
47 | - $x6 = self::load_4(self::substr($in, 0, 4)); |
|
48 | - $x7 = self::load_4(self::substr($in, 4, 4)); |
|
49 | - $x8 = self::load_4(self::substr($in, 8, 4)); |
|
50 | - $x9 = self::load_4(self::substr($in, 12, 4)); |
|
12 | + /** |
|
13 | + * Calculate an hsalsa20 hash of a single block |
|
14 | + * |
|
15 | + * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | + * one block (used to derive a subkey for xsalsa20). |
|
17 | + * |
|
18 | + * @internal You should not use this directly from another application |
|
19 | + * |
|
20 | + * @param string $in |
|
21 | + * @param string $k |
|
22 | + * @param string|null $c |
|
23 | + * @return string |
|
24 | + * @throws TypeError |
|
25 | + */ |
|
26 | + public static function hsalsa20($in, $k, $c = null) |
|
27 | + { |
|
28 | + if ($c === null) { |
|
29 | + $x0 = 0x61707865; |
|
30 | + $x5 = 0x3320646e; |
|
31 | + $x10 = 0x79622d32; |
|
32 | + $x15 = 0x6b206574; |
|
33 | + } else { |
|
34 | + $x0 = self::load_4(self::substr($c, 0, 4)); |
|
35 | + $x5 = self::load_4(self::substr($c, 4, 4)); |
|
36 | + $x10 = self::load_4(self::substr($c, 8, 4)); |
|
37 | + $x15 = self::load_4(self::substr($c, 12, 4)); |
|
38 | + } |
|
39 | + $x1 = self::load_4(self::substr($k, 0, 4)); |
|
40 | + $x2 = self::load_4(self::substr($k, 4, 4)); |
|
41 | + $x3 = self::load_4(self::substr($k, 8, 4)); |
|
42 | + $x4 = self::load_4(self::substr($k, 12, 4)); |
|
43 | + $x11 = self::load_4(self::substr($k, 16, 4)); |
|
44 | + $x12 = self::load_4(self::substr($k, 20, 4)); |
|
45 | + $x13 = self::load_4(self::substr($k, 24, 4)); |
|
46 | + $x14 = self::load_4(self::substr($k, 28, 4)); |
|
47 | + $x6 = self::load_4(self::substr($in, 0, 4)); |
|
48 | + $x7 = self::load_4(self::substr($in, 4, 4)); |
|
49 | + $x8 = self::load_4(self::substr($in, 8, 4)); |
|
50 | + $x9 = self::load_4(self::substr($in, 12, 4)); |
|
51 | 51 | |
52 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
53 | - $x4 ^= self::rotate($x0 + $x12, 7); |
|
54 | - $x8 ^= self::rotate($x4 + $x0, 9); |
|
55 | - $x12 ^= self::rotate($x8 + $x4, 13); |
|
56 | - $x0 ^= self::rotate($x12 + $x8, 18); |
|
57 | - $x9 ^= self::rotate($x5 + $x1, 7); |
|
58 | - $x13 ^= self::rotate($x9 + $x5, 9); |
|
59 | - $x1 ^= self::rotate($x13 + $x9, 13); |
|
60 | - $x5 ^= self::rotate($x1 + $x13, 18); |
|
61 | - $x14 ^= self::rotate($x10 + $x6, 7); |
|
62 | - $x2 ^= self::rotate($x14 + $x10, 9); |
|
63 | - $x6 ^= self::rotate($x2 + $x14, 13); |
|
64 | - $x10 ^= self::rotate($x6 + $x2, 18); |
|
65 | - $x3 ^= self::rotate($x15 + $x11, 7); |
|
66 | - $x7 ^= self::rotate($x3 + $x15, 9); |
|
67 | - $x11 ^= self::rotate($x7 + $x3, 13); |
|
68 | - $x15 ^= self::rotate($x11 + $x7, 18); |
|
69 | - $x1 ^= self::rotate($x0 + $x3, 7); |
|
70 | - $x2 ^= self::rotate($x1 + $x0, 9); |
|
71 | - $x3 ^= self::rotate($x2 + $x1, 13); |
|
72 | - $x0 ^= self::rotate($x3 + $x2, 18); |
|
73 | - $x6 ^= self::rotate($x5 + $x4, 7); |
|
74 | - $x7 ^= self::rotate($x6 + $x5, 9); |
|
75 | - $x4 ^= self::rotate($x7 + $x6, 13); |
|
76 | - $x5 ^= self::rotate($x4 + $x7, 18); |
|
77 | - $x11 ^= self::rotate($x10 + $x9, 7); |
|
78 | - $x8 ^= self::rotate($x11 + $x10, 9); |
|
79 | - $x9 ^= self::rotate($x8 + $x11, 13); |
|
80 | - $x10 ^= self::rotate($x9 + $x8, 18); |
|
81 | - $x12 ^= self::rotate($x15 + $x14, 7); |
|
82 | - $x13 ^= self::rotate($x12 + $x15, 9); |
|
83 | - $x14 ^= self::rotate($x13 + $x12, 13); |
|
84 | - $x15 ^= self::rotate($x14 + $x13, 18); |
|
85 | - } |
|
52 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
53 | + $x4 ^= self::rotate($x0 + $x12, 7); |
|
54 | + $x8 ^= self::rotate($x4 + $x0, 9); |
|
55 | + $x12 ^= self::rotate($x8 + $x4, 13); |
|
56 | + $x0 ^= self::rotate($x12 + $x8, 18); |
|
57 | + $x9 ^= self::rotate($x5 + $x1, 7); |
|
58 | + $x13 ^= self::rotate($x9 + $x5, 9); |
|
59 | + $x1 ^= self::rotate($x13 + $x9, 13); |
|
60 | + $x5 ^= self::rotate($x1 + $x13, 18); |
|
61 | + $x14 ^= self::rotate($x10 + $x6, 7); |
|
62 | + $x2 ^= self::rotate($x14 + $x10, 9); |
|
63 | + $x6 ^= self::rotate($x2 + $x14, 13); |
|
64 | + $x10 ^= self::rotate($x6 + $x2, 18); |
|
65 | + $x3 ^= self::rotate($x15 + $x11, 7); |
|
66 | + $x7 ^= self::rotate($x3 + $x15, 9); |
|
67 | + $x11 ^= self::rotate($x7 + $x3, 13); |
|
68 | + $x15 ^= self::rotate($x11 + $x7, 18); |
|
69 | + $x1 ^= self::rotate($x0 + $x3, 7); |
|
70 | + $x2 ^= self::rotate($x1 + $x0, 9); |
|
71 | + $x3 ^= self::rotate($x2 + $x1, 13); |
|
72 | + $x0 ^= self::rotate($x3 + $x2, 18); |
|
73 | + $x6 ^= self::rotate($x5 + $x4, 7); |
|
74 | + $x7 ^= self::rotate($x6 + $x5, 9); |
|
75 | + $x4 ^= self::rotate($x7 + $x6, 13); |
|
76 | + $x5 ^= self::rotate($x4 + $x7, 18); |
|
77 | + $x11 ^= self::rotate($x10 + $x9, 7); |
|
78 | + $x8 ^= self::rotate($x11 + $x10, 9); |
|
79 | + $x9 ^= self::rotate($x8 + $x11, 13); |
|
80 | + $x10 ^= self::rotate($x9 + $x8, 18); |
|
81 | + $x12 ^= self::rotate($x15 + $x14, 7); |
|
82 | + $x13 ^= self::rotate($x12 + $x15, 9); |
|
83 | + $x14 ^= self::rotate($x13 + $x12, 13); |
|
84 | + $x15 ^= self::rotate($x14 + $x13, 18); |
|
85 | + } |
|
86 | 86 | |
87 | - return self::store32_le($x0) . |
|
88 | - self::store32_le($x5) . |
|
89 | - self::store32_le($x10) . |
|
90 | - self::store32_le($x15) . |
|
91 | - self::store32_le($x6) . |
|
92 | - self::store32_le($x7) . |
|
93 | - self::store32_le($x8) . |
|
94 | - self::store32_le($x9); |
|
95 | - } |
|
87 | + return self::store32_le($x0) . |
|
88 | + self::store32_le($x5) . |
|
89 | + self::store32_le($x10) . |
|
90 | + self::store32_le($x15) . |
|
91 | + self::store32_le($x6) . |
|
92 | + self::store32_le($x7) . |
|
93 | + self::store32_le($x8) . |
|
94 | + self::store32_le($x9); |
|
95 | + } |
|
96 | 96 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,437 +9,437 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array<int, int> |
|
14 | - */ |
|
15 | - protected $buffer = array(); |
|
16 | - |
|
17 | - /** |
|
18 | - * @var bool |
|
19 | - */ |
|
20 | - protected $final = false; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var array<int, int> |
|
24 | - */ |
|
25 | - public $h; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - protected $leftover = 0; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var int[] |
|
34 | - */ |
|
35 | - public $r; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var int[] |
|
39 | - */ |
|
40 | - public $pad; |
|
41 | - |
|
42 | - /** |
|
43 | - * ParagonIE_Sodium_Core_Poly1305_State constructor. |
|
44 | - * |
|
45 | - * @internal You should not use this directly from another application |
|
46 | - * |
|
47 | - * @param string $key |
|
48 | - * @throws InvalidArgumentException |
|
49 | - * @throws TypeError |
|
50 | - */ |
|
51 | - public function __construct($key = '') |
|
52 | - { |
|
53 | - if (self::strlen($key) < 32) { |
|
54 | - throw new InvalidArgumentException( |
|
55 | - 'Poly1305 requires a 32-byte key' |
|
56 | - ); |
|
57 | - } |
|
58 | - /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
59 | - $this->r = array( |
|
60 | - (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), |
|
61 | - (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), |
|
62 | - (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), |
|
63 | - (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), |
|
64 | - (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) |
|
65 | - ); |
|
66 | - |
|
67 | - /* h = 0 */ |
|
68 | - $this->h = array(0, 0, 0, 0, 0); |
|
69 | - |
|
70 | - /* save pad for later */ |
|
71 | - $this->pad = array( |
|
72 | - self::load_4(self::substr($key, 16, 4)), |
|
73 | - self::load_4(self::substr($key, 20, 4)), |
|
74 | - self::load_4(self::substr($key, 24, 4)), |
|
75 | - self::load_4(self::substr($key, 28, 4)), |
|
76 | - ); |
|
77 | - |
|
78 | - $this->leftover = 0; |
|
79 | - $this->final = false; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Zero internal buffer upon destruction |
|
84 | - */ |
|
85 | - public function __destruct() |
|
86 | - { |
|
87 | - $this->r[0] ^= $this->r[0]; |
|
88 | - $this->r[1] ^= $this->r[1]; |
|
89 | - $this->r[2] ^= $this->r[2]; |
|
90 | - $this->r[3] ^= $this->r[3]; |
|
91 | - $this->r[4] ^= $this->r[4]; |
|
92 | - $this->h[0] ^= $this->h[0]; |
|
93 | - $this->h[1] ^= $this->h[1]; |
|
94 | - $this->h[2] ^= $this->h[2]; |
|
95 | - $this->h[3] ^= $this->h[3]; |
|
96 | - $this->h[4] ^= $this->h[4]; |
|
97 | - $this->pad[0] ^= $this->pad[0]; |
|
98 | - $this->pad[1] ^= $this->pad[1]; |
|
99 | - $this->pad[2] ^= $this->pad[2]; |
|
100 | - $this->pad[3] ^= $this->pad[3]; |
|
101 | - $this->leftover = 0; |
|
102 | - $this->final = true; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @internal You should not use this directly from another application |
|
107 | - * |
|
108 | - * @param string $message |
|
109 | - * @return self |
|
110 | - * @throws SodiumException |
|
111 | - * @throws TypeError |
|
112 | - */ |
|
113 | - public function update($message = '') |
|
114 | - { |
|
115 | - $bytes = self::strlen($message); |
|
116 | - if ($bytes < 1) { |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - /* handle leftover */ |
|
121 | - if ($this->leftover) { |
|
122 | - $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
|
123 | - if ($want > $bytes) { |
|
124 | - $want = $bytes; |
|
125 | - } |
|
126 | - for ($i = 0; $i < $want; ++$i) { |
|
127 | - $mi = self::chrToInt($message[$i]); |
|
128 | - $this->buffer[$this->leftover + $i] = $mi; |
|
129 | - } |
|
130 | - // We snip off the leftmost bytes. |
|
131 | - $message = self::substr($message, $want); |
|
132 | - $bytes = self::strlen($message); |
|
133 | - $this->leftover += $want; |
|
134 | - if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
135 | - // We still don't have enough to run $this->blocks() |
|
136 | - return $this; |
|
137 | - } |
|
138 | - |
|
139 | - $this->blocks( |
|
140 | - self::intArrayToString($this->buffer), |
|
141 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
142 | - ); |
|
143 | - $this->leftover = 0; |
|
144 | - } |
|
145 | - |
|
146 | - /* process full blocks */ |
|
147 | - if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
148 | - /** @var int $want */ |
|
149 | - $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); |
|
150 | - if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
151 | - $block = self::substr($message, 0, $want); |
|
152 | - if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
153 | - $this->blocks($block, $want); |
|
154 | - $message = self::substr($message, $want); |
|
155 | - $bytes = self::strlen($message); |
|
156 | - } |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /* store leftover */ |
|
161 | - if ($bytes) { |
|
162 | - for ($i = 0; $i < $bytes; ++$i) { |
|
163 | - $mi = self::chrToInt($message[$i]); |
|
164 | - $this->buffer[$this->leftover + $i] = $mi; |
|
165 | - } |
|
166 | - $this->leftover = (int) $this->leftover + $bytes; |
|
167 | - } |
|
168 | - return $this; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @internal You should not use this directly from another application |
|
173 | - * |
|
174 | - * @param string $message |
|
175 | - * @param int $bytes |
|
176 | - * @return self |
|
177 | - * @throws TypeError |
|
178 | - */ |
|
179 | - public function blocks($message, $bytes) |
|
180 | - { |
|
181 | - if (self::strlen($message) < 16) { |
|
182 | - $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
183 | - } |
|
184 | - /** @var int $hibit */ |
|
185 | - $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ |
|
186 | - $r0 = (int) $this->r[0]; |
|
187 | - $r1 = (int) $this->r[1]; |
|
188 | - $r2 = (int) $this->r[2]; |
|
189 | - $r3 = (int) $this->r[3]; |
|
190 | - $r4 = (int) $this->r[4]; |
|
191 | - |
|
192 | - $s1 = self::mul($r1, 5, 3); |
|
193 | - $s2 = self::mul($r2, 5, 3); |
|
194 | - $s3 = self::mul($r3, 5, 3); |
|
195 | - $s4 = self::mul($r4, 5, 3); |
|
196 | - |
|
197 | - $h0 = $this->h[0]; |
|
198 | - $h1 = $this->h[1]; |
|
199 | - $h2 = $this->h[2]; |
|
200 | - $h3 = $this->h[3]; |
|
201 | - $h4 = $this->h[4]; |
|
202 | - |
|
203 | - while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
204 | - /* h += m[i] */ |
|
205 | - $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; |
|
206 | - $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; |
|
207 | - $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; |
|
208 | - $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; |
|
209 | - $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; |
|
210 | - |
|
211 | - /* h *= r */ |
|
212 | - $d0 = ( |
|
213 | - self::mul($h0, $r0, 25) + |
|
214 | - self::mul($s4, $h1, 26) + |
|
215 | - self::mul($s3, $h2, 26) + |
|
216 | - self::mul($s2, $h3, 26) + |
|
217 | - self::mul($s1, $h4, 26) |
|
218 | - ); |
|
219 | - |
|
220 | - $d1 = ( |
|
221 | - self::mul($h0, $r1, 25) + |
|
222 | - self::mul($h1, $r0, 25) + |
|
223 | - self::mul($s4, $h2, 26) + |
|
224 | - self::mul($s3, $h3, 26) + |
|
225 | - self::mul($s2, $h4, 26) |
|
226 | - ); |
|
227 | - |
|
228 | - $d2 = ( |
|
229 | - self::mul($h0, $r2, 25) + |
|
230 | - self::mul($h1, $r1, 25) + |
|
231 | - self::mul($h2, $r0, 25) + |
|
232 | - self::mul($s4, $h3, 26) + |
|
233 | - self::mul($s3, $h4, 26) |
|
234 | - ); |
|
235 | - |
|
236 | - $d3 = ( |
|
237 | - self::mul($h0, $r3, 25) + |
|
238 | - self::mul($h1, $r2, 25) + |
|
239 | - self::mul($h2, $r1, 25) + |
|
240 | - self::mul($h3, $r0, 25) + |
|
241 | - self::mul($s4, $h4, 26) |
|
242 | - ); |
|
243 | - |
|
244 | - $d4 = ( |
|
245 | - self::mul($h0, $r4, 25) + |
|
246 | - self::mul($h1, $r3, 25) + |
|
247 | - self::mul($h2, $r2, 25) + |
|
248 | - self::mul($h3, $r1, 25) + |
|
249 | - self::mul($h4, $r0, 25) |
|
250 | - ); |
|
251 | - |
|
252 | - /* (partial) h %= p */ |
|
253 | - /** @var int $c */ |
|
254 | - $c = $d0 >> 26; |
|
255 | - /** @var int $h0 */ |
|
256 | - $h0 = $d0 & 0x3ffffff; |
|
257 | - $d1 += $c; |
|
258 | - |
|
259 | - /** @var int $c */ |
|
260 | - $c = $d1 >> 26; |
|
261 | - /** @var int $h1 */ |
|
262 | - $h1 = $d1 & 0x3ffffff; |
|
263 | - $d2 += $c; |
|
264 | - |
|
265 | - /** @var int $c */ |
|
266 | - $c = $d2 >> 26; |
|
267 | - /** @var int $h2 */ |
|
268 | - $h2 = $d2 & 0x3ffffff; |
|
269 | - $d3 += $c; |
|
270 | - |
|
271 | - /** @var int $c */ |
|
272 | - $c = $d3 >> 26; |
|
273 | - /** @var int $h3 */ |
|
274 | - $h3 = $d3 & 0x3ffffff; |
|
275 | - $d4 += $c; |
|
276 | - |
|
277 | - /** @var int $c */ |
|
278 | - $c = $d4 >> 26; |
|
279 | - /** @var int $h4 */ |
|
280 | - $h4 = $d4 & 0x3ffffff; |
|
281 | - $h0 += (int) self::mul($c, 5, 3); |
|
282 | - |
|
283 | - /** @var int $c */ |
|
284 | - $c = $h0 >> 26; |
|
285 | - /** @var int $h0 */ |
|
286 | - $h0 &= 0x3ffffff; |
|
287 | - $h1 += $c; |
|
288 | - |
|
289 | - // Chop off the left 32 bytes. |
|
290 | - $message = self::substr( |
|
291 | - $message, |
|
292 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
293 | - ); |
|
294 | - $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; |
|
295 | - } |
|
296 | - |
|
297 | - $this->h = array( |
|
298 | - (int) ($h0 & 0xffffffff), |
|
299 | - (int) ($h1 & 0xffffffff), |
|
300 | - (int) ($h2 & 0xffffffff), |
|
301 | - (int) ($h3 & 0xffffffff), |
|
302 | - (int) ($h4 & 0xffffffff) |
|
303 | - ); |
|
304 | - return $this; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * @internal You should not use this directly from another application |
|
309 | - * |
|
310 | - * @return string |
|
311 | - * @throws TypeError |
|
312 | - */ |
|
313 | - public function finish() |
|
314 | - { |
|
315 | - /* process the remaining block */ |
|
316 | - if ($this->leftover) { |
|
317 | - $i = $this->leftover; |
|
318 | - $this->buffer[$i++] = 1; |
|
319 | - for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
320 | - $this->buffer[$i] = 0; |
|
321 | - } |
|
322 | - $this->final = true; |
|
323 | - $this->blocks( |
|
324 | - self::substr( |
|
325 | - self::intArrayToString($this->buffer), |
|
326 | - 0, |
|
327 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
328 | - ), |
|
329 | - ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
330 | - ); |
|
331 | - } |
|
332 | - |
|
333 | - $h0 = (int) $this->h[0]; |
|
334 | - $h1 = (int) $this->h[1]; |
|
335 | - $h2 = (int) $this->h[2]; |
|
336 | - $h3 = (int) $this->h[3]; |
|
337 | - $h4 = (int) $this->h[4]; |
|
338 | - |
|
339 | - /** @var int $c */ |
|
340 | - $c = $h1 >> 26; |
|
341 | - /** @var int $h1 */ |
|
342 | - $h1 &= 0x3ffffff; |
|
343 | - /** @var int $h2 */ |
|
344 | - $h2 += $c; |
|
345 | - /** @var int $c */ |
|
346 | - $c = $h2 >> 26; |
|
347 | - /** @var int $h2 */ |
|
348 | - $h2 &= 0x3ffffff; |
|
349 | - $h3 += $c; |
|
350 | - /** @var int $c */ |
|
351 | - $c = $h3 >> 26; |
|
352 | - $h3 &= 0x3ffffff; |
|
353 | - $h4 += $c; |
|
354 | - /** @var int $c */ |
|
355 | - $c = $h4 >> 26; |
|
356 | - $h4 &= 0x3ffffff; |
|
357 | - /** @var int $h0 */ |
|
358 | - $h0 += self::mul($c, 5, 3); |
|
359 | - /** @var int $c */ |
|
360 | - $c = $h0 >> 26; |
|
361 | - /** @var int $h0 */ |
|
362 | - $h0 &= 0x3ffffff; |
|
363 | - /** @var int $h1 */ |
|
364 | - $h1 += $c; |
|
365 | - |
|
366 | - /* compute h + -p */ |
|
367 | - /** @var int $g0 */ |
|
368 | - $g0 = $h0 + 5; |
|
369 | - /** @var int $c */ |
|
370 | - $c = $g0 >> 26; |
|
371 | - /** @var int $g0 */ |
|
372 | - $g0 &= 0x3ffffff; |
|
373 | - |
|
374 | - /** @var int $g1 */ |
|
375 | - $g1 = $h1 + $c; |
|
376 | - /** @var int $c */ |
|
377 | - $c = $g1 >> 26; |
|
378 | - $g1 &= 0x3ffffff; |
|
379 | - |
|
380 | - /** @var int $g2 */ |
|
381 | - $g2 = $h2 + $c; |
|
382 | - /** @var int $c */ |
|
383 | - $c = $g2 >> 26; |
|
384 | - /** @var int $g2 */ |
|
385 | - $g2 &= 0x3ffffff; |
|
386 | - |
|
387 | - /** @var int $g3 */ |
|
388 | - $g3 = $h3 + $c; |
|
389 | - /** @var int $c */ |
|
390 | - $c = $g3 >> 26; |
|
391 | - /** @var int $g3 */ |
|
392 | - $g3 &= 0x3ffffff; |
|
393 | - |
|
394 | - /** @var int $g4 */ |
|
395 | - $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
396 | - |
|
397 | - /* select h if h < p, or h + -p if h >= p */ |
|
398 | - /** @var int $mask */ |
|
399 | - $mask = ($g4 >> 31) - 1; |
|
400 | - |
|
401 | - $g0 &= $mask; |
|
402 | - $g1 &= $mask; |
|
403 | - $g2 &= $mask; |
|
404 | - $g3 &= $mask; |
|
405 | - $g4 &= $mask; |
|
406 | - |
|
407 | - /** @var int $mask */ |
|
408 | - $mask = ~$mask & 0xffffffff; |
|
409 | - /** @var int $h0 */ |
|
410 | - $h0 = ($h0 & $mask) | $g0; |
|
411 | - /** @var int $h1 */ |
|
412 | - $h1 = ($h1 & $mask) | $g1; |
|
413 | - /** @var int $h2 */ |
|
414 | - $h2 = ($h2 & $mask) | $g2; |
|
415 | - /** @var int $h3 */ |
|
416 | - $h3 = ($h3 & $mask) | $g3; |
|
417 | - /** @var int $h4 */ |
|
418 | - $h4 = ($h4 & $mask) | $g4; |
|
419 | - |
|
420 | - /* h = h % (2^128) */ |
|
421 | - /** @var int $h0 */ |
|
422 | - $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
423 | - /** @var int $h1 */ |
|
424 | - $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
425 | - /** @var int $h2 */ |
|
426 | - $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
427 | - /** @var int $h3 */ |
|
428 | - $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
429 | - |
|
430 | - /* mac = (h + pad) % (2^128) */ |
|
431 | - $f = (int) ($h0 + $this->pad[0]); |
|
432 | - $h0 = (int) $f; |
|
433 | - $f = (int) ($h1 + $this->pad[1] + ($f >> 32)); |
|
434 | - $h1 = (int) $f; |
|
435 | - $f = (int) ($h2 + $this->pad[2] + ($f >> 32)); |
|
436 | - $h2 = (int) $f; |
|
437 | - $f = (int) ($h3 + $this->pad[3] + ($f >> 32)); |
|
438 | - $h3 = (int) $f; |
|
439 | - |
|
440 | - return self::store32_le($h0 & 0xffffffff) . |
|
441 | - self::store32_le($h1 & 0xffffffff) . |
|
442 | - self::store32_le($h2 & 0xffffffff) . |
|
443 | - self::store32_le($h3 & 0xffffffff); |
|
444 | - } |
|
12 | + /** |
|
13 | + * @var array<int, int> |
|
14 | + */ |
|
15 | + protected $buffer = array(); |
|
16 | + |
|
17 | + /** |
|
18 | + * @var bool |
|
19 | + */ |
|
20 | + protected $final = false; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var array<int, int> |
|
24 | + */ |
|
25 | + public $h; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + protected $leftover = 0; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var int[] |
|
34 | + */ |
|
35 | + public $r; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var int[] |
|
39 | + */ |
|
40 | + public $pad; |
|
41 | + |
|
42 | + /** |
|
43 | + * ParagonIE_Sodium_Core_Poly1305_State constructor. |
|
44 | + * |
|
45 | + * @internal You should not use this directly from another application |
|
46 | + * |
|
47 | + * @param string $key |
|
48 | + * @throws InvalidArgumentException |
|
49 | + * @throws TypeError |
|
50 | + */ |
|
51 | + public function __construct($key = '') |
|
52 | + { |
|
53 | + if (self::strlen($key) < 32) { |
|
54 | + throw new InvalidArgumentException( |
|
55 | + 'Poly1305 requires a 32-byte key' |
|
56 | + ); |
|
57 | + } |
|
58 | + /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
59 | + $this->r = array( |
|
60 | + (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff), |
|
61 | + (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03), |
|
62 | + (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff), |
|
63 | + (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff), |
|
64 | + (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff) |
|
65 | + ); |
|
66 | + |
|
67 | + /* h = 0 */ |
|
68 | + $this->h = array(0, 0, 0, 0, 0); |
|
69 | + |
|
70 | + /* save pad for later */ |
|
71 | + $this->pad = array( |
|
72 | + self::load_4(self::substr($key, 16, 4)), |
|
73 | + self::load_4(self::substr($key, 20, 4)), |
|
74 | + self::load_4(self::substr($key, 24, 4)), |
|
75 | + self::load_4(self::substr($key, 28, 4)), |
|
76 | + ); |
|
77 | + |
|
78 | + $this->leftover = 0; |
|
79 | + $this->final = false; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Zero internal buffer upon destruction |
|
84 | + */ |
|
85 | + public function __destruct() |
|
86 | + { |
|
87 | + $this->r[0] ^= $this->r[0]; |
|
88 | + $this->r[1] ^= $this->r[1]; |
|
89 | + $this->r[2] ^= $this->r[2]; |
|
90 | + $this->r[3] ^= $this->r[3]; |
|
91 | + $this->r[4] ^= $this->r[4]; |
|
92 | + $this->h[0] ^= $this->h[0]; |
|
93 | + $this->h[1] ^= $this->h[1]; |
|
94 | + $this->h[2] ^= $this->h[2]; |
|
95 | + $this->h[3] ^= $this->h[3]; |
|
96 | + $this->h[4] ^= $this->h[4]; |
|
97 | + $this->pad[0] ^= $this->pad[0]; |
|
98 | + $this->pad[1] ^= $this->pad[1]; |
|
99 | + $this->pad[2] ^= $this->pad[2]; |
|
100 | + $this->pad[3] ^= $this->pad[3]; |
|
101 | + $this->leftover = 0; |
|
102 | + $this->final = true; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @internal You should not use this directly from another application |
|
107 | + * |
|
108 | + * @param string $message |
|
109 | + * @return self |
|
110 | + * @throws SodiumException |
|
111 | + * @throws TypeError |
|
112 | + */ |
|
113 | + public function update($message = '') |
|
114 | + { |
|
115 | + $bytes = self::strlen($message); |
|
116 | + if ($bytes < 1) { |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + /* handle leftover */ |
|
121 | + if ($this->leftover) { |
|
122 | + $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
|
123 | + if ($want > $bytes) { |
|
124 | + $want = $bytes; |
|
125 | + } |
|
126 | + for ($i = 0; $i < $want; ++$i) { |
|
127 | + $mi = self::chrToInt($message[$i]); |
|
128 | + $this->buffer[$this->leftover + $i] = $mi; |
|
129 | + } |
|
130 | + // We snip off the leftmost bytes. |
|
131 | + $message = self::substr($message, $want); |
|
132 | + $bytes = self::strlen($message); |
|
133 | + $this->leftover += $want; |
|
134 | + if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
135 | + // We still don't have enough to run $this->blocks() |
|
136 | + return $this; |
|
137 | + } |
|
138 | + |
|
139 | + $this->blocks( |
|
140 | + self::intArrayToString($this->buffer), |
|
141 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
142 | + ); |
|
143 | + $this->leftover = 0; |
|
144 | + } |
|
145 | + |
|
146 | + /* process full blocks */ |
|
147 | + if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
148 | + /** @var int $want */ |
|
149 | + $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1); |
|
150 | + if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
151 | + $block = self::substr($message, 0, $want); |
|
152 | + if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
153 | + $this->blocks($block, $want); |
|
154 | + $message = self::substr($message, $want); |
|
155 | + $bytes = self::strlen($message); |
|
156 | + } |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /* store leftover */ |
|
161 | + if ($bytes) { |
|
162 | + for ($i = 0; $i < $bytes; ++$i) { |
|
163 | + $mi = self::chrToInt($message[$i]); |
|
164 | + $this->buffer[$this->leftover + $i] = $mi; |
|
165 | + } |
|
166 | + $this->leftover = (int) $this->leftover + $bytes; |
|
167 | + } |
|
168 | + return $this; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @internal You should not use this directly from another application |
|
173 | + * |
|
174 | + * @param string $message |
|
175 | + * @param int $bytes |
|
176 | + * @return self |
|
177 | + * @throws TypeError |
|
178 | + */ |
|
179 | + public function blocks($message, $bytes) |
|
180 | + { |
|
181 | + if (self::strlen($message) < 16) { |
|
182 | + $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
183 | + } |
|
184 | + /** @var int $hibit */ |
|
185 | + $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */ |
|
186 | + $r0 = (int) $this->r[0]; |
|
187 | + $r1 = (int) $this->r[1]; |
|
188 | + $r2 = (int) $this->r[2]; |
|
189 | + $r3 = (int) $this->r[3]; |
|
190 | + $r4 = (int) $this->r[4]; |
|
191 | + |
|
192 | + $s1 = self::mul($r1, 5, 3); |
|
193 | + $s2 = self::mul($r2, 5, 3); |
|
194 | + $s3 = self::mul($r3, 5, 3); |
|
195 | + $s4 = self::mul($r4, 5, 3); |
|
196 | + |
|
197 | + $h0 = $this->h[0]; |
|
198 | + $h1 = $this->h[1]; |
|
199 | + $h2 = $this->h[2]; |
|
200 | + $h3 = $this->h[3]; |
|
201 | + $h4 = $this->h[4]; |
|
202 | + |
|
203 | + while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
204 | + /* h += m[i] */ |
|
205 | + $h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff; |
|
206 | + $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff; |
|
207 | + $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff; |
|
208 | + $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff; |
|
209 | + $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit; |
|
210 | + |
|
211 | + /* h *= r */ |
|
212 | + $d0 = ( |
|
213 | + self::mul($h0, $r0, 25) + |
|
214 | + self::mul($s4, $h1, 26) + |
|
215 | + self::mul($s3, $h2, 26) + |
|
216 | + self::mul($s2, $h3, 26) + |
|
217 | + self::mul($s1, $h4, 26) |
|
218 | + ); |
|
219 | + |
|
220 | + $d1 = ( |
|
221 | + self::mul($h0, $r1, 25) + |
|
222 | + self::mul($h1, $r0, 25) + |
|
223 | + self::mul($s4, $h2, 26) + |
|
224 | + self::mul($s3, $h3, 26) + |
|
225 | + self::mul($s2, $h4, 26) |
|
226 | + ); |
|
227 | + |
|
228 | + $d2 = ( |
|
229 | + self::mul($h0, $r2, 25) + |
|
230 | + self::mul($h1, $r1, 25) + |
|
231 | + self::mul($h2, $r0, 25) + |
|
232 | + self::mul($s4, $h3, 26) + |
|
233 | + self::mul($s3, $h4, 26) |
|
234 | + ); |
|
235 | + |
|
236 | + $d3 = ( |
|
237 | + self::mul($h0, $r3, 25) + |
|
238 | + self::mul($h1, $r2, 25) + |
|
239 | + self::mul($h2, $r1, 25) + |
|
240 | + self::mul($h3, $r0, 25) + |
|
241 | + self::mul($s4, $h4, 26) |
|
242 | + ); |
|
243 | + |
|
244 | + $d4 = ( |
|
245 | + self::mul($h0, $r4, 25) + |
|
246 | + self::mul($h1, $r3, 25) + |
|
247 | + self::mul($h2, $r2, 25) + |
|
248 | + self::mul($h3, $r1, 25) + |
|
249 | + self::mul($h4, $r0, 25) |
|
250 | + ); |
|
251 | + |
|
252 | + /* (partial) h %= p */ |
|
253 | + /** @var int $c */ |
|
254 | + $c = $d0 >> 26; |
|
255 | + /** @var int $h0 */ |
|
256 | + $h0 = $d0 & 0x3ffffff; |
|
257 | + $d1 += $c; |
|
258 | + |
|
259 | + /** @var int $c */ |
|
260 | + $c = $d1 >> 26; |
|
261 | + /** @var int $h1 */ |
|
262 | + $h1 = $d1 & 0x3ffffff; |
|
263 | + $d2 += $c; |
|
264 | + |
|
265 | + /** @var int $c */ |
|
266 | + $c = $d2 >> 26; |
|
267 | + /** @var int $h2 */ |
|
268 | + $h2 = $d2 & 0x3ffffff; |
|
269 | + $d3 += $c; |
|
270 | + |
|
271 | + /** @var int $c */ |
|
272 | + $c = $d3 >> 26; |
|
273 | + /** @var int $h3 */ |
|
274 | + $h3 = $d3 & 0x3ffffff; |
|
275 | + $d4 += $c; |
|
276 | + |
|
277 | + /** @var int $c */ |
|
278 | + $c = $d4 >> 26; |
|
279 | + /** @var int $h4 */ |
|
280 | + $h4 = $d4 & 0x3ffffff; |
|
281 | + $h0 += (int) self::mul($c, 5, 3); |
|
282 | + |
|
283 | + /** @var int $c */ |
|
284 | + $c = $h0 >> 26; |
|
285 | + /** @var int $h0 */ |
|
286 | + $h0 &= 0x3ffffff; |
|
287 | + $h1 += $c; |
|
288 | + |
|
289 | + // Chop off the left 32 bytes. |
|
290 | + $message = self::substr( |
|
291 | + $message, |
|
292 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
293 | + ); |
|
294 | + $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; |
|
295 | + } |
|
296 | + |
|
297 | + $this->h = array( |
|
298 | + (int) ($h0 & 0xffffffff), |
|
299 | + (int) ($h1 & 0xffffffff), |
|
300 | + (int) ($h2 & 0xffffffff), |
|
301 | + (int) ($h3 & 0xffffffff), |
|
302 | + (int) ($h4 & 0xffffffff) |
|
303 | + ); |
|
304 | + return $this; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * @internal You should not use this directly from another application |
|
309 | + * |
|
310 | + * @return string |
|
311 | + * @throws TypeError |
|
312 | + */ |
|
313 | + public function finish() |
|
314 | + { |
|
315 | + /* process the remaining block */ |
|
316 | + if ($this->leftover) { |
|
317 | + $i = $this->leftover; |
|
318 | + $this->buffer[$i++] = 1; |
|
319 | + for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
320 | + $this->buffer[$i] = 0; |
|
321 | + } |
|
322 | + $this->final = true; |
|
323 | + $this->blocks( |
|
324 | + self::substr( |
|
325 | + self::intArrayToString($this->buffer), |
|
326 | + 0, |
|
327 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
328 | + ), |
|
329 | + ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
|
330 | + ); |
|
331 | + } |
|
332 | + |
|
333 | + $h0 = (int) $this->h[0]; |
|
334 | + $h1 = (int) $this->h[1]; |
|
335 | + $h2 = (int) $this->h[2]; |
|
336 | + $h3 = (int) $this->h[3]; |
|
337 | + $h4 = (int) $this->h[4]; |
|
338 | + |
|
339 | + /** @var int $c */ |
|
340 | + $c = $h1 >> 26; |
|
341 | + /** @var int $h1 */ |
|
342 | + $h1 &= 0x3ffffff; |
|
343 | + /** @var int $h2 */ |
|
344 | + $h2 += $c; |
|
345 | + /** @var int $c */ |
|
346 | + $c = $h2 >> 26; |
|
347 | + /** @var int $h2 */ |
|
348 | + $h2 &= 0x3ffffff; |
|
349 | + $h3 += $c; |
|
350 | + /** @var int $c */ |
|
351 | + $c = $h3 >> 26; |
|
352 | + $h3 &= 0x3ffffff; |
|
353 | + $h4 += $c; |
|
354 | + /** @var int $c */ |
|
355 | + $c = $h4 >> 26; |
|
356 | + $h4 &= 0x3ffffff; |
|
357 | + /** @var int $h0 */ |
|
358 | + $h0 += self::mul($c, 5, 3); |
|
359 | + /** @var int $c */ |
|
360 | + $c = $h0 >> 26; |
|
361 | + /** @var int $h0 */ |
|
362 | + $h0 &= 0x3ffffff; |
|
363 | + /** @var int $h1 */ |
|
364 | + $h1 += $c; |
|
365 | + |
|
366 | + /* compute h + -p */ |
|
367 | + /** @var int $g0 */ |
|
368 | + $g0 = $h0 + 5; |
|
369 | + /** @var int $c */ |
|
370 | + $c = $g0 >> 26; |
|
371 | + /** @var int $g0 */ |
|
372 | + $g0 &= 0x3ffffff; |
|
373 | + |
|
374 | + /** @var int $g1 */ |
|
375 | + $g1 = $h1 + $c; |
|
376 | + /** @var int $c */ |
|
377 | + $c = $g1 >> 26; |
|
378 | + $g1 &= 0x3ffffff; |
|
379 | + |
|
380 | + /** @var int $g2 */ |
|
381 | + $g2 = $h2 + $c; |
|
382 | + /** @var int $c */ |
|
383 | + $c = $g2 >> 26; |
|
384 | + /** @var int $g2 */ |
|
385 | + $g2 &= 0x3ffffff; |
|
386 | + |
|
387 | + /** @var int $g3 */ |
|
388 | + $g3 = $h3 + $c; |
|
389 | + /** @var int $c */ |
|
390 | + $c = $g3 >> 26; |
|
391 | + /** @var int $g3 */ |
|
392 | + $g3 &= 0x3ffffff; |
|
393 | + |
|
394 | + /** @var int $g4 */ |
|
395 | + $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
396 | + |
|
397 | + /* select h if h < p, or h + -p if h >= p */ |
|
398 | + /** @var int $mask */ |
|
399 | + $mask = ($g4 >> 31) - 1; |
|
400 | + |
|
401 | + $g0 &= $mask; |
|
402 | + $g1 &= $mask; |
|
403 | + $g2 &= $mask; |
|
404 | + $g3 &= $mask; |
|
405 | + $g4 &= $mask; |
|
406 | + |
|
407 | + /** @var int $mask */ |
|
408 | + $mask = ~$mask & 0xffffffff; |
|
409 | + /** @var int $h0 */ |
|
410 | + $h0 = ($h0 & $mask) | $g0; |
|
411 | + /** @var int $h1 */ |
|
412 | + $h1 = ($h1 & $mask) | $g1; |
|
413 | + /** @var int $h2 */ |
|
414 | + $h2 = ($h2 & $mask) | $g2; |
|
415 | + /** @var int $h3 */ |
|
416 | + $h3 = ($h3 & $mask) | $g3; |
|
417 | + /** @var int $h4 */ |
|
418 | + $h4 = ($h4 & $mask) | $g4; |
|
419 | + |
|
420 | + /* h = h % (2^128) */ |
|
421 | + /** @var int $h0 */ |
|
422 | + $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
423 | + /** @var int $h1 */ |
|
424 | + $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
425 | + /** @var int $h2 */ |
|
426 | + $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
427 | + /** @var int $h3 */ |
|
428 | + $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
429 | + |
|
430 | + /* mac = (h + pad) % (2^128) */ |
|
431 | + $f = (int) ($h0 + $this->pad[0]); |
|
432 | + $h0 = (int) $f; |
|
433 | + $f = (int) ($h1 + $this->pad[1] + ($f >> 32)); |
|
434 | + $h1 = (int) $f; |
|
435 | + $f = (int) ($h2 + $this->pad[2] + ($f >> 32)); |
|
436 | + $h2 = (int) $f; |
|
437 | + $f = (int) ($h3 + $this->pad[3] + ($f >> 32)); |
|
438 | + $h3 = (int) $f; |
|
439 | + |
|
440 | + return self::store32_le($h0 & 0xffffffff) . |
|
441 | + self::store32_le($h1 & 0xffffffff) . |
|
442 | + self::store32_le($h2 & 0xffffffff) . |
|
443 | + self::store32_le($h3 & 0xffffffff); |
|
444 | + } |
|
445 | 445 | } |
@@ -8,241 +8,241 @@ |
||
8 | 8 | */ |
9 | 9 | class ParagonIE_Sodium_Core_Base64_Original |
10 | 10 | { |
11 | - // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
|
12 | - /** |
|
13 | - * Encode into Base64 |
|
14 | - * |
|
15 | - * Base64 character set "[A-Z][a-z][0-9]+/" |
|
16 | - * |
|
17 | - * @param string $src |
|
18 | - * @return string |
|
19 | - * @throws TypeError |
|
20 | - */ |
|
21 | - public static function encode($src) |
|
22 | - { |
|
23 | - return self::doEncode($src, true); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Encode into Base64, no = padding |
|
28 | - * |
|
29 | - * Base64 character set "[A-Z][a-z][0-9]+/" |
|
30 | - * |
|
31 | - * @param string $src |
|
32 | - * @return string |
|
33 | - * @throws TypeError |
|
34 | - */ |
|
35 | - public static function encodeUnpadded($src) |
|
36 | - { |
|
37 | - return self::doEncode($src, false); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $src |
|
42 | - * @param bool $pad Include = padding? |
|
43 | - * @return string |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - protected static function doEncode($src, $pad = true) |
|
47 | - { |
|
48 | - $dest = ''; |
|
49 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
50 | - // Main loop (no padding): |
|
51 | - for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
52 | - /** @var array<int, int> $chunk */ |
|
53 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); |
|
54 | - $b0 = $chunk[1]; |
|
55 | - $b1 = $chunk[2]; |
|
56 | - $b2 = $chunk[3]; |
|
57 | - |
|
58 | - $dest .= |
|
59 | - self::encode6Bits( $b0 >> 2 ) . |
|
60 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
61 | - self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . |
|
62 | - self::encode6Bits( $b2 & 63); |
|
63 | - } |
|
64 | - // The last chunk, which may have padding: |
|
65 | - if ($i < $srcLen) { |
|
66 | - /** @var array<int, int> $chunk */ |
|
67 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
68 | - $b0 = $chunk[1]; |
|
69 | - if ($i + 1 < $srcLen) { |
|
70 | - $b1 = $chunk[2]; |
|
71 | - $dest .= |
|
72 | - self::encode6Bits($b0 >> 2) . |
|
73 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | - self::encode6Bits(($b1 << 2) & 63); |
|
75 | - if ($pad) { |
|
76 | - $dest .= '='; |
|
77 | - } |
|
78 | - } else { |
|
79 | - $dest .= |
|
80 | - self::encode6Bits( $b0 >> 2) . |
|
81 | - self::encode6Bits(($b0 << 4) & 63); |
|
82 | - if ($pad) { |
|
83 | - $dest .= '=='; |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
87 | - return $dest; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * decode from base64 into binary |
|
92 | - * |
|
93 | - * Base64 character set "./[A-Z][a-z][0-9]" |
|
94 | - * |
|
95 | - * @param string $src |
|
96 | - * @param bool $strictPadding |
|
97 | - * @return string |
|
98 | - * @throws RangeException |
|
99 | - * @throws TypeError |
|
100 | - * @psalm-suppress RedundantCondition |
|
101 | - */ |
|
102 | - public static function decode($src, $strictPadding = false) |
|
103 | - { |
|
104 | - // Remove padding |
|
105 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | - if ($srcLen === 0) { |
|
107 | - return ''; |
|
108 | - } |
|
109 | - |
|
110 | - if ($strictPadding) { |
|
111 | - if (($srcLen & 3) === 0) { |
|
112 | - if ($src[$srcLen - 1] === '=') { |
|
113 | - $srcLen--; |
|
114 | - if ($src[$srcLen - 1] === '=') { |
|
115 | - $srcLen--; |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - if (($srcLen & 3) === 1) { |
|
120 | - throw new RangeException( |
|
121 | - 'Incorrect padding' |
|
122 | - ); |
|
123 | - } |
|
124 | - if ($src[$srcLen - 1] === '=') { |
|
125 | - throw new RangeException( |
|
126 | - 'Incorrect padding' |
|
127 | - ); |
|
128 | - } |
|
129 | - } else { |
|
130 | - $src = rtrim($src, '='); |
|
131 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
132 | - } |
|
133 | - |
|
134 | - $err = 0; |
|
135 | - $dest = ''; |
|
136 | - // Main loop (no padding): |
|
137 | - for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
138 | - /** @var array<int, int> $chunk */ |
|
139 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); |
|
140 | - $c0 = self::decode6Bits($chunk[1]); |
|
141 | - $c1 = self::decode6Bits($chunk[2]); |
|
142 | - $c2 = self::decode6Bits($chunk[3]); |
|
143 | - $c3 = self::decode6Bits($chunk[4]); |
|
144 | - |
|
145 | - $dest .= pack( |
|
146 | - 'CCC', |
|
147 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | - ((($c2 << 6) | $c3) & 0xff) |
|
150 | - ); |
|
151 | - $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
152 | - } |
|
153 | - // The last chunk, which may have padding: |
|
154 | - if ($i < $srcLen) { |
|
155 | - /** @var array<int, int> $chunk */ |
|
156 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
157 | - $c0 = self::decode6Bits($chunk[1]); |
|
158 | - |
|
159 | - if ($i + 2 < $srcLen) { |
|
160 | - $c1 = self::decode6Bits($chunk[2]); |
|
161 | - $c2 = self::decode6Bits($chunk[3]); |
|
162 | - $dest .= pack( |
|
163 | - 'CC', |
|
164 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
166 | - ); |
|
167 | - $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | - } elseif ($i + 1 < $srcLen) { |
|
169 | - $c1 = self::decode6Bits($chunk[2]); |
|
170 | - $dest .= pack( |
|
171 | - 'C', |
|
172 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
173 | - ); |
|
174 | - $err |= ($c0 | $c1) >> 8; |
|
175 | - } elseif ($i < $srcLen && $strictPadding) { |
|
176 | - $err |= 1; |
|
177 | - } |
|
178 | - } |
|
179 | - /** @var bool $check */ |
|
180 | - $check = ($err === 0); |
|
181 | - if (!$check) { |
|
182 | - throw new RangeException( |
|
183 | - 'Base64::decode() only expects characters in the correct base64 alphabet' |
|
184 | - ); |
|
185 | - } |
|
186 | - return $dest; |
|
187 | - } |
|
188 | - // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE |
|
189 | - |
|
190 | - /** |
|
191 | - * Uses bitwise operators instead of table-lookups to turn 6-bit integers |
|
192 | - * into 8-bit integers. |
|
193 | - * |
|
194 | - * Base64 character set: |
|
195 | - * [A-Z] [a-z] [0-9] + / |
|
196 | - * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f |
|
197 | - * |
|
198 | - * @param int $src |
|
199 | - * @return int |
|
200 | - */ |
|
201 | - protected static function decode6Bits($src) |
|
202 | - { |
|
203 | - $ret = -1; |
|
204 | - |
|
205 | - // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
|
206 | - $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
207 | - |
|
208 | - // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
|
209 | - $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
210 | - |
|
211 | - // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
|
212 | - $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
213 | - |
|
214 | - // if ($src == 0x2b) $ret += 62 + 1; |
|
215 | - $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63; |
|
216 | - |
|
217 | - // if ($src == 0x2f) ret += 63 + 1; |
|
218 | - $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64; |
|
219 | - |
|
220 | - return $ret; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
|
225 | - * into 6-bit integers. |
|
226 | - * |
|
227 | - * @param int $src |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - protected static function encode6Bits($src) |
|
231 | - { |
|
232 | - $diff = 0x41; |
|
233 | - |
|
234 | - // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
|
235 | - $diff += ((25 - $src) >> 8) & 6; |
|
236 | - |
|
237 | - // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
|
238 | - $diff -= ((51 - $src) >> 8) & 75; |
|
239 | - |
|
240 | - // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15 |
|
241 | - $diff -= ((61 - $src) >> 8) & 15; |
|
242 | - |
|
243 | - // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3 |
|
244 | - $diff += ((62 - $src) >> 8) & 3; |
|
245 | - |
|
246 | - return pack('C', $src + $diff); |
|
247 | - } |
|
11 | + // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
|
12 | + /** |
|
13 | + * Encode into Base64 |
|
14 | + * |
|
15 | + * Base64 character set "[A-Z][a-z][0-9]+/" |
|
16 | + * |
|
17 | + * @param string $src |
|
18 | + * @return string |
|
19 | + * @throws TypeError |
|
20 | + */ |
|
21 | + public static function encode($src) |
|
22 | + { |
|
23 | + return self::doEncode($src, true); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Encode into Base64, no = padding |
|
28 | + * |
|
29 | + * Base64 character set "[A-Z][a-z][0-9]+/" |
|
30 | + * |
|
31 | + * @param string $src |
|
32 | + * @return string |
|
33 | + * @throws TypeError |
|
34 | + */ |
|
35 | + public static function encodeUnpadded($src) |
|
36 | + { |
|
37 | + return self::doEncode($src, false); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $src |
|
42 | + * @param bool $pad Include = padding? |
|
43 | + * @return string |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + protected static function doEncode($src, $pad = true) |
|
47 | + { |
|
48 | + $dest = ''; |
|
49 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
50 | + // Main loop (no padding): |
|
51 | + for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
52 | + /** @var array<int, int> $chunk */ |
|
53 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); |
|
54 | + $b0 = $chunk[1]; |
|
55 | + $b1 = $chunk[2]; |
|
56 | + $b2 = $chunk[3]; |
|
57 | + |
|
58 | + $dest .= |
|
59 | + self::encode6Bits( $b0 >> 2 ) . |
|
60 | + self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
61 | + self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . |
|
62 | + self::encode6Bits( $b2 & 63); |
|
63 | + } |
|
64 | + // The last chunk, which may have padding: |
|
65 | + if ($i < $srcLen) { |
|
66 | + /** @var array<int, int> $chunk */ |
|
67 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
68 | + $b0 = $chunk[1]; |
|
69 | + if ($i + 1 < $srcLen) { |
|
70 | + $b1 = $chunk[2]; |
|
71 | + $dest .= |
|
72 | + self::encode6Bits($b0 >> 2) . |
|
73 | + self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | + self::encode6Bits(($b1 << 2) & 63); |
|
75 | + if ($pad) { |
|
76 | + $dest .= '='; |
|
77 | + } |
|
78 | + } else { |
|
79 | + $dest .= |
|
80 | + self::encode6Bits( $b0 >> 2) . |
|
81 | + self::encode6Bits(($b0 << 4) & 63); |
|
82 | + if ($pad) { |
|
83 | + $dest .= '=='; |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | + return $dest; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * decode from base64 into binary |
|
92 | + * |
|
93 | + * Base64 character set "./[A-Z][a-z][0-9]" |
|
94 | + * |
|
95 | + * @param string $src |
|
96 | + * @param bool $strictPadding |
|
97 | + * @return string |
|
98 | + * @throws RangeException |
|
99 | + * @throws TypeError |
|
100 | + * @psalm-suppress RedundantCondition |
|
101 | + */ |
|
102 | + public static function decode($src, $strictPadding = false) |
|
103 | + { |
|
104 | + // Remove padding |
|
105 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | + if ($srcLen === 0) { |
|
107 | + return ''; |
|
108 | + } |
|
109 | + |
|
110 | + if ($strictPadding) { |
|
111 | + if (($srcLen & 3) === 0) { |
|
112 | + if ($src[$srcLen - 1] === '=') { |
|
113 | + $srcLen--; |
|
114 | + if ($src[$srcLen - 1] === '=') { |
|
115 | + $srcLen--; |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + if (($srcLen & 3) === 1) { |
|
120 | + throw new RangeException( |
|
121 | + 'Incorrect padding' |
|
122 | + ); |
|
123 | + } |
|
124 | + if ($src[$srcLen - 1] === '=') { |
|
125 | + throw new RangeException( |
|
126 | + 'Incorrect padding' |
|
127 | + ); |
|
128 | + } |
|
129 | + } else { |
|
130 | + $src = rtrim($src, '='); |
|
131 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
132 | + } |
|
133 | + |
|
134 | + $err = 0; |
|
135 | + $dest = ''; |
|
136 | + // Main loop (no padding): |
|
137 | + for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
138 | + /** @var array<int, int> $chunk */ |
|
139 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); |
|
140 | + $c0 = self::decode6Bits($chunk[1]); |
|
141 | + $c1 = self::decode6Bits($chunk[2]); |
|
142 | + $c2 = self::decode6Bits($chunk[3]); |
|
143 | + $c3 = self::decode6Bits($chunk[4]); |
|
144 | + |
|
145 | + $dest .= pack( |
|
146 | + 'CCC', |
|
147 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | + ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | + ((($c2 << 6) | $c3) & 0xff) |
|
150 | + ); |
|
151 | + $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
152 | + } |
|
153 | + // The last chunk, which may have padding: |
|
154 | + if ($i < $srcLen) { |
|
155 | + /** @var array<int, int> $chunk */ |
|
156 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
157 | + $c0 = self::decode6Bits($chunk[1]); |
|
158 | + |
|
159 | + if ($i + 2 < $srcLen) { |
|
160 | + $c1 = self::decode6Bits($chunk[2]); |
|
161 | + $c2 = self::decode6Bits($chunk[3]); |
|
162 | + $dest .= pack( |
|
163 | + 'CC', |
|
164 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | + ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
166 | + ); |
|
167 | + $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | + } elseif ($i + 1 < $srcLen) { |
|
169 | + $c1 = self::decode6Bits($chunk[2]); |
|
170 | + $dest .= pack( |
|
171 | + 'C', |
|
172 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
173 | + ); |
|
174 | + $err |= ($c0 | $c1) >> 8; |
|
175 | + } elseif ($i < $srcLen && $strictPadding) { |
|
176 | + $err |= 1; |
|
177 | + } |
|
178 | + } |
|
179 | + /** @var bool $check */ |
|
180 | + $check = ($err === 0); |
|
181 | + if (!$check) { |
|
182 | + throw new RangeException( |
|
183 | + 'Base64::decode() only expects characters in the correct base64 alphabet' |
|
184 | + ); |
|
185 | + } |
|
186 | + return $dest; |
|
187 | + } |
|
188 | + // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE |
|
189 | + |
|
190 | + /** |
|
191 | + * Uses bitwise operators instead of table-lookups to turn 6-bit integers |
|
192 | + * into 8-bit integers. |
|
193 | + * |
|
194 | + * Base64 character set: |
|
195 | + * [A-Z] [a-z] [0-9] + / |
|
196 | + * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f |
|
197 | + * |
|
198 | + * @param int $src |
|
199 | + * @return int |
|
200 | + */ |
|
201 | + protected static function decode6Bits($src) |
|
202 | + { |
|
203 | + $ret = -1; |
|
204 | + |
|
205 | + // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
|
206 | + $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
207 | + |
|
208 | + // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
|
209 | + $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
210 | + |
|
211 | + // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
|
212 | + $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
213 | + |
|
214 | + // if ($src == 0x2b) $ret += 62 + 1; |
|
215 | + $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63; |
|
216 | + |
|
217 | + // if ($src == 0x2f) ret += 63 + 1; |
|
218 | + $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64; |
|
219 | + |
|
220 | + return $ret; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
|
225 | + * into 6-bit integers. |
|
226 | + * |
|
227 | + * @param int $src |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + protected static function encode6Bits($src) |
|
231 | + { |
|
232 | + $diff = 0x41; |
|
233 | + |
|
234 | + // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
|
235 | + $diff += ((25 - $src) >> 8) & 6; |
|
236 | + |
|
237 | + // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
|
238 | + $diff -= ((51 - $src) >> 8) & 75; |
|
239 | + |
|
240 | + // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15 |
|
241 | + $diff -= ((61 - $src) >> 8) & 15; |
|
242 | + |
|
243 | + // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3 |
|
244 | + $diff += ((62 - $src) >> 8) & 3; |
|
245 | + |
|
246 | + return pack('C', $src + $diff); |
|
247 | + } |
|
248 | 248 | } |
@@ -8,240 +8,240 @@ |
||
8 | 8 | */ |
9 | 9 | class ParagonIE_Sodium_Core_Base64_UrlSafe |
10 | 10 | { |
11 | - // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
|
12 | - /** |
|
13 | - * Encode into Base64 |
|
14 | - * |
|
15 | - * Base64 character set "[A-Z][a-z][0-9]+/" |
|
16 | - * |
|
17 | - * @param string $src |
|
18 | - * @return string |
|
19 | - * @throws TypeError |
|
20 | - */ |
|
21 | - public static function encode($src) |
|
22 | - { |
|
23 | - return self::doEncode($src, true); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Encode into Base64, no = padding |
|
28 | - * |
|
29 | - * Base64 character set "[A-Z][a-z][0-9]+/" |
|
30 | - * |
|
31 | - * @param string $src |
|
32 | - * @return string |
|
33 | - * @throws TypeError |
|
34 | - */ |
|
35 | - public static function encodeUnpadded($src) |
|
36 | - { |
|
37 | - return self::doEncode($src, false); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $src |
|
42 | - * @param bool $pad Include = padding? |
|
43 | - * @return string |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - protected static function doEncode($src, $pad = true) |
|
47 | - { |
|
48 | - $dest = ''; |
|
49 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
50 | - // Main loop (no padding): |
|
51 | - for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
52 | - /** @var array<int, int> $chunk */ |
|
53 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); |
|
54 | - $b0 = $chunk[1]; |
|
55 | - $b1 = $chunk[2]; |
|
56 | - $b2 = $chunk[3]; |
|
57 | - |
|
58 | - $dest .= |
|
59 | - self::encode6Bits( $b0 >> 2 ) . |
|
60 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
61 | - self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . |
|
62 | - self::encode6Bits( $b2 & 63); |
|
63 | - } |
|
64 | - // The last chunk, which may have padding: |
|
65 | - if ($i < $srcLen) { |
|
66 | - /** @var array<int, int> $chunk */ |
|
67 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
68 | - $b0 = $chunk[1]; |
|
69 | - if ($i + 1 < $srcLen) { |
|
70 | - $b1 = $chunk[2]; |
|
71 | - $dest .= |
|
72 | - self::encode6Bits($b0 >> 2) . |
|
73 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | - self::encode6Bits(($b1 << 2) & 63); |
|
75 | - if ($pad) { |
|
76 | - $dest .= '='; |
|
77 | - } |
|
78 | - } else { |
|
79 | - $dest .= |
|
80 | - self::encode6Bits( $b0 >> 2) . |
|
81 | - self::encode6Bits(($b0 << 4) & 63); |
|
82 | - if ($pad) { |
|
83 | - $dest .= '=='; |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
87 | - return $dest; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * decode from base64 into binary |
|
92 | - * |
|
93 | - * Base64 character set "./[A-Z][a-z][0-9]" |
|
94 | - * |
|
95 | - * @param string $src |
|
96 | - * @param bool $strictPadding |
|
97 | - * @return string |
|
98 | - * @throws RangeException |
|
99 | - * @throws TypeError |
|
100 | - * @psalm-suppress RedundantCondition |
|
101 | - */ |
|
102 | - public static function decode($src, $strictPadding = false) |
|
103 | - { |
|
104 | - // Remove padding |
|
105 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | - if ($srcLen === 0) { |
|
107 | - return ''; |
|
108 | - } |
|
109 | - |
|
110 | - if ($strictPadding) { |
|
111 | - if (($srcLen & 3) === 0) { |
|
112 | - if ($src[$srcLen - 1] === '=') { |
|
113 | - $srcLen--; |
|
114 | - if ($src[$srcLen - 1] === '=') { |
|
115 | - $srcLen--; |
|
116 | - } |
|
117 | - } |
|
118 | - } |
|
119 | - if (($srcLen & 3) === 1) { |
|
120 | - throw new RangeException( |
|
121 | - 'Incorrect padding' |
|
122 | - ); |
|
123 | - } |
|
124 | - if ($src[$srcLen - 1] === '=') { |
|
125 | - throw new RangeException( |
|
126 | - 'Incorrect padding' |
|
127 | - ); |
|
128 | - } |
|
129 | - } else { |
|
130 | - $src = rtrim($src, '='); |
|
131 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
132 | - } |
|
133 | - |
|
134 | - $err = 0; |
|
135 | - $dest = ''; |
|
136 | - // Main loop (no padding): |
|
137 | - for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
138 | - /** @var array<int, int> $chunk */ |
|
139 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); |
|
140 | - $c0 = self::decode6Bits($chunk[1]); |
|
141 | - $c1 = self::decode6Bits($chunk[2]); |
|
142 | - $c2 = self::decode6Bits($chunk[3]); |
|
143 | - $c3 = self::decode6Bits($chunk[4]); |
|
144 | - |
|
145 | - $dest .= pack( |
|
146 | - 'CCC', |
|
147 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | - ((($c2 << 6) | $c3) & 0xff) |
|
150 | - ); |
|
151 | - $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
152 | - } |
|
153 | - // The last chunk, which may have padding: |
|
154 | - if ($i < $srcLen) { |
|
155 | - /** @var array<int, int> $chunk */ |
|
156 | - $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
157 | - $c0 = self::decode6Bits($chunk[1]); |
|
158 | - |
|
159 | - if ($i + 2 < $srcLen) { |
|
160 | - $c1 = self::decode6Bits($chunk[2]); |
|
161 | - $c2 = self::decode6Bits($chunk[3]); |
|
162 | - $dest .= pack( |
|
163 | - 'CC', |
|
164 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
166 | - ); |
|
167 | - $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | - } elseif ($i + 1 < $srcLen) { |
|
169 | - $c1 = self::decode6Bits($chunk[2]); |
|
170 | - $dest .= pack( |
|
171 | - 'C', |
|
172 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
173 | - ); |
|
174 | - $err |= ($c0 | $c1) >> 8; |
|
175 | - } elseif ($i < $srcLen && $strictPadding) { |
|
176 | - $err |= 1; |
|
177 | - } |
|
178 | - } |
|
179 | - /** @var bool $check */ |
|
180 | - $check = ($err === 0); |
|
181 | - if (!$check) { |
|
182 | - throw new RangeException( |
|
183 | - 'Base64::decode() only expects characters in the correct base64 alphabet' |
|
184 | - ); |
|
185 | - } |
|
186 | - return $dest; |
|
187 | - } |
|
188 | - // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE |
|
189 | - /** |
|
190 | - * Uses bitwise operators instead of table-lookups to turn 6-bit integers |
|
191 | - * into 8-bit integers. |
|
192 | - * |
|
193 | - * Base64 character set: |
|
194 | - * [A-Z] [a-z] [0-9] + / |
|
195 | - * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f |
|
196 | - * |
|
197 | - * @param int $src |
|
198 | - * @return int |
|
199 | - */ |
|
200 | - protected static function decode6Bits($src) |
|
201 | - { |
|
202 | - $ret = -1; |
|
203 | - |
|
204 | - // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
|
205 | - $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
206 | - |
|
207 | - // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
|
208 | - $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
209 | - |
|
210 | - // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
|
211 | - $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
212 | - |
|
213 | - // if ($src == 0x2c) $ret += 62 + 1; |
|
214 | - $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63; |
|
215 | - |
|
216 | - // if ($src == 0x5f) ret += 63 + 1; |
|
217 | - $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64; |
|
218 | - |
|
219 | - return $ret; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
|
224 | - * into 6-bit integers. |
|
225 | - * |
|
226 | - * @param int $src |
|
227 | - * @return string |
|
228 | - */ |
|
229 | - protected static function encode6Bits($src) |
|
230 | - { |
|
231 | - $diff = 0x41; |
|
232 | - |
|
233 | - // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
|
234 | - $diff += ((25 - $src) >> 8) & 6; |
|
235 | - |
|
236 | - // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
|
237 | - $diff -= ((51 - $src) >> 8) & 75; |
|
238 | - |
|
239 | - // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13 |
|
240 | - $diff -= ((61 - $src) >> 8) & 13; |
|
241 | - |
|
242 | - // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3 |
|
243 | - $diff += ((62 - $src) >> 8) & 49; |
|
244 | - |
|
245 | - return pack('C', $src + $diff); |
|
246 | - } |
|
11 | + // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
|
12 | + /** |
|
13 | + * Encode into Base64 |
|
14 | + * |
|
15 | + * Base64 character set "[A-Z][a-z][0-9]+/" |
|
16 | + * |
|
17 | + * @param string $src |
|
18 | + * @return string |
|
19 | + * @throws TypeError |
|
20 | + */ |
|
21 | + public static function encode($src) |
|
22 | + { |
|
23 | + return self::doEncode($src, true); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Encode into Base64, no = padding |
|
28 | + * |
|
29 | + * Base64 character set "[A-Z][a-z][0-9]+/" |
|
30 | + * |
|
31 | + * @param string $src |
|
32 | + * @return string |
|
33 | + * @throws TypeError |
|
34 | + */ |
|
35 | + public static function encodeUnpadded($src) |
|
36 | + { |
|
37 | + return self::doEncode($src, false); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $src |
|
42 | + * @param bool $pad Include = padding? |
|
43 | + * @return string |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + protected static function doEncode($src, $pad = true) |
|
47 | + { |
|
48 | + $dest = ''; |
|
49 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
50 | + // Main loop (no padding): |
|
51 | + for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
52 | + /** @var array<int, int> $chunk */ |
|
53 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3)); |
|
54 | + $b0 = $chunk[1]; |
|
55 | + $b1 = $chunk[2]; |
|
56 | + $b2 = $chunk[3]; |
|
57 | + |
|
58 | + $dest .= |
|
59 | + self::encode6Bits( $b0 >> 2 ) . |
|
60 | + self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
61 | + self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) . |
|
62 | + self::encode6Bits( $b2 & 63); |
|
63 | + } |
|
64 | + // The last chunk, which may have padding: |
|
65 | + if ($i < $srcLen) { |
|
66 | + /** @var array<int, int> $chunk */ |
|
67 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
68 | + $b0 = $chunk[1]; |
|
69 | + if ($i + 1 < $srcLen) { |
|
70 | + $b1 = $chunk[2]; |
|
71 | + $dest .= |
|
72 | + self::encode6Bits($b0 >> 2) . |
|
73 | + self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | + self::encode6Bits(($b1 << 2) & 63); |
|
75 | + if ($pad) { |
|
76 | + $dest .= '='; |
|
77 | + } |
|
78 | + } else { |
|
79 | + $dest .= |
|
80 | + self::encode6Bits( $b0 >> 2) . |
|
81 | + self::encode6Bits(($b0 << 4) & 63); |
|
82 | + if ($pad) { |
|
83 | + $dest .= '=='; |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | + return $dest; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * decode from base64 into binary |
|
92 | + * |
|
93 | + * Base64 character set "./[A-Z][a-z][0-9]" |
|
94 | + * |
|
95 | + * @param string $src |
|
96 | + * @param bool $strictPadding |
|
97 | + * @return string |
|
98 | + * @throws RangeException |
|
99 | + * @throws TypeError |
|
100 | + * @psalm-suppress RedundantCondition |
|
101 | + */ |
|
102 | + public static function decode($src, $strictPadding = false) |
|
103 | + { |
|
104 | + // Remove padding |
|
105 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | + if ($srcLen === 0) { |
|
107 | + return ''; |
|
108 | + } |
|
109 | + |
|
110 | + if ($strictPadding) { |
|
111 | + if (($srcLen & 3) === 0) { |
|
112 | + if ($src[$srcLen - 1] === '=') { |
|
113 | + $srcLen--; |
|
114 | + if ($src[$srcLen - 1] === '=') { |
|
115 | + $srcLen--; |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | + if (($srcLen & 3) === 1) { |
|
120 | + throw new RangeException( |
|
121 | + 'Incorrect padding' |
|
122 | + ); |
|
123 | + } |
|
124 | + if ($src[$srcLen - 1] === '=') { |
|
125 | + throw new RangeException( |
|
126 | + 'Incorrect padding' |
|
127 | + ); |
|
128 | + } |
|
129 | + } else { |
|
130 | + $src = rtrim($src, '='); |
|
131 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
132 | + } |
|
133 | + |
|
134 | + $err = 0; |
|
135 | + $dest = ''; |
|
136 | + // Main loop (no padding): |
|
137 | + for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
138 | + /** @var array<int, int> $chunk */ |
|
139 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4)); |
|
140 | + $c0 = self::decode6Bits($chunk[1]); |
|
141 | + $c1 = self::decode6Bits($chunk[2]); |
|
142 | + $c2 = self::decode6Bits($chunk[3]); |
|
143 | + $c3 = self::decode6Bits($chunk[4]); |
|
144 | + |
|
145 | + $dest .= pack( |
|
146 | + 'CCC', |
|
147 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | + ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | + ((($c2 << 6) | $c3) & 0xff) |
|
150 | + ); |
|
151 | + $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
152 | + } |
|
153 | + // The last chunk, which may have padding: |
|
154 | + if ($i < $srcLen) { |
|
155 | + /** @var array<int, int> $chunk */ |
|
156 | + $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i)); |
|
157 | + $c0 = self::decode6Bits($chunk[1]); |
|
158 | + |
|
159 | + if ($i + 2 < $srcLen) { |
|
160 | + $c1 = self::decode6Bits($chunk[2]); |
|
161 | + $c2 = self::decode6Bits($chunk[3]); |
|
162 | + $dest .= pack( |
|
163 | + 'CC', |
|
164 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | + ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
166 | + ); |
|
167 | + $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | + } elseif ($i + 1 < $srcLen) { |
|
169 | + $c1 = self::decode6Bits($chunk[2]); |
|
170 | + $dest .= pack( |
|
171 | + 'C', |
|
172 | + ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
173 | + ); |
|
174 | + $err |= ($c0 | $c1) >> 8; |
|
175 | + } elseif ($i < $srcLen && $strictPadding) { |
|
176 | + $err |= 1; |
|
177 | + } |
|
178 | + } |
|
179 | + /** @var bool $check */ |
|
180 | + $check = ($err === 0); |
|
181 | + if (!$check) { |
|
182 | + throw new RangeException( |
|
183 | + 'Base64::decode() only expects characters in the correct base64 alphabet' |
|
184 | + ); |
|
185 | + } |
|
186 | + return $dest; |
|
187 | + } |
|
188 | + // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE |
|
189 | + /** |
|
190 | + * Uses bitwise operators instead of table-lookups to turn 6-bit integers |
|
191 | + * into 8-bit integers. |
|
192 | + * |
|
193 | + * Base64 character set: |
|
194 | + * [A-Z] [a-z] [0-9] + / |
|
195 | + * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f |
|
196 | + * |
|
197 | + * @param int $src |
|
198 | + * @return int |
|
199 | + */ |
|
200 | + protected static function decode6Bits($src) |
|
201 | + { |
|
202 | + $ret = -1; |
|
203 | + |
|
204 | + // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
|
205 | + $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
206 | + |
|
207 | + // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
|
208 | + $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
209 | + |
|
210 | + // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
|
211 | + $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
212 | + |
|
213 | + // if ($src == 0x2c) $ret += 62 + 1; |
|
214 | + $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63; |
|
215 | + |
|
216 | + // if ($src == 0x5f) ret += 63 + 1; |
|
217 | + $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64; |
|
218 | + |
|
219 | + return $ret; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Uses bitwise operators instead of table-lookups to turn 8-bit integers |
|
224 | + * into 6-bit integers. |
|
225 | + * |
|
226 | + * @param int $src |
|
227 | + * @return string |
|
228 | + */ |
|
229 | + protected static function encode6Bits($src) |
|
230 | + { |
|
231 | + $diff = 0x41; |
|
232 | + |
|
233 | + // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
|
234 | + $diff += ((25 - $src) >> 8) & 6; |
|
235 | + |
|
236 | + // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
|
237 | + $diff -= ((51 - $src) >> 8) & 75; |
|
238 | + |
|
239 | + // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13 |
|
240 | + $diff -= ((61 - $src) >> 8) & 13; |
|
241 | + |
|
242 | + // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3 |
|
243 | + $diff += ((62 - $src) >> 8) & 49; |
|
244 | + |
|
245 | + return pack('C', $src + $diff); |
|
246 | + } |
|
247 | 247 | } |
@@ -5,159 +5,159 @@ |
||
5 | 5 | */ |
6 | 6 | class ParagonIE_Sodium_Core_SecretStream_State |
7 | 7 | { |
8 | - /** @var string $key */ |
|
9 | - protected $key; |
|
10 | - |
|
11 | - /** @var int $counter */ |
|
12 | - protected $counter; |
|
13 | - |
|
14 | - /** @var string $nonce */ |
|
15 | - protected $nonce; |
|
16 | - |
|
17 | - /** @var string $_pad */ |
|
18 | - protected $_pad; |
|
19 | - |
|
20 | - /** |
|
21 | - * ParagonIE_Sodium_Core_SecretStream_State constructor. |
|
22 | - * @param string $key |
|
23 | - * @param string|null $nonce |
|
24 | - */ |
|
25 | - public function __construct($key, $nonce = null) |
|
26 | - { |
|
27 | - $this->key = $key; |
|
28 | - $this->counter = 1; |
|
29 | - if (is_null($nonce)) { |
|
30 | - $nonce = str_repeat("\0", 12); |
|
31 | - } |
|
32 | - $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; |
|
33 | - $this->_pad = str_repeat("\0", 4); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @return self |
|
38 | - */ |
|
39 | - public function counterReset() |
|
40 | - { |
|
41 | - $this->counter = 1; |
|
42 | - $this->_pad = str_repeat("\0", 4); |
|
43 | - return $this; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function getKey() |
|
50 | - { |
|
51 | - return $this->key; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getCounter() |
|
58 | - { |
|
59 | - return ParagonIE_Sodium_Core_Util::store32_le($this->counter); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function getNonce() |
|
66 | - { |
|
67 | - if (!is_string($this->nonce)) { |
|
68 | - $this->nonce = str_repeat("\0", 12); |
|
69 | - } |
|
70 | - if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) { |
|
71 | - $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); |
|
72 | - } |
|
73 | - return $this->nonce; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function getCombinedNonce() |
|
80 | - { |
|
81 | - return $this->getCounter() . |
|
82 | - ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public function incrementCounter() |
|
89 | - { |
|
90 | - ++$this->counter; |
|
91 | - return $this; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - public function needsRekey() |
|
98 | - { |
|
99 | - return ($this->counter & 0xffff) === 0; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $newKeyAndNonce |
|
104 | - * @return self |
|
105 | - */ |
|
106 | - public function rekey($newKeyAndNonce) |
|
107 | - { |
|
108 | - $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32); |
|
109 | - $this->nonce = str_pad( |
|
110 | - ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32), |
|
111 | - 12, |
|
112 | - "\0", |
|
113 | - STR_PAD_RIGHT |
|
114 | - ); |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $str |
|
120 | - * @return self |
|
121 | - */ |
|
122 | - public function xorNonce($str) |
|
123 | - { |
|
124 | - $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings( |
|
125 | - $this->getNonce(), |
|
126 | - str_pad( |
|
127 | - ParagonIE_Sodium_Core_Util::substr($str, 0, 8), |
|
128 | - 12, |
|
129 | - "\0", |
|
130 | - STR_PAD_RIGHT |
|
131 | - ) |
|
132 | - ); |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $string |
|
138 | - * @return self |
|
139 | - */ |
|
140 | - public static function fromString($string) |
|
141 | - { |
|
142 | - $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
143 | - ParagonIE_Sodium_Core_Util::substr($string, 0, 32) |
|
144 | - ); |
|
145 | - $state->counter = ParagonIE_Sodium_Core_Util::load_4( |
|
146 | - ParagonIE_Sodium_Core_Util::substr($string, 32, 4) |
|
147 | - ); |
|
148 | - $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12); |
|
149 | - $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8); |
|
150 | - return $state; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - public function toString() |
|
157 | - { |
|
158 | - return $this->key . |
|
159 | - $this->getCounter() . |
|
160 | - $this->nonce . |
|
161 | - $this->_pad; |
|
162 | - } |
|
8 | + /** @var string $key */ |
|
9 | + protected $key; |
|
10 | + |
|
11 | + /** @var int $counter */ |
|
12 | + protected $counter; |
|
13 | + |
|
14 | + /** @var string $nonce */ |
|
15 | + protected $nonce; |
|
16 | + |
|
17 | + /** @var string $_pad */ |
|
18 | + protected $_pad; |
|
19 | + |
|
20 | + /** |
|
21 | + * ParagonIE_Sodium_Core_SecretStream_State constructor. |
|
22 | + * @param string $key |
|
23 | + * @param string|null $nonce |
|
24 | + */ |
|
25 | + public function __construct($key, $nonce = null) |
|
26 | + { |
|
27 | + $this->key = $key; |
|
28 | + $this->counter = 1; |
|
29 | + if (is_null($nonce)) { |
|
30 | + $nonce = str_repeat("\0", 12); |
|
31 | + } |
|
32 | + $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; |
|
33 | + $this->_pad = str_repeat("\0", 4); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @return self |
|
38 | + */ |
|
39 | + public function counterReset() |
|
40 | + { |
|
41 | + $this->counter = 1; |
|
42 | + $this->_pad = str_repeat("\0", 4); |
|
43 | + return $this; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function getKey() |
|
50 | + { |
|
51 | + return $this->key; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getCounter() |
|
58 | + { |
|
59 | + return ParagonIE_Sodium_Core_Util::store32_le($this->counter); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function getNonce() |
|
66 | + { |
|
67 | + if (!is_string($this->nonce)) { |
|
68 | + $this->nonce = str_repeat("\0", 12); |
|
69 | + } |
|
70 | + if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) { |
|
71 | + $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); |
|
72 | + } |
|
73 | + return $this->nonce; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function getCombinedNonce() |
|
80 | + { |
|
81 | + return $this->getCounter() . |
|
82 | + ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public function incrementCounter() |
|
89 | + { |
|
90 | + ++$this->counter; |
|
91 | + return $this; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + public function needsRekey() |
|
98 | + { |
|
99 | + return ($this->counter & 0xffff) === 0; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $newKeyAndNonce |
|
104 | + * @return self |
|
105 | + */ |
|
106 | + public function rekey($newKeyAndNonce) |
|
107 | + { |
|
108 | + $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32); |
|
109 | + $this->nonce = str_pad( |
|
110 | + ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32), |
|
111 | + 12, |
|
112 | + "\0", |
|
113 | + STR_PAD_RIGHT |
|
114 | + ); |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $str |
|
120 | + * @return self |
|
121 | + */ |
|
122 | + public function xorNonce($str) |
|
123 | + { |
|
124 | + $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings( |
|
125 | + $this->getNonce(), |
|
126 | + str_pad( |
|
127 | + ParagonIE_Sodium_Core_Util::substr($str, 0, 8), |
|
128 | + 12, |
|
129 | + "\0", |
|
130 | + STR_PAD_RIGHT |
|
131 | + ) |
|
132 | + ); |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $string |
|
138 | + * @return self |
|
139 | + */ |
|
140 | + public static function fromString($string) |
|
141 | + { |
|
142 | + $state = new ParagonIE_Sodium_Core_SecretStream_State( |
|
143 | + ParagonIE_Sodium_Core_Util::substr($string, 0, 32) |
|
144 | + ); |
|
145 | + $state->counter = ParagonIE_Sodium_Core_Util::load_4( |
|
146 | + ParagonIE_Sodium_Core_Util::substr($string, 32, 4) |
|
147 | + ); |
|
148 | + $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12); |
|
149 | + $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8); |
|
150 | + return $state; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + public function toString() |
|
157 | + { |
|
158 | + return $this->key . |
|
159 | + $this->getCounter() . |
|
160 | + $this->nonce . |
|
161 | + $this->_pad; |
|
162 | + } |
|
163 | 163 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Salsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,265 +9,265 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_Salsa20 extends ParagonIE_Sodium_Core_Util |
11 | 11 | { |
12 | - const ROUNDS = 20; |
|
12 | + const ROUNDS = 20; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Calculate an salsa20 hash of a single block |
|
16 | - * |
|
17 | - * @internal You should not use this directly from another application |
|
18 | - * |
|
19 | - * @param string $in |
|
20 | - * @param string $k |
|
21 | - * @param string|null $c |
|
22 | - * @return string |
|
23 | - * @throws TypeError |
|
24 | - */ |
|
25 | - public static function core_salsa20($in, $k, $c = null) |
|
26 | - { |
|
27 | - if (self::strlen($k) < 32) { |
|
28 | - throw new RangeException('Key must be 32 bytes long'); |
|
29 | - } |
|
30 | - if ($c === null) { |
|
31 | - $j0 = $x0 = 0x61707865; |
|
32 | - $j5 = $x5 = 0x3320646e; |
|
33 | - $j10 = $x10 = 0x79622d32; |
|
34 | - $j15 = $x15 = 0x6b206574; |
|
35 | - } else { |
|
36 | - $j0 = $x0 = self::load_4(self::substr($c, 0, 4)); |
|
37 | - $j5 = $x5 = self::load_4(self::substr($c, 4, 4)); |
|
38 | - $j10 = $x10 = self::load_4(self::substr($c, 8, 4)); |
|
39 | - $j15 = $x15 = self::load_4(self::substr($c, 12, 4)); |
|
40 | - } |
|
41 | - $j1 = $x1 = self::load_4(self::substr($k, 0, 4)); |
|
42 | - $j2 = $x2 = self::load_4(self::substr($k, 4, 4)); |
|
43 | - $j3 = $x3 = self::load_4(self::substr($k, 8, 4)); |
|
44 | - $j4 = $x4 = self::load_4(self::substr($k, 12, 4)); |
|
45 | - $j6 = $x6 = self::load_4(self::substr($in, 0, 4)); |
|
46 | - $j7 = $x7 = self::load_4(self::substr($in, 4, 4)); |
|
47 | - $j8 = $x8 = self::load_4(self::substr($in, 8, 4)); |
|
48 | - $j9 = $x9 = self::load_4(self::substr($in, 12, 4)); |
|
49 | - $j11 = $x11 = self::load_4(self::substr($k, 16, 4)); |
|
50 | - $j12 = $x12 = self::load_4(self::substr($k, 20, 4)); |
|
51 | - $j13 = $x13 = self::load_4(self::substr($k, 24, 4)); |
|
52 | - $j14 = $x14 = self::load_4(self::substr($k, 28, 4)); |
|
14 | + /** |
|
15 | + * Calculate an salsa20 hash of a single block |
|
16 | + * |
|
17 | + * @internal You should not use this directly from another application |
|
18 | + * |
|
19 | + * @param string $in |
|
20 | + * @param string $k |
|
21 | + * @param string|null $c |
|
22 | + * @return string |
|
23 | + * @throws TypeError |
|
24 | + */ |
|
25 | + public static function core_salsa20($in, $k, $c = null) |
|
26 | + { |
|
27 | + if (self::strlen($k) < 32) { |
|
28 | + throw new RangeException('Key must be 32 bytes long'); |
|
29 | + } |
|
30 | + if ($c === null) { |
|
31 | + $j0 = $x0 = 0x61707865; |
|
32 | + $j5 = $x5 = 0x3320646e; |
|
33 | + $j10 = $x10 = 0x79622d32; |
|
34 | + $j15 = $x15 = 0x6b206574; |
|
35 | + } else { |
|
36 | + $j0 = $x0 = self::load_4(self::substr($c, 0, 4)); |
|
37 | + $j5 = $x5 = self::load_4(self::substr($c, 4, 4)); |
|
38 | + $j10 = $x10 = self::load_4(self::substr($c, 8, 4)); |
|
39 | + $j15 = $x15 = self::load_4(self::substr($c, 12, 4)); |
|
40 | + } |
|
41 | + $j1 = $x1 = self::load_4(self::substr($k, 0, 4)); |
|
42 | + $j2 = $x2 = self::load_4(self::substr($k, 4, 4)); |
|
43 | + $j3 = $x3 = self::load_4(self::substr($k, 8, 4)); |
|
44 | + $j4 = $x4 = self::load_4(self::substr($k, 12, 4)); |
|
45 | + $j6 = $x6 = self::load_4(self::substr($in, 0, 4)); |
|
46 | + $j7 = $x7 = self::load_4(self::substr($in, 4, 4)); |
|
47 | + $j8 = $x8 = self::load_4(self::substr($in, 8, 4)); |
|
48 | + $j9 = $x9 = self::load_4(self::substr($in, 12, 4)); |
|
49 | + $j11 = $x11 = self::load_4(self::substr($k, 16, 4)); |
|
50 | + $j12 = $x12 = self::load_4(self::substr($k, 20, 4)); |
|
51 | + $j13 = $x13 = self::load_4(self::substr($k, 24, 4)); |
|
52 | + $j14 = $x14 = self::load_4(self::substr($k, 28, 4)); |
|
53 | 53 | |
54 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
55 | - $x4 ^= self::rotate($x0 + $x12, 7); |
|
56 | - $x8 ^= self::rotate($x4 + $x0, 9); |
|
57 | - $x12 ^= self::rotate($x8 + $x4, 13); |
|
58 | - $x0 ^= self::rotate($x12 + $x8, 18); |
|
54 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
55 | + $x4 ^= self::rotate($x0 + $x12, 7); |
|
56 | + $x8 ^= self::rotate($x4 + $x0, 9); |
|
57 | + $x12 ^= self::rotate($x8 + $x4, 13); |
|
58 | + $x0 ^= self::rotate($x12 + $x8, 18); |
|
59 | 59 | |
60 | - $x9 ^= self::rotate($x5 + $x1, 7); |
|
61 | - $x13 ^= self::rotate($x9 + $x5, 9); |
|
62 | - $x1 ^= self::rotate($x13 + $x9, 13); |
|
63 | - $x5 ^= self::rotate($x1 + $x13, 18); |
|
60 | + $x9 ^= self::rotate($x5 + $x1, 7); |
|
61 | + $x13 ^= self::rotate($x9 + $x5, 9); |
|
62 | + $x1 ^= self::rotate($x13 + $x9, 13); |
|
63 | + $x5 ^= self::rotate($x1 + $x13, 18); |
|
64 | 64 | |
65 | - $x14 ^= self::rotate($x10 + $x6, 7); |
|
66 | - $x2 ^= self::rotate($x14 + $x10, 9); |
|
67 | - $x6 ^= self::rotate($x2 + $x14, 13); |
|
68 | - $x10 ^= self::rotate($x6 + $x2, 18); |
|
65 | + $x14 ^= self::rotate($x10 + $x6, 7); |
|
66 | + $x2 ^= self::rotate($x14 + $x10, 9); |
|
67 | + $x6 ^= self::rotate($x2 + $x14, 13); |
|
68 | + $x10 ^= self::rotate($x6 + $x2, 18); |
|
69 | 69 | |
70 | - $x3 ^= self::rotate($x15 + $x11, 7); |
|
71 | - $x7 ^= self::rotate($x3 + $x15, 9); |
|
72 | - $x11 ^= self::rotate($x7 + $x3, 13); |
|
73 | - $x15 ^= self::rotate($x11 + $x7, 18); |
|
70 | + $x3 ^= self::rotate($x15 + $x11, 7); |
|
71 | + $x7 ^= self::rotate($x3 + $x15, 9); |
|
72 | + $x11 ^= self::rotate($x7 + $x3, 13); |
|
73 | + $x15 ^= self::rotate($x11 + $x7, 18); |
|
74 | 74 | |
75 | - $x1 ^= self::rotate($x0 + $x3, 7); |
|
76 | - $x2 ^= self::rotate($x1 + $x0, 9); |
|
77 | - $x3 ^= self::rotate($x2 + $x1, 13); |
|
78 | - $x0 ^= self::rotate($x3 + $x2, 18); |
|
75 | + $x1 ^= self::rotate($x0 + $x3, 7); |
|
76 | + $x2 ^= self::rotate($x1 + $x0, 9); |
|
77 | + $x3 ^= self::rotate($x2 + $x1, 13); |
|
78 | + $x0 ^= self::rotate($x3 + $x2, 18); |
|
79 | 79 | |
80 | - $x6 ^= self::rotate($x5 + $x4, 7); |
|
81 | - $x7 ^= self::rotate($x6 + $x5, 9); |
|
82 | - $x4 ^= self::rotate($x7 + $x6, 13); |
|
83 | - $x5 ^= self::rotate($x4 + $x7, 18); |
|
80 | + $x6 ^= self::rotate($x5 + $x4, 7); |
|
81 | + $x7 ^= self::rotate($x6 + $x5, 9); |
|
82 | + $x4 ^= self::rotate($x7 + $x6, 13); |
|
83 | + $x5 ^= self::rotate($x4 + $x7, 18); |
|
84 | 84 | |
85 | - $x11 ^= self::rotate($x10 + $x9, 7); |
|
86 | - $x8 ^= self::rotate($x11 + $x10, 9); |
|
87 | - $x9 ^= self::rotate($x8 + $x11, 13); |
|
88 | - $x10 ^= self::rotate($x9 + $x8, 18); |
|
85 | + $x11 ^= self::rotate($x10 + $x9, 7); |
|
86 | + $x8 ^= self::rotate($x11 + $x10, 9); |
|
87 | + $x9 ^= self::rotate($x8 + $x11, 13); |
|
88 | + $x10 ^= self::rotate($x9 + $x8, 18); |
|
89 | 89 | |
90 | - $x12 ^= self::rotate($x15 + $x14, 7); |
|
91 | - $x13 ^= self::rotate($x12 + $x15, 9); |
|
92 | - $x14 ^= self::rotate($x13 + $x12, 13); |
|
93 | - $x15 ^= self::rotate($x14 + $x13, 18); |
|
94 | - } |
|
90 | + $x12 ^= self::rotate($x15 + $x14, 7); |
|
91 | + $x13 ^= self::rotate($x12 + $x15, 9); |
|
92 | + $x14 ^= self::rotate($x13 + $x12, 13); |
|
93 | + $x15 ^= self::rotate($x14 + $x13, 18); |
|
94 | + } |
|
95 | 95 | |
96 | - $x0 += $j0; |
|
97 | - $x1 += $j1; |
|
98 | - $x2 += $j2; |
|
99 | - $x3 += $j3; |
|
100 | - $x4 += $j4; |
|
101 | - $x5 += $j5; |
|
102 | - $x6 += $j6; |
|
103 | - $x7 += $j7; |
|
104 | - $x8 += $j8; |
|
105 | - $x9 += $j9; |
|
106 | - $x10 += $j10; |
|
107 | - $x11 += $j11; |
|
108 | - $x12 += $j12; |
|
109 | - $x13 += $j13; |
|
110 | - $x14 += $j14; |
|
111 | - $x15 += $j15; |
|
96 | + $x0 += $j0; |
|
97 | + $x1 += $j1; |
|
98 | + $x2 += $j2; |
|
99 | + $x3 += $j3; |
|
100 | + $x4 += $j4; |
|
101 | + $x5 += $j5; |
|
102 | + $x6 += $j6; |
|
103 | + $x7 += $j7; |
|
104 | + $x8 += $j8; |
|
105 | + $x9 += $j9; |
|
106 | + $x10 += $j10; |
|
107 | + $x11 += $j11; |
|
108 | + $x12 += $j12; |
|
109 | + $x13 += $j13; |
|
110 | + $x14 += $j14; |
|
111 | + $x15 += $j15; |
|
112 | 112 | |
113 | - return self::store32_le($x0) . |
|
114 | - self::store32_le($x1) . |
|
115 | - self::store32_le($x2) . |
|
116 | - self::store32_le($x3) . |
|
117 | - self::store32_le($x4) . |
|
118 | - self::store32_le($x5) . |
|
119 | - self::store32_le($x6) . |
|
120 | - self::store32_le($x7) . |
|
121 | - self::store32_le($x8) . |
|
122 | - self::store32_le($x9) . |
|
123 | - self::store32_le($x10) . |
|
124 | - self::store32_le($x11) . |
|
125 | - self::store32_le($x12) . |
|
126 | - self::store32_le($x13) . |
|
127 | - self::store32_le($x14) . |
|
128 | - self::store32_le($x15); |
|
129 | - } |
|
113 | + return self::store32_le($x0) . |
|
114 | + self::store32_le($x1) . |
|
115 | + self::store32_le($x2) . |
|
116 | + self::store32_le($x3) . |
|
117 | + self::store32_le($x4) . |
|
118 | + self::store32_le($x5) . |
|
119 | + self::store32_le($x6) . |
|
120 | + self::store32_le($x7) . |
|
121 | + self::store32_le($x8) . |
|
122 | + self::store32_le($x9) . |
|
123 | + self::store32_le($x10) . |
|
124 | + self::store32_le($x11) . |
|
125 | + self::store32_le($x12) . |
|
126 | + self::store32_le($x13) . |
|
127 | + self::store32_le($x14) . |
|
128 | + self::store32_le($x15); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @internal You should not use this directly from another application |
|
133 | - * |
|
134 | - * @param int $len |
|
135 | - * @param string $nonce |
|
136 | - * @param string $key |
|
137 | - * @return string |
|
138 | - * @throws SodiumException |
|
139 | - * @throws TypeError |
|
140 | - */ |
|
141 | - public static function salsa20($len, $nonce, $key) |
|
142 | - { |
|
143 | - if (self::strlen($key) !== 32) { |
|
144 | - throw new RangeException('Key must be 32 bytes long'); |
|
145 | - } |
|
146 | - $kcopy = '' . $key; |
|
147 | - $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
148 | - $c = ''; |
|
149 | - while ($len >= 64) { |
|
150 | - $c .= self::core_salsa20($in, $kcopy, null); |
|
151 | - $u = 1; |
|
152 | - // Internal counter. |
|
153 | - for ($i = 8; $i < 16; ++$i) { |
|
154 | - $u += self::chrToInt($in[$i]); |
|
155 | - $in[$i] = self::intToChr($u & 0xff); |
|
156 | - $u >>= 8; |
|
157 | - } |
|
158 | - $len -= 64; |
|
159 | - } |
|
160 | - if ($len > 0) { |
|
161 | - $c .= self::substr( |
|
162 | - self::core_salsa20($in, $kcopy, null), |
|
163 | - 0, |
|
164 | - $len |
|
165 | - ); |
|
166 | - } |
|
167 | - try { |
|
168 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
169 | - } catch (SodiumException $ex) { |
|
170 | - $kcopy = null; |
|
171 | - } |
|
172 | - return $c; |
|
173 | - } |
|
131 | + /** |
|
132 | + * @internal You should not use this directly from another application |
|
133 | + * |
|
134 | + * @param int $len |
|
135 | + * @param string $nonce |
|
136 | + * @param string $key |
|
137 | + * @return string |
|
138 | + * @throws SodiumException |
|
139 | + * @throws TypeError |
|
140 | + */ |
|
141 | + public static function salsa20($len, $nonce, $key) |
|
142 | + { |
|
143 | + if (self::strlen($key) !== 32) { |
|
144 | + throw new RangeException('Key must be 32 bytes long'); |
|
145 | + } |
|
146 | + $kcopy = '' . $key; |
|
147 | + $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
148 | + $c = ''; |
|
149 | + while ($len >= 64) { |
|
150 | + $c .= self::core_salsa20($in, $kcopy, null); |
|
151 | + $u = 1; |
|
152 | + // Internal counter. |
|
153 | + for ($i = 8; $i < 16; ++$i) { |
|
154 | + $u += self::chrToInt($in[$i]); |
|
155 | + $in[$i] = self::intToChr($u & 0xff); |
|
156 | + $u >>= 8; |
|
157 | + } |
|
158 | + $len -= 64; |
|
159 | + } |
|
160 | + if ($len > 0) { |
|
161 | + $c .= self::substr( |
|
162 | + self::core_salsa20($in, $kcopy, null), |
|
163 | + 0, |
|
164 | + $len |
|
165 | + ); |
|
166 | + } |
|
167 | + try { |
|
168 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
169 | + } catch (SodiumException $ex) { |
|
170 | + $kcopy = null; |
|
171 | + } |
|
172 | + return $c; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * @internal You should not use this directly from another application |
|
177 | - * |
|
178 | - * @param string $m |
|
179 | - * @param string $n |
|
180 | - * @param int $ic |
|
181 | - * @param string $k |
|
182 | - * @return string |
|
183 | - * @throws SodiumException |
|
184 | - * @throws TypeError |
|
185 | - */ |
|
186 | - public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
187 | - { |
|
188 | - $mlen = self::strlen($m); |
|
189 | - if ($mlen < 1) { |
|
190 | - return ''; |
|
191 | - } |
|
192 | - $kcopy = self::substr($k, 0, 32); |
|
193 | - $in = self::substr($n, 0, 8); |
|
194 | - // Initialize the counter |
|
195 | - $in .= ParagonIE_Sodium_Core_Util::store64_le($ic); |
|
175 | + /** |
|
176 | + * @internal You should not use this directly from another application |
|
177 | + * |
|
178 | + * @param string $m |
|
179 | + * @param string $n |
|
180 | + * @param int $ic |
|
181 | + * @param string $k |
|
182 | + * @return string |
|
183 | + * @throws SodiumException |
|
184 | + * @throws TypeError |
|
185 | + */ |
|
186 | + public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
187 | + { |
|
188 | + $mlen = self::strlen($m); |
|
189 | + if ($mlen < 1) { |
|
190 | + return ''; |
|
191 | + } |
|
192 | + $kcopy = self::substr($k, 0, 32); |
|
193 | + $in = self::substr($n, 0, 8); |
|
194 | + // Initialize the counter |
|
195 | + $in .= ParagonIE_Sodium_Core_Util::store64_le($ic); |
|
196 | 196 | |
197 | - $c = ''; |
|
198 | - while ($mlen >= 64) { |
|
199 | - $block = self::core_salsa20($in, $kcopy, null); |
|
200 | - $c .= self::xorStrings( |
|
201 | - self::substr($m, 0, 64), |
|
202 | - self::substr($block, 0, 64) |
|
203 | - ); |
|
204 | - $u = 1; |
|
205 | - for ($i = 8; $i < 16; ++$i) { |
|
206 | - $u += self::chrToInt($in[$i]); |
|
207 | - $in[$i] = self::intToChr($u & 0xff); |
|
208 | - $u >>= 8; |
|
209 | - } |
|
197 | + $c = ''; |
|
198 | + while ($mlen >= 64) { |
|
199 | + $block = self::core_salsa20($in, $kcopy, null); |
|
200 | + $c .= self::xorStrings( |
|
201 | + self::substr($m, 0, 64), |
|
202 | + self::substr($block, 0, 64) |
|
203 | + ); |
|
204 | + $u = 1; |
|
205 | + for ($i = 8; $i < 16; ++$i) { |
|
206 | + $u += self::chrToInt($in[$i]); |
|
207 | + $in[$i] = self::intToChr($u & 0xff); |
|
208 | + $u >>= 8; |
|
209 | + } |
|
210 | 210 | |
211 | - $mlen -= 64; |
|
212 | - $m = self::substr($m, 64); |
|
213 | - } |
|
211 | + $mlen -= 64; |
|
212 | + $m = self::substr($m, 64); |
|
213 | + } |
|
214 | 214 | |
215 | - if ($mlen) { |
|
216 | - $block = self::core_salsa20($in, $kcopy, null); |
|
217 | - $c .= self::xorStrings( |
|
218 | - self::substr($m, 0, $mlen), |
|
219 | - self::substr($block, 0, $mlen) |
|
220 | - ); |
|
221 | - } |
|
222 | - try { |
|
223 | - ParagonIE_Sodium_Compat::memzero($block); |
|
224 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
225 | - } catch (SodiumException $ex) { |
|
226 | - $block = null; |
|
227 | - $kcopy = null; |
|
228 | - } |
|
215 | + if ($mlen) { |
|
216 | + $block = self::core_salsa20($in, $kcopy, null); |
|
217 | + $c .= self::xorStrings( |
|
218 | + self::substr($m, 0, $mlen), |
|
219 | + self::substr($block, 0, $mlen) |
|
220 | + ); |
|
221 | + } |
|
222 | + try { |
|
223 | + ParagonIE_Sodium_Compat::memzero($block); |
|
224 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
225 | + } catch (SodiumException $ex) { |
|
226 | + $block = null; |
|
227 | + $kcopy = null; |
|
228 | + } |
|
229 | 229 | |
230 | - return $c; |
|
231 | - } |
|
230 | + return $c; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @internal You should not use this directly from another application |
|
235 | - * |
|
236 | - * @param string $message |
|
237 | - * @param string $nonce |
|
238 | - * @param string $key |
|
239 | - * @return string |
|
240 | - * @throws SodiumException |
|
241 | - * @throws TypeError |
|
242 | - */ |
|
243 | - public static function salsa20_xor($message, $nonce, $key) |
|
244 | - { |
|
245 | - return self::xorStrings( |
|
246 | - $message, |
|
247 | - self::salsa20( |
|
248 | - self::strlen($message), |
|
249 | - $nonce, |
|
250 | - $key |
|
251 | - ) |
|
252 | - ); |
|
253 | - } |
|
233 | + /** |
|
234 | + * @internal You should not use this directly from another application |
|
235 | + * |
|
236 | + * @param string $message |
|
237 | + * @param string $nonce |
|
238 | + * @param string $key |
|
239 | + * @return string |
|
240 | + * @throws SodiumException |
|
241 | + * @throws TypeError |
|
242 | + */ |
|
243 | + public static function salsa20_xor($message, $nonce, $key) |
|
244 | + { |
|
245 | + return self::xorStrings( |
|
246 | + $message, |
|
247 | + self::salsa20( |
|
248 | + self::strlen($message), |
|
249 | + $nonce, |
|
250 | + $key |
|
251 | + ) |
|
252 | + ); |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * @internal You should not use this directly from another application |
|
257 | - * |
|
258 | - * @param int $u |
|
259 | - * @param int $c |
|
260 | - * @return int |
|
261 | - */ |
|
262 | - public static function rotate($u, $c) |
|
263 | - { |
|
264 | - $u &= 0xffffffff; |
|
265 | - $c %= 32; |
|
266 | - return (int) (0xffffffff & ( |
|
267 | - ($u << $c) |
|
268 | - | |
|
269 | - ($u >> (32 - $c)) |
|
270 | - ) |
|
271 | - ); |
|
272 | - } |
|
255 | + /** |
|
256 | + * @internal You should not use this directly from another application |
|
257 | + * |
|
258 | + * @param int $u |
|
259 | + * @param int $c |
|
260 | + * @return int |
|
261 | + */ |
|
262 | + public static function rotate($u, $c) |
|
263 | + { |
|
264 | + $u &= 0xffffffff; |
|
265 | + $c %= 32; |
|
266 | + return (int) (0xffffffff & ( |
|
267 | + ($u << $c) |
|
268 | + | |
|
269 | + ($u >> (32 - $c)) |
|
270 | + ) |
|
271 | + ); |
|
272 | + } |
|
273 | 273 | } |