ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

HSalsa20.php (6941B)


      1 <?php
      2 
      3 if (class_exists('ParagonIE_Sodium_Core32_HSalsa20', false)) {
      4     return;
      5 }
      6 
      7 /**
      8  * Class ParagonIE_Sodium_Core32_HSalsa20
      9  */
     10 abstract class ParagonIE_Sodium_Core32_HSalsa20 extends ParagonIE_Sodium_Core32_Salsa20
     11 {
     12     /**
     13      * Calculate an hsalsa20 hash of a single block
     14      *
     15      * HSalsa20 doesn't have a counter and will never be used for more than
     16      * one block (used to derive a subkey for xsalsa20).
     17      *
     18      * @internal You should not use this directly from another application
     19      *
     20      * @param string $in
     21      * @param string $k
     22      * @param string|null $c
     23      * @return string
     24      * @throws SodiumException
     25      * @throws TypeError
     26      */
     27     public static function hsalsa20($in, $k, $c = null)
     28     {
     29         /**
     30          * @var ParagonIE_Sodium_Core32_Int32 $x0
     31          * @var ParagonIE_Sodium_Core32_Int32 $x1
     32          * @var ParagonIE_Sodium_Core32_Int32 $x2
     33          * @var ParagonIE_Sodium_Core32_Int32 $x3
     34          * @var ParagonIE_Sodium_Core32_Int32 $x4
     35          * @var ParagonIE_Sodium_Core32_Int32 $x5
     36          * @var ParagonIE_Sodium_Core32_Int32 $x6
     37          * @var ParagonIE_Sodium_Core32_Int32 $x7
     38          * @var ParagonIE_Sodium_Core32_Int32 $x8
     39          * @var ParagonIE_Sodium_Core32_Int32 $x9
     40          * @var ParagonIE_Sodium_Core32_Int32 $x10
     41          * @var ParagonIE_Sodium_Core32_Int32 $x11
     42          * @var ParagonIE_Sodium_Core32_Int32 $x12
     43          * @var ParagonIE_Sodium_Core32_Int32 $x13
     44          * @var ParagonIE_Sodium_Core32_Int32 $x14
     45          * @var ParagonIE_Sodium_Core32_Int32 $x15
     46          * @var ParagonIE_Sodium_Core32_Int32 $j0
     47          * @var ParagonIE_Sodium_Core32_Int32 $j1
     48          * @var ParagonIE_Sodium_Core32_Int32 $j2
     49          * @var ParagonIE_Sodium_Core32_Int32 $j3
     50          * @var ParagonIE_Sodium_Core32_Int32 $j4
     51          * @var ParagonIE_Sodium_Core32_Int32 $j5
     52          * @var ParagonIE_Sodium_Core32_Int32 $j6
     53          * @var ParagonIE_Sodium_Core32_Int32 $j7
     54          * @var ParagonIE_Sodium_Core32_Int32 $j8
     55          * @var ParagonIE_Sodium_Core32_Int32 $j9
     56          * @var ParagonIE_Sodium_Core32_Int32 $j10
     57          * @var ParagonIE_Sodium_Core32_Int32 $j11
     58          * @var ParagonIE_Sodium_Core32_Int32 $j12
     59          * @var ParagonIE_Sodium_Core32_Int32 $j13
     60          * @var ParagonIE_Sodium_Core32_Int32 $j14
     61          * @var ParagonIE_Sodium_Core32_Int32 $j15
     62          */
     63         if (self::strlen($k) < 32) {
     64             throw new RangeException('Key must be 32 bytes long');
     65         }
     66         if ($c === null) {
     67             $x0  = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
     68             $x5  = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
     69             $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
     70             $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
     71         } else {
     72             $x0  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4));
     73             $x5  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4));
     74             $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4));
     75             $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4));
     76         }
     77         $x1  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4));
     78         $x2  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4));
     79         $x3  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4));
     80         $x4  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4));
     81         $x6  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4));
     82         $x7  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4));
     83         $x8  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4));
     84         $x9  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4));
     85         $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4));
     86         $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4));
     87         $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4));
     88         $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4));
     89 
     90         for ($i = self::ROUNDS; $i > 0; $i -= 2) {
     91             $x4  = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7));
     92             $x8  = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9));
     93             $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13));
     94             $x0  = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18));
     95 
     96             $x9  = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7));
     97             $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9));
     98             $x1  = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13));
     99             $x5  = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18));
    100 
    101             $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7));
    102             $x2  = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9));
    103             $x6  = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13));
    104             $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18));
    105 
    106             $x3  = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7));
    107             $x7  = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9));
    108             $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13));
    109             $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18));
    110 
    111             $x1  = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7));
    112             $x2  = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9));
    113             $x3  = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13));
    114             $x0  = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18));
    115 
    116             $x6  = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7));
    117             $x7  = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9));
    118             $x4  = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13));
    119             $x5  = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18));
    120 
    121             $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7));
    122             $x8  = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9));
    123             $x9  = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13));
    124             $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18));
    125 
    126             $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7));
    127             $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9));
    128             $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13));
    129             $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18));
    130         }
    131 
    132         return $x0->toReverseString() .
    133             $x5->toReverseString() .
    134             $x10->toReverseString() .
    135             $x15->toReverseString() .
    136             $x6->toReverseString() .
    137             $x7->toReverseString() .
    138             $x8->toReverseString() .
    139             $x9->toReverseString();
    140     }
    141 }