@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_X25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,319 +9,319 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519 |
11 | 11 | { |
12 | - /** |
|
13 | - * Alters the objects passed to this method in place. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
18 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
19 | - * @param int $b |
|
20 | - * @return void |
|
21 | - * @psalm-suppress MixedAssignment |
|
22 | - */ |
|
23 | - public static function fe_cswap( |
|
24 | - ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
25 | - ParagonIE_Sodium_Core_Curve25519_Fe $g, |
|
26 | - $b = 0 |
|
27 | - ) { |
|
28 | - $f0 = (int) $f[0]; |
|
29 | - $f1 = (int) $f[1]; |
|
30 | - $f2 = (int) $f[2]; |
|
31 | - $f3 = (int) $f[3]; |
|
32 | - $f4 = (int) $f[4]; |
|
33 | - $f5 = (int) $f[5]; |
|
34 | - $f6 = (int) $f[6]; |
|
35 | - $f7 = (int) $f[7]; |
|
36 | - $f8 = (int) $f[8]; |
|
37 | - $f9 = (int) $f[9]; |
|
38 | - $g0 = (int) $g[0]; |
|
39 | - $g1 = (int) $g[1]; |
|
40 | - $g2 = (int) $g[2]; |
|
41 | - $g3 = (int) $g[3]; |
|
42 | - $g4 = (int) $g[4]; |
|
43 | - $g5 = (int) $g[5]; |
|
44 | - $g6 = (int) $g[6]; |
|
45 | - $g7 = (int) $g[7]; |
|
46 | - $g8 = (int) $g[8]; |
|
47 | - $g9 = (int) $g[9]; |
|
48 | - $b = -$b; |
|
49 | - $x0 = ($f0 ^ $g0) & $b; |
|
50 | - $x1 = ($f1 ^ $g1) & $b; |
|
51 | - $x2 = ($f2 ^ $g2) & $b; |
|
52 | - $x3 = ($f3 ^ $g3) & $b; |
|
53 | - $x4 = ($f4 ^ $g4) & $b; |
|
54 | - $x5 = ($f5 ^ $g5) & $b; |
|
55 | - $x6 = ($f6 ^ $g6) & $b; |
|
56 | - $x7 = ($f7 ^ $g7) & $b; |
|
57 | - $x8 = ($f8 ^ $g8) & $b; |
|
58 | - $x9 = ($f9 ^ $g9) & $b; |
|
59 | - $f[0] = $f0 ^ $x0; |
|
60 | - $f[1] = $f1 ^ $x1; |
|
61 | - $f[2] = $f2 ^ $x2; |
|
62 | - $f[3] = $f3 ^ $x3; |
|
63 | - $f[4] = $f4 ^ $x4; |
|
64 | - $f[5] = $f5 ^ $x5; |
|
65 | - $f[6] = $f6 ^ $x6; |
|
66 | - $f[7] = $f7 ^ $x7; |
|
67 | - $f[8] = $f8 ^ $x8; |
|
68 | - $f[9] = $f9 ^ $x9; |
|
69 | - $g[0] = $g0 ^ $x0; |
|
70 | - $g[1] = $g1 ^ $x1; |
|
71 | - $g[2] = $g2 ^ $x2; |
|
72 | - $g[3] = $g3 ^ $x3; |
|
73 | - $g[4] = $g4 ^ $x4; |
|
74 | - $g[5] = $g5 ^ $x5; |
|
75 | - $g[6] = $g6 ^ $x6; |
|
76 | - $g[7] = $g7 ^ $x7; |
|
77 | - $g[8] = $g8 ^ $x8; |
|
78 | - $g[9] = $g9 ^ $x9; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @internal You should not use this directly from another application |
|
83 | - * |
|
84 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
85 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
86 | - */ |
|
87 | - public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
88 | - { |
|
89 | - $h = array( |
|
90 | - self::mul((int) $f[0], 121666, 17), |
|
91 | - self::mul((int) $f[1], 121666, 17), |
|
92 | - self::mul((int) $f[2], 121666, 17), |
|
93 | - self::mul((int) $f[3], 121666, 17), |
|
94 | - self::mul((int) $f[4], 121666, 17), |
|
95 | - self::mul((int) $f[5], 121666, 17), |
|
96 | - self::mul((int) $f[6], 121666, 17), |
|
97 | - self::mul((int) $f[7], 121666, 17), |
|
98 | - self::mul((int) $f[8], 121666, 17), |
|
99 | - self::mul((int) $f[9], 121666, 17) |
|
100 | - ); |
|
101 | - |
|
102 | - /** @var int $carry9 */ |
|
103 | - $carry9 = ($h[9] + (1 << 24)) >> 25; |
|
104 | - $h[0] += self::mul($carry9, 19, 5); |
|
105 | - $h[9] -= $carry9 << 25; |
|
106 | - /** @var int $carry1 */ |
|
107 | - $carry1 = ($h[1] + (1 << 24)) >> 25; |
|
108 | - $h[2] += $carry1; |
|
109 | - $h[1] -= $carry1 << 25; |
|
110 | - /** @var int $carry3 */ |
|
111 | - $carry3 = ($h[3] + (1 << 24)) >> 25; |
|
112 | - $h[4] += $carry3; |
|
113 | - $h[3] -= $carry3 << 25; |
|
114 | - /** @var int $carry5 */ |
|
115 | - $carry5 = ($h[5] + (1 << 24)) >> 25; |
|
116 | - $h[6] += $carry5; |
|
117 | - $h[5] -= $carry5 << 25; |
|
118 | - /** @var int $carry7 */ |
|
119 | - $carry7 = ($h[7] + (1 << 24)) >> 25; |
|
120 | - $h[8] += $carry7; |
|
121 | - $h[7] -= $carry7 << 25; |
|
122 | - |
|
123 | - /** @var int $carry0 */ |
|
124 | - $carry0 = ($h[0] + (1 << 25)) >> 26; |
|
125 | - $h[1] += $carry0; |
|
126 | - $h[0] -= $carry0 << 26; |
|
127 | - /** @var int $carry2 */ |
|
128 | - $carry2 = ($h[2] + (1 << 25)) >> 26; |
|
129 | - $h[3] += $carry2; |
|
130 | - $h[2] -= $carry2 << 26; |
|
131 | - /** @var int $carry4 */ |
|
132 | - $carry4 = ($h[4] + (1 << 25)) >> 26; |
|
133 | - $h[5] += $carry4; |
|
134 | - $h[4] -= $carry4 << 26; |
|
135 | - /** @var int $carry6 */ |
|
136 | - $carry6 = ($h[6] + (1 << 25)) >> 26; |
|
137 | - $h[7] += $carry6; |
|
138 | - $h[6] -= $carry6 << 26; |
|
139 | - /** @var int $carry8 */ |
|
140 | - $carry8 = ($h[8] + (1 << 25)) >> 26; |
|
141 | - $h[9] += $carry8; |
|
142 | - $h[8] -= $carry8 << 26; |
|
143 | - |
|
144 | - foreach ($h as $i => $value) { |
|
145 | - $h[$i] = (int) $value; |
|
146 | - } |
|
147 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @internal You should not use this directly from another application |
|
152 | - * |
|
153 | - * Inline comments preceded by # are from libsodium's ref10 code. |
|
154 | - * |
|
155 | - * @param string $n |
|
156 | - * @param string $p |
|
157 | - * @return string |
|
158 | - * @throws SodiumException |
|
159 | - * @throws TypeError |
|
160 | - */ |
|
161 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
162 | - { |
|
163 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
164 | - $e = '' . $n; |
|
165 | - # e[0] &= 248; |
|
166 | - $e[0] = self::intToChr( |
|
167 | - self::chrToInt($e[0]) & 248 |
|
168 | - ); |
|
169 | - # e[31] &= 127; |
|
170 | - # e[31] |= 64; |
|
171 | - $e[31] = self::intToChr( |
|
172 | - (self::chrToInt($e[31]) & 127) | 64 |
|
173 | - ); |
|
174 | - # fe_frombytes(x1,p); |
|
175 | - $x1 = self::fe_frombytes($p); |
|
176 | - # fe_1(x2); |
|
177 | - $x2 = self::fe_1(); |
|
178 | - # fe_0(z2); |
|
179 | - $z2 = self::fe_0(); |
|
180 | - # fe_copy(x3,x1); |
|
181 | - $x3 = self::fe_copy($x1); |
|
182 | - # fe_1(z3); |
|
183 | - $z3 = self::fe_1(); |
|
184 | - |
|
185 | - # swap = 0; |
|
186 | - /** @var int $swap */ |
|
187 | - $swap = 0; |
|
188 | - |
|
189 | - # for (pos = 254;pos >= 0;--pos) { |
|
190 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
191 | - # b = e[pos / 8] >> (pos & 7); |
|
192 | - /** @var int $b */ |
|
193 | - $b = self::chrToInt( |
|
194 | - $e[(int) floor($pos / 8)] |
|
195 | - ) >> ($pos & 7); |
|
196 | - # b &= 1; |
|
197 | - $b &= 1; |
|
198 | - # swap ^= b; |
|
199 | - $swap ^= $b; |
|
200 | - # fe_cswap(x2,x3,swap); |
|
201 | - self::fe_cswap($x2, $x3, $swap); |
|
202 | - # fe_cswap(z2,z3,swap); |
|
203 | - self::fe_cswap($z2, $z3, $swap); |
|
204 | - # swap = b; |
|
205 | - $swap = $b; |
|
206 | - # fe_sub(tmp0,x3,z3); |
|
207 | - $tmp0 = self::fe_sub($x3, $z3); |
|
208 | - # fe_sub(tmp1,x2,z2); |
|
209 | - $tmp1 = self::fe_sub($x2, $z2); |
|
210 | - |
|
211 | - # fe_add(x2,x2,z2); |
|
212 | - $x2 = self::fe_add($x2, $z2); |
|
213 | - |
|
214 | - # fe_add(z2,x3,z3); |
|
215 | - $z2 = self::fe_add($x3, $z3); |
|
216 | - |
|
217 | - # fe_mul(z3,tmp0,x2); |
|
218 | - $z3 = self::fe_mul($tmp0, $x2); |
|
219 | - |
|
220 | - # fe_mul(z2,z2,tmp1); |
|
221 | - $z2 = self::fe_mul($z2, $tmp1); |
|
222 | - |
|
223 | - # fe_sq(tmp0,tmp1); |
|
224 | - $tmp0 = self::fe_sq($tmp1); |
|
225 | - |
|
226 | - # fe_sq(tmp1,x2); |
|
227 | - $tmp1 = self::fe_sq($x2); |
|
228 | - |
|
229 | - # fe_add(x3,z3,z2); |
|
230 | - $x3 = self::fe_add($z3, $z2); |
|
231 | - |
|
232 | - # fe_sub(z2,z3,z2); |
|
233 | - $z2 = self::fe_sub($z3, $z2); |
|
234 | - |
|
235 | - # fe_mul(x2,tmp1,tmp0); |
|
236 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
237 | - |
|
238 | - # fe_sub(tmp1,tmp1,tmp0); |
|
239 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
240 | - |
|
241 | - # fe_sq(z2,z2); |
|
242 | - $z2 = self::fe_sq($z2); |
|
243 | - |
|
244 | - # fe_mul121666(z3,tmp1); |
|
245 | - $z3 = self::fe_mul121666($tmp1); |
|
246 | - |
|
247 | - # fe_sq(x3,x3); |
|
248 | - $x3 = self::fe_sq($x3); |
|
249 | - |
|
250 | - # fe_add(tmp0,tmp0,z3); |
|
251 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
252 | - |
|
253 | - # fe_mul(z3,x1,z2); |
|
254 | - $z3 = self::fe_mul($x1, $z2); |
|
255 | - |
|
256 | - # fe_mul(z2,tmp1,tmp0); |
|
257 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
258 | - } |
|
259 | - |
|
260 | - # fe_cswap(x2,x3,swap); |
|
261 | - self::fe_cswap($x2, $x3, $swap); |
|
262 | - |
|
263 | - # fe_cswap(z2,z3,swap); |
|
264 | - self::fe_cswap($z2, $z3, $swap); |
|
265 | - |
|
266 | - # fe_invert(z2,z2); |
|
267 | - $z2 = self::fe_invert($z2); |
|
268 | - |
|
269 | - # fe_mul(x2,x2,z2); |
|
270 | - $x2 = self::fe_mul($x2, $z2); |
|
271 | - # fe_tobytes(q,x2); |
|
272 | - return self::fe_tobytes($x2); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * @internal You should not use this directly from another application |
|
277 | - * |
|
278 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY |
|
279 | - * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ |
|
280 | - * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
281 | - */ |
|
282 | - public static function edwards_to_montgomery( |
|
283 | - ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY, |
|
284 | - ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ |
|
285 | - ) { |
|
286 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
287 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
288 | - $tempZ = self::fe_invert($tempZ); |
|
289 | - return self::fe_mul($tempX, $tempZ); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @internal You should not use this directly from another application |
|
294 | - * |
|
295 | - * @param string $n |
|
296 | - * @return string |
|
297 | - * @throws SodiumException |
|
298 | - * @throws TypeError |
|
299 | - */ |
|
300 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
301 | - { |
|
302 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
303 | - $e = '' . $n; |
|
304 | - |
|
305 | - # e[0] &= 248; |
|
306 | - $e[0] = self::intToChr( |
|
307 | - self::chrToInt($e[0]) & 248 |
|
308 | - ); |
|
309 | - |
|
310 | - # e[31] &= 127; |
|
311 | - # e[31] |= 64; |
|
312 | - $e[31] = self::intToChr( |
|
313 | - (self::chrToInt($e[31]) & 127) | 64 |
|
314 | - ); |
|
315 | - |
|
316 | - $A = self::ge_scalarmult_base($e); |
|
317 | - if ( |
|
318 | - !($A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
319 | - || |
|
320 | - !($A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
321 | - ) { |
|
322 | - throw new TypeError('Null points encountered'); |
|
323 | - } |
|
324 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
325 | - return self::fe_tobytes($pk); |
|
326 | - } |
|
12 | + /** |
|
13 | + * Alters the objects passed to this method in place. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
18 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $g |
|
19 | + * @param int $b |
|
20 | + * @return void |
|
21 | + * @psalm-suppress MixedAssignment |
|
22 | + */ |
|
23 | + public static function fe_cswap( |
|
24 | + ParagonIE_Sodium_Core_Curve25519_Fe $f, |
|
25 | + ParagonIE_Sodium_Core_Curve25519_Fe $g, |
|
26 | + $b = 0 |
|
27 | + ) { |
|
28 | + $f0 = (int) $f[0]; |
|
29 | + $f1 = (int) $f[1]; |
|
30 | + $f2 = (int) $f[2]; |
|
31 | + $f3 = (int) $f[3]; |
|
32 | + $f4 = (int) $f[4]; |
|
33 | + $f5 = (int) $f[5]; |
|
34 | + $f6 = (int) $f[6]; |
|
35 | + $f7 = (int) $f[7]; |
|
36 | + $f8 = (int) $f[8]; |
|
37 | + $f9 = (int) $f[9]; |
|
38 | + $g0 = (int) $g[0]; |
|
39 | + $g1 = (int) $g[1]; |
|
40 | + $g2 = (int) $g[2]; |
|
41 | + $g3 = (int) $g[3]; |
|
42 | + $g4 = (int) $g[4]; |
|
43 | + $g5 = (int) $g[5]; |
|
44 | + $g6 = (int) $g[6]; |
|
45 | + $g7 = (int) $g[7]; |
|
46 | + $g8 = (int) $g[8]; |
|
47 | + $g9 = (int) $g[9]; |
|
48 | + $b = -$b; |
|
49 | + $x0 = ($f0 ^ $g0) & $b; |
|
50 | + $x1 = ($f1 ^ $g1) & $b; |
|
51 | + $x2 = ($f2 ^ $g2) & $b; |
|
52 | + $x3 = ($f3 ^ $g3) & $b; |
|
53 | + $x4 = ($f4 ^ $g4) & $b; |
|
54 | + $x5 = ($f5 ^ $g5) & $b; |
|
55 | + $x6 = ($f6 ^ $g6) & $b; |
|
56 | + $x7 = ($f7 ^ $g7) & $b; |
|
57 | + $x8 = ($f8 ^ $g8) & $b; |
|
58 | + $x9 = ($f9 ^ $g9) & $b; |
|
59 | + $f[0] = $f0 ^ $x0; |
|
60 | + $f[1] = $f1 ^ $x1; |
|
61 | + $f[2] = $f2 ^ $x2; |
|
62 | + $f[3] = $f3 ^ $x3; |
|
63 | + $f[4] = $f4 ^ $x4; |
|
64 | + $f[5] = $f5 ^ $x5; |
|
65 | + $f[6] = $f6 ^ $x6; |
|
66 | + $f[7] = $f7 ^ $x7; |
|
67 | + $f[8] = $f8 ^ $x8; |
|
68 | + $f[9] = $f9 ^ $x9; |
|
69 | + $g[0] = $g0 ^ $x0; |
|
70 | + $g[1] = $g1 ^ $x1; |
|
71 | + $g[2] = $g2 ^ $x2; |
|
72 | + $g[3] = $g3 ^ $x3; |
|
73 | + $g[4] = $g4 ^ $x4; |
|
74 | + $g[5] = $g5 ^ $x5; |
|
75 | + $g[6] = $g6 ^ $x6; |
|
76 | + $g[7] = $g7 ^ $x7; |
|
77 | + $g[8] = $g8 ^ $x8; |
|
78 | + $g[9] = $g9 ^ $x9; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @internal You should not use this directly from another application |
|
83 | + * |
|
84 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
|
85 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
86 | + */ |
|
87 | + public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
88 | + { |
|
89 | + $h = array( |
|
90 | + self::mul((int) $f[0], 121666, 17), |
|
91 | + self::mul((int) $f[1], 121666, 17), |
|
92 | + self::mul((int) $f[2], 121666, 17), |
|
93 | + self::mul((int) $f[3], 121666, 17), |
|
94 | + self::mul((int) $f[4], 121666, 17), |
|
95 | + self::mul((int) $f[5], 121666, 17), |
|
96 | + self::mul((int) $f[6], 121666, 17), |
|
97 | + self::mul((int) $f[7], 121666, 17), |
|
98 | + self::mul((int) $f[8], 121666, 17), |
|
99 | + self::mul((int) $f[9], 121666, 17) |
|
100 | + ); |
|
101 | + |
|
102 | + /** @var int $carry9 */ |
|
103 | + $carry9 = ($h[9] + (1 << 24)) >> 25; |
|
104 | + $h[0] += self::mul($carry9, 19, 5); |
|
105 | + $h[9] -= $carry9 << 25; |
|
106 | + /** @var int $carry1 */ |
|
107 | + $carry1 = ($h[1] + (1 << 24)) >> 25; |
|
108 | + $h[2] += $carry1; |
|
109 | + $h[1] -= $carry1 << 25; |
|
110 | + /** @var int $carry3 */ |
|
111 | + $carry3 = ($h[3] + (1 << 24)) >> 25; |
|
112 | + $h[4] += $carry3; |
|
113 | + $h[3] -= $carry3 << 25; |
|
114 | + /** @var int $carry5 */ |
|
115 | + $carry5 = ($h[5] + (1 << 24)) >> 25; |
|
116 | + $h[6] += $carry5; |
|
117 | + $h[5] -= $carry5 << 25; |
|
118 | + /** @var int $carry7 */ |
|
119 | + $carry7 = ($h[7] + (1 << 24)) >> 25; |
|
120 | + $h[8] += $carry7; |
|
121 | + $h[7] -= $carry7 << 25; |
|
122 | + |
|
123 | + /** @var int $carry0 */ |
|
124 | + $carry0 = ($h[0] + (1 << 25)) >> 26; |
|
125 | + $h[1] += $carry0; |
|
126 | + $h[0] -= $carry0 << 26; |
|
127 | + /** @var int $carry2 */ |
|
128 | + $carry2 = ($h[2] + (1 << 25)) >> 26; |
|
129 | + $h[3] += $carry2; |
|
130 | + $h[2] -= $carry2 << 26; |
|
131 | + /** @var int $carry4 */ |
|
132 | + $carry4 = ($h[4] + (1 << 25)) >> 26; |
|
133 | + $h[5] += $carry4; |
|
134 | + $h[4] -= $carry4 << 26; |
|
135 | + /** @var int $carry6 */ |
|
136 | + $carry6 = ($h[6] + (1 << 25)) >> 26; |
|
137 | + $h[7] += $carry6; |
|
138 | + $h[6] -= $carry6 << 26; |
|
139 | + /** @var int $carry8 */ |
|
140 | + $carry8 = ($h[8] + (1 << 25)) >> 26; |
|
141 | + $h[9] += $carry8; |
|
142 | + $h[8] -= $carry8 << 26; |
|
143 | + |
|
144 | + foreach ($h as $i => $value) { |
|
145 | + $h[$i] = (int) $value; |
|
146 | + } |
|
147 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @internal You should not use this directly from another application |
|
152 | + * |
|
153 | + * Inline comments preceded by # are from libsodium's ref10 code. |
|
154 | + * |
|
155 | + * @param string $n |
|
156 | + * @param string $p |
|
157 | + * @return string |
|
158 | + * @throws SodiumException |
|
159 | + * @throws TypeError |
|
160 | + */ |
|
161 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
162 | + { |
|
163 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
164 | + $e = '' . $n; |
|
165 | + # e[0] &= 248; |
|
166 | + $e[0] = self::intToChr( |
|
167 | + self::chrToInt($e[0]) & 248 |
|
168 | + ); |
|
169 | + # e[31] &= 127; |
|
170 | + # e[31] |= 64; |
|
171 | + $e[31] = self::intToChr( |
|
172 | + (self::chrToInt($e[31]) & 127) | 64 |
|
173 | + ); |
|
174 | + # fe_frombytes(x1,p); |
|
175 | + $x1 = self::fe_frombytes($p); |
|
176 | + # fe_1(x2); |
|
177 | + $x2 = self::fe_1(); |
|
178 | + # fe_0(z2); |
|
179 | + $z2 = self::fe_0(); |
|
180 | + # fe_copy(x3,x1); |
|
181 | + $x3 = self::fe_copy($x1); |
|
182 | + # fe_1(z3); |
|
183 | + $z3 = self::fe_1(); |
|
184 | + |
|
185 | + # swap = 0; |
|
186 | + /** @var int $swap */ |
|
187 | + $swap = 0; |
|
188 | + |
|
189 | + # for (pos = 254;pos >= 0;--pos) { |
|
190 | + for ($pos = 254; $pos >= 0; --$pos) { |
|
191 | + # b = e[pos / 8] >> (pos & 7); |
|
192 | + /** @var int $b */ |
|
193 | + $b = self::chrToInt( |
|
194 | + $e[(int) floor($pos / 8)] |
|
195 | + ) >> ($pos & 7); |
|
196 | + # b &= 1; |
|
197 | + $b &= 1; |
|
198 | + # swap ^= b; |
|
199 | + $swap ^= $b; |
|
200 | + # fe_cswap(x2,x3,swap); |
|
201 | + self::fe_cswap($x2, $x3, $swap); |
|
202 | + # fe_cswap(z2,z3,swap); |
|
203 | + self::fe_cswap($z2, $z3, $swap); |
|
204 | + # swap = b; |
|
205 | + $swap = $b; |
|
206 | + # fe_sub(tmp0,x3,z3); |
|
207 | + $tmp0 = self::fe_sub($x3, $z3); |
|
208 | + # fe_sub(tmp1,x2,z2); |
|
209 | + $tmp1 = self::fe_sub($x2, $z2); |
|
210 | + |
|
211 | + # fe_add(x2,x2,z2); |
|
212 | + $x2 = self::fe_add($x2, $z2); |
|
213 | + |
|
214 | + # fe_add(z2,x3,z3); |
|
215 | + $z2 = self::fe_add($x3, $z3); |
|
216 | + |
|
217 | + # fe_mul(z3,tmp0,x2); |
|
218 | + $z3 = self::fe_mul($tmp0, $x2); |
|
219 | + |
|
220 | + # fe_mul(z2,z2,tmp1); |
|
221 | + $z2 = self::fe_mul($z2, $tmp1); |
|
222 | + |
|
223 | + # fe_sq(tmp0,tmp1); |
|
224 | + $tmp0 = self::fe_sq($tmp1); |
|
225 | + |
|
226 | + # fe_sq(tmp1,x2); |
|
227 | + $tmp1 = self::fe_sq($x2); |
|
228 | + |
|
229 | + # fe_add(x3,z3,z2); |
|
230 | + $x3 = self::fe_add($z3, $z2); |
|
231 | + |
|
232 | + # fe_sub(z2,z3,z2); |
|
233 | + $z2 = self::fe_sub($z3, $z2); |
|
234 | + |
|
235 | + # fe_mul(x2,tmp1,tmp0); |
|
236 | + $x2 = self::fe_mul($tmp1, $tmp0); |
|
237 | + |
|
238 | + # fe_sub(tmp1,tmp1,tmp0); |
|
239 | + $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
240 | + |
|
241 | + # fe_sq(z2,z2); |
|
242 | + $z2 = self::fe_sq($z2); |
|
243 | + |
|
244 | + # fe_mul121666(z3,tmp1); |
|
245 | + $z3 = self::fe_mul121666($tmp1); |
|
246 | + |
|
247 | + # fe_sq(x3,x3); |
|
248 | + $x3 = self::fe_sq($x3); |
|
249 | + |
|
250 | + # fe_add(tmp0,tmp0,z3); |
|
251 | + $tmp0 = self::fe_add($tmp0, $z3); |
|
252 | + |
|
253 | + # fe_mul(z3,x1,z2); |
|
254 | + $z3 = self::fe_mul($x1, $z2); |
|
255 | + |
|
256 | + # fe_mul(z2,tmp1,tmp0); |
|
257 | + $z2 = self::fe_mul($tmp1, $tmp0); |
|
258 | + } |
|
259 | + |
|
260 | + # fe_cswap(x2,x3,swap); |
|
261 | + self::fe_cswap($x2, $x3, $swap); |
|
262 | + |
|
263 | + # fe_cswap(z2,z3,swap); |
|
264 | + self::fe_cswap($z2, $z3, $swap); |
|
265 | + |
|
266 | + # fe_invert(z2,z2); |
|
267 | + $z2 = self::fe_invert($z2); |
|
268 | + |
|
269 | + # fe_mul(x2,x2,z2); |
|
270 | + $x2 = self::fe_mul($x2, $z2); |
|
271 | + # fe_tobytes(q,x2); |
|
272 | + return self::fe_tobytes($x2); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * @internal You should not use this directly from another application |
|
277 | + * |
|
278 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY |
|
279 | + * @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ |
|
280 | + * @return ParagonIE_Sodium_Core_Curve25519_Fe |
|
281 | + */ |
|
282 | + public static function edwards_to_montgomery( |
|
283 | + ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY, |
|
284 | + ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ |
|
285 | + ) { |
|
286 | + $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
287 | + $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
288 | + $tempZ = self::fe_invert($tempZ); |
|
289 | + return self::fe_mul($tempX, $tempZ); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @internal You should not use this directly from another application |
|
294 | + * |
|
295 | + * @param string $n |
|
296 | + * @return string |
|
297 | + * @throws SodiumException |
|
298 | + * @throws TypeError |
|
299 | + */ |
|
300 | + public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
301 | + { |
|
302 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
303 | + $e = '' . $n; |
|
304 | + |
|
305 | + # e[0] &= 248; |
|
306 | + $e[0] = self::intToChr( |
|
307 | + self::chrToInt($e[0]) & 248 |
|
308 | + ); |
|
309 | + |
|
310 | + # e[31] &= 127; |
|
311 | + # e[31] |= 64; |
|
312 | + $e[31] = self::intToChr( |
|
313 | + (self::chrToInt($e[31]) & 127) | 64 |
|
314 | + ); |
|
315 | + |
|
316 | + $A = self::ge_scalarmult_base($e); |
|
317 | + if ( |
|
318 | + !($A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
319 | + || |
|
320 | + !($A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
321 | + ) { |
|
322 | + throw new TypeError('Null points encountered'); |
|
323 | + } |
|
324 | + $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
325 | + return self::fe_tobytes($pk); |
|
326 | + } |
|
327 | 327 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_X25519', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_X25519', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -25,57 +25,57 @@ discard block |
||
25 | 25 | ParagonIE_Sodium_Core_Curve25519_Fe $g, |
26 | 26 | $b = 0 |
27 | 27 | ) { |
28 | - $f0 = (int) $f[0]; |
|
29 | - $f1 = (int) $f[1]; |
|
30 | - $f2 = (int) $f[2]; |
|
31 | - $f3 = (int) $f[3]; |
|
32 | - $f4 = (int) $f[4]; |
|
33 | - $f5 = (int) $f[5]; |
|
34 | - $f6 = (int) $f[6]; |
|
35 | - $f7 = (int) $f[7]; |
|
36 | - $f8 = (int) $f[8]; |
|
37 | - $f9 = (int) $f[9]; |
|
38 | - $g0 = (int) $g[0]; |
|
39 | - $g1 = (int) $g[1]; |
|
40 | - $g2 = (int) $g[2]; |
|
41 | - $g3 = (int) $g[3]; |
|
42 | - $g4 = (int) $g[4]; |
|
43 | - $g5 = (int) $g[5]; |
|
44 | - $g6 = (int) $g[6]; |
|
45 | - $g7 = (int) $g[7]; |
|
46 | - $g8 = (int) $g[8]; |
|
47 | - $g9 = (int) $g[9]; |
|
28 | + $f0 = (int)$f[ 0 ]; |
|
29 | + $f1 = (int)$f[ 1 ]; |
|
30 | + $f2 = (int)$f[ 2 ]; |
|
31 | + $f3 = (int)$f[ 3 ]; |
|
32 | + $f4 = (int)$f[ 4 ]; |
|
33 | + $f5 = (int)$f[ 5 ]; |
|
34 | + $f6 = (int)$f[ 6 ]; |
|
35 | + $f7 = (int)$f[ 7 ]; |
|
36 | + $f8 = (int)$f[ 8 ]; |
|
37 | + $f9 = (int)$f[ 9 ]; |
|
38 | + $g0 = (int)$g[ 0 ]; |
|
39 | + $g1 = (int)$g[ 1 ]; |
|
40 | + $g2 = (int)$g[ 2 ]; |
|
41 | + $g3 = (int)$g[ 3 ]; |
|
42 | + $g4 = (int)$g[ 4 ]; |
|
43 | + $g5 = (int)$g[ 5 ]; |
|
44 | + $g6 = (int)$g[ 6 ]; |
|
45 | + $g7 = (int)$g[ 7 ]; |
|
46 | + $g8 = (int)$g[ 8 ]; |
|
47 | + $g9 = (int)$g[ 9 ]; |
|
48 | 48 | $b = -$b; |
49 | - $x0 = ($f0 ^ $g0) & $b; |
|
50 | - $x1 = ($f1 ^ $g1) & $b; |
|
51 | - $x2 = ($f2 ^ $g2) & $b; |
|
52 | - $x3 = ($f3 ^ $g3) & $b; |
|
53 | - $x4 = ($f4 ^ $g4) & $b; |
|
54 | - $x5 = ($f5 ^ $g5) & $b; |
|
55 | - $x6 = ($f6 ^ $g6) & $b; |
|
56 | - $x7 = ($f7 ^ $g7) & $b; |
|
57 | - $x8 = ($f8 ^ $g8) & $b; |
|
58 | - $x9 = ($f9 ^ $g9) & $b; |
|
59 | - $f[0] = $f0 ^ $x0; |
|
60 | - $f[1] = $f1 ^ $x1; |
|
61 | - $f[2] = $f2 ^ $x2; |
|
62 | - $f[3] = $f3 ^ $x3; |
|
63 | - $f[4] = $f4 ^ $x4; |
|
64 | - $f[5] = $f5 ^ $x5; |
|
65 | - $f[6] = $f6 ^ $x6; |
|
66 | - $f[7] = $f7 ^ $x7; |
|
67 | - $f[8] = $f8 ^ $x8; |
|
68 | - $f[9] = $f9 ^ $x9; |
|
69 | - $g[0] = $g0 ^ $x0; |
|
70 | - $g[1] = $g1 ^ $x1; |
|
71 | - $g[2] = $g2 ^ $x2; |
|
72 | - $g[3] = $g3 ^ $x3; |
|
73 | - $g[4] = $g4 ^ $x4; |
|
74 | - $g[5] = $g5 ^ $x5; |
|
75 | - $g[6] = $g6 ^ $x6; |
|
76 | - $g[7] = $g7 ^ $x7; |
|
77 | - $g[8] = $g8 ^ $x8; |
|
78 | - $g[9] = $g9 ^ $x9; |
|
49 | + $x0 = ( $f0 ^ $g0 ) & $b; |
|
50 | + $x1 = ( $f1 ^ $g1 ) & $b; |
|
51 | + $x2 = ( $f2 ^ $g2 ) & $b; |
|
52 | + $x3 = ( $f3 ^ $g3 ) & $b; |
|
53 | + $x4 = ( $f4 ^ $g4 ) & $b; |
|
54 | + $x5 = ( $f5 ^ $g5 ) & $b; |
|
55 | + $x6 = ( $f6 ^ $g6 ) & $b; |
|
56 | + $x7 = ( $f7 ^ $g7 ) & $b; |
|
57 | + $x8 = ( $f8 ^ $g8 ) & $b; |
|
58 | + $x9 = ( $f9 ^ $g9 ) & $b; |
|
59 | + $f[ 0 ] = $f0 ^ $x0; |
|
60 | + $f[ 1 ] = $f1 ^ $x1; |
|
61 | + $f[ 2 ] = $f2 ^ $x2; |
|
62 | + $f[ 3 ] = $f3 ^ $x3; |
|
63 | + $f[ 4 ] = $f4 ^ $x4; |
|
64 | + $f[ 5 ] = $f5 ^ $x5; |
|
65 | + $f[ 6 ] = $f6 ^ $x6; |
|
66 | + $f[ 7 ] = $f7 ^ $x7; |
|
67 | + $f[ 8 ] = $f8 ^ $x8; |
|
68 | + $f[ 9 ] = $f9 ^ $x9; |
|
69 | + $g[ 0 ] = $g0 ^ $x0; |
|
70 | + $g[ 1 ] = $g1 ^ $x1; |
|
71 | + $g[ 2 ] = $g2 ^ $x2; |
|
72 | + $g[ 3 ] = $g3 ^ $x3; |
|
73 | + $g[ 4 ] = $g4 ^ $x4; |
|
74 | + $g[ 5 ] = $g5 ^ $x5; |
|
75 | + $g[ 6 ] = $g6 ^ $x6; |
|
76 | + $g[ 7 ] = $g7 ^ $x7; |
|
77 | + $g[ 8 ] = $g8 ^ $x8; |
|
78 | + $g[ 9 ] = $g9 ^ $x9; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -84,67 +84,67 @@ discard block |
||
84 | 84 | * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
85 | 85 | * @return ParagonIE_Sodium_Core_Curve25519_Fe |
86 | 86 | */ |
87 | - public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
87 | + public static function fe_mul121666( ParagonIE_Sodium_Core_Curve25519_Fe $f ) |
|
88 | 88 | { |
89 | 89 | $h = array( |
90 | - self::mul((int) $f[0], 121666, 17), |
|
91 | - self::mul((int) $f[1], 121666, 17), |
|
92 | - self::mul((int) $f[2], 121666, 17), |
|
93 | - self::mul((int) $f[3], 121666, 17), |
|
94 | - self::mul((int) $f[4], 121666, 17), |
|
95 | - self::mul((int) $f[5], 121666, 17), |
|
96 | - self::mul((int) $f[6], 121666, 17), |
|
97 | - self::mul((int) $f[7], 121666, 17), |
|
98 | - self::mul((int) $f[8], 121666, 17), |
|
99 | - self::mul((int) $f[9], 121666, 17) |
|
90 | + self::mul( (int)$f[ 0 ], 121666, 17 ), |
|
91 | + self::mul( (int)$f[ 1 ], 121666, 17 ), |
|
92 | + self::mul( (int)$f[ 2 ], 121666, 17 ), |
|
93 | + self::mul( (int)$f[ 3 ], 121666, 17 ), |
|
94 | + self::mul( (int)$f[ 4 ], 121666, 17 ), |
|
95 | + self::mul( (int)$f[ 5 ], 121666, 17 ), |
|
96 | + self::mul( (int)$f[ 6 ], 121666, 17 ), |
|
97 | + self::mul( (int)$f[ 7 ], 121666, 17 ), |
|
98 | + self::mul( (int)$f[ 8 ], 121666, 17 ), |
|
99 | + self::mul( (int)$f[ 9 ], 121666, 17 ) |
|
100 | 100 | ); |
101 | 101 | |
102 | 102 | /** @var int $carry9 */ |
103 | - $carry9 = ($h[9] + (1 << 24)) >> 25; |
|
104 | - $h[0] += self::mul($carry9, 19, 5); |
|
105 | - $h[9] -= $carry9 << 25; |
|
103 | + $carry9 = ( $h[ 9 ] + ( 1 << 24 ) ) >> 25; |
|
104 | + $h[ 0 ] += self::mul( $carry9, 19, 5 ); |
|
105 | + $h[ 9 ] -= $carry9 << 25; |
|
106 | 106 | /** @var int $carry1 */ |
107 | - $carry1 = ($h[1] + (1 << 24)) >> 25; |
|
108 | - $h[2] += $carry1; |
|
109 | - $h[1] -= $carry1 << 25; |
|
107 | + $carry1 = ( $h[ 1 ] + ( 1 << 24 ) ) >> 25; |
|
108 | + $h[ 2 ] += $carry1; |
|
109 | + $h[ 1 ] -= $carry1 << 25; |
|
110 | 110 | /** @var int $carry3 */ |
111 | - $carry3 = ($h[3] + (1 << 24)) >> 25; |
|
112 | - $h[4] += $carry3; |
|
113 | - $h[3] -= $carry3 << 25; |
|
111 | + $carry3 = ( $h[ 3 ] + ( 1 << 24 ) ) >> 25; |
|
112 | + $h[ 4 ] += $carry3; |
|
113 | + $h[ 3 ] -= $carry3 << 25; |
|
114 | 114 | /** @var int $carry5 */ |
115 | - $carry5 = ($h[5] + (1 << 24)) >> 25; |
|
116 | - $h[6] += $carry5; |
|
117 | - $h[5] -= $carry5 << 25; |
|
115 | + $carry5 = ( $h[ 5 ] + ( 1 << 24 ) ) >> 25; |
|
116 | + $h[ 6 ] += $carry5; |
|
117 | + $h[ 5 ] -= $carry5 << 25; |
|
118 | 118 | /** @var int $carry7 */ |
119 | - $carry7 = ($h[7] + (1 << 24)) >> 25; |
|
120 | - $h[8] += $carry7; |
|
121 | - $h[7] -= $carry7 << 25; |
|
119 | + $carry7 = ( $h[ 7 ] + ( 1 << 24 ) ) >> 25; |
|
120 | + $h[ 8 ] += $carry7; |
|
121 | + $h[ 7 ] -= $carry7 << 25; |
|
122 | 122 | |
123 | 123 | /** @var int $carry0 */ |
124 | - $carry0 = ($h[0] + (1 << 25)) >> 26; |
|
125 | - $h[1] += $carry0; |
|
126 | - $h[0] -= $carry0 << 26; |
|
124 | + $carry0 = ( $h[ 0 ] + ( 1 << 25 ) ) >> 26; |
|
125 | + $h[ 1 ] += $carry0; |
|
126 | + $h[ 0 ] -= $carry0 << 26; |
|
127 | 127 | /** @var int $carry2 */ |
128 | - $carry2 = ($h[2] + (1 << 25)) >> 26; |
|
129 | - $h[3] += $carry2; |
|
130 | - $h[2] -= $carry2 << 26; |
|
128 | + $carry2 = ( $h[ 2 ] + ( 1 << 25 ) ) >> 26; |
|
129 | + $h[ 3 ] += $carry2; |
|
130 | + $h[ 2 ] -= $carry2 << 26; |
|
131 | 131 | /** @var int $carry4 */ |
132 | - $carry4 = ($h[4] + (1 << 25)) >> 26; |
|
133 | - $h[5] += $carry4; |
|
134 | - $h[4] -= $carry4 << 26; |
|
132 | + $carry4 = ( $h[ 4 ] + ( 1 << 25 ) ) >> 26; |
|
133 | + $h[ 5 ] += $carry4; |
|
134 | + $h[ 4 ] -= $carry4 << 26; |
|
135 | 135 | /** @var int $carry6 */ |
136 | - $carry6 = ($h[6] + (1 << 25)) >> 26; |
|
137 | - $h[7] += $carry6; |
|
138 | - $h[6] -= $carry6 << 26; |
|
136 | + $carry6 = ( $h[ 6 ] + ( 1 << 25 ) ) >> 26; |
|
137 | + $h[ 7 ] += $carry6; |
|
138 | + $h[ 6 ] -= $carry6 << 26; |
|
139 | 139 | /** @var int $carry8 */ |
140 | - $carry8 = ($h[8] + (1 << 25)) >> 26; |
|
141 | - $h[9] += $carry8; |
|
142 | - $h[8] -= $carry8 << 26; |
|
140 | + $carry8 = ( $h[ 8 ] + ( 1 << 25 ) ) >> 26; |
|
141 | + $h[ 9 ] += $carry8; |
|
142 | + $h[ 8 ] -= $carry8 << 26; |
|
143 | 143 | |
144 | - foreach ($h as $i => $value) { |
|
145 | - $h[$i] = (int) $value; |
|
144 | + foreach ( $h as $i => $value ) { |
|
145 | + $h[ $i ] = (int)$value; |
|
146 | 146 | } |
147 | - return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); |
|
147 | + return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( $h ); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | /** |
@@ -158,27 +158,27 @@ discard block |
||
158 | 158 | * @throws SodiumException |
159 | 159 | * @throws TypeError |
160 | 160 | */ |
161 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
161 | + public static function crypto_scalarmult_curve25519_ref10( $n, $p ) |
|
162 | 162 | { |
163 | 163 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
164 | 164 | $e = '' . $n; |
165 | 165 | # e[0] &= 248; |
166 | - $e[0] = self::intToChr( |
|
167 | - self::chrToInt($e[0]) & 248 |
|
166 | + $e[ 0 ] = self::intToChr( |
|
167 | + self::chrToInt( $e[ 0 ] ) & 248 |
|
168 | 168 | ); |
169 | 169 | # e[31] &= 127; |
170 | 170 | # e[31] |= 64; |
171 | - $e[31] = self::intToChr( |
|
172 | - (self::chrToInt($e[31]) & 127) | 64 |
|
171 | + $e[ 31 ] = self::intToChr( |
|
172 | + ( self::chrToInt( $e[ 31 ] ) & 127 ) | 64 |
|
173 | 173 | ); |
174 | 174 | # fe_frombytes(x1,p); |
175 | - $x1 = self::fe_frombytes($p); |
|
175 | + $x1 = self::fe_frombytes( $p ); |
|
176 | 176 | # fe_1(x2); |
177 | 177 | $x2 = self::fe_1(); |
178 | 178 | # fe_0(z2); |
179 | 179 | $z2 = self::fe_0(); |
180 | 180 | # fe_copy(x3,x1); |
181 | - $x3 = self::fe_copy($x1); |
|
181 | + $x3 = self::fe_copy( $x1 ); |
|
182 | 182 | # fe_1(z3); |
183 | 183 | $z3 = self::fe_1(); |
184 | 184 | |
@@ -187,89 +187,89 @@ discard block |
||
187 | 187 | $swap = 0; |
188 | 188 | |
189 | 189 | # for (pos = 254;pos >= 0;--pos) { |
190 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
190 | + for ( $pos = 254; $pos >= 0; --$pos ) { |
|
191 | 191 | # b = e[pos / 8] >> (pos & 7); |
192 | 192 | /** @var int $b */ |
193 | 193 | $b = self::chrToInt( |
194 | - $e[(int) floor($pos / 8)] |
|
195 | - ) >> ($pos & 7); |
|
194 | + $e[ (int)floor( $pos / 8 ) ] |
|
195 | + ) >> ( $pos & 7 ); |
|
196 | 196 | # b &= 1; |
197 | 197 | $b &= 1; |
198 | 198 | # swap ^= b; |
199 | 199 | $swap ^= $b; |
200 | 200 | # fe_cswap(x2,x3,swap); |
201 | - self::fe_cswap($x2, $x3, $swap); |
|
201 | + self::fe_cswap( $x2, $x3, $swap ); |
|
202 | 202 | # fe_cswap(z2,z3,swap); |
203 | - self::fe_cswap($z2, $z3, $swap); |
|
203 | + self::fe_cswap( $z2, $z3, $swap ); |
|
204 | 204 | # swap = b; |
205 | 205 | $swap = $b; |
206 | 206 | # fe_sub(tmp0,x3,z3); |
207 | - $tmp0 = self::fe_sub($x3, $z3); |
|
207 | + $tmp0 = self::fe_sub( $x3, $z3 ); |
|
208 | 208 | # fe_sub(tmp1,x2,z2); |
209 | - $tmp1 = self::fe_sub($x2, $z2); |
|
209 | + $tmp1 = self::fe_sub( $x2, $z2 ); |
|
210 | 210 | |
211 | 211 | # fe_add(x2,x2,z2); |
212 | - $x2 = self::fe_add($x2, $z2); |
|
212 | + $x2 = self::fe_add( $x2, $z2 ); |
|
213 | 213 | |
214 | 214 | # fe_add(z2,x3,z3); |
215 | - $z2 = self::fe_add($x3, $z3); |
|
215 | + $z2 = self::fe_add( $x3, $z3 ); |
|
216 | 216 | |
217 | 217 | # fe_mul(z3,tmp0,x2); |
218 | - $z3 = self::fe_mul($tmp0, $x2); |
|
218 | + $z3 = self::fe_mul( $tmp0, $x2 ); |
|
219 | 219 | |
220 | 220 | # fe_mul(z2,z2,tmp1); |
221 | - $z2 = self::fe_mul($z2, $tmp1); |
|
221 | + $z2 = self::fe_mul( $z2, $tmp1 ); |
|
222 | 222 | |
223 | 223 | # fe_sq(tmp0,tmp1); |
224 | - $tmp0 = self::fe_sq($tmp1); |
|
224 | + $tmp0 = self::fe_sq( $tmp1 ); |
|
225 | 225 | |
226 | 226 | # fe_sq(tmp1,x2); |
227 | - $tmp1 = self::fe_sq($x2); |
|
227 | + $tmp1 = self::fe_sq( $x2 ); |
|
228 | 228 | |
229 | 229 | # fe_add(x3,z3,z2); |
230 | - $x3 = self::fe_add($z3, $z2); |
|
230 | + $x3 = self::fe_add( $z3, $z2 ); |
|
231 | 231 | |
232 | 232 | # fe_sub(z2,z3,z2); |
233 | - $z2 = self::fe_sub($z3, $z2); |
|
233 | + $z2 = self::fe_sub( $z3, $z2 ); |
|
234 | 234 | |
235 | 235 | # fe_mul(x2,tmp1,tmp0); |
236 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
236 | + $x2 = self::fe_mul( $tmp1, $tmp0 ); |
|
237 | 237 | |
238 | 238 | # fe_sub(tmp1,tmp1,tmp0); |
239 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
239 | + $tmp1 = self::fe_sub( $tmp1, $tmp0 ); |
|
240 | 240 | |
241 | 241 | # fe_sq(z2,z2); |
242 | - $z2 = self::fe_sq($z2); |
|
242 | + $z2 = self::fe_sq( $z2 ); |
|
243 | 243 | |
244 | 244 | # fe_mul121666(z3,tmp1); |
245 | - $z3 = self::fe_mul121666($tmp1); |
|
245 | + $z3 = self::fe_mul121666( $tmp1 ); |
|
246 | 246 | |
247 | 247 | # fe_sq(x3,x3); |
248 | - $x3 = self::fe_sq($x3); |
|
248 | + $x3 = self::fe_sq( $x3 ); |
|
249 | 249 | |
250 | 250 | # fe_add(tmp0,tmp0,z3); |
251 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
251 | + $tmp0 = self::fe_add( $tmp0, $z3 ); |
|
252 | 252 | |
253 | 253 | # fe_mul(z3,x1,z2); |
254 | - $z3 = self::fe_mul($x1, $z2); |
|
254 | + $z3 = self::fe_mul( $x1, $z2 ); |
|
255 | 255 | |
256 | 256 | # fe_mul(z2,tmp1,tmp0); |
257 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
257 | + $z2 = self::fe_mul( $tmp1, $tmp0 ); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | # fe_cswap(x2,x3,swap); |
261 | - self::fe_cswap($x2, $x3, $swap); |
|
261 | + self::fe_cswap( $x2, $x3, $swap ); |
|
262 | 262 | |
263 | 263 | # fe_cswap(z2,z3,swap); |
264 | - self::fe_cswap($z2, $z3, $swap); |
|
264 | + self::fe_cswap( $z2, $z3, $swap ); |
|
265 | 265 | |
266 | 266 | # fe_invert(z2,z2); |
267 | - $z2 = self::fe_invert($z2); |
|
267 | + $z2 = self::fe_invert( $z2 ); |
|
268 | 268 | |
269 | 269 | # fe_mul(x2,x2,z2); |
270 | - $x2 = self::fe_mul($x2, $z2); |
|
270 | + $x2 = self::fe_mul( $x2, $z2 ); |
|
271 | 271 | # fe_tobytes(q,x2); |
272 | - return self::fe_tobytes($x2); |
|
272 | + return self::fe_tobytes( $x2 ); |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | /** |
@@ -283,10 +283,10 @@ discard block |
||
283 | 283 | ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY, |
284 | 284 | ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ |
285 | 285 | ) { |
286 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
287 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
288 | - $tempZ = self::fe_invert($tempZ); |
|
289 | - return self::fe_mul($tempX, $tempZ); |
|
286 | + $tempX = self::fe_add( $edwardsZ, $edwardsY ); |
|
287 | + $tempZ = self::fe_sub( $edwardsZ, $edwardsY ); |
|
288 | + $tempZ = self::fe_invert( $tempZ ); |
|
289 | + return self::fe_mul( $tempX, $tempZ ); |
|
290 | 290 | } |
291 | 291 | |
292 | 292 | /** |
@@ -297,31 +297,31 @@ discard block |
||
297 | 297 | * @throws SodiumException |
298 | 298 | * @throws TypeError |
299 | 299 | */ |
300 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
300 | + public static function crypto_scalarmult_curve25519_ref10_base( $n ) |
|
301 | 301 | { |
302 | 302 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
303 | 303 | $e = '' . $n; |
304 | 304 | |
305 | 305 | # e[0] &= 248; |
306 | - $e[0] = self::intToChr( |
|
307 | - self::chrToInt($e[0]) & 248 |
|
306 | + $e[ 0 ] = self::intToChr( |
|
307 | + self::chrToInt( $e[ 0 ] ) & 248 |
|
308 | 308 | ); |
309 | 309 | |
310 | 310 | # e[31] &= 127; |
311 | 311 | # e[31] |= 64; |
312 | - $e[31] = self::intToChr( |
|
313 | - (self::chrToInt($e[31]) & 127) | 64 |
|
312 | + $e[ 31 ] = self::intToChr( |
|
313 | + ( self::chrToInt( $e[ 31 ] ) & 127 ) | 64 |
|
314 | 314 | ); |
315 | 315 | |
316 | - $A = self::ge_scalarmult_base($e); |
|
316 | + $A = self::ge_scalarmult_base( $e ); |
|
317 | 317 | if ( |
318 | - !($A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
318 | + ! ( $A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe ) |
|
319 | 319 | || |
320 | - !($A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe) |
|
320 | + ! ( $A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe ) |
|
321 | 321 | ) { |
322 | - throw new TypeError('Null points encountered'); |
|
322 | + throw new TypeError( 'Null points encountered' ); |
|
323 | 323 | } |
324 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
325 | - return self::fe_tobytes($pk); |
|
324 | + $pk = self::edwards_to_montgomery( $A->Y, $A->Z ); |
|
325 | + return self::fe_tobytes( $pk ); |
|
326 | 326 | } |
327 | 327 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_X25519 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519 { |
|
12 | 11 | /** |
13 | 12 | * Alters the objects passed to this method in place. |
14 | 13 | * |
@@ -84,8 +83,7 @@ discard block |
||
84 | 83 | * @param ParagonIE_Sodium_Core_Curve25519_Fe $f |
85 | 84 | * @return ParagonIE_Sodium_Core_Curve25519_Fe |
86 | 85 | */ |
87 | - public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) |
|
88 | - { |
|
86 | + public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f) { |
|
89 | 87 | $h = array( |
90 | 88 | self::mul((int) $f[0], 121666, 17), |
91 | 89 | self::mul((int) $f[1], 121666, 17), |
@@ -158,8 +156,7 @@ discard block |
||
158 | 156 | * @throws SodiumException |
159 | 157 | * @throws TypeError |
160 | 158 | */ |
161 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
162 | - { |
|
159 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) { |
|
163 | 160 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
164 | 161 | $e = '' . $n; |
165 | 162 | # e[0] &= 248; |
@@ -297,8 +294,7 @@ discard block |
||
297 | 294 | * @throws SodiumException |
298 | 295 | * @throws TypeError |
299 | 296 | */ |
300 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
301 | - { |
|
297 | + public static function crypto_scalarmult_curve25519_ref10_base($n) { |
|
302 | 298 | # for (i = 0;i < 32;++i) e[i] = n[i]; |
303 | 299 | $e = '' . $n; |
304 | 300 |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,55 +9,55 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util |
11 | 11 | { |
12 | - const BLOCK_SIZE = 16; |
|
12 | + const BLOCK_SIZE = 16; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param string $m |
|
18 | - * @param string $key |
|
19 | - * @return string |
|
20 | - * @throws SodiumException |
|
21 | - * @throws TypeError |
|
22 | - */ |
|
23 | - public static function onetimeauth($m, $key) |
|
24 | - { |
|
25 | - if (self::strlen($key) < 32) { |
|
26 | - throw new InvalidArgumentException( |
|
27 | - 'Key must be 32 bytes long.' |
|
28 | - ); |
|
29 | - } |
|
30 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
31 | - self::substr($key, 0, 32) |
|
32 | - ); |
|
33 | - return $state |
|
34 | - ->update($m) |
|
35 | - ->finish(); |
|
36 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param string $m |
|
18 | + * @param string $key |
|
19 | + * @return string |
|
20 | + * @throws SodiumException |
|
21 | + * @throws TypeError |
|
22 | + */ |
|
23 | + public static function onetimeauth($m, $key) |
|
24 | + { |
|
25 | + if (self::strlen($key) < 32) { |
|
26 | + throw new InvalidArgumentException( |
|
27 | + 'Key must be 32 bytes long.' |
|
28 | + ); |
|
29 | + } |
|
30 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
31 | + self::substr($key, 0, 32) |
|
32 | + ); |
|
33 | + return $state |
|
34 | + ->update($m) |
|
35 | + ->finish(); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @internal You should not use this directly from another application |
|
40 | - * |
|
41 | - * @param string $mac |
|
42 | - * @param string $m |
|
43 | - * @param string $key |
|
44 | - * @return bool |
|
45 | - * @throws SodiumException |
|
46 | - * @throws TypeError |
|
47 | - */ |
|
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
49 | - { |
|
50 | - if (self::strlen($key) < 32) { |
|
51 | - throw new InvalidArgumentException( |
|
52 | - 'Key must be 32 bytes long.' |
|
53 | - ); |
|
54 | - } |
|
55 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
56 | - self::substr($key, 0, 32) |
|
57 | - ); |
|
58 | - $calc = $state |
|
59 | - ->update($m) |
|
60 | - ->finish(); |
|
61 | - return self::verify_16($calc, $mac); |
|
62 | - } |
|
38 | + /** |
|
39 | + * @internal You should not use this directly from another application |
|
40 | + * |
|
41 | + * @param string $mac |
|
42 | + * @param string $m |
|
43 | + * @param string $key |
|
44 | + * @return bool |
|
45 | + * @throws SodiumException |
|
46 | + * @throws TypeError |
|
47 | + */ |
|
48 | + public static function onetimeauth_verify($mac, $m, $key) |
|
49 | + { |
|
50 | + if (self::strlen($key) < 32) { |
|
51 | + throw new InvalidArgumentException( |
|
52 | + 'Key must be 32 bytes long.' |
|
53 | + ); |
|
54 | + } |
|
55 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
56 | + self::substr($key, 0, 32) |
|
57 | + ); |
|
58 | + $calc = $state |
|
59 | + ->update($m) |
|
60 | + ->finish(); |
|
61 | + return self::verify_16($calc, $mac); |
|
62 | + } |
|
63 | 63 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Poly1305', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * @throws SodiumException |
21 | 21 | * @throws TypeError |
22 | 22 | */ |
23 | - public static function onetimeauth($m, $key) |
|
23 | + public static function onetimeauth( $m, $key ) |
|
24 | 24 | { |
25 | - if (self::strlen($key) < 32) { |
|
25 | + if ( self::strlen( $key ) < 32 ) { |
|
26 | 26 | throw new InvalidArgumentException( |
27 | 27 | 'Key must be 32 bytes long.' |
28 | 28 | ); |
29 | 29 | } |
30 | 30 | $state = new ParagonIE_Sodium_Core_Poly1305_State( |
31 | - self::substr($key, 0, 32) |
|
31 | + self::substr( $key, 0, 32 ) |
|
32 | 32 | ); |
33 | 33 | return $state |
34 | - ->update($m) |
|
34 | + ->update( $m ) |
|
35 | 35 | ->finish(); |
36 | 36 | } |
37 | 37 | |
@@ -45,19 +45,19 @@ discard block |
||
45 | 45 | * @throws SodiumException |
46 | 46 | * @throws TypeError |
47 | 47 | */ |
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
48 | + public static function onetimeauth_verify( $mac, $m, $key ) |
|
49 | 49 | { |
50 | - if (self::strlen($key) < 32) { |
|
50 | + if ( self::strlen( $key ) < 32 ) { |
|
51 | 51 | throw new InvalidArgumentException( |
52 | 52 | 'Key must be 32 bytes long.' |
53 | 53 | ); |
54 | 54 | } |
55 | 55 | $state = new ParagonIE_Sodium_Core_Poly1305_State( |
56 | - self::substr($key, 0, 32) |
|
56 | + self::substr( $key, 0, 32 ) |
|
57 | 57 | ); |
58 | 58 | $calc = $state |
59 | - ->update($m) |
|
59 | + ->update( $m ) |
|
60 | 60 | ->finish(); |
61 | - return self::verify_16($calc, $mac); |
|
61 | + return self::verify_16( $calc, $mac ); |
|
62 | 62 | } |
63 | 63 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_Poly1305 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util { |
|
12 | 11 | const BLOCK_SIZE = 16; |
13 | 12 | |
14 | 13 | /** |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | * @throws SodiumException |
21 | 20 | * @throws TypeError |
22 | 21 | */ |
23 | - public static function onetimeauth($m, $key) |
|
24 | - { |
|
22 | + public static function onetimeauth($m, $key) { |
|
25 | 23 | if (self::strlen($key) < 32) { |
26 | 24 | throw new InvalidArgumentException( |
27 | 25 | 'Key must be 32 bytes long.' |
@@ -45,8 +43,7 @@ discard block |
||
45 | 43 | * @throws SodiumException |
46 | 44 | * @throws TypeError |
47 | 45 | */ |
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
49 | - { |
|
46 | + public static function onetimeauth_verify($mac, $m, $key) { |
|
50 | 47 | if (self::strlen($key) < 32) { |
51 | 48 | throw new InvalidArgumentException( |
52 | 49 | 'Key must be 32 bytes long.' |
@@ -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,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_XSalsa20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_XSalsa20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * @throws SodiumException |
22 | 22 | * @throws TypeError |
23 | 23 | */ |
24 | - public static function xsalsa20($len, $nonce, $key) |
|
24 | + public static function xsalsa20( $len, $nonce, $key ) |
|
25 | 25 | { |
26 | 26 | $ret = self::salsa20( |
27 | 27 | $len, |
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
28 | + self::substr( $nonce, 16, 8 ), |
|
29 | + self::hsalsa20( $nonce, $key ) |
|
30 | 30 | ); |
31 | 31 | return $ret; |
32 | 32 | } |
@@ -43,12 +43,12 @@ discard block |
||
43 | 43 | * @throws SodiumException |
44 | 44 | * @throws TypeError |
45 | 45 | */ |
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
46 | + public static function xsalsa20_xor( $message, $nonce, $key ) |
|
47 | 47 | { |
48 | 48 | return self::xorStrings( |
49 | 49 | $message, |
50 | 50 | self::xsalsa20( |
51 | - self::strlen($message), |
|
51 | + self::strlen( $message ), |
|
52 | 52 | $nonce, |
53 | 53 | $key |
54 | 54 | ) |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_XSalsa20 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_XSalsa20 extends ParagonIE_Sodium_Core_HSalsa20 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_XSalsa20 extends ParagonIE_Sodium_Core_HSalsa20 { |
|
12 | 11 | /** |
13 | 12 | * Expand a key and nonce into an xsalsa20 keystream. |
14 | 13 | * |
@@ -21,8 +20,7 @@ discard block |
||
21 | 20 | * @throws SodiumException |
22 | 21 | * @throws TypeError |
23 | 22 | */ |
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
23 | + public static function xsalsa20($len, $nonce, $key) { |
|
26 | 24 | $ret = self::salsa20( |
27 | 25 | $len, |
28 | 26 | self::substr($nonce, 16, 8), |
@@ -43,8 +41,7 @@ discard block |
||
43 | 41 | * @throws SodiumException |
44 | 42 | * @throws TypeError |
45 | 43 | */ |
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
44 | + public static function xsalsa20_xor($message, $nonce, $key) { |
|
48 | 45 | return self::xorStrings( |
49 | 46 | $message, |
50 | 47 | self::xsalsa20( |
@@ -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 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_ChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util { |
|
12 | 11 | /** |
13 | 12 | * Bitwise left rotation |
14 | 13 | * |
@@ -18,8 +17,7 @@ discard block |
||
18 | 17 | * @param int $n |
19 | 18 | * @return int |
20 | 19 | */ |
21 | - public static function rotate($v, $n) |
|
22 | - { |
|
20 | + public static function rotate($v, $n) { |
|
23 | 21 | $v &= 0xffffffff; |
24 | 22 | $n &= 31; |
25 | 23 | return (int) ( |
@@ -42,8 +40,7 @@ discard block |
||
42 | 40 | * @param int $d |
43 | 41 | * @return array<int, int> |
44 | 42 | */ |
45 | - protected static function quarterRound($a, $b, $c, $d) |
|
46 | - { |
|
43 | + protected static function quarterRound($a, $b, $c, $d) { |
|
47 | 44 | # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
48 | 45 | /** @var int $a */ |
49 | 46 | $a = ($a + $b) & 0xffffffff; |
@@ -329,8 +326,7 @@ discard block |
||
329 | 326 | * @throws SodiumException |
330 | 327 | * @throws TypeError |
331 | 328 | */ |
332 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
333 | - { |
|
329 | + public static function stream($len = 64, $nonce = '', $key = '') { |
|
334 | 330 | return self::encryptBytes( |
335 | 331 | new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), |
336 | 332 | str_repeat("\x00", $len) |
@@ -347,8 +343,7 @@ discard block |
||
347 | 343 | * @throws SodiumException |
348 | 344 | * @throws TypeError |
349 | 345 | */ |
350 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
351 | - { |
|
346 | + public static function ietfStream($len, $nonce = '', $key = '') { |
|
352 | 347 | return self::encryptBytes( |
353 | 348 | new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), |
354 | 349 | str_repeat("\x00", $len) |
@@ -366,8 +361,7 @@ discard block |
||
366 | 361 | * @throws SodiumException |
367 | 362 | * @throws TypeError |
368 | 363 | */ |
369 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
370 | - { |
|
364 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
371 | 365 | return self::encryptBytes( |
372 | 366 | new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), |
373 | 367 | $message |
@@ -385,8 +379,7 @@ discard block |
||
385 | 379 | * @throws SodiumException |
386 | 380 | * @throws TypeError |
387 | 381 | */ |
388 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
389 | - { |
|
382 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
390 | 383 | return self::encryptBytes( |
391 | 384 | new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), |
392 | 385 | $message |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_ChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | * @param int $n |
19 | 19 | * @return int |
20 | 20 | */ |
21 | - public static function rotate($v, $n) |
|
21 | + public static function rotate( $v, $n ) |
|
22 | 22 | { |
23 | 23 | $v &= 0xffffffff; |
24 | 24 | $n &= 31; |
25 | - return (int) ( |
|
25 | + return (int)( |
|
26 | 26 | 0xffffffff & ( |
27 | - ($v << $n) |
|
27 | + ( $v << $n ) |
|
28 | 28 | | |
29 | - ($v >> (32 - $n)) |
|
29 | + ( $v >> ( 32 - $n ) ) |
|
30 | 30 | ) |
31 | 31 | ); |
32 | 32 | } |
@@ -42,28 +42,28 @@ discard block |
||
42 | 42 | * @param int $d |
43 | 43 | * @return array<int, int> |
44 | 44 | */ |
45 | - protected static function quarterRound($a, $b, $c, $d) |
|
45 | + protected static function quarterRound( $a, $b, $c, $d ) |
|
46 | 46 | { |
47 | 47 | # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
48 | 48 | /** @var int $a */ |
49 | - $a = ($a + $b) & 0xffffffff; |
|
50 | - $d = self::rotate($d ^ $a, 16); |
|
49 | + $a = ( $a + $b ) & 0xffffffff; |
|
50 | + $d = self::rotate( $d ^ $a, 16 ); |
|
51 | 51 | |
52 | 52 | # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
53 | 53 | /** @var int $c */ |
54 | - $c = ($c + $d) & 0xffffffff; |
|
55 | - $b = self::rotate($b ^ $c, 12); |
|
54 | + $c = ( $c + $d ) & 0xffffffff; |
|
55 | + $b = self::rotate( $b ^ $c, 12 ); |
|
56 | 56 | |
57 | 57 | # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
58 | 58 | /** @var int $a */ |
59 | - $a = ($a + $b) & 0xffffffff; |
|
60 | - $d = self::rotate($d ^ $a, 8); |
|
59 | + $a = ( $a + $b ) & 0xffffffff; |
|
60 | + $d = self::rotate( $d ^ $a, 8 ); |
|
61 | 61 | |
62 | 62 | # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
63 | 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); |
|
64 | + $c = ( $c + $d ) & 0xffffffff; |
|
65 | + $b = self::rotate( $b ^ $c, 7 ); |
|
66 | + return array( (int)$a, (int)$b, (int)$c, (int)$d ); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx, |
81 | 81 | $message = '' |
82 | 82 | ) { |
83 | - $bytes = self::strlen($message); |
|
83 | + $bytes = self::strlen( $message ); |
|
84 | 84 | |
85 | 85 | /* |
86 | 86 | j0 = ctx->input[0]; |
@@ -100,71 +100,71 @@ 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 | 120 | $c = ''; |
121 | - for (;;) { |
|
122 | - if ($bytes < 64) { |
|
123 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
121 | + for ( ;; ) { |
|
122 | + if ( $bytes < 64 ) { |
|
123 | + $message .= str_repeat( "\x00", 64 - $bytes ); |
|
124 | 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 | 143 | # for (i = 20; i > 0; i -= 2) { |
144 | - for ($i = 20; $i > 0; $i -= 2) { |
|
144 | + for ( $i = 20; $i > 0; $i -= 2 ) { |
|
145 | 145 | # QUARTERROUND( x0, x4, x8, x12) |
146 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
146 | + list( $x0, $x4, $x8, $x12 ) = self::quarterRound( $x0, $x4, $x8, $x12 ); |
|
147 | 147 | |
148 | 148 | # QUARTERROUND( x1, x5, x9, x13) |
149 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
149 | + list( $x1, $x5, $x9, $x13 ) = self::quarterRound( $x1, $x5, $x9, $x13 ); |
|
150 | 150 | |
151 | 151 | # QUARTERROUND( x2, x6, x10, x14) |
152 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
152 | + list( $x2, $x6, $x10, $x14 ) = self::quarterRound( $x2, $x6, $x10, $x14 ); |
|
153 | 153 | |
154 | 154 | # QUARTERROUND( x3, x7, x11, x15) |
155 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
155 | + list( $x3, $x7, $x11, $x15 ) = self::quarterRound( $x3, $x7, $x11, $x15 ); |
|
156 | 156 | |
157 | 157 | # QUARTERROUND( x0, x5, x10, x15) |
158 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
158 | + list( $x0, $x5, $x10, $x15 ) = self::quarterRound( $x0, $x5, $x10, $x15 ); |
|
159 | 159 | |
160 | 160 | # QUARTERROUND( x1, x6, x11, x12) |
161 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
161 | + list( $x1, $x6, $x11, $x12 ) = self::quarterRound( $x1, $x6, $x11, $x12 ); |
|
162 | 162 | |
163 | 163 | # QUARTERROUND( x2, x7, x8, x13) |
164 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
164 | + list( $x2, $x7, $x8, $x13 ) = self::quarterRound( $x2, $x7, $x8, $x13 ); |
|
165 | 165 | |
166 | 166 | # QUARTERROUND( x3, x4, x9, x14) |
167 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
167 | + list( $x3, $x4, $x9, $x14 ) = self::quarterRound( $x3, $x4, $x9, $x14 ); |
|
168 | 168 | } |
169 | 169 | /* |
170 | 170 | x0 = PLUS(x0, j0); |
@@ -185,37 +185,37 @@ discard block |
||
185 | 185 | x15 = PLUS(x15, j15); |
186 | 186 | */ |
187 | 187 | /** @var int $x0 */ |
188 | - $x0 = ($x0 & 0xffffffff) + $j0; |
|
188 | + $x0 = ( $x0 & 0xffffffff ) + $j0; |
|
189 | 189 | /** @var int $x1 */ |
190 | - $x1 = ($x1 & 0xffffffff) + $j1; |
|
190 | + $x1 = ( $x1 & 0xffffffff ) + $j1; |
|
191 | 191 | /** @var int $x2 */ |
192 | - $x2 = ($x2 & 0xffffffff) + $j2; |
|
192 | + $x2 = ( $x2 & 0xffffffff ) + $j2; |
|
193 | 193 | /** @var int $x3 */ |
194 | - $x3 = ($x3 & 0xffffffff) + $j3; |
|
194 | + $x3 = ( $x3 & 0xffffffff ) + $j3; |
|
195 | 195 | /** @var int $x4 */ |
196 | - $x4 = ($x4 & 0xffffffff) + $j4; |
|
196 | + $x4 = ( $x4 & 0xffffffff ) + $j4; |
|
197 | 197 | /** @var int $x5 */ |
198 | - $x5 = ($x5 & 0xffffffff) + $j5; |
|
198 | + $x5 = ( $x5 & 0xffffffff ) + $j5; |
|
199 | 199 | /** @var int $x6 */ |
200 | - $x6 = ($x6 & 0xffffffff) + $j6; |
|
200 | + $x6 = ( $x6 & 0xffffffff ) + $j6; |
|
201 | 201 | /** @var int $x7 */ |
202 | - $x7 = ($x7 & 0xffffffff) + $j7; |
|
202 | + $x7 = ( $x7 & 0xffffffff ) + $j7; |
|
203 | 203 | /** @var int $x8 */ |
204 | - $x8 = ($x8 & 0xffffffff) + $j8; |
|
204 | + $x8 = ( $x8 & 0xffffffff ) + $j8; |
|
205 | 205 | /** @var int $x9 */ |
206 | - $x9 = ($x9 & 0xffffffff) + $j9; |
|
206 | + $x9 = ( $x9 & 0xffffffff ) + $j9; |
|
207 | 207 | /** @var int $x10 */ |
208 | - $x10 = ($x10 & 0xffffffff) + $j10; |
|
208 | + $x10 = ( $x10 & 0xffffffff ) + $j10; |
|
209 | 209 | /** @var int $x11 */ |
210 | - $x11 = ($x11 & 0xffffffff) + $j11; |
|
210 | + $x11 = ( $x11 & 0xffffffff ) + $j11; |
|
211 | 211 | /** @var int $x12 */ |
212 | - $x12 = ($x12 & 0xffffffff) + $j12; |
|
212 | + $x12 = ( $x12 & 0xffffffff ) + $j12; |
|
213 | 213 | /** @var int $x13 */ |
214 | - $x13 = ($x13 & 0xffffffff) + $j13; |
|
214 | + $x13 = ( $x13 & 0xffffffff ) + $j13; |
|
215 | 215 | /** @var int $x14 */ |
216 | - $x14 = ($x14 & 0xffffffff) + $j14; |
|
216 | + $x14 = ( $x14 & 0xffffffff ) + $j14; |
|
217 | 217 | /** @var int $x15 */ |
218 | - $x15 = ($x15 & 0xffffffff) + $j15; |
|
218 | + $x15 = ( $x15 & 0xffffffff ) + $j15; |
|
219 | 219 | |
220 | 220 | /* |
221 | 221 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
@@ -235,22 +235,22 @@ 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); |
@@ -259,8 +259,8 @@ discard block |
||
259 | 259 | } |
260 | 260 | */ |
261 | 261 | ++$j12; |
262 | - if ($j12 & 0xf0000000) { |
|
263 | - throw new SodiumException('Overflow'); |
|
262 | + if ( $j12 & 0xf0000000 ) { |
|
263 | + throw new SodiumException( 'Overflow' ); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /* |
@@ -281,41 +281,41 @@ 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 | 301 | /* Partial block */ |
302 | - if ($bytes < 64) { |
|
303 | - $c .= self::substr($block, 0, $bytes); |
|
302 | + if ( $bytes < 64 ) { |
|
303 | + $c .= self::substr( $block, 0, $bytes ); |
|
304 | 304 | break; |
305 | 305 | } |
306 | 306 | |
307 | 307 | /* Full block */ |
308 | 308 | $c .= $block; |
309 | 309 | $bytes -= 64; |
310 | - if ($bytes <= 0) { |
|
310 | + if ( $bytes <= 0 ) { |
|
311 | 311 | break; |
312 | 312 | } |
313 | - $message = self::substr($message, 64); |
|
313 | + $message = self::substr( $message, 64 ); |
|
314 | 314 | } |
315 | 315 | /* end for(;;) loop */ |
316 | 316 | |
317 | - $ctx[12] = $j12; |
|
318 | - $ctx[13] = $j13; |
|
317 | + $ctx[ 12 ] = $j12; |
|
318 | + $ctx[ 13 ] = $j13; |
|
319 | 319 | return $c; |
320 | 320 | } |
321 | 321 | |
@@ -329,11 +329,11 @@ discard block |
||
329 | 329 | * @throws SodiumException |
330 | 330 | * @throws TypeError |
331 | 331 | */ |
332 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
332 | + public static function stream( $len = 64, $nonce = '', $key = '' ) |
|
333 | 333 | { |
334 | 334 | return self::encryptBytes( |
335 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), |
|
336 | - str_repeat("\x00", $len) |
|
335 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx( $key, $nonce ), |
|
336 | + str_repeat( "\x00", $len ) |
|
337 | 337 | ); |
338 | 338 | } |
339 | 339 | |
@@ -347,11 +347,11 @@ discard block |
||
347 | 347 | * @throws SodiumException |
348 | 348 | * @throws TypeError |
349 | 349 | */ |
350 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
350 | + public static function ietfStream( $len, $nonce = '', $key = '' ) |
|
351 | 351 | { |
352 | 352 | return self::encryptBytes( |
353 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), |
|
354 | - str_repeat("\x00", $len) |
|
353 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( $key, $nonce ), |
|
354 | + str_repeat( "\x00", $len ) |
|
355 | 355 | ); |
356 | 356 | } |
357 | 357 | |
@@ -366,10 +366,10 @@ discard block |
||
366 | 366 | * @throws SodiumException |
367 | 367 | * @throws TypeError |
368 | 368 | */ |
369 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
369 | + public static function ietfStreamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
370 | 370 | { |
371 | 371 | return self::encryptBytes( |
372 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
372 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( $key, $nonce, $ic ), |
|
373 | 373 | $message |
374 | 374 | ); |
375 | 375 | } |
@@ -385,10 +385,10 @@ discard block |
||
385 | 385 | * @throws SodiumException |
386 | 386 | * @throws TypeError |
387 | 387 | */ |
388 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
388 | + public static function streamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
389 | 389 | { |
390 | 390 | return self::encryptBytes( |
391 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), |
|
391 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx( $key, $nonce, $ic ), |
|
392 | 392 | $message |
393 | 393 | ); |
394 | 394 | } |
@@ -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 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_HChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20 |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20 { |
|
12 | 11 | /** |
13 | 12 | * @param string $in |
14 | 13 | * @param string $key |
@@ -16,8 +15,7 @@ discard block |
||
16 | 15 | * @return string |
17 | 16 | * @throws TypeError |
18 | 17 | */ |
19 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
20 | - { |
|
18 | + public static function hChaCha20($in = '', $key = '', $c = null) { |
|
21 | 19 | $ctx = array(); |
22 | 20 | |
23 | 21 | if ($c === null) { |
@@ -51,8 +49,7 @@ discard block |
||
51 | 49 | * @return string |
52 | 50 | * @throws TypeError |
53 | 51 | */ |
54 | - protected static function hChaCha20Bytes(array $ctx) |
|
55 | - { |
|
52 | + protected static function hChaCha20Bytes(array $ctx) { |
|
56 | 53 | $x0 = (int) $ctx[0]; |
57 | 54 | $x1 = (int) $ctx[1]; |
58 | 55 | $x2 = (int) $ctx[2]; |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_HChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -16,34 +16,34 @@ discard block |
||
16 | 16 | * @return string |
17 | 17 | * @throws TypeError |
18 | 18 | */ |
19 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
19 | + public static function hChaCha20( $in = '', $key = '', $c = null ) |
|
20 | 20 | { |
21 | 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; |
|
23 | + if ( $c === null ) { |
|
24 | + $ctx[ 0 ] = 0x61707865; |
|
25 | + $ctx[ 1 ] = 0x3320646e; |
|
26 | + $ctx[ 2 ] = 0x79622d32; |
|
27 | + $ctx[ 3 ] = 0x6b206574; |
|
28 | 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)); |
|
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 | 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); |
|
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 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -51,58 +51,58 @@ discard block |
||
51 | 51 | * @return string |
52 | 52 | * @throws TypeError |
53 | 53 | */ |
54 | - protected static function hChaCha20Bytes(array $ctx) |
|
54 | + protected static function hChaCha20Bytes( array $ctx ) |
|
55 | 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]; |
|
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) { |
|
73 | + for ( $i = 0; $i < 10; ++$i ) { |
|
74 | 74 | # QUARTERROUND( x0, x4, x8, x12) |
75 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
75 | + list( $x0, $x4, $x8, $x12 ) = self::quarterRound( $x0, $x4, $x8, $x12 ); |
|
76 | 76 | |
77 | 77 | # QUARTERROUND( x1, x5, x9, x13) |
78 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
78 | + list( $x1, $x5, $x9, $x13 ) = self::quarterRound( $x1, $x5, $x9, $x13 ); |
|
79 | 79 | |
80 | 80 | # QUARTERROUND( x2, x6, x10, x14) |
81 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
81 | + list( $x2, $x6, $x10, $x14 ) = self::quarterRound( $x2, $x6, $x10, $x14 ); |
|
82 | 82 | |
83 | 83 | # QUARTERROUND( x3, x7, x11, x15) |
84 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
84 | + list( $x3, $x7, $x11, $x15 ) = self::quarterRound( $x3, $x7, $x11, $x15 ); |
|
85 | 85 | |
86 | 86 | # QUARTERROUND( x0, x5, x10, x15) |
87 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
87 | + list( $x0, $x5, $x10, $x15 ) = self::quarterRound( $x0, $x5, $x10, $x15 ); |
|
88 | 88 | |
89 | 89 | # QUARTERROUND( x1, x6, x11, x12) |
90 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
90 | + list( $x1, $x6, $x11, $x12 ) = self::quarterRound( $x1, $x6, $x11, $x12 ); |
|
91 | 91 | |
92 | 92 | # QUARTERROUND( x2, x7, x8, x13) |
93 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
93 | + list( $x2, $x7, $x8, $x13 ) = self::quarterRound( $x2, $x7, $x8, $x13 ); |
|
94 | 94 | |
95 | 95 | # QUARTERROUND( x3, x4, x9, x14) |
96 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
96 | + list( $x3, $x4, $x9, $x14 ) = self::quarterRound( $x3, $x4, $x9, $x14 ); |
|
97 | 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)); |
|
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 | 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,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_HSalsa20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -23,74 +23,74 @@ discard block |
||
23 | 23 | * @return string |
24 | 24 | * @throws TypeError |
25 | 25 | */ |
26 | - public static function hsalsa20($in, $k, $c = null) |
|
26 | + public static function hsalsa20( $in, $k, $c = null ) |
|
27 | 27 | { |
28 | - if ($c === null) { |
|
28 | + if ( $c === null ) { |
|
29 | 29 | $x0 = 0x61707865; |
30 | 30 | $x5 = 0x3320646e; |
31 | 31 | $x10 = 0x79622d32; |
32 | 32 | $x15 = 0x6b206574; |
33 | 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)); |
|
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 | 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)); |
|
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); |
|
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 | 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); |
|
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 | 95 | } |
96 | 96 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_HSalsa20 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20 { |
|
12 | 11 | /** |
13 | 12 | * Calculate an hsalsa20 hash of a single block |
14 | 13 | * |
@@ -23,8 +22,7 @@ discard block |
||
23 | 22 | * @return string |
24 | 23 | * @throws TypeError |
25 | 24 | */ |
26 | - public static function hsalsa20($in, $k, $c = null) |
|
27 | - { |
|
25 | + public static function hsalsa20($in, $k, $c = null) { |
|
28 | 26 | if ($c === null) { |
29 | 27 | $x0 = 0x61707865; |
30 | 28 | $x5 = 0x3320646e; |
@@ -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 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_Poly1305_State |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util { |
|
12 | 11 | /** |
13 | 12 | * @var array<int, int> |
14 | 13 | */ |
@@ -48,8 +47,7 @@ discard block |
||
48 | 47 | * @throws InvalidArgumentException |
49 | 48 | * @throws TypeError |
50 | 49 | */ |
51 | - public function __construct($key = '') |
|
52 | - { |
|
50 | + public function __construct($key = '') { |
|
53 | 51 | if (self::strlen($key) < 32) { |
54 | 52 | throw new InvalidArgumentException( |
55 | 53 | 'Poly1305 requires a 32-byte key' |
@@ -82,8 +80,7 @@ discard block |
||
82 | 80 | /** |
83 | 81 | * Zero internal buffer upon destruction |
84 | 82 | */ |
85 | - public function __destruct() |
|
86 | - { |
|
83 | + public function __destruct() { |
|
87 | 84 | $this->r[0] ^= $this->r[0]; |
88 | 85 | $this->r[1] ^= $this->r[1]; |
89 | 86 | $this->r[2] ^= $this->r[2]; |
@@ -110,8 +107,7 @@ discard block |
||
110 | 107 | * @throws SodiumException |
111 | 108 | * @throws TypeError |
112 | 109 | */ |
113 | - public function update($message = '') |
|
114 | - { |
|
110 | + public function update($message = '') { |
|
115 | 111 | $bytes = self::strlen($message); |
116 | 112 | if ($bytes < 1) { |
117 | 113 | return $this; |
@@ -176,8 +172,7 @@ discard block |
||
176 | 172 | * @return self |
177 | 173 | * @throws TypeError |
178 | 174 | */ |
179 | - public function blocks($message, $bytes) |
|
180 | - { |
|
175 | + public function blocks($message, $bytes) { |
|
181 | 176 | if (self::strlen($message) < 16) { |
182 | 177 | $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
183 | 178 | } |
@@ -310,8 +305,7 @@ discard block |
||
310 | 305 | * @return string |
311 | 306 | * @throws TypeError |
312 | 307 | */ |
313 | - public function finish() |
|
314 | - { |
|
308 | + public function finish() { |
|
315 | 309 | /* process the remaining block */ |
316 | 310 | if ($this->leftover) { |
317 | 311 | $i = $this->leftover; |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Poly1305_State', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -48,31 +48,31 @@ discard block |
||
48 | 48 | * @throws InvalidArgumentException |
49 | 49 | * @throws TypeError |
50 | 50 | */ |
51 | - public function __construct($key = '') |
|
51 | + public function __construct( $key = '' ) |
|
52 | 52 | { |
53 | - if (self::strlen($key) < 32) { |
|
53 | + if ( self::strlen( $key ) < 32 ) { |
|
54 | 54 | throw new InvalidArgumentException( |
55 | 55 | 'Poly1305 requires a 32-byte key' |
56 | 56 | ); |
57 | 57 | } |
58 | 58 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
59 | 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) |
|
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 | 65 | ); |
66 | 66 | |
67 | 67 | /* h = 0 */ |
68 | - $this->h = array(0, 0, 0, 0, 0); |
|
68 | + $this->h = array( 0, 0, 0, 0, 0 ); |
|
69 | 69 | |
70 | 70 | /* save pad for later */ |
71 | 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)), |
|
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 | 76 | ); |
77 | 77 | |
78 | 78 | $this->leftover = 0; |
@@ -84,20 +84,20 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function __destruct() |
86 | 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]; |
|
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 | 101 | $this->leftover = 0; |
102 | 102 | $this->final = true; |
103 | 103 | } |
@@ -110,60 +110,60 @@ discard block |
||
110 | 110 | * @throws SodiumException |
111 | 111 | * @throws TypeError |
112 | 112 | */ |
113 | - public function update($message = '') |
|
113 | + public function update( $message = '' ) |
|
114 | 114 | { |
115 | - $bytes = self::strlen($message); |
|
116 | - if ($bytes < 1) { |
|
115 | + $bytes = self::strlen( $message ); |
|
116 | + if ( $bytes < 1 ) { |
|
117 | 117 | return $this; |
118 | 118 | } |
119 | 119 | |
120 | 120 | /* handle leftover */ |
121 | - if ($this->leftover) { |
|
121 | + if ( $this->leftover ) { |
|
122 | 122 | $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover; |
123 | - if ($want > $bytes) { |
|
123 | + if ( $want > $bytes ) { |
|
124 | 124 | $want = $bytes; |
125 | 125 | } |
126 | - for ($i = 0; $i < $want; ++$i) { |
|
127 | - $mi = self::chrToInt($message[$i]); |
|
128 | - $this->buffer[$this->leftover + $i] = $mi; |
|
126 | + for ( $i = 0; $i < $want; ++$i ) { |
|
127 | + $mi = self::chrToInt( $message[ $i ] ); |
|
128 | + $this->buffer[ $this->leftover + $i ] = $mi; |
|
129 | 129 | } |
130 | 130 | // We snip off the leftmost bytes. |
131 | - $message = self::substr($message, $want); |
|
132 | - $bytes = self::strlen($message); |
|
131 | + $message = self::substr( $message, $want ); |
|
132 | + $bytes = self::strlen( $message ); |
|
133 | 133 | $this->leftover += $want; |
134 | - if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
134 | + if ( $this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
135 | 135 | // We still don't have enough to run $this->blocks() |
136 | 136 | return $this; |
137 | 137 | } |
138 | 138 | |
139 | 139 | $this->blocks( |
140 | - self::intArrayToString($this->buffer), |
|
140 | + self::intArrayToString( $this->buffer ), |
|
141 | 141 | ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
142 | 142 | ); |
143 | 143 | $this->leftover = 0; |
144 | 144 | } |
145 | 145 | |
146 | 146 | /* process full blocks */ |
147 | - if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) { |
|
147 | + if ( $bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE ) { |
|
148 | 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); |
|
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 | 156 | } |
157 | 157 | } |
158 | 158 | } |
159 | 159 | |
160 | 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; |
|
161 | + if ( $bytes ) { |
|
162 | + for ( $i = 0; $i < $bytes; ++$i ) { |
|
163 | + $mi = self::chrToInt( $message[ $i ] ); |
|
164 | + $this->buffer[ $this->leftover + $i ] = $mi; |
|
165 | 165 | } |
166 | - $this->leftover = (int) $this->leftover + $bytes; |
|
166 | + $this->leftover = (int)$this->leftover + $bytes; |
|
167 | 167 | } |
168 | 168 | return $this; |
169 | 169 | } |
@@ -176,77 +176,77 @@ discard block |
||
176 | 176 | * @return self |
177 | 177 | * @throws TypeError |
178 | 178 | */ |
179 | - public function blocks($message, $bytes) |
|
179 | + public function blocks( $message, $bytes ) |
|
180 | 180 | { |
181 | - if (self::strlen($message) < 16) { |
|
182 | - $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
181 | + if ( self::strlen( $message ) < 16 ) { |
|
182 | + $message = str_pad( $message, 16, "\x00", STR_PAD_RIGHT ); |
|
183 | 183 | } |
184 | 184 | /** @var int $hibit */ |
185 | 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) { |
|
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 | 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; |
|
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 | 210 | |
211 | 211 | /* h *= r */ |
212 | 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) |
|
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 | 218 | ); |
219 | 219 | |
220 | 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) |
|
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 | 226 | ); |
227 | 227 | |
228 | 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) |
|
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 | 234 | ); |
235 | 235 | |
236 | 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) |
|
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 | 242 | ); |
243 | 243 | |
244 | 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) |
|
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 | 250 | ); |
251 | 251 | |
252 | 252 | /* (partial) h %= p */ |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | $c = $d4 >> 26; |
279 | 279 | /** @var int $h4 */ |
280 | 280 | $h4 = $d4 & 0x3ffffff; |
281 | - $h0 += (int) self::mul($c, 5, 3); |
|
281 | + $h0 += (int)self::mul( $c, 5, 3 ); |
|
282 | 282 | |
283 | 283 | /** @var int $c */ |
284 | 284 | $c = $h0 >> 26; |
@@ -295,11 +295,11 @@ discard block |
||
295 | 295 | } |
296 | 296 | |
297 | 297 | $this->h = array( |
298 | - (int) ($h0 & 0xffffffff), |
|
299 | - (int) ($h1 & 0xffffffff), |
|
300 | - (int) ($h2 & 0xffffffff), |
|
301 | - (int) ($h3 & 0xffffffff), |
|
302 | - (int) ($h4 & 0xffffffff) |
|
298 | + (int)( $h0 & 0xffffffff ), |
|
299 | + (int)( $h1 & 0xffffffff ), |
|
300 | + (int)( $h2 & 0xffffffff ), |
|
301 | + (int)( $h3 & 0xffffffff ), |
|
302 | + (int)( $h4 & 0xffffffff ) |
|
303 | 303 | ); |
304 | 304 | return $this; |
305 | 305 | } |
@@ -313,16 +313,16 @@ discard block |
||
313 | 313 | public function finish() |
314 | 314 | { |
315 | 315 | /* process the remaining block */ |
316 | - if ($this->leftover) { |
|
316 | + if ( $this->leftover ) { |
|
317 | 317 | $i = $this->leftover; |
318 | - $this->buffer[$i++] = 1; |
|
319 | - for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) { |
|
320 | - $this->buffer[$i] = 0; |
|
318 | + $this->buffer[ $i++ ] = 1; |
|
319 | + for ( ; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i ) { |
|
320 | + $this->buffer[ $i ] = 0; |
|
321 | 321 | } |
322 | 322 | $this->final = true; |
323 | 323 | $this->blocks( |
324 | 324 | self::substr( |
325 | - self::intArrayToString($this->buffer), |
|
325 | + self::intArrayToString( $this->buffer ), |
|
326 | 326 | 0, |
327 | 327 | ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE |
328 | 328 | ), |
@@ -330,11 +330,11 @@ discard block |
||
330 | 330 | ); |
331 | 331 | } |
332 | 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]; |
|
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 | 338 | |
339 | 339 | /** @var int $c */ |
340 | 340 | $c = $h1 >> 26; |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | $c = $h4 >> 26; |
356 | 356 | $h4 &= 0x3ffffff; |
357 | 357 | /** @var int $h0 */ |
358 | - $h0 += self::mul($c, 5, 3); |
|
358 | + $h0 += self::mul( $c, 5, 3 ); |
|
359 | 359 | /** @var int $c */ |
360 | 360 | $c = $h0 >> 26; |
361 | 361 | /** @var int $h0 */ |
@@ -392,11 +392,11 @@ discard block |
||
392 | 392 | $g3 &= 0x3ffffff; |
393 | 393 | |
394 | 394 | /** @var int $g4 */ |
395 | - $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff; |
|
395 | + $g4 = ( $h4 + $c - ( 1 << 26 ) ) & 0xffffffff; |
|
396 | 396 | |
397 | 397 | /* select h if h < p, or h + -p if h >= p */ |
398 | 398 | /** @var int $mask */ |
399 | - $mask = ($g4 >> 31) - 1; |
|
399 | + $mask = ( $g4 >> 31 ) - 1; |
|
400 | 400 | |
401 | 401 | $g0 &= $mask; |
402 | 402 | $g1 &= $mask; |
@@ -407,39 +407,39 @@ discard block |
||
407 | 407 | /** @var int $mask */ |
408 | 408 | $mask = ~$mask & 0xffffffff; |
409 | 409 | /** @var int $h0 */ |
410 | - $h0 = ($h0 & $mask) | $g0; |
|
410 | + $h0 = ( $h0 & $mask ) | $g0; |
|
411 | 411 | /** @var int $h1 */ |
412 | - $h1 = ($h1 & $mask) | $g1; |
|
412 | + $h1 = ( $h1 & $mask ) | $g1; |
|
413 | 413 | /** @var int $h2 */ |
414 | - $h2 = ($h2 & $mask) | $g2; |
|
414 | + $h2 = ( $h2 & $mask ) | $g2; |
|
415 | 415 | /** @var int $h3 */ |
416 | - $h3 = ($h3 & $mask) | $g3; |
|
416 | + $h3 = ( $h3 & $mask ) | $g3; |
|
417 | 417 | /** @var int $h4 */ |
418 | - $h4 = ($h4 & $mask) | $g4; |
|
418 | + $h4 = ( $h4 & $mask ) | $g4; |
|
419 | 419 | |
420 | 420 | /* h = h % (2^128) */ |
421 | 421 | /** @var int $h0 */ |
422 | - $h0 = (($h0) | ($h1 << 26)) & 0xffffffff; |
|
422 | + $h0 = ( ( $h0 ) | ( $h1 << 26 ) ) & 0xffffffff; |
|
423 | 423 | /** @var int $h1 */ |
424 | - $h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff; |
|
424 | + $h1 = ( ( $h1 >> 6 ) | ( $h2 << 20 ) ) & 0xffffffff; |
|
425 | 425 | /** @var int $h2 */ |
426 | - $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff; |
|
426 | + $h2 = ( ( $h2 >> 12 ) | ( $h3 << 14 ) ) & 0xffffffff; |
|
427 | 427 | /** @var int $h3 */ |
428 | - $h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff; |
|
428 | + $h3 = ( ( $h3 >> 18 ) | ( $h4 << 8 ) ) & 0xffffffff; |
|
429 | 429 | |
430 | 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); |
|
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 | 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 | } |
@@ -6,8 +6,7 @@ discard block |
||
6 | 6 | * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises. |
7 | 7 | * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) |
8 | 8 | */ |
9 | -class ParagonIE_Sodium_Core_Base64_Original |
|
10 | -{ |
|
9 | +class ParagonIE_Sodium_Core_Base64_Original { |
|
11 | 10 | // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
12 | 11 | /** |
13 | 12 | * Encode into Base64 |
@@ -18,8 +17,7 @@ discard block |
||
18 | 17 | * @return string |
19 | 18 | * @throws TypeError |
20 | 19 | */ |
21 | - public static function encode($src) |
|
22 | - { |
|
20 | + public static function encode($src) { |
|
23 | 21 | return self::doEncode($src, true); |
24 | 22 | } |
25 | 23 | |
@@ -32,8 +30,7 @@ discard block |
||
32 | 30 | * @return string |
33 | 31 | * @throws TypeError |
34 | 32 | */ |
35 | - public static function encodeUnpadded($src) |
|
36 | - { |
|
33 | + public static function encodeUnpadded($src) { |
|
37 | 34 | return self::doEncode($src, false); |
38 | 35 | } |
39 | 36 | |
@@ -43,8 +40,7 @@ discard block |
||
43 | 40 | * @return string |
44 | 41 | * @throws TypeError |
45 | 42 | */ |
46 | - protected static function doEncode($src, $pad = true) |
|
47 | - { |
|
43 | + protected static function doEncode($src, $pad = true) { |
|
48 | 44 | $dest = ''; |
49 | 45 | $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
50 | 46 | // Main loop (no padding): |
@@ -99,8 +95,7 @@ discard block |
||
99 | 95 | * @throws TypeError |
100 | 96 | * @psalm-suppress RedundantCondition |
101 | 97 | */ |
102 | - public static function decode($src, $strictPadding = false) |
|
103 | - { |
|
98 | + public static function decode($src, $strictPadding = false) { |
|
104 | 99 | // Remove padding |
105 | 100 | $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
106 | 101 | if ($srcLen === 0) { |
@@ -198,8 +193,7 @@ discard block |
||
198 | 193 | * @param int $src |
199 | 194 | * @return int |
200 | 195 | */ |
201 | - protected static function decode6Bits($src) |
|
202 | - { |
|
196 | + protected static function decode6Bits($src) { |
|
203 | 197 | $ret = -1; |
204 | 198 | |
205 | 199 | // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
@@ -227,8 +221,7 @@ discard block |
||
227 | 221 | * @param int $src |
228 | 222 | * @return string |
229 | 223 | */ |
230 | - protected static function encode6Bits($src) |
|
231 | - { |
|
224 | + protected static function encode6Bits($src) { |
|
232 | 225 | $diff = 0x41; |
233 | 226 | |
234 | 227 | // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
@@ -18,9 +18,9 @@ discard block |
||
18 | 18 | * @return string |
19 | 19 | * @throws TypeError |
20 | 20 | */ |
21 | - public static function encode($src) |
|
21 | + public static function encode( $src ) |
|
22 | 22 | { |
23 | - return self::doEncode($src, true); |
|
23 | + return self::doEncode( $src, true ); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -32,9 +32,9 @@ discard block |
||
32 | 32 | * @return string |
33 | 33 | * @throws TypeError |
34 | 34 | */ |
35 | - public static function encodeUnpadded($src) |
|
35 | + public static function encodeUnpadded( $src ) |
|
36 | 36 | { |
37 | - return self::doEncode($src, false); |
|
37 | + return self::doEncode( $src, false ); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -43,43 +43,43 @@ discard block |
||
43 | 43 | * @return string |
44 | 44 | * @throws TypeError |
45 | 45 | */ |
46 | - protected static function doEncode($src, $pad = true) |
|
46 | + protected static function doEncode( $src, $pad = true ) |
|
47 | 47 | { |
48 | 48 | $dest = ''; |
49 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
49 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
50 | 50 | // Main loop (no padding): |
51 | - for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
51 | + for ( $i = 0; $i + 3 <= $srcLen; $i += 3 ) { |
|
52 | 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]; |
|
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 | 57 | |
58 | 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); |
|
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 | 63 | } |
64 | 64 | // The last chunk, which may have padding: |
65 | - if ($i < $srcLen) { |
|
65 | + if ( $i < $srcLen ) { |
|
66 | 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]; |
|
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 | 71 | $dest .= |
72 | - self::encode6Bits($b0 >> 2) . |
|
73 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | - self::encode6Bits(($b1 << 2) & 63); |
|
75 | - if ($pad) { |
|
72 | + self::encode6Bits( $b0 >> 2 ) . |
|
73 | + self::encode6Bits( ( ( $b0 << 4 ) | ( $b1 >> 4 ) ) & 63 ) . |
|
74 | + self::encode6Bits( ( $b1 << 2 ) & 63 ); |
|
75 | + if ( $pad ) { |
|
76 | 76 | $dest .= '='; |
77 | 77 | } |
78 | 78 | } else { |
79 | 79 | $dest .= |
80 | - self::encode6Bits( $b0 >> 2) . |
|
81 | - self::encode6Bits(($b0 << 4) & 63); |
|
82 | - if ($pad) { |
|
80 | + self::encode6Bits( $b0 >> 2 ) . |
|
81 | + self::encode6Bits( ( $b0 << 4 ) & 63 ); |
|
82 | + if ( $pad ) { |
|
83 | 83 | $dest .= '=='; |
84 | 84 | } |
85 | 85 | } |
@@ -99,86 +99,86 @@ discard block |
||
99 | 99 | * @throws TypeError |
100 | 100 | * @psalm-suppress RedundantCondition |
101 | 101 | */ |
102 | - public static function decode($src, $strictPadding = false) |
|
102 | + public static function decode( $src, $strictPadding = false ) |
|
103 | 103 | { |
104 | 104 | // Remove padding |
105 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | - if ($srcLen === 0) { |
|
105 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
106 | + if ( $srcLen === 0 ) { |
|
107 | 107 | return ''; |
108 | 108 | } |
109 | 109 | |
110 | - if ($strictPadding) { |
|
111 | - if (($srcLen & 3) === 0) { |
|
112 | - if ($src[$srcLen - 1] === '=') { |
|
110 | + if ( $strictPadding ) { |
|
111 | + if ( ( $srcLen & 3 ) === 0 ) { |
|
112 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
113 | 113 | $srcLen--; |
114 | - if ($src[$srcLen - 1] === '=') { |
|
114 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
115 | 115 | $srcLen--; |
116 | 116 | } |
117 | 117 | } |
118 | 118 | } |
119 | - if (($srcLen & 3) === 1) { |
|
119 | + if ( ( $srcLen & 3 ) === 1 ) { |
|
120 | 120 | throw new RangeException( |
121 | 121 | 'Incorrect padding' |
122 | 122 | ); |
123 | 123 | } |
124 | - if ($src[$srcLen - 1] === '=') { |
|
124 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
125 | 125 | throw new RangeException( |
126 | 126 | 'Incorrect padding' |
127 | 127 | ); |
128 | 128 | } |
129 | 129 | } else { |
130 | - $src = rtrim($src, '='); |
|
131 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
130 | + $src = rtrim( $src, '=' ); |
|
131 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | $err = 0; |
135 | 135 | $dest = ''; |
136 | 136 | // Main loop (no padding): |
137 | - for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
137 | + for ( $i = 0; $i + 4 <= $srcLen; $i += 4 ) { |
|
138 | 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]); |
|
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 | 144 | |
145 | 145 | $dest .= pack( |
146 | 146 | 'CCC', |
147 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | - ((($c2 << 6) | $c3) & 0xff) |
|
147 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ), |
|
148 | + ( ( ( $c1 << 4 ) | ( $c2 >> 2 ) ) & 0xff ), |
|
149 | + ( ( ( $c2 << 6 ) | $c3 ) & 0xff ) |
|
150 | 150 | ); |
151 | - $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
151 | + $err |= ( $c0 | $c1 | $c2 | $c3 ) >> 8; |
|
152 | 152 | } |
153 | 153 | // The last chunk, which may have padding: |
154 | - if ($i < $srcLen) { |
|
154 | + if ( $i < $srcLen ) { |
|
155 | 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]); |
|
156 | + $chunk = unpack( 'C*', ParagonIE_Sodium_Core_Util::substr( $src, $i, $srcLen - $i ) ); |
|
157 | + $c0 = self::decode6Bits( $chunk[ 1 ] ); |
|
158 | 158 | |
159 | - if ($i + 2 < $srcLen) { |
|
160 | - $c1 = self::decode6Bits($chunk[2]); |
|
161 | - $c2 = self::decode6Bits($chunk[3]); |
|
159 | + if ( $i + 2 < $srcLen ) { |
|
160 | + $c1 = self::decode6Bits( $chunk[ 2 ] ); |
|
161 | + $c2 = self::decode6Bits( $chunk[ 3 ] ); |
|
162 | 162 | $dest .= pack( |
163 | 163 | 'CC', |
164 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
164 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ), |
|
165 | + ( ( ( $c1 << 4 ) | ( $c2 >> 2 ) ) & 0xff ) |
|
166 | 166 | ); |
167 | - $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | - } elseif ($i + 1 < $srcLen) { |
|
169 | - $c1 = self::decode6Bits($chunk[2]); |
|
167 | + $err |= ( $c0 | $c1 | $c2 ) >> 8; |
|
168 | + } elseif ( $i + 1 < $srcLen ) { |
|
169 | + $c1 = self::decode6Bits( $chunk[ 2 ] ); |
|
170 | 170 | $dest .= pack( |
171 | 171 | 'C', |
172 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
172 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ) |
|
173 | 173 | ); |
174 | - $err |= ($c0 | $c1) >> 8; |
|
175 | - } elseif ($i < $srcLen && $strictPadding) { |
|
174 | + $err |= ( $c0 | $c1 ) >> 8; |
|
175 | + } elseif ( $i < $srcLen && $strictPadding ) { |
|
176 | 176 | $err |= 1; |
177 | 177 | } |
178 | 178 | } |
179 | 179 | /** @var bool $check */ |
180 | - $check = ($err === 0); |
|
181 | - if (!$check) { |
|
180 | + $check = ( $err === 0 ); |
|
181 | + if ( ! $check ) { |
|
182 | 182 | throw new RangeException( |
183 | 183 | 'Base64::decode() only expects characters in the correct base64 alphabet' |
184 | 184 | ); |
@@ -198,24 +198,24 @@ discard block |
||
198 | 198 | * @param int $src |
199 | 199 | * @return int |
200 | 200 | */ |
201 | - protected static function decode6Bits($src) |
|
201 | + protected static function decode6Bits( $src ) |
|
202 | 202 | { |
203 | 203 | $ret = -1; |
204 | 204 | |
205 | 205 | // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
206 | - $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
206 | + $ret += ( ( ( 0x40 - $src ) & ( $src - 0x5b ) ) >> 8 ) & ( $src - 64 ); |
|
207 | 207 | |
208 | 208 | // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
209 | - $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
209 | + $ret += ( ( ( 0x60 - $src ) & ( $src - 0x7b ) ) >> 8 ) & ( $src - 70 ); |
|
210 | 210 | |
211 | 211 | // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
212 | - $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
212 | + $ret += ( ( ( 0x2f - $src ) & ( $src - 0x3a ) ) >> 8 ) & ( $src + 5 ); |
|
213 | 213 | |
214 | 214 | // if ($src == 0x2b) $ret += 62 + 1; |
215 | - $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63; |
|
215 | + $ret += ( ( ( 0x2a - $src ) & ( $src - 0x2c ) ) >> 8 ) & 63; |
|
216 | 216 | |
217 | 217 | // if ($src == 0x2f) ret += 63 + 1; |
218 | - $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64; |
|
218 | + $ret += ( ( ( 0x2e - $src ) & ( $src - 0x30 ) ) >> 8 ) & 64; |
|
219 | 219 | |
220 | 220 | return $ret; |
221 | 221 | } |
@@ -227,22 +227,22 @@ discard block |
||
227 | 227 | * @param int $src |
228 | 228 | * @return string |
229 | 229 | */ |
230 | - protected static function encode6Bits($src) |
|
230 | + protected static function encode6Bits( $src ) |
|
231 | 231 | { |
232 | 232 | $diff = 0x41; |
233 | 233 | |
234 | 234 | // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
235 | - $diff += ((25 - $src) >> 8) & 6; |
|
235 | + $diff += ( ( 25 - $src ) >> 8 ) & 6; |
|
236 | 236 | |
237 | 237 | // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
238 | - $diff -= ((51 - $src) >> 8) & 75; |
|
238 | + $diff -= ( ( 51 - $src ) >> 8 ) & 75; |
|
239 | 239 | |
240 | 240 | // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15 |
241 | - $diff -= ((61 - $src) >> 8) & 15; |
|
241 | + $diff -= ( ( 61 - $src ) >> 8 ) & 15; |
|
242 | 242 | |
243 | 243 | // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3 |
244 | - $diff += ((62 - $src) >> 8) & 3; |
|
244 | + $diff += ( ( 62 - $src ) >> 8 ) & 3; |
|
245 | 245 | |
246 | - return pack('C', $src + $diff); |
|
246 | + return pack( 'C', $src + $diff ); |
|
247 | 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 | } |
@@ -6,8 +6,7 @@ discard block |
||
6 | 6 | * Copyright (c) 2016 - 2018 Paragon Initiative Enterprises. |
7 | 7 | * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com) |
8 | 8 | */ |
9 | -class ParagonIE_Sodium_Core_Base64_UrlSafe |
|
10 | -{ |
|
9 | +class ParagonIE_Sodium_Core_Base64_UrlSafe { |
|
11 | 10 | // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE |
12 | 11 | /** |
13 | 12 | * Encode into Base64 |
@@ -18,8 +17,7 @@ discard block |
||
18 | 17 | * @return string |
19 | 18 | * @throws TypeError |
20 | 19 | */ |
21 | - public static function encode($src) |
|
22 | - { |
|
20 | + public static function encode($src) { |
|
23 | 21 | return self::doEncode($src, true); |
24 | 22 | } |
25 | 23 | |
@@ -32,8 +30,7 @@ discard block |
||
32 | 30 | * @return string |
33 | 31 | * @throws TypeError |
34 | 32 | */ |
35 | - public static function encodeUnpadded($src) |
|
36 | - { |
|
33 | + public static function encodeUnpadded($src) { |
|
37 | 34 | return self::doEncode($src, false); |
38 | 35 | } |
39 | 36 | |
@@ -43,8 +40,7 @@ discard block |
||
43 | 40 | * @return string |
44 | 41 | * @throws TypeError |
45 | 42 | */ |
46 | - protected static function doEncode($src, $pad = true) |
|
47 | - { |
|
43 | + protected static function doEncode($src, $pad = true) { |
|
48 | 44 | $dest = ''; |
49 | 45 | $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
50 | 46 | // Main loop (no padding): |
@@ -99,8 +95,7 @@ discard block |
||
99 | 95 | * @throws TypeError |
100 | 96 | * @psalm-suppress RedundantCondition |
101 | 97 | */ |
102 | - public static function decode($src, $strictPadding = false) |
|
103 | - { |
|
98 | + public static function decode($src, $strictPadding = false) { |
|
104 | 99 | // Remove padding |
105 | 100 | $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
106 | 101 | if ($srcLen === 0) { |
@@ -197,8 +192,7 @@ discard block |
||
197 | 192 | * @param int $src |
198 | 193 | * @return int |
199 | 194 | */ |
200 | - protected static function decode6Bits($src) |
|
201 | - { |
|
195 | + protected static function decode6Bits($src) { |
|
202 | 196 | $ret = -1; |
203 | 197 | |
204 | 198 | // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
@@ -226,8 +220,7 @@ discard block |
||
226 | 220 | * @param int $src |
227 | 221 | * @return string |
228 | 222 | */ |
229 | - protected static function encode6Bits($src) |
|
230 | - { |
|
223 | + protected static function encode6Bits($src) { |
|
231 | 224 | $diff = 0x41; |
232 | 225 | |
233 | 226 | // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
@@ -18,9 +18,9 @@ discard block |
||
18 | 18 | * @return string |
19 | 19 | * @throws TypeError |
20 | 20 | */ |
21 | - public static function encode($src) |
|
21 | + public static function encode( $src ) |
|
22 | 22 | { |
23 | - return self::doEncode($src, true); |
|
23 | + return self::doEncode( $src, true ); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -32,9 +32,9 @@ discard block |
||
32 | 32 | * @return string |
33 | 33 | * @throws TypeError |
34 | 34 | */ |
35 | - public static function encodeUnpadded($src) |
|
35 | + public static function encodeUnpadded( $src ) |
|
36 | 36 | { |
37 | - return self::doEncode($src, false); |
|
37 | + return self::doEncode( $src, false ); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -43,43 +43,43 @@ discard block |
||
43 | 43 | * @return string |
44 | 44 | * @throws TypeError |
45 | 45 | */ |
46 | - protected static function doEncode($src, $pad = true) |
|
46 | + protected static function doEncode( $src, $pad = true ) |
|
47 | 47 | { |
48 | 48 | $dest = ''; |
49 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
49 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
50 | 50 | // Main loop (no padding): |
51 | - for ($i = 0; $i + 3 <= $srcLen; $i += 3) { |
|
51 | + for ( $i = 0; $i + 3 <= $srcLen; $i += 3 ) { |
|
52 | 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]; |
|
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 | 57 | |
58 | 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); |
|
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 | 63 | } |
64 | 64 | // The last chunk, which may have padding: |
65 | - if ($i < $srcLen) { |
|
65 | + if ( $i < $srcLen ) { |
|
66 | 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]; |
|
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 | 71 | $dest .= |
72 | - self::encode6Bits($b0 >> 2) . |
|
73 | - self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) . |
|
74 | - self::encode6Bits(($b1 << 2) & 63); |
|
75 | - if ($pad) { |
|
72 | + self::encode6Bits( $b0 >> 2 ) . |
|
73 | + self::encode6Bits( ( ( $b0 << 4 ) | ( $b1 >> 4 ) ) & 63 ) . |
|
74 | + self::encode6Bits( ( $b1 << 2 ) & 63 ); |
|
75 | + if ( $pad ) { |
|
76 | 76 | $dest .= '='; |
77 | 77 | } |
78 | 78 | } else { |
79 | 79 | $dest .= |
80 | - self::encode6Bits( $b0 >> 2) . |
|
81 | - self::encode6Bits(($b0 << 4) & 63); |
|
82 | - if ($pad) { |
|
80 | + self::encode6Bits( $b0 >> 2 ) . |
|
81 | + self::encode6Bits( ( $b0 << 4 ) & 63 ); |
|
82 | + if ( $pad ) { |
|
83 | 83 | $dest .= '=='; |
84 | 84 | } |
85 | 85 | } |
@@ -99,86 +99,86 @@ discard block |
||
99 | 99 | * @throws TypeError |
100 | 100 | * @psalm-suppress RedundantCondition |
101 | 101 | */ |
102 | - public static function decode($src, $strictPadding = false) |
|
102 | + public static function decode( $src, $strictPadding = false ) |
|
103 | 103 | { |
104 | 104 | // Remove padding |
105 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
106 | - if ($srcLen === 0) { |
|
105 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
106 | + if ( $srcLen === 0 ) { |
|
107 | 107 | return ''; |
108 | 108 | } |
109 | 109 | |
110 | - if ($strictPadding) { |
|
111 | - if (($srcLen & 3) === 0) { |
|
112 | - if ($src[$srcLen - 1] === '=') { |
|
110 | + if ( $strictPadding ) { |
|
111 | + if ( ( $srcLen & 3 ) === 0 ) { |
|
112 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
113 | 113 | $srcLen--; |
114 | - if ($src[$srcLen - 1] === '=') { |
|
114 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
115 | 115 | $srcLen--; |
116 | 116 | } |
117 | 117 | } |
118 | 118 | } |
119 | - if (($srcLen & 3) === 1) { |
|
119 | + if ( ( $srcLen & 3 ) === 1 ) { |
|
120 | 120 | throw new RangeException( |
121 | 121 | 'Incorrect padding' |
122 | 122 | ); |
123 | 123 | } |
124 | - if ($src[$srcLen - 1] === '=') { |
|
124 | + if ( $src[ $srcLen - 1 ] === '=' ) { |
|
125 | 125 | throw new RangeException( |
126 | 126 | 'Incorrect padding' |
127 | 127 | ); |
128 | 128 | } |
129 | 129 | } else { |
130 | - $src = rtrim($src, '='); |
|
131 | - $srcLen = ParagonIE_Sodium_Core_Util::strlen($src); |
|
130 | + $src = rtrim( $src, '=' ); |
|
131 | + $srcLen = ParagonIE_Sodium_Core_Util::strlen( $src ); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | $err = 0; |
135 | 135 | $dest = ''; |
136 | 136 | // Main loop (no padding): |
137 | - for ($i = 0; $i + 4 <= $srcLen; $i += 4) { |
|
137 | + for ( $i = 0; $i + 4 <= $srcLen; $i += 4 ) { |
|
138 | 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]); |
|
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 | 144 | |
145 | 145 | $dest .= pack( |
146 | 146 | 'CCC', |
147 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
148 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff), |
|
149 | - ((($c2 << 6) | $c3) & 0xff) |
|
147 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ), |
|
148 | + ( ( ( $c1 << 4 ) | ( $c2 >> 2 ) ) & 0xff ), |
|
149 | + ( ( ( $c2 << 6 ) | $c3 ) & 0xff ) |
|
150 | 150 | ); |
151 | - $err |= ($c0 | $c1 | $c2 | $c3) >> 8; |
|
151 | + $err |= ( $c0 | $c1 | $c2 | $c3 ) >> 8; |
|
152 | 152 | } |
153 | 153 | // The last chunk, which may have padding: |
154 | - if ($i < $srcLen) { |
|
154 | + if ( $i < $srcLen ) { |
|
155 | 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]); |
|
156 | + $chunk = unpack( 'C*', ParagonIE_Sodium_Core_Util::substr( $src, $i, $srcLen - $i ) ); |
|
157 | + $c0 = self::decode6Bits( $chunk[ 1 ] ); |
|
158 | 158 | |
159 | - if ($i + 2 < $srcLen) { |
|
160 | - $c1 = self::decode6Bits($chunk[2]); |
|
161 | - $c2 = self::decode6Bits($chunk[3]); |
|
159 | + if ( $i + 2 < $srcLen ) { |
|
160 | + $c1 = self::decode6Bits( $chunk[ 2 ] ); |
|
161 | + $c2 = self::decode6Bits( $chunk[ 3 ] ); |
|
162 | 162 | $dest .= pack( |
163 | 163 | 'CC', |
164 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff), |
|
165 | - ((($c1 << 4) | ($c2 >> 2)) & 0xff) |
|
164 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ), |
|
165 | + ( ( ( $c1 << 4 ) | ( $c2 >> 2 ) ) & 0xff ) |
|
166 | 166 | ); |
167 | - $err |= ($c0 | $c1 | $c2) >> 8; |
|
168 | - } elseif ($i + 1 < $srcLen) { |
|
169 | - $c1 = self::decode6Bits($chunk[2]); |
|
167 | + $err |= ( $c0 | $c1 | $c2 ) >> 8; |
|
168 | + } elseif ( $i + 1 < $srcLen ) { |
|
169 | + $c1 = self::decode6Bits( $chunk[ 2 ] ); |
|
170 | 170 | $dest .= pack( |
171 | 171 | 'C', |
172 | - ((($c0 << 2) | ($c1 >> 4)) & 0xff) |
|
172 | + ( ( ( $c0 << 2 ) | ( $c1 >> 4 ) ) & 0xff ) |
|
173 | 173 | ); |
174 | - $err |= ($c0 | $c1) >> 8; |
|
175 | - } elseif ($i < $srcLen && $strictPadding) { |
|
174 | + $err |= ( $c0 | $c1 ) >> 8; |
|
175 | + } elseif ( $i < $srcLen && $strictPadding ) { |
|
176 | 176 | $err |= 1; |
177 | 177 | } |
178 | 178 | } |
179 | 179 | /** @var bool $check */ |
180 | - $check = ($err === 0); |
|
181 | - if (!$check) { |
|
180 | + $check = ( $err === 0 ); |
|
181 | + if ( ! $check ) { |
|
182 | 182 | throw new RangeException( |
183 | 183 | 'Base64::decode() only expects characters in the correct base64 alphabet' |
184 | 184 | ); |
@@ -197,24 +197,24 @@ discard block |
||
197 | 197 | * @param int $src |
198 | 198 | * @return int |
199 | 199 | */ |
200 | - protected static function decode6Bits($src) |
|
200 | + protected static function decode6Bits( $src ) |
|
201 | 201 | { |
202 | 202 | $ret = -1; |
203 | 203 | |
204 | 204 | // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64 |
205 | - $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64); |
|
205 | + $ret += ( ( ( 0x40 - $src ) & ( $src - 0x5b ) ) >> 8 ) & ( $src - 64 ); |
|
206 | 206 | |
207 | 207 | // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70 |
208 | - $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70); |
|
208 | + $ret += ( ( ( 0x60 - $src ) & ( $src - 0x7b ) ) >> 8 ) & ( $src - 70 ); |
|
209 | 209 | |
210 | 210 | // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5 |
211 | - $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5); |
|
211 | + $ret += ( ( ( 0x2f - $src ) & ( $src - 0x3a ) ) >> 8 ) & ( $src + 5 ); |
|
212 | 212 | |
213 | 213 | // if ($src == 0x2c) $ret += 62 + 1; |
214 | - $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63; |
|
214 | + $ret += ( ( ( 0x2c - $src ) & ( $src - 0x2e ) ) >> 8 ) & 63; |
|
215 | 215 | |
216 | 216 | // if ($src == 0x5f) ret += 63 + 1; |
217 | - $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64; |
|
217 | + $ret += ( ( ( 0x5e - $src ) & ( $src - 0x60 ) ) >> 8 ) & 64; |
|
218 | 218 | |
219 | 219 | return $ret; |
220 | 220 | } |
@@ -226,22 +226,22 @@ discard block |
||
226 | 226 | * @param int $src |
227 | 227 | * @return string |
228 | 228 | */ |
229 | - protected static function encode6Bits($src) |
|
229 | + protected static function encode6Bits( $src ) |
|
230 | 230 | { |
231 | 231 | $diff = 0x41; |
232 | 232 | |
233 | 233 | // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6 |
234 | - $diff += ((25 - $src) >> 8) & 6; |
|
234 | + $diff += ( ( 25 - $src ) >> 8 ) & 6; |
|
235 | 235 | |
236 | 236 | // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75 |
237 | - $diff -= ((51 - $src) >> 8) & 75; |
|
237 | + $diff -= ( ( 51 - $src ) >> 8 ) & 75; |
|
238 | 238 | |
239 | 239 | // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13 |
240 | - $diff -= ((61 - $src) >> 8) & 13; |
|
240 | + $diff -= ( ( 61 - $src ) >> 8 ) & 13; |
|
241 | 241 | |
242 | 242 | // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3 |
243 | - $diff += ((62 - $src) >> 8) & 49; |
|
243 | + $diff += ( ( 62 - $src ) >> 8 ) & 49; |
|
244 | 244 | |
245 | - return pack('C', $src + $diff); |
|
245 | + return pack( 'C', $src + $diff ); |
|
246 | 246 | } |
247 | 247 | } |