From efcecfbceac1bf014b1ede38f4588931fef92586 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Wed, 14 Apr 2021 02:07:47 -0400 Subject: [PATCH] We need a more surgical polyfill --- autoload.php | 5 ++++- lib/php72compat.php | 42 +-------------------------------------- lib/stream-xchacha20.php | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 lib/stream-xchacha20.php diff --git a/autoload.php b/autoload.php index d7c777b0..9ba42a0b 100644 --- a/autoload.php +++ b/autoload.php @@ -61,5 +61,8 @@ function sodiumCompatAutoloader($class) } else { assert(class_exists('ParagonIE_Sodium_Compat')); } - require_once (dirname(__FILE__) . '/lib/php72compat.php'); + require_once(dirname(__FILE__) . '/lib/php72compat.php'); +} elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) { + // Older versions of {PHP, ext/sodium} will not define these + require_once(dirname(__FILE__) . '/lib/php72compat.php'); } diff --git a/lib/php72compat.php b/lib/php72compat.php index 837a6753..10a061df 100644 --- a/lib/php72compat.php +++ b/lib/php72compat.php @@ -1218,47 +1218,7 @@ function sodium_crypto_stream_xor($message, $nonce, $key) return ParagonIE_Sodium_Compat::crypto_stream_xor($message, $nonce, $key); } } -if (!is_callable('sodium_crypto_stream_xchacha20')) { - /** - * @see ParagonIE_Sodium_Compat::crypto_stream_xchacha20() - * @param int $len - * @param string $nonce - * @param string $key - * @return string - * @throws SodiumException - * @throws TypeError - */ - function sodium_crypto_stream_xchacha20($len, $nonce, $key) - { - ParagonIE_Sodium_Compat::crypto_stream_xchacha20($len, $nonce, $key, true); - } -} -if (!is_callable('sodium_crypto_stream_xchacha20_keygen')) { - /** - * @see ParagonIE_Sodium_Compat::crypto_stream_xchacha20_keygen() - * @return string - * @throws Exception - */ - function sodium_crypto_stream_xchacha20_keygen() - { - return ParagonIE_Sodium_Compat::crypto_stream_xchacha20_keygen(); - } -} -if (!is_callable('sodium_crypto_stream_xchacha20_xor')) { - /** - * @see ParagonIE_Sodium_Compat::crypto_stream_xchacha20_xor() - * @param string $message - * @param string $nonce - * @param string $key - * @return string - * @throws SodiumException - * @throws TypeError - */ - function sodium_crypto_stream_xchacha20_xor($message, $nonce, $key) - { - ParagonIE_Sodium_Compat::crypto_stream_xchacha20_xor($message, $nonce, $key, true); - } -} +require_once dirname(__FILE__) . '/stream-xchacha20.php'; if (!is_callable('sodium_hex2bin')) { /** * @see ParagonIE_Sodium_Compat::hex2bin() diff --git a/lib/stream-xchacha20.php b/lib/stream-xchacha20.php new file mode 100644 index 00000000..82d42f2f --- /dev/null +++ b/lib/stream-xchacha20.php @@ -0,0 +1,43 @@ +