ecp_nistz256.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /******************************************************************************
  2. * *
  3. * Copyright 2014 Intel Corporation *
  4. * *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); *
  6. * you may not use this file except in compliance with the License. *
  7. * You may obtain a copy of the License at *
  8. * *
  9. * http://www.apache.org/licenses/LICENSE-2.0 *
  10. * *
  11. * Unless required by applicable law or agreed to in writing, software *
  12. * distributed under the License is distributed on an "AS IS" BASIS, *
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  14. * See the License for the specific language governing permissions and *
  15. * limitations under the License. *
  16. * *
  17. ******************************************************************************
  18. * *
  19. * Developers and authors: *
  20. * Shay Gueron (1, 2), and Vlad Krasnov (1) *
  21. * (1) Intel Corporation, Israel Development Center *
  22. * (2) University of Haifa *
  23. * Reference: *
  24. * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with *
  25. * 256 Bit Primes" *
  26. * *
  27. ******************************************************************************/
  28. #include <string.h>
  29. #include <openssl/bn.h>
  30. #include <openssl/err.h>
  31. #include <openssl/ec.h>
  32. #include "cryptlib.h"
  33. #include "ec_lcl.h"
  34. #if BN_BITS2 != 64
  35. # define TOBN(hi,lo) lo,hi
  36. #else
  37. # define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo)
  38. #endif
  39. #if defined(__GNUC__)
  40. # define ALIGN32 __attribute((aligned(32)))
  41. #elif defined(_MSC_VER)
  42. # define ALIGN32 __declspec(align(32))
  43. #else
  44. # define ALIGN32
  45. #endif
  46. #define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N)
  47. #define P256_LIMBS (256/BN_BITS2)
  48. typedef unsigned short u16;
  49. typedef struct {
  50. BN_ULONG X[P256_LIMBS];
  51. BN_ULONG Y[P256_LIMBS];
  52. BN_ULONG Z[P256_LIMBS];
  53. } P256_POINT;
  54. typedef struct {
  55. BN_ULONG X[P256_LIMBS];
  56. BN_ULONG Y[P256_LIMBS];
  57. } P256_POINT_AFFINE;
  58. typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
  59. /* structure for precomputed multiples of the generator */
  60. typedef struct ec_pre_comp_st {
  61. const EC_GROUP *group; /* Parent EC_GROUP object */
  62. size_t w; /* Window size */
  63. /*
  64. * Constant time access to the X and Y coordinates of the pre-computed,
  65. * generator multiplies, in the Montgomery domain. Pre-calculated
  66. * multiplies are stored in affine form.
  67. */
  68. PRECOMP256_ROW *precomp;
  69. void *precomp_storage;
  70. int references;
  71. } EC_PRE_COMP;
  72. /* Functions implemented in assembly */
  73. /*
  74. * Most of below mentioned functions *preserve* the property of inputs
  75. * being fully reduced, i.e. being in [0, modulus) range. Simply put if
  76. * inputs are fully reduced, then output is too. Note that reverse is
  77. * not true, in sense that given partially reduced inputs output can be
  78. * either, not unlikely reduced. And "most" in first sentence refers to
  79. * the fact that given the calculations flow one can tolerate that
  80. * addition, 1st function below, produces partially reduced result *if*
  81. * multiplications by 2 and 3, which customarily use addition, fully
  82. * reduce it. This effectively gives two options: a) addition produces
  83. * fully reduced result [as long as inputs are, just like remaining
  84. * functions]; b) addition is allowed to produce partially reduced
  85. * result, but multiplications by 2 and 3 perform additional reduction
  86. * step. Choice between the two can be platform-specific, but it was a)
  87. * in all cases so far...
  88. */
  89. /* Modular add: res = a+b mod P */
  90. void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
  91. const BN_ULONG a[P256_LIMBS],
  92. const BN_ULONG b[P256_LIMBS]);
  93. /* Modular mul by 2: res = 2*a mod P */
  94. void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
  95. const BN_ULONG a[P256_LIMBS]);
  96. /* Modular mul by 3: res = 3*a mod P */
  97. void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
  98. const BN_ULONG a[P256_LIMBS]);
  99. /* Modular div by 2: res = a/2 mod P */
  100. void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
  101. const BN_ULONG a[P256_LIMBS]);
  102. /* Modular sub: res = a-b mod P */
  103. void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
  104. const BN_ULONG a[P256_LIMBS],
  105. const BN_ULONG b[P256_LIMBS]);
  106. /* Modular neg: res = -a mod P */
  107. void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
  108. /* Montgomery mul: res = a*b*2^-256 mod P */
  109. void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
  110. const BN_ULONG a[P256_LIMBS],
  111. const BN_ULONG b[P256_LIMBS]);
  112. /* Montgomery sqr: res = a*a*2^-256 mod P */
  113. void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
  114. const BN_ULONG a[P256_LIMBS]);
  115. /* Convert a number from Montgomery domain, by multiplying with 1 */
  116. void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
  117. const BN_ULONG in[P256_LIMBS]);
  118. /* Convert a number to Montgomery domain, by multiplying with 2^512 mod P*/
  119. void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
  120. const BN_ULONG in[P256_LIMBS]);
  121. /* Functions that perform constant time access to the precomputed tables */
  122. void ecp_nistz256_select_w5(P256_POINT * val,
  123. const P256_POINT * in_t, int index);
  124. void ecp_nistz256_select_w7(P256_POINT_AFFINE * val,
  125. const P256_POINT_AFFINE * in_t, int index);
  126. /* One converted into the Montgomery domain */
  127. static const BN_ULONG ONE[P256_LIMBS] = {
  128. TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
  129. TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
  130. };
  131. static void *ecp_nistz256_pre_comp_dup(void *);
  132. static void ecp_nistz256_pre_comp_free(void *);
  133. static void ecp_nistz256_pre_comp_clear_free(void *);
  134. static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group);
  135. /* Precomputed tables for the default generator */
  136. #include "ecp_nistz256_table.c"
  137. /* Recode window to a signed digit, see ecp_nistputil.c for details */
  138. static unsigned int _booth_recode_w5(unsigned int in)
  139. {
  140. unsigned int s, d;
  141. s = ~((in >> 5) - 1);
  142. d = (1 << 6) - in - 1;
  143. d = (d & s) | (in & ~s);
  144. d = (d >> 1) + (d & 1);
  145. return (d << 1) + (s & 1);
  146. }
  147. static unsigned int _booth_recode_w7(unsigned int in)
  148. {
  149. unsigned int s, d;
  150. s = ~((in >> 7) - 1);
  151. d = (1 << 8) - in - 1;
  152. d = (d & s) | (in & ~s);
  153. d = (d >> 1) + (d & 1);
  154. return (d << 1) + (s & 1);
  155. }
  156. static void copy_conditional(BN_ULONG dst[P256_LIMBS],
  157. const BN_ULONG src[P256_LIMBS], BN_ULONG move)
  158. {
  159. BN_ULONG mask1 = -move;
  160. BN_ULONG mask2 = ~mask1;
  161. dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
  162. dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
  163. dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
  164. dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
  165. if (P256_LIMBS == 8) {
  166. dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
  167. dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
  168. dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
  169. dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
  170. }
  171. }
  172. static BN_ULONG is_zero(BN_ULONG in)
  173. {
  174. in |= (0 - in);
  175. in = ~in;
  176. in &= BN_MASK2;
  177. in >>= BN_BITS2 - 1;
  178. return in;
  179. }
  180. static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
  181. const BN_ULONG b[P256_LIMBS])
  182. {
  183. BN_ULONG res;
  184. res = a[0] ^ b[0];
  185. res |= a[1] ^ b[1];
  186. res |= a[2] ^ b[2];
  187. res |= a[3] ^ b[3];
  188. if (P256_LIMBS == 8) {
  189. res |= a[4] ^ b[4];
  190. res |= a[5] ^ b[5];
  191. res |= a[6] ^ b[6];
  192. res |= a[7] ^ b[7];
  193. }
  194. return is_zero(res);
  195. }
  196. static BN_ULONG is_one(const BIGNUM *z)
  197. {
  198. BN_ULONG res = 0;
  199. BN_ULONG *a = z->d;
  200. if (z->top == (P256_LIMBS - P256_LIMBS / 8)) {
  201. res = a[0] ^ ONE[0];
  202. res |= a[1] ^ ONE[1];
  203. res |= a[2] ^ ONE[2];
  204. res |= a[3] ^ ONE[3];
  205. if (P256_LIMBS == 8) {
  206. res |= a[4] ^ ONE[4];
  207. res |= a[5] ^ ONE[5];
  208. res |= a[6] ^ ONE[6];
  209. /*
  210. * no check for a[7] (being zero) on 32-bit platforms,
  211. * because value of "one" takes only 7 limbs.
  212. */
  213. }
  214. res = is_zero(res);
  215. }
  216. return res;
  217. }
  218. static int ecp_nistz256_set_words(BIGNUM *a, BN_ULONG words[P256_LIMBS])
  219. {
  220. if (bn_wexpand(a, P256_LIMBS) == NULL) {
  221. ECerr(EC_F_ECP_NISTZ256_SET_WORDS, ERR_R_MALLOC_FAILURE);
  222. return 0;
  223. }
  224. memcpy(a->d, words, sizeof(BN_ULONG) * P256_LIMBS);
  225. a->top = P256_LIMBS;
  226. bn_correct_top(a);
  227. return 1;
  228. }
  229. #ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
  230. void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
  231. void ecp_nistz256_point_add(P256_POINT *r,
  232. const P256_POINT *a, const P256_POINT *b);
  233. void ecp_nistz256_point_add_affine(P256_POINT *r,
  234. const P256_POINT *a,
  235. const P256_POINT_AFFINE *b);
  236. #else
  237. /* Point double: r = 2*a */
  238. static void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a)
  239. {
  240. BN_ULONG S[P256_LIMBS];
  241. BN_ULONG M[P256_LIMBS];
  242. BN_ULONG Zsqr[P256_LIMBS];
  243. BN_ULONG tmp0[P256_LIMBS];
  244. const BN_ULONG *in_x = a->X;
  245. const BN_ULONG *in_y = a->Y;
  246. const BN_ULONG *in_z = a->Z;
  247. BN_ULONG *res_x = r->X;
  248. BN_ULONG *res_y = r->Y;
  249. BN_ULONG *res_z = r->Z;
  250. ecp_nistz256_mul_by_2(S, in_y);
  251. ecp_nistz256_sqr_mont(Zsqr, in_z);
  252. ecp_nistz256_sqr_mont(S, S);
  253. ecp_nistz256_mul_mont(res_z, in_z, in_y);
  254. ecp_nistz256_mul_by_2(res_z, res_z);
  255. ecp_nistz256_add(M, in_x, Zsqr);
  256. ecp_nistz256_sub(Zsqr, in_x, Zsqr);
  257. ecp_nistz256_sqr_mont(res_y, S);
  258. ecp_nistz256_div_by_2(res_y, res_y);
  259. ecp_nistz256_mul_mont(M, M, Zsqr);
  260. ecp_nistz256_mul_by_3(M, M);
  261. ecp_nistz256_mul_mont(S, S, in_x);
  262. ecp_nistz256_mul_by_2(tmp0, S);
  263. ecp_nistz256_sqr_mont(res_x, M);
  264. ecp_nistz256_sub(res_x, res_x, tmp0);
  265. ecp_nistz256_sub(S, S, res_x);
  266. ecp_nistz256_mul_mont(S, S, M);
  267. ecp_nistz256_sub(res_y, S, res_y);
  268. }
  269. /* Point addition: r = a+b */
  270. static void ecp_nistz256_point_add(P256_POINT *r,
  271. const P256_POINT *a, const P256_POINT *b)
  272. {
  273. BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
  274. BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
  275. BN_ULONG Z1sqr[P256_LIMBS];
  276. BN_ULONG Z2sqr[P256_LIMBS];
  277. BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
  278. BN_ULONG Hsqr[P256_LIMBS];
  279. BN_ULONG Rsqr[P256_LIMBS];
  280. BN_ULONG Hcub[P256_LIMBS];
  281. BN_ULONG res_x[P256_LIMBS];
  282. BN_ULONG res_y[P256_LIMBS];
  283. BN_ULONG res_z[P256_LIMBS];
  284. BN_ULONG in1infty, in2infty;
  285. const BN_ULONG *in1_x = a->X;
  286. const BN_ULONG *in1_y = a->Y;
  287. const BN_ULONG *in1_z = a->Z;
  288. const BN_ULONG *in2_x = b->X;
  289. const BN_ULONG *in2_y = b->Y;
  290. const BN_ULONG *in2_z = b->Z;
  291. /*
  292. * Infinity in encoded as (,,0)
  293. */
  294. in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
  295. if (P256_LIMBS == 8)
  296. in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
  297. in2infty = (in2_z[0] | in2_z[1] | in2_z[2] | in2_z[3]);
  298. if (P256_LIMBS == 8)
  299. in2infty |= (in2_z[4] | in2_z[5] | in2_z[6] | in2_z[7]);
  300. in1infty = is_zero(in1infty);
  301. in2infty = is_zero(in2infty);
  302. ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
  303. ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
  304. ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
  305. ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
  306. ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
  307. ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
  308. ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
  309. ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
  310. ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
  311. ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
  312. /*
  313. * This should not happen during sign/ecdh, so no constant time violation
  314. */
  315. if (is_equal(U1, U2) && !in1infty && !in2infty) {
  316. if (is_equal(S1, S2)) {
  317. ecp_nistz256_point_double(r, a);
  318. return;
  319. } else {
  320. memset(r, 0, sizeof(*r));
  321. return;
  322. }
  323. }
  324. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  325. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  326. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  327. ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
  328. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  329. ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
  330. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  331. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  332. ecp_nistz256_sub(res_x, res_x, Hcub);
  333. ecp_nistz256_sub(res_y, U2, res_x);
  334. ecp_nistz256_mul_mont(S2, S1, Hcub);
  335. ecp_nistz256_mul_mont(res_y, R, res_y);
  336. ecp_nistz256_sub(res_y, res_y, S2);
  337. copy_conditional(res_x, in2_x, in1infty);
  338. copy_conditional(res_y, in2_y, in1infty);
  339. copy_conditional(res_z, in2_z, in1infty);
  340. copy_conditional(res_x, in1_x, in2infty);
  341. copy_conditional(res_y, in1_y, in2infty);
  342. copy_conditional(res_z, in1_z, in2infty);
  343. memcpy(r->X, res_x, sizeof(res_x));
  344. memcpy(r->Y, res_y, sizeof(res_y));
  345. memcpy(r->Z, res_z, sizeof(res_z));
  346. }
  347. /* Point addition when b is known to be affine: r = a+b */
  348. static void ecp_nistz256_point_add_affine(P256_POINT *r,
  349. const P256_POINT *a,
  350. const P256_POINT_AFFINE *b)
  351. {
  352. BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
  353. BN_ULONG Z1sqr[P256_LIMBS];
  354. BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
  355. BN_ULONG Hsqr[P256_LIMBS];
  356. BN_ULONG Rsqr[P256_LIMBS];
  357. BN_ULONG Hcub[P256_LIMBS];
  358. BN_ULONG res_x[P256_LIMBS];
  359. BN_ULONG res_y[P256_LIMBS];
  360. BN_ULONG res_z[P256_LIMBS];
  361. BN_ULONG in1infty, in2infty;
  362. const BN_ULONG *in1_x = a->X;
  363. const BN_ULONG *in1_y = a->Y;
  364. const BN_ULONG *in1_z = a->Z;
  365. const BN_ULONG *in2_x = b->X;
  366. const BN_ULONG *in2_y = b->Y;
  367. /*
  368. * Infinity in encoded as (,,0)
  369. */
  370. in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
  371. if (P256_LIMBS == 8)
  372. in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
  373. /*
  374. * In affine representation we encode infinity as (0,0), which is
  375. * not on the curve, so it is OK
  376. */
  377. in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
  378. in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
  379. if (P256_LIMBS == 8)
  380. in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
  381. in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
  382. in1infty = is_zero(in1infty);
  383. in2infty = is_zero(in2infty);
  384. ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
  385. ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
  386. ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
  387. ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
  388. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  389. ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
  390. ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
  391. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  392. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  393. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  394. ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
  395. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  396. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  397. ecp_nistz256_sub(res_x, res_x, Hcub);
  398. ecp_nistz256_sub(H, U2, res_x);
  399. ecp_nistz256_mul_mont(S2, in1_y, Hcub);
  400. ecp_nistz256_mul_mont(H, H, R);
  401. ecp_nistz256_sub(res_y, H, S2);
  402. copy_conditional(res_x, in2_x, in1infty);
  403. copy_conditional(res_x, in1_x, in2infty);
  404. copy_conditional(res_y, in2_y, in1infty);
  405. copy_conditional(res_y, in1_y, in2infty);
  406. copy_conditional(res_z, ONE, in1infty);
  407. copy_conditional(res_z, in1_z, in2infty);
  408. memcpy(r->X, res_x, sizeof(res_x));
  409. memcpy(r->Y, res_y, sizeof(res_y));
  410. memcpy(r->Z, res_z, sizeof(res_z));
  411. }
  412. #endif
  413. /* r = in^-1 mod p */
  414. static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
  415. const BN_ULONG in[P256_LIMBS])
  416. {
  417. /*
  418. * The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff
  419. * ffffffff ffffffff We use FLT and used poly-2 as exponent
  420. */
  421. BN_ULONG p2[P256_LIMBS];
  422. BN_ULONG p4[P256_LIMBS];
  423. BN_ULONG p8[P256_LIMBS];
  424. BN_ULONG p16[P256_LIMBS];
  425. BN_ULONG p32[P256_LIMBS];
  426. BN_ULONG res[P256_LIMBS];
  427. int i;
  428. ecp_nistz256_sqr_mont(res, in);
  429. ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
  430. ecp_nistz256_sqr_mont(res, p2);
  431. ecp_nistz256_sqr_mont(res, res);
  432. ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
  433. ecp_nistz256_sqr_mont(res, p4);
  434. ecp_nistz256_sqr_mont(res, res);
  435. ecp_nistz256_sqr_mont(res, res);
  436. ecp_nistz256_sqr_mont(res, res);
  437. ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
  438. ecp_nistz256_sqr_mont(res, p8);
  439. for (i = 0; i < 7; i++)
  440. ecp_nistz256_sqr_mont(res, res);
  441. ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
  442. ecp_nistz256_sqr_mont(res, p16);
  443. for (i = 0; i < 15; i++)
  444. ecp_nistz256_sqr_mont(res, res);
  445. ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
  446. ecp_nistz256_sqr_mont(res, p32);
  447. for (i = 0; i < 31; i++)
  448. ecp_nistz256_sqr_mont(res, res);
  449. ecp_nistz256_mul_mont(res, res, in);
  450. for (i = 0; i < 32 * 4; i++)
  451. ecp_nistz256_sqr_mont(res, res);
  452. ecp_nistz256_mul_mont(res, res, p32);
  453. for (i = 0; i < 32; i++)
  454. ecp_nistz256_sqr_mont(res, res);
  455. ecp_nistz256_mul_mont(res, res, p32);
  456. for (i = 0; i < 16; i++)
  457. ecp_nistz256_sqr_mont(res, res);
  458. ecp_nistz256_mul_mont(res, res, p16);
  459. for (i = 0; i < 8; i++)
  460. ecp_nistz256_sqr_mont(res, res);
  461. ecp_nistz256_mul_mont(res, res, p8);
  462. ecp_nistz256_sqr_mont(res, res);
  463. ecp_nistz256_sqr_mont(res, res);
  464. ecp_nistz256_sqr_mont(res, res);
  465. ecp_nistz256_sqr_mont(res, res);
  466. ecp_nistz256_mul_mont(res, res, p4);
  467. ecp_nistz256_sqr_mont(res, res);
  468. ecp_nistz256_sqr_mont(res, res);
  469. ecp_nistz256_mul_mont(res, res, p2);
  470. ecp_nistz256_sqr_mont(res, res);
  471. ecp_nistz256_sqr_mont(res, res);
  472. ecp_nistz256_mul_mont(res, res, in);
  473. memcpy(r, res, sizeof(res));
  474. }
  475. /*
  476. * ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
  477. * returns one if it fits. Otherwise it returns zero.
  478. */
  479. static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
  480. const BIGNUM *in)
  481. {
  482. if (in->top > P256_LIMBS)
  483. return 0;
  484. memset(out, 0, sizeof(BN_ULONG) * P256_LIMBS);
  485. memcpy(out, in->d, sizeof(BN_ULONG) * in->top);
  486. return 1;
  487. }
  488. /* r = sum(scalar[i]*point[i]) */
  489. static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
  490. P256_POINT *r,
  491. const BIGNUM **scalar,
  492. const EC_POINT **point,
  493. int num, BN_CTX *ctx)
  494. {
  495. int i, j, ret = 0;
  496. unsigned int index;
  497. unsigned char (*p_str)[33] = NULL;
  498. const unsigned int window_size = 5;
  499. const unsigned int mask = (1 << (window_size + 1)) - 1;
  500. unsigned int wvalue;
  501. BN_ULONG tmp[P256_LIMBS];
  502. ALIGN32 P256_POINT h;
  503. const BIGNUM **scalars = NULL;
  504. P256_POINT (*table)[16] = NULL;
  505. void *table_storage = NULL;
  506. if ((table_storage =
  507. OPENSSL_malloc(num * 16 * sizeof(P256_POINT) + 64)) == NULL
  508. || (p_str =
  509. OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
  510. || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
  511. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
  512. goto err;
  513. } else {
  514. table = (void *)ALIGNPTR(table_storage, 64);
  515. }
  516. for (i = 0; i < num; i++) {
  517. P256_POINT *row = table[i];
  518. /* This is an unusual input, we don't guarantee constant-timeness. */
  519. if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
  520. BIGNUM *mod;
  521. if ((mod = BN_CTX_get(ctx)) == NULL)
  522. goto err;
  523. if (!BN_nnmod(mod, scalar[i], &group->order, ctx)) {
  524. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
  525. goto err;
  526. }
  527. scalars[i] = mod;
  528. } else
  529. scalars[i] = scalar[i];
  530. for (j = 0; j < scalars[i]->top * BN_BYTES; j += BN_BYTES) {
  531. BN_ULONG d = scalars[i]->d[j / BN_BYTES];
  532. p_str[i][j + 0] = d & 0xff;
  533. p_str[i][j + 1] = (d >> 8) & 0xff;
  534. p_str[i][j + 2] = (d >> 16) & 0xff;
  535. p_str[i][j + 3] = (d >>= 24) & 0xff;
  536. if (BN_BYTES == 8) {
  537. d >>= 8;
  538. p_str[i][j + 4] = d & 0xff;
  539. p_str[i][j + 5] = (d >> 8) & 0xff;
  540. p_str[i][j + 6] = (d >> 16) & 0xff;
  541. p_str[i][j + 7] = (d >> 24) & 0xff;
  542. }
  543. }
  544. for (; j < 33; j++)
  545. p_str[i][j] = 0;
  546. /* table[0] is implicitly (0,0,0) (the point at infinity),
  547. * therefore it is not stored. All other values are actually
  548. * stored with an offset of -1 in table.
  549. */
  550. if (!ecp_nistz256_bignum_to_field_elem(row[1 - 1].X, &point[i]->X)
  551. || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Y, &point[i]->Y)
  552. || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Z, &point[i]->Z)) {
  553. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, EC_R_COORDINATES_OUT_OF_RANGE);
  554. goto err;
  555. }
  556. ecp_nistz256_point_double(&row[ 2 - 1], &row[ 1 - 1]);
  557. ecp_nistz256_point_add (&row[ 3 - 1], &row[ 2 - 1], &row[1 - 1]);
  558. ecp_nistz256_point_double(&row[ 4 - 1], &row[ 2 - 1]);
  559. ecp_nistz256_point_double(&row[ 6 - 1], &row[ 3 - 1]);
  560. ecp_nistz256_point_double(&row[ 8 - 1], &row[ 4 - 1]);
  561. ecp_nistz256_point_double(&row[12 - 1], &row[ 6 - 1]);
  562. ecp_nistz256_point_add (&row[ 5 - 1], &row[ 4 - 1], &row[1 - 1]);
  563. ecp_nistz256_point_add (&row[ 7 - 1], &row[ 6 - 1], &row[1 - 1]);
  564. ecp_nistz256_point_add (&row[ 9 - 1], &row[ 8 - 1], &row[1 - 1]);
  565. ecp_nistz256_point_add (&row[13 - 1], &row[12 - 1], &row[1 - 1]);
  566. ecp_nistz256_point_double(&row[14 - 1], &row[ 7 - 1]);
  567. ecp_nistz256_point_double(&row[10 - 1], &row[ 5 - 1]);
  568. ecp_nistz256_point_add (&row[15 - 1], &row[14 - 1], &row[1 - 1]);
  569. ecp_nistz256_point_add (&row[11 - 1], &row[10 - 1], &row[1 - 1]);
  570. ecp_nistz256_point_add (&row[16 - 1], &row[15 - 1], &row[1 - 1]);
  571. }
  572. index = 255;
  573. wvalue = p_str[0][(index - 1) / 8];
  574. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  575. ecp_nistz256_select_w5(r, table[0], _booth_recode_w5(wvalue) >> 1);
  576. while (index >= 5) {
  577. for (i = (index == 255 ? 1 : 0); i < num; i++) {
  578. unsigned int off = (index - 1) / 8;
  579. wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
  580. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  581. wvalue = _booth_recode_w5(wvalue);
  582. ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
  583. ecp_nistz256_neg(tmp, h.Y);
  584. copy_conditional(h.Y, tmp, (wvalue & 1));
  585. ecp_nistz256_point_add(r, r, &h);
  586. }
  587. index -= window_size;
  588. ecp_nistz256_point_double(r, r);
  589. ecp_nistz256_point_double(r, r);
  590. ecp_nistz256_point_double(r, r);
  591. ecp_nistz256_point_double(r, r);
  592. ecp_nistz256_point_double(r, r);
  593. }
  594. /* Final window */
  595. for (i = 0; i < num; i++) {
  596. wvalue = p_str[i][0];
  597. wvalue = (wvalue << 1) & mask;
  598. wvalue = _booth_recode_w5(wvalue);
  599. ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
  600. ecp_nistz256_neg(tmp, h.Y);
  601. copy_conditional(h.Y, tmp, wvalue & 1);
  602. ecp_nistz256_point_add(r, r, &h);
  603. }
  604. ret = 1;
  605. err:
  606. if (table_storage)
  607. OPENSSL_free(table_storage);
  608. if (p_str)
  609. OPENSSL_free(p_str);
  610. if (scalars)
  611. OPENSSL_free(scalars);
  612. return ret;
  613. }
  614. /* Coordinates of G, for which we have precomputed tables */
  615. const static BN_ULONG def_xG[P256_LIMBS] = {
  616. TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
  617. TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
  618. };
  619. const static BN_ULONG def_yG[P256_LIMBS] = {
  620. TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
  621. TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
  622. };
  623. /*
  624. * ecp_nistz256_is_affine_G returns one if |generator| is the standard, P-256
  625. * generator.
  626. */
  627. static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
  628. {
  629. return (generator->X.top == P256_LIMBS) &&
  630. (generator->Y.top == P256_LIMBS) &&
  631. is_equal(generator->X.d, def_xG) &&
  632. is_equal(generator->Y.d, def_yG) && is_one(&generator->Z);
  633. }
  634. static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
  635. {
  636. /*
  637. * We precompute a table for a Booth encoded exponent (wNAF) based
  638. * computation. Each table holds 64 values for safe access, with an
  639. * implicit value of infinity at index zero. We use window of size 7, and
  640. * therefore require ceil(256/7) = 37 tables.
  641. */
  642. BIGNUM *order;
  643. EC_POINT *P = NULL, *T = NULL;
  644. const EC_POINT *generator;
  645. EC_PRE_COMP *pre_comp;
  646. BN_CTX *new_ctx = NULL;
  647. int i, j, k, ret = 0;
  648. size_t w;
  649. PRECOMP256_ROW *preComputedTable = NULL;
  650. unsigned char *precomp_storage = NULL;
  651. /* if there is an old EC_PRE_COMP object, throw it away */
  652. EC_EX_DATA_free_data(&group->extra_data, ecp_nistz256_pre_comp_dup,
  653. ecp_nistz256_pre_comp_free,
  654. ecp_nistz256_pre_comp_clear_free);
  655. generator = EC_GROUP_get0_generator(group);
  656. if (generator == NULL) {
  657. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
  658. return 0;
  659. }
  660. if (ecp_nistz256_is_affine_G(generator)) {
  661. /*
  662. * No need to calculate tables for the standard generator because we
  663. * have them statically.
  664. */
  665. return 1;
  666. }
  667. if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
  668. return 0;
  669. if (ctx == NULL) {
  670. ctx = new_ctx = BN_CTX_new();
  671. if (ctx == NULL)
  672. goto err;
  673. }
  674. BN_CTX_start(ctx);
  675. order = BN_CTX_get(ctx);
  676. if (order == NULL)
  677. goto err;
  678. if (!EC_GROUP_get_order(group, order, ctx))
  679. goto err;
  680. if (BN_is_zero(order)) {
  681. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
  682. goto err;
  683. }
  684. w = 7;
  685. if ((precomp_storage =
  686. OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
  687. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
  688. goto err;
  689. } else {
  690. preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
  691. }
  692. P = EC_POINT_new(group);
  693. T = EC_POINT_new(group);
  694. if (P == NULL || T == NULL)
  695. goto err;
  696. /*
  697. * The zero entry is implicitly infinity, and we skip it, storing other
  698. * values with -1 offset.
  699. */
  700. if (!EC_POINT_copy(T, generator))
  701. goto err;
  702. for (k = 0; k < 64; k++) {
  703. if (!EC_POINT_copy(P, T))
  704. goto err;
  705. for (j = 0; j < 37; j++) {
  706. /*
  707. * It would be faster to use EC_POINTs_make_affine and
  708. * make multiple points affine at the same time.
  709. */
  710. if (!EC_POINT_make_affine(group, P, ctx))
  711. goto err;
  712. if (!ecp_nistz256_bignum_to_field_elem(preComputedTable[j][k].X,
  713. &P->X) ||
  714. !ecp_nistz256_bignum_to_field_elem(preComputedTable[j][k].Y,
  715. &P->Y)) {
  716. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE,
  717. EC_R_COORDINATES_OUT_OF_RANGE);
  718. goto err;
  719. }
  720. for (i = 0; i < 7; i++) {
  721. if (!EC_POINT_dbl(group, P, P, ctx))
  722. goto err;
  723. }
  724. }
  725. if (!EC_POINT_add(group, T, T, generator, ctx))
  726. goto err;
  727. }
  728. pre_comp->group = group;
  729. pre_comp->w = w;
  730. pre_comp->precomp = preComputedTable;
  731. pre_comp->precomp_storage = precomp_storage;
  732. precomp_storage = NULL;
  733. if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
  734. ecp_nistz256_pre_comp_dup,
  735. ecp_nistz256_pre_comp_free,
  736. ecp_nistz256_pre_comp_clear_free)) {
  737. goto err;
  738. }
  739. pre_comp = NULL;
  740. ret = 1;
  741. err:
  742. if (ctx != NULL)
  743. BN_CTX_end(ctx);
  744. BN_CTX_free(new_ctx);
  745. if (pre_comp)
  746. ecp_nistz256_pre_comp_free(pre_comp);
  747. if (precomp_storage)
  748. OPENSSL_free(precomp_storage);
  749. if (P)
  750. EC_POINT_free(P);
  751. if (T)
  752. EC_POINT_free(T);
  753. return ret;
  754. }
  755. /*
  756. * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
  757. * code processing 4 points in parallel, corresponding serial operation
  758. * is several times slower, because it uses 29x29=58-bit multiplication
  759. * as opposite to 64x64=128-bit in integer-only scalar case. As result
  760. * it doesn't provide *significant* performance improvement. Note that
  761. * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
  762. * you'd need to compile even asm/ecp_nistz256-avx.pl module.
  763. */
  764. #if defined(ECP_NISTZ256_AVX2)
  765. # if !(defined(__x86_64) || defined(__x86_64__)) || \
  766. defined(_M_AMD64) || defined(_MX64)) || \
  767. !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
  768. # undef ECP_NISTZ256_AVX2
  769. # else
  770. /* Constant time access, loading four values, from four consecutive tables */
  771. void ecp_nistz256_avx2_select_w7(P256_POINT_AFFINE * val,
  772. const P256_POINT_AFFINE * in_t, int index);
  773. void ecp_nistz256_avx2_multi_select_w7(void *result, const void *in, int index0,
  774. int index1, int index2, int index3);
  775. void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
  776. void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
  777. void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
  778. const void *Bx4);
  779. void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
  780. const void *Bx4);
  781. void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
  782. void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
  783. void ecp_nistz256_avx2_set1(void *RESULTx4);
  784. int ecp_nistz_avx2_eligible(void);
  785. static void booth_recode_w7(unsigned char *sign,
  786. unsigned char *digit, unsigned char in)
  787. {
  788. unsigned char s, d;
  789. s = ~((in >> 7) - 1);
  790. d = (1 << 8) - in - 1;
  791. d = (d & s) | (in & ~s);
  792. d = (d >> 1) + (d & 1);
  793. *sign = s & 1;
  794. *digit = d;
  795. }
  796. /*
  797. * ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
  798. * precomputed table. It does 4 affine point additions in parallel,
  799. * significantly speeding up point multiplication for a fixed value.
  800. */
  801. static void ecp_nistz256_avx2_mul_g(P256_POINT *r,
  802. unsigned char p_str[33],
  803. const P256_POINT_AFFINE(*preComputedTable)[64])
  804. {
  805. const unsigned int window_size = 7;
  806. const unsigned int mask = (1 << (window_size + 1)) - 1;
  807. unsigned int wvalue;
  808. /* Using 4 windows at a time */
  809. unsigned char sign0, digit0;
  810. unsigned char sign1, digit1;
  811. unsigned char sign2, digit2;
  812. unsigned char sign3, digit3;
  813. unsigned int index = 0;
  814. BN_ULONG tmp[P256_LIMBS];
  815. int i;
  816. ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
  817. ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
  818. ALIGN32 P256_POINT_AFFINE point_arr[P256_LIMBS];
  819. ALIGN32 P256_POINT res_point_arr[P256_LIMBS];
  820. /* Initial four windows */
  821. wvalue = *((u16 *) & p_str[0]);
  822. wvalue = (wvalue << 1) & mask;
  823. index += window_size;
  824. booth_recode_w7(&sign0, &digit0, wvalue);
  825. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  826. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  827. index += window_size;
  828. booth_recode_w7(&sign1, &digit1, wvalue);
  829. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  830. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  831. index += window_size;
  832. booth_recode_w7(&sign2, &digit2, wvalue);
  833. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  834. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  835. index += window_size;
  836. booth_recode_w7(&sign3, &digit3, wvalue);
  837. ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[0],
  838. digit0, digit1, digit2, digit3);
  839. ecp_nistz256_neg(tmp, point_arr[0].Y);
  840. copy_conditional(point_arr[0].Y, tmp, sign0);
  841. ecp_nistz256_neg(tmp, point_arr[1].Y);
  842. copy_conditional(point_arr[1].Y, tmp, sign1);
  843. ecp_nistz256_neg(tmp, point_arr[2].Y);
  844. copy_conditional(point_arr[2].Y, tmp, sign2);
  845. ecp_nistz256_neg(tmp, point_arr[3].Y);
  846. copy_conditional(point_arr[3].Y, tmp, sign3);
  847. ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
  848. ecp_nistz256_avx2_to_mont(aX4, aX4);
  849. ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
  850. ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
  851. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  852. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  853. index += window_size;
  854. booth_recode_w7(&sign0, &digit0, wvalue);
  855. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  856. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  857. index += window_size;
  858. booth_recode_w7(&sign1, &digit1, wvalue);
  859. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  860. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  861. index += window_size;
  862. booth_recode_w7(&sign2, &digit2, wvalue);
  863. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  864. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  865. index += window_size;
  866. booth_recode_w7(&sign3, &digit3, wvalue);
  867. ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[4 * 1],
  868. digit0, digit1, digit2, digit3);
  869. ecp_nistz256_neg(tmp, point_arr[0].Y);
  870. copy_conditional(point_arr[0].Y, tmp, sign0);
  871. ecp_nistz256_neg(tmp, point_arr[1].Y);
  872. copy_conditional(point_arr[1].Y, tmp, sign1);
  873. ecp_nistz256_neg(tmp, point_arr[2].Y);
  874. copy_conditional(point_arr[2].Y, tmp, sign2);
  875. ecp_nistz256_neg(tmp, point_arr[3].Y);
  876. copy_conditional(point_arr[3].Y, tmp, sign3);
  877. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  878. ecp_nistz256_avx2_to_mont(bX4, bX4);
  879. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  880. /* Optimized when both inputs are affine */
  881. ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
  882. for (i = 2; i < 9; i++) {
  883. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  884. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  885. index += window_size;
  886. booth_recode_w7(&sign0, &digit0, wvalue);
  887. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  888. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  889. index += window_size;
  890. booth_recode_w7(&sign1, &digit1, wvalue);
  891. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  892. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  893. index += window_size;
  894. booth_recode_w7(&sign2, &digit2, wvalue);
  895. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  896. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  897. index += window_size;
  898. booth_recode_w7(&sign3, &digit3, wvalue);
  899. ecp_nistz256_avx2_multi_select_w7(point_arr,
  900. preComputedTable[4 * i],
  901. digit0, digit1, digit2, digit3);
  902. ecp_nistz256_neg(tmp, point_arr[0].Y);
  903. copy_conditional(point_arr[0].Y, tmp, sign0);
  904. ecp_nistz256_neg(tmp, point_arr[1].Y);
  905. copy_conditional(point_arr[1].Y, tmp, sign1);
  906. ecp_nistz256_neg(tmp, point_arr[2].Y);
  907. copy_conditional(point_arr[2].Y, tmp, sign2);
  908. ecp_nistz256_neg(tmp, point_arr[3].Y);
  909. copy_conditional(point_arr[3].Y, tmp, sign3);
  910. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  911. ecp_nistz256_avx2_to_mont(bX4, bX4);
  912. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  913. ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
  914. }
  915. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
  916. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
  917. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
  918. ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
  919. /* Last window is performed serially */
  920. wvalue = *((u16 *) & p_str[(index - 1) / 8]);
  921. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  922. booth_recode_w7(&sign0, &digit0, wvalue);
  923. ecp_nistz256_avx2_select_w7((P256_POINT_AFFINE *) r,
  924. preComputedTable[36], digit0);
  925. ecp_nistz256_neg(tmp, r->Y);
  926. copy_conditional(r->Y, tmp, sign0);
  927. memcpy(r->Z, ONE, sizeof(ONE));
  928. /* Sum the four windows */
  929. ecp_nistz256_point_add(r, r, &res_point_arr[0]);
  930. ecp_nistz256_point_add(r, r, &res_point_arr[1]);
  931. ecp_nistz256_point_add(r, r, &res_point_arr[2]);
  932. ecp_nistz256_point_add(r, r, &res_point_arr[3]);
  933. }
  934. # endif
  935. #endif
  936. static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
  937. const P256_POINT_AFFINE *in,
  938. BN_CTX *ctx)
  939. {
  940. BIGNUM x, y;
  941. BN_ULONG d_x[P256_LIMBS], d_y[P256_LIMBS];
  942. int ret = 0;
  943. memcpy(d_x, in->X, sizeof(d_x));
  944. x.d = d_x;
  945. x.dmax = x.top = P256_LIMBS;
  946. x.neg = 0;
  947. x.flags = BN_FLG_STATIC_DATA;
  948. memcpy(d_y, in->Y, sizeof(d_y));
  949. y.d = d_y;
  950. y.dmax = y.top = P256_LIMBS;
  951. y.neg = 0;
  952. y.flags = BN_FLG_STATIC_DATA;
  953. ret = EC_POINT_set_affine_coordinates_GFp(group, out, &x, &y, ctx);
  954. return ret;
  955. }
  956. /* r = scalar*G + sum(scalars[i]*points[i]) */
  957. static int ecp_nistz256_points_mul(const EC_GROUP *group,
  958. EC_POINT *r,
  959. const BIGNUM *scalar,
  960. size_t num,
  961. const EC_POINT *points[],
  962. const BIGNUM *scalars[], BN_CTX *ctx)
  963. {
  964. int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
  965. size_t j;
  966. unsigned char p_str[33] = { 0 };
  967. const PRECOMP256_ROW *preComputedTable = NULL;
  968. const EC_PRE_COMP *pre_comp = NULL;
  969. const EC_POINT *generator = NULL;
  970. unsigned int index = 0;
  971. BN_CTX *new_ctx = NULL;
  972. const BIGNUM **new_scalars = NULL;
  973. const EC_POINT **new_points = NULL;
  974. const unsigned int window_size = 7;
  975. const unsigned int mask = (1 << (window_size + 1)) - 1;
  976. unsigned int wvalue;
  977. ALIGN32 union {
  978. P256_POINT p;
  979. P256_POINT_AFFINE a;
  980. } t, p;
  981. BIGNUM *tmp_scalar;
  982. if (group->meth != r->meth) {
  983. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  984. return 0;
  985. }
  986. if ((scalar == NULL) && (num == 0))
  987. return EC_POINT_set_to_infinity(group, r);
  988. for (j = 0; j < num; j++) {
  989. if (group->meth != points[j]->meth) {
  990. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  991. return 0;
  992. }
  993. }
  994. if (ctx == NULL) {
  995. ctx = new_ctx = BN_CTX_new();
  996. if (ctx == NULL)
  997. goto err;
  998. }
  999. BN_CTX_start(ctx);
  1000. if (scalar) {
  1001. generator = EC_GROUP_get0_generator(group);
  1002. if (generator == NULL) {
  1003. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
  1004. goto err;
  1005. }
  1006. /* look if we can use precomputed multiples of generator */
  1007. pre_comp =
  1008. EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
  1009. ecp_nistz256_pre_comp_free,
  1010. ecp_nistz256_pre_comp_clear_free);
  1011. if (pre_comp) {
  1012. /*
  1013. * If there is a precomputed table for the generator, check that
  1014. * it was generated with the same generator.
  1015. */
  1016. EC_POINT *pre_comp_generator = EC_POINT_new(group);
  1017. if (pre_comp_generator == NULL)
  1018. goto err;
  1019. if (!ecp_nistz256_set_from_affine
  1020. (pre_comp_generator, group, pre_comp->precomp[0], ctx)) {
  1021. EC_POINT_free(pre_comp_generator);
  1022. goto err;
  1023. }
  1024. if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
  1025. preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
  1026. EC_POINT_free(pre_comp_generator);
  1027. }
  1028. if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
  1029. /*
  1030. * If there is no precomputed data, but the generator
  1031. * is the default, a hardcoded table of precomputed
  1032. * data is used. This is because applications, such as
  1033. * Apache, do not use EC_KEY_precompute_mult.
  1034. */
  1035. preComputedTable = (const PRECOMP256_ROW *)ecp_nistz256_precomputed;
  1036. }
  1037. if (preComputedTable) {
  1038. if ((BN_num_bits(scalar) > 256)
  1039. || BN_is_negative(scalar)) {
  1040. if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
  1041. goto err;
  1042. if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
  1043. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
  1044. goto err;
  1045. }
  1046. scalar = tmp_scalar;
  1047. }
  1048. for (i = 0; i < scalar->top * BN_BYTES; i += BN_BYTES) {
  1049. BN_ULONG d = scalar->d[i / BN_BYTES];
  1050. p_str[i + 0] = d & 0xff;
  1051. p_str[i + 1] = (d >> 8) & 0xff;
  1052. p_str[i + 2] = (d >> 16) & 0xff;
  1053. p_str[i + 3] = (d >>= 24) & 0xff;
  1054. if (BN_BYTES == 8) {
  1055. d >>= 8;
  1056. p_str[i + 4] = d & 0xff;
  1057. p_str[i + 5] = (d >> 8) & 0xff;
  1058. p_str[i + 6] = (d >> 16) & 0xff;
  1059. p_str[i + 7] = (d >> 24) & 0xff;
  1060. }
  1061. }
  1062. for (; i < 33; i++)
  1063. p_str[i] = 0;
  1064. #if defined(ECP_NISTZ256_AVX2)
  1065. if (ecp_nistz_avx2_eligible()) {
  1066. ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
  1067. } else
  1068. #endif
  1069. {
  1070. BN_ULONG infty;
  1071. /* First window */
  1072. wvalue = (p_str[0] << 1) & mask;
  1073. index += window_size;
  1074. wvalue = _booth_recode_w7(wvalue);
  1075. ecp_nistz256_select_w7(&p.a, preComputedTable[0], wvalue >> 1);
  1076. ecp_nistz256_neg(p.p.Z, p.p.Y);
  1077. copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
  1078. /*
  1079. * Since affine infinity is encoded as (0,0) and
  1080. * Jacobian ias (,,0), we need to harmonize them
  1081. * by assigning "one" or zero to Z.
  1082. */
  1083. infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |
  1084. p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);
  1085. if (P256_LIMBS == 8)
  1086. infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |
  1087. p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);
  1088. infty = 0 - is_zero(infty);
  1089. infty = ~infty;
  1090. p.p.Z[0] = ONE[0] & infty;
  1091. p.p.Z[1] = ONE[1] & infty;
  1092. p.p.Z[2] = ONE[2] & infty;
  1093. p.p.Z[3] = ONE[3] & infty;
  1094. if (P256_LIMBS == 8) {
  1095. p.p.Z[4] = ONE[4] & infty;
  1096. p.p.Z[5] = ONE[5] & infty;
  1097. p.p.Z[6] = ONE[6] & infty;
  1098. p.p.Z[7] = ONE[7] & infty;
  1099. }
  1100. for (i = 1; i < 37; i++) {
  1101. unsigned int off = (index - 1) / 8;
  1102. wvalue = p_str[off] | p_str[off + 1] << 8;
  1103. wvalue = (wvalue >> ((index - 1) % 8)) & mask;
  1104. index += window_size;
  1105. wvalue = _booth_recode_w7(wvalue);
  1106. ecp_nistz256_select_w7(&t.a,
  1107. preComputedTable[i], wvalue >> 1);
  1108. ecp_nistz256_neg(t.p.Z, t.a.Y);
  1109. copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
  1110. ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
  1111. }
  1112. }
  1113. } else {
  1114. p_is_infinity = 1;
  1115. no_precomp_for_generator = 1;
  1116. }
  1117. } else
  1118. p_is_infinity = 1;
  1119. if (no_precomp_for_generator) {
  1120. /*
  1121. * Without a precomputed table for the generator, it has to be
  1122. * handled like a normal point.
  1123. */
  1124. new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
  1125. if (!new_scalars) {
  1126. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1127. goto err;
  1128. }
  1129. new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
  1130. if (!new_points) {
  1131. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1132. goto err;
  1133. }
  1134. memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
  1135. new_scalars[num] = scalar;
  1136. memcpy(new_points, points, num * sizeof(EC_POINT *));
  1137. new_points[num] = generator;
  1138. scalars = new_scalars;
  1139. points = new_points;
  1140. num++;
  1141. }
  1142. if (num) {
  1143. P256_POINT *out = &t.p;
  1144. if (p_is_infinity)
  1145. out = &p.p;
  1146. if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
  1147. goto err;
  1148. if (!p_is_infinity)
  1149. ecp_nistz256_point_add(&p.p, &p.p, out);
  1150. }
  1151. /* Not constant-time, but we're only operating on the public output. */
  1152. if (!ecp_nistz256_set_words(&r->X, p.p.X) ||
  1153. !ecp_nistz256_set_words(&r->Y, p.p.Y) ||
  1154. !ecp_nistz256_set_words(&r->Z, p.p.Z)) {
  1155. goto err;
  1156. }
  1157. r->Z_is_one = is_one(&r->Z) & 1;
  1158. ret = 1;
  1159. err:
  1160. if (ctx)
  1161. BN_CTX_end(ctx);
  1162. BN_CTX_free(new_ctx);
  1163. if (new_points)
  1164. OPENSSL_free(new_points);
  1165. if (new_scalars)
  1166. OPENSSL_free(new_scalars);
  1167. return ret;
  1168. }
  1169. static int ecp_nistz256_get_affine(const EC_GROUP *group,
  1170. const EC_POINT *point,
  1171. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  1172. {
  1173. BN_ULONG z_inv2[P256_LIMBS];
  1174. BN_ULONG z_inv3[P256_LIMBS];
  1175. BN_ULONG x_aff[P256_LIMBS];
  1176. BN_ULONG y_aff[P256_LIMBS];
  1177. BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
  1178. BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
  1179. if (EC_POINT_is_at_infinity(group, point)) {
  1180. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
  1181. return 0;
  1182. }
  1183. if (!ecp_nistz256_bignum_to_field_elem(point_x, &point->X) ||
  1184. !ecp_nistz256_bignum_to_field_elem(point_y, &point->Y) ||
  1185. !ecp_nistz256_bignum_to_field_elem(point_z, &point->Z)) {
  1186. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
  1187. return 0;
  1188. }
  1189. ecp_nistz256_mod_inverse(z_inv3, point_z);
  1190. ecp_nistz256_sqr_mont(z_inv2, z_inv3);
  1191. ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
  1192. if (x != NULL) {
  1193. ecp_nistz256_from_mont(x_ret, x_aff);
  1194. if (!ecp_nistz256_set_words(x, x_ret))
  1195. return 0;
  1196. }
  1197. if (y != NULL) {
  1198. ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
  1199. ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
  1200. ecp_nistz256_from_mont(y_ret, y_aff);
  1201. if (!ecp_nistz256_set_words(y, y_ret))
  1202. return 0;
  1203. }
  1204. return 1;
  1205. }
  1206. static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
  1207. {
  1208. EC_PRE_COMP *ret = NULL;
  1209. if (!group)
  1210. return NULL;
  1211. ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
  1212. if (!ret) {
  1213. ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1214. return ret;
  1215. }
  1216. ret->group = group;
  1217. ret->w = 6; /* default */
  1218. ret->precomp = NULL;
  1219. ret->precomp_storage = NULL;
  1220. ret->references = 1;
  1221. return ret;
  1222. }
  1223. static void *ecp_nistz256_pre_comp_dup(void *src_)
  1224. {
  1225. EC_PRE_COMP *src = src_;
  1226. /* no need to actually copy, these objects never change! */
  1227. CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  1228. return src_;
  1229. }
  1230. static void ecp_nistz256_pre_comp_free(void *pre_)
  1231. {
  1232. int i;
  1233. EC_PRE_COMP *pre = pre_;
  1234. if (!pre)
  1235. return;
  1236. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  1237. if (i > 0)
  1238. return;
  1239. if (pre->precomp_storage)
  1240. OPENSSL_free(pre->precomp_storage);
  1241. OPENSSL_free(pre);
  1242. }
  1243. static void ecp_nistz256_pre_comp_clear_free(void *pre_)
  1244. {
  1245. int i;
  1246. EC_PRE_COMP *pre = pre_;
  1247. if (!pre)
  1248. return;
  1249. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  1250. if (i > 0)
  1251. return;
  1252. if (pre->precomp_storage) {
  1253. OPENSSL_cleanse(pre->precomp,
  1254. 32 * sizeof(unsigned char) * (1 << pre->w) * 2 * 37);
  1255. OPENSSL_free(pre->precomp_storage);
  1256. }
  1257. OPENSSL_cleanse(pre, sizeof *pre);
  1258. OPENSSL_free(pre);
  1259. }
  1260. static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
  1261. {
  1262. /* There is a hard-coded table for the default generator. */
  1263. const EC_POINT *generator = EC_GROUP_get0_generator(group);
  1264. if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
  1265. /* There is a hard-coded table for the default generator. */
  1266. return 1;
  1267. }
  1268. return EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
  1269. ecp_nistz256_pre_comp_free,
  1270. ecp_nistz256_pre_comp_clear_free) != NULL;
  1271. }
  1272. const EC_METHOD *EC_GFp_nistz256_method(void)
  1273. {
  1274. static const EC_METHOD ret = {
  1275. EC_FLAGS_DEFAULT_OCT,
  1276. NID_X9_62_prime_field,
  1277. ec_GFp_mont_group_init,
  1278. ec_GFp_mont_group_finish,
  1279. ec_GFp_mont_group_clear_finish,
  1280. ec_GFp_mont_group_copy,
  1281. ec_GFp_mont_group_set_curve,
  1282. ec_GFp_simple_group_get_curve,
  1283. ec_GFp_simple_group_get_degree,
  1284. ec_GFp_simple_group_check_discriminant,
  1285. ec_GFp_simple_point_init,
  1286. ec_GFp_simple_point_finish,
  1287. ec_GFp_simple_point_clear_finish,
  1288. ec_GFp_simple_point_copy,
  1289. ec_GFp_simple_point_set_to_infinity,
  1290. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  1291. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  1292. ec_GFp_simple_point_set_affine_coordinates,
  1293. ecp_nistz256_get_affine,
  1294. 0, 0, 0,
  1295. ec_GFp_simple_add,
  1296. ec_GFp_simple_dbl,
  1297. ec_GFp_simple_invert,
  1298. ec_GFp_simple_is_at_infinity,
  1299. ec_GFp_simple_is_on_curve,
  1300. ec_GFp_simple_cmp,
  1301. ec_GFp_simple_make_affine,
  1302. ec_GFp_simple_points_make_affine,
  1303. ecp_nistz256_points_mul, /* mul */
  1304. ecp_nistz256_mult_precompute, /* precompute_mult */
  1305. ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
  1306. ec_GFp_mont_field_mul,
  1307. ec_GFp_mont_field_sqr,
  1308. 0, /* field_div */
  1309. ec_GFp_mont_field_encode,
  1310. ec_GFp_mont_field_decode,
  1311. ec_GFp_mont_field_set_to_one
  1312. };
  1313. return &ret;
  1314. }