angelovcom.net

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

HChaCha20.php (3871B)


      1 <?php
      2 
      3 if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) {
      4     return;
      5 }
      6 
      7 /**
      8  * Class ParagonIE_Sodium_Core_HChaCha20
      9  */
     10 class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20
     11 {
     12     /**
     13      * @param string $in
     14      * @param string $key
     15      * @param string|null $c
     16      * @return string
     17      * @throws TypeError
     18      */
     19     public static function hChaCha20($in = '', $key = '', $c = null)
     20     {
     21         $ctx = array();
     22 
     23         if ($c === null) {
     24             $ctx[0] = 0x61707865;
     25             $ctx[1] = 0x3320646e;
     26             $ctx[2] = 0x79622d32;
     27             $ctx[3] = 0x6b206574;
     28         } else {
     29             $ctx[0] = self::load_4(self::substr($c,  0, 4));
     30             $ctx[1] = self::load_4(self::substr($c,  4, 4));
     31             $ctx[2] = self::load_4(self::substr($c,  8, 4));
     32             $ctx[3] = self::load_4(self::substr($c, 12, 4));
     33         }
     34         $ctx[4]  = self::load_4(self::substr($key,  0, 4));
     35         $ctx[5]  = self::load_4(self::substr($key,  4, 4));
     36         $ctx[6]  = self::load_4(self::substr($key,  8, 4));
     37         $ctx[7]  = self::load_4(self::substr($key, 12, 4));
     38         $ctx[8]  = self::load_4(self::substr($key, 16, 4));
     39         $ctx[9]  = self::load_4(self::substr($key, 20, 4));
     40         $ctx[10] = self::load_4(self::substr($key, 24, 4));
     41         $ctx[11] = self::load_4(self::substr($key, 28, 4));
     42         $ctx[12] = self::load_4(self::substr($in,   0, 4));
     43         $ctx[13] = self::load_4(self::substr($in,   4, 4));
     44         $ctx[14] = self::load_4(self::substr($in,   8, 4));
     45         $ctx[15] = self::load_4(self::substr($in,  12, 4));
     46         return self::hChaCha20Bytes($ctx);
     47     }
     48 
     49     /**
     50      * @param array $ctx
     51      * @return string
     52      * @throws TypeError
     53      */
     54     protected static function hChaCha20Bytes(array $ctx)
     55     {
     56         $x0  = (int) $ctx[0];
     57         $x1  = (int) $ctx[1];
     58         $x2  = (int) $ctx[2];
     59         $x3  = (int) $ctx[3];
     60         $x4  = (int) $ctx[4];
     61         $x5  = (int) $ctx[5];
     62         $x6  = (int) $ctx[6];
     63         $x7  = (int) $ctx[7];
     64         $x8  = (int) $ctx[8];
     65         $x9  = (int) $ctx[9];
     66         $x10 = (int) $ctx[10];
     67         $x11 = (int) $ctx[11];
     68         $x12 = (int) $ctx[12];
     69         $x13 = (int) $ctx[13];
     70         $x14 = (int) $ctx[14];
     71         $x15 = (int) $ctx[15];
     72 
     73         for ($i = 0; $i < 10; ++$i) {
     74             # QUARTERROUND( x0,  x4,  x8,  x12)
     75             list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
     76 
     77             # QUARTERROUND( x1,  x5,  x9,  x13)
     78             list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
     79 
     80             # QUARTERROUND( x2,  x6,  x10,  x14)
     81             list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
     82 
     83             # QUARTERROUND( x3,  x7,  x11,  x15)
     84             list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
     85 
     86             # QUARTERROUND( x0,  x5,  x10,  x15)
     87             list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
     88 
     89             # QUARTERROUND( x1,  x6,  x11,  x12)
     90             list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
     91 
     92             # QUARTERROUND( x2,  x7,  x8,  x13)
     93             list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
     94 
     95             # QUARTERROUND( x3,  x4,  x9,  x14)
     96             list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
     97         }
     98 
     99         return self::store32_le((int) ($x0  & 0xffffffff)) .
    100             self::store32_le((int) ($x1  & 0xffffffff)) .
    101             self::store32_le((int) ($x2  & 0xffffffff)) .
    102             self::store32_le((int) ($x3  & 0xffffffff)) .
    103             self::store32_le((int) ($x12 & 0xffffffff)) .
    104             self::store32_le((int) ($x13 & 0xffffffff)) .
    105             self::store32_le((int) ($x14 & 0xffffffff)) .
    106             self::store32_le((int) ($x15 & 0xffffffff));
    107     }
    108 }