[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/sodium_compat/src/Core/ -> ChaCha20.php (source)

   1  <?php
   2  
   3  if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) {
   4      return;
   5  }
   6  
   7  /**
   8   * Class ParagonIE_Sodium_Core_ChaCha20
   9   */
  10  class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util
  11  {
  12      /**
  13       * Bitwise left rotation
  14       *
  15       * @internal You should not use this directly from another application
  16       *
  17       * @param int $v
  18       * @param int $n
  19       * @return int
  20       */
  21      public static function rotate($v, $n)
  22      {
  23          $v &= 0xffffffff;
  24          $n &= 31;
  25          return (int) (
  26              0xffffffff & (
  27                  ($v << $n)
  28                      |
  29                  ($v >> (32 - $n))
  30              )
  31          );
  32      }
  33  
  34      /**
  35       * The ChaCha20 quarter round function. Works on four 32-bit integers.
  36       *
  37       * @internal You should not use this directly from another application
  38       *
  39       * @param int $a
  40       * @param int $b
  41       * @param int $c
  42       * @param int $d
  43       * @return array<int, int>
  44       */
  45      protected static function quarterRound($a, $b, $c, $d)
  46      {
  47          # a = PLUS(a,b); d = ROTATE(XOR(d,a),16);
  48          /** @var int $a */
  49          $a = ($a + $b) & 0xffffffff;
  50          $d = self::rotate($d ^ $a, 16);
  51  
  52          # c = PLUS(c,d); b = ROTATE(XOR(b,c),12);
  53          /** @var int $c */
  54          $c = ($c + $d) & 0xffffffff;
  55          $b = self::rotate($b ^ $c, 12);
  56  
  57          # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8);
  58          /** @var int $a */
  59          $a = ($a + $b) & 0xffffffff;
  60          $d = self::rotate($d ^ $a, 8);
  61  
  62          # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
  63          /** @var int $c */
  64          $c = ($c + $d) & 0xffffffff;
  65          $b = self::rotate($b ^ $c, 7);
  66          return array((int) $a, (int) $b, (int) $c, (int) $d);
  67      }
  68  
  69      /**
  70       * @internal You should not use this directly from another application
  71       *
  72       * @param ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx
  73       * @param string $message
  74       *
  75       * @return string
  76       * @throws TypeError
  77       * @throws SodiumException
  78       */
  79      public static function encryptBytes(
  80          ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx,
  81          $message = ''
  82      ) {
  83          $bytes = self::strlen($message);
  84  
  85          /*
  86          j0 = ctx->input[0];
  87          j1 = ctx->input[1];
  88          j2 = ctx->input[2];
  89          j3 = ctx->input[3];
  90          j4 = ctx->input[4];
  91          j5 = ctx->input[5];
  92          j6 = ctx->input[6];
  93          j7 = ctx->input[7];
  94          j8 = ctx->input[8];
  95          j9 = ctx->input[9];
  96          j10 = ctx->input[10];
  97          j11 = ctx->input[11];
  98          j12 = ctx->input[12];
  99          j13 = ctx->input[13];
 100          j14 = ctx->input[14];
 101          j15 = ctx->input[15];
 102          */
 103          $j0  = (int) $ctx[0];
 104          $j1  = (int) $ctx[1];
 105          $j2  = (int) $ctx[2];
 106          $j3  = (int) $ctx[3];
 107          $j4  = (int) $ctx[4];
 108          $j5  = (int) $ctx[5];
 109          $j6  = (int) $ctx[6];
 110          $j7  = (int) $ctx[7];
 111          $j8  = (int) $ctx[8];
 112          $j9  = (int) $ctx[9];
 113          $j10 = (int) $ctx[10];
 114          $j11 = (int) $ctx[11];
 115          $j12 = (int) $ctx[12];
 116          $j13 = (int) $ctx[13];
 117          $j14 = (int) $ctx[14];
 118          $j15 = (int) $ctx[15];
 119  
 120          $c = '';
 121          for (;;) {
 122              if ($j12 > 0xffffffff || $j13 > 0xffffffff) {
 123                  throw new SodiumException('Overflow');
 124              }
 125              if ($bytes < 64) {
 126                  $message .= str_repeat("\x00", 64 - $bytes);
 127              }
 128  
 129              $x0 =  (int) $j0;
 130              $x1 =  (int) $j1;
 131              $x2 =  (int) $j2;
 132              $x3 =  (int) $j3;
 133              $x4 =  (int) $j4;
 134              $x5 =  (int) $j5;
 135              $x6 =  (int) $j6;
 136              $x7 =  (int) $j7;
 137              $x8 =  (int) $j8;
 138              $x9 =  (int) $j9;
 139              $x10 = (int) $j10;
 140              $x11 = (int) $j11;
 141              $x12 = (int) $j12;
 142              $x13 = (int) $j13;
 143              $x14 = (int) $j14;
 144              $x15 = (int) $j15;
 145  
 146              # for (i = 20; i > 0; i -= 2) {
 147              for ($i = 20; $i > 0; $i -= 2) {
 148                  # QUARTERROUND( x0,  x4,  x8,  x12)
 149                  list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
 150  
 151                  # QUARTERROUND( x1,  x5,  x9,  x13)
 152                  list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
 153  
 154                  # QUARTERROUND( x2,  x6,  x10,  x14)
 155                  list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
 156  
 157                  # QUARTERROUND( x3,  x7,  x11,  x15)
 158                  list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
 159  
 160                  # QUARTERROUND( x0,  x5,  x10,  x15)
 161                  list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
 162  
 163                  # QUARTERROUND( x1,  x6,  x11,  x12)
 164                  list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
 165  
 166                  # QUARTERROUND( x2,  x7,  x8,  x13)
 167                  list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
 168  
 169                  # QUARTERROUND( x3,  x4,  x9,  x14)
 170                  list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
 171              }
 172              /*
 173              x0 = PLUS(x0, j0);
 174              x1 = PLUS(x1, j1);
 175              x2 = PLUS(x2, j2);
 176              x3 = PLUS(x3, j3);
 177              x4 = PLUS(x4, j4);
 178              x5 = PLUS(x5, j5);
 179              x6 = PLUS(x6, j6);
 180              x7 = PLUS(x7, j7);
 181              x8 = PLUS(x8, j8);
 182              x9 = PLUS(x9, j9);
 183              x10 = PLUS(x10, j10);
 184              x11 = PLUS(x11, j11);
 185              x12 = PLUS(x12, j12);
 186              x13 = PLUS(x13, j13);
 187              x14 = PLUS(x14, j14);
 188              x15 = PLUS(x15, j15);
 189              */
 190              /** @var int $x0 */
 191              $x0  = ($x0 & 0xffffffff) + $j0;
 192              /** @var int $x1 */
 193              $x1  = ($x1 & 0xffffffff) + $j1;
 194              /** @var int $x2 */
 195              $x2  = ($x2 & 0xffffffff) + $j2;
 196              /** @var int $x3 */
 197              $x3  = ($x3 & 0xffffffff) + $j3;
 198              /** @var int $x4 */
 199              $x4  = ($x4 & 0xffffffff) + $j4;
 200              /** @var int $x5 */
 201              $x5  = ($x5 & 0xffffffff) + $j5;
 202              /** @var int $x6 */
 203              $x6  = ($x6 & 0xffffffff) + $j6;
 204              /** @var int $x7 */
 205              $x7  = ($x7 & 0xffffffff) + $j7;
 206              /** @var int $x8 */
 207              $x8  = ($x8 & 0xffffffff) + $j8;
 208              /** @var int $x9 */
 209              $x9  = ($x9 & 0xffffffff) + $j9;
 210              /** @var int $x10 */
 211              $x10 = ($x10 & 0xffffffff) + $j10;
 212              /** @var int $x11 */
 213              $x11 = ($x11 & 0xffffffff) + $j11;
 214              /** @var int $x12 */
 215              $x12 = ($x12 & 0xffffffff) + $j12;
 216              /** @var int $x13 */
 217              $x13 = ($x13 & 0xffffffff) + $j13;
 218              /** @var int $x14 */
 219              $x14 = ($x14 & 0xffffffff) + $j14;
 220              /** @var int $x15 */
 221              $x15 = ($x15 & 0xffffffff) + $j15;
 222  
 223              /*
 224              x0 = XOR(x0, LOAD32_LE(m + 0));
 225              x1 = XOR(x1, LOAD32_LE(m + 4));
 226              x2 = XOR(x2, LOAD32_LE(m + 8));
 227              x3 = XOR(x3, LOAD32_LE(m + 12));
 228              x4 = XOR(x4, LOAD32_LE(m + 16));
 229              x5 = XOR(x5, LOAD32_LE(m + 20));
 230              x6 = XOR(x6, LOAD32_LE(m + 24));
 231              x7 = XOR(x7, LOAD32_LE(m + 28));
 232              x8 = XOR(x8, LOAD32_LE(m + 32));
 233              x9 = XOR(x9, LOAD32_LE(m + 36));
 234              x10 = XOR(x10, LOAD32_LE(m + 40));
 235              x11 = XOR(x11, LOAD32_LE(m + 44));
 236              x12 = XOR(x12, LOAD32_LE(m + 48));
 237              x13 = XOR(x13, LOAD32_LE(m + 52));
 238              x14 = XOR(x14, LOAD32_LE(m + 56));
 239              x15 = XOR(x15, LOAD32_LE(m + 60));
 240              */
 241              $x0  ^= self::load_4(self::substr($message, 0, 4));
 242              $x1  ^= self::load_4(self::substr($message, 4, 4));
 243              $x2  ^= self::load_4(self::substr($message, 8, 4));
 244              $x3  ^= self::load_4(self::substr($message, 12, 4));
 245              $x4  ^= self::load_4(self::substr($message, 16, 4));
 246              $x5  ^= self::load_4(self::substr($message, 20, 4));
 247              $x6  ^= self::load_4(self::substr($message, 24, 4));
 248              $x7  ^= self::load_4(self::substr($message, 28, 4));
 249              $x8  ^= self::load_4(self::substr($message, 32, 4));
 250              $x9  ^= self::load_4(self::substr($message, 36, 4));
 251              $x10 ^= self::load_4(self::substr($message, 40, 4));
 252              $x11 ^= self::load_4(self::substr($message, 44, 4));
 253              $x12 ^= self::load_4(self::substr($message, 48, 4));
 254              $x13 ^= self::load_4(self::substr($message, 52, 4));
 255              $x14 ^= self::load_4(self::substr($message, 56, 4));
 256              $x15 ^= self::load_4(self::substr($message, 60, 4));
 257  
 258              /*
 259                  j12 = PLUSONE(j12);
 260                  if (!j12) {
 261                      j13 = PLUSONE(j13);
 262                  }
 263               */
 264              if (
 265                  ++$j12 > 0xffffffff &&
 266                  !($ctx instanceof ParagonIE_Sodium_Core_ChaCha20_IetfCtx)
 267              ) {
 268                  $j12 = 0;
 269                  ++$j13;
 270              }
 271  
 272              /*
 273              STORE32_LE(c + 0, x0);
 274              STORE32_LE(c + 4, x1);
 275              STORE32_LE(c + 8, x2);
 276              STORE32_LE(c + 12, x3);
 277              STORE32_LE(c + 16, x4);
 278              STORE32_LE(c + 20, x5);
 279              STORE32_LE(c + 24, x6);
 280              STORE32_LE(c + 28, x7);
 281              STORE32_LE(c + 32, x8);
 282              STORE32_LE(c + 36, x9);
 283              STORE32_LE(c + 40, x10);
 284              STORE32_LE(c + 44, x11);
 285              STORE32_LE(c + 48, x12);
 286              STORE32_LE(c + 52, x13);
 287              STORE32_LE(c + 56, x14);
 288              STORE32_LE(c + 60, x15);
 289              */
 290              $block = self::store32_le((int) ($x0  & 0xffffffff)) .
 291                   self::store32_le((int) ($x1  & 0xffffffff)) .
 292                   self::store32_le((int) ($x2  & 0xffffffff)) .
 293                   self::store32_le((int) ($x3  & 0xffffffff)) .
 294                   self::store32_le((int) ($x4  & 0xffffffff)) .
 295                   self::store32_le((int) ($x5  & 0xffffffff)) .
 296                   self::store32_le((int) ($x6  & 0xffffffff)) .
 297                   self::store32_le((int) ($x7  & 0xffffffff)) .
 298                   self::store32_le((int) ($x8  & 0xffffffff)) .
 299                   self::store32_le((int) ($x9  & 0xffffffff)) .
 300                   self::store32_le((int) ($x10 & 0xffffffff)) .
 301                   self::store32_le((int) ($x11 & 0xffffffff)) .
 302                   self::store32_le((int) ($x12 & 0xffffffff)) .
 303                   self::store32_le((int) ($x13 & 0xffffffff)) .
 304                   self::store32_le((int) ($x14 & 0xffffffff)) .
 305                   self::store32_le((int) ($x15 & 0xffffffff));
 306  
 307              /* Partial block */
 308              if ($bytes < 64) {
 309                  $c .= self::substr($block, 0, $bytes);
 310                  break;
 311              }
 312  
 313              /* Full block */
 314              $c .= $block;
 315              $bytes -= 64;
 316              if ($bytes <= 0) {
 317                  break;
 318              }
 319              $message = self::substr($message, 64);
 320          }
 321          /* end for(;;) loop */
 322  
 323          $ctx[12] = $j12;
 324          $ctx[13] = $j13;
 325          return $c;
 326      }
 327  
 328      /**
 329       * @internal You should not use this directly from another application
 330       *
 331       * @param int $len
 332       * @param string $nonce
 333       * @param string $key
 334       * @return string
 335       * @throws SodiumException
 336       * @throws TypeError
 337       */
 338      public static function stream($len, $nonce, $key)
 339      {
 340          return self::encryptBytes(
 341              new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce),
 342              str_repeat("\x00", $len)
 343          );
 344      }
 345  
 346      /**
 347       * @internal You should not use this directly from another application
 348       *
 349       * @param int $len
 350       * @param string $nonce
 351       * @param string $key
 352       * @return string
 353       * @throws SodiumException
 354       * @throws TypeError
 355       */
 356      public static function ietfStream($len, $nonce, $key)
 357      {
 358          return self::encryptBytes(
 359              new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce),
 360              str_repeat("\x00", $len)
 361          );
 362      }
 363  
 364      /**
 365       * @internal You should not use this directly from another application
 366       *
 367       * @param string $message
 368       * @param string $nonce
 369       * @param string $key
 370       * @param string $ic
 371       * @return string
 372       * @throws SodiumException
 373       * @throws TypeError
 374       */
 375      public static function ietfStreamXorIc($message, $nonce, $key, $ic = '')
 376      {
 377          return self::encryptBytes(
 378              new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic),
 379              $message
 380          );
 381      }
 382  
 383      /**
 384       * @internal You should not use this directly from another application
 385       *
 386       * @param string $message
 387       * @param string $nonce
 388       * @param string $key
 389       * @param string $ic
 390       * @return string
 391       * @throws SodiumException
 392       * @throws TypeError
 393       */
 394      public static function streamXorIc($message, $nonce, $key, $ic = '')
 395      {
 396          return self::encryptBytes(
 397              new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic),
 398              $message
 399          );
 400      }
 401  }


Generated : Wed Nov 18 08:20:02 2026 Cross-referenced by PHPXref