Make WordPress Core

Ticket #48371: 47381-update-sodium-compat-1-12-1.patch

File 47381-update-sodium-compat-1-12-1.patch, 140.3 KB (added by paragoninitiativeenterprises, 4 years ago)

Update to version 1.12.1

  • wp-includes/sodium_compat/src/Core/Poly1305/State.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    7979        $this->final = false;
    8080    }
    8181
     82    /**
     83     * Zero internal buffer upon destruction
     84     */
     85    public function __destruct()
     86    {
     87        $this->r[0] ^= $this->r[0];
     88        $this->r[1] ^= $this->r[1];
     89        $this->r[2] ^= $this->r[2];
     90        $this->r[3] ^= $this->r[3];
     91        $this->r[4] ^= $this->r[4];
     92        $this->h[0] ^= $this->h[0];
     93        $this->h[1] ^= $this->h[1];
     94        $this->h[2] ^= $this->h[2];
     95        $this->h[3] ^= $this->h[3];
     96        $this->h[4] ^= $this->h[4];
     97        $this->pad[0] ^= $this->pad[0];
     98        $this->pad[1] ^= $this->pad[1];
     99        $this->pad[2] ^= $this->pad[2];
     100        $this->pad[3] ^= $this->pad[3];
     101        $this->leftover = 0;
     102        $this->final = true;
     103    }
     104
    82105    /**
    83106     * @internal You should not use this directly from another application
    84107     *
     
    90113    public function update($message = '')
    91114    {
    92115        $bytes = self::strlen($message);
     116        if ($bytes < 1) {
     117            return $this;
     118        }
    93119
    94120        /* handle leftover */
    95121        if ($this->leftover) {
     
    111137            }
    112138
    113139            $this->blocks(
    114                 static::intArrayToString($this->buffer),
     140                self::intArrayToString($this->buffer),
    115141                ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
    116142            );
    117143            $this->leftover = 0;
     
    296322            $this->final = true;
    297323            $this->blocks(
    298324                self::substr(
    299                     static::intArrayToString($this->buffer),
     325                    self::intArrayToString($this->buffer),
    300326                    0,
    301327                    ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
    302328                ),
  • wp-includes/sodium_compat/src/Core/Base64/Original.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * Class ParagonIE_Sodium_Core_Base64
     5 *
     6 *  Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
     7 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
     8 */
     9class ParagonIE_Sodium_Core_Base64_Original
     10{
     11    // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE
     12    /**
     13     * Encode into Base64
     14     *
     15     * Base64 character set "[A-Z][a-z][0-9]+/"
     16     *
     17     * @param string $src
     18     * @return string
     19     * @throws TypeError
     20     */
     21    public static function encode($src)
     22    {
     23        return self::doEncode($src, true);
     24    }
     25
     26    /**
     27     * Encode into Base64, no = padding
     28     *
     29     * Base64 character set "[A-Z][a-z][0-9]+/"
     30     *
     31     * @param string $src
     32     * @return string
     33     * @throws TypeError
     34     */
     35    public static function encodeUnpadded($src)
     36    {
     37        return self::doEncode($src, false);
     38    }
     39
     40    /**
     41     * @param string $src
     42     * @param bool $pad   Include = padding?
     43     * @return string
     44     * @throws TypeError
     45     */
     46    protected static function doEncode($src, $pad = true)
     47    {
     48        $dest = '';
     49        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     50        // Main loop (no padding):
     51        for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
     52            /** @var array<int, int> $chunk */
     53            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
     54            $b0 = $chunk[1];
     55            $b1 = $chunk[2];
     56            $b2 = $chunk[3];
     57
     58            $dest .=
     59                self::encode6Bits(               $b0 >> 2       ) .
     60                self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     61                self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
     62                self::encode6Bits(  $b2                     & 63);
     63        }
     64        // The last chunk, which may have padding:
     65        if ($i < $srcLen) {
     66            /** @var array<int, int> $chunk */
     67            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     68            $b0 = $chunk[1];
     69            if ($i + 1 < $srcLen) {
     70                $b1 = $chunk[2];
     71                $dest .=
     72                    self::encode6Bits($b0 >> 2) .
     73                    self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     74                    self::encode6Bits(($b1 << 2) & 63);
     75                if ($pad) {
     76                    $dest .= '=';
     77                }
     78            } else {
     79                $dest .=
     80                    self::encode6Bits( $b0 >> 2) .
     81                    self::encode6Bits(($b0 << 4) & 63);
     82                if ($pad) {
     83                    $dest .= '==';
     84                }
     85            }
     86        }
     87        return $dest;
     88    }
     89
     90    /**
     91     * decode from base64 into binary
     92     *
     93     * Base64 character set "./[A-Z][a-z][0-9]"
     94     *
     95     * @param string $src
     96     * @param bool $strictPadding
     97     * @return string
     98     * @throws RangeException
     99     * @throws TypeError
     100     * @psalm-suppress RedundantCondition
     101     */
     102    public static function decode($src, $strictPadding = false)
     103    {
     104        // Remove padding
     105        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     106        if ($srcLen === 0) {
     107            return '';
     108        }
     109
     110        if ($strictPadding) {
     111            if (($srcLen & 3) === 0) {
     112                if ($src[$srcLen - 1] === '=') {
     113                    $srcLen--;
     114                    if ($src[$srcLen - 1] === '=') {
     115                        $srcLen--;
     116                    }
     117                }
     118            }
     119            if (($srcLen & 3) === 1) {
     120                throw new RangeException(
     121                    'Incorrect padding'
     122                );
     123            }
     124            if ($src[$srcLen - 1] === '=') {
     125                throw new RangeException(
     126                    'Incorrect padding'
     127                );
     128            }
     129        } else {
     130            $src = rtrim($src, '=');
     131            $srcLen =  ParagonIE_Sodium_Core_Util::strlen($src);
     132        }
     133
     134        $err = 0;
     135        $dest = '';
     136        // Main loop (no padding):
     137        for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
     138            /** @var array<int, int> $chunk */
     139            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
     140            $c0 = self::decode6Bits($chunk[1]);
     141            $c1 = self::decode6Bits($chunk[2]);
     142            $c2 = self::decode6Bits($chunk[3]);
     143            $c3 = self::decode6Bits($chunk[4]);
     144
     145            $dest .= pack(
     146                'CCC',
     147                ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     148                ((($c1 << 4) | ($c2 >> 2)) & 0xff),
     149                ((($c2 << 6) | $c3) & 0xff)
     150            );
     151            $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
     152        }
     153        // The last chunk, which may have padding:
     154        if ($i < $srcLen) {
     155            /** @var array<int, int> $chunk */
     156            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     157            $c0 = self::decode6Bits($chunk[1]);
     158
     159            if ($i + 2 < $srcLen) {
     160                $c1 = self::decode6Bits($chunk[2]);
     161                $c2 = self::decode6Bits($chunk[3]);
     162                $dest .= pack(
     163                    'CC',
     164                    ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     165                    ((($c1 << 4) | ($c2 >> 2)) & 0xff)
     166                );
     167                $err |= ($c0 | $c1 | $c2) >> 8;
     168            } elseif ($i + 1 < $srcLen) {
     169                $c1 = self::decode6Bits($chunk[2]);
     170                $dest .= pack(
     171                    'C',
     172                    ((($c0 << 2) | ($c1 >> 4)) & 0xff)
     173                );
     174                $err |= ($c0 | $c1) >> 8;
     175            } elseif ($i < $srcLen && $strictPadding) {
     176                $err |= 1;
     177            }
     178        }
     179        /** @var bool $check */
     180        $check = ($err === 0);
     181        if (!$check) {
     182            throw new RangeException(
     183                'Base64::decode() only expects characters in the correct base64 alphabet'
     184            );
     185        }
     186        return $dest;
     187    }
     188    // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
     189
     190    /**
     191     * Uses bitwise operators instead of table-lookups to turn 6-bit integers
     192     * into 8-bit integers.
     193     *
     194     * Base64 character set:
     195     * [A-Z]      [a-z]      [0-9]      +     /
     196     * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
     197     *
     198     * @param int $src
     199     * @return int
     200     */
     201    protected static function decode6Bits($src)
     202    {
     203        $ret = -1;
     204
     205        // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
     206        $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
     207
     208        // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
     209        $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
     210
     211        // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
     212        $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
     213
     214        // if ($src == 0x2b) $ret += 62 + 1;
     215        $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
     216
     217        // if ($src == 0x2f) ret += 63 + 1;
     218        $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
     219
     220        return $ret;
     221    }
     222
     223    /**
     224     * Uses bitwise operators instead of table-lookups to turn 8-bit integers
     225     * into 6-bit integers.
     226     *
     227     * @param int $src
     228     * @return string
     229     */
     230    protected static function encode6Bits($src)
     231    {
     232        $diff = 0x41;
     233
     234        // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
     235        $diff += ((25 - $src) >> 8) & 6;
     236
     237        // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
     238        $diff -= ((51 - $src) >> 8) & 75;
     239
     240        // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
     241        $diff -= ((61 - $src) >> 8) & 15;
     242
     243        // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
     244        $diff += ((62 - $src) >> 8) & 3;
     245
     246        return pack('C', $src + $diff);
     247    }
     248}
  • wp-includes/sodium_compat/LICENSE

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11/*
    22 * ISC License
    33 *
    4  * Copyright (c) 2016-2018
     4 * Copyright (c) 2016-2019
    55 * Paragon Initiative Enterprises <security at paragonie dot com>
    66 *
    7  * Copyright (c) 2013-2018
     7 * Copyright (c) 2013-2019
    88 * Frank Denis <j at pureftpd dot org>
    99 *
    1010 * Permission to use, copy, modify, and/or distribute this software for any
  • wp-includes/sodium_compat/src/Core/Base64/UrlSafe.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * Class ParagonIE_Sodium_Core_Base64UrlSafe
     5 *
     6 *  Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
     7 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
     8 */
     9class ParagonIE_Sodium_Core_Base64_UrlSafe
     10{
     11    // COPY ParagonIE_Sodium_Core_Base64_Common STARTING HERE
     12    /**
     13     * Encode into Base64
     14     *
     15     * Base64 character set "[A-Z][a-z][0-9]+/"
     16     *
     17     * @param string $src
     18     * @return string
     19     * @throws TypeError
     20     */
     21    public static function encode($src)
     22    {
     23        return self::doEncode($src, true);
     24    }
     25
     26    /**
     27     * Encode into Base64, no = padding
     28     *
     29     * Base64 character set "[A-Z][a-z][0-9]+/"
     30     *
     31     * @param string $src
     32     * @return string
     33     * @throws TypeError
     34     */
     35    public static function encodeUnpadded($src)
     36    {
     37        return self::doEncode($src, false);
     38    }
     39
     40    /**
     41     * @param string $src
     42     * @param bool $pad   Include = padding?
     43     * @return string
     44     * @throws TypeError
     45     */
     46    protected static function doEncode($src, $pad = true)
     47    {
     48        $dest = '';
     49        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     50        // Main loop (no padding):
     51        for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
     52            /** @var array<int, int> $chunk */
     53            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
     54            $b0 = $chunk[1];
     55            $b1 = $chunk[2];
     56            $b2 = $chunk[3];
     57
     58            $dest .=
     59                self::encode6Bits(               $b0 >> 2       ) .
     60                self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     61                self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
     62                self::encode6Bits(  $b2                     & 63);
     63        }
     64        // The last chunk, which may have padding:
     65        if ($i < $srcLen) {
     66            /** @var array<int, int> $chunk */
     67            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     68            $b0 = $chunk[1];
     69            if ($i + 1 < $srcLen) {
     70                $b1 = $chunk[2];
     71                $dest .=
     72                    self::encode6Bits($b0 >> 2) .
     73                    self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     74                    self::encode6Bits(($b1 << 2) & 63);
     75                if ($pad) {
     76                    $dest .= '=';
     77                }
     78            } else {
     79                $dest .=
     80                    self::encode6Bits( $b0 >> 2) .
     81                    self::encode6Bits(($b0 << 4) & 63);
     82                if ($pad) {
     83                    $dest .= '==';
     84                }
     85            }
     86        }
     87        return $dest;
     88    }
     89
     90    /**
     91     * decode from base64 into binary
     92     *
     93     * Base64 character set "./[A-Z][a-z][0-9]"
     94     *
     95     * @param string $src
     96     * @param bool $strictPadding
     97     * @return string
     98     * @throws RangeException
     99     * @throws TypeError
     100     * @psalm-suppress RedundantCondition
     101     */
     102    public static function decode($src, $strictPadding = false)
     103    {
     104        // Remove padding
     105        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     106        if ($srcLen === 0) {
     107            return '';
     108        }
     109
     110        if ($strictPadding) {
     111            if (($srcLen & 3) === 0) {
     112                if ($src[$srcLen - 1] === '=') {
     113                    $srcLen--;
     114                    if ($src[$srcLen - 1] === '=') {
     115                        $srcLen--;
     116                    }
     117                }
     118            }
     119            if (($srcLen & 3) === 1) {
     120                throw new RangeException(
     121                    'Incorrect padding'
     122                );
     123            }
     124            if ($src[$srcLen - 1] === '=') {
     125                throw new RangeException(
     126                    'Incorrect padding'
     127                );
     128            }
     129        } else {
     130            $src = rtrim($src, '=');
     131            $srcLen =  ParagonIE_Sodium_Core_Util::strlen($src);
     132        }
     133
     134        $err = 0;
     135        $dest = '';
     136        // Main loop (no padding):
     137        for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
     138            /** @var array<int, int> $chunk */
     139            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
     140            $c0 = self::decode6Bits($chunk[1]);
     141            $c1 = self::decode6Bits($chunk[2]);
     142            $c2 = self::decode6Bits($chunk[3]);
     143            $c3 = self::decode6Bits($chunk[4]);
     144
     145            $dest .= pack(
     146                'CCC',
     147                ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     148                ((($c1 << 4) | ($c2 >> 2)) & 0xff),
     149                ((($c2 << 6) | $c3) & 0xff)
     150            );
     151            $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
     152        }
     153        // The last chunk, which may have padding:
     154        if ($i < $srcLen) {
     155            /** @var array<int, int> $chunk */
     156            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     157            $c0 = self::decode6Bits($chunk[1]);
     158
     159            if ($i + 2 < $srcLen) {
     160                $c1 = self::decode6Bits($chunk[2]);
     161                $c2 = self::decode6Bits($chunk[3]);
     162                $dest .= pack(
     163                    'CC',
     164                    ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     165                    ((($c1 << 4) | ($c2 >> 2)) & 0xff)
     166                );
     167                $err |= ($c0 | $c1 | $c2) >> 8;
     168            } elseif ($i + 1 < $srcLen) {
     169                $c1 = self::decode6Bits($chunk[2]);
     170                $dest .= pack(
     171                    'C',
     172                    ((($c0 << 2) | ($c1 >> 4)) & 0xff)
     173                );
     174                $err |= ($c0 | $c1) >> 8;
     175            } elseif ($i < $srcLen && $strictPadding) {
     176                $err |= 1;
     177            }
     178        }
     179        /** @var bool $check */
     180        $check = ($err === 0);
     181        if (!$check) {
     182            throw new RangeException(
     183                'Base64::decode() only expects characters in the correct base64 alphabet'
     184            );
     185        }
     186        return $dest;
     187    }
     188    // COPY ParagonIE_Sodium_Core_Base64_Common ENDING HERE
     189    /**
     190     * Uses bitwise operators instead of table-lookups to turn 6-bit integers
     191     * into 8-bit integers.
     192     *
     193     * Base64 character set:
     194     * [A-Z]      [a-z]      [0-9]      +     /
     195     * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
     196     *
     197     * @param int $src
     198     * @return int
     199     */
     200    protected static function decode6Bits($src)
     201    {
     202        $ret = -1;
     203
     204        // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
     205        $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
     206
     207        // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
     208        $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
     209
     210        // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
     211        $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
     212
     213        // if ($src == 0x2c) $ret += 62 + 1;
     214        $ret += (((0x2c - $src) & ($src - 0x2e)) >> 8) & 63;
     215
     216        // if ($src == 0x5f) ret += 63 + 1;
     217        $ret += (((0x5e - $src) & ($src - 0x60)) >> 8) & 64;
     218
     219        return $ret;
     220    }
     221
     222    /**
     223     * Uses bitwise operators instead of table-lookups to turn 8-bit integers
     224     * into 6-bit integers.
     225     *
     226     * @param int $src
     227     * @return string
     228     */
     229    protected static function encode6Bits($src)
     230    {
     231        $diff = 0x41;
     232
     233        // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
     234        $diff += ((25 - $src) >> 8) & 6;
     235
     236        // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
     237        $diff -= ((51 - $src) >> 8) & 75;
     238
     239        // if ($src > 61) $diff += 0x2d - 0x30 - 10; // -13
     240        $diff -= ((61 - $src) >> 8) & 13;
     241
     242        // if ($src > 62) $diff += 0x5f - 0x2b - 1; // 3
     243        $diff += ((62 - $src) >> 8) & 49;
     244
     245        return pack('C', $src + $diff);
     246    }
     247}
  • wp-includes/sodium_compat/composer.json

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    5454    "paragonie/random_compat": ">=1"
    5555  },
    5656  "require-dev": {
    57     "phpunit/phpunit": "^3|^4|^5"
     57    "phpunit/phpunit": "^3|^4|^5|^6|^7"
    5858  },
    5959  "suggest": {
    6060    "ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.",
  • wp-includes/sodium_compat/src/Core/BLAKE2b.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    8888    {
    8989        $l = ($x[1] + $y[1]) & 0xffffffff;
    9090        return self::new64(
    91             $x[0] + $y[0] + (
     91            (int) ($x[0] + $y[0] + (
    9292                ($l < $x[1]) ? 1 : 0
    93             ),
    94             $l
     93            )),
     94            (int) $l
    9595        );
    9696    }
    9797
     
    132132            throw new SodiumException('y[1] is not an integer');
    133133        }
    134134        return self::new64(
    135             (int) ($x[0] ^ $y[0]),
    136             (int) ($x[1] ^ $y[1])
     135            (int) (($x[0] ^ $y[0]) & 0xffffffff),
     136            (int) (($x[1] ^ $y[1]) & 0xffffffff)
    137137        );
    138138    }
    139139
     
    299299     */
    300300    protected static function context()
    301301    {
    302         $ctx    = new SplFixedArray(5);
     302        $ctx    = new SplFixedArray(6);
    303303        $ctx[0] = new SplFixedArray(8);   // h
    304304        $ctx[1] = new SplFixedArray(2);   // t
    305305        $ctx[2] = new SplFixedArray(2);   // f
    306306        $ctx[3] = new SplFixedArray(256); // buf
    307307        $ctx[4] = 0;                      // buflen
     308        $ctx[5] = 0;                      // last_node (uint8_t)
    308309
    309310        for ($i = 8; $i--;) {
    310311            $ctx[0][$i] = self::$iv[$i];
     
    550551     *
    551552     * @param SplFixedArray|null $key
    552553     * @param int $outlen
     554     * @param SplFixedArray|null $salt
     555     * @param SplFixedArray|null $personal
    553556     * @return SplFixedArray
    554557     * @throws SodiumException
    555558     * @throws TypeError
     
    559562     * @psalm-suppress MixedArrayAssignment
    560563     * @psalm-suppress MixedArrayOffset
    561564     */
    562     public static function init($key = null, $outlen = 64)
    563     {
     565    public static function init(
     566        $key = null,
     567        $outlen = 64,
     568        $salt = null,
     569        $personal = null
     570    ) {
    564571        self::pseudoConstructor();
    565572        $klen = 0;
    566573
     
    578585        $ctx = self::context();
    579586
    580587        $p = new SplFixedArray(64);
     588        // Zero our param buffer...
    581589        for ($i = 64; --$i;) {
    582590            $p[$i] = 0;
    583591        }
     
    587595        $p[2] = 1;       // fanout
    588596        $p[3] = 1;       // depth
    589597
     598        if ($salt instanceof SplFixedArray) {
     599            // salt: [32] through [47]
     600            for ($i = 0; $i < 16; ++$i) {
     601                $p[32 + $i] = (int) $salt[$i];
     602            }
     603        }
     604        if ($personal instanceof SplFixedArray) {
     605            // personal: [48] through [63]
     606            for ($i = 0; $i < 16; ++$i) {
     607                $p[48 + $i] = (int) $personal[$i];
     608            }
     609        }
     610
    590611        $ctx[0][0] = self::xor64(
    591612            $ctx[0][0],
    592613            self::load64($p, 0)
    593614        );
     615        if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
     616            // We need to do what blake2b_init_param() does:
     617            for ($i = 1; $i < 8; ++$i) {
     618                $ctx[0][$i] = self::xor64(
     619                    $ctx[0][$i],
     620                    self::load64($p, $i << 3)
     621                );
     622            }
     623        }
    594624
    595625        if ($klen > 0 && $key instanceof SplFixedArray) {
    596626            $block = new SplFixedArray(128);
     
    601631                $block[$i] = $key[$i];
    602632            }
    603633            self::update($ctx, $block, 128);
     634            $ctx[4] = 128;
    604635        }
    605636
    606637        return $ctx;
     
    693724            self::intToChr(($ctx4 >> 56) & 0xff)
    694725        ));
    695726        # uint8_t last_node;
    696         return $str . "\x00";
     727        return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
    697728    }
    698729
    699730    /**
     
    746777        # uint8_t buf[2 * 128];
    747778        $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
    748779
    749 
    750780        # uint8_t buf[2 * 128];
    751781        $int = 0;
    752782        for ($i = 0; $i < 8; ++$i) {
  • wp-includes/sodium_compat/src/Core/SecretStream/State.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * Class ParagonIE_Sodium_Core_SecretStream_State
     5 */
     6class ParagonIE_Sodium_Core_SecretStream_State
     7{
     8    /** @var string $key */
     9    protected $key;
     10
     11    /** @var int $counter */
     12    protected $counter;
     13
     14    /** @var string $nonce */
     15    protected $nonce;
     16
     17    /** @var string $_pad */
     18    protected $_pad;
     19
     20    /**
     21     * ParagonIE_Sodium_Core_SecretStream_State constructor.
     22     * @param string $key
     23     * @param string|null $nonce
     24     */
     25    public function __construct($key, $nonce = null)
     26    {
     27        $this->key = $key;
     28        $this->counter = 1;
     29        if (is_null($nonce)) {
     30            $nonce = str_repeat("\0", 12);
     31        }
     32        $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);;
     33        $this->_pad = str_repeat("\0", 4);
     34    }
     35
     36    /**
     37     * @return self
     38     */
     39    public function counterReset()
     40    {
     41        $this->counter = 1;
     42        $this->_pad = str_repeat("\0", 4);
     43        return $this;
     44    }
     45
     46    /**
     47     * @return string
     48     */
     49    public function getKey()
     50    {
     51        return $this->key;
     52    }
     53
     54    /**
     55     * @return string
     56     */
     57    public function getCounter()
     58    {
     59        return ParagonIE_Sodium_Core_Util::store32_le($this->counter);
     60    }
     61
     62    /**
     63     * @return string
     64     */
     65    public function getNonce()
     66    {
     67        if (!is_string($this->nonce)) {
     68            $this->nonce = str_repeat("\0", 12);
     69        }
     70        if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) {
     71            $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT);
     72        }
     73        return $this->nonce;
     74    }
     75
     76    /**
     77     * @return string
     78     */
     79    public function getCombinedNonce()
     80    {
     81        return $this->getCounter() .
     82            ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8);
     83    }
     84
     85    /**
     86     * @return self
     87     */
     88    public function incrementCounter()
     89    {
     90        ++$this->counter;
     91        return $this;
     92    }
     93
     94    /**
     95     * @return bool
     96     */
     97    public function needsRekey()
     98    {
     99        return ($this->counter & 0xffff) === 0;
     100    }
     101
     102    /**
     103     * @param string $newKeyAndNonce
     104     * @return self
     105     */
     106    public function rekey($newKeyAndNonce)
     107    {
     108        $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32);
     109        $this->nonce = str_pad(
     110            ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32),
     111            12,
     112            "\0",
     113            STR_PAD_RIGHT
     114        );
     115        return $this;
     116    }
     117
     118    /**
     119     * @param string $str
     120     * @return self
     121     */
     122    public function xorNonce($str)
     123    {
     124        $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings(
     125            $this->getNonce(),
     126            str_pad(
     127                ParagonIE_Sodium_Core_Util::substr($str, 0, 8),
     128                12,
     129                "\0",
     130                STR_PAD_RIGHT
     131            )
     132        );
     133        return $this;
     134    }
     135
     136    /**
     137     * @param string $string
     138     * @return self
     139     */
     140    public static function fromString($string)
     141    {
     142        $state = new ParagonIE_Sodium_Core_SecretStream_State(
     143            ParagonIE_Sodium_Core_Util::substr($string, 0, 32)
     144        );
     145        $state->counter = ParagonIE_Sodium_Core_Util::load_4(
     146            ParagonIE_Sodium_Core_Util::substr($string, 32, 4)
     147        );
     148        $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12);
     149        $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8);
     150        return $state;
     151    }
     152
     153    /**
     154     * @return string
     155     */
     156    public function toString()
     157    {
     158        return $this->key .
     159            $this->getCounter() .
     160            $this->nonce .
     161            $this->_pad;
     162    }
     163}
  • wp-includes/sodium_compat/src/Core/Ed25519.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    276276        if (self::strlen($sig) < 64) {
    277277            throw new SodiumException('Signature is too short');
    278278        }
    279         if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
     279        if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
    280280            throw new SodiumException('S < L - Invalid signature');
    281281        }
    282282        if (self::small_order($sig)) {
  • wp-includes/sodium_compat/lib/constants.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11<?php
    22namespace Sodium;
    33
     4require_once dirname(dirname(__FILE__)) . '/autoload.php';
     5
    46use ParagonIE_Sodium_Compat;
    57
    68const CRYPTO_AEAD_AES256GCM_KEYBYTES = ParagonIE_Sodium_Compat::CRYPTO_AEAD_AES256GCM_KEYBYTES;
  • wp-includes/sodium_compat/src/PHP52/SplFixedArray.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3if (class_exists('SplFixedArray')) {
     4    return;
     5}
     6
     7/**
     8 * The SplFixedArray class provides the main functionalities of array. The
     9 * main differences between a SplFixedArray and a normal PHP array is that
     10 * the SplFixedArray is of fixed length and allows only integers within
     11 * the range as indexes. The advantage is that it allows a faster array
     12 * implementation.
     13 */
     14class SplFixedArray implements Iterator, ArrayAccess, Countable
     15{
     16    /** @var array<int, mixed> */
     17    private $internalArray = array();
     18
     19    /** @var int $size */
     20    private $size = 0;
     21
     22    /**
     23     * SplFixedArray constructor.
     24     * @param int $size
     25     */
     26    public function __construct($size = 0)
     27    {
     28        $this->size = $size;
     29        $this->internalArray = array();
     30    }
     31
     32    /**
     33     * @return int
     34     */
     35    public function count()
     36    {
     37        return count($this->internalArray);
     38    }
     39
     40    /**
     41     * @return array
     42     */
     43    public function toArray()
     44    {
     45        ksort($this->internalArray);
     46        return (array) $this->internalArray;
     47    }
     48
     49    /**
     50     * @param array $array
     51     * @param bool $save_indexes
     52     * @return SplFixedArray
     53     * @psalm-suppress MixedAssignment
     54     */
     55    public static function fromArray(array $array, $save_indexes = true)
     56    {
     57        $self = new SplFixedArray(count($array));
     58        if($save_indexes) {
     59            foreach($array as $key => $value) {
     60                $self[(int) $key] = $value;
     61            }
     62        } else {
     63            $i = 0;
     64            foreach (array_values($array) as $value) {
     65                $self[$i] = $value;
     66                $i++;
     67            }
     68        }
     69        return $self;
     70    }
     71
     72    /**
     73     * @return int
     74     */
     75    public function getSize()
     76    {
     77        return $this->size;
     78    }
     79
     80    /**
     81     * @param int $size
     82     * @return bool
     83     */
     84    public function setSize($size)
     85    {
     86        $this->size = $size;
     87        return true;
     88    }
     89
     90    /**
     91     * @param string|int $index
     92     * @return bool
     93     */
     94    public function offsetExists($index)
     95    {
     96        return array_key_exists((int) $index, $this->internalArray);
     97    }
     98
     99    /**
     100     * @param string|int $index
     101     * @return mixed
     102     */
     103    public function offsetGet($index)
     104    {
     105        return $this->internalArray[(int) $index];
     106    }
     107
     108    /**
     109     * @param string|int $index
     110     * @param mixed $newval
     111     * @psalm-suppress MixedAssignment
     112     */
     113    public function offsetSet($index, $newval)
     114    {
     115        $this->internalArray[(int) $index] = $newval;
     116    }
     117
     118    /**
     119     * @param string|int $index
     120     */
     121    public function offsetUnset($index)
     122    {
     123        unset($this->internalArray[(int) $index]);
     124    }
     125
     126    /**
     127     * Rewind iterator back to the start
     128     * @link https://php.net/manual/en/splfixedarray.rewind.php
     129     * @return void
     130     * @since 5.3.0
     131     */
     132    public function rewind()
     133    {
     134        reset($this->internalArray);
     135    }
     136
     137    /**
     138     * Return current array entry
     139     * @link https://php.net/manual/en/splfixedarray.current.php
     140     * @return mixed The current element value.
     141     * @since 5.3.0
     142     */
     143    public function current()
     144    {
     145        return current($this->internalArray);
     146    }
     147
     148    /**
     149     * Return current array index
     150     * @return int The current array index.
     151     */
     152    public function key()
     153    {
     154        return key($this->internalArray);
     155    }
     156
     157    /**
     158     * @return void
     159     */
     160    public function next()
     161    {
     162        next($this->internalArray);
     163    }
     164
     165    /**
     166     * Check whether the array contains more elements
     167     * @link https://php.net/manual/en/splfixedarray.valid.php
     168     * @return bool true if the array contains any more elements, false otherwise.
     169     */
     170    public function valid()
     171    {
     172        if (empty($this->internalArray)) {
     173            return false;
     174        }
     175        $result = next($this->internalArray) !== false;
     176        prev($this->internalArray);
     177        return $result;
     178    }
     179
     180    /**
     181     * Do nothing.
     182     */
     183    public function __wakeup()
     184    {
     185        // NOP
     186    }
     187}
     188 No newline at end of file
  • wp-includes/sodium_compat/src/Core/XChaCha20.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    3636        );
    3737    }
    3838
     39    /**
     40     * @internal You should not use this directly from another application
     41     *
     42     * @param int $len
     43     * @param string $nonce
     44     * @param string $key
     45     * @return string
     46     * @throws SodiumException
     47     * @throws TypeError
     48     */
     49    public static function ietfStream($len = 64, $nonce = '', $key = '')
     50    {
     51        if (self::strlen($nonce) !== 24) {
     52            throw new SodiumException('Nonce must be 24 bytes long');
     53        }
     54        return self::encryptBytes(
     55            new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
     56                self::hChaCha20(
     57                    self::substr($nonce, 0, 16),
     58                    $key
     59                ),
     60                "\x00\x00\x00\x00" . self::substr($nonce, 16, 8)
     61            ),
     62            str_repeat("\x00", $len)
     63        );
     64    }
     65
    3966    /**
    4067     * @internal You should not use this directly from another application
    4168     *
     
    6188            $message
    6289        );
    6390    }
     91
     92    /**
     93     * @internal You should not use this directly from another application
     94     *
     95     * @param string $message
     96     * @param string $nonce
     97     * @param string $key
     98     * @param string $ic
     99     * @return string
     100     * @throws SodiumException
     101     * @throws TypeError
     102     */
     103    public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
     104    {
     105        if (self::strlen($nonce) !== 24) {
     106            throw new SodiumException('Nonce must be 24 bytes long');
     107        }
     108        return self::encryptBytes(
     109            new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
     110                self::hChaCha20(self::substr($nonce, 0, 16), $key),
     111                "\x00\x00\x00\x00" . self::substr($nonce, 16, 8),
     112                $ic
     113            ),
     114            $message
     115        );
     116    }
    64117}
  • wp-includes/sodium_compat/lib/php72compat_const.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3const SODIUM_LIBRARY_MAJOR_VERSION = 9;
     4const SODIUM_LIBRARY_MINOR_VERSION = 1;
     5const SODIUM_LIBRARY_VERSION = '1.0.8';
     6
     7const SODIUM_BASE64_VARIANT_ORIGINAL = 1;
     8const SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
     9const SODIUM_BASE64_VARIANT_URLSAFE = 5;
     10const SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
     11const SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
     12const SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
     13const SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
     14const SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES = 16;
     15const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
     16const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
     17const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
     18const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
     19const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
     20const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
     21const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
     22const SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
     23const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
     24const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
     25const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
     26const SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
     27const SODIUM_CRYPTO_AUTH_BYTES = 32;
     28const SODIUM_CRYPTO_AUTH_KEYBYTES = 32;
     29const SODIUM_CRYPTO_BOX_SEALBYTES = 16;
     30const SODIUM_CRYPTO_BOX_SECRETKEYBYTES = 32;
     31const SODIUM_CRYPTO_BOX_PUBLICKEYBYTES = 32;
     32const SODIUM_CRYPTO_BOX_KEYPAIRBYTES = 64;
     33const SODIUM_CRYPTO_BOX_MACBYTES = 16;
     34const SODIUM_CRYPTO_BOX_NONCEBYTES = 24;
     35const SODIUM_CRYPTO_BOX_SEEDBYTES = 32;
     36const SODIUM_CRYPTO_KDF_BYTES_MIN = 16;
     37const SODIUM_CRYPTO_KDF_BYTES_MAX = 64;
     38const SODIUM_CRYPTO_KDF_CONTEXTBYTES = 8;
     39const SODIUM_CRYPTO_KDF_KEYBYTES = 32;
     40const SODIUM_CRYPTO_KX_BYTES = 32;
     41const SODIUM_CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
     42const SODIUM_CRYPTO_KX_SEEDBYTES = 32;
     43const SODIUM_CRYPTO_KX_KEYPAIRBYTES = 64;
     44const SODIUM_CRYPTO_KX_PUBLICKEYBYTES = 32;
     45const SODIUM_CRYPTO_KX_SECRETKEYBYTES = 32;
     46const SODIUM_CRYPTO_KX_SESSIONKEYBYTES = 32;
     47const SODIUM_CRYPTO_GENERICHASH_BYTES = 32;
     48const SODIUM_CRYPTO_GENERICHASH_BYTES_MIN = 16;
     49const SODIUM_CRYPTO_GENERICHASH_BYTES_MAX = 64;
     50const SODIUM_CRYPTO_GENERICHASH_KEYBYTES = 32;
     51const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
     52const SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
     53const SODIUM_CRYPTO_PWHASH_SALTBYTES = 16;
     54const SODIUM_CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
     55const SODIUM_CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
     56const SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
     57const SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
     58const SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4;
     59const SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728;
     60const SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6;
     61const SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912;
     62const SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8;
     63const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
     64const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
     65const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
     66const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
     67const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
     68const SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
     69const SODIUM_CRYPTO_SCALARMULT_BYTES = 32;
     70const SODIUM_CRYPTO_SCALARMULT_SCALARBYTES = 32;
     71const SODIUM_CRYPTO_SHORTHASH_BYTES = 8;
     72const SODIUM_CRYPTO_SHORTHASH_KEYBYTES = 16;
     73const SODIUM_CRYPTO_SECRETBOX_KEYBYTES = 32;
     74const SODIUM_CRYPTO_SECRETBOX_MACBYTES = 16;
     75const SODIUM_CRYPTO_SECRETBOX_NONCEBYTES = 24;
     76const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
     77const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
     78const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
     79const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
     80const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
     81const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
     82const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
     83const SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
     84const SODIUM_CRYPTO_SIGN_BYTES = 64;
     85const SODIUM_CRYPTO_SIGN_SEEDBYTES = 32;
     86const SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES = 32;
     87const SODIUM_CRYPTO_SIGN_SECRETKEYBYTES = 64;
     88const SODIUM_CRYPTO_SIGN_KEYPAIRBYTES = 96;
     89const SODIUM_CRYPTO_STREAM_KEYBYTES = 32;
     90const SODIUM_CRYPTO_STREAM_NONCEBYTES = 24;
  • wp-includes/sodium_compat/lib/sodium_compat.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11<?php
    22namespace Sodium;
    33
     4require_once dirname(dirname(__FILE__)) . '/autoload.php';
     5
    46use ParagonIE_Sodium_Compat;
    57
    68/**
  • wp-includes/sodium_compat/lib/php72compat.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11<?php
    22
     3require_once dirname(dirname(__FILE__)) . '/autoload.php';
     4
    35/**
    46 * This file will monkey patch the pure-PHP implementation in place of the
    57 * PECL functions and constants, but only if they do not already exist.
     
    810 * ParagonIE_Sodium_Compat method or class constant, respectively.
    911 */
    1012foreach (array(
     13    'BASE64_VARIANT_ORIGINAL',
     14    'BASE64_VARIANT_ORIGINAL_NO_PADDING',
     15    'BASE64_VARIANT_URLSAFE',
     16    'BASE64_VARIANT_URLSAFE_NO_PADDING',
    1117    'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES',
    1218    'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES',
    1319    'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES',
     
    2935    'CRYPTO_BOX_MACBYTES',
    3036    'CRYPTO_BOX_NONCEBYTES',
    3137    'CRYPTO_BOX_SEEDBYTES',
     38    'CRYPTO_KDF_BYTES_MIN',
     39    'CRYPTO_KDF_BYTES_MAX',
     40    'CRYPTO_KDF_CONTEXTBYTES',
     41    'CRYPTO_KDF_KEYBYTES',
    3242    'CRYPTO_KX_BYTES',
     43    'CRYPTO_KX_KEYPAIRBYTES',
     44    'CRYPTO_KX_PRIMITIVE',
    3345    'CRYPTO_KX_SEEDBYTES',
    3446    'CRYPTO_KX_PUBLICKEYBYTES',
    3547    'CRYPTO_KX_SECRETKEYBYTES',
     48    'CRYPTO_KX_SESSIONKEYBYTES',
    3649    'CRYPTO_GENERICHASH_BYTES',
    3750    'CRYPTO_GENERICHASH_BYTES_MIN',
    3851    'CRYPTO_GENERICHASH_BYTES_MAX',
     
    5669    'CRYPTO_SECRETBOX_KEYBYTES',
    5770    'CRYPTO_SECRETBOX_MACBYTES',
    5871    'CRYPTO_SECRETBOX_NONCEBYTES',
     72    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES',
     73    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES',
     74    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES',
     75    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH',
     76    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL',
     77    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY',
     78    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL',
     79    'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX',
    5980    'CRYPTO_SIGN_BYTES',
    6081    'CRYPTO_SIGN_SEEDBYTES',
    6182    'CRYPTO_SIGN_PUBLICKEYBYTES',
     
    6889    'VERSION_STRING'
    6990    ) as $constant
    7091) {
    71     if (!defined("SODIUM_$constant")) {
     92    if (!defined("SODIUM_$constant") && defined("ParagonIE_Sodium_Compat::$constant")) {
    7293        define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant"));
    7394    }
    7495}
    75 
     96if (!is_callable('sodium_add')) {
     97    /**
     98     * @see ParagonIE_Sodium_Compat::add()
     99     * @param string $val
     100     * @param string $addv
     101     * @return void
     102     * @throws SodiumException
     103     */
     104    function sodium_add(&$val, $addv)
     105    {
     106        ParagonIE_Sodium_Compat::add($val, $addv);
     107    }
     108}
     109if (!is_callable('sodium_base642bin')) {
     110    /**
     111     * @see ParagonIE_Sodium_Compat::bin2base64()
     112     * @param string $string
     113     * @param int $variant
     114     * @param string $ignore
     115     * @return string
     116     * @throws SodiumException
     117     * @throws TypeError
     118     */
     119    function sodium_base642bin($string, $variant, $ignore ='')
     120    {
     121        return ParagonIE_Sodium_Compat::base642bin($string, $variant, $ignore);
     122    }
     123}
     124if (!is_callable('sodium_bin2base64')) {
     125    /**
     126     * @see ParagonIE_Sodium_Compat::bin2base64()
     127     * @param string $string
     128     * @param int $variant
     129     * @return string
     130     * @throws SodiumException
     131     * @throws TypeError
     132     */
     133    function sodium_bin2base64($string, $variant)
     134    {
     135        return ParagonIE_Sodium_Compat::bin2base64($string, $variant);
     136    }
     137}
    76138if (!is_callable('sodium_bin2hex')) {
    77139    /**
    78140     * @see ParagonIE_Sodium_Compat::hex2bin()
     
    186248    /**
    187249     * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen()
    188250     * @return string
     251     * @throws Exception
    189252     */
    190253    function sodium_crypto_aead_chacha20poly1305_keygen()
    191254    {
     
    232295    /**
    233296     * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen()
    234297     * @return string
     298     * @throws Exception
    235299     */
    236300    function sodium_crypto_aead_chacha20poly1305_ietf_keygen()
    237301    {
     
    278342    /**
    279343     * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen()
    280344     * @return string
     345     * @throws Exception
    281346     */
    282347    function sodium_crypto_aead_xchacha20poly1305_ietf_keygen()
    283348    {
     
    302367    /**
    303368     * @see ParagonIE_Sodium_Compat::crypto_auth_keygen()
    304369     * @return string
     370     * @throws Exception
    305371     */
    306372    function sodium_crypto_auth_keygen()
    307373    {
     
    516582    /**
    517583     * @see ParagonIE_Sodium_Compat::crypto_generichash_keygen()
    518584     * @return string
     585     * @throws Exception
    519586     */
    520587    function sodium_crypto_generichash_keygen()
    521588    {
     
    536603        ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message);
    537604    }
    538605}
     606if (!is_callable('sodium_crypto_kdf_keygen')) {
     607    /**
     608     * @see ParagonIE_Sodium_Compat::crypto_kdf_keygen()
     609     * @return string
     610     * @throws Exception
     611     */
     612    function sodium_crypto_kdf_keygen()
     613    {
     614        return ParagonIE_Sodium_Compat::crypto_kdf_keygen();
     615    }
     616}
     617if (!is_callable('sodium_crypto_kdf_derive_from_key')) {
     618    /**
     619     * @see ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key()
     620     * @param int $subkey_len
     621     * @param int $subkey_id
     622     * @param string $context
     623     * @param string $key
     624     * @return string
     625     * @throws Exception
     626     */
     627    function sodium_crypto_kdf_derive_from_key($subkey_len, $subkey_id, $context, $key)
     628    {
     629        return ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key(
     630            $subkey_len,
     631            $subkey_id,
     632            $context,
     633            $key
     634        );
     635    }
     636}
    539637if (!is_callable('sodium_crypto_kx')) {
    540638    /**
    541639     * @see ParagonIE_Sodium_Compat::crypto_kx()
     
    557655        );
    558656    }
    559657}
     658if (!is_callable('sodium_crypto_kx_seed_keypair')) {
     659    /**
     660     * @param string $seed
     661     * @return string
     662     * @throws Exception
     663     */
     664    function sodium_crypto_kx_seed_keypair($seed)
     665    {
     666        return ParagonIE_Sodium_Compat::crypto_kx_seed_keypair($seed);
     667    }
     668}
     669if (!is_callable('sodium_crypto_kx_keypair')) {
     670    /**
     671     * @return string
     672     * @throws Exception
     673     */
     674    function sodium_crypto_kx_keypair()
     675    {
     676        return ParagonIE_Sodium_Compat::crypto_kx_keypair();
     677    }
     678}
     679if (!is_callable('sodium_crypto_kx_client_session_keys')) {
     680    /**
     681     * @param string $keypair
     682     * @param string $serverPublicKey
     683     * @return array{0: string, 1: string}
     684     * @throws SodiumException
     685     */
     686    function sodium_crypto_kx_client_session_keys($keypair, $serverPublicKey)
     687    {
     688        return ParagonIE_Sodium_Compat::crypto_kx_client_session_keys($keypair, $serverPublicKey);
     689    }
     690}
     691if (!is_callable('sodium_crypto_kx_server_session_keys')) {
     692    /**
     693     * @param string $keypair
     694     * @param string $clientPublicKey
     695     * @return array{0: string, 1: string}
     696     * @throws SodiumException
     697     */
     698    function sodium_crypto_kx_server_session_keys($keypair, $clientPublicKey)
     699    {
     700        return ParagonIE_Sodium_Compat::crypto_kx_server_session_keys($keypair, $clientPublicKey);
     701    }
     702}
     703if (!is_callable('sodium_crypto_kx_secretkey')) {
     704    /**
     705     * @param string $keypair
     706     * @return string
     707     * @throws Exception
     708     */
     709    function sodium_crypto_kx_secretkey($keypair)
     710    {
     711        return ParagonIE_Sodium_Compat::crypto_kx_secretkey($keypair);
     712    }
     713}
     714if (!is_callable('sodium_crypto_kx_publickey')) {
     715    /**
     716     * @param string $keypair
     717     * @return string
     718     * @throws Exception
     719     */
     720    function sodium_crypto_kx_publickey($keypair)
     721    {
     722        return ParagonIE_Sodium_Compat::crypto_kx_publickey($keypair);
     723    }
     724}
    560725if (!is_callable('sodium_crypto_pwhash')) {
    561726    /**
    562727     * @see ParagonIE_Sodium_Compat::crypto_pwhash()
     
    590755        return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
    591756    }
    592757}
     758if (!is_callable('sodium_crypto_pwhash_str_needs_rehash')) {
     759    /**
     760     * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash()
     761     * @param string $hash
     762     * @param int $opslimit
     763     * @param int $memlimit
     764     * @return bool
     765     *
     766     * @throws SodiumException
     767     */
     768    function sodium_crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
     769    {
     770        return ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit);
     771    }
     772}
    593773if (!is_callable('sodium_crypto_pwhash_str_verify')) {
    594774    /**
    595775     * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
     
    696876    /**
    697877     * @see ParagonIE_Sodium_Compat::crypto_secretbox_keygen()
    698878     * @return string
     879     * @throws Exception
    699880     */
    700881    function sodium_crypto_secretbox_keygen()
    701882    {
     
    721902        }
    722903    }
    723904}
     905if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_push')) {
     906    /**
     907     * @param string $key
     908     * @return array<int, string>
     909     * @throws SodiumException
     910     */
     911    function sodium_crypto_secretstream_xchacha20poly1305_init_push($key)
     912    {
     913        return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_push($key);
     914    }
     915}
     916if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_push')) {
     917    /**
     918     * @param string $state
     919     * @param string $msg
     920     * @param string $aad
     921     * @param int $tag
     922     * @return string
     923     * @throws SodiumException
     924     */
     925    function sodium_crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
     926    {
     927        return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_push($state, $msg, $aad, $tag);
     928    }
     929}
     930if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_pull')) {
     931    /**
     932     * @param string $header
     933     * @param string $key
     934     * @return string
     935     * @throws Exception
     936     */
     937    function sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
     938    {
     939        return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_pull($header, $key);
     940    }
     941}
     942if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_pull')) {
     943    /**
     944     * @param string $state
     945     * @param string $cipher
     946     * @param string $aad
     947     * @return bool|array{0: string, 1: int}
     948     * @throws SodiumException
     949     */
     950    function sodium_crypto_secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
     951    {
     952        return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_pull($state, $cipher, $aad);
     953    }
     954}
     955if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_rekey')) {
     956    /**
     957     * @param string $state
     958     * @return void
     959     * @throws SodiumException
     960     */
     961    function sodium_crypto_secretstream_xchacha20poly1305_rekey(&$state)
     962    {
     963        ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_rekey($state);
     964    }
     965}
     966if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_keygen')) {
     967    /**
     968     * @return string
     969     * @throws Exception
     970     */
     971    function sodium_crypto_secretstream_xchacha20poly1305_keygen()
     972    {
     973        return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_keygen();
     974    }
     975}
    724976if (!is_callable('sodium_crypto_shorthash')) {
    725977    /**
    726978     * @see ParagonIE_Sodium_Compat::crypto_shorthash()
     
    739991    /**
    740992     * @see ParagonIE_Sodium_Compat::crypto_shorthash_keygen()
    741993     * @return string
     994     * @throws Exception
    742995     */
    743996    function sodium_crypto_shorthash_keygen()
    744997    {
     
    7731026        return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk);
    7741027    }
    7751028}
     1029if (!is_callable('sodium_crypto_sign_keypair_from_secretkey_and_publickey')) {
     1030    /**
     1031     * @see ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey()
     1032     * @param string $sk
     1033     * @param string $pk
     1034     * @return string
     1035     * @throws SodiumException
     1036     * @throws TypeError
     1037     */
     1038    function sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
     1039    {
     1040        return ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
     1041    }
     1042}
    7761043if (!is_callable('sodium_crypto_sign_keypair')) {
    7771044    /**
    7781045     * @see ParagonIE_Sodium_Compat::crypto_sign_keypair()
     
    9151182    /**
    9161183     * @see ParagonIE_Sodium_Compat::crypto_stream_keygen()
    9171184     * @return string
     1185     * @throws Exception
    9181186     */
    9191187    function sodium_crypto_stream_keygen()
    9201188    {
     
    10191287        ParagonIE_Sodium_Compat::memzero($str);
    10201288    }
    10211289}
     1290if (!is_callable('sodium_pad')) {
     1291    /**
     1292     * @see ParagonIE_Sodium_Compat::pad()
     1293     * @param string $unpadded
     1294     * @param int $blockSize
     1295     * @return int
     1296     * @throws SodiumException
     1297     * @throws TypeError
     1298     */
     1299    function sodium_pad($unpadded, $blockSize)
     1300    {
     1301        return ParagonIE_Sodium_Compat::pad($unpadded, $blockSize, true);
     1302    }
     1303}
     1304if (!is_callable('sodium_unpad')) {
     1305    /**
     1306     * @see ParagonIE_Sodium_Compat::pad()
     1307     * @param string $padded
     1308     * @param int $blockSize
     1309     * @return int
     1310     * @throws SodiumException
     1311     * @throws TypeError
     1312     */
     1313    function sodium_unpad($padded, $blockSize)
     1314    {
     1315        return ParagonIE_Sodium_Compat::unpad($padded, $blockSize, true);
     1316    }
     1317}
    10221318if (!is_callable('sodium_randombytes_buf')) {
    10231319    /**
    10241320     * @see ParagonIE_Sodium_Compat::randombytes_buf()
     
    10491345    /**
    10501346     * @see ParagonIE_Sodium_Compat::randombytes_random16()
    10511347     * @return int
     1348     * @throws Exception
    10521349     */
    10531350    function sodium_randombytes_random16()
    10541351    {
  • wp-includes/sodium_compat/lib/namespaced.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    11<?php
    22
     3require_once dirname(dirname(__FILE__)) . '/autoload.php';
     4
    35if (PHP_VERSION_ID < 50300) {
    46    return;
    57}
     
    3638    // Replace the namespace prefix with the base directory, replace namespace
    3739    // separators with directory separators in the relative class name, append
    3840    // with .php
    39     $file = dirname(__DIR__) . '/namespaced/' . str_replace('\\', '/', $relative_class) . '.php';
     41    $file = dirname(dirname(__FILE__)) . '/namespaced/' . str_replace('\\', '/', $relative_class) . '.php';
    4042    // if the file exists, require it
    4143    if (file_exists($file)) {
    4244        require_once $file;
  • wp-includes/sodium_compat/src/Core32/Poly1305/State.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    142142            }
    143143
    144144            $this->blocks(
    145                 static::intArrayToString($this->buffer),
     145                self::intArrayToString($this->buffer),
    146146                ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
    147147            );
    148148            $this->leftover = 0;
     
    346346            $this->final = true;
    347347            $this->blocks(
    348348                self::substr(
    349                     static::intArrayToString($this->buffer),
     349                    self::intArrayToString($this->buffer),
    350350                    0,
    351351                    ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE
    352352                ),
  • wp-includes/sodium_compat/src/Core32/Ed25519.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    278278        if (self::strlen($sig) < 64) {
    279279            throw new SodiumException('Signature is too short');
    280280        }
    281         if (self::check_S_lt_L(self::substr($sig, 32, 32))) {
     281        if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
    282282            throw new SodiumException('S < L - Invalid signature');
    283283        }
    284284        if (self::small_order($sig)) {
  • wp-includes/sodium_compat/src/Core32/BLAKE2b.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    223223     */
    224224    protected static function context()
    225225    {
    226         $ctx    = new SplFixedArray(5);
     226        $ctx    = new SplFixedArray(6);
    227227        $ctx[0] = new SplFixedArray(8);   // h
    228228        $ctx[1] = new SplFixedArray(2);   // t
    229229        $ctx[2] = new SplFixedArray(2);   // f
    230230        $ctx[3] = new SplFixedArray(256); // buf
    231231        $ctx[4] = 0;                      // buflen
     232        $ctx[5] = 0;                      // last_node (uint8_t)
    232233
    233234        for ($i = 8; $i--;) {
    234235            $ctx[0][$i] = self::$iv[$i];
     
    482483     *
    483484     * @param SplFixedArray|null $key
    484485     * @param int $outlen
     486     * @param SplFixedArray|null $salt
     487     * @param SplFixedArray|null $personal
    485488     * @return SplFixedArray
    486489     * @throws SodiumException
    487490     * @throws TypeError
     
    491494     * @psalm-suppress MixedArrayAssignment
    492495     * @psalm-suppress MixedMethodCall
    493496     */
    494     public static function init($key = null, $outlen = 64)
    495     {
     497    public static function init(
     498        $key = null,
     499        $outlen = 64,
     500        $salt = null,
     501        $personal = null
     502    ) {
    496503        self::pseudoConstructor();
    497504        $klen = 0;
    498505
     
    510517        $ctx = self::context();
    511518
    512519        $p = new SplFixedArray(64);
     520        // Zero our param buffer...
    513521        for ($i = 64; --$i;) {
    514522            $p[$i] = 0;
    515523        }
     
    519527        $p[2] = 1;       // fanout
    520528        $p[3] = 1;       // depth
    521529
     530        if ($salt instanceof SplFixedArray) {
     531            // salt: [32] through [47]
     532            for ($i = 0; $i < 16; ++$i) {
     533                $p[32 + $i] = (int) $salt[$i];
     534            }
     535        }
     536        if ($personal instanceof SplFixedArray) {
     537            // personal: [48] through [63]
     538            for ($i = 0; $i < 16; ++$i) {
     539                $p[48 + $i] = (int) $personal[$i];
     540            }
     541        }
     542
    522543        $ctx[0][0] = self::xor64(
    523544            $ctx[0][0],
    524545            self::load64($p, 0)
    525546        );
    526547
     548        if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
     549            // We need to do what blake2b_init_param() does:
     550            for ($i = 1; $i < 8; ++$i) {
     551                $ctx[0][$i] = self::xor64(
     552                    $ctx[0][$i],
     553                    self::load64($p, $i << 3)
     554                );
     555            }
     556        }
     557
    527558        if ($klen > 0 && $key instanceof SplFixedArray) {
    528559            $block = new SplFixedArray(128);
    529560            for ($i = 128; $i--;) {
     
    533564                $block[$i] = $key[$i];
    534565            }
    535566            self::update($ctx, $block, 128);
     567            $ctx[4] = 128;
    536568        }
    537569
    538570        return $ctx;
     
    595627            }
    596628            /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */
    597629            $ctxAi = $ctxA[$i];
    598             $str .= $ctxAi->toString();
     630            $str .= $ctxAi->toReverseString();
    599631        }
    600632
    601633        # uint64_t t[2];
     
    608640            /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */
    609641            $ctxA2 = $ctxA[1];
    610642
    611             $str .= $ctxA1->toString();
    612             $str .= $ctxA2->toString();
     643            $str .= $ctxA1->toReverseString();
     644            $str .= $ctxA2->toReverseString();
    613645        }
    614646
    615647        # uint8_t buf[2 * 128];
     
    624656            self::intToChr(($ctx4 >> 8) & 0xff),
    625657            self::intToChr(($ctx4 >> 16) & 0xff),
    626658            self::intToChr(($ctx4 >> 24) & 0xff),
     659            "\x00\x00\x00\x00"
     660            /*
    627661            self::intToChr(($ctx4 >> 32) & 0xff),
    628662            self::intToChr(($ctx4 >> 40) & 0xff),
    629663            self::intToChr(($ctx4 >> 48) & 0xff),
    630664            self::intToChr(($ctx4 >> 56) & 0xff)
     665            */
    631666        ));
    632667        # uint8_t last_node;
    633         return $str . "\x00";
     668        return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
    634669    }
    635670
    636671    /**
     
    652687
    653688        # uint64_t h[8];
    654689        for ($i = 0; $i < 8; ++$i) {
    655             $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromString(
     690            $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString(
    656691                self::substr($string, (($i << 3) + 0), 8)
    657692            );
    658693        }
     
    660695        # uint64_t t[2];
    661696        # uint64_t f[2];
    662697        for ($i = 1; $i < 3; ++$i) {
    663             $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromString(
     698            $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString(
    664699                self::substr($string, 72 + (($i - 1) << 4), 8)
    665700            );
    666             $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromString(
     701            $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString(
    667702                self::substr($string, 64 + (($i - 1) << 4), 8)
    668703            );
    669704        }
     
    671706        # uint8_t buf[2 * 128];
    672707        $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
    673708
    674 
    675709        # uint8_t buf[2 * 128];
    676710        $int = 0;
    677711        for ($i = 0; $i < 8; ++$i) {
  • wp-includes/sodium_compat/src/Core32/X25519.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    151151        for ($i = 0; $i < 10; ++$i) {
    152152            $h[$i] = $h[$i]->toInt32();
    153153        }
    154         /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h */
    155         return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h);
     154        /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */
     155        $h2 = $h;
     156        return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2);
    156157    }
    157158
    158159    /**
  • wp-includes/sodium_compat/src/Core32/SecretStream/State.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * Class ParagonIE_Sodium_Core32_SecretStream_State
     5 */
     6class ParagonIE_Sodium_Core32_SecretStream_State
     7{
     8    /** @var string $key */
     9    protected $key;
     10
     11    /** @var int $counter */
     12    protected $counter;
     13
     14    /** @var string $nonce */
     15    protected $nonce;
     16
     17    /** @var string $_pad */
     18    protected $_pad;
     19
     20    /**
     21     * ParagonIE_Sodium_Core32_SecretStream_State constructor.
     22     * @param string $key
     23     * @param string|null $nonce
     24     */
     25    public function __construct($key, $nonce = null)
     26    {
     27        $this->key = $key;
     28        $this->counter = 1;
     29        if (is_null($nonce)) {
     30            $nonce = str_repeat("\0", 12);
     31        }
     32        $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);;
     33        $this->_pad = str_repeat("\0", 4);
     34    }
     35
     36    /**
     37     * @return self
     38     */
     39    public function counterReset()
     40    {
     41        $this->counter = 1;
     42        $this->_pad = str_repeat("\0", 4);
     43        return $this;
     44    }
     45
     46    /**
     47     * @return string
     48     */
     49    public function getKey()
     50    {
     51        return $this->key;
     52    }
     53
     54    /**
     55     * @return string
     56     */
     57    public function getCounter()
     58    {
     59        return ParagonIE_Sodium_Core32_Util::store32_le($this->counter);
     60    }
     61
     62    /**
     63     * @return string
     64     */
     65    public function getNonce()
     66    {
     67        if (!is_string($this->nonce)) {
     68            $this->nonce = str_repeat("\0", 12);
     69        }
     70        if (ParagonIE_Sodium_Core32_Util::strlen($this->nonce) !== 12) {
     71            $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT);
     72        }
     73        return $this->nonce;
     74    }
     75
     76    /**
     77     * @return string
     78     */
     79    public function getCombinedNonce()
     80    {
     81        return $this->getCounter() .
     82            ParagonIE_Sodium_Core32_Util::substr($this->getNonce(), 0, 8);
     83    }
     84
     85    /**
     86     * @return self
     87     */
     88    public function incrementCounter()
     89    {
     90        ++$this->counter;
     91        return $this;
     92    }
     93
     94    /**
     95     * @return bool
     96     */
     97    public function needsRekey()
     98    {
     99        return ($this->counter & 0xffff) === 0;
     100    }
     101
     102    /**
     103     * @param string $newKeyAndNonce
     104     * @return self
     105     */
     106    public function rekey($newKeyAndNonce)
     107    {
     108        $this->key = ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 0, 32);
     109        $this->nonce = str_pad(
     110            ParagonIE_Sodium_Core32_Util::substr($newKeyAndNonce, 32),
     111            12,
     112            "\0",
     113            STR_PAD_RIGHT
     114        );
     115        return $this;
     116    }
     117
     118    /**
     119     * @param string $str
     120     * @return self
     121     */
     122    public function xorNonce($str)
     123    {
     124        $this->nonce = ParagonIE_Sodium_Core32_Util::xorStrings(
     125            $this->getNonce(),
     126            str_pad(
     127                ParagonIE_Sodium_Core32_Util::substr($str, 0, 8),
     128                12,
     129                "\0",
     130                STR_PAD_RIGHT
     131            )
     132        );
     133        return $this;
     134    }
     135
     136    /**
     137     * @param string $string
     138     * @return self
     139     */
     140    public static function fromString($string)
     141    {
     142        $state = new ParagonIE_Sodium_Core32_SecretStream_State(
     143            ParagonIE_Sodium_Core32_Util::substr($string, 0, 32)
     144        );
     145        $state->counter = ParagonIE_Sodium_Core32_Util::load_4(
     146            ParagonIE_Sodium_Core32_Util::substr($string, 32, 4)
     147        );
     148        $state->nonce = ParagonIE_Sodium_Core32_Util::substr($string, 36, 12);
     149        $state->_pad = ParagonIE_Sodium_Core32_Util::substr($string, 48, 8);
     150        return $state;
     151    }
     152
     153    /**
     154     * @return string
     155     */
     156    public function toString()
     157    {
     158        return $this->key .
     159            $this->getCounter() .
     160            $this->nonce .
     161            $this->_pad;
     162    }
     163}
  • wp-includes/sodium_compat/src/Core/Base64/Common.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * Class ParagonIE_Sodium_Core_Base64
     5 *
     6 *  Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
     7 *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
     8 *
     9 * We have to copy/paste the contents into the variant files because PHP 5.2
     10 * doesn't support late static binding, and we have no better workaround
     11 * available that won't break PHP 7+. Therefore, we're forced to duplicate code.
     12 */
     13abstract class ParagonIE_Sodium_Core_Base64_Common
     14{
     15    /**
     16     * Encode into Base64
     17     *
     18     * Base64 character set "[A-Z][a-z][0-9]+/"
     19     *
     20     * @param string $src
     21     * @return string
     22     * @throws TypeError
     23     */
     24    public static function encode($src)
     25    {
     26        return self::doEncode($src, true);
     27    }
     28
     29    /**
     30     * Encode into Base64, no = padding
     31     *
     32     * Base64 character set "[A-Z][a-z][0-9]+/"
     33     *
     34     * @param string $src
     35     * @return string
     36     * @throws TypeError
     37     */
     38    public static function encodeUnpadded($src)
     39    {
     40        return self::doEncode($src, false);
     41    }
     42
     43    /**
     44     * @param string $src
     45     * @param bool $pad   Include = padding?
     46     * @return string
     47     * @throws TypeError
     48     */
     49    protected static function doEncode($src, $pad = true)
     50    {
     51        $dest = '';
     52        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     53        // Main loop (no padding):
     54        for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
     55            /** @var array<int, int> $chunk */
     56            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
     57            $b0 = $chunk[1];
     58            $b1 = $chunk[2];
     59            $b2 = $chunk[3];
     60
     61            $dest .=
     62                self::encode6Bits(               $b0 >> 2       ) .
     63                self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     64                self::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
     65                self::encode6Bits(  $b2                     & 63);
     66        }
     67        // The last chunk, which may have padding:
     68        if ($i < $srcLen) {
     69            /** @var array<int, int> $chunk */
     70            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     71            $b0 = $chunk[1];
     72            if ($i + 1 < $srcLen) {
     73                $b1 = $chunk[2];
     74                $dest .=
     75                    self::encode6Bits($b0 >> 2) .
     76                    self::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
     77                    self::encode6Bits(($b1 << 2) & 63);
     78                if ($pad) {
     79                    $dest .= '=';
     80                }
     81            } else {
     82                $dest .=
     83                    self::encode6Bits( $b0 >> 2) .
     84                    self::encode6Bits(($b0 << 4) & 63);
     85                if ($pad) {
     86                    $dest .= '==';
     87                }
     88            }
     89        }
     90        return $dest;
     91    }
     92
     93    /**
     94     * decode from base64 into binary
     95     *
     96     * Base64 character set "./[A-Z][a-z][0-9]"
     97     *
     98     * @param string $src
     99     * @param bool $strictPadding
     100     * @return string
     101     * @throws RangeException
     102     * @throws TypeError
     103     * @psalm-suppress RedundantCondition
     104     */
     105    public static function decode($src, $strictPadding = false)
     106    {
     107        // Remove padding
     108        $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     109        if ($srcLen === 0) {
     110            return '';
     111        }
     112
     113        if ($strictPadding) {
     114            if (($srcLen & 3) === 0) {
     115                if ($src[$srcLen - 1] === '=') {
     116                    $srcLen--;
     117                    if ($src[$srcLen - 1] === '=') {
     118                        $srcLen--;
     119                    }
     120                }
     121            }
     122            if (($srcLen & 3) === 1) {
     123                throw new RangeException(
     124                    'Incorrect padding'
     125                );
     126            }
     127            if ($src[$srcLen - 1] === '=') {
     128                throw new RangeException(
     129                    'Incorrect padding'
     130                );
     131            }
     132        } else {
     133            $src = rtrim($src, '=');
     134            $srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
     135        }
     136
     137        $err = 0;
     138        $dest = '';
     139        // Main loop (no padding):
     140        for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
     141            /** @var array<int, int> $chunk */
     142            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 4));
     143            $c0 = self::decode6Bits($chunk[1]);
     144            $c1 = self::decode6Bits($chunk[2]);
     145            $c2 = self::decode6Bits($chunk[3]);
     146            $c3 = self::decode6Bits($chunk[4]);
     147
     148            $dest .= pack(
     149                'CCC',
     150                ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     151                ((($c1 << 4) | ($c2 >> 2)) & 0xff),
     152                ((($c2 << 6) |  $c3      ) & 0xff)
     153            );
     154            $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
     155        }
     156        // The last chunk, which may have padding:
     157        if ($i < $srcLen) {
     158            /** @var array<int, int> $chunk */
     159            $chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
     160            $c0 = self::decode6Bits($chunk[1]);
     161
     162            if ($i + 2 < $srcLen) {
     163                $c1 = self::decode6Bits($chunk[2]);
     164                $c2 = self::decode6Bits($chunk[3]);
     165                $dest .= pack(
     166                    'CC',
     167                    ((($c0 << 2) | ($c1 >> 4)) & 0xff),
     168                    ((($c1 << 4) | ($c2 >> 2)) & 0xff)
     169                );
     170                $err |= ($c0 | $c1 | $c2) >> 8;
     171            } elseif ($i + 1 < $srcLen) {
     172                $c1 = self::decode6Bits($chunk[2]);
     173                $dest .= pack(
     174                    'C',
     175                    ((($c0 << 2) | ($c1 >> 4)) & 0xff)
     176                );
     177                $err |= ($c0 | $c1) >> 8;
     178            } elseif ($i < $srcLen && $strictPadding) {
     179                $err |= 1;
     180            }
     181        }
     182        /** @var bool $check */
     183        $check = ($err === 0);
     184        if (!$check) {
     185            throw new RangeException(
     186                'Base64::decode() only expects characters in the correct base64 alphabet'
     187            );
     188        }
     189        return $dest;
     190    }
     191
     192    /**
     193     * Uses bitwise operators instead of table-lookups to turn 6-bit integers
     194     * into 8-bit integers.
     195     *
     196     * Base64 character set:
     197     * [A-Z]      [a-z]      [0-9]      +     /
     198     * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
     199     *
     200     * @param int $src
     201     * @return int
     202     */
     203    abstract protected static function decode6Bits($src);
     204
     205    /**
     206     * Uses bitwise operators instead of table-lookups to turn 8-bit integers
     207     * into 6-bit integers.
     208     *
     209     * @param int $src
     210     * @return string
     211     */
     212    abstract protected static function encode6Bits($src);
     213}
  • wp-includes/sodium_compat/autoload.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    4343    // unless PHP >= 5.3.0
    4444    require_once dirname(__FILE__) . '/lib/namespaced.php';
    4545    require_once dirname(__FILE__) . '/lib/sodium_compat.php';
     46} else {
     47    require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php';
    4648}
    4749if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) {
    48     require_once dirname(__FILE__) . '/lib/php72compat.php';
     50    if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {
     51        require_once dirname(__FILE__) . '/lib/php72compat_const.php';
     52    }
     53    if (PHP_VERSION_ID >= 70000) {
     54        assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
     55    } else {
     56        assert(class_exists('ParagonIE_Sodium_Compat'));
     57    }
     58    require_once (dirname(__FILE__) . '/lib/php72compat.php');
    4959}
  • wp-includes/sodium_compat/src/Crypto32.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    776776        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
    777777    }
    778778
     779    /**
     780     * Initialize a hashing context for BLAKE2b.
     781     *
     782     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
     783     *
     784     * @param string $key
     785     * @param int $outputLength
     786     * @param string $salt
     787     * @param string $personal
     788     * @return string
     789     * @throws RangeException
     790     * @throws SodiumException
     791     * @throws TypeError
     792     */
     793    public static function generichash_init_salt_personal(
     794        $key = '',
     795        $outputLength = 32,
     796        $salt = '',
     797        $personal = ''
     798    ) {
     799        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
     800        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
     801
     802        $k = null;
     803        if (!empty($key)) {
     804            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
     805            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
     806                throw new RangeException('Invalid key size');
     807            }
     808        }
     809        if (!empty($salt)) {
     810            $s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($salt);
     811        } else {
     812            $s = null;
     813        }
     814        if (!empty($salt)) {
     815            $p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($personal);
     816        } else {
     817            $p = null;
     818        }
     819
     820        /** @var SplFixedArray $ctx */
     821        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength, $s, $p);
     822
     823        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
     824    }
     825
    779826    /**
    780827     * Update a hashing context for BLAKE2b with $message
    781828     *
     
    11841231        return $m;
    11851232    }
    11861233
     1234    /**
     1235     * @param string $key
     1236     * @return array<int, string> Returns a state and a header.
     1237     * @throws Exception
     1238     * @throws SodiumException
     1239     */
     1240    public static function secretstream_xchacha20poly1305_init_push($key)
     1241    {
     1242        # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
     1243        $out = random_bytes(24);
     1244
     1245        # crypto_core_hchacha20(state->k, out, k, NULL);
     1246        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20($out, $key);
     1247        $state = new ParagonIE_Sodium_Core32_SecretStream_State(
     1248            $subkey,
     1249            ParagonIE_Sodium_Core32_Util::substr($out, 16, 8) . str_repeat("\0", 4)
     1250        );
     1251
     1252        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
     1253        $state->counterReset();
     1254
     1255        # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
     1256        #        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1257        # memset(state->_pad, 0, sizeof state->_pad);
     1258        return array(
     1259            $state->toString(),
     1260            $out
     1261        );
     1262    }
     1263
     1264    /**
     1265     * @param string $key
     1266     * @param string $header
     1267     * @return string Returns a state.
     1268     * @throws Exception
     1269     */
     1270    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
     1271    {
     1272        # crypto_core_hchacha20(state->k, in, k, NULL);
     1273        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
     1274            ParagonIE_Sodium_Core32_Util::substr($header, 0, 16),
     1275            $key
     1276        );
     1277        $state = new ParagonIE_Sodium_Core32_SecretStream_State(
     1278            $subkey,
     1279            ParagonIE_Sodium_Core32_Util::substr($header, 16)
     1280        );
     1281        $state->counterReset();
     1282        # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
     1283        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1284        # memset(state->_pad, 0, sizeof state->_pad);
     1285        # return 0;
     1286        return $state->toString();
     1287    }
     1288
     1289    /**
     1290     * @param string $state
     1291     * @param string $msg
     1292     * @param string $aad
     1293     * @param int $tag
     1294     * @return string
     1295     * @throws SodiumException
     1296     */
     1297    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
     1298    {
     1299        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
     1300        # crypto_onetimeauth_poly1305_state poly1305_state;
     1301        # unsigned char                     block[64U];
     1302        # unsigned char                     slen[8U];
     1303        # unsigned char                    *c;
     1304        # unsigned char                    *mac;
     1305
     1306        $msglen = ParagonIE_Sodium_Core32_Util::strlen($msg);
     1307        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
     1308
     1309        if ((($msglen + 63) >> 6) > 0xfffffffe) {
     1310            throw new SodiumException(
     1311                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
     1312            );
     1313        }
     1314
     1315        # if (outlen_p != NULL) {
     1316        #     *outlen_p = 0U;
     1317        # }
     1318        # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
     1319        #     sodium_misuse();
     1320        # }
     1321
     1322        # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
     1323        # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
     1324        # sodium_memzero(block, sizeof block);
     1325        $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
     1326            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
     1327        );
     1328
     1329        # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
     1330        $auth->update($aad);
     1331
     1332        # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
     1333        #     (0x10 - adlen) & 0xf);
     1334        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
     1335
     1336        # memset(block, 0, sizeof block);
     1337        # block[0] = tag;
     1338        # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
     1339        #                                    state->nonce, 1U, state->k);
     1340        $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
     1341            ParagonIE_Sodium_Core32_Util::intToChr($tag) . str_repeat("\0", 63),
     1342            $st->getCombinedNonce(),
     1343            $st->getKey(),
     1344            ParagonIE_Sodium_Core32_Util::store64_le(1)
     1345        );
     1346
     1347        # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
     1348        $auth->update($block);
     1349
     1350        # out[0] = block[0];
     1351        $out = $block[0];
     1352        # c = out + (sizeof tag);
     1353        # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
     1354        $cipher = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
     1355            $msg,
     1356            $st->getCombinedNonce(),
     1357            $st->getKey(),
     1358            ParagonIE_Sodium_Core32_Util::store64_le(2)
     1359        );
     1360
     1361        # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
     1362        $auth->update($cipher);
     1363
     1364        $out .= $cipher;
     1365        unset($cipher);
     1366
     1367        # crypto_onetimeauth_poly1305_update
     1368        # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
     1369        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
     1370
     1371        # STORE64_LE(slen, (uint64_t) adlen);
     1372        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
     1373
     1374        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1375        $auth->update($slen);
     1376
     1377        # STORE64_LE(slen, (sizeof block) + mlen);
     1378        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
     1379
     1380        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1381        $auth->update($slen);
     1382
     1383        # mac = c + mlen;
     1384        # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
     1385        $mac = $auth->finish();
     1386        $out .= $mac;
     1387
     1388        # sodium_memzero(&poly1305_state, sizeof poly1305_state);
     1389        unset($auth);
     1390
     1391
     1392        # XOR_BUF(STATE_INONCE(state), mac,
     1393        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1394        $st->xorNonce($mac);
     1395
     1396        # sodium_increment(STATE_COUNTER(state),
     1397        #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
     1398        $st->incrementCounter();
     1399        // Overwrite by reference:
     1400        $state = $st->toString();
     1401
     1402        /** @var bool $rekey */
     1403        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
     1404        # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
     1405        #     sodium_is_zero(STATE_COUNTER(state),
     1406        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
     1407        #     crypto_secretstream_xchacha20poly1305_rekey(state);
     1408        # }
     1409        if ($rekey || $st->needsRekey()) {
     1410            // DO REKEY
     1411            self::secretstream_xchacha20poly1305_rekey($state);
     1412        }
     1413        # if (outlen_p != NULL) {
     1414        #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
     1415        # }
     1416        return $out;
     1417    }
     1418
     1419    /**
     1420     * @param string $state
     1421     * @param string $cipher
     1422     * @param string $aad
     1423     * @return bool|array{0: string, 1: int}
     1424     * @throws SodiumException
     1425     */
     1426    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
     1427    {
     1428        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
     1429
     1430        $cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher);
     1431        #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
     1432        $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
     1433        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
     1434
     1435        #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
     1436        #         sodium_misuse();
     1437        #     }
     1438        if ((($msglen + 63) >> 6) > 0xfffffffe) {
     1439            throw new SodiumException(
     1440                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
     1441            );
     1442        }
     1443
     1444        #     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
     1445        #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
     1446        #     sodium_memzero(block, sizeof block);
     1447        $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
     1448            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
     1449        );
     1450
     1451        #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
     1452        $auth->update($aad);
     1453
     1454        #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
     1455        #         (0x10 - adlen) & 0xf);
     1456        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
     1457
     1458
     1459        #     memset(block, 0, sizeof block);
     1460        #     block[0] = in[0];
     1461        #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
     1462        #                                        state->nonce, 1U, state->k);
     1463        $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
     1464            $cipher[0] . str_repeat("\0", 63),
     1465            $st->getCombinedNonce(),
     1466            $st->getKey(),
     1467            ParagonIE_Sodium_Core32_Util::store64_le(1)
     1468        );
     1469        #     tag = block[0];
     1470        #     block[0] = in[0];
     1471        #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
     1472        $tag = ParagonIE_Sodium_Core32_Util::chrToInt($block[0]);
     1473        $block[0] = $cipher[0];
     1474        $auth->update($block);
     1475
     1476
     1477        #     c = in + (sizeof tag);
     1478        #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
     1479        $auth->update(ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen));
     1480
     1481        #     crypto_onetimeauth_poly1305_update
     1482        #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
     1483        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
     1484
     1485        #     STORE64_LE(slen, (uint64_t) adlen);
     1486        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1487        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
     1488        $auth->update($slen);
     1489
     1490        #     STORE64_LE(slen, (sizeof block) + mlen);
     1491        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1492        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
     1493        $auth->update($slen);
     1494
     1495        #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
     1496        #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
     1497        $mac = $auth->finish();
     1498
     1499        #     stored_mac = c + mlen;
     1500        #     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
     1501        #     sodium_memzero(mac, sizeof mac);
     1502        #         return -1;
     1503        #     }
     1504
     1505        $stored = ParagonIE_Sodium_Core32_Util::substr($cipher, $msglen + 1, 16);
     1506        if (!ParagonIE_Sodium_Core32_Util::hashEquals($mac, $stored)) {
     1507            return false;
     1508        }
     1509
     1510        #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
     1511        $out = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
     1512            ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen),
     1513            $st->getCombinedNonce(),
     1514            $st->getKey(),
     1515            ParagonIE_Sodium_Core32_Util::store64_le(2)
     1516        );
     1517
     1518        #     XOR_BUF(STATE_INONCE(state), mac,
     1519        #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1520        $st->xorNonce($mac);
     1521
     1522        #     sodium_increment(STATE_COUNTER(state),
     1523        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
     1524        $st->incrementCounter();
     1525
     1526        #     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
     1527        #         sodium_is_zero(STATE_COUNTER(state),
     1528        #             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
     1529        #         crypto_secretstream_xchacha20poly1305_rekey(state);
     1530        #     }
     1531
     1532        // Overwrite by reference:
     1533        $state = $st->toString();
     1534
     1535        /** @var bool $rekey */
     1536        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
     1537        if ($rekey || $st->needsRekey()) {
     1538            // DO REKEY
     1539            self::secretstream_xchacha20poly1305_rekey($state);
     1540        }
     1541        return array($out, $tag);
     1542    }
     1543
     1544    /**
     1545     * @param string $state
     1546     * @return void
     1547     * @throws SodiumException
     1548     */
     1549    public static function secretstream_xchacha20poly1305_rekey(&$state)
     1550    {
     1551        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
     1552        # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
     1553        # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
     1554        # size_t        i;
     1555        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
     1556        #     new_key_and_inonce[i] = state->k[i];
     1557        # }
     1558        $new_key_and_inonce = $st->getKey();
     1559
     1560        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
     1561        #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
     1562        #         STATE_INONCE(state)[i];
     1563        # }
     1564        $new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR($st->getNonce(), 0, 8);
     1565
     1566        # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
     1567        #                                 sizeof new_key_and_inonce,
     1568        #                                 state->nonce, state->k);
     1569
     1570        $st->rekey(ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
     1571            $new_key_and_inonce,
     1572            $st->getCombinedNonce(),
     1573            $st->getKey(),
     1574            ParagonIE_Sodium_Core32_Util::store64_le(0)
     1575        ));
     1576
     1577        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
     1578        #     state->k[i] = new_key_and_inonce[i];
     1579        # }
     1580        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
     1581        #     STATE_INONCE(state)[i] =
     1582        #          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
     1583        # }
     1584        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
     1585        $st->counterReset();
     1586
     1587        $state = $st->toString();
     1588    }
     1589
    11871590    /**
    11881591     * Detached Ed25519 signature.
    11891592     *
  • wp-includes/sodium_compat/src/Crypto.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    777777        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
    778778    }
    779779
     780    /**
     781     * Initialize a hashing context for BLAKE2b.
     782     *
     783     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
     784     *
     785     * @param string $key
     786     * @param int $outputLength
     787     * @param string $salt
     788     * @param string $personal
     789     * @return string
     790     * @throws RangeException
     791     * @throws SodiumException
     792     * @throws TypeError
     793     */
     794    public static function generichash_init_salt_personal(
     795        $key = '',
     796        $outputLength = 32,
     797        $salt = '',
     798        $personal = ''
     799    ) {
     800        // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
     801        ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
     802
     803        $k = null;
     804        if (!empty($key)) {
     805            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
     806            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
     807                throw new RangeException('Invalid key size');
     808            }
     809        }
     810        if (!empty($salt)) {
     811            $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt);
     812        } else {
     813            $s = null;
     814        }
     815        if (!empty($salt)) {
     816            $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal);
     817        } else {
     818            $p = null;
     819        }
     820
     821        /** @var SplFixedArray $ctx */
     822        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p);
     823
     824        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
     825    }
     826
    780827    /**
    781828     * Update a hashing context for BLAKE2b with $message
    782829     *
     
    11851232        return $m;
    11861233    }
    11871234
     1235    /**
     1236     * @param string $key
     1237     * @return array<int, string> Returns a state and a header.
     1238     * @throws Exception
     1239     * @throws SodiumException
     1240     */
     1241    public static function secretstream_xchacha20poly1305_init_push($key)
     1242    {
     1243        # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
     1244        $out = random_bytes(24);
     1245
     1246        # crypto_core_hchacha20(state->k, out, k, NULL);
     1247        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
     1248        $state = new ParagonIE_Sodium_Core_SecretStream_State(
     1249            $subkey,
     1250            ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4)
     1251        );
     1252
     1253        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
     1254        $state->counterReset();
     1255
     1256        # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
     1257        #        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1258        # memset(state->_pad, 0, sizeof state->_pad);
     1259        return array(
     1260            $state->toString(),
     1261            $out
     1262        );
     1263    }
     1264
     1265    /**
     1266     * @param string $key
     1267     * @param string $header
     1268     * @return string Returns a state.
     1269     * @throws Exception
     1270     */
     1271    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
     1272    {
     1273        # crypto_core_hchacha20(state->k, in, k, NULL);
     1274        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
     1275            ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
     1276            $key
     1277        );
     1278        $state = new ParagonIE_Sodium_Core_SecretStream_State(
     1279            $subkey,
     1280            ParagonIE_Sodium_Core_Util::substr($header, 16)
     1281        );
     1282        $state->counterReset();
     1283        # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
     1284        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1285        # memset(state->_pad, 0, sizeof state->_pad);
     1286        # return 0;
     1287        return $state->toString();
     1288    }
     1289
     1290    /**
     1291     * @param string $state
     1292     * @param string $msg
     1293     * @param string $aad
     1294     * @param int $tag
     1295     * @return string
     1296     * @throws SodiumException
     1297     */
     1298    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
     1299    {
     1300        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
     1301        # crypto_onetimeauth_poly1305_state poly1305_state;
     1302        # unsigned char                     block[64U];
     1303        # unsigned char                     slen[8U];
     1304        # unsigned char                    *c;
     1305        # unsigned char                    *mac;
     1306
     1307        $msglen = ParagonIE_Sodium_Core_Util::strlen($msg);
     1308        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
     1309
     1310        if ((($msglen + 63) >> 6) > 0xfffffffe) {
     1311            throw new SodiumException(
     1312                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
     1313            );
     1314        }
     1315
     1316        # if (outlen_p != NULL) {
     1317        #     *outlen_p = 0U;
     1318        # }
     1319        # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
     1320        #     sodium_misuse();
     1321        # }
     1322
     1323        # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
     1324        # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
     1325        # sodium_memzero(block, sizeof block);
     1326        $auth = new ParagonIE_Sodium_Core_Poly1305_State(
     1327            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
     1328        );
     1329
     1330        # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
     1331        $auth->update($aad);
     1332
     1333        # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
     1334        #     (0x10 - adlen) & 0xf);
     1335        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
     1336
     1337        # memset(block, 0, sizeof block);
     1338        # block[0] = tag;
     1339        # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
     1340        #                                    state->nonce, 1U, state->k);
     1341        $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
     1342            ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63),
     1343            $st->getCombinedNonce(),
     1344            $st->getKey(),
     1345            ParagonIE_Sodium_Core_Util::store64_le(1)
     1346        );
     1347
     1348        # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
     1349        $auth->update($block);
     1350
     1351        # out[0] = block[0];
     1352        $out = $block[0];
     1353        # c = out + (sizeof tag);
     1354        # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
     1355        $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
     1356            $msg,
     1357            $st->getCombinedNonce(),
     1358            $st->getKey(),
     1359            ParagonIE_Sodium_Core_Util::store64_le(2)
     1360        );
     1361
     1362        # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
     1363        $auth->update($cipher);
     1364
     1365        $out .= $cipher;
     1366        unset($cipher);
     1367
     1368        # crypto_onetimeauth_poly1305_update
     1369        # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
     1370        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
     1371
     1372        # STORE64_LE(slen, (uint64_t) adlen);
     1373        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
     1374
     1375        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1376        $auth->update($slen);
     1377
     1378        # STORE64_LE(slen, (sizeof block) + mlen);
     1379        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
     1380
     1381        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1382        $auth->update($slen);
     1383
     1384        # mac = c + mlen;
     1385        # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
     1386        $mac = $auth->finish();
     1387        $out .= $mac;
     1388
     1389        # sodium_memzero(&poly1305_state, sizeof poly1305_state);
     1390        unset($auth);
     1391
     1392
     1393        # XOR_BUF(STATE_INONCE(state), mac,
     1394        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1395        $st->xorNonce($mac);
     1396
     1397        # sodium_increment(STATE_COUNTER(state),
     1398        #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
     1399        $st->incrementCounter();
     1400        // Overwrite by reference:
     1401        $state = $st->toString();
     1402
     1403        /** @var bool $rekey */
     1404        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
     1405        # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
     1406        #     sodium_is_zero(STATE_COUNTER(state),
     1407        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
     1408        #     crypto_secretstream_xchacha20poly1305_rekey(state);
     1409        # }
     1410        if ($rekey || $st->needsRekey()) {
     1411            // DO REKEY
     1412            self::secretstream_xchacha20poly1305_rekey($state);
     1413        }
     1414        # if (outlen_p != NULL) {
     1415        #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
     1416        # }
     1417        return $out;
     1418    }
     1419
     1420    /**
     1421     * @param string $state
     1422     * @param string $cipher
     1423     * @param string $aad
     1424     * @return bool|array{0: string, 1: int}
     1425     * @throws SodiumException
     1426     */
     1427    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
     1428    {
     1429        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
     1430
     1431        $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
     1432        #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
     1433        $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
     1434        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
     1435
     1436        #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
     1437        #         sodium_misuse();
     1438        #     }
     1439        if ((($msglen + 63) >> 6) > 0xfffffffe) {
     1440            throw new SodiumException(
     1441                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
     1442            );
     1443        }
     1444
     1445        #     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
     1446        #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
     1447        #     sodium_memzero(block, sizeof block);
     1448        $auth = new ParagonIE_Sodium_Core_Poly1305_State(
     1449            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
     1450        );
     1451
     1452        #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
     1453        $auth->update($aad);
     1454
     1455        #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
     1456        #         (0x10 - adlen) & 0xf);
     1457        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
     1458
     1459
     1460        #     memset(block, 0, sizeof block);
     1461        #     block[0] = in[0];
     1462        #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
     1463        #                                        state->nonce, 1U, state->k);
     1464        $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
     1465            $cipher[0] . str_repeat("\0", 63),
     1466            $st->getCombinedNonce(),
     1467            $st->getKey(),
     1468            ParagonIE_Sodium_Core_Util::store64_le(1)
     1469        );
     1470        #     tag = block[0];
     1471        #     block[0] = in[0];
     1472        #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
     1473        $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]);
     1474        $block[0] = $cipher[0];
     1475        $auth->update($block);
     1476
     1477
     1478        #     c = in + (sizeof tag);
     1479        #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
     1480        $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen));
     1481
     1482        #     crypto_onetimeauth_poly1305_update
     1483        #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
     1484        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
     1485
     1486        #     STORE64_LE(slen, (uint64_t) adlen);
     1487        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1488        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
     1489        $auth->update($slen);
     1490
     1491        #     STORE64_LE(slen, (sizeof block) + mlen);
     1492        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
     1493        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
     1494        $auth->update($slen);
     1495
     1496        #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
     1497        #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
     1498        $mac = $auth->finish();
     1499
     1500        #     stored_mac = c + mlen;
     1501        #     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
     1502        #     sodium_memzero(mac, sizeof mac);
     1503        #         return -1;
     1504        #     }
     1505
     1506        $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16);
     1507        if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) {
     1508            return false;
     1509        }
     1510
     1511        #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
     1512        $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
     1513            ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen),
     1514            $st->getCombinedNonce(),
     1515            $st->getKey(),
     1516            ParagonIE_Sodium_Core_Util::store64_le(2)
     1517        );
     1518
     1519        #     XOR_BUF(STATE_INONCE(state), mac,
     1520        #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
     1521        $st->xorNonce($mac);
     1522
     1523        #     sodium_increment(STATE_COUNTER(state),
     1524        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
     1525        $st->incrementCounter();
     1526
     1527        #     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
     1528        #         sodium_is_zero(STATE_COUNTER(state),
     1529        #             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
     1530        #         crypto_secretstream_xchacha20poly1305_rekey(state);
     1531        #     }
     1532
     1533        // Overwrite by reference:
     1534        $state = $st->toString();
     1535
     1536        /** @var bool $rekey */
     1537        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
     1538        if ($rekey || $st->needsRekey()) {
     1539            // DO REKEY
     1540            self::secretstream_xchacha20poly1305_rekey($state);
     1541        }
     1542        return array($out, $tag);
     1543    }
     1544
     1545    /**
     1546     * @param string $state
     1547     * @return void
     1548     * @throws SodiumException
     1549     */
     1550    public static function secretstream_xchacha20poly1305_rekey(&$state)
     1551    {
     1552        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
     1553        # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
     1554        # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
     1555        # size_t        i;
     1556        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
     1557        #     new_key_and_inonce[i] = state->k[i];
     1558        # }
     1559        $new_key_and_inonce = $st->getKey();
     1560
     1561        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
     1562        #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
     1563        #         STATE_INONCE(state)[i];
     1564        # }
     1565        $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8);
     1566
     1567        # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
     1568        #                                 sizeof new_key_and_inonce,
     1569        #                                 state->nonce, state->k);
     1570
     1571        $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
     1572            $new_key_and_inonce,
     1573            $st->getCombinedNonce(),
     1574            $st->getKey(),
     1575            ParagonIE_Sodium_Core_Util::store64_le(0)
     1576        ));
     1577
     1578        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
     1579        #     state->k[i] = new_key_and_inonce[i];
     1580        # }
     1581        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
     1582        #     STATE_INONCE(state)[i] =
     1583        #          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
     1584        # }
     1585        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
     1586        $st->counterReset();
     1587
     1588        $state = $st->toString();
     1589    }
     1590
    11881591    /**
    11891592     * Detached Ed25519 signature.
    11901593     *
  • wp-includes/sodium_compat/src/Compat.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    4949    const VERSION_STRING = 'polyfill-1.0.8';
    5050
    5151    // From libsodium
     52    const BASE64_VARIANT_ORIGINAL = 1;
     53    const BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
     54    const BASE64_VARIANT_URLSAFE = 5;
     55    const BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
    5256    const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
    5357    const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
    5458    const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
     
    7478    const CRYPTO_BOX_MACBYTES = 16;
    7579    const CRYPTO_BOX_NONCEBYTES = 24;
    7680    const CRYPTO_BOX_SEEDBYTES = 32;
     81    const CRYPTO_KDF_BYTES_MIN = 16;
     82    const CRYPTO_KDF_BYTES_MAX = 64;
     83    const CRYPTO_KDF_CONTEXTBYTES = 8;
     84    const CRYPTO_KDF_KEYBYTES = 32;
    7785    const CRYPTO_KX_BYTES = 32;
     86    const CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
    7887    const CRYPTO_KX_SEEDBYTES = 32;
     88    const CRYPTO_KX_KEYPAIRBYTES = 64;
    7989    const CRYPTO_KX_PUBLICKEYBYTES = 32;
    8090    const CRYPTO_KX_SECRETKEYBYTES = 32;
     91    const CRYPTO_KX_SESSIONKEYBYTES = 32;
    8192    const CRYPTO_GENERICHASH_BYTES = 32;
    8293    const CRYPTO_GENERICHASH_BYTES_MIN = 16;
    8394    const CRYPTO_GENERICHASH_BYTES_MAX = 64;
     
    8596    const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
    8697    const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
    8798    const CRYPTO_PWHASH_SALTBYTES = 16;
    88     const CRYPTO_PWHASH_STRPREFIX = '$argon2i$';
     99    const CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
    89100    const CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
    90101    const CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
    91102    const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
     
    107118    const CRYPTO_SECRETBOX_KEYBYTES = 32;
    108119    const CRYPTO_SECRETBOX_MACBYTES = 16;
    109120    const CRYPTO_SECRETBOX_NONCEBYTES = 24;
     121    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
     122    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
     123    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
     124    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
     125    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
     126    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
     127    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
     128    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
    110129    const CRYPTO_SIGN_BYTES = 64;
    111130    const CRYPTO_SIGN_SEEDBYTES = 32;
    112131    const CRYPTO_SIGN_PUBLICKEYBYTES = 32;
     
    115134    const CRYPTO_STREAM_KEYBYTES = 32;
    116135    const CRYPTO_STREAM_NONCEBYTES = 24;
    117136
     137    /**
     138     * Add two numbers (little-endian unsigned), storing the value in the first
     139     * parameter.
     140     *
     141     * This mutates $val.
     142     *
     143     * @param string $val
     144     * @param string $addv
     145     * @return void
     146     * @throws SodiumException
     147     */
     148    public static function add(&$val, $addv)
     149    {
     150        $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
     151        $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
     152        if ($val_len !== $addv_len) {
     153            throw new SodiumException('values must have the same length');
     154        }
     155        $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
     156        $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
     157
     158        $c = 0;
     159        for ($i = 0; $i < $val_len; $i++) {
     160            $c += ($A[$i] + $B[$i]);
     161            $A[$i] = ($c & 0xff);
     162            $c >>= 8;
     163        }
     164        $val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
     165    }
     166
     167    /**
     168     * @param string $encoded
     169     * @param int $variant
     170     * @param string $ignore
     171     * @return string
     172     * @throws SodiumException
     173     */
     174    public static function base642bin($encoded, $variant, $ignore = '')
     175    {
     176        /* Type checks: */
     177        ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
     178
     179        /** @var string $encoded */
     180        $encoded = (string) $encoded;
     181        if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
     182            return '';
     183        }
     184
     185        // Just strip before decoding
     186        if (!empty($ignore)) {
     187            $encoded = str_replace($ignore, '', $encoded);
     188        }
     189
     190        try {
     191            switch ($variant) {
     192                case self::BASE64_VARIANT_ORIGINAL:
     193                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
     194                case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
     195                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
     196                case self::BASE64_VARIANT_URLSAFE:
     197                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
     198                case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
     199                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
     200                default:
     201                    throw new SodiumException('invalid base64 variant identifier');
     202            }
     203        } catch (Exception $ex) {
     204            if ($ex instanceof SodiumException) {
     205                throw $ex;
     206            }
     207            throw new SodiumException('invalid base64 string');
     208        }
     209    }
     210
     211    /**
     212     * @param string $decoded
     213     * @param int $variant
     214     * @return string
     215     * @throws SodiumException
     216     */
     217    public static function bin2base64($decoded, $variant)
     218    {
     219        /* Type checks: */
     220        ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
     221        /** @var string $decoded */
     222        $decoded = (string) $decoded;
     223        if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) {
     224            return '';
     225        }
     226
     227        switch ($variant) {
     228            case self::BASE64_VARIANT_ORIGINAL:
     229                return ParagonIE_Sodium_Core_Base64_Original::encode($decoded);
     230            case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
     231                return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded);
     232            case self::BASE64_VARIANT_URLSAFE:
     233                return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded);
     234            case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
     235                return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded);
     236            default:
     237                throw new SodiumException('invalid base64 variant identifier');
     238        }
     239    }
     240
    118241    /**
    119242     * Cache-timing-safe implementation of bin2hex().
    120243     *
     
    13101433     * @throws TypeError
    13111434     * @psalm-suppress MixedArgument
    13121435     * @psalm-suppress ReferenceConstraintViolation
     1436     * @psalm-suppress ConflictingReferenceConstraint
    13131437     */
    13141438    public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES)
    13151439    {
     
    13241448            $func = '\\Sodium\\crypto_generichash_final';
    13251449            return (string) $func($ctx, $length);
    13261450        }
     1451        if ($length < 1) {
     1452            try {
     1453                self::memzero($ctx);
     1454            } catch (SodiumException $ex) {
     1455                unset($ctx);
     1456            }
     1457            return '';
     1458        }
    13271459        if (PHP_INT_SIZE === 4) {
    13281460            $result = ParagonIE_Sodium_Crypto32::generichash_final($ctx, $length);
    13291461        } else {
     
    13791511        return ParagonIE_Sodium_Crypto::generichash_init($key, $length);
    13801512    }
    13811513
     1514    /**
     1515     * Initialize a BLAKE2b hashing context, for use in a streaming interface.
     1516     *
     1517     * @param string|null $key If specified must be a string between 16 and 64 bytes
     1518     * @param int $length      The size of the desired hash output
     1519     * @param string $salt     Salt (up to 16 bytes)
     1520     * @param string $personal Personalization string (up to 16 bytes)
     1521     * @return string          A BLAKE2 hashing context, encoded as a string
     1522     *                         (To be 100% compatible with ext/libsodium)
     1523     * @throws SodiumException
     1524     * @throws TypeError
     1525     * @psalm-suppress MixedArgument
     1526     */
     1527    public static function crypto_generichash_init_salt_personal(
     1528        $key = '',
     1529        $length = self::CRYPTO_GENERICHASH_BYTES,
     1530        $salt = '',
     1531        $personal = ''
     1532    ) {
     1533        /* Type checks: */
     1534        if (is_null($key)) {
     1535            $key = '';
     1536        }
     1537        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
     1538        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
     1539        ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3);
     1540        ParagonIE_Sodium_Core_Util::declareScalarType($personal, 'string', 4);
     1541        $salt = str_pad($salt, 16, "\0", STR_PAD_RIGHT);
     1542        $personal = str_pad($personal, 16, "\0", STR_PAD_RIGHT);
     1543
     1544        /* Input validation: */
     1545        if (!empty($key)) {
     1546            /*
     1547            if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
     1548                throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
     1549            }
     1550            */
     1551            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
     1552                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
     1553            }
     1554        }
     1555        if (PHP_INT_SIZE === 4) {
     1556            return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal($key, $length, $salt, $personal);
     1557        }
     1558        return ParagonIE_Sodium_Crypto::generichash_init_salt_personal($key, $length, $salt, $personal);
     1559    }
     1560
    13821561    /**
    13831562     * Update a BLAKE2b hashing context with additional data.
    13841563     *
     
    14241603        return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES);
    14251604    }
    14261605
     1606    /**
     1607     * @param int $subkey_len
     1608     * @param int $subkey_id
     1609     * @param string $context
     1610     * @param string $key
     1611     * @return string
     1612     * @throws SodiumException
     1613     */
     1614    public static function crypto_kdf_derive_from_key(
     1615        $subkey_len,
     1616        $subkey_id,
     1617        $context,
     1618        $key
     1619    ) {
     1620        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1);
     1621        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2);
     1622        ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3);
     1623        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
     1624        $subkey_id = (int) $subkey_id;
     1625        $subkey_len = (int) $subkey_len;
     1626        $context = (string) $context;
     1627        $key = (string) $key;
     1628
     1629        if ($subkey_len < self::CRYPTO_KDF_BYTES_MIN) {
     1630            throw new SodiumException('subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN');
     1631        }
     1632        if ($subkey_len > self::CRYPTO_KDF_BYTES_MAX) {
     1633            throw new SodiumException('subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX');
     1634        }
     1635        if ($subkey_id < 0) {
     1636            throw new SodiumException('subkey_id cannot be negative');
     1637        }
     1638        if (ParagonIE_Sodium_Core_Util::strlen($context) !== self::CRYPTO_KDF_CONTEXTBYTES) {
     1639            throw new SodiumException('context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes');
     1640        }
     1641        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_KDF_KEYBYTES) {
     1642            throw new SodiumException('key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes');
     1643        }
     1644
     1645        $salt = ParagonIE_Sodium_Core_Util::store64_le($subkey_id);
     1646        $state = self::crypto_generichash_init_salt_personal(
     1647            $key,
     1648            $subkey_len,
     1649            $salt,
     1650            $context
     1651        );
     1652        return self::crypto_generichash_final($state, $subkey_len);
     1653    }
     1654
     1655    /**
     1656     * @return string
     1657     * @throws Exception
     1658     * @throws Error
     1659     */
     1660    public static function crypto_kdf_keygen()
     1661    {
     1662        return random_bytes(self::CRYPTO_KDF_KEYBYTES);
     1663    }
     1664
    14271665    /**
    14281666     * Perform a key exchange, between a designated client and a server.
    14291667     *
     
    15101748        );
    15111749    }
    15121750
     1751    /**
     1752     * @param string $seed
     1753     * @return string
     1754     * @throws SodiumException
     1755     */
     1756    public static function crypto_kx_seed_keypair($seed)
     1757    {
     1758        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
     1759
     1760        $seed = (string) $seed;
     1761
     1762        if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) {
     1763            throw new SodiumException('seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes');
     1764        }
     1765
     1766        $sk = self::crypto_generichash($seed, '', self::CRYPTO_KX_SECRETKEYBYTES);
     1767        $pk = self::crypto_scalarmult_base($sk);
     1768        return $sk . $pk;
     1769    }
     1770
     1771    /**
     1772     * @return string
     1773     * @throws Exception
     1774     */
     1775    public static function crypto_kx_keypair()
     1776    {
     1777        $sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES);
     1778        $pk = self::crypto_scalarmult_base($sk);
     1779        return $sk . $pk;
     1780    }
     1781
     1782    /**
     1783     * @param string $keypair
     1784     * @param string $serverPublicKey
     1785     * @return array{0: string, 1: string}
     1786     * @throws SodiumException
     1787     */
     1788    public static function crypto_kx_client_session_keys($keypair, $serverPublicKey)
     1789    {
     1790        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
     1791        ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2);
     1792
     1793        $keypair = (string) $keypair;
     1794        $serverPublicKey = (string) $serverPublicKey;
     1795
     1796        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
     1797            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
     1798        }
     1799        if (ParagonIE_Sodium_Core_Util::strlen($serverPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
     1800            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
     1801        }
     1802
     1803        $sk = self::crypto_kx_secretkey($keypair);
     1804        $pk = self::crypto_kx_publickey($keypair);
     1805        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
     1806        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $serverPublicKey));
     1807        self::crypto_generichash_update($h, $pk);
     1808        self::crypto_generichash_update($h, $serverPublicKey);
     1809        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
     1810        return array(
     1811            ParagonIE_Sodium_Core_Util::substr(
     1812                $sessionKeys,
     1813                0,
     1814                self::CRYPTO_KX_SESSIONKEYBYTES
     1815            ),
     1816            ParagonIE_Sodium_Core_Util::substr(
     1817                $sessionKeys,
     1818                self::CRYPTO_KX_SESSIONKEYBYTES,
     1819                self::CRYPTO_KX_SESSIONKEYBYTES
     1820            )
     1821        );
     1822    }
     1823
     1824    /**
     1825     * @param string $keypair
     1826     * @param string $clientPublicKey
     1827     * @return array{0: string, 1: string}
     1828     * @throws SodiumException
     1829     */
     1830    public static function crypto_kx_server_session_keys($keypair, $clientPublicKey)
     1831    {
     1832        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
     1833        ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2);
     1834
     1835        $keypair = (string) $keypair;
     1836        $clientPublicKey = (string) $clientPublicKey;
     1837
     1838        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
     1839            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
     1840        }
     1841        if (ParagonIE_Sodium_Core_Util::strlen($clientPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
     1842            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
     1843        }
     1844
     1845        $sk = self::crypto_kx_secretkey($keypair);
     1846        $pk = self::crypto_kx_publickey($keypair);
     1847        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
     1848        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $clientPublicKey));
     1849        self::crypto_generichash_update($h, $clientPublicKey);
     1850        self::crypto_generichash_update($h, $pk);
     1851        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
     1852        return array(
     1853            ParagonIE_Sodium_Core_Util::substr(
     1854                $sessionKeys,
     1855                self::CRYPTO_KX_SESSIONKEYBYTES,
     1856                self::CRYPTO_KX_SESSIONKEYBYTES
     1857            ),
     1858            ParagonIE_Sodium_Core_Util::substr(
     1859                $sessionKeys,
     1860                0,
     1861                self::CRYPTO_KX_SESSIONKEYBYTES
     1862            )
     1863        );
     1864    }
     1865
     1866    /**
     1867     * @param string $kp
     1868     * @return string
     1869     * @throws SodiumException
     1870     */
     1871    public static function crypto_kx_secretkey($kp)
     1872    {
     1873        return ParagonIE_Sodium_Core_Util::substr(
     1874            $kp,
     1875            0,
     1876            self::CRYPTO_KX_SECRETKEYBYTES
     1877        );
     1878    }
     1879
     1880    /**
     1881     * @param string $kp
     1882     * @return string
     1883     * @throws SodiumException
     1884     */
     1885    public static function crypto_kx_publickey($kp)
     1886    {
     1887        return ParagonIE_Sodium_Core_Util::substr(
     1888            $kp,
     1889            self::CRYPTO_KX_SECRETKEYBYTES,
     1890            self::CRYPTO_KX_PUBLICKEYBYTES
     1891        );
     1892    }
     1893
    15131894    /**
    15141895     * @param int $outlen
    15151896     * @param string $passwd
     
    15921973        );
    15931974    }
    15941975
     1976    /**
     1977     * Do we need to rehash this password?
     1978     *
     1979     * @param string $hash
     1980     * @param int $opslimit
     1981     * @param int $memlimit
     1982     * @return bool
     1983     * @throws SodiumException
     1984     */
     1985    public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
     1986    {
     1987        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1);
     1988        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
     1989        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
     1990
     1991        // Just grab the first 4 pieces.
     1992        $pieces = explode('$', (string) $hash);
     1993        $prefix = implode('$', array_slice($pieces, 0, 4));
     1994
     1995        // Rebuild the expected header.
     1996        /** @var int $ops */
     1997        $ops = (int) $opslimit;
     1998        /** @var int $mem */
     1999        $mem = (int) $memlimit >> 10;
     2000        $encoded = self::CRYPTO_PWHASH_STRPREFIX . 'v=19$m=' . $mem . ',t=' . $ops . ',p=1';
     2001
     2002        // Do they match? If so, we don't need to rehash, so return false.
     2003        return !ParagonIE_Sodium_Core_Util::hashEquals($encoded, $prefix);
     2004    }
     2005
    15952006    /**
    15962007     * @param string $passwd
    15972008     * @param string $hash
     
    19872398        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
    19882399    }
    19892400
     2401    /**
     2402     * @param string $key
     2403     * @return array<int, string> Returns a state and a header.
     2404     * @throws Exception
     2405     * @throws SodiumException
     2406     */
     2407    public static function crypto_secretstream_xchacha20poly1305_init_push($key)
     2408    {
     2409        if (PHP_INT_SIZE === 4) {
     2410            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key);
     2411        }
     2412        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key);
     2413    }
     2414
     2415    /**
     2416     * @param string $header
     2417     * @param string $key
     2418     * @return string Returns a state.
     2419     * @throws Exception
     2420     */
     2421    public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
     2422    {
     2423        if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) {
     2424            throw new SodiumException(
     2425                'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes'
     2426            );
     2427        }
     2428        if (PHP_INT_SIZE === 4) {
     2429            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull($key, $header);
     2430        }
     2431        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull($key, $header);
     2432    }
     2433
     2434    /**
     2435     * @param string $state
     2436     * @param string $msg
     2437     * @param string $aad
     2438     * @param int $tag
     2439     * @return string
     2440     * @throws SodiumException
     2441     */
     2442    public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
     2443    {
     2444        if (PHP_INT_SIZE === 4) {
     2445            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push(
     2446                $state,
     2447                $msg,
     2448                $aad,
     2449                $tag
     2450            );
     2451        }
     2452        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_push(
     2453            $state,
     2454            $msg,
     2455            $aad,
     2456            $tag
     2457        );
     2458    }
     2459
     2460    /**
     2461     * @param string $state
     2462     * @param string $msg
     2463     * @param string $aad
     2464     * @return bool|array{0: string, 1: int}
     2465     * @throws SodiumException
     2466     */
     2467    public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '')
     2468    {
     2469        if (PHP_INT_SIZE === 4) {
     2470            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull(
     2471                $state,
     2472                $msg,
     2473                $aad
     2474            );
     2475        }
     2476        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_pull(
     2477            $state,
     2478            $msg,
     2479            $aad
     2480        );
     2481    }
     2482
     2483    /**
     2484     * @return string
     2485     * @throws Exception
     2486     */
     2487    public static function crypto_secretstream_xchacha20poly1305_keygen()
     2488    {
     2489        return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES);
     2490    }
     2491
     2492    /**
     2493     * @param string $state
     2494     * @return void
     2495     * @throws SodiumException
     2496     */
     2497    public static function crypto_secretstream_xchacha20poly1305_rekey(&$state)
     2498    {
     2499        if (PHP_INT_SIZE === 4) {
     2500            ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state);
     2501        } else {
     2502            ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state);
     2503        }
     2504    }
     2505
    19902506    /**
    19912507     * Calculates a SipHash-2-4 hash of a message for a given key.
    19922508     *
     
    21362652        return ParagonIE_Sodium_Core_Ed25519::keypair();
    21372653    }
    21382654
     2655    /**
     2656     * @param string $sk
     2657     * @param string $pk
     2658     * @return string
     2659     * @throws SodiumException
     2660     */
     2661    public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
     2662    {
     2663        ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
     2664        ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
     2665        $sk = (string) $sk;
     2666        $pk = (string) $pk;
     2667
     2668        if (ParagonIE_Sodium_Core_Util::strlen($sk) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
     2669            throw new SodiumException('secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes');
     2670        }
     2671        if (ParagonIE_Sodium_Core_Util::strlen($pk) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
     2672            throw new SodiumException('publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes');
     2673        }
     2674
     2675        if (self::useNewSodiumAPI()) {
     2676            return sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
     2677        }
     2678        return $sk . $pk;
     2679    }
     2680
    21392681    /**
    21402682     * Generate an Ed25519 keypair from a seed.
    21412683     *
     
    26243166        ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
    26253167        ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
    26263168
     3169        if (self::useNewSodiumAPI()) {
     3170            return sodium_memcmp($left, $right);
     3171        }
    26273172        if (self::use_fallback('memcmp')) {
    26283173            return (int) call_user_func('\\Sodium\\memcmp', $left, $right);
    26293174        }
     
    26683213        );
    26693214    }
    26703215
     3216    /**
     3217     * @param string $unpadded
     3218     * @param int $blockSize
     3219     * @param bool $dontFallback
     3220     * @return string
     3221     * @throws SodiumException
     3222     */
     3223    public static function pad($unpadded, $blockSize, $dontFallback = false)
     3224    {
     3225        /* Type checks: */
     3226        ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1);
     3227        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
     3228
     3229        $unpadded = (string) $unpadded;
     3230        $blockSize = (int) $blockSize;
     3231
     3232        if (self::useNewSodiumAPI() && !$dontFallback) {
     3233            return (string) sodium_pad($unpadded, $blockSize);
     3234        }
     3235
     3236        if ($blockSize <= 0) {
     3237            throw new SodiumException(
     3238                'block size cannot be less than 1'
     3239            );
     3240        }
     3241        $unpadded_len = ParagonIE_Sodium_Core_Util::strlen($unpadded);
     3242        $xpadlen = ($blockSize - 1);
     3243        if (($blockSize & ($blockSize - 1)) === 0) {
     3244            $xpadlen -= $unpadded_len & ($blockSize - 1);
     3245        } else {
     3246            $xpadlen -= $unpadded_len % $blockSize;
     3247        }
     3248
     3249        $xpadded_len = $unpadded_len + $xpadlen;
     3250        $padded = str_repeat("\0", $xpadded_len - 1);
     3251        if ($unpadded_len > 0) {
     3252            $st = 1;
     3253            $i = 0;
     3254            $k = $unpadded_len;
     3255            for ($j = 0; $j <= $xpadded_len; ++$j) {
     3256                $i = (int) $i;
     3257                $k = (int) $k;
     3258                $st = (int) $st;
     3259                if ($j >= $unpadded_len) {
     3260                    $padded[$j] = "\0";
     3261                } else {
     3262                    $padded[$j] = $unpadded[$j];
     3263                }
     3264                /** @var int $k */
     3265                $k -= $st;
     3266                $st = (int) (~(
     3267                            (
     3268                                (
     3269                                    ($k >> 48)
     3270                                        |
     3271                                    ($k >> 32)
     3272                                        |
     3273                                    ($k >> 16)
     3274                                        |
     3275                                    $k
     3276                                ) - 1
     3277                            ) >> 16
     3278                        )
     3279                    ) & 1;
     3280                $i += $st;
     3281            }
     3282        }
     3283
     3284        $mask = 0;
     3285        $tail = $xpadded_len;
     3286        for ($i = 0; $i < $blockSize; ++$i) {
     3287            # barrier_mask = (unsigned char)
     3288            #     (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
     3289            $barrier_mask = (($i ^ $xpadlen) -1) >> ((PHP_INT_SIZE << 3) - 1);
     3290            # tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
     3291            $padded[$tail - $i] = ParagonIE_Sodium_Core_Util::intToChr(
     3292                (ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]) & $mask)
     3293                    |
     3294                (0x80 & $barrier_mask)
     3295            );
     3296            # mask |= barrier_mask;
     3297            $mask |= $barrier_mask;
     3298        }
     3299        return $padded;
     3300    }
     3301
     3302    /**
     3303     * @param string $padded
     3304     * @param int $blockSize
     3305     * @param bool $dontFallback
     3306     * @return string
     3307     * @throws SodiumException
     3308     */
     3309    public static function unpad($padded, $blockSize, $dontFallback = false)
     3310    {
     3311        /* Type checks: */
     3312        ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1);
     3313        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
     3314
     3315        $padded = (string) $padded;
     3316        $blockSize = (int) $blockSize;
     3317
     3318        if (self::useNewSodiumAPI() && !$dontFallback) {
     3319            return (string) sodium_unpad($padded, $blockSize);
     3320        }
     3321        if ($blockSize <= 0) {
     3322            throw new SodiumException('block size cannot be less than 1');
     3323        }
     3324        $padded_len = ParagonIE_Sodium_Core_Util::strlen($padded);
     3325        if ($padded_len < $blockSize) {
     3326            throw new SodiumException('invalid padding');
     3327        }
     3328
     3329        # tail = &padded[padded_len - 1U];
     3330        $tail = $padded_len - 1;
     3331
     3332        $acc = 0;
     3333        $valid = 0;
     3334        $pad_len = 0;
     3335
     3336        $found = 0;
     3337        for ($i = 0; $i < $blockSize; ++$i) {
     3338            # c = tail[-i];
     3339            $c = ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]);
     3340
     3341            # is_barrier =
     3342            #     (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U;
     3343            $is_barrier = (
     3344                (
     3345                    ($acc - 1) & ($pad_len - 1) & (($c ^ 80) - 1)
     3346                ) >> 7
     3347            ) & 1;
     3348            $is_barrier &= ~$found;
     3349            $found |= $is_barrier;
     3350
     3351            # acc |= c;
     3352            $acc |= $c;
     3353
     3354            # pad_len |= i & (1U + ~is_barrier);
     3355            $pad_len |= $i & (1 + ~$is_barrier);
     3356
     3357            # valid |= (unsigned char) is_barrier;
     3358            $valid |= ($is_barrier & 0xff);
     3359        }
     3360        # unpadded_len = padded_len - 1U - pad_len;
     3361        $unpadded_len = $padded_len - 1 - $pad_len;
     3362        if ($valid !== 1) {
     3363            throw new SodiumException('invalid padding');
     3364        }
     3365        return ParagonIE_Sodium_Core_Util::substr($padded, 0, $unpadded_len);
     3366    }
     3367
    26713368    /**
    26723369     * Will sodium_compat run fast on the current hardware and PHP configuration?
    26733370     *
  • wp-includes/sodium_compat/src/File.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    679679        }
    680680
    681681        /* Security checks */
    682         if (ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
     682        if (
     683            (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240)
     684                &&
     685            ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))
     686        ) {
    683687            throw new SodiumException('S < L - Invalid signature');
    684688        }
    685689        if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) {
     
    841845        if (!is_string($plaintext)) {
    842846            throw new SodiumException('Could not read input file');
    843847        }
    844         $first32 = ftell($ifp);
     848        $first32 = self::ftell($ifp);
    845849
    846850        /** @var string $subkey */
    847851        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
     
    875879        );
    876880
    877881        // Pre-write 16 blank bytes for the Poly1305 tag
    878         $start = ftell($ofp);
     882        $start = self::ftell($ofp);
    879883        fwrite($ofp, str_repeat("\x00", 16));
    880884
    881885        /** @var string $c */
     
    926930            $block0 = null;
    927931            $subkey = null;
    928932        }
    929         $end = ftell($ofp);
     933        $end = self::ftell($ofp);
    930934
    931935        /*
    932936         * Write the Poly1305 authentication tag that provides integrity
     
    10431047        $mlen = 0
    10441048    ) {
    10451049        /** @var int $pos */
    1046         $pos = ftell($ifp);
     1050        $pos = self::ftell($ifp);
    10471051
    10481052        /** @var int $iter */
    10491053        $iter = 1;
     
    11061110        }
    11071111
    11081112        /** @var int $originalPosition */
    1109         $originalPosition = ftell($fp);
     1113        $originalPosition = self::ftell($fp);
    11101114
    11111115        // Move file pointer to beginning of file
    11121116        fseek($fp, 0, SEEK_SET);
     
    13141318        if (!is_string($plaintext)) {
    13151319            throw new SodiumException('Could not read input file');
    13161320        }
    1317         $first32 = ftell($ifp);
     1321        $first32 = self::ftell($ifp);
    13181322
    13191323        /** @var string $subkey */
    13201324        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
     
    13481352        );
    13491353
    13501354        // Pre-write 16 blank bytes for the Poly1305 tag
    1351         $start = ftell($ofp);
     1355        $start = self::ftell($ofp);
    13521356        fwrite($ofp, str_repeat("\x00", 16));
    13531357
    13541358        /** @var string $c */
     
    13991403            $block0 = null;
    14001404            $subkey = null;
    14011405        }
    1402         $end = ftell($ofp);
     1406        $end = self::ftell($ofp);
    14031407
    14041408        /*
    14051409         * Write the Poly1305 authentication tag that provides integrity
     
    15151519        $mlen = 0
    15161520    ) {
    15171521        /** @var int $pos */
    1518         $pos = ftell($ifp);
     1522        $pos = self::ftell($ifp);
    15191523
    15201524        /** @var int $iter */
    15211525        $iter = 1;
     
    15401544        fseek($ifp, $pos, SEEK_SET);
    15411545        return $res;
    15421546    }
     1547
     1548    /**
     1549     * @param resource $resource
     1550     * @return int
     1551     * @throws SodiumException
     1552     */
     1553    private static function ftell($resource)
     1554    {
     1555        $return = ftell($resource);
     1556        if (!is_int($return)) {
     1557            throw new SodiumException('ftell() returned false');
     1558        }
     1559        return (int) $return;
     1560    }
    15431561}