[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/sodium_compat/src/Core32/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_Core32_ChaCha20_Ctx
   9   */
  10  class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util implements ArrayAccess
  11  {
  12      /**
  13       * @var SplFixedArray internally, <int, ParagonIE_Sodium_Core32_Int32>
  14       */
  15      protected $container;
  16  
  17      /** @var bool */
  18      protected $exhausted = false;
  19  
  20      /** @return bool */
  21      public function isExhausted()
  22      {
  23          return $this->exhausted;
  24      }
  25  
  26      /** @return void */
  27      public function markExhausted()
  28      {
  29          $this->exhausted = true;
  30      }
  31  
  32      /**
  33       * ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
  34       *
  35       * @internal You should not use this directly from another application
  36       *
  37       * @param string $key     ChaCha20 key.
  38       * @param string $iv      Initialization Vector (a.k.a. nonce).
  39       * @param string $counter The initial counter value.
  40       *                        Defaults to 8 0x00 bytes.
  41       * @throws InvalidArgumentException
  42       * @throws SodiumException
  43       * @throws TypeError
  44       */
  45      public function __construct($key = '', $iv = '', $counter = '')
  46      {
  47          if (self::strlen($key) !== 32) {
  48              throw new InvalidArgumentException('ChaCha20 expects a 256-bit key.');
  49          }
  50          if (self::strlen($iv) !== 8) {
  51              throw new InvalidArgumentException('ChaCha20 expects a 64-bit nonce.');
  52          }
  53          $this->container = new SplFixedArray(16);
  54  
  55          /* "expand 32-byte k" as per ChaCha20 spec */
  56          $this->container[0]  = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865));
  57          $this->container[1]  = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e));
  58          $this->container[2]  = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32));
  59          $this->container[3]  = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574));
  60  
  61          $this->container[4]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4));
  62          $this->container[5]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4));
  63          $this->container[6]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4));
  64          $this->container[7]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4));
  65          $this->container[8]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4));
  66          $this->container[9]  = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4));
  67          $this->container[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4));
  68          $this->container[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4));
  69  
  70          if (empty($counter)) {
  71              $this->container[12] = new ParagonIE_Sodium_Core32_Int32();
  72              $this->container[13] = new ParagonIE_Sodium_Core32_Int32();
  73          } else {
  74              $this->container[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 0, 4));
  75              $this->container[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($counter, 4, 4));
  76          }
  77          $this->container[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 0, 4));
  78          $this->container[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($iv, 4, 4));
  79      }
  80  
  81      /**
  82       * @internal You should not use this directly from another application
  83       *
  84       * @param int $offset
  85       * @param int|ParagonIE_Sodium_Core32_Int32 $value
  86       * @return void
  87       */
  88      #[ReturnTypeWillChange]
  89      public function offsetSet($offset, $value)
  90      {
  91          if (!is_int($offset)) {
  92              throw new InvalidArgumentException('Expected an integer');
  93          }
  94          if ($value instanceof ParagonIE_Sodium_Core32_Int32) {
  95              /*
  96          } elseif (is_int($value)) {
  97              $value = ParagonIE_Sodium_Core32_Int32::fromInt($value);
  98              */
  99          } else {
 100              throw new InvalidArgumentException('Expected an integer');
 101          }
 102          $this->container[$offset] = $value;
 103      }
 104  
 105      /**
 106       * @internal You should not use this directly from another application
 107       *
 108       * @param int $offset
 109       * @return bool
 110       * @psalm-suppress MixedArrayOffset
 111       */
 112      #[ReturnTypeWillChange]
 113      public function offsetExists($offset)
 114      {
 115          return isset($this->container[$offset]);
 116      }
 117  
 118      /**
 119       * @internal You should not use this directly from another application
 120       *
 121       * @param int $offset
 122       * @return void
 123       * @psalm-suppress MixedArrayOffset
 124       */
 125      #[ReturnTypeWillChange]
 126      public function offsetUnset($offset)
 127      {
 128          unset($this->container[$offset]);
 129      }
 130  
 131      /**
 132       * @internal You should not use this directly from another application
 133       *
 134       * @param int $offset
 135       * @return mixed|null
 136       * @psalm-suppress MixedArrayOffset
 137       */
 138      #[ReturnTypeWillChange]
 139      public function offsetGet($offset)
 140      {
 141          return isset($this->container[$offset])
 142              ? $this->container[$offset]
 143              : null;
 144      }
 145  }


Generated : Fri Nov 13 08:20:01 2026 Cross-referenced by PHPXref