@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,780 +11,780 @@ discard block |
||
11 | 11 | */ |
12 | 12 | abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @var SplFixedArray |
|
16 | - */ |
|
17 | - protected static $iv; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var array<int, array<int, int>> |
|
21 | - */ |
|
22 | - protected static $sigma = array( |
|
23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | - ); |
|
36 | - |
|
37 | - const BLOCKBYTES = 128; |
|
38 | - const OUTBYTES = 64; |
|
39 | - const KEYBYTES = 64; |
|
40 | - |
|
41 | - /** |
|
42 | - * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | - * |
|
44 | - * @internal You should not use this directly from another application |
|
45 | - * |
|
46 | - * @param int $high |
|
47 | - * @param int $low |
|
48 | - * @return SplFixedArray |
|
49 | - * @psalm-suppress MixedAssignment |
|
50 | - */ |
|
51 | - public static function new64($high, $low) |
|
52 | - { |
|
53 | - $i64 = new SplFixedArray(2); |
|
54 | - $i64[0] = $high & 0xffffffff; |
|
55 | - $i64[1] = $low & 0xffffffff; |
|
56 | - return $i64; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
61 | - * that represents a 64-bit integer. |
|
62 | - * |
|
63 | - * @internal You should not use this directly from another application |
|
64 | - * |
|
65 | - * @param int $num |
|
66 | - * @return SplFixedArray |
|
67 | - */ |
|
68 | - protected static function to64($num) |
|
69 | - { |
|
70 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
71 | - return self::new64($hi, $lo); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
76 | - * containing two 32-bit integers (representing a 64-bit integer). |
|
77 | - * |
|
78 | - * @internal You should not use this directly from another application |
|
79 | - * |
|
80 | - * @param SplFixedArray $x |
|
81 | - * @param SplFixedArray $y |
|
82 | - * @return SplFixedArray |
|
83 | - * @psalm-suppress MixedArgument |
|
84 | - * @psalm-suppress MixedAssignment |
|
85 | - * @psalm-suppress MixedOperand |
|
86 | - */ |
|
87 | - protected static function add64($x, $y) |
|
88 | - { |
|
89 | - $l = ($x[1] + $y[1]) & 0xffffffff; |
|
90 | - return self::new64( |
|
91 | - (int) ($x[0] + $y[0] + ( |
|
92 | - ($l < $x[1]) ? 1 : 0 |
|
93 | - )), |
|
94 | - (int) $l |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @internal You should not use this directly from another application |
|
100 | - * |
|
101 | - * @param SplFixedArray $x |
|
102 | - * @param SplFixedArray $y |
|
103 | - * @param SplFixedArray $z |
|
104 | - * @return SplFixedArray |
|
105 | - */ |
|
106 | - protected static function add364($x, $y, $z) |
|
107 | - { |
|
108 | - return self::add64($x, self::add64($y, $z)); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @internal You should not use this directly from another application |
|
113 | - * |
|
114 | - * @param SplFixedArray $x |
|
115 | - * @param SplFixedArray $y |
|
116 | - * @return SplFixedArray |
|
117 | - * @throws SodiumException |
|
118 | - * @throws TypeError |
|
119 | - */ |
|
120 | - protected static function xor64(SplFixedArray $x, SplFixedArray $y) |
|
121 | - { |
|
122 | - if (!is_numeric($x[0])) { |
|
123 | - throw new SodiumException('x[0] is not an integer'); |
|
124 | - } |
|
125 | - if (!is_numeric($x[1])) { |
|
126 | - throw new SodiumException('x[1] is not an integer'); |
|
127 | - } |
|
128 | - if (!is_numeric($y[0])) { |
|
129 | - throw new SodiumException('y[0] is not an integer'); |
|
130 | - } |
|
131 | - if (!is_numeric($y[1])) { |
|
132 | - throw new SodiumException('y[1] is not an integer'); |
|
133 | - } |
|
134 | - return self::new64( |
|
135 | - (int) (($x[0] ^ $y[0]) & 0xffffffff), |
|
136 | - (int) (($x[1] ^ $y[1]) & 0xffffffff) |
|
137 | - ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @internal You should not use this directly from another application |
|
142 | - * |
|
143 | - * @param SplFixedArray $x |
|
144 | - * @param int $c |
|
145 | - * @return SplFixedArray |
|
146 | - * @psalm-suppress MixedAssignment |
|
147 | - */ |
|
148 | - public static function rotr64($x, $c) |
|
149 | - { |
|
150 | - if ($c >= 64) { |
|
151 | - $c %= 64; |
|
152 | - } |
|
153 | - if ($c >= 32) { |
|
154 | - /** @var int $tmp */ |
|
155 | - $tmp = $x[0]; |
|
156 | - $x[0] = $x[1]; |
|
157 | - $x[1] = $tmp; |
|
158 | - $c -= 32; |
|
159 | - } |
|
160 | - if ($c === 0) { |
|
161 | - return $x; |
|
162 | - } |
|
163 | - |
|
164 | - $l0 = 0; |
|
165 | - $c = 64 - $c; |
|
166 | - |
|
167 | - if ($c < 32) { |
|
168 | - /** @var int $h0 */ |
|
169 | - $h0 = ((int) ($x[0]) << $c) | ( |
|
170 | - ( |
|
171 | - (int) ($x[1]) & ((1 << $c) - 1) |
|
172 | - << |
|
173 | - (32 - $c) |
|
174 | - ) >> (32 - $c) |
|
175 | - ); |
|
176 | - /** @var int $l0 */ |
|
177 | - $l0 = (int) ($x[1]) << $c; |
|
178 | - } else { |
|
179 | - /** @var int $h0 */ |
|
180 | - $h0 = (int) ($x[1]) << ($c - 32); |
|
181 | - } |
|
182 | - |
|
183 | - $h1 = 0; |
|
184 | - $c1 = 64 - $c; |
|
185 | - |
|
186 | - if ($c1 < 32) { |
|
187 | - /** @var int $h1 */ |
|
188 | - $h1 = (int) ($x[0]) >> $c1; |
|
189 | - /** @var int $l1 */ |
|
190 | - $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1); |
|
191 | - } else { |
|
192 | - /** @var int $l1 */ |
|
193 | - $l1 = (int) ($x[0]) >> ($c1 - 32); |
|
194 | - } |
|
195 | - |
|
196 | - return self::new64($h0 | $h1, $l0 | $l1); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @internal You should not use this directly from another application |
|
201 | - * |
|
202 | - * @param SplFixedArray $x |
|
203 | - * @return int |
|
204 | - * @psalm-suppress MixedOperand |
|
205 | - */ |
|
206 | - protected static function flatten64($x) |
|
207 | - { |
|
208 | - return (int) ($x[0] * 4294967296 + $x[1]); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @internal You should not use this directly from another application |
|
213 | - * |
|
214 | - * @param SplFixedArray $x |
|
215 | - * @param int $i |
|
216 | - * @return SplFixedArray |
|
217 | - * @psalm-suppress MixedArgument |
|
218 | - * @psalm-suppress MixedArrayOffset |
|
219 | - */ |
|
220 | - protected static function load64(SplFixedArray $x, $i) |
|
221 | - { |
|
222 | - /** @var int $l */ |
|
223 | - $l = (int) ($x[$i]) |
|
224 | - | ((int) ($x[$i+1]) << 8) |
|
225 | - | ((int) ($x[$i+2]) << 16) |
|
226 | - | ((int) ($x[$i+3]) << 24); |
|
227 | - /** @var int $h */ |
|
228 | - $h = (int) ($x[$i+4]) |
|
229 | - | ((int) ($x[$i+5]) << 8) |
|
230 | - | ((int) ($x[$i+6]) << 16) |
|
231 | - | ((int) ($x[$i+7]) << 24); |
|
232 | - return self::new64($h, $l); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @internal You should not use this directly from another application |
|
237 | - * |
|
238 | - * @param SplFixedArray $x |
|
239 | - * @param int $i |
|
240 | - * @param SplFixedArray $u |
|
241 | - * @return void |
|
242 | - * @psalm-suppress MixedAssignment |
|
243 | - */ |
|
244 | - protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) |
|
245 | - { |
|
246 | - $maxLength = $x->getSize() - 1; |
|
247 | - for ($j = 0; $j < 8; ++$j) { |
|
248 | - /* |
|
14 | + /** |
|
15 | + * @var SplFixedArray |
|
16 | + */ |
|
17 | + protected static $iv; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var array<int, array<int, int>> |
|
21 | + */ |
|
22 | + protected static $sigma = array( |
|
23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | + ); |
|
36 | + |
|
37 | + const BLOCKBYTES = 128; |
|
38 | + const OUTBYTES = 64; |
|
39 | + const KEYBYTES = 64; |
|
40 | + |
|
41 | + /** |
|
42 | + * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | + * |
|
44 | + * @internal You should not use this directly from another application |
|
45 | + * |
|
46 | + * @param int $high |
|
47 | + * @param int $low |
|
48 | + * @return SplFixedArray |
|
49 | + * @psalm-suppress MixedAssignment |
|
50 | + */ |
|
51 | + public static function new64($high, $low) |
|
52 | + { |
|
53 | + $i64 = new SplFixedArray(2); |
|
54 | + $i64[0] = $high & 0xffffffff; |
|
55 | + $i64[1] = $low & 0xffffffff; |
|
56 | + return $i64; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
61 | + * that represents a 64-bit integer. |
|
62 | + * |
|
63 | + * @internal You should not use this directly from another application |
|
64 | + * |
|
65 | + * @param int $num |
|
66 | + * @return SplFixedArray |
|
67 | + */ |
|
68 | + protected static function to64($num) |
|
69 | + { |
|
70 | + list($hi, $lo) = self::numericTo64BitInteger($num); |
|
71 | + return self::new64($hi, $lo); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
76 | + * containing two 32-bit integers (representing a 64-bit integer). |
|
77 | + * |
|
78 | + * @internal You should not use this directly from another application |
|
79 | + * |
|
80 | + * @param SplFixedArray $x |
|
81 | + * @param SplFixedArray $y |
|
82 | + * @return SplFixedArray |
|
83 | + * @psalm-suppress MixedArgument |
|
84 | + * @psalm-suppress MixedAssignment |
|
85 | + * @psalm-suppress MixedOperand |
|
86 | + */ |
|
87 | + protected static function add64($x, $y) |
|
88 | + { |
|
89 | + $l = ($x[1] + $y[1]) & 0xffffffff; |
|
90 | + return self::new64( |
|
91 | + (int) ($x[0] + $y[0] + ( |
|
92 | + ($l < $x[1]) ? 1 : 0 |
|
93 | + )), |
|
94 | + (int) $l |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @internal You should not use this directly from another application |
|
100 | + * |
|
101 | + * @param SplFixedArray $x |
|
102 | + * @param SplFixedArray $y |
|
103 | + * @param SplFixedArray $z |
|
104 | + * @return SplFixedArray |
|
105 | + */ |
|
106 | + protected static function add364($x, $y, $z) |
|
107 | + { |
|
108 | + return self::add64($x, self::add64($y, $z)); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @internal You should not use this directly from another application |
|
113 | + * |
|
114 | + * @param SplFixedArray $x |
|
115 | + * @param SplFixedArray $y |
|
116 | + * @return SplFixedArray |
|
117 | + * @throws SodiumException |
|
118 | + * @throws TypeError |
|
119 | + */ |
|
120 | + protected static function xor64(SplFixedArray $x, SplFixedArray $y) |
|
121 | + { |
|
122 | + if (!is_numeric($x[0])) { |
|
123 | + throw new SodiumException('x[0] is not an integer'); |
|
124 | + } |
|
125 | + if (!is_numeric($x[1])) { |
|
126 | + throw new SodiumException('x[1] is not an integer'); |
|
127 | + } |
|
128 | + if (!is_numeric($y[0])) { |
|
129 | + throw new SodiumException('y[0] is not an integer'); |
|
130 | + } |
|
131 | + if (!is_numeric($y[1])) { |
|
132 | + throw new SodiumException('y[1] is not an integer'); |
|
133 | + } |
|
134 | + return self::new64( |
|
135 | + (int) (($x[0] ^ $y[0]) & 0xffffffff), |
|
136 | + (int) (($x[1] ^ $y[1]) & 0xffffffff) |
|
137 | + ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @internal You should not use this directly from another application |
|
142 | + * |
|
143 | + * @param SplFixedArray $x |
|
144 | + * @param int $c |
|
145 | + * @return SplFixedArray |
|
146 | + * @psalm-suppress MixedAssignment |
|
147 | + */ |
|
148 | + public static function rotr64($x, $c) |
|
149 | + { |
|
150 | + if ($c >= 64) { |
|
151 | + $c %= 64; |
|
152 | + } |
|
153 | + if ($c >= 32) { |
|
154 | + /** @var int $tmp */ |
|
155 | + $tmp = $x[0]; |
|
156 | + $x[0] = $x[1]; |
|
157 | + $x[1] = $tmp; |
|
158 | + $c -= 32; |
|
159 | + } |
|
160 | + if ($c === 0) { |
|
161 | + return $x; |
|
162 | + } |
|
163 | + |
|
164 | + $l0 = 0; |
|
165 | + $c = 64 - $c; |
|
166 | + |
|
167 | + if ($c < 32) { |
|
168 | + /** @var int $h0 */ |
|
169 | + $h0 = ((int) ($x[0]) << $c) | ( |
|
170 | + ( |
|
171 | + (int) ($x[1]) & ((1 << $c) - 1) |
|
172 | + << |
|
173 | + (32 - $c) |
|
174 | + ) >> (32 - $c) |
|
175 | + ); |
|
176 | + /** @var int $l0 */ |
|
177 | + $l0 = (int) ($x[1]) << $c; |
|
178 | + } else { |
|
179 | + /** @var int $h0 */ |
|
180 | + $h0 = (int) ($x[1]) << ($c - 32); |
|
181 | + } |
|
182 | + |
|
183 | + $h1 = 0; |
|
184 | + $c1 = 64 - $c; |
|
185 | + |
|
186 | + if ($c1 < 32) { |
|
187 | + /** @var int $h1 */ |
|
188 | + $h1 = (int) ($x[0]) >> $c1; |
|
189 | + /** @var int $l1 */ |
|
190 | + $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1); |
|
191 | + } else { |
|
192 | + /** @var int $l1 */ |
|
193 | + $l1 = (int) ($x[0]) >> ($c1 - 32); |
|
194 | + } |
|
195 | + |
|
196 | + return self::new64($h0 | $h1, $l0 | $l1); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @internal You should not use this directly from another application |
|
201 | + * |
|
202 | + * @param SplFixedArray $x |
|
203 | + * @return int |
|
204 | + * @psalm-suppress MixedOperand |
|
205 | + */ |
|
206 | + protected static function flatten64($x) |
|
207 | + { |
|
208 | + return (int) ($x[0] * 4294967296 + $x[1]); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @internal You should not use this directly from another application |
|
213 | + * |
|
214 | + * @param SplFixedArray $x |
|
215 | + * @param int $i |
|
216 | + * @return SplFixedArray |
|
217 | + * @psalm-suppress MixedArgument |
|
218 | + * @psalm-suppress MixedArrayOffset |
|
219 | + */ |
|
220 | + protected static function load64(SplFixedArray $x, $i) |
|
221 | + { |
|
222 | + /** @var int $l */ |
|
223 | + $l = (int) ($x[$i]) |
|
224 | + | ((int) ($x[$i+1]) << 8) |
|
225 | + | ((int) ($x[$i+2]) << 16) |
|
226 | + | ((int) ($x[$i+3]) << 24); |
|
227 | + /** @var int $h */ |
|
228 | + $h = (int) ($x[$i+4]) |
|
229 | + | ((int) ($x[$i+5]) << 8) |
|
230 | + | ((int) ($x[$i+6]) << 16) |
|
231 | + | ((int) ($x[$i+7]) << 24); |
|
232 | + return self::new64($h, $l); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @internal You should not use this directly from another application |
|
237 | + * |
|
238 | + * @param SplFixedArray $x |
|
239 | + * @param int $i |
|
240 | + * @param SplFixedArray $u |
|
241 | + * @return void |
|
242 | + * @psalm-suppress MixedAssignment |
|
243 | + */ |
|
244 | + protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) |
|
245 | + { |
|
246 | + $maxLength = $x->getSize() - 1; |
|
247 | + for ($j = 0; $j < 8; ++$j) { |
|
248 | + /* |
|
249 | 249 | [0, 1, 2, 3, 4, 5, 6, 7] |
250 | 250 | ... becomes ... |
251 | 251 | [0, 0, 0, 0, 1, 1, 1, 1] |
252 | 252 | */ |
253 | - /** @var int $uIdx */ |
|
254 | - $uIdx = ((7 - $j) & 4) >> 2; |
|
255 | - $x[$i] = ((int) ($u[$uIdx]) & 0xff); |
|
256 | - if (++$i > $maxLength) { |
|
257 | - return; |
|
258 | - } |
|
259 | - /** @psalm-suppress MixedOperand */ |
|
260 | - $u[$uIdx] >>= 8; |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * This just sets the $iv static variable. |
|
266 | - * |
|
267 | - * @internal You should not use this directly from another application |
|
268 | - * |
|
269 | - * @return void |
|
270 | - */ |
|
271 | - public static function pseudoConstructor() |
|
272 | - { |
|
273 | - static $called = false; |
|
274 | - if ($called) { |
|
275 | - return; |
|
276 | - } |
|
277 | - self::$iv = new SplFixedArray(8); |
|
278 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
279 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
280 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
281 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
282 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
283 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
284 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
285 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
286 | - |
|
287 | - $called = true; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Returns a fresh BLAKE2 context. |
|
292 | - * |
|
293 | - * @internal You should not use this directly from another application |
|
294 | - * |
|
295 | - * @return SplFixedArray |
|
296 | - * @psalm-suppress MixedAssignment |
|
297 | - * @psalm-suppress MixedArrayAccess |
|
298 | - * @psalm-suppress MixedArrayAssignment |
|
299 | - */ |
|
300 | - protected static function context() |
|
301 | - { |
|
302 | - $ctx = new SplFixedArray(6); |
|
303 | - $ctx[0] = new SplFixedArray(8); // h |
|
304 | - $ctx[1] = new SplFixedArray(2); // t |
|
305 | - $ctx[2] = new SplFixedArray(2); // f |
|
306 | - $ctx[3] = new SplFixedArray(256); // buf |
|
307 | - $ctx[4] = 0; // buflen |
|
308 | - $ctx[5] = 0; // last_node (uint8_t) |
|
309 | - |
|
310 | - for ($i = 8; $i--;) { |
|
311 | - $ctx[0][$i] = self::$iv[$i]; |
|
312 | - } |
|
313 | - for ($i = 256; $i--;) { |
|
314 | - $ctx[3][$i] = 0; |
|
315 | - } |
|
316 | - |
|
317 | - $zero = self::new64(0, 0); |
|
318 | - $ctx[1][0] = $zero; |
|
319 | - $ctx[1][1] = $zero; |
|
320 | - $ctx[2][0] = $zero; |
|
321 | - $ctx[2][1] = $zero; |
|
322 | - |
|
323 | - return $ctx; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * @internal You should not use this directly from another application |
|
328 | - * |
|
329 | - * @param SplFixedArray $ctx |
|
330 | - * @param SplFixedArray $buf |
|
331 | - * @return void |
|
332 | - * @throws SodiumException |
|
333 | - * @throws TypeError |
|
334 | - * @psalm-suppress MixedArgument |
|
335 | - * @psalm-suppress MixedAssignment |
|
336 | - * @psalm-suppress MixedArrayAccess |
|
337 | - * @psalm-suppress MixedArrayAssignment |
|
338 | - * @psalm-suppress MixedArrayOffset |
|
339 | - */ |
|
340 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
341 | - { |
|
342 | - $m = new SplFixedArray(16); |
|
343 | - $v = new SplFixedArray(16); |
|
344 | - |
|
345 | - for ($i = 16; $i--;) { |
|
346 | - $m[$i] = self::load64($buf, $i << 3); |
|
347 | - } |
|
348 | - |
|
349 | - for ($i = 8; $i--;) { |
|
350 | - $v[$i] = $ctx[0][$i]; |
|
351 | - } |
|
352 | - |
|
353 | - $v[ 8] = self::$iv[0]; |
|
354 | - $v[ 9] = self::$iv[1]; |
|
355 | - $v[10] = self::$iv[2]; |
|
356 | - $v[11] = self::$iv[3]; |
|
357 | - |
|
358 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
359 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
360 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
361 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
362 | - |
|
363 | - for ($r = 0; $r < 12; ++$r) { |
|
364 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
365 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
366 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
367 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
368 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
369 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
370 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
371 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
372 | - } |
|
373 | - |
|
374 | - for ($i = 8; $i--;) { |
|
375 | - $ctx[0][$i] = self::xor64( |
|
376 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
377 | - ); |
|
378 | - } |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * @internal You should not use this directly from another application |
|
383 | - * |
|
384 | - * @param int $r |
|
385 | - * @param int $i |
|
386 | - * @param int $a |
|
387 | - * @param int $b |
|
388 | - * @param int $c |
|
389 | - * @param int $d |
|
390 | - * @param SplFixedArray $v |
|
391 | - * @param SplFixedArray $m |
|
392 | - * @return SplFixedArray |
|
393 | - * @throws SodiumException |
|
394 | - * @throws TypeError |
|
395 | - * @psalm-suppress MixedArgument |
|
396 | - * @psalm-suppress MixedArrayOffset |
|
397 | - */ |
|
398 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
399 | - { |
|
400 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
401 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
402 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
403 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
404 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
405 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
406 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
407 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
408 | - return $v; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @internal You should not use this directly from another application |
|
413 | - * |
|
414 | - * @param SplFixedArray $ctx |
|
415 | - * @param int $inc |
|
416 | - * @return void |
|
417 | - * @throws SodiumException |
|
418 | - * @psalm-suppress MixedArgument |
|
419 | - * @psalm-suppress MixedArrayAccess |
|
420 | - * @psalm-suppress MixedArrayAssignment |
|
421 | - */ |
|
422 | - public static function increment_counter($ctx, $inc) |
|
423 | - { |
|
424 | - if ($inc < 0) { |
|
425 | - throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
426 | - } |
|
427 | - $t = self::to64($inc); |
|
428 | - # S->t is $ctx[1] in our implementation |
|
429 | - |
|
430 | - # S->t[0] = ( uint64_t )( t >> 0 ); |
|
431 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
432 | - |
|
433 | - # S->t[1] += ( S->t[0] < inc ); |
|
434 | - if (self::flatten64($ctx[1][0]) < $inc) { |
|
435 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
436 | - } |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @internal You should not use this directly from another application |
|
441 | - * |
|
442 | - * @param SplFixedArray $ctx |
|
443 | - * @param SplFixedArray $p |
|
444 | - * @param int $plen |
|
445 | - * @return void |
|
446 | - * @throws SodiumException |
|
447 | - * @throws TypeError |
|
448 | - * @psalm-suppress MixedArgument |
|
449 | - * @psalm-suppress MixedAssignment |
|
450 | - * @psalm-suppress MixedArrayAccess |
|
451 | - * @psalm-suppress MixedArrayAssignment |
|
452 | - * @psalm-suppress MixedArrayOffset |
|
453 | - * @psalm-suppress MixedOperand |
|
454 | - */ |
|
455 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
456 | - { |
|
457 | - self::pseudoConstructor(); |
|
458 | - |
|
459 | - $offset = 0; |
|
460 | - while ($plen > 0) { |
|
461 | - $left = $ctx[4]; |
|
462 | - $fill = 256 - $left; |
|
463 | - |
|
464 | - if ($plen > $fill) { |
|
465 | - # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
466 | - for ($i = $fill; $i--;) { |
|
467 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
468 | - } |
|
469 | - |
|
470 | - # S->buflen += fill; |
|
471 | - $ctx[4] += $fill; |
|
472 | - |
|
473 | - # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
474 | - self::increment_counter($ctx, 128); |
|
475 | - |
|
476 | - # blake2b_compress( S, S->buf ); /* Compress */ |
|
477 | - self::compress($ctx, $ctx[3]); |
|
478 | - |
|
479 | - # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
480 | - for ($i = 128; $i--;) { |
|
481 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
482 | - } |
|
483 | - |
|
484 | - # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
485 | - $ctx[4] -= 128; |
|
486 | - |
|
487 | - # in += fill; |
|
488 | - $offset += $fill; |
|
489 | - |
|
490 | - # inlen -= fill; |
|
491 | - $plen -= $fill; |
|
492 | - } else { |
|
493 | - for ($i = $plen; $i--;) { |
|
494 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
495 | - } |
|
496 | - $ctx[4] += $plen; |
|
497 | - $offset += $plen; |
|
498 | - $plen -= $plen; |
|
499 | - } |
|
500 | - } |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * @internal You should not use this directly from another application |
|
505 | - * |
|
506 | - * @param SplFixedArray $ctx |
|
507 | - * @param SplFixedArray $out |
|
508 | - * @return SplFixedArray |
|
509 | - * @throws SodiumException |
|
510 | - * @throws TypeError |
|
511 | - * @psalm-suppress MixedArgument |
|
512 | - * @psalm-suppress MixedAssignment |
|
513 | - * @psalm-suppress MixedArrayAccess |
|
514 | - * @psalm-suppress MixedArrayAssignment |
|
515 | - * @psalm-suppress MixedArrayOffset |
|
516 | - * @psalm-suppress MixedOperand |
|
517 | - */ |
|
518 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
519 | - { |
|
520 | - self::pseudoConstructor(); |
|
521 | - if ($ctx[4] > 128) { |
|
522 | - self::increment_counter($ctx, 128); |
|
523 | - self::compress($ctx, $ctx[3]); |
|
524 | - $ctx[4] -= 128; |
|
525 | - if ($ctx[4] > 128) { |
|
526 | - throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
527 | - } |
|
528 | - for ($i = $ctx[4]; $i--;) { |
|
529 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
530 | - } |
|
531 | - } |
|
532 | - |
|
533 | - self::increment_counter($ctx, $ctx[4]); |
|
534 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
535 | - |
|
536 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
537 | - $ctx[3][$i+$ctx[4]] = 0; |
|
538 | - } |
|
539 | - |
|
540 | - self::compress($ctx, $ctx[3]); |
|
541 | - |
|
542 | - $i = (int) (($out->getSize() - 1) / 8); |
|
543 | - for (; $i >= 0; --$i) { |
|
544 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
545 | - } |
|
546 | - return $out; |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * @internal You should not use this directly from another application |
|
551 | - * |
|
552 | - * @param SplFixedArray|null $key |
|
553 | - * @param int $outlen |
|
554 | - * @param SplFixedArray|null $salt |
|
555 | - * @param SplFixedArray|null $personal |
|
556 | - * @return SplFixedArray |
|
557 | - * @throws SodiumException |
|
558 | - * @throws TypeError |
|
559 | - * @psalm-suppress MixedArgument |
|
560 | - * @psalm-suppress MixedAssignment |
|
561 | - * @psalm-suppress MixedArrayAccess |
|
562 | - * @psalm-suppress MixedArrayAssignment |
|
563 | - * @psalm-suppress MixedArrayOffset |
|
564 | - */ |
|
565 | - public static function init( |
|
566 | - $key = null, |
|
567 | - $outlen = 64, |
|
568 | - $salt = null, |
|
569 | - $personal = null |
|
570 | - ) { |
|
571 | - self::pseudoConstructor(); |
|
572 | - $klen = 0; |
|
573 | - |
|
574 | - if ($key !== null) { |
|
575 | - if (count($key) > 64) { |
|
576 | - throw new SodiumException('Invalid key size'); |
|
577 | - } |
|
578 | - $klen = count($key); |
|
579 | - } |
|
580 | - |
|
581 | - if ($outlen > 64) { |
|
582 | - throw new SodiumException('Invalid output size'); |
|
583 | - } |
|
584 | - |
|
585 | - $ctx = self::context(); |
|
586 | - |
|
587 | - $p = new SplFixedArray(64); |
|
588 | - // Zero our param buffer... |
|
589 | - for ($i = 64; --$i;) { |
|
590 | - $p[$i] = 0; |
|
591 | - } |
|
592 | - |
|
593 | - $p[0] = $outlen; // digest_length |
|
594 | - $p[1] = $klen; // key_length |
|
595 | - $p[2] = 1; // fanout |
|
596 | - $p[3] = 1; // depth |
|
597 | - |
|
598 | - if ($salt instanceof SplFixedArray) { |
|
599 | - // salt: [32] through [47] |
|
600 | - for ($i = 0; $i < 16; ++$i) { |
|
601 | - $p[32 + $i] = (int) $salt[$i]; |
|
602 | - } |
|
603 | - } |
|
604 | - if ($personal instanceof SplFixedArray) { |
|
605 | - // personal: [48] through [63] |
|
606 | - for ($i = 0; $i < 16; ++$i) { |
|
607 | - $p[48 + $i] = (int) $personal[$i]; |
|
608 | - } |
|
609 | - } |
|
610 | - |
|
611 | - $ctx[0][0] = self::xor64( |
|
612 | - $ctx[0][0], |
|
613 | - self::load64($p, 0) |
|
614 | - ); |
|
615 | - if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
616 | - // We need to do what blake2b_init_param() does: |
|
617 | - for ($i = 1; $i < 8; ++$i) { |
|
618 | - $ctx[0][$i] = self::xor64( |
|
619 | - $ctx[0][$i], |
|
620 | - self::load64($p, $i << 3) |
|
621 | - ); |
|
622 | - } |
|
623 | - } |
|
624 | - |
|
625 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
626 | - $block = new SplFixedArray(128); |
|
627 | - for ($i = 128; $i--;) { |
|
628 | - $block[$i] = 0; |
|
629 | - } |
|
630 | - for ($i = $klen; $i--;) { |
|
631 | - $block[$i] = $key[$i]; |
|
632 | - } |
|
633 | - self::update($ctx, $block, 128); |
|
634 | - $ctx[4] = 128; |
|
635 | - } |
|
636 | - |
|
637 | - return $ctx; |
|
638 | - } |
|
639 | - |
|
640 | - /** |
|
641 | - * Convert a string into an SplFixedArray of integers |
|
642 | - * |
|
643 | - * @internal You should not use this directly from another application |
|
644 | - * |
|
645 | - * @param string $str |
|
646 | - * @return SplFixedArray |
|
647 | - * @psalm-suppress MixedArgumentTypeCoercion |
|
648 | - */ |
|
649 | - public static function stringToSplFixedArray($str = '') |
|
650 | - { |
|
651 | - $values = unpack('C*', $str); |
|
652 | - return SplFixedArray::fromArray(array_values($values)); |
|
653 | - } |
|
654 | - |
|
655 | - /** |
|
656 | - * Convert an SplFixedArray of integers into a string |
|
657 | - * |
|
658 | - * @internal You should not use this directly from another application |
|
659 | - * |
|
660 | - * @param SplFixedArray $a |
|
661 | - * @return string |
|
662 | - * @throws TypeError |
|
663 | - */ |
|
664 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
665 | - { |
|
666 | - /** |
|
667 | - * @var array<int, int|string> $arr |
|
668 | - */ |
|
669 | - $arr = $a->toArray(); |
|
670 | - $c = $a->count(); |
|
671 | - array_unshift($arr, str_repeat('C', $c)); |
|
672 | - return (string) (call_user_func_array('pack', $arr)); |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * @internal You should not use this directly from another application |
|
677 | - * |
|
678 | - * @param SplFixedArray $ctx |
|
679 | - * @return string |
|
680 | - * @throws TypeError |
|
681 | - * @psalm-suppress MixedArgument |
|
682 | - * @psalm-suppress MixedAssignment |
|
683 | - * @psalm-suppress MixedArrayAccess |
|
684 | - * @psalm-suppress MixedArrayAssignment |
|
685 | - * @psalm-suppress MixedArrayOffset |
|
686 | - * @psalm-suppress MixedMethodCall |
|
687 | - */ |
|
688 | - public static function contextToString(SplFixedArray $ctx) |
|
689 | - { |
|
690 | - $str = ''; |
|
691 | - /** @var array<int, array<int, int>> $ctxA */ |
|
692 | - $ctxA = $ctx[0]->toArray(); |
|
693 | - |
|
694 | - # uint64_t h[8]; |
|
695 | - for ($i = 0; $i < 8; ++$i) { |
|
696 | - $str .= self::store32_le($ctxA[$i][1]); |
|
697 | - $str .= self::store32_le($ctxA[$i][0]); |
|
698 | - } |
|
699 | - |
|
700 | - # uint64_t t[2]; |
|
701 | - # uint64_t f[2]; |
|
702 | - for ($i = 1; $i < 3; ++$i) { |
|
703 | - $ctxA = $ctx[$i]->toArray(); |
|
704 | - $str .= self::store32_le($ctxA[0][1]); |
|
705 | - $str .= self::store32_le($ctxA[0][0]); |
|
706 | - $str .= self::store32_le($ctxA[1][1]); |
|
707 | - $str .= self::store32_le($ctxA[1][0]); |
|
708 | - } |
|
709 | - |
|
710 | - # uint8_t buf[2 * 128]; |
|
711 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
712 | - |
|
713 | - /** @var int $ctx4 */ |
|
714 | - $ctx4 = (int) $ctx[4]; |
|
715 | - |
|
716 | - # size_t buflen; |
|
717 | - $str .= implode('', array( |
|
718 | - self::intToChr($ctx4 & 0xff), |
|
719 | - self::intToChr(($ctx4 >> 8) & 0xff), |
|
720 | - self::intToChr(($ctx4 >> 16) & 0xff), |
|
721 | - self::intToChr(($ctx4 >> 24) & 0xff), |
|
722 | - self::intToChr(($ctx4 >> 32) & 0xff), |
|
723 | - self::intToChr(($ctx4 >> 40) & 0xff), |
|
724 | - self::intToChr(($ctx4 >> 48) & 0xff), |
|
725 | - self::intToChr(($ctx4 >> 56) & 0xff) |
|
726 | - )); |
|
727 | - # uint8_t last_node; |
|
728 | - return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
729 | - } |
|
730 | - |
|
731 | - /** |
|
732 | - * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
733 | - * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
734 | - * |
|
735 | - * @internal You should not use this directly from another application |
|
736 | - * |
|
737 | - * @param string $string |
|
738 | - * @return SplFixedArray |
|
739 | - * @throws SodiumException |
|
740 | - * @throws TypeError |
|
741 | - * @psalm-suppress MixedArrayAssignment |
|
742 | - */ |
|
743 | - public static function stringToContext($string) |
|
744 | - { |
|
745 | - $ctx = self::context(); |
|
746 | - |
|
747 | - # uint64_t h[8]; |
|
748 | - for ($i = 0; $i < 8; ++$i) { |
|
749 | - $ctx[0][$i] = SplFixedArray::fromArray( |
|
750 | - array( |
|
751 | - self::load_4( |
|
752 | - self::substr($string, (($i << 3) + 4), 4) |
|
753 | - ), |
|
754 | - self::load_4( |
|
755 | - self::substr($string, (($i << 3) + 0), 4) |
|
756 | - ) |
|
757 | - ) |
|
758 | - ); |
|
759 | - } |
|
760 | - |
|
761 | - # uint64_t t[2]; |
|
762 | - # uint64_t f[2]; |
|
763 | - for ($i = 1; $i < 3; ++$i) { |
|
764 | - $ctx[$i][1] = SplFixedArray::fromArray( |
|
765 | - array( |
|
766 | - self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)), |
|
767 | - self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4)) |
|
768 | - ) |
|
769 | - ); |
|
770 | - $ctx[$i][0] = SplFixedArray::fromArray( |
|
771 | - array( |
|
772 | - self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)), |
|
773 | - self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4)) |
|
774 | - ) |
|
775 | - ); |
|
776 | - } |
|
777 | - |
|
778 | - # uint8_t buf[2 * 128]; |
|
779 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
780 | - |
|
781 | - # uint8_t buf[2 * 128]; |
|
782 | - $int = 0; |
|
783 | - for ($i = 0; $i < 8; ++$i) { |
|
784 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
785 | - } |
|
786 | - $ctx[4] = $int; |
|
787 | - |
|
788 | - return $ctx; |
|
789 | - } |
|
253 | + /** @var int $uIdx */ |
|
254 | + $uIdx = ((7 - $j) & 4) >> 2; |
|
255 | + $x[$i] = ((int) ($u[$uIdx]) & 0xff); |
|
256 | + if (++$i > $maxLength) { |
|
257 | + return; |
|
258 | + } |
|
259 | + /** @psalm-suppress MixedOperand */ |
|
260 | + $u[$uIdx] >>= 8; |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * This just sets the $iv static variable. |
|
266 | + * |
|
267 | + * @internal You should not use this directly from another application |
|
268 | + * |
|
269 | + * @return void |
|
270 | + */ |
|
271 | + public static function pseudoConstructor() |
|
272 | + { |
|
273 | + static $called = false; |
|
274 | + if ($called) { |
|
275 | + return; |
|
276 | + } |
|
277 | + self::$iv = new SplFixedArray(8); |
|
278 | + self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
279 | + self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
280 | + self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
281 | + self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
282 | + self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
283 | + self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
284 | + self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
285 | + self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
286 | + |
|
287 | + $called = true; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Returns a fresh BLAKE2 context. |
|
292 | + * |
|
293 | + * @internal You should not use this directly from another application |
|
294 | + * |
|
295 | + * @return SplFixedArray |
|
296 | + * @psalm-suppress MixedAssignment |
|
297 | + * @psalm-suppress MixedArrayAccess |
|
298 | + * @psalm-suppress MixedArrayAssignment |
|
299 | + */ |
|
300 | + protected static function context() |
|
301 | + { |
|
302 | + $ctx = new SplFixedArray(6); |
|
303 | + $ctx[0] = new SplFixedArray(8); // h |
|
304 | + $ctx[1] = new SplFixedArray(2); // t |
|
305 | + $ctx[2] = new SplFixedArray(2); // f |
|
306 | + $ctx[3] = new SplFixedArray(256); // buf |
|
307 | + $ctx[4] = 0; // buflen |
|
308 | + $ctx[5] = 0; // last_node (uint8_t) |
|
309 | + |
|
310 | + for ($i = 8; $i--;) { |
|
311 | + $ctx[0][$i] = self::$iv[$i]; |
|
312 | + } |
|
313 | + for ($i = 256; $i--;) { |
|
314 | + $ctx[3][$i] = 0; |
|
315 | + } |
|
316 | + |
|
317 | + $zero = self::new64(0, 0); |
|
318 | + $ctx[1][0] = $zero; |
|
319 | + $ctx[1][1] = $zero; |
|
320 | + $ctx[2][0] = $zero; |
|
321 | + $ctx[2][1] = $zero; |
|
322 | + |
|
323 | + return $ctx; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * @internal You should not use this directly from another application |
|
328 | + * |
|
329 | + * @param SplFixedArray $ctx |
|
330 | + * @param SplFixedArray $buf |
|
331 | + * @return void |
|
332 | + * @throws SodiumException |
|
333 | + * @throws TypeError |
|
334 | + * @psalm-suppress MixedArgument |
|
335 | + * @psalm-suppress MixedAssignment |
|
336 | + * @psalm-suppress MixedArrayAccess |
|
337 | + * @psalm-suppress MixedArrayAssignment |
|
338 | + * @psalm-suppress MixedArrayOffset |
|
339 | + */ |
|
340 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
341 | + { |
|
342 | + $m = new SplFixedArray(16); |
|
343 | + $v = new SplFixedArray(16); |
|
344 | + |
|
345 | + for ($i = 16; $i--;) { |
|
346 | + $m[$i] = self::load64($buf, $i << 3); |
|
347 | + } |
|
348 | + |
|
349 | + for ($i = 8; $i--;) { |
|
350 | + $v[$i] = $ctx[0][$i]; |
|
351 | + } |
|
352 | + |
|
353 | + $v[ 8] = self::$iv[0]; |
|
354 | + $v[ 9] = self::$iv[1]; |
|
355 | + $v[10] = self::$iv[2]; |
|
356 | + $v[11] = self::$iv[3]; |
|
357 | + |
|
358 | + $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
359 | + $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
360 | + $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
361 | + $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
362 | + |
|
363 | + for ($r = 0; $r < 12; ++$r) { |
|
364 | + $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
365 | + $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
366 | + $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
367 | + $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
368 | + $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
369 | + $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
370 | + $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
371 | + $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
372 | + } |
|
373 | + |
|
374 | + for ($i = 8; $i--;) { |
|
375 | + $ctx[0][$i] = self::xor64( |
|
376 | + $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
377 | + ); |
|
378 | + } |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * @internal You should not use this directly from another application |
|
383 | + * |
|
384 | + * @param int $r |
|
385 | + * @param int $i |
|
386 | + * @param int $a |
|
387 | + * @param int $b |
|
388 | + * @param int $c |
|
389 | + * @param int $d |
|
390 | + * @param SplFixedArray $v |
|
391 | + * @param SplFixedArray $m |
|
392 | + * @return SplFixedArray |
|
393 | + * @throws SodiumException |
|
394 | + * @throws TypeError |
|
395 | + * @psalm-suppress MixedArgument |
|
396 | + * @psalm-suppress MixedArrayOffset |
|
397 | + */ |
|
398 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
399 | + { |
|
400 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
401 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
402 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
403 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
404 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
405 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
406 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
407 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
408 | + return $v; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @internal You should not use this directly from another application |
|
413 | + * |
|
414 | + * @param SplFixedArray $ctx |
|
415 | + * @param int $inc |
|
416 | + * @return void |
|
417 | + * @throws SodiumException |
|
418 | + * @psalm-suppress MixedArgument |
|
419 | + * @psalm-suppress MixedArrayAccess |
|
420 | + * @psalm-suppress MixedArrayAssignment |
|
421 | + */ |
|
422 | + public static function increment_counter($ctx, $inc) |
|
423 | + { |
|
424 | + if ($inc < 0) { |
|
425 | + throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
426 | + } |
|
427 | + $t = self::to64($inc); |
|
428 | + # S->t is $ctx[1] in our implementation |
|
429 | + |
|
430 | + # S->t[0] = ( uint64_t )( t >> 0 ); |
|
431 | + $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
432 | + |
|
433 | + # S->t[1] += ( S->t[0] < inc ); |
|
434 | + if (self::flatten64($ctx[1][0]) < $inc) { |
|
435 | + $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
436 | + } |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @internal You should not use this directly from another application |
|
441 | + * |
|
442 | + * @param SplFixedArray $ctx |
|
443 | + * @param SplFixedArray $p |
|
444 | + * @param int $plen |
|
445 | + * @return void |
|
446 | + * @throws SodiumException |
|
447 | + * @throws TypeError |
|
448 | + * @psalm-suppress MixedArgument |
|
449 | + * @psalm-suppress MixedAssignment |
|
450 | + * @psalm-suppress MixedArrayAccess |
|
451 | + * @psalm-suppress MixedArrayAssignment |
|
452 | + * @psalm-suppress MixedArrayOffset |
|
453 | + * @psalm-suppress MixedOperand |
|
454 | + */ |
|
455 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
456 | + { |
|
457 | + self::pseudoConstructor(); |
|
458 | + |
|
459 | + $offset = 0; |
|
460 | + while ($plen > 0) { |
|
461 | + $left = $ctx[4]; |
|
462 | + $fill = 256 - $left; |
|
463 | + |
|
464 | + if ($plen > $fill) { |
|
465 | + # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
466 | + for ($i = $fill; $i--;) { |
|
467 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
468 | + } |
|
469 | + |
|
470 | + # S->buflen += fill; |
|
471 | + $ctx[4] += $fill; |
|
472 | + |
|
473 | + # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
474 | + self::increment_counter($ctx, 128); |
|
475 | + |
|
476 | + # blake2b_compress( S, S->buf ); /* Compress */ |
|
477 | + self::compress($ctx, $ctx[3]); |
|
478 | + |
|
479 | + # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
480 | + for ($i = 128; $i--;) { |
|
481 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
482 | + } |
|
483 | + |
|
484 | + # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
485 | + $ctx[4] -= 128; |
|
486 | + |
|
487 | + # in += fill; |
|
488 | + $offset += $fill; |
|
489 | + |
|
490 | + # inlen -= fill; |
|
491 | + $plen -= $fill; |
|
492 | + } else { |
|
493 | + for ($i = $plen; $i--;) { |
|
494 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
495 | + } |
|
496 | + $ctx[4] += $plen; |
|
497 | + $offset += $plen; |
|
498 | + $plen -= $plen; |
|
499 | + } |
|
500 | + } |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * @internal You should not use this directly from another application |
|
505 | + * |
|
506 | + * @param SplFixedArray $ctx |
|
507 | + * @param SplFixedArray $out |
|
508 | + * @return SplFixedArray |
|
509 | + * @throws SodiumException |
|
510 | + * @throws TypeError |
|
511 | + * @psalm-suppress MixedArgument |
|
512 | + * @psalm-suppress MixedAssignment |
|
513 | + * @psalm-suppress MixedArrayAccess |
|
514 | + * @psalm-suppress MixedArrayAssignment |
|
515 | + * @psalm-suppress MixedArrayOffset |
|
516 | + * @psalm-suppress MixedOperand |
|
517 | + */ |
|
518 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
519 | + { |
|
520 | + self::pseudoConstructor(); |
|
521 | + if ($ctx[4] > 128) { |
|
522 | + self::increment_counter($ctx, 128); |
|
523 | + self::compress($ctx, $ctx[3]); |
|
524 | + $ctx[4] -= 128; |
|
525 | + if ($ctx[4] > 128) { |
|
526 | + throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
527 | + } |
|
528 | + for ($i = $ctx[4]; $i--;) { |
|
529 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
530 | + } |
|
531 | + } |
|
532 | + |
|
533 | + self::increment_counter($ctx, $ctx[4]); |
|
534 | + $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
535 | + |
|
536 | + for ($i = 256 - $ctx[4]; $i--;) { |
|
537 | + $ctx[3][$i+$ctx[4]] = 0; |
|
538 | + } |
|
539 | + |
|
540 | + self::compress($ctx, $ctx[3]); |
|
541 | + |
|
542 | + $i = (int) (($out->getSize() - 1) / 8); |
|
543 | + for (; $i >= 0; --$i) { |
|
544 | + self::store64($out, $i << 3, $ctx[0][$i]); |
|
545 | + } |
|
546 | + return $out; |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * @internal You should not use this directly from another application |
|
551 | + * |
|
552 | + * @param SplFixedArray|null $key |
|
553 | + * @param int $outlen |
|
554 | + * @param SplFixedArray|null $salt |
|
555 | + * @param SplFixedArray|null $personal |
|
556 | + * @return SplFixedArray |
|
557 | + * @throws SodiumException |
|
558 | + * @throws TypeError |
|
559 | + * @psalm-suppress MixedArgument |
|
560 | + * @psalm-suppress MixedAssignment |
|
561 | + * @psalm-suppress MixedArrayAccess |
|
562 | + * @psalm-suppress MixedArrayAssignment |
|
563 | + * @psalm-suppress MixedArrayOffset |
|
564 | + */ |
|
565 | + public static function init( |
|
566 | + $key = null, |
|
567 | + $outlen = 64, |
|
568 | + $salt = null, |
|
569 | + $personal = null |
|
570 | + ) { |
|
571 | + self::pseudoConstructor(); |
|
572 | + $klen = 0; |
|
573 | + |
|
574 | + if ($key !== null) { |
|
575 | + if (count($key) > 64) { |
|
576 | + throw new SodiumException('Invalid key size'); |
|
577 | + } |
|
578 | + $klen = count($key); |
|
579 | + } |
|
580 | + |
|
581 | + if ($outlen > 64) { |
|
582 | + throw new SodiumException('Invalid output size'); |
|
583 | + } |
|
584 | + |
|
585 | + $ctx = self::context(); |
|
586 | + |
|
587 | + $p = new SplFixedArray(64); |
|
588 | + // Zero our param buffer... |
|
589 | + for ($i = 64; --$i;) { |
|
590 | + $p[$i] = 0; |
|
591 | + } |
|
592 | + |
|
593 | + $p[0] = $outlen; // digest_length |
|
594 | + $p[1] = $klen; // key_length |
|
595 | + $p[2] = 1; // fanout |
|
596 | + $p[3] = 1; // depth |
|
597 | + |
|
598 | + if ($salt instanceof SplFixedArray) { |
|
599 | + // salt: [32] through [47] |
|
600 | + for ($i = 0; $i < 16; ++$i) { |
|
601 | + $p[32 + $i] = (int) $salt[$i]; |
|
602 | + } |
|
603 | + } |
|
604 | + if ($personal instanceof SplFixedArray) { |
|
605 | + // personal: [48] through [63] |
|
606 | + for ($i = 0; $i < 16; ++$i) { |
|
607 | + $p[48 + $i] = (int) $personal[$i]; |
|
608 | + } |
|
609 | + } |
|
610 | + |
|
611 | + $ctx[0][0] = self::xor64( |
|
612 | + $ctx[0][0], |
|
613 | + self::load64($p, 0) |
|
614 | + ); |
|
615 | + if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
616 | + // We need to do what blake2b_init_param() does: |
|
617 | + for ($i = 1; $i < 8; ++$i) { |
|
618 | + $ctx[0][$i] = self::xor64( |
|
619 | + $ctx[0][$i], |
|
620 | + self::load64($p, $i << 3) |
|
621 | + ); |
|
622 | + } |
|
623 | + } |
|
624 | + |
|
625 | + if ($klen > 0 && $key instanceof SplFixedArray) { |
|
626 | + $block = new SplFixedArray(128); |
|
627 | + for ($i = 128; $i--;) { |
|
628 | + $block[$i] = 0; |
|
629 | + } |
|
630 | + for ($i = $klen; $i--;) { |
|
631 | + $block[$i] = $key[$i]; |
|
632 | + } |
|
633 | + self::update($ctx, $block, 128); |
|
634 | + $ctx[4] = 128; |
|
635 | + } |
|
636 | + |
|
637 | + return $ctx; |
|
638 | + } |
|
639 | + |
|
640 | + /** |
|
641 | + * Convert a string into an SplFixedArray of integers |
|
642 | + * |
|
643 | + * @internal You should not use this directly from another application |
|
644 | + * |
|
645 | + * @param string $str |
|
646 | + * @return SplFixedArray |
|
647 | + * @psalm-suppress MixedArgumentTypeCoercion |
|
648 | + */ |
|
649 | + public static function stringToSplFixedArray($str = '') |
|
650 | + { |
|
651 | + $values = unpack('C*', $str); |
|
652 | + return SplFixedArray::fromArray(array_values($values)); |
|
653 | + } |
|
654 | + |
|
655 | + /** |
|
656 | + * Convert an SplFixedArray of integers into a string |
|
657 | + * |
|
658 | + * @internal You should not use this directly from another application |
|
659 | + * |
|
660 | + * @param SplFixedArray $a |
|
661 | + * @return string |
|
662 | + * @throws TypeError |
|
663 | + */ |
|
664 | + public static function SplFixedArrayToString(SplFixedArray $a) |
|
665 | + { |
|
666 | + /** |
|
667 | + * @var array<int, int|string> $arr |
|
668 | + */ |
|
669 | + $arr = $a->toArray(); |
|
670 | + $c = $a->count(); |
|
671 | + array_unshift($arr, str_repeat('C', $c)); |
|
672 | + return (string) (call_user_func_array('pack', $arr)); |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * @internal You should not use this directly from another application |
|
677 | + * |
|
678 | + * @param SplFixedArray $ctx |
|
679 | + * @return string |
|
680 | + * @throws TypeError |
|
681 | + * @psalm-suppress MixedArgument |
|
682 | + * @psalm-suppress MixedAssignment |
|
683 | + * @psalm-suppress MixedArrayAccess |
|
684 | + * @psalm-suppress MixedArrayAssignment |
|
685 | + * @psalm-suppress MixedArrayOffset |
|
686 | + * @psalm-suppress MixedMethodCall |
|
687 | + */ |
|
688 | + public static function contextToString(SplFixedArray $ctx) |
|
689 | + { |
|
690 | + $str = ''; |
|
691 | + /** @var array<int, array<int, int>> $ctxA */ |
|
692 | + $ctxA = $ctx[0]->toArray(); |
|
693 | + |
|
694 | + # uint64_t h[8]; |
|
695 | + for ($i = 0; $i < 8; ++$i) { |
|
696 | + $str .= self::store32_le($ctxA[$i][1]); |
|
697 | + $str .= self::store32_le($ctxA[$i][0]); |
|
698 | + } |
|
699 | + |
|
700 | + # uint64_t t[2]; |
|
701 | + # uint64_t f[2]; |
|
702 | + for ($i = 1; $i < 3; ++$i) { |
|
703 | + $ctxA = $ctx[$i]->toArray(); |
|
704 | + $str .= self::store32_le($ctxA[0][1]); |
|
705 | + $str .= self::store32_le($ctxA[0][0]); |
|
706 | + $str .= self::store32_le($ctxA[1][1]); |
|
707 | + $str .= self::store32_le($ctxA[1][0]); |
|
708 | + } |
|
709 | + |
|
710 | + # uint8_t buf[2 * 128]; |
|
711 | + $str .= self::SplFixedArrayToString($ctx[3]); |
|
712 | + |
|
713 | + /** @var int $ctx4 */ |
|
714 | + $ctx4 = (int) $ctx[4]; |
|
715 | + |
|
716 | + # size_t buflen; |
|
717 | + $str .= implode('', array( |
|
718 | + self::intToChr($ctx4 & 0xff), |
|
719 | + self::intToChr(($ctx4 >> 8) & 0xff), |
|
720 | + self::intToChr(($ctx4 >> 16) & 0xff), |
|
721 | + self::intToChr(($ctx4 >> 24) & 0xff), |
|
722 | + self::intToChr(($ctx4 >> 32) & 0xff), |
|
723 | + self::intToChr(($ctx4 >> 40) & 0xff), |
|
724 | + self::intToChr(($ctx4 >> 48) & 0xff), |
|
725 | + self::intToChr(($ctx4 >> 56) & 0xff) |
|
726 | + )); |
|
727 | + # uint8_t last_node; |
|
728 | + return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
729 | + } |
|
730 | + |
|
731 | + /** |
|
732 | + * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
733 | + * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
734 | + * |
|
735 | + * @internal You should not use this directly from another application |
|
736 | + * |
|
737 | + * @param string $string |
|
738 | + * @return SplFixedArray |
|
739 | + * @throws SodiumException |
|
740 | + * @throws TypeError |
|
741 | + * @psalm-suppress MixedArrayAssignment |
|
742 | + */ |
|
743 | + public static function stringToContext($string) |
|
744 | + { |
|
745 | + $ctx = self::context(); |
|
746 | + |
|
747 | + # uint64_t h[8]; |
|
748 | + for ($i = 0; $i < 8; ++$i) { |
|
749 | + $ctx[0][$i] = SplFixedArray::fromArray( |
|
750 | + array( |
|
751 | + self::load_4( |
|
752 | + self::substr($string, (($i << 3) + 4), 4) |
|
753 | + ), |
|
754 | + self::load_4( |
|
755 | + self::substr($string, (($i << 3) + 0), 4) |
|
756 | + ) |
|
757 | + ) |
|
758 | + ); |
|
759 | + } |
|
760 | + |
|
761 | + # uint64_t t[2]; |
|
762 | + # uint64_t f[2]; |
|
763 | + for ($i = 1; $i < 3; ++$i) { |
|
764 | + $ctx[$i][1] = SplFixedArray::fromArray( |
|
765 | + array( |
|
766 | + self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)), |
|
767 | + self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4)) |
|
768 | + ) |
|
769 | + ); |
|
770 | + $ctx[$i][0] = SplFixedArray::fromArray( |
|
771 | + array( |
|
772 | + self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)), |
|
773 | + self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4)) |
|
774 | + ) |
|
775 | + ); |
|
776 | + } |
|
777 | + |
|
778 | + # uint8_t buf[2 * 128]; |
|
779 | + $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
780 | + |
|
781 | + # uint8_t buf[2 * 128]; |
|
782 | + $int = 0; |
|
783 | + for ($i = 0; $i < 8; ++$i) { |
|
784 | + $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
785 | + } |
|
786 | + $ctx[4] = $int; |
|
787 | + |
|
788 | + return $ctx; |
|
789 | + } |
|
790 | 790 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_BLAKE2b', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -20,18 +20,18 @@ discard block |
||
20 | 20 | * @var array<int, array<int, int>> |
21 | 21 | */ |
22 | 22 | protected static $sigma = array( |
23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ), |
|
24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ), |
|
25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 ), |
|
26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 ), |
|
27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 ), |
|
28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 ), |
|
29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 ), |
|
30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 ), |
|
31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 ), |
|
32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 ), |
|
33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ), |
|
34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ) |
|
35 | 35 | ); |
36 | 36 | |
37 | 37 | const BLOCKBYTES = 128; |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | * @return SplFixedArray |
49 | 49 | * @psalm-suppress MixedAssignment |
50 | 50 | */ |
51 | - public static function new64($high, $low) |
|
51 | + public static function new64( $high, $low ) |
|
52 | 52 | { |
53 | - $i64 = new SplFixedArray(2); |
|
54 | - $i64[0] = $high & 0xffffffff; |
|
55 | - $i64[1] = $low & 0xffffffff; |
|
53 | + $i64 = new SplFixedArray( 2 ); |
|
54 | + $i64[ 0 ] = $high & 0xffffffff; |
|
55 | + $i64[ 1 ] = $low & 0xffffffff; |
|
56 | 56 | return $i64; |
57 | 57 | } |
58 | 58 | |
@@ -65,10 +65,10 @@ discard block |
||
65 | 65 | * @param int $num |
66 | 66 | * @return SplFixedArray |
67 | 67 | */ |
68 | - protected static function to64($num) |
|
68 | + protected static function to64( $num ) |
|
69 | 69 | { |
70 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
71 | - return self::new64($hi, $lo); |
|
70 | + list( $hi, $lo ) = self::numericTo64BitInteger( $num ); |
|
71 | + return self::new64( $hi, $lo ); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -84,14 +84,14 @@ discard block |
||
84 | 84 | * @psalm-suppress MixedAssignment |
85 | 85 | * @psalm-suppress MixedOperand |
86 | 86 | */ |
87 | - protected static function add64($x, $y) |
|
87 | + protected static function add64( $x, $y ) |
|
88 | 88 | { |
89 | - $l = ($x[1] + $y[1]) & 0xffffffff; |
|
89 | + $l = ( $x[ 1 ] + $y[ 1 ] ) & 0xffffffff; |
|
90 | 90 | return self::new64( |
91 | - (int) ($x[0] + $y[0] + ( |
|
92 | - ($l < $x[1]) ? 1 : 0 |
|
93 | - )), |
|
94 | - (int) $l |
|
91 | + (int)( $x[ 0 ] + $y[ 0 ] + ( |
|
92 | + ( $l < $x[ 1 ] ) ? 1 : 0 |
|
93 | + ) ), |
|
94 | + (int)$l |
|
95 | 95 | ); |
96 | 96 | } |
97 | 97 | |
@@ -103,9 +103,9 @@ discard block |
||
103 | 103 | * @param SplFixedArray $z |
104 | 104 | * @return SplFixedArray |
105 | 105 | */ |
106 | - protected static function add364($x, $y, $z) |
|
106 | + protected static function add364( $x, $y, $z ) |
|
107 | 107 | { |
108 | - return self::add64($x, self::add64($y, $z)); |
|
108 | + return self::add64( $x, self::add64( $y, $z ) ); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
@@ -117,23 +117,23 @@ discard block |
||
117 | 117 | * @throws SodiumException |
118 | 118 | * @throws TypeError |
119 | 119 | */ |
120 | - protected static function xor64(SplFixedArray $x, SplFixedArray $y) |
|
120 | + protected static function xor64( SplFixedArray $x, SplFixedArray $y ) |
|
121 | 121 | { |
122 | - if (!is_numeric($x[0])) { |
|
123 | - throw new SodiumException('x[0] is not an integer'); |
|
122 | + if ( ! is_numeric( $x[ 0 ] ) ) { |
|
123 | + throw new SodiumException( 'x[0] is not an integer' ); |
|
124 | 124 | } |
125 | - if (!is_numeric($x[1])) { |
|
126 | - throw new SodiumException('x[1] is not an integer'); |
|
125 | + if ( ! is_numeric( $x[ 1 ] ) ) { |
|
126 | + throw new SodiumException( 'x[1] is not an integer' ); |
|
127 | 127 | } |
128 | - if (!is_numeric($y[0])) { |
|
129 | - throw new SodiumException('y[0] is not an integer'); |
|
128 | + if ( ! is_numeric( $y[ 0 ] ) ) { |
|
129 | + throw new SodiumException( 'y[0] is not an integer' ); |
|
130 | 130 | } |
131 | - if (!is_numeric($y[1])) { |
|
132 | - throw new SodiumException('y[1] is not an integer'); |
|
131 | + if ( ! is_numeric( $y[ 1 ] ) ) { |
|
132 | + throw new SodiumException( 'y[1] is not an integer' ); |
|
133 | 133 | } |
134 | 134 | return self::new64( |
135 | - (int) (($x[0] ^ $y[0]) & 0xffffffff), |
|
136 | - (int) (($x[1] ^ $y[1]) & 0xffffffff) |
|
135 | + (int)( ( $x[ 0 ] ^ $y[ 0 ] ) & 0xffffffff ), |
|
136 | + (int)( ( $x[ 1 ] ^ $y[ 1 ] ) & 0xffffffff ) |
|
137 | 137 | ); |
138 | 138 | } |
139 | 139 | |
@@ -145,55 +145,55 @@ discard block |
||
145 | 145 | * @return SplFixedArray |
146 | 146 | * @psalm-suppress MixedAssignment |
147 | 147 | */ |
148 | - public static function rotr64($x, $c) |
|
148 | + public static function rotr64( $x, $c ) |
|
149 | 149 | { |
150 | - if ($c >= 64) { |
|
150 | + if ( $c >= 64 ) { |
|
151 | 151 | $c %= 64; |
152 | 152 | } |
153 | - if ($c >= 32) { |
|
153 | + if ( $c >= 32 ) { |
|
154 | 154 | /** @var int $tmp */ |
155 | - $tmp = $x[0]; |
|
156 | - $x[0] = $x[1]; |
|
157 | - $x[1] = $tmp; |
|
155 | + $tmp = $x[ 0 ]; |
|
156 | + $x[ 0 ] = $x[ 1 ]; |
|
157 | + $x[ 1 ] = $tmp; |
|
158 | 158 | $c -= 32; |
159 | 159 | } |
160 | - if ($c === 0) { |
|
160 | + if ( $c === 0 ) { |
|
161 | 161 | return $x; |
162 | 162 | } |
163 | 163 | |
164 | 164 | $l0 = 0; |
165 | 165 | $c = 64 - $c; |
166 | 166 | |
167 | - if ($c < 32) { |
|
167 | + if ( $c < 32 ) { |
|
168 | 168 | /** @var int $h0 */ |
169 | - $h0 = ((int) ($x[0]) << $c) | ( |
|
169 | + $h0 = ( (int)( $x[ 0 ] ) << $c ) | ( |
|
170 | 170 | ( |
171 | - (int) ($x[1]) & ((1 << $c) - 1) |
|
171 | + (int)( $x[ 1 ] ) & ( ( 1 << $c ) - 1 ) |
|
172 | 172 | << |
173 | - (32 - $c) |
|
174 | - ) >> (32 - $c) |
|
173 | + ( 32 - $c ) |
|
174 | + ) >> ( 32 - $c ) |
|
175 | 175 | ); |
176 | 176 | /** @var int $l0 */ |
177 | - $l0 = (int) ($x[1]) << $c; |
|
177 | + $l0 = (int)( $x[ 1 ] ) << $c; |
|
178 | 178 | } else { |
179 | 179 | /** @var int $h0 */ |
180 | - $h0 = (int) ($x[1]) << ($c - 32); |
|
180 | + $h0 = (int)( $x[ 1 ] ) << ( $c - 32 ); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | $h1 = 0; |
184 | 184 | $c1 = 64 - $c; |
185 | 185 | |
186 | - if ($c1 < 32) { |
|
186 | + if ( $c1 < 32 ) { |
|
187 | 187 | /** @var int $h1 */ |
188 | - $h1 = (int) ($x[0]) >> $c1; |
|
188 | + $h1 = (int)( $x[ 0 ] ) >> $c1; |
|
189 | 189 | /** @var int $l1 */ |
190 | - $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1); |
|
190 | + $l1 = ( (int)( $x[ 1 ] ) >> $c1 ) | ( (int)( $x[ 0 ] ) & ( ( 1 << $c1 ) - 1 ) ) << ( 32 - $c1 ); |
|
191 | 191 | } else { |
192 | 192 | /** @var int $l1 */ |
193 | - $l1 = (int) ($x[0]) >> ($c1 - 32); |
|
193 | + $l1 = (int)( $x[ 0 ] ) >> ( $c1 - 32 ); |
|
194 | 194 | } |
195 | 195 | |
196 | - return self::new64($h0 | $h1, $l0 | $l1); |
|
196 | + return self::new64( $h0 | $h1, $l0 | $l1 ); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | * @return int |
204 | 204 | * @psalm-suppress MixedOperand |
205 | 205 | */ |
206 | - protected static function flatten64($x) |
|
206 | + protected static function flatten64( $x ) |
|
207 | 207 | { |
208 | - return (int) ($x[0] * 4294967296 + $x[1]); |
|
208 | + return (int)( $x[ 0 ] * 4294967296 + $x[ 1 ] ); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
@@ -217,19 +217,19 @@ discard block |
||
217 | 217 | * @psalm-suppress MixedArgument |
218 | 218 | * @psalm-suppress MixedArrayOffset |
219 | 219 | */ |
220 | - protected static function load64(SplFixedArray $x, $i) |
|
220 | + protected static function load64( SplFixedArray $x, $i ) |
|
221 | 221 | { |
222 | 222 | /** @var int $l */ |
223 | - $l = (int) ($x[$i]) |
|
224 | - | ((int) ($x[$i+1]) << 8) |
|
225 | - | ((int) ($x[$i+2]) << 16) |
|
226 | - | ((int) ($x[$i+3]) << 24); |
|
223 | + $l = (int)( $x[ $i ] ) |
|
224 | + | ( (int)( $x[ $i + 1 ] ) << 8 ) |
|
225 | + | ( (int)( $x[ $i + 2 ] ) << 16 ) |
|
226 | + | ( (int)( $x[ $i + 3 ] ) << 24 ); |
|
227 | 227 | /** @var int $h */ |
228 | - $h = (int) ($x[$i+4]) |
|
229 | - | ((int) ($x[$i+5]) << 8) |
|
230 | - | ((int) ($x[$i+6]) << 16) |
|
231 | - | ((int) ($x[$i+7]) << 24); |
|
232 | - return self::new64($h, $l); |
|
228 | + $h = (int)( $x[ $i + 4 ] ) |
|
229 | + | ( (int)( $x[ $i + 5 ] ) << 8 ) |
|
230 | + | ( (int)( $x[ $i + 6 ] ) << 16 ) |
|
231 | + | ( (int)( $x[ $i + 7 ] ) << 24 ); |
|
232 | + return self::new64( $h, $l ); |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -241,23 +241,23 @@ discard block |
||
241 | 241 | * @return void |
242 | 242 | * @psalm-suppress MixedAssignment |
243 | 243 | */ |
244 | - protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) |
|
244 | + protected static function store64( SplFixedArray $x, $i, SplFixedArray $u ) |
|
245 | 245 | { |
246 | 246 | $maxLength = $x->getSize() - 1; |
247 | - for ($j = 0; $j < 8; ++$j) { |
|
247 | + for ( $j = 0; $j < 8; ++$j ) { |
|
248 | 248 | /* |
249 | 249 | [0, 1, 2, 3, 4, 5, 6, 7] |
250 | 250 | ... becomes ... |
251 | 251 | [0, 0, 0, 0, 1, 1, 1, 1] |
252 | 252 | */ |
253 | 253 | /** @var int $uIdx */ |
254 | - $uIdx = ((7 - $j) & 4) >> 2; |
|
255 | - $x[$i] = ((int) ($u[$uIdx]) & 0xff); |
|
254 | + $uIdx = ( ( 7 - $j ) & 4 ) >> 2; |
|
255 | + $x[ $i ] = ( (int)( $u[ $uIdx ] ) & 0xff ); |
|
256 | 256 | if (++$i > $maxLength) { |
257 | 257 | return; |
258 | 258 | } |
259 | 259 | /** @psalm-suppress MixedOperand */ |
260 | - $u[$uIdx] >>= 8; |
|
260 | + $u[ $uIdx ] >>= 8; |
|
261 | 261 | } |
262 | 262 | } |
263 | 263 | |
@@ -271,18 +271,18 @@ discard block |
||
271 | 271 | public static function pseudoConstructor() |
272 | 272 | { |
273 | 273 | static $called = false; |
274 | - if ($called) { |
|
274 | + if ( $called ) { |
|
275 | 275 | return; |
276 | 276 | } |
277 | - self::$iv = new SplFixedArray(8); |
|
278 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
279 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
280 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
281 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
282 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
283 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
284 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
285 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
277 | + self::$iv = new SplFixedArray( 8 ); |
|
278 | + self::$iv[ 0 ] = self::new64( 0x6a09e667, 0xf3bcc908 ); |
|
279 | + self::$iv[ 1 ] = self::new64( 0xbb67ae85, 0x84caa73b ); |
|
280 | + self::$iv[ 2 ] = self::new64( 0x3c6ef372, 0xfe94f82b ); |
|
281 | + self::$iv[ 3 ] = self::new64( 0xa54ff53a, 0x5f1d36f1 ); |
|
282 | + self::$iv[ 4 ] = self::new64( 0x510e527f, 0xade682d1 ); |
|
283 | + self::$iv[ 5 ] = self::new64( 0x9b05688c, 0x2b3e6c1f ); |
|
284 | + self::$iv[ 6 ] = self::new64( 0x1f83d9ab, 0xfb41bd6b ); |
|
285 | + self::$iv[ 7 ] = self::new64( 0x5be0cd19, 0x137e2179 ); |
|
286 | 286 | |
287 | 287 | $called = true; |
288 | 288 | } |
@@ -299,26 +299,26 @@ discard block |
||
299 | 299 | */ |
300 | 300 | protected static function context() |
301 | 301 | { |
302 | - $ctx = new SplFixedArray(6); |
|
303 | - $ctx[0] = new SplFixedArray(8); // h |
|
304 | - $ctx[1] = new SplFixedArray(2); // t |
|
305 | - $ctx[2] = new SplFixedArray(2); // f |
|
306 | - $ctx[3] = new SplFixedArray(256); // buf |
|
307 | - $ctx[4] = 0; // buflen |
|
308 | - $ctx[5] = 0; // last_node (uint8_t) |
|
302 | + $ctx = new SplFixedArray( 6 ); |
|
303 | + $ctx[ 0 ] = new SplFixedArray( 8 ); // h |
|
304 | + $ctx[ 1 ] = new SplFixedArray( 2 ); // t |
|
305 | + $ctx[ 2 ] = new SplFixedArray( 2 ); // f |
|
306 | + $ctx[ 3 ] = new SplFixedArray( 256 ); // buf |
|
307 | + $ctx[ 4 ] = 0; // buflen |
|
308 | + $ctx[ 5 ] = 0; // last_node (uint8_t) |
|
309 | 309 | |
310 | - for ($i = 8; $i--;) { |
|
311 | - $ctx[0][$i] = self::$iv[$i]; |
|
310 | + for ( $i = 8; $i--; ) { |
|
311 | + $ctx[ 0 ][ $i ] = self::$iv[ $i ]; |
|
312 | 312 | } |
313 | - for ($i = 256; $i--;) { |
|
314 | - $ctx[3][$i] = 0; |
|
313 | + for ( $i = 256; $i--; ) { |
|
314 | + $ctx[ 3 ][ $i ] = 0; |
|
315 | 315 | } |
316 | 316 | |
317 | - $zero = self::new64(0, 0); |
|
318 | - $ctx[1][0] = $zero; |
|
319 | - $ctx[1][1] = $zero; |
|
320 | - $ctx[2][0] = $zero; |
|
321 | - $ctx[2][1] = $zero; |
|
317 | + $zero = self::new64( 0, 0 ); |
|
318 | + $ctx[ 1 ][ 0 ] = $zero; |
|
319 | + $ctx[ 1 ][ 1 ] = $zero; |
|
320 | + $ctx[ 2 ][ 0 ] = $zero; |
|
321 | + $ctx[ 2 ][ 1 ] = $zero; |
|
322 | 322 | |
323 | 323 | return $ctx; |
324 | 324 | } |
@@ -337,43 +337,43 @@ discard block |
||
337 | 337 | * @psalm-suppress MixedArrayAssignment |
338 | 338 | * @psalm-suppress MixedArrayOffset |
339 | 339 | */ |
340 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
340 | + protected static function compress( SplFixedArray $ctx, SplFixedArray $buf ) |
|
341 | 341 | { |
342 | - $m = new SplFixedArray(16); |
|
343 | - $v = new SplFixedArray(16); |
|
342 | + $m = new SplFixedArray( 16 ); |
|
343 | + $v = new SplFixedArray( 16 ); |
|
344 | 344 | |
345 | - for ($i = 16; $i--;) { |
|
346 | - $m[$i] = self::load64($buf, $i << 3); |
|
345 | + for ( $i = 16; $i--; ) { |
|
346 | + $m[ $i ] = self::load64( $buf, $i << 3 ); |
|
347 | 347 | } |
348 | 348 | |
349 | - for ($i = 8; $i--;) { |
|
350 | - $v[$i] = $ctx[0][$i]; |
|
349 | + for ( $i = 8; $i--; ) { |
|
350 | + $v[ $i ] = $ctx[ 0 ][ $i ]; |
|
351 | 351 | } |
352 | 352 | |
353 | - $v[ 8] = self::$iv[0]; |
|
354 | - $v[ 9] = self::$iv[1]; |
|
355 | - $v[10] = self::$iv[2]; |
|
356 | - $v[11] = self::$iv[3]; |
|
353 | + $v[ 8 ] = self::$iv[ 0 ]; |
|
354 | + $v[ 9 ] = self::$iv[ 1 ]; |
|
355 | + $v[ 10 ] = self::$iv[ 2 ]; |
|
356 | + $v[ 11 ] = self::$iv[ 3 ]; |
|
357 | 357 | |
358 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
359 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
360 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
361 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
358 | + $v[ 12 ] = self::xor64( $ctx[ 1 ][ 0 ], self::$iv[ 4 ] ); |
|
359 | + $v[ 13 ] = self::xor64( $ctx[ 1 ][ 1 ], self::$iv[ 5 ] ); |
|
360 | + $v[ 14 ] = self::xor64( $ctx[ 2 ][ 0 ], self::$iv[ 6 ] ); |
|
361 | + $v[ 15 ] = self::xor64( $ctx[ 2 ][ 1 ], self::$iv[ 7 ] ); |
|
362 | 362 | |
363 | - for ($r = 0; $r < 12; ++$r) { |
|
364 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
365 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
366 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
367 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
368 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
369 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
370 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
371 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
363 | + for ( $r = 0; $r < 12; ++$r ) { |
|
364 | + $v = self::G( $r, 0, 0, 4, 8, 12, $v, $m ); |
|
365 | + $v = self::G( $r, 1, 1, 5, 9, 13, $v, $m ); |
|
366 | + $v = self::G( $r, 2, 2, 6, 10, 14, $v, $m ); |
|
367 | + $v = self::G( $r, 3, 3, 7, 11, 15, $v, $m ); |
|
368 | + $v = self::G( $r, 4, 0, 5, 10, 15, $v, $m ); |
|
369 | + $v = self::G( $r, 5, 1, 6, 11, 12, $v, $m ); |
|
370 | + $v = self::G( $r, 6, 2, 7, 8, 13, $v, $m ); |
|
371 | + $v = self::G( $r, 7, 3, 4, 9, 14, $v, $m ); |
|
372 | 372 | } |
373 | 373 | |
374 | - for ($i = 8; $i--;) { |
|
375 | - $ctx[0][$i] = self::xor64( |
|
376 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
374 | + for ( $i = 8; $i--; ) { |
|
375 | + $ctx[ 0 ][ $i ] = self::xor64( |
|
376 | + $ctx[ 0 ][ $i ], self::xor64( $v[ $i ], $v[ $i + 8 ] ) |
|
377 | 377 | ); |
378 | 378 | } |
379 | 379 | } |
@@ -395,16 +395,16 @@ discard block |
||
395 | 395 | * @psalm-suppress MixedArgument |
396 | 396 | * @psalm-suppress MixedArrayOffset |
397 | 397 | */ |
398 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
398 | + public static function G( $r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m ) |
|
399 | 399 | { |
400 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
401 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
402 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
403 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
404 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
405 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
406 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
407 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
400 | + $v[ $a ] = self::add364( $v[ $a ], $v[ $b ], $m[ self::$sigma[ $r ][ $i << 1 ] ] ); |
|
401 | + $v[ $d ] = self::rotr64( self::xor64( $v[ $d ], $v[ $a ] ), 32 ); |
|
402 | + $v[ $c ] = self::add64( $v[ $c ], $v[ $d ] ); |
|
403 | + $v[ $b ] = self::rotr64( self::xor64( $v[ $b ], $v[ $c ] ), 24 ); |
|
404 | + $v[ $a ] = self::add364( $v[ $a ], $v[ $b ], $m[ self::$sigma[ $r ][ ( $i << 1 ) + 1 ] ] ); |
|
405 | + $v[ $d ] = self::rotr64( self::xor64( $v[ $d ], $v[ $a ] ), 16 ); |
|
406 | + $v[ $c ] = self::add64( $v[ $c ], $v[ $d ] ); |
|
407 | + $v[ $b ] = self::rotr64( self::xor64( $v[ $b ], $v[ $c ] ), 63 ); |
|
408 | 408 | return $v; |
409 | 409 | } |
410 | 410 | |
@@ -419,20 +419,20 @@ discard block |
||
419 | 419 | * @psalm-suppress MixedArrayAccess |
420 | 420 | * @psalm-suppress MixedArrayAssignment |
421 | 421 | */ |
422 | - public static function increment_counter($ctx, $inc) |
|
422 | + public static function increment_counter( $ctx, $inc ) |
|
423 | 423 | { |
424 | - if ($inc < 0) { |
|
425 | - throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
424 | + if ( $inc < 0 ) { |
|
425 | + throw new SodiumException( 'Increasing by a negative number makes no sense.' ); |
|
426 | 426 | } |
427 | - $t = self::to64($inc); |
|
427 | + $t = self::to64( $inc ); |
|
428 | 428 | # S->t is $ctx[1] in our implementation |
429 | 429 | |
430 | 430 | # S->t[0] = ( uint64_t )( t >> 0 ); |
431 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
431 | + $ctx[ 1 ][ 0 ] = self::add64( $ctx[ 1 ][ 0 ], $t ); |
|
432 | 432 | |
433 | 433 | # S->t[1] += ( S->t[0] < inc ); |
434 | - if (self::flatten64($ctx[1][0]) < $inc) { |
|
435 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
434 | + if ( self::flatten64( $ctx[ 1 ][ 0 ] ) < $inc ) { |
|
435 | + $ctx[ 1 ][ 1 ] = self::add64( $ctx[ 1 ][ 1 ], self::to64( 1 ) ); |
|
436 | 436 | } |
437 | 437 | } |
438 | 438 | |
@@ -452,37 +452,37 @@ discard block |
||
452 | 452 | * @psalm-suppress MixedArrayOffset |
453 | 453 | * @psalm-suppress MixedOperand |
454 | 454 | */ |
455 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
455 | + public static function update( SplFixedArray $ctx, SplFixedArray $p, $plen ) |
|
456 | 456 | { |
457 | 457 | self::pseudoConstructor(); |
458 | 458 | |
459 | 459 | $offset = 0; |
460 | - while ($plen > 0) { |
|
461 | - $left = $ctx[4]; |
|
460 | + while ( $plen > 0 ) { |
|
461 | + $left = $ctx[ 4 ]; |
|
462 | 462 | $fill = 256 - $left; |
463 | 463 | |
464 | - if ($plen > $fill) { |
|
464 | + if ( $plen > $fill ) { |
|
465 | 465 | # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
466 | - for ($i = $fill; $i--;) { |
|
467 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
466 | + for ( $i = $fill; $i--; ) { |
|
467 | + $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ]; |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | # S->buflen += fill; |
471 | - $ctx[4] += $fill; |
|
471 | + $ctx[ 4 ] += $fill; |
|
472 | 472 | |
473 | 473 | # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
474 | - self::increment_counter($ctx, 128); |
|
474 | + self::increment_counter( $ctx, 128 ); |
|
475 | 475 | |
476 | 476 | # blake2b_compress( S, S->buf ); /* Compress */ |
477 | - self::compress($ctx, $ctx[3]); |
|
477 | + self::compress( $ctx, $ctx[ 3 ] ); |
|
478 | 478 | |
479 | 479 | # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
480 | - for ($i = 128; $i--;) { |
|
481 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
480 | + for ( $i = 128; $i--; ) { |
|
481 | + $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ]; |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | # S->buflen -= BLAKE2B_BLOCKBYTES; |
485 | - $ctx[4] -= 128; |
|
485 | + $ctx[ 4 ] -= 128; |
|
486 | 486 | |
487 | 487 | # in += fill; |
488 | 488 | $offset += $fill; |
@@ -490,10 +490,10 @@ discard block |
||
490 | 490 | # inlen -= fill; |
491 | 491 | $plen -= $fill; |
492 | 492 | } else { |
493 | - for ($i = $plen; $i--;) { |
|
494 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
493 | + for ( $i = $plen; $i--; ) { |
|
494 | + $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ]; |
|
495 | 495 | } |
496 | - $ctx[4] += $plen; |
|
496 | + $ctx[ 4 ] += $plen; |
|
497 | 497 | $offset += $plen; |
498 | 498 | $plen -= $plen; |
499 | 499 | } |
@@ -515,33 +515,33 @@ discard block |
||
515 | 515 | * @psalm-suppress MixedArrayOffset |
516 | 516 | * @psalm-suppress MixedOperand |
517 | 517 | */ |
518 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
518 | + public static function finish( SplFixedArray $ctx, SplFixedArray $out ) |
|
519 | 519 | { |
520 | 520 | self::pseudoConstructor(); |
521 | - if ($ctx[4] > 128) { |
|
522 | - self::increment_counter($ctx, 128); |
|
523 | - self::compress($ctx, $ctx[3]); |
|
524 | - $ctx[4] -= 128; |
|
525 | - if ($ctx[4] > 128) { |
|
526 | - throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
521 | + if ( $ctx[ 4 ] > 128 ) { |
|
522 | + self::increment_counter( $ctx, 128 ); |
|
523 | + self::compress( $ctx, $ctx[ 3 ] ); |
|
524 | + $ctx[ 4 ] -= 128; |
|
525 | + if ( $ctx[ 4 ] > 128 ) { |
|
526 | + throw new SodiumException( 'Failed to assert that buflen <= 128 bytes' ); |
|
527 | 527 | } |
528 | - for ($i = $ctx[4]; $i--;) { |
|
529 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
528 | + for ( $i = $ctx[ 4 ]; $i--; ) { |
|
529 | + $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ]; |
|
530 | 530 | } |
531 | 531 | } |
532 | 532 | |
533 | - self::increment_counter($ctx, $ctx[4]); |
|
534 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
533 | + self::increment_counter( $ctx, $ctx[ 4 ] ); |
|
534 | + $ctx[ 2 ][ 0 ] = self::new64( 0xffffffff, 0xffffffff ); |
|
535 | 535 | |
536 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
537 | - $ctx[3][$i+$ctx[4]] = 0; |
|
536 | + for ( $i = 256 - $ctx[ 4 ]; $i--; ) { |
|
537 | + $ctx[ 3 ][ $i + $ctx[ 4 ] ] = 0; |
|
538 | 538 | } |
539 | 539 | |
540 | - self::compress($ctx, $ctx[3]); |
|
540 | + self::compress( $ctx, $ctx[ 3 ] ); |
|
541 | 541 | |
542 | - $i = (int) (($out->getSize() - 1) / 8); |
|
543 | - for (; $i >= 0; --$i) { |
|
544 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
542 | + $i = (int)( ( $out->getSize() - 1 ) / 8 ); |
|
543 | + for ( ; $i >= 0; --$i ) { |
|
544 | + self::store64( $out, $i << 3, $ctx[ 0 ][ $i ] ); |
|
545 | 545 | } |
546 | 546 | return $out; |
547 | 547 | } |
@@ -571,67 +571,67 @@ discard block |
||
571 | 571 | self::pseudoConstructor(); |
572 | 572 | $klen = 0; |
573 | 573 | |
574 | - if ($key !== null) { |
|
575 | - if (count($key) > 64) { |
|
576 | - throw new SodiumException('Invalid key size'); |
|
574 | + if ( $key !== null ) { |
|
575 | + if ( count( $key ) > 64 ) { |
|
576 | + throw new SodiumException( 'Invalid key size' ); |
|
577 | 577 | } |
578 | - $klen = count($key); |
|
578 | + $klen = count( $key ); |
|
579 | 579 | } |
580 | 580 | |
581 | - if ($outlen > 64) { |
|
582 | - throw new SodiumException('Invalid output size'); |
|
581 | + if ( $outlen > 64 ) { |
|
582 | + throw new SodiumException( 'Invalid output size' ); |
|
583 | 583 | } |
584 | 584 | |
585 | 585 | $ctx = self::context(); |
586 | 586 | |
587 | - $p = new SplFixedArray(64); |
|
587 | + $p = new SplFixedArray( 64 ); |
|
588 | 588 | // Zero our param buffer... |
589 | - for ($i = 64; --$i;) { |
|
590 | - $p[$i] = 0; |
|
589 | + for ( $i = 64; --$i; ) { |
|
590 | + $p[ $i ] = 0; |
|
591 | 591 | } |
592 | 592 | |
593 | - $p[0] = $outlen; // digest_length |
|
594 | - $p[1] = $klen; // key_length |
|
595 | - $p[2] = 1; // fanout |
|
596 | - $p[3] = 1; // depth |
|
593 | + $p[ 0 ] = $outlen; // digest_length |
|
594 | + $p[ 1 ] = $klen; // key_length |
|
595 | + $p[ 2 ] = 1; // fanout |
|
596 | + $p[ 3 ] = 1; // depth |
|
597 | 597 | |
598 | - if ($salt instanceof SplFixedArray) { |
|
598 | + if ( $salt instanceof SplFixedArray ) { |
|
599 | 599 | // salt: [32] through [47] |
600 | - for ($i = 0; $i < 16; ++$i) { |
|
601 | - $p[32 + $i] = (int) $salt[$i]; |
|
600 | + for ( $i = 0; $i < 16; ++$i ) { |
|
601 | + $p[ 32 + $i ] = (int)$salt[ $i ]; |
|
602 | 602 | } |
603 | 603 | } |
604 | - if ($personal instanceof SplFixedArray) { |
|
604 | + if ( $personal instanceof SplFixedArray ) { |
|
605 | 605 | // personal: [48] through [63] |
606 | - for ($i = 0; $i < 16; ++$i) { |
|
607 | - $p[48 + $i] = (int) $personal[$i]; |
|
606 | + for ( $i = 0; $i < 16; ++$i ) { |
|
607 | + $p[ 48 + $i ] = (int)$personal[ $i ]; |
|
608 | 608 | } |
609 | 609 | } |
610 | 610 | |
611 | - $ctx[0][0] = self::xor64( |
|
612 | - $ctx[0][0], |
|
613 | - self::load64($p, 0) |
|
611 | + $ctx[ 0 ][ 0 ] = self::xor64( |
|
612 | + $ctx[ 0 ][ 0 ], |
|
613 | + self::load64( $p, 0 ) |
|
614 | 614 | ); |
615 | - if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
615 | + if ( $salt instanceof SplFixedArray || $personal instanceof SplFixedArray ) { |
|
616 | 616 | // We need to do what blake2b_init_param() does: |
617 | - for ($i = 1; $i < 8; ++$i) { |
|
618 | - $ctx[0][$i] = self::xor64( |
|
619 | - $ctx[0][$i], |
|
620 | - self::load64($p, $i << 3) |
|
617 | + for ( $i = 1; $i < 8; ++$i ) { |
|
618 | + $ctx[ 0 ][ $i ] = self::xor64( |
|
619 | + $ctx[ 0 ][ $i ], |
|
620 | + self::load64( $p, $i << 3 ) |
|
621 | 621 | ); |
622 | 622 | } |
623 | 623 | } |
624 | 624 | |
625 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
626 | - $block = new SplFixedArray(128); |
|
627 | - for ($i = 128; $i--;) { |
|
628 | - $block[$i] = 0; |
|
625 | + if ( $klen > 0 && $key instanceof SplFixedArray ) { |
|
626 | + $block = new SplFixedArray( 128 ); |
|
627 | + for ( $i = 128; $i--; ) { |
|
628 | + $block[ $i ] = 0; |
|
629 | 629 | } |
630 | - for ($i = $klen; $i--;) { |
|
631 | - $block[$i] = $key[$i]; |
|
630 | + for ( $i = $klen; $i--; ) { |
|
631 | + $block[ $i ] = $key[ $i ]; |
|
632 | 632 | } |
633 | - self::update($ctx, $block, 128); |
|
634 | - $ctx[4] = 128; |
|
633 | + self::update( $ctx, $block, 128 ); |
|
634 | + $ctx[ 4 ] = 128; |
|
635 | 635 | } |
636 | 636 | |
637 | 637 | return $ctx; |
@@ -646,10 +646,10 @@ discard block |
||
646 | 646 | * @return SplFixedArray |
647 | 647 | * @psalm-suppress MixedArgumentTypeCoercion |
648 | 648 | */ |
649 | - public static function stringToSplFixedArray($str = '') |
|
649 | + public static function stringToSplFixedArray( $str = '' ) |
|
650 | 650 | { |
651 | - $values = unpack('C*', $str); |
|
652 | - return SplFixedArray::fromArray(array_values($values)); |
|
651 | + $values = unpack( 'C*', $str ); |
|
652 | + return SplFixedArray::fromArray( array_values( $values ) ); |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
@@ -661,15 +661,15 @@ discard block |
||
661 | 661 | * @return string |
662 | 662 | * @throws TypeError |
663 | 663 | */ |
664 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
664 | + public static function SplFixedArrayToString( SplFixedArray $a ) |
|
665 | 665 | { |
666 | 666 | /** |
667 | 667 | * @var array<int, int|string> $arr |
668 | 668 | */ |
669 | 669 | $arr = $a->toArray(); |
670 | 670 | $c = $a->count(); |
671 | - array_unshift($arr, str_repeat('C', $c)); |
|
672 | - return (string) (call_user_func_array('pack', $arr)); |
|
671 | + array_unshift( $arr, str_repeat( 'C', $c ) ); |
|
672 | + return (string)( call_user_func_array( 'pack', $arr ) ); |
|
673 | 673 | } |
674 | 674 | |
675 | 675 | /** |
@@ -685,47 +685,47 @@ discard block |
||
685 | 685 | * @psalm-suppress MixedArrayOffset |
686 | 686 | * @psalm-suppress MixedMethodCall |
687 | 687 | */ |
688 | - public static function contextToString(SplFixedArray $ctx) |
|
688 | + public static function contextToString( SplFixedArray $ctx ) |
|
689 | 689 | { |
690 | 690 | $str = ''; |
691 | 691 | /** @var array<int, array<int, int>> $ctxA */ |
692 | - $ctxA = $ctx[0]->toArray(); |
|
692 | + $ctxA = $ctx[ 0 ]->toArray(); |
|
693 | 693 | |
694 | 694 | # uint64_t h[8]; |
695 | - for ($i = 0; $i < 8; ++$i) { |
|
696 | - $str .= self::store32_le($ctxA[$i][1]); |
|
697 | - $str .= self::store32_le($ctxA[$i][0]); |
|
695 | + for ( $i = 0; $i < 8; ++$i ) { |
|
696 | + $str .= self::store32_le( $ctxA[ $i ][ 1 ] ); |
|
697 | + $str .= self::store32_le( $ctxA[ $i ][ 0 ] ); |
|
698 | 698 | } |
699 | 699 | |
700 | 700 | # uint64_t t[2]; |
701 | 701 | # uint64_t f[2]; |
702 | - for ($i = 1; $i < 3; ++$i) { |
|
703 | - $ctxA = $ctx[$i]->toArray(); |
|
704 | - $str .= self::store32_le($ctxA[0][1]); |
|
705 | - $str .= self::store32_le($ctxA[0][0]); |
|
706 | - $str .= self::store32_le($ctxA[1][1]); |
|
707 | - $str .= self::store32_le($ctxA[1][0]); |
|
702 | + for ( $i = 1; $i < 3; ++$i ) { |
|
703 | + $ctxA = $ctx[ $i ]->toArray(); |
|
704 | + $str .= self::store32_le( $ctxA[ 0 ][ 1 ] ); |
|
705 | + $str .= self::store32_le( $ctxA[ 0 ][ 0 ] ); |
|
706 | + $str .= self::store32_le( $ctxA[ 1 ][ 1 ] ); |
|
707 | + $str .= self::store32_le( $ctxA[ 1 ][ 0 ] ); |
|
708 | 708 | } |
709 | 709 | |
710 | 710 | # uint8_t buf[2 * 128]; |
711 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
711 | + $str .= self::SplFixedArrayToString( $ctx[ 3 ] ); |
|
712 | 712 | |
713 | 713 | /** @var int $ctx4 */ |
714 | - $ctx4 = (int) $ctx[4]; |
|
714 | + $ctx4 = (int)$ctx[ 4 ]; |
|
715 | 715 | |
716 | 716 | # size_t buflen; |
717 | - $str .= implode('', array( |
|
718 | - self::intToChr($ctx4 & 0xff), |
|
719 | - self::intToChr(($ctx4 >> 8) & 0xff), |
|
720 | - self::intToChr(($ctx4 >> 16) & 0xff), |
|
721 | - self::intToChr(($ctx4 >> 24) & 0xff), |
|
722 | - self::intToChr(($ctx4 >> 32) & 0xff), |
|
723 | - self::intToChr(($ctx4 >> 40) & 0xff), |
|
724 | - self::intToChr(($ctx4 >> 48) & 0xff), |
|
725 | - self::intToChr(($ctx4 >> 56) & 0xff) |
|
726 | - )); |
|
717 | + $str .= implode( '', array( |
|
718 | + self::intToChr( $ctx4 & 0xff ), |
|
719 | + self::intToChr( ( $ctx4 >> 8 ) & 0xff ), |
|
720 | + self::intToChr( ( $ctx4 >> 16 ) & 0xff ), |
|
721 | + self::intToChr( ( $ctx4 >> 24 ) & 0xff ), |
|
722 | + self::intToChr( ( $ctx4 >> 32 ) & 0xff ), |
|
723 | + self::intToChr( ( $ctx4 >> 40 ) & 0xff ), |
|
724 | + self::intToChr( ( $ctx4 >> 48 ) & 0xff ), |
|
725 | + self::intToChr( ( $ctx4 >> 56 ) & 0xff ) |
|
726 | + ) ); |
|
727 | 727 | # uint8_t last_node; |
728 | - return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
728 | + return $str . self::intToChr( $ctx[ 5 ] ) . str_repeat( "\x00", 23 ); |
|
729 | 729 | } |
730 | 730 | |
731 | 731 | /** |
@@ -740,19 +740,19 @@ discard block |
||
740 | 740 | * @throws TypeError |
741 | 741 | * @psalm-suppress MixedArrayAssignment |
742 | 742 | */ |
743 | - public static function stringToContext($string) |
|
743 | + public static function stringToContext( $string ) |
|
744 | 744 | { |
745 | 745 | $ctx = self::context(); |
746 | 746 | |
747 | 747 | # uint64_t h[8]; |
748 | - for ($i = 0; $i < 8; ++$i) { |
|
749 | - $ctx[0][$i] = SplFixedArray::fromArray( |
|
748 | + for ( $i = 0; $i < 8; ++$i ) { |
|
749 | + $ctx[ 0 ][ $i ] = SplFixedArray::fromArray( |
|
750 | 750 | array( |
751 | 751 | self::load_4( |
752 | - self::substr($string, (($i << 3) + 4), 4) |
|
752 | + self::substr( $string, ( ( $i << 3 ) + 4 ), 4 ) |
|
753 | 753 | ), |
754 | 754 | self::load_4( |
755 | - self::substr($string, (($i << 3) + 0), 4) |
|
755 | + self::substr( $string, ( ( $i << 3 ) + 0 ), 4 ) |
|
756 | 756 | ) |
757 | 757 | ) |
758 | 758 | ); |
@@ -760,30 +760,30 @@ discard block |
||
760 | 760 | |
761 | 761 | # uint64_t t[2]; |
762 | 762 | # uint64_t f[2]; |
763 | - for ($i = 1; $i < 3; ++$i) { |
|
764 | - $ctx[$i][1] = SplFixedArray::fromArray( |
|
763 | + for ( $i = 1; $i < 3; ++$i ) { |
|
764 | + $ctx[ $i ][ 1 ] = SplFixedArray::fromArray( |
|
765 | 765 | array( |
766 | - self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)), |
|
767 | - self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4)) |
|
766 | + self::load_4( self::substr( $string, 76 + ( ( $i - 1 ) << 4 ), 4 ) ), |
|
767 | + self::load_4( self::substr( $string, 72 + ( ( $i - 1 ) << 4 ), 4 ) ) |
|
768 | 768 | ) |
769 | 769 | ); |
770 | - $ctx[$i][0] = SplFixedArray::fromArray( |
|
770 | + $ctx[ $i ][ 0 ] = SplFixedArray::fromArray( |
|
771 | 771 | array( |
772 | - self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)), |
|
773 | - self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4)) |
|
772 | + self::load_4( self::substr( $string, 68 + ( ( $i - 1 ) << 4 ), 4 ) ), |
|
773 | + self::load_4( self::substr( $string, 64 + ( ( $i - 1 ) << 4 ), 4 ) ) |
|
774 | 774 | ) |
775 | 775 | ); |
776 | 776 | } |
777 | 777 | |
778 | 778 | # uint8_t buf[2 * 128]; |
779 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
779 | + $ctx[ 3 ] = self::stringToSplFixedArray( self::substr( $string, 96, 256 ) ); |
|
780 | 780 | |
781 | 781 | # uint8_t buf[2 * 128]; |
782 | 782 | $int = 0; |
783 | - for ($i = 0; $i < 8; ++$i) { |
|
784 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
783 | + for ( $i = 0; $i < 8; ++$i ) { |
|
784 | + $int |= self::chrToInt( $string[ 352 + $i ] ) << ( $i << 3 ); |
|
785 | 785 | } |
786 | - $ctx[4] = $int; |
|
786 | + $ctx[ 4 ] = $int; |
|
787 | 787 | |
788 | 788 | return $ctx; |
789 | 789 | } |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | * |
10 | 10 | * Based on the work of Devi Mandiri in devi/salt. |
11 | 11 | */ |
12 | -abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util |
|
13 | -{ |
|
12 | +abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util { |
|
14 | 13 | /** |
15 | 14 | * @var SplFixedArray |
16 | 15 | */ |
@@ -48,8 +47,7 @@ discard block |
||
48 | 47 | * @return SplFixedArray |
49 | 48 | * @psalm-suppress MixedAssignment |
50 | 49 | */ |
51 | - public static function new64($high, $low) |
|
52 | - { |
|
50 | + public static function new64($high, $low) { |
|
53 | 51 | $i64 = new SplFixedArray(2); |
54 | 52 | $i64[0] = $high & 0xffffffff; |
55 | 53 | $i64[1] = $low & 0xffffffff; |
@@ -65,8 +63,7 @@ discard block |
||
65 | 63 | * @param int $num |
66 | 64 | * @return SplFixedArray |
67 | 65 | */ |
68 | - protected static function to64($num) |
|
69 | - { |
|
66 | + protected static function to64($num) { |
|
70 | 67 | list($hi, $lo) = self::numericTo64BitInteger($num); |
71 | 68 | return self::new64($hi, $lo); |
72 | 69 | } |
@@ -84,8 +81,7 @@ discard block |
||
84 | 81 | * @psalm-suppress MixedAssignment |
85 | 82 | * @psalm-suppress MixedOperand |
86 | 83 | */ |
87 | - protected static function add64($x, $y) |
|
88 | - { |
|
84 | + protected static function add64($x, $y) { |
|
89 | 85 | $l = ($x[1] + $y[1]) & 0xffffffff; |
90 | 86 | return self::new64( |
91 | 87 | (int) ($x[0] + $y[0] + ( |
@@ -103,8 +99,7 @@ discard block |
||
103 | 99 | * @param SplFixedArray $z |
104 | 100 | * @return SplFixedArray |
105 | 101 | */ |
106 | - protected static function add364($x, $y, $z) |
|
107 | - { |
|
102 | + protected static function add364($x, $y, $z) { |
|
108 | 103 | return self::add64($x, self::add64($y, $z)); |
109 | 104 | } |
110 | 105 | |
@@ -117,8 +112,7 @@ discard block |
||
117 | 112 | * @throws SodiumException |
118 | 113 | * @throws TypeError |
119 | 114 | */ |
120 | - protected static function xor64(SplFixedArray $x, SplFixedArray $y) |
|
121 | - { |
|
115 | + protected static function xor64(SplFixedArray $x, SplFixedArray $y) { |
|
122 | 116 | if (!is_numeric($x[0])) { |
123 | 117 | throw new SodiumException('x[0] is not an integer'); |
124 | 118 | } |
@@ -145,8 +139,7 @@ discard block |
||
145 | 139 | * @return SplFixedArray |
146 | 140 | * @psalm-suppress MixedAssignment |
147 | 141 | */ |
148 | - public static function rotr64($x, $c) |
|
149 | - { |
|
142 | + public static function rotr64($x, $c) { |
|
150 | 143 | if ($c >= 64) { |
151 | 144 | $c %= 64; |
152 | 145 | } |
@@ -203,8 +196,7 @@ discard block |
||
203 | 196 | * @return int |
204 | 197 | * @psalm-suppress MixedOperand |
205 | 198 | */ |
206 | - protected static function flatten64($x) |
|
207 | - { |
|
199 | + protected static function flatten64($x) { |
|
208 | 200 | return (int) ($x[0] * 4294967296 + $x[1]); |
209 | 201 | } |
210 | 202 | |
@@ -217,8 +209,7 @@ discard block |
||
217 | 209 | * @psalm-suppress MixedArgument |
218 | 210 | * @psalm-suppress MixedArrayOffset |
219 | 211 | */ |
220 | - protected static function load64(SplFixedArray $x, $i) |
|
221 | - { |
|
212 | + protected static function load64(SplFixedArray $x, $i) { |
|
222 | 213 | /** @var int $l */ |
223 | 214 | $l = (int) ($x[$i]) |
224 | 215 | | ((int) ($x[$i+1]) << 8) |
@@ -241,8 +232,7 @@ discard block |
||
241 | 232 | * @return void |
242 | 233 | * @psalm-suppress MixedAssignment |
243 | 234 | */ |
244 | - protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) |
|
245 | - { |
|
235 | + protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) { |
|
246 | 236 | $maxLength = $x->getSize() - 1; |
247 | 237 | for ($j = 0; $j < 8; ++$j) { |
248 | 238 | /* |
@@ -268,8 +258,7 @@ discard block |
||
268 | 258 | * |
269 | 259 | * @return void |
270 | 260 | */ |
271 | - public static function pseudoConstructor() |
|
272 | - { |
|
261 | + public static function pseudoConstructor() { |
|
273 | 262 | static $called = false; |
274 | 263 | if ($called) { |
275 | 264 | return; |
@@ -297,8 +286,7 @@ discard block |
||
297 | 286 | * @psalm-suppress MixedArrayAccess |
298 | 287 | * @psalm-suppress MixedArrayAssignment |
299 | 288 | */ |
300 | - protected static function context() |
|
301 | - { |
|
289 | + protected static function context() { |
|
302 | 290 | $ctx = new SplFixedArray(6); |
303 | 291 | $ctx[0] = new SplFixedArray(8); // h |
304 | 292 | $ctx[1] = new SplFixedArray(2); // t |
@@ -337,8 +325,7 @@ discard block |
||
337 | 325 | * @psalm-suppress MixedArrayAssignment |
338 | 326 | * @psalm-suppress MixedArrayOffset |
339 | 327 | */ |
340 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
341 | - { |
|
328 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) { |
|
342 | 329 | $m = new SplFixedArray(16); |
343 | 330 | $v = new SplFixedArray(16); |
344 | 331 | |
@@ -395,8 +382,7 @@ discard block |
||
395 | 382 | * @psalm-suppress MixedArgument |
396 | 383 | * @psalm-suppress MixedArrayOffset |
397 | 384 | */ |
398 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
399 | - { |
|
385 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) { |
|
400 | 386 | $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
401 | 387 | $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
402 | 388 | $v[$c] = self::add64($v[$c], $v[$d]); |
@@ -419,8 +405,7 @@ discard block |
||
419 | 405 | * @psalm-suppress MixedArrayAccess |
420 | 406 | * @psalm-suppress MixedArrayAssignment |
421 | 407 | */ |
422 | - public static function increment_counter($ctx, $inc) |
|
423 | - { |
|
408 | + public static function increment_counter($ctx, $inc) { |
|
424 | 409 | if ($inc < 0) { |
425 | 410 | throw new SodiumException('Increasing by a negative number makes no sense.'); |
426 | 411 | } |
@@ -452,8 +437,7 @@ discard block |
||
452 | 437 | * @psalm-suppress MixedArrayOffset |
453 | 438 | * @psalm-suppress MixedOperand |
454 | 439 | */ |
455 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
456 | - { |
|
440 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) { |
|
457 | 441 | self::pseudoConstructor(); |
458 | 442 | |
459 | 443 | $offset = 0; |
@@ -515,8 +499,7 @@ discard block |
||
515 | 499 | * @psalm-suppress MixedArrayOffset |
516 | 500 | * @psalm-suppress MixedOperand |
517 | 501 | */ |
518 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
519 | - { |
|
502 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) { |
|
520 | 503 | self::pseudoConstructor(); |
521 | 504 | if ($ctx[4] > 128) { |
522 | 505 | self::increment_counter($ctx, 128); |
@@ -646,8 +629,7 @@ discard block |
||
646 | 629 | * @return SplFixedArray |
647 | 630 | * @psalm-suppress MixedArgumentTypeCoercion |
648 | 631 | */ |
649 | - public static function stringToSplFixedArray($str = '') |
|
650 | - { |
|
632 | + public static function stringToSplFixedArray($str = '') { |
|
651 | 633 | $values = unpack('C*', $str); |
652 | 634 | return SplFixedArray::fromArray(array_values($values)); |
653 | 635 | } |
@@ -661,8 +643,7 @@ discard block |
||
661 | 643 | * @return string |
662 | 644 | * @throws TypeError |
663 | 645 | */ |
664 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
665 | - { |
|
646 | + public static function SplFixedArrayToString(SplFixedArray $a) { |
|
666 | 647 | /** |
667 | 648 | * @var array<int, int|string> $arr |
668 | 649 | */ |
@@ -685,8 +666,7 @@ discard block |
||
685 | 666 | * @psalm-suppress MixedArrayOffset |
686 | 667 | * @psalm-suppress MixedMethodCall |
687 | 668 | */ |
688 | - public static function contextToString(SplFixedArray $ctx) |
|
689 | - { |
|
669 | + public static function contextToString(SplFixedArray $ctx) { |
|
690 | 670 | $str = ''; |
691 | 671 | /** @var array<int, array<int, int>> $ctxA */ |
692 | 672 | $ctxA = $ctx[0]->toArray(); |
@@ -740,8 +720,7 @@ discard block |
||
740 | 720 | * @throws TypeError |
741 | 721 | * @psalm-suppress MixedArrayAssignment |
742 | 722 | */ |
743 | - public static function stringToContext($string) |
|
744 | - { |
|
723 | + public static function stringToContext($string) { |
|
745 | 724 | $ctx = self::context(); |
746 | 725 | |
747 | 726 | # uint64_t h[8]; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,543 +9,543 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 |
11 | 11 | { |
12 | - const KEYPAIR_BYTES = 96; |
|
13 | - const SEED_BYTES = 32; |
|
14 | - const SCALAR_BYTES = 32; |
|
15 | - |
|
16 | - /** |
|
17 | - * @internal You should not use this directly from another application |
|
18 | - * |
|
19 | - * @return string (96 bytes) |
|
20 | - * @throws Exception |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - */ |
|
24 | - public static function keypair() |
|
25 | - { |
|
26 | - $seed = random_bytes(self::SEED_BYTES); |
|
27 | - $pk = ''; |
|
28 | - $sk = ''; |
|
29 | - self::seed_keypair($pk, $sk, $seed); |
|
30 | - return $sk . $pk; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * @internal You should not use this directly from another application |
|
35 | - * |
|
36 | - * @param string $pk |
|
37 | - * @param string $sk |
|
38 | - * @param string $seed |
|
39 | - * @return string |
|
40 | - * @throws SodiumException |
|
41 | - * @throws TypeError |
|
42 | - */ |
|
43 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
44 | - { |
|
45 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
46 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
47 | - } |
|
48 | - |
|
49 | - /** @var string $pk */ |
|
50 | - $pk = self::publickey_from_secretkey($seed); |
|
51 | - $sk = $seed . $pk; |
|
52 | - return $sk; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @internal You should not use this directly from another application |
|
57 | - * |
|
58 | - * @param string $keypair |
|
59 | - * @return string |
|
60 | - * @throws TypeError |
|
61 | - */ |
|
62 | - public static function secretkey($keypair) |
|
63 | - { |
|
64 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
65 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
66 | - } |
|
67 | - return self::substr($keypair, 0, 64); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @internal You should not use this directly from another application |
|
72 | - * |
|
73 | - * @param string $keypair |
|
74 | - * @return string |
|
75 | - * @throws TypeError |
|
76 | - */ |
|
77 | - public static function publickey($keypair) |
|
78 | - { |
|
79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
81 | - } |
|
82 | - return self::substr($keypair, 64, 32); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @internal You should not use this directly from another application |
|
87 | - * |
|
88 | - * @param string $sk |
|
89 | - * @return string |
|
90 | - * @throws SodiumException |
|
91 | - * @throws TypeError |
|
92 | - */ |
|
93 | - public static function publickey_from_secretkey($sk) |
|
94 | - { |
|
95 | - /** @var string $sk */ |
|
96 | - $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | - $sk[0] = self::intToChr( |
|
98 | - self::chrToInt($sk[0]) & 248 |
|
99 | - ); |
|
100 | - $sk[31] = self::intToChr( |
|
101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
102 | - ); |
|
103 | - return self::sk_to_pk($sk); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $pk |
|
108 | - * @return string |
|
109 | - * @throws SodiumException |
|
110 | - * @throws TypeError |
|
111 | - */ |
|
112 | - public static function pk_to_curve25519($pk) |
|
113 | - { |
|
114 | - if (self::small_order($pk)) { |
|
115 | - throw new SodiumException('Public key is on a small order'); |
|
116 | - } |
|
117 | - $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32)); |
|
118 | - $p1 = self::ge_mul_l($A); |
|
119 | - if (!self::fe_isnonzero($p1->X)) { |
|
120 | - throw new SodiumException('Unexpected zero result'); |
|
121 | - } |
|
122 | - |
|
123 | - # fe_1(one_minus_y); |
|
124 | - # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
125 | - # fe_invert(one_minus_y, one_minus_y); |
|
126 | - $one_minux_y = self::fe_invert( |
|
127 | - self::fe_sub( |
|
128 | - self::fe_1(), |
|
129 | - $A->Y |
|
130 | - ) |
|
131 | - ); |
|
132 | - |
|
133 | - # fe_1(x); |
|
134 | - # fe_add(x, x, A.Y); |
|
135 | - # fe_mul(x, x, one_minus_y); |
|
136 | - $x = self::fe_mul( |
|
137 | - self::fe_add(self::fe_1(), $A->Y), |
|
138 | - $one_minux_y |
|
139 | - ); |
|
140 | - |
|
141 | - # fe_tobytes(curve25519_pk, x); |
|
142 | - return self::fe_tobytes($x); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @internal You should not use this directly from another application |
|
147 | - * |
|
148 | - * @param string $sk |
|
149 | - * @return string |
|
150 | - * @throws SodiumException |
|
151 | - * @throws TypeError |
|
152 | - */ |
|
153 | - public static function sk_to_pk($sk) |
|
154 | - { |
|
155 | - return self::ge_p3_tobytes( |
|
156 | - self::ge_scalarmult_base( |
|
157 | - self::substr($sk, 0, 32) |
|
158 | - ) |
|
159 | - ); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @internal You should not use this directly from another application |
|
164 | - * |
|
165 | - * @param string $message |
|
166 | - * @param string $sk |
|
167 | - * @return string |
|
168 | - * @throws SodiumException |
|
169 | - * @throws TypeError |
|
170 | - */ |
|
171 | - public static function sign($message, $sk) |
|
172 | - { |
|
173 | - /** @var string $signature */ |
|
174 | - $signature = self::sign_detached($message, $sk); |
|
175 | - return $signature . $message; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @internal You should not use this directly from another application |
|
180 | - * |
|
181 | - * @param string $message A signed message |
|
182 | - * @param string $pk Public key |
|
183 | - * @return string Message (without signature) |
|
184 | - * @throws SodiumException |
|
185 | - * @throws TypeError |
|
186 | - */ |
|
187 | - public static function sign_open($message, $pk) |
|
188 | - { |
|
189 | - /** @var string $signature */ |
|
190 | - $signature = self::substr($message, 0, 64); |
|
191 | - |
|
192 | - /** @var string $message */ |
|
193 | - $message = self::substr($message, 64); |
|
194 | - |
|
195 | - if (self::verify_detached($signature, $message, $pk)) { |
|
196 | - return $message; |
|
197 | - } |
|
198 | - throw new SodiumException('Invalid signature'); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @internal You should not use this directly from another application |
|
203 | - * |
|
204 | - * @param string $message |
|
205 | - * @param string $sk |
|
206 | - * @return string |
|
207 | - * @throws SodiumException |
|
208 | - * @throws TypeError |
|
209 | - */ |
|
210 | - public static function sign_detached($message, $sk) |
|
211 | - { |
|
212 | - # crypto_hash_sha512(az, sk, 32); |
|
213 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
214 | - |
|
215 | - # az[0] &= 248; |
|
216 | - # az[31] &= 63; |
|
217 | - # az[31] |= 64; |
|
218 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
219 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
220 | - |
|
221 | - # crypto_hash_sha512_init(&hs); |
|
222 | - # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
223 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
224 | - # crypto_hash_sha512_final(&hs, nonce); |
|
225 | - $hs = hash_init('sha512'); |
|
226 | - hash_update($hs, self::substr($az, 32, 32)); |
|
227 | - hash_update($hs, $message); |
|
228 | - $nonceHash = hash_final($hs, true); |
|
229 | - |
|
230 | - # memmove(sig + 32, sk + 32, 32); |
|
231 | - $pk = self::substr($sk, 32, 32); |
|
232 | - |
|
233 | - # sc_reduce(nonce); |
|
234 | - # ge_scalarmult_base(&R, nonce); |
|
235 | - # ge_p3_tobytes(sig, &R); |
|
236 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
237 | - $sig = self::ge_p3_tobytes( |
|
238 | - self::ge_scalarmult_base($nonce) |
|
239 | - ); |
|
240 | - |
|
241 | - # crypto_hash_sha512_init(&hs); |
|
242 | - # crypto_hash_sha512_update(&hs, sig, 64); |
|
243 | - # crypto_hash_sha512_update(&hs, m, mlen); |
|
244 | - # crypto_hash_sha512_final(&hs, hram); |
|
245 | - $hs = hash_init('sha512'); |
|
246 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
247 | - hash_update($hs, self::substr($pk, 0, 32)); |
|
248 | - hash_update($hs, $message); |
|
249 | - $hramHash = hash_final($hs, true); |
|
250 | - |
|
251 | - # sc_reduce(hram); |
|
252 | - # sc_muladd(sig + 32, hram, az, nonce); |
|
253 | - $hram = self::sc_reduce($hramHash); |
|
254 | - $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
255 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
256 | - |
|
257 | - try { |
|
258 | - ParagonIE_Sodium_Compat::memzero($az); |
|
259 | - } catch (SodiumException $ex) { |
|
260 | - $az = null; |
|
261 | - } |
|
262 | - return $sig; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @internal You should not use this directly from another application |
|
267 | - * |
|
268 | - * @param string $sig |
|
269 | - * @param string $message |
|
270 | - * @param string $pk |
|
271 | - * @return bool |
|
272 | - * @throws SodiumException |
|
273 | - * @throws TypeError |
|
274 | - */ |
|
275 | - public static function verify_detached($sig, $message, $pk) |
|
276 | - { |
|
277 | - if (self::strlen($sig) < 64) { |
|
278 | - throw new SodiumException('Signature is too short'); |
|
279 | - } |
|
280 | - if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
281 | - throw new SodiumException('S < L - Invalid signature'); |
|
282 | - } |
|
283 | - if (self::small_order($sig)) { |
|
284 | - throw new SodiumException('Signature is on too small of an order'); |
|
285 | - } |
|
286 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
287 | - throw new SodiumException('Invalid signature'); |
|
288 | - } |
|
289 | - $d = 0; |
|
290 | - for ($i = 0; $i < 32; ++$i) { |
|
291 | - $d |= self::chrToInt($pk[$i]); |
|
292 | - } |
|
293 | - if ($d === 0) { |
|
294 | - throw new SodiumException('All zero public key'); |
|
295 | - } |
|
296 | - |
|
297 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
298 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
299 | - |
|
300 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
301 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
302 | - |
|
303 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
304 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
305 | - |
|
306 | - /** @var string $hDigest */ |
|
307 | - $hDigest = hash( |
|
308 | - 'sha512', |
|
309 | - self::substr($sig, 0, 32) . |
|
310 | - self::substr($pk, 0, 32) . |
|
311 | - $message, |
|
312 | - true |
|
313 | - ); |
|
314 | - |
|
315 | - /** @var string $h */ |
|
316 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
317 | - |
|
318 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
319 | - $R = self::ge_double_scalarmult_vartime( |
|
320 | - $h, |
|
321 | - $A, |
|
322 | - self::substr($sig, 32) |
|
323 | - ); |
|
324 | - |
|
325 | - /** @var string $rcheck */ |
|
326 | - $rcheck = self::ge_tobytes($R); |
|
327 | - |
|
328 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
329 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
330 | - |
|
331 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @internal You should not use this directly from another application |
|
336 | - * |
|
337 | - * @param string $S |
|
338 | - * @return bool |
|
339 | - * @throws SodiumException |
|
340 | - * @throws TypeError |
|
341 | - */ |
|
342 | - public static function check_S_lt_L($S) |
|
343 | - { |
|
344 | - if (self::strlen($S) < 32) { |
|
345 | - throw new SodiumException('Signature must be 32 bytes'); |
|
346 | - } |
|
347 | - $L = array( |
|
348 | - 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
349 | - 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
350 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
351 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
352 | - ); |
|
353 | - $c = 0; |
|
354 | - $n = 1; |
|
355 | - $i = 32; |
|
356 | - |
|
357 | - /** @var array<int, int> $L */ |
|
358 | - do { |
|
359 | - --$i; |
|
360 | - $x = self::chrToInt($S[$i]); |
|
361 | - $c |= ( |
|
362 | - (($x - $L[$i]) >> 8) & $n |
|
363 | - ); |
|
364 | - $n &= ( |
|
365 | - (($x ^ $L[$i]) - 1) >> 8 |
|
366 | - ); |
|
367 | - } while ($i !== 0); |
|
368 | - |
|
369 | - return $c === 0; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param string $R |
|
374 | - * @return bool |
|
375 | - * @throws SodiumException |
|
376 | - * @throws TypeError |
|
377 | - */ |
|
378 | - public static function small_order($R) |
|
379 | - { |
|
380 | - /** @var array<int, array<int, int>> $blocklist */ |
|
381 | - $blocklist = array( |
|
382 | - /* 0 (order 4) */ |
|
383 | - array( |
|
384 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
385 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
386 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
387 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
388 | - ), |
|
389 | - /* 1 (order 1) */ |
|
390 | - array( |
|
391 | - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
392 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
393 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
394 | - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
395 | - ), |
|
396 | - /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
397 | - array( |
|
398 | - 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
399 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
400 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
401 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
402 | - ), |
|
403 | - /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
404 | - array( |
|
405 | - 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
406 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
407 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
408 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
409 | - ), |
|
410 | - /* p-1 (order 2) */ |
|
411 | - array( |
|
412 | - 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
413 | - 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
414 | - 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
415 | - 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
416 | - ), |
|
417 | - /* p (order 4) */ |
|
418 | - array( |
|
419 | - 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
420 | - 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
421 | - 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
422 | - 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
423 | - ), |
|
424 | - /* p+1 (order 1) */ |
|
425 | - array( |
|
426 | - 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
427 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
428 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
429 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
430 | - ), |
|
431 | - /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
432 | - array( |
|
433 | - 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
434 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
435 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
436 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
437 | - ), |
|
438 | - /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
439 | - array( |
|
440 | - 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
441 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
442 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
443 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
444 | - ), |
|
445 | - /* 2p-1 (order 2) */ |
|
446 | - array( |
|
447 | - 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
448 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
449 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
450 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
451 | - ), |
|
452 | - /* 2p (order 4) */ |
|
453 | - array( |
|
454 | - 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
455 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
456 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
457 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
458 | - ), |
|
459 | - /* 2p+1 (order 1) */ |
|
460 | - array( |
|
461 | - 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
462 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
463 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
464 | - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
465 | - ) |
|
466 | - ); |
|
467 | - /** @var int $countBlocklist */ |
|
468 | - $countBlocklist = count($blocklist); |
|
469 | - |
|
470 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
471 | - $c = 0; |
|
472 | - for ($j = 0; $j < 32; ++$j) { |
|
473 | - $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j]; |
|
474 | - } |
|
475 | - if ($c === 0) { |
|
476 | - return true; |
|
477 | - } |
|
478 | - } |
|
479 | - return false; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * @param string $s |
|
484 | - * @return string |
|
485 | - * @throws SodiumException |
|
486 | - */ |
|
487 | - public static function scalar_complement($s) |
|
488 | - { |
|
489 | - $t_ = self::L . str_repeat("\x00", 32); |
|
490 | - sodium_increment($t_); |
|
491 | - $s_ = $s . str_repeat("\x00", 32); |
|
492 | - ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
493 | - return self::sc_reduce($t_); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * @return string |
|
498 | - * @throws SodiumException |
|
499 | - */ |
|
500 | - public static function scalar_random() |
|
501 | - { |
|
502 | - do { |
|
503 | - $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES); |
|
504 | - $r[self::SCALAR_BYTES - 1] = self::intToChr( |
|
505 | - self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f |
|
506 | - ); |
|
507 | - } while ( |
|
508 | - !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r) |
|
509 | - ); |
|
510 | - return $r; |
|
511 | - } |
|
512 | - |
|
513 | - /** |
|
514 | - * @param string $s |
|
515 | - * @return string |
|
516 | - * @throws SodiumException |
|
517 | - */ |
|
518 | - public static function scalar_negate($s) |
|
519 | - { |
|
520 | - $t_ = self::L . str_repeat("\x00", 32) ; |
|
521 | - $s_ = $s . str_repeat("\x00", 32) ; |
|
522 | - ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
523 | - return self::sc_reduce($t_); |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * @param string $a |
|
528 | - * @param string $b |
|
529 | - * @return string |
|
530 | - * @throws SodiumException |
|
531 | - */ |
|
532 | - public static function scalar_add($a, $b) |
|
533 | - { |
|
534 | - $a_ = $a . str_repeat("\x00", 32); |
|
535 | - $b_ = $b . str_repeat("\x00", 32); |
|
536 | - ParagonIE_Sodium_Compat::add($a_, $b_); |
|
537 | - return self::sc_reduce($a_); |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * @param string $x |
|
542 | - * @param string $y |
|
543 | - * @return string |
|
544 | - * @throws SodiumException |
|
545 | - */ |
|
546 | - public static function scalar_sub($x, $y) |
|
547 | - { |
|
548 | - $yn = self::scalar_negate($y); |
|
549 | - return self::scalar_add($x, $yn); |
|
550 | - } |
|
12 | + const KEYPAIR_BYTES = 96; |
|
13 | + const SEED_BYTES = 32; |
|
14 | + const SCALAR_BYTES = 32; |
|
15 | + |
|
16 | + /** |
|
17 | + * @internal You should not use this directly from another application |
|
18 | + * |
|
19 | + * @return string (96 bytes) |
|
20 | + * @throws Exception |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + */ |
|
24 | + public static function keypair() |
|
25 | + { |
|
26 | + $seed = random_bytes(self::SEED_BYTES); |
|
27 | + $pk = ''; |
|
28 | + $sk = ''; |
|
29 | + self::seed_keypair($pk, $sk, $seed); |
|
30 | + return $sk . $pk; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * @internal You should not use this directly from another application |
|
35 | + * |
|
36 | + * @param string $pk |
|
37 | + * @param string $sk |
|
38 | + * @param string $seed |
|
39 | + * @return string |
|
40 | + * @throws SodiumException |
|
41 | + * @throws TypeError |
|
42 | + */ |
|
43 | + public static function seed_keypair(&$pk, &$sk, $seed) |
|
44 | + { |
|
45 | + if (self::strlen($seed) !== self::SEED_BYTES) { |
|
46 | + throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
47 | + } |
|
48 | + |
|
49 | + /** @var string $pk */ |
|
50 | + $pk = self::publickey_from_secretkey($seed); |
|
51 | + $sk = $seed . $pk; |
|
52 | + return $sk; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @internal You should not use this directly from another application |
|
57 | + * |
|
58 | + * @param string $keypair |
|
59 | + * @return string |
|
60 | + * @throws TypeError |
|
61 | + */ |
|
62 | + public static function secretkey($keypair) |
|
63 | + { |
|
64 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
65 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
66 | + } |
|
67 | + return self::substr($keypair, 0, 64); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @internal You should not use this directly from another application |
|
72 | + * |
|
73 | + * @param string $keypair |
|
74 | + * @return string |
|
75 | + * @throws TypeError |
|
76 | + */ |
|
77 | + public static function publickey($keypair) |
|
78 | + { |
|
79 | + if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | + throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
81 | + } |
|
82 | + return self::substr($keypair, 64, 32); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @internal You should not use this directly from another application |
|
87 | + * |
|
88 | + * @param string $sk |
|
89 | + * @return string |
|
90 | + * @throws SodiumException |
|
91 | + * @throws TypeError |
|
92 | + */ |
|
93 | + public static function publickey_from_secretkey($sk) |
|
94 | + { |
|
95 | + /** @var string $sk */ |
|
96 | + $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | + $sk[0] = self::intToChr( |
|
98 | + self::chrToInt($sk[0]) & 248 |
|
99 | + ); |
|
100 | + $sk[31] = self::intToChr( |
|
101 | + (self::chrToInt($sk[31]) & 63) | 64 |
|
102 | + ); |
|
103 | + return self::sk_to_pk($sk); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $pk |
|
108 | + * @return string |
|
109 | + * @throws SodiumException |
|
110 | + * @throws TypeError |
|
111 | + */ |
|
112 | + public static function pk_to_curve25519($pk) |
|
113 | + { |
|
114 | + if (self::small_order($pk)) { |
|
115 | + throw new SodiumException('Public key is on a small order'); |
|
116 | + } |
|
117 | + $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32)); |
|
118 | + $p1 = self::ge_mul_l($A); |
|
119 | + if (!self::fe_isnonzero($p1->X)) { |
|
120 | + throw new SodiumException('Unexpected zero result'); |
|
121 | + } |
|
122 | + |
|
123 | + # fe_1(one_minus_y); |
|
124 | + # fe_sub(one_minus_y, one_minus_y, A.Y); |
|
125 | + # fe_invert(one_minus_y, one_minus_y); |
|
126 | + $one_minux_y = self::fe_invert( |
|
127 | + self::fe_sub( |
|
128 | + self::fe_1(), |
|
129 | + $A->Y |
|
130 | + ) |
|
131 | + ); |
|
132 | + |
|
133 | + # fe_1(x); |
|
134 | + # fe_add(x, x, A.Y); |
|
135 | + # fe_mul(x, x, one_minus_y); |
|
136 | + $x = self::fe_mul( |
|
137 | + self::fe_add(self::fe_1(), $A->Y), |
|
138 | + $one_minux_y |
|
139 | + ); |
|
140 | + |
|
141 | + # fe_tobytes(curve25519_pk, x); |
|
142 | + return self::fe_tobytes($x); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @internal You should not use this directly from another application |
|
147 | + * |
|
148 | + * @param string $sk |
|
149 | + * @return string |
|
150 | + * @throws SodiumException |
|
151 | + * @throws TypeError |
|
152 | + */ |
|
153 | + public static function sk_to_pk($sk) |
|
154 | + { |
|
155 | + return self::ge_p3_tobytes( |
|
156 | + self::ge_scalarmult_base( |
|
157 | + self::substr($sk, 0, 32) |
|
158 | + ) |
|
159 | + ); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @internal You should not use this directly from another application |
|
164 | + * |
|
165 | + * @param string $message |
|
166 | + * @param string $sk |
|
167 | + * @return string |
|
168 | + * @throws SodiumException |
|
169 | + * @throws TypeError |
|
170 | + */ |
|
171 | + public static function sign($message, $sk) |
|
172 | + { |
|
173 | + /** @var string $signature */ |
|
174 | + $signature = self::sign_detached($message, $sk); |
|
175 | + return $signature . $message; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @internal You should not use this directly from another application |
|
180 | + * |
|
181 | + * @param string $message A signed message |
|
182 | + * @param string $pk Public key |
|
183 | + * @return string Message (without signature) |
|
184 | + * @throws SodiumException |
|
185 | + * @throws TypeError |
|
186 | + */ |
|
187 | + public static function sign_open($message, $pk) |
|
188 | + { |
|
189 | + /** @var string $signature */ |
|
190 | + $signature = self::substr($message, 0, 64); |
|
191 | + |
|
192 | + /** @var string $message */ |
|
193 | + $message = self::substr($message, 64); |
|
194 | + |
|
195 | + if (self::verify_detached($signature, $message, $pk)) { |
|
196 | + return $message; |
|
197 | + } |
|
198 | + throw new SodiumException('Invalid signature'); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @internal You should not use this directly from another application |
|
203 | + * |
|
204 | + * @param string $message |
|
205 | + * @param string $sk |
|
206 | + * @return string |
|
207 | + * @throws SodiumException |
|
208 | + * @throws TypeError |
|
209 | + */ |
|
210 | + public static function sign_detached($message, $sk) |
|
211 | + { |
|
212 | + # crypto_hash_sha512(az, sk, 32); |
|
213 | + $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
214 | + |
|
215 | + # az[0] &= 248; |
|
216 | + # az[31] &= 63; |
|
217 | + # az[31] |= 64; |
|
218 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
219 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
220 | + |
|
221 | + # crypto_hash_sha512_init(&hs); |
|
222 | + # crypto_hash_sha512_update(&hs, az + 32, 32); |
|
223 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
224 | + # crypto_hash_sha512_final(&hs, nonce); |
|
225 | + $hs = hash_init('sha512'); |
|
226 | + hash_update($hs, self::substr($az, 32, 32)); |
|
227 | + hash_update($hs, $message); |
|
228 | + $nonceHash = hash_final($hs, true); |
|
229 | + |
|
230 | + # memmove(sig + 32, sk + 32, 32); |
|
231 | + $pk = self::substr($sk, 32, 32); |
|
232 | + |
|
233 | + # sc_reduce(nonce); |
|
234 | + # ge_scalarmult_base(&R, nonce); |
|
235 | + # ge_p3_tobytes(sig, &R); |
|
236 | + $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
237 | + $sig = self::ge_p3_tobytes( |
|
238 | + self::ge_scalarmult_base($nonce) |
|
239 | + ); |
|
240 | + |
|
241 | + # crypto_hash_sha512_init(&hs); |
|
242 | + # crypto_hash_sha512_update(&hs, sig, 64); |
|
243 | + # crypto_hash_sha512_update(&hs, m, mlen); |
|
244 | + # crypto_hash_sha512_final(&hs, hram); |
|
245 | + $hs = hash_init('sha512'); |
|
246 | + hash_update($hs, self::substr($sig, 0, 32)); |
|
247 | + hash_update($hs, self::substr($pk, 0, 32)); |
|
248 | + hash_update($hs, $message); |
|
249 | + $hramHash = hash_final($hs, true); |
|
250 | + |
|
251 | + # sc_reduce(hram); |
|
252 | + # sc_muladd(sig + 32, hram, az, nonce); |
|
253 | + $hram = self::sc_reduce($hramHash); |
|
254 | + $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
255 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
256 | + |
|
257 | + try { |
|
258 | + ParagonIE_Sodium_Compat::memzero($az); |
|
259 | + } catch (SodiumException $ex) { |
|
260 | + $az = null; |
|
261 | + } |
|
262 | + return $sig; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @internal You should not use this directly from another application |
|
267 | + * |
|
268 | + * @param string $sig |
|
269 | + * @param string $message |
|
270 | + * @param string $pk |
|
271 | + * @return bool |
|
272 | + * @throws SodiumException |
|
273 | + * @throws TypeError |
|
274 | + */ |
|
275 | + public static function verify_detached($sig, $message, $pk) |
|
276 | + { |
|
277 | + if (self::strlen($sig) < 64) { |
|
278 | + throw new SodiumException('Signature is too short'); |
|
279 | + } |
|
280 | + if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
281 | + throw new SodiumException('S < L - Invalid signature'); |
|
282 | + } |
|
283 | + if (self::small_order($sig)) { |
|
284 | + throw new SodiumException('Signature is on too small of an order'); |
|
285 | + } |
|
286 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
287 | + throw new SodiumException('Invalid signature'); |
|
288 | + } |
|
289 | + $d = 0; |
|
290 | + for ($i = 0; $i < 32; ++$i) { |
|
291 | + $d |= self::chrToInt($pk[$i]); |
|
292 | + } |
|
293 | + if ($d === 0) { |
|
294 | + throw new SodiumException('All zero public key'); |
|
295 | + } |
|
296 | + |
|
297 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
298 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
299 | + |
|
300 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
301 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
302 | + |
|
303 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
304 | + $A = self::ge_frombytes_negate_vartime($pk); |
|
305 | + |
|
306 | + /** @var string $hDigest */ |
|
307 | + $hDigest = hash( |
|
308 | + 'sha512', |
|
309 | + self::substr($sig, 0, 32) . |
|
310 | + self::substr($pk, 0, 32) . |
|
311 | + $message, |
|
312 | + true |
|
313 | + ); |
|
314 | + |
|
315 | + /** @var string $h */ |
|
316 | + $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
317 | + |
|
318 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
319 | + $R = self::ge_double_scalarmult_vartime( |
|
320 | + $h, |
|
321 | + $A, |
|
322 | + self::substr($sig, 32) |
|
323 | + ); |
|
324 | + |
|
325 | + /** @var string $rcheck */ |
|
326 | + $rcheck = self::ge_tobytes($R); |
|
327 | + |
|
328 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
329 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
330 | + |
|
331 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @internal You should not use this directly from another application |
|
336 | + * |
|
337 | + * @param string $S |
|
338 | + * @return bool |
|
339 | + * @throws SodiumException |
|
340 | + * @throws TypeError |
|
341 | + */ |
|
342 | + public static function check_S_lt_L($S) |
|
343 | + { |
|
344 | + if (self::strlen($S) < 32) { |
|
345 | + throw new SodiumException('Signature must be 32 bytes'); |
|
346 | + } |
|
347 | + $L = array( |
|
348 | + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
|
349 | + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
|
350 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
351 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
|
352 | + ); |
|
353 | + $c = 0; |
|
354 | + $n = 1; |
|
355 | + $i = 32; |
|
356 | + |
|
357 | + /** @var array<int, int> $L */ |
|
358 | + do { |
|
359 | + --$i; |
|
360 | + $x = self::chrToInt($S[$i]); |
|
361 | + $c |= ( |
|
362 | + (($x - $L[$i]) >> 8) & $n |
|
363 | + ); |
|
364 | + $n &= ( |
|
365 | + (($x ^ $L[$i]) - 1) >> 8 |
|
366 | + ); |
|
367 | + } while ($i !== 0); |
|
368 | + |
|
369 | + return $c === 0; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param string $R |
|
374 | + * @return bool |
|
375 | + * @throws SodiumException |
|
376 | + * @throws TypeError |
|
377 | + */ |
|
378 | + public static function small_order($R) |
|
379 | + { |
|
380 | + /** @var array<int, array<int, int>> $blocklist */ |
|
381 | + $blocklist = array( |
|
382 | + /* 0 (order 4) */ |
|
383 | + array( |
|
384 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
385 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
386 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
387 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
388 | + ), |
|
389 | + /* 1 (order 1) */ |
|
390 | + array( |
|
391 | + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
392 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
393 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
394 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
395 | + ), |
|
396 | + /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
397 | + array( |
|
398 | + 0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
399 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
400 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
401 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05 |
|
402 | + ), |
|
403 | + /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
404 | + array( |
|
405 | + 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
406 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
407 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
408 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a |
|
409 | + ), |
|
410 | + /* p-1 (order 2) */ |
|
411 | + array( |
|
412 | + 0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0, |
|
413 | + 0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0, |
|
414 | + 0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39, |
|
415 | + 0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85 |
|
416 | + ), |
|
417 | + /* p (order 4) */ |
|
418 | + array( |
|
419 | + 0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, |
|
420 | + 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f, |
|
421 | + 0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, |
|
422 | + 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa |
|
423 | + ), |
|
424 | + /* p+1 (order 1) */ |
|
425 | + array( |
|
426 | + 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
427 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
428 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
429 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
430 | + ), |
|
431 | + /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */ |
|
432 | + array( |
|
433 | + 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
434 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
435 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
436 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
437 | + ), |
|
438 | + /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */ |
|
439 | + array( |
|
440 | + 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
441 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
442 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
443 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f |
|
444 | + ), |
|
445 | + /* 2p-1 (order 2) */ |
|
446 | + array( |
|
447 | + 0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
448 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
449 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
450 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
451 | + ), |
|
452 | + /* 2p (order 4) */ |
|
453 | + array( |
|
454 | + 0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
455 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
456 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
457 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
458 | + ), |
|
459 | + /* 2p+1 (order 1) */ |
|
460 | + array( |
|
461 | + 0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
462 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
463 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
|
464 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
|
465 | + ) |
|
466 | + ); |
|
467 | + /** @var int $countBlocklist */ |
|
468 | + $countBlocklist = count($blocklist); |
|
469 | + |
|
470 | + for ($i = 0; $i < $countBlocklist; ++$i) { |
|
471 | + $c = 0; |
|
472 | + for ($j = 0; $j < 32; ++$j) { |
|
473 | + $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j]; |
|
474 | + } |
|
475 | + if ($c === 0) { |
|
476 | + return true; |
|
477 | + } |
|
478 | + } |
|
479 | + return false; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * @param string $s |
|
484 | + * @return string |
|
485 | + * @throws SodiumException |
|
486 | + */ |
|
487 | + public static function scalar_complement($s) |
|
488 | + { |
|
489 | + $t_ = self::L . str_repeat("\x00", 32); |
|
490 | + sodium_increment($t_); |
|
491 | + $s_ = $s . str_repeat("\x00", 32); |
|
492 | + ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
493 | + return self::sc_reduce($t_); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * @return string |
|
498 | + * @throws SodiumException |
|
499 | + */ |
|
500 | + public static function scalar_random() |
|
501 | + { |
|
502 | + do { |
|
503 | + $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES); |
|
504 | + $r[self::SCALAR_BYTES - 1] = self::intToChr( |
|
505 | + self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f |
|
506 | + ); |
|
507 | + } while ( |
|
508 | + !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r) |
|
509 | + ); |
|
510 | + return $r; |
|
511 | + } |
|
512 | + |
|
513 | + /** |
|
514 | + * @param string $s |
|
515 | + * @return string |
|
516 | + * @throws SodiumException |
|
517 | + */ |
|
518 | + public static function scalar_negate($s) |
|
519 | + { |
|
520 | + $t_ = self::L . str_repeat("\x00", 32) ; |
|
521 | + $s_ = $s . str_repeat("\x00", 32) ; |
|
522 | + ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
523 | + return self::sc_reduce($t_); |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * @param string $a |
|
528 | + * @param string $b |
|
529 | + * @return string |
|
530 | + * @throws SodiumException |
|
531 | + */ |
|
532 | + public static function scalar_add($a, $b) |
|
533 | + { |
|
534 | + $a_ = $a . str_repeat("\x00", 32); |
|
535 | + $b_ = $b . str_repeat("\x00", 32); |
|
536 | + ParagonIE_Sodium_Compat::add($a_, $b_); |
|
537 | + return self::sc_reduce($a_); |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * @param string $x |
|
542 | + * @param string $y |
|
543 | + * @return string |
|
544 | + * @throws SodiumException |
|
545 | + */ |
|
546 | + public static function scalar_sub($x, $y) |
|
547 | + { |
|
548 | + $yn = self::scalar_negate($y); |
|
549 | + return self::scalar_add($x, $yn); |
|
550 | + } |
|
551 | 551 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Ed25519', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -23,10 +23,10 @@ discard block |
||
23 | 23 | */ |
24 | 24 | public static function keypair() |
25 | 25 | { |
26 | - $seed = random_bytes(self::SEED_BYTES); |
|
26 | + $seed = random_bytes( self::SEED_BYTES ); |
|
27 | 27 | $pk = ''; |
28 | 28 | $sk = ''; |
29 | - self::seed_keypair($pk, $sk, $seed); |
|
29 | + self::seed_keypair( $pk, $sk, $seed ); |
|
30 | 30 | return $sk . $pk; |
31 | 31 | } |
32 | 32 | |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | * @throws SodiumException |
41 | 41 | * @throws TypeError |
42 | 42 | */ |
43 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
43 | + public static function seed_keypair( &$pk, &$sk, $seed ) |
|
44 | 44 | { |
45 | - if (self::strlen($seed) !== self::SEED_BYTES) { |
|
46 | - throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
|
45 | + if ( self::strlen( $seed ) !== self::SEED_BYTES ) { |
|
46 | + throw new RangeException( 'crypto_sign keypair seed must be 32 bytes long' ); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** @var string $pk */ |
50 | - $pk = self::publickey_from_secretkey($seed); |
|
50 | + $pk = self::publickey_from_secretkey( $seed ); |
|
51 | 51 | $sk = $seed . $pk; |
52 | 52 | return $sk; |
53 | 53 | } |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | * @return string |
60 | 60 | * @throws TypeError |
61 | 61 | */ |
62 | - public static function secretkey($keypair) |
|
62 | + public static function secretkey( $keypair ) |
|
63 | 63 | { |
64 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
65 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
64 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
65 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
66 | 66 | } |
67 | - return self::substr($keypair, 0, 64); |
|
67 | + return self::substr( $keypair, 0, 64 ); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -74,12 +74,12 @@ discard block |
||
74 | 74 | * @return string |
75 | 75 | * @throws TypeError |
76 | 76 | */ |
77 | - public static function publickey($keypair) |
|
77 | + public static function publickey( $keypair ) |
|
78 | 78 | { |
79 | - if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
|
80 | - throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
|
79 | + if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) { |
|
80 | + throw new RangeException( 'crypto_sign keypair must be 96 bytes long' ); |
|
81 | 81 | } |
82 | - return self::substr($keypair, 64, 32); |
|
82 | + return self::substr( $keypair, 64, 32 ); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -90,17 +90,17 @@ discard block |
||
90 | 90 | * @throws SodiumException |
91 | 91 | * @throws TypeError |
92 | 92 | */ |
93 | - public static function publickey_from_secretkey($sk) |
|
93 | + public static function publickey_from_secretkey( $sk ) |
|
94 | 94 | { |
95 | 95 | /** @var string $sk */ |
96 | - $sk = hash('sha512', self::substr($sk, 0, 32), true); |
|
97 | - $sk[0] = self::intToChr( |
|
98 | - self::chrToInt($sk[0]) & 248 |
|
96 | + $sk = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
97 | + $sk[ 0 ] = self::intToChr( |
|
98 | + self::chrToInt( $sk[ 0 ] ) & 248 |
|
99 | 99 | ); |
100 | - $sk[31] = self::intToChr( |
|
101 | - (self::chrToInt($sk[31]) & 63) | 64 |
|
100 | + $sk[ 31 ] = self::intToChr( |
|
101 | + ( self::chrToInt( $sk[ 31 ] ) & 63 ) | 64 |
|
102 | 102 | ); |
103 | - return self::sk_to_pk($sk); |
|
103 | + return self::sk_to_pk( $sk ); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -109,15 +109,15 @@ discard block |
||
109 | 109 | * @throws SodiumException |
110 | 110 | * @throws TypeError |
111 | 111 | */ |
112 | - public static function pk_to_curve25519($pk) |
|
112 | + public static function pk_to_curve25519( $pk ) |
|
113 | 113 | { |
114 | - if (self::small_order($pk)) { |
|
115 | - throw new SodiumException('Public key is on a small order'); |
|
114 | + if ( self::small_order( $pk ) ) { |
|
115 | + throw new SodiumException( 'Public key is on a small order' ); |
|
116 | 116 | } |
117 | - $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32)); |
|
118 | - $p1 = self::ge_mul_l($A); |
|
119 | - if (!self::fe_isnonzero($p1->X)) { |
|
120 | - throw new SodiumException('Unexpected zero result'); |
|
117 | + $A = self::ge_frombytes_negate_vartime( self::substr( $pk, 0, 32 ) ); |
|
118 | + $p1 = self::ge_mul_l( $A ); |
|
119 | + if ( ! self::fe_isnonzero( $p1->X ) ) { |
|
120 | + throw new SodiumException( 'Unexpected zero result' ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | # fe_1(one_minus_y); |
@@ -134,12 +134,12 @@ discard block |
||
134 | 134 | # fe_add(x, x, A.Y); |
135 | 135 | # fe_mul(x, x, one_minus_y); |
136 | 136 | $x = self::fe_mul( |
137 | - self::fe_add(self::fe_1(), $A->Y), |
|
137 | + self::fe_add( self::fe_1(), $A->Y ), |
|
138 | 138 | $one_minux_y |
139 | 139 | ); |
140 | 140 | |
141 | 141 | # fe_tobytes(curve25519_pk, x); |
142 | - return self::fe_tobytes($x); |
|
142 | + return self::fe_tobytes( $x ); |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | /** |
@@ -150,11 +150,11 @@ discard block |
||
150 | 150 | * @throws SodiumException |
151 | 151 | * @throws TypeError |
152 | 152 | */ |
153 | - public static function sk_to_pk($sk) |
|
153 | + public static function sk_to_pk( $sk ) |
|
154 | 154 | { |
155 | 155 | return self::ge_p3_tobytes( |
156 | 156 | self::ge_scalarmult_base( |
157 | - self::substr($sk, 0, 32) |
|
157 | + self::substr( $sk, 0, 32 ) |
|
158 | 158 | ) |
159 | 159 | ); |
160 | 160 | } |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | * @throws SodiumException |
169 | 169 | * @throws TypeError |
170 | 170 | */ |
171 | - public static function sign($message, $sk) |
|
171 | + public static function sign( $message, $sk ) |
|
172 | 172 | { |
173 | 173 | /** @var string $signature */ |
174 | - $signature = self::sign_detached($message, $sk); |
|
174 | + $signature = self::sign_detached( $message, $sk ); |
|
175 | 175 | return $signature . $message; |
176 | 176 | } |
177 | 177 | |
@@ -184,18 +184,18 @@ discard block |
||
184 | 184 | * @throws SodiumException |
185 | 185 | * @throws TypeError |
186 | 186 | */ |
187 | - public static function sign_open($message, $pk) |
|
187 | + public static function sign_open( $message, $pk ) |
|
188 | 188 | { |
189 | 189 | /** @var string $signature */ |
190 | - $signature = self::substr($message, 0, 64); |
|
190 | + $signature = self::substr( $message, 0, 64 ); |
|
191 | 191 | |
192 | 192 | /** @var string $message */ |
193 | - $message = self::substr($message, 64); |
|
193 | + $message = self::substr( $message, 64 ); |
|
194 | 194 | |
195 | - if (self::verify_detached($signature, $message, $pk)) { |
|
195 | + if ( self::verify_detached( $signature, $message, $pk ) ) { |
|
196 | 196 | return $message; |
197 | 197 | } |
198 | - throw new SodiumException('Invalid signature'); |
|
198 | + throw new SodiumException( 'Invalid signature' ); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -207,56 +207,56 @@ discard block |
||
207 | 207 | * @throws SodiumException |
208 | 208 | * @throws TypeError |
209 | 209 | */ |
210 | - public static function sign_detached($message, $sk) |
|
210 | + public static function sign_detached( $message, $sk ) |
|
211 | 211 | { |
212 | 212 | # crypto_hash_sha512(az, sk, 32); |
213 | - $az = hash('sha512', self::substr($sk, 0, 32), true); |
|
213 | + $az = hash( 'sha512', self::substr( $sk, 0, 32 ), true ); |
|
214 | 214 | |
215 | 215 | # az[0] &= 248; |
216 | 216 | # az[31] &= 63; |
217 | 217 | # az[31] |= 64; |
218 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
219 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
218 | + $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 ); |
|
219 | + $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 ); |
|
220 | 220 | |
221 | 221 | # crypto_hash_sha512_init(&hs); |
222 | 222 | # crypto_hash_sha512_update(&hs, az + 32, 32); |
223 | 223 | # crypto_hash_sha512_update(&hs, m, mlen); |
224 | 224 | # crypto_hash_sha512_final(&hs, nonce); |
225 | - $hs = hash_init('sha512'); |
|
226 | - hash_update($hs, self::substr($az, 32, 32)); |
|
227 | - hash_update($hs, $message); |
|
228 | - $nonceHash = hash_final($hs, true); |
|
225 | + $hs = hash_init( 'sha512' ); |
|
226 | + hash_update( $hs, self::substr( $az, 32, 32 ) ); |
|
227 | + hash_update( $hs, $message ); |
|
228 | + $nonceHash = hash_final( $hs, true ); |
|
229 | 229 | |
230 | 230 | # memmove(sig + 32, sk + 32, 32); |
231 | - $pk = self::substr($sk, 32, 32); |
|
231 | + $pk = self::substr( $sk, 32, 32 ); |
|
232 | 232 | |
233 | 233 | # sc_reduce(nonce); |
234 | 234 | # ge_scalarmult_base(&R, nonce); |
235 | 235 | # ge_p3_tobytes(sig, &R); |
236 | - $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
236 | + $nonce = self::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
237 | 237 | $sig = self::ge_p3_tobytes( |
238 | - self::ge_scalarmult_base($nonce) |
|
238 | + self::ge_scalarmult_base( $nonce ) |
|
239 | 239 | ); |
240 | 240 | |
241 | 241 | # crypto_hash_sha512_init(&hs); |
242 | 242 | # crypto_hash_sha512_update(&hs, sig, 64); |
243 | 243 | # crypto_hash_sha512_update(&hs, m, mlen); |
244 | 244 | # crypto_hash_sha512_final(&hs, hram); |
245 | - $hs = hash_init('sha512'); |
|
246 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
247 | - hash_update($hs, self::substr($pk, 0, 32)); |
|
248 | - hash_update($hs, $message); |
|
249 | - $hramHash = hash_final($hs, true); |
|
245 | + $hs = hash_init( 'sha512' ); |
|
246 | + hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
247 | + hash_update( $hs, self::substr( $pk, 0, 32 ) ); |
|
248 | + hash_update( $hs, $message ); |
|
249 | + $hramHash = hash_final( $hs, true ); |
|
250 | 250 | |
251 | 251 | # sc_reduce(hram); |
252 | 252 | # sc_muladd(sig + 32, hram, az, nonce); |
253 | - $hram = self::sc_reduce($hramHash); |
|
254 | - $sigAfter = self::sc_muladd($hram, $az, $nonce); |
|
255 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
253 | + $hram = self::sc_reduce( $hramHash ); |
|
254 | + $sigAfter = self::sc_muladd( $hram, $az, $nonce ); |
|
255 | + $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 ); |
|
256 | 256 | |
257 | 257 | try { |
258 | - ParagonIE_Sodium_Compat::memzero($az); |
|
259 | - } catch (SodiumException $ex) { |
|
258 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
259 | + } catch ( SodiumException $ex ) { |
|
260 | 260 | $az = null; |
261 | 261 | } |
262 | 262 | return $sig; |
@@ -272,26 +272,26 @@ discard block |
||
272 | 272 | * @throws SodiumException |
273 | 273 | * @throws TypeError |
274 | 274 | */ |
275 | - public static function verify_detached($sig, $message, $pk) |
|
275 | + public static function verify_detached( $sig, $message, $pk ) |
|
276 | 276 | { |
277 | - if (self::strlen($sig) < 64) { |
|
278 | - throw new SodiumException('Signature is too short'); |
|
277 | + if ( self::strlen( $sig ) < 64 ) { |
|
278 | + throw new SodiumException( 'Signature is too short' ); |
|
279 | 279 | } |
280 | - if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
281 | - throw new SodiumException('S < L - Invalid signature'); |
|
280 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 240 ) && self::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) { |
|
281 | + throw new SodiumException( 'S < L - Invalid signature' ); |
|
282 | 282 | } |
283 | - if (self::small_order($sig)) { |
|
284 | - throw new SodiumException('Signature is on too small of an order'); |
|
283 | + if ( self::small_order( $sig ) ) { |
|
284 | + throw new SodiumException( 'Signature is on too small of an order' ); |
|
285 | 285 | } |
286 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
287 | - throw new SodiumException('Invalid signature'); |
|
286 | + if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) { |
|
287 | + throw new SodiumException( 'Invalid signature' ); |
|
288 | 288 | } |
289 | 289 | $d = 0; |
290 | - for ($i = 0; $i < 32; ++$i) { |
|
291 | - $d |= self::chrToInt($pk[$i]); |
|
290 | + for ( $i = 0; $i < 32; ++$i ) { |
|
291 | + $d |= self::chrToInt( $pk[ $i ] ); |
|
292 | 292 | } |
293 | - if ($d === 0) { |
|
294 | - throw new SodiumException('All zero public key'); |
|
293 | + if ( $d === 0 ) { |
|
294 | + throw new SodiumException( 'All zero public key' ); |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
@@ -301,34 +301,34 @@ discard block |
||
301 | 301 | ParagonIE_Sodium_Compat::$fastMult = true; |
302 | 302 | |
303 | 303 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
304 | - $A = self::ge_frombytes_negate_vartime($pk); |
|
304 | + $A = self::ge_frombytes_negate_vartime( $pk ); |
|
305 | 305 | |
306 | 306 | /** @var string $hDigest */ |
307 | 307 | $hDigest = hash( |
308 | 308 | 'sha512', |
309 | - self::substr($sig, 0, 32) . |
|
310 | - self::substr($pk, 0, 32) . |
|
309 | + self::substr( $sig, 0, 32 ) . |
|
310 | + self::substr( $pk, 0, 32 ) . |
|
311 | 311 | $message, |
312 | 312 | true |
313 | 313 | ); |
314 | 314 | |
315 | 315 | /** @var string $h */ |
316 | - $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
316 | + $h = self::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
317 | 317 | |
318 | 318 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
319 | 319 | $R = self::ge_double_scalarmult_vartime( |
320 | 320 | $h, |
321 | 321 | $A, |
322 | - self::substr($sig, 32) |
|
322 | + self::substr( $sig, 32 ) |
|
323 | 323 | ); |
324 | 324 | |
325 | 325 | /** @var string $rcheck */ |
326 | - $rcheck = self::ge_tobytes($R); |
|
326 | + $rcheck = self::ge_tobytes( $R ); |
|
327 | 327 | |
328 | 328 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
329 | 329 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
330 | 330 | |
331 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
331 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
332 | 332 | } |
333 | 333 | |
334 | 334 | /** |
@@ -339,10 +339,10 @@ discard block |
||
339 | 339 | * @throws SodiumException |
340 | 340 | * @throws TypeError |
341 | 341 | */ |
342 | - public static function check_S_lt_L($S) |
|
342 | + public static function check_S_lt_L( $S ) |
|
343 | 343 | { |
344 | - if (self::strlen($S) < 32) { |
|
345 | - throw new SodiumException('Signature must be 32 bytes'); |
|
344 | + if ( self::strlen( $S ) < 32 ) { |
|
345 | + throw new SodiumException( 'Signature must be 32 bytes' ); |
|
346 | 346 | } |
347 | 347 | $L = array( |
348 | 348 | 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
@@ -357,14 +357,14 @@ discard block |
||
357 | 357 | /** @var array<int, int> $L */ |
358 | 358 | do { |
359 | 359 | --$i; |
360 | - $x = self::chrToInt($S[$i]); |
|
360 | + $x = self::chrToInt( $S[ $i ] ); |
|
361 | 361 | $c |= ( |
362 | - (($x - $L[$i]) >> 8) & $n |
|
362 | + ( ( $x - $L[ $i ] ) >> 8 ) & $n |
|
363 | 363 | ); |
364 | 364 | $n &= ( |
365 | - (($x ^ $L[$i]) - 1) >> 8 |
|
365 | + ( ( $x ^ $L[ $i ] ) - 1 ) >> 8 |
|
366 | 366 | ); |
367 | - } while ($i !== 0); |
|
367 | + } while ( $i !== 0 ); |
|
368 | 368 | |
369 | 369 | return $c === 0; |
370 | 370 | } |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | * @throws SodiumException |
376 | 376 | * @throws TypeError |
377 | 377 | */ |
378 | - public static function small_order($R) |
|
378 | + public static function small_order( $R ) |
|
379 | 379 | { |
380 | 380 | /** @var array<int, array<int, int>> $blocklist */ |
381 | 381 | $blocklist = array( |
@@ -465,14 +465,14 @@ discard block |
||
465 | 465 | ) |
466 | 466 | ); |
467 | 467 | /** @var int $countBlocklist */ |
468 | - $countBlocklist = count($blocklist); |
|
468 | + $countBlocklist = count( $blocklist ); |
|
469 | 469 | |
470 | - for ($i = 0; $i < $countBlocklist; ++$i) { |
|
470 | + for ( $i = 0; $i < $countBlocklist; ++$i ) { |
|
471 | 471 | $c = 0; |
472 | - for ($j = 0; $j < 32; ++$j) { |
|
473 | - $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j]; |
|
472 | + for ( $j = 0; $j < 32; ++$j ) { |
|
473 | + $c |= self::chrToInt( $R[ $j ] ) ^ (int)$blocklist[ $i ][ $j ]; |
|
474 | 474 | } |
475 | - if ($c === 0) { |
|
475 | + if ( $c === 0 ) { |
|
476 | 476 | return true; |
477 | 477 | } |
478 | 478 | } |
@@ -484,13 +484,13 @@ discard block |
||
484 | 484 | * @return string |
485 | 485 | * @throws SodiumException |
486 | 486 | */ |
487 | - public static function scalar_complement($s) |
|
487 | + public static function scalar_complement( $s ) |
|
488 | 488 | { |
489 | - $t_ = self::L . str_repeat("\x00", 32); |
|
490 | - sodium_increment($t_); |
|
491 | - $s_ = $s . str_repeat("\x00", 32); |
|
492 | - ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
493 | - return self::sc_reduce($t_); |
|
489 | + $t_ = self::L . str_repeat( "\x00", 32 ); |
|
490 | + sodium_increment( $t_ ); |
|
491 | + $s_ = $s . str_repeat( "\x00", 32 ); |
|
492 | + ParagonIE_Sodium_Compat::sub( $t_, $s_ ); |
|
493 | + return self::sc_reduce( $t_ ); |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | /** |
@@ -500,12 +500,12 @@ discard block |
||
500 | 500 | public static function scalar_random() |
501 | 501 | { |
502 | 502 | do { |
503 | - $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES); |
|
504 | - $r[self::SCALAR_BYTES - 1] = self::intToChr( |
|
505 | - self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f |
|
503 | + $r = ParagonIE_Sodium_Compat::randombytes_buf( self::SCALAR_BYTES ); |
|
504 | + $r[ self::SCALAR_BYTES - 1 ] = self::intToChr( |
|
505 | + self::chrToInt( $r[ self::SCALAR_BYTES - 1 ] ) & 0x1f |
|
506 | 506 | ); |
507 | 507 | } while ( |
508 | - !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r) |
|
508 | + ! self::check_S_lt_L( $r ) || ParagonIE_Sodium_Compat::is_zero( $r ) |
|
509 | 509 | ); |
510 | 510 | return $r; |
511 | 511 | } |
@@ -515,12 +515,12 @@ discard block |
||
515 | 515 | * @return string |
516 | 516 | * @throws SodiumException |
517 | 517 | */ |
518 | - public static function scalar_negate($s) |
|
518 | + public static function scalar_negate( $s ) |
|
519 | 519 | { |
520 | - $t_ = self::L . str_repeat("\x00", 32) ; |
|
521 | - $s_ = $s . str_repeat("\x00", 32) ; |
|
522 | - ParagonIE_Sodium_Compat::sub($t_, $s_); |
|
523 | - return self::sc_reduce($t_); |
|
520 | + $t_ = self::L . str_repeat( "\x00", 32 ); |
|
521 | + $s_ = $s . str_repeat( "\x00", 32 ); |
|
522 | + ParagonIE_Sodium_Compat::sub( $t_, $s_ ); |
|
523 | + return self::sc_reduce( $t_ ); |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | /** |
@@ -529,12 +529,12 @@ discard block |
||
529 | 529 | * @return string |
530 | 530 | * @throws SodiumException |
531 | 531 | */ |
532 | - public static function scalar_add($a, $b) |
|
532 | + public static function scalar_add( $a, $b ) |
|
533 | 533 | { |
534 | - $a_ = $a . str_repeat("\x00", 32); |
|
535 | - $b_ = $b . str_repeat("\x00", 32); |
|
536 | - ParagonIE_Sodium_Compat::add($a_, $b_); |
|
537 | - return self::sc_reduce($a_); |
|
534 | + $a_ = $a . str_repeat( "\x00", 32 ); |
|
535 | + $b_ = $b . str_repeat( "\x00", 32 ); |
|
536 | + ParagonIE_Sodium_Compat::add( $a_, $b_ ); |
|
537 | + return self::sc_reduce( $a_ ); |
|
538 | 538 | } |
539 | 539 | |
540 | 540 | /** |
@@ -543,9 +543,9 @@ discard block |
||
543 | 543 | * @return string |
544 | 544 | * @throws SodiumException |
545 | 545 | */ |
546 | - public static function scalar_sub($x, $y) |
|
546 | + public static function scalar_sub( $x, $y ) |
|
547 | 547 | { |
548 | - $yn = self::scalar_negate($y); |
|
549 | - return self::scalar_add($x, $yn); |
|
548 | + $yn = self::scalar_negate( $y ); |
|
549 | + return self::scalar_add( $x, $yn ); |
|
550 | 550 | } |
551 | 551 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_Ed25519 |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 { |
|
12 | 11 | const KEYPAIR_BYTES = 96; |
13 | 12 | const SEED_BYTES = 32; |
14 | 13 | const SCALAR_BYTES = 32; |
@@ -21,8 +20,7 @@ discard block |
||
21 | 20 | * @throws SodiumException |
22 | 21 | * @throws TypeError |
23 | 22 | */ |
24 | - public static function keypair() |
|
25 | - { |
|
23 | + public static function keypair() { |
|
26 | 24 | $seed = random_bytes(self::SEED_BYTES); |
27 | 25 | $pk = ''; |
28 | 26 | $sk = ''; |
@@ -40,8 +38,7 @@ discard block |
||
40 | 38 | * @throws SodiumException |
41 | 39 | * @throws TypeError |
42 | 40 | */ |
43 | - public static function seed_keypair(&$pk, &$sk, $seed) |
|
44 | - { |
|
41 | + public static function seed_keypair(&$pk, &$sk, $seed) { |
|
45 | 42 | if (self::strlen($seed) !== self::SEED_BYTES) { |
46 | 43 | throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); |
47 | 44 | } |
@@ -59,8 +56,7 @@ discard block |
||
59 | 56 | * @return string |
60 | 57 | * @throws TypeError |
61 | 58 | */ |
62 | - public static function secretkey($keypair) |
|
63 | - { |
|
59 | + public static function secretkey($keypair) { |
|
64 | 60 | if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
65 | 61 | throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
66 | 62 | } |
@@ -74,8 +70,7 @@ discard block |
||
74 | 70 | * @return string |
75 | 71 | * @throws TypeError |
76 | 72 | */ |
77 | - public static function publickey($keypair) |
|
78 | - { |
|
73 | + public static function publickey($keypair) { |
|
79 | 74 | if (self::strlen($keypair) !== self::KEYPAIR_BYTES) { |
80 | 75 | throw new RangeException('crypto_sign keypair must be 96 bytes long'); |
81 | 76 | } |
@@ -90,8 +85,7 @@ discard block |
||
90 | 85 | * @throws SodiumException |
91 | 86 | * @throws TypeError |
92 | 87 | */ |
93 | - public static function publickey_from_secretkey($sk) |
|
94 | - { |
|
88 | + public static function publickey_from_secretkey($sk) { |
|
95 | 89 | /** @var string $sk */ |
96 | 90 | $sk = hash('sha512', self::substr($sk, 0, 32), true); |
97 | 91 | $sk[0] = self::intToChr( |
@@ -109,8 +103,7 @@ discard block |
||
109 | 103 | * @throws SodiumException |
110 | 104 | * @throws TypeError |
111 | 105 | */ |
112 | - public static function pk_to_curve25519($pk) |
|
113 | - { |
|
106 | + public static function pk_to_curve25519($pk) { |
|
114 | 107 | if (self::small_order($pk)) { |
115 | 108 | throw new SodiumException('Public key is on a small order'); |
116 | 109 | } |
@@ -150,8 +143,7 @@ discard block |
||
150 | 143 | * @throws SodiumException |
151 | 144 | * @throws TypeError |
152 | 145 | */ |
153 | - public static function sk_to_pk($sk) |
|
154 | - { |
|
146 | + public static function sk_to_pk($sk) { |
|
155 | 147 | return self::ge_p3_tobytes( |
156 | 148 | self::ge_scalarmult_base( |
157 | 149 | self::substr($sk, 0, 32) |
@@ -168,8 +160,7 @@ discard block |
||
168 | 160 | * @throws SodiumException |
169 | 161 | * @throws TypeError |
170 | 162 | */ |
171 | - public static function sign($message, $sk) |
|
172 | - { |
|
163 | + public static function sign($message, $sk) { |
|
173 | 164 | /** @var string $signature */ |
174 | 165 | $signature = self::sign_detached($message, $sk); |
175 | 166 | return $signature . $message; |
@@ -184,8 +175,7 @@ discard block |
||
184 | 175 | * @throws SodiumException |
185 | 176 | * @throws TypeError |
186 | 177 | */ |
187 | - public static function sign_open($message, $pk) |
|
188 | - { |
|
178 | + public static function sign_open($message, $pk) { |
|
189 | 179 | /** @var string $signature */ |
190 | 180 | $signature = self::substr($message, 0, 64); |
191 | 181 | |
@@ -207,8 +197,7 @@ discard block |
||
207 | 197 | * @throws SodiumException |
208 | 198 | * @throws TypeError |
209 | 199 | */ |
210 | - public static function sign_detached($message, $sk) |
|
211 | - { |
|
200 | + public static function sign_detached($message, $sk) { |
|
212 | 201 | # crypto_hash_sha512(az, sk, 32); |
213 | 202 | $az = hash('sha512', self::substr($sk, 0, 32), true); |
214 | 203 | |
@@ -272,8 +261,7 @@ discard block |
||
272 | 261 | * @throws SodiumException |
273 | 262 | * @throws TypeError |
274 | 263 | */ |
275 | - public static function verify_detached($sig, $message, $pk) |
|
276 | - { |
|
264 | + public static function verify_detached($sig, $message, $pk) { |
|
277 | 265 | if (self::strlen($sig) < 64) { |
278 | 266 | throw new SodiumException('Signature is too short'); |
279 | 267 | } |
@@ -339,8 +327,7 @@ discard block |
||
339 | 327 | * @throws SodiumException |
340 | 328 | * @throws TypeError |
341 | 329 | */ |
342 | - public static function check_S_lt_L($S) |
|
343 | - { |
|
330 | + public static function check_S_lt_L($S) { |
|
344 | 331 | if (self::strlen($S) < 32) { |
345 | 332 | throw new SodiumException('Signature must be 32 bytes'); |
346 | 333 | } |
@@ -375,8 +362,7 @@ discard block |
||
375 | 362 | * @throws SodiumException |
376 | 363 | * @throws TypeError |
377 | 364 | */ |
378 | - public static function small_order($R) |
|
379 | - { |
|
365 | + public static function small_order($R) { |
|
380 | 366 | /** @var array<int, array<int, int>> $blocklist */ |
381 | 367 | $blocklist = array( |
382 | 368 | /* 0 (order 4) */ |
@@ -484,8 +470,7 @@ discard block |
||
484 | 470 | * @return string |
485 | 471 | * @throws SodiumException |
486 | 472 | */ |
487 | - public static function scalar_complement($s) |
|
488 | - { |
|
473 | + public static function scalar_complement($s) { |
|
489 | 474 | $t_ = self::L . str_repeat("\x00", 32); |
490 | 475 | sodium_increment($t_); |
491 | 476 | $s_ = $s . str_repeat("\x00", 32); |
@@ -497,8 +482,7 @@ discard block |
||
497 | 482 | * @return string |
498 | 483 | * @throws SodiumException |
499 | 484 | */ |
500 | - public static function scalar_random() |
|
501 | - { |
|
485 | + public static function scalar_random() { |
|
502 | 486 | do { |
503 | 487 | $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES); |
504 | 488 | $r[self::SCALAR_BYTES - 1] = self::intToChr( |
@@ -515,8 +499,7 @@ discard block |
||
515 | 499 | * @return string |
516 | 500 | * @throws SodiumException |
517 | 501 | */ |
518 | - public static function scalar_negate($s) |
|
519 | - { |
|
502 | + public static function scalar_negate($s) { |
|
520 | 503 | $t_ = self::L . str_repeat("\x00", 32) ; |
521 | 504 | $s_ = $s . str_repeat("\x00", 32) ; |
522 | 505 | ParagonIE_Sodium_Compat::sub($t_, $s_); |
@@ -529,8 +512,7 @@ discard block |
||
529 | 512 | * @return string |
530 | 513 | * @throws SodiumException |
531 | 514 | */ |
532 | - public static function scalar_add($a, $b) |
|
533 | - { |
|
515 | + public static function scalar_add($a, $b) { |
|
534 | 516 | $a_ = $a . str_repeat("\x00", 32); |
535 | 517 | $b_ = $b . str_repeat("\x00", 32); |
536 | 518 | ParagonIE_Sodium_Compat::add($a_, $b_); |
@@ -543,8 +525,7 @@ discard block |
||
543 | 525 | * @return string |
544 | 526 | * @throws SodiumException |
545 | 527 | */ |
546 | - public static function scalar_sub($x, $y) |
|
547 | - { |
|
528 | + public static function scalar_sub($x, $y) { |
|
548 | 529 | $yn = self::scalar_negate($y); |
549 | 530 | return self::scalar_add($x, $yn); |
550 | 531 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_Util', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,939 +9,939 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * @param int $integer |
|
14 | - * @param int $size (16, 32, 64) |
|
15 | - * @return int |
|
16 | - */ |
|
17 | - public static function abs($integer, $size = 0) |
|
18 | - { |
|
19 | - /** @var int $realSize */ |
|
20 | - $realSize = (PHP_INT_SIZE << 3) - 1; |
|
21 | - if ($size) { |
|
22 | - --$size; |
|
23 | - } else { |
|
24 | - /** @var int $size */ |
|
25 | - $size = $realSize; |
|
26 | - } |
|
27 | - |
|
28 | - $negative = -(($integer >> $size) & 1); |
|
29 | - return (int) ( |
|
30 | - ($integer ^ $negative) |
|
31 | - + |
|
32 | - (($negative >> $realSize) & 1) |
|
33 | - ); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Convert a binary string into a hexadecimal string without cache-timing |
|
38 | - * leaks |
|
39 | - * |
|
40 | - * @internal You should not use this directly from another application |
|
41 | - * |
|
42 | - * @param string $binaryString (raw binary) |
|
43 | - * @return string |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - public static function bin2hex($binaryString) |
|
47 | - { |
|
48 | - /* Type checks: */ |
|
49 | - if (!is_string($binaryString)) { |
|
50 | - throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.'); |
|
51 | - } |
|
52 | - |
|
53 | - $hex = ''; |
|
54 | - $len = self::strlen($binaryString); |
|
55 | - for ($i = 0; $i < $len; ++$i) { |
|
56 | - /** @var array<int, int> $chunk */ |
|
57 | - $chunk = unpack('C', $binaryString[$i]); |
|
58 | - /** @var int $c */ |
|
59 | - $c = $chunk[1] & 0xf; |
|
60 | - /** @var int $b */ |
|
61 | - $b = $chunk[1] >> 4; |
|
62 | - $hex .= pack( |
|
63 | - 'CC', |
|
64 | - (87 + $b + ((($b - 10) >> 8) & ~38)), |
|
65 | - (87 + $c + ((($c - 10) >> 8) & ~38)) |
|
66 | - ); |
|
67 | - } |
|
68 | - return $hex; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Convert a binary string into a hexadecimal string without cache-timing |
|
73 | - * leaks, returning uppercase letters (as per RFC 4648) |
|
74 | - * |
|
75 | - * @internal You should not use this directly from another application |
|
76 | - * |
|
77 | - * @param string $bin_string (raw binary) |
|
78 | - * @return string |
|
79 | - * @throws TypeError |
|
80 | - */ |
|
81 | - public static function bin2hexUpper($bin_string) |
|
82 | - { |
|
83 | - $hex = ''; |
|
84 | - $len = self::strlen($bin_string); |
|
85 | - for ($i = 0; $i < $len; ++$i) { |
|
86 | - /** @var array<int, int> $chunk */ |
|
87 | - $chunk = unpack('C', $bin_string[$i]); |
|
88 | - /** |
|
89 | - * Lower 16 bits |
|
90 | - * |
|
91 | - * @var int $c |
|
92 | - */ |
|
93 | - $c = $chunk[1] & 0xf; |
|
94 | - |
|
95 | - /** |
|
96 | - * Upper 16 bits |
|
97 | - * @var int $b |
|
98 | - */ |
|
99 | - $b = $chunk[1] >> 4; |
|
100 | - |
|
101 | - /** |
|
102 | - * Use pack() and binary operators to turn the two integers |
|
103 | - * into hexadecimal characters. We don't use chr() here, because |
|
104 | - * it uses a lookup table internally and we want to avoid |
|
105 | - * cache-timing side-channels. |
|
106 | - */ |
|
107 | - $hex .= pack( |
|
108 | - 'CC', |
|
109 | - (55 + $b + ((($b - 10) >> 8) & ~6)), |
|
110 | - (55 + $c + ((($c - 10) >> 8) & ~6)) |
|
111 | - ); |
|
112 | - } |
|
113 | - return $hex; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Cache-timing-safe variant of ord() |
|
118 | - * |
|
119 | - * @internal You should not use this directly from another application |
|
120 | - * |
|
121 | - * @param string $chr |
|
122 | - * @return int |
|
123 | - * @throws SodiumException |
|
124 | - * @throws TypeError |
|
125 | - */ |
|
126 | - public static function chrToInt($chr) |
|
127 | - { |
|
128 | - /* Type checks: */ |
|
129 | - if (!is_string($chr)) { |
|
130 | - throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.'); |
|
131 | - } |
|
132 | - if (self::strlen($chr) !== 1) { |
|
133 | - throw new SodiumException('chrToInt() expects a string that is exactly 1 character long'); |
|
134 | - } |
|
135 | - /** @var array<int, int> $chunk */ |
|
136 | - $chunk = unpack('C', $chr); |
|
137 | - return (int) ($chunk[1]); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Compares two strings. |
|
142 | - * |
|
143 | - * @internal You should not use this directly from another application |
|
144 | - * |
|
145 | - * @param string $left |
|
146 | - * @param string $right |
|
147 | - * @param int $len |
|
148 | - * @return int |
|
149 | - * @throws SodiumException |
|
150 | - * @throws TypeError |
|
151 | - */ |
|
152 | - public static function compare($left, $right, $len = null) |
|
153 | - { |
|
154 | - $leftLen = self::strlen($left); |
|
155 | - $rightLen = self::strlen($right); |
|
156 | - if ($len === null) { |
|
157 | - $len = max($leftLen, $rightLen); |
|
158 | - $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT); |
|
159 | - $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT); |
|
160 | - } |
|
161 | - |
|
162 | - $gt = 0; |
|
163 | - $eq = 1; |
|
164 | - $i = $len; |
|
165 | - while ($i !== 0) { |
|
166 | - --$i; |
|
167 | - $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq; |
|
168 | - $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8; |
|
169 | - } |
|
170 | - return ($gt + $gt + $eq) - 1; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * If a variable does not match a given type, throw a TypeError. |
|
175 | - * |
|
176 | - * @param mixed $mixedVar |
|
177 | - * @param string $type |
|
178 | - * @param int $argumentIndex |
|
179 | - * @throws TypeError |
|
180 | - * @throws SodiumException |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) |
|
184 | - { |
|
185 | - if (func_num_args() === 0) { |
|
186 | - /* Tautology, by default */ |
|
187 | - return; |
|
188 | - } |
|
189 | - if (func_num_args() === 1) { |
|
190 | - throw new TypeError('Declared void, but passed a variable'); |
|
191 | - } |
|
192 | - $realType = strtolower(gettype($mixedVar)); |
|
193 | - $type = strtolower($type); |
|
194 | - switch ($type) { |
|
195 | - case 'null': |
|
196 | - if ($mixedVar !== null) { |
|
197 | - throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.'); |
|
198 | - } |
|
199 | - break; |
|
200 | - case 'integer': |
|
201 | - case 'int': |
|
202 | - $allow = array('int', 'integer'); |
|
203 | - if (!in_array($type, $allow)) { |
|
204 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.'); |
|
205 | - } |
|
206 | - $mixedVar = (int) $mixedVar; |
|
207 | - break; |
|
208 | - case 'boolean': |
|
209 | - case 'bool': |
|
210 | - $allow = array('bool', 'boolean'); |
|
211 | - if (!in_array($type, $allow)) { |
|
212 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.'); |
|
213 | - } |
|
214 | - $mixedVar = (bool) $mixedVar; |
|
215 | - break; |
|
216 | - case 'string': |
|
217 | - if (!is_string($mixedVar)) { |
|
218 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.'); |
|
219 | - } |
|
220 | - $mixedVar = (string) $mixedVar; |
|
221 | - break; |
|
222 | - case 'decimal': |
|
223 | - case 'double': |
|
224 | - case 'float': |
|
225 | - $allow = array('decimal', 'double', 'float'); |
|
226 | - if (!in_array($type, $allow)) { |
|
227 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.'); |
|
228 | - } |
|
229 | - $mixedVar = (float) $mixedVar; |
|
230 | - break; |
|
231 | - case 'object': |
|
232 | - if (!is_object($mixedVar)) { |
|
233 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.'); |
|
234 | - } |
|
235 | - break; |
|
236 | - case 'array': |
|
237 | - if (!is_array($mixedVar)) { |
|
238 | - if (is_object($mixedVar)) { |
|
239 | - if ($mixedVar instanceof ArrayAccess) { |
|
240 | - return; |
|
241 | - } |
|
242 | - } |
|
243 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.'); |
|
244 | - } |
|
245 | - break; |
|
246 | - default: |
|
247 | - throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')'); |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Evaluate whether or not two strings are equal (in constant-time) |
|
253 | - * |
|
254 | - * @param string $left |
|
255 | - * @param string $right |
|
256 | - * @return bool |
|
257 | - * @throws SodiumException |
|
258 | - * @throws TypeError |
|
259 | - */ |
|
260 | - public static function hashEquals($left, $right) |
|
261 | - { |
|
262 | - /* Type checks: */ |
|
263 | - if (!is_string($left)) { |
|
264 | - throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.'); |
|
265 | - } |
|
266 | - if (!is_string($right)) { |
|
267 | - throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.'); |
|
268 | - } |
|
269 | - |
|
270 | - if (is_callable('hash_equals')) { |
|
271 | - return hash_equals($left, $right); |
|
272 | - } |
|
273 | - $d = 0; |
|
274 | - /** @var int $len */ |
|
275 | - $len = self::strlen($left); |
|
276 | - if ($len !== self::strlen($right)) { |
|
277 | - return false; |
|
278 | - } |
|
279 | - for ($i = 0; $i < $len; ++$i) { |
|
280 | - $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]); |
|
281 | - } |
|
282 | - |
|
283 | - if ($d !== 0) { |
|
284 | - return false; |
|
285 | - } |
|
286 | - return $left === $right; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Catch hash_update() failures and throw instead of silently proceeding |
|
291 | - * |
|
292 | - * @param HashContext|resource &$hs |
|
293 | - * @param string $data |
|
294 | - * @return void |
|
295 | - * @throws SodiumException |
|
296 | - * @psalm-suppress PossiblyInvalidArgument |
|
297 | - */ |
|
298 | - protected static function hash_update(&$hs, $data) |
|
299 | - { |
|
300 | - if (!hash_update($hs, $data)) { |
|
301 | - throw new SodiumException('hash_update() failed'); |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Convert a hexadecimal string into a binary string without cache-timing |
|
307 | - * leaks |
|
308 | - * |
|
309 | - * @internal You should not use this directly from another application |
|
310 | - * |
|
311 | - * @param string $hexString |
|
312 | - * @param bool $strictPadding |
|
313 | - * @return string (raw binary) |
|
314 | - * @throws RangeException |
|
315 | - * @throws TypeError |
|
316 | - */ |
|
317 | - public static function hex2bin($hexString, $strictPadding = false) |
|
318 | - { |
|
319 | - /* Type checks: */ |
|
320 | - if (!is_string($hexString)) { |
|
321 | - throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.'); |
|
322 | - } |
|
323 | - |
|
324 | - /** @var int $hex_pos */ |
|
325 | - $hex_pos = 0; |
|
326 | - /** @var string $bin */ |
|
327 | - $bin = ''; |
|
328 | - /** @var int $c_acc */ |
|
329 | - $c_acc = 0; |
|
330 | - /** @var int $hex_len */ |
|
331 | - $hex_len = self::strlen($hexString); |
|
332 | - /** @var int $state */ |
|
333 | - $state = 0; |
|
334 | - if (($hex_len & 1) !== 0) { |
|
335 | - if ($strictPadding) { |
|
336 | - throw new RangeException( |
|
337 | - 'Expected an even number of hexadecimal characters' |
|
338 | - ); |
|
339 | - } else { |
|
340 | - $hexString = '0' . $hexString; |
|
341 | - ++$hex_len; |
|
342 | - } |
|
343 | - } |
|
344 | - |
|
345 | - $chunk = unpack('C*', $hexString); |
|
346 | - while ($hex_pos < $hex_len) { |
|
347 | - ++$hex_pos; |
|
348 | - /** @var int $c */ |
|
349 | - $c = $chunk[$hex_pos]; |
|
350 | - /** @var int $c_num */ |
|
351 | - $c_num = $c ^ 48; |
|
352 | - /** @var int $c_num0 */ |
|
353 | - $c_num0 = ($c_num - 10) >> 8; |
|
354 | - /** @var int $c_alpha */ |
|
355 | - $c_alpha = ($c & ~32) - 55; |
|
356 | - /** @var int $c_alpha0 */ |
|
357 | - $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; |
|
358 | - if (($c_num0 | $c_alpha0) === 0) { |
|
359 | - throw new RangeException( |
|
360 | - 'hex2bin() only expects hexadecimal characters' |
|
361 | - ); |
|
362 | - } |
|
363 | - /** @var int $c_val */ |
|
364 | - $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0); |
|
365 | - if ($state === 0) { |
|
366 | - $c_acc = $c_val * 16; |
|
367 | - } else { |
|
368 | - $bin .= pack('C', $c_acc | $c_val); |
|
369 | - } |
|
370 | - $state ^= 1; |
|
371 | - } |
|
372 | - return $bin; |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Turn an array of integers into a string |
|
377 | - * |
|
378 | - * @internal You should not use this directly from another application |
|
379 | - * |
|
380 | - * @param array<int, int> $ints |
|
381 | - * @return string |
|
382 | - */ |
|
383 | - public static function intArrayToString(array $ints) |
|
384 | - { |
|
385 | - /** @var array<int, int> $args */ |
|
386 | - $args = $ints; |
|
387 | - foreach ($args as $i => $v) { |
|
388 | - $args[$i] = (int) ($v & 0xff); |
|
389 | - } |
|
390 | - array_unshift($args, str_repeat('C', count($ints))); |
|
391 | - return (string) (call_user_func_array('pack', $args)); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Cache-timing-safe variant of ord() |
|
396 | - * |
|
397 | - * @internal You should not use this directly from another application |
|
398 | - * |
|
399 | - * @param int $int |
|
400 | - * @return string |
|
401 | - * @throws TypeError |
|
402 | - */ |
|
403 | - public static function intToChr($int) |
|
404 | - { |
|
405 | - return pack('C', $int); |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Load a 3 character substring into an integer |
|
410 | - * |
|
411 | - * @internal You should not use this directly from another application |
|
412 | - * |
|
413 | - * @param string $string |
|
414 | - * @return int |
|
415 | - * @throws RangeException |
|
416 | - * @throws TypeError |
|
417 | - */ |
|
418 | - public static function load_3($string) |
|
419 | - { |
|
420 | - /* Type checks: */ |
|
421 | - if (!is_string($string)) { |
|
422 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
423 | - } |
|
424 | - |
|
425 | - /* Input validation: */ |
|
426 | - if (self::strlen($string) < 3) { |
|
427 | - throw new RangeException( |
|
428 | - 'String must be 3 bytes or more; ' . self::strlen($string) . ' given.' |
|
429 | - ); |
|
430 | - } |
|
431 | - /** @var array<int, int> $unpacked */ |
|
432 | - $unpacked = unpack('V', $string . "\0"); |
|
433 | - return (int) ($unpacked[1] & 0xffffff); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Load a 4 character substring into an integer |
|
438 | - * |
|
439 | - * @internal You should not use this directly from another application |
|
440 | - * |
|
441 | - * @param string $string |
|
442 | - * @return int |
|
443 | - * @throws RangeException |
|
444 | - * @throws TypeError |
|
445 | - */ |
|
446 | - public static function load_4($string) |
|
447 | - { |
|
448 | - /* Type checks: */ |
|
449 | - if (!is_string($string)) { |
|
450 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
451 | - } |
|
452 | - |
|
453 | - /* Input validation: */ |
|
454 | - if (self::strlen($string) < 4) { |
|
455 | - throw new RangeException( |
|
456 | - 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
457 | - ); |
|
458 | - } |
|
459 | - /** @var array<int, int> $unpacked */ |
|
460 | - $unpacked = unpack('V', $string); |
|
461 | - return (int) ($unpacked[1] & 0xffffffff); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Load a 8 character substring into an integer |
|
466 | - * |
|
467 | - * @internal You should not use this directly from another application |
|
468 | - * |
|
469 | - * @param string $string |
|
470 | - * @return int |
|
471 | - * @throws RangeException |
|
472 | - * @throws SodiumException |
|
473 | - * @throws TypeError |
|
474 | - */ |
|
475 | - public static function load64_le($string) |
|
476 | - { |
|
477 | - /* Type checks: */ |
|
478 | - if (!is_string($string)) { |
|
479 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
480 | - } |
|
481 | - |
|
482 | - /* Input validation: */ |
|
483 | - if (self::strlen($string) < 4) { |
|
484 | - throw new RangeException( |
|
485 | - 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
486 | - ); |
|
487 | - } |
|
488 | - if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) { |
|
489 | - /** @var array<int, int> $unpacked */ |
|
490 | - $unpacked = unpack('P', $string); |
|
491 | - return (int) $unpacked[1]; |
|
492 | - } |
|
493 | - |
|
494 | - /** @var int $result */ |
|
495 | - $result = (self::chrToInt($string[0]) & 0xff); |
|
496 | - $result |= (self::chrToInt($string[1]) & 0xff) << 8; |
|
497 | - $result |= (self::chrToInt($string[2]) & 0xff) << 16; |
|
498 | - $result |= (self::chrToInt($string[3]) & 0xff) << 24; |
|
499 | - $result |= (self::chrToInt($string[4]) & 0xff) << 32; |
|
500 | - $result |= (self::chrToInt($string[5]) & 0xff) << 40; |
|
501 | - $result |= (self::chrToInt($string[6]) & 0xff) << 48; |
|
502 | - $result |= (self::chrToInt($string[7]) & 0xff) << 56; |
|
503 | - return (int) $result; |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * @internal You should not use this directly from another application |
|
508 | - * |
|
509 | - * @param string $left |
|
510 | - * @param string $right |
|
511 | - * @return int |
|
512 | - * @throws SodiumException |
|
513 | - * @throws TypeError |
|
514 | - */ |
|
515 | - public static function memcmp($left, $right) |
|
516 | - { |
|
517 | - if (self::hashEquals($left, $right)) { |
|
518 | - return 0; |
|
519 | - } |
|
520 | - return -1; |
|
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * Multiply two integers in constant-time |
|
525 | - * |
|
526 | - * Micro-architecture timing side-channels caused by how your CPU |
|
527 | - * implements multiplication are best prevented by never using the |
|
528 | - * multiplication operators and ensuring that our code always takes |
|
529 | - * the same number of operations to complete, regardless of the values |
|
530 | - * of $a and $b. |
|
531 | - * |
|
532 | - * @internal You should not use this directly from another application |
|
533 | - * |
|
534 | - * @param int $a |
|
535 | - * @param int $b |
|
536 | - * @param int $size Limits the number of operations (useful for small, |
|
537 | - * constant operands) |
|
538 | - * @return int |
|
539 | - */ |
|
540 | - public static function mul($a, $b, $size = 0) |
|
541 | - { |
|
542 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
543 | - return (int) ($a * $b); |
|
544 | - } |
|
545 | - |
|
546 | - static $defaultSize = null; |
|
547 | - /** @var int $defaultSize */ |
|
548 | - if (!$defaultSize) { |
|
549 | - /** @var int $defaultSize */ |
|
550 | - $defaultSize = (PHP_INT_SIZE << 3) - 1; |
|
551 | - } |
|
552 | - if ($size < 1) { |
|
553 | - /** @var int $size */ |
|
554 | - $size = $defaultSize; |
|
555 | - } |
|
556 | - /** @var int $size */ |
|
557 | - |
|
558 | - $c = 0; |
|
559 | - |
|
560 | - /** |
|
561 | - * Mask is either -1 or 0. |
|
562 | - * |
|
563 | - * -1 in binary looks like 0x1111 ... 1111 |
|
564 | - * 0 in binary looks like 0x0000 ... 0000 |
|
565 | - * |
|
566 | - * @var int |
|
567 | - */ |
|
568 | - $mask = -(($b >> ((int) $defaultSize)) & 1); |
|
569 | - |
|
570 | - /** |
|
571 | - * Ensure $b is a positive integer, without creating |
|
572 | - * a branching side-channel |
|
573 | - * |
|
574 | - * @var int $b |
|
575 | - */ |
|
576 | - $b = ($b & ~$mask) | ($mask & -$b); |
|
577 | - |
|
578 | - /** |
|
579 | - * Unless $size is provided: |
|
580 | - * |
|
581 | - * This loop always runs 32 times when PHP_INT_SIZE is 4. |
|
582 | - * This loop always runs 64 times when PHP_INT_SIZE is 8. |
|
583 | - */ |
|
584 | - for ($i = $size; $i >= 0; --$i) { |
|
585 | - $c += (int) ($a & -($b & 1)); |
|
586 | - $a <<= 1; |
|
587 | - $b >>= 1; |
|
588 | - } |
|
589 | - $c = (int) @($c & -1); |
|
590 | - |
|
591 | - /** |
|
592 | - * If $b was negative, we then apply the same value to $c here. |
|
593 | - * It doesn't matter much if $a was negative; the $c += above would |
|
594 | - * have produced a negative integer to begin with. But a negative $b |
|
595 | - * makes $b >>= 1 never return 0, so we would end up with incorrect |
|
596 | - * results. |
|
597 | - * |
|
598 | - * The end result is what we'd expect from integer multiplication. |
|
599 | - */ |
|
600 | - return (int) (($c & ~$mask) | ($mask & -$c)); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * Convert any arbitrary numbers into two 32-bit integers that represent |
|
605 | - * a 64-bit integer. |
|
606 | - * |
|
607 | - * @internal You should not use this directly from another application |
|
608 | - * |
|
609 | - * @param int|float $num |
|
610 | - * @return array<int, int> |
|
611 | - */ |
|
612 | - public static function numericTo64BitInteger($num) |
|
613 | - { |
|
614 | - $high = 0; |
|
615 | - /** @var int $low */ |
|
616 | - $low = $num & 0xffffffff; |
|
617 | - |
|
618 | - if ((+(abs($num))) >= 1) { |
|
619 | - if ($num > 0) { |
|
620 | - /** @var int $high */ |
|
621 | - $high = min((+(floor($num/4294967296))), 4294967295); |
|
622 | - } else { |
|
623 | - /** @var int $high */ |
|
624 | - $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296)))); |
|
625 | - } |
|
626 | - } |
|
627 | - return array((int) $high, (int) $low); |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Store a 24-bit integer into a string, treating it as big-endian. |
|
632 | - * |
|
633 | - * @internal You should not use this directly from another application |
|
634 | - * |
|
635 | - * @param int $int |
|
636 | - * @return string |
|
637 | - * @throws TypeError |
|
638 | - */ |
|
639 | - public static function store_3($int) |
|
640 | - { |
|
641 | - /* Type checks: */ |
|
642 | - if (!is_int($int)) { |
|
643 | - if (is_numeric($int)) { |
|
644 | - $int = (int) $int; |
|
645 | - } else { |
|
646 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
647 | - } |
|
648 | - } |
|
649 | - /** @var string $packed */ |
|
650 | - $packed = pack('N', $int); |
|
651 | - return self::substr($packed, 1, 3); |
|
652 | - } |
|
653 | - |
|
654 | - /** |
|
655 | - * Store a 32-bit integer into a string, treating it as little-endian. |
|
656 | - * |
|
657 | - * @internal You should not use this directly from another application |
|
658 | - * |
|
659 | - * @param int $int |
|
660 | - * @return string |
|
661 | - * @throws TypeError |
|
662 | - */ |
|
663 | - public static function store32_le($int) |
|
664 | - { |
|
665 | - /* Type checks: */ |
|
666 | - if (!is_int($int)) { |
|
667 | - if (is_numeric($int)) { |
|
668 | - $int = (int) $int; |
|
669 | - } else { |
|
670 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
671 | - } |
|
672 | - } |
|
673 | - |
|
674 | - /** @var string $packed */ |
|
675 | - $packed = pack('V', $int); |
|
676 | - return $packed; |
|
677 | - } |
|
678 | - |
|
679 | - /** |
|
680 | - * Store a 32-bit integer into a string, treating it as big-endian. |
|
681 | - * |
|
682 | - * @internal You should not use this directly from another application |
|
683 | - * |
|
684 | - * @param int $int |
|
685 | - * @return string |
|
686 | - * @throws TypeError |
|
687 | - */ |
|
688 | - public static function store_4($int) |
|
689 | - { |
|
690 | - /* Type checks: */ |
|
691 | - if (!is_int($int)) { |
|
692 | - if (is_numeric($int)) { |
|
693 | - $int = (int) $int; |
|
694 | - } else { |
|
695 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
696 | - } |
|
697 | - } |
|
698 | - |
|
699 | - /** @var string $packed */ |
|
700 | - $packed = pack('N', $int); |
|
701 | - return $packed; |
|
702 | - } |
|
703 | - |
|
704 | - /** |
|
705 | - * Stores a 64-bit integer as an string, treating it as little-endian. |
|
706 | - * |
|
707 | - * @internal You should not use this directly from another application |
|
708 | - * |
|
709 | - * @param int $int |
|
710 | - * @return string |
|
711 | - * @throws TypeError |
|
712 | - */ |
|
713 | - public static function store64_le($int) |
|
714 | - { |
|
715 | - /* Type checks: */ |
|
716 | - if (!is_int($int)) { |
|
717 | - if (is_numeric($int)) { |
|
718 | - $int = (int) $int; |
|
719 | - } else { |
|
720 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
721 | - } |
|
722 | - } |
|
723 | - |
|
724 | - if (PHP_INT_SIZE === 8) { |
|
725 | - if (PHP_VERSION_ID >= 50603) { |
|
726 | - /** @var string $packed */ |
|
727 | - $packed = pack('P', $int); |
|
728 | - return $packed; |
|
729 | - } |
|
730 | - return self::intToChr($int & 0xff) . |
|
731 | - self::intToChr(($int >> 8) & 0xff) . |
|
732 | - self::intToChr(($int >> 16) & 0xff) . |
|
733 | - self::intToChr(($int >> 24) & 0xff) . |
|
734 | - self::intToChr(($int >> 32) & 0xff) . |
|
735 | - self::intToChr(($int >> 40) & 0xff) . |
|
736 | - self::intToChr(($int >> 48) & 0xff) . |
|
737 | - self::intToChr(($int >> 56) & 0xff); |
|
738 | - } |
|
739 | - if ($int > PHP_INT_MAX) { |
|
740 | - list($hiB, $int) = self::numericTo64BitInteger($int); |
|
741 | - } else { |
|
742 | - $hiB = 0; |
|
743 | - } |
|
744 | - return |
|
745 | - self::intToChr(($int ) & 0xff) . |
|
746 | - self::intToChr(($int >> 8) & 0xff) . |
|
747 | - self::intToChr(($int >> 16) & 0xff) . |
|
748 | - self::intToChr(($int >> 24) & 0xff) . |
|
749 | - self::intToChr($hiB & 0xff) . |
|
750 | - self::intToChr(($hiB >> 8) & 0xff) . |
|
751 | - self::intToChr(($hiB >> 16) & 0xff) . |
|
752 | - self::intToChr(($hiB >> 24) & 0xff); |
|
753 | - } |
|
754 | - |
|
755 | - /** |
|
756 | - * Safe string length |
|
757 | - * |
|
758 | - * @internal You should not use this directly from another application |
|
759 | - * |
|
760 | - * @ref mbstring.func_overload |
|
761 | - * |
|
762 | - * @param string $str |
|
763 | - * @return int |
|
764 | - * @throws TypeError |
|
765 | - */ |
|
766 | - public static function strlen($str) |
|
767 | - { |
|
768 | - /* Type checks: */ |
|
769 | - if (!is_string($str)) { |
|
770 | - throw new TypeError('String expected'); |
|
771 | - } |
|
772 | - |
|
773 | - return (int) ( |
|
774 | - self::isMbStringOverride() |
|
775 | - ? mb_strlen($str, '8bit') |
|
776 | - : strlen($str) |
|
777 | - ); |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Turn a string into an array of integers |
|
782 | - * |
|
783 | - * @internal You should not use this directly from another application |
|
784 | - * |
|
785 | - * @param string $string |
|
786 | - * @return array<int, int> |
|
787 | - * @throws TypeError |
|
788 | - */ |
|
789 | - public static function stringToIntArray($string) |
|
790 | - { |
|
791 | - if (!is_string($string)) { |
|
792 | - throw new TypeError('String expected'); |
|
793 | - } |
|
794 | - /** |
|
795 | - * @var array<int, int> |
|
796 | - */ |
|
797 | - $values = array_values( |
|
798 | - unpack('C*', $string) |
|
799 | - ); |
|
800 | - return $values; |
|
801 | - } |
|
802 | - |
|
803 | - /** |
|
804 | - * Safe substring |
|
805 | - * |
|
806 | - * @internal You should not use this directly from another application |
|
807 | - * |
|
808 | - * @ref mbstring.func_overload |
|
809 | - * |
|
810 | - * @param string $str |
|
811 | - * @param int $start |
|
812 | - * @param int $length |
|
813 | - * @return string |
|
814 | - * @throws TypeError |
|
815 | - */ |
|
816 | - public static function substr($str, $start = 0, $length = null) |
|
817 | - { |
|
818 | - /* Type checks: */ |
|
819 | - if (!is_string($str)) { |
|
820 | - throw new TypeError('String expected'); |
|
821 | - } |
|
822 | - |
|
823 | - if ($length === 0) { |
|
824 | - return ''; |
|
825 | - } |
|
826 | - |
|
827 | - if (self::isMbStringOverride()) { |
|
828 | - if (PHP_VERSION_ID < 50400 && $length === null) { |
|
829 | - $length = self::strlen($str); |
|
830 | - } |
|
831 | - $sub = (string) mb_substr($str, $start, $length, '8bit'); |
|
832 | - } elseif ($length === null) { |
|
833 | - $sub = (string) substr($str, $start); |
|
834 | - } else { |
|
835 | - $sub = (string) substr($str, $start, $length); |
|
836 | - } |
|
837 | - if ($sub !== '') { |
|
838 | - return $sub; |
|
839 | - } |
|
840 | - return ''; |
|
841 | - } |
|
842 | - |
|
843 | - /** |
|
844 | - * Compare a 16-character byte string in constant time. |
|
845 | - * |
|
846 | - * @internal You should not use this directly from another application |
|
847 | - * |
|
848 | - * @param string $a |
|
849 | - * @param string $b |
|
850 | - * @return bool |
|
851 | - * @throws SodiumException |
|
852 | - * @throws TypeError |
|
853 | - */ |
|
854 | - public static function verify_16($a, $b) |
|
855 | - { |
|
856 | - /* Type checks: */ |
|
857 | - if (!is_string($a)) { |
|
858 | - throw new TypeError('String expected'); |
|
859 | - } |
|
860 | - if (!is_string($b)) { |
|
861 | - throw new TypeError('String expected'); |
|
862 | - } |
|
863 | - return self::hashEquals( |
|
864 | - self::substr($a, 0, 16), |
|
865 | - self::substr($b, 0, 16) |
|
866 | - ); |
|
867 | - } |
|
868 | - |
|
869 | - /** |
|
870 | - * Compare a 32-character byte string in constant time. |
|
871 | - * |
|
872 | - * @internal You should not use this directly from another application |
|
873 | - * |
|
874 | - * @param string $a |
|
875 | - * @param string $b |
|
876 | - * @return bool |
|
877 | - * @throws SodiumException |
|
878 | - * @throws TypeError |
|
879 | - */ |
|
880 | - public static function verify_32($a, $b) |
|
881 | - { |
|
882 | - /* Type checks: */ |
|
883 | - if (!is_string($a)) { |
|
884 | - throw new TypeError('String expected'); |
|
885 | - } |
|
886 | - if (!is_string($b)) { |
|
887 | - throw new TypeError('String expected'); |
|
888 | - } |
|
889 | - return self::hashEquals( |
|
890 | - self::substr($a, 0, 32), |
|
891 | - self::substr($b, 0, 32) |
|
892 | - ); |
|
893 | - } |
|
894 | - |
|
895 | - /** |
|
896 | - * Calculate $a ^ $b for two strings. |
|
897 | - * |
|
898 | - * @internal You should not use this directly from another application |
|
899 | - * |
|
900 | - * @param string $a |
|
901 | - * @param string $b |
|
902 | - * @return string |
|
903 | - * @throws TypeError |
|
904 | - */ |
|
905 | - public static function xorStrings($a, $b) |
|
906 | - { |
|
907 | - /* Type checks: */ |
|
908 | - if (!is_string($a)) { |
|
909 | - throw new TypeError('Argument 1 must be a string'); |
|
910 | - } |
|
911 | - if (!is_string($b)) { |
|
912 | - throw new TypeError('Argument 2 must be a string'); |
|
913 | - } |
|
914 | - |
|
915 | - return (string) ($a ^ $b); |
|
916 | - } |
|
917 | - |
|
918 | - /** |
|
919 | - * Returns whether or not mbstring.func_overload is in effect. |
|
920 | - * |
|
921 | - * @internal You should not use this directly from another application |
|
922 | - * |
|
923 | - * Note: MB_OVERLOAD_STRING === 2, but we don't reference the constant |
|
924 | - * (for nuisance-free PHP 8 support) |
|
925 | - * |
|
926 | - * @return bool |
|
927 | - */ |
|
928 | - protected static function isMbStringOverride() |
|
929 | - { |
|
930 | - static $mbstring = null; |
|
931 | - |
|
932 | - if ($mbstring === null) { |
|
933 | - if (!defined('MB_OVERLOAD_STRING')) { |
|
934 | - $mbstring = false; |
|
935 | - return $mbstring; |
|
936 | - } |
|
937 | - $mbstring = extension_loaded('mbstring') |
|
938 | - && defined('MB_OVERLOAD_STRING') |
|
939 | - && |
|
940 | - ((int) (ini_get('mbstring.func_overload')) & 2); |
|
941 | - // MB_OVERLOAD_STRING === 2 |
|
942 | - } |
|
943 | - /** @var bool $mbstring */ |
|
944 | - |
|
945 | - return $mbstring; |
|
946 | - } |
|
12 | + /** |
|
13 | + * @param int $integer |
|
14 | + * @param int $size (16, 32, 64) |
|
15 | + * @return int |
|
16 | + */ |
|
17 | + public static function abs($integer, $size = 0) |
|
18 | + { |
|
19 | + /** @var int $realSize */ |
|
20 | + $realSize = (PHP_INT_SIZE << 3) - 1; |
|
21 | + if ($size) { |
|
22 | + --$size; |
|
23 | + } else { |
|
24 | + /** @var int $size */ |
|
25 | + $size = $realSize; |
|
26 | + } |
|
27 | + |
|
28 | + $negative = -(($integer >> $size) & 1); |
|
29 | + return (int) ( |
|
30 | + ($integer ^ $negative) |
|
31 | + + |
|
32 | + (($negative >> $realSize) & 1) |
|
33 | + ); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Convert a binary string into a hexadecimal string without cache-timing |
|
38 | + * leaks |
|
39 | + * |
|
40 | + * @internal You should not use this directly from another application |
|
41 | + * |
|
42 | + * @param string $binaryString (raw binary) |
|
43 | + * @return string |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + public static function bin2hex($binaryString) |
|
47 | + { |
|
48 | + /* Type checks: */ |
|
49 | + if (!is_string($binaryString)) { |
|
50 | + throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.'); |
|
51 | + } |
|
52 | + |
|
53 | + $hex = ''; |
|
54 | + $len = self::strlen($binaryString); |
|
55 | + for ($i = 0; $i < $len; ++$i) { |
|
56 | + /** @var array<int, int> $chunk */ |
|
57 | + $chunk = unpack('C', $binaryString[$i]); |
|
58 | + /** @var int $c */ |
|
59 | + $c = $chunk[1] & 0xf; |
|
60 | + /** @var int $b */ |
|
61 | + $b = $chunk[1] >> 4; |
|
62 | + $hex .= pack( |
|
63 | + 'CC', |
|
64 | + (87 + $b + ((($b - 10) >> 8) & ~38)), |
|
65 | + (87 + $c + ((($c - 10) >> 8) & ~38)) |
|
66 | + ); |
|
67 | + } |
|
68 | + return $hex; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Convert a binary string into a hexadecimal string without cache-timing |
|
73 | + * leaks, returning uppercase letters (as per RFC 4648) |
|
74 | + * |
|
75 | + * @internal You should not use this directly from another application |
|
76 | + * |
|
77 | + * @param string $bin_string (raw binary) |
|
78 | + * @return string |
|
79 | + * @throws TypeError |
|
80 | + */ |
|
81 | + public static function bin2hexUpper($bin_string) |
|
82 | + { |
|
83 | + $hex = ''; |
|
84 | + $len = self::strlen($bin_string); |
|
85 | + for ($i = 0; $i < $len; ++$i) { |
|
86 | + /** @var array<int, int> $chunk */ |
|
87 | + $chunk = unpack('C', $bin_string[$i]); |
|
88 | + /** |
|
89 | + * Lower 16 bits |
|
90 | + * |
|
91 | + * @var int $c |
|
92 | + */ |
|
93 | + $c = $chunk[1] & 0xf; |
|
94 | + |
|
95 | + /** |
|
96 | + * Upper 16 bits |
|
97 | + * @var int $b |
|
98 | + */ |
|
99 | + $b = $chunk[1] >> 4; |
|
100 | + |
|
101 | + /** |
|
102 | + * Use pack() and binary operators to turn the two integers |
|
103 | + * into hexadecimal characters. We don't use chr() here, because |
|
104 | + * it uses a lookup table internally and we want to avoid |
|
105 | + * cache-timing side-channels. |
|
106 | + */ |
|
107 | + $hex .= pack( |
|
108 | + 'CC', |
|
109 | + (55 + $b + ((($b - 10) >> 8) & ~6)), |
|
110 | + (55 + $c + ((($c - 10) >> 8) & ~6)) |
|
111 | + ); |
|
112 | + } |
|
113 | + return $hex; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Cache-timing-safe variant of ord() |
|
118 | + * |
|
119 | + * @internal You should not use this directly from another application |
|
120 | + * |
|
121 | + * @param string $chr |
|
122 | + * @return int |
|
123 | + * @throws SodiumException |
|
124 | + * @throws TypeError |
|
125 | + */ |
|
126 | + public static function chrToInt($chr) |
|
127 | + { |
|
128 | + /* Type checks: */ |
|
129 | + if (!is_string($chr)) { |
|
130 | + throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.'); |
|
131 | + } |
|
132 | + if (self::strlen($chr) !== 1) { |
|
133 | + throw new SodiumException('chrToInt() expects a string that is exactly 1 character long'); |
|
134 | + } |
|
135 | + /** @var array<int, int> $chunk */ |
|
136 | + $chunk = unpack('C', $chr); |
|
137 | + return (int) ($chunk[1]); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Compares two strings. |
|
142 | + * |
|
143 | + * @internal You should not use this directly from another application |
|
144 | + * |
|
145 | + * @param string $left |
|
146 | + * @param string $right |
|
147 | + * @param int $len |
|
148 | + * @return int |
|
149 | + * @throws SodiumException |
|
150 | + * @throws TypeError |
|
151 | + */ |
|
152 | + public static function compare($left, $right, $len = null) |
|
153 | + { |
|
154 | + $leftLen = self::strlen($left); |
|
155 | + $rightLen = self::strlen($right); |
|
156 | + if ($len === null) { |
|
157 | + $len = max($leftLen, $rightLen); |
|
158 | + $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT); |
|
159 | + $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT); |
|
160 | + } |
|
161 | + |
|
162 | + $gt = 0; |
|
163 | + $eq = 1; |
|
164 | + $i = $len; |
|
165 | + while ($i !== 0) { |
|
166 | + --$i; |
|
167 | + $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq; |
|
168 | + $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8; |
|
169 | + } |
|
170 | + return ($gt + $gt + $eq) - 1; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * If a variable does not match a given type, throw a TypeError. |
|
175 | + * |
|
176 | + * @param mixed $mixedVar |
|
177 | + * @param string $type |
|
178 | + * @param int $argumentIndex |
|
179 | + * @throws TypeError |
|
180 | + * @throws SodiumException |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) |
|
184 | + { |
|
185 | + if (func_num_args() === 0) { |
|
186 | + /* Tautology, by default */ |
|
187 | + return; |
|
188 | + } |
|
189 | + if (func_num_args() === 1) { |
|
190 | + throw new TypeError('Declared void, but passed a variable'); |
|
191 | + } |
|
192 | + $realType = strtolower(gettype($mixedVar)); |
|
193 | + $type = strtolower($type); |
|
194 | + switch ($type) { |
|
195 | + case 'null': |
|
196 | + if ($mixedVar !== null) { |
|
197 | + throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.'); |
|
198 | + } |
|
199 | + break; |
|
200 | + case 'integer': |
|
201 | + case 'int': |
|
202 | + $allow = array('int', 'integer'); |
|
203 | + if (!in_array($type, $allow)) { |
|
204 | + throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.'); |
|
205 | + } |
|
206 | + $mixedVar = (int) $mixedVar; |
|
207 | + break; |
|
208 | + case 'boolean': |
|
209 | + case 'bool': |
|
210 | + $allow = array('bool', 'boolean'); |
|
211 | + if (!in_array($type, $allow)) { |
|
212 | + throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.'); |
|
213 | + } |
|
214 | + $mixedVar = (bool) $mixedVar; |
|
215 | + break; |
|
216 | + case 'string': |
|
217 | + if (!is_string($mixedVar)) { |
|
218 | + throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.'); |
|
219 | + } |
|
220 | + $mixedVar = (string) $mixedVar; |
|
221 | + break; |
|
222 | + case 'decimal': |
|
223 | + case 'double': |
|
224 | + case 'float': |
|
225 | + $allow = array('decimal', 'double', 'float'); |
|
226 | + if (!in_array($type, $allow)) { |
|
227 | + throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.'); |
|
228 | + } |
|
229 | + $mixedVar = (float) $mixedVar; |
|
230 | + break; |
|
231 | + case 'object': |
|
232 | + if (!is_object($mixedVar)) { |
|
233 | + throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.'); |
|
234 | + } |
|
235 | + break; |
|
236 | + case 'array': |
|
237 | + if (!is_array($mixedVar)) { |
|
238 | + if (is_object($mixedVar)) { |
|
239 | + if ($mixedVar instanceof ArrayAccess) { |
|
240 | + return; |
|
241 | + } |
|
242 | + } |
|
243 | + throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.'); |
|
244 | + } |
|
245 | + break; |
|
246 | + default: |
|
247 | + throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')'); |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Evaluate whether or not two strings are equal (in constant-time) |
|
253 | + * |
|
254 | + * @param string $left |
|
255 | + * @param string $right |
|
256 | + * @return bool |
|
257 | + * @throws SodiumException |
|
258 | + * @throws TypeError |
|
259 | + */ |
|
260 | + public static function hashEquals($left, $right) |
|
261 | + { |
|
262 | + /* Type checks: */ |
|
263 | + if (!is_string($left)) { |
|
264 | + throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.'); |
|
265 | + } |
|
266 | + if (!is_string($right)) { |
|
267 | + throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.'); |
|
268 | + } |
|
269 | + |
|
270 | + if (is_callable('hash_equals')) { |
|
271 | + return hash_equals($left, $right); |
|
272 | + } |
|
273 | + $d = 0; |
|
274 | + /** @var int $len */ |
|
275 | + $len = self::strlen($left); |
|
276 | + if ($len !== self::strlen($right)) { |
|
277 | + return false; |
|
278 | + } |
|
279 | + for ($i = 0; $i < $len; ++$i) { |
|
280 | + $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]); |
|
281 | + } |
|
282 | + |
|
283 | + if ($d !== 0) { |
|
284 | + return false; |
|
285 | + } |
|
286 | + return $left === $right; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Catch hash_update() failures and throw instead of silently proceeding |
|
291 | + * |
|
292 | + * @param HashContext|resource &$hs |
|
293 | + * @param string $data |
|
294 | + * @return void |
|
295 | + * @throws SodiumException |
|
296 | + * @psalm-suppress PossiblyInvalidArgument |
|
297 | + */ |
|
298 | + protected static function hash_update(&$hs, $data) |
|
299 | + { |
|
300 | + if (!hash_update($hs, $data)) { |
|
301 | + throw new SodiumException('hash_update() failed'); |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Convert a hexadecimal string into a binary string without cache-timing |
|
307 | + * leaks |
|
308 | + * |
|
309 | + * @internal You should not use this directly from another application |
|
310 | + * |
|
311 | + * @param string $hexString |
|
312 | + * @param bool $strictPadding |
|
313 | + * @return string (raw binary) |
|
314 | + * @throws RangeException |
|
315 | + * @throws TypeError |
|
316 | + */ |
|
317 | + public static function hex2bin($hexString, $strictPadding = false) |
|
318 | + { |
|
319 | + /* Type checks: */ |
|
320 | + if (!is_string($hexString)) { |
|
321 | + throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.'); |
|
322 | + } |
|
323 | + |
|
324 | + /** @var int $hex_pos */ |
|
325 | + $hex_pos = 0; |
|
326 | + /** @var string $bin */ |
|
327 | + $bin = ''; |
|
328 | + /** @var int $c_acc */ |
|
329 | + $c_acc = 0; |
|
330 | + /** @var int $hex_len */ |
|
331 | + $hex_len = self::strlen($hexString); |
|
332 | + /** @var int $state */ |
|
333 | + $state = 0; |
|
334 | + if (($hex_len & 1) !== 0) { |
|
335 | + if ($strictPadding) { |
|
336 | + throw new RangeException( |
|
337 | + 'Expected an even number of hexadecimal characters' |
|
338 | + ); |
|
339 | + } else { |
|
340 | + $hexString = '0' . $hexString; |
|
341 | + ++$hex_len; |
|
342 | + } |
|
343 | + } |
|
344 | + |
|
345 | + $chunk = unpack('C*', $hexString); |
|
346 | + while ($hex_pos < $hex_len) { |
|
347 | + ++$hex_pos; |
|
348 | + /** @var int $c */ |
|
349 | + $c = $chunk[$hex_pos]; |
|
350 | + /** @var int $c_num */ |
|
351 | + $c_num = $c ^ 48; |
|
352 | + /** @var int $c_num0 */ |
|
353 | + $c_num0 = ($c_num - 10) >> 8; |
|
354 | + /** @var int $c_alpha */ |
|
355 | + $c_alpha = ($c & ~32) - 55; |
|
356 | + /** @var int $c_alpha0 */ |
|
357 | + $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; |
|
358 | + if (($c_num0 | $c_alpha0) === 0) { |
|
359 | + throw new RangeException( |
|
360 | + 'hex2bin() only expects hexadecimal characters' |
|
361 | + ); |
|
362 | + } |
|
363 | + /** @var int $c_val */ |
|
364 | + $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0); |
|
365 | + if ($state === 0) { |
|
366 | + $c_acc = $c_val * 16; |
|
367 | + } else { |
|
368 | + $bin .= pack('C', $c_acc | $c_val); |
|
369 | + } |
|
370 | + $state ^= 1; |
|
371 | + } |
|
372 | + return $bin; |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Turn an array of integers into a string |
|
377 | + * |
|
378 | + * @internal You should not use this directly from another application |
|
379 | + * |
|
380 | + * @param array<int, int> $ints |
|
381 | + * @return string |
|
382 | + */ |
|
383 | + public static function intArrayToString(array $ints) |
|
384 | + { |
|
385 | + /** @var array<int, int> $args */ |
|
386 | + $args = $ints; |
|
387 | + foreach ($args as $i => $v) { |
|
388 | + $args[$i] = (int) ($v & 0xff); |
|
389 | + } |
|
390 | + array_unshift($args, str_repeat('C', count($ints))); |
|
391 | + return (string) (call_user_func_array('pack', $args)); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Cache-timing-safe variant of ord() |
|
396 | + * |
|
397 | + * @internal You should not use this directly from another application |
|
398 | + * |
|
399 | + * @param int $int |
|
400 | + * @return string |
|
401 | + * @throws TypeError |
|
402 | + */ |
|
403 | + public static function intToChr($int) |
|
404 | + { |
|
405 | + return pack('C', $int); |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Load a 3 character substring into an integer |
|
410 | + * |
|
411 | + * @internal You should not use this directly from another application |
|
412 | + * |
|
413 | + * @param string $string |
|
414 | + * @return int |
|
415 | + * @throws RangeException |
|
416 | + * @throws TypeError |
|
417 | + */ |
|
418 | + public static function load_3($string) |
|
419 | + { |
|
420 | + /* Type checks: */ |
|
421 | + if (!is_string($string)) { |
|
422 | + throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
423 | + } |
|
424 | + |
|
425 | + /* Input validation: */ |
|
426 | + if (self::strlen($string) < 3) { |
|
427 | + throw new RangeException( |
|
428 | + 'String must be 3 bytes or more; ' . self::strlen($string) . ' given.' |
|
429 | + ); |
|
430 | + } |
|
431 | + /** @var array<int, int> $unpacked */ |
|
432 | + $unpacked = unpack('V', $string . "\0"); |
|
433 | + return (int) ($unpacked[1] & 0xffffff); |
|
434 | + } |
|
435 | + |
|
436 | + /** |
|
437 | + * Load a 4 character substring into an integer |
|
438 | + * |
|
439 | + * @internal You should not use this directly from another application |
|
440 | + * |
|
441 | + * @param string $string |
|
442 | + * @return int |
|
443 | + * @throws RangeException |
|
444 | + * @throws TypeError |
|
445 | + */ |
|
446 | + public static function load_4($string) |
|
447 | + { |
|
448 | + /* Type checks: */ |
|
449 | + if (!is_string($string)) { |
|
450 | + throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
451 | + } |
|
452 | + |
|
453 | + /* Input validation: */ |
|
454 | + if (self::strlen($string) < 4) { |
|
455 | + throw new RangeException( |
|
456 | + 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
457 | + ); |
|
458 | + } |
|
459 | + /** @var array<int, int> $unpacked */ |
|
460 | + $unpacked = unpack('V', $string); |
|
461 | + return (int) ($unpacked[1] & 0xffffffff); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Load a 8 character substring into an integer |
|
466 | + * |
|
467 | + * @internal You should not use this directly from another application |
|
468 | + * |
|
469 | + * @param string $string |
|
470 | + * @return int |
|
471 | + * @throws RangeException |
|
472 | + * @throws SodiumException |
|
473 | + * @throws TypeError |
|
474 | + */ |
|
475 | + public static function load64_le($string) |
|
476 | + { |
|
477 | + /* Type checks: */ |
|
478 | + if (!is_string($string)) { |
|
479 | + throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
480 | + } |
|
481 | + |
|
482 | + /* Input validation: */ |
|
483 | + if (self::strlen($string) < 4) { |
|
484 | + throw new RangeException( |
|
485 | + 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
486 | + ); |
|
487 | + } |
|
488 | + if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) { |
|
489 | + /** @var array<int, int> $unpacked */ |
|
490 | + $unpacked = unpack('P', $string); |
|
491 | + return (int) $unpacked[1]; |
|
492 | + } |
|
493 | + |
|
494 | + /** @var int $result */ |
|
495 | + $result = (self::chrToInt($string[0]) & 0xff); |
|
496 | + $result |= (self::chrToInt($string[1]) & 0xff) << 8; |
|
497 | + $result |= (self::chrToInt($string[2]) & 0xff) << 16; |
|
498 | + $result |= (self::chrToInt($string[3]) & 0xff) << 24; |
|
499 | + $result |= (self::chrToInt($string[4]) & 0xff) << 32; |
|
500 | + $result |= (self::chrToInt($string[5]) & 0xff) << 40; |
|
501 | + $result |= (self::chrToInt($string[6]) & 0xff) << 48; |
|
502 | + $result |= (self::chrToInt($string[7]) & 0xff) << 56; |
|
503 | + return (int) $result; |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * @internal You should not use this directly from another application |
|
508 | + * |
|
509 | + * @param string $left |
|
510 | + * @param string $right |
|
511 | + * @return int |
|
512 | + * @throws SodiumException |
|
513 | + * @throws TypeError |
|
514 | + */ |
|
515 | + public static function memcmp($left, $right) |
|
516 | + { |
|
517 | + if (self::hashEquals($left, $right)) { |
|
518 | + return 0; |
|
519 | + } |
|
520 | + return -1; |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * Multiply two integers in constant-time |
|
525 | + * |
|
526 | + * Micro-architecture timing side-channels caused by how your CPU |
|
527 | + * implements multiplication are best prevented by never using the |
|
528 | + * multiplication operators and ensuring that our code always takes |
|
529 | + * the same number of operations to complete, regardless of the values |
|
530 | + * of $a and $b. |
|
531 | + * |
|
532 | + * @internal You should not use this directly from another application |
|
533 | + * |
|
534 | + * @param int $a |
|
535 | + * @param int $b |
|
536 | + * @param int $size Limits the number of operations (useful for small, |
|
537 | + * constant operands) |
|
538 | + * @return int |
|
539 | + */ |
|
540 | + public static function mul($a, $b, $size = 0) |
|
541 | + { |
|
542 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
543 | + return (int) ($a * $b); |
|
544 | + } |
|
545 | + |
|
546 | + static $defaultSize = null; |
|
547 | + /** @var int $defaultSize */ |
|
548 | + if (!$defaultSize) { |
|
549 | + /** @var int $defaultSize */ |
|
550 | + $defaultSize = (PHP_INT_SIZE << 3) - 1; |
|
551 | + } |
|
552 | + if ($size < 1) { |
|
553 | + /** @var int $size */ |
|
554 | + $size = $defaultSize; |
|
555 | + } |
|
556 | + /** @var int $size */ |
|
557 | + |
|
558 | + $c = 0; |
|
559 | + |
|
560 | + /** |
|
561 | + * Mask is either -1 or 0. |
|
562 | + * |
|
563 | + * -1 in binary looks like 0x1111 ... 1111 |
|
564 | + * 0 in binary looks like 0x0000 ... 0000 |
|
565 | + * |
|
566 | + * @var int |
|
567 | + */ |
|
568 | + $mask = -(($b >> ((int) $defaultSize)) & 1); |
|
569 | + |
|
570 | + /** |
|
571 | + * Ensure $b is a positive integer, without creating |
|
572 | + * a branching side-channel |
|
573 | + * |
|
574 | + * @var int $b |
|
575 | + */ |
|
576 | + $b = ($b & ~$mask) | ($mask & -$b); |
|
577 | + |
|
578 | + /** |
|
579 | + * Unless $size is provided: |
|
580 | + * |
|
581 | + * This loop always runs 32 times when PHP_INT_SIZE is 4. |
|
582 | + * This loop always runs 64 times when PHP_INT_SIZE is 8. |
|
583 | + */ |
|
584 | + for ($i = $size; $i >= 0; --$i) { |
|
585 | + $c += (int) ($a & -($b & 1)); |
|
586 | + $a <<= 1; |
|
587 | + $b >>= 1; |
|
588 | + } |
|
589 | + $c = (int) @($c & -1); |
|
590 | + |
|
591 | + /** |
|
592 | + * If $b was negative, we then apply the same value to $c here. |
|
593 | + * It doesn't matter much if $a was negative; the $c += above would |
|
594 | + * have produced a negative integer to begin with. But a negative $b |
|
595 | + * makes $b >>= 1 never return 0, so we would end up with incorrect |
|
596 | + * results. |
|
597 | + * |
|
598 | + * The end result is what we'd expect from integer multiplication. |
|
599 | + */ |
|
600 | + return (int) (($c & ~$mask) | ($mask & -$c)); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * Convert any arbitrary numbers into two 32-bit integers that represent |
|
605 | + * a 64-bit integer. |
|
606 | + * |
|
607 | + * @internal You should not use this directly from another application |
|
608 | + * |
|
609 | + * @param int|float $num |
|
610 | + * @return array<int, int> |
|
611 | + */ |
|
612 | + public static function numericTo64BitInteger($num) |
|
613 | + { |
|
614 | + $high = 0; |
|
615 | + /** @var int $low */ |
|
616 | + $low = $num & 0xffffffff; |
|
617 | + |
|
618 | + if ((+(abs($num))) >= 1) { |
|
619 | + if ($num > 0) { |
|
620 | + /** @var int $high */ |
|
621 | + $high = min((+(floor($num/4294967296))), 4294967295); |
|
622 | + } else { |
|
623 | + /** @var int $high */ |
|
624 | + $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296)))); |
|
625 | + } |
|
626 | + } |
|
627 | + return array((int) $high, (int) $low); |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Store a 24-bit integer into a string, treating it as big-endian. |
|
632 | + * |
|
633 | + * @internal You should not use this directly from another application |
|
634 | + * |
|
635 | + * @param int $int |
|
636 | + * @return string |
|
637 | + * @throws TypeError |
|
638 | + */ |
|
639 | + public static function store_3($int) |
|
640 | + { |
|
641 | + /* Type checks: */ |
|
642 | + if (!is_int($int)) { |
|
643 | + if (is_numeric($int)) { |
|
644 | + $int = (int) $int; |
|
645 | + } else { |
|
646 | + throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
647 | + } |
|
648 | + } |
|
649 | + /** @var string $packed */ |
|
650 | + $packed = pack('N', $int); |
|
651 | + return self::substr($packed, 1, 3); |
|
652 | + } |
|
653 | + |
|
654 | + /** |
|
655 | + * Store a 32-bit integer into a string, treating it as little-endian. |
|
656 | + * |
|
657 | + * @internal You should not use this directly from another application |
|
658 | + * |
|
659 | + * @param int $int |
|
660 | + * @return string |
|
661 | + * @throws TypeError |
|
662 | + */ |
|
663 | + public static function store32_le($int) |
|
664 | + { |
|
665 | + /* Type checks: */ |
|
666 | + if (!is_int($int)) { |
|
667 | + if (is_numeric($int)) { |
|
668 | + $int = (int) $int; |
|
669 | + } else { |
|
670 | + throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
671 | + } |
|
672 | + } |
|
673 | + |
|
674 | + /** @var string $packed */ |
|
675 | + $packed = pack('V', $int); |
|
676 | + return $packed; |
|
677 | + } |
|
678 | + |
|
679 | + /** |
|
680 | + * Store a 32-bit integer into a string, treating it as big-endian. |
|
681 | + * |
|
682 | + * @internal You should not use this directly from another application |
|
683 | + * |
|
684 | + * @param int $int |
|
685 | + * @return string |
|
686 | + * @throws TypeError |
|
687 | + */ |
|
688 | + public static function store_4($int) |
|
689 | + { |
|
690 | + /* Type checks: */ |
|
691 | + if (!is_int($int)) { |
|
692 | + if (is_numeric($int)) { |
|
693 | + $int = (int) $int; |
|
694 | + } else { |
|
695 | + throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
696 | + } |
|
697 | + } |
|
698 | + |
|
699 | + /** @var string $packed */ |
|
700 | + $packed = pack('N', $int); |
|
701 | + return $packed; |
|
702 | + } |
|
703 | + |
|
704 | + /** |
|
705 | + * Stores a 64-bit integer as an string, treating it as little-endian. |
|
706 | + * |
|
707 | + * @internal You should not use this directly from another application |
|
708 | + * |
|
709 | + * @param int $int |
|
710 | + * @return string |
|
711 | + * @throws TypeError |
|
712 | + */ |
|
713 | + public static function store64_le($int) |
|
714 | + { |
|
715 | + /* Type checks: */ |
|
716 | + if (!is_int($int)) { |
|
717 | + if (is_numeric($int)) { |
|
718 | + $int = (int) $int; |
|
719 | + } else { |
|
720 | + throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
721 | + } |
|
722 | + } |
|
723 | + |
|
724 | + if (PHP_INT_SIZE === 8) { |
|
725 | + if (PHP_VERSION_ID >= 50603) { |
|
726 | + /** @var string $packed */ |
|
727 | + $packed = pack('P', $int); |
|
728 | + return $packed; |
|
729 | + } |
|
730 | + return self::intToChr($int & 0xff) . |
|
731 | + self::intToChr(($int >> 8) & 0xff) . |
|
732 | + self::intToChr(($int >> 16) & 0xff) . |
|
733 | + self::intToChr(($int >> 24) & 0xff) . |
|
734 | + self::intToChr(($int >> 32) & 0xff) . |
|
735 | + self::intToChr(($int >> 40) & 0xff) . |
|
736 | + self::intToChr(($int >> 48) & 0xff) . |
|
737 | + self::intToChr(($int >> 56) & 0xff); |
|
738 | + } |
|
739 | + if ($int > PHP_INT_MAX) { |
|
740 | + list($hiB, $int) = self::numericTo64BitInteger($int); |
|
741 | + } else { |
|
742 | + $hiB = 0; |
|
743 | + } |
|
744 | + return |
|
745 | + self::intToChr(($int ) & 0xff) . |
|
746 | + self::intToChr(($int >> 8) & 0xff) . |
|
747 | + self::intToChr(($int >> 16) & 0xff) . |
|
748 | + self::intToChr(($int >> 24) & 0xff) . |
|
749 | + self::intToChr($hiB & 0xff) . |
|
750 | + self::intToChr(($hiB >> 8) & 0xff) . |
|
751 | + self::intToChr(($hiB >> 16) & 0xff) . |
|
752 | + self::intToChr(($hiB >> 24) & 0xff); |
|
753 | + } |
|
754 | + |
|
755 | + /** |
|
756 | + * Safe string length |
|
757 | + * |
|
758 | + * @internal You should not use this directly from another application |
|
759 | + * |
|
760 | + * @ref mbstring.func_overload |
|
761 | + * |
|
762 | + * @param string $str |
|
763 | + * @return int |
|
764 | + * @throws TypeError |
|
765 | + */ |
|
766 | + public static function strlen($str) |
|
767 | + { |
|
768 | + /* Type checks: */ |
|
769 | + if (!is_string($str)) { |
|
770 | + throw new TypeError('String expected'); |
|
771 | + } |
|
772 | + |
|
773 | + return (int) ( |
|
774 | + self::isMbStringOverride() |
|
775 | + ? mb_strlen($str, '8bit') |
|
776 | + : strlen($str) |
|
777 | + ); |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Turn a string into an array of integers |
|
782 | + * |
|
783 | + * @internal You should not use this directly from another application |
|
784 | + * |
|
785 | + * @param string $string |
|
786 | + * @return array<int, int> |
|
787 | + * @throws TypeError |
|
788 | + */ |
|
789 | + public static function stringToIntArray($string) |
|
790 | + { |
|
791 | + if (!is_string($string)) { |
|
792 | + throw new TypeError('String expected'); |
|
793 | + } |
|
794 | + /** |
|
795 | + * @var array<int, int> |
|
796 | + */ |
|
797 | + $values = array_values( |
|
798 | + unpack('C*', $string) |
|
799 | + ); |
|
800 | + return $values; |
|
801 | + } |
|
802 | + |
|
803 | + /** |
|
804 | + * Safe substring |
|
805 | + * |
|
806 | + * @internal You should not use this directly from another application |
|
807 | + * |
|
808 | + * @ref mbstring.func_overload |
|
809 | + * |
|
810 | + * @param string $str |
|
811 | + * @param int $start |
|
812 | + * @param int $length |
|
813 | + * @return string |
|
814 | + * @throws TypeError |
|
815 | + */ |
|
816 | + public static function substr($str, $start = 0, $length = null) |
|
817 | + { |
|
818 | + /* Type checks: */ |
|
819 | + if (!is_string($str)) { |
|
820 | + throw new TypeError('String expected'); |
|
821 | + } |
|
822 | + |
|
823 | + if ($length === 0) { |
|
824 | + return ''; |
|
825 | + } |
|
826 | + |
|
827 | + if (self::isMbStringOverride()) { |
|
828 | + if (PHP_VERSION_ID < 50400 && $length === null) { |
|
829 | + $length = self::strlen($str); |
|
830 | + } |
|
831 | + $sub = (string) mb_substr($str, $start, $length, '8bit'); |
|
832 | + } elseif ($length === null) { |
|
833 | + $sub = (string) substr($str, $start); |
|
834 | + } else { |
|
835 | + $sub = (string) substr($str, $start, $length); |
|
836 | + } |
|
837 | + if ($sub !== '') { |
|
838 | + return $sub; |
|
839 | + } |
|
840 | + return ''; |
|
841 | + } |
|
842 | + |
|
843 | + /** |
|
844 | + * Compare a 16-character byte string in constant time. |
|
845 | + * |
|
846 | + * @internal You should not use this directly from another application |
|
847 | + * |
|
848 | + * @param string $a |
|
849 | + * @param string $b |
|
850 | + * @return bool |
|
851 | + * @throws SodiumException |
|
852 | + * @throws TypeError |
|
853 | + */ |
|
854 | + public static function verify_16($a, $b) |
|
855 | + { |
|
856 | + /* Type checks: */ |
|
857 | + if (!is_string($a)) { |
|
858 | + throw new TypeError('String expected'); |
|
859 | + } |
|
860 | + if (!is_string($b)) { |
|
861 | + throw new TypeError('String expected'); |
|
862 | + } |
|
863 | + return self::hashEquals( |
|
864 | + self::substr($a, 0, 16), |
|
865 | + self::substr($b, 0, 16) |
|
866 | + ); |
|
867 | + } |
|
868 | + |
|
869 | + /** |
|
870 | + * Compare a 32-character byte string in constant time. |
|
871 | + * |
|
872 | + * @internal You should not use this directly from another application |
|
873 | + * |
|
874 | + * @param string $a |
|
875 | + * @param string $b |
|
876 | + * @return bool |
|
877 | + * @throws SodiumException |
|
878 | + * @throws TypeError |
|
879 | + */ |
|
880 | + public static function verify_32($a, $b) |
|
881 | + { |
|
882 | + /* Type checks: */ |
|
883 | + if (!is_string($a)) { |
|
884 | + throw new TypeError('String expected'); |
|
885 | + } |
|
886 | + if (!is_string($b)) { |
|
887 | + throw new TypeError('String expected'); |
|
888 | + } |
|
889 | + return self::hashEquals( |
|
890 | + self::substr($a, 0, 32), |
|
891 | + self::substr($b, 0, 32) |
|
892 | + ); |
|
893 | + } |
|
894 | + |
|
895 | + /** |
|
896 | + * Calculate $a ^ $b for two strings. |
|
897 | + * |
|
898 | + * @internal You should not use this directly from another application |
|
899 | + * |
|
900 | + * @param string $a |
|
901 | + * @param string $b |
|
902 | + * @return string |
|
903 | + * @throws TypeError |
|
904 | + */ |
|
905 | + public static function xorStrings($a, $b) |
|
906 | + { |
|
907 | + /* Type checks: */ |
|
908 | + if (!is_string($a)) { |
|
909 | + throw new TypeError('Argument 1 must be a string'); |
|
910 | + } |
|
911 | + if (!is_string($b)) { |
|
912 | + throw new TypeError('Argument 2 must be a string'); |
|
913 | + } |
|
914 | + |
|
915 | + return (string) ($a ^ $b); |
|
916 | + } |
|
917 | + |
|
918 | + /** |
|
919 | + * Returns whether or not mbstring.func_overload is in effect. |
|
920 | + * |
|
921 | + * @internal You should not use this directly from another application |
|
922 | + * |
|
923 | + * Note: MB_OVERLOAD_STRING === 2, but we don't reference the constant |
|
924 | + * (for nuisance-free PHP 8 support) |
|
925 | + * |
|
926 | + * @return bool |
|
927 | + */ |
|
928 | + protected static function isMbStringOverride() |
|
929 | + { |
|
930 | + static $mbstring = null; |
|
931 | + |
|
932 | + if ($mbstring === null) { |
|
933 | + if (!defined('MB_OVERLOAD_STRING')) { |
|
934 | + $mbstring = false; |
|
935 | + return $mbstring; |
|
936 | + } |
|
937 | + $mbstring = extension_loaded('mbstring') |
|
938 | + && defined('MB_OVERLOAD_STRING') |
|
939 | + && |
|
940 | + ((int) (ini_get('mbstring.func_overload')) & 2); |
|
941 | + // MB_OVERLOAD_STRING === 2 |
|
942 | + } |
|
943 | + /** @var bool $mbstring */ |
|
944 | + |
|
945 | + return $mbstring; |
|
946 | + } |
|
947 | 947 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_Util', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_Util', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -14,22 +14,22 @@ discard block |
||
14 | 14 | * @param int $size (16, 32, 64) |
15 | 15 | * @return int |
16 | 16 | */ |
17 | - public static function abs($integer, $size = 0) |
|
17 | + public static function abs( $integer, $size = 0 ) |
|
18 | 18 | { |
19 | 19 | /** @var int $realSize */ |
20 | - $realSize = (PHP_INT_SIZE << 3) - 1; |
|
21 | - if ($size) { |
|
20 | + $realSize = ( PHP_INT_SIZE << 3 ) - 1; |
|
21 | + if ( $size ) { |
|
22 | 22 | --$size; |
23 | 23 | } else { |
24 | 24 | /** @var int $size */ |
25 | 25 | $size = $realSize; |
26 | 26 | } |
27 | 27 | |
28 | - $negative = -(($integer >> $size) & 1); |
|
29 | - return (int) ( |
|
30 | - ($integer ^ $negative) |
|
28 | + $negative = -( ( $integer >> $size ) & 1 ); |
|
29 | + return (int)( |
|
30 | + ( $integer ^ $negative ) |
|
31 | 31 | + |
32 | - (($negative >> $realSize) & 1) |
|
32 | + ( ( $negative >> $realSize ) & 1 ) |
|
33 | 33 | ); |
34 | 34 | } |
35 | 35 | |
@@ -43,26 +43,26 @@ discard block |
||
43 | 43 | * @return string |
44 | 44 | * @throws TypeError |
45 | 45 | */ |
46 | - public static function bin2hex($binaryString) |
|
46 | + public static function bin2hex( $binaryString ) |
|
47 | 47 | { |
48 | 48 | /* Type checks: */ |
49 | - if (!is_string($binaryString)) { |
|
50 | - throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.'); |
|
49 | + if ( ! is_string( $binaryString ) ) { |
|
50 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $binaryString ) . ' given.' ); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | $hex = ''; |
54 | - $len = self::strlen($binaryString); |
|
55 | - for ($i = 0; $i < $len; ++$i) { |
|
54 | + $len = self::strlen( $binaryString ); |
|
55 | + for ( $i = 0; $i < $len; ++$i ) { |
|
56 | 56 | /** @var array<int, int> $chunk */ |
57 | - $chunk = unpack('C', $binaryString[$i]); |
|
57 | + $chunk = unpack( 'C', $binaryString[ $i ] ); |
|
58 | 58 | /** @var int $c */ |
59 | - $c = $chunk[1] & 0xf; |
|
59 | + $c = $chunk[ 1 ] & 0xf; |
|
60 | 60 | /** @var int $b */ |
61 | - $b = $chunk[1] >> 4; |
|
61 | + $b = $chunk[ 1 ] >> 4; |
|
62 | 62 | $hex .= pack( |
63 | 63 | 'CC', |
64 | - (87 + $b + ((($b - 10) >> 8) & ~38)), |
|
65 | - (87 + $c + ((($c - 10) >> 8) & ~38)) |
|
64 | + ( 87 + $b + ( ( ( $b - 10 ) >> 8 ) & ~38 ) ), |
|
65 | + ( 87 + $c + ( ( ( $c - 10 ) >> 8 ) & ~38 ) ) |
|
66 | 66 | ); |
67 | 67 | } |
68 | 68 | return $hex; |
@@ -78,25 +78,25 @@ discard block |
||
78 | 78 | * @return string |
79 | 79 | * @throws TypeError |
80 | 80 | */ |
81 | - public static function bin2hexUpper($bin_string) |
|
81 | + public static function bin2hexUpper( $bin_string ) |
|
82 | 82 | { |
83 | 83 | $hex = ''; |
84 | - $len = self::strlen($bin_string); |
|
85 | - for ($i = 0; $i < $len; ++$i) { |
|
84 | + $len = self::strlen( $bin_string ); |
|
85 | + for ( $i = 0; $i < $len; ++$i ) { |
|
86 | 86 | /** @var array<int, int> $chunk */ |
87 | - $chunk = unpack('C', $bin_string[$i]); |
|
87 | + $chunk = unpack( 'C', $bin_string[ $i ] ); |
|
88 | 88 | /** |
89 | 89 | * Lower 16 bits |
90 | 90 | * |
91 | 91 | * @var int $c |
92 | 92 | */ |
93 | - $c = $chunk[1] & 0xf; |
|
93 | + $c = $chunk[ 1 ] & 0xf; |
|
94 | 94 | |
95 | 95 | /** |
96 | 96 | * Upper 16 bits |
97 | 97 | * @var int $b |
98 | 98 | */ |
99 | - $b = $chunk[1] >> 4; |
|
99 | + $b = $chunk[ 1 ] >> 4; |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * Use pack() and binary operators to turn the two integers |
@@ -106,8 +106,8 @@ discard block |
||
106 | 106 | */ |
107 | 107 | $hex .= pack( |
108 | 108 | 'CC', |
109 | - (55 + $b + ((($b - 10) >> 8) & ~6)), |
|
110 | - (55 + $c + ((($c - 10) >> 8) & ~6)) |
|
109 | + ( 55 + $b + ( ( ( $b - 10 ) >> 8 ) & ~6 ) ), |
|
110 | + ( 55 + $c + ( ( ( $c - 10 ) >> 8 ) & ~6 ) ) |
|
111 | 111 | ); |
112 | 112 | } |
113 | 113 | return $hex; |
@@ -123,18 +123,18 @@ discard block |
||
123 | 123 | * @throws SodiumException |
124 | 124 | * @throws TypeError |
125 | 125 | */ |
126 | - public static function chrToInt($chr) |
|
126 | + public static function chrToInt( $chr ) |
|
127 | 127 | { |
128 | 128 | /* Type checks: */ |
129 | - if (!is_string($chr)) { |
|
130 | - throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.'); |
|
129 | + if ( ! is_string( $chr ) ) { |
|
130 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $chr ) . ' given.' ); |
|
131 | 131 | } |
132 | - if (self::strlen($chr) !== 1) { |
|
133 | - throw new SodiumException('chrToInt() expects a string that is exactly 1 character long'); |
|
132 | + if ( self::strlen( $chr ) !== 1 ) { |
|
133 | + throw new SodiumException( 'chrToInt() expects a string that is exactly 1 character long' ); |
|
134 | 134 | } |
135 | 135 | /** @var array<int, int> $chunk */ |
136 | - $chunk = unpack('C', $chr); |
|
137 | - return (int) ($chunk[1]); |
|
136 | + $chunk = unpack( 'C', $chr ); |
|
137 | + return (int)( $chunk[ 1 ] ); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | /** |
@@ -149,25 +149,25 @@ discard block |
||
149 | 149 | * @throws SodiumException |
150 | 150 | * @throws TypeError |
151 | 151 | */ |
152 | - public static function compare($left, $right, $len = null) |
|
152 | + public static function compare( $left, $right, $len = null ) |
|
153 | 153 | { |
154 | - $leftLen = self::strlen($left); |
|
155 | - $rightLen = self::strlen($right); |
|
156 | - if ($len === null) { |
|
157 | - $len = max($leftLen, $rightLen); |
|
158 | - $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT); |
|
159 | - $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT); |
|
154 | + $leftLen = self::strlen( $left ); |
|
155 | + $rightLen = self::strlen( $right ); |
|
156 | + if ( $len === null ) { |
|
157 | + $len = max( $leftLen, $rightLen ); |
|
158 | + $left = str_pad( $left, $len, "\x00", STR_PAD_RIGHT ); |
|
159 | + $right = str_pad( $right, $len, "\x00", STR_PAD_RIGHT ); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | $gt = 0; |
163 | 163 | $eq = 1; |
164 | 164 | $i = $len; |
165 | - while ($i !== 0) { |
|
165 | + while ( $i !== 0 ) { |
|
166 | 166 | --$i; |
167 | - $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq; |
|
168 | - $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8; |
|
167 | + $gt |= ( ( self::chrToInt( $right[ $i ] ) - self::chrToInt( $left[ $i ] ) ) >> 8 ) & $eq; |
|
168 | + $eq &= ( ( self::chrToInt( $right[ $i ] ) ^ self::chrToInt( $left[ $i ] ) ) - 1 ) >> 8; |
|
169 | 169 | } |
170 | - return ($gt + $gt + $eq) - 1; |
|
170 | + return ( $gt + $gt + $eq ) - 1; |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -180,71 +180,71 @@ discard block |
||
180 | 180 | * @throws SodiumException |
181 | 181 | * @return void |
182 | 182 | */ |
183 | - public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) |
|
183 | + public static function declareScalarType( &$mixedVar = null, $type = 'void', $argumentIndex = 0 ) |
|
184 | 184 | { |
185 | - if (func_num_args() === 0) { |
|
185 | + if ( func_num_args() === 0 ) { |
|
186 | 186 | /* Tautology, by default */ |
187 | 187 | return; |
188 | 188 | } |
189 | - if (func_num_args() === 1) { |
|
190 | - throw new TypeError('Declared void, but passed a variable'); |
|
189 | + if ( func_num_args() === 1 ) { |
|
190 | + throw new TypeError( 'Declared void, but passed a variable' ); |
|
191 | 191 | } |
192 | - $realType = strtolower(gettype($mixedVar)); |
|
193 | - $type = strtolower($type); |
|
194 | - switch ($type) { |
|
192 | + $realType = strtolower( gettype( $mixedVar ) ); |
|
193 | + $type = strtolower( $type ); |
|
194 | + switch ( $type ) { |
|
195 | 195 | case 'null': |
196 | - if ($mixedVar !== null) { |
|
197 | - throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.'); |
|
196 | + if ( $mixedVar !== null ) { |
|
197 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.' ); |
|
198 | 198 | } |
199 | 199 | break; |
200 | 200 | case 'integer': |
201 | 201 | case 'int': |
202 | - $allow = array('int', 'integer'); |
|
203 | - if (!in_array($type, $allow)) { |
|
204 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.'); |
|
202 | + $allow = array( 'int', 'integer' ); |
|
203 | + if ( ! in_array( $type, $allow ) ) { |
|
204 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.' ); |
|
205 | 205 | } |
206 | - $mixedVar = (int) $mixedVar; |
|
206 | + $mixedVar = (int)$mixedVar; |
|
207 | 207 | break; |
208 | 208 | case 'boolean': |
209 | 209 | case 'bool': |
210 | - $allow = array('bool', 'boolean'); |
|
211 | - if (!in_array($type, $allow)) { |
|
212 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.'); |
|
210 | + $allow = array( 'bool', 'boolean' ); |
|
211 | + if ( ! in_array( $type, $allow ) ) { |
|
212 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.' ); |
|
213 | 213 | } |
214 | - $mixedVar = (bool) $mixedVar; |
|
214 | + $mixedVar = (bool)$mixedVar; |
|
215 | 215 | break; |
216 | 216 | case 'string': |
217 | - if (!is_string($mixedVar)) { |
|
218 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.'); |
|
217 | + if ( ! is_string( $mixedVar ) ) { |
|
218 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.' ); |
|
219 | 219 | } |
220 | - $mixedVar = (string) $mixedVar; |
|
220 | + $mixedVar = (string)$mixedVar; |
|
221 | 221 | break; |
222 | 222 | case 'decimal': |
223 | 223 | case 'double': |
224 | 224 | case 'float': |
225 | - $allow = array('decimal', 'double', 'float'); |
|
226 | - if (!in_array($type, $allow)) { |
|
227 | - throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.'); |
|
225 | + $allow = array( 'decimal', 'double', 'float' ); |
|
226 | + if ( ! in_array( $type, $allow ) ) { |
|
227 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.' ); |
|
228 | 228 | } |
229 | - $mixedVar = (float) $mixedVar; |
|
229 | + $mixedVar = (float)$mixedVar; |
|
230 | 230 | break; |
231 | 231 | case 'object': |
232 | - if (!is_object($mixedVar)) { |
|
233 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.'); |
|
232 | + if ( ! is_object( $mixedVar ) ) { |
|
233 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.' ); |
|
234 | 234 | } |
235 | 235 | break; |
236 | 236 | case 'array': |
237 | - if (!is_array($mixedVar)) { |
|
238 | - if (is_object($mixedVar)) { |
|
239 | - if ($mixedVar instanceof ArrayAccess) { |
|
237 | + if ( ! is_array( $mixedVar ) ) { |
|
238 | + if ( is_object( $mixedVar ) ) { |
|
239 | + if ( $mixedVar instanceof ArrayAccess ) { |
|
240 | 240 | return; |
241 | 241 | } |
242 | 242 | } |
243 | - throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.'); |
|
243 | + throw new TypeError( 'Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.' ); |
|
244 | 244 | } |
245 | 245 | break; |
246 | 246 | default: |
247 | - throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')'); |
|
247 | + throw new SodiumException( 'Unknown type (' . $realType . ') does not match expect type (' . $type . ')' ); |
|
248 | 248 | } |
249 | 249 | } |
250 | 250 | |
@@ -257,30 +257,30 @@ discard block |
||
257 | 257 | * @throws SodiumException |
258 | 258 | * @throws TypeError |
259 | 259 | */ |
260 | - public static function hashEquals($left, $right) |
|
260 | + public static function hashEquals( $left, $right ) |
|
261 | 261 | { |
262 | 262 | /* Type checks: */ |
263 | - if (!is_string($left)) { |
|
264 | - throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.'); |
|
263 | + if ( ! is_string( $left ) ) { |
|
264 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $left ) . ' given.' ); |
|
265 | 265 | } |
266 | - if (!is_string($right)) { |
|
267 | - throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.'); |
|
266 | + if ( ! is_string( $right ) ) { |
|
267 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $right ) . ' given.' ); |
|
268 | 268 | } |
269 | 269 | |
270 | - if (is_callable('hash_equals')) { |
|
271 | - return hash_equals($left, $right); |
|
270 | + if ( is_callable( 'hash_equals' ) ) { |
|
271 | + return hash_equals( $left, $right ); |
|
272 | 272 | } |
273 | 273 | $d = 0; |
274 | 274 | /** @var int $len */ |
275 | - $len = self::strlen($left); |
|
276 | - if ($len !== self::strlen($right)) { |
|
275 | + $len = self::strlen( $left ); |
|
276 | + if ( $len !== self::strlen( $right ) ) { |
|
277 | 277 | return false; |
278 | 278 | } |
279 | - for ($i = 0; $i < $len; ++$i) { |
|
280 | - $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]); |
|
279 | + for ( $i = 0; $i < $len; ++$i ) { |
|
280 | + $d |= self::chrToInt( $left[ $i ] ) ^ self::chrToInt( $right[ $i ] ); |
|
281 | 281 | } |
282 | 282 | |
283 | - if ($d !== 0) { |
|
283 | + if ( $d !== 0 ) { |
|
284 | 284 | return false; |
285 | 285 | } |
286 | 286 | return $left === $right; |
@@ -295,10 +295,10 @@ discard block |
||
295 | 295 | * @throws SodiumException |
296 | 296 | * @psalm-suppress PossiblyInvalidArgument |
297 | 297 | */ |
298 | - protected static function hash_update(&$hs, $data) |
|
298 | + protected static function hash_update( &$hs, $data ) |
|
299 | 299 | { |
300 | - if (!hash_update($hs, $data)) { |
|
301 | - throw new SodiumException('hash_update() failed'); |
|
300 | + if ( ! hash_update( $hs, $data ) ) { |
|
301 | + throw new SodiumException( 'hash_update() failed' ); |
|
302 | 302 | } |
303 | 303 | } |
304 | 304 | |
@@ -314,11 +314,11 @@ discard block |
||
314 | 314 | * @throws RangeException |
315 | 315 | * @throws TypeError |
316 | 316 | */ |
317 | - public static function hex2bin($hexString, $strictPadding = false) |
|
317 | + public static function hex2bin( $hexString, $strictPadding = false ) |
|
318 | 318 | { |
319 | 319 | /* Type checks: */ |
320 | - if (!is_string($hexString)) { |
|
321 | - throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.'); |
|
320 | + if ( ! is_string( $hexString ) ) { |
|
321 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $hexString ) . ' given.' ); |
|
322 | 322 | } |
323 | 323 | |
324 | 324 | /** @var int $hex_pos */ |
@@ -328,11 +328,11 @@ discard block |
||
328 | 328 | /** @var int $c_acc */ |
329 | 329 | $c_acc = 0; |
330 | 330 | /** @var int $hex_len */ |
331 | - $hex_len = self::strlen($hexString); |
|
331 | + $hex_len = self::strlen( $hexString ); |
|
332 | 332 | /** @var int $state */ |
333 | 333 | $state = 0; |
334 | - if (($hex_len & 1) !== 0) { |
|
335 | - if ($strictPadding) { |
|
334 | + if ( ( $hex_len & 1 ) !== 0 ) { |
|
335 | + if ( $strictPadding ) { |
|
336 | 336 | throw new RangeException( |
337 | 337 | 'Expected an even number of hexadecimal characters' |
338 | 338 | ); |
@@ -342,30 +342,30 @@ discard block |
||
342 | 342 | } |
343 | 343 | } |
344 | 344 | |
345 | - $chunk = unpack('C*', $hexString); |
|
346 | - while ($hex_pos < $hex_len) { |
|
345 | + $chunk = unpack( 'C*', $hexString ); |
|
346 | + while ( $hex_pos < $hex_len ) { |
|
347 | 347 | ++$hex_pos; |
348 | 348 | /** @var int $c */ |
349 | - $c = $chunk[$hex_pos]; |
|
349 | + $c = $chunk[ $hex_pos ]; |
|
350 | 350 | /** @var int $c_num */ |
351 | 351 | $c_num = $c ^ 48; |
352 | 352 | /** @var int $c_num0 */ |
353 | - $c_num0 = ($c_num - 10) >> 8; |
|
353 | + $c_num0 = ( $c_num - 10 ) >> 8; |
|
354 | 354 | /** @var int $c_alpha */ |
355 | - $c_alpha = ($c & ~32) - 55; |
|
355 | + $c_alpha = ( $c & ~32 ) - 55; |
|
356 | 356 | /** @var int $c_alpha0 */ |
357 | - $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; |
|
358 | - if (($c_num0 | $c_alpha0) === 0) { |
|
357 | + $c_alpha0 = ( ( $c_alpha - 10 ) ^ ( $c_alpha - 16 ) ) >> 8; |
|
358 | + if ( ( $c_num0 | $c_alpha0 ) === 0 ) { |
|
359 | 359 | throw new RangeException( |
360 | 360 | 'hex2bin() only expects hexadecimal characters' |
361 | 361 | ); |
362 | 362 | } |
363 | 363 | /** @var int $c_val */ |
364 | - $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0); |
|
365 | - if ($state === 0) { |
|
364 | + $c_val = ( $c_num0 & $c_num ) | ( $c_alpha & $c_alpha0 ); |
|
365 | + if ( $state === 0 ) { |
|
366 | 366 | $c_acc = $c_val * 16; |
367 | 367 | } else { |
368 | - $bin .= pack('C', $c_acc | $c_val); |
|
368 | + $bin .= pack( 'C', $c_acc | $c_val ); |
|
369 | 369 | } |
370 | 370 | $state ^= 1; |
371 | 371 | } |
@@ -380,15 +380,15 @@ discard block |
||
380 | 380 | * @param array<int, int> $ints |
381 | 381 | * @return string |
382 | 382 | */ |
383 | - public static function intArrayToString(array $ints) |
|
383 | + public static function intArrayToString( array $ints ) |
|
384 | 384 | { |
385 | 385 | /** @var array<int, int> $args */ |
386 | 386 | $args = $ints; |
387 | - foreach ($args as $i => $v) { |
|
388 | - $args[$i] = (int) ($v & 0xff); |
|
387 | + foreach ( $args as $i => $v ) { |
|
388 | + $args[ $i ] = (int)( $v & 0xff ); |
|
389 | 389 | } |
390 | - array_unshift($args, str_repeat('C', count($ints))); |
|
391 | - return (string) (call_user_func_array('pack', $args)); |
|
390 | + array_unshift( $args, str_repeat( 'C', count( $ints ) ) ); |
|
391 | + return (string)( call_user_func_array( 'pack', $args ) ); |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
@@ -400,9 +400,9 @@ discard block |
||
400 | 400 | * @return string |
401 | 401 | * @throws TypeError |
402 | 402 | */ |
403 | - public static function intToChr($int) |
|
403 | + public static function intToChr( $int ) |
|
404 | 404 | { |
405 | - return pack('C', $int); |
|
405 | + return pack( 'C', $int ); |
|
406 | 406 | } |
407 | 407 | |
408 | 408 | /** |
@@ -415,22 +415,22 @@ discard block |
||
415 | 415 | * @throws RangeException |
416 | 416 | * @throws TypeError |
417 | 417 | */ |
418 | - public static function load_3($string) |
|
418 | + public static function load_3( $string ) |
|
419 | 419 | { |
420 | 420 | /* Type checks: */ |
421 | - if (!is_string($string)) { |
|
422 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
421 | + if ( ! is_string( $string ) ) { |
|
422 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' ); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | /* Input validation: */ |
426 | - if (self::strlen($string) < 3) { |
|
426 | + if ( self::strlen( $string ) < 3 ) { |
|
427 | 427 | throw new RangeException( |
428 | - 'String must be 3 bytes or more; ' . self::strlen($string) . ' given.' |
|
428 | + 'String must be 3 bytes or more; ' . self::strlen( $string ) . ' given.' |
|
429 | 429 | ); |
430 | 430 | } |
431 | 431 | /** @var array<int, int> $unpacked */ |
432 | - $unpacked = unpack('V', $string . "\0"); |
|
433 | - return (int) ($unpacked[1] & 0xffffff); |
|
432 | + $unpacked = unpack( 'V', $string . "\0" ); |
|
433 | + return (int)( $unpacked[ 1 ] & 0xffffff ); |
|
434 | 434 | } |
435 | 435 | |
436 | 436 | /** |
@@ -443,22 +443,22 @@ discard block |
||
443 | 443 | * @throws RangeException |
444 | 444 | * @throws TypeError |
445 | 445 | */ |
446 | - public static function load_4($string) |
|
446 | + public static function load_4( $string ) |
|
447 | 447 | { |
448 | 448 | /* Type checks: */ |
449 | - if (!is_string($string)) { |
|
450 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
449 | + if ( ! is_string( $string ) ) { |
|
450 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' ); |
|
451 | 451 | } |
452 | 452 | |
453 | 453 | /* Input validation: */ |
454 | - if (self::strlen($string) < 4) { |
|
454 | + if ( self::strlen( $string ) < 4 ) { |
|
455 | 455 | throw new RangeException( |
456 | - 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
456 | + 'String must be 4 bytes or more; ' . self::strlen( $string ) . ' given.' |
|
457 | 457 | ); |
458 | 458 | } |
459 | 459 | /** @var array<int, int> $unpacked */ |
460 | - $unpacked = unpack('V', $string); |
|
461 | - return (int) ($unpacked[1] & 0xffffffff); |
|
460 | + $unpacked = unpack( 'V', $string ); |
|
461 | + return (int)( $unpacked[ 1 ] & 0xffffffff ); |
|
462 | 462 | } |
463 | 463 | |
464 | 464 | /** |
@@ -472,35 +472,35 @@ discard block |
||
472 | 472 | * @throws SodiumException |
473 | 473 | * @throws TypeError |
474 | 474 | */ |
475 | - public static function load64_le($string) |
|
475 | + public static function load64_le( $string ) |
|
476 | 476 | { |
477 | 477 | /* Type checks: */ |
478 | - if (!is_string($string)) { |
|
479 | - throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
|
478 | + if ( ! is_string( $string ) ) { |
|
479 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' ); |
|
480 | 480 | } |
481 | 481 | |
482 | 482 | /* Input validation: */ |
483 | - if (self::strlen($string) < 4) { |
|
483 | + if ( self::strlen( $string ) < 4 ) { |
|
484 | 484 | throw new RangeException( |
485 | - 'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
|
485 | + 'String must be 4 bytes or more; ' . self::strlen( $string ) . ' given.' |
|
486 | 486 | ); |
487 | 487 | } |
488 | - if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) { |
|
488 | + if ( PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8 ) { |
|
489 | 489 | /** @var array<int, int> $unpacked */ |
490 | - $unpacked = unpack('P', $string); |
|
491 | - return (int) $unpacked[1]; |
|
490 | + $unpacked = unpack( 'P', $string ); |
|
491 | + return (int)$unpacked[ 1 ]; |
|
492 | 492 | } |
493 | 493 | |
494 | 494 | /** @var int $result */ |
495 | - $result = (self::chrToInt($string[0]) & 0xff); |
|
496 | - $result |= (self::chrToInt($string[1]) & 0xff) << 8; |
|
497 | - $result |= (self::chrToInt($string[2]) & 0xff) << 16; |
|
498 | - $result |= (self::chrToInt($string[3]) & 0xff) << 24; |
|
499 | - $result |= (self::chrToInt($string[4]) & 0xff) << 32; |
|
500 | - $result |= (self::chrToInt($string[5]) & 0xff) << 40; |
|
501 | - $result |= (self::chrToInt($string[6]) & 0xff) << 48; |
|
502 | - $result |= (self::chrToInt($string[7]) & 0xff) << 56; |
|
503 | - return (int) $result; |
|
495 | + $result = ( self::chrToInt( $string[ 0 ] ) & 0xff ); |
|
496 | + $result |= ( self::chrToInt( $string[ 1 ] ) & 0xff ) << 8; |
|
497 | + $result |= ( self::chrToInt( $string[ 2 ] ) & 0xff ) << 16; |
|
498 | + $result |= ( self::chrToInt( $string[ 3 ] ) & 0xff ) << 24; |
|
499 | + $result |= ( self::chrToInt( $string[ 4 ] ) & 0xff ) << 32; |
|
500 | + $result |= ( self::chrToInt( $string[ 5 ] ) & 0xff ) << 40; |
|
501 | + $result |= ( self::chrToInt( $string[ 6 ] ) & 0xff ) << 48; |
|
502 | + $result |= ( self::chrToInt( $string[ 7 ] ) & 0xff ) << 56; |
|
503 | + return (int)$result; |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | /** |
@@ -512,9 +512,9 @@ discard block |
||
512 | 512 | * @throws SodiumException |
513 | 513 | * @throws TypeError |
514 | 514 | */ |
515 | - public static function memcmp($left, $right) |
|
515 | + public static function memcmp( $left, $right ) |
|
516 | 516 | { |
517 | - if (self::hashEquals($left, $right)) { |
|
517 | + if ( self::hashEquals( $left, $right ) ) { |
|
518 | 518 | return 0; |
519 | 519 | } |
520 | 520 | return -1; |
@@ -537,19 +537,19 @@ discard block |
||
537 | 537 | * constant operands) |
538 | 538 | * @return int |
539 | 539 | */ |
540 | - public static function mul($a, $b, $size = 0) |
|
540 | + public static function mul( $a, $b, $size = 0 ) |
|
541 | 541 | { |
542 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
543 | - return (int) ($a * $b); |
|
542 | + if ( ParagonIE_Sodium_Compat::$fastMult ) { |
|
543 | + return (int)( $a * $b ); |
|
544 | 544 | } |
545 | 545 | |
546 | 546 | static $defaultSize = null; |
547 | 547 | /** @var int $defaultSize */ |
548 | - if (!$defaultSize) { |
|
548 | + if ( ! $defaultSize ) { |
|
549 | 549 | /** @var int $defaultSize */ |
550 | - $defaultSize = (PHP_INT_SIZE << 3) - 1; |
|
550 | + $defaultSize = ( PHP_INT_SIZE << 3 ) - 1; |
|
551 | 551 | } |
552 | - if ($size < 1) { |
|
552 | + if ( $size < 1 ) { |
|
553 | 553 | /** @var int $size */ |
554 | 554 | $size = $defaultSize; |
555 | 555 | } |
@@ -565,7 +565,7 @@ discard block |
||
565 | 565 | * |
566 | 566 | * @var int |
567 | 567 | */ |
568 | - $mask = -(($b >> ((int) $defaultSize)) & 1); |
|
568 | + $mask = -( ( $b >> ( (int)$defaultSize ) ) & 1 ); |
|
569 | 569 | |
570 | 570 | /** |
571 | 571 | * Ensure $b is a positive integer, without creating |
@@ -573,7 +573,7 @@ discard block |
||
573 | 573 | * |
574 | 574 | * @var int $b |
575 | 575 | */ |
576 | - $b = ($b & ~$mask) | ($mask & -$b); |
|
576 | + $b = ( $b & ~$mask ) | ( $mask & -$b ); |
|
577 | 577 | |
578 | 578 | /** |
579 | 579 | * Unless $size is provided: |
@@ -581,12 +581,12 @@ discard block |
||
581 | 581 | * This loop always runs 32 times when PHP_INT_SIZE is 4. |
582 | 582 | * This loop always runs 64 times when PHP_INT_SIZE is 8. |
583 | 583 | */ |
584 | - for ($i = $size; $i >= 0; --$i) { |
|
585 | - $c += (int) ($a & -($b & 1)); |
|
584 | + for ( $i = $size; $i >= 0; --$i ) { |
|
585 | + $c += (int)( $a & -( $b & 1 ) ); |
|
586 | 586 | $a <<= 1; |
587 | 587 | $b >>= 1; |
588 | 588 | } |
589 | - $c = (int) @($c & -1); |
|
589 | + $c = (int)@( $c & -1 ); |
|
590 | 590 | |
591 | 591 | /** |
592 | 592 | * If $b was negative, we then apply the same value to $c here. |
@@ -597,7 +597,7 @@ discard block |
||
597 | 597 | * |
598 | 598 | * The end result is what we'd expect from integer multiplication. |
599 | 599 | */ |
600 | - return (int) (($c & ~$mask) | ($mask & -$c)); |
|
600 | + return (int)( ( $c & ~$mask ) | ( $mask & -$c ) ); |
|
601 | 601 | } |
602 | 602 | |
603 | 603 | /** |
@@ -609,22 +609,22 @@ discard block |
||
609 | 609 | * @param int|float $num |
610 | 610 | * @return array<int, int> |
611 | 611 | */ |
612 | - public static function numericTo64BitInteger($num) |
|
612 | + public static function numericTo64BitInteger( $num ) |
|
613 | 613 | { |
614 | 614 | $high = 0; |
615 | 615 | /** @var int $low */ |
616 | 616 | $low = $num & 0xffffffff; |
617 | 617 | |
618 | - if ((+(abs($num))) >= 1) { |
|
619 | - if ($num > 0) { |
|
618 | + if ( (+( abs( $num ) )) >= 1 ) { |
|
619 | + if ( $num > 0 ) { |
|
620 | 620 | /** @var int $high */ |
621 | - $high = min((+(floor($num/4294967296))), 4294967295); |
|
621 | + $high = min( (+( floor( $num / 4294967296 ) )), 4294967295 ); |
|
622 | 622 | } else { |
623 | 623 | /** @var int $high */ |
624 | - $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296)))); |
|
624 | + $high = ~~( (+( ceil( ( $num - (+( ( ~~( $num ) ) )) ) / 4294967296 ) )) ); |
|
625 | 625 | } |
626 | 626 | } |
627 | - return array((int) $high, (int) $low); |
|
627 | + return array( (int)$high, (int)$low ); |
|
628 | 628 | } |
629 | 629 | |
630 | 630 | /** |
@@ -636,19 +636,19 @@ discard block |
||
636 | 636 | * @return string |
637 | 637 | * @throws TypeError |
638 | 638 | */ |
639 | - public static function store_3($int) |
|
639 | + public static function store_3( $int ) |
|
640 | 640 | { |
641 | 641 | /* Type checks: */ |
642 | - if (!is_int($int)) { |
|
643 | - if (is_numeric($int)) { |
|
644 | - $int = (int) $int; |
|
642 | + if ( ! is_int( $int ) ) { |
|
643 | + if ( is_numeric( $int ) ) { |
|
644 | + $int = (int)$int; |
|
645 | 645 | } else { |
646 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
646 | + throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' ); |
|
647 | 647 | } |
648 | 648 | } |
649 | 649 | /** @var string $packed */ |
650 | - $packed = pack('N', $int); |
|
651 | - return self::substr($packed, 1, 3); |
|
650 | + $packed = pack( 'N', $int ); |
|
651 | + return self::substr( $packed, 1, 3 ); |
|
652 | 652 | } |
653 | 653 | |
654 | 654 | /** |
@@ -660,19 +660,19 @@ discard block |
||
660 | 660 | * @return string |
661 | 661 | * @throws TypeError |
662 | 662 | */ |
663 | - public static function store32_le($int) |
|
663 | + public static function store32_le( $int ) |
|
664 | 664 | { |
665 | 665 | /* Type checks: */ |
666 | - if (!is_int($int)) { |
|
667 | - if (is_numeric($int)) { |
|
668 | - $int = (int) $int; |
|
666 | + if ( ! is_int( $int ) ) { |
|
667 | + if ( is_numeric( $int ) ) { |
|
668 | + $int = (int)$int; |
|
669 | 669 | } else { |
670 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
670 | + throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' ); |
|
671 | 671 | } |
672 | 672 | } |
673 | 673 | |
674 | 674 | /** @var string $packed */ |
675 | - $packed = pack('V', $int); |
|
675 | + $packed = pack( 'V', $int ); |
|
676 | 676 | return $packed; |
677 | 677 | } |
678 | 678 | |
@@ -685,19 +685,19 @@ discard block |
||
685 | 685 | * @return string |
686 | 686 | * @throws TypeError |
687 | 687 | */ |
688 | - public static function store_4($int) |
|
688 | + public static function store_4( $int ) |
|
689 | 689 | { |
690 | 690 | /* Type checks: */ |
691 | - if (!is_int($int)) { |
|
692 | - if (is_numeric($int)) { |
|
693 | - $int = (int) $int; |
|
691 | + if ( ! is_int( $int ) ) { |
|
692 | + if ( is_numeric( $int ) ) { |
|
693 | + $int = (int)$int; |
|
694 | 694 | } else { |
695 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
695 | + throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' ); |
|
696 | 696 | } |
697 | 697 | } |
698 | 698 | |
699 | 699 | /** @var string $packed */ |
700 | - $packed = pack('N', $int); |
|
700 | + $packed = pack( 'N', $int ); |
|
701 | 701 | return $packed; |
702 | 702 | } |
703 | 703 | |
@@ -710,46 +710,46 @@ discard block |
||
710 | 710 | * @return string |
711 | 711 | * @throws TypeError |
712 | 712 | */ |
713 | - public static function store64_le($int) |
|
713 | + public static function store64_le( $int ) |
|
714 | 714 | { |
715 | 715 | /* Type checks: */ |
716 | - if (!is_int($int)) { |
|
717 | - if (is_numeric($int)) { |
|
718 | - $int = (int) $int; |
|
716 | + if ( ! is_int( $int ) ) { |
|
717 | + if ( is_numeric( $int ) ) { |
|
718 | + $int = (int)$int; |
|
719 | 719 | } else { |
720 | - throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.'); |
|
720 | + throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' ); |
|
721 | 721 | } |
722 | 722 | } |
723 | 723 | |
724 | - if (PHP_INT_SIZE === 8) { |
|
725 | - if (PHP_VERSION_ID >= 50603) { |
|
724 | + if ( PHP_INT_SIZE === 8 ) { |
|
725 | + if ( PHP_VERSION_ID >= 50603 ) { |
|
726 | 726 | /** @var string $packed */ |
727 | - $packed = pack('P', $int); |
|
727 | + $packed = pack( 'P', $int ); |
|
728 | 728 | return $packed; |
729 | 729 | } |
730 | - return self::intToChr($int & 0xff) . |
|
731 | - self::intToChr(($int >> 8) & 0xff) . |
|
732 | - self::intToChr(($int >> 16) & 0xff) . |
|
733 | - self::intToChr(($int >> 24) & 0xff) . |
|
734 | - self::intToChr(($int >> 32) & 0xff) . |
|
735 | - self::intToChr(($int >> 40) & 0xff) . |
|
736 | - self::intToChr(($int >> 48) & 0xff) . |
|
737 | - self::intToChr(($int >> 56) & 0xff); |
|
738 | - } |
|
739 | - if ($int > PHP_INT_MAX) { |
|
740 | - list($hiB, $int) = self::numericTo64BitInteger($int); |
|
730 | + return self::intToChr( $int & 0xff ) . |
|
731 | + self::intToChr( ( $int >> 8 ) & 0xff ) . |
|
732 | + self::intToChr( ( $int >> 16 ) & 0xff ) . |
|
733 | + self::intToChr( ( $int >> 24 ) & 0xff ) . |
|
734 | + self::intToChr( ( $int >> 32 ) & 0xff ) . |
|
735 | + self::intToChr( ( $int >> 40 ) & 0xff ) . |
|
736 | + self::intToChr( ( $int >> 48 ) & 0xff ) . |
|
737 | + self::intToChr( ( $int >> 56 ) & 0xff ); |
|
738 | + } |
|
739 | + if ( $int > PHP_INT_MAX ) { |
|
740 | + list( $hiB, $int ) = self::numericTo64BitInteger( $int ); |
|
741 | 741 | } else { |
742 | 742 | $hiB = 0; |
743 | 743 | } |
744 | 744 | return |
745 | - self::intToChr(($int ) & 0xff) . |
|
746 | - self::intToChr(($int >> 8) & 0xff) . |
|
747 | - self::intToChr(($int >> 16) & 0xff) . |
|
748 | - self::intToChr(($int >> 24) & 0xff) . |
|
749 | - self::intToChr($hiB & 0xff) . |
|
750 | - self::intToChr(($hiB >> 8) & 0xff) . |
|
751 | - self::intToChr(($hiB >> 16) & 0xff) . |
|
752 | - self::intToChr(($hiB >> 24) & 0xff); |
|
745 | + self::intToChr( ( $int ) & 0xff ) . |
|
746 | + self::intToChr( ( $int >> 8 ) & 0xff ) . |
|
747 | + self::intToChr( ( $int >> 16 ) & 0xff ) . |
|
748 | + self::intToChr( ( $int >> 24 ) & 0xff ) . |
|
749 | + self::intToChr( $hiB & 0xff ) . |
|
750 | + self::intToChr( ( $hiB >> 8 ) & 0xff ) . |
|
751 | + self::intToChr( ( $hiB >> 16 ) & 0xff ) . |
|
752 | + self::intToChr( ( $hiB >> 24 ) & 0xff ); |
|
753 | 753 | } |
754 | 754 | |
755 | 755 | /** |
@@ -763,17 +763,17 @@ discard block |
||
763 | 763 | * @return int |
764 | 764 | * @throws TypeError |
765 | 765 | */ |
766 | - public static function strlen($str) |
|
766 | + public static function strlen( $str ) |
|
767 | 767 | { |
768 | 768 | /* Type checks: */ |
769 | - if (!is_string($str)) { |
|
770 | - throw new TypeError('String expected'); |
|
769 | + if ( ! is_string( $str ) ) { |
|
770 | + throw new TypeError( 'String expected' ); |
|
771 | 771 | } |
772 | 772 | |
773 | - return (int) ( |
|
773 | + return (int)( |
|
774 | 774 | self::isMbStringOverride() |
775 | - ? mb_strlen($str, '8bit') |
|
776 | - : strlen($str) |
|
775 | + ? mb_strlen( $str, '8bit' ) |
|
776 | + : strlen( $str ) |
|
777 | 777 | ); |
778 | 778 | } |
779 | 779 | |
@@ -786,16 +786,16 @@ discard block |
||
786 | 786 | * @return array<int, int> |
787 | 787 | * @throws TypeError |
788 | 788 | */ |
789 | - public static function stringToIntArray($string) |
|
789 | + public static function stringToIntArray( $string ) |
|
790 | 790 | { |
791 | - if (!is_string($string)) { |
|
792 | - throw new TypeError('String expected'); |
|
791 | + if ( ! is_string( $string ) ) { |
|
792 | + throw new TypeError( 'String expected' ); |
|
793 | 793 | } |
794 | 794 | /** |
795 | 795 | * @var array<int, int> |
796 | 796 | */ |
797 | 797 | $values = array_values( |
798 | - unpack('C*', $string) |
|
798 | + unpack( 'C*', $string ) |
|
799 | 799 | ); |
800 | 800 | return $values; |
801 | 801 | } |
@@ -813,28 +813,28 @@ discard block |
||
813 | 813 | * @return string |
814 | 814 | * @throws TypeError |
815 | 815 | */ |
816 | - public static function substr($str, $start = 0, $length = null) |
|
816 | + public static function substr( $str, $start = 0, $length = null ) |
|
817 | 817 | { |
818 | 818 | /* Type checks: */ |
819 | - if (!is_string($str)) { |
|
820 | - throw new TypeError('String expected'); |
|
819 | + if ( ! is_string( $str ) ) { |
|
820 | + throw new TypeError( 'String expected' ); |
|
821 | 821 | } |
822 | 822 | |
823 | - if ($length === 0) { |
|
823 | + if ( $length === 0 ) { |
|
824 | 824 | return ''; |
825 | 825 | } |
826 | 826 | |
827 | - if (self::isMbStringOverride()) { |
|
828 | - if (PHP_VERSION_ID < 50400 && $length === null) { |
|
829 | - $length = self::strlen($str); |
|
827 | + if ( self::isMbStringOverride() ) { |
|
828 | + if ( PHP_VERSION_ID < 50400 && $length === null ) { |
|
829 | + $length = self::strlen( $str ); |
|
830 | 830 | } |
831 | - $sub = (string) mb_substr($str, $start, $length, '8bit'); |
|
832 | - } elseif ($length === null) { |
|
833 | - $sub = (string) substr($str, $start); |
|
831 | + $sub = (string)mb_substr( $str, $start, $length, '8bit' ); |
|
832 | + } elseif ( $length === null ) { |
|
833 | + $sub = (string)substr( $str, $start ); |
|
834 | 834 | } else { |
835 | - $sub = (string) substr($str, $start, $length); |
|
835 | + $sub = (string)substr( $str, $start, $length ); |
|
836 | 836 | } |
837 | - if ($sub !== '') { |
|
837 | + if ( $sub !== '' ) { |
|
838 | 838 | return $sub; |
839 | 839 | } |
840 | 840 | return ''; |
@@ -851,18 +851,18 @@ discard block |
||
851 | 851 | * @throws SodiumException |
852 | 852 | * @throws TypeError |
853 | 853 | */ |
854 | - public static function verify_16($a, $b) |
|
854 | + public static function verify_16( $a, $b ) |
|
855 | 855 | { |
856 | 856 | /* Type checks: */ |
857 | - if (!is_string($a)) { |
|
858 | - throw new TypeError('String expected'); |
|
857 | + if ( ! is_string( $a ) ) { |
|
858 | + throw new TypeError( 'String expected' ); |
|
859 | 859 | } |
860 | - if (!is_string($b)) { |
|
861 | - throw new TypeError('String expected'); |
|
860 | + if ( ! is_string( $b ) ) { |
|
861 | + throw new TypeError( 'String expected' ); |
|
862 | 862 | } |
863 | 863 | return self::hashEquals( |
864 | - self::substr($a, 0, 16), |
|
865 | - self::substr($b, 0, 16) |
|
864 | + self::substr( $a, 0, 16 ), |
|
865 | + self::substr( $b, 0, 16 ) |
|
866 | 866 | ); |
867 | 867 | } |
868 | 868 | |
@@ -877,18 +877,18 @@ discard block |
||
877 | 877 | * @throws SodiumException |
878 | 878 | * @throws TypeError |
879 | 879 | */ |
880 | - public static function verify_32($a, $b) |
|
880 | + public static function verify_32( $a, $b ) |
|
881 | 881 | { |
882 | 882 | /* Type checks: */ |
883 | - if (!is_string($a)) { |
|
884 | - throw new TypeError('String expected'); |
|
883 | + if ( ! is_string( $a ) ) { |
|
884 | + throw new TypeError( 'String expected' ); |
|
885 | 885 | } |
886 | - if (!is_string($b)) { |
|
887 | - throw new TypeError('String expected'); |
|
886 | + if ( ! is_string( $b ) ) { |
|
887 | + throw new TypeError( 'String expected' ); |
|
888 | 888 | } |
889 | 889 | return self::hashEquals( |
890 | - self::substr($a, 0, 32), |
|
891 | - self::substr($b, 0, 32) |
|
890 | + self::substr( $a, 0, 32 ), |
|
891 | + self::substr( $b, 0, 32 ) |
|
892 | 892 | ); |
893 | 893 | } |
894 | 894 | |
@@ -902,17 +902,17 @@ discard block |
||
902 | 902 | * @return string |
903 | 903 | * @throws TypeError |
904 | 904 | */ |
905 | - public static function xorStrings($a, $b) |
|
905 | + public static function xorStrings( $a, $b ) |
|
906 | 906 | { |
907 | 907 | /* Type checks: */ |
908 | - if (!is_string($a)) { |
|
909 | - throw new TypeError('Argument 1 must be a string'); |
|
908 | + if ( ! is_string( $a ) ) { |
|
909 | + throw new TypeError( 'Argument 1 must be a string' ); |
|
910 | 910 | } |
911 | - if (!is_string($b)) { |
|
912 | - throw new TypeError('Argument 2 must be a string'); |
|
911 | + if ( ! is_string( $b ) ) { |
|
912 | + throw new TypeError( 'Argument 2 must be a string' ); |
|
913 | 913 | } |
914 | 914 | |
915 | - return (string) ($a ^ $b); |
|
915 | + return (string)( $a ^ $b ); |
|
916 | 916 | } |
917 | 917 | |
918 | 918 | /** |
@@ -929,15 +929,15 @@ discard block |
||
929 | 929 | { |
930 | 930 | static $mbstring = null; |
931 | 931 | |
932 | - if ($mbstring === null) { |
|
933 | - if (!defined('MB_OVERLOAD_STRING')) { |
|
932 | + if ( $mbstring === null ) { |
|
933 | + if ( ! defined( 'MB_OVERLOAD_STRING' ) ) { |
|
934 | 934 | $mbstring = false; |
935 | 935 | return $mbstring; |
936 | 936 | } |
937 | - $mbstring = extension_loaded('mbstring') |
|
938 | - && defined('MB_OVERLOAD_STRING') |
|
937 | + $mbstring = extension_loaded( 'mbstring' ) |
|
938 | + && defined( 'MB_OVERLOAD_STRING' ) |
|
939 | 939 | && |
940 | - ((int) (ini_get('mbstring.func_overload')) & 2); |
|
940 | + ( (int)( ini_get( 'mbstring.func_overload' ) ) & 2 ); |
|
941 | 941 | // MB_OVERLOAD_STRING === 2 |
942 | 942 | } |
943 | 943 | /** @var bool $mbstring */ |
@@ -7,15 +7,13 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_Util |
9 | 9 | */ |
10 | -abstract class ParagonIE_Sodium_Core_Util |
|
11 | -{ |
|
10 | +abstract class ParagonIE_Sodium_Core_Util { |
|
12 | 11 | /** |
13 | 12 | * @param int $integer |
14 | 13 | * @param int $size (16, 32, 64) |
15 | 14 | * @return int |
16 | 15 | */ |
17 | - public static function abs($integer, $size = 0) |
|
18 | - { |
|
16 | + public static function abs($integer, $size = 0) { |
|
19 | 17 | /** @var int $realSize */ |
20 | 18 | $realSize = (PHP_INT_SIZE << 3) - 1; |
21 | 19 | if ($size) { |
@@ -43,8 +41,7 @@ discard block |
||
43 | 41 | * @return string |
44 | 42 | * @throws TypeError |
45 | 43 | */ |
46 | - public static function bin2hex($binaryString) |
|
47 | - { |
|
44 | + public static function bin2hex($binaryString) { |
|
48 | 45 | /* Type checks: */ |
49 | 46 | if (!is_string($binaryString)) { |
50 | 47 | throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.'); |
@@ -78,8 +75,7 @@ discard block |
||
78 | 75 | * @return string |
79 | 76 | * @throws TypeError |
80 | 77 | */ |
81 | - public static function bin2hexUpper($bin_string) |
|
82 | - { |
|
78 | + public static function bin2hexUpper($bin_string) { |
|
83 | 79 | $hex = ''; |
84 | 80 | $len = self::strlen($bin_string); |
85 | 81 | for ($i = 0; $i < $len; ++$i) { |
@@ -123,8 +119,7 @@ discard block |
||
123 | 119 | * @throws SodiumException |
124 | 120 | * @throws TypeError |
125 | 121 | */ |
126 | - public static function chrToInt($chr) |
|
127 | - { |
|
122 | + public static function chrToInt($chr) { |
|
128 | 123 | /* Type checks: */ |
129 | 124 | if (!is_string($chr)) { |
130 | 125 | throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.'); |
@@ -149,8 +144,7 @@ discard block |
||
149 | 144 | * @throws SodiumException |
150 | 145 | * @throws TypeError |
151 | 146 | */ |
152 | - public static function compare($left, $right, $len = null) |
|
153 | - { |
|
147 | + public static function compare($left, $right, $len = null) { |
|
154 | 148 | $leftLen = self::strlen($left); |
155 | 149 | $rightLen = self::strlen($right); |
156 | 150 | if ($len === null) { |
@@ -180,8 +174,7 @@ discard block |
||
180 | 174 | * @throws SodiumException |
181 | 175 | * @return void |
182 | 176 | */ |
183 | - public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) |
|
184 | - { |
|
177 | + public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) { |
|
185 | 178 | if (func_num_args() === 0) { |
186 | 179 | /* Tautology, by default */ |
187 | 180 | return; |
@@ -257,8 +250,7 @@ discard block |
||
257 | 250 | * @throws SodiumException |
258 | 251 | * @throws TypeError |
259 | 252 | */ |
260 | - public static function hashEquals($left, $right) |
|
261 | - { |
|
253 | + public static function hashEquals($left, $right) { |
|
262 | 254 | /* Type checks: */ |
263 | 255 | if (!is_string($left)) { |
264 | 256 | throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.'); |
@@ -295,8 +287,7 @@ discard block |
||
295 | 287 | * @throws SodiumException |
296 | 288 | * @psalm-suppress PossiblyInvalidArgument |
297 | 289 | */ |
298 | - protected static function hash_update(&$hs, $data) |
|
299 | - { |
|
290 | + protected static function hash_update(&$hs, $data) { |
|
300 | 291 | if (!hash_update($hs, $data)) { |
301 | 292 | throw new SodiumException('hash_update() failed'); |
302 | 293 | } |
@@ -314,8 +305,7 @@ discard block |
||
314 | 305 | * @throws RangeException |
315 | 306 | * @throws TypeError |
316 | 307 | */ |
317 | - public static function hex2bin($hexString, $strictPadding = false) |
|
318 | - { |
|
308 | + public static function hex2bin($hexString, $strictPadding = false) { |
|
319 | 309 | /* Type checks: */ |
320 | 310 | if (!is_string($hexString)) { |
321 | 311 | throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.'); |
@@ -380,8 +370,7 @@ discard block |
||
380 | 370 | * @param array<int, int> $ints |
381 | 371 | * @return string |
382 | 372 | */ |
383 | - public static function intArrayToString(array $ints) |
|
384 | - { |
|
373 | + public static function intArrayToString(array $ints) { |
|
385 | 374 | /** @var array<int, int> $args */ |
386 | 375 | $args = $ints; |
387 | 376 | foreach ($args as $i => $v) { |
@@ -400,8 +389,7 @@ discard block |
||
400 | 389 | * @return string |
401 | 390 | * @throws TypeError |
402 | 391 | */ |
403 | - public static function intToChr($int) |
|
404 | - { |
|
392 | + public static function intToChr($int) { |
|
405 | 393 | return pack('C', $int); |
406 | 394 | } |
407 | 395 | |
@@ -415,8 +403,7 @@ discard block |
||
415 | 403 | * @throws RangeException |
416 | 404 | * @throws TypeError |
417 | 405 | */ |
418 | - public static function load_3($string) |
|
419 | - { |
|
406 | + public static function load_3($string) { |
|
420 | 407 | /* Type checks: */ |
421 | 408 | if (!is_string($string)) { |
422 | 409 | throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
@@ -443,8 +430,7 @@ discard block |
||
443 | 430 | * @throws RangeException |
444 | 431 | * @throws TypeError |
445 | 432 | */ |
446 | - public static function load_4($string) |
|
447 | - { |
|
433 | + public static function load_4($string) { |
|
448 | 434 | /* Type checks: */ |
449 | 435 | if (!is_string($string)) { |
450 | 436 | throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
@@ -472,8 +458,7 @@ discard block |
||
472 | 458 | * @throws SodiumException |
473 | 459 | * @throws TypeError |
474 | 460 | */ |
475 | - public static function load64_le($string) |
|
476 | - { |
|
461 | + public static function load64_le($string) { |
|
477 | 462 | /* Type checks: */ |
478 | 463 | if (!is_string($string)) { |
479 | 464 | throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.'); |
@@ -512,8 +497,7 @@ discard block |
||
512 | 497 | * @throws SodiumException |
513 | 498 | * @throws TypeError |
514 | 499 | */ |
515 | - public static function memcmp($left, $right) |
|
516 | - { |
|
500 | + public static function memcmp($left, $right) { |
|
517 | 501 | if (self::hashEquals($left, $right)) { |
518 | 502 | return 0; |
519 | 503 | } |
@@ -537,8 +521,7 @@ discard block |
||
537 | 521 | * constant operands) |
538 | 522 | * @return int |
539 | 523 | */ |
540 | - public static function mul($a, $b, $size = 0) |
|
541 | - { |
|
524 | + public static function mul($a, $b, $size = 0) { |
|
542 | 525 | if (ParagonIE_Sodium_Compat::$fastMult) { |
543 | 526 | return (int) ($a * $b); |
544 | 527 | } |
@@ -609,8 +592,7 @@ discard block |
||
609 | 592 | * @param int|float $num |
610 | 593 | * @return array<int, int> |
611 | 594 | */ |
612 | - public static function numericTo64BitInteger($num) |
|
613 | - { |
|
595 | + public static function numericTo64BitInteger($num) { |
|
614 | 596 | $high = 0; |
615 | 597 | /** @var int $low */ |
616 | 598 | $low = $num & 0xffffffff; |
@@ -636,8 +618,7 @@ discard block |
||
636 | 618 | * @return string |
637 | 619 | * @throws TypeError |
638 | 620 | */ |
639 | - public static function store_3($int) |
|
640 | - { |
|
621 | + public static function store_3($int) { |
|
641 | 622 | /* Type checks: */ |
642 | 623 | if (!is_int($int)) { |
643 | 624 | if (is_numeric($int)) { |
@@ -660,8 +641,7 @@ discard block |
||
660 | 641 | * @return string |
661 | 642 | * @throws TypeError |
662 | 643 | */ |
663 | - public static function store32_le($int) |
|
664 | - { |
|
644 | + public static function store32_le($int) { |
|
665 | 645 | /* Type checks: */ |
666 | 646 | if (!is_int($int)) { |
667 | 647 | if (is_numeric($int)) { |
@@ -685,8 +665,7 @@ discard block |
||
685 | 665 | * @return string |
686 | 666 | * @throws TypeError |
687 | 667 | */ |
688 | - public static function store_4($int) |
|
689 | - { |
|
668 | + public static function store_4($int) { |
|
690 | 669 | /* Type checks: */ |
691 | 670 | if (!is_int($int)) { |
692 | 671 | if (is_numeric($int)) { |
@@ -710,8 +689,7 @@ discard block |
||
710 | 689 | * @return string |
711 | 690 | * @throws TypeError |
712 | 691 | */ |
713 | - public static function store64_le($int) |
|
714 | - { |
|
692 | + public static function store64_le($int) { |
|
715 | 693 | /* Type checks: */ |
716 | 694 | if (!is_int($int)) { |
717 | 695 | if (is_numeric($int)) { |
@@ -763,8 +741,7 @@ discard block |
||
763 | 741 | * @return int |
764 | 742 | * @throws TypeError |
765 | 743 | */ |
766 | - public static function strlen($str) |
|
767 | - { |
|
744 | + public static function strlen($str) { |
|
768 | 745 | /* Type checks: */ |
769 | 746 | if (!is_string($str)) { |
770 | 747 | throw new TypeError('String expected'); |
@@ -786,8 +763,7 @@ discard block |
||
786 | 763 | * @return array<int, int> |
787 | 764 | * @throws TypeError |
788 | 765 | */ |
789 | - public static function stringToIntArray($string) |
|
790 | - { |
|
766 | + public static function stringToIntArray($string) { |
|
791 | 767 | if (!is_string($string)) { |
792 | 768 | throw new TypeError('String expected'); |
793 | 769 | } |
@@ -813,8 +789,7 @@ discard block |
||
813 | 789 | * @return string |
814 | 790 | * @throws TypeError |
815 | 791 | */ |
816 | - public static function substr($str, $start = 0, $length = null) |
|
817 | - { |
|
792 | + public static function substr($str, $start = 0, $length = null) { |
|
818 | 793 | /* Type checks: */ |
819 | 794 | if (!is_string($str)) { |
820 | 795 | throw new TypeError('String expected'); |
@@ -851,8 +826,7 @@ discard block |
||
851 | 826 | * @throws SodiumException |
852 | 827 | * @throws TypeError |
853 | 828 | */ |
854 | - public static function verify_16($a, $b) |
|
855 | - { |
|
829 | + public static function verify_16($a, $b) { |
|
856 | 830 | /* Type checks: */ |
857 | 831 | if (!is_string($a)) { |
858 | 832 | throw new TypeError('String expected'); |
@@ -877,8 +851,7 @@ discard block |
||
877 | 851 | * @throws SodiumException |
878 | 852 | * @throws TypeError |
879 | 853 | */ |
880 | - public static function verify_32($a, $b) |
|
881 | - { |
|
854 | + public static function verify_32($a, $b) { |
|
882 | 855 | /* Type checks: */ |
883 | 856 | if (!is_string($a)) { |
884 | 857 | throw new TypeError('String expected'); |
@@ -902,8 +875,7 @@ discard block |
||
902 | 875 | * @return string |
903 | 876 | * @throws TypeError |
904 | 877 | */ |
905 | - public static function xorStrings($a, $b) |
|
906 | - { |
|
878 | + public static function xorStrings($a, $b) { |
|
907 | 879 | /* Type checks: */ |
908 | 880 | if (!is_string($a)) { |
909 | 881 | throw new TypeError('Argument 1 must be a string'); |
@@ -925,8 +897,7 @@ discard block |
||
925 | 897 | * |
926 | 898 | * @return bool |
927 | 899 | */ |
928 | - protected static function isMbStringOverride() |
|
929 | - { |
|
900 | + protected static function isMbStringOverride() { |
|
930 | 901 | static $mbstring = null; |
931 | 902 | |
932 | 903 | if ($mbstring === null) { |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,296 +11,296 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int[] $v |
|
18 | - * @return int[] |
|
19 | - * |
|
20 | - */ |
|
21 | - public static function sipRound(array $v) |
|
22 | - { |
|
23 | - # v0 += v1; |
|
24 | - list($v[0], $v[1]) = self::add( |
|
25 | - array($v[0], $v[1]), |
|
26 | - array($v[2], $v[3]) |
|
27 | - ); |
|
28 | - |
|
29 | - # v1=ROTL(v1,13); |
|
30 | - list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13); |
|
31 | - |
|
32 | - # v1 ^= v0; |
|
33 | - $v[2] = (int) $v[2] ^ (int) $v[0]; |
|
34 | - $v[3] = (int) $v[3] ^ (int) $v[1]; |
|
35 | - |
|
36 | - # v0=ROTL(v0,32); |
|
37 | - list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32); |
|
38 | - |
|
39 | - # v2 += v3; |
|
40 | - list($v[4], $v[5]) = self::add( |
|
41 | - array((int) $v[4], (int) $v[5]), |
|
42 | - array((int) $v[6], (int) $v[7]) |
|
43 | - ); |
|
44 | - |
|
45 | - # v3=ROTL(v3,16); |
|
46 | - list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16); |
|
47 | - |
|
48 | - # v3 ^= v2; |
|
49 | - $v[6] = (int) $v[6] ^ (int) $v[4]; |
|
50 | - $v[7] = (int) $v[7] ^ (int) $v[5]; |
|
51 | - |
|
52 | - # v0 += v3; |
|
53 | - list($v[0], $v[1]) = self::add( |
|
54 | - array((int) $v[0], (int) $v[1]), |
|
55 | - array((int) $v[6], (int) $v[7]) |
|
56 | - ); |
|
57 | - |
|
58 | - # v3=ROTL(v3,21); |
|
59 | - list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21); |
|
60 | - |
|
61 | - # v3 ^= v0; |
|
62 | - $v[6] = (int) $v[6] ^ (int) $v[0]; |
|
63 | - $v[7] = (int) $v[7] ^ (int) $v[1]; |
|
64 | - |
|
65 | - # v2 += v1; |
|
66 | - list($v[4], $v[5]) = self::add( |
|
67 | - array((int) $v[4], (int) $v[5]), |
|
68 | - array((int) $v[2], (int) $v[3]) |
|
69 | - ); |
|
70 | - |
|
71 | - # v1=ROTL(v1,17); |
|
72 | - list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17); |
|
73 | - |
|
74 | - # v1 ^= v2;; |
|
75 | - $v[2] = (int) $v[2] ^ (int) $v[4]; |
|
76 | - $v[3] = (int) $v[3] ^ (int) $v[5]; |
|
77 | - |
|
78 | - # v2=ROTL(v2,32) |
|
79 | - list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32); |
|
80 | - |
|
81 | - return $v; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Add two 32 bit integers representing a 64-bit integer. |
|
86 | - * |
|
87 | - * @internal You should not use this directly from another application |
|
88 | - * |
|
89 | - * @param int[] $a |
|
90 | - * @param int[] $b |
|
91 | - * @return array<int, mixed> |
|
92 | - */ |
|
93 | - public static function add(array $a, array $b) |
|
94 | - { |
|
95 | - /** @var int $x1 */ |
|
96 | - $x1 = $a[1] + $b[1]; |
|
97 | - /** @var int $c */ |
|
98 | - $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff |
|
99 | - /** @var int $x0 */ |
|
100 | - $x0 = $a[0] + $b[0] + $c; |
|
101 | - return array( |
|
102 | - $x0 & 0xffffffff, |
|
103 | - $x1 & 0xffffffff |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @internal You should not use this directly from another application |
|
109 | - * |
|
110 | - * @param int $int0 |
|
111 | - * @param int $int1 |
|
112 | - * @param int $c |
|
113 | - * @return array<int, mixed> |
|
114 | - */ |
|
115 | - public static function rotl_64($int0, $int1, $c) |
|
116 | - { |
|
117 | - $int0 &= 0xffffffff; |
|
118 | - $int1 &= 0xffffffff; |
|
119 | - $c &= 63; |
|
120 | - if ($c === 32) { |
|
121 | - return array($int1, $int0); |
|
122 | - } |
|
123 | - if ($c > 31) { |
|
124 | - $tmp = $int1; |
|
125 | - $int1 = $int0; |
|
126 | - $int0 = $tmp; |
|
127 | - $c &= 31; |
|
128 | - } |
|
129 | - if ($c === 0) { |
|
130 | - return array($int0, $int1); |
|
131 | - } |
|
132 | - return array( |
|
133 | - 0xffffffff & ( |
|
134 | - ($int0 << $c) |
|
135 | - | |
|
136 | - ($int1 >> (32 - $c)) |
|
137 | - ), |
|
138 | - 0xffffffff & ( |
|
139 | - ($int1 << $c) |
|
140 | - | |
|
141 | - ($int0 >> (32 - $c)) |
|
142 | - ), |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Implements Siphash-2-4 using only 32-bit numbers. |
|
148 | - * |
|
149 | - * When we split an int into two, the higher bits go to the lower index. |
|
150 | - * e.g. 0xDEADBEEFAB10C92D becomes [ |
|
151 | - * 0 => 0xDEADBEEF, |
|
152 | - * 1 => 0xAB10C92D |
|
153 | - * ]. |
|
154 | - * |
|
155 | - * @internal You should not use this directly from another application |
|
156 | - * |
|
157 | - * @param string $in |
|
158 | - * @param string $key |
|
159 | - * @return string |
|
160 | - * @throws SodiumException |
|
161 | - * @throws TypeError |
|
162 | - */ |
|
163 | - public static function sipHash24($in, $key) |
|
164 | - { |
|
165 | - $inlen = self::strlen($in); |
|
166 | - |
|
167 | - # /* "somepseudorandomlygeneratedbytes" */ |
|
168 | - # u64 v0 = 0x736f6d6570736575ULL; |
|
169 | - # u64 v1 = 0x646f72616e646f6dULL; |
|
170 | - # u64 v2 = 0x6c7967656e657261ULL; |
|
171 | - # u64 v3 = 0x7465646279746573ULL; |
|
172 | - $v = array( |
|
173 | - 0x736f6d65, // 0 |
|
174 | - 0x70736575, // 1 |
|
175 | - 0x646f7261, // 2 |
|
176 | - 0x6e646f6d, // 3 |
|
177 | - 0x6c796765, // 4 |
|
178 | - 0x6e657261, // 5 |
|
179 | - 0x74656462, // 6 |
|
180 | - 0x79746573 // 7 |
|
181 | - ); |
|
182 | - // v0 => $v[0], $v[1] |
|
183 | - // v1 => $v[2], $v[3] |
|
184 | - // v2 => $v[4], $v[5] |
|
185 | - // v3 => $v[6], $v[7] |
|
186 | - |
|
187 | - # u64 k0 = LOAD64_LE( k ); |
|
188 | - # u64 k1 = LOAD64_LE( k + 8 ); |
|
189 | - $k = array( |
|
190 | - self::load_4(self::substr($key, 4, 4)), |
|
191 | - self::load_4(self::substr($key, 0, 4)), |
|
192 | - self::load_4(self::substr($key, 12, 4)), |
|
193 | - self::load_4(self::substr($key, 8, 4)) |
|
194 | - ); |
|
195 | - // k0 => $k[0], $k[1] |
|
196 | - // k1 => $k[2], $k[3] |
|
197 | - |
|
198 | - # b = ( ( u64 )inlen ) << 56; |
|
199 | - $b = array( |
|
200 | - $inlen << 24, |
|
201 | - 0 |
|
202 | - ); |
|
203 | - // See docblock for why the 0th index gets the higher bits. |
|
204 | - |
|
205 | - # v3 ^= k1; |
|
206 | - $v[6] ^= $k[2]; |
|
207 | - $v[7] ^= $k[3]; |
|
208 | - # v2 ^= k0; |
|
209 | - $v[4] ^= $k[0]; |
|
210 | - $v[5] ^= $k[1]; |
|
211 | - # v1 ^= k1; |
|
212 | - $v[2] ^= $k[2]; |
|
213 | - $v[3] ^= $k[3]; |
|
214 | - # v0 ^= k0; |
|
215 | - $v[0] ^= $k[0]; |
|
216 | - $v[1] ^= $k[1]; |
|
217 | - |
|
218 | - $left = $inlen; |
|
219 | - # for ( ; in != end; in += 8 ) |
|
220 | - while ($left >= 8) { |
|
221 | - # m = LOAD64_LE( in ); |
|
222 | - $m = array( |
|
223 | - self::load_4(self::substr($in, 4, 4)), |
|
224 | - self::load_4(self::substr($in, 0, 4)) |
|
225 | - ); |
|
226 | - |
|
227 | - # v3 ^= m; |
|
228 | - $v[6] ^= $m[0]; |
|
229 | - $v[7] ^= $m[1]; |
|
230 | - |
|
231 | - # SIPROUND; |
|
232 | - # SIPROUND; |
|
233 | - $v = self::sipRound($v); |
|
234 | - $v = self::sipRound($v); |
|
235 | - |
|
236 | - # v0 ^= m; |
|
237 | - $v[0] ^= $m[0]; |
|
238 | - $v[1] ^= $m[1]; |
|
239 | - |
|
240 | - $in = self::substr($in, 8); |
|
241 | - $left -= 8; |
|
242 | - } |
|
243 | - |
|
244 | - # switch( left ) |
|
245 | - # { |
|
246 | - # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
247 | - # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
248 | - # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
249 | - # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
250 | - # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
251 | - # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
252 | - # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
253 | - # case 0: break; |
|
254 | - # } |
|
255 | - switch ($left) { |
|
256 | - case 7: |
|
257 | - $b[0] |= self::chrToInt($in[6]) << 16; |
|
258 | - case 6: |
|
259 | - $b[0] |= self::chrToInt($in[5]) << 8; |
|
260 | - case 5: |
|
261 | - $b[0] |= self::chrToInt($in[4]); |
|
262 | - case 4: |
|
263 | - $b[1] |= self::chrToInt($in[3]) << 24; |
|
264 | - case 3: |
|
265 | - $b[1] |= self::chrToInt($in[2]) << 16; |
|
266 | - case 2: |
|
267 | - $b[1] |= self::chrToInt($in[1]) << 8; |
|
268 | - case 1: |
|
269 | - $b[1] |= self::chrToInt($in[0]); |
|
270 | - case 0: |
|
271 | - break; |
|
272 | - } |
|
273 | - // See docblock for why the 0th index gets the higher bits. |
|
274 | - |
|
275 | - # v3 ^= b; |
|
276 | - $v[6] ^= $b[0]; |
|
277 | - $v[7] ^= $b[1]; |
|
278 | - |
|
279 | - # SIPROUND; |
|
280 | - # SIPROUND; |
|
281 | - $v = self::sipRound($v); |
|
282 | - $v = self::sipRound($v); |
|
283 | - |
|
284 | - # v0 ^= b; |
|
285 | - $v[0] ^= $b[0]; |
|
286 | - $v[1] ^= $b[1]; |
|
287 | - |
|
288 | - // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
289 | - # v2 ^= 0xff; |
|
290 | - $v[5] ^= 0xff; |
|
291 | - |
|
292 | - # SIPROUND; |
|
293 | - # SIPROUND; |
|
294 | - # SIPROUND; |
|
295 | - # SIPROUND; |
|
296 | - $v = self::sipRound($v); |
|
297 | - $v = self::sipRound($v); |
|
298 | - $v = self::sipRound($v); |
|
299 | - $v = self::sipRound($v); |
|
300 | - |
|
301 | - # b = v0 ^ v1 ^ v2 ^ v3; |
|
302 | - # STORE64_LE( out, b ); |
|
303 | - return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) . |
|
304 | - self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]); |
|
305 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int[] $v |
|
18 | + * @return int[] |
|
19 | + * |
|
20 | + */ |
|
21 | + public static function sipRound(array $v) |
|
22 | + { |
|
23 | + # v0 += v1; |
|
24 | + list($v[0], $v[1]) = self::add( |
|
25 | + array($v[0], $v[1]), |
|
26 | + array($v[2], $v[3]) |
|
27 | + ); |
|
28 | + |
|
29 | + # v1=ROTL(v1,13); |
|
30 | + list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13); |
|
31 | + |
|
32 | + # v1 ^= v0; |
|
33 | + $v[2] = (int) $v[2] ^ (int) $v[0]; |
|
34 | + $v[3] = (int) $v[3] ^ (int) $v[1]; |
|
35 | + |
|
36 | + # v0=ROTL(v0,32); |
|
37 | + list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32); |
|
38 | + |
|
39 | + # v2 += v3; |
|
40 | + list($v[4], $v[5]) = self::add( |
|
41 | + array((int) $v[4], (int) $v[5]), |
|
42 | + array((int) $v[6], (int) $v[7]) |
|
43 | + ); |
|
44 | + |
|
45 | + # v3=ROTL(v3,16); |
|
46 | + list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16); |
|
47 | + |
|
48 | + # v3 ^= v2; |
|
49 | + $v[6] = (int) $v[6] ^ (int) $v[4]; |
|
50 | + $v[7] = (int) $v[7] ^ (int) $v[5]; |
|
51 | + |
|
52 | + # v0 += v3; |
|
53 | + list($v[0], $v[1]) = self::add( |
|
54 | + array((int) $v[0], (int) $v[1]), |
|
55 | + array((int) $v[6], (int) $v[7]) |
|
56 | + ); |
|
57 | + |
|
58 | + # v3=ROTL(v3,21); |
|
59 | + list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21); |
|
60 | + |
|
61 | + # v3 ^= v0; |
|
62 | + $v[6] = (int) $v[6] ^ (int) $v[0]; |
|
63 | + $v[7] = (int) $v[7] ^ (int) $v[1]; |
|
64 | + |
|
65 | + # v2 += v1; |
|
66 | + list($v[4], $v[5]) = self::add( |
|
67 | + array((int) $v[4], (int) $v[5]), |
|
68 | + array((int) $v[2], (int) $v[3]) |
|
69 | + ); |
|
70 | + |
|
71 | + # v1=ROTL(v1,17); |
|
72 | + list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17); |
|
73 | + |
|
74 | + # v1 ^= v2;; |
|
75 | + $v[2] = (int) $v[2] ^ (int) $v[4]; |
|
76 | + $v[3] = (int) $v[3] ^ (int) $v[5]; |
|
77 | + |
|
78 | + # v2=ROTL(v2,32) |
|
79 | + list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32); |
|
80 | + |
|
81 | + return $v; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Add two 32 bit integers representing a 64-bit integer. |
|
86 | + * |
|
87 | + * @internal You should not use this directly from another application |
|
88 | + * |
|
89 | + * @param int[] $a |
|
90 | + * @param int[] $b |
|
91 | + * @return array<int, mixed> |
|
92 | + */ |
|
93 | + public static function add(array $a, array $b) |
|
94 | + { |
|
95 | + /** @var int $x1 */ |
|
96 | + $x1 = $a[1] + $b[1]; |
|
97 | + /** @var int $c */ |
|
98 | + $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff |
|
99 | + /** @var int $x0 */ |
|
100 | + $x0 = $a[0] + $b[0] + $c; |
|
101 | + return array( |
|
102 | + $x0 & 0xffffffff, |
|
103 | + $x1 & 0xffffffff |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @internal You should not use this directly from another application |
|
109 | + * |
|
110 | + * @param int $int0 |
|
111 | + * @param int $int1 |
|
112 | + * @param int $c |
|
113 | + * @return array<int, mixed> |
|
114 | + */ |
|
115 | + public static function rotl_64($int0, $int1, $c) |
|
116 | + { |
|
117 | + $int0 &= 0xffffffff; |
|
118 | + $int1 &= 0xffffffff; |
|
119 | + $c &= 63; |
|
120 | + if ($c === 32) { |
|
121 | + return array($int1, $int0); |
|
122 | + } |
|
123 | + if ($c > 31) { |
|
124 | + $tmp = $int1; |
|
125 | + $int1 = $int0; |
|
126 | + $int0 = $tmp; |
|
127 | + $c &= 31; |
|
128 | + } |
|
129 | + if ($c === 0) { |
|
130 | + return array($int0, $int1); |
|
131 | + } |
|
132 | + return array( |
|
133 | + 0xffffffff & ( |
|
134 | + ($int0 << $c) |
|
135 | + | |
|
136 | + ($int1 >> (32 - $c)) |
|
137 | + ), |
|
138 | + 0xffffffff & ( |
|
139 | + ($int1 << $c) |
|
140 | + | |
|
141 | + ($int0 >> (32 - $c)) |
|
142 | + ), |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Implements Siphash-2-4 using only 32-bit numbers. |
|
148 | + * |
|
149 | + * When we split an int into two, the higher bits go to the lower index. |
|
150 | + * e.g. 0xDEADBEEFAB10C92D becomes [ |
|
151 | + * 0 => 0xDEADBEEF, |
|
152 | + * 1 => 0xAB10C92D |
|
153 | + * ]. |
|
154 | + * |
|
155 | + * @internal You should not use this directly from another application |
|
156 | + * |
|
157 | + * @param string $in |
|
158 | + * @param string $key |
|
159 | + * @return string |
|
160 | + * @throws SodiumException |
|
161 | + * @throws TypeError |
|
162 | + */ |
|
163 | + public static function sipHash24($in, $key) |
|
164 | + { |
|
165 | + $inlen = self::strlen($in); |
|
166 | + |
|
167 | + # /* "somepseudorandomlygeneratedbytes" */ |
|
168 | + # u64 v0 = 0x736f6d6570736575ULL; |
|
169 | + # u64 v1 = 0x646f72616e646f6dULL; |
|
170 | + # u64 v2 = 0x6c7967656e657261ULL; |
|
171 | + # u64 v3 = 0x7465646279746573ULL; |
|
172 | + $v = array( |
|
173 | + 0x736f6d65, // 0 |
|
174 | + 0x70736575, // 1 |
|
175 | + 0x646f7261, // 2 |
|
176 | + 0x6e646f6d, // 3 |
|
177 | + 0x6c796765, // 4 |
|
178 | + 0x6e657261, // 5 |
|
179 | + 0x74656462, // 6 |
|
180 | + 0x79746573 // 7 |
|
181 | + ); |
|
182 | + // v0 => $v[0], $v[1] |
|
183 | + // v1 => $v[2], $v[3] |
|
184 | + // v2 => $v[4], $v[5] |
|
185 | + // v3 => $v[6], $v[7] |
|
186 | + |
|
187 | + # u64 k0 = LOAD64_LE( k ); |
|
188 | + # u64 k1 = LOAD64_LE( k + 8 ); |
|
189 | + $k = array( |
|
190 | + self::load_4(self::substr($key, 4, 4)), |
|
191 | + self::load_4(self::substr($key, 0, 4)), |
|
192 | + self::load_4(self::substr($key, 12, 4)), |
|
193 | + self::load_4(self::substr($key, 8, 4)) |
|
194 | + ); |
|
195 | + // k0 => $k[0], $k[1] |
|
196 | + // k1 => $k[2], $k[3] |
|
197 | + |
|
198 | + # b = ( ( u64 )inlen ) << 56; |
|
199 | + $b = array( |
|
200 | + $inlen << 24, |
|
201 | + 0 |
|
202 | + ); |
|
203 | + // See docblock for why the 0th index gets the higher bits. |
|
204 | + |
|
205 | + # v3 ^= k1; |
|
206 | + $v[6] ^= $k[2]; |
|
207 | + $v[7] ^= $k[3]; |
|
208 | + # v2 ^= k0; |
|
209 | + $v[4] ^= $k[0]; |
|
210 | + $v[5] ^= $k[1]; |
|
211 | + # v1 ^= k1; |
|
212 | + $v[2] ^= $k[2]; |
|
213 | + $v[3] ^= $k[3]; |
|
214 | + # v0 ^= k0; |
|
215 | + $v[0] ^= $k[0]; |
|
216 | + $v[1] ^= $k[1]; |
|
217 | + |
|
218 | + $left = $inlen; |
|
219 | + # for ( ; in != end; in += 8 ) |
|
220 | + while ($left >= 8) { |
|
221 | + # m = LOAD64_LE( in ); |
|
222 | + $m = array( |
|
223 | + self::load_4(self::substr($in, 4, 4)), |
|
224 | + self::load_4(self::substr($in, 0, 4)) |
|
225 | + ); |
|
226 | + |
|
227 | + # v3 ^= m; |
|
228 | + $v[6] ^= $m[0]; |
|
229 | + $v[7] ^= $m[1]; |
|
230 | + |
|
231 | + # SIPROUND; |
|
232 | + # SIPROUND; |
|
233 | + $v = self::sipRound($v); |
|
234 | + $v = self::sipRound($v); |
|
235 | + |
|
236 | + # v0 ^= m; |
|
237 | + $v[0] ^= $m[0]; |
|
238 | + $v[1] ^= $m[1]; |
|
239 | + |
|
240 | + $in = self::substr($in, 8); |
|
241 | + $left -= 8; |
|
242 | + } |
|
243 | + |
|
244 | + # switch( left ) |
|
245 | + # { |
|
246 | + # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
247 | + # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
248 | + # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
249 | + # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
250 | + # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
251 | + # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
252 | + # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
253 | + # case 0: break; |
|
254 | + # } |
|
255 | + switch ($left) { |
|
256 | + case 7: |
|
257 | + $b[0] |= self::chrToInt($in[6]) << 16; |
|
258 | + case 6: |
|
259 | + $b[0] |= self::chrToInt($in[5]) << 8; |
|
260 | + case 5: |
|
261 | + $b[0] |= self::chrToInt($in[4]); |
|
262 | + case 4: |
|
263 | + $b[1] |= self::chrToInt($in[3]) << 24; |
|
264 | + case 3: |
|
265 | + $b[1] |= self::chrToInt($in[2]) << 16; |
|
266 | + case 2: |
|
267 | + $b[1] |= self::chrToInt($in[1]) << 8; |
|
268 | + case 1: |
|
269 | + $b[1] |= self::chrToInt($in[0]); |
|
270 | + case 0: |
|
271 | + break; |
|
272 | + } |
|
273 | + // See docblock for why the 0th index gets the higher bits. |
|
274 | + |
|
275 | + # v3 ^= b; |
|
276 | + $v[6] ^= $b[0]; |
|
277 | + $v[7] ^= $b[1]; |
|
278 | + |
|
279 | + # SIPROUND; |
|
280 | + # SIPROUND; |
|
281 | + $v = self::sipRound($v); |
|
282 | + $v = self::sipRound($v); |
|
283 | + |
|
284 | + # v0 ^= b; |
|
285 | + $v[0] ^= $b[0]; |
|
286 | + $v[1] ^= $b[1]; |
|
287 | + |
|
288 | + // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
289 | + # v2 ^= 0xff; |
|
290 | + $v[5] ^= 0xff; |
|
291 | + |
|
292 | + # SIPROUND; |
|
293 | + # SIPROUND; |
|
294 | + # SIPROUND; |
|
295 | + # SIPROUND; |
|
296 | + $v = self::sipRound($v); |
|
297 | + $v = self::sipRound($v); |
|
298 | + $v = self::sipRound($v); |
|
299 | + $v = self::sipRound($v); |
|
300 | + |
|
301 | + # b = v0 ^ v1 ^ v2 ^ v3; |
|
302 | + # STORE64_LE( out, b ); |
|
303 | + return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) . |
|
304 | + self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]); |
|
305 | + } |
|
306 | 306 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_SipHash', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -18,65 +18,65 @@ discard block |
||
18 | 18 | * @return int[] |
19 | 19 | * |
20 | 20 | */ |
21 | - public static function sipRound(array $v) |
|
21 | + public static function sipRound( array $v ) |
|
22 | 22 | { |
23 | 23 | # v0 += v1; |
24 | - list($v[0], $v[1]) = self::add( |
|
25 | - array($v[0], $v[1]), |
|
26 | - array($v[2], $v[3]) |
|
24 | + list( $v[ 0 ], $v[ 1 ] ) = self::add( |
|
25 | + array( $v[ 0 ], $v[ 1 ] ), |
|
26 | + array( $v[ 2 ], $v[ 3 ] ) |
|
27 | 27 | ); |
28 | 28 | |
29 | 29 | # v1=ROTL(v1,13); |
30 | - list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13); |
|
30 | + list( $v[ 2 ], $v[ 3 ] ) = self::rotl_64( (int)$v[ 2 ], (int)$v[ 3 ], 13 ); |
|
31 | 31 | |
32 | 32 | # v1 ^= v0; |
33 | - $v[2] = (int) $v[2] ^ (int) $v[0]; |
|
34 | - $v[3] = (int) $v[3] ^ (int) $v[1]; |
|
33 | + $v[ 2 ] = (int)$v[ 2 ] ^ (int)$v[ 0 ]; |
|
34 | + $v[ 3 ] = (int)$v[ 3 ] ^ (int)$v[ 1 ]; |
|
35 | 35 | |
36 | 36 | # v0=ROTL(v0,32); |
37 | - list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32); |
|
37 | + list( $v[ 0 ], $v[ 1 ] ) = self::rotl_64( (int)$v[ 0 ], (int)$v[ 1 ], 32 ); |
|
38 | 38 | |
39 | 39 | # v2 += v3; |
40 | - list($v[4], $v[5]) = self::add( |
|
41 | - array((int) $v[4], (int) $v[5]), |
|
42 | - array((int) $v[6], (int) $v[7]) |
|
40 | + list( $v[ 4 ], $v[ 5 ] ) = self::add( |
|
41 | + array( (int)$v[ 4 ], (int)$v[ 5 ] ), |
|
42 | + array( (int)$v[ 6 ], (int)$v[ 7 ] ) |
|
43 | 43 | ); |
44 | 44 | |
45 | 45 | # v3=ROTL(v3,16); |
46 | - list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16); |
|
46 | + list( $v[ 6 ], $v[ 7 ] ) = self::rotl_64( (int)$v[ 6 ], (int)$v[ 7 ], 16 ); |
|
47 | 47 | |
48 | 48 | # v3 ^= v2; |
49 | - $v[6] = (int) $v[6] ^ (int) $v[4]; |
|
50 | - $v[7] = (int) $v[7] ^ (int) $v[5]; |
|
49 | + $v[ 6 ] = (int)$v[ 6 ] ^ (int)$v[ 4 ]; |
|
50 | + $v[ 7 ] = (int)$v[ 7 ] ^ (int)$v[ 5 ]; |
|
51 | 51 | |
52 | 52 | # v0 += v3; |
53 | - list($v[0], $v[1]) = self::add( |
|
54 | - array((int) $v[0], (int) $v[1]), |
|
55 | - array((int) $v[6], (int) $v[7]) |
|
53 | + list( $v[ 0 ], $v[ 1 ] ) = self::add( |
|
54 | + array( (int)$v[ 0 ], (int)$v[ 1 ] ), |
|
55 | + array( (int)$v[ 6 ], (int)$v[ 7 ] ) |
|
56 | 56 | ); |
57 | 57 | |
58 | 58 | # v3=ROTL(v3,21); |
59 | - list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21); |
|
59 | + list( $v[ 6 ], $v[ 7 ] ) = self::rotl_64( (int)$v[ 6 ], (int)$v[ 7 ], 21 ); |
|
60 | 60 | |
61 | 61 | # v3 ^= v0; |
62 | - $v[6] = (int) $v[6] ^ (int) $v[0]; |
|
63 | - $v[7] = (int) $v[7] ^ (int) $v[1]; |
|
62 | + $v[ 6 ] = (int)$v[ 6 ] ^ (int)$v[ 0 ]; |
|
63 | + $v[ 7 ] = (int)$v[ 7 ] ^ (int)$v[ 1 ]; |
|
64 | 64 | |
65 | 65 | # v2 += v1; |
66 | - list($v[4], $v[5]) = self::add( |
|
67 | - array((int) $v[4], (int) $v[5]), |
|
68 | - array((int) $v[2], (int) $v[3]) |
|
66 | + list( $v[ 4 ], $v[ 5 ] ) = self::add( |
|
67 | + array( (int)$v[ 4 ], (int)$v[ 5 ] ), |
|
68 | + array( (int)$v[ 2 ], (int)$v[ 3 ] ) |
|
69 | 69 | ); |
70 | 70 | |
71 | 71 | # v1=ROTL(v1,17); |
72 | - list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17); |
|
72 | + list( $v[ 2 ], $v[ 3 ] ) = self::rotl_64( (int)$v[ 2 ], (int)$v[ 3 ], 17 ); |
|
73 | 73 | |
74 | 74 | # v1 ^= v2;; |
75 | - $v[2] = (int) $v[2] ^ (int) $v[4]; |
|
76 | - $v[3] = (int) $v[3] ^ (int) $v[5]; |
|
75 | + $v[ 2 ] = (int)$v[ 2 ] ^ (int)$v[ 4 ]; |
|
76 | + $v[ 3 ] = (int)$v[ 3 ] ^ (int)$v[ 5 ]; |
|
77 | 77 | |
78 | 78 | # v2=ROTL(v2,32) |
79 | - list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32); |
|
79 | + list( $v[ 4 ], $v[ 5 ] ) = self::rotl_64( (int)$v[ 4 ], (int)$v[ 5 ], 32 ); |
|
80 | 80 | |
81 | 81 | return $v; |
82 | 82 | } |
@@ -90,14 +90,14 @@ discard block |
||
90 | 90 | * @param int[] $b |
91 | 91 | * @return array<int, mixed> |
92 | 92 | */ |
93 | - public static function add(array $a, array $b) |
|
93 | + public static function add( array $a, array $b ) |
|
94 | 94 | { |
95 | 95 | /** @var int $x1 */ |
96 | - $x1 = $a[1] + $b[1]; |
|
96 | + $x1 = $a[ 1 ] + $b[ 1 ]; |
|
97 | 97 | /** @var int $c */ |
98 | 98 | $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff |
99 | 99 | /** @var int $x0 */ |
100 | - $x0 = $a[0] + $b[0] + $c; |
|
100 | + $x0 = $a[ 0 ] + $b[ 0 ] + $c; |
|
101 | 101 | return array( |
102 | 102 | $x0 & 0xffffffff, |
103 | 103 | $x1 & 0xffffffff |
@@ -112,33 +112,33 @@ discard block |
||
112 | 112 | * @param int $c |
113 | 113 | * @return array<int, mixed> |
114 | 114 | */ |
115 | - public static function rotl_64($int0, $int1, $c) |
|
115 | + public static function rotl_64( $int0, $int1, $c ) |
|
116 | 116 | { |
117 | 117 | $int0 &= 0xffffffff; |
118 | 118 | $int1 &= 0xffffffff; |
119 | 119 | $c &= 63; |
120 | - if ($c === 32) { |
|
121 | - return array($int1, $int0); |
|
120 | + if ( $c === 32 ) { |
|
121 | + return array( $int1, $int0 ); |
|
122 | 122 | } |
123 | - if ($c > 31) { |
|
123 | + if ( $c > 31 ) { |
|
124 | 124 | $tmp = $int1; |
125 | 125 | $int1 = $int0; |
126 | 126 | $int0 = $tmp; |
127 | 127 | $c &= 31; |
128 | 128 | } |
129 | - if ($c === 0) { |
|
130 | - return array($int0, $int1); |
|
129 | + if ( $c === 0 ) { |
|
130 | + return array( $int0, $int1 ); |
|
131 | 131 | } |
132 | 132 | return array( |
133 | 133 | 0xffffffff & ( |
134 | - ($int0 << $c) |
|
134 | + ( $int0 << $c ) |
|
135 | 135 | | |
136 | - ($int1 >> (32 - $c)) |
|
136 | + ( $int1 >> ( 32 - $c ) ) |
|
137 | 137 | ), |
138 | 138 | 0xffffffff & ( |
139 | - ($int1 << $c) |
|
139 | + ( $int1 << $c ) |
|
140 | 140 | | |
141 | - ($int0 >> (32 - $c)) |
|
141 | + ( $int0 >> ( 32 - $c ) ) |
|
142 | 142 | ), |
143 | 143 | ); |
144 | 144 | } |
@@ -160,9 +160,9 @@ discard block |
||
160 | 160 | * @throws SodiumException |
161 | 161 | * @throws TypeError |
162 | 162 | */ |
163 | - public static function sipHash24($in, $key) |
|
163 | + public static function sipHash24( $in, $key ) |
|
164 | 164 | { |
165 | - $inlen = self::strlen($in); |
|
165 | + $inlen = self::strlen( $in ); |
|
166 | 166 | |
167 | 167 | # /* "somepseudorandomlygeneratedbytes" */ |
168 | 168 | # u64 v0 = 0x736f6d6570736575ULL; |
@@ -187,10 +187,10 @@ discard block |
||
187 | 187 | # u64 k0 = LOAD64_LE( k ); |
188 | 188 | # u64 k1 = LOAD64_LE( k + 8 ); |
189 | 189 | $k = array( |
190 | - self::load_4(self::substr($key, 4, 4)), |
|
191 | - self::load_4(self::substr($key, 0, 4)), |
|
192 | - self::load_4(self::substr($key, 12, 4)), |
|
193 | - self::load_4(self::substr($key, 8, 4)) |
|
190 | + self::load_4( self::substr( $key, 4, 4 ) ), |
|
191 | + self::load_4( self::substr( $key, 0, 4 ) ), |
|
192 | + self::load_4( self::substr( $key, 12, 4 ) ), |
|
193 | + self::load_4( self::substr( $key, 8, 4 ) ) |
|
194 | 194 | ); |
195 | 195 | // k0 => $k[0], $k[1] |
196 | 196 | // k1 => $k[2], $k[3] |
@@ -203,41 +203,41 @@ discard block |
||
203 | 203 | // See docblock for why the 0th index gets the higher bits. |
204 | 204 | |
205 | 205 | # v3 ^= k1; |
206 | - $v[6] ^= $k[2]; |
|
207 | - $v[7] ^= $k[3]; |
|
206 | + $v[ 6 ] ^= $k[ 2 ]; |
|
207 | + $v[ 7 ] ^= $k[ 3 ]; |
|
208 | 208 | # v2 ^= k0; |
209 | - $v[4] ^= $k[0]; |
|
210 | - $v[5] ^= $k[1]; |
|
209 | + $v[ 4 ] ^= $k[ 0 ]; |
|
210 | + $v[ 5 ] ^= $k[ 1 ]; |
|
211 | 211 | # v1 ^= k1; |
212 | - $v[2] ^= $k[2]; |
|
213 | - $v[3] ^= $k[3]; |
|
212 | + $v[ 2 ] ^= $k[ 2 ]; |
|
213 | + $v[ 3 ] ^= $k[ 3 ]; |
|
214 | 214 | # v0 ^= k0; |
215 | - $v[0] ^= $k[0]; |
|
216 | - $v[1] ^= $k[1]; |
|
215 | + $v[ 0 ] ^= $k[ 0 ]; |
|
216 | + $v[ 1 ] ^= $k[ 1 ]; |
|
217 | 217 | |
218 | 218 | $left = $inlen; |
219 | 219 | # for ( ; in != end; in += 8 ) |
220 | - while ($left >= 8) { |
|
220 | + while ( $left >= 8 ) { |
|
221 | 221 | # m = LOAD64_LE( in ); |
222 | 222 | $m = array( |
223 | - self::load_4(self::substr($in, 4, 4)), |
|
224 | - self::load_4(self::substr($in, 0, 4)) |
|
223 | + self::load_4( self::substr( $in, 4, 4 ) ), |
|
224 | + self::load_4( self::substr( $in, 0, 4 ) ) |
|
225 | 225 | ); |
226 | 226 | |
227 | 227 | # v3 ^= m; |
228 | - $v[6] ^= $m[0]; |
|
229 | - $v[7] ^= $m[1]; |
|
228 | + $v[ 6 ] ^= $m[ 0 ]; |
|
229 | + $v[ 7 ] ^= $m[ 1 ]; |
|
230 | 230 | |
231 | 231 | # SIPROUND; |
232 | 232 | # SIPROUND; |
233 | - $v = self::sipRound($v); |
|
234 | - $v = self::sipRound($v); |
|
233 | + $v = self::sipRound( $v ); |
|
234 | + $v = self::sipRound( $v ); |
|
235 | 235 | |
236 | 236 | # v0 ^= m; |
237 | - $v[0] ^= $m[0]; |
|
238 | - $v[1] ^= $m[1]; |
|
237 | + $v[ 0 ] ^= $m[ 0 ]; |
|
238 | + $v[ 1 ] ^= $m[ 1 ]; |
|
239 | 239 | |
240 | - $in = self::substr($in, 8); |
|
240 | + $in = self::substr( $in, 8 ); |
|
241 | 241 | $left -= 8; |
242 | 242 | } |
243 | 243 | |
@@ -252,55 +252,55 @@ discard block |
||
252 | 252 | # case 1: b |= ( ( u64 )in[ 0] ); break; |
253 | 253 | # case 0: break; |
254 | 254 | # } |
255 | - switch ($left) { |
|
255 | + switch ( $left ) { |
|
256 | 256 | case 7: |
257 | - $b[0] |= self::chrToInt($in[6]) << 16; |
|
257 | + $b[ 0 ] |= self::chrToInt( $in[ 6 ] ) << 16; |
|
258 | 258 | case 6: |
259 | - $b[0] |= self::chrToInt($in[5]) << 8; |
|
259 | + $b[ 0 ] |= self::chrToInt( $in[ 5 ] ) << 8; |
|
260 | 260 | case 5: |
261 | - $b[0] |= self::chrToInt($in[4]); |
|
261 | + $b[ 0 ] |= self::chrToInt( $in[ 4 ] ); |
|
262 | 262 | case 4: |
263 | - $b[1] |= self::chrToInt($in[3]) << 24; |
|
263 | + $b[ 1 ] |= self::chrToInt( $in[ 3 ] ) << 24; |
|
264 | 264 | case 3: |
265 | - $b[1] |= self::chrToInt($in[2]) << 16; |
|
265 | + $b[ 1 ] |= self::chrToInt( $in[ 2 ] ) << 16; |
|
266 | 266 | case 2: |
267 | - $b[1] |= self::chrToInt($in[1]) << 8; |
|
267 | + $b[ 1 ] |= self::chrToInt( $in[ 1 ] ) << 8; |
|
268 | 268 | case 1: |
269 | - $b[1] |= self::chrToInt($in[0]); |
|
269 | + $b[ 1 ] |= self::chrToInt( $in[ 0 ] ); |
|
270 | 270 | case 0: |
271 | 271 | break; |
272 | 272 | } |
273 | 273 | // See docblock for why the 0th index gets the higher bits. |
274 | 274 | |
275 | 275 | # v3 ^= b; |
276 | - $v[6] ^= $b[0]; |
|
277 | - $v[7] ^= $b[1]; |
|
276 | + $v[ 6 ] ^= $b[ 0 ]; |
|
277 | + $v[ 7 ] ^= $b[ 1 ]; |
|
278 | 278 | |
279 | 279 | # SIPROUND; |
280 | 280 | # SIPROUND; |
281 | - $v = self::sipRound($v); |
|
282 | - $v = self::sipRound($v); |
|
281 | + $v = self::sipRound( $v ); |
|
282 | + $v = self::sipRound( $v ); |
|
283 | 283 | |
284 | 284 | # v0 ^= b; |
285 | - $v[0] ^= $b[0]; |
|
286 | - $v[1] ^= $b[1]; |
|
285 | + $v[ 0 ] ^= $b[ 0 ]; |
|
286 | + $v[ 1 ] ^= $b[ 1 ]; |
|
287 | 287 | |
288 | 288 | // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
289 | 289 | # v2 ^= 0xff; |
290 | - $v[5] ^= 0xff; |
|
290 | + $v[ 5 ] ^= 0xff; |
|
291 | 291 | |
292 | 292 | # SIPROUND; |
293 | 293 | # SIPROUND; |
294 | 294 | # SIPROUND; |
295 | 295 | # SIPROUND; |
296 | - $v = self::sipRound($v); |
|
297 | - $v = self::sipRound($v); |
|
298 | - $v = self::sipRound($v); |
|
299 | - $v = self::sipRound($v); |
|
296 | + $v = self::sipRound( $v ); |
|
297 | + $v = self::sipRound( $v ); |
|
298 | + $v = self::sipRound( $v ); |
|
299 | + $v = self::sipRound( $v ); |
|
300 | 300 | |
301 | 301 | # b = v0 ^ v1 ^ v2 ^ v3; |
302 | 302 | # STORE64_LE( out, b ); |
303 | - return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) . |
|
304 | - self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]); |
|
303 | + return self::store32_le( $v[ 1 ] ^ $v[ 3 ] ^ $v[ 5 ] ^ $v[ 7 ] ) . |
|
304 | + self::store32_le( $v[ 0 ] ^ $v[ 2 ] ^ $v[ 4 ] ^ $v[ 6 ] ); |
|
305 | 305 | } |
306 | 306 | } |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | * |
10 | 10 | * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers |
11 | 11 | */ |
12 | -class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util |
|
13 | -{ |
|
12 | +class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util { |
|
14 | 13 | /** |
15 | 14 | * @internal You should not use this directly from another application |
16 | 15 | * |
@@ -18,8 +17,7 @@ discard block |
||
18 | 17 | * @return int[] |
19 | 18 | * |
20 | 19 | */ |
21 | - public static function sipRound(array $v) |
|
22 | - { |
|
20 | + public static function sipRound(array $v) { |
|
23 | 21 | # v0 += v1; |
24 | 22 | list($v[0], $v[1]) = self::add( |
25 | 23 | array($v[0], $v[1]), |
@@ -90,8 +88,7 @@ discard block |
||
90 | 88 | * @param int[] $b |
91 | 89 | * @return array<int, mixed> |
92 | 90 | */ |
93 | - public static function add(array $a, array $b) |
|
94 | - { |
|
91 | + public static function add(array $a, array $b) { |
|
95 | 92 | /** @var int $x1 */ |
96 | 93 | $x1 = $a[1] + $b[1]; |
97 | 94 | /** @var int $c */ |
@@ -112,8 +109,7 @@ discard block |
||
112 | 109 | * @param int $c |
113 | 110 | * @return array<int, mixed> |
114 | 111 | */ |
115 | - public static function rotl_64($int0, $int1, $c) |
|
116 | - { |
|
112 | + public static function rotl_64($int0, $int1, $c) { |
|
117 | 113 | $int0 &= 0xffffffff; |
118 | 114 | $int1 &= 0xffffffff; |
119 | 115 | $c &= 63; |
@@ -160,8 +156,7 @@ discard block |
||
160 | 156 | * @throws SodiumException |
161 | 157 | * @throws TypeError |
162 | 158 | */ |
163 | - public static function sipHash24($in, $key) |
|
164 | - { |
|
159 | + public static function sipHash24($in, $key) { |
|
165 | 160 | $inlen = self::strlen($in); |
166 | 161 | |
167 | 162 | # /* "somepseudorandomlygeneratedbytes" */ |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,109 +9,109 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20 |
11 | 11 | { |
12 | - /** |
|
13 | - * @internal You should not use this directly from another application |
|
14 | - * |
|
15 | - * @param int $len |
|
16 | - * @param string $nonce |
|
17 | - * @param string $key |
|
18 | - * @return string |
|
19 | - * @throws SodiumException |
|
20 | - * @throws TypeError |
|
21 | - */ |
|
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | - { |
|
24 | - if (self::strlen($nonce) !== 24) { |
|
25 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
26 | - } |
|
27 | - return self::encryptBytes( |
|
28 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
|
29 | - self::hChaCha20( |
|
30 | - self::substr($nonce, 0, 16), |
|
31 | - $key |
|
32 | - ), |
|
33 | - self::substr($nonce, 16, 8) |
|
34 | - ), |
|
35 | - str_repeat("\x00", $len) |
|
36 | - ); |
|
37 | - } |
|
12 | + /** |
|
13 | + * @internal You should not use this directly from another application |
|
14 | + * |
|
15 | + * @param int $len |
|
16 | + * @param string $nonce |
|
17 | + * @param string $key |
|
18 | + * @return string |
|
19 | + * @throws SodiumException |
|
20 | + * @throws TypeError |
|
21 | + */ |
|
22 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | + { |
|
24 | + if (self::strlen($nonce) !== 24) { |
|
25 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
26 | + } |
|
27 | + return self::encryptBytes( |
|
28 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
|
29 | + self::hChaCha20( |
|
30 | + self::substr($nonce, 0, 16), |
|
31 | + $key |
|
32 | + ), |
|
33 | + self::substr($nonce, 16, 8) |
|
34 | + ), |
|
35 | + str_repeat("\x00", $len) |
|
36 | + ); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @internal You should not use this directly from another application |
|
41 | - * |
|
42 | - * @param int $len |
|
43 | - * @param string $nonce |
|
44 | - * @param string $key |
|
45 | - * @return string |
|
46 | - * @throws SodiumException |
|
47 | - * @throws TypeError |
|
48 | - */ |
|
49 | - public static function ietfStream($len = 64, $nonce = '', $key = '') |
|
50 | - { |
|
51 | - if (self::strlen($nonce) !== 24) { |
|
52 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
53 | - } |
|
54 | - return self::encryptBytes( |
|
55 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
|
56 | - self::hChaCha20( |
|
57 | - self::substr($nonce, 0, 16), |
|
58 | - $key |
|
59 | - ), |
|
60 | - "\x00\x00\x00\x00" . self::substr($nonce, 16, 8) |
|
61 | - ), |
|
62 | - str_repeat("\x00", $len) |
|
63 | - ); |
|
64 | - } |
|
39 | + /** |
|
40 | + * @internal You should not use this directly from another application |
|
41 | + * |
|
42 | + * @param int $len |
|
43 | + * @param string $nonce |
|
44 | + * @param string $key |
|
45 | + * @return string |
|
46 | + * @throws SodiumException |
|
47 | + * @throws TypeError |
|
48 | + */ |
|
49 | + public static function ietfStream($len = 64, $nonce = '', $key = '') |
|
50 | + { |
|
51 | + if (self::strlen($nonce) !== 24) { |
|
52 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
53 | + } |
|
54 | + return self::encryptBytes( |
|
55 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
|
56 | + self::hChaCha20( |
|
57 | + self::substr($nonce, 0, 16), |
|
58 | + $key |
|
59 | + ), |
|
60 | + "\x00\x00\x00\x00" . self::substr($nonce, 16, 8) |
|
61 | + ), |
|
62 | + str_repeat("\x00", $len) |
|
63 | + ); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @internal You should not use this directly from another application |
|
68 | - * |
|
69 | - * @param string $message |
|
70 | - * @param string $nonce |
|
71 | - * @param string $key |
|
72 | - * @param string $ic |
|
73 | - * @return string |
|
74 | - * @throws SodiumException |
|
75 | - * @throws TypeError |
|
76 | - */ |
|
77 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
78 | - { |
|
79 | - if (self::strlen($nonce) !== 24) { |
|
80 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
81 | - } |
|
82 | - return self::encryptBytes( |
|
83 | - new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
|
84 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
85 | - self::substr($nonce, 16, 8), |
|
86 | - $ic |
|
87 | - ), |
|
88 | - $message |
|
89 | - ); |
|
90 | - } |
|
66 | + /** |
|
67 | + * @internal You should not use this directly from another application |
|
68 | + * |
|
69 | + * @param string $message |
|
70 | + * @param string $nonce |
|
71 | + * @param string $key |
|
72 | + * @param string $ic |
|
73 | + * @return string |
|
74 | + * @throws SodiumException |
|
75 | + * @throws TypeError |
|
76 | + */ |
|
77 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
78 | + { |
|
79 | + if (self::strlen($nonce) !== 24) { |
|
80 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
81 | + } |
|
82 | + return self::encryptBytes( |
|
83 | + new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
|
84 | + self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
85 | + self::substr($nonce, 16, 8), |
|
86 | + $ic |
|
87 | + ), |
|
88 | + $message |
|
89 | + ); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @internal You should not use this directly from another application |
|
94 | - * |
|
95 | - * @param string $message |
|
96 | - * @param string $nonce |
|
97 | - * @param string $key |
|
98 | - * @param string $ic |
|
99 | - * @return string |
|
100 | - * @throws SodiumException |
|
101 | - * @throws TypeError |
|
102 | - */ |
|
103 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
104 | - { |
|
105 | - if (self::strlen($nonce) !== 24) { |
|
106 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
107 | - } |
|
108 | - return self::encryptBytes( |
|
109 | - new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
|
110 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
111 | - "\x00\x00\x00\x00" . self::substr($nonce, 16, 8), |
|
112 | - $ic |
|
113 | - ), |
|
114 | - $message |
|
115 | - ); |
|
116 | - } |
|
92 | + /** |
|
93 | + * @internal You should not use this directly from another application |
|
94 | + * |
|
95 | + * @param string $message |
|
96 | + * @param string $nonce |
|
97 | + * @param string $key |
|
98 | + * @param string $ic |
|
99 | + * @return string |
|
100 | + * @throws SodiumException |
|
101 | + * @throws TypeError |
|
102 | + */ |
|
103 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
104 | + { |
|
105 | + if (self::strlen($nonce) !== 24) { |
|
106 | + throw new SodiumException('Nonce must be 24 bytes long'); |
|
107 | + } |
|
108 | + return self::encryptBytes( |
|
109 | + new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
|
110 | + self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
111 | + "\x00\x00\x00\x00" . self::substr($nonce, 16, 8), |
|
112 | + $ic |
|
113 | + ), |
|
114 | + $message |
|
115 | + ); |
|
116 | + } |
|
117 | 117 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) { |
|
3 | +if ( class_exists( 'ParagonIE_Sodium_Core_XChaCha20', false ) ) { |
|
4 | 4 | return; |
5 | 5 | } |
6 | 6 | |
@@ -19,20 +19,20 @@ discard block |
||
19 | 19 | * @throws SodiumException |
20 | 20 | * @throws TypeError |
21 | 21 | */ |
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
22 | + public static function stream( $len = 64, $nonce = '', $key = '' ) |
|
23 | 23 | { |
24 | - if (self::strlen($nonce) !== 24) { |
|
25 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
24 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
25 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
26 | 26 | } |
27 | 27 | return self::encryptBytes( |
28 | 28 | new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
29 | 29 | self::hChaCha20( |
30 | - self::substr($nonce, 0, 16), |
|
30 | + self::substr( $nonce, 0, 16 ), |
|
31 | 31 | $key |
32 | 32 | ), |
33 | - self::substr($nonce, 16, 8) |
|
33 | + self::substr( $nonce, 16, 8 ) |
|
34 | 34 | ), |
35 | - str_repeat("\x00", $len) |
|
35 | + str_repeat( "\x00", $len ) |
|
36 | 36 | ); |
37 | 37 | } |
38 | 38 | |
@@ -46,20 +46,20 @@ discard block |
||
46 | 46 | * @throws SodiumException |
47 | 47 | * @throws TypeError |
48 | 48 | */ |
49 | - public static function ietfStream($len = 64, $nonce = '', $key = '') |
|
49 | + public static function ietfStream( $len = 64, $nonce = '', $key = '' ) |
|
50 | 50 | { |
51 | - if (self::strlen($nonce) !== 24) { |
|
52 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
51 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
52 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
53 | 53 | } |
54 | 54 | return self::encryptBytes( |
55 | 55 | new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
56 | 56 | self::hChaCha20( |
57 | - self::substr($nonce, 0, 16), |
|
57 | + self::substr( $nonce, 0, 16 ), |
|
58 | 58 | $key |
59 | 59 | ), |
60 | - "\x00\x00\x00\x00" . self::substr($nonce, 16, 8) |
|
60 | + "\x00\x00\x00\x00" . self::substr( $nonce, 16, 8 ) |
|
61 | 61 | ), |
62 | - str_repeat("\x00", $len) |
|
62 | + str_repeat( "\x00", $len ) |
|
63 | 63 | ); |
64 | 64 | } |
65 | 65 | |
@@ -74,15 +74,15 @@ discard block |
||
74 | 74 | * @throws SodiumException |
75 | 75 | * @throws TypeError |
76 | 76 | */ |
77 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
77 | + public static function streamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
78 | 78 | { |
79 | - if (self::strlen($nonce) !== 24) { |
|
80 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
79 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
80 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
81 | 81 | } |
82 | 82 | return self::encryptBytes( |
83 | 83 | new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
84 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
85 | - self::substr($nonce, 16, 8), |
|
84 | + self::hChaCha20( self::substr( $nonce, 0, 16 ), $key ), |
|
85 | + self::substr( $nonce, 16, 8 ), |
|
86 | 86 | $ic |
87 | 87 | ), |
88 | 88 | $message |
@@ -100,15 +100,15 @@ discard block |
||
100 | 100 | * @throws SodiumException |
101 | 101 | * @throws TypeError |
102 | 102 | */ |
103 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
103 | + public static function ietfStreamXorIc( $message, $nonce = '', $key = '', $ic = '' ) |
|
104 | 104 | { |
105 | - if (self::strlen($nonce) !== 24) { |
|
106 | - throw new SodiumException('Nonce must be 24 bytes long'); |
|
105 | + if ( self::strlen( $nonce ) !== 24 ) { |
|
106 | + throw new SodiumException( 'Nonce must be 24 bytes long' ); |
|
107 | 107 | } |
108 | 108 | return self::encryptBytes( |
109 | 109 | new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
110 | - self::hChaCha20(self::substr($nonce, 0, 16), $key), |
|
111 | - "\x00\x00\x00\x00" . self::substr($nonce, 16, 8), |
|
110 | + self::hChaCha20( self::substr( $nonce, 0, 16 ), $key ), |
|
111 | + "\x00\x00\x00\x00" . self::substr( $nonce, 16, 8 ), |
|
112 | 112 | $ic |
113 | 113 | ), |
114 | 114 | $message |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | /** |
8 | 8 | * Class ParagonIE_Sodium_Core_XChaCha20 |
9 | 9 | */ |
10 | -class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20 |
|
11 | -{ |
|
10 | +class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20 { |
|
12 | 11 | /** |
13 | 12 | * @internal You should not use this directly from another application |
14 | 13 | * |
@@ -19,8 +18,7 @@ discard block |
||
19 | 18 | * @throws SodiumException |
20 | 19 | * @throws TypeError |
21 | 20 | */ |
22 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
23 | - { |
|
21 | + public static function stream($len = 64, $nonce = '', $key = '') { |
|
24 | 22 | if (self::strlen($nonce) !== 24) { |
25 | 23 | throw new SodiumException('Nonce must be 24 bytes long'); |
26 | 24 | } |
@@ -46,8 +44,7 @@ discard block |
||
46 | 44 | * @throws SodiumException |
47 | 45 | * @throws TypeError |
48 | 46 | */ |
49 | - public static function ietfStream($len = 64, $nonce = '', $key = '') |
|
50 | - { |
|
47 | + public static function ietfStream($len = 64, $nonce = '', $key = '') { |
|
51 | 48 | if (self::strlen($nonce) !== 24) { |
52 | 49 | throw new SodiumException('Nonce must be 24 bytes long'); |
53 | 50 | } |
@@ -74,8 +71,7 @@ discard block |
||
74 | 71 | * @throws SodiumException |
75 | 72 | * @throws TypeError |
76 | 73 | */ |
77 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
78 | - { |
|
74 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
79 | 75 | if (self::strlen($nonce) !== 24) { |
80 | 76 | throw new SodiumException('Nonce must be 24 bytes long'); |
81 | 77 | } |
@@ -100,8 +96,7 @@ discard block |
||
100 | 96 | * @throws SodiumException |
101 | 97 | * @throws TypeError |
102 | 98 | */ |
103 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
104 | - { |
|
99 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') { |
|
105 | 100 | if (self::strlen($nonce) !== 24) { |
106 | 101 | throw new SodiumException('Nonce must be 24 bytes long'); |
107 | 102 | } |
@@ -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 | } |