ecp_nistz256.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /*
  2. * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
  4. * Copyright (c) 2015, CloudFlare, Inc.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. *
  11. * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1, 3)
  12. * (1) Intel Corporation, Israel Development Center, Haifa, Israel
  13. * (2) University of Haifa, Israel
  14. * (3) CloudFlare, Inc.
  15. *
  16. * Reference:
  17. * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
  18. * 256 Bit Primes"
  19. */
  20. #include <string.h>
  21. #include "internal/cryptlib.h"
  22. #include "crypto/bn.h"
  23. #include "ec_local.h"
  24. #include "internal/refcount.h"
  25. #if BN_BITS2 != 64
  26. # define TOBN(hi,lo) lo,hi
  27. #else
  28. # define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo)
  29. #endif
  30. #if defined(__GNUC__)
  31. # define ALIGN32 __attribute((aligned(32)))
  32. #elif defined(_MSC_VER)
  33. # define ALIGN32 __declspec(align(32))
  34. #else
  35. # define ALIGN32
  36. #endif
  37. #define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N)
  38. #define P256_LIMBS (256/BN_BITS2)
  39. typedef unsigned short u16;
  40. typedef struct {
  41. BN_ULONG X[P256_LIMBS];
  42. BN_ULONG Y[P256_LIMBS];
  43. BN_ULONG Z[P256_LIMBS];
  44. } P256_POINT;
  45. typedef struct {
  46. BN_ULONG X[P256_LIMBS];
  47. BN_ULONG Y[P256_LIMBS];
  48. } P256_POINT_AFFINE;
  49. typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
  50. /* structure for precomputed multiples of the generator */
  51. struct nistz256_pre_comp_st {
  52. const EC_GROUP *group; /* Parent EC_GROUP object */
  53. size_t w; /* Window size */
  54. /*
  55. * Constant time access to the X and Y coordinates of the pre-computed,
  56. * generator multiplies, in the Montgomery domain. Pre-calculated
  57. * multiplies are stored in affine form.
  58. */
  59. PRECOMP256_ROW *precomp;
  60. void *precomp_storage;
  61. CRYPTO_REF_COUNT references;
  62. CRYPTO_RWLOCK *lock;
  63. };
  64. /* Functions implemented in assembly */
  65. /*
  66. * Most of below mentioned functions *preserve* the property of inputs
  67. * being fully reduced, i.e. being in [0, modulus) range. Simply put if
  68. * inputs are fully reduced, then output is too. Note that reverse is
  69. * not true, in sense that given partially reduced inputs output can be
  70. * either, not unlikely reduced. And "most" in first sentence refers to
  71. * the fact that given the calculations flow one can tolerate that
  72. * addition, 1st function below, produces partially reduced result *if*
  73. * multiplications by 2 and 3, which customarily use addition, fully
  74. * reduce it. This effectively gives two options: a) addition produces
  75. * fully reduced result [as long as inputs are, just like remaining
  76. * functions]; b) addition is allowed to produce partially reduced
  77. * result, but multiplications by 2 and 3 perform additional reduction
  78. * step. Choice between the two can be platform-specific, but it was a)
  79. * in all cases so far...
  80. */
  81. /* Modular add: res = a+b mod P */
  82. void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
  83. const BN_ULONG a[P256_LIMBS],
  84. const BN_ULONG b[P256_LIMBS]);
  85. /* Modular mul by 2: res = 2*a mod P */
  86. void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
  87. const BN_ULONG a[P256_LIMBS]);
  88. /* Modular mul by 3: res = 3*a mod P */
  89. void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
  90. const BN_ULONG a[P256_LIMBS]);
  91. /* Modular div by 2: res = a/2 mod P */
  92. void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
  93. const BN_ULONG a[P256_LIMBS]);
  94. /* Modular sub: res = a-b mod P */
  95. void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
  96. const BN_ULONG a[P256_LIMBS],
  97. const BN_ULONG b[P256_LIMBS]);
  98. /* Modular neg: res = -a mod P */
  99. void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
  100. /* Montgomery mul: res = a*b*2^-256 mod P */
  101. void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
  102. const BN_ULONG a[P256_LIMBS],
  103. const BN_ULONG b[P256_LIMBS]);
  104. /* Montgomery sqr: res = a*a*2^-256 mod P */
  105. void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
  106. const BN_ULONG a[P256_LIMBS]);
  107. /* Convert a number from Montgomery domain, by multiplying with 1 */
  108. void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
  109. const BN_ULONG in[P256_LIMBS]);
  110. /* Convert a number to Montgomery domain, by multiplying with 2^512 mod P*/
  111. void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
  112. const BN_ULONG in[P256_LIMBS]);
  113. /* Functions that perform constant time access to the precomputed tables */
  114. void ecp_nistz256_scatter_w5(P256_POINT *val,
  115. const P256_POINT *in_t, int idx);
  116. void ecp_nistz256_gather_w5(P256_POINT *val,
  117. const P256_POINT *in_t, int idx);
  118. void ecp_nistz256_scatter_w7(P256_POINT_AFFINE *val,
  119. const P256_POINT_AFFINE *in_t, int idx);
  120. void ecp_nistz256_gather_w7(P256_POINT_AFFINE *val,
  121. const P256_POINT_AFFINE *in_t, int idx);
  122. /* One converted into the Montgomery domain */
  123. static const BN_ULONG ONE[P256_LIMBS] = {
  124. TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
  125. TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
  126. };
  127. static NISTZ256_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group);
  128. /* Precomputed tables for the default generator */
  129. extern const PRECOMP256_ROW ecp_nistz256_precomputed[37];
  130. /* Recode window to a signed digit, see ecp_nistputil.c for details */
  131. static unsigned int _booth_recode_w5(unsigned int in)
  132. {
  133. unsigned int s, d;
  134. s = ~((in >> 5) - 1);
  135. d = (1 << 6) - in - 1;
  136. d = (d & s) | (in & ~s);
  137. d = (d >> 1) + (d & 1);
  138. return (d << 1) + (s & 1);
  139. }
  140. static unsigned int _booth_recode_w7(unsigned int in)
  141. {
  142. unsigned int s, d;
  143. s = ~((in >> 7) - 1);
  144. d = (1 << 8) - in - 1;
  145. d = (d & s) | (in & ~s);
  146. d = (d >> 1) + (d & 1);
  147. return (d << 1) + (s & 1);
  148. }
  149. static void copy_conditional(BN_ULONG dst[P256_LIMBS],
  150. const BN_ULONG src[P256_LIMBS], BN_ULONG move)
  151. {
  152. BN_ULONG mask1 = 0-move;
  153. BN_ULONG mask2 = ~mask1;
  154. dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
  155. dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
  156. dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
  157. dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
  158. if (P256_LIMBS == 8) {
  159. dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
  160. dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
  161. dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
  162. dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
  163. }
  164. }
  165. static BN_ULONG is_zero(BN_ULONG in)
  166. {
  167. in |= (0 - in);
  168. in = ~in;
  169. in >>= BN_BITS2 - 1;
  170. return in;
  171. }
  172. static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
  173. const BN_ULONG b[P256_LIMBS])
  174. {
  175. BN_ULONG res;
  176. res = a[0] ^ b[0];
  177. res |= a[1] ^ b[1];
  178. res |= a[2] ^ b[2];
  179. res |= a[3] ^ b[3];
  180. if (P256_LIMBS == 8) {
  181. res |= a[4] ^ b[4];
  182. res |= a[5] ^ b[5];
  183. res |= a[6] ^ b[6];
  184. res |= a[7] ^ b[7];
  185. }
  186. return is_zero(res);
  187. }
  188. static BN_ULONG is_one(const BIGNUM *z)
  189. {
  190. BN_ULONG res = 0;
  191. BN_ULONG *a = bn_get_words(z);
  192. if (bn_get_top(z) == (P256_LIMBS - P256_LIMBS / 8)) {
  193. res = a[0] ^ ONE[0];
  194. res |= a[1] ^ ONE[1];
  195. res |= a[2] ^ ONE[2];
  196. res |= a[3] ^ ONE[3];
  197. if (P256_LIMBS == 8) {
  198. res |= a[4] ^ ONE[4];
  199. res |= a[5] ^ ONE[5];
  200. res |= a[6] ^ ONE[6];
  201. /*
  202. * no check for a[7] (being zero) on 32-bit platforms,
  203. * because value of "one" takes only 7 limbs.
  204. */
  205. }
  206. res = is_zero(res);
  207. }
  208. return res;
  209. }
  210. /*
  211. * For reference, this macro is used only when new ecp_nistz256 assembly
  212. * module is being developed. For example, configure with
  213. * -DECP_NISTZ256_REFERENCE_IMPLEMENTATION and implement only functions
  214. * performing simplest arithmetic operations on 256-bit vectors. Then
  215. * work on implementation of higher-level functions performing point
  216. * operations. Then remove ECP_NISTZ256_REFERENCE_IMPLEMENTATION
  217. * and never define it again. (The correct macro denoting presence of
  218. * ecp_nistz256 module is ECP_NISTZ256_ASM.)
  219. */
  220. #ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
  221. void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
  222. void ecp_nistz256_point_add(P256_POINT *r,
  223. const P256_POINT *a, const P256_POINT *b);
  224. void ecp_nistz256_point_add_affine(P256_POINT *r,
  225. const P256_POINT *a,
  226. const P256_POINT_AFFINE *b);
  227. #else
  228. /* Point double: r = 2*a */
  229. static void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a)
  230. {
  231. BN_ULONG S[P256_LIMBS];
  232. BN_ULONG M[P256_LIMBS];
  233. BN_ULONG Zsqr[P256_LIMBS];
  234. BN_ULONG tmp0[P256_LIMBS];
  235. const BN_ULONG *in_x = a->X;
  236. const BN_ULONG *in_y = a->Y;
  237. const BN_ULONG *in_z = a->Z;
  238. BN_ULONG *res_x = r->X;
  239. BN_ULONG *res_y = r->Y;
  240. BN_ULONG *res_z = r->Z;
  241. ecp_nistz256_mul_by_2(S, in_y);
  242. ecp_nistz256_sqr_mont(Zsqr, in_z);
  243. ecp_nistz256_sqr_mont(S, S);
  244. ecp_nistz256_mul_mont(res_z, in_z, in_y);
  245. ecp_nistz256_mul_by_2(res_z, res_z);
  246. ecp_nistz256_add(M, in_x, Zsqr);
  247. ecp_nistz256_sub(Zsqr, in_x, Zsqr);
  248. ecp_nistz256_sqr_mont(res_y, S);
  249. ecp_nistz256_div_by_2(res_y, res_y);
  250. ecp_nistz256_mul_mont(M, M, Zsqr);
  251. ecp_nistz256_mul_by_3(M, M);
  252. ecp_nistz256_mul_mont(S, S, in_x);
  253. ecp_nistz256_mul_by_2(tmp0, S);
  254. ecp_nistz256_sqr_mont(res_x, M);
  255. ecp_nistz256_sub(res_x, res_x, tmp0);
  256. ecp_nistz256_sub(S, S, res_x);
  257. ecp_nistz256_mul_mont(S, S, M);
  258. ecp_nistz256_sub(res_y, S, res_y);
  259. }
  260. /* Point addition: r = a+b */
  261. static void ecp_nistz256_point_add(P256_POINT *r,
  262. const P256_POINT *a, const P256_POINT *b)
  263. {
  264. BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
  265. BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
  266. BN_ULONG Z1sqr[P256_LIMBS];
  267. BN_ULONG Z2sqr[P256_LIMBS];
  268. BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
  269. BN_ULONG Hsqr[P256_LIMBS];
  270. BN_ULONG Rsqr[P256_LIMBS];
  271. BN_ULONG Hcub[P256_LIMBS];
  272. BN_ULONG res_x[P256_LIMBS];
  273. BN_ULONG res_y[P256_LIMBS];
  274. BN_ULONG res_z[P256_LIMBS];
  275. BN_ULONG in1infty, in2infty;
  276. const BN_ULONG *in1_x = a->X;
  277. const BN_ULONG *in1_y = a->Y;
  278. const BN_ULONG *in1_z = a->Z;
  279. const BN_ULONG *in2_x = b->X;
  280. const BN_ULONG *in2_y = b->Y;
  281. const BN_ULONG *in2_z = b->Z;
  282. /*
  283. * Infinity in encoded as (,,0)
  284. */
  285. in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
  286. if (P256_LIMBS == 8)
  287. in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
  288. in2infty = (in2_z[0] | in2_z[1] | in2_z[2] | in2_z[3]);
  289. if (P256_LIMBS == 8)
  290. in2infty |= (in2_z[4] | in2_z[5] | in2_z[6] | in2_z[7]);
  291. in1infty = is_zero(in1infty);
  292. in2infty = is_zero(in2infty);
  293. ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
  294. ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
  295. ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
  296. ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
  297. ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
  298. ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
  299. ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
  300. ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
  301. ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
  302. ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
  303. /*
  304. * The formulae are incorrect if the points are equal so we check for
  305. * this and do doubling if this happens.
  306. *
  307. * Points here are in Jacobian projective coordinates (Xi, Yi, Zi)
  308. * that are bound to the affine coordinates (xi, yi) by the following
  309. * equations:
  310. * - xi = Xi / (Zi)^2
  311. * - y1 = Yi / (Zi)^3
  312. *
  313. * For the sake of optimization, the algorithm operates over
  314. * intermediate variables U1, U2 and S1, S2 that are derived from
  315. * the projective coordinates:
  316. * - U1 = X1 * (Z2)^2 ; U2 = X2 * (Z1)^2
  317. * - S1 = Y1 * (Z2)^3 ; S2 = Y2 * (Z1)^3
  318. *
  319. * It is easy to prove that is_equal(U1, U2) implies that the affine
  320. * x-coordinates are equal, or either point is at infinity.
  321. * Likewise is_equal(S1, S2) implies that the affine y-coordinates are
  322. * equal, or either point is at infinity.
  323. *
  324. * The special case of either point being the point at infinity (Z1 or Z2
  325. * is zero), is handled separately later on in this function, so we avoid
  326. * jumping to point_double here in those special cases.
  327. *
  328. * When both points are inverse of each other, we know that the affine
  329. * x-coordinates are equal, and the y-coordinates have different sign.
  330. * Therefore since U1 = U2, we know H = 0, and therefore Z3 = H*Z1*Z2
  331. * will equal 0, thus the result is infinity, if we simply let this
  332. * function continue normally.
  333. *
  334. * We use bitwise operations to avoid potential side-channels introduced by
  335. * the short-circuiting behaviour of boolean operators.
  336. */
  337. if (is_equal(U1, U2) & ~in1infty & ~in2infty & is_equal(S1, S2)) {
  338. /*
  339. * This is obviously not constant-time but it should never happen during
  340. * single point multiplication, so there is no timing leak for ECDH or
  341. * ECDSA signing.
  342. */
  343. ecp_nistz256_point_double(r, a);
  344. return;
  345. }
  346. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  347. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  348. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  349. ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
  350. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  351. ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
  352. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  353. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  354. ecp_nistz256_sub(res_x, res_x, Hcub);
  355. ecp_nistz256_sub(res_y, U2, res_x);
  356. ecp_nistz256_mul_mont(S2, S1, Hcub);
  357. ecp_nistz256_mul_mont(res_y, R, res_y);
  358. ecp_nistz256_sub(res_y, res_y, S2);
  359. copy_conditional(res_x, in2_x, in1infty);
  360. copy_conditional(res_y, in2_y, in1infty);
  361. copy_conditional(res_z, in2_z, in1infty);
  362. copy_conditional(res_x, in1_x, in2infty);
  363. copy_conditional(res_y, in1_y, in2infty);
  364. copy_conditional(res_z, in1_z, in2infty);
  365. memcpy(r->X, res_x, sizeof(res_x));
  366. memcpy(r->Y, res_y, sizeof(res_y));
  367. memcpy(r->Z, res_z, sizeof(res_z));
  368. }
  369. /* Point addition when b is known to be affine: r = a+b */
  370. static void ecp_nistz256_point_add_affine(P256_POINT *r,
  371. const P256_POINT *a,
  372. const P256_POINT_AFFINE *b)
  373. {
  374. BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
  375. BN_ULONG Z1sqr[P256_LIMBS];
  376. BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
  377. BN_ULONG Hsqr[P256_LIMBS];
  378. BN_ULONG Rsqr[P256_LIMBS];
  379. BN_ULONG Hcub[P256_LIMBS];
  380. BN_ULONG res_x[P256_LIMBS];
  381. BN_ULONG res_y[P256_LIMBS];
  382. BN_ULONG res_z[P256_LIMBS];
  383. BN_ULONG in1infty, in2infty;
  384. const BN_ULONG *in1_x = a->X;
  385. const BN_ULONG *in1_y = a->Y;
  386. const BN_ULONG *in1_z = a->Z;
  387. const BN_ULONG *in2_x = b->X;
  388. const BN_ULONG *in2_y = b->Y;
  389. /*
  390. * Infinity in encoded as (,,0)
  391. */
  392. in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
  393. if (P256_LIMBS == 8)
  394. in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
  395. /*
  396. * In affine representation we encode infinity as (0,0), which is
  397. * not on the curve, so it is OK
  398. */
  399. in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
  400. in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
  401. if (P256_LIMBS == 8)
  402. in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
  403. in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
  404. in1infty = is_zero(in1infty);
  405. in2infty = is_zero(in2infty);
  406. ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
  407. ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
  408. ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
  409. ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
  410. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  411. ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
  412. ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
  413. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  414. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  415. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  416. ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
  417. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  418. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  419. ecp_nistz256_sub(res_x, res_x, Hcub);
  420. ecp_nistz256_sub(H, U2, res_x);
  421. ecp_nistz256_mul_mont(S2, in1_y, Hcub);
  422. ecp_nistz256_mul_mont(H, H, R);
  423. ecp_nistz256_sub(res_y, H, S2);
  424. copy_conditional(res_x, in2_x, in1infty);
  425. copy_conditional(res_x, in1_x, in2infty);
  426. copy_conditional(res_y, in2_y, in1infty);
  427. copy_conditional(res_y, in1_y, in2infty);
  428. copy_conditional(res_z, ONE, in1infty);
  429. copy_conditional(res_z, in1_z, in2infty);
  430. memcpy(r->X, res_x, sizeof(res_x));
  431. memcpy(r->Y, res_y, sizeof(res_y));
  432. memcpy(r->Z, res_z, sizeof(res_z));
  433. }
  434. #endif
  435. /* r = in^-1 mod p */
  436. static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
  437. const BN_ULONG in[P256_LIMBS])
  438. {
  439. /*
  440. * The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff
  441. * ffffffff ffffffff We use FLT and used poly-2 as exponent
  442. */
  443. BN_ULONG p2[P256_LIMBS];
  444. BN_ULONG p4[P256_LIMBS];
  445. BN_ULONG p8[P256_LIMBS];
  446. BN_ULONG p16[P256_LIMBS];
  447. BN_ULONG p32[P256_LIMBS];
  448. BN_ULONG res[P256_LIMBS];
  449. int i;
  450. ecp_nistz256_sqr_mont(res, in);
  451. ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
  452. ecp_nistz256_sqr_mont(res, p2);
  453. ecp_nistz256_sqr_mont(res, res);
  454. ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
  455. ecp_nistz256_sqr_mont(res, p4);
  456. ecp_nistz256_sqr_mont(res, res);
  457. ecp_nistz256_sqr_mont(res, res);
  458. ecp_nistz256_sqr_mont(res, res);
  459. ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
  460. ecp_nistz256_sqr_mont(res, p8);
  461. for (i = 0; i < 7; i++)
  462. ecp_nistz256_sqr_mont(res, res);
  463. ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
  464. ecp_nistz256_sqr_mont(res, p16);
  465. for (i = 0; i < 15; i++)
  466. ecp_nistz256_sqr_mont(res, res);
  467. ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
  468. ecp_nistz256_sqr_mont(res, p32);
  469. for (i = 0; i < 31; i++)
  470. ecp_nistz256_sqr_mont(res, res);
  471. ecp_nistz256_mul_mont(res, res, in);
  472. for (i = 0; i < 32 * 4; i++)
  473. ecp_nistz256_sqr_mont(res, res);
  474. ecp_nistz256_mul_mont(res, res, p32);
  475. for (i = 0; i < 32; i++)
  476. ecp_nistz256_sqr_mont(res, res);
  477. ecp_nistz256_mul_mont(res, res, p32);
  478. for (i = 0; i < 16; i++)
  479. ecp_nistz256_sqr_mont(res, res);
  480. ecp_nistz256_mul_mont(res, res, p16);
  481. for (i = 0; i < 8; i++)
  482. ecp_nistz256_sqr_mont(res, res);
  483. ecp_nistz256_mul_mont(res, res, p8);
  484. ecp_nistz256_sqr_mont(res, res);
  485. ecp_nistz256_sqr_mont(res, res);
  486. ecp_nistz256_sqr_mont(res, res);
  487. ecp_nistz256_sqr_mont(res, res);
  488. ecp_nistz256_mul_mont(res, res, p4);
  489. ecp_nistz256_sqr_mont(res, res);
  490. ecp_nistz256_sqr_mont(res, res);
  491. ecp_nistz256_mul_mont(res, res, p2);
  492. ecp_nistz256_sqr_mont(res, res);
  493. ecp_nistz256_sqr_mont(res, res);
  494. ecp_nistz256_mul_mont(res, res, in);
  495. memcpy(r, res, sizeof(res));
  496. }
  497. /*
  498. * ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
  499. * returns one if it fits. Otherwise it returns zero.
  500. */
  501. __owur static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
  502. const BIGNUM *in)
  503. {
  504. return bn_copy_words(out, in, P256_LIMBS);
  505. }
  506. /* r = sum(scalar[i]*point[i]) */
  507. __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
  508. P256_POINT *r,
  509. const BIGNUM **scalar,
  510. const EC_POINT **point,
  511. size_t num, BN_CTX *ctx)
  512. {
  513. size_t i;
  514. int j, ret = 0;
  515. unsigned int idx;
  516. unsigned char (*p_str)[33] = NULL;
  517. const unsigned int window_size = 5;
  518. const unsigned int mask = (1 << (window_size + 1)) - 1;
  519. unsigned int wvalue;
  520. P256_POINT *temp; /* place for 5 temporary points */
  521. const BIGNUM **scalars = NULL;
  522. P256_POINT (*table)[16] = NULL;
  523. void *table_storage = NULL;
  524. if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
  525. || (table_storage =
  526. OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL
  527. || (p_str =
  528. OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
  529. || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
  530. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
  531. goto err;
  532. }
  533. table = (void *)ALIGNPTR(table_storage, 64);
  534. temp = (P256_POINT *)(table + num);
  535. for (i = 0; i < num; i++) {
  536. P256_POINT *row = table[i];
  537. /* This is an unusual input, we don't guarantee constant-timeness. */
  538. if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
  539. BIGNUM *mod;
  540. if ((mod = BN_CTX_get(ctx)) == NULL)
  541. goto err;
  542. if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
  543. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
  544. goto err;
  545. }
  546. scalars[i] = mod;
  547. } else
  548. scalars[i] = scalar[i];
  549. for (j = 0; j < bn_get_top(scalars[i]) * BN_BYTES; j += BN_BYTES) {
  550. BN_ULONG d = bn_get_words(scalars[i])[j / BN_BYTES];
  551. p_str[i][j + 0] = (unsigned char)d;
  552. p_str[i][j + 1] = (unsigned char)(d >> 8);
  553. p_str[i][j + 2] = (unsigned char)(d >> 16);
  554. p_str[i][j + 3] = (unsigned char)(d >>= 24);
  555. if (BN_BYTES == 8) {
  556. d >>= 8;
  557. p_str[i][j + 4] = (unsigned char)d;
  558. p_str[i][j + 5] = (unsigned char)(d >> 8);
  559. p_str[i][j + 6] = (unsigned char)(d >> 16);
  560. p_str[i][j + 7] = (unsigned char)(d >> 24);
  561. }
  562. }
  563. for (; j < 33; j++)
  564. p_str[i][j] = 0;
  565. if (!ecp_nistz256_bignum_to_field_elem(temp[0].X, point[i]->X)
  566. || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)
  567. || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {
  568. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL,
  569. EC_R_COORDINATES_OUT_OF_RANGE);
  570. goto err;
  571. }
  572. /*
  573. * row[0] is implicitly (0,0,0) (the point at infinity), therefore it
  574. * is not stored. All other values are actually stored with an offset
  575. * of -1 in table.
  576. */
  577. ecp_nistz256_scatter_w5 (row, &temp[0], 1);
  578. ecp_nistz256_point_double(&temp[1], &temp[0]); /*1+1=2 */
  579. ecp_nistz256_scatter_w5 (row, &temp[1], 2);
  580. ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*2+1=3 */
  581. ecp_nistz256_scatter_w5 (row, &temp[2], 3);
  582. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*2=4 */
  583. ecp_nistz256_scatter_w5 (row, &temp[1], 4);
  584. ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*3=6 */
  585. ecp_nistz256_scatter_w5 (row, &temp[2], 6);
  586. ecp_nistz256_point_add (&temp[3], &temp[1], &temp[0]); /*4+1=5 */
  587. ecp_nistz256_scatter_w5 (row, &temp[3], 5);
  588. ecp_nistz256_point_add (&temp[4], &temp[2], &temp[0]); /*6+1=7 */
  589. ecp_nistz256_scatter_w5 (row, &temp[4], 7);
  590. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*4=8 */
  591. ecp_nistz256_scatter_w5 (row, &temp[1], 8);
  592. ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*6=12 */
  593. ecp_nistz256_scatter_w5 (row, &temp[2], 12);
  594. ecp_nistz256_point_double(&temp[3], &temp[3]); /*2*5=10 */
  595. ecp_nistz256_scatter_w5 (row, &temp[3], 10);
  596. ecp_nistz256_point_double(&temp[4], &temp[4]); /*2*7=14 */
  597. ecp_nistz256_scatter_w5 (row, &temp[4], 14);
  598. ecp_nistz256_point_add (&temp[2], &temp[2], &temp[0]); /*12+1=13*/
  599. ecp_nistz256_scatter_w5 (row, &temp[2], 13);
  600. ecp_nistz256_point_add (&temp[3], &temp[3], &temp[0]); /*10+1=11*/
  601. ecp_nistz256_scatter_w5 (row, &temp[3], 11);
  602. ecp_nistz256_point_add (&temp[4], &temp[4], &temp[0]); /*14+1=15*/
  603. ecp_nistz256_scatter_w5 (row, &temp[4], 15);
  604. ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*8+1=9 */
  605. ecp_nistz256_scatter_w5 (row, &temp[2], 9);
  606. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*8=16 */
  607. ecp_nistz256_scatter_w5 (row, &temp[1], 16);
  608. }
  609. idx = 255;
  610. wvalue = p_str[0][(idx - 1) / 8];
  611. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  612. /*
  613. * We gather to temp[0], because we know it's position relative
  614. * to table
  615. */
  616. ecp_nistz256_gather_w5(&temp[0], table[0], _booth_recode_w5(wvalue) >> 1);
  617. memcpy(r, &temp[0], sizeof(temp[0]));
  618. while (idx >= 5) {
  619. for (i = (idx == 255 ? 1 : 0); i < num; i++) {
  620. unsigned int off = (idx - 1) / 8;
  621. wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
  622. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  623. wvalue = _booth_recode_w5(wvalue);
  624. ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
  625. ecp_nistz256_neg(temp[1].Y, temp[0].Y);
  626. copy_conditional(temp[0].Y, temp[1].Y, (wvalue & 1));
  627. ecp_nistz256_point_add(r, r, &temp[0]);
  628. }
  629. idx -= window_size;
  630. ecp_nistz256_point_double(r, r);
  631. ecp_nistz256_point_double(r, r);
  632. ecp_nistz256_point_double(r, r);
  633. ecp_nistz256_point_double(r, r);
  634. ecp_nistz256_point_double(r, r);
  635. }
  636. /* Final window */
  637. for (i = 0; i < num; i++) {
  638. wvalue = p_str[i][0];
  639. wvalue = (wvalue << 1) & mask;
  640. wvalue = _booth_recode_w5(wvalue);
  641. ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
  642. ecp_nistz256_neg(temp[1].Y, temp[0].Y);
  643. copy_conditional(temp[0].Y, temp[1].Y, wvalue & 1);
  644. ecp_nistz256_point_add(r, r, &temp[0]);
  645. }
  646. ret = 1;
  647. err:
  648. OPENSSL_free(table_storage);
  649. OPENSSL_free(p_str);
  650. OPENSSL_free(scalars);
  651. return ret;
  652. }
  653. /* Coordinates of G, for which we have precomputed tables */
  654. static const BN_ULONG def_xG[P256_LIMBS] = {
  655. TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
  656. TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
  657. };
  658. static const BN_ULONG def_yG[P256_LIMBS] = {
  659. TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
  660. TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
  661. };
  662. /*
  663. * ecp_nistz256_is_affine_G returns one if |generator| is the standard, P-256
  664. * generator.
  665. */
  666. static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
  667. {
  668. return (bn_get_top(generator->X) == P256_LIMBS) &&
  669. (bn_get_top(generator->Y) == P256_LIMBS) &&
  670. is_equal(bn_get_words(generator->X), def_xG) &&
  671. is_equal(bn_get_words(generator->Y), def_yG) &&
  672. is_one(generator->Z);
  673. }
  674. __owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
  675. {
  676. /*
  677. * We precompute a table for a Booth encoded exponent (wNAF) based
  678. * computation. Each table holds 64 values for safe access, with an
  679. * implicit value of infinity at index zero. We use window of size 7, and
  680. * therefore require ceil(256/7) = 37 tables.
  681. */
  682. const BIGNUM *order;
  683. EC_POINT *P = NULL, *T = NULL;
  684. const EC_POINT *generator;
  685. NISTZ256_PRE_COMP *pre_comp;
  686. BN_CTX *new_ctx = NULL;
  687. int i, j, k, ret = 0;
  688. size_t w;
  689. PRECOMP256_ROW *preComputedTable = NULL;
  690. unsigned char *precomp_storage = NULL;
  691. /* if there is an old NISTZ256_PRE_COMP object, throw it away */
  692. EC_pre_comp_free(group);
  693. generator = EC_GROUP_get0_generator(group);
  694. if (generator == NULL) {
  695. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
  696. return 0;
  697. }
  698. if (ecp_nistz256_is_affine_G(generator)) {
  699. /*
  700. * No need to calculate tables for the standard generator because we
  701. * have them statically.
  702. */
  703. return 1;
  704. }
  705. if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
  706. return 0;
  707. if (ctx == NULL) {
  708. ctx = new_ctx = BN_CTX_new_ex(group->libctx);
  709. if (ctx == NULL)
  710. goto err;
  711. }
  712. BN_CTX_start(ctx);
  713. order = EC_GROUP_get0_order(group);
  714. if (order == NULL)
  715. goto err;
  716. if (BN_is_zero(order)) {
  717. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
  718. goto err;
  719. }
  720. w = 7;
  721. if ((precomp_storage =
  722. OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
  723. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
  724. goto err;
  725. }
  726. preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
  727. P = EC_POINT_new(group);
  728. T = EC_POINT_new(group);
  729. if (P == NULL || T == NULL)
  730. goto err;
  731. /*
  732. * The zero entry is implicitly infinity, and we skip it, storing other
  733. * values with -1 offset.
  734. */
  735. if (!EC_POINT_copy(T, generator))
  736. goto err;
  737. for (k = 0; k < 64; k++) {
  738. if (!EC_POINT_copy(P, T))
  739. goto err;
  740. for (j = 0; j < 37; j++) {
  741. P256_POINT_AFFINE temp;
  742. /*
  743. * It would be faster to use EC_POINTs_make_affine and
  744. * make multiple points affine at the same time.
  745. */
  746. if (!EC_POINT_make_affine(group, P, ctx))
  747. goto err;
  748. if (!ecp_nistz256_bignum_to_field_elem(temp.X, P->X) ||
  749. !ecp_nistz256_bignum_to_field_elem(temp.Y, P->Y)) {
  750. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE,
  751. EC_R_COORDINATES_OUT_OF_RANGE);
  752. goto err;
  753. }
  754. ecp_nistz256_scatter_w7(preComputedTable[j], &temp, k);
  755. for (i = 0; i < 7; i++) {
  756. if (!EC_POINT_dbl(group, P, P, ctx))
  757. goto err;
  758. }
  759. }
  760. if (!EC_POINT_add(group, T, T, generator, ctx))
  761. goto err;
  762. }
  763. pre_comp->group = group;
  764. pre_comp->w = w;
  765. pre_comp->precomp = preComputedTable;
  766. pre_comp->precomp_storage = precomp_storage;
  767. precomp_storage = NULL;
  768. SETPRECOMP(group, nistz256, pre_comp);
  769. pre_comp = NULL;
  770. ret = 1;
  771. err:
  772. BN_CTX_end(ctx);
  773. BN_CTX_free(new_ctx);
  774. EC_nistz256_pre_comp_free(pre_comp);
  775. OPENSSL_free(precomp_storage);
  776. EC_POINT_free(P);
  777. EC_POINT_free(T);
  778. return ret;
  779. }
  780. /*
  781. * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
  782. * code processing 4 points in parallel, corresponding serial operation
  783. * is several times slower, because it uses 29x29=58-bit multiplication
  784. * as opposite to 64x64=128-bit in integer-only scalar case. As result
  785. * it doesn't provide *significant* performance improvement. Note that
  786. * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
  787. * you'd need to compile even asm/ecp_nistz256-avx.pl module.
  788. */
  789. #if defined(ECP_NISTZ256_AVX2)
  790. # if !(defined(__x86_64) || defined(__x86_64__) || \
  791. defined(_M_AMD64) || defined(_M_X64)) || \
  792. !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
  793. # undef ECP_NISTZ256_AVX2
  794. # else
  795. /* Constant time access, loading four values, from four consecutive tables */
  796. void ecp_nistz256_avx2_multi_gather_w7(void *result, const void *in,
  797. int index0, int index1, int index2,
  798. int index3);
  799. void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
  800. void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
  801. void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
  802. const void *Bx4);
  803. void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
  804. const void *Bx4);
  805. void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
  806. void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
  807. void ecp_nistz256_avx2_set1(void *RESULTx4);
  808. int ecp_nistz_avx2_eligible(void);
  809. static void booth_recode_w7(unsigned char *sign,
  810. unsigned char *digit, unsigned char in)
  811. {
  812. unsigned char s, d;
  813. s = ~((in >> 7) - 1);
  814. d = (1 << 8) - in - 1;
  815. d = (d & s) | (in & ~s);
  816. d = (d >> 1) + (d & 1);
  817. *sign = s & 1;
  818. *digit = d;
  819. }
  820. /*
  821. * ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
  822. * precomputed table. It does 4 affine point additions in parallel,
  823. * significantly speeding up point multiplication for a fixed value.
  824. */
  825. static void ecp_nistz256_avx2_mul_g(P256_POINT *r,
  826. unsigned char p_str[33],
  827. const P256_POINT_AFFINE(*preComputedTable)[64])
  828. {
  829. const unsigned int window_size = 7;
  830. const unsigned int mask = (1 << (window_size + 1)) - 1;
  831. unsigned int wvalue;
  832. /* Using 4 windows at a time */
  833. unsigned char sign0, digit0;
  834. unsigned char sign1, digit1;
  835. unsigned char sign2, digit2;
  836. unsigned char sign3, digit3;
  837. unsigned int idx = 0;
  838. BN_ULONG tmp[P256_LIMBS];
  839. int i;
  840. ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
  841. ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
  842. ALIGN32 P256_POINT_AFFINE point_arr[4];
  843. ALIGN32 P256_POINT res_point_arr[4];
  844. /* Initial four windows */
  845. wvalue = *((u16 *) & p_str[0]);
  846. wvalue = (wvalue << 1) & mask;
  847. idx += window_size;
  848. booth_recode_w7(&sign0, &digit0, wvalue);
  849. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  850. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  851. idx += window_size;
  852. booth_recode_w7(&sign1, &digit1, wvalue);
  853. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  854. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  855. idx += window_size;
  856. booth_recode_w7(&sign2, &digit2, wvalue);
  857. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  858. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  859. idx += window_size;
  860. booth_recode_w7(&sign3, &digit3, wvalue);
  861. ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[0],
  862. digit0, digit1, digit2, digit3);
  863. ecp_nistz256_neg(tmp, point_arr[0].Y);
  864. copy_conditional(point_arr[0].Y, tmp, sign0);
  865. ecp_nistz256_neg(tmp, point_arr[1].Y);
  866. copy_conditional(point_arr[1].Y, tmp, sign1);
  867. ecp_nistz256_neg(tmp, point_arr[2].Y);
  868. copy_conditional(point_arr[2].Y, tmp, sign2);
  869. ecp_nistz256_neg(tmp, point_arr[3].Y);
  870. copy_conditional(point_arr[3].Y, tmp, sign3);
  871. ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
  872. ecp_nistz256_avx2_to_mont(aX4, aX4);
  873. ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
  874. ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
  875. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  876. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  877. idx += window_size;
  878. booth_recode_w7(&sign0, &digit0, wvalue);
  879. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  880. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  881. idx += window_size;
  882. booth_recode_w7(&sign1, &digit1, wvalue);
  883. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  884. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  885. idx += window_size;
  886. booth_recode_w7(&sign2, &digit2, wvalue);
  887. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  888. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  889. idx += window_size;
  890. booth_recode_w7(&sign3, &digit3, wvalue);
  891. ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[4 * 1],
  892. digit0, digit1, digit2, digit3);
  893. ecp_nistz256_neg(tmp, point_arr[0].Y);
  894. copy_conditional(point_arr[0].Y, tmp, sign0);
  895. ecp_nistz256_neg(tmp, point_arr[1].Y);
  896. copy_conditional(point_arr[1].Y, tmp, sign1);
  897. ecp_nistz256_neg(tmp, point_arr[2].Y);
  898. copy_conditional(point_arr[2].Y, tmp, sign2);
  899. ecp_nistz256_neg(tmp, point_arr[3].Y);
  900. copy_conditional(point_arr[3].Y, tmp, sign3);
  901. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  902. ecp_nistz256_avx2_to_mont(bX4, bX4);
  903. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  904. /* Optimized when both inputs are affine */
  905. ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
  906. for (i = 2; i < 9; i++) {
  907. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  908. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  909. idx += window_size;
  910. booth_recode_w7(&sign0, &digit0, wvalue);
  911. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  912. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  913. idx += window_size;
  914. booth_recode_w7(&sign1, &digit1, wvalue);
  915. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  916. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  917. idx += window_size;
  918. booth_recode_w7(&sign2, &digit2, wvalue);
  919. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  920. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  921. idx += window_size;
  922. booth_recode_w7(&sign3, &digit3, wvalue);
  923. ecp_nistz256_avx2_multi_gather_w7(point_arr,
  924. preComputedTable[4 * i],
  925. digit0, digit1, digit2, digit3);
  926. ecp_nistz256_neg(tmp, point_arr[0].Y);
  927. copy_conditional(point_arr[0].Y, tmp, sign0);
  928. ecp_nistz256_neg(tmp, point_arr[1].Y);
  929. copy_conditional(point_arr[1].Y, tmp, sign1);
  930. ecp_nistz256_neg(tmp, point_arr[2].Y);
  931. copy_conditional(point_arr[2].Y, tmp, sign2);
  932. ecp_nistz256_neg(tmp, point_arr[3].Y);
  933. copy_conditional(point_arr[3].Y, tmp, sign3);
  934. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  935. ecp_nistz256_avx2_to_mont(bX4, bX4);
  936. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  937. ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
  938. }
  939. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
  940. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
  941. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
  942. ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
  943. /* Last window is performed serially */
  944. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  945. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  946. booth_recode_w7(&sign0, &digit0, wvalue);
  947. ecp_nistz256_gather_w7((P256_POINT_AFFINE *)r,
  948. preComputedTable[36], digit0);
  949. ecp_nistz256_neg(tmp, r->Y);
  950. copy_conditional(r->Y, tmp, sign0);
  951. memcpy(r->Z, ONE, sizeof(ONE));
  952. /* Sum the four windows */
  953. ecp_nistz256_point_add(r, r, &res_point_arr[0]);
  954. ecp_nistz256_point_add(r, r, &res_point_arr[1]);
  955. ecp_nistz256_point_add(r, r, &res_point_arr[2]);
  956. ecp_nistz256_point_add(r, r, &res_point_arr[3]);
  957. }
  958. # endif
  959. #endif
  960. __owur static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
  961. const P256_POINT_AFFINE *in,
  962. BN_CTX *ctx)
  963. {
  964. int ret = 0;
  965. if ((ret = bn_set_words(out->X, in->X, P256_LIMBS))
  966. && (ret = bn_set_words(out->Y, in->Y, P256_LIMBS))
  967. && (ret = bn_set_words(out->Z, ONE, P256_LIMBS)))
  968. out->Z_is_one = 1;
  969. return ret;
  970. }
  971. /* r = scalar*G + sum(scalars[i]*points[i]) */
  972. __owur static int ecp_nistz256_points_mul(const EC_GROUP *group,
  973. EC_POINT *r,
  974. const BIGNUM *scalar,
  975. size_t num,
  976. const EC_POINT *points[],
  977. const BIGNUM *scalars[], BN_CTX *ctx)
  978. {
  979. int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
  980. unsigned char p_str[33] = { 0 };
  981. const PRECOMP256_ROW *preComputedTable = NULL;
  982. const NISTZ256_PRE_COMP *pre_comp = NULL;
  983. const EC_POINT *generator = NULL;
  984. const BIGNUM **new_scalars = NULL;
  985. const EC_POINT **new_points = NULL;
  986. unsigned int idx = 0;
  987. const unsigned int window_size = 7;
  988. const unsigned int mask = (1 << (window_size + 1)) - 1;
  989. unsigned int wvalue;
  990. ALIGN32 union {
  991. P256_POINT p;
  992. P256_POINT_AFFINE a;
  993. } t, p;
  994. BIGNUM *tmp_scalar;
  995. if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
  996. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  997. return 0;
  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 = group->pre_comp.nistz256;
  1008. if (pre_comp) {
  1009. /*
  1010. * If there is a precomputed table for the generator, check that
  1011. * it was generated with the same generator.
  1012. */
  1013. EC_POINT *pre_comp_generator = EC_POINT_new(group);
  1014. if (pre_comp_generator == NULL)
  1015. goto err;
  1016. ecp_nistz256_gather_w7(&p.a, pre_comp->precomp[0], 1);
  1017. if (!ecp_nistz256_set_from_affine(pre_comp_generator,
  1018. group, &p.a, ctx)) {
  1019. EC_POINT_free(pre_comp_generator);
  1020. goto err;
  1021. }
  1022. if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
  1023. preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
  1024. EC_POINT_free(pre_comp_generator);
  1025. }
  1026. if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
  1027. /*
  1028. * If there is no precomputed data, but the generator is the
  1029. * default, a hardcoded table of precomputed data is used. This
  1030. * is because applications, such as Apache, do not use
  1031. * EC_KEY_precompute_mult.
  1032. */
  1033. preComputedTable = ecp_nistz256_precomputed;
  1034. }
  1035. if (preComputedTable) {
  1036. if ((BN_num_bits(scalar) > 256)
  1037. || BN_is_negative(scalar)) {
  1038. if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
  1039. goto err;
  1040. if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
  1041. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
  1042. goto err;
  1043. }
  1044. scalar = tmp_scalar;
  1045. }
  1046. for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {
  1047. BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];
  1048. p_str[i + 0] = (unsigned char)d;
  1049. p_str[i + 1] = (unsigned char)(d >> 8);
  1050. p_str[i + 2] = (unsigned char)(d >> 16);
  1051. p_str[i + 3] = (unsigned char)(d >>= 24);
  1052. if (BN_BYTES == 8) {
  1053. d >>= 8;
  1054. p_str[i + 4] = (unsigned char)d;
  1055. p_str[i + 5] = (unsigned char)(d >> 8);
  1056. p_str[i + 6] = (unsigned char)(d >> 16);
  1057. p_str[i + 7] = (unsigned char)(d >> 24);
  1058. }
  1059. }
  1060. for (; i < 33; i++)
  1061. p_str[i] = 0;
  1062. #if defined(ECP_NISTZ256_AVX2)
  1063. if (ecp_nistz_avx2_eligible()) {
  1064. ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
  1065. } else
  1066. #endif
  1067. {
  1068. BN_ULONG infty;
  1069. /* First window */
  1070. wvalue = (p_str[0] << 1) & mask;
  1071. idx += window_size;
  1072. wvalue = _booth_recode_w7(wvalue);
  1073. ecp_nistz256_gather_w7(&p.a, preComputedTable[0],
  1074. wvalue >> 1);
  1075. ecp_nistz256_neg(p.p.Z, p.p.Y);
  1076. copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
  1077. /*
  1078. * Since affine infinity is encoded as (0,0) and
  1079. * Jacobian ias (,,0), we need to harmonize them
  1080. * by assigning "one" or zero to Z.
  1081. */
  1082. infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |
  1083. p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);
  1084. if (P256_LIMBS == 8)
  1085. infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |
  1086. p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);
  1087. infty = 0 - is_zero(infty);
  1088. infty = ~infty;
  1089. p.p.Z[0] = ONE[0] & infty;
  1090. p.p.Z[1] = ONE[1] & infty;
  1091. p.p.Z[2] = ONE[2] & infty;
  1092. p.p.Z[3] = ONE[3] & infty;
  1093. if (P256_LIMBS == 8) {
  1094. p.p.Z[4] = ONE[4] & infty;
  1095. p.p.Z[5] = ONE[5] & infty;
  1096. p.p.Z[6] = ONE[6] & infty;
  1097. p.p.Z[7] = ONE[7] & infty;
  1098. }
  1099. for (i = 1; i < 37; i++) {
  1100. unsigned int off = (idx - 1) / 8;
  1101. wvalue = p_str[off] | p_str[off + 1] << 8;
  1102. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  1103. idx += window_size;
  1104. wvalue = _booth_recode_w7(wvalue);
  1105. ecp_nistz256_gather_w7(&t.a,
  1106. preComputedTable[i], wvalue >> 1);
  1107. ecp_nistz256_neg(t.p.Z, t.a.Y);
  1108. copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
  1109. ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
  1110. }
  1111. }
  1112. } else {
  1113. p_is_infinity = 1;
  1114. no_precomp_for_generator = 1;
  1115. }
  1116. } else
  1117. p_is_infinity = 1;
  1118. if (no_precomp_for_generator) {
  1119. /*
  1120. * Without a precomputed table for the generator, it has to be
  1121. * handled like a normal point.
  1122. */
  1123. new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
  1124. if (new_scalars == NULL) {
  1125. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1126. goto err;
  1127. }
  1128. new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
  1129. if (new_points == NULL) {
  1130. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1131. goto err;
  1132. }
  1133. memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
  1134. new_scalars[num] = scalar;
  1135. memcpy(new_points, points, num * sizeof(EC_POINT *));
  1136. new_points[num] = generator;
  1137. scalars = new_scalars;
  1138. points = new_points;
  1139. num++;
  1140. }
  1141. if (num) {
  1142. P256_POINT *out = &t.p;
  1143. if (p_is_infinity)
  1144. out = &p.p;
  1145. if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
  1146. goto err;
  1147. if (!p_is_infinity)
  1148. ecp_nistz256_point_add(&p.p, &p.p, out);
  1149. }
  1150. /* Not constant-time, but we're only operating on the public output. */
  1151. if (!bn_set_words(r->X, p.p.X, P256_LIMBS) ||
  1152. !bn_set_words(r->Y, p.p.Y, P256_LIMBS) ||
  1153. !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {
  1154. goto err;
  1155. }
  1156. r->Z_is_one = is_one(r->Z) & 1;
  1157. ret = 1;
  1158. err:
  1159. BN_CTX_end(ctx);
  1160. OPENSSL_free(new_points);
  1161. OPENSSL_free(new_scalars);
  1162. return ret;
  1163. }
  1164. __owur static int ecp_nistz256_get_affine(const EC_GROUP *group,
  1165. const EC_POINT *point,
  1166. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  1167. {
  1168. BN_ULONG z_inv2[P256_LIMBS];
  1169. BN_ULONG z_inv3[P256_LIMBS];
  1170. BN_ULONG x_aff[P256_LIMBS];
  1171. BN_ULONG y_aff[P256_LIMBS];
  1172. BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
  1173. BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
  1174. if (EC_POINT_is_at_infinity(group, point)) {
  1175. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
  1176. return 0;
  1177. }
  1178. if (!ecp_nistz256_bignum_to_field_elem(point_x, point->X) ||
  1179. !ecp_nistz256_bignum_to_field_elem(point_y, point->Y) ||
  1180. !ecp_nistz256_bignum_to_field_elem(point_z, point->Z)) {
  1181. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
  1182. return 0;
  1183. }
  1184. ecp_nistz256_mod_inverse(z_inv3, point_z);
  1185. ecp_nistz256_sqr_mont(z_inv2, z_inv3);
  1186. ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
  1187. if (x != NULL) {
  1188. ecp_nistz256_from_mont(x_ret, x_aff);
  1189. if (!bn_set_words(x, x_ret, P256_LIMBS))
  1190. return 0;
  1191. }
  1192. if (y != NULL) {
  1193. ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
  1194. ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
  1195. ecp_nistz256_from_mont(y_ret, y_aff);
  1196. if (!bn_set_words(y, y_ret, P256_LIMBS))
  1197. return 0;
  1198. }
  1199. return 1;
  1200. }
  1201. static NISTZ256_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
  1202. {
  1203. NISTZ256_PRE_COMP *ret = NULL;
  1204. if (!group)
  1205. return NULL;
  1206. ret = OPENSSL_zalloc(sizeof(*ret));
  1207. if (ret == NULL) {
  1208. ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1209. return ret;
  1210. }
  1211. ret->group = group;
  1212. ret->w = 6; /* default */
  1213. ret->references = 1;
  1214. ret->lock = CRYPTO_THREAD_lock_new();
  1215. if (ret->lock == NULL) {
  1216. ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1217. OPENSSL_free(ret);
  1218. return NULL;
  1219. }
  1220. return ret;
  1221. }
  1222. NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *p)
  1223. {
  1224. int i;
  1225. if (p != NULL)
  1226. CRYPTO_UP_REF(&p->references, &i, p->lock);
  1227. return p;
  1228. }
  1229. void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *pre)
  1230. {
  1231. int i;
  1232. if (pre == NULL)
  1233. return;
  1234. CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
  1235. REF_PRINT_COUNT("EC_nistz256", pre);
  1236. if (i > 0)
  1237. return;
  1238. REF_ASSERT_ISNT(i < 0);
  1239. OPENSSL_free(pre->precomp_storage);
  1240. CRYPTO_THREAD_lock_free(pre->lock);
  1241. OPENSSL_free(pre);
  1242. }
  1243. static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
  1244. {
  1245. /* There is a hard-coded table for the default generator. */
  1246. const EC_POINT *generator = EC_GROUP_get0_generator(group);
  1247. if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
  1248. /* There is a hard-coded table for the default generator. */
  1249. return 1;
  1250. }
  1251. return HAVEPRECOMP(group, nistz256);
  1252. }
  1253. #if defined(__x86_64) || defined(__x86_64__) || \
  1254. defined(_M_AMD64) || defined(_M_X64) || \
  1255. defined(__powerpc64__) || defined(_ARCH_PP64) || \
  1256. defined(__aarch64__)
  1257. /*
  1258. * Montgomery mul modulo Order(P): res = a*b*2^-256 mod Order(P)
  1259. */
  1260. void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS],
  1261. const BN_ULONG a[P256_LIMBS],
  1262. const BN_ULONG b[P256_LIMBS]);
  1263. void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS],
  1264. const BN_ULONG a[P256_LIMBS],
  1265. BN_ULONG rep);
  1266. static int ecp_nistz256_inv_mod_ord(const EC_GROUP *group, BIGNUM *r,
  1267. const BIGNUM *x, BN_CTX *ctx)
  1268. {
  1269. /* RR = 2^512 mod ord(p256) */
  1270. static const BN_ULONG RR[P256_LIMBS] = {
  1271. TOBN(0x83244c95,0xbe79eea2), TOBN(0x4699799c,0x49bd6fa6),
  1272. TOBN(0x2845b239,0x2b6bec59), TOBN(0x66e12d94,0xf3d95620)
  1273. };
  1274. /* The constant 1 (unlike ONE that is one in Montgomery representation) */
  1275. static const BN_ULONG one[P256_LIMBS] = {
  1276. TOBN(0,1), TOBN(0,0), TOBN(0,0), TOBN(0,0)
  1277. };
  1278. /*
  1279. * We don't use entry 0 in the table, so we omit it and address
  1280. * with -1 offset.
  1281. */
  1282. BN_ULONG table[15][P256_LIMBS];
  1283. BN_ULONG out[P256_LIMBS], t[P256_LIMBS];
  1284. int i, ret = 0;
  1285. enum {
  1286. i_1 = 0, i_10, i_11, i_101, i_111, i_1010, i_1111,
  1287. i_10101, i_101010, i_101111, i_x6, i_x8, i_x16, i_x32
  1288. };
  1289. /*
  1290. * Catch allocation failure early.
  1291. */
  1292. if (bn_wexpand(r, P256_LIMBS) == NULL) {
  1293. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
  1294. goto err;
  1295. }
  1296. if ((BN_num_bits(x) > 256) || BN_is_negative(x)) {
  1297. BIGNUM *tmp;
  1298. if ((tmp = BN_CTX_get(ctx)) == NULL
  1299. || !BN_nnmod(tmp, x, group->order, ctx)) {
  1300. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
  1301. goto err;
  1302. }
  1303. x = tmp;
  1304. }
  1305. if (!ecp_nistz256_bignum_to_field_elem(t, x)) {
  1306. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, EC_R_COORDINATES_OUT_OF_RANGE);
  1307. goto err;
  1308. }
  1309. ecp_nistz256_ord_mul_mont(table[0], t, RR);
  1310. #if 0
  1311. /*
  1312. * Original sparse-then-fixed-window algorithm, retained for reference.
  1313. */
  1314. for (i = 2; i < 16; i += 2) {
  1315. ecp_nistz256_ord_sqr_mont(table[i-1], table[i/2-1], 1);
  1316. ecp_nistz256_ord_mul_mont(table[i], table[i-1], table[0]);
  1317. }
  1318. /*
  1319. * The top 128bit of the exponent are highly redudndant, so we
  1320. * perform an optimized flow
  1321. */
  1322. ecp_nistz256_ord_sqr_mont(t, table[15-1], 4); /* f0 */
  1323. ecp_nistz256_ord_mul_mont(t, t, table[15-1]); /* ff */
  1324. ecp_nistz256_ord_sqr_mont(out, t, 8); /* ff00 */
  1325. ecp_nistz256_ord_mul_mont(out, out, t); /* ffff */
  1326. ecp_nistz256_ord_sqr_mont(t, out, 16); /* ffff0000 */
  1327. ecp_nistz256_ord_mul_mont(t, t, out); /* ffffffff */
  1328. ecp_nistz256_ord_sqr_mont(out, t, 64); /* ffffffff0000000000000000 */
  1329. ecp_nistz256_ord_mul_mont(out, out, t); /* ffffffff00000000ffffffff */
  1330. ecp_nistz256_ord_sqr_mont(out, out, 32); /* ffffffff00000000ffffffff00000000 */
  1331. ecp_nistz256_ord_mul_mont(out, out, t); /* ffffffff00000000ffffffffffffffff */
  1332. /*
  1333. * The bottom 128 bit of the exponent are processed with fixed 4-bit window
  1334. */
  1335. for(i = 0; i < 32; i++) {
  1336. /* expLo - the low 128 bits of the exponent we use (ord(p256) - 2),
  1337. * split into nibbles */
  1338. static const unsigned char expLo[32] = {
  1339. 0xb,0xc,0xe,0x6,0xf,0xa,0xa,0xd,0xa,0x7,0x1,0x7,0x9,0xe,0x8,0x4,
  1340. 0xf,0x3,0xb,0x9,0xc,0xa,0xc,0x2,0xf,0xc,0x6,0x3,0x2,0x5,0x4,0xf
  1341. };
  1342. ecp_nistz256_ord_sqr_mont(out, out, 4);
  1343. /* The exponent is public, no need in constant-time access */
  1344. ecp_nistz256_ord_mul_mont(out, out, table[expLo[i]-1]);
  1345. }
  1346. #else
  1347. /*
  1348. * https://briansmith.org/ecc-inversion-addition-chains-01#p256_scalar_inversion
  1349. *
  1350. * Even though this code path spares 12 squarings, 4.5%, and 13
  1351. * multiplications, 25%, on grand scale sign operation is not that
  1352. * much faster, not more that 2%...
  1353. */
  1354. /* pre-calculate powers */
  1355. ecp_nistz256_ord_sqr_mont(table[i_10], table[i_1], 1);
  1356. ecp_nistz256_ord_mul_mont(table[i_11], table[i_1], table[i_10]);
  1357. ecp_nistz256_ord_mul_mont(table[i_101], table[i_11], table[i_10]);
  1358. ecp_nistz256_ord_mul_mont(table[i_111], table[i_101], table[i_10]);
  1359. ecp_nistz256_ord_sqr_mont(table[i_1010], table[i_101], 1);
  1360. ecp_nistz256_ord_mul_mont(table[i_1111], table[i_1010], table[i_101]);
  1361. ecp_nistz256_ord_sqr_mont(table[i_10101], table[i_1010], 1);
  1362. ecp_nistz256_ord_mul_mont(table[i_10101], table[i_10101], table[i_1]);
  1363. ecp_nistz256_ord_sqr_mont(table[i_101010], table[i_10101], 1);
  1364. ecp_nistz256_ord_mul_mont(table[i_101111], table[i_101010], table[i_101]);
  1365. ecp_nistz256_ord_mul_mont(table[i_x6], table[i_101010], table[i_10101]);
  1366. ecp_nistz256_ord_sqr_mont(table[i_x8], table[i_x6], 2);
  1367. ecp_nistz256_ord_mul_mont(table[i_x8], table[i_x8], table[i_11]);
  1368. ecp_nistz256_ord_sqr_mont(table[i_x16], table[i_x8], 8);
  1369. ecp_nistz256_ord_mul_mont(table[i_x16], table[i_x16], table[i_x8]);
  1370. ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16);
  1371. ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]);
  1372. /* calculations */
  1373. ecp_nistz256_ord_sqr_mont(out, table[i_x32], 64);
  1374. ecp_nistz256_ord_mul_mont(out, out, table[i_x32]);
  1375. for (i = 0; i < 27; i++) {
  1376. static const struct { unsigned char p, i; } chain[27] = {
  1377. { 32, i_x32 }, { 6, i_101111 }, { 5, i_111 },
  1378. { 4, i_11 }, { 5, i_1111 }, { 5, i_10101 },
  1379. { 4, i_101 }, { 3, i_101 }, { 3, i_101 },
  1380. { 5, i_111 }, { 9, i_101111 }, { 6, i_1111 },
  1381. { 2, i_1 }, { 5, i_1 }, { 6, i_1111 },
  1382. { 5, i_111 }, { 4, i_111 }, { 5, i_111 },
  1383. { 5, i_101 }, { 3, i_11 }, { 10, i_101111 },
  1384. { 2, i_11 }, { 5, i_11 }, { 5, i_11 },
  1385. { 3, i_1 }, { 7, i_10101 }, { 6, i_1111 }
  1386. };
  1387. ecp_nistz256_ord_sqr_mont(out, out, chain[i].p);
  1388. ecp_nistz256_ord_mul_mont(out, out, table[chain[i].i]);
  1389. }
  1390. #endif
  1391. ecp_nistz256_ord_mul_mont(out, out, one);
  1392. /*
  1393. * Can't fail, but check return code to be consistent anyway.
  1394. */
  1395. if (!bn_set_words(r, out, P256_LIMBS))
  1396. goto err;
  1397. ret = 1;
  1398. err:
  1399. return ret;
  1400. }
  1401. #else
  1402. # define ecp_nistz256_inv_mod_ord NULL
  1403. #endif
  1404. const EC_METHOD *EC_GFp_nistz256_method(void)
  1405. {
  1406. static const EC_METHOD ret = {
  1407. EC_FLAGS_DEFAULT_OCT,
  1408. NID_X9_62_prime_field,
  1409. ec_GFp_mont_group_init,
  1410. ec_GFp_mont_group_finish,
  1411. ec_GFp_mont_group_clear_finish,
  1412. ec_GFp_mont_group_copy,
  1413. ec_GFp_mont_group_set_curve,
  1414. ec_GFp_simple_group_get_curve,
  1415. ec_GFp_simple_group_get_degree,
  1416. ec_group_simple_order_bits,
  1417. ec_GFp_simple_group_check_discriminant,
  1418. ec_GFp_simple_point_init,
  1419. ec_GFp_simple_point_finish,
  1420. ec_GFp_simple_point_clear_finish,
  1421. ec_GFp_simple_point_copy,
  1422. ec_GFp_simple_point_set_to_infinity,
  1423. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  1424. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  1425. ec_GFp_simple_point_set_affine_coordinates,
  1426. ecp_nistz256_get_affine,
  1427. 0, 0, 0,
  1428. ec_GFp_simple_add,
  1429. ec_GFp_simple_dbl,
  1430. ec_GFp_simple_invert,
  1431. ec_GFp_simple_is_at_infinity,
  1432. ec_GFp_simple_is_on_curve,
  1433. ec_GFp_simple_cmp,
  1434. ec_GFp_simple_make_affine,
  1435. ec_GFp_simple_points_make_affine,
  1436. ecp_nistz256_points_mul, /* mul */
  1437. ecp_nistz256_mult_precompute, /* precompute_mult */
  1438. ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
  1439. ec_GFp_mont_field_mul,
  1440. ec_GFp_mont_field_sqr,
  1441. 0, /* field_div */
  1442. ec_GFp_mont_field_inv,
  1443. ec_GFp_mont_field_encode,
  1444. ec_GFp_mont_field_decode,
  1445. ec_GFp_mont_field_set_to_one,
  1446. ec_key_simple_priv2oct,
  1447. ec_key_simple_oct2priv,
  1448. 0, /* set private */
  1449. ec_key_simple_generate_key,
  1450. ec_key_simple_check_key,
  1451. ec_key_simple_generate_public_key,
  1452. 0, /* keycopy */
  1453. 0, /* keyfinish */
  1454. ecdh_simple_compute_key,
  1455. ecdsa_simple_sign_setup,
  1456. ecdsa_simple_sign_sig,
  1457. ecdsa_simple_verify_sig,
  1458. ecp_nistz256_inv_mod_ord, /* can be #define-d NULL */
  1459. 0, /* blind_coordinates */
  1460. 0, /* ladder_pre */
  1461. 0, /* ladder_step */
  1462. 0 /* ladder_post */
  1463. };
  1464. return &ret;
  1465. }