Make WordPress Core

Changeset 60905


Ignore:
Timestamp:
10/06/2025 03:46:40 PM (2 months ago)
Author:
SergeyBiryukov
Message:

Upgrade/Install: Update sodium_compat to v1.23.0.

The previous version of sodium_compat was overly permissible with sodium_base642bin() when the *_NO_PADDING variants were specified, which was not compatible with ext-sodium. This has been fixed in version 1.22.0.

Version 1.23.0 includes some optimizations by replacing the array in the Curve25519 field element with 10 integer object properties instead. The result is a 7% to 12% speedup for the overall PHPUnit suite.

References:

Follow-up to [55699], [58752], [58753], [60787].

Props paragoninitiativeenterprises, SergeyBiryukov.
Fixes #64079.

Location:
trunk/src/wp-includes/sodium_compat/src
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sodium_compat/src/Compat.php

    r58752 r60905  
    2626}
    2727
     28/**
     29 * @api
     30 */
    2831class ParagonIE_Sodium_Compat
    2932{
     
    205208        /** @var string $encoded */
    206209        $encoded = (string) $encoded;
    207         if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
    208             return '';
    209         }
    210210
    211211        // Just strip before decoding
     
    219219                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
    220220                case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
    221                     return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
     221                    return ParagonIE_Sodium_Core_Base64_Original::decodeNoPadding($encoded);
    222222                case self::BASE64_VARIANT_URLSAFE:
    223223                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
    224224                case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
    225                     return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
     225                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decodeNoPadding($encoded);
    226226                default:
    227227                    throw new SodiumException('invalid base64 variant identifier');
     
    231231                throw $ex;
    232232            }
    233             throw new SodiumException('invalid base64 string');
     233            throw new SodiumException('invalid base64 string', 0, $ex);
    234234        }
    235235    }
     
    357357        /* Input validation: */
    358358        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) {
    359             throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS_128L_NPUBBYTES long');
     359            throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long');
    360360        }
    361361        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) {
     
    411411        /* Input validation: */
    412412        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS128L_NPUBBYTES) {
    413             throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
     413            throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_NPUBBYTES long');
    414414        }
    415415        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS128L_KEYBYTES) {
     
    520520        /* Input validation: */
    521521        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AEGIS256_NPUBBYTES) {
    522             throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
     522            throw new SodiumException('Nonce must be CRYPTO_AEAD_AEGIS256_NPUBBYTES long');
    523523        }
    524524        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AEGIS256_KEYBYTES) {
    525             throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS128L_KEYBYTES long');
     525            throw new SodiumException('Key must be CRYPTO_AEAD_AEGIS256_KEYBYTES long');
    526526        }
    527527
     
    563563            return false;
    564564        }
     565        if (!extension_loaded('openssl')) {
     566            return false;
     567        }
    565568        if (!is_callable('openssl_encrypt') || !is_callable('openssl_decrypt')) {
    566569            // OpenSSL isn't installed
     
    615618        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_AES256GCM_ABYTES) {
    616619            throw new SodiumException('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long');
     620        }
     621        if (!extension_loaded('openssl')) {
     622            throw new SodiumException('The OpenSSL extension is not installed');
    617623        }
    618624        if (!is_callable('openssl_decrypt')) {
     
    676682        }
    677683
     684        if (!extension_loaded('openssl')) {
     685            throw new SodiumException('The OpenSSL extension is not installed');
     686        }
    678687        if (!is_callable('openssl_encrypt')) {
    679688            throw new SodiumException('The OpenSSL extension is not installed, or openssl_encrypt() is not available');
     
    824833        /* Input validation: */
    825834        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
    826             throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
     835            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
    827836        }
    828837        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
    829             throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
     838            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long');
    830839        }
    831840
     
    902911        }
    903912        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
    904             throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
     913            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES long');
    905914        }
    906915        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
    907             throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
     916            throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES long');
    908917        }
    909918
     
    27822791            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
    27832792        }
     2793        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_SECRETBOX_MACBYTES) {
     2794            throw new SodiumException("Ciphertext must be at least CRYPTO_SECRETBOX_MACBYTES long");
     2795        }
    27842796
    27852797        if (PHP_INT_SIZE === 4) {
     
    37123724
    37133725        $len = ParagonIE_Sodium_Core_Util::strlen($var);
     3726        if ($len < 1) {
     3727            throw new SodiumException('Argument 1 cannot be empty');
     3728        }
    37143729        $c = 1;
    37153730        $copy = '';
  • trunk/src/wp-includes/sodium_compat/src/Core/Base64/Original.php

    r46858 r60905  
    186186        return $dest;
    187187    }
     188
     189    /**
     190     * @param string $encodedString
     191     * @return string
     192     */
     193    public static function decodeNoPadding(
     194        #[SensitiveParameter]
     195        $encodedString
     196    ) {
     197        $srcLen = strlen($encodedString);
     198        if ($srcLen === 0) {
     199            return '';
     200        }
     201        if (($srcLen & 3) === 0) {
     202            // If $strLen is not zero, and it is divisible by 4, then it's at least 4.
     203            if ($encodedString[$srcLen - 1] === '=' || $encodedString[$srcLen - 2] === '=') {
     204                throw new InvalidArgumentException(
     205                    "decodeNoPadding() doesn't tolerate padding"
     206                );
     207            }
     208        }
     209        return self::decode(
     210            $encodedString,
     211            true
     212        );
     213    }
    188214    // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
    189215
  • trunk/src/wp-includes/sodium_compat/src/Core/Base64/UrlSafe.php

    r46858 r60905  
    186186        return $dest;
    187187    }
     188
     189    /**
     190     * @param string $encodedString
     191     * @return string
     192     */
     193    public static function decodeNoPadding(
     194        #[SensitiveParameter]
     195        $encodedString
     196    ) {
     197        $srcLen = strlen($encodedString);
     198        if ($srcLen === 0) {
     199            return '';
     200        }
     201        if (($srcLen & 3) === 0) {
     202            // If $strLen is not zero, and it is divisible by 4, then it's at least 4.
     203            if ($encodedString[$srcLen - 1] === '=' || $encodedString[$srcLen - 2] === '=') {
     204                throw new InvalidArgumentException(
     205                    "decodeNoPadding() doesn't tolerate padding"
     206                );
     207            }
     208        }
     209        return self::decode(
     210            $encodedString,
     211            true
     212        );
     213    }
     214
    188215    // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
    189216    /**
  • trunk/src/wp-includes/sodium_compat/src/Core/ChaCha20.php

    r46586 r60905  
    330330     * @throws TypeError
    331331     */
    332     public static function stream($len = 64, $nonce = '', $key = '')
     332    public static function stream($len, $nonce, $key)
    333333    {
    334334        return self::encryptBytes(
     
    348348     * @throws TypeError
    349349     */
    350     public static function ietfStream($len, $nonce = '', $key = '')
     350    public static function ietfStream($len, $nonce, $key)
    351351    {
    352352        return self::encryptBytes(
     
    367367     * @throws TypeError
    368368     */
    369     public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
     369    public static function ietfStreamXorIc($message, $nonce, $key, $ic = '')
    370370    {
    371371        return self::encryptBytes(
     
    386386     * @throws TypeError
    387387     */
    388     public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
     388    public static function streamXorIc($message, $nonce, $key, $ic = '')
    389389    {
    390390        return self::encryptBytes(
  • trunk/src/wp-includes/sodium_compat/src/Core/ChaCha20/Ctx.php

    r51591 r60905  
    5151        $this->container[11] = self::load_4(self::substr($key, 28, 4));
    5252
    53         if (empty($counter)) {
    54             $this->container[12] = 0;
    55             $this->container[13] = 0;
    56         } else {
    57             $this->container[12] = self::load_4(self::substr($counter, 0, 4));
    58             $this->container[13] = self::load_4(self::substr($counter, 4, 4));
    59         }
     53        $counter = $this->initCounter($counter);
     54        $this->container[12] = self::load_4(self::substr($counter, 0, 4));
     55        $this->container[13] = self::load_4(self::substr($counter, 4, 4));
    6056        $this->container[14] = self::load_4(self::substr($iv, 0, 4));
    6157        $this->container[15] = self::load_4(self::substr($iv, 4, 4));
     
    121117            : null;
    122118    }
     119
     120    /**
     121     * Initialize (pad) a counter value.
     122     * @throws SodiumException
     123     *
     124     * @param string $ctr
     125     * @return string
     126     */
     127    public function initCounter(
     128        #[SensitiveParameter]
     129        $ctr
     130    ) {
     131        $len = self::strlen($ctr);
     132        if ($len === 0) {
     133            return str_repeat("\0", 8);
     134        }
     135        if ($len < 8) {
     136            return $ctr . str_repeat("\0", 8 - $len);
     137        }
     138        if ($len > 8) {
     139            throw new SodiumException("counter cannot be more than 8 bytes");
     140        }
     141        return $ctr;
     142    }
    123143}
  • trunk/src/wp-includes/sodium_compat/src/Core/ChaCha20/IetfCtx.php

    r46586 r60905  
    2727            throw new InvalidArgumentException('ChaCha20 expects a 96-bit nonce in IETF mode.');
    2828        }
     29        $counter = $this->initCounter($counter);
    2930        parent::__construct($key, self::substr($iv, 0, 8), $counter);
    30 
    31         if (!empty($counter)) {
    32             $this->container[12] = self::load_4(self::substr($counter, 0, 4));
    33         }
     31        $this->container[12] = self::load_4(self::substr($counter, 0, 4));
    3432        $this->container[13] = self::load_4(self::substr($iv, 0, 4));
    3533        $this->container[14] = self::load_4(self::substr($iv, 4, 4));
  • trunk/src/wp-includes/sodium_compat/src/Core/Curve25519.php

    r52988 r60905  
    2525    public static function fe_0()
    2626    {
    27         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    28             array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    29         );
     27        return new ParagonIE_Sodium_Core_Curve25519_Fe();
    3028    }
    3129
     
    3937    public static function fe_1()
    4038    {
    41         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    42             array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    43         );
     39        $fe = new ParagonIE_Sodium_Core_Curve25519_Fe();
     40        $fe->e0 = 1;
     41        return $fe;
    4442    }
    4543
     
    5957        ParagonIE_Sodium_Core_Curve25519_Fe $g
    6058    ) {
    61         /** @var array<int, int> $arr */
    62         $arr = array();
    63         for ($i = 0; $i < 10; ++$i) {
    64             $arr[$i] = (int) ($f[$i] + $g[$i]);
    65         }
    66         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($arr);
     59        return new ParagonIE_Sodium_Core_Curve25519_Fe(
     60            (int)($f->e0 + $g->e0),
     61            (int)($f->e1 + $g->e1),
     62            (int)($f->e2 + $g->e2),
     63            (int)($f->e3 + $g->e3),
     64            (int)($f->e4 + $g->e4),
     65            (int)($f->e5 + $g->e5),
     66            (int)($f->e6 + $g->e6),
     67            (int)($f->e7 + $g->e7),
     68            (int)($f->e8 + $g->e8),
     69            (int)($f->e9 + $g->e9)
     70        );
    6771    }
    6872
     
    8387        $b = 0
    8488    ) {
    85         /** @var array<int, int> $h */
    86         $h = array();
     89        $h = new ParagonIE_Sodium_Core_Curve25519_Fe();
    8790        $b *= -1;
    88         for ($i = 0; $i < 10; ++$i) {
    89             $x = (($f[$i] ^ $g[$i]) & $b);
    90             $h[$i] = ($f[$i]) ^ $x;
    91         }
    92         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h);
     91        $x = (($f->e0 ^ $g->e0) & $b); $h->e0 = $f->e0 ^ $x;
     92        $x = (($f->e1 ^ $g->e1) & $b); $h->e1 = $f->e1 ^ $x;
     93        $x = (($f->e2 ^ $g->e2) & $b); $h->e2 = $f->e2 ^ $x;
     94        $x = (($f->e3 ^ $g->e3) & $b); $h->e3 = $f->e3 ^ $x;
     95        $x = (($f->e4 ^ $g->e4) & $b); $h->e4 = $f->e4 ^ $x;
     96        $x = (($f->e5 ^ $g->e5) & $b); $h->e5 = $f->e5 ^ $x;
     97        $x = (($f->e6 ^ $g->e6) & $b); $h->e6 = $f->e6 ^ $x;
     98        $x = (($f->e7 ^ $g->e7) & $b); $h->e7 = $f->e7 ^ $x;
     99        $x = (($f->e8 ^ $g->e8) & $b); $h->e8 = $f->e8 ^ $x;
     100        $x = (($f->e9 ^ $g->e9) & $b); $h->e9 = $f->e9 ^ $x;
     101        return $h;
    93102    }
    94103
     
    103112    public static function fe_copy(ParagonIE_Sodium_Core_Curve25519_Fe $f)
    104113    {
    105         $h = clone $f;
    106         return $h;
     114        return clone $f;
    107115    }
    108116
     
    166174        $h8 -= $carry8 << 26;
    167175
    168         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    169             array(
    170                 (int) $h0,
    171                 (int) $h1,
    172                 (int) $h2,
    173                 (int) $h3,
    174                 (int) $h4,
    175                 (int) $h5,
    176                 (int) $h6,
    177                 (int) $h7,
    178                 (int) $h8,
    179                 (int) $h9
    180             )
     176        return new ParagonIE_Sodium_Core_Curve25519_Fe(
     177            (int) $h0,
     178            (int) $h1,
     179            (int) $h2,
     180            (int) $h3,
     181            (int) $h4,
     182            (int) $h5,
     183            (int) $h6,
     184            (int) $h7,
     185            (int) $h8,
     186            (int) $h9
    181187        );
    182188    }
     
    192198    public static function fe_tobytes(ParagonIE_Sodium_Core_Curve25519_Fe $h)
    193199    {
    194         $h0 = (int) $h[0];
    195         $h1 = (int) $h[1];
    196         $h2 = (int) $h[2];
    197         $h3 = (int) $h[3];
    198         $h4 = (int) $h[4];
    199         $h5 = (int) $h[5];
    200         $h6 = (int) $h[6];
    201         $h7 = (int) $h[7];
    202         $h8 = (int) $h[8];
    203         $h9 = (int) $h[9];
     200        $h0 = (int) $h->e0;
     201        $h1 = (int) $h->e1;
     202        $h2 = (int) $h->e2;
     203        $h3 = (int) $h->e3;
     204        $h4 = (int) $h->e4;
     205        $h5 = (int) $h->e5;
     206        $h6 = (int) $h->e6;
     207        $h7 = (int) $h->e7;
     208        $h8 = (int) $h->e8;
     209        $h9 = (int) $h->e9;
    204210
    205211        $q = (self::mul($h9, 19, 5) + (1 << 24)) >> 25;
     
    346352        $f = self::fe_normalize($f);
    347353        $g = self::fe_normalize($g);
    348         $f0 = $f[0];
    349         $f1 = $f[1];
    350         $f2 = $f[2];
    351         $f3 = $f[3];
    352         $f4 = $f[4];
    353         $f5 = $f[5];
    354         $f6 = $f[6];
    355         $f7 = $f[7];
    356         $f8 = $f[8];
    357         $f9 = $f[9];
    358         $g0 = $g[0];
    359         $g1 = $g[1];
    360         $g2 = $g[2];
    361         $g3 = $g[3];
    362         $g4 = $g[4];
    363         $g5 = $g[5];
    364         $g6 = $g[6];
    365         $g7 = $g[7];
    366         $g8 = $g[8];
    367         $g9 = $g[9];
     354        $f0 = $f->e0;
     355        $f1 = $f->e1;
     356        $f2 = $f->e2;
     357        $f3 = $f->e3;
     358        $f4 = $f->e4;
     359        $f5 = $f->e5;
     360        $f6 = $f->e6;
     361        $f7 = $f->e7;
     362        $f8 = $f->e8;
     363        $f9 = $f->e9;
     364        $g0 = $g->e0;
     365        $g1 = $g->e1;
     366        $g2 = $g->e2;
     367        $g3 = $g->e3;
     368        $g4 = $g->e4;
     369        $g5 = $g->e5;
     370        $g6 = $g->e6;
     371        $g7 = $g->e7;
     372        $g8 = $g->e8;
     373        $g9 = $g->e9;
    368374        $g1_19 = self::mul($g1, 19, 5);
    369375        $g2_19 = self::mul($g2, 19, 5);
     
    536542
    537543        return self::fe_normalize(
    538             ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    539                 array(
    540                     (int) $h0,
    541                     (int) $h1,
    542                     (int) $h2,
    543                     (int) $h3,
    544                     (int) $h4,
    545                     (int) $h5,
    546                     (int) $h6,
    547                     (int) $h7,
    548                     (int) $h8,
    549                     (int) $h9
    550                 )
     544            new ParagonIE_Sodium_Core_Curve25519_Fe(
     545                (int) $h0,
     546                (int) $h1,
     547                (int) $h2,
     548                (int) $h3,
     549                (int) $h4,
     550                (int) $h5,
     551                (int) $h6,
     552                (int) $h7,
     553                (int) $h8,
     554                (int) $h9
    551555            )
    552556        );
     
    566570    public static function fe_neg(ParagonIE_Sodium_Core_Curve25519_Fe $f)
    567571    {
    568         $h = new ParagonIE_Sodium_Core_Curve25519_Fe();
    569         for ($i = 0; $i < 10; ++$i) {
    570             $h[$i] = -$f[$i];
    571         }
    572         return self::fe_normalize($h);
     572        return self::fe_normalize(
     573            new ParagonIE_Sodium_Core_Curve25519_Fe(
     574                -$f->e0,
     575                -$f->e1,
     576                -$f->e2,
     577                -$f->e3,
     578                -$f->e4,
     579                -$f->e5,
     580                -$f->e6,
     581                -$f->e7,
     582                -$f->e8,
     583                -$f->e9
     584            )
     585        );
    573586    }
    574587
     
    586599    {
    587600        $f = self::fe_normalize($f);
    588         $f0 = (int) $f[0];
    589         $f1 = (int) $f[1];
    590         $f2 = (int) $f[2];
    591         $f3 = (int) $f[3];
    592         $f4 = (int) $f[4];
    593         $f5 = (int) $f[5];
    594         $f6 = (int) $f[6];
    595         $f7 = (int) $f[7];
    596         $f8 = (int) $f[8];
    597         $f9 = (int) $f[9];
     601        $f0 = (int) $f->e0;
     602        $f1 = (int) $f->e1;
     603        $f2 = (int) $f->e2;
     604        $f3 = (int) $f->e3;
     605        $f4 = (int) $f->e4;
     606        $f5 = (int) $f->e5;
     607        $f6 = (int) $f->e6;
     608        $f7 = (int) $f->e7;
     609        $f8 = (int) $f->e8;
     610        $f9 = (int) $f->e9;
    598611
    599612        $f0_2 = $f0 << 1;
     
    720733
    721734        return self::fe_normalize(
    722             ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    723                 array(
    724                     (int) $h0,
    725                     (int) $h1,
    726                     (int) $h2,
    727                     (int) $h3,
    728                     (int) $h4,
    729                     (int) $h5,
    730                     (int) $h6,
    731                     (int) $h7,
    732                     (int) $h8,
    733                     (int) $h9
    734                 )
     735            new ParagonIE_Sodium_Core_Curve25519_Fe(
     736                (int) $h0,
     737                (int) $h1,
     738                (int) $h2,
     739                (int) $h3,
     740                (int) $h4,
     741                (int) $h5,
     742                (int) $h6,
     743                (int) $h7,
     744                (int) $h8,
     745                (int) $h9
    735746            )
    736747        );
     
    751762    {
    752763        $f = self::fe_normalize($f);
    753         $f0 = (int) $f[0];
    754         $f1 = (int) $f[1];
    755         $f2 = (int) $f[2];
    756         $f3 = (int) $f[3];
    757         $f4 = (int) $f[4];
    758         $f5 = (int) $f[5];
    759         $f6 = (int) $f[6];
    760         $f7 = (int) $f[7];
    761         $f8 = (int) $f[8];
    762         $f9 = (int) $f[9];
     764        $f0 = (int) $f->e0;
     765        $f1 = (int) $f->e1;
     766        $f2 = (int) $f->e2;
     767        $f3 = (int) $f->e3;
     768        $f4 = (int) $f->e4;
     769        $f5 = (int) $f->e5;
     770        $f6 = (int) $f->e6;
     771        $f7 = (int) $f->e7;
     772        $f8 = (int) $f->e8;
     773        $f9 = (int) $f->e9;
    763774
    764775        $f0_2 = $f0 << 1;
     
    886897
    887898        return self::fe_normalize(
    888             ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    889                 array(
    890                     (int) $h0,
    891                     (int) $h1,
    892                     (int) $h2,
    893                     (int) $h3,
    894                     (int) $h4,
    895                     (int) $h5,
    896                     (int) $h6,
    897                     (int) $h7,
    898                     (int) $h8,
    899                     (int) $h9
    900                 )
     899            new ParagonIE_Sodium_Core_Curve25519_Fe(
     900                (int) $h0,
     901                (int) $h1,
     902                (int) $h2,
     903                (int) $h3,
     904                (int) $h4,
     905                (int) $h5,
     906                (int) $h6,
     907                (int) $h7,
     908                (int) $h8,
     909                (int) $h9
    901910            )
    902911        );
     
    11001109    {
    11011110        return self::fe_normalize(
    1102             ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
    1103                 array(
    1104                     (int) ($f[0] - $g[0]),
    1105                     (int) ($f[1] - $g[1]),
    1106                     (int) ($f[2] - $g[2]),
    1107                     (int) ($f[3] - $g[3]),
    1108                     (int) ($f[4] - $g[4]),
    1109                     (int) ($f[5] - $g[5]),
    1110                     (int) ($f[6] - $g[6]),
    1111                     (int) ($f[7] - $g[7]),
    1112                     (int) ($f[8] - $g[8]),
    1113                     (int) ($f[9] - $g[9])
    1114                 )
     1111            new ParagonIE_Sodium_Core_Curve25519_Fe(
     1112                (int) ($f->e0 - $g->e0),
     1113                (int) ($f->e1 - $g->e1),
     1114                (int) ($f->e2 - $g->e2),
     1115                (int) ($f->e3 - $g->e3),
     1116                (int) ($f->e4 - $g->e4),
     1117                (int) ($f->e5 - $g->e5),
     1118                (int) ($f->e6 - $g->e6),
     1119                (int) ($f->e7 - $g->e7),
     1120                (int) ($f->e8 - $g->e8),
     1121                (int) ($f->e9 - $g->e9)
    11151122            )
    11161123        );
     
    28012808         */
    28022809        $arr = array(
    2803             (int) ($s0 >> 0),
    2804             (int) ($s0 >> 8),
    2805             (int) (($s0 >> 16) | $s1 << 5),
    2806             (int) ($s1 >> 3),
    2807             (int) ($s1 >> 11),
    2808             (int) (($s1 >> 19) | $s2 << 2),
    2809             (int) ($s2 >> 6),
    2810             (int) (($s2 >> 14) | $s3 << 7),
    2811             (int) ($s3 >> 1),
    2812             (int) ($s3 >> 9),
    2813             (int) (($s3 >> 17) | $s4 << 4),
    2814             (int) ($s4 >> 4),
    2815             (int) ($s4 >> 12),
    2816             (int) (($s4 >> 20) | $s5 << 1),
    2817             (int) ($s5 >> 7),
    2818             (int) (($s5 >> 15) | $s6 << 6),
    2819             (int) ($s6 >> 2),
    2820             (int) ($s6 >> 10),
    2821             (int) (($s6 >> 18) | $s7 << 3),
    2822             (int) ($s7 >> 5),
    2823             (int) ($s7 >> 13),
    2824             (int) ($s8 >> 0),
    2825             (int) ($s8 >> 8),
    2826             (int) (($s8 >> 16) | $s9 << 5),
    2827             (int) ($s9 >> 3),
    2828             (int) ($s9 >> 11),
    2829             (int) (($s9 >> 19) | $s10 << 2),
    2830             (int) ($s10 >> 6),
    2831             (int) (($s10 >> 14) | $s11 << 7),
    2832             (int) ($s11 >> 1),
    2833             (int) ($s11 >> 9),
    2834             (int) $s11 >> 17
     2810            (int) (0xff & ($s0 >> 0)),
     2811            (int) (0xff & ($s0 >> 8)),
     2812            (int) (0xff & (($s0 >> 16) | $s1 << 5)),
     2813            (int) (0xff & ($s1 >> 3)),
     2814            (int) (0xff & ($s1 >> 11)),
     2815            (int) (0xff & (($s1 >> 19) | $s2 << 2)),
     2816            (int) (0xff & ($s2 >> 6)),
     2817            (int) (0xff & (($s2 >> 14) | $s3 << 7)),
     2818            (int) (0xff & ($s3 >> 1)),
     2819            (int) (0xff & ($s3 >> 9)),
     2820            (int) (0xff & (($s3 >> 17) | $s4 << 4)),
     2821            (int) (0xff & ($s4 >> 4)),
     2822            (int) (0xff & ($s4 >> 12)),
     2823            (int) (0xff & (($s4 >> 20) | $s5 << 1)),
     2824            (int) (0xff & ($s5 >> 7)),
     2825            (int) (0xff & (($s5 >> 15) | $s6 << 6)),
     2826            (int) (0xff & ($s6 >> 2)),
     2827            (int) (0xff & ($s6 >> 10)),
     2828            (int) (0xff & (($s6 >> 18) | $s7 << 3)),
     2829            (int) (0xff & ($s7 >> 5)),
     2830            (int) (0xff & ($s7 >> 13)),
     2831            (int) (0xff & ($s8 >> 0)),
     2832            (int) (0xff & ($s8 >> 8)),
     2833            (int) (0xff & (($s8 >> 16) | $s9 << 5)),
     2834            (int) (0xff & ($s9 >> 3)),
     2835            (int) (0xff & ($s9 >> 11)),
     2836            (int) (0xff & (($s9 >> 19) | $s10 << 2)),
     2837            (int) (0xff & ($s10 >> 6)),
     2838            (int) (0xff & (($s10 >> 14) | $s11 << 7)),
     2839            (int) (0xff & ($s11 >> 1)),
     2840            (int) (0xff & ($s11 >> 9)),
     2841            (int) (0xff & ($s11 >> 17))
    28352842        );
    28362843        return self::intArrayToString($arr);
     
    37953802        $s_[0] &= 248;
    37963803        $s_[31] |= 64;
    3797         $s_[31] &= 128;
     3804        $s_[31] &= 127;
    37983805        return self::intArrayToString($s_);
    37993806    }
     
    38123819
    38133820        $g = self::fe_copy($f);
     3821        $e = array(
     3822            $g->e0, $g->e1, $g->e2, $g->e3, $g->e4,
     3823            $g->e5, $g->e6, $g->e7, $g->e8, $g->e9
     3824        );
    38143825        for ($i = 0; $i < 10; ++$i) {
    3815             $mask = -(($g[$i] >> $x) & 1);
     3826            $mask = -(($e[$i] >> $x) & 1);
    38163827
    38173828            /*
    3818              * Get two candidate normalized values for $g[$i], depending on the sign of $g[$i]:
     3829             * Get two candidate normalized values for $e[$i], depending on the sign of $e[$i]:
    38193830             */
    3820             $a = $g[$i] & 0x7ffffff;
    3821             $b = -((-$g[$i]) & 0x7ffffff);
     3831            $a = $e[$i] & 0x7ffffff;
     3832            $b = -((-$e[$i]) & 0x7ffffff);
    38223833
    38233834            /*
     
    38263837             * The following is equivalent to this ternary:
    38273838             *
    3828              * $g[$i] = (($g[$i] >> $x) & 1) ? $a : $b;
     3839             * $e[$i] = (($e[$i] >> $x) & 1) ? $a : $b;
    38293840             *
    38303841             * Except what's written doesn't contain timing leaks.
    38313842             */
    3832             $g[$i] = ($a ^ (($a ^ $b) & $mask));
    3833         }
     3843            $e[$i] = ($a ^ (($a ^ $b) & $mask));
     3844        }
     3845        $g->e0 = $e[0];
     3846        $g->e1 = $e[1];
     3847        $g->e2 = $e[2];
     3848        $g->e3 = $e[3];
     3849        $g->e4 = $e[4];
     3850        $g->e5 = $e[5];
     3851        $g->e6 = $e[6];
     3852        $g->e7 = $e[7];
     3853        $g->e8 = $e[8];
     3854        $g->e9 = $e[9];
    38343855        return $g;
    38353856    }
  • trunk/src/wp-includes/sodium_compat/src/Core/Curve25519/Fe.php

    r51591 r60905  
    1313{
    1414    /**
    15      * @var array<int, int>
    16      */
    17     protected $container = array();
    18 
    19     /**
    20      * @var int
    21      */
    22     protected $size = 10;
    23 
    24     /**
    25      * @internal You should not use this directly from another application
    26      *
    27      * @param array<int, int> $array
    28      * @param bool $save_indexes
     15     * @var int
     16     */
     17    public $e0 = 0;
     18
     19    /**
     20     * @var int
     21     */
     22    public $e1 = 0;
     23
     24    /**
     25     * @var int
     26     */
     27    public $e2 = 0;
     28
     29    /**
     30     * @var int
     31     */
     32    public $e3 = 0;
     33
     34    /**
     35     * @var int
     36     */
     37    public $e4 = 0;
     38
     39    /**
     40     * @var int
     41     */
     42    public $e5 = 0;
     43
     44    /**
     45     * @var int
     46     */
     47    public $e6 = 0;
     48
     49    /**
     50     * @var int
     51     */
     52    public $e7 = 0;
     53
     54    /**
     55     * @var int
     56     */
     57    public $e8 = 0;
     58
     59    /**
     60     * @var int
     61     */
     62    public $e9 = 0;
     63
     64    /**
     65     * @param int $e0
     66     * @param int $e1
     67     * @param int $e2
     68     * @param int $e3
     69     * @param int $e4
     70     * @param int $e5
     71     * @param int $e6
     72     * @param int $e7
     73     * @param int $e8
     74     * @param int $e9
     75     */
     76    public function __construct(
     77        $e0 = 0,
     78        $e1 = 0,
     79        $e2 = 0,
     80        $e3 = 0,
     81        $e4 = 0,
     82        $e5 = 0,
     83        $e6 = 0,
     84        $e7 = 0,
     85        $e8 = 0,
     86        $e9 = 0
     87    ) {
     88        $this->e0 = $e0;
     89        $this->e1 = $e1;
     90        $this->e2 = $e2;
     91        $this->e3 = $e3;
     92        $this->e4 = $e4;
     93        $this->e5 = $e5;
     94        $this->e6 = $e6;
     95        $this->e7 = $e7;
     96        $this->e8 = $e8;
     97        $this->e9 = $e9;
     98    }
     99
     100    /**
     101     * @internal You should not use this directly from another application
     102     *
     103     * @param array $array
    29104     * @return self
    30105     */
    31     public static function fromArray($array, $save_indexes = null)
    32     {
    33         $count = count($array);
    34         if ($save_indexes) {
    35             $keys = array_keys($array);
    36         } else {
    37             $keys = range(0, $count - 1);
    38         }
    39         $array = array_values($array);
    40         /** @var array<int, int> $keys */
    41 
     106    public static function fromArray($array)
     107    {
    42108        $obj = new ParagonIE_Sodium_Core_Curve25519_Fe();
    43         if ($save_indexes) {
    44             for ($i = 0; $i < $count; ++$i) {
    45                 $obj->offsetSet($keys[$i], $array[$i]);
    46             }
    47         } else {
    48             for ($i = 0; $i < $count; ++$i) {
    49                 $obj->offsetSet($i, $array[$i]);
    50             }
    51         }
     109        $obj->e0 = isset($array[0]) ? (int) $array[0] : 0;
     110        $obj->e1 = isset($array[1]) ? (int) $array[1] : 0;
     111        $obj->e2 = isset($array[2]) ? (int) $array[2] : 0;
     112        $obj->e3 = isset($array[3]) ? (int) $array[3] : 0;
     113        $obj->e4 = isset($array[4]) ? (int) $array[4] : 0;
     114        $obj->e5 = isset($array[5]) ? (int) $array[5] : 0;
     115        $obj->e6 = isset($array[6]) ? (int) $array[6] : 0;
     116        $obj->e7 = isset($array[7]) ? (int) $array[7] : 0;
     117        $obj->e8 = isset($array[8]) ? (int) $array[8] : 0;
     118        $obj->e9 = isset($array[9]) ? (int) $array[9] : 0;
    52119        return $obj;
    53120    }
     
    59126     * @param int $value
    60127     * @return void
    61      * @psalm-suppress MixedArrayOffset
    62128     */
    63129    #[ReturnTypeWillChange]
     
    67133            throw new InvalidArgumentException('Expected an integer');
    68134        }
    69         if (is_null($offset)) {
    70             $this->container[] = $value;
    71         } else {
    72             $this->container[$offset] = $value;
     135        switch ($offset) {
     136            case 0:
     137                $this->e0 = $value;
     138                break;
     139            case 1:
     140                $this->e1 = $value;
     141                break;
     142            case 2:
     143                $this->e2 = $value;
     144                break;
     145            case 3:
     146                $this->e3 = $value;
     147                break;
     148            case 4:
     149                $this->e4 = $value;
     150                break;
     151            case 5:
     152                $this->e5 = $value;
     153                break;
     154            case 6:
     155                $this->e6 = $value;
     156                break;
     157            case 7:
     158                $this->e7 = $value;
     159                break;
     160            case 8:
     161                $this->e8 = $value;
     162                break;
     163            case 9:
     164                $this->e9 = $value;
     165                break;
     166            default:
     167                throw new OutOfBoundsException('Index out of bounds');
    73168        }
    74169    }
     
    79174     * @param int $offset
    80175     * @return bool
    81      * @psalm-suppress MixedArrayOffset
    82176     */
    83177    #[ReturnTypeWillChange]
    84178    public function offsetExists($offset)
    85179    {
    86         return isset($this->container[$offset]);
     180        return $offset >= 0 && $offset < 10;
    87181    }
    88182
     
    92186     * @param int $offset
    93187     * @return void
    94      * @psalm-suppress MixedArrayOffset
    95188     */
    96189    #[ReturnTypeWillChange]
    97190    public function offsetUnset($offset)
    98191    {
    99         unset($this->container[$offset]);
     192        switch ($offset) {
     193            case 0:
     194                $this->e0 = 0;
     195                break;
     196            case 1:
     197                $this->e1 = 0;
     198                break;
     199            case 2:
     200                $this->e2 = 0;
     201                break;
     202            case 3:
     203                $this->e3 = 0;
     204                break;
     205            case 4:
     206                $this->e4 = 0;
     207                break;
     208            case 5:
     209                $this->e5 = 0;
     210                break;
     211            case 6:
     212                $this->e6 = 0;
     213                break;
     214            case 7:
     215                $this->e7 = 0;
     216                break;
     217            case 8:
     218                $this->e8 = 0;
     219                break;
     220            case 9:
     221                $this->e9 = 0;
     222                break;
     223            default:
     224                throw new OutOfBoundsException('Index out of bounds');
     225        }
    100226    }
    101227
     
    105231     * @param int $offset
    106232     * @return int
    107      * @psalm-suppress MixedArrayOffset
    108233     */
    109234    #[ReturnTypeWillChange]
    110235    public function offsetGet($offset)
    111236    {
    112         if (!isset($this->container[$offset])) {
    113             $this->container[$offset] = 0;
    114         }
    115         return (int) ($this->container[$offset]);
     237        switch ($offset) {
     238            case 0:
     239                return (int) $this->e0;
     240            case 1:
     241                return (int) $this->e1;
     242            case 2:
     243                return (int) $this->e2;
     244            case 3:
     245                return (int) $this->e3;
     246            case 4:
     247                return (int) $this->e4;
     248            case 5:
     249                return (int) $this->e5;
     250            case 6:
     251                return (int) $this->e6;
     252            case 7:
     253                return (int) $this->e7;
     254            case 8:
     255                return (int) $this->e8;
     256            case 9:
     257                return (int) $this->e9;
     258            default:
     259                throw new OutOfBoundsException('Index out of bounds');
     260        }
    116261    }
    117262
     
    123268    public function __debugInfo()
    124269    {
    125         return array(implode(', ', $this->container));
     270        return array(
     271            implode(', ', array(
     272                $this->e0, $this->e1, $this->e2, $this->e3, $this->e4,
     273                $this->e5, $this->e6, $this->e7, $this->e8, $this->e9
     274            ))
     275        );
    126276    }
    127277}
  • trunk/src/wp-includes/sodium_compat/src/Core/Ed25519.php

    r54150 r60905  
    4747    {
    4848        if (self::strlen($seed) !== self::SEED_BYTES) {
    49             throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
     49            throw new SodiumException('crypto_sign keypair seed must be 32 bytes long');
    5050        }
    5151
     
    6666    {
    6767        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
    68             throw new RangeException('crypto_sign keypair must be 96 bytes long');
     68            throw new SodiumException('crypto_sign keypair must be 96 bytes long');
    6969        }
    7070        return self::substr($keypair, 0, 64);
     
    8181    {
    8282        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
    83             throw new RangeException('crypto_sign keypair must be 96 bytes long');
     83            throw new SodiumException('crypto_sign keypair must be 96 bytes long');
    8484        }
    8585        return self::substr($keypair, 64, 32);
     
    213213    public static function sign_detached($message, $sk)
    214214    {
     215        if (self::strlen($sk) !== 64) {
     216            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long');
     217        }
    215218        # crypto_hash_sha512(az, sk, 32);
    216219        $az =  hash('sha512', self::substr($sk, 0, 32), true);
     
    278281    public static function verify_detached($sig, $message, $pk)
    279282    {
    280         if (self::strlen($sig) < 64) {
    281             throw new SodiumException('Signature is too short');
     283        if (self::strlen($sig) !== 64) {
     284            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long');
     285        }
     286        if (self::strlen($pk) !== 32) {
     287            throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long');
    282288        }
    283289        if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
  • trunk/src/wp-includes/sodium_compat/src/Core/HChaCha20.php

    r46586 r60905  
    1515     * @param string|null $c
    1616     * @return string
     17     *
     18     * @throws SodiumException
    1719     * @throws TypeError
    1820     */
    19     public static function hChaCha20($in = '', $key = '', $c = null)
     21    public static function hChaCha20($in, $key, $c = null)
    2022    {
     23        if (self::strlen($in) !== 16) {
     24            throw new SodiumException('Argument 1 must be 16 bytes');
     25        }
     26        if (self::strlen($key) !== 32) {
     27            throw new SodiumException('Argument 2 must be 32 bytes');
     28        }
    2129        $ctx = array();
    2230
  • trunk/src/wp-includes/sodium_compat/src/Core/Poly1305.php

    r46586 r60905  
    2323    public static function onetimeauth($m, $key)
    2424    {
    25         if (self::strlen($key) < 32) {
     25        if (self::strlen($key) !== 32) {
    2626            throw new InvalidArgumentException(
    2727                'Key must be 32 bytes long.'
  • trunk/src/wp-includes/sodium_compat/src/Core/Util.php

    r58752 r60905  
    182182            $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT);
    183183            $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT);
     184        } elseif ($leftLen !== $rightLen) {
     185            throw new SodiumException("Argument #1 and argument #2 must have the same length");
    184186        }
    185187
     
    337339     * @param bool $strictPadding
    338340     * @return string (raw binary)
    339      * @throws RangeException
     341     *
     342     * @throws SodiumException
    340343     * @throws TypeError
    341344     */
     
    355358        $hex_len = self::strlen($hexString);
    356359        $state = 0;
    357         if (($hex_len & 1) !== 0) {
    358             if ($strictPadding) {
    359                 throw new RangeException(
    360                     'Expected an even number of hexadecimal characters'
    361                 );
    362             } else {
    363                 $hexString = '0' . $hexString;
    364                 ++$hex_len;
    365             }
    366         }
    367360
    368361        $chunk = unpack('C*', $hexString);
     
    391384            $state ^= 1;
    392385        }
     386        if ($strictPadding && $state !== 0) {
     387            throw new SodiumException(
     388                'Expected an even number of hexadecimal characters'
     389            );
     390        }
    393391        return $bin;
    394392    }
     
    535533    public static function memcmp($left, $right)
    536534    {
    537         if (self::hashEquals($left, $right)) {
    538             return 0;
    539         }
    540         return -1;
     535        $e = (int) !self::hashEquals($left, $right);
     536        return 0 - $e;
    541537    }
    542538
  • trunk/src/wp-includes/sodium_compat/src/Core/X25519.php

    r46586 r60905  
    2626        $b = 0
    2727    ) {
    28         $f0 = (int) $f[0];
    29         $f1 = (int) $f[1];
    30         $f2 = (int) $f[2];
    31         $f3 = (int) $f[3];
    32         $f4 = (int) $f[4];
    33         $f5 = (int) $f[5];
    34         $f6 = (int) $f[6];
    35         $f7 = (int) $f[7];
    36         $f8 = (int) $f[8];
    37         $f9 = (int) $f[9];
    38         $g0 = (int) $g[0];
    39         $g1 = (int) $g[1];
    40         $g2 = (int) $g[2];
    41         $g3 = (int) $g[3];
    42         $g4 = (int) $g[4];
    43         $g5 = (int) $g[5];
    44         $g6 = (int) $g[6];
    45         $g7 = (int) $g[7];
    46         $g8 = (int) $g[8];
    47         $g9 = (int) $g[9];
    4828        $b = -$b;
    49         $x0 = ($f0 ^ $g0) & $b;
    50         $x1 = ($f1 ^ $g1) & $b;
    51         $x2 = ($f2 ^ $g2) & $b;
    52         $x3 = ($f3 ^ $g3) & $b;
    53         $x4 = ($f4 ^ $g4) & $b;
    54         $x5 = ($f5 ^ $g5) & $b;
    55         $x6 = ($f6 ^ $g6) & $b;
    56         $x7 = ($f7 ^ $g7) & $b;
    57         $x8 = ($f8 ^ $g8) & $b;
    58         $x9 = ($f9 ^ $g9) & $b;
    59         $f[0] = $f0 ^ $x0;
    60         $f[1] = $f1 ^ $x1;
    61         $f[2] = $f2 ^ $x2;
    62         $f[3] = $f3 ^ $x3;
    63         $f[4] = $f4 ^ $x4;
    64         $f[5] = $f5 ^ $x5;
    65         $f[6] = $f6 ^ $x6;
    66         $f[7] = $f7 ^ $x7;
    67         $f[8] = $f8 ^ $x8;
    68         $f[9] = $f9 ^ $x9;
    69         $g[0] = $g0 ^ $x0;
    70         $g[1] = $g1 ^ $x1;
    71         $g[2] = $g2 ^ $x2;
    72         $g[3] = $g3 ^ $x3;
    73         $g[4] = $g4 ^ $x4;
    74         $g[5] = $g5 ^ $x5;
    75         $g[6] = $g6 ^ $x6;
    76         $g[7] = $g7 ^ $x7;
    77         $g[8] = $g8 ^ $x8;
    78         $g[9] = $g9 ^ $x9;
     29        $x0 = ($f->e0 ^ $g->e0) & $b;
     30        $x1 = ($f->e1 ^ $g->e1) & $b;
     31        $x2 = ($f->e2 ^ $g->e2) & $b;
     32        $x3 = ($f->e3 ^ $g->e3) & $b;
     33        $x4 = ($f->e4 ^ $g->e4) & $b;
     34        $x5 = ($f->e5 ^ $g->e5) & $b;
     35        $x6 = ($f->e6 ^ $g->e6) & $b;
     36        $x7 = ($f->e7 ^ $g->e7) & $b;
     37        $x8 = ($f->e8 ^ $g->e8) & $b;
     38        $x9 = ($f->e9 ^ $g->e9) & $b;
     39        $f->e0 ^= $x0;
     40        $f->e1 ^= $x1;
     41        $f->e2 ^= $x2;
     42        $f->e3 ^= $x3;
     43        $f->e4 ^= $x4;
     44        $f->e5 ^= $x5;
     45        $f->e6 ^= $x6;
     46        $f->e7 ^= $x7;
     47        $f->e8 ^= $x8;
     48        $f->e9 ^= $x9;
     49        $g->e0 ^= $x0;
     50        $g->e1 ^= $x1;
     51        $g->e2 ^= $x2;
     52        $g->e3 ^= $x3;
     53        $g->e4 ^= $x4;
     54        $g->e5 ^= $x5;
     55        $g->e6 ^= $x6;
     56        $g->e7 ^= $x7;
     57        $g->e8 ^= $x8;
     58        $g->e9 ^= $x9;
    7959    }
    8060
     
    8767    public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f)
    8868    {
    89         $h = array(
    90             self::mul((int) $f[0], 121666, 17),
    91             self::mul((int) $f[1], 121666, 17),
    92             self::mul((int) $f[2], 121666, 17),
    93             self::mul((int) $f[3], 121666, 17),
    94             self::mul((int) $f[4], 121666, 17),
    95             self::mul((int) $f[5], 121666, 17),
    96             self::mul((int) $f[6], 121666, 17),
    97             self::mul((int) $f[7], 121666, 17),
    98             self::mul((int) $f[8], 121666, 17),
    99             self::mul((int) $f[9], 121666, 17)
    100         );
    101 
    102         /** @var int $carry9 */
    103         $carry9 = ($h[9] + (1 << 24)) >> 25;
    104         $h[0] += self::mul($carry9, 19, 5);
    105         $h[9] -= $carry9 << 25;
    106         /** @var int $carry1 */
    107         $carry1 = ($h[1] + (1 << 24)) >> 25;
    108         $h[2] += $carry1;
    109         $h[1] -= $carry1 << 25;
    110         /** @var int $carry3 */
    111         $carry3 = ($h[3] + (1 << 24)) >> 25;
    112         $h[4] += $carry3;
    113         $h[3] -= $carry3 << 25;
    114         /** @var int $carry5 */
    115         $carry5 = ($h[5] + (1 << 24)) >> 25;
    116         $h[6] += $carry5;
    117         $h[5] -= $carry5 << 25;
    118         /** @var int $carry7 */
    119         $carry7 = ($h[7] + (1 << 24)) >> 25;
    120         $h[8] += $carry7;
    121         $h[7] -= $carry7 << 25;
    122 
    123         /** @var int $carry0 */
    124         $carry0 = ($h[0] + (1 << 25)) >> 26;
    125         $h[1] += $carry0;
    126         $h[0] -= $carry0 << 26;
    127         /** @var int $carry2 */
    128         $carry2 = ($h[2] + (1 << 25)) >> 26;
    129         $h[3] += $carry2;
    130         $h[2] -= $carry2 << 26;
    131         /** @var int $carry4 */
    132         $carry4 = ($h[4] + (1 << 25)) >> 26;
    133         $h[5] += $carry4;
    134         $h[4] -= $carry4 << 26;
    135         /** @var int $carry6 */
    136         $carry6 = ($h[6] + (1 << 25)) >> 26;
    137         $h[7] += $carry6;
    138         $h[6] -= $carry6 << 26;
    139         /** @var int $carry8 */
    140         $carry8 = ($h[8] + (1 << 25)) >> 26;
    141         $h[9] += $carry8;
    142         $h[8] -= $carry8 << 26;
    143 
    144         foreach ($h as $i => $value) {
    145             $h[$i] = (int) $value;
    146         }
    147         return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h);
     69        $h0 = self::mul($f->e0, 121666, 17);
     70        $h1 = self::mul($f->e1, 121666, 17);
     71        $h2 = self::mul($f->e2, 121666, 17);
     72        $h3 = self::mul($f->e3, 121666, 17);
     73        $h4 = self::mul($f->e4, 121666, 17);
     74        $h5 = self::mul($f->e5, 121666, 17);
     75        $h6 = self::mul($f->e6, 121666, 17);
     76        $h7 = self::mul($f->e7, 121666, 17);
     77        $h8 = self::mul($f->e8, 121666, 17);
     78        $h9 = self::mul($f->e9, 121666, 17);
     79
     80        $carry9 = ($h9 + (1 << 24)) >> 25;
     81        $h0 += self::mul($carry9, 19, 5);
     82        $h9 -= $carry9 << 25;
     83
     84        $carry1 = ($h1 + (1 << 24)) >> 25;
     85        $h2 += $carry1;
     86        $h1 -= $carry1 << 25;
     87
     88        $carry3 = ($h3 + (1 << 24)) >> 25;
     89        $h4 += $carry3;
     90        $h3 -= $carry3 << 25;
     91
     92        $carry5 = ($h5 + (1 << 24)) >> 25;
     93        $h6 += $carry5;
     94        $h5 -= $carry5 << 25;
     95
     96        $carry7 = ($h7 + (1 << 24)) >> 25;
     97        $h8 += $carry7;
     98        $h7 -= $carry7 << 25;
     99
     100
     101        $carry0 = ($h0 + (1 << 25)) >> 26;
     102        $h1 += $carry0;
     103        $h0 -= $carry0 << 26;
     104
     105        $carry2 = ($h2 + (1 << 25)) >> 26;
     106        $h3 += $carry2;
     107        $h2 -= $carry2 << 26;
     108
     109        $carry4 = ($h4 + (1 << 25)) >> 26;
     110        $h5 += $carry4;
     111        $h4 -= $carry4 << 26;
     112
     113        $carry6 = ($h6 + (1 << 25)) >> 26;
     114        $h7 += $carry6;
     115        $h6 -= $carry6 << 26;
     116
     117        $carry8 = ($h8 + (1 << 25)) >> 26;
     118        $h9 += $carry8;
     119        $h8 -= $carry8 << 26;
     120        return new ParagonIE_Sodium_Core_Curve25519_Fe($h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9);
    148121    }
    149122
  • trunk/src/wp-includes/sodium_compat/src/Core/XChaCha20.php

    r46858 r60905  
    2020     * @throws TypeError
    2121     */
    22     public static function stream($len = 64, $nonce = '', $key = '')
     22    public static function stream($len, $nonce, $key)
    2323    {
    2424        if (self::strlen($nonce) !== 24) {
     
    4747     * @throws TypeError
    4848     */
    49     public static function ietfStream($len = 64, $nonce = '', $key = '')
     49    public static function ietfStream($len, $nonce, $key)
    5050    {
    5151        if (self::strlen($nonce) !== 24) {
     
    7575     * @throws TypeError
    7676     */
    77     public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
     77    public static function streamXorIc($message, $nonce, $key, $ic = '')
    7878    {
    7979        if (self::strlen($nonce) !== 24) {
     
    101101     * @throws TypeError
    102102     */
    103     public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
     103    public static function ietfStreamXorIc($message, $nonce, $key, $ic = '')
    104104    {
    105105        if (self::strlen($nonce) !== 24) {
  • trunk/src/wp-includes/sodium_compat/src/Crypto.php

    r46858 r60905  
    11891189
    11901190        /** @var string $subkey */
    1191         $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key);
     1191        $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20(
     1192            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
     1193            $key
     1194        );
    11921195
    11931196        /** @var string $block0 */
     
    12451248
    12461249        # crypto_core_hchacha20(state->k, out, k, NULL);
    1247         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
     1250        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
     1251            ParagonIE_Sodium_Core_Util::substr($out, 0, 16),
     1252            $key
     1253        );
    12481254        $state = new ParagonIE_Sodium_Core_SecretStream_State(
    12491255            $subkey,
  • trunk/src/wp-includes/sodium_compat/src/File.php

    r58752 r60905  
    126126        }
    127127
     128        if (!file_exists($inputFile)) {
     129            throw new SodiumException('Input file does not exist');
     130        }
    128131        /** @var int $size */
    129132        $size = filesize($inputFile);
     
    139142
    140143        /** @var resource $ofp */
    141         $ofp = fopen($outputFile, 'wb');
     144        $ofp = @fopen($outputFile, 'wb');
    142145        if (!is_resource($ofp)) {
    143146            fclose($ifp);
     
    194197        }
    195198
     199        if (!file_exists($inputFile)) {
     200            throw new SodiumException('Input file does not exist');
     201        }
    196202        /** @var int $size */
    197203        $size = filesize($inputFile);
     
    207213
    208214        /** @var resource $ofp */
    209         $ofp = fopen($outputFile, 'wb');
     215        $ofp = @fopen($outputFile, 'wb');
    210216        if (!is_resource($ofp)) {
    211217            fclose($ifp);
     
    304310        $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
    305311
     312        if (!file_exists($inputFile)) {
     313            throw new SodiumException('Input file does not exist');
     314        }
    306315        /** @var int $size */
    307316        $size = filesize($inputFile);
     
    317326
    318327        /** @var resource $ofp */
    319         $ofp = fopen($outputFile, 'wb');
     328        $ofp = @fopen($outputFile, 'wb');
    320329        if (!is_resource($ofp)) {
    321330            fclose($ifp);
     
    409418        }
    410419
     420        if (!file_exists($filePath)) {
     421            throw new SodiumException('File does not exist');
     422        }
    411423        /** @var int $size */
    412424        $size = filesize($filePath);
     
    480492        }
    481493
     494        if (!file_exists($inputFile)) {
     495            throw new SodiumException('Input file does not exist');
     496        }
    482497        /** @var int $size */
    483498        $size = filesize($inputFile);
     
    487502
    488503        /** @var resource $ifp */
    489         $ifp = fopen($inputFile, 'rb');
     504        $ifp = @fopen($inputFile, 'rb');
    490505        if (!is_resource($ifp)) {
    491506            throw new SodiumException('Could not open input file for reading');
     
    544559        /* Input validation: */
    545560        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
    546             throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
     561            throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
    547562        }
    548563        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
    549             throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
    550         }
    551 
     564            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
     565        }
     566
     567        if (!file_exists($inputFile)) {
     568            throw new SodiumException('Input file does not exist');
     569        }
    552570        /** @var int $size */
    553571        $size = filesize($inputFile);
     
    563581
    564582        /** @var resource $ofp */
    565         $ofp = fopen($outputFile, 'wb');
     583        $ofp = @fopen($outputFile, 'wb');
    566584        if (!is_resource($ofp)) {
    567585            fclose($ifp);
     
    614632        }
    615633
     634        if (!file_exists($filePath)) {
     635            throw new SodiumException('File does not exist');
     636        }
    616637        /** @var int $size */
    617638        $size = filesize($filePath);
     
    745766        }
    746767
     768        if (!file_exists($filePath)) {
     769            throw new SodiumException('File does not exist');
     770        }
    747771        /** @var int $size */
    748772        $size = filesize($filePath);
Note: See TracChangeset for help on using the changeset viewer.