Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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