/htdocs/wp-includes/sodium_compat/src/Core/ChaCha20.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 395 lines · 178 code · 28 blank · 189 comment · 7 complexity · e5c615062d72876a6949ded8a3658a62 MD5 · raw file

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