PageRenderTime 258ms CodeModel.GetById 30ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 345 lines | 184 code | 48 blank | 113 comment | 5 complexity | 67e9a562f1663e158e95ebbd3d99b2b7 MD5 | raw file
  1. <?php
  2. if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) {
  3. return;
  4. }
  5. /**
  6. * Class ParagonIE_Sodium_Core32_X25519
  7. */
  8. abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519
  9. {
  10. /**
  11. * Alters the objects passed to this method in place.
  12. *
  13. * @internal You should not use this directly from another application
  14. *
  15. * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f
  16. * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g
  17. * @param int $b
  18. * @return void
  19. * @throws SodiumException
  20. * @throws TypeError
  21. * @psalm-suppress MixedMethodCall
  22. */
  23. public static function fe_cswap(
  24. ParagonIE_Sodium_Core32_Curve25519_Fe $f,
  25. ParagonIE_Sodium_Core32_Curve25519_Fe $g,
  26. $b = 0
  27. ) {
  28. $f0 = (int) $f[0]->toInt();
  29. $f1 = (int) $f[1]->toInt();
  30. $f2 = (int) $f[2]->toInt();
  31. $f3 = (int) $f[3]->toInt();
  32. $f4 = (int) $f[4]->toInt();
  33. $f5 = (int) $f[5]->toInt();
  34. $f6 = (int) $f[6]->toInt();
  35. $f7 = (int) $f[7]->toInt();
  36. $f8 = (int) $f[8]->toInt();
  37. $f9 = (int) $f[9]->toInt();
  38. $g0 = (int) $g[0]->toInt();
  39. $g1 = (int) $g[1]->toInt();
  40. $g2 = (int) $g[2]->toInt();
  41. $g3 = (int) $g[3]->toInt();
  42. $g4 = (int) $g[4]->toInt();
  43. $g5 = (int) $g[5]->toInt();
  44. $g6 = (int) $g[6]->toInt();
  45. $g7 = (int) $g[7]->toInt();
  46. $g8 = (int) $g[8]->toInt();
  47. $g9 = (int) $g[9]->toInt();
  48. $b = -$b;
  49. /** @var int $x0 */
  50. $x0 = ($f0 ^ $g0) & $b;
  51. /** @var int $x1 */
  52. $x1 = ($f1 ^ $g1) & $b;
  53. /** @var int $x2 */
  54. $x2 = ($f2 ^ $g2) & $b;
  55. /** @var int $x3 */
  56. $x3 = ($f3 ^ $g3) & $b;
  57. /** @var int $x4 */
  58. $x4 = ($f4 ^ $g4) & $b;
  59. /** @var int $x5 */
  60. $x5 = ($f5 ^ $g5) & $b;
  61. /** @var int $x6 */
  62. $x6 = ($f6 ^ $g6) & $b;
  63. /** @var int $x7 */
  64. $x7 = ($f7 ^ $g7) & $b;
  65. /** @var int $x8 */
  66. $x8 = ($f8 ^ $g8) & $b;
  67. /** @var int $x9 */
  68. $x9 = ($f9 ^ $g9) & $b;
  69. $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0);
  70. $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1);
  71. $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2);
  72. $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3);
  73. $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4);
  74. $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5);
  75. $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6);
  76. $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7);
  77. $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8);
  78. $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9);
  79. $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0);
  80. $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1);
  81. $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2);
  82. $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3);
  83. $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4);
  84. $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5);
  85. $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6);
  86. $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7);
  87. $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8);
  88. $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9);
  89. }
  90. /**
  91. * @internal You should not use this directly from another application
  92. *
  93. * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f
  94. * @return ParagonIE_Sodium_Core32_Curve25519_Fe
  95. * @throws SodiumException
  96. * @throws TypeError
  97. * @psalm-suppress MixedAssignment
  98. * @psalm-suppress MixedMethodCall
  99. */
  100. public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f)
  101. {
  102. /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */
  103. $h = array();
  104. for ($i = 0; $i < 10; ++$i) {
  105. $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17);
  106. }
  107. $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25);
  108. $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5));
  109. $h[9] = $h[9]->subInt64($carry9->shiftLeft(25));
  110. $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25);
  111. $h[2] = $h[2]->addInt64($carry1);
  112. $h[1] = $h[1]->subInt64($carry1->shiftLeft(25));
  113. $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25);
  114. $h[4] = $h[4]->addInt64($carry3);
  115. $h[3] = $h[3]->subInt64($carry3->shiftLeft(25));
  116. $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25);
  117. $h[6] = $h[6]->addInt64($carry5);
  118. $h[5] = $h[5]->subInt64($carry5->shiftLeft(25));
  119. $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25);
  120. $h[8] = $h[8]->addInt64($carry7);
  121. $h[7] = $h[7]->subInt64($carry7->shiftLeft(25));
  122. $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26);
  123. $h[1] = $h[1]->addInt64($carry0);
  124. $h[0] = $h[0]->subInt64($carry0->shiftLeft(26));
  125. $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26);
  126. $h[3] = $h[3]->addInt64($carry2);
  127. $h[2] = $h[2]->subInt64($carry2->shiftLeft(26));
  128. $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26);
  129. $h[5] = $h[5]->addInt64($carry4);
  130. $h[4] = $h[4]->subInt64($carry4->shiftLeft(26));
  131. $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26);
  132. $h[7] = $h[7]->addInt64($carry6);
  133. $h[6] = $h[6]->subInt64($carry6->shiftLeft(26));
  134. $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26);
  135. $h[9] = $h[9]->addInt64($carry8);
  136. $h[8] = $h[8]->subInt64($carry8->shiftLeft(26));
  137. for ($i = 0; $i < 10; ++$i) {
  138. $h[$i] = $h[$i]->toInt32();
  139. }
  140. /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */
  141. $h2 = $h;
  142. return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2);
  143. }
  144. /**
  145. * @internal You should not use this directly from another application
  146. *
  147. * Inline comments preceded by # are from libsodium's ref10 code.
  148. *
  149. * @param string $n
  150. * @param string $p
  151. * @return string
  152. * @throws SodiumException
  153. * @throws TypeError
  154. */
  155. public static function crypto_scalarmult_curve25519_ref10($n, $p)
  156. {
  157. # for (i = 0;i < 32;++i) e[i] = n[i];
  158. $e = '' . $n;
  159. # e[0] &= 248;
  160. $e[0] = self::intToChr(
  161. self::chrToInt($e[0]) & 248
  162. );
  163. # e[31] &= 127;
  164. # e[31] |= 64;
  165. $e[31] = self::intToChr(
  166. (self::chrToInt($e[31]) & 127) | 64
  167. );
  168. # fe_frombytes(x1,p);
  169. $x1 = self::fe_frombytes($p);
  170. # fe_1(x2);
  171. $x2 = self::fe_1();
  172. # fe_0(z2);
  173. $z2 = self::fe_0();
  174. # fe_copy(x3,x1);
  175. $x3 = self::fe_copy($x1);
  176. # fe_1(z3);
  177. $z3 = self::fe_1();
  178. # swap = 0;
  179. /** @var int $swap */
  180. $swap = 0;
  181. # for (pos = 254;pos >= 0;--pos) {
  182. for ($pos = 254; $pos >= 0; --$pos) {
  183. # b = e[pos / 8] >> (pos & 7);
  184. /** @var int $b */
  185. $b = self::chrToInt(
  186. $e[(int) floor($pos / 8)]
  187. ) >> ($pos & 7);
  188. # b &= 1;
  189. $b &= 1;
  190. # swap ^= b;
  191. $swap ^= $b;
  192. # fe_cswap(x2,x3,swap);
  193. self::fe_cswap($x2, $x3, $swap);
  194. # fe_cswap(z2,z3,swap);
  195. self::fe_cswap($z2, $z3, $swap);
  196. # swap = b;
  197. /** @var int $swap */
  198. $swap = $b;
  199. # fe_sub(tmp0,x3,z3);
  200. $tmp0 = self::fe_sub($x3, $z3);
  201. # fe_sub(tmp1,x2,z2);
  202. $tmp1 = self::fe_sub($x2, $z2);
  203. # fe_add(x2,x2,z2);
  204. $x2 = self::fe_add($x2, $z2);
  205. # fe_add(z2,x3,z3);
  206. $z2 = self::fe_add($x3, $z3);
  207. # fe_mul(z3,tmp0,x2);
  208. $z3 = self::fe_mul($tmp0, $x2);
  209. # fe_mul(z2,z2,tmp1);
  210. $z2 = self::fe_mul($z2, $tmp1);
  211. # fe_sq(tmp0,tmp1);
  212. $tmp0 = self::fe_sq($tmp1);
  213. # fe_sq(tmp1,x2);
  214. $tmp1 = self::fe_sq($x2);
  215. # fe_add(x3,z3,z2);
  216. $x3 = self::fe_add($z3, $z2);
  217. # fe_sub(z2,z3,z2);
  218. $z2 = self::fe_sub($z3, $z2);
  219. # fe_mul(x2,tmp1,tmp0);
  220. $x2 = self::fe_mul($tmp1, $tmp0);
  221. # fe_sub(tmp1,tmp1,tmp0);
  222. $tmp1 = self::fe_sub($tmp1, $tmp0);
  223. # fe_sq(z2,z2);
  224. $z2 = self::fe_sq($z2);
  225. # fe_mul121666(z3,tmp1);
  226. $z3 = self::fe_mul121666($tmp1);
  227. # fe_sq(x3,x3);
  228. $x3 = self::fe_sq($x3);
  229. # fe_add(tmp0,tmp0,z3);
  230. $tmp0 = self::fe_add($tmp0, $z3);
  231. # fe_mul(z3,x1,z2);
  232. $z3 = self::fe_mul($x1, $z2);
  233. # fe_mul(z2,tmp1,tmp0);
  234. $z2 = self::fe_mul($tmp1, $tmp0);
  235. }
  236. # fe_cswap(x2,x3,swap);
  237. self::fe_cswap($x2, $x3, $swap);
  238. # fe_cswap(z2,z3,swap);
  239. self::fe_cswap($z2, $z3, $swap);
  240. # fe_invert(z2,z2);
  241. $z2 = self::fe_invert($z2);
  242. # fe_mul(x2,x2,z2);
  243. $x2 = self::fe_mul($x2, $z2);
  244. # fe_tobytes(q,x2);
  245. return (string) self::fe_tobytes($x2);
  246. }
  247. /**
  248. * @internal You should not use this directly from another application
  249. *
  250. * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY
  251. * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ
  252. * @return ParagonIE_Sodium_Core32_Curve25519_Fe
  253. * @throws SodiumException
  254. * @throws TypeError
  255. */
  256. public static function edwards_to_montgomery(
  257. ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY,
  258. ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ
  259. ) {
  260. $tempX = self::fe_add($edwardsZ, $edwardsY);
  261. $tempZ = self::fe_sub($edwardsZ, $edwardsY);
  262. $tempZ = self::fe_invert($tempZ);
  263. return self::fe_mul($tempX, $tempZ);
  264. }
  265. /**
  266. * @internal You should not use this directly from another application
  267. *
  268. * @param string $n
  269. * @return string
  270. * @throws SodiumException
  271. * @throws TypeError
  272. */
  273. public static function crypto_scalarmult_curve25519_ref10_base($n)
  274. {
  275. # for (i = 0;i < 32;++i) e[i] = n[i];
  276. $e = '' . $n;
  277. # e[0] &= 248;
  278. $e[0] = self::intToChr(
  279. self::chrToInt($e[0]) & 248
  280. );
  281. # e[31] &= 127;
  282. # e[31] |= 64;
  283. $e[31] = self::intToChr(
  284. (self::chrToInt($e[31]) & 127) | 64
  285. );
  286. $A = self::ge_scalarmult_base($e);
  287. if (
  288. !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe)
  289. ||
  290. !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe)
  291. ) {
  292. throw new TypeError('Null points encountered');
  293. }
  294. $pk = self::edwards_to_montgomery($A->Y, $A->Z);
  295. return self::fe_tobytes($pk);
  296. }
  297. }