[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  
   3  if (class_exists('ParagonIE_Sodium_Core_Poly1305_State', false)) {
   4      return;
   5  }
   6  
   7  /**
   8   * Class ParagonIE_Sodium_Core_Poly1305_State
   9   */
  10  class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util
  11  {
  12      /**
  13       * @var array<int, int>
  14       */
  15      protected $buffer = array();
  16  
  17      /**
  18       * @var bool
  19       */
  20      protected $final = false;
  21  
  22      /**
  23       * @var array<int, int>
  24       */
  25      public $h;
  26  
  27      /**
  28       * @var int
  29       */
  30      protected $leftover = 0;
  31  
  32      /**
  33       * @var int[]
  34       */
  35      public $r;
  36  
  37      /**
  38       * @var int[]
  39       */
  40      public $pad;
  41  
  42      /**
  43       * ParagonIE_Sodium_Core_Poly1305_State constructor.
  44       *
  45       * @internal You should not use this directly from another application
  46       *
  47       * @param string $key
  48       * @throws InvalidArgumentException
  49       * @throws TypeError
  50       */
  51      public function __construct($key = '')
  52      {
  53          if (self::strlen($key) < 32) {
  54              throw new InvalidArgumentException(
  55                  'Poly1305 requires a 32-byte key'
  56              );
  57          }
  58          /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  59          $this->r = array(
  60              (int) ((self::load_4(self::substr($key, 0, 4))) & 0x3ffffff),
  61              (int) ((self::load_4(self::substr($key, 3, 4)) >> 2) & 0x3ffff03),
  62              (int) ((self::load_4(self::substr($key, 6, 4)) >> 4) & 0x3ffc0ff),
  63              (int) ((self::load_4(self::substr($key, 9, 4)) >> 6) & 0x3f03fff),
  64              (int) ((self::load_4(self::substr($key, 12, 4)) >> 8) & 0x00fffff)
  65          );
  66  
  67          /* h = 0 */
  68          $this->h = array(0, 0, 0, 0, 0);
  69  
  70          /* save pad for later */
  71          $this->pad = array(
  72              self::load_4(self::substr($key, 16, 4)),
  73              self::load_4(self::substr($key, 20, 4)),
  74              self::load_4(self::substr($key, 24, 4)),
  75              self::load_4(self::substr($key, 28, 4)),
  76          );
  77  
  78          $this->leftover = 0;
  79          $this->final = false;
  80      }
  81  
  82      /**
  83       * @internal You should not use this directly from another application
  84       *
  85       * @param string $message
  86       * @return self
  87       * @throws SodiumException
  88       * @throws TypeError
  89       */
  90      public function update($message = '')
  91      {
  92          $bytes = self::strlen($message);
  93  
  94          /* handle leftover */
  95          if ($this->leftover) {
  96              $want = ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - $this->leftover;
  97              if ($want > $bytes) {
  98                  $want = $bytes;
  99              }
 100              for ($i = 0; $i < $want; ++$i) {
 101                  $mi = self::chrToInt($message[$i]);
 102                  $this->buffer[$this->leftover + $i] = $mi;
 103              }
 104              // We snip off the leftmost bytes.
 105              $message = self::substr($message, $want);
 106              $bytes = self::strlen($message);
 107              $this->leftover += $want;
 108              if ($this->leftover < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
 109                  // We still don't have enough to run $this->blocks()
 110                  return $this;
 111              }
 112  
 113              $this->blocks(
 114                  static::intArrayToString($this->buffer),
 115                  ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
 116              );
 117              $this->leftover = 0;
 118          }
 119  
 120          /* process full blocks */
 121          if ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
 122              /** @var int $want */
 123              $want = $bytes & ~(ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE - 1);
 124              if ($want >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
 125                  $block = self::substr($message, 0, $want);
 126                  if (self::strlen($block) >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
 127                      $this->blocks($block, $want);
 128                      $message = self::substr($message, $want);
 129                      $bytes = self::strlen($message);
 130                  }
 131              }
 132          }
 133  
 134          /* store leftover */
 135          if ($bytes) {
 136              for ($i = 0; $i < $bytes; ++$i) {
 137                  $mi = self::chrToInt($message[$i]);
 138                  $this->buffer[$this->leftover + $i] = $mi;
 139              }
 140              $this->leftover = (int) $this->leftover + $bytes;
 141          }
 142          return $this;
 143      }
 144  
 145      /**
 146       * @internal You should not use this directly from another application
 147       *
 148       * @param string $message
 149       * @param int $bytes
 150       * @return self
 151       * @throws TypeError
 152       */
 153      public function blocks($message, $bytes)
 154      {
 155          if (self::strlen($message) < 16) {
 156              $message = str_pad($message, 16, "\x00", STR_PAD_RIGHT);
 157          }
 158          /** @var int $hibit */
 159          $hibit = $this->final ? 0 : 1 << 24; /* 1 << 128 */
 160          $r0 = (int) $this->r[0];
 161          $r1 = (int) $this->r[1];
 162          $r2 = (int) $this->r[2];
 163          $r3 = (int) $this->r[3];
 164          $r4 = (int) $this->r[4];
 165  
 166          $s1 = self::mul($r1, 5, 3);
 167          $s2 = self::mul($r2, 5, 3);
 168          $s3 = self::mul($r3, 5, 3);
 169          $s4 = self::mul($r4, 5, 3);
 170  
 171          $h0 = $this->h[0];
 172          $h1 = $this->h[1];
 173          $h2 = $this->h[2];
 174          $h3 = $this->h[3];
 175          $h4 = $this->h[4];
 176  
 177          while ($bytes >= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE) {
 178              /* h += m[i] */
 179              $h0 +=  self::load_4(self::substr($message, 0, 4))       & 0x3ffffff;
 180              $h1 += (self::load_4(self::substr($message, 3, 4)) >> 2) & 0x3ffffff;
 181              $h2 += (self::load_4(self::substr($message, 6, 4)) >> 4) & 0x3ffffff;
 182              $h3 += (self::load_4(self::substr($message, 9, 4)) >> 6) & 0x3ffffff;
 183              $h4 += (self::load_4(self::substr($message, 12, 4)) >> 8) | $hibit;
 184  
 185              /* h *= r */
 186              $d0 = (
 187                  self::mul($h0, $r0, 25) +
 188                  self::mul($s4, $h1, 26) +
 189                  self::mul($s3, $h2, 26) +
 190                  self::mul($s2, $h3, 26) +
 191                  self::mul($s1, $h4, 26)
 192              );
 193  
 194              $d1 = (
 195                  self::mul($h0, $r1, 25) +
 196                  self::mul($h1, $r0, 25) +
 197                  self::mul($s4, $h2, 26) +
 198                  self::mul($s3, $h3, 26) +
 199                  self::mul($s2, $h4, 26)
 200              );
 201  
 202              $d2 = (
 203                  self::mul($h0, $r2, 25) +
 204                  self::mul($h1, $r1, 25) +
 205                  self::mul($h2, $r0, 25) +
 206                  self::mul($s4, $h3, 26) +
 207                  self::mul($s3, $h4, 26)
 208              );
 209  
 210              $d3 = (
 211                  self::mul($h0, $r3, 25) +
 212                  self::mul($h1, $r2, 25) +
 213                  self::mul($h2, $r1, 25) +
 214                  self::mul($h3, $r0, 25) +
 215                  self::mul($s4, $h4, 26)
 216              );
 217  
 218              $d4 = (
 219                  self::mul($h0, $r4, 25) +
 220                  self::mul($h1, $r3, 25) +
 221                  self::mul($h2, $r2, 25) +
 222                  self::mul($h3, $r1, 25) +
 223                  self::mul($h4, $r0, 25)
 224              );
 225  
 226              /* (partial) h %= p */
 227              /** @var int $c */
 228              $c = $d0 >> 26;
 229              /** @var int $h0 */
 230              $h0 = $d0 & 0x3ffffff;
 231              $d1 += $c;
 232  
 233              /** @var int $c */
 234              $c = $d1 >> 26;
 235              /** @var int $h1 */
 236              $h1 = $d1 & 0x3ffffff;
 237              $d2 += $c;
 238  
 239              /** @var int $c */
 240              $c = $d2 >> 26;
 241              /** @var int $h2  */
 242              $h2 = $d2 & 0x3ffffff;
 243              $d3 += $c;
 244  
 245              /** @var int $c */
 246              $c = $d3 >> 26;
 247              /** @var int $h3 */
 248              $h3 = $d3 & 0x3ffffff;
 249              $d4 += $c;
 250  
 251              /** @var int $c */
 252              $c = $d4 >> 26;
 253              /** @var int $h4 */
 254              $h4 = $d4 & 0x3ffffff;
 255              $h0 += (int) self::mul($c, 5, 3);
 256  
 257              /** @var int $c */
 258              $c = $h0 >> 26;
 259              /** @var int $h0 */
 260              $h0 &= 0x3ffffff;
 261              $h1 += $c;
 262  
 263              // Chop off the left 32 bytes.
 264              $message = self::substr(
 265                  $message,
 266                  ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
 267              );
 268              $bytes -= ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE;
 269          }
 270  
 271          $this->h = array(
 272              (int) ($h0 & 0xffffffff),
 273              (int) ($h1 & 0xffffffff),
 274              (int) ($h2 & 0xffffffff),
 275              (int) ($h3 & 0xffffffff),
 276              (int) ($h4 & 0xffffffff)
 277          );
 278          return $this;
 279      }
 280  
 281      /**
 282       * @internal You should not use this directly from another application
 283       *
 284       * @return string
 285       * @throws TypeError
 286       */
 287      public function finish()
 288      {
 289          /* process the remaining block */
 290          if ($this->leftover) {
 291              $i = $this->leftover;
 292              $this->buffer[$i++] = 1;
 293              for (; $i < ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE; ++$i) {
 294                  $this->buffer[$i] = 0;
 295              }
 296              $this->final = true;
 297              $this->blocks(
 298                  self::substr(
 299                      static::intArrayToString($this->buffer),
 300                      0,
 301                      ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
 302                  ),
 303                  ParagonIE_Sodium_Core_Poly1305::BLOCK_SIZE
 304              );
 305          }
 306  
 307          $h0 = (int) $this->h[0];
 308          $h1 = (int) $this->h[1];
 309          $h2 = (int) $this->h[2];
 310          $h3 = (int) $this->h[3];
 311          $h4 = (int) $this->h[4];
 312  
 313          /** @var int $c */
 314          $c = $h1 >> 26;
 315          /** @var int $h1 */
 316          $h1 &= 0x3ffffff;
 317          /** @var int $h2 */
 318          $h2 += $c;
 319          /** @var int $c */
 320          $c = $h2 >> 26;
 321          /** @var int $h2 */
 322          $h2 &= 0x3ffffff;
 323          $h3 += $c;
 324          /** @var int $c */
 325          $c = $h3 >> 26;
 326          $h3 &= 0x3ffffff;
 327          $h4 += $c;
 328          /** @var int $c */
 329          $c = $h4 >> 26;
 330          $h4 &= 0x3ffffff;
 331          /** @var int $h0 */
 332          $h0 += self::mul($c, 5, 3);
 333          /** @var int $c */
 334          $c = $h0 >> 26;
 335          /** @var int $h0 */
 336          $h0 &= 0x3ffffff;
 337          /** @var int $h1 */
 338          $h1 += $c;
 339  
 340          /* compute h + -p */
 341          /** @var int $g0 */
 342          $g0 = $h0 + 5;
 343          /** @var int $c */
 344          $c = $g0 >> 26;
 345          /** @var int $g0 */
 346          $g0 &= 0x3ffffff;
 347  
 348          /** @var int $g1 */
 349          $g1 = $h1 + $c;
 350          /** @var int $c */
 351          $c = $g1 >> 26;
 352          $g1 &= 0x3ffffff;
 353  
 354          /** @var int $g2 */
 355          $g2 = $h2 + $c;
 356          /** @var int $c */
 357          $c = $g2 >> 26;
 358          /** @var int $g2 */
 359          $g2 &= 0x3ffffff;
 360  
 361          /** @var int $g3 */
 362          $g3 = $h3 + $c;
 363          /** @var int $c */
 364          $c = $g3 >> 26;
 365          /** @var int $g3 */
 366          $g3 &= 0x3ffffff;
 367  
 368          /** @var int $g4 */
 369          $g4 = ($h4 + $c - (1 << 26)) & 0xffffffff;
 370  
 371          /* select h if h < p, or h + -p if h >= p */
 372          /** @var int $mask */
 373          $mask = ($g4 >> 31) - 1;
 374  
 375          $g0 &= $mask;
 376          $g1 &= $mask;
 377          $g2 &= $mask;
 378          $g3 &= $mask;
 379          $g4 &= $mask;
 380  
 381          /** @var int $mask */
 382          $mask = ~$mask & 0xffffffff;
 383          /** @var int $h0 */
 384          $h0 = ($h0 & $mask) | $g0;
 385          /** @var int $h1 */
 386          $h1 = ($h1 & $mask) | $g1;
 387          /** @var int $h2 */
 388          $h2 = ($h2 & $mask) | $g2;
 389          /** @var int $h3 */
 390          $h3 = ($h3 & $mask) | $g3;
 391          /** @var int $h4 */
 392          $h4 = ($h4 & $mask) | $g4;
 393  
 394          /* h = h % (2^128) */
 395          /** @var int $h0 */
 396          $h0 = (($h0) | ($h1 << 26)) & 0xffffffff;
 397          /** @var int $h1 */
 398          $h1 = (($h1 >>  6) | ($h2 << 20)) & 0xffffffff;
 399          /** @var int $h2 */
 400          $h2 = (($h2 >> 12) | ($h3 << 14)) & 0xffffffff;
 401          /** @var int $h3 */
 402          $h3 = (($h3 >> 18) | ($h4 <<  8)) & 0xffffffff;
 403  
 404          /* mac = (h + pad) % (2^128) */
 405          $f = (int) ($h0 + $this->pad[0]);
 406          $h0 = (int) $f;
 407          $f = (int) ($h1 + $this->pad[1] + ($f >> 32));
 408          $h1 = (int) $f;
 409          $f = (int) ($h2 + $this->pad[2] + ($f >> 32));
 410          $h2 = (int) $f;
 411          $f = (int) ($h3 + $this->pad[3] + ($f >> 32));
 412          $h3 = (int) $f;
 413  
 414          return self::store32_le($h0 & 0xffffffff) .
 415              self::store32_le($h1 & 0xffffffff) .
 416              self::store32_le($h2 & 0xffffffff) .
 417              self::store32_le($h3 & 0xffffffff);
 418      }
 419  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7