Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1742 Zeilen
53 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. /* A 64-bit implementation of the NIST P-256 elliptic curve point
  15. * multiplication
  16. *
  17. * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
  18. * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
  19. * work which got its smarts from Daniel J. Bernstein's work on the same. */
  20. #include <openssl/base.h>
  21. #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
  22. #include <openssl/bn.h>
  23. #include <openssl/ec.h>
  24. #include <openssl/err.h>
  25. #include <openssl/mem.h>
  26. #include <string.h>
  27. #include "internal.h"
  28. #include "../internal.h"
  29. typedef uint8_t u8;
  30. typedef uint64_t u64;
  31. typedef int64_t s64;
  32. /* The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
  33. * can serialise an element of this field into 32 bytes. We call this an
  34. * felem_bytearray. */
  35. typedef u8 felem_bytearray[32];
  36. /* The representation of field elements.
  37. * ------------------------------------
  38. *
  39. * We represent field elements with either four 128-bit values, eight 128-bit
  40. * values, or four 64-bit values. The field element represented is:
  41. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
  42. * or:
  43. * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
  44. *
  45. * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
  46. * apart, but are 128-bits wide, the most significant bits of each limb overlap
  47. * with the least significant bits of the next.
  48. *
  49. * A field element with four limbs is an 'felem'. One with eight limbs is a
  50. * 'longfelem'
  51. *
  52. * A field element with four, 64-bit values is called a 'smallfelem'. Small
  53. * values are used as intermediate values before multiplication. */
  54. #define NLIMBS 4
  55. typedef uint128_t limb;
  56. typedef limb felem[NLIMBS];
  57. typedef limb longfelem[NLIMBS * 2];
  58. typedef u64 smallfelem[NLIMBS];
  59. /* This is the value of the prime as four 64-bit words, little-endian. */
  60. static const u64 kPrime[4] = {0xfffffffffffffffful, 0xffffffff, 0,
  61. 0xffffffff00000001ul};
  62. static const u64 bottom63bits = 0x7ffffffffffffffful;
  63. /* bin32_to_felem takes a little-endian byte array and converts it into felem
  64. * form. This assumes that the CPU is little-endian. */
  65. static void bin32_to_felem(felem out, const u8 in[32]) {
  66. out[0] = *((const u64 *)&in[0]);
  67. out[1] = *((const u64 *)&in[8]);
  68. out[2] = *((const u64 *)&in[16]);
  69. out[3] = *((const u64 *)&in[24]);
  70. }
  71. /* smallfelem_to_bin32 takes a smallfelem and serialises into a little endian,
  72. * 32 byte array. This assumes that the CPU is little-endian. */
  73. static void smallfelem_to_bin32(u8 out[32], const smallfelem in) {
  74. *((u64 *)&out[0]) = in[0];
  75. *((u64 *)&out[8]) = in[1];
  76. *((u64 *)&out[16]) = in[2];
  77. *((u64 *)&out[24]) = in[3];
  78. }
  79. /* To preserve endianness when using BN_bn2bin and BN_bin2bn. */
  80. static void flip_endian(u8 *out, const u8 *in, size_t len) {
  81. for (size_t i = 0; i < len; ++i) {
  82. out[i] = in[len - 1 - i];
  83. }
  84. }
  85. /* BN_to_felem converts an OpenSSL BIGNUM into an felem. */
  86. static int BN_to_felem(felem out, const BIGNUM *bn) {
  87. if (BN_is_negative(bn)) {
  88. OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
  89. return 0;
  90. }
  91. felem_bytearray b_out;
  92. /* BN_bn2bin eats leading zeroes */
  93. memset(b_out, 0, sizeof(b_out));
  94. size_t num_bytes = BN_num_bytes(bn);
  95. if (num_bytes > sizeof(b_out)) {
  96. OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
  97. return 0;
  98. }
  99. felem_bytearray b_in;
  100. num_bytes = BN_bn2bin(bn, b_in);
  101. flip_endian(b_out, b_in, num_bytes);
  102. bin32_to_felem(out, b_out);
  103. return 1;
  104. }
  105. /* felem_to_BN converts an felem into an OpenSSL BIGNUM. */
  106. static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in) {
  107. felem_bytearray b_in, b_out;
  108. smallfelem_to_bin32(b_in, in);
  109. flip_endian(b_out, b_in, sizeof(b_out));
  110. return BN_bin2bn(b_out, sizeof(b_out), out);
  111. }
  112. /* Field operations. */
  113. static void felem_assign(felem out, const felem in) {
  114. out[0] = in[0];
  115. out[1] = in[1];
  116. out[2] = in[2];
  117. out[3] = in[3];
  118. }
  119. /* felem_sum sets out = out + in. */
  120. static void felem_sum(felem out, const felem in) {
  121. out[0] += in[0];
  122. out[1] += in[1];
  123. out[2] += in[2];
  124. out[3] += in[3];
  125. }
  126. /* felem_small_sum sets out = out + in. */
  127. static void felem_small_sum(felem out, const smallfelem in) {
  128. out[0] += in[0];
  129. out[1] += in[1];
  130. out[2] += in[2];
  131. out[3] += in[3];
  132. }
  133. /* felem_scalar sets out = out * scalar */
  134. static void felem_scalar(felem out, const u64 scalar) {
  135. out[0] *= scalar;
  136. out[1] *= scalar;
  137. out[2] *= scalar;
  138. out[3] *= scalar;
  139. }
  140. /* longfelem_scalar sets out = out * scalar */
  141. static void longfelem_scalar(longfelem out, const u64 scalar) {
  142. out[0] *= scalar;
  143. out[1] *= scalar;
  144. out[2] *= scalar;
  145. out[3] *= scalar;
  146. out[4] *= scalar;
  147. out[5] *= scalar;
  148. out[6] *= scalar;
  149. out[7] *= scalar;
  150. }
  151. #define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
  152. #define two105 (((limb)1) << 105)
  153. #define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
  154. /* zero105 is 0 mod p */
  155. static const felem zero105 = {two105m41m9, two105, two105m41p9, two105m41p9};
  156. /* smallfelem_neg sets |out| to |-small|
  157. * On exit:
  158. * out[i] < out[i] + 2^105 */
  159. static void smallfelem_neg(felem out, const smallfelem small) {
  160. /* In order to prevent underflow, we subtract from 0 mod p. */
  161. out[0] = zero105[0] - small[0];
  162. out[1] = zero105[1] - small[1];
  163. out[2] = zero105[2] - small[2];
  164. out[3] = zero105[3] - small[3];
  165. }
  166. /* felem_diff subtracts |in| from |out|
  167. * On entry:
  168. * in[i] < 2^104
  169. * On exit:
  170. * out[i] < out[i] + 2^105. */
  171. static void felem_diff(felem out, const felem in) {
  172. /* In order to prevent underflow, we add 0 mod p before subtracting. */
  173. out[0] += zero105[0];
  174. out[1] += zero105[1];
  175. out[2] += zero105[2];
  176. out[3] += zero105[3];
  177. out[0] -= in[0];
  178. out[1] -= in[1];
  179. out[2] -= in[2];
  180. out[3] -= in[3];
  181. }
  182. #define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
  183. #define two107 (((limb)1) << 107)
  184. #define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
  185. /* zero107 is 0 mod p */
  186. static const felem zero107 = {two107m43m11, two107, two107m43p11, two107m43p11};
  187. /* An alternative felem_diff for larger inputs |in|
  188. * felem_diff_zero107 subtracts |in| from |out|
  189. * On entry:
  190. * in[i] < 2^106
  191. * On exit:
  192. * out[i] < out[i] + 2^107. */
  193. static void felem_diff_zero107(felem out, const felem in) {
  194. /* In order to prevent underflow, we add 0 mod p before subtracting. */
  195. out[0] += zero107[0];
  196. out[1] += zero107[1];
  197. out[2] += zero107[2];
  198. out[3] += zero107[3];
  199. out[0] -= in[0];
  200. out[1] -= in[1];
  201. out[2] -= in[2];
  202. out[3] -= in[3];
  203. }
  204. /* longfelem_diff subtracts |in| from |out|
  205. * On entry:
  206. * in[i] < 7*2^67
  207. * On exit:
  208. * out[i] < out[i] + 2^70 + 2^40. */
  209. static void longfelem_diff(longfelem out, const longfelem in) {
  210. static const limb two70m8p6 =
  211. (((limb)1) << 70) - (((limb)1) << 8) + (((limb)1) << 6);
  212. static const limb two70p40 = (((limb)1) << 70) + (((limb)1) << 40);
  213. static const limb two70 = (((limb)1) << 70);
  214. static const limb two70m40m38p6 = (((limb)1) << 70) - (((limb)1) << 40) -
  215. (((limb)1) << 38) + (((limb)1) << 6);
  216. static const limb two70m6 = (((limb)1) << 70) - (((limb)1) << 6);
  217. /* add 0 mod p to avoid underflow */
  218. out[0] += two70m8p6;
  219. out[1] += two70p40;
  220. out[2] += two70;
  221. out[3] += two70m40m38p6;
  222. out[4] += two70m6;
  223. out[5] += two70m6;
  224. out[6] += two70m6;
  225. out[7] += two70m6;
  226. /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
  227. out[0] -= in[0];
  228. out[1] -= in[1];
  229. out[2] -= in[2];
  230. out[3] -= in[3];
  231. out[4] -= in[4];
  232. out[5] -= in[5];
  233. out[6] -= in[6];
  234. out[7] -= in[7];
  235. }
  236. #define two64m0 (((limb)1) << 64) - 1
  237. #define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
  238. #define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
  239. #define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
  240. /* zero110 is 0 mod p. */
  241. static const felem zero110 = {two64m0, two110p32m0, two64m46, two64m32};
  242. /* felem_shrink converts an felem into a smallfelem. The result isn't quite
  243. * minimal as the value may be greater than p.
  244. *
  245. * On entry:
  246. * in[i] < 2^109
  247. * On exit:
  248. * out[i] < 2^64. */
  249. static void felem_shrink(smallfelem out, const felem in) {
  250. felem tmp;
  251. u64 a, b, mask;
  252. s64 high, low;
  253. static const u64 kPrime3Test = 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
  254. /* Carry 2->3 */
  255. tmp[3] = zero110[3] + in[3] + ((u64)(in[2] >> 64));
  256. /* tmp[3] < 2^110 */
  257. tmp[2] = zero110[2] + (u64)in[2];
  258. tmp[0] = zero110[0] + in[0];
  259. tmp[1] = zero110[1] + in[1];
  260. /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
  261. /* We perform two partial reductions where we eliminate the high-word of
  262. * tmp[3]. We don't update the other words till the end. */
  263. a = tmp[3] >> 64; /* a < 2^46 */
  264. tmp[3] = (u64)tmp[3];
  265. tmp[3] -= a;
  266. tmp[3] += ((limb)a) << 32;
  267. /* tmp[3] < 2^79 */
  268. b = a;
  269. a = tmp[3] >> 64; /* a < 2^15 */
  270. b += a; /* b < 2^46 + 2^15 < 2^47 */
  271. tmp[3] = (u64)tmp[3];
  272. tmp[3] -= a;
  273. tmp[3] += ((limb)a) << 32;
  274. /* tmp[3] < 2^64 + 2^47 */
  275. /* This adjusts the other two words to complete the two partial
  276. * reductions. */
  277. tmp[0] += b;
  278. tmp[1] -= (((limb)b) << 32);
  279. /* In order to make space in tmp[3] for the carry from 2 -> 3, we
  280. * conditionally subtract kPrime if tmp[3] is large enough. */
  281. high = tmp[3] >> 64;
  282. /* As tmp[3] < 2^65, high is either 1 or 0 */
  283. high = ~(high - 1);
  284. /* high is:
  285. * all ones if the high word of tmp[3] is 1
  286. * all zeros if the high word of tmp[3] if 0 */
  287. low = tmp[3];
  288. mask = low >> 63;
  289. /* mask is:
  290. * all ones if the MSB of low is 1
  291. * all zeros if the MSB of low if 0 */
  292. low &= bottom63bits;
  293. low -= kPrime3Test;
  294. /* if low was greater than kPrime3Test then the MSB is zero */
  295. low = ~low;
  296. low >>= 63;
  297. /* low is:
  298. * all ones if low was > kPrime3Test
  299. * all zeros if low was <= kPrime3Test */
  300. mask = (mask & low) | high;
  301. tmp[0] -= mask & kPrime[0];
  302. tmp[1] -= mask & kPrime[1];
  303. /* kPrime[2] is zero, so omitted */
  304. tmp[3] -= mask & kPrime[3];
  305. /* tmp[3] < 2**64 - 2**32 + 1 */
  306. tmp[1] += ((u64)(tmp[0] >> 64));
  307. tmp[0] = (u64)tmp[0];
  308. tmp[2] += ((u64)(tmp[1] >> 64));
  309. tmp[1] = (u64)tmp[1];
  310. tmp[3] += ((u64)(tmp[2] >> 64));
  311. tmp[2] = (u64)tmp[2];
  312. /* tmp[i] < 2^64 */
  313. out[0] = tmp[0];
  314. out[1] = tmp[1];
  315. out[2] = tmp[2];
  316. out[3] = tmp[3];
  317. }
  318. /* smallfelem_expand converts a smallfelem to an felem */
  319. static void smallfelem_expand(felem out, const smallfelem in) {
  320. out[0] = in[0];
  321. out[1] = in[1];
  322. out[2] = in[2];
  323. out[3] = in[3];
  324. }
  325. /* smallfelem_square sets |out| = |small|^2
  326. * On entry:
  327. * small[i] < 2^64
  328. * On exit:
  329. * out[i] < 7 * 2^64 < 2^67 */
  330. static void smallfelem_square(longfelem out, const smallfelem small) {
  331. limb a;
  332. u64 high, low;
  333. a = ((uint128_t)small[0]) * small[0];
  334. low = a;
  335. high = a >> 64;
  336. out[0] = low;
  337. out[1] = high;
  338. a = ((uint128_t)small[0]) * small[1];
  339. low = a;
  340. high = a >> 64;
  341. out[1] += low;
  342. out[1] += low;
  343. out[2] = high;
  344. a = ((uint128_t)small[0]) * small[2];
  345. low = a;
  346. high = a >> 64;
  347. out[2] += low;
  348. out[2] *= 2;
  349. out[3] = high;
  350. a = ((uint128_t)small[0]) * small[3];
  351. low = a;
  352. high = a >> 64;
  353. out[3] += low;
  354. out[4] = high;
  355. a = ((uint128_t)small[1]) * small[2];
  356. low = a;
  357. high = a >> 64;
  358. out[3] += low;
  359. out[3] *= 2;
  360. out[4] += high;
  361. a = ((uint128_t)small[1]) * small[1];
  362. low = a;
  363. high = a >> 64;
  364. out[2] += low;
  365. out[3] += high;
  366. a = ((uint128_t)small[1]) * small[3];
  367. low = a;
  368. high = a >> 64;
  369. out[4] += low;
  370. out[4] *= 2;
  371. out[5] = high;
  372. a = ((uint128_t)small[2]) * small[3];
  373. low = a;
  374. high = a >> 64;
  375. out[5] += low;
  376. out[5] *= 2;
  377. out[6] = high;
  378. out[6] += high;
  379. a = ((uint128_t)small[2]) * small[2];
  380. low = a;
  381. high = a >> 64;
  382. out[4] += low;
  383. out[5] += high;
  384. a = ((uint128_t)small[3]) * small[3];
  385. low = a;
  386. high = a >> 64;
  387. out[6] += low;
  388. out[7] = high;
  389. }
  390. /*felem_square sets |out| = |in|^2
  391. * On entry:
  392. * in[i] < 2^109
  393. * On exit:
  394. * out[i] < 7 * 2^64 < 2^67. */
  395. static void felem_square(longfelem out, const felem in) {
  396. u64 small[4];
  397. felem_shrink(small, in);
  398. smallfelem_square(out, small);
  399. }
  400. /* smallfelem_mul sets |out| = |small1| * |small2|
  401. * On entry:
  402. * small1[i] < 2^64
  403. * small2[i] < 2^64
  404. * On exit:
  405. * out[i] < 7 * 2^64 < 2^67. */
  406. static void smallfelem_mul(longfelem out, const smallfelem small1,
  407. const smallfelem small2) {
  408. limb a;
  409. u64 high, low;
  410. a = ((uint128_t)small1[0]) * small2[0];
  411. low = a;
  412. high = a >> 64;
  413. out[0] = low;
  414. out[1] = high;
  415. a = ((uint128_t)small1[0]) * small2[1];
  416. low = a;
  417. high = a >> 64;
  418. out[1] += low;
  419. out[2] = high;
  420. a = ((uint128_t)small1[1]) * small2[0];
  421. low = a;
  422. high = a >> 64;
  423. out[1] += low;
  424. out[2] += high;
  425. a = ((uint128_t)small1[0]) * small2[2];
  426. low = a;
  427. high = a >> 64;
  428. out[2] += low;
  429. out[3] = high;
  430. a = ((uint128_t)small1[1]) * small2[1];
  431. low = a;
  432. high = a >> 64;
  433. out[2] += low;
  434. out[3] += high;
  435. a = ((uint128_t)small1[2]) * small2[0];
  436. low = a;
  437. high = a >> 64;
  438. out[2] += low;
  439. out[3] += high;
  440. a = ((uint128_t)small1[0]) * small2[3];
  441. low = a;
  442. high = a >> 64;
  443. out[3] += low;
  444. out[4] = high;
  445. a = ((uint128_t)small1[1]) * small2[2];
  446. low = a;
  447. high = a >> 64;
  448. out[3] += low;
  449. out[4] += high;
  450. a = ((uint128_t)small1[2]) * small2[1];
  451. low = a;
  452. high = a >> 64;
  453. out[3] += low;
  454. out[4] += high;
  455. a = ((uint128_t)small1[3]) * small2[0];
  456. low = a;
  457. high = a >> 64;
  458. out[3] += low;
  459. out[4] += high;
  460. a = ((uint128_t)small1[1]) * small2[3];
  461. low = a;
  462. high = a >> 64;
  463. out[4] += low;
  464. out[5] = high;
  465. a = ((uint128_t)small1[2]) * small2[2];
  466. low = a;
  467. high = a >> 64;
  468. out[4] += low;
  469. out[5] += high;
  470. a = ((uint128_t)small1[3]) * small2[1];
  471. low = a;
  472. high = a >> 64;
  473. out[4] += low;
  474. out[5] += high;
  475. a = ((uint128_t)small1[2]) * small2[3];
  476. low = a;
  477. high = a >> 64;
  478. out[5] += low;
  479. out[6] = high;
  480. a = ((uint128_t)small1[3]) * small2[2];
  481. low = a;
  482. high = a >> 64;
  483. out[5] += low;
  484. out[6] += high;
  485. a = ((uint128_t)small1[3]) * small2[3];
  486. low = a;
  487. high = a >> 64;
  488. out[6] += low;
  489. out[7] = high;
  490. }
  491. /* felem_mul sets |out| = |in1| * |in2|
  492. * On entry:
  493. * in1[i] < 2^109
  494. * in2[i] < 2^109
  495. * On exit:
  496. * out[i] < 7 * 2^64 < 2^67 */
  497. static void felem_mul(longfelem out, const felem in1, const felem in2) {
  498. smallfelem small1, small2;
  499. felem_shrink(small1, in1);
  500. felem_shrink(small2, in2);
  501. smallfelem_mul(out, small1, small2);
  502. }
  503. /* felem_small_mul sets |out| = |small1| * |in2|
  504. * On entry:
  505. * small1[i] < 2^64
  506. * in2[i] < 2^109
  507. * On exit:
  508. * out[i] < 7 * 2^64 < 2^67 */
  509. static void felem_small_mul(longfelem out, const smallfelem small1,
  510. const felem in2) {
  511. smallfelem small2;
  512. felem_shrink(small2, in2);
  513. smallfelem_mul(out, small1, small2);
  514. }
  515. #define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
  516. #define two100 (((limb)1) << 100)
  517. #define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
  518. /* zero100 is 0 mod p */
  519. static const felem zero100 = {two100m36m4, two100, two100m36p4, two100m36p4};
  520. /* Internal function for the different flavours of felem_reduce.
  521. * felem_reduce_ reduces the higher coefficients in[4]-in[7].
  522. * On entry:
  523. * out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
  524. * out[1] >= in[7] + 2^32*in[4]
  525. * out[2] >= in[5] + 2^32*in[5]
  526. * out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
  527. * On exit:
  528. * out[0] <= out[0] + in[4] + 2^32*in[5]
  529. * out[1] <= out[1] + in[5] + 2^33*in[6]
  530. * out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
  531. * out[3] <= out[3] + 2^32*in[4] + 3*in[7] */
  532. static void felem_reduce_(felem out, const longfelem in) {
  533. int128_t c;
  534. /* combine common terms from below */
  535. c = in[4] + (in[5] << 32);
  536. out[0] += c;
  537. out[3] -= c;
  538. c = in[5] - in[7];
  539. out[1] += c;
  540. out[2] -= c;
  541. /* the remaining terms */
  542. /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
  543. out[1] -= (in[4] << 32);
  544. out[3] += (in[4] << 32);
  545. /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
  546. out[2] -= (in[5] << 32);
  547. /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
  548. out[0] -= in[6];
  549. out[0] -= (in[6] << 32);
  550. out[1] += (in[6] << 33);
  551. out[2] += (in[6] * 2);
  552. out[3] -= (in[6] << 32);
  553. /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
  554. out[0] -= in[7];
  555. out[0] -= (in[7] << 32);
  556. out[2] += (in[7] << 33);
  557. out[3] += (in[7] * 3);
  558. }
  559. /* felem_reduce converts a longfelem into an felem.
  560. * To be called directly after felem_square or felem_mul.
  561. * On entry:
  562. * in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
  563. * in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
  564. * On exit:
  565. * out[i] < 2^101 */
  566. static void felem_reduce(felem out, const longfelem in) {
  567. out[0] = zero100[0] + in[0];
  568. out[1] = zero100[1] + in[1];
  569. out[2] = zero100[2] + in[2];
  570. out[3] = zero100[3] + in[3];
  571. felem_reduce_(out, in);
  572. /* out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
  573. * out[1] > 2^100 - 2^64 - 7*2^96 > 0
  574. * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
  575. * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
  576. *
  577. * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
  578. * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
  579. * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
  580. * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101 */
  581. }
  582. /* felem_reduce_zero105 converts a larger longfelem into an felem.
  583. * On entry:
  584. * in[0] < 2^71
  585. * On exit:
  586. * out[i] < 2^106 */
  587. static void felem_reduce_zero105(felem out, const longfelem in) {
  588. out[0] = zero105[0] + in[0];
  589. out[1] = zero105[1] + in[1];
  590. out[2] = zero105[2] + in[2];
  591. out[3] = zero105[3] + in[3];
  592. felem_reduce_(out, in);
  593. /* out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
  594. * out[1] > 2^105 - 2^71 - 2^103 > 0
  595. * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
  596. * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
  597. *
  598. * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  599. * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
  600. * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
  601. * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106 */
  602. }
  603. /* subtract_u64 sets *result = *result - v and *carry to one if the
  604. * subtraction underflowed. */
  605. static void subtract_u64(u64 *result, u64 *carry, u64 v) {
  606. uint128_t r = *result;
  607. r -= v;
  608. *carry = (r >> 64) & 1;
  609. *result = (u64)r;
  610. }
  611. /* felem_contract converts |in| to its unique, minimal representation. On
  612. * entry: in[i] < 2^109. */
  613. static void felem_contract(smallfelem out, const felem in) {
  614. u64 all_equal_so_far = 0, result = 0;
  615. felem_shrink(out, in);
  616. /* small is minimal except that the value might be > p */
  617. all_equal_so_far--;
  618. /* We are doing a constant time test if out >= kPrime. We need to compare
  619. * each u64, from most-significant to least significant. For each one, if
  620. * all words so far have been equal (m is all ones) then a non-equal
  621. * result is the answer. Otherwise we continue. */
  622. for (size_t i = 3; i < 4; i--) {
  623. u64 equal;
  624. uint128_t a = ((uint128_t)kPrime[i]) - out[i];
  625. /* if out[i] > kPrime[i] then a will underflow and the high 64-bits
  626. * will all be set. */
  627. result |= all_equal_so_far & ((u64)(a >> 64));
  628. /* if kPrime[i] == out[i] then |equal| will be all zeros and the
  629. * decrement will make it all ones. */
  630. equal = kPrime[i] ^ out[i];
  631. equal--;
  632. equal &= equal << 32;
  633. equal &= equal << 16;
  634. equal &= equal << 8;
  635. equal &= equal << 4;
  636. equal &= equal << 2;
  637. equal &= equal << 1;
  638. equal = ((s64)equal) >> 63;
  639. all_equal_so_far &= equal;
  640. }
  641. /* if all_equal_so_far is still all ones then the two values are equal
  642. * and so out >= kPrime is true. */
  643. result |= all_equal_so_far;
  644. /* if out >= kPrime then we subtract kPrime. */
  645. u64 carry;
  646. subtract_u64(&out[0], &carry, result & kPrime[0]);
  647. subtract_u64(&out[1], &carry, carry);
  648. subtract_u64(&out[2], &carry, carry);
  649. subtract_u64(&out[3], &carry, carry);
  650. subtract_u64(&out[1], &carry, result & kPrime[1]);
  651. subtract_u64(&out[2], &carry, carry);
  652. subtract_u64(&out[3], &carry, carry);
  653. subtract_u64(&out[2], &carry, result & kPrime[2]);
  654. subtract_u64(&out[3], &carry, carry);
  655. subtract_u64(&out[3], &carry, result & kPrime[3]);
  656. }
  657. /* felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  658. * otherwise.
  659. * On entry:
  660. * small[i] < 2^64 */
  661. static limb smallfelem_is_zero(const smallfelem small) {
  662. limb result;
  663. u64 is_p;
  664. u64 is_zero = small[0] | small[1] | small[2] | small[3];
  665. is_zero--;
  666. is_zero &= is_zero << 32;
  667. is_zero &= is_zero << 16;
  668. is_zero &= is_zero << 8;
  669. is_zero &= is_zero << 4;
  670. is_zero &= is_zero << 2;
  671. is_zero &= is_zero << 1;
  672. is_zero = ((s64)is_zero) >> 63;
  673. is_p = (small[0] ^ kPrime[0]) | (small[1] ^ kPrime[1]) |
  674. (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
  675. is_p--;
  676. is_p &= is_p << 32;
  677. is_p &= is_p << 16;
  678. is_p &= is_p << 8;
  679. is_p &= is_p << 4;
  680. is_p &= is_p << 2;
  681. is_p &= is_p << 1;
  682. is_p = ((s64)is_p) >> 63;
  683. is_zero |= is_p;
  684. result = is_zero;
  685. result |= ((limb)is_zero) << 64;
  686. return result;
  687. }
  688. /* felem_inv calculates |out| = |in|^{-1}
  689. *
  690. * Based on Fermat's Little Theorem:
  691. * a^p = a (mod p)
  692. * a^{p-1} = 1 (mod p)
  693. * a^{p-2} = a^{-1} (mod p) */
  694. static void felem_inv(felem out, const felem in) {
  695. felem ftmp, ftmp2;
  696. /* each e_I will hold |in|^{2^I - 1} */
  697. felem e2, e4, e8, e16, e32, e64;
  698. longfelem tmp;
  699. felem_square(tmp, in);
  700. felem_reduce(ftmp, tmp); /* 2^1 */
  701. felem_mul(tmp, in, ftmp);
  702. felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
  703. felem_assign(e2, ftmp);
  704. felem_square(tmp, ftmp);
  705. felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
  706. felem_square(tmp, ftmp);
  707. felem_reduce(ftmp, tmp); /* 2^4 - 2^2 */
  708. felem_mul(tmp, ftmp, e2);
  709. felem_reduce(ftmp, tmp); /* 2^4 - 2^0 */
  710. felem_assign(e4, ftmp);
  711. felem_square(tmp, ftmp);
  712. felem_reduce(ftmp, tmp); /* 2^5 - 2^1 */
  713. felem_square(tmp, ftmp);
  714. felem_reduce(ftmp, tmp); /* 2^6 - 2^2 */
  715. felem_square(tmp, ftmp);
  716. felem_reduce(ftmp, tmp); /* 2^7 - 2^3 */
  717. felem_square(tmp, ftmp);
  718. felem_reduce(ftmp, tmp); /* 2^8 - 2^4 */
  719. felem_mul(tmp, ftmp, e4);
  720. felem_reduce(ftmp, tmp); /* 2^8 - 2^0 */
  721. felem_assign(e8, ftmp);
  722. for (size_t i = 0; i < 8; i++) {
  723. felem_square(tmp, ftmp);
  724. felem_reduce(ftmp, tmp);
  725. } /* 2^16 - 2^8 */
  726. felem_mul(tmp, ftmp, e8);
  727. felem_reduce(ftmp, tmp); /* 2^16 - 2^0 */
  728. felem_assign(e16, ftmp);
  729. for (size_t i = 0; i < 16; i++) {
  730. felem_square(tmp, ftmp);
  731. felem_reduce(ftmp, tmp);
  732. } /* 2^32 - 2^16 */
  733. felem_mul(tmp, ftmp, e16);
  734. felem_reduce(ftmp, tmp); /* 2^32 - 2^0 */
  735. felem_assign(e32, ftmp);
  736. for (size_t i = 0; i < 32; i++) {
  737. felem_square(tmp, ftmp);
  738. felem_reduce(ftmp, tmp);
  739. } /* 2^64 - 2^32 */
  740. felem_assign(e64, ftmp);
  741. felem_mul(tmp, ftmp, in);
  742. felem_reduce(ftmp, tmp); /* 2^64 - 2^32 + 2^0 */
  743. for (size_t i = 0; i < 192; i++) {
  744. felem_square(tmp, ftmp);
  745. felem_reduce(ftmp, tmp);
  746. } /* 2^256 - 2^224 + 2^192 */
  747. felem_mul(tmp, e64, e32);
  748. felem_reduce(ftmp2, tmp); /* 2^64 - 2^0 */
  749. for (size_t i = 0; i < 16; i++) {
  750. felem_square(tmp, ftmp2);
  751. felem_reduce(ftmp2, tmp);
  752. } /* 2^80 - 2^16 */
  753. felem_mul(tmp, ftmp2, e16);
  754. felem_reduce(ftmp2, tmp); /* 2^80 - 2^0 */
  755. for (size_t i = 0; i < 8; i++) {
  756. felem_square(tmp, ftmp2);
  757. felem_reduce(ftmp2, tmp);
  758. } /* 2^88 - 2^8 */
  759. felem_mul(tmp, ftmp2, e8);
  760. felem_reduce(ftmp2, tmp); /* 2^88 - 2^0 */
  761. for (size_t i = 0; i < 4; i++) {
  762. felem_square(tmp, ftmp2);
  763. felem_reduce(ftmp2, tmp);
  764. } /* 2^92 - 2^4 */
  765. felem_mul(tmp, ftmp2, e4);
  766. felem_reduce(ftmp2, tmp); /* 2^92 - 2^0 */
  767. felem_square(tmp, ftmp2);
  768. felem_reduce(ftmp2, tmp); /* 2^93 - 2^1 */
  769. felem_square(tmp, ftmp2);
  770. felem_reduce(ftmp2, tmp); /* 2^94 - 2^2 */
  771. felem_mul(tmp, ftmp2, e2);
  772. felem_reduce(ftmp2, tmp); /* 2^94 - 2^0 */
  773. felem_square(tmp, ftmp2);
  774. felem_reduce(ftmp2, tmp); /* 2^95 - 2^1 */
  775. felem_square(tmp, ftmp2);
  776. felem_reduce(ftmp2, tmp); /* 2^96 - 2^2 */
  777. felem_mul(tmp, ftmp2, in);
  778. felem_reduce(ftmp2, tmp); /* 2^96 - 3 */
  779. felem_mul(tmp, ftmp2, ftmp);
  780. felem_reduce(out, tmp); /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
  781. }
  782. /* Group operations
  783. * ----------------
  784. *
  785. * Building on top of the field operations we have the operations on the
  786. * elliptic curve group itself. Points on the curve are represented in Jacobian
  787. * coordinates. */
  788. /* point_double calculates 2*(x_in, y_in, z_in)
  789. *
  790. * The method is taken from:
  791. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  792. *
  793. * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  794. * while x_out == y_in is not (maybe this works, but it's not tested). */
  795. static void point_double(felem x_out, felem y_out, felem z_out,
  796. const felem x_in, const felem y_in, const felem z_in) {
  797. longfelem tmp, tmp2;
  798. felem delta, gamma, beta, alpha, ftmp, ftmp2;
  799. smallfelem small1, small2;
  800. felem_assign(ftmp, x_in);
  801. /* ftmp[i] < 2^106 */
  802. felem_assign(ftmp2, x_in);
  803. /* ftmp2[i] < 2^106 */
  804. /* delta = z^2 */
  805. felem_square(tmp, z_in);
  806. felem_reduce(delta, tmp);
  807. /* delta[i] < 2^101 */
  808. /* gamma = y^2 */
  809. felem_square(tmp, y_in);
  810. felem_reduce(gamma, tmp);
  811. /* gamma[i] < 2^101 */
  812. felem_shrink(small1, gamma);
  813. /* beta = x*gamma */
  814. felem_small_mul(tmp, small1, x_in);
  815. felem_reduce(beta, tmp);
  816. /* beta[i] < 2^101 */
  817. /* alpha = 3*(x-delta)*(x+delta) */
  818. felem_diff(ftmp, delta);
  819. /* ftmp[i] < 2^105 + 2^106 < 2^107 */
  820. felem_sum(ftmp2, delta);
  821. /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
  822. felem_scalar(ftmp2, 3);
  823. /* ftmp2[i] < 3 * 2^107 < 2^109 */
  824. felem_mul(tmp, ftmp, ftmp2);
  825. felem_reduce(alpha, tmp);
  826. /* alpha[i] < 2^101 */
  827. felem_shrink(small2, alpha);
  828. /* x' = alpha^2 - 8*beta */
  829. smallfelem_square(tmp, small2);
  830. felem_reduce(x_out, tmp);
  831. felem_assign(ftmp, beta);
  832. felem_scalar(ftmp, 8);
  833. /* ftmp[i] < 8 * 2^101 = 2^104 */
  834. felem_diff(x_out, ftmp);
  835. /* x_out[i] < 2^105 + 2^101 < 2^106 */
  836. /* z' = (y + z)^2 - gamma - delta */
  837. felem_sum(delta, gamma);
  838. /* delta[i] < 2^101 + 2^101 = 2^102 */
  839. felem_assign(ftmp, y_in);
  840. felem_sum(ftmp, z_in);
  841. /* ftmp[i] < 2^106 + 2^106 = 2^107 */
  842. felem_square(tmp, ftmp);
  843. felem_reduce(z_out, tmp);
  844. felem_diff(z_out, delta);
  845. /* z_out[i] < 2^105 + 2^101 < 2^106 */
  846. /* y' = alpha*(4*beta - x') - 8*gamma^2 */
  847. felem_scalar(beta, 4);
  848. /* beta[i] < 4 * 2^101 = 2^103 */
  849. felem_diff_zero107(beta, x_out);
  850. /* beta[i] < 2^107 + 2^103 < 2^108 */
  851. felem_small_mul(tmp, small2, beta);
  852. /* tmp[i] < 7 * 2^64 < 2^67 */
  853. smallfelem_square(tmp2, small1);
  854. /* tmp2[i] < 7 * 2^64 */
  855. longfelem_scalar(tmp2, 8);
  856. /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
  857. longfelem_diff(tmp, tmp2);
  858. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  859. felem_reduce_zero105(y_out, tmp);
  860. /* y_out[i] < 2^106 */
  861. }
  862. /* point_double_small is the same as point_double, except that it operates on
  863. * smallfelems. */
  864. static void point_double_small(smallfelem x_out, smallfelem y_out,
  865. smallfelem z_out, const smallfelem x_in,
  866. const smallfelem y_in, const smallfelem z_in) {
  867. felem felem_x_out, felem_y_out, felem_z_out;
  868. felem felem_x_in, felem_y_in, felem_z_in;
  869. smallfelem_expand(felem_x_in, x_in);
  870. smallfelem_expand(felem_y_in, y_in);
  871. smallfelem_expand(felem_z_in, z_in);
  872. point_double(felem_x_out, felem_y_out, felem_z_out, felem_x_in, felem_y_in,
  873. felem_z_in);
  874. felem_shrink(x_out, felem_x_out);
  875. felem_shrink(y_out, felem_y_out);
  876. felem_shrink(z_out, felem_z_out);
  877. }
  878. /* copy_conditional copies in to out iff mask is all ones. */
  879. static void copy_conditional(felem out, const felem in, limb mask) {
  880. for (size_t i = 0; i < NLIMBS; ++i) {
  881. const limb tmp = mask & (in[i] ^ out[i]);
  882. out[i] ^= tmp;
  883. }
  884. }
  885. /* copy_small_conditional copies in to out iff mask is all ones. */
  886. static void copy_small_conditional(felem out, const smallfelem in, limb mask) {
  887. const u64 mask64 = mask;
  888. for (size_t i = 0; i < NLIMBS; ++i) {
  889. out[i] = ((limb)(in[i] & mask64)) | (out[i] & ~mask);
  890. }
  891. }
  892. /* point_add calcuates (x1, y1, z1) + (x2, y2, z2)
  893. *
  894. * The method is taken from:
  895. * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  896. * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  897. *
  898. * This function includes a branch for checking whether the two input points
  899. * are equal, (while not equal to the point at infinity). This case never
  900. * happens during single point multiplication, so there is no timing leak for
  901. * ECDH or ECDSA signing. */
  902. static void point_add(felem x3, felem y3, felem z3, const felem x1,
  903. const felem y1, const felem z1, const int mixed,
  904. const smallfelem x2, const smallfelem y2,
  905. const smallfelem z2) {
  906. felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
  907. longfelem tmp, tmp2;
  908. smallfelem small1, small2, small3, small4, small5;
  909. limb x_equal, y_equal, z1_is_zero, z2_is_zero;
  910. felem_shrink(small3, z1);
  911. z1_is_zero = smallfelem_is_zero(small3);
  912. z2_is_zero = smallfelem_is_zero(z2);
  913. /* ftmp = z1z1 = z1**2 */
  914. smallfelem_square(tmp, small3);
  915. felem_reduce(ftmp, tmp);
  916. /* ftmp[i] < 2^101 */
  917. felem_shrink(small1, ftmp);
  918. if (!mixed) {
  919. /* ftmp2 = z2z2 = z2**2 */
  920. smallfelem_square(tmp, z2);
  921. felem_reduce(ftmp2, tmp);
  922. /* ftmp2[i] < 2^101 */
  923. felem_shrink(small2, ftmp2);
  924. felem_shrink(small5, x1);
  925. /* u1 = ftmp3 = x1*z2z2 */
  926. smallfelem_mul(tmp, small5, small2);
  927. felem_reduce(ftmp3, tmp);
  928. /* ftmp3[i] < 2^101 */
  929. /* ftmp5 = z1 + z2 */
  930. felem_assign(ftmp5, z1);
  931. felem_small_sum(ftmp5, z2);
  932. /* ftmp5[i] < 2^107 */
  933. /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
  934. felem_square(tmp, ftmp5);
  935. felem_reduce(ftmp5, tmp);
  936. /* ftmp2 = z2z2 + z1z1 */
  937. felem_sum(ftmp2, ftmp);
  938. /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
  939. felem_diff(ftmp5, ftmp2);
  940. /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
  941. /* ftmp2 = z2 * z2z2 */
  942. smallfelem_mul(tmp, small2, z2);
  943. felem_reduce(ftmp2, tmp);
  944. /* s1 = ftmp2 = y1 * z2**3 */
  945. felem_mul(tmp, y1, ftmp2);
  946. felem_reduce(ftmp6, tmp);
  947. /* ftmp6[i] < 2^101 */
  948. } else {
  949. /* We'll assume z2 = 1 (special case z2 = 0 is handled later). */
  950. /* u1 = ftmp3 = x1*z2z2 */
  951. felem_assign(ftmp3, x1);
  952. /* ftmp3[i] < 2^106 */
  953. /* ftmp5 = 2z1z2 */
  954. felem_assign(ftmp5, z1);
  955. felem_scalar(ftmp5, 2);
  956. /* ftmp5[i] < 2*2^106 = 2^107 */
  957. /* s1 = ftmp2 = y1 * z2**3 */
  958. felem_assign(ftmp6, y1);
  959. /* ftmp6[i] < 2^106 */
  960. }
  961. /* u2 = x2*z1z1 */
  962. smallfelem_mul(tmp, x2, small1);
  963. felem_reduce(ftmp4, tmp);
  964. /* h = ftmp4 = u2 - u1 */
  965. felem_diff_zero107(ftmp4, ftmp3);
  966. /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
  967. felem_shrink(small4, ftmp4);
  968. x_equal = smallfelem_is_zero(small4);
  969. /* z_out = ftmp5 * h */
  970. felem_small_mul(tmp, small4, ftmp5);
  971. felem_reduce(z_out, tmp);
  972. /* z_out[i] < 2^101 */
  973. /* ftmp = z1 * z1z1 */
  974. smallfelem_mul(tmp, small1, small3);
  975. felem_reduce(ftmp, tmp);
  976. /* s2 = tmp = y2 * z1**3 */
  977. felem_small_mul(tmp, y2, ftmp);
  978. felem_reduce(ftmp5, tmp);
  979. /* r = ftmp5 = (s2 - s1)*2 */
  980. felem_diff_zero107(ftmp5, ftmp6);
  981. /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
  982. felem_scalar(ftmp5, 2);
  983. /* ftmp5[i] < 2^109 */
  984. felem_shrink(small1, ftmp5);
  985. y_equal = smallfelem_is_zero(small1);
  986. if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
  987. point_double(x3, y3, z3, x1, y1, z1);
  988. return;
  989. }
  990. /* I = ftmp = (2h)**2 */
  991. felem_assign(ftmp, ftmp4);
  992. felem_scalar(ftmp, 2);
  993. /* ftmp[i] < 2*2^108 = 2^109 */
  994. felem_square(tmp, ftmp);
  995. felem_reduce(ftmp, tmp);
  996. /* J = ftmp2 = h * I */
  997. felem_mul(tmp, ftmp4, ftmp);
  998. felem_reduce(ftmp2, tmp);
  999. /* V = ftmp4 = U1 * I */
  1000. felem_mul(tmp, ftmp3, ftmp);
  1001. felem_reduce(ftmp4, tmp);
  1002. /* x_out = r**2 - J - 2V */
  1003. smallfelem_square(tmp, small1);
  1004. felem_reduce(x_out, tmp);
  1005. felem_assign(ftmp3, ftmp4);
  1006. felem_scalar(ftmp4, 2);
  1007. felem_sum(ftmp4, ftmp2);
  1008. /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
  1009. felem_diff(x_out, ftmp4);
  1010. /* x_out[i] < 2^105 + 2^101 */
  1011. /* y_out = r(V-x_out) - 2 * s1 * J */
  1012. felem_diff_zero107(ftmp3, x_out);
  1013. /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
  1014. felem_small_mul(tmp, small1, ftmp3);
  1015. felem_mul(tmp2, ftmp6, ftmp2);
  1016. longfelem_scalar(tmp2, 2);
  1017. /* tmp2[i] < 2*2^67 = 2^68 */
  1018. longfelem_diff(tmp, tmp2);
  1019. /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
  1020. felem_reduce_zero105(y_out, tmp);
  1021. /* y_out[i] < 2^106 */
  1022. copy_small_conditional(x_out, x2, z1_is_zero);
  1023. copy_conditional(x_out, x1, z2_is_zero);
  1024. copy_small_conditional(y_out, y2, z1_is_zero);
  1025. copy_conditional(y_out, y1, z2_is_zero);
  1026. copy_small_conditional(z_out, z2, z1_is_zero);
  1027. copy_conditional(z_out, z1, z2_is_zero);
  1028. felem_assign(x3, x_out);
  1029. felem_assign(y3, y_out);
  1030. felem_assign(z3, z_out);
  1031. }
  1032. /* point_add_small is the same as point_add, except that it operates on
  1033. * smallfelems. */
  1034. static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
  1035. smallfelem x1, smallfelem y1, smallfelem z1,
  1036. smallfelem x2, smallfelem y2, smallfelem z2) {
  1037. felem felem_x3, felem_y3, felem_z3;
  1038. felem felem_x1, felem_y1, felem_z1;
  1039. smallfelem_expand(felem_x1, x1);
  1040. smallfelem_expand(felem_y1, y1);
  1041. smallfelem_expand(felem_z1, z1);
  1042. point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0, x2,
  1043. y2, z2);
  1044. felem_shrink(x3, felem_x3);
  1045. felem_shrink(y3, felem_y3);
  1046. felem_shrink(z3, felem_z3);
  1047. }
  1048. /* Base point pre computation
  1049. * --------------------------
  1050. *
  1051. * Two different sorts of precomputed tables are used in the following code.
  1052. * Each contain various points on the curve, where each point is three field
  1053. * elements (x, y, z).
  1054. *
  1055. * For the base point table, z is usually 1 (0 for the point at infinity).
  1056. * This table has 2 * 16 elements, starting with the following:
  1057. * index | bits | point
  1058. * ------+---------+------------------------------
  1059. * 0 | 0 0 0 0 | 0G
  1060. * 1 | 0 0 0 1 | 1G
  1061. * 2 | 0 0 1 0 | 2^64G
  1062. * 3 | 0 0 1 1 | (2^64 + 1)G
  1063. * 4 | 0 1 0 0 | 2^128G
  1064. * 5 | 0 1 0 1 | (2^128 + 1)G
  1065. * 6 | 0 1 1 0 | (2^128 + 2^64)G
  1066. * 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
  1067. * 8 | 1 0 0 0 | 2^192G
  1068. * 9 | 1 0 0 1 | (2^192 + 1)G
  1069. * 10 | 1 0 1 0 | (2^192 + 2^64)G
  1070. * 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
  1071. * 12 | 1 1 0 0 | (2^192 + 2^128)G
  1072. * 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
  1073. * 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
  1074. * 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
  1075. * followed by a copy of this with each element multiplied by 2^32.
  1076. *
  1077. * The reason for this is so that we can clock bits into four different
  1078. * locations when doing simple scalar multiplies against the base point,
  1079. * and then another four locations using the second 16 elements.
  1080. *
  1081. * Tables for other points have table[i] = iG for i in 0 .. 16. */
  1082. /* g_pre_comp is the table of precomputed base points */
  1083. static const smallfelem g_pre_comp[2][16][3] = {
  1084. {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
  1085. {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
  1086. 0x6b17d1f2e12c4247},
  1087. {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
  1088. 0x4fe342e2fe1a7f9b},
  1089. {1, 0, 0, 0}},
  1090. {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
  1091. 0x0fa822bc2811aaa5},
  1092. {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
  1093. 0xbff44ae8f5dba80d},
  1094. {1, 0, 0, 0}},
  1095. {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
  1096. 0x300a4bbc89d6726f},
  1097. {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
  1098. 0x72aac7e0d09b4644},
  1099. {1, 0, 0, 0}},
  1100. {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
  1101. 0x447d739beedb5e67},
  1102. {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
  1103. 0x2d4825ab834131ee},
  1104. {1, 0, 0, 0}},
  1105. {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
  1106. 0xef9519328a9c72ff},
  1107. {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
  1108. 0x611e9fc37dbb2c9b},
  1109. {1, 0, 0, 0}},
  1110. {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
  1111. 0x550663797b51f5d8},
  1112. {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
  1113. 0x157164848aecb851},
  1114. {1, 0, 0, 0}},
  1115. {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
  1116. 0xeb5d7745b21141ea},
  1117. {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
  1118. 0xeafd72ebdbecc17b},
  1119. {1, 0, 0, 0}},
  1120. {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
  1121. 0xa6d39677a7849276},
  1122. {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
  1123. 0x674f84749b0b8816},
  1124. {1, 0, 0, 0}},
  1125. {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
  1126. 0x4e769e7672c9ddad},
  1127. {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
  1128. 0x42b99082de830663},
  1129. {1, 0, 0, 0}},
  1130. {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
  1131. 0x78878ef61c6ce04d},
  1132. {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
  1133. 0xb6cb3f5d7b72c321},
  1134. {1, 0, 0, 0}},
  1135. {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
  1136. 0x0c88bc4d716b1287},
  1137. {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
  1138. 0xdd5ddea3f3901dc6},
  1139. {1, 0, 0, 0}},
  1140. {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
  1141. 0x68f344af6b317466},
  1142. {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
  1143. 0x31b9c405f8540a20},
  1144. {1, 0, 0, 0}},
  1145. {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
  1146. 0x4052bf4b6f461db9},
  1147. {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
  1148. 0xfecf4d5190b0fc61},
  1149. {1, 0, 0, 0}},
  1150. {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
  1151. 0x1eddbae2c802e41a},
  1152. {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
  1153. 0x43104d86560ebcfc},
  1154. {1, 0, 0, 0}},
  1155. {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
  1156. 0xb48e26b484f7a21c},
  1157. {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
  1158. 0xfac015404d4d3dab},
  1159. {1, 0, 0, 0}}},
  1160. {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
  1161. {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
  1162. 0x7fe36b40af22af89},
  1163. {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
  1164. 0xe697d45825b63624},
  1165. {1, 0, 0, 0}},
  1166. {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
  1167. 0x4a5b506612a677a6},
  1168. {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
  1169. 0xeb13461ceac089f1},
  1170. {1, 0, 0, 0}},
  1171. {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
  1172. 0x0781b8291c6a220a},
  1173. {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
  1174. 0x690cde8df0151593},
  1175. {1, 0, 0, 0}},
  1176. {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
  1177. 0x8a535f566ec73617},
  1178. {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
  1179. 0x0455c08468b08bd7},
  1180. {1, 0, 0, 0}},
  1181. {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
  1182. 0x06bada7ab77f8276},
  1183. {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
  1184. 0x5b476dfd0e6cb18a},
  1185. {1, 0, 0, 0}},
  1186. {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
  1187. 0x3e29864e8a2ec908},
  1188. {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
  1189. 0x239b90ea3dc31e7e},
  1190. {1, 0, 0, 0}},
  1191. {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
  1192. 0x820f4dd949f72ff7},
  1193. {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
  1194. 0x140406ec783a05ec},
  1195. {1, 0, 0, 0}},
  1196. {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
  1197. 0x68f6b8542783dfee},
  1198. {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
  1199. 0xcbe1feba92e40ce6},
  1200. {1, 0, 0, 0}},
  1201. {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
  1202. 0xd0b2f94d2f420109},
  1203. {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
  1204. 0x971459828b0719e5},
  1205. {1, 0, 0, 0}},
  1206. {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
  1207. 0x961610004a866aba},
  1208. {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
  1209. 0x7acb9fadcee75e44},
  1210. {1, 0, 0, 0}},
  1211. {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
  1212. 0x24eb9acca333bf5b},
  1213. {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
  1214. 0x69f891c5acd079cc},
  1215. {1, 0, 0, 0}},
  1216. {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
  1217. 0xe51f547c5972a107},
  1218. {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
  1219. 0x1c309a2b25bb1387},
  1220. {1, 0, 0, 0}},
  1221. {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
  1222. 0x20b87b8aa2c4e503},
  1223. {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
  1224. 0xf5c6fa49919776be},
  1225. {1, 0, 0, 0}},
  1226. {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
  1227. 0x1ed7d1b9332010b9},
  1228. {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
  1229. 0x3a2b03f03217257a},
  1230. {1, 0, 0, 0}},
  1231. {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
  1232. 0x15fee545c78dd9f6},
  1233. {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
  1234. 0x4ab5b6b2b8753f81},
  1235. {1, 0, 0, 0}}}};
  1236. /* select_point selects the |idx|th point from a precomputation table and
  1237. * copies it to out. */
  1238. static void select_point(const u64 idx, size_t size,
  1239. const smallfelem pre_comp[/*size*/][3],
  1240. smallfelem out[3]) {
  1241. u64 *outlimbs = &out[0][0];
  1242. memset(outlimbs, 0, 3 * sizeof(smallfelem));
  1243. for (size_t i = 0; i < size; i++) {
  1244. const u64 *inlimbs = (const u64 *)&pre_comp[i][0][0];
  1245. u64 mask = i ^ idx;
  1246. mask |= mask >> 4;
  1247. mask |= mask >> 2;
  1248. mask |= mask >> 1;
  1249. mask &= 1;
  1250. mask--;
  1251. for (size_t j = 0; j < NLIMBS * 3; j++) {
  1252. outlimbs[j] |= inlimbs[j] & mask;
  1253. }
  1254. }
  1255. }
  1256. /* get_bit returns the |i|th bit in |in| */
  1257. static char get_bit(const felem_bytearray in, int i) {
  1258. if (i < 0 || i >= 256) {
  1259. return 0;
  1260. }
  1261. return (in[i >> 3] >> (i & 7)) & 1;
  1262. }
  1263. /* Interleaved point multiplication using precomputed point multiples: The
  1264. * small point multiples 0*P, 1*P, ..., 17*P are in pre_comp[], the scalars
  1265. * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  1266. * generator, using certain (large) precomputed multiples in g_pre_comp.
  1267. * Output point (X, Y, Z) is stored in x_out, y_out, z_out. */
  1268. static void batch_mul(felem x_out, felem y_out, felem z_out,
  1269. const felem_bytearray scalars[],
  1270. const size_t num_points, const u8 *g_scalar,
  1271. const smallfelem pre_comp[][17][3]) {
  1272. felem nq[3], ftmp;
  1273. smallfelem tmp[3];
  1274. u64 bits;
  1275. u8 sign, digit;
  1276. /* set nq to the point at infinity */
  1277. memset(nq, 0, 3 * sizeof(felem));
  1278. /* Loop over all scalars msb-to-lsb, interleaving additions of multiples
  1279. * of the generator (two in each of the last 32 rounds) and additions of
  1280. * other points multiples (every 5th round). */
  1281. int skip = 1; /* save two point operations in the first round */
  1282. size_t i = num_points != 0 ? 255 : 31;
  1283. for (;;) {
  1284. /* double */
  1285. if (!skip) {
  1286. point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
  1287. }
  1288. /* add multiples of the generator */
  1289. if (g_scalar != NULL && i <= 31) {
  1290. /* first, look 32 bits upwards */
  1291. bits = get_bit(g_scalar, i + 224) << 3;
  1292. bits |= get_bit(g_scalar, i + 160) << 2;
  1293. bits |= get_bit(g_scalar, i + 96) << 1;
  1294. bits |= get_bit(g_scalar, i + 32);
  1295. /* select the point to add, in constant time */
  1296. select_point(bits, 16, g_pre_comp[1], tmp);
  1297. if (!skip) {
  1298. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
  1299. tmp[0], tmp[1], tmp[2]);
  1300. } else {
  1301. smallfelem_expand(nq[0], tmp[0]);
  1302. smallfelem_expand(nq[1], tmp[1]);
  1303. smallfelem_expand(nq[2], tmp[2]);
  1304. skip = 0;
  1305. }
  1306. /* second, look at the current position */
  1307. bits = get_bit(g_scalar, i + 192) << 3;
  1308. bits |= get_bit(g_scalar, i + 128) << 2;
  1309. bits |= get_bit(g_scalar, i + 64) << 1;
  1310. bits |= get_bit(g_scalar, i);
  1311. /* select the point to add, in constant time */
  1312. select_point(bits, 16, g_pre_comp[0], tmp);
  1313. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */, tmp[0],
  1314. tmp[1], tmp[2]);
  1315. }
  1316. /* do other additions every 5 doublings */
  1317. if (num_points != 0 && i % 5 == 0) {
  1318. /* loop over all scalars */
  1319. size_t num;
  1320. for (num = 0; num < num_points; ++num) {
  1321. bits = get_bit(scalars[num], i + 4) << 5;
  1322. bits |= get_bit(scalars[num], i + 3) << 4;
  1323. bits |= get_bit(scalars[num], i + 2) << 3;
  1324. bits |= get_bit(scalars[num], i + 1) << 2;
  1325. bits |= get_bit(scalars[num], i) << 1;
  1326. bits |= get_bit(scalars[num], i - 1);
  1327. ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
  1328. /* select the point to add or subtract, in constant time. */
  1329. select_point(digit, 17, pre_comp[num], tmp);
  1330. smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
  1331. * point */
  1332. copy_small_conditional(ftmp, tmp[1], (((limb)sign) - 1));
  1333. felem_contract(tmp[1], ftmp);
  1334. if (!skip) {
  1335. point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 0 /* mixed */,
  1336. tmp[0], tmp[1], tmp[2]);
  1337. } else {
  1338. smallfelem_expand(nq[0], tmp[0]);
  1339. smallfelem_expand(nq[1], tmp[1]);
  1340. smallfelem_expand(nq[2], tmp[2]);
  1341. skip = 0;
  1342. }
  1343. }
  1344. }
  1345. if (i == 0) {
  1346. break;
  1347. }
  1348. --i;
  1349. }
  1350. felem_assign(x_out, nq[0]);
  1351. felem_assign(y_out, nq[1]);
  1352. felem_assign(z_out, nq[2]);
  1353. }
  1354. /******************************************************************************/
  1355. /*
  1356. * OPENSSL EC_METHOD FUNCTIONS
  1357. */
  1358. /* Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  1359. * (X/Z^2, Y/Z^3). */
  1360. static int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
  1361. const EC_POINT *point,
  1362. BIGNUM *x, BIGNUM *y,
  1363. BN_CTX *ctx) {
  1364. felem z1, z2, x_in, y_in;
  1365. smallfelem x_out, y_out;
  1366. longfelem tmp;
  1367. if (EC_POINT_is_at_infinity(group, point)) {
  1368. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  1369. return 0;
  1370. }
  1371. if (!BN_to_felem(x_in, &point->X) ||
  1372. !BN_to_felem(y_in, &point->Y) ||
  1373. !BN_to_felem(z1, &point->Z)) {
  1374. return 0;
  1375. }
  1376. felem_inv(z2, z1);
  1377. felem_square(tmp, z2);
  1378. felem_reduce(z1, tmp);
  1379. if (x != NULL) {
  1380. felem_mul(tmp, x_in, z1);
  1381. felem_reduce(x_in, tmp);
  1382. felem_contract(x_out, x_in);
  1383. if (!smallfelem_to_BN(x, x_out)) {
  1384. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1385. return 0;
  1386. }
  1387. }
  1388. if (y != NULL) {
  1389. felem_mul(tmp, z1, z2);
  1390. felem_reduce(z1, tmp);
  1391. felem_mul(tmp, y_in, z1);
  1392. felem_reduce(y_in, tmp);
  1393. felem_contract(y_out, y_in);
  1394. if (!smallfelem_to_BN(y, y_out)) {
  1395. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1396. return 0;
  1397. }
  1398. }
  1399. return 1;
  1400. }
  1401. static int ec_GFp_nistp256_points_mul(const EC_GROUP *group,
  1402. EC_POINT *r,
  1403. const BIGNUM *g_scalar,
  1404. const EC_POINT *p_,
  1405. const BIGNUM *p_scalar_,
  1406. BN_CTX *ctx) {
  1407. /* TODO: This function used to take |points| and |scalars| as arrays of
  1408. * |num| elements. The code below should be simplified to work in terms of |p|
  1409. * and |p_scalar|. */
  1410. size_t num = p_ != NULL ? 1 : 0;
  1411. const EC_POINT **points = p_ != NULL ? &p_ : NULL;
  1412. BIGNUM const *const *scalars = p_ != NULL ? &p_scalar_ : NULL;
  1413. int ret = 0;
  1414. BN_CTX *new_ctx = NULL;
  1415. BIGNUM *x, *y, *z, *tmp_scalar;
  1416. felem_bytearray g_secret;
  1417. felem_bytearray *secrets = NULL;
  1418. smallfelem(*pre_comp)[17][3] = NULL;
  1419. felem_bytearray tmp;
  1420. size_t num_points = num;
  1421. smallfelem x_in, y_in, z_in;
  1422. felem x_out, y_out, z_out;
  1423. const EC_POINT *p = NULL;
  1424. const BIGNUM *p_scalar = NULL;
  1425. if (ctx == NULL) {
  1426. ctx = new_ctx = BN_CTX_new();
  1427. if (ctx == NULL) {
  1428. return 0;
  1429. }
  1430. }
  1431. BN_CTX_start(ctx);
  1432. if ((x = BN_CTX_get(ctx)) == NULL ||
  1433. (y = BN_CTX_get(ctx)) == NULL ||
  1434. (z = BN_CTX_get(ctx)) == NULL ||
  1435. (tmp_scalar = BN_CTX_get(ctx)) == NULL) {
  1436. goto err;
  1437. }
  1438. if (num_points > 0) {
  1439. secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
  1440. pre_comp = OPENSSL_malloc(num_points * sizeof(smallfelem[17][3]));
  1441. if (secrets == NULL || pre_comp == NULL) {
  1442. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  1443. goto err;
  1444. }
  1445. /* we treat NULL scalars as 0, and NULL points as points at infinity,
  1446. * i.e., they contribute nothing to the linear combination. */
  1447. memset(secrets, 0, num_points * sizeof(felem_bytearray));
  1448. memset(pre_comp, 0, num_points * 17 * 3 * sizeof(smallfelem));
  1449. for (size_t i = 0; i < num_points; ++i) {
  1450. if (i == num) {
  1451. /* we didn't have a valid precomputation, so we pick the generator. */
  1452. p = EC_GROUP_get0_generator(group);
  1453. p_scalar = g_scalar;
  1454. } else {
  1455. /* the i^th point */
  1456. p = points[i];
  1457. p_scalar = scalars[i];
  1458. }
  1459. if (p_scalar != NULL && p != NULL) {
  1460. size_t num_bytes;
  1461. /* reduce g_scalar to 0 <= g_scalar < 2^256 */
  1462. if (BN_num_bits(p_scalar) > 256 || BN_is_negative(p_scalar)) {
  1463. /* this is an unusual input, and we don't guarantee
  1464. * constant-timeness. */
  1465. if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
  1466. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1467. goto err;
  1468. }
  1469. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1470. } else {
  1471. num_bytes = BN_bn2bin(p_scalar, tmp);
  1472. }
  1473. flip_endian(secrets[i], tmp, num_bytes);
  1474. /* precompute multiples */
  1475. if (!BN_to_felem(x_out, &p->X) ||
  1476. !BN_to_felem(y_out, &p->Y) ||
  1477. !BN_to_felem(z_out, &p->Z)) {
  1478. goto err;
  1479. }
  1480. felem_shrink(pre_comp[i][1][0], x_out);
  1481. felem_shrink(pre_comp[i][1][1], y_out);
  1482. felem_shrink(pre_comp[i][1][2], z_out);
  1483. for (size_t j = 2; j <= 16; ++j) {
  1484. if (j & 1) {
  1485. point_add_small(pre_comp[i][j][0], pre_comp[i][j][1],
  1486. pre_comp[i][j][2], pre_comp[i][1][0],
  1487. pre_comp[i][1][1], pre_comp[i][1][2],
  1488. pre_comp[i][j - 1][0], pre_comp[i][j - 1][1],
  1489. pre_comp[i][j - 1][2]);
  1490. } else {
  1491. point_double_small(pre_comp[i][j][0], pre_comp[i][j][1],
  1492. pre_comp[i][j][2], pre_comp[i][j / 2][0],
  1493. pre_comp[i][j / 2][1], pre_comp[i][j / 2][2]);
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. if (g_scalar != NULL) {
  1500. size_t num_bytes;
  1501. memset(g_secret, 0, sizeof(g_secret));
  1502. /* reduce g_scalar to 0 <= g_scalar < 2^256 */
  1503. if (BN_num_bits(g_scalar) > 256 || BN_is_negative(g_scalar)) {
  1504. /* this is an unusual input, and we don't guarantee
  1505. * constant-timeness. */
  1506. if (!BN_nnmod(tmp_scalar, g_scalar, &group->order, ctx)) {
  1507. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1508. goto err;
  1509. }
  1510. num_bytes = BN_bn2bin(tmp_scalar, tmp);
  1511. } else {
  1512. num_bytes = BN_bn2bin(g_scalar, tmp);
  1513. }
  1514. flip_endian(g_secret, tmp, num_bytes);
  1515. }
  1516. batch_mul(x_out, y_out, z_out, (const felem_bytearray(*))secrets,
  1517. num_points, g_scalar != NULL ? g_secret : NULL,
  1518. (const smallfelem(*)[17][3])pre_comp);
  1519. /* reduce the output to its unique minimal representation */
  1520. felem_contract(x_in, x_out);
  1521. felem_contract(y_in, y_out);
  1522. felem_contract(z_in, z_out);
  1523. if (!smallfelem_to_BN(x, x_in) ||
  1524. !smallfelem_to_BN(y, y_in) ||
  1525. !smallfelem_to_BN(z, z_in)) {
  1526. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  1527. goto err;
  1528. }
  1529. ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
  1530. err:
  1531. BN_CTX_end(ctx);
  1532. BN_CTX_free(new_ctx);
  1533. OPENSSL_free(secrets);
  1534. OPENSSL_free(pre_comp);
  1535. return ret;
  1536. }
  1537. const EC_METHOD EC_GFp_nistp256_method = {
  1538. ec_GFp_simple_group_init,
  1539. ec_GFp_simple_group_finish,
  1540. ec_GFp_simple_group_copy,
  1541. ec_GFp_simple_group_set_curve,
  1542. ec_GFp_nistp256_point_get_affine_coordinates,
  1543. ec_GFp_nistp256_points_mul,
  1544. ec_GFp_simple_field_mul,
  1545. ec_GFp_simple_field_sqr,
  1546. NULL /* field_encode */,
  1547. NULL /* field_decode */,
  1548. };
  1549. #endif /* 64_BIT && !WINDOWS */