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

https://gitlab.com/VTTE/sitios-vtte · PHP · 400 lines · 170 code · 31 blank · 199 comment · 8 complexity · 971e45164f78d99558da32eb96add883 MD5 · raw file

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