|
@@ 612-637 (lines=26) @@
|
| 609 |
|
* @throws Error |
| 610 |
|
* @throws TypeError |
| 611 |
|
*/ |
| 612 |
|
public static function crypto_auth_verify($mac, $message, $key) |
| 613 |
|
{ |
| 614 |
|
/* Type checks: */ |
| 615 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1); |
| 616 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
| 617 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
| 618 |
|
|
| 619 |
|
/* Input validation: */ |
| 620 |
|
if (ParagonIE_Sodium_Core_Util::strlen($mac) !== self::CRYPTO_AUTH_BYTES) { |
| 621 |
|
throw new Error('Argument 1 must be CRYPTO_AUTH_BYTES long.'); |
| 622 |
|
} |
| 623 |
|
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) { |
| 624 |
|
throw new Error('Argument 3 must be CRYPTO_AUTH_KEYBYTES long.'); |
| 625 |
|
} |
| 626 |
|
|
| 627 |
|
if (self::isPhp72OrGreater()) { |
| 628 |
|
return sodium_crypto_auth_verify($mac, $message, $key); |
| 629 |
|
} |
| 630 |
|
if (self::use_fallback('crypto_auth_verify')) { |
| 631 |
|
return call_user_func('\\Sodium\\crypto_auth_verify', $mac, $message, $key); |
| 632 |
|
} |
| 633 |
|
if (PHP_INT_SIZE === 4) { |
| 634 |
|
return ParagonIE_Sodium_Crypto32::auth_verify($mac, $message, $key); |
| 635 |
|
} |
| 636 |
|
return ParagonIE_Sodium_Crypto::auth_verify($mac, $message, $key); |
| 637 |
|
} |
| 638 |
|
|
| 639 |
|
/** |
| 640 |
|
* Authenticated asymmetric-key encryption. Both the sender and recipient |
|
@@ 655-680 (lines=26) @@
|
| 652 |
|
* @throws Error |
| 653 |
|
* @throws TypeError |
| 654 |
|
*/ |
| 655 |
|
public static function crypto_box($plaintext, $nonce, $keypair) |
| 656 |
|
{ |
| 657 |
|
/* Type checks: */ |
| 658 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
| 659 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
| 660 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3); |
| 661 |
|
|
| 662 |
|
/* Input validation: */ |
| 663 |
|
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) { |
| 664 |
|
throw new Error('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.'); |
| 665 |
|
} |
| 666 |
|
if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) { |
| 667 |
|
throw new Error('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.'); |
| 668 |
|
} |
| 669 |
|
|
| 670 |
|
if (self::isPhp72OrGreater()) { |
| 671 |
|
return sodium_crypto_box($plaintext, $nonce, $keypair); |
| 672 |
|
} |
| 673 |
|
if (self::use_fallback('crypto_box')) { |
| 674 |
|
return call_user_func('\\Sodium\\crypto_box', $plaintext, $nonce, $keypair); |
| 675 |
|
} |
| 676 |
|
if (PHP_INT_SIZE === 4) { |
| 677 |
|
return ParagonIE_Sodium_Crypto32::box($plaintext, $nonce, $keypair); |
| 678 |
|
} |
| 679 |
|
return ParagonIE_Sodium_Crypto::box($plaintext, $nonce, $keypair); |
| 680 |
|
} |
| 681 |
|
|
| 682 |
|
/** |
| 683 |
|
* Anonymous public-key encryption. Only the recipient may decrypt messages. |
|
@@ 1442-1467 (lines=26) @@
|
| 1439 |
|
* @throws Error |
| 1440 |
|
* @throws TypeError |
| 1441 |
|
*/ |
| 1442 |
|
public static function crypto_secretbox($plaintext, $nonce, $key) |
| 1443 |
|
{ |
| 1444 |
|
/* Type checks: */ |
| 1445 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1); |
| 1446 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
| 1447 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
| 1448 |
|
|
| 1449 |
|
/* Input validation: */ |
| 1450 |
|
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { |
| 1451 |
|
throw new Error('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); |
| 1452 |
|
} |
| 1453 |
|
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { |
| 1454 |
|
throw new Error('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); |
| 1455 |
|
} |
| 1456 |
|
|
| 1457 |
|
if (self::isPhp72OrGreater()) { |
| 1458 |
|
return sodium_crypto_secretbox($plaintext, $nonce, $key); |
| 1459 |
|
} |
| 1460 |
|
if (self::use_fallback('crypto_secretbox')) { |
| 1461 |
|
return call_user_func('\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key); |
| 1462 |
|
} |
| 1463 |
|
if (PHP_INT_SIZE === 4) { |
| 1464 |
|
return ParagonIE_Sodium_Crypto32::secretbox($plaintext, $nonce, $key); |
| 1465 |
|
} |
| 1466 |
|
return ParagonIE_Sodium_Crypto::secretbox($plaintext, $nonce, $key); |
| 1467 |
|
} |
| 1468 |
|
|
| 1469 |
|
/** |
| 1470 |
|
* Decrypts a message previously encrypted with crypto_secretbox(). |
|
@@ 1479-1504 (lines=26) @@
|
| 1476 |
|
* @throws Error |
| 1477 |
|
* @throws TypeError |
| 1478 |
|
*/ |
| 1479 |
|
public static function crypto_secretbox_open($ciphertext, $nonce, $key) |
| 1480 |
|
{ |
| 1481 |
|
/* Type checks: */ |
| 1482 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1); |
| 1483 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
| 1484 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
| 1485 |
|
|
| 1486 |
|
/* Input validation: */ |
| 1487 |
|
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) { |
| 1488 |
|
throw new Error('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); |
| 1489 |
|
} |
| 1490 |
|
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) { |
| 1491 |
|
throw new Error('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); |
| 1492 |
|
} |
| 1493 |
|
|
| 1494 |
|
if (self::isPhp72OrGreater()) { |
| 1495 |
|
return sodium_crypto_secretbox_open($ciphertext, $nonce, $key); |
| 1496 |
|
} |
| 1497 |
|
if (self::use_fallback('crypto_secretbox_open')) { |
| 1498 |
|
return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key); |
| 1499 |
|
} |
| 1500 |
|
if (PHP_INT_SIZE === 4) { |
| 1501 |
|
return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext, $nonce, $key); |
| 1502 |
|
} |
| 1503 |
|
return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext, $nonce, $key); |
| 1504 |
|
} |
| 1505 |
|
|
| 1506 |
|
/** |
| 1507 |
|
* Authenticated symmetric-key encryption. |
|
@@ 1613-1638 (lines=26) @@
|
| 1610 |
|
* @throws Error |
| 1611 |
|
* @throws TypeError |
| 1612 |
|
*/ |
| 1613 |
|
public static function crypto_stream($len, $nonce, $key) |
| 1614 |
|
{ |
| 1615 |
|
/* Type checks: */ |
| 1616 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1); |
| 1617 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
| 1618 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
| 1619 |
|
|
| 1620 |
|
/* Input validation: */ |
| 1621 |
|
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) { |
| 1622 |
|
throw new Error('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); |
| 1623 |
|
} |
| 1624 |
|
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) { |
| 1625 |
|
throw new Error('Argument 3 must be CRYPTO_STREAM_KEYBYTES long.'); |
| 1626 |
|
} |
| 1627 |
|
|
| 1628 |
|
if (self::isPhp72OrGreater()) { |
| 1629 |
|
return sodium_crypto_stream($len, $nonce, $key); |
| 1630 |
|
} |
| 1631 |
|
if (self::use_fallback('crypto_stream')) { |
| 1632 |
|
return call_user_func('\\Sodium\\crypto_stream', $len, $nonce, $key); |
| 1633 |
|
} |
| 1634 |
|
if (PHP_INT_SIZE === 4) { |
| 1635 |
|
return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20($len, $nonce, $key); |
| 1636 |
|
} |
| 1637 |
|
return ParagonIE_Sodium_Core_XSalsa20::xsalsa20($len, $nonce, $key); |
| 1638 |
|
} |
| 1639 |
|
|
| 1640 |
|
/** |
| 1641 |
|
* DANGER! UNAUTHENTICATED ENCRYPTION! |
|
@@ 1659-1684 (lines=26) @@
|
| 1656 |
|
* @throws Error |
| 1657 |
|
* @throws TypeError |
| 1658 |
|
*/ |
| 1659 |
|
public static function crypto_stream_xor($message, $nonce, $key) |
| 1660 |
|
{ |
| 1661 |
|
/* Type checks: */ |
| 1662 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1); |
| 1663 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2); |
| 1664 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3); |
| 1665 |
|
|
| 1666 |
|
/* Input validation: */ |
| 1667 |
|
if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) { |
| 1668 |
|
throw new Error('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.'); |
| 1669 |
|
} |
| 1670 |
|
if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) { |
| 1671 |
|
throw new Error('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.'); |
| 1672 |
|
} |
| 1673 |
|
|
| 1674 |
|
if (self::isPhp72OrGreater()) { |
| 1675 |
|
return sodium_crypto_stream_xor($message, $nonce, $key); |
| 1676 |
|
} |
| 1677 |
|
if (self::use_fallback('crypto_stream_xor')) { |
| 1678 |
|
return call_user_func('\\Sodium\\crypto_stream_xor', $message, $nonce, $key); |
| 1679 |
|
} |
| 1680 |
|
if (PHP_INT_SIZE === 4) { |
| 1681 |
|
return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor($message, $nonce, $key); |
| 1682 |
|
} |
| 1683 |
|
return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor($message, $nonce, $key); |
| 1684 |
|
} |
| 1685 |
|
|
| 1686 |
|
/** |
| 1687 |
|
* Returns a signed message. You probably want crypto_sign_detached() |
|
@@ 1939-1964 (lines=26) @@
|
| 1936 |
|
* @throws Error |
| 1937 |
|
* @throws TypeError |
| 1938 |
|
*/ |
| 1939 |
|
public static function crypto_sign_verify_detached($signature, $message, $publicKey) |
| 1940 |
|
{ |
| 1941 |
|
/* Type checks: */ |
| 1942 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1); |
| 1943 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2); |
| 1944 |
|
ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 3); |
| 1945 |
|
|
| 1946 |
|
/* Input validation: */ |
| 1947 |
|
if (ParagonIE_Sodium_Core_Util::strlen($signature) !== self::CRYPTO_SIGN_BYTES) { |
| 1948 |
|
throw new Error('Argument 1 must be CRYPTO_SIGN_BYTES long.'); |
| 1949 |
|
} |
| 1950 |
|
if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) { |
| 1951 |
|
throw new Error('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.'); |
| 1952 |
|
} |
| 1953 |
|
|
| 1954 |
|
if (self::isPhp72OrGreater()) { |
| 1955 |
|
return sodium_crypto_sign_verify_detached($signature, $message, $publicKey); |
| 1956 |
|
} |
| 1957 |
|
if (self::use_fallback('crypto_sign_verify_detached')) { |
| 1958 |
|
return call_user_func('\\Sodium\\crypto_sign_verify_detached', $signature, $message, $publicKey); |
| 1959 |
|
} |
| 1960 |
|
if (PHP_INT_SIZE === 4) { |
| 1961 |
|
return ParagonIE_Sodium_Crypto32::sign_verify_detached($signature, $message, $publicKey); |
| 1962 |
|
} |
| 1963 |
|
return ParagonIE_Sodium_Crypto::sign_verify_detached($signature, $message, $publicKey); |
| 1964 |
|
} |
| 1965 |
|
|
| 1966 |
|
/** |
| 1967 |
|
* Convert an Ed25519 secret key to a Curve25519 secret key |