p256-x86_64.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /* Copyright (c) 2014, Intel Corporation.
  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. /* Developers and authors:
  15. * Shay Gueron (1, 2), and Vlad Krasnov (1)
  16. * (1) Intel Corporation, Israel Development Center
  17. * (2) University of Haifa
  18. * Reference:
  19. * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
  20. * 256 Bit Primes" */
  21. #include <openssl/ec.h>
  22. #include <assert.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <openssl/bn.h>
  26. #include <openssl/crypto.h>
  27. #include <openssl/err.h>
  28. #include "../bn/internal.h"
  29. #include "../ec/internal.h"
  30. #include "../internal.h"
  31. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  32. !defined(OPENSSL_SMALL)
  33. #define P256_LIMBS (256 / BN_BITS2)
  34. typedef struct {
  35. BN_ULONG X[P256_LIMBS];
  36. BN_ULONG Y[P256_LIMBS];
  37. BN_ULONG Z[P256_LIMBS];
  38. } P256_POINT;
  39. typedef struct {
  40. BN_ULONG X[P256_LIMBS];
  41. BN_ULONG Y[P256_LIMBS];
  42. } P256_POINT_AFFINE;
  43. typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
  44. /* Functions implemented in assembly */
  45. /* Modular neg: res = -a mod P */
  46. void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
  47. /* Montgomery mul: res = a*b*2^-256 mod P */
  48. void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
  49. const BN_ULONG a[P256_LIMBS],
  50. const BN_ULONG b[P256_LIMBS]);
  51. /* Montgomery sqr: res = a*a*2^-256 mod P */
  52. void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
  53. const BN_ULONG a[P256_LIMBS]);
  54. /* Convert a number from Montgomery domain, by multiplying with 1 */
  55. void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
  56. const BN_ULONG in[P256_LIMBS]);
  57. /* Functions that perform constant time access to the precomputed tables */
  58. void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT *in_t, int index);
  59. void ecp_nistz256_select_w7(P256_POINT_AFFINE *val,
  60. const P256_POINT_AFFINE *in_t, int index);
  61. /* One converted into the Montgomery domain */
  62. static const BN_ULONG ONE[P256_LIMBS] = {
  63. TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
  64. TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe),
  65. };
  66. /* Precomputed tables for the default generator */
  67. #include "p256-x86_64-table.h"
  68. /* Recode window to a signed digit, see ecp_nistputil.c for details */
  69. static unsigned booth_recode_w5(unsigned in) {
  70. unsigned s, d;
  71. s = ~((in >> 5) - 1);
  72. d = (1 << 6) - in - 1;
  73. d = (d & s) | (in & ~s);
  74. d = (d >> 1) + (d & 1);
  75. return (d << 1) + (s & 1);
  76. }
  77. static unsigned booth_recode_w7(unsigned in) {
  78. unsigned s, d;
  79. s = ~((in >> 7) - 1);
  80. d = (1 << 8) - in - 1;
  81. d = (d & s) | (in & ~s);
  82. d = (d >> 1) + (d & 1);
  83. return (d << 1) + (s & 1);
  84. }
  85. static void copy_conditional(BN_ULONG dst[P256_LIMBS],
  86. const BN_ULONG src[P256_LIMBS], BN_ULONG move) {
  87. BN_ULONG mask1 = ((BN_ULONG)0) - move;
  88. BN_ULONG mask2 = ~mask1;
  89. dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
  90. dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
  91. dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
  92. dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
  93. if (P256_LIMBS == 8) {
  94. dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
  95. dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
  96. dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
  97. dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
  98. }
  99. }
  100. void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
  101. void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a,
  102. const P256_POINT *b);
  103. void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a,
  104. const P256_POINT_AFFINE *b);
  105. /* r = in^-1 mod p */
  106. static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
  107. const BN_ULONG in[P256_LIMBS]) {
  108. /* The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff
  109. ffffffff
  110. We use FLT and used poly-2 as exponent */
  111. BN_ULONG p2[P256_LIMBS];
  112. BN_ULONG p4[P256_LIMBS];
  113. BN_ULONG p8[P256_LIMBS];
  114. BN_ULONG p16[P256_LIMBS];
  115. BN_ULONG p32[P256_LIMBS];
  116. BN_ULONG res[P256_LIMBS];
  117. int i;
  118. ecp_nistz256_sqr_mont(res, in);
  119. ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
  120. ecp_nistz256_sqr_mont(res, p2);
  121. ecp_nistz256_sqr_mont(res, res);
  122. ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
  123. ecp_nistz256_sqr_mont(res, p4);
  124. ecp_nistz256_sqr_mont(res, res);
  125. ecp_nistz256_sqr_mont(res, res);
  126. ecp_nistz256_sqr_mont(res, res);
  127. ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
  128. ecp_nistz256_sqr_mont(res, p8);
  129. for (i = 0; i < 7; i++) {
  130. ecp_nistz256_sqr_mont(res, res);
  131. }
  132. ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
  133. ecp_nistz256_sqr_mont(res, p16);
  134. for (i = 0; i < 15; i++) {
  135. ecp_nistz256_sqr_mont(res, res);
  136. }
  137. ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
  138. ecp_nistz256_sqr_mont(res, p32);
  139. for (i = 0; i < 31; i++) {
  140. ecp_nistz256_sqr_mont(res, res);
  141. }
  142. ecp_nistz256_mul_mont(res, res, in);
  143. for (i = 0; i < 32 * 4; i++) {
  144. ecp_nistz256_sqr_mont(res, res);
  145. }
  146. ecp_nistz256_mul_mont(res, res, p32);
  147. for (i = 0; i < 32; i++) {
  148. ecp_nistz256_sqr_mont(res, res);
  149. }
  150. ecp_nistz256_mul_mont(res, res, p32);
  151. for (i = 0; i < 16; i++) {
  152. ecp_nistz256_sqr_mont(res, res);
  153. }
  154. ecp_nistz256_mul_mont(res, res, p16);
  155. for (i = 0; i < 8; i++) {
  156. ecp_nistz256_sqr_mont(res, res);
  157. }
  158. ecp_nistz256_mul_mont(res, res, p8);
  159. ecp_nistz256_sqr_mont(res, res);
  160. ecp_nistz256_sqr_mont(res, res);
  161. ecp_nistz256_sqr_mont(res, res);
  162. ecp_nistz256_sqr_mont(res, res);
  163. ecp_nistz256_mul_mont(res, res, p4);
  164. ecp_nistz256_sqr_mont(res, res);
  165. ecp_nistz256_sqr_mont(res, res);
  166. ecp_nistz256_mul_mont(res, res, p2);
  167. ecp_nistz256_sqr_mont(res, res);
  168. ecp_nistz256_sqr_mont(res, res);
  169. ecp_nistz256_mul_mont(res, res, in);
  170. memcpy(r, res, sizeof(res));
  171. }
  172. /* ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
  173. * returns one if it fits. Otherwise it returns zero. */
  174. static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
  175. const BIGNUM *in) {
  176. if (in->top > P256_LIMBS) {
  177. return 0;
  178. }
  179. memset(out, 0, sizeof(BN_ULONG) * P256_LIMBS);
  180. memcpy(out, in->d, sizeof(BN_ULONG) * in->top);
  181. return 1;
  182. }
  183. /* r = p * p_scalar */
  184. static int ecp_nistz256_windowed_mul(const EC_GROUP *group, P256_POINT *r,
  185. const EC_POINT *p, const BIGNUM *p_scalar,
  186. BN_CTX *ctx) {
  187. assert(p != NULL);
  188. assert(p_scalar != NULL);
  189. static const unsigned kWindowSize = 5;
  190. static const unsigned kMask = (1 << (5 /* kWindowSize */ + 1)) - 1;
  191. /* A |P256_POINT| is (3 * 32) = 96 bytes, and the 64-byte alignment should
  192. * add no more than 63 bytes of overhead. Thus, |table| should require
  193. * ~1599 ((96 * 16) + 63) bytes of stack space. */
  194. alignas(64) P256_POINT table[16];
  195. uint8_t p_str[33];
  196. int ret = 0;
  197. BN_CTX *new_ctx = NULL;
  198. int ctx_started = 0;
  199. if (BN_num_bits(p_scalar) > 256 || BN_is_negative(p_scalar)) {
  200. if (ctx == NULL) {
  201. new_ctx = BN_CTX_new();
  202. if (new_ctx == NULL) {
  203. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  204. goto err;
  205. }
  206. ctx = new_ctx;
  207. }
  208. BN_CTX_start(ctx);
  209. ctx_started = 1;
  210. BIGNUM *mod = BN_CTX_get(ctx);
  211. if (mod == NULL) {
  212. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. if (!BN_nnmod(mod, p_scalar, &group->order, ctx)) {
  216. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  217. goto err;
  218. }
  219. p_scalar = mod;
  220. }
  221. int j;
  222. for (j = 0; j < p_scalar->top * BN_BYTES; j += BN_BYTES) {
  223. BN_ULONG d = p_scalar->d[j / BN_BYTES];
  224. p_str[j + 0] = d & 0xff;
  225. p_str[j + 1] = (d >> 8) & 0xff;
  226. p_str[j + 2] = (d >> 16) & 0xff;
  227. p_str[j + 3] = (d >>= 24) & 0xff;
  228. if (BN_BYTES == 8) {
  229. d >>= 8;
  230. p_str[j + 4] = d & 0xff;
  231. p_str[j + 5] = (d >> 8) & 0xff;
  232. p_str[j + 6] = (d >> 16) & 0xff;
  233. p_str[j + 7] = (d >> 24) & 0xff;
  234. }
  235. }
  236. for (; j < 33; j++) {
  237. p_str[j] = 0;
  238. }
  239. /* table[0] is implicitly (0,0,0) (the point at infinity), therefore it is
  240. * not stored. All other values are actually stored with an offset of -1 in
  241. * table. */
  242. P256_POINT *row = table;
  243. if (!ecp_nistz256_bignum_to_field_elem(row[1 - 1].X, &p->X) ||
  244. !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Y, &p->Y) ||
  245. !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Z, &p->Z)) {
  246. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  247. goto err;
  248. }
  249. ecp_nistz256_point_double(&row[2 - 1], &row[1 - 1]);
  250. ecp_nistz256_point_add(&row[3 - 1], &row[2 - 1], &row[1 - 1]);
  251. ecp_nistz256_point_double(&row[4 - 1], &row[2 - 1]);
  252. ecp_nistz256_point_double(&row[6 - 1], &row[3 - 1]);
  253. ecp_nistz256_point_double(&row[8 - 1], &row[4 - 1]);
  254. ecp_nistz256_point_double(&row[12 - 1], &row[6 - 1]);
  255. ecp_nistz256_point_add(&row[5 - 1], &row[4 - 1], &row[1 - 1]);
  256. ecp_nistz256_point_add(&row[7 - 1], &row[6 - 1], &row[1 - 1]);
  257. ecp_nistz256_point_add(&row[9 - 1], &row[8 - 1], &row[1 - 1]);
  258. ecp_nistz256_point_add(&row[13 - 1], &row[12 - 1], &row[1 - 1]);
  259. ecp_nistz256_point_double(&row[14 - 1], &row[7 - 1]);
  260. ecp_nistz256_point_double(&row[10 - 1], &row[5 - 1]);
  261. ecp_nistz256_point_add(&row[15 - 1], &row[14 - 1], &row[1 - 1]);
  262. ecp_nistz256_point_add(&row[11 - 1], &row[10 - 1], &row[1 - 1]);
  263. ecp_nistz256_point_add(&row[16 - 1], &row[15 - 1], &row[1 - 1]);
  264. BN_ULONG tmp[P256_LIMBS];
  265. alignas(32) P256_POINT h;
  266. unsigned index = 255;
  267. unsigned wvalue = p_str[(index - 1) / 8];
  268. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  269. ecp_nistz256_select_w5(r, table, booth_recode_w5(wvalue) >> 1);
  270. while (index >= 5) {
  271. if (index != 255) {
  272. unsigned off = (index - 1) / 8;
  273. wvalue = p_str[off] | p_str[off + 1] << 8;
  274. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  275. wvalue = booth_recode_w5(wvalue);
  276. ecp_nistz256_select_w5(&h, table, wvalue >> 1);
  277. ecp_nistz256_neg(tmp, h.Y);
  278. copy_conditional(h.Y, tmp, (wvalue & 1));
  279. ecp_nistz256_point_add(r, r, &h);
  280. }
  281. index -= kWindowSize;
  282. ecp_nistz256_point_double(r, r);
  283. ecp_nistz256_point_double(r, r);
  284. ecp_nistz256_point_double(r, r);
  285. ecp_nistz256_point_double(r, r);
  286. ecp_nistz256_point_double(r, r);
  287. }
  288. /* Final window */
  289. wvalue = p_str[0];
  290. wvalue = (wvalue << 1) & kMask;
  291. wvalue = booth_recode_w5(wvalue);
  292. ecp_nistz256_select_w5(&h, table, wvalue >> 1);
  293. ecp_nistz256_neg(tmp, h.Y);
  294. copy_conditional(h.Y, tmp, wvalue & 1);
  295. ecp_nistz256_point_add(r, r, &h);
  296. ret = 1;
  297. err:
  298. if (ctx_started) {
  299. BN_CTX_end(ctx);
  300. }
  301. BN_CTX_free(new_ctx);
  302. return ret;
  303. }
  304. static int ecp_nistz256_points_mul(
  305. const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  306. const EC_POINT *p_, const BIGNUM *p_scalar, BN_CTX *ctx) {
  307. assert((p_ != NULL) == (p_scalar != NULL));
  308. static const unsigned kWindowSize = 7;
  309. static const unsigned kMask = (1 << (7 /* kWindowSize */ + 1)) - 1;
  310. alignas(32) union {
  311. P256_POINT p;
  312. P256_POINT_AFFINE a;
  313. } t, p;
  314. int ret = 0;
  315. BN_CTX *new_ctx = NULL;
  316. int ctx_started = 0;
  317. /* Need 256 bits for space for all coordinates. */
  318. if (bn_wexpand(&r->X, P256_LIMBS) == NULL ||
  319. bn_wexpand(&r->Y, P256_LIMBS) == NULL ||
  320. bn_wexpand(&r->Z, P256_LIMBS) == NULL) {
  321. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  322. goto err;
  323. }
  324. r->X.top = P256_LIMBS;
  325. r->Y.top = P256_LIMBS;
  326. r->Z.top = P256_LIMBS;
  327. if (g_scalar != NULL) {
  328. if (BN_num_bits(g_scalar) > 256 || BN_is_negative(g_scalar)) {
  329. if (ctx == NULL) {
  330. new_ctx = BN_CTX_new();
  331. if (new_ctx == NULL) {
  332. goto err;
  333. }
  334. ctx = new_ctx;
  335. }
  336. BN_CTX_start(ctx);
  337. ctx_started = 1;
  338. BIGNUM *tmp_scalar = BN_CTX_get(ctx);
  339. if (tmp_scalar == NULL) {
  340. goto err;
  341. }
  342. if (!BN_nnmod(tmp_scalar, g_scalar, &group->order, ctx)) {
  343. OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
  344. goto err;
  345. }
  346. g_scalar = tmp_scalar;
  347. }
  348. uint8_t p_str[33] = {0};
  349. int i;
  350. for (i = 0; i < g_scalar->top * BN_BYTES; i += BN_BYTES) {
  351. BN_ULONG d = g_scalar->d[i / BN_BYTES];
  352. p_str[i + 0] = d & 0xff;
  353. p_str[i + 1] = (d >> 8) & 0xff;
  354. p_str[i + 2] = (d >> 16) & 0xff;
  355. p_str[i + 3] = (d >>= 24) & 0xff;
  356. if (BN_BYTES == 8) {
  357. d >>= 8;
  358. p_str[i + 4] = d & 0xff;
  359. p_str[i + 5] = (d >> 8) & 0xff;
  360. p_str[i + 6] = (d >> 16) & 0xff;
  361. p_str[i + 7] = (d >> 24) & 0xff;
  362. }
  363. }
  364. for (; i < (int) sizeof(p_str); i++) {
  365. p_str[i] = 0;
  366. }
  367. /* First window */
  368. unsigned wvalue = (p_str[0] << 1) & kMask;
  369. unsigned index = kWindowSize;
  370. wvalue = booth_recode_w7(wvalue);
  371. const PRECOMP256_ROW *const precomputed_table =
  372. (const PRECOMP256_ROW *)ecp_nistz256_precomputed;
  373. ecp_nistz256_select_w7(&p.a, precomputed_table[0], wvalue >> 1);
  374. ecp_nistz256_neg(p.p.Z, p.p.Y);
  375. copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
  376. memcpy(p.p.Z, ONE, sizeof(ONE));
  377. for (i = 1; i < 37; i++) {
  378. unsigned off = (index - 1) / 8;
  379. wvalue = p_str[off] | p_str[off + 1] << 8;
  380. wvalue = (wvalue >> ((index - 1) % 8)) & kMask;
  381. index += kWindowSize;
  382. wvalue = booth_recode_w7(wvalue);
  383. ecp_nistz256_select_w7(&t.a, precomputed_table[i], wvalue >> 1);
  384. ecp_nistz256_neg(t.p.Z, t.a.Y);
  385. copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
  386. ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
  387. }
  388. }
  389. const int p_is_infinity = g_scalar == NULL;
  390. if (p_scalar != NULL) {
  391. P256_POINT *out = &t.p;
  392. if (p_is_infinity) {
  393. out = &p.p;
  394. }
  395. if (!ecp_nistz256_windowed_mul(group, out, p_, p_scalar, ctx)) {
  396. goto err;
  397. }
  398. if (!p_is_infinity) {
  399. ecp_nistz256_point_add(&p.p, &p.p, out);
  400. }
  401. }
  402. memcpy(r->X.d, p.p.X, sizeof(p.p.X));
  403. memcpy(r->Y.d, p.p.Y, sizeof(p.p.Y));
  404. memcpy(r->Z.d, p.p.Z, sizeof(p.p.Z));
  405. /* Not constant-time, but we're only operating on the public output. */
  406. bn_correct_top(&r->X);
  407. bn_correct_top(&r->Y);
  408. bn_correct_top(&r->Z);
  409. r->Z_is_one = BN_is_one(&r->Z);
  410. ret = 1;
  411. err:
  412. if (ctx_started) {
  413. BN_CTX_end(ctx);
  414. }
  415. BN_CTX_free(new_ctx);
  416. return ret;
  417. }
  418. static int ecp_nistz256_get_affine(const EC_GROUP *group, const EC_POINT *point,
  419. BIGNUM *x, BIGNUM *y, BN_CTX *ctx) {
  420. BN_ULONG z_inv2[P256_LIMBS];
  421. BN_ULONG z_inv3[P256_LIMBS];
  422. BN_ULONG x_aff[P256_LIMBS];
  423. BN_ULONG y_aff[P256_LIMBS];
  424. BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
  425. if (EC_POINT_is_at_infinity(group, point)) {
  426. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  427. return 0;
  428. }
  429. if (!ecp_nistz256_bignum_to_field_elem(point_x, &point->X) ||
  430. !ecp_nistz256_bignum_to_field_elem(point_y, &point->Y) ||
  431. !ecp_nistz256_bignum_to_field_elem(point_z, &point->Z)) {
  432. OPENSSL_PUT_ERROR(EC, EC_R_COORDINATES_OUT_OF_RANGE);
  433. return 0;
  434. }
  435. ecp_nistz256_mod_inverse(z_inv3, point_z);
  436. ecp_nistz256_sqr_mont(z_inv2, z_inv3);
  437. ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
  438. if (x != NULL) {
  439. if (bn_wexpand(x, P256_LIMBS) == NULL) {
  440. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  441. return 0;
  442. }
  443. x->top = P256_LIMBS;
  444. ecp_nistz256_from_mont(x->d, x_aff);
  445. bn_correct_top(x);
  446. }
  447. if (y != NULL) {
  448. ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
  449. ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
  450. if (bn_wexpand(y, P256_LIMBS) == NULL) {
  451. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  452. return 0;
  453. }
  454. y->top = P256_LIMBS;
  455. ecp_nistz256_from_mont(y->d, y_aff);
  456. bn_correct_top(y);
  457. }
  458. return 1;
  459. }
  460. const EC_METHOD *EC_GFp_nistz256_method(void) {
  461. static const EC_METHOD ret = {
  462. ec_GFp_mont_group_init,
  463. ec_GFp_mont_group_finish,
  464. ec_GFp_mont_group_copy,
  465. ec_GFp_mont_group_set_curve,
  466. ecp_nistz256_get_affine,
  467. ecp_nistz256_points_mul,
  468. 0 /* check_pub_key_order */,
  469. ec_GFp_mont_field_mul,
  470. ec_GFp_mont_field_sqr,
  471. ec_GFp_mont_field_encode,
  472. ec_GFp_mont_field_decode,
  473. ec_GFp_mont_field_set_to_one,
  474. };
  475. return &ret;
  476. }
  477. #endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
  478. !defined(OPENSSL_SMALL) */