@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,337 +9,337 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Alters the objects passed to this method in place. |
|
| 14 | - * |
|
| 15 | - * @internal You should not use this directly from another application |
|
| 16 | - * |
|
| 17 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
| 18 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
| 19 | - * @param int $b |
|
| 20 | - * @return void |
|
| 21 | - * @throws SodiumException |
|
| 22 | - * @throws TypeError |
|
| 23 | - * @psalm-suppress MixedMethodCall |
|
| 24 | - */ |
|
| 25 | - public static function fe_cswap( |
|
| 26 | - ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
| 27 | - ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
| 28 | - $b = 0 |
|
| 29 | - ) { |
|
| 30 | - $f0 = (int) $f[0]->toInt(); |
|
| 31 | - $f1 = (int) $f[1]->toInt(); |
|
| 32 | - $f2 = (int) $f[2]->toInt(); |
|
| 33 | - $f3 = (int) $f[3]->toInt(); |
|
| 34 | - $f4 = (int) $f[4]->toInt(); |
|
| 35 | - $f5 = (int) $f[5]->toInt(); |
|
| 36 | - $f6 = (int) $f[6]->toInt(); |
|
| 37 | - $f7 = (int) $f[7]->toInt(); |
|
| 38 | - $f8 = (int) $f[8]->toInt(); |
|
| 39 | - $f9 = (int) $f[9]->toInt(); |
|
| 40 | - $g0 = (int) $g[0]->toInt(); |
|
| 41 | - $g1 = (int) $g[1]->toInt(); |
|
| 42 | - $g2 = (int) $g[2]->toInt(); |
|
| 43 | - $g3 = (int) $g[3]->toInt(); |
|
| 44 | - $g4 = (int) $g[4]->toInt(); |
|
| 45 | - $g5 = (int) $g[5]->toInt(); |
|
| 46 | - $g6 = (int) $g[6]->toInt(); |
|
| 47 | - $g7 = (int) $g[7]->toInt(); |
|
| 48 | - $g8 = (int) $g[8]->toInt(); |
|
| 49 | - $g9 = (int) $g[9]->toInt(); |
|
| 50 | - $b = -$b; |
|
| 51 | - /** @var int $x0 */ |
|
| 52 | - $x0 = ($f0 ^ $g0) & $b; |
|
| 53 | - /** @var int $x1 */ |
|
| 54 | - $x1 = ($f1 ^ $g1) & $b; |
|
| 55 | - /** @var int $x2 */ |
|
| 56 | - $x2 = ($f2 ^ $g2) & $b; |
|
| 57 | - /** @var int $x3 */ |
|
| 58 | - $x3 = ($f3 ^ $g3) & $b; |
|
| 59 | - /** @var int $x4 */ |
|
| 60 | - $x4 = ($f4 ^ $g4) & $b; |
|
| 61 | - /** @var int $x5 */ |
|
| 62 | - $x5 = ($f5 ^ $g5) & $b; |
|
| 63 | - /** @var int $x6 */ |
|
| 64 | - $x6 = ($f6 ^ $g6) & $b; |
|
| 65 | - /** @var int $x7 */ |
|
| 66 | - $x7 = ($f7 ^ $g7) & $b; |
|
| 67 | - /** @var int $x8 */ |
|
| 68 | - $x8 = ($f8 ^ $g8) & $b; |
|
| 69 | - /** @var int $x9 */ |
|
| 70 | - $x9 = ($f9 ^ $g9) & $b; |
|
| 71 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
| 72 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
| 73 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
| 74 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
| 75 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
| 76 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
| 77 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
| 78 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
| 79 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
| 80 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
| 81 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
| 82 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
| 83 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
| 84 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
| 85 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
| 86 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
| 87 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
| 88 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
| 89 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
| 90 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @internal You should not use this directly from another application |
|
| 95 | - * |
|
| 96 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
| 97 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
| 98 | - * @throws SodiumException |
|
| 99 | - * @throws TypeError |
|
| 100 | - * @psalm-suppress MixedAssignment |
|
| 101 | - * @psalm-suppress MixedMethodCall |
|
| 102 | - */ |
|
| 103 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
| 104 | - { |
|
| 105 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
| 106 | - $h = array(); |
|
| 107 | - for ($i = 0; $i < 10; ++$i) { |
|
| 108 | - $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
| 112 | - $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
| 113 | - $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
| 114 | - |
|
| 115 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
| 116 | - $h[2] = $h[2]->addInt64($carry1); |
|
| 117 | - $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
| 118 | - |
|
| 119 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
| 120 | - $h[4] = $h[4]->addInt64($carry3); |
|
| 121 | - $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
| 122 | - |
|
| 123 | - $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
| 124 | - $h[6] = $h[6]->addInt64($carry5); |
|
| 125 | - $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
| 126 | - |
|
| 127 | - $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
| 128 | - $h[8] = $h[8]->addInt64($carry7); |
|
| 129 | - $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
| 130 | - |
|
| 131 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
| 132 | - $h[1] = $h[1]->addInt64($carry0); |
|
| 133 | - $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
| 134 | - |
|
| 135 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
| 136 | - $h[3] = $h[3]->addInt64($carry2); |
|
| 137 | - $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
| 138 | - |
|
| 139 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
| 140 | - $h[5] = $h[5]->addInt64($carry4); |
|
| 141 | - $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
| 142 | - |
|
| 143 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
| 144 | - $h[7] = $h[7]->addInt64($carry6); |
|
| 145 | - $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
| 146 | - |
|
| 147 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
| 148 | - $h[9] = $h[9]->addInt64($carry8); |
|
| 149 | - $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
| 150 | - |
|
| 151 | - for ($i = 0; $i < 10; ++$i) { |
|
| 152 | - $h[$i] = $h[$i]->toInt32(); |
|
| 153 | - } |
|
| 154 | - /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
| 155 | - $h2 = $h; |
|
| 156 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @internal You should not use this directly from another application |
|
| 161 | - * |
|
| 162 | - * Inline comments preceded by # are from libsodium's ref10 code. |
|
| 163 | - * |
|
| 164 | - * @param string $n |
|
| 165 | - * @param string $p |
|
| 166 | - * @return string |
|
| 167 | - * @throws SodiumException |
|
| 168 | - * @throws TypeError |
|
| 169 | - */ |
|
| 170 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
| 171 | - { |
|
| 172 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
| 173 | - $e = '' . $n; |
|
| 174 | - # e[0] &= 248; |
|
| 175 | - $e[0] = self::intToChr( |
|
| 176 | - self::chrToInt($e[0]) & 248 |
|
| 177 | - ); |
|
| 178 | - # e[31] &= 127; |
|
| 179 | - # e[31] |= 64; |
|
| 180 | - $e[31] = self::intToChr( |
|
| 181 | - (self::chrToInt($e[31]) & 127) | 64 |
|
| 182 | - ); |
|
| 183 | - # fe_frombytes(x1,p); |
|
| 184 | - $x1 = self::fe_frombytes($p); |
|
| 185 | - # fe_1(x2); |
|
| 186 | - $x2 = self::fe_1(); |
|
| 187 | - # fe_0(z2); |
|
| 188 | - $z2 = self::fe_0(); |
|
| 189 | - # fe_copy(x3,x1); |
|
| 190 | - $x3 = self::fe_copy($x1); |
|
| 191 | - # fe_1(z3); |
|
| 192 | - $z3 = self::fe_1(); |
|
| 193 | - |
|
| 194 | - # swap = 0; |
|
| 195 | - /** @var int $swap */ |
|
| 196 | - $swap = 0; |
|
| 197 | - |
|
| 198 | - # for (pos = 254;pos >= 0;--pos) { |
|
| 199 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
| 200 | - # b = e[pos / 8] >> (pos & 7); |
|
| 201 | - /** @var int $b */ |
|
| 202 | - $b = self::chrToInt( |
|
| 203 | - $e[(int) floor($pos / 8)] |
|
| 204 | - ) >> ($pos & 7); |
|
| 205 | - # b &= 1; |
|
| 206 | - $b &= 1; |
|
| 207 | - |
|
| 208 | - # swap ^= b; |
|
| 209 | - $swap ^= $b; |
|
| 210 | - |
|
| 211 | - # fe_cswap(x2,x3,swap); |
|
| 212 | - self::fe_cswap($x2, $x3, $swap); |
|
| 213 | - |
|
| 214 | - # fe_cswap(z2,z3,swap); |
|
| 215 | - self::fe_cswap($z2, $z3, $swap); |
|
| 216 | - |
|
| 217 | - # swap = b; |
|
| 218 | - /** @var int $swap */ |
|
| 219 | - $swap = $b; |
|
| 220 | - |
|
| 221 | - # fe_sub(tmp0,x3,z3); |
|
| 222 | - $tmp0 = self::fe_sub($x3, $z3); |
|
| 223 | - |
|
| 224 | - # fe_sub(tmp1,x2,z2); |
|
| 225 | - $tmp1 = self::fe_sub($x2, $z2); |
|
| 226 | - |
|
| 227 | - # fe_add(x2,x2,z2); |
|
| 228 | - $x2 = self::fe_add($x2, $z2); |
|
| 229 | - |
|
| 230 | - # fe_add(z2,x3,z3); |
|
| 231 | - $z2 = self::fe_add($x3, $z3); |
|
| 232 | - |
|
| 233 | - # fe_mul(z3,tmp0,x2); |
|
| 234 | - $z3 = self::fe_mul($tmp0, $x2); |
|
| 235 | - |
|
| 236 | - # fe_mul(z2,z2,tmp1); |
|
| 237 | - $z2 = self::fe_mul($z2, $tmp1); |
|
| 238 | - |
|
| 239 | - # fe_sq(tmp0,tmp1); |
|
| 240 | - $tmp0 = self::fe_sq($tmp1); |
|
| 241 | - |
|
| 242 | - # fe_sq(tmp1,x2); |
|
| 243 | - $tmp1 = self::fe_sq($x2); |
|
| 244 | - |
|
| 245 | - # fe_add(x3,z3,z2); |
|
| 246 | - $x3 = self::fe_add($z3, $z2); |
|
| 247 | - |
|
| 248 | - # fe_sub(z2,z3,z2); |
|
| 249 | - $z2 = self::fe_sub($z3, $z2); |
|
| 250 | - |
|
| 251 | - # fe_mul(x2,tmp1,tmp0); |
|
| 252 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
| 253 | - |
|
| 254 | - # fe_sub(tmp1,tmp1,tmp0); |
|
| 255 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
| 256 | - |
|
| 257 | - # fe_sq(z2,z2); |
|
| 258 | - $z2 = self::fe_sq($z2); |
|
| 259 | - |
|
| 260 | - # fe_mul121666(z3,tmp1); |
|
| 261 | - $z3 = self::fe_mul121666($tmp1); |
|
| 262 | - |
|
| 263 | - # fe_sq(x3,x3); |
|
| 264 | - $x3 = self::fe_sq($x3); |
|
| 265 | - |
|
| 266 | - # fe_add(tmp0,tmp0,z3); |
|
| 267 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
| 268 | - |
|
| 269 | - # fe_mul(z3,x1,z2); |
|
| 270 | - $z3 = self::fe_mul($x1, $z2); |
|
| 271 | - |
|
| 272 | - # fe_mul(z2,tmp1,tmp0); |
|
| 273 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - # fe_cswap(x2,x3,swap); |
|
| 277 | - self::fe_cswap($x2, $x3, $swap); |
|
| 278 | - |
|
| 279 | - # fe_cswap(z2,z3,swap); |
|
| 280 | - self::fe_cswap($z2, $z3, $swap); |
|
| 281 | - |
|
| 282 | - # fe_invert(z2,z2); |
|
| 283 | - $z2 = self::fe_invert($z2); |
|
| 284 | - |
|
| 285 | - # fe_mul(x2,x2,z2); |
|
| 286 | - $x2 = self::fe_mul($x2, $z2); |
|
| 287 | - # fe_tobytes(q,x2); |
|
| 288 | - return (string) self::fe_tobytes($x2); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * @internal You should not use this directly from another application |
|
| 293 | - * |
|
| 294 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
| 295 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
| 296 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
| 297 | - * @throws SodiumException |
|
| 298 | - * @throws TypeError |
|
| 299 | - */ |
|
| 300 | - public static function edwards_to_montgomery( |
|
| 301 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
| 302 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
| 303 | - ) { |
|
| 304 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
| 305 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
| 306 | - $tempZ = self::fe_invert($tempZ); |
|
| 307 | - return self::fe_mul($tempX, $tempZ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @internal You should not use this directly from another application |
|
| 312 | - * |
|
| 313 | - * @param string $n |
|
| 314 | - * @return string |
|
| 315 | - * @throws SodiumException |
|
| 316 | - * @throws TypeError |
|
| 317 | - */ |
|
| 318 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
| 319 | - { |
|
| 320 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
| 321 | - $e = '' . $n; |
|
| 322 | - |
|
| 323 | - # e[0] &= 248; |
|
| 324 | - $e[0] = self::intToChr( |
|
| 325 | - self::chrToInt($e[0]) & 248 |
|
| 326 | - ); |
|
| 327 | - |
|
| 328 | - # e[31] &= 127; |
|
| 329 | - # e[31] |= 64; |
|
| 330 | - $e[31] = self::intToChr( |
|
| 331 | - (self::chrToInt($e[31]) & 127) | 64 |
|
| 332 | - ); |
|
| 333 | - |
|
| 334 | - $A = self::ge_scalarmult_base($e); |
|
| 335 | - if ( |
|
| 336 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
| 337 | - || |
|
| 338 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
| 339 | - ) { |
|
| 340 | - throw new TypeError('Null points encountered'); |
|
| 341 | - } |
|
| 342 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
| 343 | - return self::fe_tobytes($pk); |
|
| 344 | - } |
|
| 12 | + /** |
|
| 13 | + * Alters the objects passed to this method in place. |
|
| 14 | + * |
|
| 15 | + * @internal You should not use this directly from another application |
|
| 16 | + * |
|
| 17 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
| 18 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
| 19 | + * @param int $b |
|
| 20 | + * @return void |
|
| 21 | + * @throws SodiumException |
|
| 22 | + * @throws TypeError |
|
| 23 | + * @psalm-suppress MixedMethodCall |
|
| 24 | + */ |
|
| 25 | + public static function fe_cswap( |
|
| 26 | + ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
| 27 | + ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
| 28 | + $b = 0 |
|
| 29 | + ) { |
|
| 30 | + $f0 = (int) $f[0]->toInt(); |
|
| 31 | + $f1 = (int) $f[1]->toInt(); |
|
| 32 | + $f2 = (int) $f[2]->toInt(); |
|
| 33 | + $f3 = (int) $f[3]->toInt(); |
|
| 34 | + $f4 = (int) $f[4]->toInt(); |
|
| 35 | + $f5 = (int) $f[5]->toInt(); |
|
| 36 | + $f6 = (int) $f[6]->toInt(); |
|
| 37 | + $f7 = (int) $f[7]->toInt(); |
|
| 38 | + $f8 = (int) $f[8]->toInt(); |
|
| 39 | + $f9 = (int) $f[9]->toInt(); |
|
| 40 | + $g0 = (int) $g[0]->toInt(); |
|
| 41 | + $g1 = (int) $g[1]->toInt(); |
|
| 42 | + $g2 = (int) $g[2]->toInt(); |
|
| 43 | + $g3 = (int) $g[3]->toInt(); |
|
| 44 | + $g4 = (int) $g[4]->toInt(); |
|
| 45 | + $g5 = (int) $g[5]->toInt(); |
|
| 46 | + $g6 = (int) $g[6]->toInt(); |
|
| 47 | + $g7 = (int) $g[7]->toInt(); |
|
| 48 | + $g8 = (int) $g[8]->toInt(); |
|
| 49 | + $g9 = (int) $g[9]->toInt(); |
|
| 50 | + $b = -$b; |
|
| 51 | + /** @var int $x0 */ |
|
| 52 | + $x0 = ($f0 ^ $g0) & $b; |
|
| 53 | + /** @var int $x1 */ |
|
| 54 | + $x1 = ($f1 ^ $g1) & $b; |
|
| 55 | + /** @var int $x2 */ |
|
| 56 | + $x2 = ($f2 ^ $g2) & $b; |
|
| 57 | + /** @var int $x3 */ |
|
| 58 | + $x3 = ($f3 ^ $g3) & $b; |
|
| 59 | + /** @var int $x4 */ |
|
| 60 | + $x4 = ($f4 ^ $g4) & $b; |
|
| 61 | + /** @var int $x5 */ |
|
| 62 | + $x5 = ($f5 ^ $g5) & $b; |
|
| 63 | + /** @var int $x6 */ |
|
| 64 | + $x6 = ($f6 ^ $g6) & $b; |
|
| 65 | + /** @var int $x7 */ |
|
| 66 | + $x7 = ($f7 ^ $g7) & $b; |
|
| 67 | + /** @var int $x8 */ |
|
| 68 | + $x8 = ($f8 ^ $g8) & $b; |
|
| 69 | + /** @var int $x9 */ |
|
| 70 | + $x9 = ($f9 ^ $g9) & $b; |
|
| 71 | + $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
| 72 | + $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
| 73 | + $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
| 74 | + $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
| 75 | + $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
| 76 | + $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
| 77 | + $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
| 78 | + $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
| 79 | + $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
| 80 | + $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
| 81 | + $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
| 82 | + $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
| 83 | + $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
| 84 | + $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
| 85 | + $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
| 86 | + $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
| 87 | + $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
| 88 | + $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
| 89 | + $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
| 90 | + $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @internal You should not use this directly from another application |
|
| 95 | + * |
|
| 96 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
| 97 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
| 98 | + * @throws SodiumException |
|
| 99 | + * @throws TypeError |
|
| 100 | + * @psalm-suppress MixedAssignment |
|
| 101 | + * @psalm-suppress MixedMethodCall |
|
| 102 | + */ |
|
| 103 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
| 104 | + { |
|
| 105 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
| 106 | + $h = array(); |
|
| 107 | + for ($i = 0; $i < 10; ++$i) { |
|
| 108 | + $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
| 112 | + $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
| 113 | + $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
| 114 | + |
|
| 115 | + $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
| 116 | + $h[2] = $h[2]->addInt64($carry1); |
|
| 117 | + $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
| 118 | + |
|
| 119 | + $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
| 120 | + $h[4] = $h[4]->addInt64($carry3); |
|
| 121 | + $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
| 122 | + |
|
| 123 | + $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
| 124 | + $h[6] = $h[6]->addInt64($carry5); |
|
| 125 | + $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
| 126 | + |
|
| 127 | + $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
| 128 | + $h[8] = $h[8]->addInt64($carry7); |
|
| 129 | + $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
| 130 | + |
|
| 131 | + $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
| 132 | + $h[1] = $h[1]->addInt64($carry0); |
|
| 133 | + $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
| 134 | + |
|
| 135 | + $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
| 136 | + $h[3] = $h[3]->addInt64($carry2); |
|
| 137 | + $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
| 138 | + |
|
| 139 | + $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
| 140 | + $h[5] = $h[5]->addInt64($carry4); |
|
| 141 | + $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
| 142 | + |
|
| 143 | + $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
| 144 | + $h[7] = $h[7]->addInt64($carry6); |
|
| 145 | + $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
| 146 | + |
|
| 147 | + $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
| 148 | + $h[9] = $h[9]->addInt64($carry8); |
|
| 149 | + $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
| 150 | + |
|
| 151 | + for ($i = 0; $i < 10; ++$i) { |
|
| 152 | + $h[$i] = $h[$i]->toInt32(); |
|
| 153 | + } |
|
| 154 | + /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
| 155 | + $h2 = $h; |
|
| 156 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @internal You should not use this directly from another application |
|
| 161 | + * |
|
| 162 | + * Inline comments preceded by # are from libsodium's ref10 code. |
|
| 163 | + * |
|
| 164 | + * @param string $n |
|
| 165 | + * @param string $p |
|
| 166 | + * @return string |
|
| 167 | + * @throws SodiumException |
|
| 168 | + * @throws TypeError |
|
| 169 | + */ |
|
| 170 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
| 171 | + { |
|
| 172 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
| 173 | + $e = '' . $n; |
|
| 174 | + # e[0] &= 248; |
|
| 175 | + $e[0] = self::intToChr( |
|
| 176 | + self::chrToInt($e[0]) & 248 |
|
| 177 | + ); |
|
| 178 | + # e[31] &= 127; |
|
| 179 | + # e[31] |= 64; |
|
| 180 | + $e[31] = self::intToChr( |
|
| 181 | + (self::chrToInt($e[31]) & 127) | 64 |
|
| 182 | + ); |
|
| 183 | + # fe_frombytes(x1,p); |
|
| 184 | + $x1 = self::fe_frombytes($p); |
|
| 185 | + # fe_1(x2); |
|
| 186 | + $x2 = self::fe_1(); |
|
| 187 | + # fe_0(z2); |
|
| 188 | + $z2 = self::fe_0(); |
|
| 189 | + # fe_copy(x3,x1); |
|
| 190 | + $x3 = self::fe_copy($x1); |
|
| 191 | + # fe_1(z3); |
|
| 192 | + $z3 = self::fe_1(); |
|
| 193 | + |
|
| 194 | + # swap = 0; |
|
| 195 | + /** @var int $swap */ |
|
| 196 | + $swap = 0; |
|
| 197 | + |
|
| 198 | + # for (pos = 254;pos >= 0;--pos) { |
|
| 199 | + for ($pos = 254; $pos >= 0; --$pos) { |
|
| 200 | + # b = e[pos / 8] >> (pos & 7); |
|
| 201 | + /** @var int $b */ |
|
| 202 | + $b = self::chrToInt( |
|
| 203 | + $e[(int) floor($pos / 8)] |
|
| 204 | + ) >> ($pos & 7); |
|
| 205 | + # b &= 1; |
|
| 206 | + $b &= 1; |
|
| 207 | + |
|
| 208 | + # swap ^= b; |
|
| 209 | + $swap ^= $b; |
|
| 210 | + |
|
| 211 | + # fe_cswap(x2,x3,swap); |
|
| 212 | + self::fe_cswap($x2, $x3, $swap); |
|
| 213 | + |
|
| 214 | + # fe_cswap(z2,z3,swap); |
|
| 215 | + self::fe_cswap($z2, $z3, $swap); |
|
| 216 | + |
|
| 217 | + # swap = b; |
|
| 218 | + /** @var int $swap */ |
|
| 219 | + $swap = $b; |
|
| 220 | + |
|
| 221 | + # fe_sub(tmp0,x3,z3); |
|
| 222 | + $tmp0 = self::fe_sub($x3, $z3); |
|
| 223 | + |
|
| 224 | + # fe_sub(tmp1,x2,z2); |
|
| 225 | + $tmp1 = self::fe_sub($x2, $z2); |
|
| 226 | + |
|
| 227 | + # fe_add(x2,x2,z2); |
|
| 228 | + $x2 = self::fe_add($x2, $z2); |
|
| 229 | + |
|
| 230 | + # fe_add(z2,x3,z3); |
|
| 231 | + $z2 = self::fe_add($x3, $z3); |
|
| 232 | + |
|
| 233 | + # fe_mul(z3,tmp0,x2); |
|
| 234 | + $z3 = self::fe_mul($tmp0, $x2); |
|
| 235 | + |
|
| 236 | + # fe_mul(z2,z2,tmp1); |
|
| 237 | + $z2 = self::fe_mul($z2, $tmp1); |
|
| 238 | + |
|
| 239 | + # fe_sq(tmp0,tmp1); |
|
| 240 | + $tmp0 = self::fe_sq($tmp1); |
|
| 241 | + |
|
| 242 | + # fe_sq(tmp1,x2); |
|
| 243 | + $tmp1 = self::fe_sq($x2); |
|
| 244 | + |
|
| 245 | + # fe_add(x3,z3,z2); |
|
| 246 | + $x3 = self::fe_add($z3, $z2); |
|
| 247 | + |
|
| 248 | + # fe_sub(z2,z3,z2); |
|
| 249 | + $z2 = self::fe_sub($z3, $z2); |
|
| 250 | + |
|
| 251 | + # fe_mul(x2,tmp1,tmp0); |
|
| 252 | + $x2 = self::fe_mul($tmp1, $tmp0); |
|
| 253 | + |
|
| 254 | + # fe_sub(tmp1,tmp1,tmp0); |
|
| 255 | + $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
| 256 | + |
|
| 257 | + # fe_sq(z2,z2); |
|
| 258 | + $z2 = self::fe_sq($z2); |
|
| 259 | + |
|
| 260 | + # fe_mul121666(z3,tmp1); |
|
| 261 | + $z3 = self::fe_mul121666($tmp1); |
|
| 262 | + |
|
| 263 | + # fe_sq(x3,x3); |
|
| 264 | + $x3 = self::fe_sq($x3); |
|
| 265 | + |
|
| 266 | + # fe_add(tmp0,tmp0,z3); |
|
| 267 | + $tmp0 = self::fe_add($tmp0, $z3); |
|
| 268 | + |
|
| 269 | + # fe_mul(z3,x1,z2); |
|
| 270 | + $z3 = self::fe_mul($x1, $z2); |
|
| 271 | + |
|
| 272 | + # fe_mul(z2,tmp1,tmp0); |
|
| 273 | + $z2 = self::fe_mul($tmp1, $tmp0); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + # fe_cswap(x2,x3,swap); |
|
| 277 | + self::fe_cswap($x2, $x3, $swap); |
|
| 278 | + |
|
| 279 | + # fe_cswap(z2,z3,swap); |
|
| 280 | + self::fe_cswap($z2, $z3, $swap); |
|
| 281 | + |
|
| 282 | + # fe_invert(z2,z2); |
|
| 283 | + $z2 = self::fe_invert($z2); |
|
| 284 | + |
|
| 285 | + # fe_mul(x2,x2,z2); |
|
| 286 | + $x2 = self::fe_mul($x2, $z2); |
|
| 287 | + # fe_tobytes(q,x2); |
|
| 288 | + return (string) self::fe_tobytes($x2); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * @internal You should not use this directly from another application |
|
| 293 | + * |
|
| 294 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
| 295 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
| 296 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
| 297 | + * @throws SodiumException |
|
| 298 | + * @throws TypeError |
|
| 299 | + */ |
|
| 300 | + public static function edwards_to_montgomery( |
|
| 301 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
| 302 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
| 303 | + ) { |
|
| 304 | + $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
| 305 | + $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
| 306 | + $tempZ = self::fe_invert($tempZ); |
|
| 307 | + return self::fe_mul($tempX, $tempZ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @internal You should not use this directly from another application |
|
| 312 | + * |
|
| 313 | + * @param string $n |
|
| 314 | + * @return string |
|
| 315 | + * @throws SodiumException |
|
| 316 | + * @throws TypeError |
|
| 317 | + */ |
|
| 318 | + public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
| 319 | + { |
|
| 320 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
| 321 | + $e = '' . $n; |
|
| 322 | + |
|
| 323 | + # e[0] &= 248; |
|
| 324 | + $e[0] = self::intToChr( |
|
| 325 | + self::chrToInt($e[0]) & 248 |
|
| 326 | + ); |
|
| 327 | + |
|
| 328 | + # e[31] &= 127; |
|
| 329 | + # e[31] |= 64; |
|
| 330 | + $e[31] = self::intToChr( |
|
| 331 | + (self::chrToInt($e[31]) & 127) | 64 |
|
| 332 | + ); |
|
| 333 | + |
|
| 334 | + $A = self::ge_scalarmult_base($e); |
|
| 335 | + if ( |
|
| 336 | + !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
| 337 | + || |
|
| 338 | + !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
| 339 | + ) { |
|
| 340 | + throw new TypeError('Null points encountered'); |
|
| 341 | + } |
|
| 342 | + $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
| 343 | + return self::fe_tobytes($pk); |
|
| 344 | + } |
|
| 345 | 345 | } |
@@ -9,863 +9,863 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core32_Int32 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array<int, int> - two 16-bit integers |
|
| 14 | - * |
|
| 15 | - * 0 is the higher 16 bits |
|
| 16 | - * 1 is the lower 16 bits |
|
| 17 | - */ |
|
| 18 | - public $limbs = array(0, 0); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var int |
|
| 22 | - */ |
|
| 23 | - public $overflow = 0; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - public $unsignedInt = false; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * ParagonIE_Sodium_Core32_Int32 constructor. |
|
| 32 | - * @param array $array |
|
| 33 | - * @param bool $unsignedInt |
|
| 34 | - */ |
|
| 35 | - public function __construct($array = array(0, 0), $unsignedInt = false) |
|
| 36 | - { |
|
| 37 | - $this->limbs = array( |
|
| 38 | - (int) $array[0], |
|
| 39 | - (int) $array[1] |
|
| 40 | - ); |
|
| 41 | - $this->overflow = 0; |
|
| 42 | - $this->unsignedInt = $unsignedInt; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Adds two int32 objects |
|
| 47 | - * |
|
| 48 | - * @param ParagonIE_Sodium_Core32_Int32 $addend |
|
| 49 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 50 | - */ |
|
| 51 | - public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
| 52 | - { |
|
| 53 | - $i0 = $this->limbs[0]; |
|
| 54 | - $i1 = $this->limbs[1]; |
|
| 55 | - $j0 = $addend->limbs[0]; |
|
| 56 | - $j1 = $addend->limbs[1]; |
|
| 57 | - |
|
| 58 | - $r1 = $i1 + ($j1 & 0xffff); |
|
| 59 | - $carry = $r1 >> 16; |
|
| 60 | - |
|
| 61 | - $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
| 62 | - $carry = $r0 >> 16; |
|
| 63 | - |
|
| 64 | - $r0 &= 0xffff; |
|
| 65 | - $r1 &= 0xffff; |
|
| 66 | - |
|
| 67 | - $return = new ParagonIE_Sodium_Core32_Int32( |
|
| 68 | - array($r0, $r1) |
|
| 69 | - ); |
|
| 70 | - $return->overflow = $carry; |
|
| 71 | - $return->unsignedInt = $this->unsignedInt; |
|
| 72 | - return $return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Adds a normal integer to an int32 object |
|
| 77 | - * |
|
| 78 | - * @param int $int |
|
| 79 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 80 | - * @throws SodiumException |
|
| 81 | - * @throws TypeError |
|
| 82 | - */ |
|
| 83 | - public function addInt($int) |
|
| 84 | - { |
|
| 85 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 86 | - /** @var int $int */ |
|
| 87 | - $int = (int) $int; |
|
| 88 | - |
|
| 89 | - $int = (int) $int; |
|
| 90 | - |
|
| 91 | - $i0 = $this->limbs[0]; |
|
| 92 | - $i1 = $this->limbs[1]; |
|
| 93 | - |
|
| 94 | - $r1 = $i1 + ($int & 0xffff); |
|
| 95 | - $carry = $r1 >> 16; |
|
| 96 | - |
|
| 97 | - $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; |
|
| 98 | - $carry = $r0 >> 16; |
|
| 99 | - $r0 &= 0xffff; |
|
| 100 | - $r1 &= 0xffff; |
|
| 101 | - $return = new ParagonIE_Sodium_Core32_Int32( |
|
| 102 | - array($r0, $r1) |
|
| 103 | - ); |
|
| 104 | - $return->overflow = $carry; |
|
| 105 | - $return->unsignedInt = $this->unsignedInt; |
|
| 106 | - return $return; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param int $b |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function compareInt($b = 0) |
|
| 114 | - { |
|
| 115 | - $gt = 0; |
|
| 116 | - $eq = 1; |
|
| 117 | - |
|
| 118 | - $i = 2; |
|
| 119 | - $j = 0; |
|
| 120 | - while ($i > 0) { |
|
| 121 | - --$i; |
|
| 122 | - /** @var int $x1 */ |
|
| 123 | - $x1 = $this->limbs[$i]; |
|
| 124 | - /** @var int $x2 */ |
|
| 125 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
| 126 | - /** @var int $gt */ |
|
| 127 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
| 128 | - /** @var int $eq */ |
|
| 129 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
| 130 | - } |
|
| 131 | - return ($gt + $gt - $eq) + 1; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param int $m |
|
| 136 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 137 | - */ |
|
| 138 | - public function mask($m = 0) |
|
| 139 | - { |
|
| 140 | - /** @var int $hi */ |
|
| 141 | - $hi = ($m >> 16) & 0xffff; |
|
| 142 | - /** @var int $lo */ |
|
| 143 | - $lo = ($m & 0xffff); |
|
| 144 | - return new ParagonIE_Sodium_Core32_Int32( |
|
| 145 | - array( |
|
| 146 | - (int) ($this->limbs[0] & $hi), |
|
| 147 | - (int) ($this->limbs[1] & $lo) |
|
| 148 | - ), |
|
| 149 | - $this->unsignedInt |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param array<int, int> $a |
|
| 155 | - * @param array<int, int> $b |
|
| 156 | - * @param int $baseLog2 |
|
| 157 | - * @return array<int, int> |
|
| 158 | - */ |
|
| 159 | - public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
| 160 | - { |
|
| 161 | - $a_l = count($a); |
|
| 162 | - $b_l = count($b); |
|
| 163 | - /** @var array<int, int> $r */ |
|
| 164 | - $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
| 165 | - $base = 1 << $baseLog2; |
|
| 166 | - for ($i = 0; $i < $a_l; ++$i) { |
|
| 167 | - $a_i = $a[$i]; |
|
| 168 | - for ($j = 0; $j < $a_l; ++$j) { |
|
| 169 | - $b_j = $b[$j]; |
|
| 170 | - $product = ($a_i * $b_j) + $r[$i + $j]; |
|
| 171 | - $carry = ($product >> $baseLog2 & 0xffff); |
|
| 172 | - $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
| 173 | - $r[$i + $j + 1] += $carry; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - return array_slice($r, 0, 5); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param int $int |
|
| 181 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 182 | - */ |
|
| 183 | - public function mulIntFast($int) |
|
| 184 | - { |
|
| 185 | - // Handle negative numbers |
|
| 186 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
| 187 | - $bNeg = ($int >> 31) & 1; |
|
| 188 | - $a = array_reverse($this->limbs); |
|
| 189 | - $b = array( |
|
| 190 | - $int & 0xffff, |
|
| 191 | - ($int >> 16) & 0xffff |
|
| 192 | - ); |
|
| 193 | - if ($aNeg) { |
|
| 194 | - for ($i = 0; $i < 2; ++$i) { |
|
| 195 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
| 196 | - } |
|
| 197 | - ++$a[0]; |
|
| 198 | - } |
|
| 199 | - if ($bNeg) { |
|
| 200 | - for ($i = 0; $i < 2; ++$i) { |
|
| 201 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
| 202 | - } |
|
| 203 | - ++$b[0]; |
|
| 204 | - } |
|
| 205 | - // Multiply |
|
| 206 | - $res = $this->multiplyLong($a, $b); |
|
| 207 | - |
|
| 208 | - // Re-apply negation to results |
|
| 209 | - if ($aNeg !== $bNeg) { |
|
| 210 | - for ($i = 0; $i < 2; ++$i) { |
|
| 211 | - $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
| 212 | - } |
|
| 213 | - // Handle integer overflow |
|
| 214 | - $c = 1; |
|
| 215 | - for ($i = 0; $i < 2; ++$i) { |
|
| 216 | - $res[$i] += $c; |
|
| 217 | - $c = $res[$i] >> 16; |
|
| 218 | - $res[$i] &= 0xffff; |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // Return our values |
|
| 223 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 224 | - $return->limbs = array( |
|
| 225 | - $res[1] & 0xffff, |
|
| 226 | - $res[0] & 0xffff |
|
| 227 | - ); |
|
| 228 | - if (count($res) > 2) { |
|
| 229 | - $return->overflow = $res[2] & 0xffff; |
|
| 230 | - } |
|
| 231 | - $return->unsignedInt = $this->unsignedInt; |
|
| 232 | - return $return; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param ParagonIE_Sodium_Core32_Int32 $right |
|
| 237 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 238 | - */ |
|
| 239 | - public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
| 240 | - { |
|
| 241 | - $aNeg = ($this->limbs[0] >> 15) & 1; |
|
| 242 | - $bNeg = ($right->limbs[0] >> 15) & 1; |
|
| 243 | - |
|
| 244 | - $a = array_reverse($this->limbs); |
|
| 245 | - $b = array_reverse($right->limbs); |
|
| 246 | - if ($aNeg) { |
|
| 247 | - for ($i = 0; $i < 2; ++$i) { |
|
| 248 | - $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
| 249 | - } |
|
| 250 | - ++$a[0]; |
|
| 251 | - } |
|
| 252 | - if ($bNeg) { |
|
| 253 | - for ($i = 0; $i < 2; ++$i) { |
|
| 254 | - $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
| 255 | - } |
|
| 256 | - ++$b[0]; |
|
| 257 | - } |
|
| 258 | - $res = $this->multiplyLong($a, $b); |
|
| 259 | - if ($aNeg !== $bNeg) { |
|
| 260 | - if ($aNeg !== $bNeg) { |
|
| 261 | - for ($i = 0; $i < 2; ++$i) { |
|
| 262 | - $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
| 263 | - } |
|
| 264 | - $c = 1; |
|
| 265 | - for ($i = 0; $i < 2; ++$i) { |
|
| 266 | - $res[$i] += $c; |
|
| 267 | - $c = $res[$i] >> 16; |
|
| 268 | - $res[$i] &= 0xffff; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 273 | - $return->limbs = array( |
|
| 274 | - $res[1] & 0xffff, |
|
| 275 | - $res[0] & 0xffff |
|
| 276 | - ); |
|
| 277 | - if (count($res) > 2) { |
|
| 278 | - $return->overflow = $res[2]; |
|
| 279 | - } |
|
| 280 | - return $return; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @param int $int |
|
| 285 | - * @param int $size |
|
| 286 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 287 | - * @throws SodiumException |
|
| 288 | - * @throws TypeError |
|
| 289 | - */ |
|
| 290 | - public function mulInt($int = 0, $size = 0) |
|
| 291 | - { |
|
| 292 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 293 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 294 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
| 295 | - return $this->mulIntFast((int) $int); |
|
| 296 | - } |
|
| 297 | - /** @var int $int */ |
|
| 298 | - $int = (int) $int; |
|
| 299 | - /** @var int $size */ |
|
| 300 | - $size = (int) $size; |
|
| 301 | - |
|
| 302 | - if (!$size) { |
|
| 303 | - $size = 31; |
|
| 304 | - } |
|
| 305 | - /** @var int $size */ |
|
| 306 | - |
|
| 307 | - $a = clone $this; |
|
| 308 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 309 | - $return->unsignedInt = $this->unsignedInt; |
|
| 310 | - |
|
| 311 | - // Initialize: |
|
| 312 | - $ret0 = 0; |
|
| 313 | - $ret1 = 0; |
|
| 314 | - $a0 = $a->limbs[0]; |
|
| 315 | - $a1 = $a->limbs[1]; |
|
| 316 | - |
|
| 317 | - /** @var int $size */ |
|
| 318 | - /** @var int $i */ |
|
| 319 | - for ($i = $size; $i >= 0; --$i) { |
|
| 320 | - $m = (int) (-($int & 1)); |
|
| 321 | - $x0 = $a0 & $m; |
|
| 322 | - $x1 = $a1 & $m; |
|
| 323 | - |
|
| 324 | - $ret1 += $x1; |
|
| 325 | - $c = $ret1 >> 16; |
|
| 326 | - |
|
| 327 | - $ret0 += $x0 + $c; |
|
| 328 | - |
|
| 329 | - $ret0 &= 0xffff; |
|
| 330 | - $ret1 &= 0xffff; |
|
| 331 | - |
|
| 332 | - $a1 = ($a1 << 1); |
|
| 333 | - $x1 = $a1 >> 16; |
|
| 334 | - $a0 = ($a0 << 1) | $x1; |
|
| 335 | - $a0 &= 0xffff; |
|
| 336 | - $a1 &= 0xffff; |
|
| 337 | - $int >>= 1; |
|
| 338 | - } |
|
| 339 | - $return->limbs[0] = $ret0; |
|
| 340 | - $return->limbs[1] = $ret1; |
|
| 341 | - return $return; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * @param ParagonIE_Sodium_Core32_Int32 $int |
|
| 346 | - * @param int $size |
|
| 347 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 348 | - * @throws SodiumException |
|
| 349 | - * @throws TypeError |
|
| 350 | - */ |
|
| 351 | - public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
| 352 | - { |
|
| 353 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 354 | - if (ParagonIE_Sodium_Compat::$fastMult) { |
|
| 355 | - return $this->mulInt32Fast($int); |
|
| 356 | - } |
|
| 357 | - if (!$size) { |
|
| 358 | - $size = 31; |
|
| 359 | - } |
|
| 360 | - /** @var int $size */ |
|
| 361 | - |
|
| 362 | - $a = clone $this; |
|
| 363 | - $b = clone $int; |
|
| 364 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 365 | - $return->unsignedInt = $this->unsignedInt; |
|
| 366 | - |
|
| 367 | - // Initialize: |
|
| 368 | - $ret0 = 0; |
|
| 369 | - $ret1 = 0; |
|
| 370 | - $a0 = $a->limbs[0]; |
|
| 371 | - $a1 = $a->limbs[1]; |
|
| 372 | - $b0 = $b->limbs[0]; |
|
| 373 | - $b1 = $b->limbs[1]; |
|
| 374 | - |
|
| 375 | - /** @var int $size */ |
|
| 376 | - /** @var int $i */ |
|
| 377 | - for ($i = $size; $i >= 0; --$i) { |
|
| 378 | - $m = (int) (-($b1 & 1)); |
|
| 379 | - $x0 = $a0 & $m; |
|
| 380 | - $x1 = $a1 & $m; |
|
| 381 | - |
|
| 382 | - $ret1 += $x1; |
|
| 383 | - $c = $ret1 >> 16; |
|
| 384 | - |
|
| 385 | - $ret0 += $x0 + $c; |
|
| 386 | - |
|
| 387 | - $ret0 &= 0xffff; |
|
| 388 | - $ret1 &= 0xffff; |
|
| 389 | - |
|
| 390 | - $a1 = ($a1 << 1); |
|
| 391 | - $x1 = $a1 >> 16; |
|
| 392 | - $a0 = ($a0 << 1) | $x1; |
|
| 393 | - $a0 &= 0xffff; |
|
| 394 | - $a1 &= 0xffff; |
|
| 395 | - |
|
| 396 | - $x0 = ($b0 & 1) << 16; |
|
| 397 | - $b0 = ($b0 >> 1); |
|
| 398 | - $b1 = (($b1 | $x0) >> 1); |
|
| 399 | - |
|
| 400 | - $b0 &= 0xffff; |
|
| 401 | - $b1 &= 0xffff; |
|
| 402 | - |
|
| 403 | - } |
|
| 404 | - $return->limbs[0] = $ret0; |
|
| 405 | - $return->limbs[1] = $ret1; |
|
| 406 | - |
|
| 407 | - return $return; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * OR this 32-bit integer with another. |
|
| 412 | - * |
|
| 413 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 414 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 415 | - */ |
|
| 416 | - public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 417 | - { |
|
| 418 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 419 | - $return->unsignedInt = $this->unsignedInt; |
|
| 420 | - $return->limbs = array( |
|
| 421 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
| 422 | - (int) ($this->limbs[1] | $b->limbs[1]) |
|
| 423 | - ); |
|
| 424 | - /** @var int overflow */ |
|
| 425 | - $return->overflow = $this->overflow | $b->overflow; |
|
| 426 | - return $return; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * @param int $b |
|
| 431 | - * @return bool |
|
| 432 | - */ |
|
| 433 | - public function isGreaterThan($b = 0) |
|
| 434 | - { |
|
| 435 | - return $this->compareInt($b) > 0; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * @param int $b |
|
| 440 | - * @return bool |
|
| 441 | - */ |
|
| 442 | - public function isLessThanInt($b = 0) |
|
| 443 | - { |
|
| 444 | - return $this->compareInt($b) < 0; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * @param int $c |
|
| 449 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 450 | - * @throws SodiumException |
|
| 451 | - * @throws TypeError |
|
| 452 | - * @psalm-suppress MixedArrayAccess |
|
| 453 | - */ |
|
| 454 | - public function rotateLeft($c = 0) |
|
| 455 | - { |
|
| 456 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 457 | - /** @var int $c */ |
|
| 458 | - $c = (int) $c; |
|
| 459 | - |
|
| 460 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 461 | - $return->unsignedInt = $this->unsignedInt; |
|
| 462 | - $c &= 31; |
|
| 463 | - if ($c === 0) { |
|
| 464 | - // NOP, but we want a copy. |
|
| 465 | - $return->limbs = $this->limbs; |
|
| 466 | - } else { |
|
| 467 | - /** @var int $c */ |
|
| 468 | - |
|
| 469 | - /** @var int $idx_shift */ |
|
| 470 | - $idx_shift = ($c >> 4) & 1; |
|
| 471 | - |
|
| 472 | - /** @var int $sub_shift */ |
|
| 473 | - $sub_shift = $c & 15; |
|
| 474 | - |
|
| 475 | - /** @var array<int, int> $limbs */ |
|
| 476 | - $limbs =& $return->limbs; |
|
| 477 | - |
|
| 478 | - /** @var array<int, int> $myLimbs */ |
|
| 479 | - $myLimbs =& $this->limbs; |
|
| 480 | - |
|
| 481 | - for ($i = 1; $i >= 0; --$i) { |
|
| 482 | - /** @var int $j */ |
|
| 483 | - $j = ($i + $idx_shift) & 1; |
|
| 484 | - /** @var int $k */ |
|
| 485 | - $k = ($i + $idx_shift + 1) & 1; |
|
| 486 | - $limbs[$i] = (int) ( |
|
| 487 | - ( |
|
| 488 | - ((int) ($myLimbs[$j]) << $sub_shift) |
|
| 489 | - | |
|
| 490 | - ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
| 491 | - ) & 0xffff |
|
| 492 | - ); |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - return $return; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Rotate to the right |
|
| 500 | - * |
|
| 501 | - * @param int $c |
|
| 502 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 503 | - * @throws SodiumException |
|
| 504 | - * @throws TypeError |
|
| 505 | - * @psalm-suppress MixedArrayAccess |
|
| 506 | - */ |
|
| 507 | - public function rotateRight($c = 0) |
|
| 508 | - { |
|
| 509 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 510 | - /** @var int $c */ |
|
| 511 | - $c = (int) $c; |
|
| 512 | - |
|
| 513 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 514 | - $return->unsignedInt = $this->unsignedInt; |
|
| 515 | - $c &= 31; |
|
| 516 | - /** @var int $c */ |
|
| 517 | - if ($c === 0) { |
|
| 518 | - // NOP, but we want a copy. |
|
| 519 | - $return->limbs = $this->limbs; |
|
| 520 | - } else { |
|
| 521 | - /** @var int $c */ |
|
| 522 | - |
|
| 523 | - /** @var int $idx_shift */ |
|
| 524 | - $idx_shift = ($c >> 4) & 1; |
|
| 525 | - |
|
| 526 | - /** @var int $sub_shift */ |
|
| 527 | - $sub_shift = $c & 15; |
|
| 528 | - |
|
| 529 | - /** @var array<int, int> $limbs */ |
|
| 530 | - $limbs =& $return->limbs; |
|
| 531 | - |
|
| 532 | - /** @var array<int, int> $myLimbs */ |
|
| 533 | - $myLimbs =& $this->limbs; |
|
| 534 | - |
|
| 535 | - for ($i = 1; $i >= 0; --$i) { |
|
| 536 | - /** @var int $j */ |
|
| 537 | - $j = ($i - $idx_shift) & 1; |
|
| 538 | - /** @var int $k */ |
|
| 539 | - $k = ($i - $idx_shift - 1) & 1; |
|
| 540 | - $limbs[$i] = (int) ( |
|
| 541 | - ( |
|
| 542 | - ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
| 543 | - | |
|
| 544 | - ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
| 545 | - ) & 0xffff |
|
| 546 | - ); |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - return $return; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * @param bool $bool |
|
| 554 | - * @return self |
|
| 555 | - */ |
|
| 556 | - public function setUnsignedInt($bool = false) |
|
| 557 | - { |
|
| 558 | - $this->unsignedInt = !empty($bool); |
|
| 559 | - return $this; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * @param int $c |
|
| 564 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 565 | - * @throws SodiumException |
|
| 566 | - * @throws TypeError |
|
| 567 | - */ |
|
| 568 | - public function shiftLeft($c = 0) |
|
| 569 | - { |
|
| 570 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 571 | - /** @var int $c */ |
|
| 572 | - $c = (int) $c; |
|
| 573 | - |
|
| 574 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 575 | - $return->unsignedInt = $this->unsignedInt; |
|
| 576 | - $c &= 63; |
|
| 577 | - /** @var int $c */ |
|
| 578 | - if ($c === 0) { |
|
| 579 | - $return->limbs = $this->limbs; |
|
| 580 | - } elseif ($c < 0) { |
|
| 581 | - /** @var int $c */ |
|
| 582 | - return $this->shiftRight(-$c); |
|
| 583 | - } else { |
|
| 584 | - /** @var int $c */ |
|
| 585 | - /** @var int $tmp */ |
|
| 586 | - $tmp = $this->limbs[1] << $c; |
|
| 587 | - $return->limbs[1] = (int)($tmp & 0xffff); |
|
| 588 | - /** @var int $carry */ |
|
| 589 | - $carry = $tmp >> 16; |
|
| 590 | - |
|
| 591 | - /** @var int $tmp */ |
|
| 592 | - $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); |
|
| 593 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 594 | - } |
|
| 595 | - return $return; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * @param int $c |
|
| 600 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 601 | - * @throws SodiumException |
|
| 602 | - * @throws TypeError |
|
| 603 | - * @psalm-suppress MixedAssignment |
|
| 604 | - * @psalm-suppress MixedOperand |
|
| 605 | - */ |
|
| 606 | - public function shiftRight($c = 0) |
|
| 607 | - { |
|
| 608 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 609 | - /** @var int $c */ |
|
| 610 | - $c = (int) $c; |
|
| 611 | - |
|
| 612 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 613 | - $return->unsignedInt = $this->unsignedInt; |
|
| 614 | - $c &= 63; |
|
| 615 | - /** @var int $c */ |
|
| 616 | - if ($c >= 16) { |
|
| 617 | - $return->limbs = array( |
|
| 618 | - (int) ($this->overflow & 0xffff), |
|
| 619 | - (int) ($this->limbs[0]) |
|
| 620 | - ); |
|
| 621 | - $return->overflow = $this->overflow >> 16; |
|
| 622 | - return $return->shiftRight($c & 15); |
|
| 623 | - } |
|
| 624 | - if ($c === 0) { |
|
| 625 | - $return->limbs = $this->limbs; |
|
| 626 | - } elseif ($c < 0) { |
|
| 627 | - /** @var int $c */ |
|
| 628 | - return $this->shiftLeft(-$c); |
|
| 629 | - } else { |
|
| 630 | - if (!is_int($c)) { |
|
| 631 | - throw new TypeError(); |
|
| 632 | - } |
|
| 633 | - /** @var int $c */ |
|
| 634 | - // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); |
|
| 635 | - $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); |
|
| 636 | - $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); |
|
| 637 | - $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); |
|
| 638 | - $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); |
|
| 639 | - $return->overflow >>= $c; |
|
| 640 | - } |
|
| 641 | - return $return; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Subtract a normal integer from an int32 object. |
|
| 646 | - * |
|
| 647 | - * @param int $int |
|
| 648 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 649 | - * @throws SodiumException |
|
| 650 | - * @throws TypeError |
|
| 651 | - */ |
|
| 652 | - public function subInt($int) |
|
| 653 | - { |
|
| 654 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 655 | - /** @var int $int */ |
|
| 656 | - $int = (int) $int; |
|
| 657 | - |
|
| 658 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 659 | - $return->unsignedInt = $this->unsignedInt; |
|
| 660 | - |
|
| 661 | - /** @var int $tmp */ |
|
| 662 | - $tmp = $this->limbs[1] - ($int & 0xffff); |
|
| 663 | - /** @var int $carry */ |
|
| 664 | - $carry = $tmp >> 16; |
|
| 665 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
| 666 | - |
|
| 667 | - /** @var int $tmp */ |
|
| 668 | - $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; |
|
| 669 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 670 | - return $return; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Subtract two int32 objects from each other |
|
| 675 | - * |
|
| 676 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 677 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 678 | - */ |
|
| 679 | - public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 680 | - { |
|
| 681 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 682 | - $return->unsignedInt = $this->unsignedInt; |
|
| 683 | - |
|
| 684 | - /** @var int $tmp */ |
|
| 685 | - $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); |
|
| 686 | - /** @var int $carry */ |
|
| 687 | - $carry = $tmp >> 16; |
|
| 688 | - $return->limbs[1] = (int) ($tmp & 0xffff); |
|
| 689 | - |
|
| 690 | - /** @var int $tmp */ |
|
| 691 | - $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; |
|
| 692 | - $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 693 | - return $return; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * XOR this 32-bit integer with another. |
|
| 698 | - * |
|
| 699 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 700 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 701 | - */ |
|
| 702 | - public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 703 | - { |
|
| 704 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 705 | - $return->unsignedInt = $this->unsignedInt; |
|
| 706 | - $return->limbs = array( |
|
| 707 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
| 708 | - (int) ($this->limbs[1] ^ $b->limbs[1]) |
|
| 709 | - ); |
|
| 710 | - return $return; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * @param int $signed |
|
| 715 | - * @return self |
|
| 716 | - * @throws SodiumException |
|
| 717 | - * @throws TypeError |
|
| 718 | - */ |
|
| 719 | - public static function fromInt($signed) |
|
| 720 | - { |
|
| 721 | - ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
|
| 722 | - /** @var int $signed */ |
|
| 723 | - $signed = (int) $signed; |
|
| 724 | - |
|
| 725 | - return new ParagonIE_Sodium_Core32_Int32( |
|
| 726 | - array( |
|
| 727 | - (int) (($signed >> 16) & 0xffff), |
|
| 728 | - (int) ($signed & 0xffff) |
|
| 729 | - ) |
|
| 730 | - ); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * @param string $string |
|
| 735 | - * @return self |
|
| 736 | - * @throws SodiumException |
|
| 737 | - * @throws TypeError |
|
| 738 | - */ |
|
| 739 | - public static function fromString($string) |
|
| 740 | - { |
|
| 741 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 742 | - $string = (string) $string; |
|
| 743 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
| 744 | - throw new RangeException( |
|
| 745 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 746 | - ); |
|
| 747 | - } |
|
| 748 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 749 | - |
|
| 750 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
| 751 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
| 752 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
| 753 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
| 754 | - return $return; |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * @param string $string |
|
| 759 | - * @return self |
|
| 760 | - * @throws SodiumException |
|
| 761 | - * @throws TypeError |
|
| 762 | - */ |
|
| 763 | - public static function fromReverseString($string) |
|
| 764 | - { |
|
| 765 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 766 | - $string = (string) $string; |
|
| 767 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
| 768 | - throw new RangeException( |
|
| 769 | - 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 770 | - ); |
|
| 771 | - } |
|
| 772 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 773 | - |
|
| 774 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
| 775 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
| 776 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
| 777 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
| 778 | - return $return; |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - /** |
|
| 782 | - * @return array<int, int> |
|
| 783 | - */ |
|
| 784 | - public function toArray() |
|
| 785 | - { |
|
| 786 | - return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - /** |
|
| 790 | - * @return string |
|
| 791 | - * @throws TypeError |
|
| 792 | - */ |
|
| 793 | - public function toString() |
|
| 794 | - { |
|
| 795 | - return |
|
| 796 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
| 797 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 798 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 799 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * @return int |
|
| 804 | - */ |
|
| 805 | - public function toInt() |
|
| 806 | - { |
|
| 807 | - return (int) ( |
|
| 808 | - (($this->limbs[0] & 0xffff) << 16) |
|
| 809 | - | |
|
| 810 | - ($this->limbs[1] & 0xffff) |
|
| 811 | - ); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 816 | - */ |
|
| 817 | - public function toInt32() |
|
| 818 | - { |
|
| 819 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 820 | - $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
|
| 821 | - $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
|
| 822 | - $return->unsignedInt = $this->unsignedInt; |
|
| 823 | - $return->overflow = (int) ($this->overflow & 0x7fffffff); |
|
| 824 | - return $return; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - /** |
|
| 828 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 829 | - */ |
|
| 830 | - public function toInt64() |
|
| 831 | - { |
|
| 832 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 833 | - $return->unsignedInt = $this->unsignedInt; |
|
| 834 | - if ($this->unsignedInt) { |
|
| 835 | - $return->limbs[0] += (($this->overflow >> 16) & 0xffff); |
|
| 836 | - $return->limbs[1] += (($this->overflow) & 0xffff); |
|
| 837 | - } else { |
|
| 838 | - $neg = -(($this->limbs[0] >> 15) & 1); |
|
| 839 | - $return->limbs[0] = (int)($neg & 0xffff); |
|
| 840 | - $return->limbs[1] = (int)($neg & 0xffff); |
|
| 841 | - } |
|
| 842 | - $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); |
|
| 843 | - $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); |
|
| 844 | - return $return; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * @return string |
|
| 849 | - * @throws TypeError |
|
| 850 | - */ |
|
| 851 | - public function toReverseString() |
|
| 852 | - { |
|
| 853 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 854 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 855 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 856 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - /** |
|
| 860 | - * @return string |
|
| 861 | - */ |
|
| 862 | - public function __toString() |
|
| 863 | - { |
|
| 864 | - try { |
|
| 865 | - return $this->toString(); |
|
| 866 | - } catch (TypeError $ex) { |
|
| 867 | - // PHP engine can't handle exceptions from __toString() |
|
| 868 | - return ''; |
|
| 869 | - } |
|
| 870 | - } |
|
| 12 | + /** |
|
| 13 | + * @var array<int, int> - two 16-bit integers |
|
| 14 | + * |
|
| 15 | + * 0 is the higher 16 bits |
|
| 16 | + * 1 is the lower 16 bits |
|
| 17 | + */ |
|
| 18 | + public $limbs = array(0, 0); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var int |
|
| 22 | + */ |
|
| 23 | + public $overflow = 0; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + public $unsignedInt = false; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * ParagonIE_Sodium_Core32_Int32 constructor. |
|
| 32 | + * @param array $array |
|
| 33 | + * @param bool $unsignedInt |
|
| 34 | + */ |
|
| 35 | + public function __construct($array = array(0, 0), $unsignedInt = false) |
|
| 36 | + { |
|
| 37 | + $this->limbs = array( |
|
| 38 | + (int) $array[0], |
|
| 39 | + (int) $array[1] |
|
| 40 | + ); |
|
| 41 | + $this->overflow = 0; |
|
| 42 | + $this->unsignedInt = $unsignedInt; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Adds two int32 objects |
|
| 47 | + * |
|
| 48 | + * @param ParagonIE_Sodium_Core32_Int32 $addend |
|
| 49 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 50 | + */ |
|
| 51 | + public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend) |
|
| 52 | + { |
|
| 53 | + $i0 = $this->limbs[0]; |
|
| 54 | + $i1 = $this->limbs[1]; |
|
| 55 | + $j0 = $addend->limbs[0]; |
|
| 56 | + $j1 = $addend->limbs[1]; |
|
| 57 | + |
|
| 58 | + $r1 = $i1 + ($j1 & 0xffff); |
|
| 59 | + $carry = $r1 >> 16; |
|
| 60 | + |
|
| 61 | + $r0 = $i0 + ($j0 & 0xffff) + $carry; |
|
| 62 | + $carry = $r0 >> 16; |
|
| 63 | + |
|
| 64 | + $r0 &= 0xffff; |
|
| 65 | + $r1 &= 0xffff; |
|
| 66 | + |
|
| 67 | + $return = new ParagonIE_Sodium_Core32_Int32( |
|
| 68 | + array($r0, $r1) |
|
| 69 | + ); |
|
| 70 | + $return->overflow = $carry; |
|
| 71 | + $return->unsignedInt = $this->unsignedInt; |
|
| 72 | + return $return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Adds a normal integer to an int32 object |
|
| 77 | + * |
|
| 78 | + * @param int $int |
|
| 79 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 80 | + * @throws SodiumException |
|
| 81 | + * @throws TypeError |
|
| 82 | + */ |
|
| 83 | + public function addInt($int) |
|
| 84 | + { |
|
| 85 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 86 | + /** @var int $int */ |
|
| 87 | + $int = (int) $int; |
|
| 88 | + |
|
| 89 | + $int = (int) $int; |
|
| 90 | + |
|
| 91 | + $i0 = $this->limbs[0]; |
|
| 92 | + $i1 = $this->limbs[1]; |
|
| 93 | + |
|
| 94 | + $r1 = $i1 + ($int & 0xffff); |
|
| 95 | + $carry = $r1 >> 16; |
|
| 96 | + |
|
| 97 | + $r0 = $i0 + (($int >> 16) & 0xffff) + $carry; |
|
| 98 | + $carry = $r0 >> 16; |
|
| 99 | + $r0 &= 0xffff; |
|
| 100 | + $r1 &= 0xffff; |
|
| 101 | + $return = new ParagonIE_Sodium_Core32_Int32( |
|
| 102 | + array($r0, $r1) |
|
| 103 | + ); |
|
| 104 | + $return->overflow = $carry; |
|
| 105 | + $return->unsignedInt = $this->unsignedInt; |
|
| 106 | + return $return; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param int $b |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function compareInt($b = 0) |
|
| 114 | + { |
|
| 115 | + $gt = 0; |
|
| 116 | + $eq = 1; |
|
| 117 | + |
|
| 118 | + $i = 2; |
|
| 119 | + $j = 0; |
|
| 120 | + while ($i > 0) { |
|
| 121 | + --$i; |
|
| 122 | + /** @var int $x1 */ |
|
| 123 | + $x1 = $this->limbs[$i]; |
|
| 124 | + /** @var int $x2 */ |
|
| 125 | + $x2 = ($b >> ($j << 4)) & 0xffff; |
|
| 126 | + /** @var int $gt */ |
|
| 127 | + $gt |= (($x2 - $x1) >> 8) & $eq; |
|
| 128 | + /** @var int $eq */ |
|
| 129 | + $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
| 130 | + } |
|
| 131 | + return ($gt + $gt - $eq) + 1; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param int $m |
|
| 136 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 137 | + */ |
|
| 138 | + public function mask($m = 0) |
|
| 139 | + { |
|
| 140 | + /** @var int $hi */ |
|
| 141 | + $hi = ($m >> 16) & 0xffff; |
|
| 142 | + /** @var int $lo */ |
|
| 143 | + $lo = ($m & 0xffff); |
|
| 144 | + return new ParagonIE_Sodium_Core32_Int32( |
|
| 145 | + array( |
|
| 146 | + (int) ($this->limbs[0] & $hi), |
|
| 147 | + (int) ($this->limbs[1] & $lo) |
|
| 148 | + ), |
|
| 149 | + $this->unsignedInt |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param array<int, int> $a |
|
| 155 | + * @param array<int, int> $b |
|
| 156 | + * @param int $baseLog2 |
|
| 157 | + * @return array<int, int> |
|
| 158 | + */ |
|
| 159 | + public function multiplyLong(array $a, array $b, $baseLog2 = 16) |
|
| 160 | + { |
|
| 161 | + $a_l = count($a); |
|
| 162 | + $b_l = count($b); |
|
| 163 | + /** @var array<int, int> $r */ |
|
| 164 | + $r = array_fill(0, $a_l + $b_l + 1, 0); |
|
| 165 | + $base = 1 << $baseLog2; |
|
| 166 | + for ($i = 0; $i < $a_l; ++$i) { |
|
| 167 | + $a_i = $a[$i]; |
|
| 168 | + for ($j = 0; $j < $a_l; ++$j) { |
|
| 169 | + $b_j = $b[$j]; |
|
| 170 | + $product = ($a_i * $b_j) + $r[$i + $j]; |
|
| 171 | + $carry = ($product >> $baseLog2 & 0xffff); |
|
| 172 | + $r[$i + $j] = ($product - (int) ($carry * $base)) & 0xffff; |
|
| 173 | + $r[$i + $j + 1] += $carry; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + return array_slice($r, 0, 5); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param int $int |
|
| 181 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 182 | + */ |
|
| 183 | + public function mulIntFast($int) |
|
| 184 | + { |
|
| 185 | + // Handle negative numbers |
|
| 186 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
| 187 | + $bNeg = ($int >> 31) & 1; |
|
| 188 | + $a = array_reverse($this->limbs); |
|
| 189 | + $b = array( |
|
| 190 | + $int & 0xffff, |
|
| 191 | + ($int >> 16) & 0xffff |
|
| 192 | + ); |
|
| 193 | + if ($aNeg) { |
|
| 194 | + for ($i = 0; $i < 2; ++$i) { |
|
| 195 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
| 196 | + } |
|
| 197 | + ++$a[0]; |
|
| 198 | + } |
|
| 199 | + if ($bNeg) { |
|
| 200 | + for ($i = 0; $i < 2; ++$i) { |
|
| 201 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
| 202 | + } |
|
| 203 | + ++$b[0]; |
|
| 204 | + } |
|
| 205 | + // Multiply |
|
| 206 | + $res = $this->multiplyLong($a, $b); |
|
| 207 | + |
|
| 208 | + // Re-apply negation to results |
|
| 209 | + if ($aNeg !== $bNeg) { |
|
| 210 | + for ($i = 0; $i < 2; ++$i) { |
|
| 211 | + $res[$i] = (0xffff ^ $res[$i]) & 0xffff; |
|
| 212 | + } |
|
| 213 | + // Handle integer overflow |
|
| 214 | + $c = 1; |
|
| 215 | + for ($i = 0; $i < 2; ++$i) { |
|
| 216 | + $res[$i] += $c; |
|
| 217 | + $c = $res[$i] >> 16; |
|
| 218 | + $res[$i] &= 0xffff; |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // Return our values |
|
| 223 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 224 | + $return->limbs = array( |
|
| 225 | + $res[1] & 0xffff, |
|
| 226 | + $res[0] & 0xffff |
|
| 227 | + ); |
|
| 228 | + if (count($res) > 2) { |
|
| 229 | + $return->overflow = $res[2] & 0xffff; |
|
| 230 | + } |
|
| 231 | + $return->unsignedInt = $this->unsignedInt; |
|
| 232 | + return $return; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param ParagonIE_Sodium_Core32_Int32 $right |
|
| 237 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 238 | + */ |
|
| 239 | + public function mulInt32Fast(ParagonIE_Sodium_Core32_Int32 $right) |
|
| 240 | + { |
|
| 241 | + $aNeg = ($this->limbs[0] >> 15) & 1; |
|
| 242 | + $bNeg = ($right->limbs[0] >> 15) & 1; |
|
| 243 | + |
|
| 244 | + $a = array_reverse($this->limbs); |
|
| 245 | + $b = array_reverse($right->limbs); |
|
| 246 | + if ($aNeg) { |
|
| 247 | + for ($i = 0; $i < 2; ++$i) { |
|
| 248 | + $a[$i] = ($a[$i] ^ 0xffff) & 0xffff; |
|
| 249 | + } |
|
| 250 | + ++$a[0]; |
|
| 251 | + } |
|
| 252 | + if ($bNeg) { |
|
| 253 | + for ($i = 0; $i < 2; ++$i) { |
|
| 254 | + $b[$i] = ($b[$i] ^ 0xffff) & 0xffff; |
|
| 255 | + } |
|
| 256 | + ++$b[0]; |
|
| 257 | + } |
|
| 258 | + $res = $this->multiplyLong($a, $b); |
|
| 259 | + if ($aNeg !== $bNeg) { |
|
| 260 | + if ($aNeg !== $bNeg) { |
|
| 261 | + for ($i = 0; $i < 2; ++$i) { |
|
| 262 | + $res[$i] = ($res[$i] ^ 0xffff) & 0xffff; |
|
| 263 | + } |
|
| 264 | + $c = 1; |
|
| 265 | + for ($i = 0; $i < 2; ++$i) { |
|
| 266 | + $res[$i] += $c; |
|
| 267 | + $c = $res[$i] >> 16; |
|
| 268 | + $res[$i] &= 0xffff; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 273 | + $return->limbs = array( |
|
| 274 | + $res[1] & 0xffff, |
|
| 275 | + $res[0] & 0xffff |
|
| 276 | + ); |
|
| 277 | + if (count($res) > 2) { |
|
| 278 | + $return->overflow = $res[2]; |
|
| 279 | + } |
|
| 280 | + return $return; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @param int $int |
|
| 285 | + * @param int $size |
|
| 286 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 287 | + * @throws SodiumException |
|
| 288 | + * @throws TypeError |
|
| 289 | + */ |
|
| 290 | + public function mulInt($int = 0, $size = 0) |
|
| 291 | + { |
|
| 292 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 293 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 294 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
| 295 | + return $this->mulIntFast((int) $int); |
|
| 296 | + } |
|
| 297 | + /** @var int $int */ |
|
| 298 | + $int = (int) $int; |
|
| 299 | + /** @var int $size */ |
|
| 300 | + $size = (int) $size; |
|
| 301 | + |
|
| 302 | + if (!$size) { |
|
| 303 | + $size = 31; |
|
| 304 | + } |
|
| 305 | + /** @var int $size */ |
|
| 306 | + |
|
| 307 | + $a = clone $this; |
|
| 308 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 309 | + $return->unsignedInt = $this->unsignedInt; |
|
| 310 | + |
|
| 311 | + // Initialize: |
|
| 312 | + $ret0 = 0; |
|
| 313 | + $ret1 = 0; |
|
| 314 | + $a0 = $a->limbs[0]; |
|
| 315 | + $a1 = $a->limbs[1]; |
|
| 316 | + |
|
| 317 | + /** @var int $size */ |
|
| 318 | + /** @var int $i */ |
|
| 319 | + for ($i = $size; $i >= 0; --$i) { |
|
| 320 | + $m = (int) (-($int & 1)); |
|
| 321 | + $x0 = $a0 & $m; |
|
| 322 | + $x1 = $a1 & $m; |
|
| 323 | + |
|
| 324 | + $ret1 += $x1; |
|
| 325 | + $c = $ret1 >> 16; |
|
| 326 | + |
|
| 327 | + $ret0 += $x0 + $c; |
|
| 328 | + |
|
| 329 | + $ret0 &= 0xffff; |
|
| 330 | + $ret1 &= 0xffff; |
|
| 331 | + |
|
| 332 | + $a1 = ($a1 << 1); |
|
| 333 | + $x1 = $a1 >> 16; |
|
| 334 | + $a0 = ($a0 << 1) | $x1; |
|
| 335 | + $a0 &= 0xffff; |
|
| 336 | + $a1 &= 0xffff; |
|
| 337 | + $int >>= 1; |
|
| 338 | + } |
|
| 339 | + $return->limbs[0] = $ret0; |
|
| 340 | + $return->limbs[1] = $ret1; |
|
| 341 | + return $return; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * @param ParagonIE_Sodium_Core32_Int32 $int |
|
| 346 | + * @param int $size |
|
| 347 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 348 | + * @throws SodiumException |
|
| 349 | + * @throws TypeError |
|
| 350 | + */ |
|
| 351 | + public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0) |
|
| 352 | + { |
|
| 353 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 354 | + if (ParagonIE_Sodium_Compat::$fastMult) { |
|
| 355 | + return $this->mulInt32Fast($int); |
|
| 356 | + } |
|
| 357 | + if (!$size) { |
|
| 358 | + $size = 31; |
|
| 359 | + } |
|
| 360 | + /** @var int $size */ |
|
| 361 | + |
|
| 362 | + $a = clone $this; |
|
| 363 | + $b = clone $int; |
|
| 364 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 365 | + $return->unsignedInt = $this->unsignedInt; |
|
| 366 | + |
|
| 367 | + // Initialize: |
|
| 368 | + $ret0 = 0; |
|
| 369 | + $ret1 = 0; |
|
| 370 | + $a0 = $a->limbs[0]; |
|
| 371 | + $a1 = $a->limbs[1]; |
|
| 372 | + $b0 = $b->limbs[0]; |
|
| 373 | + $b1 = $b->limbs[1]; |
|
| 374 | + |
|
| 375 | + /** @var int $size */ |
|
| 376 | + /** @var int $i */ |
|
| 377 | + for ($i = $size; $i >= 0; --$i) { |
|
| 378 | + $m = (int) (-($b1 & 1)); |
|
| 379 | + $x0 = $a0 & $m; |
|
| 380 | + $x1 = $a1 & $m; |
|
| 381 | + |
|
| 382 | + $ret1 += $x1; |
|
| 383 | + $c = $ret1 >> 16; |
|
| 384 | + |
|
| 385 | + $ret0 += $x0 + $c; |
|
| 386 | + |
|
| 387 | + $ret0 &= 0xffff; |
|
| 388 | + $ret1 &= 0xffff; |
|
| 389 | + |
|
| 390 | + $a1 = ($a1 << 1); |
|
| 391 | + $x1 = $a1 >> 16; |
|
| 392 | + $a0 = ($a0 << 1) | $x1; |
|
| 393 | + $a0 &= 0xffff; |
|
| 394 | + $a1 &= 0xffff; |
|
| 395 | + |
|
| 396 | + $x0 = ($b0 & 1) << 16; |
|
| 397 | + $b0 = ($b0 >> 1); |
|
| 398 | + $b1 = (($b1 | $x0) >> 1); |
|
| 399 | + |
|
| 400 | + $b0 &= 0xffff; |
|
| 401 | + $b1 &= 0xffff; |
|
| 402 | + |
|
| 403 | + } |
|
| 404 | + $return->limbs[0] = $ret0; |
|
| 405 | + $return->limbs[1] = $ret1; |
|
| 406 | + |
|
| 407 | + return $return; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * OR this 32-bit integer with another. |
|
| 412 | + * |
|
| 413 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 414 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 415 | + */ |
|
| 416 | + public function orInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 417 | + { |
|
| 418 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 419 | + $return->unsignedInt = $this->unsignedInt; |
|
| 420 | + $return->limbs = array( |
|
| 421 | + (int) ($this->limbs[0] | $b->limbs[0]), |
|
| 422 | + (int) ($this->limbs[1] | $b->limbs[1]) |
|
| 423 | + ); |
|
| 424 | + /** @var int overflow */ |
|
| 425 | + $return->overflow = $this->overflow | $b->overflow; |
|
| 426 | + return $return; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * @param int $b |
|
| 431 | + * @return bool |
|
| 432 | + */ |
|
| 433 | + public function isGreaterThan($b = 0) |
|
| 434 | + { |
|
| 435 | + return $this->compareInt($b) > 0; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * @param int $b |
|
| 440 | + * @return bool |
|
| 441 | + */ |
|
| 442 | + public function isLessThanInt($b = 0) |
|
| 443 | + { |
|
| 444 | + return $this->compareInt($b) < 0; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * @param int $c |
|
| 449 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 450 | + * @throws SodiumException |
|
| 451 | + * @throws TypeError |
|
| 452 | + * @psalm-suppress MixedArrayAccess |
|
| 453 | + */ |
|
| 454 | + public function rotateLeft($c = 0) |
|
| 455 | + { |
|
| 456 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 457 | + /** @var int $c */ |
|
| 458 | + $c = (int) $c; |
|
| 459 | + |
|
| 460 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 461 | + $return->unsignedInt = $this->unsignedInt; |
|
| 462 | + $c &= 31; |
|
| 463 | + if ($c === 0) { |
|
| 464 | + // NOP, but we want a copy. |
|
| 465 | + $return->limbs = $this->limbs; |
|
| 466 | + } else { |
|
| 467 | + /** @var int $c */ |
|
| 468 | + |
|
| 469 | + /** @var int $idx_shift */ |
|
| 470 | + $idx_shift = ($c >> 4) & 1; |
|
| 471 | + |
|
| 472 | + /** @var int $sub_shift */ |
|
| 473 | + $sub_shift = $c & 15; |
|
| 474 | + |
|
| 475 | + /** @var array<int, int> $limbs */ |
|
| 476 | + $limbs =& $return->limbs; |
|
| 477 | + |
|
| 478 | + /** @var array<int, int> $myLimbs */ |
|
| 479 | + $myLimbs =& $this->limbs; |
|
| 480 | + |
|
| 481 | + for ($i = 1; $i >= 0; --$i) { |
|
| 482 | + /** @var int $j */ |
|
| 483 | + $j = ($i + $idx_shift) & 1; |
|
| 484 | + /** @var int $k */ |
|
| 485 | + $k = ($i + $idx_shift + 1) & 1; |
|
| 486 | + $limbs[$i] = (int) ( |
|
| 487 | + ( |
|
| 488 | + ((int) ($myLimbs[$j]) << $sub_shift) |
|
| 489 | + | |
|
| 490 | + ((int) ($myLimbs[$k]) >> (16 - $sub_shift)) |
|
| 491 | + ) & 0xffff |
|
| 492 | + ); |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + return $return; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Rotate to the right |
|
| 500 | + * |
|
| 501 | + * @param int $c |
|
| 502 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 503 | + * @throws SodiumException |
|
| 504 | + * @throws TypeError |
|
| 505 | + * @psalm-suppress MixedArrayAccess |
|
| 506 | + */ |
|
| 507 | + public function rotateRight($c = 0) |
|
| 508 | + { |
|
| 509 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 510 | + /** @var int $c */ |
|
| 511 | + $c = (int) $c; |
|
| 512 | + |
|
| 513 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 514 | + $return->unsignedInt = $this->unsignedInt; |
|
| 515 | + $c &= 31; |
|
| 516 | + /** @var int $c */ |
|
| 517 | + if ($c === 0) { |
|
| 518 | + // NOP, but we want a copy. |
|
| 519 | + $return->limbs = $this->limbs; |
|
| 520 | + } else { |
|
| 521 | + /** @var int $c */ |
|
| 522 | + |
|
| 523 | + /** @var int $idx_shift */ |
|
| 524 | + $idx_shift = ($c >> 4) & 1; |
|
| 525 | + |
|
| 526 | + /** @var int $sub_shift */ |
|
| 527 | + $sub_shift = $c & 15; |
|
| 528 | + |
|
| 529 | + /** @var array<int, int> $limbs */ |
|
| 530 | + $limbs =& $return->limbs; |
|
| 531 | + |
|
| 532 | + /** @var array<int, int> $myLimbs */ |
|
| 533 | + $myLimbs =& $this->limbs; |
|
| 534 | + |
|
| 535 | + for ($i = 1; $i >= 0; --$i) { |
|
| 536 | + /** @var int $j */ |
|
| 537 | + $j = ($i - $idx_shift) & 1; |
|
| 538 | + /** @var int $k */ |
|
| 539 | + $k = ($i - $idx_shift - 1) & 1; |
|
| 540 | + $limbs[$i] = (int) ( |
|
| 541 | + ( |
|
| 542 | + ((int) ($myLimbs[$j]) >> (int) ($sub_shift)) |
|
| 543 | + | |
|
| 544 | + ((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift))) |
|
| 545 | + ) & 0xffff |
|
| 546 | + ); |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + return $return; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * @param bool $bool |
|
| 554 | + * @return self |
|
| 555 | + */ |
|
| 556 | + public function setUnsignedInt($bool = false) |
|
| 557 | + { |
|
| 558 | + $this->unsignedInt = !empty($bool); |
|
| 559 | + return $this; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * @param int $c |
|
| 564 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 565 | + * @throws SodiumException |
|
| 566 | + * @throws TypeError |
|
| 567 | + */ |
|
| 568 | + public function shiftLeft($c = 0) |
|
| 569 | + { |
|
| 570 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 571 | + /** @var int $c */ |
|
| 572 | + $c = (int) $c; |
|
| 573 | + |
|
| 574 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 575 | + $return->unsignedInt = $this->unsignedInt; |
|
| 576 | + $c &= 63; |
|
| 577 | + /** @var int $c */ |
|
| 578 | + if ($c === 0) { |
|
| 579 | + $return->limbs = $this->limbs; |
|
| 580 | + } elseif ($c < 0) { |
|
| 581 | + /** @var int $c */ |
|
| 582 | + return $this->shiftRight(-$c); |
|
| 583 | + } else { |
|
| 584 | + /** @var int $c */ |
|
| 585 | + /** @var int $tmp */ |
|
| 586 | + $tmp = $this->limbs[1] << $c; |
|
| 587 | + $return->limbs[1] = (int)($tmp & 0xffff); |
|
| 588 | + /** @var int $carry */ |
|
| 589 | + $carry = $tmp >> 16; |
|
| 590 | + |
|
| 591 | + /** @var int $tmp */ |
|
| 592 | + $tmp = ($this->limbs[0] << $c) | ($carry & 0xffff); |
|
| 593 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 594 | + } |
|
| 595 | + return $return; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * @param int $c |
|
| 600 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 601 | + * @throws SodiumException |
|
| 602 | + * @throws TypeError |
|
| 603 | + * @psalm-suppress MixedAssignment |
|
| 604 | + * @psalm-suppress MixedOperand |
|
| 605 | + */ |
|
| 606 | + public function shiftRight($c = 0) |
|
| 607 | + { |
|
| 608 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 609 | + /** @var int $c */ |
|
| 610 | + $c = (int) $c; |
|
| 611 | + |
|
| 612 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 613 | + $return->unsignedInt = $this->unsignedInt; |
|
| 614 | + $c &= 63; |
|
| 615 | + /** @var int $c */ |
|
| 616 | + if ($c >= 16) { |
|
| 617 | + $return->limbs = array( |
|
| 618 | + (int) ($this->overflow & 0xffff), |
|
| 619 | + (int) ($this->limbs[0]) |
|
| 620 | + ); |
|
| 621 | + $return->overflow = $this->overflow >> 16; |
|
| 622 | + return $return->shiftRight($c & 15); |
|
| 623 | + } |
|
| 624 | + if ($c === 0) { |
|
| 625 | + $return->limbs = $this->limbs; |
|
| 626 | + } elseif ($c < 0) { |
|
| 627 | + /** @var int $c */ |
|
| 628 | + return $this->shiftLeft(-$c); |
|
| 629 | + } else { |
|
| 630 | + if (!is_int($c)) { |
|
| 631 | + throw new TypeError(); |
|
| 632 | + } |
|
| 633 | + /** @var int $c */ |
|
| 634 | + // $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff); |
|
| 635 | + $carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1)); |
|
| 636 | + $return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff); |
|
| 637 | + $carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1)); |
|
| 638 | + $return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff); |
|
| 639 | + $return->overflow >>= $c; |
|
| 640 | + } |
|
| 641 | + return $return; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Subtract a normal integer from an int32 object. |
|
| 646 | + * |
|
| 647 | + * @param int $int |
|
| 648 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 649 | + * @throws SodiumException |
|
| 650 | + * @throws TypeError |
|
| 651 | + */ |
|
| 652 | + public function subInt($int) |
|
| 653 | + { |
|
| 654 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 655 | + /** @var int $int */ |
|
| 656 | + $int = (int) $int; |
|
| 657 | + |
|
| 658 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 659 | + $return->unsignedInt = $this->unsignedInt; |
|
| 660 | + |
|
| 661 | + /** @var int $tmp */ |
|
| 662 | + $tmp = $this->limbs[1] - ($int & 0xffff); |
|
| 663 | + /** @var int $carry */ |
|
| 664 | + $carry = $tmp >> 16; |
|
| 665 | + $return->limbs[1] = (int) ($tmp & 0xffff); |
|
| 666 | + |
|
| 667 | + /** @var int $tmp */ |
|
| 668 | + $tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry; |
|
| 669 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 670 | + return $return; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Subtract two int32 objects from each other |
|
| 675 | + * |
|
| 676 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 677 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 678 | + */ |
|
| 679 | + public function subInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 680 | + { |
|
| 681 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 682 | + $return->unsignedInt = $this->unsignedInt; |
|
| 683 | + |
|
| 684 | + /** @var int $tmp */ |
|
| 685 | + $tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff); |
|
| 686 | + /** @var int $carry */ |
|
| 687 | + $carry = $tmp >> 16; |
|
| 688 | + $return->limbs[1] = (int) ($tmp & 0xffff); |
|
| 689 | + |
|
| 690 | + /** @var int $tmp */ |
|
| 691 | + $tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry; |
|
| 692 | + $return->limbs[0] = (int) ($tmp & 0xffff); |
|
| 693 | + return $return; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * XOR this 32-bit integer with another. |
|
| 698 | + * |
|
| 699 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
| 700 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 701 | + */ |
|
| 702 | + public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b) |
|
| 703 | + { |
|
| 704 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 705 | + $return->unsignedInt = $this->unsignedInt; |
|
| 706 | + $return->limbs = array( |
|
| 707 | + (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
| 708 | + (int) ($this->limbs[1] ^ $b->limbs[1]) |
|
| 709 | + ); |
|
| 710 | + return $return; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * @param int $signed |
|
| 715 | + * @return self |
|
| 716 | + * @throws SodiumException |
|
| 717 | + * @throws TypeError |
|
| 718 | + */ |
|
| 719 | + public static function fromInt($signed) |
|
| 720 | + { |
|
| 721 | + ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);; |
|
| 722 | + /** @var int $signed */ |
|
| 723 | + $signed = (int) $signed; |
|
| 724 | + |
|
| 725 | + return new ParagonIE_Sodium_Core32_Int32( |
|
| 726 | + array( |
|
| 727 | + (int) (($signed >> 16) & 0xffff), |
|
| 728 | + (int) ($signed & 0xffff) |
|
| 729 | + ) |
|
| 730 | + ); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * @param string $string |
|
| 735 | + * @return self |
|
| 736 | + * @throws SodiumException |
|
| 737 | + * @throws TypeError |
|
| 738 | + */ |
|
| 739 | + public static function fromString($string) |
|
| 740 | + { |
|
| 741 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 742 | + $string = (string) $string; |
|
| 743 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
| 744 | + throw new RangeException( |
|
| 745 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 746 | + ); |
|
| 747 | + } |
|
| 748 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 749 | + |
|
| 750 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
| 751 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
| 752 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
| 753 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
| 754 | + return $return; |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * @param string $string |
|
| 759 | + * @return self |
|
| 760 | + * @throws SodiumException |
|
| 761 | + * @throws TypeError |
|
| 762 | + */ |
|
| 763 | + public static function fromReverseString($string) |
|
| 764 | + { |
|
| 765 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 766 | + $string = (string) $string; |
|
| 767 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) { |
|
| 768 | + throw new RangeException( |
|
| 769 | + 'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 770 | + ); |
|
| 771 | + } |
|
| 772 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 773 | + |
|
| 774 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
| 775 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
| 776 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
| 777 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
| 778 | + return $return; |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + /** |
|
| 782 | + * @return array<int, int> |
|
| 783 | + */ |
|
| 784 | + public function toArray() |
|
| 785 | + { |
|
| 786 | + return array((int) ($this->limbs[0] << 16 | $this->limbs[1])); |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + /** |
|
| 790 | + * @return string |
|
| 791 | + * @throws TypeError |
|
| 792 | + */ |
|
| 793 | + public function toString() |
|
| 794 | + { |
|
| 795 | + return |
|
| 796 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
| 797 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 798 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 799 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * @return int |
|
| 804 | + */ |
|
| 805 | + public function toInt() |
|
| 806 | + { |
|
| 807 | + return (int) ( |
|
| 808 | + (($this->limbs[0] & 0xffff) << 16) |
|
| 809 | + | |
|
| 810 | + ($this->limbs[1] & 0xffff) |
|
| 811 | + ); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 816 | + */ |
|
| 817 | + public function toInt32() |
|
| 818 | + { |
|
| 819 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 820 | + $return->limbs[0] = (int) ($this->limbs[0] & 0xffff); |
|
| 821 | + $return->limbs[1] = (int) ($this->limbs[1] & 0xffff); |
|
| 822 | + $return->unsignedInt = $this->unsignedInt; |
|
| 823 | + $return->overflow = (int) ($this->overflow & 0x7fffffff); |
|
| 824 | + return $return; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + /** |
|
| 828 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 829 | + */ |
|
| 830 | + public function toInt64() |
|
| 831 | + { |
|
| 832 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 833 | + $return->unsignedInt = $this->unsignedInt; |
|
| 834 | + if ($this->unsignedInt) { |
|
| 835 | + $return->limbs[0] += (($this->overflow >> 16) & 0xffff); |
|
| 836 | + $return->limbs[1] += (($this->overflow) & 0xffff); |
|
| 837 | + } else { |
|
| 838 | + $neg = -(($this->limbs[0] >> 15) & 1); |
|
| 839 | + $return->limbs[0] = (int)($neg & 0xffff); |
|
| 840 | + $return->limbs[1] = (int)($neg & 0xffff); |
|
| 841 | + } |
|
| 842 | + $return->limbs[2] = (int) ($this->limbs[0] & 0xffff); |
|
| 843 | + $return->limbs[3] = (int) ($this->limbs[1] & 0xffff); |
|
| 844 | + return $return; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * @return string |
|
| 849 | + * @throws TypeError |
|
| 850 | + */ |
|
| 851 | + public function toReverseString() |
|
| 852 | + { |
|
| 853 | + return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 854 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 855 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 856 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + /** |
|
| 860 | + * @return string |
|
| 861 | + */ |
|
| 862 | + public function __toString() |
|
| 863 | + { |
|
| 864 | + try { |
|
| 865 | + return $this->toString(); |
|
| 866 | + } catch (TypeError $ex) { |
|
| 867 | + // PHP engine can't handle exceptions from __toString() |
|
| 868 | + return ''; |
|
| 869 | + } |
|
| 870 | + } |
|
| 871 | 871 | } |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -9,55 +9,55 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util |
| 11 | 11 | { |
| 12 | - const BLOCK_SIZE = 16; |
|
| 12 | + const BLOCK_SIZE = 16; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @internal You should not use this directly from another application |
|
| 16 | - * |
|
| 17 | - * @param string $m |
|
| 18 | - * @param string $key |
|
| 19 | - * @return string |
|
| 20 | - * @throws SodiumException |
|
| 21 | - * @throws TypeError |
|
| 22 | - */ |
|
| 23 | - public static function onetimeauth($m, $key) |
|
| 24 | - { |
|
| 25 | - if (self::strlen($key) < 32) { |
|
| 26 | - throw new InvalidArgumentException( |
|
| 27 | - 'Key must be 32 bytes long.' |
|
| 28 | - ); |
|
| 29 | - } |
|
| 30 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
| 31 | - self::substr($key, 0, 32) |
|
| 32 | - ); |
|
| 33 | - return $state |
|
| 34 | - ->update($m) |
|
| 35 | - ->finish(); |
|
| 36 | - } |
|
| 14 | + /** |
|
| 15 | + * @internal You should not use this directly from another application |
|
| 16 | + * |
|
| 17 | + * @param string $m |
|
| 18 | + * @param string $key |
|
| 19 | + * @return string |
|
| 20 | + * @throws SodiumException |
|
| 21 | + * @throws TypeError |
|
| 22 | + */ |
|
| 23 | + public static function onetimeauth($m, $key) |
|
| 24 | + { |
|
| 25 | + if (self::strlen($key) < 32) { |
|
| 26 | + throw new InvalidArgumentException( |
|
| 27 | + 'Key must be 32 bytes long.' |
|
| 28 | + ); |
|
| 29 | + } |
|
| 30 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
| 31 | + self::substr($key, 0, 32) |
|
| 32 | + ); |
|
| 33 | + return $state |
|
| 34 | + ->update($m) |
|
| 35 | + ->finish(); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @internal You should not use this directly from another application |
|
| 40 | - * |
|
| 41 | - * @param string $mac |
|
| 42 | - * @param string $m |
|
| 43 | - * @param string $key |
|
| 44 | - * @return bool |
|
| 45 | - * @throws SodiumException |
|
| 46 | - * @throws TypeError |
|
| 47 | - */ |
|
| 48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
| 49 | - { |
|
| 50 | - if (self::strlen($key) < 32) { |
|
| 51 | - throw new InvalidArgumentException( |
|
| 52 | - 'Key must be 32 bytes long.' |
|
| 53 | - ); |
|
| 54 | - } |
|
| 55 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
| 56 | - self::substr($key, 0, 32) |
|
| 57 | - ); |
|
| 58 | - $calc = $state |
|
| 59 | - ->update($m) |
|
| 60 | - ->finish(); |
|
| 61 | - return self::verify_16($calc, $mac); |
|
| 62 | - } |
|
| 38 | + /** |
|
| 39 | + * @internal You should not use this directly from another application |
|
| 40 | + * |
|
| 41 | + * @param string $mac |
|
| 42 | + * @param string $m |
|
| 43 | + * @param string $key |
|
| 44 | + * @return bool |
|
| 45 | + * @throws SodiumException |
|
| 46 | + * @throws TypeError |
|
| 47 | + */ |
|
| 48 | + public static function onetimeauth_verify($mac, $m, $key) |
|
| 49 | + { |
|
| 50 | + if (self::strlen($key) < 32) { |
|
| 51 | + throw new InvalidArgumentException( |
|
| 52 | + 'Key must be 32 bytes long.' |
|
| 53 | + ); |
|
| 54 | + } |
|
| 55 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
| 56 | + self::substr($key, 0, 32) |
|
| 57 | + ); |
|
| 58 | + $calc = $state |
|
| 59 | + ->update($m) |
|
| 60 | + ->finish(); |
|
| 61 | + return self::verify_16($calc, $mac); |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -1,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 | } |