/htdocs/wp-includes/sodium_compat/src/Core/SecretStream/State.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 163 lines · 99 code · 16 blank · 48 comment · 3 complexity · e9ebf5e93ac75a7be38ff0eb35b26f0f MD5 · raw file

  1. <?php
  2. /**
  3. * Class ParagonIE_Sodium_Core_SecretStream_State
  4. */
  5. class ParagonIE_Sodium_Core_SecretStream_State
  6. {
  7. /** @var string $key */
  8. protected $key;
  9. /** @var int $counter */
  10. protected $counter;
  11. /** @var string $nonce */
  12. protected $nonce;
  13. /** @var string $_pad */
  14. protected $_pad;
  15. /**
  16. * ParagonIE_Sodium_Core_SecretStream_State constructor.
  17. * @param string $key
  18. * @param string|null $nonce
  19. */
  20. public function __construct($key, $nonce = null)
  21. {
  22. $this->key = $key;
  23. $this->counter = 1;
  24. if (is_null($nonce)) {
  25. $nonce = str_repeat("\0", 12);
  26. }
  27. $this->nonce = str_pad($nonce, 12, "\0", STR_PAD_RIGHT);;
  28. $this->_pad = str_repeat("\0", 4);
  29. }
  30. /**
  31. * @return self
  32. */
  33. public function counterReset()
  34. {
  35. $this->counter = 1;
  36. $this->_pad = str_repeat("\0", 4);
  37. return $this;
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getKey()
  43. {
  44. return $this->key;
  45. }
  46. /**
  47. * @return string
  48. */
  49. public function getCounter()
  50. {
  51. return ParagonIE_Sodium_Core_Util::store32_le($this->counter);
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getNonce()
  57. {
  58. if (!is_string($this->nonce)) {
  59. $this->nonce = str_repeat("\0", 12);
  60. }
  61. if (ParagonIE_Sodium_Core_Util::strlen($this->nonce) !== 12) {
  62. $this->nonce = str_pad($this->nonce, 12, "\0", STR_PAD_RIGHT);
  63. }
  64. return $this->nonce;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getCombinedNonce()
  70. {
  71. return $this->getCounter() .
  72. ParagonIE_Sodium_Core_Util::substr($this->getNonce(), 0, 8);
  73. }
  74. /**
  75. * @return self
  76. */
  77. public function incrementCounter()
  78. {
  79. ++$this->counter;
  80. return $this;
  81. }
  82. /**
  83. * @return bool
  84. */
  85. public function needsRekey()
  86. {
  87. return ($this->counter & 0xffff) === 0;
  88. }
  89. /**
  90. * @param string $newKeyAndNonce
  91. * @return self
  92. */
  93. public function rekey($newKeyAndNonce)
  94. {
  95. $this->key = ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 0, 32);
  96. $this->nonce = str_pad(
  97. ParagonIE_Sodium_Core_Util::substr($newKeyAndNonce, 32),
  98. 12,
  99. "\0",
  100. STR_PAD_RIGHT
  101. );
  102. return $this;
  103. }
  104. /**
  105. * @param string $str
  106. * @return self
  107. */
  108. public function xorNonce($str)
  109. {
  110. $this->nonce = ParagonIE_Sodium_Core_Util::xorStrings(
  111. $this->getNonce(),
  112. str_pad(
  113. ParagonIE_Sodium_Core_Util::substr($str, 0, 8),
  114. 12,
  115. "\0",
  116. STR_PAD_RIGHT
  117. )
  118. );
  119. return $this;
  120. }
  121. /**
  122. * @param string $string
  123. * @return self
  124. */
  125. public static function fromString($string)
  126. {
  127. $state = new ParagonIE_Sodium_Core_SecretStream_State(
  128. ParagonIE_Sodium_Core_Util::substr($string, 0, 32)
  129. );
  130. $state->counter = ParagonIE_Sodium_Core_Util::load_4(
  131. ParagonIE_Sodium_Core_Util::substr($string, 32, 4)
  132. );
  133. $state->nonce = ParagonIE_Sodium_Core_Util::substr($string, 36, 12);
  134. $state->_pad = ParagonIE_Sodium_Core_Util::substr($string, 48, 8);
  135. return $state;
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function toString()
  141. {
  142. return $this->key .
  143. $this->getCounter() .
  144. $this->nonce .
  145. $this->_pad;
  146. }
  147. }