mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-24 13:22:49 +02:00
General: Add sodium_compat library for crypto APIs in PHP < 7.2
This adds a pure PHP implementation of the cryptographic functions supported in PHP 7.2+. It provides the necessary backwards compatibility required to support signature verification and other security features going forward across all supported PHP versions. Props paragoninitiativeenterprises Fixes #45806. See #39309. Built from https://develop.svn.wordpress.org/trunk@44953 git-svn-id: http://core.svn.wordpress.org/trunk@44784 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
217849aab3
commit
cb01bbf97b
@ -442,6 +442,10 @@ if ( ! interface_exists( 'JsonSerializable' ) ) {
|
||||
if ( ! function_exists( 'random_int' ) ) {
|
||||
require ABSPATH . WPINC . '/random_compat/random.php';
|
||||
}
|
||||
// sodium_crypto_box was introduced in PHP 7.2
|
||||
if ( ! function_exists( 'sodium_crypto_box' ) ) {
|
||||
require ABSPATH . WPINC . '/sodium_compat/autoload.php';
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'array_replace_recursive' ) ) :
|
||||
/**
|
||||
|
21
wp-includes/sodium_compat/LICENSE
Normal file
21
wp-includes/sodium_compat/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* ISC License
|
||||
*
|
||||
* Copyright (c) 2016-2019
|
||||
* Paragon Initiative Enterprises <security at paragonie dot com>
|
||||
*
|
||||
* Copyright (c) 2013-2019
|
||||
* Frank Denis <j at pureftpd dot org>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
49
wp-includes/sodium_compat/autoload.php
Normal file
49
wp-includes/sodium_compat/autoload.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
if (!is_callable('sodiumCompatAutoloader')) {
|
||||
/**
|
||||
* Sodium_Compat autoloader.
|
||||
*
|
||||
* @param string $class Class name to be autoloaded.
|
||||
*
|
||||
* @return bool Stop autoloading?
|
||||
*/
|
||||
function sodiumCompatAutoloader($class)
|
||||
{
|
||||
$namespace = 'ParagonIE_Sodium_';
|
||||
// Does the class use the namespace prefix?
|
||||
$len = strlen($namespace);
|
||||
if (strncmp($namespace, $class, $len) !== 0) {
|
||||
// no, move to the next registered autoloader
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the relative class name
|
||||
$relative_class = substr($class, $len);
|
||||
|
||||
// Replace the namespace prefix with the base directory, replace namespace
|
||||
// separators with directory separators in the relative class name, append
|
||||
// with .php
|
||||
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
|
||||
// if the file exists, require it
|
||||
if (file_exists($file)) {
|
||||
require_once $file;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now that we have an autoloader, let's register it!
|
||||
spl_autoload_register('sodiumCompatAutoloader');
|
||||
}
|
||||
|
||||
require_once dirname(__FILE__) . '/src/SodiumException.php';
|
||||
if (PHP_VERSION_ID >= 50300) {
|
||||
// Namespaces didn't exist before 5.3.0, so don't even try to use this
|
||||
// unless PHP >= 5.3.0
|
||||
require_once dirname(__FILE__) . '/lib/namespaced.php';
|
||||
require_once dirname(__FILE__) . '/lib/sodium_compat.php';
|
||||
}
|
||||
if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) {
|
||||
require_once dirname(__FILE__) . '/lib/php72compat.php';
|
||||
}
|
63
wp-includes/sodium_compat/composer.json
Normal file
63
wp-includes/sodium_compat/composer.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "paragonie/sodium_compat",
|
||||
"description": "Pure PHP implementation of libsodium; uses the PHP extension if it exists",
|
||||
"keywords": [
|
||||
"PHP",
|
||||
"cryptography",
|
||||
"elliptic curve",
|
||||
"elliptic curve cryptography",
|
||||
"Pure-PHP cryptography",
|
||||
"side-channel resistant",
|
||||
"Curve25519",
|
||||
"X25519",
|
||||
"ECDH",
|
||||
"Elliptic Curve Diffie-Hellman",
|
||||
"Ed25519",
|
||||
"RFC 7748",
|
||||
"RFC 8032",
|
||||
"EdDSA",
|
||||
"Edwards-curve Digital Signature Algorithm",
|
||||
"ChaCha20",
|
||||
"Salsa20",
|
||||
"Xchacha20",
|
||||
"Xsalsa20",
|
||||
"Poly1305",
|
||||
"BLAKE2b",
|
||||
"public-key cryptography",
|
||||
"secret-key cryptography",
|
||||
"AEAD",
|
||||
"Chapoly",
|
||||
"Salpoly",
|
||||
"ChaCha20-Poly1305",
|
||||
"XSalsa20-Poly1305",
|
||||
"XChaCha20-Poly1305",
|
||||
"encryption",
|
||||
"authentication",
|
||||
"libsodium"
|
||||
],
|
||||
"license": "ISC",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Paragon Initiative Enterprises",
|
||||
"email": "security@paragonie.com"
|
||||
},
|
||||
{
|
||||
"name": "Frank Denis",
|
||||
"email": "jedisct1@pureftpd.org"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"files": ["autoload.php"]
|
||||
},
|
||||
"require": {
|
||||
"php": "^5.2.4|^5.3|^5.4|^5.5|^5.6|^7",
|
||||
"paragonie/random_compat": ">=1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^3|^4|^5"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.",
|
||||
"ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security."
|
||||
}
|
||||
}
|
50
wp-includes/sodium_compat/lib/constants.php
Normal file
50
wp-includes/sodium_compat/lib/constants.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Sodium;
|
||||
|
||||
use ParagonIE_Sodium_Compat;
|
||||
|
||||
const CRYPTO_AEAD_AES256GCM_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_KEYBYTES;
|
||||
const CRYPTO_AEAD_AES256GCM_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_NSECBYTES;
|
||||
const CRYPTO_AEAD_AES256GCM_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_NPUBBYTES;
|
||||
const CRYPTO_AEAD_AES256GCM_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_ABYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES;
|
||||
const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES;
|
||||
const CRYPTO_AUTH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_AUTH_BYTES;
|
||||
const CRYPTO_AUTH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AUTH_KEYBYTES;
|
||||
const CRYPTO_BOX_SEALBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SEALBYTES;
|
||||
const CRYPTO_BOX_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES;
|
||||
const CRYPTO_BOX_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES;
|
||||
const CRYPTO_BOX_KEYPAIRBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES;
|
||||
const CRYPTO_BOX_MACBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_MACBYTES;
|
||||
const CRYPTO_BOX_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES;
|
||||
const CRYPTO_BOX_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_BOX_SEEDBYTES;
|
||||
const CRYPTO_KX_BYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_BYTES;
|
||||
const CRYPTO_KX_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_SEEDBYTES;
|
||||
const CRYPTO_KX_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_PUBLICKEYBYTES;
|
||||
const CRYPTO_KX_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_KX_SECRETKEYBYTES;
|
||||
const CRYPTO_GENERICHASH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES;
|
||||
const CRYPTO_GENERICHASH_BYTES_MIN = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN;
|
||||
const CRYPTO_GENERICHASH_BYTES_MAX = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX;
|
||||
const CRYPTO_GENERICHASH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES;
|
||||
const CRYPTO_GENERICHASH_KEYBYTES_MIN = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN;
|
||||
const CRYPTO_GENERICHASH_KEYBYTES_MAX = ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX;
|
||||
const CRYPTO_SCALARMULT_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SCALARMULT_BYTES;
|
||||
const CRYPTO_SCALARMULT_SCALARBYTES = ParagonIE_Sodium_Compat::CRYPTO_SCALARMULT_SCALARBYTES;
|
||||
const CRYPTO_SHORTHASH_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SHORTHASH_BYTES;
|
||||
const CRYPTO_SHORTHASH_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SHORTHASH_KEYBYTES;
|
||||
const CRYPTO_SECRETBOX_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES;
|
||||
const CRYPTO_SECRETBOX_MACBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES;
|
||||
const CRYPTO_SECRETBOX_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES;
|
||||
const CRYPTO_SIGN_BYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES;
|
||||
const CRYPTO_SIGN_SEEDBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_SEEDBYTES;
|
||||
const CRYPTO_SIGN_PUBLICKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES;
|
||||
const CRYPTO_SIGN_SECRETKEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES;
|
||||
const CRYPTO_SIGN_KEYPAIRBYTES = ParagonIE_Sodium_Compat::CRYPTO_SIGN_KEYPAIRBYTES;
|
||||
const CRYPTO_STREAM_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_STREAM_KEYBYTES;
|
||||
const CRYPTO_STREAM_NONCEBYTES = ParagonIE_Sodium_Compat::CRYPTO_STREAM_NONCEBYTES;
|
46
wp-includes/sodium_compat/lib/namespaced.php
Normal file
46
wp-includes/sodium_compat/lib/namespaced.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
if (PHP_VERSION_ID < 50300) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is just for convenience, to allow developers to reduce verbosity when
|
||||
* they add this project to their libraries.
|
||||
*
|
||||
* Replace this:
|
||||
*
|
||||
* $x = ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
|
||||
*
|
||||
* with this:
|
||||
*
|
||||
* use ParagonIE\Sodium\Compat;
|
||||
*
|
||||
* $x = Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
|
||||
*/
|
||||
spl_autoload_register(function ($class) {
|
||||
if ($class[0] === '\\') {
|
||||
$class = substr($class, 1);
|
||||
}
|
||||
$namespace = 'ParagonIE\\Sodium';
|
||||
// Does the class use the namespace prefix?
|
||||
$len = strlen($namespace);
|
||||
if (strncmp($namespace, $class, $len) !== 0) {
|
||||
// no, move to the next registered autoloader
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the relative class name
|
||||
$relative_class = substr($class, $len);
|
||||
|
||||
// Replace the namespace prefix with the base directory, replace namespace
|
||||
// separators with directory separators in the relative class name, append
|
||||
// with .php
|
||||
$file = dirname(__DIR__) . '/namespaced/' . str_replace('\\', '/', $relative_class) . '.php';
|
||||
// if the file exists, require it
|
||||
if (file_exists($file)) {
|
||||
require_once $file;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
1057
wp-includes/sodium_compat/lib/php72compat.php
Normal file
1057
wp-includes/sodium_compat/lib/php72compat.php
Normal file
File diff suppressed because it is too large
Load Diff
835
wp-includes/sodium_compat/lib/sodium_compat.php
Normal file
835
wp-includes/sodium_compat/lib/sodium_compat.php
Normal file
@ -0,0 +1,835 @@
|
||||
<?php
|
||||
namespace Sodium;
|
||||
|
||||
use ParagonIE_Sodium_Compat;
|
||||
|
||||
/**
|
||||
* This file will monkey patch the pure-PHP implementation in place of the
|
||||
* PECL functions, but only if they do not already exist.
|
||||
*
|
||||
* Thus, the functions just proxy to the appropriate ParagonIE_Sodium_Compat
|
||||
* method.
|
||||
*/
|
||||
if (!is_callable('\\Sodium\\bin2hex')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::bin2hex()
|
||||
* @param string $string
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function bin2hex($string)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::bin2hex($string);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\compare')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::compare()
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return int
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function compare($a, $b)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::compare($a, $b);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_decrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string|bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key);
|
||||
} catch (Error $ex) {
|
||||
return false;
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_encrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_is_available')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()
|
||||
* @return bool
|
||||
*/
|
||||
function crypto_aead_aes256gcm_is_available()
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available();
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_decrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string|bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
|
||||
} catch (Error $ex) {
|
||||
return false;
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_encrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string|bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
|
||||
} catch (Error $ex) {
|
||||
return false;
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()
|
||||
* @param string $message
|
||||
* @param string $assocData
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_auth')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_auth()
|
||||
* @param string $message
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_auth($message, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_auth_verify')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_auth_verify()
|
||||
* @param string $mac
|
||||
* @param string $message
|
||||
* @param string $key
|
||||
* @return bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_auth_verify($mac, $message, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_auth_verify($mac, $message, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box()
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $kp
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box($message, $nonce, $kp)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $kp);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_keypair')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_keypair()
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_keypair()
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_keypair();
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()
|
||||
* @param string $sk
|
||||
* @param string $pk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($sk, $pk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_open')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_open()
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $kp
|
||||
* @return string|bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_open($message, $nonce, $kp)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
|
||||
} catch (Error $ex) {
|
||||
return false;
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_publickey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_publickey()
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_publickey($keypair)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_publickey($keypair);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_publickey_from_secretkey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_publickey_from_secretkey($sk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($sk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_seal')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
|
||||
* @param string $message
|
||||
* @param string $publicKey
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_seal($message, $publicKey)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_seal($message, $publicKey);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_seal_open')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
|
||||
* @param string $message
|
||||
* @param string $kp
|
||||
* @return string|bool
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_seal_open($message, $kp)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_box_seal_open($message, $kp);
|
||||
} catch (\Error $ex) {
|
||||
return false;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_box_secretkey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_box_secretkey()
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_box_secretkey($keypair)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_box_secretkey($keypair);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_generichash')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_generichash()
|
||||
* @param string $message
|
||||
* @param string|null $key
|
||||
* @param int $outLen
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_generichash($message, $key = null, $outLen = 32)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_generichash($message, $key, $outLen);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_generichash_final')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_generichash_final()
|
||||
* @param string|null $ctx
|
||||
* @param int $outputLength
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_generichash_final(&$ctx, $outputLength = 32)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_generichash_init')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_generichash_init()
|
||||
* @param string|null $key
|
||||
* @param int $outLen
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_generichash_init($key = null, $outLen = 32)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outLen);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_generichash_update')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_generichash_update()
|
||||
* @param string|null $ctx
|
||||
* @param string $message
|
||||
* @return void
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_generichash_update(&$ctx, $message = '')
|
||||
{
|
||||
ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_kx')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_kx()
|
||||
* @param string $my_secret
|
||||
* @param string $their_public
|
||||
* @param string $client_public
|
||||
* @param string $server_public
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_kx($my_secret, $their_public, $client_public, $server_public)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_kx(
|
||||
$my_secret,
|
||||
$their_public,
|
||||
$client_public,
|
||||
$server_public
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash()
|
||||
* @param int $outlen
|
||||
* @param string $passwd
|
||||
* @param string $salt
|
||||
* @param int $opslimit
|
||||
* @param int $memlimit
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash_str')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash_str()
|
||||
* @param string $passwd
|
||||
* @param int $opslimit
|
||||
* @param int $memlimit
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash_str($passwd, $opslimit, $memlimit)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash_str_verify')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
|
||||
* @param string $passwd
|
||||
* @param string $hash
|
||||
* @return bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash_str_verify($passwd, $hash)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash_str_verify($passwd, $hash);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
|
||||
* @param int $outlen
|
||||
* @param string $passwd
|
||||
* @param string $salt
|
||||
* @param int $opslimit
|
||||
* @param int $memlimit
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
|
||||
* @param string $passwd
|
||||
* @param int $opslimit
|
||||
* @param int $memlimit
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
|
||||
* @param string $passwd
|
||||
* @param string $hash
|
||||
* @return bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_scalarmult')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_scalarmult()
|
||||
* @param string $n
|
||||
* @param string $p
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_scalarmult($n, $p)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_scalarmult($n, $p);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_scalarmult_base')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_scalarmult_base()
|
||||
* @param string $n
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_scalarmult_base($n)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_scalarmult_base($n);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_secretbox')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_secretbox()
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_secretbox($message, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_secretbox_open')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_secretbox_open()
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string|bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_secretbox_open($message, $nonce, $key)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
|
||||
} catch (Error $ex) {
|
||||
return false;
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_shorthash')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_shorthash()
|
||||
* @param string $message
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_shorthash($message, $key = '')
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_shorthash($message, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign()
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign($message, $sk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign($message, $sk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_detached')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_detached()
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_detached($message, $sk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_keypair')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_keypair()
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_keypair()
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_keypair();
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_open')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_open()
|
||||
* @param string $signedMessage
|
||||
* @param string $pk
|
||||
* @return string|bool
|
||||
*/
|
||||
function crypto_sign_open($signedMessage, $pk)
|
||||
{
|
||||
try {
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
|
||||
} catch (\Error $ex) {
|
||||
return false;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_publickey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_publickey()
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_publickey($keypair)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_publickey($keypair);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_publickey_from_secretkey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_publickey_from_secretkey($sk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey($sk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_secretkey')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_secretkey()
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_secretkey($keypair)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_secretkey($keypair);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_seed_keypair')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_seed_keypair()
|
||||
* @param string $seed
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_seed_keypair($seed)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_seed_keypair($seed);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_verify_detached')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_verify_detached()
|
||||
* @param string $signature
|
||||
* @param string $message
|
||||
* @param string $pk
|
||||
* @return bool
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_verify_detached($signature, $message, $pk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_verify_detached($signature, $message, $pk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()
|
||||
* @param string $pk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_ed25519_pk_to_curve25519($pk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($pk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_sign_ed25519_sk_to_curve25519($sk)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($sk);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_stream')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_stream()
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_stream($len, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_stream($len, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\crypto_stream_xor')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::crypto_stream_xor()
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function crypto_stream_xor($message, $nonce, $key)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::crypto_stream_xor($message, $nonce, $key);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\hex2bin')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::hex2bin()
|
||||
* @param string $string
|
||||
* @return string
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function hex2bin($string)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::hex2bin($string);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\memcmp')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::memcmp()
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return int
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function memcmp($a, $b)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::memcmp($a, $b);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\memzero')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::memzero()
|
||||
* @param string $str
|
||||
* @return void
|
||||
* @throws \SodiumException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function memzero(&$str)
|
||||
{
|
||||
ParagonIE_Sodium_Compat::memzero($str);
|
||||
}
|
||||
}
|
||||
if (!is_callable('\\Sodium\\randombytes_buf')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::randombytes_buf()
|
||||
* @param int $amount
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*/
|
||||
function randombytes_buf($amount)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::randombytes_buf($amount);
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_callable('\\Sodium\\randombytes_uniform')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::randombytes_uniform()
|
||||
* @param int $upperLimit
|
||||
* @return int
|
||||
* @throws \Exception
|
||||
* @throws \Error
|
||||
*/
|
||||
function randombytes_uniform($upperLimit)
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::randombytes_uniform($upperLimit);
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_callable('\\Sodium\\randombytes_random16')) {
|
||||
/**
|
||||
* @see ParagonIE_Sodium_Compat::randombytes_random16()
|
||||
* @return int
|
||||
*/
|
||||
function randombytes_random16()
|
||||
{
|
||||
return ParagonIE_Sodium_Compat::randombytes_random16();
|
||||
}
|
||||
}
|
||||
|
||||
if (!defined('\\Sodium\\CRYPTO_AUTH_BYTES')) {
|
||||
require_once dirname(__FILE__) . '/constants.php';
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Compat.php
Normal file
7
wp-includes/sodium_compat/namespaced/Compat.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium;
|
||||
|
||||
class Compat extends \ParagonIE_Sodium_Compat
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/BLAKE2b.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/BLAKE2b.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class BLAKE2b extends \ParagonIE_Sodium_Core_BLAKE2b
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/ChaCha20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/ChaCha20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class ChaCha20 extends \ParagonIE_Sodium_Core_ChaCha20
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\ChaCha20;
|
||||
|
||||
class Ctx extends \ParagonIE_Sodium_Core_ChaCha20_Ctx
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\ChaCha20;
|
||||
|
||||
class IetfCtx extends \ParagonIE_Sodium_Core_ChaCha20_IetfCtx
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Curve25519.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Curve25519.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class Curve25519 extends \ParagonIE_Sodium_Core_Curve25519
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519;
|
||||
|
||||
class Fe extends \ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519\Ge;
|
||||
|
||||
class Cached extends \ParagonIE_Sodium_Core_Curve25519_Ge_Cached
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519\Ge;
|
||||
|
||||
class P1p1 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519\Ge;
|
||||
|
||||
class P2 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P2
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519\Ge;
|
||||
|
||||
class P3 extends \ParagonIE_Sodium_Core_Curve25519_Ge_P3
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519\Ge;
|
||||
|
||||
class Precomp extends \ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Curve25519;
|
||||
|
||||
class H extends \ParagonIE_Sodium_Core_Curve25519_H
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Ed25519.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Ed25519.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class Ed25519 extends \ParagonIE_Sodium_Core_Ed25519
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/HChaCha20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/HChaCha20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class HChaCha20 extends \ParagonIE_Sodium_Core_HChaCha20
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/HSalsa20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/HSalsa20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class HSalsa20 extends \ParagonIE_Sodium_Core_HSalsa20
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Poly1305.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Poly1305.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class Poly1305 extends \ParagonIE_Sodium_Core_Poly1305
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core\Poly1305;
|
||||
|
||||
class State extends \ParagonIE_Sodium_Core_Poly1305_State
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Salsa20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Salsa20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class SipHash extends \ParagonIE_Sodium_Core_Salsa20
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/SipHash.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/SipHash.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class SipHash extends \ParagonIE_Sodium_Core_SipHash
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Util.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Util.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class Util extends \ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/X25519.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/X25519.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class X25519 extends \ParagonIE_Sodium_Core_X25519
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/XChaCha20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/XChaCha20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class XChaCha20 extends \ParagonIE_Sodium_Core_XChaCha20
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Core/Xsalsa20.php
Normal file
7
wp-includes/sodium_compat/namespaced/Core/Xsalsa20.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium\Core;
|
||||
|
||||
class Xsalsa20 extends \ParagonIE_Sodium_Core_Xsalsa20
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/Crypto.php
Normal file
7
wp-includes/sodium_compat/namespaced/Crypto.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium;
|
||||
|
||||
class Crypto extends \ParagonIE_Sodium_Crypto
|
||||
{
|
||||
|
||||
}
|
7
wp-includes/sodium_compat/namespaced/File.php
Normal file
7
wp-includes/sodium_compat/namespaced/File.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ParagonIE\Sodium;
|
||||
|
||||
class File extends \ParagonIE_Sodium_File
|
||||
{
|
||||
|
||||
}
|
2798
wp-includes/sodium_compat/src/Compat.php
Normal file
2798
wp-includes/sodium_compat/src/Compat.php
Normal file
File diff suppressed because it is too large
Load Diff
758
wp-includes/sodium_compat/src/Core/BLAKE2b.php
Normal file
758
wp-includes/sodium_compat/src/Core/BLAKE2b.php
Normal file
@ -0,0 +1,758 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_BLAKE2b
|
||||
*
|
||||
* Based on the work of Devi Mandiri in devi/salt.
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* @var SplFixedArray
|
||||
*/
|
||||
protected static $iv;
|
||||
|
||||
/**
|
||||
* @var array<int, array<int, int>>
|
||||
*/
|
||||
protected static $sigma = array(
|
||||
array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
|
||||
array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
|
||||
array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
|
||||
array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
|
||||
array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
|
||||
array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
|
||||
array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
|
||||
array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
|
||||
array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
|
||||
array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0),
|
||||
array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
|
||||
array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3)
|
||||
);
|
||||
|
||||
const BLOCKBYTES = 128;
|
||||
const OUTBYTES = 64;
|
||||
const KEYBYTES = 64;
|
||||
|
||||
/**
|
||||
* Turn two 32-bit integers into a fixed array representing a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $high
|
||||
* @param int $low
|
||||
* @return SplFixedArray
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
public static function new64($high, $low)
|
||||
{
|
||||
$i64 = new SplFixedArray(2);
|
||||
$i64[0] = $high & 0xffffffff;
|
||||
$i64[1] = $low & 0xffffffff;
|
||||
return $i64;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an arbitrary number into an SplFixedArray of two 32-bit integers
|
||||
* that represents a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $num
|
||||
* @return SplFixedArray
|
||||
*/
|
||||
protected static function to64($num)
|
||||
{
|
||||
list($hi, $lo) = self::numericTo64BitInteger($num);
|
||||
return self::new64($hi, $lo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two 64-bit integers together, returning their sum as a SplFixedArray
|
||||
* containing two 32-bit integers (representing a 64-bit integer).
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param SplFixedArray $y
|
||||
* @return SplFixedArray
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
protected static function add64($x, $y)
|
||||
{
|
||||
$l = ($x[1] + $y[1]) & 0xffffffff;
|
||||
return self::new64(
|
||||
$x[0] + $y[0] + (
|
||||
($l < $x[1]) ? 1 : 0
|
||||
),
|
||||
$l
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param SplFixedArray $y
|
||||
* @param SplFixedArray $z
|
||||
* @return SplFixedArray
|
||||
*/
|
||||
protected static function add364($x, $y, $z)
|
||||
{
|
||||
return self::add64($x, self::add64($y, $z));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param SplFixedArray $y
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function xor64(SplFixedArray $x, SplFixedArray $y)
|
||||
{
|
||||
if (!is_numeric($x[0])) {
|
||||
throw new SodiumException('x[0] is not an integer');
|
||||
}
|
||||
if (!is_numeric($x[1])) {
|
||||
throw new SodiumException('x[1] is not an integer');
|
||||
}
|
||||
if (!is_numeric($y[0])) {
|
||||
throw new SodiumException('y[0] is not an integer');
|
||||
}
|
||||
if (!is_numeric($y[1])) {
|
||||
throw new SodiumException('y[1] is not an integer');
|
||||
}
|
||||
return self::new64(
|
||||
(int) ($x[0] ^ $y[0]),
|
||||
(int) ($x[1] ^ $y[1])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param int $c
|
||||
* @return SplFixedArray
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
public static function rotr64($x, $c)
|
||||
{
|
||||
if ($c >= 64) {
|
||||
$c %= 64;
|
||||
}
|
||||
if ($c >= 32) {
|
||||
/** @var int $tmp */
|
||||
$tmp = $x[0];
|
||||
$x[0] = $x[1];
|
||||
$x[1] = $tmp;
|
||||
$c -= 32;
|
||||
}
|
||||
if ($c === 0) {
|
||||
return $x;
|
||||
}
|
||||
|
||||
$l0 = 0;
|
||||
$c = 64 - $c;
|
||||
|
||||
if ($c < 32) {
|
||||
/** @var int $h0 */
|
||||
$h0 = ((int) ($x[0]) << $c) | (
|
||||
(
|
||||
(int) ($x[1]) & ((1 << $c) - 1)
|
||||
<<
|
||||
(32 - $c)
|
||||
) >> (32 - $c)
|
||||
);
|
||||
/** @var int $l0 */
|
||||
$l0 = (int) ($x[1]) << $c;
|
||||
} else {
|
||||
/** @var int $h0 */
|
||||
$h0 = (int) ($x[1]) << ($c - 32);
|
||||
}
|
||||
|
||||
$h1 = 0;
|
||||
$c1 = 64 - $c;
|
||||
|
||||
if ($c1 < 32) {
|
||||
/** @var int $h1 */
|
||||
$h1 = (int) ($x[0]) >> $c1;
|
||||
/** @var int $l1 */
|
||||
$l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
|
||||
} else {
|
||||
/** @var int $l1 */
|
||||
$l1 = (int) ($x[0]) >> ($c1 - 32);
|
||||
}
|
||||
|
||||
return self::new64($h0 | $h1, $l0 | $l1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @return int
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
protected static function flatten64($x)
|
||||
{
|
||||
return (int) ($x[0] * 4294967296 + $x[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param int $i
|
||||
* @return SplFixedArray
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
protected static function load64(SplFixedArray $x, $i)
|
||||
{
|
||||
/** @var int $l */
|
||||
$l = (int) ($x[$i])
|
||||
| ((int) ($x[$i+1]) << 8)
|
||||
| ((int) ($x[$i+2]) << 16)
|
||||
| ((int) ($x[$i+3]) << 24);
|
||||
/** @var int $h */
|
||||
$h = (int) ($x[$i+4])
|
||||
| ((int) ($x[$i+5]) << 8)
|
||||
| ((int) ($x[$i+6]) << 16)
|
||||
| ((int) ($x[$i+7]) << 24);
|
||||
return self::new64($h, $l);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param int $i
|
||||
* @param SplFixedArray $u
|
||||
* @return void
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
|
||||
{
|
||||
$maxLength = $x->getSize() - 1;
|
||||
for ($j = 0; $j < 8; ++$j) {
|
||||
/*
|
||||
[0, 1, 2, 3, 4, 5, 6, 7]
|
||||
... becomes ...
|
||||
[0, 0, 0, 0, 1, 1, 1, 1]
|
||||
*/
|
||||
/** @var int $uIdx */
|
||||
$uIdx = ((7 - $j) & 4) >> 2;
|
||||
$x[$i] = ((int) ($u[$uIdx]) & 0xff);
|
||||
if (++$i > $maxLength) {
|
||||
return;
|
||||
}
|
||||
$u[$uIdx] >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This just sets the $iv static variable.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function pseudoConstructor()
|
||||
{
|
||||
static $called = false;
|
||||
if ($called) {
|
||||
return;
|
||||
}
|
||||
self::$iv = new SplFixedArray(8);
|
||||
self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
|
||||
self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
|
||||
self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
|
||||
self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
|
||||
self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
|
||||
self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
|
||||
self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
|
||||
self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
|
||||
|
||||
$called = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fresh BLAKE2 context.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return SplFixedArray
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
*/
|
||||
protected static function context()
|
||||
{
|
||||
$ctx = new SplFixedArray(5);
|
||||
$ctx[0] = new SplFixedArray(8); // h
|
||||
$ctx[1] = new SplFixedArray(2); // t
|
||||
$ctx[2] = new SplFixedArray(2); // f
|
||||
$ctx[3] = new SplFixedArray(256); // buf
|
||||
$ctx[4] = 0; // buflen
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$ctx[0][$i] = self::$iv[$i];
|
||||
}
|
||||
for ($i = 256; $i--;) {
|
||||
$ctx[3][$i] = 0;
|
||||
}
|
||||
|
||||
$zero = self::new64(0, 0);
|
||||
$ctx[1][0] = $zero;
|
||||
$ctx[1][1] = $zero;
|
||||
$ctx[2][0] = $zero;
|
||||
$ctx[2][1] = $zero;
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $buf
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
|
||||
{
|
||||
$m = new SplFixedArray(16);
|
||||
$v = new SplFixedArray(16);
|
||||
|
||||
for ($i = 16; $i--;) {
|
||||
$m[$i] = self::load64($buf, $i << 3);
|
||||
}
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$v[$i] = $ctx[0][$i];
|
||||
}
|
||||
|
||||
$v[ 8] = self::$iv[0];
|
||||
$v[ 9] = self::$iv[1];
|
||||
$v[10] = self::$iv[2];
|
||||
$v[11] = self::$iv[3];
|
||||
|
||||
$v[12] = self::xor64($ctx[1][0], self::$iv[4]);
|
||||
$v[13] = self::xor64($ctx[1][1], self::$iv[5]);
|
||||
$v[14] = self::xor64($ctx[2][0], self::$iv[6]);
|
||||
$v[15] = self::xor64($ctx[2][1], self::$iv[7]);
|
||||
|
||||
for ($r = 0; $r < 12; ++$r) {
|
||||
$v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
|
||||
$v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
|
||||
$v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
|
||||
$v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
|
||||
$v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
|
||||
$v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
|
||||
$v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
|
||||
$v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
|
||||
}
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$ctx[0][$i] = self::xor64(
|
||||
$ctx[0][$i], self::xor64($v[$i], $v[$i+8])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $r
|
||||
* @param int $i
|
||||
* @param int $a
|
||||
* @param int $b
|
||||
* @param int $c
|
||||
* @param int $d
|
||||
* @param SplFixedArray $v
|
||||
* @param SplFixedArray $m
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
|
||||
{
|
||||
$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
|
||||
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
|
||||
$v[$c] = self::add64($v[$c], $v[$d]);
|
||||
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
|
||||
$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]);
|
||||
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
|
||||
$v[$c] = self::add64($v[$c], $v[$d]);
|
||||
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
|
||||
return $v;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param int $inc
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
*/
|
||||
public static function increment_counter($ctx, $inc)
|
||||
{
|
||||
if ($inc < 0) {
|
||||
throw new SodiumException('Increasing by a negative number makes no sense.');
|
||||
}
|
||||
$t = self::to64($inc);
|
||||
# S->t is $ctx[1] in our implementation
|
||||
|
||||
# S->t[0] = ( uint64_t )( t >> 0 );
|
||||
$ctx[1][0] = self::add64($ctx[1][0], $t);
|
||||
|
||||
# S->t[1] += ( S->t[0] < inc );
|
||||
if (self::flatten64($ctx[1][0]) < $inc) {
|
||||
$ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $p
|
||||
* @param int $plen
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
|
||||
$offset = 0;
|
||||
while ($plen > 0) {
|
||||
$left = $ctx[4];
|
||||
$fill = 256 - $left;
|
||||
|
||||
if ($plen > $fill) {
|
||||
# memcpy( S->buf + left, in, fill ); /* Fill buffer */
|
||||
for ($i = $fill; $i--;) {
|
||||
$ctx[3][$i + $left] = $p[$i + $offset];
|
||||
}
|
||||
|
||||
# S->buflen += fill;
|
||||
$ctx[4] += $fill;
|
||||
|
||||
# blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
||||
self::increment_counter($ctx, 128);
|
||||
|
||||
# blake2b_compress( S, S->buf ); /* Compress */
|
||||
self::compress($ctx, $ctx[3]);
|
||||
|
||||
# memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
|
||||
for ($i = 128; $i--;) {
|
||||
$ctx[3][$i] = $ctx[3][$i + 128];
|
||||
}
|
||||
|
||||
# S->buflen -= BLAKE2B_BLOCKBYTES;
|
||||
$ctx[4] -= 128;
|
||||
|
||||
# in += fill;
|
||||
$offset += $fill;
|
||||
|
||||
# inlen -= fill;
|
||||
$plen -= $fill;
|
||||
} else {
|
||||
for ($i = $plen; $i--;) {
|
||||
$ctx[3][$i + $left] = $p[$i + $offset];
|
||||
}
|
||||
$ctx[4] += $plen;
|
||||
$offset += $plen;
|
||||
$plen -= $plen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $out
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
public static function finish(SplFixedArray $ctx, SplFixedArray $out)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
if ($ctx[4] > 128) {
|
||||
self::increment_counter($ctx, 128);
|
||||
self::compress($ctx, $ctx[3]);
|
||||
$ctx[4] -= 128;
|
||||
if ($ctx[4] > 128) {
|
||||
throw new SodiumException('Failed to assert that buflen <= 128 bytes');
|
||||
}
|
||||
for ($i = $ctx[4]; $i--;) {
|
||||
$ctx[3][$i] = $ctx[3][$i + 128];
|
||||
}
|
||||
}
|
||||
|
||||
self::increment_counter($ctx, $ctx[4]);
|
||||
$ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
|
||||
|
||||
for ($i = 256 - $ctx[4]; $i--;) {
|
||||
$ctx[3][$i+$ctx[4]] = 0;
|
||||
}
|
||||
|
||||
self::compress($ctx, $ctx[3]);
|
||||
|
||||
$i = (int) (($out->getSize() - 1) / 8);
|
||||
for (; $i >= 0; --$i) {
|
||||
self::store64($out, $i << 3, $ctx[0][$i]);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray|null $key
|
||||
* @param int $outlen
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public static function init($key = null, $outlen = 64)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
$klen = 0;
|
||||
|
||||
if ($key !== null) {
|
||||
if (count($key) > 64) {
|
||||
throw new SodiumException('Invalid key size');
|
||||
}
|
||||
$klen = count($key);
|
||||
}
|
||||
|
||||
if ($outlen > 64) {
|
||||
throw new SodiumException('Invalid output size');
|
||||
}
|
||||
|
||||
$ctx = self::context();
|
||||
|
||||
$p = new SplFixedArray(64);
|
||||
for ($i = 64; --$i;) {
|
||||
$p[$i] = 0;
|
||||
}
|
||||
|
||||
$p[0] = $outlen; // digest_length
|
||||
$p[1] = $klen; // key_length
|
||||
$p[2] = 1; // fanout
|
||||
$p[3] = 1; // depth
|
||||
|
||||
$ctx[0][0] = self::xor64(
|
||||
$ctx[0][0],
|
||||
self::load64($p, 0)
|
||||
);
|
||||
|
||||
if ($klen > 0 && $key instanceof SplFixedArray) {
|
||||
$block = new SplFixedArray(128);
|
||||
for ($i = 128; $i--;) {
|
||||
$block[$i] = 0;
|
||||
}
|
||||
for ($i = $klen; $i--;) {
|
||||
$block[$i] = $key[$i];
|
||||
}
|
||||
self::update($ctx, $block, 128);
|
||||
}
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string into an SplFixedArray of integers
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $str
|
||||
* @return SplFixedArray
|
||||
*/
|
||||
public static function stringToSplFixedArray($str = '')
|
||||
{
|
||||
$values = unpack('C*', $str);
|
||||
return SplFixedArray::fromArray(array_values($values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an SplFixedArray of integers into a string
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $a
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function SplFixedArrayToString(SplFixedArray $a)
|
||||
{
|
||||
/**
|
||||
* @var array<int, int|string> $arr
|
||||
*/
|
||||
$arr = $a->toArray();
|
||||
$c = $a->count();
|
||||
array_unshift($arr, str_repeat('C', $c));
|
||||
return (string) (call_user_func_array('pack', $arr));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray[SplFixedArray] $ctx
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @psalm-suppress MixedMethodCall
|
||||
*/
|
||||
public static function contextToString(SplFixedArray $ctx)
|
||||
{
|
||||
$str = '';
|
||||
/** @var array<int, array<int, int>> $ctxA */
|
||||
$ctxA = $ctx[0]->toArray();
|
||||
|
||||
# uint64_t h[8];
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
$str .= self::store32_le($ctxA[$i][1]);
|
||||
$str .= self::store32_le($ctxA[$i][0]);
|
||||
}
|
||||
|
||||
# uint64_t t[2];
|
||||
# uint64_t f[2];
|
||||
for ($i = 1; $i < 3; ++$i) {
|
||||
$ctxA = $ctx[$i]->toArray();
|
||||
$str .= self::store32_le($ctxA[0][1]);
|
||||
$str .= self::store32_le($ctxA[0][0]);
|
||||
$str .= self::store32_le($ctxA[1][1]);
|
||||
$str .= self::store32_le($ctxA[1][0]);
|
||||
}
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$str .= self::SplFixedArrayToString($ctx[3]);
|
||||
|
||||
/** @var int $ctx4 */
|
||||
$ctx4 = (int) $ctx[4];
|
||||
|
||||
# size_t buflen;
|
||||
$str .= implode('', array(
|
||||
self::intToChr($ctx4 & 0xff),
|
||||
self::intToChr(($ctx4 >> 8) & 0xff),
|
||||
self::intToChr(($ctx4 >> 16) & 0xff),
|
||||
self::intToChr(($ctx4 >> 24) & 0xff),
|
||||
self::intToChr(($ctx4 >> 32) & 0xff),
|
||||
self::intToChr(($ctx4 >> 40) & 0xff),
|
||||
self::intToChr(($ctx4 >> 48) & 0xff),
|
||||
self::intToChr(($ctx4 >> 56) & 0xff)
|
||||
));
|
||||
# uint8_t last_node;
|
||||
return $str . "\x00";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an SplFixedArray containing other SplFixedArray elements, from
|
||||
* a string (compatible with \Sodium\crypto_generichash_{init, update, final})
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
*/
|
||||
public static function stringToContext($string)
|
||||
{
|
||||
$ctx = self::context();
|
||||
|
||||
# uint64_t h[8];
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
$ctx[0][$i] = SplFixedArray::fromArray(
|
||||
array(
|
||||
self::load_4(
|
||||
self::substr($string, (($i << 3) + 4), 4)
|
||||
),
|
||||
self::load_4(
|
||||
self::substr($string, (($i << 3) + 0), 4)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
# uint64_t t[2];
|
||||
# uint64_t f[2];
|
||||
for ($i = 1; $i < 3; ++$i) {
|
||||
$ctx[$i][1] = SplFixedArray::fromArray(
|
||||
array(
|
||||
self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)),
|
||||
self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4))
|
||||
)
|
||||
);
|
||||
$ctx[$i][0] = SplFixedArray::fromArray(
|
||||
array(
|
||||
self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)),
|
||||
self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
|
||||
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$int = 0;
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
$int |= self::chrToInt($string[352 + $i]) << ($i << 3);
|
||||
}
|
||||
$ctx[4] = $int;
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
}
|
395
wp-includes/sodium_compat/src/Core/ChaCha20.php
Normal file
395
wp-includes/sodium_compat/src/Core/ChaCha20.php
Normal file
@ -0,0 +1,395 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_ChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* Bitwise left rotation
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $v
|
||||
* @param int $n
|
||||
* @return int
|
||||
*/
|
||||
public static function rotate($v, $n)
|
||||
{
|
||||
$v &= 0xffffffff;
|
||||
$n &= 31;
|
||||
return (int) (
|
||||
0xffffffff & (
|
||||
($v << $n)
|
||||
|
|
||||
($v >> (32 - $n))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ChaCha20 quarter round function. Works on four 32-bit integers.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $a
|
||||
* @param int $b
|
||||
* @param int $c
|
||||
* @param int $d
|
||||
* @return array<int, int>
|
||||
*/
|
||||
protected static function quarterRound($a, $b, $c, $d)
|
||||
{
|
||||
# a = PLUS(a,b); d = ROTATE(XOR(d,a),16);
|
||||
/** @var int $a */
|
||||
$a = ($a + $b) & 0xffffffff;
|
||||
$d = self::rotate($d ^ $a, 16);
|
||||
|
||||
# c = PLUS(c,d); b = ROTATE(XOR(b,c),12);
|
||||
/** @var int $c */
|
||||
$c = ($c + $d) & 0xffffffff;
|
||||
$b = self::rotate($b ^ $c, 12);
|
||||
|
||||
# a = PLUS(a,b); d = ROTATE(XOR(d,a), 8);
|
||||
/** @var int $a */
|
||||
$a = ($a + $b) & 0xffffffff;
|
||||
$d = self::rotate($d ^ $a, 8);
|
||||
|
||||
# c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
|
||||
/** @var int $c */
|
||||
$c = ($c + $d) & 0xffffffff;
|
||||
$b = self::rotate($b ^ $c, 7);
|
||||
return array((int) $a, (int) $b, (int) $c, (int) $d);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx
|
||||
* @param string $message
|
||||
*
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
* @throws SodiumException
|
||||
*/
|
||||
public static function encryptBytes(
|
||||
ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx,
|
||||
$message = ''
|
||||
) {
|
||||
$bytes = self::strlen($message);
|
||||
|
||||
/*
|
||||
j0 = ctx->input[0];
|
||||
j1 = ctx->input[1];
|
||||
j2 = ctx->input[2];
|
||||
j3 = ctx->input[3];
|
||||
j4 = ctx->input[4];
|
||||
j5 = ctx->input[5];
|
||||
j6 = ctx->input[6];
|
||||
j7 = ctx->input[7];
|
||||
j8 = ctx->input[8];
|
||||
j9 = ctx->input[9];
|
||||
j10 = ctx->input[10];
|
||||
j11 = ctx->input[11];
|
||||
j12 = ctx->input[12];
|
||||
j13 = ctx->input[13];
|
||||
j14 = ctx->input[14];
|
||||
j15 = ctx->input[15];
|
||||
*/
|
||||
$j0 = (int) $ctx[0];
|
||||
$j1 = (int) $ctx[1];
|
||||
$j2 = (int) $ctx[2];
|
||||
$j3 = (int) $ctx[3];
|
||||
$j4 = (int) $ctx[4];
|
||||
$j5 = (int) $ctx[5];
|
||||
$j6 = (int) $ctx[6];
|
||||
$j7 = (int) $ctx[7];
|
||||
$j8 = (int) $ctx[8];
|
||||
$j9 = (int) $ctx[9];
|
||||
$j10 = (int) $ctx[10];
|
||||
$j11 = (int) $ctx[11];
|
||||
$j12 = (int) $ctx[12];
|
||||
$j13 = (int) $ctx[13];
|
||||
$j14 = (int) $ctx[14];
|
||||
$j15 = (int) $ctx[15];
|
||||
|
||||
$c = '';
|
||||
for (;;) {
|
||||
if ($bytes < 64) {
|
||||
$message .= str_repeat("\x00", 64 - $bytes);
|
||||
}
|
||||
|
||||
$x0 = (int) $j0;
|
||||
$x1 = (int) $j1;
|
||||
$x2 = (int) $j2;
|
||||
$x3 = (int) $j3;
|
||||
$x4 = (int) $j4;
|
||||
$x5 = (int) $j5;
|
||||
$x6 = (int) $j6;
|
||||
$x7 = (int) $j7;
|
||||
$x8 = (int) $j8;
|
||||
$x9 = (int) $j9;
|
||||
$x10 = (int) $j10;
|
||||
$x11 = (int) $j11;
|
||||
$x12 = (int) $j12;
|
||||
$x13 = (int) $j13;
|
||||
$x14 = (int) $j14;
|
||||
$x15 = (int) $j15;
|
||||
|
||||
# for (i = 20; i > 0; i -= 2) {
|
||||
for ($i = 20; $i > 0; $i -= 2) {
|
||||
# QUARTERROUND( x0, x4, x8, x12)
|
||||
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
|
||||
|
||||
# QUARTERROUND( x1, x5, x9, x13)
|
||||
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
|
||||
|
||||
# QUARTERROUND( x2, x6, x10, x14)
|
||||
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
|
||||
|
||||
# QUARTERROUND( x3, x7, x11, x15)
|
||||
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
|
||||
|
||||
# QUARTERROUND( x0, x5, x10, x15)
|
||||
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
|
||||
|
||||
# QUARTERROUND( x1, x6, x11, x12)
|
||||
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
|
||||
|
||||
# QUARTERROUND( x2, x7, x8, x13)
|
||||
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
|
||||
|
||||
# QUARTERROUND( x3, x4, x9, x14)
|
||||
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
|
||||
}
|
||||
/*
|
||||
x0 = PLUS(x0, j0);
|
||||
x1 = PLUS(x1, j1);
|
||||
x2 = PLUS(x2, j2);
|
||||
x3 = PLUS(x3, j3);
|
||||
x4 = PLUS(x4, j4);
|
||||
x5 = PLUS(x5, j5);
|
||||
x6 = PLUS(x6, j6);
|
||||
x7 = PLUS(x7, j7);
|
||||
x8 = PLUS(x8, j8);
|
||||
x9 = PLUS(x9, j9);
|
||||
x10 = PLUS(x10, j10);
|
||||
x11 = PLUS(x11, j11);
|
||||
x12 = PLUS(x12, j12);
|
||||
x13 = PLUS(x13, j13);
|
||||
x14 = PLUS(x14, j14);
|
||||
x15 = PLUS(x15, j15);
|
||||
*/
|
||||
/** @var int $x0 */
|
||||
$x0 = ($x0 & 0xffffffff) + $j0;
|
||||
/** @var int $x1 */
|
||||
$x1 = ($x1 & 0xffffffff) + $j1;
|
||||
/** @var int $x2 */
|
||||
$x2 = ($x2 & 0xffffffff) + $j2;
|
||||
/** @var int $x3 */
|
||||
$x3 = ($x3 & 0xffffffff) + $j3;
|
||||
/** @var int $x4 */
|
||||
$x4 = ($x4 & 0xffffffff) + $j4;
|
||||
/** @var int $x5 */
|
||||
$x5 = ($x5 & 0xffffffff) + $j5;
|
||||
/** @var int $x6 */
|
||||
$x6 = ($x6 & 0xffffffff) + $j6;
|
||||
/** @var int $x7 */
|
||||
$x7 = ($x7 & 0xffffffff) + $j7;
|
||||
/** @var int $x8 */
|
||||
$x8 = ($x8 & 0xffffffff) + $j8;
|
||||
/** @var int $x9 */
|
||||
$x9 = ($x9 & 0xffffffff) + $j9;
|
||||
/** @var int $x10 */
|
||||
$x10 = ($x10 & 0xffffffff) + $j10;
|
||||
/** @var int $x11 */
|
||||
$x11 = ($x11 & 0xffffffff) + $j11;
|
||||
/** @var int $x12 */
|
||||
$x12 = ($x12 & 0xffffffff) + $j12;
|
||||
/** @var int $x13 */
|
||||
$x13 = ($x13 & 0xffffffff) + $j13;
|
||||
/** @var int $x14 */
|
||||
$x14 = ($x14 & 0xffffffff) + $j14;
|
||||
/** @var int $x15 */
|
||||
$x15 = ($x15 & 0xffffffff) + $j15;
|
||||
|
||||
/*
|
||||
x0 = XOR(x0, LOAD32_LE(m + 0));
|
||||
x1 = XOR(x1, LOAD32_LE(m + 4));
|
||||
x2 = XOR(x2, LOAD32_LE(m + 8));
|
||||
x3 = XOR(x3, LOAD32_LE(m + 12));
|
||||
x4 = XOR(x4, LOAD32_LE(m + 16));
|
||||
x5 = XOR(x5, LOAD32_LE(m + 20));
|
||||
x6 = XOR(x6, LOAD32_LE(m + 24));
|
||||
x7 = XOR(x7, LOAD32_LE(m + 28));
|
||||
x8 = XOR(x8, LOAD32_LE(m + 32));
|
||||
x9 = XOR(x9, LOAD32_LE(m + 36));
|
||||
x10 = XOR(x10, LOAD32_LE(m + 40));
|
||||
x11 = XOR(x11, LOAD32_LE(m + 44));
|
||||
x12 = XOR(x12, LOAD32_LE(m + 48));
|
||||
x13 = XOR(x13, LOAD32_LE(m + 52));
|
||||
x14 = XOR(x14, LOAD32_LE(m + 56));
|
||||
x15 = XOR(x15, LOAD32_LE(m + 60));
|
||||
*/
|
||||
$x0 ^= self::load_4(self::substr($message, 0, 4));
|
||||
$x1 ^= self::load_4(self::substr($message, 4, 4));
|
||||
$x2 ^= self::load_4(self::substr($message, 8, 4));
|
||||
$x3 ^= self::load_4(self::substr($message, 12, 4));
|
||||
$x4 ^= self::load_4(self::substr($message, 16, 4));
|
||||
$x5 ^= self::load_4(self::substr($message, 20, 4));
|
||||
$x6 ^= self::load_4(self::substr($message, 24, 4));
|
||||
$x7 ^= self::load_4(self::substr($message, 28, 4));
|
||||
$x8 ^= self::load_4(self::substr($message, 32, 4));
|
||||
$x9 ^= self::load_4(self::substr($message, 36, 4));
|
||||
$x10 ^= self::load_4(self::substr($message, 40, 4));
|
||||
$x11 ^= self::load_4(self::substr($message, 44, 4));
|
||||
$x12 ^= self::load_4(self::substr($message, 48, 4));
|
||||
$x13 ^= self::load_4(self::substr($message, 52, 4));
|
||||
$x14 ^= self::load_4(self::substr($message, 56, 4));
|
||||
$x15 ^= self::load_4(self::substr($message, 60, 4));
|
||||
|
||||
/*
|
||||
j12 = PLUSONE(j12);
|
||||
if (!j12) {
|
||||
j13 = PLUSONE(j13);
|
||||
}
|
||||
*/
|
||||
++$j12;
|
||||
if ($j12 & 0xf0000000) {
|
||||
throw new SodiumException('Overflow');
|
||||
}
|
||||
|
||||
/*
|
||||
STORE32_LE(c + 0, x0);
|
||||
STORE32_LE(c + 4, x1);
|
||||
STORE32_LE(c + 8, x2);
|
||||
STORE32_LE(c + 12, x3);
|
||||
STORE32_LE(c + 16, x4);
|
||||
STORE32_LE(c + 20, x5);
|
||||
STORE32_LE(c + 24, x6);
|
||||
STORE32_LE(c + 28, x7);
|
||||
STORE32_LE(c + 32, x8);
|
||||
STORE32_LE(c + 36, x9);
|
||||
STORE32_LE(c + 40, x10);
|
||||
STORE32_LE(c + 44, x11);
|
||||
STORE32_LE(c + 48, x12);
|
||||
STORE32_LE(c + 52, x13);
|
||||
STORE32_LE(c + 56, x14);
|
||||
STORE32_LE(c + 60, x15);
|
||||
*/
|
||||
$block = self::store32_le((int) ($x0 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x1 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x2 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x3 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x4 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x5 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x6 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x7 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x8 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x9 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x10 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x11 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x12 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x13 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x14 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x15 & 0xffffffff));
|
||||
|
||||
/* Partial block */
|
||||
if ($bytes < 64) {
|
||||
$c .= self::substr($block, 0, $bytes);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Full block */
|
||||
$c .= $block;
|
||||
$bytes -= 64;
|
||||
if ($bytes <= 0) {
|
||||
break;
|
||||
}
|
||||
$message = self::substr($message, 64);
|
||||
}
|
||||
/* end for(;;) loop */
|
||||
|
||||
$ctx[12] = $j12;
|
||||
$ctx[13] = $j13;
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function stream($len = 64, $nonce = '', $key = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function ietfStream($len, $nonce = '', $key = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic),
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
120
wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php
Normal file
120
wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_ChaCha20_Ctx
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_ChaCha20_Ctx extends ParagonIE_Sodium_Core_Util implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var SplFixedArray internally, <int, int>
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key ChaCha20 key.
|
||||
* @param string $iv Initialization Vector (a.k.a. nonce).
|
||||
* @param string $counter The initial counter value.
|
||||
* Defaults to 8 0x00 bytes.
|
||||
* @throws InvalidArgumentException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '', $iv = '', $counter = '')
|
||||
{
|
||||
if (self::strlen($key) !== 32) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.');
|
||||
}
|
||||
if (self::strlen($iv) !== 8) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.');
|
||||
}
|
||||
$this->container = new SplFixedArray(16);
|
||||
|
||||
/* "expand 32-byte k" as per ChaCha20 spec */
|
||||
$this->container[0] = 0x61707865;
|
||||
$this->container[1] = 0x3320646e;
|
||||
$this->container[2] = 0x79622d32;
|
||||
$this->container[3] = 0x6b206574;
|
||||
$this->container[4] = self::load_4(self::substr($key, 0, 4));
|
||||
$this->container[5] = self::load_4(self::substr($key, 4, 4));
|
||||
$this->container[6] = self::load_4(self::substr($key, 8, 4));
|
||||
$this->container[7] = self::load_4(self::substr($key, 12, 4));
|
||||
$this->container[8] = self::load_4(self::substr($key, 16, 4));
|
||||
$this->container[9] = self::load_4(self::substr($key, 20, 4));
|
||||
$this->container[10] = self::load_4(self::substr($key, 24, 4));
|
||||
$this->container[11] = self::load_4(self::substr($key, 28, 4));
|
||||
|
||||
if (empty($counter)) {
|
||||
$this->container[12] = 0;
|
||||
$this->container[13] = 0;
|
||||
} else {
|
||||
$this->container[12] = self::load_4(self::substr($counter, 0, 4));
|
||||
$this->container[13] = self::load_4(self::substr($counter, 4, 4));
|
||||
}
|
||||
$this->container[14] = self::load_4(self::substr($iv, 0, 4));
|
||||
$this->container[15] = self::load_4(self::substr($iv, 4, 4));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $offset
|
||||
* @param int $value
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (!is_int($offset)) {
|
||||
throw new InvalidArgumentException('Expected an integer');
|
||||
}
|
||||
if (!is_int($value)) {
|
||||
throw new InvalidArgumentException('Expected an integer');
|
||||
}
|
||||
$this->container[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return mixed|null
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->container[$offset])
|
||||
? $this->container[$offset]
|
||||
: null;
|
||||
}
|
||||
}
|
38
wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php
Normal file
38
wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_ChaCha20_IetfCtx
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core_ChaCha20_Ctx
|
||||
{
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key ChaCha20 key.
|
||||
* @param string $iv Initialization Vector (a.k.a. nonce).
|
||||
* @param string $counter The initial counter value.
|
||||
* Defaults to 4 0x00 bytes.
|
||||
* @throws InvalidArgumentException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '', $iv = '', $counter = '')
|
||||
{
|
||||
if (self::strlen($iv) !== 12) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.');
|
||||
}
|
||||
parent::__construct($key, self::substr($iv, 0, 8), $counter);
|
||||
|
||||
if (!empty($counter)) {
|
||||
$this->container[12] = self::load_4(self::substr($counter, 0, 4));
|
||||
}
|
||||
$this->container[13] = self::load_4(self::substr($iv, 0, 4));
|
||||
$this->container[14] = self::load_4(self::substr($iv, 4, 4));
|
||||
$this->container[15] = self::load_4(self::substr($iv, 8, 4));
|
||||
}
|
||||
}
|
3002
wp-includes/sodium_compat/src/Core/Curve25519.php
Normal file
3002
wp-includes/sodium_compat/src/Core/Curve25519.php
Normal file
File diff suppressed because it is too large
Load Diff
121
wp-includes/sodium_compat/src/Core/Curve25519/Fe.php
Normal file
121
wp-includes/sodium_compat/src/Core/Curve25519/Fe.php
Normal file
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Fe', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*
|
||||
* This represents a Field Element
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $container = array();
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $size = 10;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param array $array
|
||||
* @param bool $save_indexes
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray($array, $save_indexes = null)
|
||||
{
|
||||
$count = count($array);
|
||||
if ($save_indexes) {
|
||||
$keys = array_keys($array);
|
||||
} else {
|
||||
$keys = range(0, $count - 1);
|
||||
}
|
||||
$array = array_values($array);
|
||||
|
||||
$obj = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
if ($save_indexes) {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$obj->offsetSet($keys[$i], $array[$i]);
|
||||
}
|
||||
} else {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$obj->offsetSet($i, $array[$i]);
|
||||
}
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (!is_int($value)) {
|
||||
throw new InvalidArgumentException('Expected an integer');
|
||||
}
|
||||
if (is_null($offset)) {
|
||||
$this->container[] = $value;
|
||||
} else {
|
||||
$this->container[$offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return mixed|null
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->container[$offset])
|
||||
? $this->container[$offset]
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __debugInfo()
|
||||
{
|
||||
return array(implode(', ', $this->container));
|
||||
}
|
||||
}
|
65
wp-includes/sodium_compat/src/Core/Curve25519/Ge/Cached.php
Normal file
65
wp-includes/sodium_compat/src/Core/Curve25519/Ge/Cached.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Cached', false)) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Ge_Cached
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Ge_Cached
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $YplusX;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $YminusX;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $T2d;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Curve25519_Ge_Cached constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YplusX
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $YminusX
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $Z
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $T2d
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $YplusX = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $YminusX = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $Z = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $T2d = null
|
||||
) {
|
||||
if ($YplusX === null) {
|
||||
$YplusX = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->YplusX = $YplusX;
|
||||
if ($YminusX === null) {
|
||||
$YminusX = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->YminusX = $YminusX;
|
||||
if ($Z === null) {
|
||||
$Z = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $Z;
|
||||
if ($T2d === null) {
|
||||
$T2d = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->T2d = $T2d;
|
||||
}
|
||||
}
|
64
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P1p1.php
Normal file
64
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P1p1.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P1p1', false)) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $T;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Curve25519_Ge_P1p1 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $z = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $t = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $z;
|
||||
if ($t === null) {
|
||||
$t = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->T = $t;
|
||||
}
|
||||
}
|
54
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P2.php
Normal file
54
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P2.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P2', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Ge_P2
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Ge_P2
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Curve25519_Ge_P2 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $z = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $z;
|
||||
}
|
||||
}
|
65
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P3.php
Normal file
65
wp-includes/sodium_compat/src/Core/Curve25519/Ge/P3.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_P3', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Ge_P3
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Ge_P3
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $T;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Curve25519_Ge_P3 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $z
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe|null $t
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $z = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $t = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $z;
|
||||
if ($t === null) {
|
||||
$t = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->T = $t;
|
||||
}
|
||||
}
|
54
wp-includes/sodium_compat/src/Core/Curve25519/Ge/Precomp.php
Normal file
54
wp-includes/sodium_compat/src/Core/Curve25519/Ge/Precomp.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Curve25519_Ge_Precomp', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $yplusx;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $yminusx;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public $xy2d;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Curve25519_Ge_Precomp constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $yplusx
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $yminusx
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $xy2d
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $yplusx = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $yminusx = null,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $xy2d = null
|
||||
) {
|
||||
if ($yplusx === null) {
|
||||
$yplusx = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->yplusx = $yplusx;
|
||||
if ($yminusx === null) {
|
||||
$yminusx = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->yminusx = $yminusx;
|
||||
if ($xy2d === null) {
|
||||
$xy2d = new ParagonIE_Sodium_Core_Curve25519_Fe();
|
||||
}
|
||||
$this->xy2d = $xy2d;
|
||||
}
|
||||
}
|
1467
wp-includes/sodium_compat/src/Core/Curve25519/H.php
Normal file
1467
wp-includes/sodium_compat/src/Core/Curve25519/H.php
Normal file
File diff suppressed because it is too large
Load Diff
3
wp-includes/sodium_compat/src/Core/Curve25519/README.md
Normal file
3
wp-includes/sodium_compat/src/Core/Curve25519/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Curve25519 Data Structures
|
||||
|
||||
These are PHP implementation of the [structs used in the ref10 curve25519 code](https://github.com/jedisct1/libsodium/blob/master/src/libsodium/include/sodium/private/curve25519_ref10.h).
|
480
wp-includes/sodium_compat/src/Core/Ed25519.php
Normal file
480
wp-includes/sodium_compat/src/Core/Ed25519.php
Normal file
@ -0,0 +1,480 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Ed25519
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
|
||||
{
|
||||
const KEYPAIR_BYTES = 96;
|
||||
const SEED_BYTES = 32;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return string (96 bytes)
|
||||
* @throws Exception
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function keypair()
|
||||
{
|
||||
$seed = random_bytes(self::SEED_BYTES);
|
||||
$pk = '';
|
||||
$sk = '';
|
||||
self::seed_keypair($pk, $sk, $seed);
|
||||
return $sk . $pk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $pk
|
||||
* @param string $sk
|
||||
* @param string $seed
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function seed_keypair(&$pk, &$sk, $seed)
|
||||
{
|
||||
if (self::strlen($seed) !== self::SEED_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
|
||||
}
|
||||
|
||||
/** @var string $pk */
|
||||
$pk = self::publickey_from_secretkey($seed);
|
||||
$sk = $seed . $pk;
|
||||
return $sk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function secretkey($keypair)
|
||||
{
|
||||
if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair must be 96 bytes long');
|
||||
}
|
||||
return self::substr($keypair, 0, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function publickey($keypair)
|
||||
{
|
||||
if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair must be 96 bytes long');
|
||||
}
|
||||
return self::substr($keypair, 64, 32);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function publickey_from_secretkey($sk)
|
||||
{
|
||||
/** @var string $sk */
|
||||
$sk = hash('sha512', self::substr($sk, 0, 32), true);
|
||||
$sk[0] = self::intToChr(
|
||||
self::chrToInt($sk[0]) & 248
|
||||
);
|
||||
$sk[31] = self::intToChr(
|
||||
(self::chrToInt($sk[31]) & 63) | 64
|
||||
);
|
||||
return self::sk_to_pk($sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function pk_to_curve25519($pk)
|
||||
{
|
||||
if (self::small_order($pk)) {
|
||||
throw new SodiumException('Public key is on a small order');
|
||||
}
|
||||
$A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32));
|
||||
$p1 = self::ge_mul_l($A);
|
||||
if (!self::fe_isnonzero($p1->X)) {
|
||||
throw new SodiumException('Unexpected zero result');
|
||||
}
|
||||
|
||||
# fe_1(one_minus_y);
|
||||
# fe_sub(one_minus_y, one_minus_y, A.Y);
|
||||
# fe_invert(one_minus_y, one_minus_y);
|
||||
$one_minux_y = self::fe_invert(
|
||||
self::fe_sub(
|
||||
self::fe_1(),
|
||||
$A->Y
|
||||
)
|
||||
);
|
||||
|
||||
# fe_1(x);
|
||||
# fe_add(x, x, A.Y);
|
||||
# fe_mul(x, x, one_minus_y);
|
||||
$x = self::fe_mul(
|
||||
self::fe_add(self::fe_1(), $A->Y),
|
||||
$one_minux_y
|
||||
);
|
||||
|
||||
# fe_tobytes(curve25519_pk, x);
|
||||
return self::fe_tobytes($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sk_to_pk($sk)
|
||||
{
|
||||
return self::ge_p3_tobytes(
|
||||
self::ge_scalarmult_base(
|
||||
self::substr($sk, 0, 32)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign($message, $sk)
|
||||
{
|
||||
/** @var string $signature */
|
||||
$signature = self::sign_detached($message, $sk);
|
||||
return $signature . $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message A signed message
|
||||
* @param string $pk Public key
|
||||
* @return string Message (without signature)
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign_open($message, $pk)
|
||||
{
|
||||
/** @var string $signature */
|
||||
$signature = self::substr($message, 0, 64);
|
||||
|
||||
/** @var string $message */
|
||||
$message = self::substr($message, 64);
|
||||
|
||||
if (self::verify_detached($signature, $message, $pk)) {
|
||||
return $message;
|
||||
}
|
||||
throw new SodiumException('Invalid signature');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign_detached($message, $sk)
|
||||
{
|
||||
# crypto_hash_sha512(az, sk, 32);
|
||||
$az = hash('sha512', self::substr($sk, 0, 32), true);
|
||||
|
||||
# az[0] &= 248;
|
||||
# az[31] &= 63;
|
||||
# az[31] |= 64;
|
||||
$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
|
||||
$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
|
||||
|
||||
# crypto_hash_sha512_init(&hs);
|
||||
# crypto_hash_sha512_update(&hs, az + 32, 32);
|
||||
# crypto_hash_sha512_update(&hs, m, mlen);
|
||||
# crypto_hash_sha512_final(&hs, nonce);
|
||||
$hs = hash_init('sha512');
|
||||
hash_update($hs, self::substr($az, 32, 32));
|
||||
hash_update($hs, $message);
|
||||
$nonceHash = hash_final($hs, true);
|
||||
|
||||
# memmove(sig + 32, sk + 32, 32);
|
||||
$pk = self::substr($sk, 32, 32);
|
||||
|
||||
# sc_reduce(nonce);
|
||||
# ge_scalarmult_base(&R, nonce);
|
||||
# ge_p3_tobytes(sig, &R);
|
||||
$nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
|
||||
$sig = self::ge_p3_tobytes(
|
||||
self::ge_scalarmult_base($nonce)
|
||||
);
|
||||
|
||||
# crypto_hash_sha512_init(&hs);
|
||||
# crypto_hash_sha512_update(&hs, sig, 64);
|
||||
# crypto_hash_sha512_update(&hs, m, mlen);
|
||||
# crypto_hash_sha512_final(&hs, hram);
|
||||
$hs = hash_init('sha512');
|
||||
hash_update($hs, self::substr($sig, 0, 32));
|
||||
hash_update($hs, self::substr($pk, 0, 32));
|
||||
hash_update($hs, $message);
|
||||
$hramHash = hash_final($hs, true);
|
||||
|
||||
# sc_reduce(hram);
|
||||
# sc_muladd(sig + 32, hram, az, nonce);
|
||||
$hram = self::sc_reduce($hramHash);
|
||||
$sigAfter = self::sc_muladd($hram, $az, $nonce);
|
||||
$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
|
||||
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($az);
|
||||
} catch (SodiumException $ex) {
|
||||
$az = null;
|
||||
}
|
||||
return $sig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sig
|
||||
* @param string $message
|
||||
* @param string $pk
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function verify_detached($sig, $message, $pk)
|
||||
{
|
||||
if (self::strlen($sig) < 64) {
|
||||
throw new SodiumException('Signature is too short');
|
||||
}
|
||||
if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
|
||||
throw new SodiumException('S < L - Invalid signature');
|
||||
}
|
||||
if (self::small_order($sig)) {
|
||||
throw new SodiumException('Signature is on too small of an order');
|
||||
}
|
||||
if ((self::chrToInt($sig[63]) & 224) !== 0) {
|
||||
throw new SodiumException('Invalid signature');
|
||||
}
|
||||
$d = 0;
|
||||
for ($i = 0; $i < 32; ++$i) {
|
||||
$d |= self::chrToInt($pk[$i]);
|
||||
}
|
||||
if ($d === 0) {
|
||||
throw new SodiumException('All zero public key');
|
||||
}
|
||||
|
||||
/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
|
||||
$orig = ParagonIE_Sodium_Compat::$fastMult;
|
||||
|
||||
// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
|
||||
ParagonIE_Sodium_Compat::$fastMult = true;
|
||||
|
||||
/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
|
||||
$A = self::ge_frombytes_negate_vartime($pk);
|
||||
|
||||
/** @var string $hDigest */
|
||||
$hDigest = hash(
|
||||
'sha512',
|
||||
self::substr($sig, 0, 32) .
|
||||
self::substr($pk, 0, 32) .
|
||||
$message,
|
||||
true
|
||||
);
|
||||
|
||||
/** @var string $h */
|
||||
$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
|
||||
|
||||
/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
|
||||
$R = self::ge_double_scalarmult_vartime(
|
||||
$h,
|
||||
$A,
|
||||
self::substr($sig, 32)
|
||||
);
|
||||
|
||||
/** @var string $rcheck */
|
||||
$rcheck = self::ge_tobytes($R);
|
||||
|
||||
// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
|
||||
ParagonIE_Sodium_Compat::$fastMult = $orig;
|
||||
|
||||
return self::verify_32($rcheck, self::substr($sig, 0, 32));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $S
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function check_S_lt_L($S)
|
||||
{
|
||||
if (self::strlen($S) < 32) {
|
||||
throw new SodiumException('Signature must be 32 bytes');
|
||||
}
|
||||
$L = array(
|
||||
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
|
||||
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
|
||||
);
|
||||
$c = 0;
|
||||
$n = 1;
|
||||
$i = 32;
|
||||
|
||||
/** @var array<int, int> $L */
|
||||
do {
|
||||
--$i;
|
||||
$x = self::chrToInt($S[$i]);
|
||||
$c |= (
|
||||
(($x - $L[$i]) >> 8) & $n
|
||||
);
|
||||
$n &= (
|
||||
(($x ^ $L[$i]) - 1) >> 8
|
||||
);
|
||||
} while ($i !== 0);
|
||||
|
||||
return $c === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $R
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function small_order($R)
|
||||
{
|
||||
/** @var array<int, array<int, int>> $blacklist */
|
||||
$blacklist = array(
|
||||
/* 0 (order 4) */
|
||||
array(
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
),
|
||||
/* 1 (order 1) */
|
||||
array(
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
),
|
||||
/* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
|
||||
array(
|
||||
0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
|
||||
0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
|
||||
0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
|
||||
0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
|
||||
),
|
||||
/* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
|
||||
array(
|
||||
0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
|
||||
0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
|
||||
0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
|
||||
0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
|
||||
),
|
||||
/* p-1 (order 2) */
|
||||
array(
|
||||
0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
|
||||
0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
|
||||
0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
|
||||
0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
|
||||
),
|
||||
/* p (order 4) */
|
||||
array(
|
||||
0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
|
||||
0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
|
||||
0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
|
||||
0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
|
||||
),
|
||||
/* p+1 (order 1) */
|
||||
array(
|
||||
0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
|
||||
array(
|
||||
0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
|
||||
array(
|
||||
0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* 2p-1 (order 2) */
|
||||
array(
|
||||
0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
),
|
||||
/* 2p (order 4) */
|
||||
array(
|
||||
0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
),
|
||||
/* 2p+1 (order 1) */
|
||||
array(
|
||||
0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
)
|
||||
);
|
||||
/** @var int $countBlacklist */
|
||||
$countBlacklist = count($blacklist);
|
||||
|
||||
for ($i = 0; $i < $countBlacklist; ++$i) {
|
||||
$c = 0;
|
||||
for ($j = 0; $j < 32; ++$j) {
|
||||
$c |= self::chrToInt($R[$j]) ^ (int) $blacklist[$i][$j];
|
||||
}
|
||||
if ($c === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
108
wp-includes/sodium_compat/src/Core/HChaCha20.php
Normal file
108
wp-includes/sodium_compat/src/Core/HChaCha20.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_HChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20
|
||||
{
|
||||
/**
|
||||
* @param string $in
|
||||
* @param string $key
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hChaCha20($in = '', $key = '', $c = null)
|
||||
{
|
||||
$ctx = array();
|
||||
|
||||
if ($c === null) {
|
||||
$ctx[0] = 0x61707865;
|
||||
$ctx[1] = 0x3320646e;
|
||||
$ctx[2] = 0x79622d32;
|
||||
$ctx[3] = 0x6b206574;
|
||||
} else {
|
||||
$ctx[0] = self::load_4(self::substr($c, 0, 4));
|
||||
$ctx[1] = self::load_4(self::substr($c, 4, 4));
|
||||
$ctx[2] = self::load_4(self::substr($c, 8, 4));
|
||||
$ctx[3] = self::load_4(self::substr($c, 12, 4));
|
||||
}
|
||||
$ctx[4] = self::load_4(self::substr($key, 0, 4));
|
||||
$ctx[5] = self::load_4(self::substr($key, 4, 4));
|
||||
$ctx[6] = self::load_4(self::substr($key, 8, 4));
|
||||
$ctx[7] = self::load_4(self::substr($key, 12, 4));
|
||||
$ctx[8] = self::load_4(self::substr($key, 16, 4));
|
||||
$ctx[9] = self::load_4(self::substr($key, 20, 4));
|
||||
$ctx[10] = self::load_4(self::substr($key, 24, 4));
|
||||
$ctx[11] = self::load_4(self::substr($key, 28, 4));
|
||||
$ctx[12] = self::load_4(self::substr($in, 0, 4));
|
||||
$ctx[13] = self::load_4(self::substr($in, 4, 4));
|
||||
$ctx[14] = self::load_4(self::substr($in, 8, 4));
|
||||
$ctx[15] = self::load_4(self::substr($in, 12, 4));
|
||||
return self::hChaCha20Bytes($ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ctx
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function hChaCha20Bytes(array $ctx)
|
||||
{
|
||||
$x0 = (int) $ctx[0];
|
||||
$x1 = (int) $ctx[1];
|
||||
$x2 = (int) $ctx[2];
|
||||
$x3 = (int) $ctx[3];
|
||||
$x4 = (int) $ctx[4];
|
||||
$x5 = (int) $ctx[5];
|
||||
$x6 = (int) $ctx[6];
|
||||
$x7 = (int) $ctx[7];
|
||||
$x8 = (int) $ctx[8];
|
||||
$x9 = (int) $ctx[9];
|
||||
$x10 = (int) $ctx[10];
|
||||
$x11 = (int) $ctx[11];
|
||||
$x12 = (int) $ctx[12];
|
||||
$x13 = (int) $ctx[13];
|
||||
$x14 = (int) $ctx[14];
|
||||
$x15 = (int) $ctx[15];
|
||||
|
||||
for ($i = 0; $i < 10; ++$i) {
|
||||
# QUARTERROUND( x0, x4, x8, x12)
|
||||
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
|
||||
|
||||
# QUARTERROUND( x1, x5, x9, x13)
|
||||
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
|
||||
|
||||
# QUARTERROUND( x2, x6, x10, x14)
|
||||
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
|
||||
|
||||
# QUARTERROUND( x3, x7, x11, x15)
|
||||
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
|
||||
|
||||
# QUARTERROUND( x0, x5, x10, x15)
|
||||
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
|
||||
|
||||
# QUARTERROUND( x1, x6, x11, x12)
|
||||
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
|
||||
|
||||
# QUARTERROUND( x2, x7, x8, x13)
|
||||
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
|
||||
|
||||
# QUARTERROUND( x3, x4, x9, x14)
|
||||
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
|
||||
}
|
||||
|
||||
return self::store32_le((int) ($x0 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x1 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x2 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x3 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x12 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x13 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x14 & 0xffffffff)) .
|
||||
self::store32_le((int) ($x15 & 0xffffffff));
|
||||
}
|
||||
}
|
96
wp-includes/sodium_compat/src/Core/HSalsa20.php
Normal file
96
wp-includes/sodium_compat/src/Core/HSalsa20.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_HSalsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20
|
||||
{
|
||||
/**
|
||||
* Calculate an hsalsa20 hash of a single block
|
||||
*
|
||||
* HSalsa20 doesn't have a counter and will never be used for more than
|
||||
* one block (used to derive a subkey for xsalsa20).
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $k
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hsalsa20($in, $k, $c = null)
|
||||
{
|
||||
if ($c === null) {
|
||||
$x0 = 0x61707865;
|
||||
$x5 = 0x3320646e;
|
||||
$x10 = 0x79622d32;
|
||||
$x15 = 0x6b206574;
|
||||
} else {
|
||||
$x0 = self::load_4(self::substr($c, 0, 4));
|
||||
$x5 = self::load_4(self::substr($c, 4, 4));
|
||||
$x10 = self::load_4(self::substr($c, 8, 4));
|
||||
$x15 = self::load_4(self::substr($c, 12, 4));
|
||||
}
|
||||
$x1 = self::load_4(self::substr($k, 0, 4));
|
||||
$x2 = self::load_4(self::substr($k, 4, 4));
|
||||
$x3 = self::load_4(self::substr($k, 8, 4));
|
||||
$x4 = self::load_4(self::substr($k, 12, 4));
|
||||
$x11 = self::load_4(self::substr($k, 16, 4));
|
||||
$x12 = self::load_4(self::substr($k, 20, 4));
|
||||
$x13 = self::load_4(self::substr($k, 24, 4));
|
||||
$x14 = self::load_4(self::substr($k, 28, 4));
|
||||
$x6 = self::load_4(self::substr($in, 0, 4));
|
||||
$x7 = self::load_4(self::substr($in, 4, 4));
|
||||
$x8 = self::load_4(self::substr($in, 8, 4));
|
||||
$x9 = self::load_4(self::substr($in, 12, 4));
|
||||
|
||||
for ($i = self::ROUNDS; $i > 0; $i -= 2) {
|
||||
$x4 ^= self::rotate($x0 + $x12, 7);
|
||||
$x8 ^= self::rotate($x4 + $x0, 9);
|
||||
$x12 ^= self::rotate($x8 + $x4, 13);
|
||||
$x0 ^= self::rotate($x12 + $x8, 18);
|
||||
$x9 ^= self::rotate($x5 + $x1, 7);
|
||||
$x13 ^= self::rotate($x9 + $x5, 9);
|
||||
$x1 ^= self::rotate($x13 + $x9, 13);
|
||||
$x5 ^= self::rotate($x1 + $x13, 18);
|
||||
$x14 ^= self::rotate($x10 + $x6, 7);
|
||||
$x2 ^= self::rotate($x14 + $x10, 9);
|
||||
$x6 ^= self::rotate($x2 + $x14, 13);
|
||||
$x10 ^= self::rotate($x6 + $x2, 18);
|
||||
$x3 ^= self::rotate($x15 + $x11, 7);
|
||||
$x7 ^= self::rotate($x3 + $x15, 9);
|
||||
$x11 ^= self::rotate($x7 + $x3, 13);
|
||||
$x15 ^= self::rotate($x11 + $x7, 18);
|
||||
$x1 ^= self::rotate($x0 + $x3, 7);
|
||||
$x2 ^= self::rotate($x1 + $x0, 9);
|
||||
$x3 ^= self::rotate($x2 + $x1, 13);
|
||||
$x0 ^= self::rotate($x3 + $x2, 18);
|
||||
$x6 ^= self::rotate($x5 + $x4, 7);
|
||||
$x7 ^= self::rotate($x6 + $x5, 9);
|
||||
$x4 ^= self::rotate($x7 + $x6, 13);
|
||||
$x5 ^= self::rotate($x4 + $x7, 18);
|
||||
$x11 ^= self::rotate($x10 + $x9, 7);
|
||||
$x8 ^= self::rotate($x11 + $x10, 9);
|
||||
$x9 ^= self::rotate($x8 + $x11, 13);
|
||||
$x10 ^= self::rotate($x9 + $x8, 18);
|
||||
$x12 ^= self::rotate($x15 + $x14, 7);
|
||||
$x13 ^= self::rotate($x12 + $x15, 9);
|
||||
$x14 ^= self::rotate($x13 + $x12, 13);
|
||||
$x15 ^= self::rotate($x14 + $x13, 18);
|
||||
}
|
||||
|
||||
return self::store32_le($x0) .
|
||||
self::store32_le($x5) .
|
||||
self::store32_le($x10) .
|
||||
self::store32_le($x15) .
|
||||
self::store32_le($x6) .
|
||||
self::store32_le($x7) .
|
||||
self::store32_le($x8) .
|
||||
self::store32_le($x9);
|
||||
}
|
||||
}
|
63
wp-includes/sodium_compat/src/Core/Poly1305.php
Normal file
63
wp-includes/sodium_compat/src/Core/Poly1305.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Poly1305', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Poly1305
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_Poly1305 extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
const BLOCK_SIZE = 16;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $m
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function onetimeauth($m, $key)
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Key must be 32 bytes long.'
|
||||
);
|
||||
}
|
||||
$state = new ParagonIE_Sodium_Core_Poly1305_State(
|
||||
self::substr($key, 0, 32)
|
||||
);
|
||||
return $state
|
||||
->update($m)
|
||||
->finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $mac
|
||||
* @param string $m
|
||||
* @param string $key
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function onetimeauth_verify($mac, $m, $key)
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Key must be 32 bytes long.'
|
||||
);
|
||||
}
|
||||
$state = new ParagonIE_Sodium_Core_Poly1305_State(
|
||||
self::substr($key, 0, 32)
|
||||
);
|
||||
$calc = $state
|
||||
->update($m)
|
||||
->finish();
|
||||
return self::verify_16($calc, $mac);
|
||||
}
|
||||
}
|
419
wp-includes/sodium_compat/src/Core/Poly1305/State.php
Normal file
419
wp-includes/sodium_compat/src/Core/Poly1305/State.php
Normal file
@ -0,0 +1,419 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Poly1305_State
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected $buffer = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $final = false;
|
||||
|
||||
/**
|
||||
* @var array<int, int>
|
||||
*/
|
||||
public $h;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $leftover = 0;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
public $r;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
public $pad;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_Poly1305_State constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key
|
||||
* @throws InvalidArgumentException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '')
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Poly1305 requires a 32-byte key'
|
||||
);
|
||||
}
|
||||
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
|
||||
$this->r = array(
|
||||
(int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff),
|
||||
(int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03),
|
||||
(int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff),
|
||||
(int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff),
|
||||
(int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff)
|
||||
);
|
||||
|
||||
/* h = 0 */
|
||||
$this->h = array(0, 0, 0, 0, 0);
|
||||
|
||||
/* save pad for later */
|
||||
$this->pad = array(
|
||||
self::load_4(self::substr($key, 16, 4)),
|
||||
self::load_4(self::substr($key, 20, 4)),
|
||||
self::load_4(self::substr($key, 24, 4)),
|
||||
self::load_4(self::substr($key, 28, 4)),
|
||||
);
|
||||
|
||||
$this->leftover = 0;
|
||||
$this->final = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function update($message = '')
|
||||
{
|
||||
$bytes = self::strlen($message);
|
||||
|
||||
/* handle leftover */
|
||||
if ($this->leftover) {
|
||||
$want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover;
|
||||
if ($want > $bytes) {
|
||||
$want = $bytes;
|
||||
}
|
||||
for ($i = 0; $i < $want; ++$i) {
|
||||
$mi = self::chrToInt($message[$i]);
|
||||
$this->buffer[$this->leftover + $i] = $mi;
|
||||
}
|
||||
// We snip off the leftmost bytes.
|
||||
$message = self::substr($message, $want);
|
||||
$bytes = self::strlen($message);
|
||||
$this->leftover += $want;
|
||||
if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
|
||||
// We still don't have enough to run $this->blocks()
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->blocks(
|
||||
static::intArrayToString($this->buffer),
|
||||
ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
$this->leftover = 0;
|
||||
}
|
||||
|
||||
/* process full blocks */
|
||||
if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
|
||||
/** @var int $want */
|
||||
$want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1);
|
||||
if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
|
||||
$block = self::substr($message, 0, $want);
|
||||
if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
|
||||
$this->blocks($block, $want);
|
||||
$message = self::substr($message, $want);
|
||||
$bytes = self::strlen($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* store leftover */
|
||||
if ($bytes) {
|
||||
for ($i = 0; $i < $bytes; ++$i) {
|
||||
$mi = self::chrToInt($message[$i]);
|
||||
$this->buffer[$this->leftover + $i] = $mi;
|
||||
}
|
||||
$this->leftover = (int) $this->leftover + $bytes;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $bytes
|
||||
* @return self
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function blocks($message, $bytes)
|
||||
{
|
||||
if (self::strlen($message) < 16) {
|
||||
$message = str_pad($message, 16, "\x00", STR_PAD_RIGHT);
|
||||
}
|
||||
/** @var int $hibit */
|
||||
$hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */
|
||||
$r0 = (int) $this->r[0];
|
||||
$r1 = (int) $this->r[1];
|
||||
$r2 = (int) $this->r[2];
|
||||
$r3 = (int) $this->r[3];
|
||||
$r4 = (int) $this->r[4];
|
||||
|
||||
$s1 = self::mul($r1, 5, 3);
|
||||
$s2 = self::mul($r2, 5, 3);
|
||||
$s3 = self::mul($r3, 5, 3);
|
||||
$s4 = self::mul($r4, 5, 3);
|
||||
|
||||
$h0 = $this->h[0];
|
||||
$h1 = $this->h[1];
|
||||
$h2 = $this->h[2];
|
||||
$h3 = $this->h[3];
|
||||
$h4 = $this->h[4];
|
||||
|
||||
while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
|
||||
/* h += m[i] */
|
||||
$h0 += self::load_4(self::substr($message, 0, 4)) & 0x3ffffff;
|
||||
$h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff;
|
||||
$h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff;
|
||||
$h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff;
|
||||
$h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit;
|
||||
|
||||
/* h *= r */
|
||||
$d0 = (
|
||||
self::mul($h0, $r0, 25) +
|
||||
self::mul($s4, $h1, 26) +
|
||||
self::mul($s3, $h2, 26) +
|
||||
self::mul($s2, $h3, 26) +
|
||||
self::mul($s1, $h4, 26)
|
||||
);
|
||||
|
||||
$d1 = (
|
||||
self::mul($h0, $r1, 25) +
|
||||
self::mul($h1, $r0, 25) +
|
||||
self::mul($s4, $h2, 26) +
|
||||
self::mul($s3, $h3, 26) +
|
||||
self::mul($s2, $h4, 26)
|
||||
);
|
||||
|
||||
$d2 = (
|
||||
self::mul($h0, $r2, 25) +
|
||||
self::mul($h1, $r1, 25) +
|
||||
self::mul($h2, $r0, 25) +
|
||||
self::mul($s4, $h3, 26) +
|
||||
self::mul($s3, $h4, 26)
|
||||
);
|
||||
|
||||
$d3 = (
|
||||
self::mul($h0, $r3, 25) +
|
||||
self::mul($h1, $r2, 25) +
|
||||
self::mul($h2, $r1, 25) +
|
||||
self::mul($h3, $r0, 25) +
|
||||
self::mul($s4, $h4, 26)
|
||||
);
|
||||
|
||||
$d4 = (
|
||||
self::mul($h0, $r4, 25) +
|
||||
self::mul($h1, $r3, 25) +
|
||||
self::mul($h2, $r2, 25) +
|
||||
self::mul($h3, $r1, 25) +
|
||||
self::mul($h4, $r0, 25)
|
||||
);
|
||||
|
||||
/* (partial) h %= p */
|
||||
/** @var int $c */
|
||||
$c = $d0 >> 26;
|
||||
/** @var int $h0 */
|
||||
$h0 = $d0 & 0x3ffffff;
|
||||
$d1 += $c;
|
||||
|
||||
/** @var int $c */
|
||||
$c = $d1 >> 26;
|
||||
/** @var int $h1 */
|
||||
$h1 = $d1 & 0x3ffffff;
|
||||
$d2 += $c;
|
||||
|
||||
/** @var int $c */
|
||||
$c = $d2 >> 26;
|
||||
/** @var int $h2 */
|
||||
$h2 = $d2 & 0x3ffffff;
|
||||
$d3 += $c;
|
||||
|
||||
/** @var int $c */
|
||||
$c = $d3 >> 26;
|
||||
/** @var int $h3 */
|
||||
$h3 = $d3 & 0x3ffffff;
|
||||
$d4 += $c;
|
||||
|
||||
/** @var int $c */
|
||||
$c = $d4 >> 26;
|
||||
/** @var int $h4 */
|
||||
$h4 = $d4 & 0x3ffffff;
|
||||
$h0 += (int) self::mul($c, 5, 3);
|
||||
|
||||
/** @var int $c */
|
||||
$c = $h0 >> 26;
|
||||
/** @var int $h0 */
|
||||
$h0 &= 0x3ffffff;
|
||||
$h1 += $c;
|
||||
|
||||
// Chop off the left 32 bytes.
|
||||
$message = self::substr(
|
||||
$message,
|
||||
ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
$bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE;
|
||||
}
|
||||
|
||||
$this->h = array(
|
||||
(int) ($h0 & 0xffffffff),
|
||||
(int) ($h1 & 0xffffffff),
|
||||
(int) ($h2 & 0xffffffff),
|
||||
(int) ($h3 & 0xffffffff),
|
||||
(int) ($h4 & 0xffffffff)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function finish()
|
||||
{
|
||||
/* process the remaining block */
|
||||
if ($this->leftover) {
|
||||
$i = $this->leftover;
|
||||
$this->buffer[$i++] = 1;
|
||||
for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) {
|
||||
$this->buffer[$i] = 0;
|
||||
}
|
||||
$this->final = true;
|
||||
$this->blocks(
|
||||
self::substr(
|
||||
static::intArrayToString($this->buffer),
|
||||
0,
|
||||
ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
|
||||
),
|
||||
ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
$h0 = (int) $this->h[0];
|
||||
$h1 = (int) $this->h[1];
|
||||
$h2 = (int) $this->h[2];
|
||||
$h3 = (int) $this->h[3];
|
||||
$h4 = (int) $this->h[4];
|
||||
|
||||
/** @var int $c */
|
||||
$c = $h1 >> 26;
|
||||
/** @var int $h1 */
|
||||
$h1 &= 0x3ffffff;
|
||||
/** @var int $h2 */
|
||||
$h2 += $c;
|
||||
/** @var int $c */
|
||||
$c = $h2 >> 26;
|
||||
/** @var int $h2 */
|
||||
$h2 &= 0x3ffffff;
|
||||
$h3 += $c;
|
||||
/** @var int $c */
|
||||
$c = $h3 >> 26;
|
||||
$h3 &= 0x3ffffff;
|
||||
$h4 += $c;
|
||||
/** @var int $c */
|
||||
$c = $h4 >> 26;
|
||||
$h4 &= 0x3ffffff;
|
||||
/** @var int $h0 */
|
||||
$h0 += self::mul($c, 5, 3);
|
||||
/** @var int $c */
|
||||
$c = $h0 >> 26;
|
||||
/** @var int $h0 */
|
||||
$h0 &= 0x3ffffff;
|
||||
/** @var int $h1 */
|
||||
$h1 += $c;
|
||||
|
||||
/* compute h + -p */
|
||||
/** @var int $g0 */
|
||||
$g0 = $h0 + 5;
|
||||
/** @var int $c */
|
||||
$c = $g0 >> 26;
|
||||
/** @var int $g0 */
|
||||
$g0 &= 0x3ffffff;
|
||||
|
||||
/** @var int $g1 */
|
||||
$g1 = $h1 + $c;
|
||||
/** @var int $c */
|
||||
$c = $g1 >> 26;
|
||||
$g1 &= 0x3ffffff;
|
||||
|
||||
/** @var int $g2 */
|
||||
$g2 = $h2 + $c;
|
||||
/** @var int $c */
|
||||
$c = $g2 >> 26;
|
||||
/** @var int $g2 */
|
||||
$g2 &= 0x3ffffff;
|
||||
|
||||
/** @var int $g3 */
|
||||
$g3 = $h3 + $c;
|
||||
/** @var int $c */
|
||||
$c = $g3 >> 26;
|
||||
/** @var int $g3 */
|
||||
$g3 &= 0x3ffffff;
|
||||
|
||||
/** @var int $g4 */
|
||||
$g4 = ($h4 + $c - (1 << 26)) & 0xffffffff;
|
||||
|
||||
/* select h if h < p, or h + -p if h >= p */
|
||||
/** @var int $mask */
|
||||
$mask = ($g4 >> 31) - 1;
|
||||
|
||||
$g0 &= $mask;
|
||||
$g1 &= $mask;
|
||||
$g2 &= $mask;
|
||||
$g3 &= $mask;
|
||||
$g4 &= $mask;
|
||||
|
||||
/** @var int $mask */
|
||||
$mask = ~$mask & 0xffffffff;
|
||||
/** @var int $h0 */
|
||||
$h0 = ($h0 & $mask) | $g0;
|
||||
/** @var int $h1 */
|
||||
$h1 = ($h1 & $mask) | $g1;
|
||||
/** @var int $h2 */
|
||||
$h2 = ($h2 & $mask) | $g2;
|
||||
/** @var int $h3 */
|
||||
$h3 = ($h3 & $mask) | $g3;
|
||||
/** @var int $h4 */
|
||||
$h4 = ($h4 & $mask) | $g4;
|
||||
|
||||
/* h = h % (2^128) */
|
||||
/** @var int $h0 */
|
||||
$h0 = (($h0) | ($h1 << 26)) & 0xffffffff;
|
||||
/** @var int $h1 */
|
||||
$h1 = (($h1 >> 6) | ($h2 << 20)) & 0xffffffff;
|
||||
/** @var int $h2 */
|
||||
$h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff;
|
||||
/** @var int $h3 */
|
||||
$h3 = (($h3 >> 18) | ($h4 << 8)) & 0xffffffff;
|
||||
|
||||
/* mac = (h + pad) % (2^128) */
|
||||
$f = (int) ($h0 + $this->pad[0]);
|
||||
$h0 = (int) $f;
|
||||
$f = (int) ($h1 + $this->pad[1] + ($f >> 32));
|
||||
$h1 = (int) $f;
|
||||
$f = (int) ($h2 + $this->pad[2] + ($f >> 32));
|
||||
$h2 = (int) $f;
|
||||
$f = (int) ($h3 + $this->pad[3] + ($f >> 32));
|
||||
$h3 = (int) $f;
|
||||
|
||||
return self::store32_le($h0 & 0xffffffff) .
|
||||
self::store32_le($h1 & 0xffffffff) .
|
||||
self::store32_le($h2 & 0xffffffff) .
|
||||
self::store32_le($h3 & 0xffffffff);
|
||||
}
|
||||
}
|
273
wp-includes/sodium_compat/src/Core/Salsa20.php
Normal file
273
wp-includes/sodium_compat/src/Core/Salsa20.php
Normal file
@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Salsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Salsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_Salsa20 extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
const ROUNDS = 20;
|
||||
|
||||
/**
|
||||
* Calculate an salsa20 hash of a single block
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $k
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function core_salsa20($in, $k, $c = null)
|
||||
{
|
||||
if (self::strlen($k) < 32) {
|
||||
throw new RangeException('Key must be 32 bytes long');
|
||||
}
|
||||
if ($c === null) {
|
||||
$j0 = $x0 = 0x61707865;
|
||||
$j5 = $x5 = 0x3320646e;
|
||||
$j10 = $x10 = 0x79622d32;
|
||||
$j15 = $x15 = 0x6b206574;
|
||||
} else {
|
||||
$j0 = $x0 = self::load_4(self::substr($c, 0, 4));
|
||||
$j5 = $x5 = self::load_4(self::substr($c, 4, 4));
|
||||
$j10 = $x10 = self::load_4(self::substr($c, 8, 4));
|
||||
$j15 = $x15 = self::load_4(self::substr($c, 12, 4));
|
||||
}
|
||||
$j1 = $x1 = self::load_4(self::substr($k, 0, 4));
|
||||
$j2 = $x2 = self::load_4(self::substr($k, 4, 4));
|
||||
$j3 = $x3 = self::load_4(self::substr($k, 8, 4));
|
||||
$j4 = $x4 = self::load_4(self::substr($k, 12, 4));
|
||||
$j6 = $x6 = self::load_4(self::substr($in, 0, 4));
|
||||
$j7 = $x7 = self::load_4(self::substr($in, 4, 4));
|
||||
$j8 = $x8 = self::load_4(self::substr($in, 8, 4));
|
||||
$j9 = $x9 = self::load_4(self::substr($in, 12, 4));
|
||||
$j11 = $x11 = self::load_4(self::substr($k, 16, 4));
|
||||
$j12 = $x12 = self::load_4(self::substr($k, 20, 4));
|
||||
$j13 = $x13 = self::load_4(self::substr($k, 24, 4));
|
||||
$j14 = $x14 = self::load_4(self::substr($k, 28, 4));
|
||||
|
||||
for ($i = self::ROUNDS; $i > 0; $i -= 2) {
|
||||
$x4 ^= self::rotate($x0 + $x12, 7);
|
||||
$x8 ^= self::rotate($x4 + $x0, 9);
|
||||
$x12 ^= self::rotate($x8 + $x4, 13);
|
||||
$x0 ^= self::rotate($x12 + $x8, 18);
|
||||
|
||||
$x9 ^= self::rotate($x5 + $x1, 7);
|
||||
$x13 ^= self::rotate($x9 + $x5, 9);
|
||||
$x1 ^= self::rotate($x13 + $x9, 13);
|
||||
$x5 ^= self::rotate($x1 + $x13, 18);
|
||||
|
||||
$x14 ^= self::rotate($x10 + $x6, 7);
|
||||
$x2 ^= self::rotate($x14 + $x10, 9);
|
||||
$x6 ^= self::rotate($x2 + $x14, 13);
|
||||
$x10 ^= self::rotate($x6 + $x2, 18);
|
||||
|
||||
$x3 ^= self::rotate($x15 + $x11, 7);
|
||||
$x7 ^= self::rotate($x3 + $x15, 9);
|
||||
$x11 ^= self::rotate($x7 + $x3, 13);
|
||||
$x15 ^= self::rotate($x11 + $x7, 18);
|
||||
|
||||
$x1 ^= self::rotate($x0 + $x3, 7);
|
||||
$x2 ^= self::rotate($x1 + $x0, 9);
|
||||
$x3 ^= self::rotate($x2 + $x1, 13);
|
||||
$x0 ^= self::rotate($x3 + $x2, 18);
|
||||
|
||||
$x6 ^= self::rotate($x5 + $x4, 7);
|
||||
$x7 ^= self::rotate($x6 + $x5, 9);
|
||||
$x4 ^= self::rotate($x7 + $x6, 13);
|
||||
$x5 ^= self::rotate($x4 + $x7, 18);
|
||||
|
||||
$x11 ^= self::rotate($x10 + $x9, 7);
|
||||
$x8 ^= self::rotate($x11 + $x10, 9);
|
||||
$x9 ^= self::rotate($x8 + $x11, 13);
|
||||
$x10 ^= self::rotate($x9 + $x8, 18);
|
||||
|
||||
$x12 ^= self::rotate($x15 + $x14, 7);
|
||||
$x13 ^= self::rotate($x12 + $x15, 9);
|
||||
$x14 ^= self::rotate($x13 + $x12, 13);
|
||||
$x15 ^= self::rotate($x14 + $x13, 18);
|
||||
}
|
||||
|
||||
$x0 += $j0;
|
||||
$x1 += $j1;
|
||||
$x2 += $j2;
|
||||
$x3 += $j3;
|
||||
$x4 += $j4;
|
||||
$x5 += $j5;
|
||||
$x6 += $j6;
|
||||
$x7 += $j7;
|
||||
$x8 += $j8;
|
||||
$x9 += $j9;
|
||||
$x10 += $j10;
|
||||
$x11 += $j11;
|
||||
$x12 += $j12;
|
||||
$x13 += $j13;
|
||||
$x14 += $j14;
|
||||
$x15 += $j15;
|
||||
|
||||
return self::store32_le($x0) .
|
||||
self::store32_le($x1) .
|
||||
self::store32_le($x2) .
|
||||
self::store32_le($x3) .
|
||||
self::store32_le($x4) .
|
||||
self::store32_le($x5) .
|
||||
self::store32_le($x6) .
|
||||
self::store32_le($x7) .
|
||||
self::store32_le($x8) .
|
||||
self::store32_le($x9) .
|
||||
self::store32_le($x10) .
|
||||
self::store32_le($x11) .
|
||||
self::store32_le($x12) .
|
||||
self::store32_le($x13) .
|
||||
self::store32_le($x14) .
|
||||
self::store32_le($x15);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20($len, $nonce, $key)
|
||||
{
|
||||
if (self::strlen($key) !== 32) {
|
||||
throw new RangeException('Key must be 32 bytes long');
|
||||
}
|
||||
$kcopy = '' . $key;
|
||||
$in = self::substr($nonce, 0, 8) . str_repeat("\0", 8);
|
||||
$c = '';
|
||||
while ($len >= 64) {
|
||||
$c .= self::core_salsa20($in, $kcopy, null);
|
||||
$u = 1;
|
||||
// Internal counter.
|
||||
for ($i = 8; $i < 16; ++$i) {
|
||||
$u += self::chrToInt($in[$i]);
|
||||
$in[$i] = self::intToChr($u & 0xff);
|
||||
$u >>= 8;
|
||||
}
|
||||
$len -= 64;
|
||||
}
|
||||
if ($len > 0) {
|
||||
$c .= self::substr(
|
||||
self::core_salsa20($in, $kcopy, null),
|
||||
0,
|
||||
$len
|
||||
);
|
||||
}
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($kcopy);
|
||||
} catch (SodiumException $ex) {
|
||||
$kcopy = null;
|
||||
}
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $m
|
||||
* @param string $n
|
||||
* @param int $ic
|
||||
* @param string $k
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20_xor_ic($m, $n, $ic, $k)
|
||||
{
|
||||
$mlen = self::strlen($m);
|
||||
if ($mlen < 1) {
|
||||
return '';
|
||||
}
|
||||
$kcopy = self::substr($k, 0, 32);
|
||||
$in = self::substr($n, 0, 8);
|
||||
// Initialize the counter
|
||||
$in .= ParagonIE_Sodium_Core_Util::store64_le($ic);
|
||||
|
||||
$c = '';
|
||||
while ($mlen >= 64) {
|
||||
$block = self::core_salsa20($in, $kcopy, null);
|
||||
$c .= self::xorStrings(
|
||||
self::substr($m, 0, 64),
|
||||
self::substr($block, 0, 64)
|
||||
);
|
||||
$u = 1;
|
||||
for ($i = 8; $i < 16; ++$i) {
|
||||
$u += self::chrToInt($in[$i]);
|
||||
$in[$i] = self::intToChr($u & 0xff);
|
||||
$u >>= 8;
|
||||
}
|
||||
|
||||
$mlen -= 64;
|
||||
$m = self::substr($m, 64);
|
||||
}
|
||||
|
||||
if ($mlen) {
|
||||
$block = self::core_salsa20($in, $kcopy, null);
|
||||
$c .= self::xorStrings(
|
||||
self::substr($m, 0, $mlen),
|
||||
self::substr($block, 0, $mlen)
|
||||
);
|
||||
}
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($block);
|
||||
ParagonIE_Sodium_Compat::memzero($kcopy);
|
||||
} catch (SodiumException $ex) {
|
||||
$block = null;
|
||||
$kcopy = null;
|
||||
}
|
||||
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20_xor($message, $nonce, $key)
|
||||
{
|
||||
return self::xorStrings(
|
||||
$message,
|
||||
self::salsa20(
|
||||
self::strlen($message),
|
||||
$nonce,
|
||||
$key
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $u
|
||||
* @param int $c
|
||||
* @return int
|
||||
*/
|
||||
public static function rotate($u, $c)
|
||||
{
|
||||
$u &= 0xffffffff;
|
||||
$c %= 32;
|
||||
return (int) (0xffffffff & (
|
||||
($u << $c)
|
||||
|
|
||||
($u >> (32 - $c))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
305
wp-includes/sodium_compat/src/Core/SipHash.php
Normal file
305
wp-includes/sodium_compat/src/Core/SipHash.php
Normal file
@ -0,0 +1,305 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_SodiumCompat_Core_SipHash
|
||||
*
|
||||
* Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int[] $v
|
||||
* @return int[]
|
||||
*/
|
||||
public static function sipRound(array $v)
|
||||
{
|
||||
# v0 += v1;
|
||||
list($v[0], $v[1]) = self::add(
|
||||
array($v[0], $v[1]),
|
||||
array($v[2], $v[3])
|
||||
);
|
||||
|
||||
# v1=ROTL(v1,13);
|
||||
list($v[2], $v[3]) = self::rotl_64($v[2], $v[3], 13);
|
||||
|
||||
# v1 ^= v0;
|
||||
$v[2] ^= $v[0];
|
||||
$v[3] ^= $v[1];
|
||||
|
||||
# v0=ROTL(v0,32);
|
||||
list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32);
|
||||
|
||||
# v2 += v3;
|
||||
list($v[4], $v[5]) = self::add(
|
||||
array($v[4], $v[5]),
|
||||
array($v[6], $v[7])
|
||||
);
|
||||
|
||||
# v3=ROTL(v3,16);
|
||||
list($v[6], $v[7]) = self::rotl_64($v[6], $v[7], 16);
|
||||
|
||||
# v3 ^= v2;
|
||||
$v[6] ^= $v[4];
|
||||
$v[7] ^= $v[5];
|
||||
|
||||
# v0 += v3;
|
||||
list($v[0], $v[1]) = self::add(
|
||||
array((int) $v[0], (int) $v[1]),
|
||||
array((int) $v[6], (int) $v[7])
|
||||
);
|
||||
|
||||
# v3=ROTL(v3,21);
|
||||
list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21);
|
||||
|
||||
# v3 ^= v0;
|
||||
$v[6] ^= $v[0];
|
||||
$v[7] ^= $v[1];
|
||||
|
||||
# v2 += v1;
|
||||
list($v[4], $v[5]) = self::add(
|
||||
array((int) $v[4], (int) $v[5]),
|
||||
array((int) $v[2], (int) $v[3])
|
||||
);
|
||||
|
||||
# v1=ROTL(v1,17);
|
||||
list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17);
|
||||
|
||||
# v1 ^= v2;;
|
||||
$v[2] ^= $v[4];
|
||||
$v[3] ^= $v[5];
|
||||
|
||||
# v2=ROTL(v2,32)
|
||||
list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32);
|
||||
|
||||
return $v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add two 32 bit integers representing a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int[] $a
|
||||
* @param int[] $b
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public static function add(array $a, array $b)
|
||||
{
|
||||
/** @var int $x1 */
|
||||
$x1 = $a[1] + $b[1];
|
||||
/** @var int $c */
|
||||
$c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
|
||||
/** @var int $x0 */
|
||||
$x0 = $a[0] + $b[0] + $c;
|
||||
return array(
|
||||
$x0 & 0xffffffff,
|
||||
$x1 & 0xffffffff
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int0
|
||||
* @param int $int1
|
||||
* @param int $c
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public static function rotl_64($int0, $int1, $c)
|
||||
{
|
||||
$int0 &= 0xffffffff;
|
||||
$int1 &= 0xffffffff;
|
||||
$c &= 63;
|
||||
if ($c === 32) {
|
||||
return array($int1, $int0);
|
||||
}
|
||||
if ($c > 31) {
|
||||
$tmp = $int1;
|
||||
$int1 = $int0;
|
||||
$int0 = $tmp;
|
||||
$c &= 31;
|
||||
}
|
||||
if ($c === 0) {
|
||||
return array($int0, $int1);
|
||||
}
|
||||
return array(
|
||||
0xffffffff & (
|
||||
($int0 << $c)
|
||||
|
|
||||
($int1 >> (32 - $c))
|
||||
),
|
||||
0xffffffff & (
|
||||
($int1 << $c)
|
||||
|
|
||||
($int0 >> (32 - $c))
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Siphash-2-4 using only 32-bit numbers.
|
||||
*
|
||||
* When we split an int into two, the higher bits go to the lower index.
|
||||
* e.g. 0xDEADBEEFAB10C92D becomes [
|
||||
* 0 => 0xDEADBEEF,
|
||||
* 1 => 0xAB10C92D
|
||||
* ].
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sipHash24($in, $key)
|
||||
{
|
||||
$inlen = self::strlen($in);
|
||||
|
||||
# /* "somepseudorandomlygeneratedbytes" */
|
||||
# u64 v0 = 0x736f6d6570736575ULL;
|
||||
# u64 v1 = 0x646f72616e646f6dULL;
|
||||
# u64 v2 = 0x6c7967656e657261ULL;
|
||||
# u64 v3 = 0x7465646279746573ULL;
|
||||
$v = array(
|
||||
0x736f6d65, // 0
|
||||
0x70736575, // 1
|
||||
0x646f7261, // 2
|
||||
0x6e646f6d, // 3
|
||||
0x6c796765, // 4
|
||||
0x6e657261, // 5
|
||||
0x74656462, // 6
|
||||
0x79746573 // 7
|
||||
);
|
||||
// v0 => $v[0], $v[1]
|
||||
// v1 => $v[2], $v[3]
|
||||
// v2 => $v[4], $v[5]
|
||||
// v3 => $v[6], $v[7]
|
||||
|
||||
# u64 k0 = LOAD64_LE( k );
|
||||
# u64 k1 = LOAD64_LE( k + 8 );
|
||||
$k = array(
|
||||
self::load_4(self::substr($key, 4, 4)),
|
||||
self::load_4(self::substr($key, 0, 4)),
|
||||
self::load_4(self::substr($key, 12, 4)),
|
||||
self::load_4(self::substr($key, 8, 4))
|
||||
);
|
||||
// k0 => $k[0], $k[1]
|
||||
// k1 => $k[2], $k[3]
|
||||
|
||||
# b = ( ( u64 )inlen ) << 56;
|
||||
$b = array(
|
||||
$inlen << 24,
|
||||
0
|
||||
);
|
||||
// See docblock for why the 0th index gets the higher bits.
|
||||
|
||||
# v3 ^= k1;
|
||||
$v[6] ^= $k[2];
|
||||
$v[7] ^= $k[3];
|
||||
# v2 ^= k0;
|
||||
$v[4] ^= $k[0];
|
||||
$v[5] ^= $k[1];
|
||||
# v1 ^= k1;
|
||||
$v[2] ^= $k[2];
|
||||
$v[3] ^= $k[3];
|
||||
# v0 ^= k0;
|
||||
$v[0] ^= $k[0];
|
||||
$v[1] ^= $k[1];
|
||||
|
||||
$left = $inlen;
|
||||
# for ( ; in != end; in += 8 )
|
||||
while ($left >= 8) {
|
||||
# m = LOAD64_LE( in );
|
||||
$m = array(
|
||||
self::load_4(self::substr($in, 4, 4)),
|
||||
self::load_4(self::substr($in, 0, 4))
|
||||
);
|
||||
|
||||
# v3 ^= m;
|
||||
$v[6] ^= $m[0];
|
||||
$v[7] ^= $m[1];
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# v0 ^= m;
|
||||
$v[0] ^= $m[0];
|
||||
$v[1] ^= $m[1];
|
||||
|
||||
$in = self::substr($in, 8);
|
||||
$left -= 8;
|
||||
}
|
||||
|
||||
# switch( left )
|
||||
# {
|
||||
# case 7: b |= ( ( u64 )in[ 6] ) << 48;
|
||||
# case 6: b |= ( ( u64 )in[ 5] ) << 40;
|
||||
# case 5: b |= ( ( u64 )in[ 4] ) << 32;
|
||||
# case 4: b |= ( ( u64 )in[ 3] ) << 24;
|
||||
# case 3: b |= ( ( u64 )in[ 2] ) << 16;
|
||||
# case 2: b |= ( ( u64 )in[ 1] ) << 8;
|
||||
# case 1: b |= ( ( u64 )in[ 0] ); break;
|
||||
# case 0: break;
|
||||
# }
|
||||
switch ($left) {
|
||||
case 7:
|
||||
$b[0] |= self::chrToInt($in[6]) << 16;
|
||||
case 6:
|
||||
$b[0] |= self::chrToInt($in[5]) << 8;
|
||||
case 5:
|
||||
$b[0] |= self::chrToInt($in[4]);
|
||||
case 4:
|
||||
$b[1] |= self::chrToInt($in[3]) << 24;
|
||||
case 3:
|
||||
$b[1] |= self::chrToInt($in[2]) << 16;
|
||||
case 2:
|
||||
$b[1] |= self::chrToInt($in[1]) << 8;
|
||||
case 1:
|
||||
$b[1] |= self::chrToInt($in[0]);
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
// See docblock for why the 0th index gets the higher bits.
|
||||
|
||||
# v3 ^= b;
|
||||
$v[6] ^= $b[0];
|
||||
$v[7] ^= $b[1];
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# v0 ^= b;
|
||||
$v[0] ^= $b[0];
|
||||
$v[1] ^= $b[1];
|
||||
|
||||
// Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
|
||||
# v2 ^= 0xff;
|
||||
$v[5] ^= 0xff;
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# b = v0 ^ v1 ^ v2 ^ v3;
|
||||
# STORE64_LE( out, b );
|
||||
return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
|
||||
self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
|
||||
}
|
||||
}
|
921
wp-includes/sodium_compat/src/Core/Util.php
Normal file
921
wp-includes/sodium_compat/src/Core/Util.php
Normal file
@ -0,0 +1,921 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_Util', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Util
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* @param int $integer
|
||||
* @param int $size (16, 32, 64)
|
||||
* @return int
|
||||
*/
|
||||
public static function abs($integer, $size = 0)
|
||||
{
|
||||
/** @var int $realSize */
|
||||
$realSize = (PHP_INT_SIZE << 3) - 1;
|
||||
if ($size) {
|
||||
--$size;
|
||||
} else {
|
||||
/** @var int $size */
|
||||
$size = $realSize;
|
||||
}
|
||||
|
||||
$negative = -(($integer >> $size) & 1);
|
||||
return (int) (
|
||||
($integer ^ $negative)
|
||||
+
|
||||
(($negative >> $realSize) & 1)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a binary string into a hexadecimal string without cache-timing
|
||||
* leaks
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $binaryString (raw binary)
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function bin2hex($binaryString)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($binaryString)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
|
||||
}
|
||||
|
||||
$hex = '';
|
||||
$len = self::strlen($binaryString);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = unpack('C', $binaryString[$i]);
|
||||
/** @var int $c */
|
||||
$c = $chunk[1] & 0xf;
|
||||
/** @var int $b */
|
||||
$b = $chunk[1] >> 4;
|
||||
$hex .= pack(
|
||||
'CC',
|
||||
(87 + $b + ((($b - 10) >> 8) & ~38)),
|
||||
(87 + $c + ((($c - 10) >> 8) & ~38))
|
||||
);
|
||||
}
|
||||
return $hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a binary string into a hexadecimal string without cache-timing
|
||||
* leaks, returning uppercase letters (as per RFC 4648)
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $bin_string (raw binary)
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function bin2hexUpper($bin_string)
|
||||
{
|
||||
$hex = '';
|
||||
$len = self::strlen($bin_string);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = unpack('C', $bin_string[$i]);
|
||||
/**
|
||||
* Lower 16 bits
|
||||
*
|
||||
* @var int $c
|
||||
*/
|
||||
$c = $chunk[1] & 0xf;
|
||||
|
||||
/**
|
||||
* Upper 16 bits
|
||||
* @var int $b
|
||||
*/
|
||||
$b = $chunk[1] >> 4;
|
||||
|
||||
/**
|
||||
* Use pack() and binary operators to turn the two integers
|
||||
* into hexadecimal characters. We don't use chr() here, because
|
||||
* it uses a lookup table internally and we want to avoid
|
||||
* cache-timing side-channels.
|
||||
*/
|
||||
$hex .= pack(
|
||||
'CC',
|
||||
(55 + $b + ((($b - 10) >> 8) & ~6)),
|
||||
(55 + $c + ((($c - 10) >> 8) & ~6))
|
||||
);
|
||||
}
|
||||
return $hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache-timing-safe variant of ord()
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $chr
|
||||
* @return int
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function chrToInt($chr)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($chr)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.');
|
||||
}
|
||||
if (self::strlen($chr) !== 1) {
|
||||
throw new SodiumException('chrToInt() expects a string that is exactly 1 character long');
|
||||
}
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = unpack('C', $chr);
|
||||
return (int) ($chunk[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two strings.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $left
|
||||
* @param string $right
|
||||
* @param int $len
|
||||
* @return int
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function compare($left, $right, $len = null)
|
||||
{
|
||||
$leftLen = self::strlen($left);
|
||||
$rightLen = self::strlen($right);
|
||||
if ($len === null) {
|
||||
$len = max($leftLen, $rightLen);
|
||||
$left = str_pad($left, $len, "\x00", STR_PAD_RIGHT);
|
||||
$right = str_pad($right, $len, "\x00", STR_PAD_RIGHT);
|
||||
}
|
||||
|
||||
$gt = 0;
|
||||
$eq = 1;
|
||||
$i = $len;
|
||||
while ($i !== 0) {
|
||||
--$i;
|
||||
$gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq;
|
||||
$eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8;
|
||||
}
|
||||
return ($gt + $gt + $eq) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a variable does not match a given type, throw a TypeError.
|
||||
*
|
||||
* @param mixed $mixedVar
|
||||
* @param string $type
|
||||
* @param int $argumentIndex
|
||||
* @throws TypeError
|
||||
* @throws SodiumException
|
||||
* @return void
|
||||
*/
|
||||
public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0)
|
||||
{
|
||||
if (func_num_args() === 0) {
|
||||
/* Tautology, by default */
|
||||
return;
|
||||
}
|
||||
if (func_num_args() === 1) {
|
||||
throw new TypeError('Declared void, but passed a variable');
|
||||
}
|
||||
$realType = strtolower(gettype($mixedVar));
|
||||
$type = strtolower($type);
|
||||
switch ($type) {
|
||||
case 'null':
|
||||
if ($mixedVar !== null) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.');
|
||||
}
|
||||
break;
|
||||
case 'integer':
|
||||
case 'int':
|
||||
$allow = array('int', 'integer');
|
||||
if (!in_array($type, $allow)) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.');
|
||||
}
|
||||
$mixedVar = (int) $mixedVar;
|
||||
break;
|
||||
case 'boolean':
|
||||
case 'bool':
|
||||
$allow = array('bool', 'boolean');
|
||||
if (!in_array($type, $allow)) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.');
|
||||
}
|
||||
$mixedVar = (bool) $mixedVar;
|
||||
break;
|
||||
case 'string':
|
||||
if (!is_string($mixedVar)) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.');
|
||||
}
|
||||
$mixedVar = (string) $mixedVar;
|
||||
break;
|
||||
case 'decimal':
|
||||
case 'double':
|
||||
case 'float':
|
||||
$allow = array('decimal', 'double', 'float');
|
||||
if (!in_array($type, $allow)) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.');
|
||||
}
|
||||
$mixedVar = (float) $mixedVar;
|
||||
break;
|
||||
case 'object':
|
||||
if (!is_object($mixedVar)) {
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.');
|
||||
}
|
||||
break;
|
||||
case 'array':
|
||||
if (!is_array($mixedVar)) {
|
||||
if (is_object($mixedVar)) {
|
||||
if ($mixedVar instanceof ArrayAccess) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate whether or not two strings are equal (in constant-time)
|
||||
*
|
||||
* @param string $left
|
||||
* @param string $right
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hashEquals($left, $right)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($left)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.');
|
||||
}
|
||||
if (!is_string($right)) {
|
||||
throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.');
|
||||
}
|
||||
|
||||
if (is_callable('hash_equals')) {
|
||||
return hash_equals($left, $right);
|
||||
}
|
||||
$d = 0;
|
||||
/** @var int $len */
|
||||
$len = self::strlen($left);
|
||||
if ($len !== self::strlen($right)) {
|
||||
return false;
|
||||
}
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
$d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]);
|
||||
}
|
||||
|
||||
if ($d !== 0) {
|
||||
return false;
|
||||
}
|
||||
return $left === $right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a hexadecimal string into a binary string without cache-timing
|
||||
* leaks
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $hexString
|
||||
* @param bool $strictPadding
|
||||
* @return string (raw binary)
|
||||
* @throws RangeException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hex2bin($hexString, $strictPadding = false)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($hexString)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.');
|
||||
}
|
||||
|
||||
/** @var int $hex_pos */
|
||||
$hex_pos = 0;
|
||||
/** @var string $bin */
|
||||
$bin = '';
|
||||
/** @var int $c_acc */
|
||||
$c_acc = 0;
|
||||
/** @var int $hex_len */
|
||||
$hex_len = self::strlen($hexString);
|
||||
/** @var int $state */
|
||||
$state = 0;
|
||||
if (($hex_len & 1) !== 0) {
|
||||
if ($strictPadding) {
|
||||
throw new RangeException(
|
||||
'Expected an even number of hexadecimal characters'
|
||||
);
|
||||
} else {
|
||||
$hexString = '0' . $hexString;
|
||||
++$hex_len;
|
||||
}
|
||||
}
|
||||
|
||||
$chunk = unpack('C*', $hexString);
|
||||
while ($hex_pos < $hex_len) {
|
||||
++$hex_pos;
|
||||
/** @var int $c */
|
||||
$c = $chunk[$hex_pos];
|
||||
/** @var int $c_num */
|
||||
$c_num = $c ^ 48;
|
||||
/** @var int $c_num0 */
|
||||
$c_num0 = ($c_num - 10) >> 8;
|
||||
/** @var int $c_alpha */
|
||||
$c_alpha = ($c & ~32) - 55;
|
||||
/** @var int $c_alpha0 */
|
||||
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
|
||||
if (($c_num0 | $c_alpha0) === 0) {
|
||||
throw new RangeException(
|
||||
'hex2bin() only expects hexadecimal characters'
|
||||
);
|
||||
}
|
||||
/** @var int $c_val */
|
||||
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
|
||||
if ($state === 0) {
|
||||
$c_acc = $c_val * 16;
|
||||
} else {
|
||||
$bin .= pack('C', $c_acc | $c_val);
|
||||
}
|
||||
$state ^= 1;
|
||||
}
|
||||
return $bin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an array of integers into a string
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param array<int, int> $ints
|
||||
* @return string
|
||||
*/
|
||||
public static function intArrayToString(array $ints)
|
||||
{
|
||||
/** @var array<int, int> $args */
|
||||
$args = $ints;
|
||||
foreach ($args as $i => $v) {
|
||||
$args[$i] = (int) ($v & 0xff);
|
||||
}
|
||||
array_unshift($args, str_repeat('C', count($ints)));
|
||||
return (string) (call_user_func_array('pack', $args));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache-timing-safe variant of ord()
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function intToChr($int)
|
||||
{
|
||||
return pack('C', $int);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a 3 character substring into an integer
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
* @throws RangeException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function load_3($string)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($string)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
|
||||
}
|
||||
|
||||
/* Input validation: */
|
||||
if (self::strlen($string) < 3) {
|
||||
throw new RangeException(
|
||||
'String must be 3 bytes or more; ' . self::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
/** @var array<int, int> $unpacked */
|
||||
$unpacked = unpack('V', $string . "\0");
|
||||
return (int) ($unpacked[1] & 0xffffff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a 4 character substring into an integer
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
* @throws RangeException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function load_4($string)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($string)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
|
||||
}
|
||||
|
||||
/* Input validation: */
|
||||
if (self::strlen($string) < 4) {
|
||||
throw new RangeException(
|
||||
'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
/** @var array<int, int> $unpacked */
|
||||
$unpacked = unpack('V', $string);
|
||||
return (int) ($unpacked[1] & 0xffffffff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a 8 character substring into an integer
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
* @throws RangeException
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function load64_le($string)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($string)) {
|
||||
throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
|
||||
}
|
||||
|
||||
/* Input validation: */
|
||||
if (self::strlen($string) < 4) {
|
||||
throw new RangeException(
|
||||
'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) {
|
||||
/** @var array<int, int> $unpacked */
|
||||
$unpacked = unpack('P', $string);
|
||||
return (int) $unpacked[1];
|
||||
}
|
||||
|
||||
/** @var int $result */
|
||||
$result = (self::chrToInt($string[0]) & 0xff);
|
||||
$result |= (self::chrToInt($string[1]) & 0xff) << 8;
|
||||
$result |= (self::chrToInt($string[2]) & 0xff) << 16;
|
||||
$result |= (self::chrToInt($string[3]) & 0xff) << 24;
|
||||
$result |= (self::chrToInt($string[4]) & 0xff) << 32;
|
||||
$result |= (self::chrToInt($string[5]) & 0xff) << 40;
|
||||
$result |= (self::chrToInt($string[6]) & 0xff) << 48;
|
||||
$result |= (self::chrToInt($string[7]) & 0xff) << 56;
|
||||
return (int) $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $left
|
||||
* @param string $right
|
||||
* @return int
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function memcmp($left, $right)
|
||||
{
|
||||
if (self::hashEquals($left, $right)) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply two integers in constant-time
|
||||
*
|
||||
* Micro-architecture timing side-channels caused by how your CPU
|
||||
* implements multiplication are best prevented by never using the
|
||||
* multiplication operators and ensuring that our code always takes
|
||||
* the same number of operations to complete, regardless of the values
|
||||
* of $a and $b.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $a
|
||||
* @param int $b
|
||||
* @param int $size Limits the number of operations (useful for small,
|
||||
* constant operands)
|
||||
* @return int
|
||||
*/
|
||||
public static function mul($a, $b, $size = 0)
|
||||
{
|
||||
if (ParagonIE_Sodium_Compat::$fastMult) {
|
||||
return (int) ($a * $b);
|
||||
}
|
||||
|
||||
static $defaultSize = null;
|
||||
/** @var int $defaultSize */
|
||||
if (!$defaultSize) {
|
||||
/** @var int $defaultSize */
|
||||
$defaultSize = (PHP_INT_SIZE << 3) - 1;
|
||||
}
|
||||
if ($size < 1) {
|
||||
/** @var int $size */
|
||||
$size = $defaultSize;
|
||||
}
|
||||
/** @var int $size */
|
||||
|
||||
$c = 0;
|
||||
|
||||
/**
|
||||
* Mask is either -1 or 0.
|
||||
*
|
||||
* -1 in binary looks like 0x1111 ... 1111
|
||||
* 0 in binary looks like 0x0000 ... 0000
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
$mask = -(($b >> ((int) $defaultSize)) & 1);
|
||||
|
||||
/**
|
||||
* Ensure $b is a positive integer, without creating
|
||||
* a branching side-channel
|
||||
*
|
||||
* @var int $b
|
||||
*/
|
||||
$b = ($b & ~$mask) | ($mask & -$b);
|
||||
|
||||
/**
|
||||
* Unless $size is provided:
|
||||
*
|
||||
* This loop always runs 32 times when PHP_INT_SIZE is 4.
|
||||
* This loop always runs 64 times when PHP_INT_SIZE is 8.
|
||||
*/
|
||||
for ($i = $size; $i >= 0; --$i) {
|
||||
$c += (int) ($a & -($b & 1));
|
||||
$a <<= 1;
|
||||
$b >>= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* If $b was negative, we then apply the same value to $c here.
|
||||
* It doesn't matter much if $a was negative; the $c += above would
|
||||
* have produced a negative integer to begin with. But a negative $b
|
||||
* makes $b >>= 1 never return 0, so we would end up with incorrect
|
||||
* results.
|
||||
*
|
||||
* The end result is what we'd expect from integer multiplication.
|
||||
*/
|
||||
return (int) (($c & ~$mask) | ($mask & -$c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert any arbitrary numbers into two 32-bit integers that represent
|
||||
* a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int|float $num
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public static function numericTo64BitInteger($num)
|
||||
{
|
||||
$high = 0;
|
||||
/** @var int $low */
|
||||
$low = $num & 0xffffffff;
|
||||
|
||||
if ((+(abs($num))) >= 1) {
|
||||
if ($num > 0) {
|
||||
/** @var int $high */
|
||||
$high = min((+(floor($num/4294967296))), 4294967295);
|
||||
} else {
|
||||
/** @var int $high */
|
||||
$high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296))));
|
||||
}
|
||||
}
|
||||
return array((int) $high, (int) $low);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a 24-bit integer into a string, treating it as big-endian.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function store_3($int)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_int($int)) {
|
||||
if (is_numeric($int)) {
|
||||
$int = (int) $int;
|
||||
} else {
|
||||
throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
|
||||
}
|
||||
}
|
||||
/** @var string $packed */
|
||||
$packed = pack('N', $int);
|
||||
return self::substr($packed, 1, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a 32-bit integer into a string, treating it as little-endian.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function store32_le($int)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_int($int)) {
|
||||
if (is_numeric($int)) {
|
||||
$int = (int) $int;
|
||||
} else {
|
||||
throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
|
||||
}
|
||||
}
|
||||
|
||||
/** @var string $packed */
|
||||
$packed = pack('V', $int);
|
||||
return $packed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a 32-bit integer into a string, treating it as big-endian.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function store_4($int)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_int($int)) {
|
||||
if (is_numeric($int)) {
|
||||
$int = (int) $int;
|
||||
} else {
|
||||
throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
|
||||
}
|
||||
}
|
||||
|
||||
/** @var string $packed */
|
||||
$packed = pack('N', $int);
|
||||
return $packed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a 64-bit integer as an string, treating it as little-endian.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $int
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function store64_le($int)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_int($int)) {
|
||||
if (is_numeric($int)) {
|
||||
$int = (int) $int;
|
||||
} else {
|
||||
throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_INT_SIZE === 8) {
|
||||
if (PHP_VERSION_ID >= 50603) {
|
||||
/** @var string $packed */
|
||||
$packed = pack('P', $int);
|
||||
return $packed;
|
||||
}
|
||||
return self::intToChr($int & 0xff) .
|
||||
self::intToChr(($int >> 8) & 0xff) .
|
||||
self::intToChr(($int >> 16) & 0xff) .
|
||||
self::intToChr(($int >> 24) & 0xff) .
|
||||
self::intToChr(($int >> 32) & 0xff) .
|
||||
self::intToChr(($int >> 40) & 0xff) .
|
||||
self::intToChr(($int >> 48) & 0xff) .
|
||||
self::intToChr(($int >> 56) & 0xff);
|
||||
}
|
||||
if ($int > PHP_INT_MAX) {
|
||||
list($hiB, $int) = self::numericTo64BitInteger($int);
|
||||
} else {
|
||||
$hiB = 0;
|
||||
}
|
||||
return
|
||||
self::intToChr(($int ) & 0xff) .
|
||||
self::intToChr(($int >> 8) & 0xff) .
|
||||
self::intToChr(($int >> 16) & 0xff) .
|
||||
self::intToChr(($int >> 24) & 0xff) .
|
||||
self::intToChr($hiB & 0xff) .
|
||||
self::intToChr(($hiB >> 8) & 0xff) .
|
||||
self::intToChr(($hiB >> 16) & 0xff) .
|
||||
self::intToChr(($hiB >> 24) & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe string length
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @ref mbstring.func_overload
|
||||
*
|
||||
* @param string $str
|
||||
* @return int
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function strlen($str)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($str)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
|
||||
return (int) (
|
||||
self::isMbStringOverride()
|
||||
? mb_strlen($str, '8bit')
|
||||
: strlen($str)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a string into an array of integers
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return array<int, int>
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function stringToIntArray($string)
|
||||
{
|
||||
if (!is_string($string)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
/**
|
||||
* @var array<int, int>
|
||||
*/
|
||||
$values = array_values(
|
||||
unpack('C*', $string)
|
||||
);
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe substring
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @ref mbstring.func_overload
|
||||
*
|
||||
* @param string $str
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function substr($str, $start = 0, $length = null)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($str)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
|
||||
if ($length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (self::isMbStringOverride()) {
|
||||
if (PHP_VERSION_ID < 50400 && $length === null) {
|
||||
$length = self::strlen($str);
|
||||
}
|
||||
$sub = (string) mb_substr($str, $start, $length, '8bit');
|
||||
} elseif ($length === null) {
|
||||
$sub = (string) substr($str, $start);
|
||||
} else {
|
||||
$sub = (string) substr($str, $start, $length);
|
||||
}
|
||||
if (isset($sub)) {
|
||||
return $sub;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare a 16-character byte string in constant time.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function verify_16($a, $b)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($a)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
if (!is_string($b)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
return self::hashEquals(
|
||||
self::substr($a, 0, 16),
|
||||
self::substr($b, 0, 16)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare a 32-character byte string in constant time.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function verify_32($a, $b)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($a)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
if (!is_string($b)) {
|
||||
throw new TypeError('String expected');
|
||||
}
|
||||
return self::hashEquals(
|
||||
self::substr($a, 0, 32),
|
||||
self::substr($b, 0, 32)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate $a ^ $b for two strings.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xorStrings($a, $b)
|
||||
{
|
||||
/* Type checks: */
|
||||
if (!is_string($a)) {
|
||||
throw new TypeError('Argument 1 must be a string');
|
||||
}
|
||||
if (!is_string($b)) {
|
||||
throw new TypeError('Argument 2 must be a string');
|
||||
}
|
||||
|
||||
return (string) ($a ^ $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not mbstring.func_overload is in effect.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isMbStringOverride()
|
||||
{
|
||||
static $mbstring = null;
|
||||
|
||||
if ($mbstring === null) {
|
||||
$mbstring = extension_loaded('mbstring')
|
||||
&&
|
||||
((int) (ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING);
|
||||
}
|
||||
/** @var bool $mbstring */
|
||||
|
||||
return $mbstring;
|
||||
}
|
||||
}
|
327
wp-includes/sodium_compat/src/Core/X25519.php
Normal file
327
wp-includes/sodium_compat/src/Core/X25519.php
Normal file
@ -0,0 +1,327 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_X25519', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_X25519
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_X25519 extends ParagonIE_Sodium_Core_Curve25519
|
||||
{
|
||||
/**
|
||||
* Alters the objects passed to this method in place.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $f
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $g
|
||||
* @param int $b
|
||||
* @return void
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
public static function fe_cswap(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $f,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $g,
|
||||
$b = 0
|
||||
) {
|
||||
$f0 = (int) $f[0];
|
||||
$f1 = (int) $f[1];
|
||||
$f2 = (int) $f[2];
|
||||
$f3 = (int) $f[3];
|
||||
$f4 = (int) $f[4];
|
||||
$f5 = (int) $f[5];
|
||||
$f6 = (int) $f[6];
|
||||
$f7 = (int) $f[7];
|
||||
$f8 = (int) $f[8];
|
||||
$f9 = (int) $f[9];
|
||||
$g0 = (int) $g[0];
|
||||
$g1 = (int) $g[1];
|
||||
$g2 = (int) $g[2];
|
||||
$g3 = (int) $g[3];
|
||||
$g4 = (int) $g[4];
|
||||
$g5 = (int) $g[5];
|
||||
$g6 = (int) $g[6];
|
||||
$g7 = (int) $g[7];
|
||||
$g8 = (int) $g[8];
|
||||
$g9 = (int) $g[9];
|
||||
$b = -$b;
|
||||
$x0 = ($f0 ^ $g0) & $b;
|
||||
$x1 = ($f1 ^ $g1) & $b;
|
||||
$x2 = ($f2 ^ $g2) & $b;
|
||||
$x3 = ($f3 ^ $g3) & $b;
|
||||
$x4 = ($f4 ^ $g4) & $b;
|
||||
$x5 = ($f5 ^ $g5) & $b;
|
||||
$x6 = ($f6 ^ $g6) & $b;
|
||||
$x7 = ($f7 ^ $g7) & $b;
|
||||
$x8 = ($f8 ^ $g8) & $b;
|
||||
$x9 = ($f9 ^ $g9) & $b;
|
||||
$f[0] = $f0 ^ $x0;
|
||||
$f[1] = $f1 ^ $x1;
|
||||
$f[2] = $f2 ^ $x2;
|
||||
$f[3] = $f3 ^ $x3;
|
||||
$f[4] = $f4 ^ $x4;
|
||||
$f[5] = $f5 ^ $x5;
|
||||
$f[6] = $f6 ^ $x6;
|
||||
$f[7] = $f7 ^ $x7;
|
||||
$f[8] = $f8 ^ $x8;
|
||||
$f[9] = $f9 ^ $x9;
|
||||
$g[0] = $g0 ^ $x0;
|
||||
$g[1] = $g1 ^ $x1;
|
||||
$g[2] = $g2 ^ $x2;
|
||||
$g[3] = $g3 ^ $x3;
|
||||
$g[4] = $g4 ^ $x4;
|
||||
$g[5] = $g5 ^ $x5;
|
||||
$g[6] = $g6 ^ $x6;
|
||||
$g[7] = $g7 ^ $x7;
|
||||
$g[8] = $g8 ^ $x8;
|
||||
$g[9] = $g9 ^ $x9;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $f
|
||||
* @return ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f)
|
||||
{
|
||||
$h = array(
|
||||
self::mul((int) $f[0], 121666, 17),
|
||||
self::mul((int) $f[1], 121666, 17),
|
||||
self::mul((int) $f[2], 121666, 17),
|
||||
self::mul((int) $f[3], 121666, 17),
|
||||
self::mul((int) $f[4], 121666, 17),
|
||||
self::mul((int) $f[5], 121666, 17),
|
||||
self::mul((int) $f[6], 121666, 17),
|
||||
self::mul((int) $f[7], 121666, 17),
|
||||
self::mul((int) $f[8], 121666, 17),
|
||||
self::mul((int) $f[9], 121666, 17)
|
||||
);
|
||||
|
||||
/** @var int $carry9 */
|
||||
$carry9 = ($h[9] + (1 << 24)) >> 25;
|
||||
$h[0] += self::mul($carry9, 19, 5);
|
||||
$h[9] -= $carry9 << 25;
|
||||
/** @var int $carry1 */
|
||||
$carry1 = ($h[1] + (1 << 24)) >> 25;
|
||||
$h[2] += $carry1;
|
||||
$h[1] -= $carry1 << 25;
|
||||
/** @var int $carry3 */
|
||||
$carry3 = ($h[3] + (1 << 24)) >> 25;
|
||||
$h[4] += $carry3;
|
||||
$h[3] -= $carry3 << 25;
|
||||
/** @var int $carry5 */
|
||||
$carry5 = ($h[5] + (1 << 24)) >> 25;
|
||||
$h[6] += $carry5;
|
||||
$h[5] -= $carry5 << 25;
|
||||
/** @var int $carry7 */
|
||||
$carry7 = ($h[7] + (1 << 24)) >> 25;
|
||||
$h[8] += $carry7;
|
||||
$h[7] -= $carry7 << 25;
|
||||
|
||||
/** @var int $carry0 */
|
||||
$carry0 = ($h[0] + (1 << 25)) >> 26;
|
||||
$h[1] += $carry0;
|
||||
$h[0] -= $carry0 << 26;
|
||||
/** @var int $carry2 */
|
||||
$carry2 = ($h[2] + (1 << 25)) >> 26;
|
||||
$h[3] += $carry2;
|
||||
$h[2] -= $carry2 << 26;
|
||||
/** @var int $carry4 */
|
||||
$carry4 = ($h[4] + (1 << 25)) >> 26;
|
||||
$h[5] += $carry4;
|
||||
$h[4] -= $carry4 << 26;
|
||||
/** @var int $carry6 */
|
||||
$carry6 = ($h[6] + (1 << 25)) >> 26;
|
||||
$h[7] += $carry6;
|
||||
$h[6] -= $carry6 << 26;
|
||||
/** @var int $carry8 */
|
||||
$carry8 = ($h[8] + (1 << 25)) >> 26;
|
||||
$h[9] += $carry8;
|
||||
$h[8] -= $carry8 << 26;
|
||||
|
||||
foreach ($h as $i => $value) {
|
||||
$h[$i] = (int) $value;
|
||||
}
|
||||
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* Inline comments preceded by # are from libsodium's ref10 code.
|
||||
*
|
||||
* @param string $n
|
||||
* @param string $p
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function crypto_scalarmult_curve25519_ref10($n, $p)
|
||||
{
|
||||
# for (i = 0;i < 32;++i) e[i] = n[i];
|
||||
$e = '' . $n;
|
||||
# e[0] &= 248;
|
||||
$e[0] = self::intToChr(
|
||||
self::chrToInt($e[0]) & 248
|
||||
);
|
||||
# e[31] &= 127;
|
||||
# e[31] |= 64;
|
||||
$e[31] = self::intToChr(
|
||||
(self::chrToInt($e[31]) & 127) | 64
|
||||
);
|
||||
# fe_frombytes(x1,p);
|
||||
$x1 = self::fe_frombytes($p);
|
||||
# fe_1(x2);
|
||||
$x2 = self::fe_1();
|
||||
# fe_0(z2);
|
||||
$z2 = self::fe_0();
|
||||
# fe_copy(x3,x1);
|
||||
$x3 = self::fe_copy($x1);
|
||||
# fe_1(z3);
|
||||
$z3 = self::fe_1();
|
||||
|
||||
# swap = 0;
|
||||
/** @var int $swap */
|
||||
$swap = 0;
|
||||
|
||||
# for (pos = 254;pos >= 0;--pos) {
|
||||
for ($pos = 254; $pos >= 0; --$pos) {
|
||||
# b = e[pos / 8] >> (pos & 7);
|
||||
/** @var int $b */
|
||||
$b = self::chrToInt(
|
||||
$e[(int) floor($pos / 8)]
|
||||
) >> ($pos & 7);
|
||||
# b &= 1;
|
||||
$b &= 1;
|
||||
# swap ^= b;
|
||||
$swap ^= $b;
|
||||
# fe_cswap(x2,x3,swap);
|
||||
self::fe_cswap($x2, $x3, $swap);
|
||||
# fe_cswap(z2,z3,swap);
|
||||
self::fe_cswap($z2, $z3, $swap);
|
||||
# swap = b;
|
||||
$swap = $b;
|
||||
# fe_sub(tmp0,x3,z3);
|
||||
$tmp0 = self::fe_sub($x3, $z3);
|
||||
# fe_sub(tmp1,x2,z2);
|
||||
$tmp1 = self::fe_sub($x2, $z2);
|
||||
|
||||
# fe_add(x2,x2,z2);
|
||||
$x2 = self::fe_add($x2, $z2);
|
||||
|
||||
# fe_add(z2,x3,z3);
|
||||
$z2 = self::fe_add($x3, $z3);
|
||||
|
||||
# fe_mul(z3,tmp0,x2);
|
||||
$z3 = self::fe_mul($tmp0, $x2);
|
||||
|
||||
# fe_mul(z2,z2,tmp1);
|
||||
$z2 = self::fe_mul($z2, $tmp1);
|
||||
|
||||
# fe_sq(tmp0,tmp1);
|
||||
$tmp0 = self::fe_sq($tmp1);
|
||||
|
||||
# fe_sq(tmp1,x2);
|
||||
$tmp1 = self::fe_sq($x2);
|
||||
|
||||
# fe_add(x3,z3,z2);
|
||||
$x3 = self::fe_add($z3, $z2);
|
||||
|
||||
# fe_sub(z2,z3,z2);
|
||||
$z2 = self::fe_sub($z3, $z2);
|
||||
|
||||
# fe_mul(x2,tmp1,tmp0);
|
||||
$x2 = self::fe_mul($tmp1, $tmp0);
|
||||
|
||||
# fe_sub(tmp1,tmp1,tmp0);
|
||||
$tmp1 = self::fe_sub($tmp1, $tmp0);
|
||||
|
||||
# fe_sq(z2,z2);
|
||||
$z2 = self::fe_sq($z2);
|
||||
|
||||
# fe_mul121666(z3,tmp1);
|
||||
$z3 = self::fe_mul121666($tmp1);
|
||||
|
||||
# fe_sq(x3,x3);
|
||||
$x3 = self::fe_sq($x3);
|
||||
|
||||
# fe_add(tmp0,tmp0,z3);
|
||||
$tmp0 = self::fe_add($tmp0, $z3);
|
||||
|
||||
# fe_mul(z3,x1,z2);
|
||||
$z3 = self::fe_mul($x1, $z2);
|
||||
|
||||
# fe_mul(z2,tmp1,tmp0);
|
||||
$z2 = self::fe_mul($tmp1, $tmp0);
|
||||
}
|
||||
|
||||
# fe_cswap(x2,x3,swap);
|
||||
self::fe_cswap($x2, $x3, $swap);
|
||||
|
||||
# fe_cswap(z2,z3,swap);
|
||||
self::fe_cswap($z2, $z3, $swap);
|
||||
|
||||
# fe_invert(z2,z2);
|
||||
$z2 = self::fe_invert($z2);
|
||||
|
||||
# fe_mul(x2,x2,z2);
|
||||
$x2 = self::fe_mul($x2, $z2);
|
||||
# fe_tobytes(q,x2);
|
||||
return self::fe_tobytes($x2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY
|
||||
* @param ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ
|
||||
* @return ParagonIE_Sodium_Core_Curve25519_Fe
|
||||
*/
|
||||
public static function edwards_to_montgomery(
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $edwardsY,
|
||||
ParagonIE_Sodium_Core_Curve25519_Fe $edwardsZ
|
||||
) {
|
||||
$tempX = self::fe_add($edwardsZ, $edwardsY);
|
||||
$tempZ = self::fe_sub($edwardsZ, $edwardsY);
|
||||
$tempZ = self::fe_invert($tempZ);
|
||||
return self::fe_mul($tempX, $tempZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $n
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function crypto_scalarmult_curve25519_ref10_base($n)
|
||||
{
|
||||
# for (i = 0;i < 32;++i) e[i] = n[i];
|
||||
$e = '' . $n;
|
||||
|
||||
# e[0] &= 248;
|
||||
$e[0] = self::intToChr(
|
||||
self::chrToInt($e[0]) & 248
|
||||
);
|
||||
|
||||
# e[31] &= 127;
|
||||
# e[31] |= 64;
|
||||
$e[31] = self::intToChr(
|
||||
(self::chrToInt($e[31]) & 127) | 64
|
||||
);
|
||||
|
||||
$A = self::ge_scalarmult_base($e);
|
||||
if (
|
||||
!($A->Y instanceof ParagonIE_Sodium_Core_Curve25519_Fe)
|
||||
||
|
||||
!($A->Z instanceof ParagonIE_Sodium_Core_Curve25519_Fe)
|
||||
) {
|
||||
throw new TypeError('Null points encountered');
|
||||
}
|
||||
$pk = self::edwards_to_montgomery($A->Y, $A->Z);
|
||||
return self::fe_tobytes($pk);
|
||||
}
|
||||
}
|
64
wp-includes/sodium_compat/src/Core/XChaCha20.php
Normal file
64
wp-includes/sodium_compat/src/Core/XChaCha20.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_XChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20
|
||||
{
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function stream($len = 64, $nonce = '', $key = '')
|
||||
{
|
||||
if (self::strlen($nonce) !== 24) {
|
||||
throw new SodiumException('Nonce must be 24 bytes long');
|
||||
}
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_Ctx(
|
||||
self::hChaCha20(
|
||||
self::substr($nonce, 0, 16),
|
||||
$key
|
||||
),
|
||||
self::substr($nonce, 16, 8)
|
||||
),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
if (self::strlen($nonce) !== 24) {
|
||||
throw new SodiumException('Nonce must be 24 bytes long');
|
||||
}
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core_ChaCha20_Ctx(
|
||||
self::hChaCha20(self::substr($nonce, 0, 16), $key),
|
||||
self::substr($nonce, 16, 8),
|
||||
$ic
|
||||
),
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
57
wp-includes/sodium_compat/src/Core/XSalsa20.php
Normal file
57
wp-includes/sodium_compat/src/Core/XSalsa20.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_XSalsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_XSalsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core_XSalsa20 extends ParagonIE_Sodium_Core_HSalsa20
|
||||
{
|
||||
/**
|
||||
* Expand a key and nonce into an xsalsa20 keystream.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xsalsa20($len, $nonce, $key)
|
||||
{
|
||||
$ret = self::salsa20(
|
||||
$len,
|
||||
self::substr($nonce, 16, 8),
|
||||
self::hsalsa20($nonce, $key)
|
||||
);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a string with XSalsa20. Doesn't provide integrity.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xsalsa20_xor($message, $nonce, $key)
|
||||
{
|
||||
return self::xorStrings(
|
||||
$message,
|
||||
self::xsalsa20(
|
||||
self::strlen($message),
|
||||
$nonce,
|
||||
$key
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
684
wp-includes/sodium_compat/src/Core32/BLAKE2b.php
Normal file
684
wp-includes/sodium_compat/src/Core32/BLAKE2b.php
Normal file
@ -0,0 +1,684 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_BLAKE2b
|
||||
*
|
||||
* Based on the work of Devi Mandiri in devi/salt.
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
/**
|
||||
* @var SplFixedArray
|
||||
*/
|
||||
public static $iv;
|
||||
|
||||
/**
|
||||
* @var array<int, array<int, int>>
|
||||
*/
|
||||
public static $sigma = array(
|
||||
array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
|
||||
array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
|
||||
array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
|
||||
array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
|
||||
array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
|
||||
array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
|
||||
array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
|
||||
array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
|
||||
array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
|
||||
array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0),
|
||||
array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
|
||||
array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3)
|
||||
);
|
||||
|
||||
const BLOCKBYTES = 128;
|
||||
const OUTBYTES = 64;
|
||||
const KEYBYTES = 64;
|
||||
|
||||
/**
|
||||
* Turn two 32-bit integers into a fixed array representing a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $high
|
||||
* @param int $low
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function new64($high, $low)
|
||||
{
|
||||
return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an arbitrary number into an SplFixedArray of two 32-bit integers
|
||||
* that represents a 64-bit integer.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $num
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function to64($num)
|
||||
{
|
||||
list($hi, $lo) = self::numericTo64BitInteger($num);
|
||||
return self::new64($hi, $lo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two 64-bit integers together, returning their sum as a SplFixedArray
|
||||
* containing two 32-bit integers (representing a 64-bit integer).
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $x
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $y
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
protected static function add64($x, $y)
|
||||
{
|
||||
return $x->addInt64($y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $x
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $y
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $z
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public static function add364($x, $y, $z)
|
||||
{
|
||||
return $x->addInt64($y)->addInt64($z);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $x
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $y
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y)
|
||||
{
|
||||
return $x->xorInt64($y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $x
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c)
|
||||
{
|
||||
return $x->rotateRight($c);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param int $i
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function load64($x, $i)
|
||||
{
|
||||
/** @var int $l */
|
||||
$l = (int) ($x[$i])
|
||||
| ((int) ($x[$i+1]) << 8)
|
||||
| ((int) ($x[$i+2]) << 16)
|
||||
| ((int) ($x[$i+3]) << 24);
|
||||
/** @var int $h */
|
||||
$h = (int) ($x[$i+4])
|
||||
| ((int) ($x[$i+5]) << 8)
|
||||
| ((int) ($x[$i+6]) << 16)
|
||||
| ((int) ($x[$i+7]) << 24);
|
||||
return self::new64($h, $l);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $x
|
||||
* @param int $i
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $u
|
||||
* @return void
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u)
|
||||
{
|
||||
$v = clone $u;
|
||||
$maxLength = $x->getSize() - 1;
|
||||
for ($j = 0; $j < 8; ++$j) {
|
||||
$k = 3 - ($j >> 1);
|
||||
$x[$i] = $v->limbs[$k] & 0xff;
|
||||
if (++$i > $maxLength) {
|
||||
return;
|
||||
}
|
||||
$v->limbs[$k] >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This just sets the $iv static variable.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function pseudoConstructor()
|
||||
{
|
||||
static $called = false;
|
||||
if ($called) {
|
||||
return;
|
||||
}
|
||||
self::$iv = new SplFixedArray(8);
|
||||
self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
|
||||
self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
|
||||
self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
|
||||
self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
|
||||
self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
|
||||
self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
|
||||
self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
|
||||
self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
|
||||
|
||||
$called = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fresh BLAKE2 context.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return SplFixedArray
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function context()
|
||||
{
|
||||
$ctx = new SplFixedArray(5);
|
||||
$ctx[0] = new SplFixedArray(8); // h
|
||||
$ctx[1] = new SplFixedArray(2); // t
|
||||
$ctx[2] = new SplFixedArray(2); // f
|
||||
$ctx[3] = new SplFixedArray(256); // buf
|
||||
$ctx[4] = 0; // buflen
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$ctx[0][$i] = self::$iv[$i];
|
||||
}
|
||||
for ($i = 256; $i--;) {
|
||||
$ctx[3][$i] = 0;
|
||||
}
|
||||
|
||||
$zero = self::new64(0, 0);
|
||||
$ctx[1][0] = $zero;
|
||||
$ctx[1][1] = $zero;
|
||||
$ctx[2][0] = $zero;
|
||||
$ctx[2][1] = $zero;
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $buf
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
|
||||
{
|
||||
$m = new SplFixedArray(16);
|
||||
$v = new SplFixedArray(16);
|
||||
|
||||
for ($i = 16; $i--;) {
|
||||
$m[$i] = self::load64($buf, $i << 3);
|
||||
}
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$v[$i] = $ctx[0][$i];
|
||||
}
|
||||
|
||||
$v[ 8] = self::$iv[0];
|
||||
$v[ 9] = self::$iv[1];
|
||||
$v[10] = self::$iv[2];
|
||||
$v[11] = self::$iv[3];
|
||||
|
||||
$v[12] = self::xor64($ctx[1][0], self::$iv[4]);
|
||||
$v[13] = self::xor64($ctx[1][1], self::$iv[5]);
|
||||
$v[14] = self::xor64($ctx[2][0], self::$iv[6]);
|
||||
$v[15] = self::xor64($ctx[2][1], self::$iv[7]);
|
||||
|
||||
for ($r = 0; $r < 12; ++$r) {
|
||||
$v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
|
||||
$v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
|
||||
$v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
|
||||
$v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
|
||||
$v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
|
||||
$v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
|
||||
$v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
|
||||
$v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
|
||||
}
|
||||
|
||||
for ($i = 8; $i--;) {
|
||||
$ctx[0][$i] = self::xor64(
|
||||
$ctx[0][$i], self::xor64($v[$i], $v[$i+8])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $r
|
||||
* @param int $i
|
||||
* @param int $a
|
||||
* @param int $b
|
||||
* @param int $c
|
||||
* @param int $d
|
||||
* @param SplFixedArray $v
|
||||
* @param SplFixedArray $m
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
|
||||
{
|
||||
$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
|
||||
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
|
||||
$v[$c] = self::add64($v[$c], $v[$d]);
|
||||
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
|
||||
$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]);
|
||||
$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
|
||||
$v[$c] = self::add64($v[$c], $v[$d]);
|
||||
$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
|
||||
return $v;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param int $inc
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
*/
|
||||
public static function increment_counter($ctx, $inc)
|
||||
{
|
||||
if ($inc < 0) {
|
||||
throw new SodiumException('Increasing by a negative number makes no sense.');
|
||||
}
|
||||
$t = self::to64($inc);
|
||||
# S->t is $ctx[1] in our implementation
|
||||
|
||||
# S->t[0] = ( uint64_t )( t >> 0 );
|
||||
$ctx[1][0] = self::add64($ctx[1][0], $t);
|
||||
|
||||
# S->t[1] += ( S->t[0] < inc );
|
||||
if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) {
|
||||
throw new TypeError('Not an int64');
|
||||
}
|
||||
/** @var ParagonIE_Sodium_Core32_Int64 $c*/
|
||||
$c = $ctx[1][0];
|
||||
if ($c->isLessThanInt($inc)) {
|
||||
$ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $p
|
||||
* @param int $plen
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @psalm-suppress MixedMethodCall
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
|
||||
$offset = 0;
|
||||
while ($plen > 0) {
|
||||
$left = $ctx[4];
|
||||
$fill = 256 - $left;
|
||||
|
||||
if ($plen > $fill) {
|
||||
# memcpy( S->buf + left, in, fill ); /* Fill buffer */
|
||||
for ($i = $fill; $i--;) {
|
||||
$ctx[3][$i + $left] = $p[$i + $offset];
|
||||
}
|
||||
|
||||
# S->buflen += fill;
|
||||
$ctx[4] += $fill;
|
||||
|
||||
# blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
||||
self::increment_counter($ctx, 128);
|
||||
|
||||
# blake2b_compress( S, S->buf ); /* Compress */
|
||||
self::compress($ctx, $ctx[3]);
|
||||
|
||||
# memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
|
||||
for ($i = 128; $i--;) {
|
||||
$ctx[3][$i] = $ctx[3][$i + 128];
|
||||
}
|
||||
|
||||
# S->buflen -= BLAKE2B_BLOCKBYTES;
|
||||
$ctx[4] -= 128;
|
||||
|
||||
# in += fill;
|
||||
$offset += $fill;
|
||||
|
||||
# inlen -= fill;
|
||||
$plen -= $fill;
|
||||
} else {
|
||||
for ($i = $plen; $i--;) {
|
||||
$ctx[3][$i + $left] = $p[$i + $offset];
|
||||
}
|
||||
$ctx[4] += $plen;
|
||||
$offset += $plen;
|
||||
$plen -= $plen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $ctx
|
||||
* @param SplFixedArray $out
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
* @psalm-suppress MixedMethodCall
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
public static function finish(SplFixedArray $ctx, SplFixedArray $out)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
if ($ctx[4] > 128) {
|
||||
self::increment_counter($ctx, 128);
|
||||
self::compress($ctx, $ctx[3]);
|
||||
$ctx[4] -= 128;
|
||||
if ($ctx[4] > 128) {
|
||||
throw new SodiumException('Failed to assert that buflen <= 128 bytes');
|
||||
}
|
||||
for ($i = $ctx[4]; $i--;) {
|
||||
$ctx[3][$i] = $ctx[3][$i + 128];
|
||||
}
|
||||
}
|
||||
|
||||
self::increment_counter($ctx, $ctx[4]);
|
||||
$ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
|
||||
|
||||
for ($i = 256 - $ctx[4]; $i--;) {
|
||||
/** @var int $i */
|
||||
$ctx[3][$i + $ctx[4]] = 0;
|
||||
}
|
||||
|
||||
self::compress($ctx, $ctx[3]);
|
||||
|
||||
$i = (int) (($out->getSize() - 1) / 8);
|
||||
for (; $i >= 0; --$i) {
|
||||
self::store64($out, $i << 3, $ctx[0][$i]);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray|null $key
|
||||
* @param int $outlen
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedMethodCall
|
||||
*/
|
||||
public static function init($key = null, $outlen = 64)
|
||||
{
|
||||
self::pseudoConstructor();
|
||||
$klen = 0;
|
||||
|
||||
if ($key !== null) {
|
||||
if (count($key) > 64) {
|
||||
throw new SodiumException('Invalid key size');
|
||||
}
|
||||
$klen = count($key);
|
||||
}
|
||||
|
||||
if ($outlen > 64) {
|
||||
throw new SodiumException('Invalid output size');
|
||||
}
|
||||
|
||||
$ctx = self::context();
|
||||
|
||||
$p = new SplFixedArray(64);
|
||||
for ($i = 64; --$i;) {
|
||||
$p[$i] = 0;
|
||||
}
|
||||
|
||||
$p[0] = $outlen; // digest_length
|
||||
$p[1] = $klen; // key_length
|
||||
$p[2] = 1; // fanout
|
||||
$p[3] = 1; // depth
|
||||
|
||||
$ctx[0][0] = self::xor64(
|
||||
$ctx[0][0],
|
||||
self::load64($p, 0)
|
||||
);
|
||||
|
||||
if ($klen > 0 && $key instanceof SplFixedArray) {
|
||||
$block = new SplFixedArray(128);
|
||||
for ($i = 128; $i--;) {
|
||||
$block[$i] = 0;
|
||||
}
|
||||
for ($i = $klen; $i--;) {
|
||||
$block[$i] = $key[$i];
|
||||
}
|
||||
self::update($ctx, $block, 128);
|
||||
}
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string into an SplFixedArray of integers
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $str
|
||||
* @return SplFixedArray
|
||||
*/
|
||||
public static function stringToSplFixedArray($str = '')
|
||||
{
|
||||
$values = unpack('C*', $str);
|
||||
return SplFixedArray::fromArray(array_values($values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an SplFixedArray of integers into a string
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray $a
|
||||
* @return string
|
||||
*/
|
||||
public static function SplFixedArrayToString(SplFixedArray $a)
|
||||
{
|
||||
/**
|
||||
* @var array<int, string|int>
|
||||
*/
|
||||
$arr = $a->toArray();
|
||||
$c = $a->count();
|
||||
array_unshift($arr, str_repeat('C', $c));
|
||||
return (string) (call_user_func_array('pack', $arr));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param SplFixedArray[SplFixedArray] $ctx
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArgument
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
* @psalm-suppress MixedMethodCall
|
||||
*/
|
||||
public static function contextToString(SplFixedArray $ctx)
|
||||
{
|
||||
$str = '';
|
||||
/** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */
|
||||
$ctxA = $ctx[0]->toArray();
|
||||
|
||||
# uint64_t h[8];
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) {
|
||||
throw new TypeError('Not an instance of Int64');
|
||||
}
|
||||
/** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */
|
||||
$ctxAi = $ctxA[$i];
|
||||
$str .= $ctxAi->toString();
|
||||
}
|
||||
|
||||
# uint64_t t[2];
|
||||
# uint64_t f[2];
|
||||
for ($i = 1; $i < 3; ++$i) {
|
||||
/** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */
|
||||
$ctxA = $ctx[$i]->toArray();
|
||||
/** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */
|
||||
$ctxA1 = $ctxA[0];
|
||||
/** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */
|
||||
$ctxA2 = $ctxA[1];
|
||||
|
||||
$str .= $ctxA1->toString();
|
||||
$str .= $ctxA2->toString();
|
||||
}
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$str .= self::SplFixedArrayToString($ctx[3]);
|
||||
|
||||
/** @var int $ctx4 */
|
||||
$ctx4 = $ctx[4];
|
||||
|
||||
# size_t buflen;
|
||||
$str .= implode('', array(
|
||||
self::intToChr($ctx4 & 0xff),
|
||||
self::intToChr(($ctx4 >> 8) & 0xff),
|
||||
self::intToChr(($ctx4 >> 16) & 0xff),
|
||||
self::intToChr(($ctx4 >> 24) & 0xff),
|
||||
self::intToChr(($ctx4 >> 32) & 0xff),
|
||||
self::intToChr(($ctx4 >> 40) & 0xff),
|
||||
self::intToChr(($ctx4 >> 48) & 0xff),
|
||||
self::intToChr(($ctx4 >> 56) & 0xff)
|
||||
));
|
||||
# uint8_t last_node;
|
||||
return $str . "\x00";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an SplFixedArray containing other SplFixedArray elements, from
|
||||
* a string (compatible with \Sodium\crypto_generichash_{init, update, final})
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $string
|
||||
* @return SplFixedArray
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
* @psalm-suppress MixedArrayAssignment
|
||||
*/
|
||||
public static function stringToContext($string)
|
||||
{
|
||||
$ctx = self::context();
|
||||
|
||||
# uint64_t h[8];
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
$ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromString(
|
||||
self::substr($string, (($i << 3) + 0), 8)
|
||||
);
|
||||
}
|
||||
|
||||
# uint64_t t[2];
|
||||
# uint64_t f[2];
|
||||
for ($i = 1; $i < 3; ++$i) {
|
||||
$ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromString(
|
||||
self::substr($string, 72 + (($i - 1) << 4), 8)
|
||||
);
|
||||
$ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromString(
|
||||
self::substr($string, 64 + (($i - 1) << 4), 8)
|
||||
);
|
||||
}
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
|
||||
|
||||
|
||||
# uint8_t buf[2 * 128];
|
||||
$int = 0;
|
||||
for ($i = 0; $i < 8; ++$i) {
|
||||
$int |= self::chrToInt($string[352 + $i]) << ($i << 3);
|
||||
}
|
||||
$ctx[4] = $int;
|
||||
|
||||
return $ctx;
|
||||
}
|
||||
}
|
400
wp-includes/sodium_compat/src/Core32/ChaCha20.php
Normal file
400
wp-includes/sodium_compat/src/Core32/ChaCha20.php
Normal file
@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_ChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util
|
||||
{
|
||||
/**
|
||||
* The ChaCha20 quarter round function. Works on four 32-bit integers.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $a
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $b
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $c
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $d
|
||||
* @return array<int, ParagonIE_Sodium_Core32_Int32>
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function quarterRound(
|
||||
ParagonIE_Sodium_Core32_Int32 $a,
|
||||
ParagonIE_Sodium_Core32_Int32 $b,
|
||||
ParagonIE_Sodium_Core32_Int32 $c,
|
||||
ParagonIE_Sodium_Core32_Int32 $d
|
||||
) {
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $a */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $b */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $c */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $d */
|
||||
|
||||
# a = PLUS(a,b); d = ROTATE(XOR(d,a),16);
|
||||
$a = $a->addInt32($b);
|
||||
$d = $d->xorInt32($a)->rotateLeft(16);
|
||||
|
||||
# c = PLUS(c,d); b = ROTATE(XOR(b,c),12);
|
||||
$c = $c->addInt32($d);
|
||||
$b = $b->xorInt32($c)->rotateLeft(12);
|
||||
|
||||
# a = PLUS(a,b); d = ROTATE(XOR(d,a), 8);
|
||||
$a = $a->addInt32($b);
|
||||
$d = $d->xorInt32($a)->rotateLeft(8);
|
||||
|
||||
# c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
|
||||
$c = $c->addInt32($d);
|
||||
$b = $b->xorInt32($c)->rotateLeft(7);
|
||||
|
||||
return array($a, $b, $c, $d);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx
|
||||
* @param string $message
|
||||
*
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encryptBytes(
|
||||
ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx,
|
||||
$message = ''
|
||||
) {
|
||||
$bytes = self::strlen($message);
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x0 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x1 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x2 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x3 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x4 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x5 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x6 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x7 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x8 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x9 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x10 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x11 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x12 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x13 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x14 */
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x15 */
|
||||
|
||||
/*
|
||||
j0 = ctx->input[0];
|
||||
j1 = ctx->input[1];
|
||||
j2 = ctx->input[2];
|
||||
j3 = ctx->input[3];
|
||||
j4 = ctx->input[4];
|
||||
j5 = ctx->input[5];
|
||||
j6 = ctx->input[6];
|
||||
j7 = ctx->input[7];
|
||||
j8 = ctx->input[8];
|
||||
j9 = ctx->input[9];
|
||||
j10 = ctx->input[10];
|
||||
j11 = ctx->input[11];
|
||||
j12 = ctx->input[12];
|
||||
j13 = ctx->input[13];
|
||||
j14 = ctx->input[14];
|
||||
j15 = ctx->input[15];
|
||||
*/
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j0 */
|
||||
$j0 = $ctx[0];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j1 */
|
||||
$j1 = $ctx[1];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j2 */
|
||||
$j2 = $ctx[2];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j3 */
|
||||
$j3 = $ctx[3];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j4 */
|
||||
$j4 = $ctx[4];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j5 */
|
||||
$j5 = $ctx[5];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j6 */
|
||||
$j6 = $ctx[6];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j7 */
|
||||
$j7 = $ctx[7];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j8 */
|
||||
$j8 = $ctx[8];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j9 */
|
||||
$j9 = $ctx[9];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j10 */
|
||||
$j10 = $ctx[10];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j11 */
|
||||
$j11 = $ctx[11];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j12 */
|
||||
$j12 = $ctx[12];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j13 */
|
||||
$j13 = $ctx[13];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j14 */
|
||||
$j14 = $ctx[14];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j15 */
|
||||
$j15 = $ctx[15];
|
||||
|
||||
$c = '';
|
||||
for (;;) {
|
||||
if ($bytes < 64) {
|
||||
$message .= str_repeat("\x00", 64 - $bytes);
|
||||
}
|
||||
|
||||
$x0 = clone $j0;
|
||||
$x1 = clone $j1;
|
||||
$x2 = clone $j2;
|
||||
$x3 = clone $j3;
|
||||
$x4 = clone $j4;
|
||||
$x5 = clone $j5;
|
||||
$x6 = clone $j6;
|
||||
$x7 = clone $j7;
|
||||
$x8 = clone $j8;
|
||||
$x9 = clone $j9;
|
||||
$x10 = clone $j10;
|
||||
$x11 = clone $j11;
|
||||
$x12 = clone $j12;
|
||||
$x13 = clone $j13;
|
||||
$x14 = clone $j14;
|
||||
$x15 = clone $j15;
|
||||
|
||||
# for (i = 20; i > 0; i -= 2) {
|
||||
for ($i = 20; $i > 0; $i -= 2) {
|
||||
# QUARTERROUND( x0, x4, x8, x12)
|
||||
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
|
||||
|
||||
# QUARTERROUND( x1, x5, x9, x13)
|
||||
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
|
||||
|
||||
# QUARTERROUND( x2, x6, x10, x14)
|
||||
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
|
||||
|
||||
# QUARTERROUND( x3, x7, x11, x15)
|
||||
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
|
||||
|
||||
# QUARTERROUND( x0, x5, x10, x15)
|
||||
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
|
||||
|
||||
# QUARTERROUND( x1, x6, x11, x12)
|
||||
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
|
||||
|
||||
# QUARTERROUND( x2, x7, x8, x13)
|
||||
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
|
||||
|
||||
# QUARTERROUND( x3, x4, x9, x14)
|
||||
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
|
||||
}
|
||||
/*
|
||||
x0 = PLUS(x0, j0);
|
||||
x1 = PLUS(x1, j1);
|
||||
x2 = PLUS(x2, j2);
|
||||
x3 = PLUS(x3, j3);
|
||||
x4 = PLUS(x4, j4);
|
||||
x5 = PLUS(x5, j5);
|
||||
x6 = PLUS(x6, j6);
|
||||
x7 = PLUS(x7, j7);
|
||||
x8 = PLUS(x8, j8);
|
||||
x9 = PLUS(x9, j9);
|
||||
x10 = PLUS(x10, j10);
|
||||
x11 = PLUS(x11, j11);
|
||||
x12 = PLUS(x12, j12);
|
||||
x13 = PLUS(x13, j13);
|
||||
x14 = PLUS(x14, j14);
|
||||
x15 = PLUS(x15, j15);
|
||||
*/
|
||||
$x0 = $x0->addInt32($j0);
|
||||
$x1 = $x1->addInt32($j1);
|
||||
$x2 = $x2->addInt32($j2);
|
||||
$x3 = $x3->addInt32($j3);
|
||||
$x4 = $x4->addInt32($j4);
|
||||
$x5 = $x5->addInt32($j5);
|
||||
$x6 = $x6->addInt32($j6);
|
||||
$x7 = $x7->addInt32($j7);
|
||||
$x8 = $x8->addInt32($j8);
|
||||
$x9 = $x9->addInt32($j9);
|
||||
$x10 = $x10->addInt32($j10);
|
||||
$x11 = $x11->addInt32($j11);
|
||||
$x12 = $x12->addInt32($j12);
|
||||
$x13 = $x13->addInt32($j13);
|
||||
$x14 = $x14->addInt32($j14);
|
||||
$x15 = $x15->addInt32($j15);
|
||||
|
||||
/*
|
||||
x0 = XOR(x0, LOAD32_LE(m + 0));
|
||||
x1 = XOR(x1, LOAD32_LE(m + 4));
|
||||
x2 = XOR(x2, LOAD32_LE(m + 8));
|
||||
x3 = XOR(x3, LOAD32_LE(m + 12));
|
||||
x4 = XOR(x4, LOAD32_LE(m + 16));
|
||||
x5 = XOR(x5, LOAD32_LE(m + 20));
|
||||
x6 = XOR(x6, LOAD32_LE(m + 24));
|
||||
x7 = XOR(x7, LOAD32_LE(m + 28));
|
||||
x8 = XOR(x8, LOAD32_LE(m + 32));
|
||||
x9 = XOR(x9, LOAD32_LE(m + 36));
|
||||
x10 = XOR(x10, LOAD32_LE(m + 40));
|
||||
x11 = XOR(x11, LOAD32_LE(m + 44));
|
||||
x12 = XOR(x12, LOAD32_LE(m + 48));
|
||||
x13 = XOR(x13, LOAD32_LE(m + 52));
|
||||
x14 = XOR(x14, LOAD32_LE(m + 56));
|
||||
x15 = XOR(x15, LOAD32_LE(m + 60));
|
||||
*/
|
||||
$x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4)));
|
||||
$x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4)));
|
||||
$x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4)));
|
||||
$x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4)));
|
||||
$x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4)));
|
||||
$x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4)));
|
||||
$x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4)));
|
||||
$x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4)));
|
||||
$x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4)));
|
||||
$x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4)));
|
||||
$x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4)));
|
||||
$x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4)));
|
||||
$x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4)));
|
||||
$x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4)));
|
||||
$x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4)));
|
||||
$x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4)));
|
||||
|
||||
/*
|
||||
j12 = PLUSONE(j12);
|
||||
if (!j12) {
|
||||
j13 = PLUSONE(j13);
|
||||
}
|
||||
*/
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $j12 */
|
||||
$j12 = $j12->addInt(1);
|
||||
if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) {
|
||||
$j13 = $j13->addInt(1);
|
||||
}
|
||||
|
||||
/*
|
||||
STORE32_LE(c + 0, x0);
|
||||
STORE32_LE(c + 4, x1);
|
||||
STORE32_LE(c + 8, x2);
|
||||
STORE32_LE(c + 12, x3);
|
||||
STORE32_LE(c + 16, x4);
|
||||
STORE32_LE(c + 20, x5);
|
||||
STORE32_LE(c + 24, x6);
|
||||
STORE32_LE(c + 28, x7);
|
||||
STORE32_LE(c + 32, x8);
|
||||
STORE32_LE(c + 36, x9);
|
||||
STORE32_LE(c + 40, x10);
|
||||
STORE32_LE(c + 44, x11);
|
||||
STORE32_LE(c + 48, x12);
|
||||
STORE32_LE(c + 52, x13);
|
||||
STORE32_LE(c + 56, x14);
|
||||
STORE32_LE(c + 60, x15);
|
||||
*/
|
||||
|
||||
$block = $x0->toReverseString() .
|
||||
$x1->toReverseString() .
|
||||
$x2->toReverseString() .
|
||||
$x3->toReverseString() .
|
||||
$x4->toReverseString() .
|
||||
$x5->toReverseString() .
|
||||
$x6->toReverseString() .
|
||||
$x7->toReverseString() .
|
||||
$x8->toReverseString() .
|
||||
$x9->toReverseString() .
|
||||
$x10->toReverseString() .
|
||||
$x11->toReverseString() .
|
||||
$x12->toReverseString() .
|
||||
$x13->toReverseString() .
|
||||
$x14->toReverseString() .
|
||||
$x15->toReverseString();
|
||||
|
||||
/* Partial block */
|
||||
if ($bytes < 64) {
|
||||
$c .= self::substr($block, 0, $bytes);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Full block */
|
||||
$c .= $block;
|
||||
$bytes -= 64;
|
||||
if ($bytes <= 0) {
|
||||
break;
|
||||
}
|
||||
$message = self::substr($message, 64);
|
||||
}
|
||||
/* end for(;;) loop */
|
||||
|
||||
$ctx[12] = $j12;
|
||||
$ctx[13] = $j13;
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function stream($len = 64, $nonce = '', $key = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function ietfStream($len, $nonce = '', $key = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic),
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
126
wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php
Normal file
126
wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_ChaCha20_Ctx
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var SplFixedArray internally, <int, ParagonIE_Sodium_Core32_Int32>
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key ChaCha20 key.
|
||||
* @param string $iv Initialization Vector (a.k.a. nonce).
|
||||
* @param string $counter The initial counter value.
|
||||
* Defaults to 8 0x00 bytes.
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '', $iv = '', $counter = '')
|
||||
{
|
||||
if (self::strlen($key) !== 32) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.');
|
||||
}
|
||||
if (self::strlen($iv) !== 8) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.');
|
||||
}
|
||||
$this->container = new SplFixedArray(16);
|
||||
|
||||
/* "expand 32-byte k" as per ChaCha20 spec */
|
||||
$this->container[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
|
||||
$this->container[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
|
||||
$this->container[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
|
||||
$this->container[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
|
||||
|
||||
$this->container[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4));
|
||||
$this->container[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4));
|
||||
$this->container[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4));
|
||||
$this->container[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4));
|
||||
$this->container[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4));
|
||||
$this->container[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4));
|
||||
$this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4));
|
||||
$this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4));
|
||||
|
||||
if (empty($counter)) {
|
||||
$this->container[12] = new ParagonIE_Sodium_Core32_Int32();
|
||||
$this->container[13] = new ParagonIE_Sodium_Core32_Int32();
|
||||
} else {
|
||||
$this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4));
|
||||
$this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4));
|
||||
}
|
||||
$this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4));
|
||||
$this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $offset
|
||||
* @param int|ParagonIE_Sodium_Core32_Int32 $value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (!is_int($offset)) {
|
||||
throw new InvalidArgumentException('Expected an integer');
|
||||
}
|
||||
if ($value instanceof ParagonIE_Sodium_Core32_Int32) {
|
||||
/*
|
||||
} elseif (is_int($value)) {
|
||||
$value = ParagonIE_Sodium_Core32_Int32::fromInt($value);
|
||||
*/
|
||||
} else {
|
||||
throw new InvalidArgumentException('Expected an integer');
|
||||
}
|
||||
$this->container[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return mixed|null
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->container[$offset])
|
||||
? $this->container[$offset]
|
||||
: null;
|
||||
}
|
||||
}
|
39
wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php
Normal file
39
wp-includes/sodium_compat/src/Core32/ChaCha20/IetfCtx.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core_ChaCha20_IetfCtx', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_ChaCha20_IetfCtx extends ParagonIE_Sodium_Core32_ChaCha20_Ctx
|
||||
{
|
||||
/**
|
||||
* ParagonIE_Sodium_Core_ChaCha20_IetfCtx constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key ChaCha20 key.
|
||||
* @param string $iv Initialization Vector (a.k.a. nonce).
|
||||
* @param string $counter The initial counter value.
|
||||
* Defaults to 4 0x00 bytes.
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '', $iv = '', $counter = '')
|
||||
{
|
||||
if (self::strlen($iv) !== 12) {
|
||||
throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.');
|
||||
}
|
||||
parent::__construct($key, self::substr($iv, 0, 8), $counter);
|
||||
|
||||
if (!empty($counter)) {
|
||||
$this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4));
|
||||
}
|
||||
$this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4));
|
||||
$this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4));
|
||||
$this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 8, 4));
|
||||
}
|
||||
}
|
3196
wp-includes/sodium_compat/src/Core32/Curve25519.php
Normal file
3196
wp-includes/sodium_compat/src/Core32/Curve25519.php
Normal file
File diff suppressed because it is too large
Load Diff
185
wp-includes/sodium_compat/src/Core32/Curve25519/Fe.php
Normal file
185
wp-includes/sodium_compat/src/Core32/Curve25519/Fe.php
Normal file
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Fe', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*
|
||||
* This represents a Field Element
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Fe implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* @var array<int, ParagonIE_Sodium_Core32_Int32>
|
||||
*/
|
||||
protected $container = array();
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $size = 10;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param array<int, ParagonIE_Sodium_Core32_Int32> $array
|
||||
* @param bool $save_indexes
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromArray($array, $save_indexes = null)
|
||||
{
|
||||
$count = count($array);
|
||||
if ($save_indexes) {
|
||||
$keys = array_keys($array);
|
||||
} else {
|
||||
$keys = range(0, $count - 1);
|
||||
}
|
||||
$array = array_values($array);
|
||||
|
||||
$obj = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
if ($save_indexes) {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$array[$i]->overflow = 0;
|
||||
$obj->offsetSet($keys[$i], $array[$i]);
|
||||
}
|
||||
} else {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$array[$i]->overflow = 0;
|
||||
$obj->offsetSet($i, $array[$i]);
|
||||
}
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param array<int, int> $array
|
||||
* @param bool $save_indexes
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromIntArray($array, $save_indexes = null)
|
||||
{
|
||||
$count = count($array);
|
||||
if ($save_indexes) {
|
||||
$keys = array_keys($array);
|
||||
} else {
|
||||
$keys = range(0, $count - 1);
|
||||
}
|
||||
$array = array_values($array);
|
||||
$set = array();
|
||||
/** @var int $i */
|
||||
/** @var int $v */
|
||||
foreach ($array as $i => $v) {
|
||||
$set[$i] = ParagonIE_Sodium_Core32_Int32::fromInt($v);
|
||||
}
|
||||
|
||||
$obj = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
if ($save_indexes) {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$set[$i]->overflow = 0;
|
||||
$obj->offsetSet($keys[$i], $set[$i]);
|
||||
}
|
||||
} else {
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$set[$i]->overflow = 0;
|
||||
$obj->offsetSet($i, $set[$i]);
|
||||
}
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (!($value instanceof ParagonIE_Sodium_Core32_Int32)) {
|
||||
throw new InvalidArgumentException('Expected an instance of ParagonIE_Sodium_Core32_Int32');
|
||||
}
|
||||
if (is_null($offset)) {
|
||||
$this->container[] = $value;
|
||||
} else {
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($offset, 'int', 1);
|
||||
$this->container[(int) $offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return void
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @psalm-suppress MixedArrayOffset
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
if (!isset($this->container[$offset])) {
|
||||
$this->container[(int) $offset] = new ParagonIE_Sodium_Core32_Int32();
|
||||
}
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $get */
|
||||
$get = $this->container[$offset];
|
||||
return $get;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __debugInfo()
|
||||
{
|
||||
if (empty($this->container)) {
|
||||
return array();
|
||||
}
|
||||
$c = array(
|
||||
(int) ($this->container[0]->toInt()),
|
||||
(int) ($this->container[1]->toInt()),
|
||||
(int) ($this->container[2]->toInt()),
|
||||
(int) ($this->container[3]->toInt()),
|
||||
(int) ($this->container[4]->toInt()),
|
||||
(int) ($this->container[5]->toInt()),
|
||||
(int) ($this->container[6]->toInt()),
|
||||
(int) ($this->container[7]->toInt()),
|
||||
(int) ($this->container[8]->toInt()),
|
||||
(int) ($this->container[9]->toInt())
|
||||
);
|
||||
return array(implode(', ', $c));
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_Cached', false)) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Ge_Cached
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Ge_Cached
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $YplusX;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $YminusX;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $T2d;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Curve25519_Ge_Cached constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $YplusX
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $YminusX
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $Z
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $T2d
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $YplusX = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $YminusX = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $Z = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $T2d = null
|
||||
) {
|
||||
if ($YplusX === null) {
|
||||
$YplusX = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->YplusX = $YplusX;
|
||||
if ($YminusX === null) {
|
||||
$YminusX = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->YminusX = $YminusX;
|
||||
if ($Z === null) {
|
||||
$Z = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $Z;
|
||||
if ($T2d === null) {
|
||||
$T2d = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->T2d = $T2d;
|
||||
}
|
||||
}
|
67
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P1p1.php
Normal file
67
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P1p1.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1', false)) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $T;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $t
|
||||
*
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $z = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $t = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->Z = $z;
|
||||
if ($t === null) {
|
||||
$t = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->T = $t;
|
||||
}
|
||||
}
|
54
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P2.php
Normal file
54
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P2.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P2', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Ge_P2
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Ge_P2
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Curve25519_Ge_P2 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $z = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $z;
|
||||
}
|
||||
}
|
65
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P3.php
Normal file
65
wp-includes/sodium_compat/src/Core32/Curve25519/Ge/P3.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_P3', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Ge_P3
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Ge_P3
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $X;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Y;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $Z;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $T;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Curve25519_Ge_P3 constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $x
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $y
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $z
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe|null $t
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $x = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $y = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $z = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $t = null
|
||||
) {
|
||||
if ($x === null) {
|
||||
$x = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->X = $x;
|
||||
if ($y === null) {
|
||||
$y = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->Y = $y;
|
||||
if ($z === null) {
|
||||
$z = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->Z = $z;
|
||||
if ($t === null) {
|
||||
$t = new ParagonIE_Sodium_Core32_Curve25519_Fe();
|
||||
}
|
||||
$this->T = $t;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $yplusx;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $yminusx;
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
*/
|
||||
public $xy2d;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $yplusx
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $yminusx
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $xy2d
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $yplusx = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $yminusx = null,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $xy2d = null
|
||||
) {
|
||||
if ($yplusx === null) {
|
||||
$yplusx = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->yplusx = $yplusx;
|
||||
if ($yminusx === null) {
|
||||
$yminusx = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->yminusx = $yminusx;
|
||||
if ($xy2d === null) {
|
||||
$xy2d = ParagonIE_Sodium_Core32_Curve25519::fe_0();
|
||||
}
|
||||
$this->xy2d = $xy2d;
|
||||
}
|
||||
}
|
1467
wp-includes/sodium_compat/src/Core32/Curve25519/H.php
Normal file
1467
wp-includes/sodium_compat/src/Core32/Curve25519/H.php
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,3 @@
|
||||
# Curve25519 Data Structures
|
||||
|
||||
These are PHP implementation of the [structs used in the ref10 curve25519 code](https://github.com/jedisct1/libsodium/blob/master/src/libsodium/include/sodium/private/curve25519_ref10.h).
|
481
wp-includes/sodium_compat/src/Core32/Ed25519.php
Normal file
481
wp-includes/sodium_compat/src/Core32/Ed25519.php
Normal file
@ -0,0 +1,481 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Ed25519', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Ed25519
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_Ed25519 extends ParagonIE_Sodium_Core32_Curve25519
|
||||
{
|
||||
const KEYPAIR_BYTES = 96;
|
||||
const SEED_BYTES = 32;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return string (96 bytes)
|
||||
* @throws Exception
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function keypair()
|
||||
{
|
||||
$seed = random_bytes(self::SEED_BYTES);
|
||||
$pk = '';
|
||||
$sk = '';
|
||||
self::seed_keypair($pk, $sk, $seed);
|
||||
return $sk . $pk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $pk
|
||||
* @param string $sk
|
||||
* @param string $seed
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function seed_keypair(&$pk, &$sk, $seed)
|
||||
{
|
||||
if (self::strlen($seed) !== self::SEED_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
|
||||
}
|
||||
|
||||
/** @var string $pk */
|
||||
$pk = self::publickey_from_secretkey($seed);
|
||||
$sk = $seed . $pk;
|
||||
return $sk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function secretkey($keypair)
|
||||
{
|
||||
if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair must be 96 bytes long');
|
||||
}
|
||||
return self::substr($keypair, 0, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $keypair
|
||||
* @return string
|
||||
* @throws RangeException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function publickey($keypair)
|
||||
{
|
||||
if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
|
||||
throw new RangeException('crypto_sign keypair must be 96 bytes long');
|
||||
}
|
||||
return self::substr($keypair, 64, 32);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function publickey_from_secretkey($sk)
|
||||
{
|
||||
/** @var string $sk */
|
||||
$sk = hash('sha512', self::substr($sk, 0, 32), true);
|
||||
$sk[0] = self::intToChr(
|
||||
self::chrToInt($sk[0]) & 248
|
||||
);
|
||||
$sk[31] = self::intToChr(
|
||||
(self::chrToInt($sk[31]) & 63) | 64
|
||||
);
|
||||
return self::sk_to_pk($sk);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function pk_to_curve25519($pk)
|
||||
{
|
||||
if (self::small_order($pk)) {
|
||||
throw new SodiumException('Public key is on a small order');
|
||||
}
|
||||
$A = self::ge_frombytes_negate_vartime($pk);
|
||||
$p1 = self::ge_mul_l($A);
|
||||
if (!self::fe_isnonzero($p1->X)) {
|
||||
throw new SodiumException('Unexpected zero result');
|
||||
}
|
||||
|
||||
# fe_1(one_minus_y);
|
||||
# fe_sub(one_minus_y, one_minus_y, A.Y);
|
||||
# fe_invert(one_minus_y, one_minus_y);
|
||||
$one_minux_y = self::fe_invert(
|
||||
self::fe_sub(
|
||||
self::fe_1(),
|
||||
$A->Y
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
# fe_1(x);
|
||||
# fe_add(x, x, A.Y);
|
||||
# fe_mul(x, x, one_minus_y);
|
||||
$x = self::fe_mul(
|
||||
self::fe_add(self::fe_1(), $A->Y),
|
||||
$one_minux_y
|
||||
);
|
||||
|
||||
# fe_tobytes(curve25519_pk, x);
|
||||
return self::fe_tobytes($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sk_to_pk($sk)
|
||||
{
|
||||
return self::ge_p3_tobytes(
|
||||
self::ge_scalarmult_base(
|
||||
self::substr($sk, 0, 32)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign($message, $sk)
|
||||
{
|
||||
/** @var string $signature */
|
||||
$signature = self::sign_detached($message, $sk);
|
||||
return $signature . $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message A signed message
|
||||
* @param string $pk Public key
|
||||
* @return string Message (without signature)
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign_open($message, $pk)
|
||||
{
|
||||
/** @var string $signature */
|
||||
$signature = self::substr($message, 0, 64);
|
||||
|
||||
/** @var string $message */
|
||||
$message = self::substr($message, 64);
|
||||
|
||||
if (self::verify_detached($signature, $message, $pk)) {
|
||||
return $message;
|
||||
}
|
||||
throw new SodiumException('Invalid signature');
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $sk
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sign_detached($message, $sk)
|
||||
{
|
||||
# crypto_hash_sha512(az, sk, 32);
|
||||
$az = hash('sha512', self::substr($sk, 0, 32), true);
|
||||
|
||||
# az[0] &= 248;
|
||||
# az[31] &= 63;
|
||||
# az[31] |= 64;
|
||||
$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
|
||||
$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
|
||||
|
||||
# crypto_hash_sha512_init(&hs);
|
||||
# crypto_hash_sha512_update(&hs, az + 32, 32);
|
||||
# crypto_hash_sha512_update(&hs, m, mlen);
|
||||
# crypto_hash_sha512_final(&hs, nonce);
|
||||
$hs = hash_init('sha512');
|
||||
hash_update($hs, self::substr($az, 32, 32));
|
||||
hash_update($hs, $message);
|
||||
$nonceHash = hash_final($hs, true);
|
||||
|
||||
# memmove(sig + 32, sk + 32, 32);
|
||||
$pk = self::substr($sk, 32, 32);
|
||||
|
||||
# sc_reduce(nonce);
|
||||
# ge_scalarmult_base(&R, nonce);
|
||||
# ge_p3_tobytes(sig, &R);
|
||||
$nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
|
||||
$sig = self::ge_p3_tobytes(
|
||||
self::ge_scalarmult_base($nonce)
|
||||
);
|
||||
|
||||
# crypto_hash_sha512_init(&hs);
|
||||
# crypto_hash_sha512_update(&hs, sig, 64);
|
||||
# crypto_hash_sha512_update(&hs, m, mlen);
|
||||
# crypto_hash_sha512_final(&hs, hram);
|
||||
$hs = hash_init('sha512');
|
||||
hash_update($hs, self::substr($sig, 0, 32));
|
||||
hash_update($hs, self::substr($pk, 0, 32));
|
||||
hash_update($hs, $message);
|
||||
$hramHash = hash_final($hs, true);
|
||||
|
||||
# sc_reduce(hram);
|
||||
# sc_muladd(sig + 32, hram, az, nonce);
|
||||
$hram = self::sc_reduce($hramHash);
|
||||
$sigAfter = self::sc_muladd($hram, $az, $nonce);
|
||||
$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
|
||||
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($az);
|
||||
} catch (SodiumException $ex) {
|
||||
$az = null;
|
||||
}
|
||||
return $sig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $sig
|
||||
* @param string $message
|
||||
* @param string $pk
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function verify_detached($sig, $message, $pk)
|
||||
{
|
||||
if (self::strlen($sig) < 64) {
|
||||
throw new SodiumException('Signature is too short');
|
||||
}
|
||||
if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
|
||||
throw new SodiumException('S < L - Invalid signature');
|
||||
}
|
||||
if (self::small_order($sig)) {
|
||||
throw new SodiumException('Signature is on too small of an order');
|
||||
}
|
||||
if ((self::chrToInt($sig[63]) & 224) !== 0) {
|
||||
throw new SodiumException('Invalid signature');
|
||||
}
|
||||
$d = 0;
|
||||
for ($i = 0; $i < 32; ++$i) {
|
||||
$d |= self::chrToInt($pk[$i]);
|
||||
}
|
||||
if ($d === 0) {
|
||||
throw new SodiumException('All zero public key');
|
||||
}
|
||||
|
||||
/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
|
||||
$orig = ParagonIE_Sodium_Compat::$fastMult;
|
||||
|
||||
// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
|
||||
ParagonIE_Sodium_Compat::$fastMult = true;
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
|
||||
$A = self::ge_frombytes_negate_vartime($pk);
|
||||
|
||||
/** @var string $hDigest */
|
||||
$hDigest = hash(
|
||||
'sha512',
|
||||
self::substr($sig, 0, 32) .
|
||||
self::substr($pk, 0, 32) .
|
||||
$message,
|
||||
true
|
||||
);
|
||||
|
||||
/** @var string $h */
|
||||
$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
|
||||
$R = self::ge_double_scalarmult_vartime(
|
||||
$h,
|
||||
$A,
|
||||
self::substr($sig, 32)
|
||||
);
|
||||
|
||||
/** @var string $rcheck */
|
||||
$rcheck = self::ge_tobytes($R);
|
||||
|
||||
// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
|
||||
ParagonIE_Sodium_Compat::$fastMult = $orig;
|
||||
|
||||
return self::verify_32($rcheck, self::substr($sig, 0, 32));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $S
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function check_S_lt_L($S)
|
||||
{
|
||||
if (self::strlen($S) < 32) {
|
||||
throw new SodiumException('Signature must be 32 bytes');
|
||||
}
|
||||
static $L = array(
|
||||
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
|
||||
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
|
||||
);
|
||||
/** @var array<int, int> $L */
|
||||
$c = 0;
|
||||
$n = 1;
|
||||
$i = 32;
|
||||
|
||||
do {
|
||||
--$i;
|
||||
$x = self::chrToInt($S[$i]);
|
||||
$c |= (
|
||||
(($x - $L[$i]) >> 8) & $n
|
||||
);
|
||||
$n &= (
|
||||
(($x ^ $L[$i]) - 1) >> 8
|
||||
);
|
||||
} while ($i !== 0);
|
||||
|
||||
return $c === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $R
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function small_order($R)
|
||||
{
|
||||
static $blacklist = array(
|
||||
/* 0 (order 4) */
|
||||
array(
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
),
|
||||
/* 1 (order 1) */
|
||||
array(
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
),
|
||||
/* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
|
||||
array(
|
||||
0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
|
||||
0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
|
||||
0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
|
||||
0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
|
||||
),
|
||||
/* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
|
||||
array(
|
||||
0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
|
||||
0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
|
||||
0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
|
||||
0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
|
||||
),
|
||||
/* p-1 (order 2) */
|
||||
array(
|
||||
0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
|
||||
0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
|
||||
0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
|
||||
0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
|
||||
),
|
||||
/* p (order 4) */
|
||||
array(
|
||||
0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
|
||||
0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
|
||||
0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
|
||||
0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
|
||||
),
|
||||
/* p+1 (order 1) */
|
||||
array(
|
||||
0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
|
||||
array(
|
||||
0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
|
||||
array(
|
||||
0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
|
||||
),
|
||||
/* 2p-1 (order 2) */
|
||||
array(
|
||||
0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
),
|
||||
/* 2p (order 4) */
|
||||
array(
|
||||
0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
),
|
||||
/* 2p+1 (order 1) */
|
||||
array(
|
||||
0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
)
|
||||
);
|
||||
/** @var array<int, array<int, int>> $blacklist */
|
||||
$countBlacklist = count($blacklist);
|
||||
|
||||
for ($i = 0; $i < $countBlacklist; ++$i) {
|
||||
$c = 0;
|
||||
for ($j = 0; $j < 32; ++$j) {
|
||||
$c |= self::chrToInt($R[$j]) ^ $blacklist[$i][$j];
|
||||
}
|
||||
if ($c === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
127
wp-includes/sodium_compat/src/Core32/HChaCha20.php
Normal file
127
wp-includes/sodium_compat/src/Core32/HChaCha20.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_HChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20
|
||||
{
|
||||
/**
|
||||
* @param string $in
|
||||
* @param string $key
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hChaCha20($in = '', $key = '', $c = null)
|
||||
{
|
||||
$ctx = array();
|
||||
|
||||
if ($c === null) {
|
||||
$ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
|
||||
$ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
|
||||
$ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
|
||||
$ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
|
||||
} else {
|
||||
$ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4));
|
||||
$ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4));
|
||||
$ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4));
|
||||
$ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4));
|
||||
}
|
||||
$ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4));
|
||||
$ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4));
|
||||
$ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4));
|
||||
$ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4));
|
||||
$ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4));
|
||||
$ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4));
|
||||
$ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4));
|
||||
$ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4));
|
||||
$ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4));
|
||||
$ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4));
|
||||
$ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4));
|
||||
$ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4));
|
||||
|
||||
return self::hChaCha20Bytes($ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ctx
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function hChaCha20Bytes(array $ctx)
|
||||
{
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x0 */
|
||||
$x0 = $ctx[0];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x1 */
|
||||
$x1 = $ctx[1];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x2 */
|
||||
$x2 = $ctx[2];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x3 */
|
||||
$x3 = $ctx[3];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x4 */
|
||||
$x4 = $ctx[4];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x5 */
|
||||
$x5 = $ctx[5];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x6 */
|
||||
$x6 = $ctx[6];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x7 */
|
||||
$x7 = $ctx[7];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x8 */
|
||||
$x8 = $ctx[8];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x9 */
|
||||
$x9 = $ctx[9];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x10 */
|
||||
$x10 = $ctx[10];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x11 */
|
||||
$x11 = $ctx[11];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x12 */
|
||||
$x12 = $ctx[12];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x13 */
|
||||
$x13 = $ctx[13];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x14 */
|
||||
$x14 = $ctx[14];
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $x15 */
|
||||
$x15 = $ctx[15];
|
||||
|
||||
for ($i = 0; $i < 10; ++$i) {
|
||||
# QUARTERROUND( x0, x4, x8, x12)
|
||||
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
|
||||
|
||||
# QUARTERROUND( x1, x5, x9, x13)
|
||||
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
|
||||
|
||||
# QUARTERROUND( x2, x6, x10, x14)
|
||||
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
|
||||
|
||||
# QUARTERROUND( x3, x7, x11, x15)
|
||||
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
|
||||
|
||||
# QUARTERROUND( x0, x5, x10, x15)
|
||||
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
|
||||
|
||||
# QUARTERROUND( x1, x6, x11, x12)
|
||||
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
|
||||
|
||||
# QUARTERROUND( x2, x7, x8, x13)
|
||||
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
|
||||
|
||||
# QUARTERROUND( x3, x4, x9, x14)
|
||||
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
|
||||
}
|
||||
|
||||
return $x0->toReverseString() .
|
||||
$x1->toReverseString() .
|
||||
$x2->toReverseString() .
|
||||
$x3->toReverseString() .
|
||||
$x12->toReverseString() .
|
||||
$x13->toReverseString() .
|
||||
$x14->toReverseString() .
|
||||
$x15->toReverseString();
|
||||
}
|
||||
}
|
141
wp-includes/sodium_compat/src/Core32/HSalsa20.php
Normal file
141
wp-includes/sodium_compat/src/Core32/HSalsa20.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_HSalsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_HSalsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_HSalsa20 extends ParagonIE_Sodium_Core32_Salsa20
|
||||
{
|
||||
/**
|
||||
* Calculate an hsalsa20 hash of a single block
|
||||
*
|
||||
* HSalsa20 doesn't have a counter and will never be used for more than
|
||||
* one block (used to derive a subkey for xsalsa20).
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $k
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hsalsa20($in, $k, $c = null)
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x4
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x5
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x6
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x7
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x8
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x9
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x10
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x11
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x12
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x13
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x14
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x15
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j4
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j5
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j6
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j7
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j8
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j9
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j10
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j11
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j12
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j13
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j14
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j15
|
||||
*/
|
||||
if (self::strlen($k) < 32) {
|
||||
throw new RangeException('Key must be 32 bytes long');
|
||||
}
|
||||
if ($c === null) {
|
||||
$x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
|
||||
$x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
|
||||
$x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
|
||||
$x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
|
||||
} else {
|
||||
$x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4));
|
||||
$x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4));
|
||||
$x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4));
|
||||
$x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4));
|
||||
}
|
||||
$x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4));
|
||||
$x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4));
|
||||
$x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4));
|
||||
$x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4));
|
||||
$x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4));
|
||||
$x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4));
|
||||
$x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4));
|
||||
$x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4));
|
||||
$x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4));
|
||||
$x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4));
|
||||
$x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4));
|
||||
$x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4));
|
||||
|
||||
for ($i = self::ROUNDS; $i > 0; $i -= 2) {
|
||||
$x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7));
|
||||
$x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9));
|
||||
$x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13));
|
||||
$x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18));
|
||||
|
||||
$x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7));
|
||||
$x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9));
|
||||
$x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13));
|
||||
$x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18));
|
||||
|
||||
$x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7));
|
||||
$x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9));
|
||||
$x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13));
|
||||
$x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18));
|
||||
|
||||
$x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7));
|
||||
$x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9));
|
||||
$x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13));
|
||||
$x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18));
|
||||
|
||||
$x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7));
|
||||
$x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9));
|
||||
$x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13));
|
||||
$x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18));
|
||||
|
||||
$x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7));
|
||||
$x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9));
|
||||
$x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13));
|
||||
$x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18));
|
||||
|
||||
$x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7));
|
||||
$x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9));
|
||||
$x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13));
|
||||
$x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18));
|
||||
|
||||
$x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7));
|
||||
$x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9));
|
||||
$x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13));
|
||||
$x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18));
|
||||
}
|
||||
|
||||
return $x0->toReverseString() .
|
||||
$x5->toReverseString() .
|
||||
$x10->toReverseString() .
|
||||
$x15->toReverseString() .
|
||||
$x6->toReverseString() .
|
||||
$x7->toReverseString() .
|
||||
$x8->toReverseString() .
|
||||
$x9->toReverseString();
|
||||
}
|
||||
}
|
735
wp-includes/sodium_compat/src/Core32/Int32.php
Normal file
735
wp-includes/sodium_compat/src/Core32/Int32.php
Normal file
@ -0,0 +1,735 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Int32
|
||||
*
|
||||
* Encapsulates a 32-bit integer.
|
||||
*
|
||||
* These are immutable. It always returns a new instance.
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Int32
|
||||
{
|
||||
/**
|
||||
* @var array<int, int> - two 16-bit integers
|
||||
*
|
||||
* 0 is the higher 16 bits
|
||||
* 1 is the lower 16 bits
|
||||
*/
|
||||
public $limbs = array(0, 0);
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $overflow = 0;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $unsignedInt = false;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Int32 constructor.
|
||||
* @param array $array
|
||||
* @param bool $unsignedInt
|
||||
*/
|
||||
public function __construct($array = array(0, 0), $unsignedInt = false)
|
||||
{
|
||||
$this->limbs = array(
|
||||
(int) $array[0],
|
||||
(int) $array[1]
|
||||
);
|
||||
$this->overflow = 0;
|
||||
$this->unsignedInt = $unsignedInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two int32 objects
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $addend
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function addInt32(ParagonIE_Sodium_Core32_Int32 $addend)
|
||||
{
|
||||
$i0 = $this->limbs[0];
|
||||
$i1 = $this->limbs[1];
|
||||
$j0 = $addend->limbs[0];
|
||||
$j1 = $addend->limbs[1];
|
||||
|
||||
$r1 = $i1 + ($j1 & 0xffff);
|
||||
$carry = $r1 >> 16;
|
||||
|
||||
$r0 = $i0 + ($j0 & 0xffff) + $carry;
|
||||
$carry = $r0 >> 16;
|
||||
|
||||
$r0 &= 0xffff;
|
||||
$r1 &= 0xffff;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32(
|
||||
array($r0, $r1)
|
||||
);
|
||||
$return->overflow = $carry;
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a normal integer to an int32 object
|
||||
*
|
||||
* @param int $int
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function addInt($int)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
/** @var int $int */
|
||||
$int = (int) $int;
|
||||
|
||||
$int = (int) $int;
|
||||
|
||||
$i0 = $this->limbs[0];
|
||||
$i1 = $this->limbs[1];
|
||||
|
||||
$r1 = $i1 + ($int & 0xffff);
|
||||
$carry = $r1 >> 16;
|
||||
|
||||
$r0 = $i0 + (($int >> 16) & 0xffff) + $carry;
|
||||
$carry = $r0 >> 16;
|
||||
$r0 &= 0xffff;
|
||||
$r1 &= 0xffff;
|
||||
$return = new ParagonIE_Sodium_Core32_Int32(
|
||||
array($r0, $r1)
|
||||
);
|
||||
$return->overflow = $carry;
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return int
|
||||
*/
|
||||
public function compareInt($b = 0)
|
||||
{
|
||||
$gt = 0;
|
||||
$eq = 1;
|
||||
|
||||
$i = 2;
|
||||
$j = 0;
|
||||
while ($i > 0) {
|
||||
--$i;
|
||||
/** @var int $x1 */
|
||||
$x1 = $this->limbs[$i];
|
||||
/** @var int $x2 */
|
||||
$x2 = ($b >> ($j << 4)) & 0xffff;
|
||||
/** @var int $gt */
|
||||
$gt |= (($x2 - $x1) >> 8) & $eq;
|
||||
/** @var int $eq */
|
||||
$eq &= (($x2 ^ $x1) - 1) >> 8;
|
||||
}
|
||||
return ($gt + $gt - $eq) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $m
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function mask($m = 0)
|
||||
{
|
||||
/** @var int $hi */
|
||||
$hi = ($m >> 16) & 0xffff;
|
||||
/** @var int $lo */
|
||||
$lo = ($m & 0xffff);
|
||||
return new ParagonIE_Sodium_Core32_Int32(
|
||||
array(
|
||||
(int) ($this->limbs[0] & $hi),
|
||||
(int) ($this->limbs[1] & $lo)
|
||||
),
|
||||
$this->unsignedInt
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $int
|
||||
* @param int $size
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function mulInt($int = 0, $size = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2);
|
||||
/** @var int $int */
|
||||
$int = (int) $int;
|
||||
/** @var int $size */
|
||||
$size = (int) $size;
|
||||
|
||||
if (!$size) {
|
||||
$size = 31;
|
||||
}
|
||||
/** @var int $size */
|
||||
|
||||
$a = clone $this;
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
// Initialize:
|
||||
$ret0 = 0;
|
||||
$ret1 = 0;
|
||||
$a0 = $a->limbs[0];
|
||||
$a1 = $a->limbs[1];
|
||||
|
||||
/** @var int $size */
|
||||
/** @var int $i */
|
||||
for ($i = $size; $i >= 0; --$i) {
|
||||
$m = (int) (-($int & 1));
|
||||
$x0 = $a0 & $m;
|
||||
$x1 = $a1 & $m;
|
||||
|
||||
$ret1 += $x1;
|
||||
$c = $ret1 >> 16;
|
||||
|
||||
$ret0 += $x0 + $c;
|
||||
|
||||
$ret0 &= 0xffff;
|
||||
$ret1 &= 0xffff;
|
||||
|
||||
$a1 = ($a1 << 1);
|
||||
$x1 = $a1 >> 16;
|
||||
$a0 = ($a0 << 1) | $x1;
|
||||
$a0 &= 0xffff;
|
||||
$a1 &= 0xffff;
|
||||
$int >>= 1;
|
||||
}
|
||||
$return->limbs[0] = $ret0;
|
||||
$return->limbs[1] = $ret1;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $int
|
||||
* @param int $size
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function mulInt32(ParagonIE_Sodium_Core32_Int32 $int, $size = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2);
|
||||
if (!$size) {
|
||||
$size = 31;
|
||||
}
|
||||
/** @var int $size */
|
||||
|
||||
$a = clone $this;
|
||||
$b = clone $int;
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
// Initialize:
|
||||
$ret0 = 0;
|
||||
$ret1 = 0;
|
||||
$a0 = $a->limbs[0];
|
||||
$a1 = $a->limbs[1];
|
||||
$b0 = $b->limbs[0];
|
||||
$b1 = $b->limbs[1];
|
||||
|
||||
/** @var int $size */
|
||||
/** @var int $i */
|
||||
for ($i = $size; $i >= 0; --$i) {
|
||||
$m = (int) (-($b1 & 1));
|
||||
$x0 = $a0 & $m;
|
||||
$x1 = $a1 & $m;
|
||||
|
||||
$ret1 += $x1;
|
||||
$c = $ret1 >> 16;
|
||||
|
||||
$ret0 += $x0 + $c;
|
||||
|
||||
$ret0 &= 0xffff;
|
||||
$ret1 &= 0xffff;
|
||||
|
||||
$a1 = ($a1 << 1);
|
||||
$x1 = $a1 >> 16;
|
||||
$a0 = ($a0 << 1) | $x1;
|
||||
$a0 &= 0xffff;
|
||||
$a1 &= 0xffff;
|
||||
|
||||
$x0 = ($b0 & 1) << 16;
|
||||
$b0 = ($b0 >> 1);
|
||||
$b1 = (($b1 | $x0) >> 1);
|
||||
|
||||
$b0 &= 0xffff;
|
||||
$b1 &= 0xffff;
|
||||
|
||||
}
|
||||
$return->limbs[0] = $ret0;
|
||||
$return->limbs[1] = $ret1;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* OR this 32-bit integer with another.
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function orInt32(ParagonIE_Sodium_Core32_Int32 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->limbs = array(
|
||||
(int) ($this->limbs[0] | $b->limbs[0]),
|
||||
(int) ($this->limbs[1] | $b->limbs[1])
|
||||
);
|
||||
/** @var int overflow */
|
||||
$return->overflow = $this->overflow | $b->overflow;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return bool
|
||||
*/
|
||||
public function isGreaterThan($b = 0)
|
||||
{
|
||||
return $this->compareInt($b) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return bool
|
||||
*/
|
||||
public function isLessThanInt($b = 0)
|
||||
{
|
||||
return $this->compareInt($b) < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
*/
|
||||
public function rotateLeft($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 31;
|
||||
if ($c === 0) {
|
||||
// NOP, but we want a copy.
|
||||
$return->limbs = $this->limbs;
|
||||
} else {
|
||||
/** @var int $c */
|
||||
|
||||
/** @var int $idx_shift */
|
||||
$idx_shift = ($c >> 4) & 1;
|
||||
|
||||
/** @var int $sub_shift */
|
||||
$sub_shift = $c & 15;
|
||||
|
||||
/** @var array<int, int> $limbs */
|
||||
$limbs =& $return->limbs;
|
||||
|
||||
/** @var array<int, int> $myLimbs */
|
||||
$myLimbs =& $this->limbs;
|
||||
|
||||
for ($i = 1; $i >= 0; --$i) {
|
||||
/** @var int $j */
|
||||
$j = ($i + $idx_shift) & 1;
|
||||
/** @var int $k */
|
||||
$k = ($i + $idx_shift + 1) & 1;
|
||||
$limbs[$i] = (int) (
|
||||
(
|
||||
((int) ($myLimbs[$j]) << $sub_shift)
|
||||
|
|
||||
((int) ($myLimbs[$k]) >> (16 - $sub_shift))
|
||||
) & 0xffff
|
||||
);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate to the right
|
||||
*
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
*/
|
||||
public function rotateRight($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 31;
|
||||
/** @var int $c */
|
||||
if ($c === 0) {
|
||||
// NOP, but we want a copy.
|
||||
$return->limbs = $this->limbs;
|
||||
} else {
|
||||
/** @var int $c */
|
||||
|
||||
/** @var int $idx_shift */
|
||||
$idx_shift = ($c >> 4) & 1;
|
||||
|
||||
/** @var int $sub_shift */
|
||||
$sub_shift = $c & 15;
|
||||
|
||||
/** @var array<int, int> $limbs */
|
||||
$limbs =& $return->limbs;
|
||||
|
||||
/** @var array<int, int> $myLimbs */
|
||||
$myLimbs =& $this->limbs;
|
||||
|
||||
for ($i = 1; $i >= 0; --$i) {
|
||||
/** @var int $j */
|
||||
$j = ($i - $idx_shift) & 1;
|
||||
/** @var int $k */
|
||||
$k = ($i - $idx_shift - 1) & 1;
|
||||
$limbs[$i] = (int) (
|
||||
(
|
||||
((int) ($myLimbs[$j]) >> (int) ($sub_shift))
|
||||
|
|
||||
((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift)))
|
||||
) & 0xffff
|
||||
);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $bool
|
||||
* @return self
|
||||
*/
|
||||
public function setUnsignedInt($bool = false)
|
||||
{
|
||||
$this->unsignedInt = !empty($bool);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function shiftLeft($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
/** @var int $c */
|
||||
if ($c === 0) {
|
||||
$return->limbs = $this->limbs;
|
||||
} elseif ($c < 0) {
|
||||
/** @var int $c */
|
||||
return $this->shiftRight(-$c);
|
||||
} else {
|
||||
/** @var int $c */
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[1] << $c;
|
||||
$return->limbs[1] = (int)($tmp & 0xffff);
|
||||
/** @var int $carry */
|
||||
$carry = $tmp >> 16;
|
||||
|
||||
/** @var int $tmp */
|
||||
$tmp = ($this->limbs[0] << $c) | ($carry & 0xffff);
|
||||
$return->limbs[0] = (int) ($tmp & 0xffff);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedOperand
|
||||
*/
|
||||
public function shiftRight($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
/** @var int $c */
|
||||
if ($c >= 16) {
|
||||
$return->limbs = array(
|
||||
(int) ($this->overflow & 0xffff),
|
||||
(int) ($this->limbs[0])
|
||||
);
|
||||
$return->overflow = $this->overflow >> 16;
|
||||
return $return->shiftRight($c & 15);
|
||||
}
|
||||
if ($c === 0) {
|
||||
$return->limbs = $this->limbs;
|
||||
} elseif ($c < 0) {
|
||||
/** @var int $c */
|
||||
return $this->shiftLeft(-$c);
|
||||
} else {
|
||||
if (is_null($c)) {
|
||||
throw new TypeError();
|
||||
}
|
||||
/** @var int $c */
|
||||
// $return->limbs[0] = (int) (($this->limbs[0] >> $c) & 0xffff);
|
||||
$carryLeft = (int) ($this->overflow & ((1 << ($c + 1)) - 1));
|
||||
$return->limbs[0] = (int) ((($this->limbs[0] >> $c) | ($carryLeft << (16 - $c))) & 0xffff);
|
||||
$carryRight = (int) ($this->limbs[0] & ((1 << ($c + 1)) - 1));
|
||||
$return->limbs[1] = (int) ((($this->limbs[1] >> $c) | ($carryRight << (16 - $c))) & 0xffff);
|
||||
$return->overflow >>= $c;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract a normal integer from an int32 object.
|
||||
*
|
||||
* @param int $int
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function subInt($int)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
/** @var int $int */
|
||||
$int = (int) $int;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[1] - ($int & 0xffff);
|
||||
/** @var int $carry */
|
||||
$carry = $tmp >> 16;
|
||||
$return->limbs[1] = (int) ($tmp & 0xffff);
|
||||
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[0] - (($int >> 16) & 0xffff) + $carry;
|
||||
$return->limbs[0] = (int) ($tmp & 0xffff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract two int32 objects from each other
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function subInt32(ParagonIE_Sodium_Core32_Int32 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[1] - ($b->limbs[1] & 0xffff);
|
||||
/** @var int $carry */
|
||||
$carry = $tmp >> 16;
|
||||
$return->limbs[1] = (int) ($tmp & 0xffff);
|
||||
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[0] - ($b->limbs[0] & 0xffff) + $carry;
|
||||
$return->limbs[0] = (int) ($tmp & 0xffff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* XOR this 32-bit integer with another.
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int32 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function xorInt32(ParagonIE_Sodium_Core32_Int32 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->limbs = array(
|
||||
(int) ($this->limbs[0] ^ $b->limbs[0]),
|
||||
(int) ($this->limbs[1] ^ $b->limbs[1])
|
||||
);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $signed
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromInt($signed)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($signed, 'int', 1);;
|
||||
/** @var int $signed */
|
||||
$signed = (int) $signed;
|
||||
|
||||
return new ParagonIE_Sodium_Core32_Int32(
|
||||
array(
|
||||
(int) (($signed >> 16) & 0xffff),
|
||||
(int) ($signed & 0xffff)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromString($string)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1);
|
||||
$string = (string) $string;
|
||||
if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) {
|
||||
throw new RangeException(
|
||||
'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
|
||||
$return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8);
|
||||
$return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff);
|
||||
$return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8);
|
||||
$return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromReverseString($string)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1);
|
||||
$string = (string) $string;
|
||||
if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 4) {
|
||||
throw new RangeException(
|
||||
'String must be 4 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
|
||||
$return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8);
|
||||
$return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff);
|
||||
$return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8);
|
||||
$return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return array((int) ($this->limbs[0] << 16 | $this->limbs[1]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function toInt()
|
||||
{
|
||||
return (int) (
|
||||
(($this->limbs[0] & 0xffff) << 16)
|
||||
|
|
||||
($this->limbs[1] & 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function toInt32()
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->limbs[0] = (int) ($this->limbs[0] & 0xffff);
|
||||
$return->limbs[1] = (int) ($this->limbs[1] & 0xffff);
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->overflow = (int) ($this->overflow & 0x7fffffff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function toInt64()
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
if ($this->unsignedInt) {
|
||||
$return->limbs[0] += (($this->overflow >> 16) & 0xffff);
|
||||
$return->limbs[1] += (($this->overflow) & 0xffff);
|
||||
} else {
|
||||
$neg = -(($this->limbs[0] >> 15) & 1);
|
||||
$return->limbs[0] = (int)($neg & 0xffff);
|
||||
$return->limbs[1] = (int)($neg & 0xffff);
|
||||
}
|
||||
$return->limbs[2] = (int) ($this->limbs[0] & 0xffff);
|
||||
$return->limbs[3] = (int) ($this->limbs[1] & 0xffff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function toReverseString()
|
||||
{
|
||||
return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
try {
|
||||
return $this->toString();
|
||||
} catch (TypeError $ex) {
|
||||
// PHP engine can't handle exceptions from __toString()
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
923
wp-includes/sodium_compat/src/Core32/Int64.php
Normal file
923
wp-includes/sodium_compat/src/Core32/Int64.php
Normal file
@ -0,0 +1,923 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Int64
|
||||
*
|
||||
* Encapsulates a 64-bit integer.
|
||||
*
|
||||
* These are immutable. It always returns a new instance.
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Int64
|
||||
{
|
||||
/**
|
||||
* @var array<int, int> - four 16-bit integers
|
||||
*/
|
||||
public $limbs = array(0, 0, 0, 0);
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $overflow = 0;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $unsignedInt = false;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Int64 constructor.
|
||||
* @param array $array
|
||||
* @param bool $unsignedInt
|
||||
*/
|
||||
public function __construct($array = array(0, 0, 0, 0), $unsignedInt = false)
|
||||
{
|
||||
$this->limbs = array(
|
||||
(int) $array[0],
|
||||
(int) $array[1],
|
||||
(int) $array[2],
|
||||
(int) $array[3]
|
||||
);
|
||||
$this->overflow = 0;
|
||||
$this->unsignedInt = $unsignedInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two int64 objects
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $addend
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend)
|
||||
{
|
||||
$i0 = $this->limbs[0];
|
||||
$i1 = $this->limbs[1];
|
||||
$i2 = $this->limbs[2];
|
||||
$i3 = $this->limbs[3];
|
||||
$j0 = $addend->limbs[0];
|
||||
$j1 = $addend->limbs[1];
|
||||
$j2 = $addend->limbs[2];
|
||||
$j3 = $addend->limbs[3];
|
||||
|
||||
$r3 = $i3 + ($j3 & 0xffff);
|
||||
$carry = $r3 >> 16;
|
||||
|
||||
$r2 = $i2 + ($j2 & 0xffff) + $carry;
|
||||
$carry = $r2 >> 16;
|
||||
|
||||
$r1 = $i1 + ($j1 & 0xffff) + $carry;
|
||||
$carry = $r1 >> 16;
|
||||
|
||||
$r0 = $i0 + ($j0 & 0xffff) + $carry;
|
||||
$carry = $r0 >> 16;
|
||||
|
||||
$r0 &= 0xffff;
|
||||
$r1 &= 0xffff;
|
||||
$r2 &= 0xffff;
|
||||
$r3 &= 0xffff;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int64(
|
||||
array($r0, $r1, $r2, $r3)
|
||||
);
|
||||
$return->overflow = $carry;
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a normal integer to an int64 object
|
||||
*
|
||||
* @param int $int
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function addInt($int)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
/** @var int $int */
|
||||
$int = (int) $int;
|
||||
|
||||
$i0 = $this->limbs[0];
|
||||
$i1 = $this->limbs[1];
|
||||
$i2 = $this->limbs[2];
|
||||
$i3 = $this->limbs[3];
|
||||
|
||||
$r3 = $i3 + ($int & 0xffff);
|
||||
$carry = $r3 >> 16;
|
||||
|
||||
$r2 = $i2 + (($int >> 16) & 0xffff) + $carry;
|
||||
$carry = $r2 >> 16;
|
||||
|
||||
$r1 = $i1 + $carry;
|
||||
$carry = $r1 >> 16;
|
||||
|
||||
$r0 = $i0 + $carry;
|
||||
$carry = $r0 >> 16;
|
||||
|
||||
$r0 &= 0xffff;
|
||||
$r1 &= 0xffff;
|
||||
$r2 &= 0xffff;
|
||||
$r3 &= 0xffff;
|
||||
$return = new ParagonIE_Sodium_Core32_Int64(
|
||||
array($r0, $r1, $r2, $r3)
|
||||
);
|
||||
$return->overflow = $carry;
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return int
|
||||
*/
|
||||
public function compareInt($b = 0)
|
||||
{
|
||||
$gt = 0;
|
||||
$eq = 1;
|
||||
|
||||
$i = 4;
|
||||
$j = 0;
|
||||
while ($i > 0) {
|
||||
--$i;
|
||||
/** @var int $x1 */
|
||||
$x1 = $this->limbs[$i];
|
||||
/** @var int $x2 */
|
||||
$x2 = ($b >> ($j << 4)) & 0xffff;
|
||||
/** int */
|
||||
$gt |= (($x2 - $x1) >> 8) & $eq;
|
||||
/** int */
|
||||
$eq &= (($x2 ^ $x1) - 1) >> 8;
|
||||
}
|
||||
return ($gt + $gt - $eq) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return bool
|
||||
*/
|
||||
public function isGreaterThan($b = 0)
|
||||
{
|
||||
return $this->compareInt($b) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $b
|
||||
* @return bool
|
||||
*/
|
||||
public function isLessThanInt($b = 0)
|
||||
{
|
||||
return $this->compareInt($b) < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $hi
|
||||
* @param int $lo
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function mask64($hi = 0, $lo = 0)
|
||||
{
|
||||
/** @var int $a */
|
||||
$a = ($hi >> 16) & 0xffff;
|
||||
/** @var int $b */
|
||||
$b = ($hi) & 0xffff;
|
||||
/** @var int $c */
|
||||
$c = ($lo >> 16) & 0xffff;
|
||||
/** @var int $d */
|
||||
$d = ($lo & 0xffff);
|
||||
return new ParagonIE_Sodium_Core32_Int64(
|
||||
array(
|
||||
$this->limbs[0] & $a,
|
||||
$this->limbs[1] & $b,
|
||||
$this->limbs[2] & $c,
|
||||
$this->limbs[3] & $d
|
||||
),
|
||||
$this->unsignedInt
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $int
|
||||
* @param int $size
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
public function mulInt($int = 0, $size = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2);
|
||||
/** @var int $int */
|
||||
$int = (int) $int;
|
||||
/** @var int $size */
|
||||
$size = (int) $size;
|
||||
|
||||
if (!$size) {
|
||||
$size = 63;
|
||||
}
|
||||
|
||||
$a = clone $this;
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
// Initialize:
|
||||
$ret0 = 0;
|
||||
$ret1 = 0;
|
||||
$ret2 = 0;
|
||||
$ret3 = 0;
|
||||
$a0 = $a->limbs[0];
|
||||
$a1 = $a->limbs[1];
|
||||
$a2 = $a->limbs[2];
|
||||
$a3 = $a->limbs[3];
|
||||
|
||||
/** @var int $size */
|
||||
/** @var int $i */
|
||||
for ($i = $size; $i >= 0; --$i) {
|
||||
$mask = -($int & 1);
|
||||
$x0 = $a0 & $mask;
|
||||
$x1 = $a1 & $mask;
|
||||
$x2 = $a2 & $mask;
|
||||
$x3 = $a3 & $mask;
|
||||
|
||||
$ret3 += $x3;
|
||||
$c = $ret3 >> 16;
|
||||
|
||||
$ret2 += $x2 + $c;
|
||||
$c = $ret2 >> 16;
|
||||
|
||||
$ret1 += $x1 + $c;
|
||||
$c = $ret1 >> 16;
|
||||
|
||||
$ret0 += $x0 + $c;
|
||||
|
||||
$ret0 &= 0xffff;
|
||||
$ret1 &= 0xffff;
|
||||
$ret2 &= 0xffff;
|
||||
$ret3 &= 0xffff;
|
||||
|
||||
$a3 = $a3 << 1;
|
||||
$x3 = $a3 >> 16;
|
||||
$a2 = ($a2 << 1) | $x3;
|
||||
$x2 = $a2 >> 16;
|
||||
$a1 = ($a1 << 1) | $x2;
|
||||
$x1 = $a1 >> 16;
|
||||
$a0 = ($a0 << 1) | $x1;
|
||||
$a0 &= 0xffff;
|
||||
$a1 &= 0xffff;
|
||||
$a2 &= 0xffff;
|
||||
$a3 &= 0xffff;
|
||||
|
||||
$int >>= 1;
|
||||
$return->limbs[0] = $ret0;
|
||||
$return->limbs[1] = $ret1;
|
||||
$return->limbs[2] = $ret2;
|
||||
$return->limbs[3] = $ret3;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $A
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $B
|
||||
* @return array<int, ParagonIE_Sodium_Core32_Int64>
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedInferredReturnType
|
||||
*/
|
||||
public static function ctSelect(
|
||||
ParagonIE_Sodium_Core32_Int64 $A,
|
||||
ParagonIE_Sodium_Core32_Int64 $B
|
||||
) {
|
||||
$a = clone $A;
|
||||
$b = clone $B;
|
||||
/** @var int $aNeg */
|
||||
$aNeg = ($a->limbs[0] >> 15) & 1;
|
||||
/** @var int $bNeg */
|
||||
$bNeg = ($b->limbs[0] >> 15) & 1;
|
||||
/** @var int $m */
|
||||
$m = (-($aNeg & $bNeg)) | 1;
|
||||
/** @var int $swap */
|
||||
$swap = $bNeg & ~$aNeg;
|
||||
/** @var int $d */
|
||||
$d = -$swap;
|
||||
|
||||
/*
|
||||
if ($bNeg && !$aNeg) {
|
||||
$a = clone $int;
|
||||
$b = clone $this;
|
||||
} elseif($bNeg && $aNeg) {
|
||||
$a = $this->mulInt(-1);
|
||||
$b = $int->mulInt(-1);
|
||||
}
|
||||
*/
|
||||
$x = $a->xorInt64($b)->mask64($d, $d);
|
||||
return array(
|
||||
$a->xorInt64($x)->mulInt($m),
|
||||
$b->xorInt64($x)->mulInt($m)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $int
|
||||
* @param int $size
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedAssignment
|
||||
*/
|
||||
public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2);
|
||||
if (!$size) {
|
||||
$size = 63;
|
||||
}
|
||||
list($a, $b) = self::ctSelect($this, $int);
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
// Initialize:
|
||||
$ret0 = 0;
|
||||
$ret1 = 0;
|
||||
$ret2 = 0;
|
||||
$ret3 = 0;
|
||||
$a0 = $a->limbs[0];
|
||||
$a1 = $a->limbs[1];
|
||||
$a2 = $a->limbs[2];
|
||||
$a3 = $a->limbs[3];
|
||||
$b0 = $b->limbs[0];
|
||||
$b1 = $b->limbs[1];
|
||||
$b2 = $b->limbs[2];
|
||||
$b3 = $b->limbs[3];
|
||||
|
||||
/** @var int $size */
|
||||
/** @var int $i */
|
||||
for ($i = (int) $size; $i >= 0; --$i) {
|
||||
$mask = -($b3 & 1);
|
||||
$x0 = $a0 & $mask;
|
||||
$x1 = $a1 & $mask;
|
||||
$x2 = $a2 & $mask;
|
||||
$x3 = $a3 & $mask;
|
||||
|
||||
$ret3 += $x3;
|
||||
$c = $ret3 >> 16;
|
||||
|
||||
$ret2 += $x2 + $c;
|
||||
$c = $ret2 >> 16;
|
||||
|
||||
$ret1 += $x1 + $c;
|
||||
$c = $ret1 >> 16;
|
||||
|
||||
$ret0 += $x0 + $c;
|
||||
|
||||
$ret0 &= 0xffff;
|
||||
$ret1 &= 0xffff;
|
||||
$ret2 &= 0xffff;
|
||||
$ret3 &= 0xffff;
|
||||
|
||||
$a3 = $a3 << 1;
|
||||
$x3 = $a3 >> 16;
|
||||
$a2 = ($a2 << 1) | $x3;
|
||||
$x2 = $a2 >> 16;
|
||||
$a1 = ($a1 << 1) | $x2;
|
||||
$x1 = $a1 >> 16;
|
||||
$a0 = ($a0 << 1) | $x1;
|
||||
$a0 &= 0xffff;
|
||||
$a1 &= 0xffff;
|
||||
$a2 &= 0xffff;
|
||||
$a3 &= 0xffff;
|
||||
|
||||
$x0 = ($b0 & 1) << 16;
|
||||
$x1 = ($b1 & 1) << 16;
|
||||
$x2 = ($b2 & 1) << 16;
|
||||
|
||||
$b0 = ($b0 >> 1);
|
||||
$b1 = (($b1 | $x0) >> 1);
|
||||
$b2 = (($b2 | $x1) >> 1);
|
||||
$b3 = (($b3 | $x2) >> 1);
|
||||
|
||||
$b0 &= 0xffff;
|
||||
$b1 &= 0xffff;
|
||||
$b2 &= 0xffff;
|
||||
$b3 &= 0xffff;
|
||||
|
||||
}
|
||||
$return->limbs[0] = $ret0;
|
||||
$return->limbs[1] = $ret1;
|
||||
$return->limbs[2] = $ret2;
|
||||
$return->limbs[3] = $ret3;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* OR this 64-bit integer with another.
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function orInt64(ParagonIE_Sodium_Core32_Int64 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->limbs = array(
|
||||
(int) ($this->limbs[0] | $b->limbs[0]),
|
||||
(int) ($this->limbs[1] | $b->limbs[1]),
|
||||
(int) ($this->limbs[2] | $b->limbs[2]),
|
||||
(int) ($this->limbs[3] | $b->limbs[3])
|
||||
);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
*/
|
||||
public function rotateLeft($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
if ($c === 0) {
|
||||
// NOP, but we want a copy.
|
||||
$return->limbs = $this->limbs;
|
||||
} else {
|
||||
/** @var array<int, int> $limbs */
|
||||
$limbs =& $return->limbs;
|
||||
|
||||
/** @var array<int, int> $myLimbs */
|
||||
$myLimbs =& $this->limbs;
|
||||
|
||||
/** @var int $idx_shift */
|
||||
$idx_shift = ($c >> 4) & 3;
|
||||
/** @var int $sub_shift */
|
||||
$sub_shift = $c & 15;
|
||||
|
||||
for ($i = 3; $i >= 0; --$i) {
|
||||
/** @var int $j */
|
||||
$j = ($i + $idx_shift) & 3;
|
||||
/** @var int $k */
|
||||
$k = ($i + $idx_shift + 1) & 3;
|
||||
$limbs[$i] = (int) (
|
||||
(
|
||||
((int) ($myLimbs[$j]) << $sub_shift)
|
||||
|
|
||||
((int) ($myLimbs[$k]) >> (16 - $sub_shift))
|
||||
) & 0xffff
|
||||
);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate to the right
|
||||
*
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedArrayAccess
|
||||
*/
|
||||
public function rotateRight($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int64 $return */
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
/** @var int $c */
|
||||
if ($c === 0) {
|
||||
// NOP, but we want a copy.
|
||||
$return->limbs = $this->limbs;
|
||||
} else {
|
||||
/** @var array<int, int> $limbs */
|
||||
$limbs =& $return->limbs;
|
||||
|
||||
/** @var array<int, int> $myLimbs */
|
||||
$myLimbs =& $this->limbs;
|
||||
|
||||
/** @var int $idx_shift */
|
||||
$idx_shift = ($c >> 4) & 3;
|
||||
/** @var int $sub_shift */
|
||||
$sub_shift = $c & 15;
|
||||
|
||||
for ($i = 3; $i >= 0; --$i) {
|
||||
/** @var int $j */
|
||||
$j = ($i - $idx_shift) & 3;
|
||||
/** @var int $k */
|
||||
$k = ($i - $idx_shift - 1) & 3;
|
||||
$limbs[$i] = (int) (
|
||||
(
|
||||
((int) ($myLimbs[$j]) >> (int) ($sub_shift))
|
||||
|
|
||||
((int) ($myLimbs[$k]) << (16 - (int) ($sub_shift)))
|
||||
) & 0xffff
|
||||
);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function shiftLeft($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
/** @var int $c */
|
||||
$c = (int) $c;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
|
||||
if ($c >= 16) {
|
||||
if ($c >= 48) {
|
||||
$return->limbs = array(
|
||||
$this->limbs[3], 0, 0, 0
|
||||
);
|
||||
} elseif ($c >= 32) {
|
||||
$return->limbs = array(
|
||||
$this->limbs[2], $this->limbs[3], 0, 0
|
||||
);
|
||||
} else {
|
||||
$return->limbs = array(
|
||||
$this->limbs[1], $this->limbs[2], $this->limbs[3], 0
|
||||
);
|
||||
}
|
||||
return $return->shiftLeft($c & 15);
|
||||
}
|
||||
if ($c === 0) {
|
||||
$return->limbs = $this->limbs;
|
||||
} elseif ($c < 0) {
|
||||
/** @var int $c */
|
||||
return $this->shiftRight(-$c);
|
||||
} else {
|
||||
if (is_null($c)) {
|
||||
throw new TypeError();
|
||||
}
|
||||
/** @var int $carry */
|
||||
$carry = 0;
|
||||
for ($i = 3; $i >= 0; --$i) {
|
||||
/** @var int $tmp */
|
||||
$tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff);
|
||||
$return->limbs[$i] = (int) ($tmp & 0xffff);
|
||||
/** @var int $carry */
|
||||
$carry = $tmp >> 16;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $c
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function shiftRight($c = 0)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1);
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$c &= 63;
|
||||
/** @var int $c */
|
||||
|
||||
$negative = -(($this->limbs[0] >> 15) & 1);
|
||||
if ($c >= 16) {
|
||||
if ($c >= 48) {
|
||||
$return->limbs = array(
|
||||
(int) ($negative & 0xffff),
|
||||
(int) ($negative & 0xffff),
|
||||
(int) ($negative & 0xffff),
|
||||
(int) $this->limbs[0]
|
||||
);
|
||||
} elseif ($c >= 32) {
|
||||
$return->limbs = array(
|
||||
(int) ($negative & 0xffff),
|
||||
(int) ($negative & 0xffff),
|
||||
(int) $this->limbs[0],
|
||||
(int) $this->limbs[1]
|
||||
);
|
||||
} else {
|
||||
$return->limbs = array(
|
||||
(int) ($negative & 0xffff),
|
||||
(int) $this->limbs[0],
|
||||
(int) $this->limbs[1],
|
||||
(int) $this->limbs[2]
|
||||
);
|
||||
}
|
||||
return $return->shiftRight($c & 15);
|
||||
}
|
||||
|
||||
if ($c === 0) {
|
||||
$return->limbs = $this->limbs;
|
||||
} elseif ($c < 0) {
|
||||
return $this->shiftLeft(-$c);
|
||||
} else {
|
||||
if (!is_int($c)) {
|
||||
throw new TypeError();
|
||||
}
|
||||
/** @var int $carryRight */
|
||||
$carryRight = ($negative & 0xffff);
|
||||
$mask = (int) (((1 << ($c + 1)) - 1) & 0xffff);
|
||||
for ($i = 0; $i < 4; ++$i) {
|
||||
$return->limbs[$i] = (int) (
|
||||
(($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff
|
||||
);
|
||||
$carryRight = (int) ($this->limbs[$i] & $mask);
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Subtract a normal integer from an int64 object.
|
||||
*
|
||||
* @param int $int
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function subInt($int)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1);
|
||||
$int = (int) $int;
|
||||
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
|
||||
/** @var int $carry */
|
||||
$carry = 0;
|
||||
for ($i = 3; $i >= 0; --$i) {
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry;
|
||||
/** @var int $carry */
|
||||
$carry = $tmp >> 16;
|
||||
$return->limbs[$i] = (int) ($tmp & 0xffff);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* The difference between two Int64 objects.
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function subInt64(ParagonIE_Sodium_Core32_Int64 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
/** @var int $carry */
|
||||
$carry = 0;
|
||||
for ($i = 3; $i >= 0; --$i) {
|
||||
/** @var int $tmp */
|
||||
$tmp = $this->limbs[$i] - $b->limbs[$i] + $carry;
|
||||
/** @var int $carry */
|
||||
$carry = ($tmp >> 16);
|
||||
$return->limbs[$i] = (int) ($tmp & 0xffff);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* XOR this 64-bit integer with another.
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Int64 $b
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b)
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->limbs = array(
|
||||
(int) ($this->limbs[0] ^ $b->limbs[0]),
|
||||
(int) ($this->limbs[1] ^ $b->limbs[1]),
|
||||
(int) ($this->limbs[2] ^ $b->limbs[2]),
|
||||
(int) ($this->limbs[3] ^ $b->limbs[3])
|
||||
);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $low
|
||||
* @param int $high
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromInts($low, $high)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1);
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2);
|
||||
|
||||
$high = (int) $high;
|
||||
$low = (int) $low;
|
||||
return new ParagonIE_Sodium_Core32_Int64(
|
||||
array(
|
||||
(int) (($high >> 16) & 0xffff),
|
||||
(int) ($high & 0xffff),
|
||||
(int) (($low >> 16) & 0xffff),
|
||||
(int) ($low & 0xffff)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $low
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromInt($low)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1);
|
||||
$low = (int) $low;
|
||||
|
||||
return new ParagonIE_Sodium_Core32_Int64(
|
||||
array(
|
||||
0,
|
||||
0,
|
||||
(int) (($low >> 16) & 0xffff),
|
||||
(int) ($low & 0xffff)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function toInt()
|
||||
{
|
||||
return (int) (
|
||||
(($this->limbs[2] & 0xffff) << 16)
|
||||
|
|
||||
($this->limbs[3] & 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromString($string)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1);
|
||||
$string = (string) $string;
|
||||
if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) {
|
||||
throw new RangeException(
|
||||
'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
|
||||
$return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8);
|
||||
$return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff);
|
||||
$return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8);
|
||||
$return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff);
|
||||
$return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8);
|
||||
$return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff);
|
||||
$return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8);
|
||||
$return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function fromReverseString($string)
|
||||
{
|
||||
ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1);
|
||||
$string = (string) $string;
|
||||
if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) {
|
||||
throw new RangeException(
|
||||
'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.'
|
||||
);
|
||||
}
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
|
||||
$return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8);
|
||||
$return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff);
|
||||
$return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8);
|
||||
$return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff);
|
||||
$return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8);
|
||||
$return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff);
|
||||
$return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8);
|
||||
$return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return array(
|
||||
(int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)),
|
||||
(int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParagonIE_Sodium_Core32_Int32
|
||||
*/
|
||||
public function toInt32()
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int32();
|
||||
$return->limbs[0] = (int) ($this->limbs[2]);
|
||||
$return->limbs[1] = (int) ($this->limbs[3]);
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->overflow = (int) (ParagonIE_Sodium_Core32_Util::abs($this->limbs[1], 16) & 0xffff);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ParagonIE_Sodium_Core32_Int64
|
||||
*/
|
||||
public function toInt64()
|
||||
{
|
||||
$return = new ParagonIE_Sodium_Core32_Int64();
|
||||
$return->limbs[0] = (int) ($this->limbs[0]);
|
||||
$return->limbs[1] = (int) ($this->limbs[1]);
|
||||
$return->limbs[2] = (int) ($this->limbs[2]);
|
||||
$return->limbs[3] = (int) ($this->limbs[3]);
|
||||
$return->unsignedInt = $this->unsignedInt;
|
||||
$return->overflow = ParagonIE_Sodium_Core32_Util::abs($this->overflow);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $bool
|
||||
* @return self
|
||||
*/
|
||||
public function setUnsignedInt($bool = false)
|
||||
{
|
||||
$this->unsignedInt = !empty($bool);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function toReverseString()
|
||||
{
|
||||
return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) .
|
||||
ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
try {
|
||||
return $this->toString();
|
||||
} catch (TypeError $ex) {
|
||||
// PHP engine can't handle exceptions from __toString()
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
63
wp-includes/sodium_compat/src/Core32/Poly1305.php
Normal file
63
wp-includes/sodium_compat/src/Core32/Poly1305.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Poly1305
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util
|
||||
{
|
||||
const BLOCK_SIZE = 16;
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $m
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function onetimeauth($m, $key)
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Key must be 32 bytes long.'
|
||||
);
|
||||
}
|
||||
$state = new ParagonIE_Sodium_Core32_Poly1305_State(
|
||||
self::substr($key, 0, 32)
|
||||
);
|
||||
return $state
|
||||
->update($m)
|
||||
->finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $mac
|
||||
* @param string $m
|
||||
* @param string $key
|
||||
* @return bool
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function onetimeauth_verify($mac, $m, $key)
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Key must be 32 bytes long.'
|
||||
);
|
||||
}
|
||||
$state = new ParagonIE_Sodium_Core32_Poly1305_State(
|
||||
self::substr($key, 0, 32)
|
||||
);
|
||||
$calc = $state
|
||||
->update($m)
|
||||
->finish();
|
||||
return self::verify_16($calc, $mac);
|
||||
}
|
||||
}
|
451
wp-includes/sodium_compat/src/Core32/Poly1305/State.php
Normal file
451
wp-includes/sodium_compat/src/Core32/Poly1305/State.php
Normal file
@ -0,0 +1,451 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Poly1305_State', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Poly1305_State
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_Poly1305_State extends ParagonIE_Sodium_Core32_Util
|
||||
{
|
||||
/**
|
||||
* @var array<int, int>
|
||||
*/
|
||||
protected $buffer = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $final = false;
|
||||
|
||||
/**
|
||||
* @var array<int, ParagonIE_Sodium_Core32_Int32>
|
||||
*/
|
||||
public $h;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $leftover = 0;
|
||||
|
||||
/**
|
||||
* @var array<int, ParagonIE_Sodium_Core32_Int32>
|
||||
*/
|
||||
public $r;
|
||||
|
||||
/**
|
||||
* @var array<int, ParagonIE_Sodium_Core32_Int64>
|
||||
*/
|
||||
public $pad;
|
||||
|
||||
/**
|
||||
* ParagonIE_Sodium_Core32_Poly1305_State constructor.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $key
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function __construct($key = '')
|
||||
{
|
||||
if (self::strlen($key) < 32) {
|
||||
throw new InvalidArgumentException(
|
||||
'Poly1305 requires a 32-byte key'
|
||||
);
|
||||
}
|
||||
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
|
||||
$this->r = array(
|
||||
// st->r[0] = ...
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4))
|
||||
->setUnsignedInt(true)
|
||||
->mask(0x3ffffff),
|
||||
// st->r[1] = ...
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 3, 4))
|
||||
->setUnsignedInt(true)
|
||||
->shiftRight(2)
|
||||
->mask(0x3ffff03),
|
||||
// st->r[2] = ...
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 6, 4))
|
||||
->setUnsignedInt(true)
|
||||
->shiftRight(4)
|
||||
->mask(0x3ffc0ff),
|
||||
// st->r[3] = ...
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 9, 4))
|
||||
->setUnsignedInt(true)
|
||||
->shiftRight(6)
|
||||
->mask(0x3f03fff),
|
||||
// st->r[4] = ...
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4))
|
||||
->setUnsignedInt(true)
|
||||
->shiftRight(8)
|
||||
->mask(0x00fffff)
|
||||
);
|
||||
|
||||
/* h = 0 */
|
||||
$this->h = array(
|
||||
new ParagonIE_Sodium_Core32_Int32(array(0, 0), true),
|
||||
new ParagonIE_Sodium_Core32_Int32(array(0, 0), true),
|
||||
new ParagonIE_Sodium_Core32_Int32(array(0, 0), true),
|
||||
new ParagonIE_Sodium_Core32_Int32(array(0, 0), true),
|
||||
new ParagonIE_Sodium_Core32_Int32(array(0, 0), true)
|
||||
);
|
||||
|
||||
/* save pad for later */
|
||||
$this->pad = array(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4))
|
||||
->setUnsignedInt(true)->toInt64(),
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4))
|
||||
->setUnsignedInt(true)->toInt64(),
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4))
|
||||
->setUnsignedInt(true)->toInt64(),
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4))
|
||||
->setUnsignedInt(true)->toInt64(),
|
||||
);
|
||||
|
||||
$this->leftover = 0;
|
||||
$this->final = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function update($message = '')
|
||||
{
|
||||
$bytes = self::strlen($message);
|
||||
|
||||
/* handle leftover */
|
||||
if ($this->leftover) {
|
||||
/** @var int $want */
|
||||
$want = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - $this->leftover;
|
||||
if ($want > $bytes) {
|
||||
$want = $bytes;
|
||||
}
|
||||
for ($i = 0; $i < $want; ++$i) {
|
||||
$mi = self::chrToInt($message[$i]);
|
||||
$this->buffer[$this->leftover + $i] = $mi;
|
||||
}
|
||||
// We snip off the leftmost bytes.
|
||||
$message = self::substr($message, $want);
|
||||
$bytes = self::strlen($message);
|
||||
$this->leftover += $want;
|
||||
if ($this->leftover < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) {
|
||||
// We still don't have enough to run $this->blocks()
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->blocks(
|
||||
static::intArrayToString($this->buffer),
|
||||
ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
$this->leftover = 0;
|
||||
}
|
||||
|
||||
/* process full blocks */
|
||||
if ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) {
|
||||
/** @var int $want */
|
||||
$want = $bytes & ~(ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE - 1);
|
||||
if ($want >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) {
|
||||
/** @var string $block */
|
||||
$block = self::substr($message, 0, $want);
|
||||
if (self::strlen($block) >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) {
|
||||
$this->blocks($block, $want);
|
||||
$message = self::substr($message, $want);
|
||||
$bytes = self::strlen($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* store leftover */
|
||||
if ($bytes) {
|
||||
for ($i = 0; $i < $bytes; ++$i) {
|
||||
$mi = self::chrToInt($message[$i]);
|
||||
$this->buffer[$this->leftover + $i] = $mi;
|
||||
}
|
||||
$this->leftover = (int) $this->leftover + $bytes;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $bytes
|
||||
* @return self
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function blocks($message, $bytes)
|
||||
{
|
||||
if (self::strlen($message) < 16) {
|
||||
$message = str_pad($message, 16, "\x00", STR_PAD_RIGHT);
|
||||
}
|
||||
$hibit = ParagonIE_Sodium_Core32_Int32::fromInt((int) ($this->final ? 0 : 1 << 24)); /* 1 << 128 */
|
||||
$hibit->setUnsignedInt(true);
|
||||
$zero = new ParagonIE_Sodium_Core32_Int64(array(0, 0, 0, 0), true);
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $d0
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $d1
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $d2
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $d3
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $d4
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $r0
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $r1
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $r2
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $r3
|
||||
* @var ParagonIE_Sodium_Core32_Int64 $r4
|
||||
*
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h4
|
||||
*/
|
||||
$r0 = $this->r[0]->toInt64();
|
||||
$r1 = $this->r[1]->toInt64();
|
||||
$r2 = $this->r[2]->toInt64();
|
||||
$r3 = $this->r[3]->toInt64();
|
||||
$r4 = $this->r[4]->toInt64();
|
||||
|
||||
$s1 = $r1->toInt64()->mulInt(5, 3);
|
||||
$s2 = $r2->toInt64()->mulInt(5, 3);
|
||||
$s3 = $r3->toInt64()->mulInt(5, 3);
|
||||
$s4 = $r4->toInt64()->mulInt(5, 3);
|
||||
|
||||
$h0 = $this->h[0];
|
||||
$h1 = $this->h[1];
|
||||
$h2 = $this->h[2];
|
||||
$h3 = $this->h[3];
|
||||
$h4 = $this->h[4];
|
||||
|
||||
while ($bytes >= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE) {
|
||||
/* h += m[i] */
|
||||
$h0 = $h0->addInt32(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))
|
||||
->mask(0x3ffffff)
|
||||
)->toInt64();
|
||||
$h1 = $h1->addInt32(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 3, 4))
|
||||
->shiftRight(2)
|
||||
->mask(0x3ffffff)
|
||||
)->toInt64();
|
||||
$h2 = $h2->addInt32(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 6, 4))
|
||||
->shiftRight(4)
|
||||
->mask(0x3ffffff)
|
||||
)->toInt64();
|
||||
$h3 = $h3->addInt32(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 9, 4))
|
||||
->shiftRight(6)
|
||||
->mask(0x3ffffff)
|
||||
)->toInt64();
|
||||
$h4 = $h4->addInt32(
|
||||
ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))
|
||||
->shiftRight(8)
|
||||
->orInt32($hibit)
|
||||
)->toInt64();
|
||||
|
||||
/* h *= r */
|
||||
$d0 = $zero
|
||||
->addInt64($h0->mulInt64($r0, 25))
|
||||
->addInt64($s4->mulInt64($h1, 26))
|
||||
->addInt64($s3->mulInt64($h2, 26))
|
||||
->addInt64($s2->mulInt64($h3, 26))
|
||||
->addInt64($s1->mulInt64($h4, 26));
|
||||
|
||||
$d1 = $zero
|
||||
->addInt64($h0->mulInt64($r1, 25))
|
||||
->addInt64($h1->mulInt64($r0, 25))
|
||||
->addInt64($s4->mulInt64($h2, 26))
|
||||
->addInt64($s3->mulInt64($h3, 26))
|
||||
->addInt64($s2->mulInt64($h4, 26));
|
||||
|
||||
$d2 = $zero
|
||||
->addInt64($h0->mulInt64($r2, 25))
|
||||
->addInt64($h1->mulInt64($r1, 25))
|
||||
->addInt64($h2->mulInt64($r0, 25))
|
||||
->addInt64($s4->mulInt64($h3, 26))
|
||||
->addInt64($s3->mulInt64($h4, 26));
|
||||
|
||||
$d3 = $zero
|
||||
->addInt64($h0->mulInt64($r3, 25))
|
||||
->addInt64($h1->mulInt64($r2, 25))
|
||||
->addInt64($h2->mulInt64($r1, 25))
|
||||
->addInt64($h3->mulInt64($r0, 25))
|
||||
->addInt64($s4->mulInt64($h4, 26));
|
||||
|
||||
$d4 = $zero
|
||||
->addInt64($h0->mulInt64($r4, 25))
|
||||
->addInt64($h1->mulInt64($r3, 25))
|
||||
->addInt64($h2->mulInt64($r2, 25))
|
||||
->addInt64($h3->mulInt64($r1, 25))
|
||||
->addInt64($h4->mulInt64($r0, 25));
|
||||
|
||||
/* (partial) h %= p */
|
||||
$c = $d0->shiftRight(26);
|
||||
$h0 = $d0->toInt32()->mask(0x3ffffff);
|
||||
$d1 = $d1->addInt64($c);
|
||||
|
||||
$c = $d1->shiftRight(26);
|
||||
$h1 = $d1->toInt32()->mask(0x3ffffff);
|
||||
$d2 = $d2->addInt64($c);
|
||||
|
||||
$c = $d2->shiftRight(26);
|
||||
$h2 = $d2->toInt32()->mask(0x3ffffff);
|
||||
$d3 = $d3->addInt64($c);
|
||||
|
||||
$c = $d3->shiftRight(26);
|
||||
$h3 = $d3->toInt32()->mask(0x3ffffff);
|
||||
$d4 = $d4->addInt64($c);
|
||||
|
||||
$c = $d4->shiftRight(26);
|
||||
$h4 = $d4->toInt32()->mask(0x3ffffff);
|
||||
$h0 = $h0->addInt32($c->toInt32()->mulInt(5, 3));
|
||||
|
||||
$c = $h0->shiftRight(26);
|
||||
$h0 = $h0->mask(0x3ffffff);
|
||||
$h1 = $h1->addInt32($c);
|
||||
|
||||
// Chop off the left 32 bytes.
|
||||
$message = self::substr(
|
||||
$message,
|
||||
ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
$bytes -= ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE;
|
||||
}
|
||||
|
||||
/** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */
|
||||
$this->h = array($h0, $h1, $h2, $h3, $h4);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function finish()
|
||||
{
|
||||
/* process the remaining block */
|
||||
if ($this->leftover) {
|
||||
$i = $this->leftover;
|
||||
$this->buffer[$i++] = 1;
|
||||
for (; $i < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; ++$i) {
|
||||
$this->buffer[$i] = 0;
|
||||
}
|
||||
$this->final = true;
|
||||
$this->blocks(
|
||||
self::substr(
|
||||
static::intArrayToString($this->buffer),
|
||||
0,
|
||||
ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
|
||||
),
|
||||
$b = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $f
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $g0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $g1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $g2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $g3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $g4
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $h4
|
||||
*/
|
||||
$h0 = $this->h[0];
|
||||
$h1 = $this->h[1];
|
||||
$h2 = $this->h[2];
|
||||
$h3 = $this->h[3];
|
||||
$h4 = $this->h[4];
|
||||
|
||||
$c = $h1->shiftRight(26); # $c = $h1 >> 26;
|
||||
$h1 = $h1->mask(0x3ffffff); # $h1 &= 0x3ffffff;
|
||||
|
||||
$h2 = $h2->addInt32($c); # $h2 += $c;
|
||||
$c = $h2->shiftRight(26); # $c = $h2 >> 26;
|
||||
$h2 = $h2->mask(0x3ffffff); # $h2 &= 0x3ffffff;
|
||||
|
||||
$h3 = $h3->addInt32($c); # $h3 += $c;
|
||||
$c = $h3->shiftRight(26); # $c = $h3 >> 26;
|
||||
$h3 = $h3->mask(0x3ffffff); # $h3 &= 0x3ffffff;
|
||||
|
||||
$h4 = $h4->addInt32($c); # $h4 += $c;
|
||||
$c = $h4->shiftRight(26); # $c = $h4 >> 26;
|
||||
$h4 = $h4->mask(0x3ffffff); # $h4 &= 0x3ffffff;
|
||||
|
||||
$h0 = $h0->addInt32($c->mulInt(5, 3)); # $h0 += self::mul($c, 5);
|
||||
$c = $h0->shiftRight(26); # $c = $h0 >> 26;
|
||||
$h0 = $h0->mask(0x3ffffff); # $h0 &= 0x3ffffff;
|
||||
$h1 = $h1->addInt32($c); # $h1 += $c;
|
||||
|
||||
/* compute h + -p */
|
||||
$g0 = $h0->addInt(5);
|
||||
$c = $g0->shiftRight(26);
|
||||
$g0 = $g0->mask(0x3ffffff);
|
||||
$g1 = $h1->addInt32($c);
|
||||
$c = $g1->shiftRight(26);
|
||||
$g1 = $g1->mask(0x3ffffff);
|
||||
$g2 = $h2->addInt32($c);
|
||||
$c = $g2->shiftRight(26);
|
||||
$g2 = $g2->mask(0x3ffffff);
|
||||
$g3 = $h3->addInt32($c);
|
||||
$c = $g3->shiftRight(26);
|
||||
$g3 = $g3->mask(0x3ffffff);
|
||||
$g4 = $h4->addInt32($c)->subInt(1 << 26);
|
||||
|
||||
# $mask = ($g4 >> 31) - 1;
|
||||
/* select h if h < p, or h + -p if h >= p */
|
||||
$mask = (int) (($g4->toInt() >> 31) + 1);
|
||||
|
||||
$g0 = $g0->mask($mask);
|
||||
$g1 = $g1->mask($mask);
|
||||
$g2 = $g2->mask($mask);
|
||||
$g3 = $g3->mask($mask);
|
||||
$g4 = $g4->mask($mask);
|
||||
|
||||
/** @var int $mask */
|
||||
$mask = (~$mask) & 0xffffffff;
|
||||
|
||||
$h0 = $h0->mask($mask)->orInt32($g0);
|
||||
$h1 = $h1->mask($mask)->orInt32($g1);
|
||||
$h2 = $h2->mask($mask)->orInt32($g2);
|
||||
$h3 = $h3->mask($mask)->orInt32($g3);
|
||||
$h4 = $h4->mask($mask)->orInt32($g4);
|
||||
|
||||
/* h = h % (2^128) */
|
||||
$h0 = $h0->orInt32($h1->shiftLeft(26));
|
||||
$h1 = $h1->shiftRight(6)->orInt32($h2->shiftLeft(20));
|
||||
$h2 = $h2->shiftRight(12)->orInt32($h3->shiftLeft(14));
|
||||
$h3 = $h3->shiftRight(18)->orInt32($h4->shiftLeft(8));
|
||||
|
||||
/* mac = (h + pad) % (2^128) */
|
||||
$f = $h0->toInt64()->addInt64($this->pad[0]);
|
||||
$h0 = $f->toInt32();
|
||||
$f = $h1->toInt64()->addInt64($this->pad[1])->addInt($h0->overflow);
|
||||
$h1 = $f->toInt32();
|
||||
$f = $h2->toInt64()->addInt64($this->pad[2])->addInt($h1->overflow);
|
||||
$h2 = $f->toInt32();
|
||||
$f = $h3->toInt64()->addInt64($this->pad[3])->addInt($h2->overflow);
|
||||
$h3 = $f->toInt32();
|
||||
|
||||
return $h0->toReverseString() .
|
||||
$h1->toReverseString() .
|
||||
$h2->toReverseString() .
|
||||
$h3->toReverseString();
|
||||
}
|
||||
}
|
306
wp-includes/sodium_compat/src/Core32/Salsa20.php
Normal file
306
wp-includes/sodium_compat/src/Core32/Salsa20.php
Normal file
@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Salsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_Salsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_Salsa20 extends ParagonIE_Sodium_Core32_Util
|
||||
{
|
||||
const ROUNDS = 20;
|
||||
|
||||
/**
|
||||
* Calculate an salsa20 hash of a single block
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $k
|
||||
* @param string|null $c
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function core_salsa20($in, $k, $c = null)
|
||||
{
|
||||
/**
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x4
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x5
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x6
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x7
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x8
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x9
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x10
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x11
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x12
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x13
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x14
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $x15
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j0
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j1
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j2
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j3
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j4
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j5
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j6
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j7
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j8
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j9
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j10
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j11
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j12
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j13
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j14
|
||||
* @var ParagonIE_Sodium_Core32_Int32 $j15
|
||||
*/
|
||||
if (self::strlen($k) < 32) {
|
||||
throw new RangeException('Key must be 32 bytes long');
|
||||
}
|
||||
if ($c === null) {
|
||||
$x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
|
||||
$x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
|
||||
$x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
|
||||
$x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
|
||||
} else {
|
||||
$x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4));
|
||||
$x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4));
|
||||
$x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4));
|
||||
$x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4));
|
||||
}
|
||||
$x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4));
|
||||
$x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4));
|
||||
$x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4));
|
||||
$x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4));
|
||||
$x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4));
|
||||
$x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4));
|
||||
$x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4));
|
||||
$x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4));
|
||||
$x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4));
|
||||
$x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4));
|
||||
$x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4));
|
||||
$x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4));
|
||||
|
||||
$j0 = clone $x0;
|
||||
$j1 = clone $x1;
|
||||
$j2 = clone $x2;
|
||||
$j3 = clone $x3;
|
||||
$j4 = clone $x4;
|
||||
$j5 = clone $x5;
|
||||
$j6 = clone $x6;
|
||||
$j7 = clone $x7;
|
||||
$j8 = clone $x8;
|
||||
$j9 = clone $x9;
|
||||
$j10 = clone $x10;
|
||||
$j11 = clone $x11;
|
||||
$j12 = clone $x12;
|
||||
$j13 = clone $x13;
|
||||
$j14 = clone $x14;
|
||||
$j15 = clone $x15;
|
||||
|
||||
for ($i = self::ROUNDS; $i > 0; $i -= 2) {
|
||||
$x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7));
|
||||
$x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9));
|
||||
$x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13));
|
||||
$x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18));
|
||||
|
||||
$x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7));
|
||||
$x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9));
|
||||
$x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13));
|
||||
$x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18));
|
||||
|
||||
$x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7));
|
||||
$x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9));
|
||||
$x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13));
|
||||
$x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18));
|
||||
|
||||
$x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7));
|
||||
$x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9));
|
||||
$x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13));
|
||||
$x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18));
|
||||
|
||||
$x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7));
|
||||
$x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9));
|
||||
$x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13));
|
||||
$x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18));
|
||||
|
||||
$x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7));
|
||||
$x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9));
|
||||
$x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13));
|
||||
$x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18));
|
||||
|
||||
$x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7));
|
||||
$x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9));
|
||||
$x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13));
|
||||
$x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18));
|
||||
|
||||
$x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7));
|
||||
$x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9));
|
||||
$x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13));
|
||||
$x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18));
|
||||
}
|
||||
|
||||
$x0 = $x0->addInt32($j0);
|
||||
$x1 = $x1->addInt32($j1);
|
||||
$x2 = $x2->addInt32($j2);
|
||||
$x3 = $x3->addInt32($j3);
|
||||
$x4 = $x4->addInt32($j4);
|
||||
$x5 = $x5->addInt32($j5);
|
||||
$x6 = $x6->addInt32($j6);
|
||||
$x7 = $x7->addInt32($j7);
|
||||
$x8 = $x8->addInt32($j8);
|
||||
$x9 = $x9->addInt32($j9);
|
||||
$x10 = $x10->addInt32($j10);
|
||||
$x11 = $x11->addInt32($j11);
|
||||
$x12 = $x12->addInt32($j12);
|
||||
$x13 = $x13->addInt32($j13);
|
||||
$x14 = $x14->addInt32($j14);
|
||||
$x15 = $x15->addInt32($j15);
|
||||
|
||||
return $x0->toReverseString() .
|
||||
$x1->toReverseString() .
|
||||
$x2->toReverseString() .
|
||||
$x3->toReverseString() .
|
||||
$x4->toReverseString() .
|
||||
$x5->toReverseString() .
|
||||
$x6->toReverseString() .
|
||||
$x7->toReverseString() .
|
||||
$x8->toReverseString() .
|
||||
$x9->toReverseString() .
|
||||
$x10->toReverseString() .
|
||||
$x11->toReverseString() .
|
||||
$x12->toReverseString() .
|
||||
$x13->toReverseString() .
|
||||
$x14->toReverseString() .
|
||||
$x15->toReverseString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20($len, $nonce, $key)
|
||||
{
|
||||
if (self::strlen($key) !== 32) {
|
||||
throw new RangeException('Key must be 32 bytes long');
|
||||
}
|
||||
$kcopy = '' . $key;
|
||||
$in = self::substr($nonce, 0, 8) . str_repeat("\0", 8);
|
||||
$c = '';
|
||||
while ($len >= 64) {
|
||||
$c .= self::core_salsa20($in, $kcopy, null);
|
||||
$u = 1;
|
||||
// Internal counter.
|
||||
for ($i = 8; $i < 16; ++$i) {
|
||||
$u += self::chrToInt($in[$i]);
|
||||
$in[$i] = self::intToChr($u & 0xff);
|
||||
$u >>= 8;
|
||||
}
|
||||
$len -= 64;
|
||||
}
|
||||
if ($len > 0) {
|
||||
$c .= self::substr(
|
||||
self::core_salsa20($in, $kcopy, null),
|
||||
0,
|
||||
$len
|
||||
);
|
||||
}
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($kcopy);
|
||||
} catch (SodiumException $ex) {
|
||||
$kcopy = null;
|
||||
}
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $m
|
||||
* @param string $n
|
||||
* @param int $ic
|
||||
* @param string $k
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20_xor_ic($m, $n, $ic, $k)
|
||||
{
|
||||
$mlen = self::strlen($m);
|
||||
if ($mlen < 1) {
|
||||
return '';
|
||||
}
|
||||
$kcopy = self::substr($k, 0, 32);
|
||||
$in = self::substr($n, 0, 8);
|
||||
// Initialize the counter
|
||||
$in .= ParagonIE_Sodium_Core32_Util::store64_le($ic);
|
||||
|
||||
$c = '';
|
||||
while ($mlen >= 64) {
|
||||
$block = self::core_salsa20($in, $kcopy, null);
|
||||
$c .= self::xorStrings(
|
||||
self::substr($m, 0, 64),
|
||||
self::substr($block, 0, 64)
|
||||
);
|
||||
$u = 1;
|
||||
for ($i = 8; $i < 16; ++$i) {
|
||||
$u += self::chrToInt($in[$i]);
|
||||
$in[$i] = self::intToChr($u & 0xff);
|
||||
$u >>= 8;
|
||||
}
|
||||
|
||||
$mlen -= 64;
|
||||
$m = self::substr($m, 64);
|
||||
}
|
||||
|
||||
if ($mlen) {
|
||||
$block = self::core_salsa20($in, $kcopy, null);
|
||||
$c .= self::xorStrings(
|
||||
self::substr($m, 0, $mlen),
|
||||
self::substr($block, 0, $mlen)
|
||||
);
|
||||
}
|
||||
try {
|
||||
ParagonIE_Sodium_Compat::memzero($block);
|
||||
ParagonIE_Sodium_Compat::memzero($kcopy);
|
||||
} catch (SodiumException $ex) {
|
||||
$block = null;
|
||||
$kcopy = null;
|
||||
}
|
||||
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function salsa20_xor($message, $nonce, $key)
|
||||
{
|
||||
return self::xorStrings(
|
||||
$message,
|
||||
self::salsa20(
|
||||
self::strlen($message),
|
||||
$nonce,
|
||||
$key
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
238
wp-includes/sodium_compat/src/Core32/SipHash.php
Normal file
238
wp-includes/sodium_compat/src/Core32/SipHash.php
Normal file
@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_SodiumCompat_Core32_SipHash
|
||||
*
|
||||
* Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util
|
||||
{
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param array<int, ParagonIE_Sodium_Core32_Int64> $v
|
||||
* @return array<int, ParagonIE_Sodium_Core32_Int64>
|
||||
*/
|
||||
public static function sipRound(array $v)
|
||||
{
|
||||
# v0 += v1;
|
||||
$v[0] = $v[0]->addInt64($v[1]);
|
||||
|
||||
# v1 = ROTL(v1, 13);
|
||||
$v[1] = $v[1]->rotateLeft(13);
|
||||
|
||||
# v1 ^= v0;
|
||||
$v[1] = $v[1]->xorInt64($v[0]);
|
||||
|
||||
# v0=ROTL(v0,32);
|
||||
$v[0] = $v[0]->rotateLeft(32);
|
||||
|
||||
# v2 += v3;
|
||||
$v[2] = $v[2]->addInt64($v[3]);
|
||||
|
||||
# v3=ROTL(v3,16);
|
||||
$v[3] = $v[3]->rotateLeft(16);
|
||||
|
||||
# v3 ^= v2;
|
||||
$v[3] = $v[3]->xorInt64($v[2]);
|
||||
|
||||
# v0 += v3;
|
||||
$v[0] = $v[0]->addInt64($v[3]);
|
||||
|
||||
# v3=ROTL(v3,21);
|
||||
$v[3] = $v[3]->rotateLeft(21);
|
||||
|
||||
# v3 ^= v0;
|
||||
$v[3] = $v[3]->xorInt64($v[0]);
|
||||
|
||||
# v2 += v1;
|
||||
$v[2] = $v[2]->addInt64($v[1]);
|
||||
|
||||
# v1=ROTL(v1,17);
|
||||
$v[1] = $v[1]->rotateLeft(17);
|
||||
|
||||
# v1 ^= v2;
|
||||
$v[1] = $v[1]->xorInt64($v[2]);
|
||||
|
||||
# v2=ROTL(v2,32)
|
||||
$v[2] = $v[2]->rotateLeft(32);
|
||||
|
||||
return $v;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $in
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function sipHash24($in, $key)
|
||||
{
|
||||
$inlen = self::strlen($in);
|
||||
|
||||
# /* "somepseudorandomlygeneratedbytes" */
|
||||
# u64 v0 = 0x736f6d6570736575ULL;
|
||||
# u64 v1 = 0x646f72616e646f6dULL;
|
||||
# u64 v2 = 0x6c7967656e657261ULL;
|
||||
# u64 v3 = 0x7465646279746573ULL;
|
||||
$v = array(
|
||||
new ParagonIE_Sodium_Core32_Int64(
|
||||
array(0x736f, 0x6d65, 0x7073, 0x6575)
|
||||
),
|
||||
new ParagonIE_Sodium_Core32_Int64(
|
||||
array(0x646f, 0x7261, 0x6e64, 0x6f6d)
|
||||
),
|
||||
new ParagonIE_Sodium_Core32_Int64(
|
||||
array(0x6c79, 0x6765, 0x6e65, 0x7261)
|
||||
),
|
||||
new ParagonIE_Sodium_Core32_Int64(
|
||||
array(0x7465, 0x6462, 0x7974, 0x6573)
|
||||
)
|
||||
);
|
||||
|
||||
# u64 k0 = LOAD64_LE( k );
|
||||
# u64 k1 = LOAD64_LE( k + 8 );
|
||||
$k = array(
|
||||
ParagonIE_Sodium_Core32_Int64::fromReverseString(
|
||||
self::substr($key, 0, 8)
|
||||
),
|
||||
ParagonIE_Sodium_Core32_Int64::fromReverseString(
|
||||
self::substr($key, 8, 8)
|
||||
)
|
||||
);
|
||||
|
||||
# b = ( ( u64 )inlen ) << 56;
|
||||
$b = new ParagonIE_Sodium_Core32_Int64(
|
||||
array(($inlen << 8) & 0xffff, 0, 0, 0)
|
||||
);
|
||||
|
||||
# v3 ^= k1;
|
||||
$v[3] = $v[3]->xorInt64($k[1]);
|
||||
# v2 ^= k0;
|
||||
$v[2] = $v[2]->xorInt64($k[0]);
|
||||
# v1 ^= k1;
|
||||
$v[1] = $v[1]->xorInt64($k[1]);
|
||||
# v0 ^= k0;
|
||||
$v[0] = $v[0]->xorInt64($k[0]);
|
||||
|
||||
$left = $inlen;
|
||||
# for ( ; in != end; in += 8 )
|
||||
while ($left >= 8) {
|
||||
# m = LOAD64_LE( in );
|
||||
$m = ParagonIE_Sodium_Core32_Int64::fromReverseString(
|
||||
self::substr($in, 0, 8)
|
||||
);
|
||||
|
||||
# v3 ^= m;
|
||||
$v[3] = $v[3]->xorInt64($m);
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# v0 ^= m;
|
||||
$v[0] = $v[0]->xorInt64($m);
|
||||
|
||||
$in = self::substr($in, 8);
|
||||
$left -= 8;
|
||||
}
|
||||
|
||||
# switch( left )
|
||||
# {
|
||||
# case 7: b |= ( ( u64 )in[ 6] ) << 48;
|
||||
# case 6: b |= ( ( u64 )in[ 5] ) << 40;
|
||||
# case 5: b |= ( ( u64 )in[ 4] ) << 32;
|
||||
# case 4: b |= ( ( u64 )in[ 3] ) << 24;
|
||||
# case 3: b |= ( ( u64 )in[ 2] ) << 16;
|
||||
# case 2: b |= ( ( u64 )in[ 1] ) << 8;
|
||||
# case 1: b |= ( ( u64 )in[ 0] ); break;
|
||||
# case 0: break;
|
||||
# }
|
||||
switch ($left) {
|
||||
case 7:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
0, self::chrToInt($in[6]) << 16
|
||||
)
|
||||
);
|
||||
case 6:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
0, self::chrToInt($in[5]) << 8
|
||||
)
|
||||
);
|
||||
case 5:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
0, self::chrToInt($in[4])
|
||||
)
|
||||
);
|
||||
case 4:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
self::chrToInt($in[3]) << 24, 0
|
||||
)
|
||||
);
|
||||
case 3:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
self::chrToInt($in[2]) << 16, 0
|
||||
)
|
||||
);
|
||||
case 2:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
self::chrToInt($in[1]) << 8, 0
|
||||
)
|
||||
);
|
||||
case 1:
|
||||
$b = $b->orInt64(
|
||||
ParagonIE_Sodium_Core32_Int64::fromInts(
|
||||
self::chrToInt($in[0]), 0
|
||||
)
|
||||
);
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
|
||||
# v3 ^= b;
|
||||
$v[3] = $v[3]->xorInt64($b);
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# v0 ^= b;
|
||||
$v[0] = $v[0]->xorInt64($b);
|
||||
|
||||
// Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
|
||||
# v2 ^= 0xff;
|
||||
$v[2]->limbs[3] ^= 0xff;
|
||||
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
# SIPROUND;
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
$v = self::sipRound($v);
|
||||
|
||||
# b = v0 ^ v1 ^ v2 ^ v3;
|
||||
# STORE64_LE( out, b );
|
||||
return $v[0]
|
||||
->xorInt64($v[1])
|
||||
->xorInt64($v[2])
|
||||
->xorInt64($v[3])
|
||||
->toReverseString();
|
||||
}
|
||||
}
|
13
wp-includes/sodium_compat/src/Core32/Util.php
Normal file
13
wp-includes/sodium_compat/src/Core32/Util.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_Util', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core_Util
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_Util extends ParagonIE_Sodium_Core_Util
|
||||
{
|
||||
|
||||
}
|
354
wp-includes/sodium_compat/src/Core32/X25519.php
Normal file
354
wp-includes/sodium_compat/src/Core32/X25519.php
Normal file
@ -0,0 +1,354 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_X25519
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519
|
||||
{
|
||||
/**
|
||||
* Alters the objects passed to this method in place.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $f
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $g
|
||||
* @param int $b
|
||||
* @return void
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedMethodCall
|
||||
*/
|
||||
public static function fe_cswap(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $f,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $g,
|
||||
$b = 0
|
||||
) {
|
||||
$f0 = (int) $f[0]->toInt();
|
||||
$f1 = (int) $f[1]->toInt();
|
||||
$f2 = (int) $f[2]->toInt();
|
||||
$f3 = (int) $f[3]->toInt();
|
||||
$f4 = (int) $f[4]->toInt();
|
||||
$f5 = (int) $f[5]->toInt();
|
||||
$f6 = (int) $f[6]->toInt();
|
||||
$f7 = (int) $f[7]->toInt();
|
||||
$f8 = (int) $f[8]->toInt();
|
||||
$f9 = (int) $f[9]->toInt();
|
||||
$g0 = (int) $g[0]->toInt();
|
||||
$g1 = (int) $g[1]->toInt();
|
||||
$g2 = (int) $g[2]->toInt();
|
||||
$g3 = (int) $g[3]->toInt();
|
||||
$g4 = (int) $g[4]->toInt();
|
||||
$g5 = (int) $g[5]->toInt();
|
||||
$g6 = (int) $g[6]->toInt();
|
||||
$g7 = (int) $g[7]->toInt();
|
||||
$g8 = (int) $g[8]->toInt();
|
||||
$g9 = (int) $g[9]->toInt();
|
||||
$b = -$b;
|
||||
/** @var int $x0 */
|
||||
$x0 = ($f0 ^ $g0) & $b;
|
||||
/** @var int $x1 */
|
||||
$x1 = ($f1 ^ $g1) & $b;
|
||||
/** @var int $x2 */
|
||||
$x2 = ($f2 ^ $g2) & $b;
|
||||
/** @var int $x3 */
|
||||
$x3 = ($f3 ^ $g3) & $b;
|
||||
/** @var int $x4 */
|
||||
$x4 = ($f4 ^ $g4) & $b;
|
||||
/** @var int $x5 */
|
||||
$x5 = ($f5 ^ $g5) & $b;
|
||||
/** @var int $x6 */
|
||||
$x6 = ($f6 ^ $g6) & $b;
|
||||
/** @var int $x7 */
|
||||
$x7 = ($f7 ^ $g7) & $b;
|
||||
/** @var int $x8 */
|
||||
$x8 = ($f8 ^ $g8) & $b;
|
||||
/** @var int $x9 */
|
||||
$x9 = ($f9 ^ $g9) & $b;
|
||||
$f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0);
|
||||
$f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1);
|
||||
$f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2);
|
||||
$f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3);
|
||||
$f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4);
|
||||
$f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5);
|
||||
$f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6);
|
||||
$f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7);
|
||||
$f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8);
|
||||
$f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9);
|
||||
$g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0);
|
||||
$g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1);
|
||||
$g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2);
|
||||
$g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3);
|
||||
$g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4);
|
||||
$g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5);
|
||||
$g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6);
|
||||
$g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7);
|
||||
$g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8);
|
||||
$g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $f
|
||||
* @return ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress MixedAssignment
|
||||
* @psalm-suppress MixedMethodCall
|
||||
*/
|
||||
public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
|
||||
{
|
||||
/** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */
|
||||
$h = array();
|
||||
for ($i = 0; $i < 10; ++$i) {
|
||||
$h[$i] = $f[$i]->toInt64()->mulInt(121666, 17);
|
||||
}
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry9 */
|
||||
$carry9 = $h[9]->addInt(1 << 24)->shiftRight(25);
|
||||
$h[0] = $h[0]->addInt64($carry9->mulInt(19, 5));
|
||||
$h[9] = $h[9]->subInt64($carry9->shiftLeft(25));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry1 */
|
||||
$carry1 = $h[1]->addInt(1 << 24)->shiftRight(25);
|
||||
$h[2] = $h[2]->addInt64($carry1);
|
||||
$h[1] = $h[1]->subInt64($carry1->shiftLeft(25));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry3 */
|
||||
$carry3 = $h[3]->addInt(1 << 24)->shiftRight(25);
|
||||
$h[4] = $h[4]->addInt64($carry3);
|
||||
$h[3] = $h[3]->subInt64($carry3->shiftLeft(25));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry5 */
|
||||
$carry5 = $h[5]->addInt(1 << 24)->shiftRight(25);
|
||||
$h[6] = $h[6]->addInt64($carry5);
|
||||
$h[5] = $h[5]->subInt64($carry5->shiftLeft(25));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry7 */
|
||||
$carry7 = $h[7]->addInt(1 << 24)->shiftRight(25);
|
||||
$h[8] = $h[8]->addInt64($carry7);
|
||||
$h[7] = $h[7]->subInt64($carry7->shiftLeft(25));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry0 */
|
||||
$carry0 = $h[0]->addInt(1 << 25)->shiftRight(26);
|
||||
$h[1] = $h[1]->addInt64($carry0);
|
||||
$h[0] = $h[0]->subInt64($carry0->shiftLeft(26));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry2 */
|
||||
$carry2 = $h[2]->addInt(1 << 25)->shiftRight(26);
|
||||
$h[3] = $h[3]->addInt64($carry2);
|
||||
$h[2] = $h[2]->subInt64($carry2->shiftLeft(26));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry4 */
|
||||
$carry4 = $h[4]->addInt(1 << 25)->shiftRight(26);
|
||||
$h[5] = $h[5]->addInt64($carry4);
|
||||
$h[4] = $h[4]->subInt64($carry4->shiftLeft(26));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry6 */
|
||||
$carry6 = $h[6]->addInt(1 << 25)->shiftRight(26);
|
||||
$h[7] = $h[7]->addInt64($carry6);
|
||||
$h[6] = $h[6]->subInt64($carry6->shiftLeft(26));
|
||||
|
||||
/** @var ParagonIE_Sodium_Core32_Int32 $carry8 */
|
||||
$carry8 = $h[8]->addInt(1 << 25)->shiftRight(26);
|
||||
$h[9] = $h[9]->addInt64($carry8);
|
||||
$h[8] = $h[8]->subInt64($carry8->shiftLeft(26));
|
||||
|
||||
for ($i = 0; $i < 10; ++$i) {
|
||||
$h[$i] = $h[$i]->toInt32();
|
||||
}
|
||||
/** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */
|
||||
return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* Inline comments preceded by # are from libsodium's ref10 code.
|
||||
*
|
||||
* @param string $n
|
||||
* @param string $p
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function crypto_scalarmult_curve25519_ref10($n, $p)
|
||||
{
|
||||
# for (i = 0;i < 32;++i) e[i] = n[i];
|
||||
$e = '' . $n;
|
||||
# e[0] &= 248;
|
||||
$e[0] = self::intToChr(
|
||||
self::chrToInt($e[0]) & 248
|
||||
);
|
||||
# e[31] &= 127;
|
||||
# e[31] |= 64;
|
||||
$e[31] = self::intToChr(
|
||||
(self::chrToInt($e[31]) & 127) | 64
|
||||
);
|
||||
# fe_frombytes(x1,p);
|
||||
$x1 = self::fe_frombytes($p);
|
||||
# fe_1(x2);
|
||||
$x2 = self::fe_1();
|
||||
# fe_0(z2);
|
||||
$z2 = self::fe_0();
|
||||
# fe_copy(x3,x1);
|
||||
$x3 = self::fe_copy($x1);
|
||||
# fe_1(z3);
|
||||
$z3 = self::fe_1();
|
||||
|
||||
# swap = 0;
|
||||
/** @var int $swap */
|
||||
$swap = 0;
|
||||
|
||||
# for (pos = 254;pos >= 0;--pos) {
|
||||
for ($pos = 254; $pos >= 0; --$pos) {
|
||||
# b = e[pos / 8] >> (pos & 7);
|
||||
/** @var int $b */
|
||||
$b = self::chrToInt(
|
||||
$e[(int) floor($pos / 8)]
|
||||
) >> ($pos & 7);
|
||||
# b &= 1;
|
||||
$b &= 1;
|
||||
|
||||
# swap ^= b;
|
||||
$swap ^= $b;
|
||||
|
||||
# fe_cswap(x2,x3,swap);
|
||||
self::fe_cswap($x2, $x3, $swap);
|
||||
|
||||
# fe_cswap(z2,z3,swap);
|
||||
self::fe_cswap($z2, $z3, $swap);
|
||||
|
||||
# swap = b;
|
||||
/** @var int $swap */
|
||||
$swap = $b;
|
||||
|
||||
# fe_sub(tmp0,x3,z3);
|
||||
$tmp0 = self::fe_sub($x3, $z3);
|
||||
|
||||
# fe_sub(tmp1,x2,z2);
|
||||
$tmp1 = self::fe_sub($x2, $z2);
|
||||
|
||||
# fe_add(x2,x2,z2);
|
||||
$x2 = self::fe_add($x2, $z2);
|
||||
|
||||
# fe_add(z2,x3,z3);
|
||||
$z2 = self::fe_add($x3, $z3);
|
||||
|
||||
# fe_mul(z3,tmp0,x2);
|
||||
$z3 = self::fe_mul($tmp0, $x2);
|
||||
|
||||
# fe_mul(z2,z2,tmp1);
|
||||
$z2 = self::fe_mul($z2, $tmp1);
|
||||
|
||||
# fe_sq(tmp0,tmp1);
|
||||
$tmp0 = self::fe_sq($tmp1);
|
||||
|
||||
# fe_sq(tmp1,x2);
|
||||
$tmp1 = self::fe_sq($x2);
|
||||
|
||||
# fe_add(x3,z3,z2);
|
||||
$x3 = self::fe_add($z3, $z2);
|
||||
|
||||
# fe_sub(z2,z3,z2);
|
||||
$z2 = self::fe_sub($z3, $z2);
|
||||
|
||||
# fe_mul(x2,tmp1,tmp0);
|
||||
$x2 = self::fe_mul($tmp1, $tmp0);
|
||||
|
||||
# fe_sub(tmp1,tmp1,tmp0);
|
||||
$tmp1 = self::fe_sub($tmp1, $tmp0);
|
||||
|
||||
# fe_sq(z2,z2);
|
||||
$z2 = self::fe_sq($z2);
|
||||
|
||||
# fe_mul121666(z3,tmp1);
|
||||
$z3 = self::fe_mul121666($tmp1);
|
||||
|
||||
# fe_sq(x3,x3);
|
||||
$x3 = self::fe_sq($x3);
|
||||
|
||||
# fe_add(tmp0,tmp0,z3);
|
||||
$tmp0 = self::fe_add($tmp0, $z3);
|
||||
|
||||
# fe_mul(z3,x1,z2);
|
||||
$z3 = self::fe_mul($x1, $z2);
|
||||
|
||||
# fe_mul(z2,tmp1,tmp0);
|
||||
$z2 = self::fe_mul($tmp1, $tmp0);
|
||||
}
|
||||
|
||||
# fe_cswap(x2,x3,swap);
|
||||
self::fe_cswap($x2, $x3, $swap);
|
||||
|
||||
# fe_cswap(z2,z3,swap);
|
||||
self::fe_cswap($z2, $z3, $swap);
|
||||
|
||||
# fe_invert(z2,z2);
|
||||
$z2 = self::fe_invert($z2);
|
||||
|
||||
# fe_mul(x2,x2,z2);
|
||||
$x2 = self::fe_mul($x2, $z2);
|
||||
# fe_tobytes(q,x2);
|
||||
return (string) self::fe_tobytes($x2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY
|
||||
* @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ
|
||||
* @return ParagonIE_Sodium_Core32_Curve25519_Fe
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function edwards_to_montgomery(
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY,
|
||||
ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ
|
||||
) {
|
||||
$tempX = self::fe_add($edwardsZ, $edwardsY);
|
||||
$tempZ = self::fe_sub($edwardsZ, $edwardsY);
|
||||
$tempZ = self::fe_invert($tempZ);
|
||||
return self::fe_mul($tempX, $tempZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $n
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function crypto_scalarmult_curve25519_ref10_base($n)
|
||||
{
|
||||
# for (i = 0;i < 32;++i) e[i] = n[i];
|
||||
$e = '' . $n;
|
||||
|
||||
# e[0] &= 248;
|
||||
$e[0] = self::intToChr(
|
||||
self::chrToInt($e[0]) & 248
|
||||
);
|
||||
|
||||
# e[31] &= 127;
|
||||
# e[31] |= 64;
|
||||
$e[31] = self::intToChr(
|
||||
(self::chrToInt($e[31]) & 127) | 64
|
||||
);
|
||||
|
||||
$A = self::ge_scalarmult_base($e);
|
||||
if (
|
||||
!($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe)
|
||||
||
|
||||
!($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe)
|
||||
) {
|
||||
throw new TypeError('Null points encountered');
|
||||
}
|
||||
$pk = self::edwards_to_montgomery($A->Y, $A->Z);
|
||||
return self::fe_tobytes($pk);
|
||||
}
|
||||
}
|
64
wp-includes/sodium_compat/src/Core32/XChaCha20.php
Normal file
64
wp-includes/sodium_compat/src/Core32/XChaCha20.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_XChaCha20
|
||||
*/
|
||||
class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20
|
||||
{
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function stream($len = 64, $nonce = '', $key = '')
|
||||
{
|
||||
if (self::strlen($nonce) !== 24) {
|
||||
throw new SodiumException('Nonce must be 24 bytes long');
|
||||
}
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
|
||||
self::hChaCha20(
|
||||
self::substr($nonce, 0, 16),
|
||||
$key
|
||||
),
|
||||
self::substr($nonce, 16, 8)
|
||||
),
|
||||
str_repeat("\x00", $len)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @param string $ic
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
|
||||
{
|
||||
if (self::strlen($nonce) !== 24) {
|
||||
throw new SodiumException('Nonce must be 24 bytes long');
|
||||
}
|
||||
return self::encryptBytes(
|
||||
new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
|
||||
self::hChaCha20(self::substr($nonce, 0, 16), $key),
|
||||
self::substr($nonce, 16, 8),
|
||||
$ic
|
||||
),
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
57
wp-includes/sodium_compat/src/Core32/XSalsa20.php
Normal file
57
wp-includes/sodium_compat/src/Core32/XSalsa20.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParagonIE_Sodium_Core32_XSalsa20
|
||||
*/
|
||||
abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20
|
||||
{
|
||||
/**
|
||||
* Expand a key and nonce into an xsalsa20 keystream.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param int $len
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xsalsa20($len, $nonce, $key)
|
||||
{
|
||||
$ret = self::salsa20(
|
||||
$len,
|
||||
self::substr($nonce, 16, 8),
|
||||
self::hsalsa20($nonce, $key)
|
||||
);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a string with XSalsa20. Doesn't provide integrity.
|
||||
*
|
||||
* @internal You should not use this directly from another application
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $nonce
|
||||
* @param string $key
|
||||
* @return string
|
||||
* @throws SodiumException
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function xsalsa20_xor($message, $nonce, $key)
|
||||
{
|
||||
return self::xorStrings(
|
||||
$message,
|
||||
self::xsalsa20(
|
||||
self::strlen($message),
|
||||
$nonce,
|
||||
$key
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
1252
wp-includes/sodium_compat/src/Crypto.php
Normal file
1252
wp-includes/sodium_compat/src/Crypto.php
Normal file
File diff suppressed because it is too large
Load Diff
1251
wp-includes/sodium_compat/src/Crypto32.php
Normal file
1251
wp-includes/sodium_compat/src/Crypto32.php
Normal file
File diff suppressed because it is too large
Load Diff
1549
wp-includes/sodium_compat/src/File.php
Normal file
1549
wp-includes/sodium_compat/src/File.php
Normal file
File diff suppressed because it is too large
Load Diff
11
wp-includes/sodium_compat/src/SodiumException.php
Normal file
11
wp-includes/sodium_compat/src/SodiumException.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
if (!class_exists('SodiumException', false)) {
|
||||
/**
|
||||
* Class SodiumException
|
||||
*/
|
||||
class SodiumException extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '5.2-alpha-44952';
|
||||
$wp_version = '5.2-alpha-44953';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
Reference in New Issue
Block a user