| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
if (class_exists('ParagonIE_Sodium_Core_Util', false)) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
abstract class ParagonIE_Sodium_Core_Util |
| 12 |
{ |
| 13 |
const U32_MAX = 0xFFFFFFFF; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public static function abs( |
| 21 |
#[SensitiveParameter] |
| 22 |
int $integer, |
| 23 |
int $size = 0 |
| 24 |
): int { |
| 25 |
$realSize = (PHP_INT_SIZE << 3) - 1; |
| 26 |
if ($size) { |
| 27 |
--$size; |
| 28 |
} else { |
| 29 |
$size = $realSize; |
| 30 |
} |
| 31 |
|
| 32 |
$negative = -(($integer >> $size) & 1); |
| 33 |
return ( |
| 34 |
($integer ^ $negative) |
| 35 |
+ |
| 36 |
(($negative >> $realSize) & 1) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static function andStrings( |
| 47 |
#[SensitiveParameter] |
| 48 |
string $a, |
| 49 |
#[SensitiveParameter] |
| 50 |
string $b |
| 51 |
): string { |
| 52 |
$len = self::strlen($a); |
| 53 |
if (self::strlen($b) !== $len) { |
| 54 |
throw new SodiumException('Both strings must be of equal length to combine with bitwise AND'); |
| 55 |
} |
| 56 |
return $a & $b; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static function bin2hex( |
| 70 |
#[SensitiveParameter] |
| 71 |
string $binaryString |
| 72 |
): string { |
| 73 |
$hex = ''; |
| 74 |
$len = self::strlen($binaryString); |
| 75 |
for ($i = 0; $i < $len; ++$i) { |
| 76 |
|
| 77 |
$chunk = unpack('C', $binaryString[$i]); |
| 78 |
|
| 79 |
$c = $chunk[1] & 0xf; |
| 80 |
|
| 81 |
$b = $chunk[1] >> 4; |
| 82 |
$hex .= pack( |
| 83 |
'CC', |
| 84 |
(87 + $b + ((($b - 10) >> 8) & ~38)), |
| 85 |
(87 + $c + ((($c - 10) >> 8) & ~38)) |
| 86 |
); |
| 87 |
} |
| 88 |
return $hex; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static function chrToInt( |
| 102 |
#[SensitiveParameter] |
| 103 |
string $chr |
| 104 |
): int { |
| 105 |
if (self::strlen($chr) !== 1) { |
| 106 |
throw new SodiumException('chrToInt() expects a string that is exactly 1 character long'); |
| 107 |
} |
| 108 |
|
| 109 |
$chunk = unpack('C', $chr); |
| 110 |
return $chunk[1]; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static function compare( |
| 126 |
#[SensitiveParameter] |
| 127 |
string $left, |
| 128 |
#[SensitiveParameter] |
| 129 |
string $right, |
| 130 |
?int $len = null |
| 131 |
): int { |
| 132 |
$leftLen = self::strlen($left); |
| 133 |
$rightLen = self::strlen($right); |
| 134 |
if ($len === null) { |
| 135 |
$len = max($leftLen, $rightLen); |
| 136 |
$left = str_pad($left, $len, "\x00"); |
| 137 |
$right = str_pad($right, $len, "\x00"); |
| 138 |
} elseif ($leftLen !== $rightLen) { |
| 139 |
throw new SodiumException("Argument #1 and argument #2 must have the same length"); |
| 140 |
} |
| 141 |
|
| 142 |
$gt = 0; |
| 143 |
$eq = 1; |
| 144 |
$i = $len; |
| 145 |
while ($i !== 0) { |
| 146 |
--$i; |
| 147 |
$gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq; |
| 148 |
$eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8; |
| 149 |
} |
| 150 |
return ($gt + $gt + $eq) - 1; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public static function hashEquals( |
| 163 |
#[SensitiveParameter] |
| 164 |
string $left, |
| 165 |
#[SensitiveParameter] |
| 166 |
string $right |
| 167 |
): bool { |
| 168 |
return hash_equals($left, $right); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static function hex2bin( |
| 185 |
#[SensitiveParameter] |
| 186 |
string $hexString, |
| 187 |
string $ignore = '', |
| 188 |
bool $strictPadding = false |
| 189 |
): string { |
| 190 |
$hex_pos = 0; |
| 191 |
$bin = ''; |
| 192 |
$c_acc = 0; |
| 193 |
$hex_len = self::strlen($hexString); |
| 194 |
$state = 0; |
| 195 |
$chunk = unpack('C*', $hexString); |
| 196 |
while ($hex_pos < $hex_len) { |
| 197 |
++$hex_pos; |
| 198 |
|
| 199 |
$c = $chunk[$hex_pos]; |
| 200 |
$c_num = $c ^ 48; |
| 201 |
$c_num0 = ($c_num - 10) >> 8; |
| 202 |
$c_alpha = ($c & ~32) - 55; |
| 203 |
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8; |
| 204 |
if (($c_num0 | $c_alpha0) === 0) { |
| 205 |
if ($ignore && $state === 0 && str_contains($ignore, self::intToChr($c))) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
throw new SodiumException( |
| 209 |
'hex2bin() only expects hexadecimal characters' |
| 210 |
); |
| 211 |
} |
| 212 |
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0); |
| 213 |
if ($state === 0) { |
| 214 |
$c_acc = $c_val * 16; |
| 215 |
} else { |
| 216 |
$bin .= pack('C', $c_acc | $c_val); |
| 217 |
} |
| 218 |
$state ^= 1; |
| 219 |
} |
| 220 |
if ($strictPadding && $state !== 0) { |
| 221 |
throw new SodiumException( |
| 222 |
'Expected an even number of hexadecimal characters' |
| 223 |
); |
| 224 |
} |
| 225 |
return $bin; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public static function intArrayToString(array $ints): string |
| 237 |
{ |
| 238 |
$args = $ints; |
| 239 |
foreach ($args as $i => $v) { |
| 240 |
$args[$i] = ($v & 0xff); |
| 241 |
} |
| 242 |
array_unshift($args, str_repeat('C', count($ints))); |
| 243 |
return (string) (call_user_func_array('pack', $args)); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static function intToChr( |
| 256 |
#[SensitiveParameter] |
| 257 |
int $int |
| 258 |
): string { |
| 259 |
return pack('C', $int); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public static function load_3( |
| 273 |
#[SensitiveParameter] |
| 274 |
string $string |
| 275 |
): int { |
| 276 |
|
| 277 |
if (self::strlen($string) < 3) { |
| 278 |
throw new RangeException( |
| 279 |
'String must be 3 bytes or more; ' . self::strlen($string) . ' given.' |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
$unpacked = unpack('V', $string . "\0"); |
| 284 |
return ($unpacked[1] & 0xffffff); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public static function load_4( |
| 298 |
#[SensitiveParameter] |
| 299 |
string $string |
| 300 |
): int { |
| 301 |
|
| 302 |
if (self::strlen($string) < 4) { |
| 303 |
throw new RangeException( |
| 304 |
'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
$unpacked = unpack('V', $string); |
| 309 |
return $unpacked[1]; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public static function load64_le( |
| 325 |
#[SensitiveParameter] |
| 326 |
string $string |
| 327 |
): int { |
| 328 |
|
| 329 |
if (self::strlen($string) < 4) { |
| 330 |
throw new RangeException( |
| 331 |
'String must be 4 bytes or more; ' . self::strlen($string) . ' given.' |
| 332 |
); |
| 333 |
} |
| 334 |
$unpacked = unpack('P', $string); |
| 335 |
return (int) $unpacked[1]; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public static function memcmp( |
| 347 |
#[SensitiveParameter] |
| 348 |
string $left, |
| 349 |
#[SensitiveParameter] |
| 350 |
string $right |
| 351 |
): int { |
| 352 |
$e = (int) !self::hashEquals($left, $right); |
| 353 |
return 0 - $e; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public static function mul( |
| 374 |
#[SensitiveParameter] |
| 375 |
int $a, |
| 376 |
#[SensitiveParameter] |
| 377 |
int $b, |
| 378 |
int $size = 0 |
| 379 |
): int { |
| 380 |
if (ParagonIE_Sodium_Compat::$fastMult) { |
| 381 |
return (int) ($a * $b); |
| 382 |
} |
| 383 |
|
| 384 |
static $defaultSize = null; |
| 385 |
if (!$defaultSize) { |
| 386 |
$defaultSize = (PHP_INT_SIZE << 3) - 1; |
| 387 |
} |
| 388 |
if ($size < 1) { |
| 389 |
$size = $defaultSize; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
$c = 0; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
$mask = -(($b >> ($defaultSize)) & 1); |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
$b = ($b & ~$mask) | ($mask & -$b); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
for ($i = $size; $i >= 0; --$i) { |
| 418 |
$c += ($a & -($b & 1)); |
| 419 |
$a <<= 1; |
| 420 |
$b >>= 1; |
| 421 |
} |
| 422 |
$c = (int) @($c & -1); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
return (($c & ~$mask) | ($mask & -$c)); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public static function numericTo64BitInteger(int|float $num): array |
| 447 |
{ |
| 448 |
$high = 0; |
| 449 |
|
| 450 |
if (PHP_INT_SIZE === 4) { |
| 451 |
$low = (int) $num; |
| 452 |
} else { |
| 453 |
$low = $num & 0xffffffff; |
| 454 |
} |
| 455 |
|
| 456 |
if ((+(abs($num))) >= 1) { |
| 457 |
if ($num > 0) { |
| 458 |
|
| 459 |
$high = min((+(floor($num/4294967296))), 4294967295); |
| 460 |
} else { |
| 461 |
$high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296)))); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
return array((int) $high, (int) $low); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
public static function store32_le(int $int): string |
| 481 |
{ |
| 482 |
return pack('V', $int); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public static function store64_le(int $int): string |
| 495 |
{ |
| 496 |
return pack('P', $int); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public static function strlen( |
| 509 |
#[SensitiveParameter] |
| 510 |
string $str |
| 511 |
): int { |
| 512 |
return strlen($str); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
public static function stringToIntArray(string $string): array |
| 525 |
{ |
| 526 |
return array_values( |
| 527 |
unpack('C*', $string) |
| 528 |
); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public static function substr( |
| 543 |
#[SensitiveParameter] |
| 544 |
string $str, |
| 545 |
int $start = 0, |
| 546 |
?int $length = null |
| 547 |
): string { |
| 548 |
if ($length === 0) { |
| 549 |
return ''; |
| 550 |
} |
| 551 |
|
| 552 |
return substr($str, $start, $length); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public static function verify_16( |
| 566 |
#[SensitiveParameter] |
| 567 |
string $a, |
| 568 |
#[SensitiveParameter] |
| 569 |
string $b |
| 570 |
): bool { |
| 571 |
return self::hashEquals( |
| 572 |
self::substr($a, 0, 16), |
| 573 |
self::substr($b, 0, 16) |
| 574 |
); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public static function verify_32( |
| 590 |
#[SensitiveParameter] |
| 591 |
string $a, |
| 592 |
#[SensitiveParameter] |
| 593 |
string $b |
| 594 |
): bool { |
| 595 |
return self::hashEquals( |
| 596 |
self::substr($a, 0, 32), |
| 597 |
self::substr($b, 0, 32) |
| 598 |
); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public static function xorStrings( |
| 612 |
#[SensitiveParameter] |
| 613 |
string $a, |
| 614 |
#[SensitiveParameter] |
| 615 |
string $b |
| 616 |
): string { |
| 617 |
return $a ^ $b; |
| 618 |
} |
| 619 |
|
| 620 |
} |