Changeset 45344 for trunk/src/wpincludes/sodium_compat/src/Compat.php
 Timestamp:
 05/17/2019 10:57:19 AM (5 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/wpincludes/sodium_compat/src/Compat.php
r44953 r45344 145 145 * @param string $left The left operand; must be a string 146 146 * @param string $right The right operand; must be a string 147 * @return int < 0 if the left operand is less than the right148 * = 0 if both strings are equal149 * > 0 if the right operand is less than the left147 * @return int If < 0 if the left operand is less than the right 148 * If = 0 if both strings are equal 149 * If > 0 if the right operand is less than the left 150 150 * @throws SodiumException 151 151 * @throws TypeError … … 670 670 * IETF mode uses a 96bit random nonce with a 32bit counter. 671 671 * 672 * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) 673 * @param string $assocData Authenticated Associated Data (unencrypted) 674 * @param string $nonce Number to be used only Once; must be 8 bytes 675 * @param string $key Encryption key 676 * 677 * @return string The original plaintext message 672 * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) 673 * @param string $assocData Authenticated Associated Data (unencrypted) 674 * @param string $nonce Number to be used only Once; must be 8 bytes 675 * @param string $key Encryption key 676 * @param bool $dontFallback Don't fallback to ext/sodium 677 * 678 * @return stringbool The original plaintext message 678 679 * @throws SodiumException 679 680 * @throws TypeError … … 684 685 $assocData = '', 685 686 $nonce = '', 686 $key = '' 687 $key = '', 688 $dontFallback = false 687 689 ) { 688 690 /* Type checks: */ … … 701 703 if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) { 702 704 throw new SodiumException('Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long'); 705 } 706 if (self::useNewSodiumAPI() && !$dontFallback) { 707 if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) { 708 return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt( 709 $ciphertext, 710 $assocData, 711 $nonce, 712 $key 713 ); 714 } 703 715 } 704 716 … … 728 740 * IETF mode uses a 96bit random nonce with a 32bit counter. 729 741 * 730 * @param string $plaintext Message to be encrypted 731 * @param string $assocData Authenticated Associated Data (unencrypted) 732 * @param string $nonce Number to be used only Once; must be 8 bytes 733 * @param string $key Encryption key 742 * @param string $plaintext Message to be encrypted 743 * @param string $assocData Authenticated Associated Data (unencrypted) 744 * @param string $nonce Number to be used only Once; must be 8 bytes 745 * @param string $key Encryption key 746 * @param bool $dontFallback Don't fallback to ext/sodium 734 747 * 735 748 * @return string Ciphertext with a 16byte Poly1305 message … … 743 756 $assocData = '', 744 757 $nonce = '', 745 $key = '' 758 $key = '', 759 $dontFallback = false 746 760 ) { 747 761 /* Type checks: */ … … 757 771 if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) { 758 772 throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long'); 773 } 774 if (self::useNewSodiumAPI() && !$dontFallback) { 775 if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) { 776 return sodium_crypto_aead_xchacha20poly1305_ietf_encrypt( 777 $plaintext, 778 $assocData, 779 $nonce, 780 $key 781 ); 782 } 759 783 } 760 784 … … 1280 1304 * Get the final BLAKE2b hash output for a given context. 1281 1305 * 1282 * @param string &$ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). 1283 * @param int $length Hash output size. 1284 * @return string Final BLAKE2b hash. 1285 * @throws SodiumException 1286 * @throws TypeError 1287 * @psalmsuppress MixedArgument 1306 * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). 1307 * @param int $length Hash output size. 1308 * @return string Final BLAKE2b hash. 1309 * @throws SodiumException 1310 * @throws TypeError 1311 * @psalmsuppress MixedArgument 1312 * @psalmsuppress ReferenceConstraintViolation 1288 1313 */ 1289 1314 public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES) … … 1358 1383 * Update a BLAKE2b hashing context with additional data. 1359 1384 * 1360 * @param string &$ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). 1361 * $ctx is passed by reference and gets updated inplace. 1385 * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). 1386 * $ctx is passed by reference and gets updated inplace. 1387 * @paramout string $ctx 1362 1388 * @param string $message The message to append to the existing hash state. 1363 1389 * @return void … … 1365 1391 * @throws TypeError 1366 1392 * @psalmsuppress MixedArgument 1393 * @psalmsuppress ReferenceConstraintViolation 1367 1394 */ 1368 1395 public static function crypto_generichash_update(&$ctx, $message) … … 2611 2638 * 2612 2639 * @param stringnull $var 2640 * @paramout stringnull $var 2613 2641 * 2614 2642 * @return void … … 2623 2651 2624 2652 if (self::useNewSodiumAPI()) { 2653 /** @psalmsuppress MixedArgument */ 2625 2654 sodium_memzero($var); 2626 2655 return; … … 2727 2756 2728 2757 /** 2758 * Runtime testing method for 32bit platforms. 2759 * 2760 * Usage: If runtime_speed_test() returns FALSE, then our 32bit 2761 * implementation is to slow to use safely without risking timeouts. 2762 * If this happens, install sodium from PECL to get acceptable 2763 * performance. 2764 * 2765 * @param int $iterations Number of multiplications to attempt 2766 * @param int $maxTimeout Milliseconds 2767 * @return bool TRUE if we're fast enough, FALSE is not 2768 * @throws SodiumException 2769 */ 2770 public static function runtime_speed_test($iterations, $maxTimeout) 2771 { 2772 if (self::polyfill_is_fast()) { 2773 return true; 2774 } 2775 /** @var float $end */ 2776 $end = 0.0; 2777 /** @var float $start */ 2778 $start = microtime(true); 2779 /** @var ParagonIE_Sodium_Core32_Int64 $a */ 2780 $a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16)); 2781 for ($i = 0; $i < $iterations; ++$i) { 2782 /** @var ParagonIE_Sodium_Core32_Int64 $b */ 2783 $b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16)); 2784 $a>mulInt64($b); 2785 } 2786 /** @var float $end */ 2787 $end = microtime(true); 2788 /** @var int $diff */ 2789 $diff = (int) ceil(($end  $start) * 1000); 2790 return $diff < $maxTimeout; 2791 } 2792 2793 /** 2729 2794 * This emulates libsodium's version_string() function, except ours is 2730 2795 * prefixed with 'polyfill'.
Note: See TracChangeset
for help on using the changeset viewer.