[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  
   3  if (class_exists('ParagonIE_Sodium_Core_ChaCha20_Ctx', false)) {
   4      return;
   5  }
   6  
   7  /**
   8   * Class ParagonIE_Sodium_Core_ChaCha20_Ctx
   9   */
  10  class ParagonIE_Sodium_Core_ChaCha20_Ctx extends ParagonIE_Sodium_Core_Util implements ArrayAccess
  11  {
  12      /**
  13       * @var SplFixedArray internally, <int, int>
  14       */
  15      protected $container;
  16  
  17      /**
  18       * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
  19       *
  20       * @internal You should not use this directly from another application
  21       *
  22       * @param string $key     ChaCha20 key.
  23       * @param string $iv      Initialization Vector (a.k.a. nonce).
  24       * @param string $counter The initial counter value.
  25       *                        Defaults to 8 0x00 bytes.
  26       * @throws InvalidArgumentException
  27       * @throws TypeError
  28       */
  29      public function __construct($key = '', $iv = '', $counter = '')
  30      {
  31          if (self::strlen($key) !== 32) {
  32              throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.');
  33          }
  34          if (self::strlen($iv) !== 8) {
  35              throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.');
  36          }
  37          $this->container = new SplFixedArray(16);
  38  
  39          /* "expand 32-byte k" as per ChaCha20 spec */
  40          $this->container[0]  = 0x61707865;
  41          $this->container[1]  = 0x3320646e;
  42          $this->container[2]  = 0x79622d32;
  43          $this->container[3]  = 0x6b206574;
  44          $this->container[4]  = self::load_4(self::substr($key, 0, 4));
  45          $this->container[5]  = self::load_4(self::substr($key, 4, 4));
  46          $this->container[6]  = self::load_4(self::substr($key, 8, 4));
  47          $this->container[7]  = self::load_4(self::substr($key, 12, 4));
  48          $this->container[8]  = self::load_4(self::substr($key, 16, 4));
  49          $this->container[9]  = self::load_4(self::substr($key, 20, 4));
  50          $this->container[10] = self::load_4(self::substr($key, 24, 4));
  51          $this->container[11] = self::load_4(self::substr($key, 28, 4));
  52  
  53          if (empty($counter)) {
  54              $this->container[12] = 0;
  55              $this->container[13] = 0;
  56          } else {
  57              $this->container[12] = self::load_4(self::substr($counter, 0, 4));
  58              $this->container[13] = self::load_4(self::substr($counter, 4, 4));
  59          }
  60          $this->container[14] = self::load_4(self::substr($iv, 0, 4));
  61          $this->container[15] = self::load_4(self::substr($iv, 4, 4));
  62      }
  63  
  64      /**
  65       * @internal You should not use this directly from another application
  66       *
  67       * @param int $offset
  68       * @param int $value
  69       * @return void
  70       * @psalm-suppress MixedArrayOffset
  71       */
  72      #[ReturnTypeWillChange]
  73      public function offsetSet($offset, $value)
  74      {
  75          if (!is_int($offset)) {
  76              throw new InvalidArgumentException('Expected an integer');
  77          }
  78          if (!is_int($value)) {
  79              throw new InvalidArgumentException('Expected an integer');
  80          }
  81          $this->container[$offset] = $value;
  82      }
  83  
  84      /**
  85       * @internal You should not use this directly from another application
  86       *
  87       * @param int $offset
  88       * @return bool
  89       */
  90      #[ReturnTypeWillChange]
  91      public function offsetExists($offset)
  92      {
  93          return isset($this->container[$offset]);
  94      }
  95  
  96      /**
  97       * @internal You should not use this directly from another application
  98       *
  99       * @param int $offset
 100       * @return void
 101       * @psalm-suppress MixedArrayOffset
 102       */
 103      #[ReturnTypeWillChange]
 104      public function offsetUnset($offset)
 105      {
 106          unset($this->container[$offset]);
 107      }
 108  
 109      /**
 110       * @internal You should not use this directly from another application
 111       *
 112       * @param int $offset
 113       * @return mixed|null
 114       * @psalm-suppress MixedArrayOffset
 115       */
 116      #[ReturnTypeWillChange]
 117      public function offsetGet($offset)
 118      {
 119          return isset($this->container[$offset])
 120              ? $this->container[$offset]
 121              : null;
 122      }
 123  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref