wp/wp-includes/sodium_compat/src/Core/HSalsa20.php
author ymh <ymh.work@gmail.com>
Wed, 16 Oct 2019 11:23:38 +0200
changeset 14 00ac8f60d73f
parent 9 177826044cd9
permissions -rw-r--r--
remove unused plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
if (class_exists('ParagonIE_Sodium_Core_HSalsa20', false)) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
    return;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * Class ParagonIE_Sodium_Core_HSalsa20
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
abstract class ParagonIE_Sodium_Core_HSalsa20 extends ParagonIE_Sodium_Core_Salsa20
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
{
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    /**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
     * Calculate an hsalsa20 hash of a single block
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
     *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
     * HSalsa20 doesn't have a counter and will never be used for more than
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     * one block (used to derive a subkey for xsalsa20).
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     * @internal You should not use this directly from another application
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
     *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
     * @param string $in
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
     * @param string $k
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
     * @param string|null $c
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
     * @return string
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
     * @throws TypeError
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
     */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    public static function hsalsa20($in, $k, $c = null)
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        if ($c === null) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            $x0  = 0x61707865;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            $x5  = 0x3320646e;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            $x10 = 0x79622d32;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            $x15 = 0x6b206574;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        } else {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            $x0  = self::load_4(self::substr($c, 0, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            $x5  = self::load_4(self::substr($c, 4, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            $x10 = self::load_4(self::substr($c, 8, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            $x15 = self::load_4(self::substr($c, 12, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        }
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $x1  = self::load_4(self::substr($k, 0, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $x2  = self::load_4(self::substr($k, 4, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        $x3  = self::load_4(self::substr($k, 8, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        $x4  = self::load_4(self::substr($k, 12, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        $x11 = self::load_4(self::substr($k, 16, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        $x12 = self::load_4(self::substr($k, 20, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        $x13 = self::load_4(self::substr($k, 24, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        $x14 = self::load_4(self::substr($k, 28, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $x6  = self::load_4(self::substr($in, 0, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        $x7  = self::load_4(self::substr($in, 4, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        $x8  = self::load_4(self::substr($in, 8, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        $x9  = self::load_4(self::substr($in, 12, 4));
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        for ($i = self::ROUNDS; $i > 0; $i -= 2) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            $x4 ^= self::rotate($x0 + $x12, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            $x8 ^= self::rotate($x4 + $x0, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            $x12 ^= self::rotate($x8 + $x4, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            $x0 ^= self::rotate($x12 + $x8, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            $x9 ^= self::rotate($x5 + $x1, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            $x13 ^= self::rotate($x9 + $x5, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            $x1 ^= self::rotate($x13 + $x9, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            $x5 ^= self::rotate($x1 + $x13, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            $x14 ^= self::rotate($x10 + $x6, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            $x2 ^= self::rotate($x14 + $x10, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            $x6 ^= self::rotate($x2 + $x14, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            $x10 ^= self::rotate($x6 + $x2, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            $x3 ^= self::rotate($x15 + $x11, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            $x7 ^= self::rotate($x3 + $x15, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            $x11 ^= self::rotate($x7 + $x3, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            $x15 ^= self::rotate($x11 + $x7, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            $x1 ^= self::rotate($x0 + $x3, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            $x2 ^= self::rotate($x1 + $x0, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            $x3 ^= self::rotate($x2 + $x1, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            $x0 ^= self::rotate($x3 + $x2, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            $x6 ^= self::rotate($x5 + $x4, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            $x7 ^= self::rotate($x6 + $x5, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            $x4 ^= self::rotate($x7 + $x6, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            $x5 ^= self::rotate($x4 + $x7, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
            $x11 ^= self::rotate($x10 + $x9, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            $x8 ^= self::rotate($x11 + $x10, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            $x9 ^= self::rotate($x8 + $x11, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            $x10 ^= self::rotate($x9 + $x8, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            $x12 ^= self::rotate($x15 + $x14, 7);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            $x13 ^= self::rotate($x12 + $x15, 9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            $x14 ^= self::rotate($x13 + $x12, 13);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            $x15 ^= self::rotate($x14 + $x13, 18);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        }
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        return self::store32_le($x0) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            self::store32_le($x5) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
            self::store32_le($x10) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            self::store32_le($x15) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            self::store32_le($x6) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            self::store32_le($x7) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            self::store32_le($x8) .
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            self::store32_le($x9);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    }
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
}