@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_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_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $len |
|
18 | - * @param string $nonce |
|
19 | - * @param string $key |
|
20 | - * @return string |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - */ |
|
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
26 | - $ret = self::salsa20( |
|
27 | - $len, |
|
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
30 | - ); |
|
31 | - return $ret; |
|
32 | - } |
|
12 | + /** |
|
13 | + * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $len |
|
18 | + * @param string $nonce |
|
19 | + * @param string $key |
|
20 | + * @return string |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + */ |
|
24 | + public static function xsalsa20($len, $nonce, $key) |
|
25 | + { |
|
26 | + $ret = self::salsa20( |
|
27 | + $len, |
|
28 | + self::substr($nonce, 16, 8), |
|
29 | + self::hsalsa20($nonce, $key) |
|
30 | + ); |
|
31 | + return $ret; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | - * |
|
37 | - * @internal You should not use this directly from another application |
|
38 | - * |
|
39 | - * @param string $message |
|
40 | - * @param string $nonce |
|
41 | - * @param string $key |
|
42 | - * @return string |
|
43 | - * @throws SodiumException |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
48 | - return self::xorStrings( |
|
49 | - $message, |
|
50 | - self::xsalsa20( |
|
51 | - self::strlen($message), |
|
52 | - $nonce, |
|
53 | - $key |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
34 | + /** |
|
35 | + * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | + * |
|
37 | + * @internal You should not use this directly from another application |
|
38 | + * |
|
39 | + * @param string $message |
|
40 | + * @param string $nonce |
|
41 | + * @param string $key |
|
42 | + * @return string |
|
43 | + * @throws SodiumException |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | + { |
|
48 | + return self::xorStrings( |
|
49 | + $message, |
|
50 | + self::xsalsa20( |
|
51 | + self::strlen($message), |
|
52 | + $nonce, |
|
53 | + $key |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,83 +9,83 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | - * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | - * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | - * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | - * @throws SodiumException |
|
23 | - * @throws TypeError |
|
24 | - */ |
|
25 | - protected static function quarterRound( |
|
26 | - ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | - ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | - ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | - ParagonIE_Sodium_Core32_Int32 $d |
|
30 | - ) { |
|
31 | - /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | - /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | - /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | - /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
12 | + /** |
|
13 | + * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | + * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | + * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | + * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | + * @throws SodiumException |
|
23 | + * @throws TypeError |
|
24 | + */ |
|
25 | + protected static function quarterRound( |
|
26 | + ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | + ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | + ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | + ParagonIE_Sodium_Core32_Int32 $d |
|
30 | + ) { |
|
31 | + /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | + /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | + /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | + /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
35 | 35 | |
36 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | - $a = $a->addInt32($b); |
|
38 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
36 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | + $a = $a->addInt32($b); |
|
38 | + $d = $d->xorInt32($a)->rotateLeft(16); |
|
39 | 39 | |
40 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | - $c = $c->addInt32($d); |
|
42 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
40 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | + $c = $c->addInt32($d); |
|
42 | + $b = $b->xorInt32($c)->rotateLeft(12); |
|
43 | 43 | |
44 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | - $a = $a->addInt32($b); |
|
46 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
44 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | + $a = $a->addInt32($b); |
|
46 | + $d = $d->xorInt32($a)->rotateLeft(8); |
|
47 | 47 | |
48 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | - $c = $c->addInt32($d); |
|
50 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
48 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | + $c = $c->addInt32($d); |
|
50 | + $b = $b->xorInt32($c)->rotateLeft(7); |
|
51 | 51 | |
52 | - return array($a, $b, $c, $d); |
|
53 | - } |
|
52 | + return array($a, $b, $c, $d); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @internal You should not use this directly from another application |
|
57 | - * |
|
58 | - * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | - * @param string $message |
|
60 | - * |
|
61 | - * @return string |
|
62 | - * @throws SodiumException |
|
63 | - * @throws TypeError |
|
64 | - */ |
|
65 | - public static function encryptBytes( |
|
66 | - ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | - $message = '' |
|
68 | - ) { |
|
69 | - $bytes = self::strlen($message); |
|
55 | + /** |
|
56 | + * @internal You should not use this directly from another application |
|
57 | + * |
|
58 | + * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | + * @param string $message |
|
60 | + * |
|
61 | + * @return string |
|
62 | + * @throws SodiumException |
|
63 | + * @throws TypeError |
|
64 | + */ |
|
65 | + public static function encryptBytes( |
|
66 | + ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | + $message = '' |
|
68 | + ) { |
|
69 | + $bytes = self::strlen($message); |
|
70 | 70 | |
71 | - /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | - /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | - /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | - /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | - /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | - /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | - /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | - /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | - /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | - /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | - /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | - /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | - /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | - /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | - /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | - /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
71 | + /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | + /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | + /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | + /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | + /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | + /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | + /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | + /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | + /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | + /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | + /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | + /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | + /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | + /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | + /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | + /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
87 | 87 | |
88 | - /* |
|
88 | + /* |
|
89 | 89 | j0 = ctx->input[0]; |
90 | 90 | j1 = ctx->input[1]; |
91 | 91 | j2 = ctx->input[2]; |
@@ -103,89 +103,89 @@ discard block |
||
103 | 103 | j14 = ctx->input[14]; |
104 | 104 | j15 = ctx->input[15]; |
105 | 105 | */ |
106 | - /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | - $j0 = $ctx[0]; |
|
108 | - /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | - $j1 = $ctx[1]; |
|
110 | - /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | - $j2 = $ctx[2]; |
|
112 | - /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | - $j3 = $ctx[3]; |
|
114 | - /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | - $j4 = $ctx[4]; |
|
116 | - /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | - $j5 = $ctx[5]; |
|
118 | - /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | - $j6 = $ctx[6]; |
|
120 | - /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | - $j7 = $ctx[7]; |
|
122 | - /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | - $j8 = $ctx[8]; |
|
124 | - /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | - $j9 = $ctx[9]; |
|
126 | - /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | - $j10 = $ctx[10]; |
|
128 | - /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | - $j11 = $ctx[11]; |
|
130 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | - $j12 = $ctx[12]; |
|
132 | - /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | - $j13 = $ctx[13]; |
|
134 | - /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | - $j14 = $ctx[14]; |
|
136 | - /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | - $j15 = $ctx[15]; |
|
106 | + /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | + $j0 = $ctx[0]; |
|
108 | + /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | + $j1 = $ctx[1]; |
|
110 | + /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | + $j2 = $ctx[2]; |
|
112 | + /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | + $j3 = $ctx[3]; |
|
114 | + /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | + $j4 = $ctx[4]; |
|
116 | + /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | + $j5 = $ctx[5]; |
|
118 | + /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | + $j6 = $ctx[6]; |
|
120 | + /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | + $j7 = $ctx[7]; |
|
122 | + /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | + $j8 = $ctx[8]; |
|
124 | + /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | + $j9 = $ctx[9]; |
|
126 | + /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | + $j10 = $ctx[10]; |
|
128 | + /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | + $j11 = $ctx[11]; |
|
130 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | + $j12 = $ctx[12]; |
|
132 | + /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | + $j13 = $ctx[13]; |
|
134 | + /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | + $j14 = $ctx[14]; |
|
136 | + /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | + $j15 = $ctx[15]; |
|
138 | 138 | |
139 | - $c = ''; |
|
140 | - for (;;) { |
|
141 | - if ($bytes < 64) { |
|
142 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | - } |
|
139 | + $c = ''; |
|
140 | + for (;;) { |
|
141 | + if ($bytes < 64) { |
|
142 | + $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | + } |
|
144 | 144 | |
145 | - $x0 = clone $j0; |
|
146 | - $x1 = clone $j1; |
|
147 | - $x2 = clone $j2; |
|
148 | - $x3 = clone $j3; |
|
149 | - $x4 = clone $j4; |
|
150 | - $x5 = clone $j5; |
|
151 | - $x6 = clone $j6; |
|
152 | - $x7 = clone $j7; |
|
153 | - $x8 = clone $j8; |
|
154 | - $x9 = clone $j9; |
|
155 | - $x10 = clone $j10; |
|
156 | - $x11 = clone $j11; |
|
157 | - $x12 = clone $j12; |
|
158 | - $x13 = clone $j13; |
|
159 | - $x14 = clone $j14; |
|
160 | - $x15 = clone $j15; |
|
145 | + $x0 = clone $j0; |
|
146 | + $x1 = clone $j1; |
|
147 | + $x2 = clone $j2; |
|
148 | + $x3 = clone $j3; |
|
149 | + $x4 = clone $j4; |
|
150 | + $x5 = clone $j5; |
|
151 | + $x6 = clone $j6; |
|
152 | + $x7 = clone $j7; |
|
153 | + $x8 = clone $j8; |
|
154 | + $x9 = clone $j9; |
|
155 | + $x10 = clone $j10; |
|
156 | + $x11 = clone $j11; |
|
157 | + $x12 = clone $j12; |
|
158 | + $x13 = clone $j13; |
|
159 | + $x14 = clone $j14; |
|
160 | + $x15 = clone $j15; |
|
161 | 161 | |
162 | - # for (i = 20; i > 0; i -= 2) { |
|
163 | - for ($i = 20; $i > 0; $i -= 2) { |
|
164 | - # QUARTERROUND( x0, x4, x8, x12) |
|
165 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
162 | + # for (i = 20; i > 0; i -= 2) { |
|
163 | + for ($i = 20; $i > 0; $i -= 2) { |
|
164 | + # QUARTERROUND( x0, x4, x8, x12) |
|
165 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
166 | 166 | |
167 | - # QUARTERROUND( x1, x5, x9, x13) |
|
168 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
167 | + # QUARTERROUND( x1, x5, x9, x13) |
|
168 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
169 | 169 | |
170 | - # QUARTERROUND( x2, x6, x10, x14) |
|
171 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
170 | + # QUARTERROUND( x2, x6, x10, x14) |
|
171 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
172 | 172 | |
173 | - # QUARTERROUND( x3, x7, x11, x15) |
|
174 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
173 | + # QUARTERROUND( x3, x7, x11, x15) |
|
174 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
175 | 175 | |
176 | - # QUARTERROUND( x0, x5, x10, x15) |
|
177 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
176 | + # QUARTERROUND( x0, x5, x10, x15) |
|
177 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
178 | 178 | |
179 | - # QUARTERROUND( x1, x6, x11, x12) |
|
180 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
179 | + # QUARTERROUND( x1, x6, x11, x12) |
|
180 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
181 | 181 | |
182 | - # QUARTERROUND( x2, x7, x8, x13) |
|
183 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
182 | + # QUARTERROUND( x2, x7, x8, x13) |
|
183 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
184 | 184 | |
185 | - # QUARTERROUND( x3, x4, x9, x14) |
|
186 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | - } |
|
188 | - /* |
|
185 | + # QUARTERROUND( x3, x4, x9, x14) |
|
186 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | + } |
|
188 | + /* |
|
189 | 189 | x0 = PLUS(x0, j0); |
190 | 190 | x1 = PLUS(x1, j1); |
191 | 191 | x2 = PLUS(x2, j2); |
@@ -203,24 +203,24 @@ discard block |
||
203 | 203 | x14 = PLUS(x14, j14); |
204 | 204 | x15 = PLUS(x15, j15); |
205 | 205 | */ |
206 | - $x0 = $x0->addInt32($j0); |
|
207 | - $x1 = $x1->addInt32($j1); |
|
208 | - $x2 = $x2->addInt32($j2); |
|
209 | - $x3 = $x3->addInt32($j3); |
|
210 | - $x4 = $x4->addInt32($j4); |
|
211 | - $x5 = $x5->addInt32($j5); |
|
212 | - $x6 = $x6->addInt32($j6); |
|
213 | - $x7 = $x7->addInt32($j7); |
|
214 | - $x8 = $x8->addInt32($j8); |
|
215 | - $x9 = $x9->addInt32($j9); |
|
216 | - $x10 = $x10->addInt32($j10); |
|
217 | - $x11 = $x11->addInt32($j11); |
|
218 | - $x12 = $x12->addInt32($j12); |
|
219 | - $x13 = $x13->addInt32($j13); |
|
220 | - $x14 = $x14->addInt32($j14); |
|
221 | - $x15 = $x15->addInt32($j15); |
|
206 | + $x0 = $x0->addInt32($j0); |
|
207 | + $x1 = $x1->addInt32($j1); |
|
208 | + $x2 = $x2->addInt32($j2); |
|
209 | + $x3 = $x3->addInt32($j3); |
|
210 | + $x4 = $x4->addInt32($j4); |
|
211 | + $x5 = $x5->addInt32($j5); |
|
212 | + $x6 = $x6->addInt32($j6); |
|
213 | + $x7 = $x7->addInt32($j7); |
|
214 | + $x8 = $x8->addInt32($j8); |
|
215 | + $x9 = $x9->addInt32($j9); |
|
216 | + $x10 = $x10->addInt32($j10); |
|
217 | + $x11 = $x11->addInt32($j11); |
|
218 | + $x12 = $x12->addInt32($j12); |
|
219 | + $x13 = $x13->addInt32($j13); |
|
220 | + $x14 = $x14->addInt32($j14); |
|
221 | + $x15 = $x15->addInt32($j15); |
|
222 | 222 | |
223 | - /* |
|
223 | + /* |
|
224 | 224 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
225 | 225 | x1 = XOR(x1, LOAD32_LE(m + 4)); |
226 | 226 | x2 = XOR(x2, LOAD32_LE(m + 8)); |
@@ -238,36 +238,36 @@ discard block |
||
238 | 238 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
239 | 239 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
240 | 240 | */ |
241 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
241 | + $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | + $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | + $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | + $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | + $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | + $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | + $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | + $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | + $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | + $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | + $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | + $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | + $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | + $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | + $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | + $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
257 | 257 | |
258 | - /* |
|
258 | + /* |
|
259 | 259 | j12 = PLUSONE(j12); |
260 | 260 | if (!j12) { |
261 | 261 | j13 = PLUSONE(j13); |
262 | 262 | } |
263 | 263 | */ |
264 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | - $j12 = $j12->addInt(1); |
|
266 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | - $j13 = $j13->addInt(1); |
|
268 | - } |
|
264 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | + $j12 = $j12->addInt(1); |
|
266 | + if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | + $j13 = $j13->addInt(1); |
|
268 | + } |
|
269 | 269 | |
270 | - /* |
|
270 | + /* |
|
271 | 271 | STORE32_LE(c + 0, x0); |
272 | 272 | STORE32_LE(c + 4, x1); |
273 | 273 | STORE32_LE(c + 8, x2); |
@@ -286,115 +286,115 @@ discard block |
||
286 | 286 | STORE32_LE(c + 60, x15); |
287 | 287 | */ |
288 | 288 | |
289 | - $block = $x0->toReverseString() . |
|
290 | - $x1->toReverseString() . |
|
291 | - $x2->toReverseString() . |
|
292 | - $x3->toReverseString() . |
|
293 | - $x4->toReverseString() . |
|
294 | - $x5->toReverseString() . |
|
295 | - $x6->toReverseString() . |
|
296 | - $x7->toReverseString() . |
|
297 | - $x8->toReverseString() . |
|
298 | - $x9->toReverseString() . |
|
299 | - $x10->toReverseString() . |
|
300 | - $x11->toReverseString() . |
|
301 | - $x12->toReverseString() . |
|
302 | - $x13->toReverseString() . |
|
303 | - $x14->toReverseString() . |
|
304 | - $x15->toReverseString(); |
|
289 | + $block = $x0->toReverseString() . |
|
290 | + $x1->toReverseString() . |
|
291 | + $x2->toReverseString() . |
|
292 | + $x3->toReverseString() . |
|
293 | + $x4->toReverseString() . |
|
294 | + $x5->toReverseString() . |
|
295 | + $x6->toReverseString() . |
|
296 | + $x7->toReverseString() . |
|
297 | + $x8->toReverseString() . |
|
298 | + $x9->toReverseString() . |
|
299 | + $x10->toReverseString() . |
|
300 | + $x11->toReverseString() . |
|
301 | + $x12->toReverseString() . |
|
302 | + $x13->toReverseString() . |
|
303 | + $x14->toReverseString() . |
|
304 | + $x15->toReverseString(); |
|
305 | 305 | |
306 | - /* Partial block */ |
|
307 | - if ($bytes < 64) { |
|
308 | - $c .= self::substr($block, 0, $bytes); |
|
309 | - break; |
|
310 | - } |
|
306 | + /* Partial block */ |
|
307 | + if ($bytes < 64) { |
|
308 | + $c .= self::substr($block, 0, $bytes); |
|
309 | + break; |
|
310 | + } |
|
311 | 311 | |
312 | - /* Full block */ |
|
313 | - $c .= $block; |
|
314 | - $bytes -= 64; |
|
315 | - if ($bytes <= 0) { |
|
316 | - break; |
|
317 | - } |
|
318 | - $message = self::substr($message, 64); |
|
319 | - } |
|
320 | - /* end for(;;) loop */ |
|
312 | + /* Full block */ |
|
313 | + $c .= $block; |
|
314 | + $bytes -= 64; |
|
315 | + if ($bytes <= 0) { |
|
316 | + break; |
|
317 | + } |
|
318 | + $message = self::substr($message, 64); |
|
319 | + } |
|
320 | + /* end for(;;) loop */ |
|
321 | 321 | |
322 | - $ctx[12] = $j12; |
|
323 | - $ctx[13] = $j13; |
|
324 | - return $c; |
|
325 | - } |
|
322 | + $ctx[12] = $j12; |
|
323 | + $ctx[13] = $j13; |
|
324 | + return $c; |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * @internal You should not use this directly from another application |
|
329 | - * |
|
330 | - * @param int $len |
|
331 | - * @param string $nonce |
|
332 | - * @param string $key |
|
333 | - * @return string |
|
334 | - * @throws SodiumException |
|
335 | - * @throws TypeError |
|
336 | - */ |
|
337 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | - { |
|
339 | - return self::encryptBytes( |
|
340 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | - str_repeat("\x00", $len) |
|
342 | - ); |
|
343 | - } |
|
327 | + /** |
|
328 | + * @internal You should not use this directly from another application |
|
329 | + * |
|
330 | + * @param int $len |
|
331 | + * @param string $nonce |
|
332 | + * @param string $key |
|
333 | + * @return string |
|
334 | + * @throws SodiumException |
|
335 | + * @throws TypeError |
|
336 | + */ |
|
337 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | + { |
|
339 | + return self::encryptBytes( |
|
340 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | + str_repeat("\x00", $len) |
|
342 | + ); |
|
343 | + } |
|
344 | 344 | |
345 | - /** |
|
346 | - * @internal You should not use this directly from another application |
|
347 | - * |
|
348 | - * @param int $len |
|
349 | - * @param string $nonce |
|
350 | - * @param string $key |
|
351 | - * @return string |
|
352 | - * @throws SodiumException |
|
353 | - * @throws TypeError |
|
354 | - */ |
|
355 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | - { |
|
357 | - return self::encryptBytes( |
|
358 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | - str_repeat("\x00", $len) |
|
360 | - ); |
|
361 | - } |
|
345 | + /** |
|
346 | + * @internal You should not use this directly from another application |
|
347 | + * |
|
348 | + * @param int $len |
|
349 | + * @param string $nonce |
|
350 | + * @param string $key |
|
351 | + * @return string |
|
352 | + * @throws SodiumException |
|
353 | + * @throws TypeError |
|
354 | + */ |
|
355 | + public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | + { |
|
357 | + return self::encryptBytes( |
|
358 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | + str_repeat("\x00", $len) |
|
360 | + ); |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
364 | - * @internal You should not use this directly from another application |
|
365 | - * |
|
366 | - * @param string $message |
|
367 | - * @param string $nonce |
|
368 | - * @param string $key |
|
369 | - * @param string $ic |
|
370 | - * @return string |
|
371 | - * @throws SodiumException |
|
372 | - * @throws TypeError |
|
373 | - */ |
|
374 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | - { |
|
376 | - return self::encryptBytes( |
|
377 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | - $message |
|
379 | - ); |
|
380 | - } |
|
363 | + /** |
|
364 | + * @internal You should not use this directly from another application |
|
365 | + * |
|
366 | + * @param string $message |
|
367 | + * @param string $nonce |
|
368 | + * @param string $key |
|
369 | + * @param string $ic |
|
370 | + * @return string |
|
371 | + * @throws SodiumException |
|
372 | + * @throws TypeError |
|
373 | + */ |
|
374 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | + { |
|
376 | + return self::encryptBytes( |
|
377 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | + $message |
|
379 | + ); |
|
380 | + } |
|
381 | 381 | |
382 | - /** |
|
383 | - * @internal You should not use this directly from another application |
|
384 | - * |
|
385 | - * @param string $message |
|
386 | - * @param string $nonce |
|
387 | - * @param string $key |
|
388 | - * @param string $ic |
|
389 | - * @return string |
|
390 | - * @throws SodiumException |
|
391 | - * @throws TypeError |
|
392 | - */ |
|
393 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | - { |
|
395 | - return self::encryptBytes( |
|
396 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | - $message |
|
398 | - ); |
|
399 | - } |
|
382 | + /** |
|
383 | + * @internal You should not use this directly from another application |
|
384 | + * |
|
385 | + * @param string $message |
|
386 | + * @param string $nonce |
|
387 | + * @param string $key |
|
388 | + * @param string $ic |
|
389 | + * @return string |
|
390 | + * @throws SodiumException |
|
391 | + * @throws TypeError |
|
392 | + */ |
|
393 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | + { |
|
395 | + return self::encryptBytes( |
|
396 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | + $message |
|
398 | + ); |
|
399 | + } |
|
400 | 400 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,119 +9,119 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 |
11 | 11 | { |
12 | - /** |
|
13 | - * @param string $in |
|
14 | - * @param string $key |
|
15 | - * @param string|null $c |
|
16 | - * @return string |
|
17 | - * @throws SodiumException |
|
18 | - * @throws TypeError |
|
19 | - */ |
|
20 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
21 | - { |
|
22 | - $ctx = array(); |
|
12 | + /** |
|
13 | + * @param string $in |
|
14 | + * @param string $key |
|
15 | + * @param string|null $c |
|
16 | + * @return string |
|
17 | + * @throws SodiumException |
|
18 | + * @throws TypeError |
|
19 | + */ |
|
20 | + public static function hChaCha20($in = '', $key = '', $c = null) |
|
21 | + { |
|
22 | + $ctx = array(); |
|
23 | 23 | |
24 | - if ($c === null) { |
|
25 | - $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
26 | - $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
27 | - $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
28 | - $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
29 | - } else { |
|
30 | - $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
31 | - $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
32 | - $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
33 | - $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
34 | - } |
|
35 | - $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
36 | - $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
37 | - $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
38 | - $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
39 | - $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
40 | - $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
41 | - $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
42 | - $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
43 | - $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
44 | - $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
45 | - $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
46 | - $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
24 | + if ($c === null) { |
|
25 | + $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
26 | + $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
27 | + $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
28 | + $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
29 | + } else { |
|
30 | + $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
31 | + $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
32 | + $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
33 | + $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
34 | + } |
|
35 | + $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
36 | + $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
37 | + $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
38 | + $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
39 | + $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
40 | + $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
41 | + $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
42 | + $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
43 | + $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
44 | + $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
45 | + $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
46 | + $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
47 | 47 | |
48 | - return self::hChaCha20Bytes($ctx); |
|
49 | - } |
|
48 | + return self::hChaCha20Bytes($ctx); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param array $ctx |
|
53 | - * @return string |
|
54 | - * @throws SodiumException |
|
55 | - * @throws TypeError |
|
56 | - */ |
|
57 | - protected static function hChaCha20Bytes(array $ctx) |
|
58 | - { |
|
59 | - /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
60 | - $x0 = $ctx[0]; |
|
61 | - /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
62 | - $x1 = $ctx[1]; |
|
63 | - /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
64 | - $x2 = $ctx[2]; |
|
65 | - /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
66 | - $x3 = $ctx[3]; |
|
67 | - /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
68 | - $x4 = $ctx[4]; |
|
69 | - /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
70 | - $x5 = $ctx[5]; |
|
71 | - /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
72 | - $x6 = $ctx[6]; |
|
73 | - /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
74 | - $x7 = $ctx[7]; |
|
75 | - /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
76 | - $x8 = $ctx[8]; |
|
77 | - /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
78 | - $x9 = $ctx[9]; |
|
79 | - /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
80 | - $x10 = $ctx[10]; |
|
81 | - /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
82 | - $x11 = $ctx[11]; |
|
83 | - /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | - $x12 = $ctx[12]; |
|
85 | - /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
86 | - $x13 = $ctx[13]; |
|
87 | - /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
88 | - $x14 = $ctx[14]; |
|
89 | - /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
90 | - $x15 = $ctx[15]; |
|
51 | + /** |
|
52 | + * @param array $ctx |
|
53 | + * @return string |
|
54 | + * @throws SodiumException |
|
55 | + * @throws TypeError |
|
56 | + */ |
|
57 | + protected static function hChaCha20Bytes(array $ctx) |
|
58 | + { |
|
59 | + /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
60 | + $x0 = $ctx[0]; |
|
61 | + /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
62 | + $x1 = $ctx[1]; |
|
63 | + /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
64 | + $x2 = $ctx[2]; |
|
65 | + /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
66 | + $x3 = $ctx[3]; |
|
67 | + /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
68 | + $x4 = $ctx[4]; |
|
69 | + /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
70 | + $x5 = $ctx[5]; |
|
71 | + /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
72 | + $x6 = $ctx[6]; |
|
73 | + /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
74 | + $x7 = $ctx[7]; |
|
75 | + /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
76 | + $x8 = $ctx[8]; |
|
77 | + /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
78 | + $x9 = $ctx[9]; |
|
79 | + /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
80 | + $x10 = $ctx[10]; |
|
81 | + /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
82 | + $x11 = $ctx[11]; |
|
83 | + /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | + $x12 = $ctx[12]; |
|
85 | + /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
86 | + $x13 = $ctx[13]; |
|
87 | + /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
88 | + $x14 = $ctx[14]; |
|
89 | + /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
90 | + $x15 = $ctx[15]; |
|
91 | 91 | |
92 | - for ($i = 0; $i < 10; ++$i) { |
|
93 | - # QUARTERROUND( x0, x4, x8, x12) |
|
94 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
92 | + for ($i = 0; $i < 10; ++$i) { |
|
93 | + # QUARTERROUND( x0, x4, x8, x12) |
|
94 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
95 | 95 | |
96 | - # QUARTERROUND( x1, x5, x9, x13) |
|
97 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
96 | + # QUARTERROUND( x1, x5, x9, x13) |
|
97 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
98 | 98 | |
99 | - # QUARTERROUND( x2, x6, x10, x14) |
|
100 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
99 | + # QUARTERROUND( x2, x6, x10, x14) |
|
100 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
101 | 101 | |
102 | - # QUARTERROUND( x3, x7, x11, x15) |
|
103 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
102 | + # QUARTERROUND( x3, x7, x11, x15) |
|
103 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
104 | 104 | |
105 | - # QUARTERROUND( x0, x5, x10, x15) |
|
106 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
105 | + # QUARTERROUND( x0, x5, x10, x15) |
|
106 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
107 | 107 | |
108 | - # QUARTERROUND( x1, x6, x11, x12) |
|
109 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
108 | + # QUARTERROUND( x1, x6, x11, x12) |
|
109 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
110 | 110 | |
111 | - # QUARTERROUND( x2, x7, x8, x13) |
|
112 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
111 | + # QUARTERROUND( x2, x7, x8, x13) |
|
112 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
113 | 113 | |
114 | - # QUARTERROUND( x3, x4, x9, x14) |
|
115 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
116 | - } |
|
114 | + # QUARTERROUND( x3, x4, x9, x14) |
|
115 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
116 | + } |
|
117 | 117 | |
118 | - return $x0->toReverseString() . |
|
119 | - $x1->toReverseString() . |
|
120 | - $x2->toReverseString() . |
|
121 | - $x3->toReverseString() . |
|
122 | - $x12->toReverseString() . |
|
123 | - $x13->toReverseString() . |
|
124 | - $x14->toReverseString() . |
|
125 | - $x15->toReverseString(); |
|
126 | - } |
|
118 | + return $x0->toReverseString() . |
|
119 | + $x1->toReverseString() . |
|
120 | + $x2->toReverseString() . |
|
121 | + $x3->toReverseString() . |
|
122 | + $x12->toReverseString() . |
|
123 | + $x13->toReverseString() . |
|
124 | + $x14->toReverseString() . |
|
125 | + $x15->toReverseString(); |
|
126 | + } |
|
127 | 127 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_HSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,133 +9,133 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_HSalsa20 extends ParagonIE_Sodium_Core32_Salsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Calculate an hsalsa20 hash of a single block |
|
14 | - * |
|
15 | - * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | - * one block (used to derive a subkey for xsalsa20). |
|
17 | - * |
|
18 | - * @internal You should not use this directly from another application |
|
19 | - * |
|
20 | - * @param string $in |
|
21 | - * @param string $k |
|
22 | - * @param string|null $c |
|
23 | - * @return string |
|
24 | - * @throws SodiumException |
|
25 | - * @throws TypeError |
|
26 | - */ |
|
27 | - public static function hsalsa20($in, $k, $c = null) |
|
28 | - { |
|
29 | - /** |
|
30 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
31 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
32 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
33 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
34 | - * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
35 | - * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
36 | - * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
37 | - * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
38 | - * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
39 | - * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
40 | - * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
41 | - * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
42 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
43 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
44 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
45 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
46 | - * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
47 | - * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
48 | - * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
49 | - * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
50 | - * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
51 | - * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
52 | - * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
53 | - * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
54 | - * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
55 | - * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
56 | - * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
57 | - * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
58 | - * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
59 | - * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
60 | - * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
61 | - * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
62 | - */ |
|
63 | - if (self::strlen($k) < 32) { |
|
64 | - throw new RangeException('Key must be 32 bytes long'); |
|
65 | - } |
|
66 | - if ($c === null) { |
|
67 | - $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
68 | - $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
69 | - $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
70 | - $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
71 | - } else { |
|
72 | - $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
73 | - $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
74 | - $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
75 | - $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
76 | - } |
|
77 | - $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
78 | - $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
79 | - $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
80 | - $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
81 | - $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
82 | - $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
83 | - $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
84 | - $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
85 | - $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
86 | - $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
87 | - $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
88 | - $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
12 | + /** |
|
13 | + * Calculate an hsalsa20 hash of a single block |
|
14 | + * |
|
15 | + * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | + * one block (used to derive a subkey for xsalsa20). |
|
17 | + * |
|
18 | + * @internal You should not use this directly from another application |
|
19 | + * |
|
20 | + * @param string $in |
|
21 | + * @param string $k |
|
22 | + * @param string|null $c |
|
23 | + * @return string |
|
24 | + * @throws SodiumException |
|
25 | + * @throws TypeError |
|
26 | + */ |
|
27 | + public static function hsalsa20($in, $k, $c = null) |
|
28 | + { |
|
29 | + /** |
|
30 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
31 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
32 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
33 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
34 | + * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
35 | + * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
36 | + * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
37 | + * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
38 | + * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
39 | + * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
40 | + * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
41 | + * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
42 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
43 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
44 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
45 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
46 | + * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
47 | + * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
48 | + * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
49 | + * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
50 | + * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
51 | + * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
52 | + * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
53 | + * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
54 | + * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
55 | + * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
56 | + * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
57 | + * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
58 | + * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
59 | + * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
60 | + * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
61 | + * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
62 | + */ |
|
63 | + if (self::strlen($k) < 32) { |
|
64 | + throw new RangeException('Key must be 32 bytes long'); |
|
65 | + } |
|
66 | + if ($c === null) { |
|
67 | + $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
68 | + $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
69 | + $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
70 | + $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
71 | + } else { |
|
72 | + $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
73 | + $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
74 | + $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
75 | + $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
76 | + } |
|
77 | + $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
78 | + $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
79 | + $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
80 | + $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
81 | + $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
82 | + $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
83 | + $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
84 | + $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
85 | + $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
86 | + $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
87 | + $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
88 | + $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
89 | 89 | |
90 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
91 | - $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
92 | - $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
93 | - $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
94 | - $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
90 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
91 | + $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
92 | + $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
93 | + $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
94 | + $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
95 | 95 | |
96 | - $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
97 | - $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
98 | - $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
99 | - $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
96 | + $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
97 | + $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
98 | + $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
99 | + $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
100 | 100 | |
101 | - $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
102 | - $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
103 | - $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
104 | - $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
101 | + $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
102 | + $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
103 | + $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
104 | + $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
105 | 105 | |
106 | - $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
107 | - $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
108 | - $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
109 | - $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
106 | + $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
107 | + $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
108 | + $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
109 | + $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
110 | 110 | |
111 | - $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
112 | - $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
113 | - $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
114 | - $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
111 | + $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
112 | + $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
113 | + $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
114 | + $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
115 | 115 | |
116 | - $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
117 | - $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
118 | - $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
119 | - $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
116 | + $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
117 | + $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
118 | + $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
119 | + $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
120 | 120 | |
121 | - $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
122 | - $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
123 | - $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
124 | - $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
121 | + $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
122 | + $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
123 | + $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
124 | + $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
125 | 125 | |
126 | - $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
127 | - $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
128 | - $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
129 | - $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
130 | - } |
|
126 | + $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
127 | + $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
128 | + $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
129 | + $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
130 | + } |
|
131 | 131 | |
132 | - return $x0->toReverseString() . |
|
133 | - $x5->toReverseString() . |
|
134 | - $x10->toReverseString() . |
|
135 | - $x15->toReverseString() . |
|
136 | - $x6->toReverseString() . |
|
137 | - $x7->toReverseString() . |
|
138 | - $x8->toReverseString() . |
|
139 | - $x9->toReverseString(); |
|
140 | - } |
|
132 | + return $x0->toReverseString() . |
|
133 | + $x5->toReverseString() . |
|
134 | + $x10->toReverseString() . |
|
135 | + $x15->toReverseString() . |
|
136 | + $x6->toReverseString() . |
|
137 | + $x7->toReverseString() . |
|
138 | + $x8->toReverseString() . |
|
139 | + $x9->toReverseString(); |
|
140 | + } |
|
141 | 141 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Poly1305_State', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,443 +9,443 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_Poly1305_State extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array<int, int> |
|
14 | - */ |
|
15 | - protected $buffer = array(); |
|
16 | - |
|
17 | - /** |
|
18 | - * @var bool |
|
19 | - */ |
|
20 | - protected $final = false; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
24 | - */ |
|
25 | - public $h; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - protected $leftover = 0; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
34 | - */ |
|
35 | - public $r; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var array<int, ParagonIE_Sodium_Core32_Int64> |
|
39 | - */ |
|
40 | - public $pad; |
|
41 | - |
|
42 | - /** |
|
43 | - * ParagonIE_Sodium_Core32_Poly1305_State constructor. |
|
44 | - * |
|
45 | - * @internal You should not use this directly from another application |
|
46 | - * |
|
47 | - * @param string $key |
|
48 | - * @throws InvalidArgumentException |
|
49 | - * @throws SodiumException |
|
50 | - * @throws TypeError |
|
51 | - */ |
|
52 | - public function __construct($key = '') |
|
53 | - { |
|
54 | - if (self::strlen($key) < 32) { |
|
55 | - throw new InvalidArgumentException( |
|
56 | - 'Poly1305 requires a 32-byte key' |
|
57 | - ); |
|
58 | - } |
|
59 | - /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
60 | - $this->r = array( |
|
61 | - // st->r[0] = ... |
|
62 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)) |
|
63 | - ->setUnsignedInt(true) |
|
64 | - ->mask(0x3ffffff), |
|
65 | - // st->r[1] = ... |
|
66 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 3, 4)) |
|
67 | - ->setUnsignedInt(true) |
|
68 | - ->shiftRight(2) |
|
69 | - ->mask(0x3ffff03), |
|
70 | - // st->r[2] = ... |
|
71 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 6, 4)) |
|
72 | - ->setUnsignedInt(true) |
|
73 | - ->shiftRight(4) |
|
74 | - ->mask(0x3ffc0ff), |
|
75 | - // st->r[3] = ... |
|
76 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 9, 4)) |
|
77 | - ->setUnsignedInt(true) |
|
78 | - ->shiftRight(6) |
|
79 | - ->mask(0x3f03fff), |
|
80 | - // st->r[4] = ... |
|
81 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)) |
|
82 | - ->setUnsignedInt(true) |
|
83 | - ->shiftRight(8) |
|
84 | - ->mask(0x00fffff) |
|
85 | - ); |
|
86 | - |
|
87 | - /* h = 0 */ |
|
88 | - $this->h = array( |
|
89 | - new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
90 | - new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
91 | - new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
92 | - new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
93 | - new ParagonIE_Sodium_Core32_Int32(array(0, 0), true) |
|
94 | - ); |
|
95 | - |
|
96 | - /* save pad for later */ |
|
97 | - $this->pad = array( |
|
98 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)) |
|
99 | - ->setUnsignedInt(true)->toInt64(), |
|
100 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)) |
|
101 | - ->setUnsignedInt(true)->toInt64(), |
|
102 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)) |
|
103 | - ->setUnsignedInt(true)->toInt64(), |
|
104 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)) |
|
105 | - ->setUnsignedInt(true)->toInt64(), |
|
106 | - ); |
|
107 | - |
|
108 | - $this->leftover = 0; |
|
109 | - $this->final = false; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @internal You should not use this directly from another application |
|
114 | - * |
|
115 | - * @param string $message |
|
116 | - * @return self |
|
117 | - * @throws SodiumException |
|
118 | - * @throws TypeError |
|
119 | - */ |
|
120 | - public function update($message = '') |
|
121 | - { |
|
122 | - $bytes = self::strlen($message); |
|
123 | - |
|
124 | - /* handle leftover */ |
|
125 | - if ($this->leftover) { |
|
126 | - /** @var int $want */ |
|
127 | - $want = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - $this->leftover; |
|
128 | - if ($want > $bytes) { |
|
129 | - $want = $bytes; |
|
130 | - } |
|
131 | - for ($i = 0; $i < $want; ++$i) { |
|
132 | - $mi = self::chrToInt($message[$i]); |
|
133 | - $this->buffer[$this->leftover + $i] = $mi; |
|
134 | - } |
|
135 | - // We snip off the leftmost bytes. |
|
136 | - $message = self::substr($message, $want); |
|
137 | - $bytes = self::strlen($message); |
|
138 | - $this->leftover += $want; |
|
139 | - if ($this->leftover < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
140 | - // We still don't have enough to run $this->blocks() |
|
141 | - return $this; |
|
142 | - } |
|
143 | - |
|
144 | - $this->blocks( |
|
145 | - self::intArrayToString($this->buffer), |
|
146 | - ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
147 | - ); |
|
148 | - $this->leftover = 0; |
|
149 | - } |
|
150 | - |
|
151 | - /* process full blocks */ |
|
152 | - if ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
153 | - /** @var int $want */ |
|
154 | - $want = $bytes & ~(ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - 1); |
|
155 | - if ($want >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
156 | - /** @var string $block */ |
|
157 | - $block = self::substr($message, 0, $want); |
|
158 | - if (self::strlen($block) >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
159 | - $this->blocks($block, $want); |
|
160 | - $message = self::substr($message, $want); |
|
161 | - $bytes = self::strlen($message); |
|
162 | - } |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /* store leftover */ |
|
167 | - if ($bytes) { |
|
168 | - for ($i = 0; $i < $bytes; ++$i) { |
|
169 | - $mi = self::chrToInt($message[$i]); |
|
170 | - $this->buffer[$this->leftover + $i] = $mi; |
|
171 | - } |
|
172 | - $this->leftover = (int) $this->leftover + $bytes; |
|
173 | - } |
|
174 | - return $this; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @internal You should not use this directly from another application |
|
179 | - * |
|
180 | - * @param string $message |
|
181 | - * @param int $bytes |
|
182 | - * @return self |
|
183 | - * @throws SodiumException |
|
184 | - * @throws TypeError |
|
185 | - */ |
|
186 | - public function blocks($message, $bytes) |
|
187 | - { |
|
188 | - if (self::strlen($message) < 16) { |
|
189 | - $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
190 | - } |
|
191 | - $hibit = ParagonIE_Sodium_Core32_Int32::fromInt((int) ($this->final ? 0 : 1 << 24)); /* 1 << 128 */ |
|
192 | - $hibit->setUnsignedInt(true); |
|
193 | - $zero = new ParagonIE_Sodium_Core32_Int64(array(0, 0, 0, 0), true); |
|
194 | - /** |
|
195 | - * @var ParagonIE_Sodium_Core32_Int64 $d0 |
|
196 | - * @var ParagonIE_Sodium_Core32_Int64 $d1 |
|
197 | - * @var ParagonIE_Sodium_Core32_Int64 $d2 |
|
198 | - * @var ParagonIE_Sodium_Core32_Int64 $d3 |
|
199 | - * @var ParagonIE_Sodium_Core32_Int64 $d4 |
|
200 | - * @var ParagonIE_Sodium_Core32_Int64 $r0 |
|
201 | - * @var ParagonIE_Sodium_Core32_Int64 $r1 |
|
202 | - * @var ParagonIE_Sodium_Core32_Int64 $r2 |
|
203 | - * @var ParagonIE_Sodium_Core32_Int64 $r3 |
|
204 | - * @var ParagonIE_Sodium_Core32_Int64 $r4 |
|
205 | - * |
|
206 | - * @var ParagonIE_Sodium_Core32_Int32 $h0 |
|
207 | - * @var ParagonIE_Sodium_Core32_Int32 $h1 |
|
208 | - * @var ParagonIE_Sodium_Core32_Int32 $h2 |
|
209 | - * @var ParagonIE_Sodium_Core32_Int32 $h3 |
|
210 | - * @var ParagonIE_Sodium_Core32_Int32 $h4 |
|
211 | - */ |
|
212 | - $r0 = $this->r[0]->toInt64(); |
|
213 | - $r1 = $this->r[1]->toInt64(); |
|
214 | - $r2 = $this->r[2]->toInt64(); |
|
215 | - $r3 = $this->r[3]->toInt64(); |
|
216 | - $r4 = $this->r[4]->toInt64(); |
|
217 | - |
|
218 | - $s1 = $r1->toInt64()->mulInt(5, 3); |
|
219 | - $s2 = $r2->toInt64()->mulInt(5, 3); |
|
220 | - $s3 = $r3->toInt64()->mulInt(5, 3); |
|
221 | - $s4 = $r4->toInt64()->mulInt(5, 3); |
|
222 | - |
|
223 | - $h0 = $this->h[0]; |
|
224 | - $h1 = $this->h[1]; |
|
225 | - $h2 = $this->h[2]; |
|
226 | - $h3 = $this->h[3]; |
|
227 | - $h4 = $this->h[4]; |
|
228 | - |
|
229 | - while ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
230 | - /* h += m[i] */ |
|
231 | - $h0 = $h0->addInt32( |
|
232 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4)) |
|
233 | - ->mask(0x3ffffff) |
|
234 | - )->toInt64(); |
|
235 | - $h1 = $h1->addInt32( |
|
236 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 3, 4)) |
|
237 | - ->shiftRight(2) |
|
238 | - ->mask(0x3ffffff) |
|
239 | - )->toInt64(); |
|
240 | - $h2 = $h2->addInt32( |
|
241 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 6, 4)) |
|
242 | - ->shiftRight(4) |
|
243 | - ->mask(0x3ffffff) |
|
244 | - )->toInt64(); |
|
245 | - $h3 = $h3->addInt32( |
|
246 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 9, 4)) |
|
247 | - ->shiftRight(6) |
|
248 | - ->mask(0x3ffffff) |
|
249 | - )->toInt64(); |
|
250 | - $h4 = $h4->addInt32( |
|
251 | - ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4)) |
|
252 | - ->shiftRight(8) |
|
253 | - ->orInt32($hibit) |
|
254 | - )->toInt64(); |
|
255 | - |
|
256 | - /* h *= r */ |
|
257 | - $d0 = $zero |
|
258 | - ->addInt64($h0->mulInt64($r0, 25)) |
|
259 | - ->addInt64($s4->mulInt64($h1, 26)) |
|
260 | - ->addInt64($s3->mulInt64($h2, 26)) |
|
261 | - ->addInt64($s2->mulInt64($h3, 26)) |
|
262 | - ->addInt64($s1->mulInt64($h4, 26)); |
|
263 | - |
|
264 | - $d1 = $zero |
|
265 | - ->addInt64($h0->mulInt64($r1, 25)) |
|
266 | - ->addInt64($h1->mulInt64($r0, 25)) |
|
267 | - ->addInt64($s4->mulInt64($h2, 26)) |
|
268 | - ->addInt64($s3->mulInt64($h3, 26)) |
|
269 | - ->addInt64($s2->mulInt64($h4, 26)); |
|
270 | - |
|
271 | - $d2 = $zero |
|
272 | - ->addInt64($h0->mulInt64($r2, 25)) |
|
273 | - ->addInt64($h1->mulInt64($r1, 25)) |
|
274 | - ->addInt64($h2->mulInt64($r0, 25)) |
|
275 | - ->addInt64($s4->mulInt64($h3, 26)) |
|
276 | - ->addInt64($s3->mulInt64($h4, 26)); |
|
277 | - |
|
278 | - $d3 = $zero |
|
279 | - ->addInt64($h0->mulInt64($r3, 25)) |
|
280 | - ->addInt64($h1->mulInt64($r2, 25)) |
|
281 | - ->addInt64($h2->mulInt64($r1, 25)) |
|
282 | - ->addInt64($h3->mulInt64($r0, 25)) |
|
283 | - ->addInt64($s4->mulInt64($h4, 26)); |
|
284 | - |
|
285 | - $d4 = $zero |
|
286 | - ->addInt64($h0->mulInt64($r4, 25)) |
|
287 | - ->addInt64($h1->mulInt64($r3, 25)) |
|
288 | - ->addInt64($h2->mulInt64($r2, 25)) |
|
289 | - ->addInt64($h3->mulInt64($r1, 25)) |
|
290 | - ->addInt64($h4->mulInt64($r0, 25)); |
|
291 | - |
|
292 | - /* (partial) h %= p */ |
|
293 | - $c = $d0->shiftRight(26); |
|
294 | - $h0 = $d0->toInt32()->mask(0x3ffffff); |
|
295 | - $d1 = $d1->addInt64($c); |
|
296 | - |
|
297 | - $c = $d1->shiftRight(26); |
|
298 | - $h1 = $d1->toInt32()->mask(0x3ffffff); |
|
299 | - $d2 = $d2->addInt64($c); |
|
300 | - |
|
301 | - $c = $d2->shiftRight(26); |
|
302 | - $h2 = $d2->toInt32()->mask(0x3ffffff); |
|
303 | - $d3 = $d3->addInt64($c); |
|
304 | - |
|
305 | - $c = $d3->shiftRight(26); |
|
306 | - $h3 = $d3->toInt32()->mask(0x3ffffff); |
|
307 | - $d4 = $d4->addInt64($c); |
|
308 | - |
|
309 | - $c = $d4->shiftRight(26); |
|
310 | - $h4 = $d4->toInt32()->mask(0x3ffffff); |
|
311 | - $h0 = $h0->addInt32($c->toInt32()->mulInt(5, 3)); |
|
312 | - |
|
313 | - $c = $h0->shiftRight(26); |
|
314 | - $h0 = $h0->mask(0x3ffffff); |
|
315 | - $h1 = $h1->addInt32($c); |
|
316 | - |
|
317 | - // Chop off the left 32 bytes. |
|
318 | - $message = self::substr( |
|
319 | - $message, |
|
320 | - ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
321 | - ); |
|
322 | - $bytes -= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; |
|
323 | - } |
|
324 | - |
|
325 | - /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */ |
|
326 | - $this->h = array($h0, $h1, $h2, $h3, $h4); |
|
327 | - return $this; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * @internal You should not use this directly from another application |
|
332 | - * |
|
333 | - * @return string |
|
334 | - * @throws SodiumException |
|
335 | - * @throws TypeError |
|
336 | - */ |
|
337 | - public function finish() |
|
338 | - { |
|
339 | - /* process the remaining block */ |
|
340 | - if ($this->leftover) { |
|
341 | - $i = $this->leftover; |
|
342 | - $this->buffer[$i++] = 1; |
|
343 | - for (; $i < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; ++$i) { |
|
344 | - $this->buffer[$i] = 0; |
|
345 | - } |
|
346 | - $this->final = true; |
|
347 | - $this->blocks( |
|
348 | - self::substr( |
|
349 | - self::intArrayToString($this->buffer), |
|
350 | - 0, |
|
351 | - ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
352 | - ), |
|
353 | - $b = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
354 | - ); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * @var ParagonIE_Sodium_Core32_Int32 $f |
|
359 | - * @var ParagonIE_Sodium_Core32_Int32 $g0 |
|
360 | - * @var ParagonIE_Sodium_Core32_Int32 $g1 |
|
361 | - * @var ParagonIE_Sodium_Core32_Int32 $g2 |
|
362 | - * @var ParagonIE_Sodium_Core32_Int32 $g3 |
|
363 | - * @var ParagonIE_Sodium_Core32_Int32 $g4 |
|
364 | - * @var ParagonIE_Sodium_Core32_Int32 $h0 |
|
365 | - * @var ParagonIE_Sodium_Core32_Int32 $h1 |
|
366 | - * @var ParagonIE_Sodium_Core32_Int32 $h2 |
|
367 | - * @var ParagonIE_Sodium_Core32_Int32 $h3 |
|
368 | - * @var ParagonIE_Sodium_Core32_Int32 $h4 |
|
369 | - */ |
|
370 | - $h0 = $this->h[0]; |
|
371 | - $h1 = $this->h[1]; |
|
372 | - $h2 = $this->h[2]; |
|
373 | - $h3 = $this->h[3]; |
|
374 | - $h4 = $this->h[4]; |
|
375 | - |
|
376 | - $c = $h1->shiftRight(26); # $c = $h1 >> 26; |
|
377 | - $h1 = $h1->mask(0x3ffffff); # $h1 &= 0x3ffffff; |
|
378 | - |
|
379 | - $h2 = $h2->addInt32($c); # $h2 += $c; |
|
380 | - $c = $h2->shiftRight(26); # $c = $h2 >> 26; |
|
381 | - $h2 = $h2->mask(0x3ffffff); # $h2 &= 0x3ffffff; |
|
382 | - |
|
383 | - $h3 = $h3->addInt32($c); # $h3 += $c; |
|
384 | - $c = $h3->shiftRight(26); # $c = $h3 >> 26; |
|
385 | - $h3 = $h3->mask(0x3ffffff); # $h3 &= 0x3ffffff; |
|
386 | - |
|
387 | - $h4 = $h4->addInt32($c); # $h4 += $c; |
|
388 | - $c = $h4->shiftRight(26); # $c = $h4 >> 26; |
|
389 | - $h4 = $h4->mask(0x3ffffff); # $h4 &= 0x3ffffff; |
|
390 | - |
|
391 | - $h0 = $h0->addInt32($c->mulInt(5, 3)); # $h0 += self::mul($c, 5); |
|
392 | - $c = $h0->shiftRight(26); # $c = $h0 >> 26; |
|
393 | - $h0 = $h0->mask(0x3ffffff); # $h0 &= 0x3ffffff; |
|
394 | - $h1 = $h1->addInt32($c); # $h1 += $c; |
|
395 | - |
|
396 | - /* compute h + -p */ |
|
397 | - $g0 = $h0->addInt(5); |
|
398 | - $c = $g0->shiftRight(26); |
|
399 | - $g0 = $g0->mask(0x3ffffff); |
|
400 | - $g1 = $h1->addInt32($c); |
|
401 | - $c = $g1->shiftRight(26); |
|
402 | - $g1 = $g1->mask(0x3ffffff); |
|
403 | - $g2 = $h2->addInt32($c); |
|
404 | - $c = $g2->shiftRight(26); |
|
405 | - $g2 = $g2->mask(0x3ffffff); |
|
406 | - $g3 = $h3->addInt32($c); |
|
407 | - $c = $g3->shiftRight(26); |
|
408 | - $g3 = $g3->mask(0x3ffffff); |
|
409 | - $g4 = $h4->addInt32($c)->subInt(1 << 26); |
|
410 | - |
|
411 | - # $mask = ($g4 >> 31) - 1; |
|
412 | - /* select h if h < p, or h + -p if h >= p */ |
|
413 | - $mask = (int) (($g4->toInt() >> 31) + 1); |
|
414 | - |
|
415 | - $g0 = $g0->mask($mask); |
|
416 | - $g1 = $g1->mask($mask); |
|
417 | - $g2 = $g2->mask($mask); |
|
418 | - $g3 = $g3->mask($mask); |
|
419 | - $g4 = $g4->mask($mask); |
|
420 | - |
|
421 | - /** @var int $mask */ |
|
422 | - $mask = (~$mask) & 0xffffffff; |
|
423 | - |
|
424 | - $h0 = $h0->mask($mask)->orInt32($g0); |
|
425 | - $h1 = $h1->mask($mask)->orInt32($g1); |
|
426 | - $h2 = $h2->mask($mask)->orInt32($g2); |
|
427 | - $h3 = $h3->mask($mask)->orInt32($g3); |
|
428 | - $h4 = $h4->mask($mask)->orInt32($g4); |
|
429 | - |
|
430 | - /* h = h % (2^128) */ |
|
431 | - $h0 = $h0->orInt32($h1->shiftLeft(26)); |
|
432 | - $h1 = $h1->shiftRight(6)->orInt32($h2->shiftLeft(20)); |
|
433 | - $h2 = $h2->shiftRight(12)->orInt32($h3->shiftLeft(14)); |
|
434 | - $h3 = $h3->shiftRight(18)->orInt32($h4->shiftLeft(8)); |
|
435 | - |
|
436 | - /* mac = (h + pad) % (2^128) */ |
|
437 | - $f = $h0->toInt64()->addInt64($this->pad[0]); |
|
438 | - $h0 = $f->toInt32(); |
|
439 | - $f = $h1->toInt64()->addInt64($this->pad[1])->addInt($h0->overflow); |
|
440 | - $h1 = $f->toInt32(); |
|
441 | - $f = $h2->toInt64()->addInt64($this->pad[2])->addInt($h1->overflow); |
|
442 | - $h2 = $f->toInt32(); |
|
443 | - $f = $h3->toInt64()->addInt64($this->pad[3])->addInt($h2->overflow); |
|
444 | - $h3 = $f->toInt32(); |
|
445 | - |
|
446 | - return $h0->toReverseString() . |
|
447 | - $h1->toReverseString() . |
|
448 | - $h2->toReverseString() . |
|
449 | - $h3->toReverseString(); |
|
450 | - } |
|
12 | + /** |
|
13 | + * @var array<int, int> |
|
14 | + */ |
|
15 | + protected $buffer = array(); |
|
16 | + |
|
17 | + /** |
|
18 | + * @var bool |
|
19 | + */ |
|
20 | + protected $final = false; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
24 | + */ |
|
25 | + public $h; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + protected $leftover = 0; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
34 | + */ |
|
35 | + public $r; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var array<int, ParagonIE_Sodium_Core32_Int64> |
|
39 | + */ |
|
40 | + public $pad; |
|
41 | + |
|
42 | + /** |
|
43 | + * ParagonIE_Sodium_Core32_Poly1305_State constructor. |
|
44 | + * |
|
45 | + * @internal You should not use this directly from another application |
|
46 | + * |
|
47 | + * @param string $key |
|
48 | + * @throws InvalidArgumentException |
|
49 | + * @throws SodiumException |
|
50 | + * @throws TypeError |
|
51 | + */ |
|
52 | + public function __construct($key = '') |
|
53 | + { |
|
54 | + if (self::strlen($key) < 32) { |
|
55 | + throw new InvalidArgumentException( |
|
56 | + 'Poly1305 requires a 32-byte key' |
|
57 | + ); |
|
58 | + } |
|
59 | + /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ |
|
60 | + $this->r = array( |
|
61 | + // st->r[0] = ... |
|
62 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)) |
|
63 | + ->setUnsignedInt(true) |
|
64 | + ->mask(0x3ffffff), |
|
65 | + // st->r[1] = ... |
|
66 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 3, 4)) |
|
67 | + ->setUnsignedInt(true) |
|
68 | + ->shiftRight(2) |
|
69 | + ->mask(0x3ffff03), |
|
70 | + // st->r[2] = ... |
|
71 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 6, 4)) |
|
72 | + ->setUnsignedInt(true) |
|
73 | + ->shiftRight(4) |
|
74 | + ->mask(0x3ffc0ff), |
|
75 | + // st->r[3] = ... |
|
76 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 9, 4)) |
|
77 | + ->setUnsignedInt(true) |
|
78 | + ->shiftRight(6) |
|
79 | + ->mask(0x3f03fff), |
|
80 | + // st->r[4] = ... |
|
81 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)) |
|
82 | + ->setUnsignedInt(true) |
|
83 | + ->shiftRight(8) |
|
84 | + ->mask(0x00fffff) |
|
85 | + ); |
|
86 | + |
|
87 | + /* h = 0 */ |
|
88 | + $this->h = array( |
|
89 | + new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
90 | + new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
91 | + new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
92 | + new ParagonIE_Sodium_Core32_Int32(array(0, 0), true), |
|
93 | + new ParagonIE_Sodium_Core32_Int32(array(0, 0), true) |
|
94 | + ); |
|
95 | + |
|
96 | + /* save pad for later */ |
|
97 | + $this->pad = array( |
|
98 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)) |
|
99 | + ->setUnsignedInt(true)->toInt64(), |
|
100 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)) |
|
101 | + ->setUnsignedInt(true)->toInt64(), |
|
102 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)) |
|
103 | + ->setUnsignedInt(true)->toInt64(), |
|
104 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)) |
|
105 | + ->setUnsignedInt(true)->toInt64(), |
|
106 | + ); |
|
107 | + |
|
108 | + $this->leftover = 0; |
|
109 | + $this->final = false; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @internal You should not use this directly from another application |
|
114 | + * |
|
115 | + * @param string $message |
|
116 | + * @return self |
|
117 | + * @throws SodiumException |
|
118 | + * @throws TypeError |
|
119 | + */ |
|
120 | + public function update($message = '') |
|
121 | + { |
|
122 | + $bytes = self::strlen($message); |
|
123 | + |
|
124 | + /* handle leftover */ |
|
125 | + if ($this->leftover) { |
|
126 | + /** @var int $want */ |
|
127 | + $want = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - $this->leftover; |
|
128 | + if ($want > $bytes) { |
|
129 | + $want = $bytes; |
|
130 | + } |
|
131 | + for ($i = 0; $i < $want; ++$i) { |
|
132 | + $mi = self::chrToInt($message[$i]); |
|
133 | + $this->buffer[$this->leftover + $i] = $mi; |
|
134 | + } |
|
135 | + // We snip off the leftmost bytes. |
|
136 | + $message = self::substr($message, $want); |
|
137 | + $bytes = self::strlen($message); |
|
138 | + $this->leftover += $want; |
|
139 | + if ($this->leftover < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
140 | + // We still don't have enough to run $this->blocks() |
|
141 | + return $this; |
|
142 | + } |
|
143 | + |
|
144 | + $this->blocks( |
|
145 | + self::intArrayToString($this->buffer), |
|
146 | + ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
147 | + ); |
|
148 | + $this->leftover = 0; |
|
149 | + } |
|
150 | + |
|
151 | + /* process full blocks */ |
|
152 | + if ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
153 | + /** @var int $want */ |
|
154 | + $want = $bytes & ~(ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - 1); |
|
155 | + if ($want >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
156 | + /** @var string $block */ |
|
157 | + $block = self::substr($message, 0, $want); |
|
158 | + if (self::strlen($block) >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
159 | + $this->blocks($block, $want); |
|
160 | + $message = self::substr($message, $want); |
|
161 | + $bytes = self::strlen($message); |
|
162 | + } |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /* store leftover */ |
|
167 | + if ($bytes) { |
|
168 | + for ($i = 0; $i < $bytes; ++$i) { |
|
169 | + $mi = self::chrToInt($message[$i]); |
|
170 | + $this->buffer[$this->leftover + $i] = $mi; |
|
171 | + } |
|
172 | + $this->leftover = (int) $this->leftover + $bytes; |
|
173 | + } |
|
174 | + return $this; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @internal You should not use this directly from another application |
|
179 | + * |
|
180 | + * @param string $message |
|
181 | + * @param int $bytes |
|
182 | + * @return self |
|
183 | + * @throws SodiumException |
|
184 | + * @throws TypeError |
|
185 | + */ |
|
186 | + public function blocks($message, $bytes) |
|
187 | + { |
|
188 | + if (self::strlen($message) < 16) { |
|
189 | + $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT); |
|
190 | + } |
|
191 | + $hibit = ParagonIE_Sodium_Core32_Int32::fromInt((int) ($this->final ? 0 : 1 << 24)); /* 1 << 128 */ |
|
192 | + $hibit->setUnsignedInt(true); |
|
193 | + $zero = new ParagonIE_Sodium_Core32_Int64(array(0, 0, 0, 0), true); |
|
194 | + /** |
|
195 | + * @var ParagonIE_Sodium_Core32_Int64 $d0 |
|
196 | + * @var ParagonIE_Sodium_Core32_Int64 $d1 |
|
197 | + * @var ParagonIE_Sodium_Core32_Int64 $d2 |
|
198 | + * @var ParagonIE_Sodium_Core32_Int64 $d3 |
|
199 | + * @var ParagonIE_Sodium_Core32_Int64 $d4 |
|
200 | + * @var ParagonIE_Sodium_Core32_Int64 $r0 |
|
201 | + * @var ParagonIE_Sodium_Core32_Int64 $r1 |
|
202 | + * @var ParagonIE_Sodium_Core32_Int64 $r2 |
|
203 | + * @var ParagonIE_Sodium_Core32_Int64 $r3 |
|
204 | + * @var ParagonIE_Sodium_Core32_Int64 $r4 |
|
205 | + * |
|
206 | + * @var ParagonIE_Sodium_Core32_Int32 $h0 |
|
207 | + * @var ParagonIE_Sodium_Core32_Int32 $h1 |
|
208 | + * @var ParagonIE_Sodium_Core32_Int32 $h2 |
|
209 | + * @var ParagonIE_Sodium_Core32_Int32 $h3 |
|
210 | + * @var ParagonIE_Sodium_Core32_Int32 $h4 |
|
211 | + */ |
|
212 | + $r0 = $this->r[0]->toInt64(); |
|
213 | + $r1 = $this->r[1]->toInt64(); |
|
214 | + $r2 = $this->r[2]->toInt64(); |
|
215 | + $r3 = $this->r[3]->toInt64(); |
|
216 | + $r4 = $this->r[4]->toInt64(); |
|
217 | + |
|
218 | + $s1 = $r1->toInt64()->mulInt(5, 3); |
|
219 | + $s2 = $r2->toInt64()->mulInt(5, 3); |
|
220 | + $s3 = $r3->toInt64()->mulInt(5, 3); |
|
221 | + $s4 = $r4->toInt64()->mulInt(5, 3); |
|
222 | + |
|
223 | + $h0 = $this->h[0]; |
|
224 | + $h1 = $this->h[1]; |
|
225 | + $h2 = $this->h[2]; |
|
226 | + $h3 = $this->h[3]; |
|
227 | + $h4 = $this->h[4]; |
|
228 | + |
|
229 | + while ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) { |
|
230 | + /* h += m[i] */ |
|
231 | + $h0 = $h0->addInt32( |
|
232 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4)) |
|
233 | + ->mask(0x3ffffff) |
|
234 | + )->toInt64(); |
|
235 | + $h1 = $h1->addInt32( |
|
236 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 3, 4)) |
|
237 | + ->shiftRight(2) |
|
238 | + ->mask(0x3ffffff) |
|
239 | + )->toInt64(); |
|
240 | + $h2 = $h2->addInt32( |
|
241 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 6, 4)) |
|
242 | + ->shiftRight(4) |
|
243 | + ->mask(0x3ffffff) |
|
244 | + )->toInt64(); |
|
245 | + $h3 = $h3->addInt32( |
|
246 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 9, 4)) |
|
247 | + ->shiftRight(6) |
|
248 | + ->mask(0x3ffffff) |
|
249 | + )->toInt64(); |
|
250 | + $h4 = $h4->addInt32( |
|
251 | + ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4)) |
|
252 | + ->shiftRight(8) |
|
253 | + ->orInt32($hibit) |
|
254 | + )->toInt64(); |
|
255 | + |
|
256 | + /* h *= r */ |
|
257 | + $d0 = $zero |
|
258 | + ->addInt64($h0->mulInt64($r0, 25)) |
|
259 | + ->addInt64($s4->mulInt64($h1, 26)) |
|
260 | + ->addInt64($s3->mulInt64($h2, 26)) |
|
261 | + ->addInt64($s2->mulInt64($h3, 26)) |
|
262 | + ->addInt64($s1->mulInt64($h4, 26)); |
|
263 | + |
|
264 | + $d1 = $zero |
|
265 | + ->addInt64($h0->mulInt64($r1, 25)) |
|
266 | + ->addInt64($h1->mulInt64($r0, 25)) |
|
267 | + ->addInt64($s4->mulInt64($h2, 26)) |
|
268 | + ->addInt64($s3->mulInt64($h3, 26)) |
|
269 | + ->addInt64($s2->mulInt64($h4, 26)); |
|
270 | + |
|
271 | + $d2 = $zero |
|
272 | + ->addInt64($h0->mulInt64($r2, 25)) |
|
273 | + ->addInt64($h1->mulInt64($r1, 25)) |
|
274 | + ->addInt64($h2->mulInt64($r0, 25)) |
|
275 | + ->addInt64($s4->mulInt64($h3, 26)) |
|
276 | + ->addInt64($s3->mulInt64($h4, 26)); |
|
277 | + |
|
278 | + $d3 = $zero |
|
279 | + ->addInt64($h0->mulInt64($r3, 25)) |
|
280 | + ->addInt64($h1->mulInt64($r2, 25)) |
|
281 | + ->addInt64($h2->mulInt64($r1, 25)) |
|
282 | + ->addInt64($h3->mulInt64($r0, 25)) |
|
283 | + ->addInt64($s4->mulInt64($h4, 26)); |
|
284 | + |
|
285 | + $d4 = $zero |
|
286 | + ->addInt64($h0->mulInt64($r4, 25)) |
|
287 | + ->addInt64($h1->mulInt64($r3, 25)) |
|
288 | + ->addInt64($h2->mulInt64($r2, 25)) |
|
289 | + ->addInt64($h3->mulInt64($r1, 25)) |
|
290 | + ->addInt64($h4->mulInt64($r0, 25)); |
|
291 | + |
|
292 | + /* (partial) h %= p */ |
|
293 | + $c = $d0->shiftRight(26); |
|
294 | + $h0 = $d0->toInt32()->mask(0x3ffffff); |
|
295 | + $d1 = $d1->addInt64($c); |
|
296 | + |
|
297 | + $c = $d1->shiftRight(26); |
|
298 | + $h1 = $d1->toInt32()->mask(0x3ffffff); |
|
299 | + $d2 = $d2->addInt64($c); |
|
300 | + |
|
301 | + $c = $d2->shiftRight(26); |
|
302 | + $h2 = $d2->toInt32()->mask(0x3ffffff); |
|
303 | + $d3 = $d3->addInt64($c); |
|
304 | + |
|
305 | + $c = $d3->shiftRight(26); |
|
306 | + $h3 = $d3->toInt32()->mask(0x3ffffff); |
|
307 | + $d4 = $d4->addInt64($c); |
|
308 | + |
|
309 | + $c = $d4->shiftRight(26); |
|
310 | + $h4 = $d4->toInt32()->mask(0x3ffffff); |
|
311 | + $h0 = $h0->addInt32($c->toInt32()->mulInt(5, 3)); |
|
312 | + |
|
313 | + $c = $h0->shiftRight(26); |
|
314 | + $h0 = $h0->mask(0x3ffffff); |
|
315 | + $h1 = $h1->addInt32($c); |
|
316 | + |
|
317 | + // Chop off the left 32 bytes. |
|
318 | + $message = self::substr( |
|
319 | + $message, |
|
320 | + ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
321 | + ); |
|
322 | + $bytes -= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; |
|
323 | + } |
|
324 | + |
|
325 | + /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */ |
|
326 | + $this->h = array($h0, $h1, $h2, $h3, $h4); |
|
327 | + return $this; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * @internal You should not use this directly from another application |
|
332 | + * |
|
333 | + * @return string |
|
334 | + * @throws SodiumException |
|
335 | + * @throws TypeError |
|
336 | + */ |
|
337 | + public function finish() |
|
338 | + { |
|
339 | + /* process the remaining block */ |
|
340 | + if ($this->leftover) { |
|
341 | + $i = $this->leftover; |
|
342 | + $this->buffer[$i++] = 1; |
|
343 | + for (; $i < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; ++$i) { |
|
344 | + $this->buffer[$i] = 0; |
|
345 | + } |
|
346 | + $this->final = true; |
|
347 | + $this->blocks( |
|
348 | + self::substr( |
|
349 | + self::intArrayToString($this->buffer), |
|
350 | + 0, |
|
351 | + ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
352 | + ), |
|
353 | + $b = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE |
|
354 | + ); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * @var ParagonIE_Sodium_Core32_Int32 $f |
|
359 | + * @var ParagonIE_Sodium_Core32_Int32 $g0 |
|
360 | + * @var ParagonIE_Sodium_Core32_Int32 $g1 |
|
361 | + * @var ParagonIE_Sodium_Core32_Int32 $g2 |
|
362 | + * @var ParagonIE_Sodium_Core32_Int32 $g3 |
|
363 | + * @var ParagonIE_Sodium_Core32_Int32 $g4 |
|
364 | + * @var ParagonIE_Sodium_Core32_Int32 $h0 |
|
365 | + * @var ParagonIE_Sodium_Core32_Int32 $h1 |
|
366 | + * @var ParagonIE_Sodium_Core32_Int32 $h2 |
|
367 | + * @var ParagonIE_Sodium_Core32_Int32 $h3 |
|
368 | + * @var ParagonIE_Sodium_Core32_Int32 $h4 |
|
369 | + */ |
|
370 | + $h0 = $this->h[0]; |
|
371 | + $h1 = $this->h[1]; |
|
372 | + $h2 = $this->h[2]; |
|
373 | + $h3 = $this->h[3]; |
|
374 | + $h4 = $this->h[4]; |
|
375 | + |
|
376 | + $c = $h1->shiftRight(26); # $c = $h1 >> 26; |
|
377 | + $h1 = $h1->mask(0x3ffffff); # $h1 &= 0x3ffffff; |
|
378 | + |
|
379 | + $h2 = $h2->addInt32($c); # $h2 += $c; |
|
380 | + $c = $h2->shiftRight(26); # $c = $h2 >> 26; |
|
381 | + $h2 = $h2->mask(0x3ffffff); # $h2 &= 0x3ffffff; |
|
382 | + |
|
383 | + $h3 = $h3->addInt32($c); # $h3 += $c; |
|
384 | + $c = $h3->shiftRight(26); # $c = $h3 >> 26; |
|
385 | + $h3 = $h3->mask(0x3ffffff); # $h3 &= 0x3ffffff; |
|
386 | + |
|
387 | + $h4 = $h4->addInt32($c); # $h4 += $c; |
|
388 | + $c = $h4->shiftRight(26); # $c = $h4 >> 26; |
|
389 | + $h4 = $h4->mask(0x3ffffff); # $h4 &= 0x3ffffff; |
|
390 | + |
|
391 | + $h0 = $h0->addInt32($c->mulInt(5, 3)); # $h0 += self::mul($c, 5); |
|
392 | + $c = $h0->shiftRight(26); # $c = $h0 >> 26; |
|
393 | + $h0 = $h0->mask(0x3ffffff); # $h0 &= 0x3ffffff; |
|
394 | + $h1 = $h1->addInt32($c); # $h1 += $c; |
|
395 | + |
|
396 | + /* compute h + -p */ |
|
397 | + $g0 = $h0->addInt(5); |
|
398 | + $c = $g0->shiftRight(26); |
|
399 | + $g0 = $g0->mask(0x3ffffff); |
|
400 | + $g1 = $h1->addInt32($c); |
|
401 | + $c = $g1->shiftRight(26); |
|
402 | + $g1 = $g1->mask(0x3ffffff); |
|
403 | + $g2 = $h2->addInt32($c); |
|
404 | + $c = $g2->shiftRight(26); |
|
405 | + $g2 = $g2->mask(0x3ffffff); |
|
406 | + $g3 = $h3->addInt32($c); |
|
407 | + $c = $g3->shiftRight(26); |
|
408 | + $g3 = $g3->mask(0x3ffffff); |
|
409 | + $g4 = $h4->addInt32($c)->subInt(1 << 26); |
|
410 | + |
|
411 | + # $mask = ($g4 >> 31) - 1; |
|
412 | + /* select h if h < p, or h + -p if h >= p */ |
|
413 | + $mask = (int) (($g4->toInt() >> 31) + 1); |
|
414 | + |
|
415 | + $g0 = $g0->mask($mask); |
|
416 | + $g1 = $g1->mask($mask); |
|
417 | + $g2 = $g2->mask($mask); |
|
418 | + $g3 = $g3->mask($mask); |
|
419 | + $g4 = $g4->mask($mask); |
|
420 | + |
|
421 | + /** @var int $mask */ |
|
422 | + $mask = (~$mask) & 0xffffffff; |
|
423 | + |
|
424 | + $h0 = $h0->mask($mask)->orInt32($g0); |
|
425 | + $h1 = $h1->mask($mask)->orInt32($g1); |
|
426 | + $h2 = $h2->mask($mask)->orInt32($g2); |
|
427 | + $h3 = $h3->mask($mask)->orInt32($g3); |
|
428 | + $h4 = $h4->mask($mask)->orInt32($g4); |
|
429 | + |
|
430 | + /* h = h % (2^128) */ |
|
431 | + $h0 = $h0->orInt32($h1->shiftLeft(26)); |
|
432 | + $h1 = $h1->shiftRight(6)->orInt32($h2->shiftLeft(20)); |
|
433 | + $h2 = $h2->shiftRight(12)->orInt32($h3->shiftLeft(14)); |
|
434 | + $h3 = $h3->shiftRight(18)->orInt32($h4->shiftLeft(8)); |
|
435 | + |
|
436 | + /* mac = (h + pad) % (2^128) */ |
|
437 | + $f = $h0->toInt64()->addInt64($this->pad[0]); |
|
438 | + $h0 = $f->toInt32(); |
|
439 | + $f = $h1->toInt64()->addInt64($this->pad[1])->addInt($h0->overflow); |
|
440 | + $h1 = $f->toInt32(); |
|
441 | + $f = $h2->toInt64()->addInt64($this->pad[2])->addInt($h1->overflow); |
|
442 | + $h2 = $f->toInt32(); |
|
443 | + $f = $h3->toInt64()->addInt64($this->pad[3])->addInt($h2->overflow); |
|
444 | + $h3 = $f->toInt32(); |
|
445 | + |
|
446 | + return $h0->toReverseString() . |
|
447 | + $h1->toReverseString() . |
|
448 | + $h2->toReverseString() . |
|
449 | + $h3->toReverseString(); |
|
450 | + } |
|
451 | 451 | } |
@@ -5,159 +5,159 @@ |
||
5 | 5 | */ |
6 | 6 | class ParagonIE_Sodium_Core32_SecretStream_State |
7 | 7 | { |
8 | - /** @var string $key */ |
|
9 | - protected $key; |
|
10 | - |
|
11 | - /** @var int $counter */ |
|
12 | - protected $counter; |
|
13 | - |
|
14 | - /** @var string $nonce */ |
|
15 | - protected $nonce; |
|
16 | - |
|
17 | - /** @var string $_pad */ |
|
18 | - protected $_pad; |
|
19 | - |
|
20 | - /** |
|
21 | - * ParagonIE_Sodium_Core32_SecretStream_State constructor. |
|
22 | - * @param string $key |
|
23 | - * @param string|null $nonce |
|
24 | - */ |
|
25 | - public function __construct($key, $nonce = null) |
|
26 | - { |
|
27 | - $this->key = $key; |
|
28 | - $this->counter = 1; |
|
29 | - if (is_null($nonce)) { |
|
30 | - $nonce = str_repeat("\0", 12); |
|
31 | - } |
|
32 | - $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; |
|
33 | - $this->_pad = str_repeat("\0", 4); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @return self |
|
38 | - */ |
|
39 | - public function counterReset() |
|
40 | - { |
|
41 | - $this->counter = 1; |
|
42 | - $this->_pad = str_repeat("\0", 4); |
|
43 | - return $this; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function getKey() |
|
50 | - { |
|
51 | - return $this->key; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getCounter() |
|
58 | - { |
|
59 | - return ParagonIE_Sodium_Core32_Util::store32_le($this->counter); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function getNonce() |
|
66 | - { |
|
67 | - if (!is_string($this->nonce)) { |
|
68 | - $this->nonce = str_repeat("\0", 12); |
|
69 | - } |
|
70 | - if (ParagonIE_Sodium_Core32_Util::strlen($this->nonce) !== 12) { |
|
71 | - $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); |
|
72 | - } |
|
73 | - return $this->nonce; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function getCombinedNonce() |
|
80 | - { |
|
81 | - return $this->getCounter() . |
|
82 | - ParagonIE_Sodium_Core32_Util::substr($this->getNonce(), 0, 8); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public function incrementCounter() |
|
89 | - { |
|
90 | - ++$this->counter; |
|
91 | - return $this; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - public function needsRekey() |
|
98 | - { |
|
99 | - return ($this->counter & 0xffff) === 0; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $newKeyAndNonce |
|
104 | - * @return self |
|
105 | - */ |
|
106 | - public function rekey($newKeyAndNonce) |
|
107 | - { |
|
108 | - $this->key = ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 0, 32); |
|
109 | - $this->nonce = str_pad( |
|
110 | - ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 32), |
|
111 | - 12, |
|
112 | - "\0", |
|
113 | - STR_PAD_RIGHT |
|
114 | - ); |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $str |
|
120 | - * @return self |
|
121 | - */ |
|
122 | - public function xorNonce($str) |
|
123 | - { |
|
124 | - $this->nonce = ParagonIE_Sodium_Core32_Util::xorStrings( |
|
125 | - $this->getNonce(), |
|
126 | - str_pad( |
|
127 | - ParagonIE_Sodium_Core32_Util::substr($str, 0, 8), |
|
128 | - 12, |
|
129 | - "\0", |
|
130 | - STR_PAD_RIGHT |
|
131 | - ) |
|
132 | - ); |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $string |
|
138 | - * @return self |
|
139 | - */ |
|
140 | - public static function fromString($string) |
|
141 | - { |
|
142 | - $state = new ParagonIE_Sodium_Core32_SecretStream_State( |
|
143 | - ParagonIE_Sodium_Core32_Util::substr($string, 0, 32) |
|
144 | - ); |
|
145 | - $state->counter = ParagonIE_Sodium_Core32_Util::load_4( |
|
146 | - ParagonIE_Sodium_Core32_Util::substr($string, 32, 4) |
|
147 | - ); |
|
148 | - $state->nonce = ParagonIE_Sodium_Core32_Util::substr($string, 36, 12); |
|
149 | - $state->_pad = ParagonIE_Sodium_Core32_Util::substr($string, 48, 8); |
|
150 | - return $state; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - public function toString() |
|
157 | - { |
|
158 | - return $this->key . |
|
159 | - $this->getCounter() . |
|
160 | - $this->nonce . |
|
161 | - $this->_pad; |
|
162 | - } |
|
8 | + /** @var string $key */ |
|
9 | + protected $key; |
|
10 | + |
|
11 | + /** @var int $counter */ |
|
12 | + protected $counter; |
|
13 | + |
|
14 | + /** @var string $nonce */ |
|
15 | + protected $nonce; |
|
16 | + |
|
17 | + /** @var string $_pad */ |
|
18 | + protected $_pad; |
|
19 | + |
|
20 | + /** |
|
21 | + * ParagonIE_Sodium_Core32_SecretStream_State constructor. |
|
22 | + * @param string $key |
|
23 | + * @param string|null $nonce |
|
24 | + */ |
|
25 | + public function __construct($key, $nonce = null) |
|
26 | + { |
|
27 | + $this->key = $key; |
|
28 | + $this->counter = 1; |
|
29 | + if (is_null($nonce)) { |
|
30 | + $nonce = str_repeat("\0", 12); |
|
31 | + } |
|
32 | + $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);; |
|
33 | + $this->_pad = str_repeat("\0", 4); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @return self |
|
38 | + */ |
|
39 | + public function counterReset() |
|
40 | + { |
|
41 | + $this->counter = 1; |
|
42 | + $this->_pad = str_repeat("\0", 4); |
|
43 | + return $this; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function getKey() |
|
50 | + { |
|
51 | + return $this->key; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getCounter() |
|
58 | + { |
|
59 | + return ParagonIE_Sodium_Core32_Util::store32_le($this->counter); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function getNonce() |
|
66 | + { |
|
67 | + if (!is_string($this->nonce)) { |
|
68 | + $this->nonce = str_repeat("\0", 12); |
|
69 | + } |
|
70 | + if (ParagonIE_Sodium_Core32_Util::strlen($this->nonce) !== 12) { |
|
71 | + $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT); |
|
72 | + } |
|
73 | + return $this->nonce; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function getCombinedNonce() |
|
80 | + { |
|
81 | + return $this->getCounter() . |
|
82 | + ParagonIE_Sodium_Core32_Util::substr($this->getNonce(), 0, 8); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public function incrementCounter() |
|
89 | + { |
|
90 | + ++$this->counter; |
|
91 | + return $this; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + public function needsRekey() |
|
98 | + { |
|
99 | + return ($this->counter & 0xffff) === 0; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $newKeyAndNonce |
|
104 | + * @return self |
|
105 | + */ |
|
106 | + public function rekey($newKeyAndNonce) |
|
107 | + { |
|
108 | + $this->key = ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 0, 32); |
|
109 | + $this->nonce = str_pad( |
|
110 | + ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 32), |
|
111 | + 12, |
|
112 | + "\0", |
|
113 | + STR_PAD_RIGHT |
|
114 | + ); |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $str |
|
120 | + * @return self |
|
121 | + */ |
|
122 | + public function xorNonce($str) |
|
123 | + { |
|
124 | + $this->nonce = ParagonIE_Sodium_Core32_Util::xorStrings( |
|
125 | + $this->getNonce(), |
|
126 | + str_pad( |
|
127 | + ParagonIE_Sodium_Core32_Util::substr($str, 0, 8), |
|
128 | + 12, |
|
129 | + "\0", |
|
130 | + STR_PAD_RIGHT |
|
131 | + ) |
|
132 | + ); |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $string |
|
138 | + * @return self |
|
139 | + */ |
|
140 | + public static function fromString($string) |
|
141 | + { |
|
142 | + $state = new ParagonIE_Sodium_Core32_SecretStream_State( |
|
143 | + ParagonIE_Sodium_Core32_Util::substr($string, 0, 32) |
|
144 | + ); |
|
145 | + $state->counter = ParagonIE_Sodium_Core32_Util::load_4( |
|
146 | + ParagonIE_Sodium_Core32_Util::substr($string, 32, 4) |
|
147 | + ); |
|
148 | + $state->nonce = ParagonIE_Sodium_Core32_Util::substr($string, 36, 12); |
|
149 | + $state->_pad = ParagonIE_Sodium_Core32_Util::substr($string, 48, 8); |
|
150 | + return $state; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + public function toString() |
|
157 | + { |
|
158 | + return $this->key . |
|
159 | + $this->getCounter() . |
|
160 | + $this->nonce . |
|
161 | + $this->_pad; |
|
162 | + } |
|
163 | 163 | } |
@@ -9,302 +9,302 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_Int64 |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array<int, int> - four 16-bit integers |
|
14 | - */ |
|
15 | - public $limbs = array(0, 0, 0, 0); |
|
16 | - |
|
17 | - /** |
|
18 | - * @var int |
|
19 | - */ |
|
20 | - public $overflow = 0; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var bool |
|
24 | - */ |
|
25 | - public $unsignedInt = false; |
|
26 | - |
|
27 | - /** |
|
28 | - * ParagonIE_Sodium_Core32_Int64 constructor. |
|
29 | - * @param array $array |
|
30 | - * @param bool $unsignedInt |
|
31 | - */ |
|
32 | - public function __construct($array = array(0, 0, 0, 0), $unsignedInt = false) |
|
33 | - { |
|
34 | - $this->limbs = array( |
|
35 | - (int) $array[0], |
|
36 | - (int) $array[1], |
|
37 | - (int) $array[2], |
|
38 | - (int) $array[3] |
|
39 | - ); |
|
40 | - $this->overflow = 0; |
|
41 | - $this->unsignedInt = $unsignedInt; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Adds two int64 objects |
|
46 | - * |
|
47 | - * @param ParagonIE_Sodium_Core32_Int64 $addend |
|
48 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | - */ |
|
50 | - public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
51 | - { |
|
52 | - $i0 = $this->limbs[0]; |
|
53 | - $i1 = $this->limbs[1]; |
|
54 | - $i2 = $this->limbs[2]; |
|
55 | - $i3 = $this->limbs[3]; |
|
56 | - $j0 = $addend->limbs[0]; |
|
57 | - $j1 = $addend->limbs[1]; |
|
58 | - $j2 = $addend->limbs[2]; |
|
59 | - $j3 = $addend->limbs[3]; |
|
60 | - |
|
61 | - $r3 = $i3 + ($j3 & 0xffff); |
|
62 | - $carry = $r3 >> 16; |
|
63 | - |
|
64 | - $r2 = $i2 + ($j2 & 0xffff) + $carry; |
|
65 | - $carry = $r2 >> 16; |
|
66 | - |
|
67 | - $r1 = $i1 + ($j1 & 0xffff) + $carry; |
|
68 | - $carry = $r1 >> 16; |
|
69 | - |
|
70 | - $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
71 | - $carry = $r0 >> 16; |
|
72 | - |
|
73 | - $r0 &= 0xffff; |
|
74 | - $r1 &= 0xffff; |
|
75 | - $r2 &= 0xffff; |
|
76 | - $r3 &= 0xffff; |
|
77 | - |
|
78 | - $return = new ParagonIE_Sodium_Core32_Int64( |
|
79 | - array($r0, $r1, $r2, $r3) |
|
80 | - ); |
|
81 | - $return->overflow = $carry; |
|
82 | - $return->unsignedInt = $this->unsignedInt; |
|
83 | - return $return; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Adds a normal integer to an int64 object |
|
88 | - * |
|
89 | - * @param int $int |
|
90 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
91 | - * @throws SodiumException |
|
92 | - * @throws TypeError |
|
93 | - */ |
|
94 | - public function addInt($int) |
|
95 | - { |
|
96 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
97 | - /** @var int $int */ |
|
98 | - $int = (int) $int; |
|
99 | - |
|
100 | - $i0 = $this->limbs[0]; |
|
101 | - $i1 = $this->limbs[1]; |
|
102 | - $i2 = $this->limbs[2]; |
|
103 | - $i3 = $this->limbs[3]; |
|
104 | - |
|
105 | - $r3 = $i3 + ($int & 0xffff); |
|
106 | - $carry = $r3 >> 16; |
|
107 | - |
|
108 | - $r2 = $i2 + (($int >> 16) & 0xffff) + $carry; |
|
109 | - $carry = $r2 >> 16; |
|
110 | - |
|
111 | - $r1 = $i1 + $carry; |
|
112 | - $carry = $r1 >> 16; |
|
113 | - |
|
114 | - $r0 = $i0 + $carry; |
|
115 | - $carry = $r0 >> 16; |
|
116 | - |
|
117 | - $r0 &= 0xffff; |
|
118 | - $r1 &= 0xffff; |
|
119 | - $r2 &= 0xffff; |
|
120 | - $r3 &= 0xffff; |
|
121 | - $return = new ParagonIE_Sodium_Core32_Int64( |
|
122 | - array($r0, $r1, $r2, $r3) |
|
123 | - ); |
|
124 | - $return->overflow = $carry; |
|
125 | - $return->unsignedInt = $this->unsignedInt; |
|
126 | - return $return; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param int $b |
|
131 | - * @return int |
|
132 | - */ |
|
133 | - public function compareInt($b = 0) |
|
134 | - { |
|
135 | - $gt = 0; |
|
136 | - $eq = 1; |
|
137 | - |
|
138 | - $i = 4; |
|
139 | - $j = 0; |
|
140 | - while ($i > 0) { |
|
141 | - --$i; |
|
142 | - /** @var int $x1 */ |
|
143 | - $x1 = $this->limbs[$i]; |
|
144 | - /** @var int $x2 */ |
|
145 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
146 | - /** int */ |
|
147 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
148 | - /** int */ |
|
149 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
150 | - } |
|
151 | - return ($gt + $gt - $eq) + 1; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @param int $b |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function isGreaterThan($b = 0) |
|
159 | - { |
|
160 | - return $this->compareInt($b) > 0; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param int $b |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public function isLessThanInt($b = 0) |
|
168 | - { |
|
169 | - return $this->compareInt($b) < 0; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param int $hi |
|
174 | - * @param int $lo |
|
175 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
176 | - */ |
|
177 | - public function mask64($hi = 0, $lo = 0) |
|
178 | - { |
|
179 | - /** @var int $a */ |
|
180 | - $a = ($hi >> 16) & 0xffff; |
|
181 | - /** @var int $b */ |
|
182 | - $b = ($hi) & 0xffff; |
|
183 | - /** @var int $c */ |
|
184 | - $c = ($lo >> 16) & 0xffff; |
|
185 | - /** @var int $d */ |
|
186 | - $d = ($lo & 0xffff); |
|
187 | - return new ParagonIE_Sodium_Core32_Int64( |
|
188 | - array( |
|
189 | - $this->limbs[0] & $a, |
|
190 | - $this->limbs[1] & $b, |
|
191 | - $this->limbs[2] & $c, |
|
192 | - $this->limbs[3] & $d |
|
193 | - ), |
|
194 | - $this->unsignedInt |
|
195 | - ); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * @param int $int |
|
200 | - * @param int $size |
|
201 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
202 | - * @throws SodiumException |
|
203 | - * @throws TypeError |
|
204 | - * @psalm-suppress MixedAssignment |
|
205 | - */ |
|
206 | - public function mulInt($int = 0, $size = 0) |
|
207 | - { |
|
208 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
209 | - return $this->mulIntFast($int); |
|
210 | - } |
|
211 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
212 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
213 | - /** @var int $int */ |
|
214 | - $int = (int) $int; |
|
215 | - /** @var int $size */ |
|
216 | - $size = (int) $size; |
|
217 | - |
|
218 | - if (!$size) { |
|
219 | - $size = 63; |
|
220 | - } |
|
221 | - |
|
222 | - $a = clone $this; |
|
223 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
224 | - $return->unsignedInt = $this->unsignedInt; |
|
225 | - |
|
226 | - // Initialize: |
|
227 | - $ret0 = 0; |
|
228 | - $ret1 = 0; |
|
229 | - $ret2 = 0; |
|
230 | - $ret3 = 0; |
|
231 | - $a0 = $a->limbs[0]; |
|
232 | - $a1 = $a->limbs[1]; |
|
233 | - $a2 = $a->limbs[2]; |
|
234 | - $a3 = $a->limbs[3]; |
|
235 | - |
|
236 | - /** @var int $size */ |
|
237 | - /** @var int $i */ |
|
238 | - for ($i = $size; $i >= 0; --$i) { |
|
239 | - $mask = -($int & 1); |
|
240 | - $x0 = $a0 & $mask; |
|
241 | - $x1 = $a1 & $mask; |
|
242 | - $x2 = $a2 & $mask; |
|
243 | - $x3 = $a3 & $mask; |
|
244 | - |
|
245 | - $ret3 += $x3; |
|
246 | - $c = $ret3 >> 16; |
|
247 | - |
|
248 | - $ret2 += $x2 + $c; |
|
249 | - $c = $ret2 >> 16; |
|
250 | - |
|
251 | - $ret1 += $x1 + $c; |
|
252 | - $c = $ret1 >> 16; |
|
253 | - |
|
254 | - $ret0 += $x0 + $c; |
|
255 | - |
|
256 | - $ret0 &= 0xffff; |
|
257 | - $ret1 &= 0xffff; |
|
258 | - $ret2 &= 0xffff; |
|
259 | - $ret3 &= 0xffff; |
|
260 | - |
|
261 | - $a3 = $a3 << 1; |
|
262 | - $x3 = $a3 >> 16; |
|
263 | - $a2 = ($a2 << 1) | $x3; |
|
264 | - $x2 = $a2 >> 16; |
|
265 | - $a1 = ($a1 << 1) | $x2; |
|
266 | - $x1 = $a1 >> 16; |
|
267 | - $a0 = ($a0 << 1) | $x1; |
|
268 | - $a0 &= 0xffff; |
|
269 | - $a1 &= 0xffff; |
|
270 | - $a2 &= 0xffff; |
|
271 | - $a3 &= 0xffff; |
|
272 | - |
|
273 | - $int >>= 1; |
|
274 | - } |
|
275 | - $return->limbs[0] = $ret0; |
|
276 | - $return->limbs[1] = $ret1; |
|
277 | - $return->limbs[2] = $ret2; |
|
278 | - $return->limbs[3] = $ret3; |
|
279 | - return $return; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @param ParagonIE_Sodium_Core32_Int64 $A |
|
284 | - * @param ParagonIE_Sodium_Core32_Int64 $B |
|
285 | - * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
286 | - * @throws SodiumException |
|
287 | - * @throws TypeError |
|
288 | - * @psalm-suppress MixedInferredReturnType |
|
289 | - */ |
|
290 | - public static function ctSelect( |
|
291 | - ParagonIE_Sodium_Core32_Int64 $A, |
|
292 | - ParagonIE_Sodium_Core32_Int64 $B |
|
293 | - ) { |
|
294 | - $a = clone $A; |
|
295 | - $b = clone $B; |
|
296 | - /** @var int $aNeg */ |
|
297 | - $aNeg = ($a->limbs[0] >> 15) & 1; |
|
298 | - /** @var int $bNeg */ |
|
299 | - $bNeg = ($b->limbs[0] >> 15) & 1; |
|
300 | - /** @var int $m */ |
|
301 | - $m = (-($aNeg & $bNeg)) | 1; |
|
302 | - /** @var int $swap */ |
|
303 | - $swap = $bNeg & ~$aNeg; |
|
304 | - /** @var int $d */ |
|
305 | - $d = -$swap; |
|
306 | - |
|
307 | - /* |
|
12 | + /** |
|
13 | + * @var array<int, int> - four 16-bit integers |
|
14 | + */ |
|
15 | + public $limbs = array(0, 0, 0, 0); |
|
16 | + |
|
17 | + /** |
|
18 | + * @var int |
|
19 | + */ |
|
20 | + public $overflow = 0; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var bool |
|
24 | + */ |
|
25 | + public $unsignedInt = false; |
|
26 | + |
|
27 | + /** |
|
28 | + * ParagonIE_Sodium_Core32_Int64 constructor. |
|
29 | + * @param array $array |
|
30 | + * @param bool $unsignedInt |
|
31 | + */ |
|
32 | + public function __construct($array = array(0, 0, 0, 0), $unsignedInt = false) |
|
33 | + { |
|
34 | + $this->limbs = array( |
|
35 | + (int) $array[0], |
|
36 | + (int) $array[1], |
|
37 | + (int) $array[2], |
|
38 | + (int) $array[3] |
|
39 | + ); |
|
40 | + $this->overflow = 0; |
|
41 | + $this->unsignedInt = $unsignedInt; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Adds two int64 objects |
|
46 | + * |
|
47 | + * @param ParagonIE_Sodium_Core32_Int64 $addend |
|
48 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | + */ |
|
50 | + public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
51 | + { |
|
52 | + $i0 = $this->limbs[0]; |
|
53 | + $i1 = $this->limbs[1]; |
|
54 | + $i2 = $this->limbs[2]; |
|
55 | + $i3 = $this->limbs[3]; |
|
56 | + $j0 = $addend->limbs[0]; |
|
57 | + $j1 = $addend->limbs[1]; |
|
58 | + $j2 = $addend->limbs[2]; |
|
59 | + $j3 = $addend->limbs[3]; |
|
60 | + |
|
61 | + $r3 = $i3 + ($j3 & 0xffff); |
|
62 | + $carry = $r3 >> 16; |
|
63 | + |
|
64 | + $r2 = $i2 + ($j2 & 0xffff) + $carry; |
|
65 | + $carry = $r2 >> 16; |
|
66 | + |
|
67 | + $r1 = $i1 + ($j1 & 0xffff) + $carry; |
|
68 | + $carry = $r1 >> 16; |
|
69 | + |
|
70 | + $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
71 | + $carry = $r0 >> 16; |
|
72 | + |
|
73 | + $r0 &= 0xffff; |
|
74 | + $r1 &= 0xffff; |
|
75 | + $r2 &= 0xffff; |
|
76 | + $r3 &= 0xffff; |
|
77 | + |
|
78 | + $return = new ParagonIE_Sodium_Core32_Int64( |
|
79 | + array($r0, $r1, $r2, $r3) |
|
80 | + ); |
|
81 | + $return->overflow = $carry; |
|
82 | + $return->unsignedInt = $this->unsignedInt; |
|
83 | + return $return; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Adds a normal integer to an int64 object |
|
88 | + * |
|
89 | + * @param int $int |
|
90 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
91 | + * @throws SodiumException |
|
92 | + * @throws TypeError |
|
93 | + */ |
|
94 | + public function addInt($int) |
|
95 | + { |
|
96 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
97 | + /** @var int $int */ |
|
98 | + $int = (int) $int; |
|
99 | + |
|
100 | + $i0 = $this->limbs[0]; |
|
101 | + $i1 = $this->limbs[1]; |
|
102 | + $i2 = $this->limbs[2]; |
|
103 | + $i3 = $this->limbs[3]; |
|
104 | + |
|
105 | + $r3 = $i3 + ($int & 0xffff); |
|
106 | + $carry = $r3 >> 16; |
|
107 | + |
|
108 | + $r2 = $i2 + (($int >> 16) & 0xffff) + $carry; |
|
109 | + $carry = $r2 >> 16; |
|
110 | + |
|
111 | + $r1 = $i1 + $carry; |
|
112 | + $carry = $r1 >> 16; |
|
113 | + |
|
114 | + $r0 = $i0 + $carry; |
|
115 | + $carry = $r0 >> 16; |
|
116 | + |
|
117 | + $r0 &= 0xffff; |
|
118 | + $r1 &= 0xffff; |
|
119 | + $r2 &= 0xffff; |
|
120 | + $r3 &= 0xffff; |
|
121 | + $return = new ParagonIE_Sodium_Core32_Int64( |
|
122 | + array($r0, $r1, $r2, $r3) |
|
123 | + ); |
|
124 | + $return->overflow = $carry; |
|
125 | + $return->unsignedInt = $this->unsignedInt; |
|
126 | + return $return; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param int $b |
|
131 | + * @return int |
|
132 | + */ |
|
133 | + public function compareInt($b = 0) |
|
134 | + { |
|
135 | + $gt = 0; |
|
136 | + $eq = 1; |
|
137 | + |
|
138 | + $i = 4; |
|
139 | + $j = 0; |
|
140 | + while ($i > 0) { |
|
141 | + --$i; |
|
142 | + /** @var int $x1 */ |
|
143 | + $x1 = $this->limbs[$i]; |
|
144 | + /** @var int $x2 */ |
|
145 | + $x2 = ($b >> ($j << 4)) & 0xffff; |
|
146 | + /** int */ |
|
147 | + $gt |= (($x2 - $x1) >> 8) & $eq; |
|
148 | + /** int */ |
|
149 | + $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
150 | + } |
|
151 | + return ($gt + $gt - $eq) + 1; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @param int $b |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function isGreaterThan($b = 0) |
|
159 | + { |
|
160 | + return $this->compareInt($b) > 0; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param int $b |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public function isLessThanInt($b = 0) |
|
168 | + { |
|
169 | + return $this->compareInt($b) < 0; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param int $hi |
|
174 | + * @param int $lo |
|
175 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
176 | + */ |
|
177 | + public function mask64($hi = 0, $lo = 0) |
|
178 | + { |
|
179 | + /** @var int $a */ |
|
180 | + $a = ($hi >> 16) & 0xffff; |
|
181 | + /** @var int $b */ |
|
182 | + $b = ($hi) & 0xffff; |
|
183 | + /** @var int $c */ |
|
184 | + $c = ($lo >> 16) & 0xffff; |
|
185 | + /** @var int $d */ |
|
186 | + $d = ($lo & 0xffff); |
|
187 | + return new ParagonIE_Sodium_Core32_Int64( |
|
188 | + array( |
|
189 | + $this->limbs[0] & $a, |
|
190 | + $this->limbs[1] & $b, |
|
191 | + $this->limbs[2] & $c, |
|
192 | + $this->limbs[3] & $d |
|
193 | + ), |
|
194 | + $this->unsignedInt |
|
195 | + ); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * @param int $int |
|
200 | + * @param int $size |
|
201 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
202 | + * @throws SodiumException |
|
203 | + * @throws TypeError |
|
204 | + * @psalm-suppress MixedAssignment |
|
205 | + */ |
|
206 | + public function mulInt($int = 0, $size = 0) |
|
207 | + { |
|
208 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
209 | + return $this->mulIntFast($int); |
|
210 | + } |
|
211 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
212 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
213 | + /** @var int $int */ |
|
214 | + $int = (int) $int; |
|
215 | + /** @var int $size */ |
|
216 | + $size = (int) $size; |
|
217 | + |
|
218 | + if (!$size) { |
|
219 | + $size = 63; |
|
220 | + } |
|
221 | + |
|
222 | + $a = clone $this; |
|
223 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
224 | + $return->unsignedInt = $this->unsignedInt; |
|
225 | + |
|
226 | + // Initialize: |
|
227 | + $ret0 = 0; |
|
228 | + $ret1 = 0; |
|
229 | + $ret2 = 0; |
|
230 | + $ret3 = 0; |
|
231 | + $a0 = $a->limbs[0]; |
|
232 | + $a1 = $a->limbs[1]; |
|
233 | + $a2 = $a->limbs[2]; |
|
234 | + $a3 = $a->limbs[3]; |
|
235 | + |
|
236 | + /** @var int $size */ |
|
237 | + /** @var int $i */ |
|
238 | + for ($i = $size; $i >= 0; --$i) { |
|
239 | + $mask = -($int & 1); |
|
240 | + $x0 = $a0 & $mask; |
|
241 | + $x1 = $a1 & $mask; |
|
242 | + $x2 = $a2 & $mask; |
|
243 | + $x3 = $a3 & $mask; |
|
244 | + |
|
245 | + $ret3 += $x3; |
|
246 | + $c = $ret3 >> 16; |
|
247 | + |
|
248 | + $ret2 += $x2 + $c; |
|
249 | + $c = $ret2 >> 16; |
|
250 | + |
|
251 | + $ret1 += $x1 + $c; |
|
252 | + $c = $ret1 >> 16; |
|
253 | + |
|
254 | + $ret0 += $x0 + $c; |
|
255 | + |
|
256 | + $ret0 &= 0xffff; |
|
257 | + $ret1 &= 0xffff; |
|
258 | + $ret2 &= 0xffff; |
|
259 | + $ret3 &= 0xffff; |
|
260 | + |
|
261 | + $a3 = $a3 << 1; |
|
262 | + $x3 = $a3 >> 16; |
|
263 | + $a2 = ($a2 << 1) | $x3; |
|
264 | + $x2 = $a2 >> 16; |
|
265 | + $a1 = ($a1 << 1) | $x2; |
|
266 | + $x1 = $a1 >> 16; |
|
267 | + $a0 = ($a0 << 1) | $x1; |
|
268 | + $a0 &= 0xffff; |
|
269 | + $a1 &= 0xffff; |
|
270 | + $a2 &= 0xffff; |
|
271 | + $a3 &= 0xffff; |
|
272 | + |
|
273 | + $int >>= 1; |
|
274 | + } |
|
275 | + $return->limbs[0] = $ret0; |
|
276 | + $return->limbs[1] = $ret1; |
|
277 | + $return->limbs[2] = $ret2; |
|
278 | + $return->limbs[3] = $ret3; |
|
279 | + return $return; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @param ParagonIE_Sodium_Core32_Int64 $A |
|
284 | + * @param ParagonIE_Sodium_Core32_Int64 $B |
|
285 | + * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
286 | + * @throws SodiumException |
|
287 | + * @throws TypeError |
|
288 | + * @psalm-suppress MixedInferredReturnType |
|
289 | + */ |
|
290 | + public static function ctSelect( |
|
291 | + ParagonIE_Sodium_Core32_Int64 $A, |
|
292 | + ParagonIE_Sodium_Core32_Int64 $B |
|
293 | + ) { |
|
294 | + $a = clone $A; |
|
295 | + $b = clone $B; |
|
296 | + /** @var int $aNeg */ |
|
297 | + $aNeg = ($a->limbs[0] >> 15) & 1; |
|
298 | + /** @var int $bNeg */ |
|
299 | + $bNeg = ($b->limbs[0] >> 15) & 1; |
|
300 | + /** @var int $m */ |
|
301 | + $m = (-($aNeg & $bNeg)) | 1; |
|
302 | + /** @var int $swap */ |
|
303 | + $swap = $bNeg & ~$aNeg; |
|
304 | + /** @var int $d */ |
|
305 | + $d = -$swap; |
|
306 | + |
|
307 | + /* |
|
308 | 308 | if ($bNeg && !$aNeg) { |
309 | 309 | $a = clone $int; |
310 | 310 | $b = clone $this; |
@@ -313,754 +313,754 @@ discard block |
||
313 | 313 | $b = $int->mulInt(-1); |
314 | 314 | } |
315 | 315 | */ |
316 | - $x = $a->xorInt64($b)->mask64($d, $d); |
|
317 | - return array( |
|
318 | - $a->xorInt64($x)->mulInt($m), |
|
319 | - $b->xorInt64($x)->mulInt($m) |
|
320 | - ); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * @param array<int, int> $a |
|
325 | - * @param array<int, int> $b |
|
326 | - * @param int $baseLog2 |
|
327 | - * @return array<int, int> |
|
328 | - */ |
|
329 | - public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
330 | - { |
|
331 | - $a_l = count($a); |
|
332 | - $b_l = count($b); |
|
333 | - /** @var array<int, int> $r */ |
|
334 | - $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
335 | - $base = 1 << $baseLog2; |
|
336 | - for ($i = 0; $i < $a_l; ++$i) { |
|
337 | - $a_i = $a[$i]; |
|
338 | - for ($j = 0; $j < $a_l; ++$j) { |
|
339 | - $b_j = $b[$j]; |
|
340 | - $product = ($a_i * $b_j) + $r[$i + $j]; |
|
341 | - $carry = ($product >> $baseLog2 & 0xffff); |
|
342 | - $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
343 | - $r[$i + $j + 1] += $carry; |
|
344 | - } |
|
345 | - } |
|
346 | - return array_slice($r, 0, 5); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @param int $int |
|
351 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
352 | - */ |
|
353 | - public function mulIntFast($int) |
|
354 | - { |
|
355 | - // Handle negative numbers |
|
356 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
357 | - $bNeg = ($int >> 31) & 1; |
|
358 | - $a = array_reverse($this->limbs); |
|
359 | - $b = array( |
|
360 | - $int & 0xffff, |
|
361 | - ($int >> 16) & 0xffff, |
|
362 | - -$bNeg & 0xffff, |
|
363 | - -$bNeg & 0xffff |
|
364 | - ); |
|
365 | - if ($aNeg) { |
|
366 | - for ($i = 0; $i < 4; ++$i) { |
|
367 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
368 | - } |
|
369 | - ++$a[0]; |
|
370 | - } |
|
371 | - if ($bNeg) { |
|
372 | - for ($i = 0; $i < 4; ++$i) { |
|
373 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
374 | - } |
|
375 | - ++$b[0]; |
|
376 | - } |
|
377 | - // Multiply |
|
378 | - $res = $this->multiplyLong($a, $b); |
|
379 | - |
|
380 | - // Re-apply negation to results |
|
381 | - if ($aNeg !== $bNeg) { |
|
382 | - for ($i = 0; $i < 4; ++$i) { |
|
383 | - $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
384 | - } |
|
385 | - // Handle integer overflow |
|
386 | - $c = 1; |
|
387 | - for ($i = 0; $i < 4; ++$i) { |
|
388 | - $res[$i] += $c; |
|
389 | - $c = $res[$i] >> 16; |
|
390 | - $res[$i] &= 0xffff; |
|
391 | - } |
|
392 | - } |
|
393 | - |
|
394 | - // Return our values |
|
395 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
396 | - $return->limbs = array( |
|
397 | - $res[3] & 0xffff, |
|
398 | - $res[2] & 0xffff, |
|
399 | - $res[1] & 0xffff, |
|
400 | - $res[0] & 0xffff |
|
401 | - ); |
|
402 | - if (count($res) > 4) { |
|
403 | - $return->overflow = $res[4] & 0xffff; |
|
404 | - } |
|
405 | - $return->unsignedInt = $this->unsignedInt; |
|
406 | - return $return; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * @param ParagonIE_Sodium_Core32_Int64 $right |
|
411 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
412 | - */ |
|
413 | - public function mulInt64Fast(ParagonIE_Sodium_Core32_Int64 $right) |
|
414 | - { |
|
415 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
416 | - $bNeg = ($right->limbs[0] >> 15) & 1; |
|
417 | - |
|
418 | - $a = array_reverse($this->limbs); |
|
419 | - $b = array_reverse($right->limbs); |
|
420 | - if ($aNeg) { |
|
421 | - for ($i = 0; $i < 4; ++$i) { |
|
422 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
423 | - } |
|
424 | - ++$a[0]; |
|
425 | - } |
|
426 | - if ($bNeg) { |
|
427 | - for ($i = 0; $i < 4; ++$i) { |
|
428 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
429 | - } |
|
430 | - ++$b[0]; |
|
431 | - } |
|
432 | - $res = $this->multiplyLong($a, $b); |
|
433 | - if ($aNeg !== $bNeg) { |
|
434 | - if ($aNeg !== $bNeg) { |
|
435 | - for ($i = 0; $i < 4; ++$i) { |
|
436 | - $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
437 | - } |
|
438 | - $c = 1; |
|
439 | - for ($i = 0; $i < 4; ++$i) { |
|
440 | - $res[$i] += $c; |
|
441 | - $c = $res[$i] >> 16; |
|
442 | - $res[$i] &= 0xffff; |
|
443 | - } |
|
444 | - } |
|
445 | - } |
|
446 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
447 | - $return->limbs = array( |
|
448 | - $res[3] & 0xffff, |
|
449 | - $res[2] & 0xffff, |
|
450 | - $res[1] & 0xffff, |
|
451 | - $res[0] & 0xffff |
|
452 | - ); |
|
453 | - if (count($res) > 4) { |
|
454 | - $return->overflow = $res[4]; |
|
455 | - } |
|
456 | - return $return; |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * @param ParagonIE_Sodium_Core32_Int64 $int |
|
461 | - * @param int $size |
|
462 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
463 | - * @throws SodiumException |
|
464 | - * @throws TypeError |
|
465 | - * @psalm-suppress MixedAssignment |
|
466 | - */ |
|
467 | - public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
468 | - { |
|
469 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
470 | - return $this->mulInt64Fast($int); |
|
471 | - } |
|
472 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
473 | - if (!$size) { |
|
474 | - $size = 63; |
|
475 | - } |
|
476 | - list($a, $b) = self::ctSelect($this, $int); |
|
477 | - |
|
478 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
479 | - $return->unsignedInt = $this->unsignedInt; |
|
480 | - |
|
481 | - // Initialize: |
|
482 | - $ret0 = 0; |
|
483 | - $ret1 = 0; |
|
484 | - $ret2 = 0; |
|
485 | - $ret3 = 0; |
|
486 | - $a0 = $a->limbs[0]; |
|
487 | - $a1 = $a->limbs[1]; |
|
488 | - $a2 = $a->limbs[2]; |
|
489 | - $a3 = $a->limbs[3]; |
|
490 | - $b0 = $b->limbs[0]; |
|
491 | - $b1 = $b->limbs[1]; |
|
492 | - $b2 = $b->limbs[2]; |
|
493 | - $b3 = $b->limbs[3]; |
|
494 | - |
|
495 | - /** @var int $size */ |
|
496 | - /** @var int $i */ |
|
497 | - for ($i = (int) $size; $i >= 0; --$i) { |
|
498 | - $mask = -($b3 & 1); |
|
499 | - $x0 = $a0 & $mask; |
|
500 | - $x1 = $a1 & $mask; |
|
501 | - $x2 = $a2 & $mask; |
|
502 | - $x3 = $a3 & $mask; |
|
503 | - |
|
504 | - $ret3 += $x3; |
|
505 | - $c = $ret3 >> 16; |
|
506 | - |
|
507 | - $ret2 += $x2 + $c; |
|
508 | - $c = $ret2 >> 16; |
|
509 | - |
|
510 | - $ret1 += $x1 + $c; |
|
511 | - $c = $ret1 >> 16; |
|
512 | - |
|
513 | - $ret0 += $x0 + $c; |
|
514 | - |
|
515 | - $ret0 &= 0xffff; |
|
516 | - $ret1 &= 0xffff; |
|
517 | - $ret2 &= 0xffff; |
|
518 | - $ret3 &= 0xffff; |
|
519 | - |
|
520 | - $a3 = $a3 << 1; |
|
521 | - $x3 = $a3 >> 16; |
|
522 | - $a2 = ($a2 << 1) | $x3; |
|
523 | - $x2 = $a2 >> 16; |
|
524 | - $a1 = ($a1 << 1) | $x2; |
|
525 | - $x1 = $a1 >> 16; |
|
526 | - $a0 = ($a0 << 1) | $x1; |
|
527 | - $a0 &= 0xffff; |
|
528 | - $a1 &= 0xffff; |
|
529 | - $a2 &= 0xffff; |
|
530 | - $a3 &= 0xffff; |
|
531 | - |
|
532 | - $x0 = ($b0 & 1) << 16; |
|
533 | - $x1 = ($b1 & 1) << 16; |
|
534 | - $x2 = ($b2 & 1) << 16; |
|
535 | - |
|
536 | - $b0 = ($b0 >> 1); |
|
537 | - $b1 = (($b1 | $x0) >> 1); |
|
538 | - $b2 = (($b2 | $x1) >> 1); |
|
539 | - $b3 = (($b3 | $x2) >> 1); |
|
540 | - |
|
541 | - $b0 &= 0xffff; |
|
542 | - $b1 &= 0xffff; |
|
543 | - $b2 &= 0xffff; |
|
544 | - $b3 &= 0xffff; |
|
545 | - |
|
546 | - } |
|
547 | - $return->limbs[0] = $ret0; |
|
548 | - $return->limbs[1] = $ret1; |
|
549 | - $return->limbs[2] = $ret2; |
|
550 | - $return->limbs[3] = $ret3; |
|
551 | - |
|
552 | - return $return; |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * OR this 64-bit integer with another. |
|
557 | - * |
|
558 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
559 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
560 | - */ |
|
561 | - public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
562 | - { |
|
563 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
564 | - $return->unsignedInt = $this->unsignedInt; |
|
565 | - $return->limbs = array( |
|
566 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
567 | - (int) ($this->limbs[1] | $b->limbs[1]), |
|
568 | - (int) ($this->limbs[2] | $b->limbs[2]), |
|
569 | - (int) ($this->limbs[3] | $b->limbs[3]) |
|
570 | - ); |
|
571 | - return $return; |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * @param int $c |
|
576 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
577 | - * @throws SodiumException |
|
578 | - * @throws TypeError |
|
579 | - * @psalm-suppress MixedArrayAccess |
|
580 | - */ |
|
581 | - public function rotateLeft($c = 0) |
|
582 | - { |
|
583 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
584 | - /** @var int $c */ |
|
585 | - $c = (int) $c; |
|
586 | - |
|
587 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
588 | - $return->unsignedInt = $this->unsignedInt; |
|
589 | - $c &= 63; |
|
590 | - if ($c === 0) { |
|
591 | - // NOP, but we want a copy. |
|
592 | - $return->limbs = $this->limbs; |
|
593 | - } else { |
|
594 | - /** @var array<int, int> $limbs */ |
|
595 | - $limbs =& $return->limbs; |
|
596 | - |
|
597 | - /** @var array<int, int> $myLimbs */ |
|
598 | - $myLimbs =& $this->limbs; |
|
599 | - |
|
600 | - /** @var int $idx_shift */ |
|
601 | - $idx_shift = ($c >> 4) & 3; |
|
602 | - /** @var int $sub_shift */ |
|
603 | - $sub_shift = $c & 15; |
|
604 | - |
|
605 | - for ($i = 3; $i >= 0; --$i) { |
|
606 | - /** @var int $j */ |
|
607 | - $j = ($i + $idx_shift) & 3; |
|
608 | - /** @var int $k */ |
|
609 | - $k = ($i + $idx_shift + 1) & 3; |
|
610 | - $limbs[$i] = (int) ( |
|
611 | - ( |
|
612 | - ((int) ($myLimbs[$j]) << $sub_shift) |
|
613 | - | |
|
614 | - ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
615 | - ) & 0xffff |
|
616 | - ); |
|
617 | - } |
|
618 | - } |
|
619 | - return $return; |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Rotate to the right |
|
624 | - * |
|
625 | - * @param int $c |
|
626 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
627 | - * @throws SodiumException |
|
628 | - * @throws TypeError |
|
629 | - * @psalm-suppress MixedArrayAccess |
|
630 | - */ |
|
631 | - public function rotateRight($c = 0) |
|
632 | - { |
|
633 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
634 | - /** @var int $c */ |
|
635 | - $c = (int) $c; |
|
636 | - |
|
637 | - /** @var ParagonIE_Sodium_Core32_Int64 $return */ |
|
638 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
639 | - $return->unsignedInt = $this->unsignedInt; |
|
640 | - $c &= 63; |
|
641 | - /** @var int $c */ |
|
642 | - if ($c === 0) { |
|
643 | - // NOP, but we want a copy. |
|
644 | - $return->limbs = $this->limbs; |
|
645 | - } else { |
|
646 | - /** @var array<int, int> $limbs */ |
|
647 | - $limbs =& $return->limbs; |
|
648 | - |
|
649 | - /** @var array<int, int> $myLimbs */ |
|
650 | - $myLimbs =& $this->limbs; |
|
651 | - |
|
652 | - /** @var int $idx_shift */ |
|
653 | - $idx_shift = ($c >> 4) & 3; |
|
654 | - /** @var int $sub_shift */ |
|
655 | - $sub_shift = $c & 15; |
|
656 | - |
|
657 | - for ($i = 3; $i >= 0; --$i) { |
|
658 | - /** @var int $j */ |
|
659 | - $j = ($i - $idx_shift) & 3; |
|
660 | - /** @var int $k */ |
|
661 | - $k = ($i - $idx_shift - 1) & 3; |
|
662 | - $limbs[$i] = (int) ( |
|
663 | - ( |
|
664 | - ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
665 | - | |
|
666 | - ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
667 | - ) & 0xffff |
|
668 | - ); |
|
669 | - } |
|
670 | - } |
|
671 | - return $return; |
|
672 | - } |
|
673 | - /** |
|
674 | - * @param int $c |
|
675 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
676 | - * @throws SodiumException |
|
677 | - * @throws TypeError |
|
678 | - */ |
|
679 | - public function shiftLeft($c = 0) |
|
680 | - { |
|
681 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
682 | - /** @var int $c */ |
|
683 | - $c = (int) $c; |
|
684 | - |
|
685 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
686 | - $return->unsignedInt = $this->unsignedInt; |
|
687 | - $c &= 63; |
|
688 | - |
|
689 | - if ($c >= 16) { |
|
690 | - if ($c >= 48) { |
|
691 | - $return->limbs = array( |
|
692 | - $this->limbs[3], 0, 0, 0 |
|
693 | - ); |
|
694 | - } elseif ($c >= 32) { |
|
695 | - $return->limbs = array( |
|
696 | - $this->limbs[2], $this->limbs[3], 0, 0 |
|
697 | - ); |
|
698 | - } else { |
|
699 | - $return->limbs = array( |
|
700 | - $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 |
|
701 | - ); |
|
702 | - } |
|
703 | - return $return->shiftLeft($c & 15); |
|
704 | - } |
|
705 | - if ($c === 0) { |
|
706 | - $return->limbs = $this->limbs; |
|
707 | - } elseif ($c < 0) { |
|
708 | - /** @var int $c */ |
|
709 | - return $this->shiftRight(-$c); |
|
710 | - } else { |
|
711 | - if (!is_int($c)) { |
|
712 | - throw new TypeError(); |
|
713 | - } |
|
714 | - /** @var int $carry */ |
|
715 | - $carry = 0; |
|
716 | - for ($i = 3; $i >= 0; --$i) { |
|
717 | - /** @var int $tmp */ |
|
718 | - $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); |
|
719 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
720 | - /** @var int $carry */ |
|
721 | - $carry = $tmp >> 16; |
|
722 | - } |
|
723 | - } |
|
724 | - return $return; |
|
725 | - } |
|
726 | - |
|
727 | - /** |
|
728 | - * @param int $c |
|
729 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
730 | - * @throws SodiumException |
|
731 | - * @throws TypeError |
|
732 | - */ |
|
733 | - public function shiftRight($c = 0) |
|
734 | - { |
|
735 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
736 | - $c = (int) $c; |
|
737 | - /** @var int $c */ |
|
738 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
739 | - $return->unsignedInt = $this->unsignedInt; |
|
740 | - $c &= 63; |
|
741 | - |
|
742 | - $negative = -(($this->limbs[0] >> 15) & 1); |
|
743 | - if ($c >= 16) { |
|
744 | - if ($c >= 48) { |
|
745 | - $return->limbs = array( |
|
746 | - (int) ($negative & 0xffff), |
|
747 | - (int) ($negative & 0xffff), |
|
748 | - (int) ($negative & 0xffff), |
|
749 | - (int) $this->limbs[0] |
|
750 | - ); |
|
751 | - } elseif ($c >= 32) { |
|
752 | - $return->limbs = array( |
|
753 | - (int) ($negative & 0xffff), |
|
754 | - (int) ($negative & 0xffff), |
|
755 | - (int) $this->limbs[0], |
|
756 | - (int) $this->limbs[1] |
|
757 | - ); |
|
758 | - } else { |
|
759 | - $return->limbs = array( |
|
760 | - (int) ($negative & 0xffff), |
|
761 | - (int) $this->limbs[0], |
|
762 | - (int) $this->limbs[1], |
|
763 | - (int) $this->limbs[2] |
|
764 | - ); |
|
765 | - } |
|
766 | - return $return->shiftRight($c & 15); |
|
767 | - } |
|
768 | - |
|
769 | - if ($c === 0) { |
|
770 | - $return->limbs = $this->limbs; |
|
771 | - } elseif ($c < 0) { |
|
772 | - return $this->shiftLeft(-$c); |
|
773 | - } else { |
|
774 | - if (!is_int($c)) { |
|
775 | - throw new TypeError(); |
|
776 | - } |
|
777 | - /** @var int $carryRight */ |
|
778 | - $carryRight = ($negative & 0xffff); |
|
779 | - $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); |
|
780 | - for ($i = 0; $i < 4; ++$i) { |
|
781 | - $return->limbs[$i] = (int) ( |
|
782 | - (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff |
|
783 | - ); |
|
784 | - $carryRight = (int) ($this->limbs[$i] & $mask); |
|
785 | - } |
|
786 | - } |
|
787 | - return $return; |
|
788 | - } |
|
789 | - |
|
790 | - |
|
791 | - /** |
|
792 | - * Subtract a normal integer from an int64 object. |
|
793 | - * |
|
794 | - * @param int $int |
|
795 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
796 | - * @throws SodiumException |
|
797 | - * @throws TypeError |
|
798 | - */ |
|
799 | - public function subInt($int) |
|
800 | - { |
|
801 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
802 | - $int = (int) $int; |
|
803 | - |
|
804 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
805 | - $return->unsignedInt = $this->unsignedInt; |
|
806 | - |
|
807 | - /** @var int $carry */ |
|
808 | - $carry = 0; |
|
809 | - for ($i = 3; $i >= 0; --$i) { |
|
810 | - /** @var int $tmp */ |
|
811 | - $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; |
|
812 | - /** @var int $carry */ |
|
813 | - $carry = $tmp >> 16; |
|
814 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
815 | - } |
|
816 | - return $return; |
|
817 | - } |
|
818 | - |
|
819 | - /** |
|
820 | - * The difference between two Int64 objects. |
|
821 | - * |
|
822 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
823 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
824 | - */ |
|
825 | - public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
826 | - { |
|
827 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
828 | - $return->unsignedInt = $this->unsignedInt; |
|
829 | - /** @var int $carry */ |
|
830 | - $carry = 0; |
|
831 | - for ($i = 3; $i >= 0; --$i) { |
|
832 | - /** @var int $tmp */ |
|
833 | - $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; |
|
834 | - /** @var int $carry */ |
|
835 | - $carry = ($tmp >> 16); |
|
836 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
837 | - } |
|
838 | - return $return; |
|
839 | - } |
|
840 | - |
|
841 | - /** |
|
842 | - * XOR this 64-bit integer with another. |
|
843 | - * |
|
844 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
845 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
846 | - */ |
|
847 | - public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
848 | - { |
|
849 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
850 | - $return->unsignedInt = $this->unsignedInt; |
|
851 | - $return->limbs = array( |
|
852 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
853 | - (int) ($this->limbs[1] ^ $b->limbs[1]), |
|
854 | - (int) ($this->limbs[2] ^ $b->limbs[2]), |
|
855 | - (int) ($this->limbs[3] ^ $b->limbs[3]) |
|
856 | - ); |
|
857 | - return $return; |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * @param int $low |
|
862 | - * @param int $high |
|
863 | - * @return self |
|
864 | - * @throws SodiumException |
|
865 | - * @throws TypeError |
|
866 | - */ |
|
867 | - public static function fromInts($low, $high) |
|
868 | - { |
|
869 | - ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
870 | - ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
|
871 | - |
|
872 | - $high = (int) $high; |
|
873 | - $low = (int) $low; |
|
874 | - return new ParagonIE_Sodium_Core32_Int64( |
|
875 | - array( |
|
876 | - (int) (($high >> 16) & 0xffff), |
|
877 | - (int) ($high & 0xffff), |
|
878 | - (int) (($low >> 16) & 0xffff), |
|
879 | - (int) ($low & 0xffff) |
|
880 | - ) |
|
881 | - ); |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * @param int $low |
|
886 | - * @return self |
|
887 | - * @throws SodiumException |
|
888 | - * @throws TypeError |
|
889 | - */ |
|
890 | - public static function fromInt($low) |
|
891 | - { |
|
892 | - ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
893 | - $low = (int) $low; |
|
894 | - |
|
895 | - return new ParagonIE_Sodium_Core32_Int64( |
|
896 | - array( |
|
897 | - 0, |
|
898 | - 0, |
|
899 | - (int) (($low >> 16) & 0xffff), |
|
900 | - (int) ($low & 0xffff) |
|
901 | - ) |
|
902 | - ); |
|
903 | - } |
|
904 | - |
|
905 | - /** |
|
906 | - * @return int |
|
907 | - */ |
|
908 | - public function toInt() |
|
909 | - { |
|
910 | - return (int) ( |
|
911 | - (($this->limbs[2] & 0xffff) << 16) |
|
912 | - | |
|
913 | - ($this->limbs[3] & 0xffff) |
|
914 | - ); |
|
915 | - } |
|
916 | - |
|
917 | - /** |
|
918 | - * @param string $string |
|
919 | - * @return self |
|
920 | - * @throws SodiumException |
|
921 | - * @throws TypeError |
|
922 | - */ |
|
923 | - public static function fromString($string) |
|
924 | - { |
|
925 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
926 | - $string = (string) $string; |
|
927 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
928 | - throw new RangeException( |
|
929 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
930 | - ); |
|
931 | - } |
|
932 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
933 | - |
|
934 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
935 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
936 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
937 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
938 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); |
|
939 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); |
|
940 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); |
|
941 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); |
|
942 | - return $return; |
|
943 | - } |
|
944 | - |
|
945 | - /** |
|
946 | - * @param string $string |
|
947 | - * @return self |
|
948 | - * @throws SodiumException |
|
949 | - * @throws TypeError |
|
950 | - */ |
|
951 | - public static function fromReverseString($string) |
|
952 | - { |
|
953 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
954 | - $string = (string) $string; |
|
955 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
956 | - throw new RangeException( |
|
957 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
958 | - ); |
|
959 | - } |
|
960 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
961 | - |
|
962 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); |
|
963 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); |
|
964 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); |
|
965 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); |
|
966 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
967 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
968 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
969 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
970 | - return $return; |
|
971 | - } |
|
972 | - |
|
973 | - /** |
|
974 | - * @return array<int, int> |
|
975 | - */ |
|
976 | - public function toArray() |
|
977 | - { |
|
978 | - return array( |
|
979 | - (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
|
980 | - (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
|
981 | - ); |
|
982 | - } |
|
983 | - |
|
984 | - /** |
|
985 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
986 | - */ |
|
987 | - public function toInt32() |
|
988 | - { |
|
989 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
990 | - $return->limbs[0] = (int) ($this->limbs[2]); |
|
991 | - $return->limbs[1] = (int) ($this->limbs[3]); |
|
992 | - $return->unsignedInt = $this->unsignedInt; |
|
993 | - $return->overflow = (int) (ParagonIE_Sodium_Core32_Util::abs($this->limbs[1], 16) & 0xffff); |
|
994 | - return $return; |
|
995 | - } |
|
996 | - |
|
997 | - /** |
|
998 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
999 | - */ |
|
1000 | - public function toInt64() |
|
1001 | - { |
|
1002 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
1003 | - $return->limbs[0] = (int) ($this->limbs[0]); |
|
1004 | - $return->limbs[1] = (int) ($this->limbs[1]); |
|
1005 | - $return->limbs[2] = (int) ($this->limbs[2]); |
|
1006 | - $return->limbs[3] = (int) ($this->limbs[3]); |
|
1007 | - $return->unsignedInt = $this->unsignedInt; |
|
1008 | - $return->overflow = ParagonIE_Sodium_Core32_Util::abs($this->overflow); |
|
1009 | - return $return; |
|
1010 | - } |
|
1011 | - |
|
1012 | - /** |
|
1013 | - * @param bool $bool |
|
1014 | - * @return self |
|
1015 | - */ |
|
1016 | - public function setUnsignedInt($bool = false) |
|
1017 | - { |
|
1018 | - $this->unsignedInt = !empty($bool); |
|
1019 | - return $this; |
|
1020 | - } |
|
1021 | - |
|
1022 | - /** |
|
1023 | - * @return string |
|
1024 | - * @throws TypeError |
|
1025 | - */ |
|
1026 | - public function toString() |
|
1027 | - { |
|
1028 | - return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
1029 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
1030 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
1031 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
1032 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
1033 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
1034 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
1035 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); |
|
1036 | - } |
|
1037 | - |
|
1038 | - /** |
|
1039 | - * @return string |
|
1040 | - * @throws TypeError |
|
1041 | - */ |
|
1042 | - public function toReverseString() |
|
1043 | - { |
|
1044 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
|
1045 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
1046 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
1047 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
1048 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
1049 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
1050 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
1051 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
1052 | - } |
|
1053 | - |
|
1054 | - /** |
|
1055 | - * @return string |
|
1056 | - */ |
|
1057 | - public function __toString() |
|
1058 | - { |
|
1059 | - try { |
|
1060 | - return $this->toString(); |
|
1061 | - } catch (TypeError $ex) { |
|
1062 | - // PHP engine can't handle exceptions from __toString() |
|
1063 | - return ''; |
|
1064 | - } |
|
1065 | - } |
|
316 | + $x = $a->xorInt64($b)->mask64($d, $d); |
|
317 | + return array( |
|
318 | + $a->xorInt64($x)->mulInt($m), |
|
319 | + $b->xorInt64($x)->mulInt($m) |
|
320 | + ); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * @param array<int, int> $a |
|
325 | + * @param array<int, int> $b |
|
326 | + * @param int $baseLog2 |
|
327 | + * @return array<int, int> |
|
328 | + */ |
|
329 | + public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
330 | + { |
|
331 | + $a_l = count($a); |
|
332 | + $b_l = count($b); |
|
333 | + /** @var array<int, int> $r */ |
|
334 | + $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
335 | + $base = 1 << $baseLog2; |
|
336 | + for ($i = 0; $i < $a_l; ++$i) { |
|
337 | + $a_i = $a[$i]; |
|
338 | + for ($j = 0; $j < $a_l; ++$j) { |
|
339 | + $b_j = $b[$j]; |
|
340 | + $product = ($a_i * $b_j) + $r[$i + $j]; |
|
341 | + $carry = ($product >> $baseLog2 & 0xffff); |
|
342 | + $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
343 | + $r[$i + $j + 1] += $carry; |
|
344 | + } |
|
345 | + } |
|
346 | + return array_slice($r, 0, 5); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @param int $int |
|
351 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
352 | + */ |
|
353 | + public function mulIntFast($int) |
|
354 | + { |
|
355 | + // Handle negative numbers |
|
356 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
357 | + $bNeg = ($int >> 31) & 1; |
|
358 | + $a = array_reverse($this->limbs); |
|
359 | + $b = array( |
|
360 | + $int & 0xffff, |
|
361 | + ($int >> 16) & 0xffff, |
|
362 | + -$bNeg & 0xffff, |
|
363 | + -$bNeg & 0xffff |
|
364 | + ); |
|
365 | + if ($aNeg) { |
|
366 | + for ($i = 0; $i < 4; ++$i) { |
|
367 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
368 | + } |
|
369 | + ++$a[0]; |
|
370 | + } |
|
371 | + if ($bNeg) { |
|
372 | + for ($i = 0; $i < 4; ++$i) { |
|
373 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
374 | + } |
|
375 | + ++$b[0]; |
|
376 | + } |
|
377 | + // Multiply |
|
378 | + $res = $this->multiplyLong($a, $b); |
|
379 | + |
|
380 | + // Re-apply negation to results |
|
381 | + if ($aNeg !== $bNeg) { |
|
382 | + for ($i = 0; $i < 4; ++$i) { |
|
383 | + $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
384 | + } |
|
385 | + // Handle integer overflow |
|
386 | + $c = 1; |
|
387 | + for ($i = 0; $i < 4; ++$i) { |
|
388 | + $res[$i] += $c; |
|
389 | + $c = $res[$i] >> 16; |
|
390 | + $res[$i] &= 0xffff; |
|
391 | + } |
|
392 | + } |
|
393 | + |
|
394 | + // Return our values |
|
395 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
396 | + $return->limbs = array( |
|
397 | + $res[3] & 0xffff, |
|
398 | + $res[2] & 0xffff, |
|
399 | + $res[1] & 0xffff, |
|
400 | + $res[0] & 0xffff |
|
401 | + ); |
|
402 | + if (count($res) > 4) { |
|
403 | + $return->overflow = $res[4] & 0xffff; |
|
404 | + } |
|
405 | + $return->unsignedInt = $this->unsignedInt; |
|
406 | + return $return; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * @param ParagonIE_Sodium_Core32_Int64 $right |
|
411 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
412 | + */ |
|
413 | + public function mulInt64Fast(ParagonIE_Sodium_Core32_Int64 $right) |
|
414 | + { |
|
415 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
416 | + $bNeg = ($right->limbs[0] >> 15) & 1; |
|
417 | + |
|
418 | + $a = array_reverse($this->limbs); |
|
419 | + $b = array_reverse($right->limbs); |
|
420 | + if ($aNeg) { |
|
421 | + for ($i = 0; $i < 4; ++$i) { |
|
422 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
423 | + } |
|
424 | + ++$a[0]; |
|
425 | + } |
|
426 | + if ($bNeg) { |
|
427 | + for ($i = 0; $i < 4; ++$i) { |
|
428 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
429 | + } |
|
430 | + ++$b[0]; |
|
431 | + } |
|
432 | + $res = $this->multiplyLong($a, $b); |
|
433 | + if ($aNeg !== $bNeg) { |
|
434 | + if ($aNeg !== $bNeg) { |
|
435 | + for ($i = 0; $i < 4; ++$i) { |
|
436 | + $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
437 | + } |
|
438 | + $c = 1; |
|
439 | + for ($i = 0; $i < 4; ++$i) { |
|
440 | + $res[$i] += $c; |
|
441 | + $c = $res[$i] >> 16; |
|
442 | + $res[$i] &= 0xffff; |
|
443 | + } |
|
444 | + } |
|
445 | + } |
|
446 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
447 | + $return->limbs = array( |
|
448 | + $res[3] & 0xffff, |
|
449 | + $res[2] & 0xffff, |
|
450 | + $res[1] & 0xffff, |
|
451 | + $res[0] & 0xffff |
|
452 | + ); |
|
453 | + if (count($res) > 4) { |
|
454 | + $return->overflow = $res[4]; |
|
455 | + } |
|
456 | + return $return; |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * @param ParagonIE_Sodium_Core32_Int64 $int |
|
461 | + * @param int $size |
|
462 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
463 | + * @throws SodiumException |
|
464 | + * @throws TypeError |
|
465 | + * @psalm-suppress MixedAssignment |
|
466 | + */ |
|
467 | + public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
468 | + { |
|
469 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
470 | + return $this->mulInt64Fast($int); |
|
471 | + } |
|
472 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
473 | + if (!$size) { |
|
474 | + $size = 63; |
|
475 | + } |
|
476 | + list($a, $b) = self::ctSelect($this, $int); |
|
477 | + |
|
478 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
479 | + $return->unsignedInt = $this->unsignedInt; |
|
480 | + |
|
481 | + // Initialize: |
|
482 | + $ret0 = 0; |
|
483 | + $ret1 = 0; |
|
484 | + $ret2 = 0; |
|
485 | + $ret3 = 0; |
|
486 | + $a0 = $a->limbs[0]; |
|
487 | + $a1 = $a->limbs[1]; |
|
488 | + $a2 = $a->limbs[2]; |
|
489 | + $a3 = $a->limbs[3]; |
|
490 | + $b0 = $b->limbs[0]; |
|
491 | + $b1 = $b->limbs[1]; |
|
492 | + $b2 = $b->limbs[2]; |
|
493 | + $b3 = $b->limbs[3]; |
|
494 | + |
|
495 | + /** @var int $size */ |
|
496 | + /** @var int $i */ |
|
497 | + for ($i = (int) $size; $i >= 0; --$i) { |
|
498 | + $mask = -($b3 & 1); |
|
499 | + $x0 = $a0 & $mask; |
|
500 | + $x1 = $a1 & $mask; |
|
501 | + $x2 = $a2 & $mask; |
|
502 | + $x3 = $a3 & $mask; |
|
503 | + |
|
504 | + $ret3 += $x3; |
|
505 | + $c = $ret3 >> 16; |
|
506 | + |
|
507 | + $ret2 += $x2 + $c; |
|
508 | + $c = $ret2 >> 16; |
|
509 | + |
|
510 | + $ret1 += $x1 + $c; |
|
511 | + $c = $ret1 >> 16; |
|
512 | + |
|
513 | + $ret0 += $x0 + $c; |
|
514 | + |
|
515 | + $ret0 &= 0xffff; |
|
516 | + $ret1 &= 0xffff; |
|
517 | + $ret2 &= 0xffff; |
|
518 | + $ret3 &= 0xffff; |
|
519 | + |
|
520 | + $a3 = $a3 << 1; |
|
521 | + $x3 = $a3 >> 16; |
|
522 | + $a2 = ($a2 << 1) | $x3; |
|
523 | + $x2 = $a2 >> 16; |
|
524 | + $a1 = ($a1 << 1) | $x2; |
|
525 | + $x1 = $a1 >> 16; |
|
526 | + $a0 = ($a0 << 1) | $x1; |
|
527 | + $a0 &= 0xffff; |
|
528 | + $a1 &= 0xffff; |
|
529 | + $a2 &= 0xffff; |
|
530 | + $a3 &= 0xffff; |
|
531 | + |
|
532 | + $x0 = ($b0 & 1) << 16; |
|
533 | + $x1 = ($b1 & 1) << 16; |
|
534 | + $x2 = ($b2 & 1) << 16; |
|
535 | + |
|
536 | + $b0 = ($b0 >> 1); |
|
537 | + $b1 = (($b1 | $x0) >> 1); |
|
538 | + $b2 = (($b2 | $x1) >> 1); |
|
539 | + $b3 = (($b3 | $x2) >> 1); |
|
540 | + |
|
541 | + $b0 &= 0xffff; |
|
542 | + $b1 &= 0xffff; |
|
543 | + $b2 &= 0xffff; |
|
544 | + $b3 &= 0xffff; |
|
545 | + |
|
546 | + } |
|
547 | + $return->limbs[0] = $ret0; |
|
548 | + $return->limbs[1] = $ret1; |
|
549 | + $return->limbs[2] = $ret2; |
|
550 | + $return->limbs[3] = $ret3; |
|
551 | + |
|
552 | + return $return; |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * OR this 64-bit integer with another. |
|
557 | + * |
|
558 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
559 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
560 | + */ |
|
561 | + public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
562 | + { |
|
563 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
564 | + $return->unsignedInt = $this->unsignedInt; |
|
565 | + $return->limbs = array( |
|
566 | + (int) ($this->limbs[0] | $b->limbs[0]), |
|
567 | + (int) ($this->limbs[1] | $b->limbs[1]), |
|
568 | + (int) ($this->limbs[2] | $b->limbs[2]), |
|
569 | + (int) ($this->limbs[3] | $b->limbs[3]) |
|
570 | + ); |
|
571 | + return $return; |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * @param int $c |
|
576 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
577 | + * @throws SodiumException |
|
578 | + * @throws TypeError |
|
579 | + * @psalm-suppress MixedArrayAccess |
|
580 | + */ |
|
581 | + public function rotateLeft($c = 0) |
|
582 | + { |
|
583 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
584 | + /** @var int $c */ |
|
585 | + $c = (int) $c; |
|
586 | + |
|
587 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
588 | + $return->unsignedInt = $this->unsignedInt; |
|
589 | + $c &= 63; |
|
590 | + if ($c === 0) { |
|
591 | + // NOP, but we want a copy. |
|
592 | + $return->limbs = $this->limbs; |
|
593 | + } else { |
|
594 | + /** @var array<int, int> $limbs */ |
|
595 | + $limbs =& $return->limbs; |
|
596 | + |
|
597 | + /** @var array<int, int> $myLimbs */ |
|
598 | + $myLimbs =& $this->limbs; |
|
599 | + |
|
600 | + /** @var int $idx_shift */ |
|
601 | + $idx_shift = ($c >> 4) & 3; |
|
602 | + /** @var int $sub_shift */ |
|
603 | + $sub_shift = $c & 15; |
|
604 | + |
|
605 | + for ($i = 3; $i >= 0; --$i) { |
|
606 | + /** @var int $j */ |
|
607 | + $j = ($i + $idx_shift) & 3; |
|
608 | + /** @var int $k */ |
|
609 | + $k = ($i + $idx_shift + 1) & 3; |
|
610 | + $limbs[$i] = (int) ( |
|
611 | + ( |
|
612 | + ((int) ($myLimbs[$j]) << $sub_shift) |
|
613 | + | |
|
614 | + ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
615 | + ) & 0xffff |
|
616 | + ); |
|
617 | + } |
|
618 | + } |
|
619 | + return $return; |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Rotate to the right |
|
624 | + * |
|
625 | + * @param int $c |
|
626 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
627 | + * @throws SodiumException |
|
628 | + * @throws TypeError |
|
629 | + * @psalm-suppress MixedArrayAccess |
|
630 | + */ |
|
631 | + public function rotateRight($c = 0) |
|
632 | + { |
|
633 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
634 | + /** @var int $c */ |
|
635 | + $c = (int) $c; |
|
636 | + |
|
637 | + /** @var ParagonIE_Sodium_Core32_Int64 $return */ |
|
638 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
639 | + $return->unsignedInt = $this->unsignedInt; |
|
640 | + $c &= 63; |
|
641 | + /** @var int $c */ |
|
642 | + if ($c === 0) { |
|
643 | + // NOP, but we want a copy. |
|
644 | + $return->limbs = $this->limbs; |
|
645 | + } else { |
|
646 | + /** @var array<int, int> $limbs */ |
|
647 | + $limbs =& $return->limbs; |
|
648 | + |
|
649 | + /** @var array<int, int> $myLimbs */ |
|
650 | + $myLimbs =& $this->limbs; |
|
651 | + |
|
652 | + /** @var int $idx_shift */ |
|
653 | + $idx_shift = ($c >> 4) & 3; |
|
654 | + /** @var int $sub_shift */ |
|
655 | + $sub_shift = $c & 15; |
|
656 | + |
|
657 | + for ($i = 3; $i >= 0; --$i) { |
|
658 | + /** @var int $j */ |
|
659 | + $j = ($i - $idx_shift) & 3; |
|
660 | + /** @var int $k */ |
|
661 | + $k = ($i - $idx_shift - 1) & 3; |
|
662 | + $limbs[$i] = (int) ( |
|
663 | + ( |
|
664 | + ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
665 | + | |
|
666 | + ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
667 | + ) & 0xffff |
|
668 | + ); |
|
669 | + } |
|
670 | + } |
|
671 | + return $return; |
|
672 | + } |
|
673 | + /** |
|
674 | + * @param int $c |
|
675 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
676 | + * @throws SodiumException |
|
677 | + * @throws TypeError |
|
678 | + */ |
|
679 | + public function shiftLeft($c = 0) |
|
680 | + { |
|
681 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
682 | + /** @var int $c */ |
|
683 | + $c = (int) $c; |
|
684 | + |
|
685 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
686 | + $return->unsignedInt = $this->unsignedInt; |
|
687 | + $c &= 63; |
|
688 | + |
|
689 | + if ($c >= 16) { |
|
690 | + if ($c >= 48) { |
|
691 | + $return->limbs = array( |
|
692 | + $this->limbs[3], 0, 0, 0 |
|
693 | + ); |
|
694 | + } elseif ($c >= 32) { |
|
695 | + $return->limbs = array( |
|
696 | + $this->limbs[2], $this->limbs[3], 0, 0 |
|
697 | + ); |
|
698 | + } else { |
|
699 | + $return->limbs = array( |
|
700 | + $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 |
|
701 | + ); |
|
702 | + } |
|
703 | + return $return->shiftLeft($c & 15); |
|
704 | + } |
|
705 | + if ($c === 0) { |
|
706 | + $return->limbs = $this->limbs; |
|
707 | + } elseif ($c < 0) { |
|
708 | + /** @var int $c */ |
|
709 | + return $this->shiftRight(-$c); |
|
710 | + } else { |
|
711 | + if (!is_int($c)) { |
|
712 | + throw new TypeError(); |
|
713 | + } |
|
714 | + /** @var int $carry */ |
|
715 | + $carry = 0; |
|
716 | + for ($i = 3; $i >= 0; --$i) { |
|
717 | + /** @var int $tmp */ |
|
718 | + $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); |
|
719 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
720 | + /** @var int $carry */ |
|
721 | + $carry = $tmp >> 16; |
|
722 | + } |
|
723 | + } |
|
724 | + return $return; |
|
725 | + } |
|
726 | + |
|
727 | + /** |
|
728 | + * @param int $c |
|
729 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
730 | + * @throws SodiumException |
|
731 | + * @throws TypeError |
|
732 | + */ |
|
733 | + public function shiftRight($c = 0) |
|
734 | + { |
|
735 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
736 | + $c = (int) $c; |
|
737 | + /** @var int $c */ |
|
738 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
739 | + $return->unsignedInt = $this->unsignedInt; |
|
740 | + $c &= 63; |
|
741 | + |
|
742 | + $negative = -(($this->limbs[0] >> 15) & 1); |
|
743 | + if ($c >= 16) { |
|
744 | + if ($c >= 48) { |
|
745 | + $return->limbs = array( |
|
746 | + (int) ($negative & 0xffff), |
|
747 | + (int) ($negative & 0xffff), |
|
748 | + (int) ($negative & 0xffff), |
|
749 | + (int) $this->limbs[0] |
|
750 | + ); |
|
751 | + } elseif ($c >= 32) { |
|
752 | + $return->limbs = array( |
|
753 | + (int) ($negative & 0xffff), |
|
754 | + (int) ($negative & 0xffff), |
|
755 | + (int) $this->limbs[0], |
|
756 | + (int) $this->limbs[1] |
|
757 | + ); |
|
758 | + } else { |
|
759 | + $return->limbs = array( |
|
760 | + (int) ($negative & 0xffff), |
|
761 | + (int) $this->limbs[0], |
|
762 | + (int) $this->limbs[1], |
|
763 | + (int) $this->limbs[2] |
|
764 | + ); |
|
765 | + } |
|
766 | + return $return->shiftRight($c & 15); |
|
767 | + } |
|
768 | + |
|
769 | + if ($c === 0) { |
|
770 | + $return->limbs = $this->limbs; |
|
771 | + } elseif ($c < 0) { |
|
772 | + return $this->shiftLeft(-$c); |
|
773 | + } else { |
|
774 | + if (!is_int($c)) { |
|
775 | + throw new TypeError(); |
|
776 | + } |
|
777 | + /** @var int $carryRight */ |
|
778 | + $carryRight = ($negative & 0xffff); |
|
779 | + $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); |
|
780 | + for ($i = 0; $i < 4; ++$i) { |
|
781 | + $return->limbs[$i] = (int) ( |
|
782 | + (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff |
|
783 | + ); |
|
784 | + $carryRight = (int) ($this->limbs[$i] & $mask); |
|
785 | + } |
|
786 | + } |
|
787 | + return $return; |
|
788 | + } |
|
789 | + |
|
790 | + |
|
791 | + /** |
|
792 | + * Subtract a normal integer from an int64 object. |
|
793 | + * |
|
794 | + * @param int $int |
|
795 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
796 | + * @throws SodiumException |
|
797 | + * @throws TypeError |
|
798 | + */ |
|
799 | + public function subInt($int) |
|
800 | + { |
|
801 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
802 | + $int = (int) $int; |
|
803 | + |
|
804 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
805 | + $return->unsignedInt = $this->unsignedInt; |
|
806 | + |
|
807 | + /** @var int $carry */ |
|
808 | + $carry = 0; |
|
809 | + for ($i = 3; $i >= 0; --$i) { |
|
810 | + /** @var int $tmp */ |
|
811 | + $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; |
|
812 | + /** @var int $carry */ |
|
813 | + $carry = $tmp >> 16; |
|
814 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
815 | + } |
|
816 | + return $return; |
|
817 | + } |
|
818 | + |
|
819 | + /** |
|
820 | + * The difference between two Int64 objects. |
|
821 | + * |
|
822 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
823 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
824 | + */ |
|
825 | + public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
826 | + { |
|
827 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
828 | + $return->unsignedInt = $this->unsignedInt; |
|
829 | + /** @var int $carry */ |
|
830 | + $carry = 0; |
|
831 | + for ($i = 3; $i >= 0; --$i) { |
|
832 | + /** @var int $tmp */ |
|
833 | + $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; |
|
834 | + /** @var int $carry */ |
|
835 | + $carry = ($tmp >> 16); |
|
836 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
837 | + } |
|
838 | + return $return; |
|
839 | + } |
|
840 | + |
|
841 | + /** |
|
842 | + * XOR this 64-bit integer with another. |
|
843 | + * |
|
844 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
845 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
846 | + */ |
|
847 | + public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
848 | + { |
|
849 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
850 | + $return->unsignedInt = $this->unsignedInt; |
|
851 | + $return->limbs = array( |
|
852 | + (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
853 | + (int) ($this->limbs[1] ^ $b->limbs[1]), |
|
854 | + (int) ($this->limbs[2] ^ $b->limbs[2]), |
|
855 | + (int) ($this->limbs[3] ^ $b->limbs[3]) |
|
856 | + ); |
|
857 | + return $return; |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * @param int $low |
|
862 | + * @param int $high |
|
863 | + * @return self |
|
864 | + * @throws SodiumException |
|
865 | + * @throws TypeError |
|
866 | + */ |
|
867 | + public static function fromInts($low, $high) |
|
868 | + { |
|
869 | + ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
870 | + ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
|
871 | + |
|
872 | + $high = (int) $high; |
|
873 | + $low = (int) $low; |
|
874 | + return new ParagonIE_Sodium_Core32_Int64( |
|
875 | + array( |
|
876 | + (int) (($high >> 16) & 0xffff), |
|
877 | + (int) ($high & 0xffff), |
|
878 | + (int) (($low >> 16) & 0xffff), |
|
879 | + (int) ($low & 0xffff) |
|
880 | + ) |
|
881 | + ); |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * @param int $low |
|
886 | + * @return self |
|
887 | + * @throws SodiumException |
|
888 | + * @throws TypeError |
|
889 | + */ |
|
890 | + public static function fromInt($low) |
|
891 | + { |
|
892 | + ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
893 | + $low = (int) $low; |
|
894 | + |
|
895 | + return new ParagonIE_Sodium_Core32_Int64( |
|
896 | + array( |
|
897 | + 0, |
|
898 | + 0, |
|
899 | + (int) (($low >> 16) & 0xffff), |
|
900 | + (int) ($low & 0xffff) |
|
901 | + ) |
|
902 | + ); |
|
903 | + } |
|
904 | + |
|
905 | + /** |
|
906 | + * @return int |
|
907 | + */ |
|
908 | + public function toInt() |
|
909 | + { |
|
910 | + return (int) ( |
|
911 | + (($this->limbs[2] & 0xffff) << 16) |
|
912 | + | |
|
913 | + ($this->limbs[3] & 0xffff) |
|
914 | + ); |
|
915 | + } |
|
916 | + |
|
917 | + /** |
|
918 | + * @param string $string |
|
919 | + * @return self |
|
920 | + * @throws SodiumException |
|
921 | + * @throws TypeError |
|
922 | + */ |
|
923 | + public static function fromString($string) |
|
924 | + { |
|
925 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
926 | + $string = (string) $string; |
|
927 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
928 | + throw new RangeException( |
|
929 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
930 | + ); |
|
931 | + } |
|
932 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
933 | + |
|
934 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
935 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
936 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
937 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
938 | + $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); |
|
939 | + $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); |
|
940 | + $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); |
|
941 | + $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); |
|
942 | + return $return; |
|
943 | + } |
|
944 | + |
|
945 | + /** |
|
946 | + * @param string $string |
|
947 | + * @return self |
|
948 | + * @throws SodiumException |
|
949 | + * @throws TypeError |
|
950 | + */ |
|
951 | + public static function fromReverseString($string) |
|
952 | + { |
|
953 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
954 | + $string = (string) $string; |
|
955 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
956 | + throw new RangeException( |
|
957 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
958 | + ); |
|
959 | + } |
|
960 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
961 | + |
|
962 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); |
|
963 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); |
|
964 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); |
|
965 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); |
|
966 | + $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
967 | + $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
968 | + $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
969 | + $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
970 | + return $return; |
|
971 | + } |
|
972 | + |
|
973 | + /** |
|
974 | + * @return array<int, int> |
|
975 | + */ |
|
976 | + public function toArray() |
|
977 | + { |
|
978 | + return array( |
|
979 | + (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
|
980 | + (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
|
981 | + ); |
|
982 | + } |
|
983 | + |
|
984 | + /** |
|
985 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
986 | + */ |
|
987 | + public function toInt32() |
|
988 | + { |
|
989 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
990 | + $return->limbs[0] = (int) ($this->limbs[2]); |
|
991 | + $return->limbs[1] = (int) ($this->limbs[3]); |
|
992 | + $return->unsignedInt = $this->unsignedInt; |
|
993 | + $return->overflow = (int) (ParagonIE_Sodium_Core32_Util::abs($this->limbs[1], 16) & 0xffff); |
|
994 | + return $return; |
|
995 | + } |
|
996 | + |
|
997 | + /** |
|
998 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
999 | + */ |
|
1000 | + public function toInt64() |
|
1001 | + { |
|
1002 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
1003 | + $return->limbs[0] = (int) ($this->limbs[0]); |
|
1004 | + $return->limbs[1] = (int) ($this->limbs[1]); |
|
1005 | + $return->limbs[2] = (int) ($this->limbs[2]); |
|
1006 | + $return->limbs[3] = (int) ($this->limbs[3]); |
|
1007 | + $return->unsignedInt = $this->unsignedInt; |
|
1008 | + $return->overflow = ParagonIE_Sodium_Core32_Util::abs($this->overflow); |
|
1009 | + return $return; |
|
1010 | + } |
|
1011 | + |
|
1012 | + /** |
|
1013 | + * @param bool $bool |
|
1014 | + * @return self |
|
1015 | + */ |
|
1016 | + public function setUnsignedInt($bool = false) |
|
1017 | + { |
|
1018 | + $this->unsignedInt = !empty($bool); |
|
1019 | + return $this; |
|
1020 | + } |
|
1021 | + |
|
1022 | + /** |
|
1023 | + * @return string |
|
1024 | + * @throws TypeError |
|
1025 | + */ |
|
1026 | + public function toString() |
|
1027 | + { |
|
1028 | + return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
1029 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
1030 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
1031 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
1032 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
1033 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
1034 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
1035 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); |
|
1036 | + } |
|
1037 | + |
|
1038 | + /** |
|
1039 | + * @return string |
|
1040 | + * @throws TypeError |
|
1041 | + */ |
|
1042 | + public function toReverseString() |
|
1043 | + { |
|
1044 | + return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
|
1045 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
1046 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
1047 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
1048 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
1049 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
1050 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
1051 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
1052 | + } |
|
1053 | + |
|
1054 | + /** |
|
1055 | + * @return string |
|
1056 | + */ |
|
1057 | + public function __toString() |
|
1058 | + { |
|
1059 | + try { |
|
1060 | + return $this->toString(); |
|
1061 | + } catch (TypeError $ex) { |
|
1062 | + // PHP engine can't handle exceptions from __toString() |
|
1063 | + return ''; |
|
1064 | + } |
|
1065 | + } |
|
1066 | 1066 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Salsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,298 +9,298 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - const ROUNDS = 20; |
|
12 | + const ROUNDS = 20; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Calculate an salsa20 hash of a single block |
|
16 | - * |
|
17 | - * @internal You should not use this directly from another application |
|
18 | - * |
|
19 | - * @param string $in |
|
20 | - * @param string $k |
|
21 | - * @param string|null $c |
|
22 | - * @return string |
|
23 | - * @throws SodiumException |
|
24 | - * @throws TypeError |
|
25 | - */ |
|
26 | - public static function core_salsa20($in, $k, $c = null) |
|
27 | - { |
|
28 | - /** |
|
29 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
30 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
31 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
32 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
33 | - * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
34 | - * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
35 | - * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
36 | - * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
37 | - * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
38 | - * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
39 | - * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
40 | - * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
41 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
42 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
43 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
44 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
45 | - * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
46 | - * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
47 | - * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
48 | - * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
49 | - * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
50 | - * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
51 | - * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
52 | - * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
53 | - * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
54 | - * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
55 | - * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
56 | - * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
57 | - * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
58 | - * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
59 | - * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
60 | - * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
61 | - */ |
|
62 | - if (self::strlen($k) < 32) { |
|
63 | - throw new RangeException('Key must be 32 bytes long'); |
|
64 | - } |
|
65 | - if ($c === null) { |
|
66 | - $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
67 | - $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
68 | - $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
69 | - $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
70 | - } else { |
|
71 | - $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
72 | - $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
73 | - $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
74 | - $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
75 | - } |
|
76 | - $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
77 | - $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
78 | - $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
79 | - $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
80 | - $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
81 | - $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
82 | - $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
83 | - $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
84 | - $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
85 | - $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
86 | - $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
87 | - $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
14 | + /** |
|
15 | + * Calculate an salsa20 hash of a single block |
|
16 | + * |
|
17 | + * @internal You should not use this directly from another application |
|
18 | + * |
|
19 | + * @param string $in |
|
20 | + * @param string $k |
|
21 | + * @param string|null $c |
|
22 | + * @return string |
|
23 | + * @throws SodiumException |
|
24 | + * @throws TypeError |
|
25 | + */ |
|
26 | + public static function core_salsa20($in, $k, $c = null) |
|
27 | + { |
|
28 | + /** |
|
29 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
30 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
31 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
32 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
33 | + * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
34 | + * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
35 | + * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
36 | + * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
37 | + * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
38 | + * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
39 | + * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
40 | + * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
41 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
42 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
43 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
44 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
45 | + * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
46 | + * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
47 | + * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
48 | + * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
49 | + * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
50 | + * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
51 | + * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
52 | + * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
53 | + * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
54 | + * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
55 | + * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
56 | + * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
57 | + * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
58 | + * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
59 | + * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
60 | + * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
61 | + */ |
|
62 | + if (self::strlen($k) < 32) { |
|
63 | + throw new RangeException('Key must be 32 bytes long'); |
|
64 | + } |
|
65 | + if ($c === null) { |
|
66 | + $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
67 | + $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
68 | + $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
69 | + $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
70 | + } else { |
|
71 | + $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
72 | + $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
73 | + $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
74 | + $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
75 | + } |
|
76 | + $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
77 | + $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
78 | + $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
79 | + $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
80 | + $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
81 | + $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
82 | + $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
83 | + $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
84 | + $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
85 | + $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
86 | + $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
87 | + $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
88 | 88 | |
89 | - $j0 = clone $x0; |
|
90 | - $j1 = clone $x1; |
|
91 | - $j2 = clone $x2; |
|
92 | - $j3 = clone $x3; |
|
93 | - $j4 = clone $x4; |
|
94 | - $j5 = clone $x5; |
|
95 | - $j6 = clone $x6; |
|
96 | - $j7 = clone $x7; |
|
97 | - $j8 = clone $x8; |
|
98 | - $j9 = clone $x9; |
|
99 | - $j10 = clone $x10; |
|
100 | - $j11 = clone $x11; |
|
101 | - $j12 = clone $x12; |
|
102 | - $j13 = clone $x13; |
|
103 | - $j14 = clone $x14; |
|
104 | - $j15 = clone $x15; |
|
89 | + $j0 = clone $x0; |
|
90 | + $j1 = clone $x1; |
|
91 | + $j2 = clone $x2; |
|
92 | + $j3 = clone $x3; |
|
93 | + $j4 = clone $x4; |
|
94 | + $j5 = clone $x5; |
|
95 | + $j6 = clone $x6; |
|
96 | + $j7 = clone $x7; |
|
97 | + $j8 = clone $x8; |
|
98 | + $j9 = clone $x9; |
|
99 | + $j10 = clone $x10; |
|
100 | + $j11 = clone $x11; |
|
101 | + $j12 = clone $x12; |
|
102 | + $j13 = clone $x13; |
|
103 | + $j14 = clone $x14; |
|
104 | + $j15 = clone $x15; |
|
105 | 105 | |
106 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
107 | - $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
108 | - $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
109 | - $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
110 | - $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
106 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
107 | + $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
108 | + $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
109 | + $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
110 | + $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
111 | 111 | |
112 | - $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
113 | - $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
114 | - $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
115 | - $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
112 | + $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
113 | + $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
114 | + $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
115 | + $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
116 | 116 | |
117 | - $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
118 | - $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
119 | - $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
120 | - $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
117 | + $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
118 | + $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
119 | + $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
120 | + $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
121 | 121 | |
122 | - $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
123 | - $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
124 | - $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
125 | - $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
122 | + $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
123 | + $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
124 | + $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
125 | + $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
126 | 126 | |
127 | - $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
128 | - $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
129 | - $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
130 | - $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
127 | + $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
128 | + $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
129 | + $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
130 | + $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
131 | 131 | |
132 | - $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
133 | - $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
134 | - $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
135 | - $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
132 | + $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
133 | + $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
134 | + $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
135 | + $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
136 | 136 | |
137 | - $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
138 | - $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
139 | - $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
140 | - $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
137 | + $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
138 | + $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
139 | + $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
140 | + $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
141 | 141 | |
142 | - $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
143 | - $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
144 | - $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
145 | - $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
146 | - } |
|
142 | + $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
143 | + $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
144 | + $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
145 | + $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
146 | + } |
|
147 | 147 | |
148 | - $x0 = $x0->addInt32($j0); |
|
149 | - $x1 = $x1->addInt32($j1); |
|
150 | - $x2 = $x2->addInt32($j2); |
|
151 | - $x3 = $x3->addInt32($j3); |
|
152 | - $x4 = $x4->addInt32($j4); |
|
153 | - $x5 = $x5->addInt32($j5); |
|
154 | - $x6 = $x6->addInt32($j6); |
|
155 | - $x7 = $x7->addInt32($j7); |
|
156 | - $x8 = $x8->addInt32($j8); |
|
157 | - $x9 = $x9->addInt32($j9); |
|
158 | - $x10 = $x10->addInt32($j10); |
|
159 | - $x11 = $x11->addInt32($j11); |
|
160 | - $x12 = $x12->addInt32($j12); |
|
161 | - $x13 = $x13->addInt32($j13); |
|
162 | - $x14 = $x14->addInt32($j14); |
|
163 | - $x15 = $x15->addInt32($j15); |
|
148 | + $x0 = $x0->addInt32($j0); |
|
149 | + $x1 = $x1->addInt32($j1); |
|
150 | + $x2 = $x2->addInt32($j2); |
|
151 | + $x3 = $x3->addInt32($j3); |
|
152 | + $x4 = $x4->addInt32($j4); |
|
153 | + $x5 = $x5->addInt32($j5); |
|
154 | + $x6 = $x6->addInt32($j6); |
|
155 | + $x7 = $x7->addInt32($j7); |
|
156 | + $x8 = $x8->addInt32($j8); |
|
157 | + $x9 = $x9->addInt32($j9); |
|
158 | + $x10 = $x10->addInt32($j10); |
|
159 | + $x11 = $x11->addInt32($j11); |
|
160 | + $x12 = $x12->addInt32($j12); |
|
161 | + $x13 = $x13->addInt32($j13); |
|
162 | + $x14 = $x14->addInt32($j14); |
|
163 | + $x15 = $x15->addInt32($j15); |
|
164 | 164 | |
165 | - return $x0->toReverseString() . |
|
166 | - $x1->toReverseString() . |
|
167 | - $x2->toReverseString() . |
|
168 | - $x3->toReverseString() . |
|
169 | - $x4->toReverseString() . |
|
170 | - $x5->toReverseString() . |
|
171 | - $x6->toReverseString() . |
|
172 | - $x7->toReverseString() . |
|
173 | - $x8->toReverseString() . |
|
174 | - $x9->toReverseString() . |
|
175 | - $x10->toReverseString() . |
|
176 | - $x11->toReverseString() . |
|
177 | - $x12->toReverseString() . |
|
178 | - $x13->toReverseString() . |
|
179 | - $x14->toReverseString() . |
|
180 | - $x15->toReverseString(); |
|
181 | - } |
|
165 | + return $x0->toReverseString() . |
|
166 | + $x1->toReverseString() . |
|
167 | + $x2->toReverseString() . |
|
168 | + $x3->toReverseString() . |
|
169 | + $x4->toReverseString() . |
|
170 | + $x5->toReverseString() . |
|
171 | + $x6->toReverseString() . |
|
172 | + $x7->toReverseString() . |
|
173 | + $x8->toReverseString() . |
|
174 | + $x9->toReverseString() . |
|
175 | + $x10->toReverseString() . |
|
176 | + $x11->toReverseString() . |
|
177 | + $x12->toReverseString() . |
|
178 | + $x13->toReverseString() . |
|
179 | + $x14->toReverseString() . |
|
180 | + $x15->toReverseString(); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @internal You should not use this directly from another application |
|
185 | - * |
|
186 | - * @param int $len |
|
187 | - * @param string $nonce |
|
188 | - * @param string $key |
|
189 | - * @return string |
|
190 | - * @throws SodiumException |
|
191 | - * @throws TypeError |
|
192 | - */ |
|
193 | - public static function salsa20($len, $nonce, $key) |
|
194 | - { |
|
195 | - if (self::strlen($key) !== 32) { |
|
196 | - throw new RangeException('Key must be 32 bytes long'); |
|
197 | - } |
|
198 | - $kcopy = '' . $key; |
|
199 | - $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
200 | - $c = ''; |
|
201 | - while ($len >= 64) { |
|
202 | - $c .= self::core_salsa20($in, $kcopy, null); |
|
203 | - $u = 1; |
|
204 | - // Internal counter. |
|
205 | - for ($i = 8; $i < 16; ++$i) { |
|
206 | - $u += self::chrToInt($in[$i]); |
|
207 | - $in[$i] = self::intToChr($u & 0xff); |
|
208 | - $u >>= 8; |
|
209 | - } |
|
210 | - $len -= 64; |
|
211 | - } |
|
212 | - if ($len > 0) { |
|
213 | - $c .= self::substr( |
|
214 | - self::core_salsa20($in, $kcopy, null), |
|
215 | - 0, |
|
216 | - $len |
|
217 | - ); |
|
218 | - } |
|
219 | - try { |
|
220 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
221 | - } catch (SodiumException $ex) { |
|
222 | - $kcopy = null; |
|
223 | - } |
|
224 | - return $c; |
|
225 | - } |
|
183 | + /** |
|
184 | + * @internal You should not use this directly from another application |
|
185 | + * |
|
186 | + * @param int $len |
|
187 | + * @param string $nonce |
|
188 | + * @param string $key |
|
189 | + * @return string |
|
190 | + * @throws SodiumException |
|
191 | + * @throws TypeError |
|
192 | + */ |
|
193 | + public static function salsa20($len, $nonce, $key) |
|
194 | + { |
|
195 | + if (self::strlen($key) !== 32) { |
|
196 | + throw new RangeException('Key must be 32 bytes long'); |
|
197 | + } |
|
198 | + $kcopy = '' . $key; |
|
199 | + $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8); |
|
200 | + $c = ''; |
|
201 | + while ($len >= 64) { |
|
202 | + $c .= self::core_salsa20($in, $kcopy, null); |
|
203 | + $u = 1; |
|
204 | + // Internal counter. |
|
205 | + for ($i = 8; $i < 16; ++$i) { |
|
206 | + $u += self::chrToInt($in[$i]); |
|
207 | + $in[$i] = self::intToChr($u & 0xff); |
|
208 | + $u >>= 8; |
|
209 | + } |
|
210 | + $len -= 64; |
|
211 | + } |
|
212 | + if ($len > 0) { |
|
213 | + $c .= self::substr( |
|
214 | + self::core_salsa20($in, $kcopy, null), |
|
215 | + 0, |
|
216 | + $len |
|
217 | + ); |
|
218 | + } |
|
219 | + try { |
|
220 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
221 | + } catch (SodiumException $ex) { |
|
222 | + $kcopy = null; |
|
223 | + } |
|
224 | + return $c; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * @internal You should not use this directly from another application |
|
229 | - * |
|
230 | - * @param string $m |
|
231 | - * @param string $n |
|
232 | - * @param int $ic |
|
233 | - * @param string $k |
|
234 | - * @return string |
|
235 | - * @throws SodiumException |
|
236 | - * @throws TypeError |
|
237 | - */ |
|
238 | - public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
239 | - { |
|
240 | - $mlen = self::strlen($m); |
|
241 | - if ($mlen < 1) { |
|
242 | - return ''; |
|
243 | - } |
|
244 | - $kcopy = self::substr($k, 0, 32); |
|
245 | - $in = self::substr($n, 0, 8); |
|
246 | - // Initialize the counter |
|
247 | - $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); |
|
227 | + /** |
|
228 | + * @internal You should not use this directly from another application |
|
229 | + * |
|
230 | + * @param string $m |
|
231 | + * @param string $n |
|
232 | + * @param int $ic |
|
233 | + * @param string $k |
|
234 | + * @return string |
|
235 | + * @throws SodiumException |
|
236 | + * @throws TypeError |
|
237 | + */ |
|
238 | + public static function salsa20_xor_ic($m, $n, $ic, $k) |
|
239 | + { |
|
240 | + $mlen = self::strlen($m); |
|
241 | + if ($mlen < 1) { |
|
242 | + return ''; |
|
243 | + } |
|
244 | + $kcopy = self::substr($k, 0, 32); |
|
245 | + $in = self::substr($n, 0, 8); |
|
246 | + // Initialize the counter |
|
247 | + $in .= ParagonIE_Sodium_Core32_Util::store64_le($ic); |
|
248 | 248 | |
249 | - $c = ''; |
|
250 | - while ($mlen >= 64) { |
|
251 | - $block = self::core_salsa20($in, $kcopy, null); |
|
252 | - $c .= self::xorStrings( |
|
253 | - self::substr($m, 0, 64), |
|
254 | - self::substr($block, 0, 64) |
|
255 | - ); |
|
256 | - $u = 1; |
|
257 | - for ($i = 8; $i < 16; ++$i) { |
|
258 | - $u += self::chrToInt($in[$i]); |
|
259 | - $in[$i] = self::intToChr($u & 0xff); |
|
260 | - $u >>= 8; |
|
261 | - } |
|
249 | + $c = ''; |
|
250 | + while ($mlen >= 64) { |
|
251 | + $block = self::core_salsa20($in, $kcopy, null); |
|
252 | + $c .= self::xorStrings( |
|
253 | + self::substr($m, 0, 64), |
|
254 | + self::substr($block, 0, 64) |
|
255 | + ); |
|
256 | + $u = 1; |
|
257 | + for ($i = 8; $i < 16; ++$i) { |
|
258 | + $u += self::chrToInt($in[$i]); |
|
259 | + $in[$i] = self::intToChr($u & 0xff); |
|
260 | + $u >>= 8; |
|
261 | + } |
|
262 | 262 | |
263 | - $mlen -= 64; |
|
264 | - $m = self::substr($m, 64); |
|
265 | - } |
|
263 | + $mlen -= 64; |
|
264 | + $m = self::substr($m, 64); |
|
265 | + } |
|
266 | 266 | |
267 | - if ($mlen) { |
|
268 | - $block = self::core_salsa20($in, $kcopy, null); |
|
269 | - $c .= self::xorStrings( |
|
270 | - self::substr($m, 0, $mlen), |
|
271 | - self::substr($block, 0, $mlen) |
|
272 | - ); |
|
273 | - } |
|
274 | - try { |
|
275 | - ParagonIE_Sodium_Compat::memzero($block); |
|
276 | - ParagonIE_Sodium_Compat::memzero($kcopy); |
|
277 | - } catch (SodiumException $ex) { |
|
278 | - $block = null; |
|
279 | - $kcopy = null; |
|
280 | - } |
|
267 | + if ($mlen) { |
|
268 | + $block = self::core_salsa20($in, $kcopy, null); |
|
269 | + $c .= self::xorStrings( |
|
270 | + self::substr($m, 0, $mlen), |
|
271 | + self::substr($block, 0, $mlen) |
|
272 | + ); |
|
273 | + } |
|
274 | + try { |
|
275 | + ParagonIE_Sodium_Compat::memzero($block); |
|
276 | + ParagonIE_Sodium_Compat::memzero($kcopy); |
|
277 | + } catch (SodiumException $ex) { |
|
278 | + $block = null; |
|
279 | + $kcopy = null; |
|
280 | + } |
|
281 | 281 | |
282 | - return $c; |
|
283 | - } |
|
282 | + return $c; |
|
283 | + } |
|
284 | 284 | |
285 | - /** |
|
286 | - * @internal You should not use this directly from another application |
|
287 | - * |
|
288 | - * @param string $message |
|
289 | - * @param string $nonce |
|
290 | - * @param string $key |
|
291 | - * @return string |
|
292 | - * @throws SodiumException |
|
293 | - * @throws TypeError |
|
294 | - */ |
|
295 | - public static function salsa20_xor($message, $nonce, $key) |
|
296 | - { |
|
297 | - return self::xorStrings( |
|
298 | - $message, |
|
299 | - self::salsa20( |
|
300 | - self::strlen($message), |
|
301 | - $nonce, |
|
302 | - $key |
|
303 | - ) |
|
304 | - ); |
|
305 | - } |
|
285 | + /** |
|
286 | + * @internal You should not use this directly from another application |
|
287 | + * |
|
288 | + * @param string $message |
|
289 | + * @param string $nonce |
|
290 | + * @param string $key |
|
291 | + * @return string |
|
292 | + * @throws SodiumException |
|
293 | + * @throws TypeError |
|
294 | + */ |
|
295 | + public static function salsa20_xor($message, $nonce, $key) |
|
296 | + { |
|
297 | + return self::xorStrings( |
|
298 | + $message, |
|
299 | + self::salsa20( |
|
300 | + self::strlen($message), |
|
301 | + $nonce, |
|
302 | + $key |
|
303 | + ) |
|
304 | + ); |
|
305 | + } |
|
306 | 306 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Fe', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,179 +11,179 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ParagonIE_Sodium_Core32_Curve25519_Fe implements ArrayAccess |
13 | 13 | { |
14 | - /** |
|
15 | - * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
16 | - */ |
|
17 | - protected $container = array(); |
|
14 | + /** |
|
15 | + * @var array<int, ParagonIE_Sodium_Core32_Int32> |
|
16 | + */ |
|
17 | + protected $container = array(); |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var int |
|
21 | - */ |
|
22 | - protected $size = 10; |
|
19 | + /** |
|
20 | + * @var int |
|
21 | + */ |
|
22 | + protected $size = 10; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @internal You should not use this directly from another application |
|
26 | - * |
|
27 | - * @param array<int, ParagonIE_Sodium_Core32_Int32> $array |
|
28 | - * @param bool $save_indexes |
|
29 | - * @return self |
|
30 | - * @throws SodiumException |
|
31 | - * @throws TypeError |
|
32 | - */ |
|
33 | - public static function fromArray($array, $save_indexes = null) |
|
34 | - { |
|
35 | - $count = count($array); |
|
36 | - if ($save_indexes) { |
|
37 | - $keys = array_keys($array); |
|
38 | - } else { |
|
39 | - $keys = range(0, $count - 1); |
|
40 | - } |
|
41 | - $array = array_values($array); |
|
24 | + /** |
|
25 | + * @internal You should not use this directly from another application |
|
26 | + * |
|
27 | + * @param array<int, ParagonIE_Sodium_Core32_Int32> $array |
|
28 | + * @param bool $save_indexes |
|
29 | + * @return self |
|
30 | + * @throws SodiumException |
|
31 | + * @throws TypeError |
|
32 | + */ |
|
33 | + public static function fromArray($array, $save_indexes = null) |
|
34 | + { |
|
35 | + $count = count($array); |
|
36 | + if ($save_indexes) { |
|
37 | + $keys = array_keys($array); |
|
38 | + } else { |
|
39 | + $keys = range(0, $count - 1); |
|
40 | + } |
|
41 | + $array = array_values($array); |
|
42 | 42 | |
43 | - $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); |
|
44 | - if ($save_indexes) { |
|
45 | - for ($i = 0; $i < $count; ++$i) { |
|
46 | - $array[$i]->overflow = 0; |
|
47 | - $obj->offsetSet($keys[$i], $array[$i]); |
|
48 | - } |
|
49 | - } else { |
|
50 | - for ($i = 0; $i < $count; ++$i) { |
|
51 | - $array[$i]->overflow = 0; |
|
52 | - $obj->offsetSet($i, $array[$i]); |
|
53 | - } |
|
54 | - } |
|
55 | - return $obj; |
|
56 | - } |
|
43 | + $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); |
|
44 | + if ($save_indexes) { |
|
45 | + for ($i = 0; $i < $count; ++$i) { |
|
46 | + $array[$i]->overflow = 0; |
|
47 | + $obj->offsetSet($keys[$i], $array[$i]); |
|
48 | + } |
|
49 | + } else { |
|
50 | + for ($i = 0; $i < $count; ++$i) { |
|
51 | + $array[$i]->overflow = 0; |
|
52 | + $obj->offsetSet($i, $array[$i]); |
|
53 | + } |
|
54 | + } |
|
55 | + return $obj; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @internal You should not use this directly from another application |
|
60 | - * |
|
61 | - * @param array<int, int> $array |
|
62 | - * @param bool $save_indexes |
|
63 | - * @return self |
|
64 | - * @throws SodiumException |
|
65 | - * @throws TypeError |
|
66 | - */ |
|
67 | - public static function fromIntArray($array, $save_indexes = null) |
|
68 | - { |
|
69 | - $count = count($array); |
|
70 | - if ($save_indexes) { |
|
71 | - $keys = array_keys($array); |
|
72 | - } else { |
|
73 | - $keys = range(0, $count - 1); |
|
74 | - } |
|
75 | - $array = array_values($array); |
|
76 | - $set = array(); |
|
77 | - /** @var int $i */ |
|
78 | - /** @var int $v */ |
|
79 | - foreach ($array as $i => $v) { |
|
80 | - $set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v); |
|
81 | - } |
|
58 | + /** |
|
59 | + * @internal You should not use this directly from another application |
|
60 | + * |
|
61 | + * @param array<int, int> $array |
|
62 | + * @param bool $save_indexes |
|
63 | + * @return self |
|
64 | + * @throws SodiumException |
|
65 | + * @throws TypeError |
|
66 | + */ |
|
67 | + public static function fromIntArray($array, $save_indexes = null) |
|
68 | + { |
|
69 | + $count = count($array); |
|
70 | + if ($save_indexes) { |
|
71 | + $keys = array_keys($array); |
|
72 | + } else { |
|
73 | + $keys = range(0, $count - 1); |
|
74 | + } |
|
75 | + $array = array_values($array); |
|
76 | + $set = array(); |
|
77 | + /** @var int $i */ |
|
78 | + /** @var int $v */ |
|
79 | + foreach ($array as $i => $v) { |
|
80 | + $set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v); |
|
81 | + } |
|
82 | 82 | |
83 | - $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); |
|
84 | - if ($save_indexes) { |
|
85 | - for ($i = 0; $i < $count; ++$i) { |
|
86 | - $set[$i]->overflow = 0; |
|
87 | - $obj->offsetSet($keys[$i], $set[$i]); |
|
88 | - } |
|
89 | - } else { |
|
90 | - for ($i = 0; $i < $count; ++$i) { |
|
91 | - $set[$i]->overflow = 0; |
|
92 | - $obj->offsetSet($i, $set[$i]); |
|
93 | - } |
|
94 | - } |
|
95 | - return $obj; |
|
96 | - } |
|
83 | + $obj = new ParagonIE_Sodium_Core32_Curve25519_Fe(); |
|
84 | + if ($save_indexes) { |
|
85 | + for ($i = 0; $i < $count; ++$i) { |
|
86 | + $set[$i]->overflow = 0; |
|
87 | + $obj->offsetSet($keys[$i], $set[$i]); |
|
88 | + } |
|
89 | + } else { |
|
90 | + for ($i = 0; $i < $count; ++$i) { |
|
91 | + $set[$i]->overflow = 0; |
|
92 | + $obj->offsetSet($i, $set[$i]); |
|
93 | + } |
|
94 | + } |
|
95 | + return $obj; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @internal You should not use this directly from another application |
|
100 | - * |
|
101 | - * @param mixed $offset |
|
102 | - * @param mixed $value |
|
103 | - * @return void |
|
104 | - * @throws SodiumException |
|
105 | - * @throws TypeError |
|
106 | - */ |
|
107 | - #[ReturnTypeWillChange] |
|
108 | - public function offsetSet($offset, $value) |
|
109 | - { |
|
110 | - if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) { |
|
111 | - throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32'); |
|
112 | - } |
|
113 | - if (is_null($offset)) { |
|
114 | - $this->container[] = $value; |
|
115 | - } else { |
|
116 | - ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1); |
|
117 | - $this->container[(int) $offset] = $value; |
|
118 | - } |
|
119 | - } |
|
98 | + /** |
|
99 | + * @internal You should not use this directly from another application |
|
100 | + * |
|
101 | + * @param mixed $offset |
|
102 | + * @param mixed $value |
|
103 | + * @return void |
|
104 | + * @throws SodiumException |
|
105 | + * @throws TypeError |
|
106 | + */ |
|
107 | + #[ReturnTypeWillChange] |
|
108 | + public function offsetSet($offset, $value) |
|
109 | + { |
|
110 | + if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) { |
|
111 | + throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32'); |
|
112 | + } |
|
113 | + if (is_null($offset)) { |
|
114 | + $this->container[] = $value; |
|
115 | + } else { |
|
116 | + ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1); |
|
117 | + $this->container[(int) $offset] = $value; |
|
118 | + } |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * @internal You should not use this directly from another application |
|
123 | - * |
|
124 | - * @param mixed $offset |
|
125 | - * @return bool |
|
126 | - * @psalm-suppress MixedArrayOffset |
|
127 | - */ |
|
128 | - #[ReturnTypeWillChange] |
|
129 | - public function offsetExists($offset) |
|
130 | - { |
|
131 | - return isset($this->container[$offset]); |
|
132 | - } |
|
121 | + /** |
|
122 | + * @internal You should not use this directly from another application |
|
123 | + * |
|
124 | + * @param mixed $offset |
|
125 | + * @return bool |
|
126 | + * @psalm-suppress MixedArrayOffset |
|
127 | + */ |
|
128 | + #[ReturnTypeWillChange] |
|
129 | + public function offsetExists($offset) |
|
130 | + { |
|
131 | + return isset($this->container[$offset]); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @internal You should not use this directly from another application |
|
136 | - * |
|
137 | - * @param mixed $offset |
|
138 | - * @return void |
|
139 | - * @psalm-suppress MixedArrayOffset |
|
140 | - */ |
|
141 | - #[ReturnTypeWillChange] |
|
142 | - public function offsetUnset($offset) |
|
143 | - { |
|
144 | - unset($this->container[$offset]); |
|
145 | - } |
|
134 | + /** |
|
135 | + * @internal You should not use this directly from another application |
|
136 | + * |
|
137 | + * @param mixed $offset |
|
138 | + * @return void |
|
139 | + * @psalm-suppress MixedArrayOffset |
|
140 | + */ |
|
141 | + #[ReturnTypeWillChange] |
|
142 | + public function offsetUnset($offset) |
|
143 | + { |
|
144 | + unset($this->container[$offset]); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * @internal You should not use this directly from another application |
|
149 | - * |
|
150 | - * @param mixed $offset |
|
151 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
152 | - * @psalm-suppress MixedArrayOffset |
|
153 | - */ |
|
154 | - #[ReturnTypeWillChange] |
|
155 | - public function offsetGet($offset) |
|
156 | - { |
|
157 | - if (!isset($this->container[$offset])) { |
|
158 | - $this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32(); |
|
159 | - } |
|
160 | - /** @var ParagonIE_Sodium_Core32_Int32 $get */ |
|
161 | - $get = $this->container[$offset]; |
|
162 | - return $get; |
|
163 | - } |
|
147 | + /** |
|
148 | + * @internal You should not use this directly from another application |
|
149 | + * |
|
150 | + * @param mixed $offset |
|
151 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
152 | + * @psalm-suppress MixedArrayOffset |
|
153 | + */ |
|
154 | + #[ReturnTypeWillChange] |
|
155 | + public function offsetGet($offset) |
|
156 | + { |
|
157 | + if (!isset($this->container[$offset])) { |
|
158 | + $this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32(); |
|
159 | + } |
|
160 | + /** @var ParagonIE_Sodium_Core32_Int32 $get */ |
|
161 | + $get = $this->container[$offset]; |
|
162 | + return $get; |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * @internal You should not use this directly from another application |
|
167 | - * |
|
168 | - * @return array |
|
169 | - */ |
|
170 | - public function __debugInfo() |
|
171 | - { |
|
172 | - if (empty($this->container)) { |
|
173 | - return array(); |
|
174 | - } |
|
175 | - $c = array( |
|
176 | - (int) ($this->container[0]->toInt()), |
|
177 | - (int) ($this->container[1]->toInt()), |
|
178 | - (int) ($this->container[2]->toInt()), |
|
179 | - (int) ($this->container[3]->toInt()), |
|
180 | - (int) ($this->container[4]->toInt()), |
|
181 | - (int) ($this->container[5]->toInt()), |
|
182 | - (int) ($this->container[6]->toInt()), |
|
183 | - (int) ($this->container[7]->toInt()), |
|
184 | - (int) ($this->container[8]->toInt()), |
|
185 | - (int) ($this->container[9]->toInt()) |
|
186 | - ); |
|
187 | - return array(implode(', ', $c)); |
|
188 | - } |
|
165 | + /** |
|
166 | + * @internal You should not use this directly from another application |
|
167 | + * |
|
168 | + * @return array |
|
169 | + */ |
|
170 | + public function __debugInfo() |
|
171 | + { |
|
172 | + if (empty($this->container)) { |
|
173 | + return array(); |
|
174 | + } |
|
175 | + $c = array( |
|
176 | + (int) ($this->container[0]->toInt()), |
|
177 | + (int) ($this->container[1]->toInt()), |
|
178 | + (int) ($this->container[2]->toInt()), |
|
179 | + (int) ($this->container[3]->toInt()), |
|
180 | + (int) ($this->container[4]->toInt()), |
|
181 | + (int) ($this->container[5]->toInt()), |
|
182 | + (int) ($this->container[6]->toInt()), |
|
183 | + (int) ($this->container[7]->toInt()), |
|
184 | + (int) ($this->container[8]->toInt()), |
|
185 | + (int) ($this->container[9]->toInt()) |
|
186 | + ); |
|
187 | + return array(implode(', ', $c)); |
|
188 | + } |
|
189 | 189 | } |