ecp_nistz256.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. * This should not happen during sign/ecdh, so no constant time violation
  305. */
  306. if (is_equal(U1, U2) && !in1infty && !in2infty) {
  307. if (is_equal(S1, S2)) {
  308. ecp_nistz256_point_double(r, a);
  309. return;
  310. } else {
  311. memset(r, 0, sizeof(*r));
  312. return;
  313. }
  314. }
  315. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  316. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  317. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  318. ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
  319. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  320. ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
  321. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  322. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  323. ecp_nistz256_sub(res_x, res_x, Hcub);
  324. ecp_nistz256_sub(res_y, U2, res_x);
  325. ecp_nistz256_mul_mont(S2, S1, Hcub);
  326. ecp_nistz256_mul_mont(res_y, R, res_y);
  327. ecp_nistz256_sub(res_y, res_y, S2);
  328. copy_conditional(res_x, in2_x, in1infty);
  329. copy_conditional(res_y, in2_y, in1infty);
  330. copy_conditional(res_z, in2_z, in1infty);
  331. copy_conditional(res_x, in1_x, in2infty);
  332. copy_conditional(res_y, in1_y, in2infty);
  333. copy_conditional(res_z, in1_z, in2infty);
  334. memcpy(r->X, res_x, sizeof(res_x));
  335. memcpy(r->Y, res_y, sizeof(res_y));
  336. memcpy(r->Z, res_z, sizeof(res_z));
  337. }
  338. /* Point addition when b is known to be affine: r = a+b */
  339. static void ecp_nistz256_point_add_affine(P256_POINT *r,
  340. const P256_POINT *a,
  341. const P256_POINT_AFFINE *b)
  342. {
  343. BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
  344. BN_ULONG Z1sqr[P256_LIMBS];
  345. BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
  346. BN_ULONG Hsqr[P256_LIMBS];
  347. BN_ULONG Rsqr[P256_LIMBS];
  348. BN_ULONG Hcub[P256_LIMBS];
  349. BN_ULONG res_x[P256_LIMBS];
  350. BN_ULONG res_y[P256_LIMBS];
  351. BN_ULONG res_z[P256_LIMBS];
  352. BN_ULONG in1infty, in2infty;
  353. const BN_ULONG *in1_x = a->X;
  354. const BN_ULONG *in1_y = a->Y;
  355. const BN_ULONG *in1_z = a->Z;
  356. const BN_ULONG *in2_x = b->X;
  357. const BN_ULONG *in2_y = b->Y;
  358. /*
  359. * Infinity in encoded as (,,0)
  360. */
  361. in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
  362. if (P256_LIMBS == 8)
  363. in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
  364. /*
  365. * In affine representation we encode infinity as (0,0), which is
  366. * not on the curve, so it is OK
  367. */
  368. in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
  369. in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
  370. if (P256_LIMBS == 8)
  371. in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
  372. in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
  373. in1infty = is_zero(in1infty);
  374. in2infty = is_zero(in2infty);
  375. ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
  376. ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
  377. ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
  378. ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
  379. ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
  380. ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
  381. ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
  382. ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
  383. ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
  384. ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
  385. ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
  386. ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
  387. ecp_nistz256_sub(res_x, Rsqr, Hsqr);
  388. ecp_nistz256_sub(res_x, res_x, Hcub);
  389. ecp_nistz256_sub(H, U2, res_x);
  390. ecp_nistz256_mul_mont(S2, in1_y, Hcub);
  391. ecp_nistz256_mul_mont(H, H, R);
  392. ecp_nistz256_sub(res_y, H, S2);
  393. copy_conditional(res_x, in2_x, in1infty);
  394. copy_conditional(res_x, in1_x, in2infty);
  395. copy_conditional(res_y, in2_y, in1infty);
  396. copy_conditional(res_y, in1_y, in2infty);
  397. copy_conditional(res_z, ONE, in1infty);
  398. copy_conditional(res_z, in1_z, in2infty);
  399. memcpy(r->X, res_x, sizeof(res_x));
  400. memcpy(r->Y, res_y, sizeof(res_y));
  401. memcpy(r->Z, res_z, sizeof(res_z));
  402. }
  403. #endif
  404. /* r = in^-1 mod p */
  405. static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
  406. const BN_ULONG in[P256_LIMBS])
  407. {
  408. /*
  409. * The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff
  410. * ffffffff ffffffff We use FLT and used poly-2 as exponent
  411. */
  412. BN_ULONG p2[P256_LIMBS];
  413. BN_ULONG p4[P256_LIMBS];
  414. BN_ULONG p8[P256_LIMBS];
  415. BN_ULONG p16[P256_LIMBS];
  416. BN_ULONG p32[P256_LIMBS];
  417. BN_ULONG res[P256_LIMBS];
  418. int i;
  419. ecp_nistz256_sqr_mont(res, in);
  420. ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
  421. ecp_nistz256_sqr_mont(res, p2);
  422. ecp_nistz256_sqr_mont(res, res);
  423. ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
  424. ecp_nistz256_sqr_mont(res, p4);
  425. ecp_nistz256_sqr_mont(res, res);
  426. ecp_nistz256_sqr_mont(res, res);
  427. ecp_nistz256_sqr_mont(res, res);
  428. ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
  429. ecp_nistz256_sqr_mont(res, p8);
  430. for (i = 0; i < 7; i++)
  431. ecp_nistz256_sqr_mont(res, res);
  432. ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
  433. ecp_nistz256_sqr_mont(res, p16);
  434. for (i = 0; i < 15; i++)
  435. ecp_nistz256_sqr_mont(res, res);
  436. ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
  437. ecp_nistz256_sqr_mont(res, p32);
  438. for (i = 0; i < 31; i++)
  439. ecp_nistz256_sqr_mont(res, res);
  440. ecp_nistz256_mul_mont(res, res, in);
  441. for (i = 0; i < 32 * 4; i++)
  442. ecp_nistz256_sqr_mont(res, res);
  443. ecp_nistz256_mul_mont(res, res, p32);
  444. for (i = 0; i < 32; i++)
  445. ecp_nistz256_sqr_mont(res, res);
  446. ecp_nistz256_mul_mont(res, res, p32);
  447. for (i = 0; i < 16; i++)
  448. ecp_nistz256_sqr_mont(res, res);
  449. ecp_nistz256_mul_mont(res, res, p16);
  450. for (i = 0; i < 8; i++)
  451. ecp_nistz256_sqr_mont(res, res);
  452. ecp_nistz256_mul_mont(res, res, p8);
  453. ecp_nistz256_sqr_mont(res, res);
  454. ecp_nistz256_sqr_mont(res, res);
  455. ecp_nistz256_sqr_mont(res, res);
  456. ecp_nistz256_sqr_mont(res, res);
  457. ecp_nistz256_mul_mont(res, res, p4);
  458. ecp_nistz256_sqr_mont(res, res);
  459. ecp_nistz256_sqr_mont(res, res);
  460. ecp_nistz256_mul_mont(res, res, p2);
  461. ecp_nistz256_sqr_mont(res, res);
  462. ecp_nistz256_sqr_mont(res, res);
  463. ecp_nistz256_mul_mont(res, res, in);
  464. memcpy(r, res, sizeof(res));
  465. }
  466. /*
  467. * ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
  468. * returns one if it fits. Otherwise it returns zero.
  469. */
  470. __owur static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
  471. const BIGNUM *in)
  472. {
  473. return bn_copy_words(out, in, P256_LIMBS);
  474. }
  475. /* r = sum(scalar[i]*point[i]) */
  476. __owur static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
  477. P256_POINT *r,
  478. const BIGNUM **scalar,
  479. const EC_POINT **point,
  480. size_t num, BN_CTX *ctx)
  481. {
  482. size_t i;
  483. int j, ret = 0;
  484. unsigned int idx;
  485. unsigned char (*p_str)[33] = NULL;
  486. const unsigned int window_size = 5;
  487. const unsigned int mask = (1 << (window_size + 1)) - 1;
  488. unsigned int wvalue;
  489. P256_POINT *temp; /* place for 5 temporary points */
  490. const BIGNUM **scalars = NULL;
  491. P256_POINT (*table)[16] = NULL;
  492. void *table_storage = NULL;
  493. if ((num * 16 + 6) > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
  494. || (table_storage =
  495. OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL
  496. || (p_str =
  497. OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
  498. || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
  499. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
  500. goto err;
  501. }
  502. table = (void *)ALIGNPTR(table_storage, 64);
  503. temp = (P256_POINT *)(table + num);
  504. for (i = 0; i < num; i++) {
  505. P256_POINT *row = table[i];
  506. /* This is an unusual input, we don't guarantee constant-timeness. */
  507. if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
  508. BIGNUM *mod;
  509. if ((mod = BN_CTX_get(ctx)) == NULL)
  510. goto err;
  511. if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
  512. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
  513. goto err;
  514. }
  515. scalars[i] = mod;
  516. } else
  517. scalars[i] = scalar[i];
  518. for (j = 0; j < bn_get_top(scalars[i]) * BN_BYTES; j += BN_BYTES) {
  519. BN_ULONG d = bn_get_words(scalars[i])[j / BN_BYTES];
  520. p_str[i][j + 0] = (unsigned char)d;
  521. p_str[i][j + 1] = (unsigned char)(d >> 8);
  522. p_str[i][j + 2] = (unsigned char)(d >> 16);
  523. p_str[i][j + 3] = (unsigned char)(d >>= 24);
  524. if (BN_BYTES == 8) {
  525. d >>= 8;
  526. p_str[i][j + 4] = (unsigned char)d;
  527. p_str[i][j + 5] = (unsigned char)(d >> 8);
  528. p_str[i][j + 6] = (unsigned char)(d >> 16);
  529. p_str[i][j + 7] = (unsigned char)(d >> 24);
  530. }
  531. }
  532. for (; j < 33; j++)
  533. p_str[i][j] = 0;
  534. if (!ecp_nistz256_bignum_to_field_elem(temp[0].X, point[i]->X)
  535. || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)
  536. || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {
  537. ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL,
  538. EC_R_COORDINATES_OUT_OF_RANGE);
  539. goto err;
  540. }
  541. /*
  542. * row[0] is implicitly (0,0,0) (the point at infinity), therefore it
  543. * is not stored. All other values are actually stored with an offset
  544. * of -1 in table.
  545. */
  546. ecp_nistz256_scatter_w5 (row, &temp[0], 1);
  547. ecp_nistz256_point_double(&temp[1], &temp[0]); /*1+1=2 */
  548. ecp_nistz256_scatter_w5 (row, &temp[1], 2);
  549. ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*2+1=3 */
  550. ecp_nistz256_scatter_w5 (row, &temp[2], 3);
  551. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*2=4 */
  552. ecp_nistz256_scatter_w5 (row, &temp[1], 4);
  553. ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*3=6 */
  554. ecp_nistz256_scatter_w5 (row, &temp[2], 6);
  555. ecp_nistz256_point_add (&temp[3], &temp[1], &temp[0]); /*4+1=5 */
  556. ecp_nistz256_scatter_w5 (row, &temp[3], 5);
  557. ecp_nistz256_point_add (&temp[4], &temp[2], &temp[0]); /*6+1=7 */
  558. ecp_nistz256_scatter_w5 (row, &temp[4], 7);
  559. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*4=8 */
  560. ecp_nistz256_scatter_w5 (row, &temp[1], 8);
  561. ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*6=12 */
  562. ecp_nistz256_scatter_w5 (row, &temp[2], 12);
  563. ecp_nistz256_point_double(&temp[3], &temp[3]); /*2*5=10 */
  564. ecp_nistz256_scatter_w5 (row, &temp[3], 10);
  565. ecp_nistz256_point_double(&temp[4], &temp[4]); /*2*7=14 */
  566. ecp_nistz256_scatter_w5 (row, &temp[4], 14);
  567. ecp_nistz256_point_add (&temp[2], &temp[2], &temp[0]); /*12+1=13*/
  568. ecp_nistz256_scatter_w5 (row, &temp[2], 13);
  569. ecp_nistz256_point_add (&temp[3], &temp[3], &temp[0]); /*10+1=11*/
  570. ecp_nistz256_scatter_w5 (row, &temp[3], 11);
  571. ecp_nistz256_point_add (&temp[4], &temp[4], &temp[0]); /*14+1=15*/
  572. ecp_nistz256_scatter_w5 (row, &temp[4], 15);
  573. ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*8+1=9 */
  574. ecp_nistz256_scatter_w5 (row, &temp[2], 9);
  575. ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*8=16 */
  576. ecp_nistz256_scatter_w5 (row, &temp[1], 16);
  577. }
  578. idx = 255;
  579. wvalue = p_str[0][(idx - 1) / 8];
  580. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  581. /*
  582. * We gather to temp[0], because we know it's position relative
  583. * to table
  584. */
  585. ecp_nistz256_gather_w5(&temp[0], table[0], _booth_recode_w5(wvalue) >> 1);
  586. memcpy(r, &temp[0], sizeof(temp[0]));
  587. while (idx >= 5) {
  588. for (i = (idx == 255 ? 1 : 0); i < num; i++) {
  589. unsigned int off = (idx - 1) / 8;
  590. wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
  591. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  592. wvalue = _booth_recode_w5(wvalue);
  593. ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
  594. ecp_nistz256_neg(temp[1].Y, temp[0].Y);
  595. copy_conditional(temp[0].Y, temp[1].Y, (wvalue & 1));
  596. ecp_nistz256_point_add(r, r, &temp[0]);
  597. }
  598. idx -= window_size;
  599. ecp_nistz256_point_double(r, r);
  600. ecp_nistz256_point_double(r, r);
  601. ecp_nistz256_point_double(r, r);
  602. ecp_nistz256_point_double(r, r);
  603. ecp_nistz256_point_double(r, r);
  604. }
  605. /* Final window */
  606. for (i = 0; i < num; i++) {
  607. wvalue = p_str[i][0];
  608. wvalue = (wvalue << 1) & mask;
  609. wvalue = _booth_recode_w5(wvalue);
  610. ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
  611. ecp_nistz256_neg(temp[1].Y, temp[0].Y);
  612. copy_conditional(temp[0].Y, temp[1].Y, wvalue & 1);
  613. ecp_nistz256_point_add(r, r, &temp[0]);
  614. }
  615. ret = 1;
  616. err:
  617. OPENSSL_free(table_storage);
  618. OPENSSL_free(p_str);
  619. OPENSSL_free(scalars);
  620. return ret;
  621. }
  622. /* Coordinates of G, for which we have precomputed tables */
  623. static const BN_ULONG def_xG[P256_LIMBS] = {
  624. TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
  625. TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
  626. };
  627. static const BN_ULONG def_yG[P256_LIMBS] = {
  628. TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
  629. TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
  630. };
  631. /*
  632. * ecp_nistz256_is_affine_G returns one if |generator| is the standard, P-256
  633. * generator.
  634. */
  635. static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
  636. {
  637. return (bn_get_top(generator->X) == P256_LIMBS) &&
  638. (bn_get_top(generator->Y) == P256_LIMBS) &&
  639. is_equal(bn_get_words(generator->X), def_xG) &&
  640. is_equal(bn_get_words(generator->Y), def_yG) &&
  641. is_one(generator->Z);
  642. }
  643. __owur static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
  644. {
  645. /*
  646. * We precompute a table for a Booth encoded exponent (wNAF) based
  647. * computation. Each table holds 64 values for safe access, with an
  648. * implicit value of infinity at index zero. We use window of size 7, and
  649. * therefore require ceil(256/7) = 37 tables.
  650. */
  651. const BIGNUM *order;
  652. EC_POINT *P = NULL, *T = NULL;
  653. const EC_POINT *generator;
  654. NISTZ256_PRE_COMP *pre_comp;
  655. BN_CTX *new_ctx = NULL;
  656. int i, j, k, ret = 0;
  657. size_t w;
  658. PRECOMP256_ROW *preComputedTable = NULL;
  659. unsigned char *precomp_storage = NULL;
  660. /* if there is an old NISTZ256_PRE_COMP object, throw it away */
  661. EC_pre_comp_free(group);
  662. generator = EC_GROUP_get0_generator(group);
  663. if (generator == NULL) {
  664. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
  665. return 0;
  666. }
  667. if (ecp_nistz256_is_affine_G(generator)) {
  668. /*
  669. * No need to calculate tables for the standard generator because we
  670. * have them statically.
  671. */
  672. return 1;
  673. }
  674. if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
  675. return 0;
  676. if (ctx == NULL) {
  677. ctx = new_ctx = BN_CTX_new_ex(group->libctx);
  678. if (ctx == NULL)
  679. goto err;
  680. }
  681. BN_CTX_start(ctx);
  682. order = EC_GROUP_get0_order(group);
  683. if (order == NULL)
  684. goto err;
  685. if (BN_is_zero(order)) {
  686. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
  687. goto err;
  688. }
  689. w = 7;
  690. if ((precomp_storage =
  691. OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
  692. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
  693. goto err;
  694. }
  695. preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
  696. P = EC_POINT_new(group);
  697. T = EC_POINT_new(group);
  698. if (P == NULL || T == NULL)
  699. goto err;
  700. /*
  701. * The zero entry is implicitly infinity, and we skip it, storing other
  702. * values with -1 offset.
  703. */
  704. if (!EC_POINT_copy(T, generator))
  705. goto err;
  706. for (k = 0; k < 64; k++) {
  707. if (!EC_POINT_copy(P, T))
  708. goto err;
  709. for (j = 0; j < 37; j++) {
  710. P256_POINT_AFFINE temp;
  711. /*
  712. * It would be faster to use EC_POINTs_make_affine and
  713. * make multiple points affine at the same time.
  714. */
  715. if (!EC_POINT_make_affine(group, P, ctx))
  716. goto err;
  717. if (!ecp_nistz256_bignum_to_field_elem(temp.X, P->X) ||
  718. !ecp_nistz256_bignum_to_field_elem(temp.Y, P->Y)) {
  719. ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE,
  720. EC_R_COORDINATES_OUT_OF_RANGE);
  721. goto err;
  722. }
  723. ecp_nistz256_scatter_w7(preComputedTable[j], &temp, k);
  724. for (i = 0; i < 7; i++) {
  725. if (!EC_POINT_dbl(group, P, P, ctx))
  726. goto err;
  727. }
  728. }
  729. if (!EC_POINT_add(group, T, T, generator, ctx))
  730. goto err;
  731. }
  732. pre_comp->group = group;
  733. pre_comp->w = w;
  734. pre_comp->precomp = preComputedTable;
  735. pre_comp->precomp_storage = precomp_storage;
  736. precomp_storage = NULL;
  737. SETPRECOMP(group, nistz256, pre_comp);
  738. pre_comp = NULL;
  739. ret = 1;
  740. err:
  741. BN_CTX_end(ctx);
  742. BN_CTX_free(new_ctx);
  743. EC_nistz256_pre_comp_free(pre_comp);
  744. OPENSSL_free(precomp_storage);
  745. EC_POINT_free(P);
  746. EC_POINT_free(T);
  747. return ret;
  748. }
  749. /*
  750. * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
  751. * code processing 4 points in parallel, corresponding serial operation
  752. * is several times slower, because it uses 29x29=58-bit multiplication
  753. * as opposite to 64x64=128-bit in integer-only scalar case. As result
  754. * it doesn't provide *significant* performance improvement. Note that
  755. * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
  756. * you'd need to compile even asm/ecp_nistz256-avx.pl module.
  757. */
  758. #if defined(ECP_NISTZ256_AVX2)
  759. # if !(defined(__x86_64) || defined(__x86_64__) || \
  760. defined(_M_AMD64) || defined(_M_X64)) || \
  761. !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
  762. # undef ECP_NISTZ256_AVX2
  763. # else
  764. /* Constant time access, loading four values, from four consecutive tables */
  765. void ecp_nistz256_avx2_multi_gather_w7(void *result, const void *in,
  766. int index0, int index1, int index2,
  767. int index3);
  768. void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
  769. void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
  770. void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
  771. const void *Bx4);
  772. void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
  773. const void *Bx4);
  774. void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
  775. void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
  776. void ecp_nistz256_avx2_set1(void *RESULTx4);
  777. int ecp_nistz_avx2_eligible(void);
  778. static void booth_recode_w7(unsigned char *sign,
  779. unsigned char *digit, unsigned char in)
  780. {
  781. unsigned char s, d;
  782. s = ~((in >> 7) - 1);
  783. d = (1 << 8) - in - 1;
  784. d = (d & s) | (in & ~s);
  785. d = (d >> 1) + (d & 1);
  786. *sign = s & 1;
  787. *digit = d;
  788. }
  789. /*
  790. * ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
  791. * precomputed table. It does 4 affine point additions in parallel,
  792. * significantly speeding up point multiplication for a fixed value.
  793. */
  794. static void ecp_nistz256_avx2_mul_g(P256_POINT *r,
  795. unsigned char p_str[33],
  796. const P256_POINT_AFFINE(*preComputedTable)[64])
  797. {
  798. const unsigned int window_size = 7;
  799. const unsigned int mask = (1 << (window_size + 1)) - 1;
  800. unsigned int wvalue;
  801. /* Using 4 windows at a time */
  802. unsigned char sign0, digit0;
  803. unsigned char sign1, digit1;
  804. unsigned char sign2, digit2;
  805. unsigned char sign3, digit3;
  806. unsigned int idx = 0;
  807. BN_ULONG tmp[P256_LIMBS];
  808. int i;
  809. ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
  810. ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
  811. ALIGN32 P256_POINT_AFFINE point_arr[4];
  812. ALIGN32 P256_POINT res_point_arr[4];
  813. /* Initial four windows */
  814. wvalue = *((u16 *) & p_str[0]);
  815. wvalue = (wvalue << 1) & mask;
  816. idx += window_size;
  817. booth_recode_w7(&sign0, &digit0, wvalue);
  818. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  819. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  820. idx += window_size;
  821. booth_recode_w7(&sign1, &digit1, wvalue);
  822. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  823. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  824. idx += window_size;
  825. booth_recode_w7(&sign2, &digit2, wvalue);
  826. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  827. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  828. idx += window_size;
  829. booth_recode_w7(&sign3, &digit3, wvalue);
  830. ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[0],
  831. digit0, digit1, digit2, digit3);
  832. ecp_nistz256_neg(tmp, point_arr[0].Y);
  833. copy_conditional(point_arr[0].Y, tmp, sign0);
  834. ecp_nistz256_neg(tmp, point_arr[1].Y);
  835. copy_conditional(point_arr[1].Y, tmp, sign1);
  836. ecp_nistz256_neg(tmp, point_arr[2].Y);
  837. copy_conditional(point_arr[2].Y, tmp, sign2);
  838. ecp_nistz256_neg(tmp, point_arr[3].Y);
  839. copy_conditional(point_arr[3].Y, tmp, sign3);
  840. ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
  841. ecp_nistz256_avx2_to_mont(aX4, aX4);
  842. ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
  843. ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
  844. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  845. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  846. idx += window_size;
  847. booth_recode_w7(&sign0, &digit0, wvalue);
  848. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  849. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  850. idx += window_size;
  851. booth_recode_w7(&sign1, &digit1, wvalue);
  852. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  853. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  854. idx += window_size;
  855. booth_recode_w7(&sign2, &digit2, wvalue);
  856. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  857. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  858. idx += window_size;
  859. booth_recode_w7(&sign3, &digit3, wvalue);
  860. ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[4 * 1],
  861. digit0, digit1, digit2, digit3);
  862. ecp_nistz256_neg(tmp, point_arr[0].Y);
  863. copy_conditional(point_arr[0].Y, tmp, sign0);
  864. ecp_nistz256_neg(tmp, point_arr[1].Y);
  865. copy_conditional(point_arr[1].Y, tmp, sign1);
  866. ecp_nistz256_neg(tmp, point_arr[2].Y);
  867. copy_conditional(point_arr[2].Y, tmp, sign2);
  868. ecp_nistz256_neg(tmp, point_arr[3].Y);
  869. copy_conditional(point_arr[3].Y, tmp, sign3);
  870. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  871. ecp_nistz256_avx2_to_mont(bX4, bX4);
  872. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  873. /* Optimized when both inputs are affine */
  874. ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
  875. for (i = 2; i < 9; i++) {
  876. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  877. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  878. idx += window_size;
  879. booth_recode_w7(&sign0, &digit0, wvalue);
  880. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  881. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  882. idx += window_size;
  883. booth_recode_w7(&sign1, &digit1, wvalue);
  884. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  885. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  886. idx += window_size;
  887. booth_recode_w7(&sign2, &digit2, wvalue);
  888. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  889. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  890. idx += window_size;
  891. booth_recode_w7(&sign3, &digit3, wvalue);
  892. ecp_nistz256_avx2_multi_gather_w7(point_arr,
  893. preComputedTable[4 * i],
  894. digit0, digit1, digit2, digit3);
  895. ecp_nistz256_neg(tmp, point_arr[0].Y);
  896. copy_conditional(point_arr[0].Y, tmp, sign0);
  897. ecp_nistz256_neg(tmp, point_arr[1].Y);
  898. copy_conditional(point_arr[1].Y, tmp, sign1);
  899. ecp_nistz256_neg(tmp, point_arr[2].Y);
  900. copy_conditional(point_arr[2].Y, tmp, sign2);
  901. ecp_nistz256_neg(tmp, point_arr[3].Y);
  902. copy_conditional(point_arr[3].Y, tmp, sign3);
  903. ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
  904. ecp_nistz256_avx2_to_mont(bX4, bX4);
  905. ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
  906. ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
  907. }
  908. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
  909. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
  910. ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
  911. ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
  912. /* Last window is performed serially */
  913. wvalue = *((u16 *) & p_str[(idx - 1) / 8]);
  914. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  915. booth_recode_w7(&sign0, &digit0, wvalue);
  916. ecp_nistz256_gather_w7((P256_POINT_AFFINE *)r,
  917. preComputedTable[36], digit0);
  918. ecp_nistz256_neg(tmp, r->Y);
  919. copy_conditional(r->Y, tmp, sign0);
  920. memcpy(r->Z, ONE, sizeof(ONE));
  921. /* Sum the four windows */
  922. ecp_nistz256_point_add(r, r, &res_point_arr[0]);
  923. ecp_nistz256_point_add(r, r, &res_point_arr[1]);
  924. ecp_nistz256_point_add(r, r, &res_point_arr[2]);
  925. ecp_nistz256_point_add(r, r, &res_point_arr[3]);
  926. }
  927. # endif
  928. #endif
  929. __owur static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
  930. const P256_POINT_AFFINE *in,
  931. BN_CTX *ctx)
  932. {
  933. int ret = 0;
  934. if ((ret = bn_set_words(out->X, in->X, P256_LIMBS))
  935. && (ret = bn_set_words(out->Y, in->Y, P256_LIMBS))
  936. && (ret = bn_set_words(out->Z, ONE, P256_LIMBS)))
  937. out->Z_is_one = 1;
  938. return ret;
  939. }
  940. /* r = scalar*G + sum(scalars[i]*points[i]) */
  941. __owur static int ecp_nistz256_points_mul(const EC_GROUP *group,
  942. EC_POINT *r,
  943. const BIGNUM *scalar,
  944. size_t num,
  945. const EC_POINT *points[],
  946. const BIGNUM *scalars[], BN_CTX *ctx)
  947. {
  948. int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
  949. unsigned char p_str[33] = { 0 };
  950. const PRECOMP256_ROW *preComputedTable = NULL;
  951. const NISTZ256_PRE_COMP *pre_comp = NULL;
  952. const EC_POINT *generator = NULL;
  953. const BIGNUM **new_scalars = NULL;
  954. const EC_POINT **new_points = NULL;
  955. unsigned int idx = 0;
  956. const unsigned int window_size = 7;
  957. const unsigned int mask = (1 << (window_size + 1)) - 1;
  958. unsigned int wvalue;
  959. ALIGN32 union {
  960. P256_POINT p;
  961. P256_POINT_AFFINE a;
  962. } t, p;
  963. BIGNUM *tmp_scalar;
  964. if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
  965. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  966. return 0;
  967. }
  968. BN_CTX_start(ctx);
  969. if (scalar) {
  970. generator = EC_GROUP_get0_generator(group);
  971. if (generator == NULL) {
  972. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
  973. goto err;
  974. }
  975. /* look if we can use precomputed multiples of generator */
  976. pre_comp = group->pre_comp.nistz256;
  977. if (pre_comp) {
  978. /*
  979. * If there is a precomputed table for the generator, check that
  980. * it was generated with the same generator.
  981. */
  982. EC_POINT *pre_comp_generator = EC_POINT_new(group);
  983. if (pre_comp_generator == NULL)
  984. goto err;
  985. ecp_nistz256_gather_w7(&p.a, pre_comp->precomp[0], 1);
  986. if (!ecp_nistz256_set_from_affine(pre_comp_generator,
  987. group, &p.a, ctx)) {
  988. EC_POINT_free(pre_comp_generator);
  989. goto err;
  990. }
  991. if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
  992. preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
  993. EC_POINT_free(pre_comp_generator);
  994. }
  995. if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
  996. /*
  997. * If there is no precomputed data, but the generator is the
  998. * default, a hardcoded table of precomputed data is used. This
  999. * is because applications, such as Apache, do not use
  1000. * EC_KEY_precompute_mult.
  1001. */
  1002. preComputedTable = ecp_nistz256_precomputed;
  1003. }
  1004. if (preComputedTable) {
  1005. if ((BN_num_bits(scalar) > 256)
  1006. || BN_is_negative(scalar)) {
  1007. if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
  1008. goto err;
  1009. if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
  1010. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
  1011. goto err;
  1012. }
  1013. scalar = tmp_scalar;
  1014. }
  1015. for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {
  1016. BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];
  1017. p_str[i + 0] = (unsigned char)d;
  1018. p_str[i + 1] = (unsigned char)(d >> 8);
  1019. p_str[i + 2] = (unsigned char)(d >> 16);
  1020. p_str[i + 3] = (unsigned char)(d >>= 24);
  1021. if (BN_BYTES == 8) {
  1022. d >>= 8;
  1023. p_str[i + 4] = (unsigned char)d;
  1024. p_str[i + 5] = (unsigned char)(d >> 8);
  1025. p_str[i + 6] = (unsigned char)(d >> 16);
  1026. p_str[i + 7] = (unsigned char)(d >> 24);
  1027. }
  1028. }
  1029. for (; i < 33; i++)
  1030. p_str[i] = 0;
  1031. #if defined(ECP_NISTZ256_AVX2)
  1032. if (ecp_nistz_avx2_eligible()) {
  1033. ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
  1034. } else
  1035. #endif
  1036. {
  1037. BN_ULONG infty;
  1038. /* First window */
  1039. wvalue = (p_str[0] << 1) & mask;
  1040. idx += window_size;
  1041. wvalue = _booth_recode_w7(wvalue);
  1042. ecp_nistz256_gather_w7(&p.a, preComputedTable[0],
  1043. wvalue >> 1);
  1044. ecp_nistz256_neg(p.p.Z, p.p.Y);
  1045. copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
  1046. /*
  1047. * Since affine infinity is encoded as (0,0) and
  1048. * Jacobian ias (,,0), we need to harmonize them
  1049. * by assigning "one" or zero to Z.
  1050. */
  1051. infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |
  1052. p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);
  1053. if (P256_LIMBS == 8)
  1054. infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |
  1055. p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);
  1056. infty = 0 - is_zero(infty);
  1057. infty = ~infty;
  1058. p.p.Z[0] = ONE[0] & infty;
  1059. p.p.Z[1] = ONE[1] & infty;
  1060. p.p.Z[2] = ONE[2] & infty;
  1061. p.p.Z[3] = ONE[3] & infty;
  1062. if (P256_LIMBS == 8) {
  1063. p.p.Z[4] = ONE[4] & infty;
  1064. p.p.Z[5] = ONE[5] & infty;
  1065. p.p.Z[6] = ONE[6] & infty;
  1066. p.p.Z[7] = ONE[7] & infty;
  1067. }
  1068. for (i = 1; i < 37; i++) {
  1069. unsigned int off = (idx - 1) / 8;
  1070. wvalue = p_str[off] | p_str[off + 1] << 8;
  1071. wvalue = (wvalue >> ((idx - 1) % 8)) & mask;
  1072. idx += window_size;
  1073. wvalue = _booth_recode_w7(wvalue);
  1074. ecp_nistz256_gather_w7(&t.a,
  1075. preComputedTable[i], wvalue >> 1);
  1076. ecp_nistz256_neg(t.p.Z, t.a.Y);
  1077. copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
  1078. ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
  1079. }
  1080. }
  1081. } else {
  1082. p_is_infinity = 1;
  1083. no_precomp_for_generator = 1;
  1084. }
  1085. } else
  1086. p_is_infinity = 1;
  1087. if (no_precomp_for_generator) {
  1088. /*
  1089. * Without a precomputed table for the generator, it has to be
  1090. * handled like a normal point.
  1091. */
  1092. new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
  1093. if (new_scalars == NULL) {
  1094. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1095. goto err;
  1096. }
  1097. new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
  1098. if (new_points == NULL) {
  1099. ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
  1100. goto err;
  1101. }
  1102. memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
  1103. new_scalars[num] = scalar;
  1104. memcpy(new_points, points, num * sizeof(EC_POINT *));
  1105. new_points[num] = generator;
  1106. scalars = new_scalars;
  1107. points = new_points;
  1108. num++;
  1109. }
  1110. if (num) {
  1111. P256_POINT *out = &t.p;
  1112. if (p_is_infinity)
  1113. out = &p.p;
  1114. if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
  1115. goto err;
  1116. if (!p_is_infinity)
  1117. ecp_nistz256_point_add(&p.p, &p.p, out);
  1118. }
  1119. /* Not constant-time, but we're only operating on the public output. */
  1120. if (!bn_set_words(r->X, p.p.X, P256_LIMBS) ||
  1121. !bn_set_words(r->Y, p.p.Y, P256_LIMBS) ||
  1122. !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {
  1123. goto err;
  1124. }
  1125. r->Z_is_one = is_one(r->Z) & 1;
  1126. ret = 1;
  1127. err:
  1128. BN_CTX_end(ctx);
  1129. OPENSSL_free(new_points);
  1130. OPENSSL_free(new_scalars);
  1131. return ret;
  1132. }
  1133. __owur static int ecp_nistz256_get_affine(const EC_GROUP *group,
  1134. const EC_POINT *point,
  1135. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  1136. {
  1137. BN_ULONG z_inv2[P256_LIMBS];
  1138. BN_ULONG z_inv3[P256_LIMBS];
  1139. BN_ULONG x_aff[P256_LIMBS];
  1140. BN_ULONG y_aff[P256_LIMBS];
  1141. BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
  1142. BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
  1143. if (EC_POINT_is_at_infinity(group, point)) {
  1144. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
  1145. return 0;
  1146. }
  1147. if (!ecp_nistz256_bignum_to_field_elem(point_x, point->X) ||
  1148. !ecp_nistz256_bignum_to_field_elem(point_y, point->Y) ||
  1149. !ecp_nistz256_bignum_to_field_elem(point_z, point->Z)) {
  1150. ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
  1151. return 0;
  1152. }
  1153. ecp_nistz256_mod_inverse(z_inv3, point_z);
  1154. ecp_nistz256_sqr_mont(z_inv2, z_inv3);
  1155. ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
  1156. if (x != NULL) {
  1157. ecp_nistz256_from_mont(x_ret, x_aff);
  1158. if (!bn_set_words(x, x_ret, P256_LIMBS))
  1159. return 0;
  1160. }
  1161. if (y != NULL) {
  1162. ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
  1163. ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
  1164. ecp_nistz256_from_mont(y_ret, y_aff);
  1165. if (!bn_set_words(y, y_ret, P256_LIMBS))
  1166. return 0;
  1167. }
  1168. return 1;
  1169. }
  1170. static NISTZ256_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
  1171. {
  1172. NISTZ256_PRE_COMP *ret = NULL;
  1173. if (!group)
  1174. return NULL;
  1175. ret = OPENSSL_zalloc(sizeof(*ret));
  1176. if (ret == NULL) {
  1177. ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1178. return ret;
  1179. }
  1180. ret->group = group;
  1181. ret->w = 6; /* default */
  1182. ret->references = 1;
  1183. ret->lock = CRYPTO_THREAD_lock_new();
  1184. if (ret->lock == NULL) {
  1185. ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  1186. OPENSSL_free(ret);
  1187. return NULL;
  1188. }
  1189. return ret;
  1190. }
  1191. NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *p)
  1192. {
  1193. int i;
  1194. if (p != NULL)
  1195. CRYPTO_UP_REF(&p->references, &i, p->lock);
  1196. return p;
  1197. }
  1198. void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *pre)
  1199. {
  1200. int i;
  1201. if (pre == NULL)
  1202. return;
  1203. CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
  1204. REF_PRINT_COUNT("EC_nistz256", pre);
  1205. if (i > 0)
  1206. return;
  1207. REF_ASSERT_ISNT(i < 0);
  1208. OPENSSL_free(pre->precomp_storage);
  1209. CRYPTO_THREAD_lock_free(pre->lock);
  1210. OPENSSL_free(pre);
  1211. }
  1212. static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
  1213. {
  1214. /* There is a hard-coded table for the default generator. */
  1215. const EC_POINT *generator = EC_GROUP_get0_generator(group);
  1216. if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
  1217. /* There is a hard-coded table for the default generator. */
  1218. return 1;
  1219. }
  1220. return HAVEPRECOMP(group, nistz256);
  1221. }
  1222. #if defined(__x86_64) || defined(__x86_64__) || \
  1223. defined(_M_AMD64) || defined(_M_X64) || \
  1224. defined(__powerpc64__) || defined(_ARCH_PP64) || \
  1225. defined(__aarch64__)
  1226. /*
  1227. * Montgomery mul modulo Order(P): res = a*b*2^-256 mod Order(P)
  1228. */
  1229. void ecp_nistz256_ord_mul_mont(BN_ULONG res[P256_LIMBS],
  1230. const BN_ULONG a[P256_LIMBS],
  1231. const BN_ULONG b[P256_LIMBS]);
  1232. void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS],
  1233. const BN_ULONG a[P256_LIMBS],
  1234. BN_ULONG rep);
  1235. static int ecp_nistz256_inv_mod_ord(const EC_GROUP *group, BIGNUM *r,
  1236. const BIGNUM *x, BN_CTX *ctx)
  1237. {
  1238. /* RR = 2^512 mod ord(p256) */
  1239. static const BN_ULONG RR[P256_LIMBS] = {
  1240. TOBN(0x83244c95,0xbe79eea2), TOBN(0x4699799c,0x49bd6fa6),
  1241. TOBN(0x2845b239,0x2b6bec59), TOBN(0x66e12d94,0xf3d95620)
  1242. };
  1243. /* The constant 1 (unlike ONE that is one in Montgomery representation) */
  1244. static const BN_ULONG one[P256_LIMBS] = {
  1245. TOBN(0,1), TOBN(0,0), TOBN(0,0), TOBN(0,0)
  1246. };
  1247. /*
  1248. * We don't use entry 0 in the table, so we omit it and address
  1249. * with -1 offset.
  1250. */
  1251. BN_ULONG table[15][P256_LIMBS];
  1252. BN_ULONG out[P256_LIMBS], t[P256_LIMBS];
  1253. int i, ret = 0;
  1254. enum {
  1255. i_1 = 0, i_10, i_11, i_101, i_111, i_1010, i_1111,
  1256. i_10101, i_101010, i_101111, i_x6, i_x8, i_x16, i_x32
  1257. };
  1258. /*
  1259. * Catch allocation failure early.
  1260. */
  1261. if (bn_wexpand(r, P256_LIMBS) == NULL) {
  1262. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
  1263. goto err;
  1264. }
  1265. if ((BN_num_bits(x) > 256) || BN_is_negative(x)) {
  1266. BIGNUM *tmp;
  1267. if ((tmp = BN_CTX_get(ctx)) == NULL
  1268. || !BN_nnmod(tmp, x, group->order, ctx)) {
  1269. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
  1270. goto err;
  1271. }
  1272. x = tmp;
  1273. }
  1274. if (!ecp_nistz256_bignum_to_field_elem(t, x)) {
  1275. ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, EC_R_COORDINATES_OUT_OF_RANGE);
  1276. goto err;
  1277. }
  1278. ecp_nistz256_ord_mul_mont(table[0], t, RR);
  1279. #if 0
  1280. /*
  1281. * Original sparse-then-fixed-window algorithm, retained for reference.
  1282. */
  1283. for (i = 2; i < 16; i += 2) {
  1284. ecp_nistz256_ord_sqr_mont(table[i-1], table[i/2-1], 1);
  1285. ecp_nistz256_ord_mul_mont(table[i], table[i-1], table[0]);
  1286. }
  1287. /*
  1288. * The top 128bit of the exponent are highly redudndant, so we
  1289. * perform an optimized flow
  1290. */
  1291. ecp_nistz256_ord_sqr_mont(t, table[15-1], 4); /* f0 */
  1292. ecp_nistz256_ord_mul_mont(t, t, table[15-1]); /* ff */
  1293. ecp_nistz256_ord_sqr_mont(out, t, 8); /* ff00 */
  1294. ecp_nistz256_ord_mul_mont(out, out, t); /* ffff */
  1295. ecp_nistz256_ord_sqr_mont(t, out, 16); /* ffff0000 */
  1296. ecp_nistz256_ord_mul_mont(t, t, out); /* ffffffff */
  1297. ecp_nistz256_ord_sqr_mont(out, t, 64); /* ffffffff0000000000000000 */
  1298. ecp_nistz256_ord_mul_mont(out, out, t); /* ffffffff00000000ffffffff */
  1299. ecp_nistz256_ord_sqr_mont(out, out, 32); /* ffffffff00000000ffffffff00000000 */
  1300. ecp_nistz256_ord_mul_mont(out, out, t); /* ffffffff00000000ffffffffffffffff */
  1301. /*
  1302. * The bottom 128 bit of the exponent are processed with fixed 4-bit window
  1303. */
  1304. for(i = 0; i < 32; i++) {
  1305. /* expLo - the low 128 bits of the exponent we use (ord(p256) - 2),
  1306. * split into nibbles */
  1307. static const unsigned char expLo[32] = {
  1308. 0xb,0xc,0xe,0x6,0xf,0xa,0xa,0xd,0xa,0x7,0x1,0x7,0x9,0xe,0x8,0x4,
  1309. 0xf,0x3,0xb,0x9,0xc,0xa,0xc,0x2,0xf,0xc,0x6,0x3,0x2,0x5,0x4,0xf
  1310. };
  1311. ecp_nistz256_ord_sqr_mont(out, out, 4);
  1312. /* The exponent is public, no need in constant-time access */
  1313. ecp_nistz256_ord_mul_mont(out, out, table[expLo[i]-1]);
  1314. }
  1315. #else
  1316. /*
  1317. * https://briansmith.org/ecc-inversion-addition-chains-01#p256_scalar_inversion
  1318. *
  1319. * Even though this code path spares 12 squarings, 4.5%, and 13
  1320. * multiplications, 25%, on grand scale sign operation is not that
  1321. * much faster, not more that 2%...
  1322. */
  1323. /* pre-calculate powers */
  1324. ecp_nistz256_ord_sqr_mont(table[i_10], table[i_1], 1);
  1325. ecp_nistz256_ord_mul_mont(table[i_11], table[i_1], table[i_10]);
  1326. ecp_nistz256_ord_mul_mont(table[i_101], table[i_11], table[i_10]);
  1327. ecp_nistz256_ord_mul_mont(table[i_111], table[i_101], table[i_10]);
  1328. ecp_nistz256_ord_sqr_mont(table[i_1010], table[i_101], 1);
  1329. ecp_nistz256_ord_mul_mont(table[i_1111], table[i_1010], table[i_101]);
  1330. ecp_nistz256_ord_sqr_mont(table[i_10101], table[i_1010], 1);
  1331. ecp_nistz256_ord_mul_mont(table[i_10101], table[i_10101], table[i_1]);
  1332. ecp_nistz256_ord_sqr_mont(table[i_101010], table[i_10101], 1);
  1333. ecp_nistz256_ord_mul_mont(table[i_101111], table[i_101010], table[i_101]);
  1334. ecp_nistz256_ord_mul_mont(table[i_x6], table[i_101010], table[i_10101]);
  1335. ecp_nistz256_ord_sqr_mont(table[i_x8], table[i_x6], 2);
  1336. ecp_nistz256_ord_mul_mont(table[i_x8], table[i_x8], table[i_11]);
  1337. ecp_nistz256_ord_sqr_mont(table[i_x16], table[i_x8], 8);
  1338. ecp_nistz256_ord_mul_mont(table[i_x16], table[i_x16], table[i_x8]);
  1339. ecp_nistz256_ord_sqr_mont(table[i_x32], table[i_x16], 16);
  1340. ecp_nistz256_ord_mul_mont(table[i_x32], table[i_x32], table[i_x16]);
  1341. /* calculations */
  1342. ecp_nistz256_ord_sqr_mont(out, table[i_x32], 64);
  1343. ecp_nistz256_ord_mul_mont(out, out, table[i_x32]);
  1344. for (i = 0; i < 27; i++) {
  1345. static const struct { unsigned char p, i; } chain[27] = {
  1346. { 32, i_x32 }, { 6, i_101111 }, { 5, i_111 },
  1347. { 4, i_11 }, { 5, i_1111 }, { 5, i_10101 },
  1348. { 4, i_101 }, { 3, i_101 }, { 3, i_101 },
  1349. { 5, i_111 }, { 9, i_101111 }, { 6, i_1111 },
  1350. { 2, i_1 }, { 5, i_1 }, { 6, i_1111 },
  1351. { 5, i_111 }, { 4, i_111 }, { 5, i_111 },
  1352. { 5, i_101 }, { 3, i_11 }, { 10, i_101111 },
  1353. { 2, i_11 }, { 5, i_11 }, { 5, i_11 },
  1354. { 3, i_1 }, { 7, i_10101 }, { 6, i_1111 }
  1355. };
  1356. ecp_nistz256_ord_sqr_mont(out, out, chain[i].p);
  1357. ecp_nistz256_ord_mul_mont(out, out, table[chain[i].i]);
  1358. }
  1359. #endif
  1360. ecp_nistz256_ord_mul_mont(out, out, one);
  1361. /*
  1362. * Can't fail, but check return code to be consistent anyway.
  1363. */
  1364. if (!bn_set_words(r, out, P256_LIMBS))
  1365. goto err;
  1366. ret = 1;
  1367. err:
  1368. return ret;
  1369. }
  1370. #else
  1371. # define ecp_nistz256_inv_mod_ord NULL
  1372. #endif
  1373. const EC_METHOD *EC_GFp_nistz256_method(void)
  1374. {
  1375. static const EC_METHOD ret = {
  1376. EC_FLAGS_DEFAULT_OCT,
  1377. NID_X9_62_prime_field,
  1378. ec_GFp_mont_group_init,
  1379. ec_GFp_mont_group_finish,
  1380. ec_GFp_mont_group_clear_finish,
  1381. ec_GFp_mont_group_copy,
  1382. ec_GFp_mont_group_set_curve,
  1383. ec_GFp_simple_group_get_curve,
  1384. ec_GFp_simple_group_get_degree,
  1385. ec_group_simple_order_bits,
  1386. ec_GFp_simple_group_check_discriminant,
  1387. ec_GFp_simple_point_init,
  1388. ec_GFp_simple_point_finish,
  1389. ec_GFp_simple_point_clear_finish,
  1390. ec_GFp_simple_point_copy,
  1391. ec_GFp_simple_point_set_to_infinity,
  1392. ec_GFp_simple_set_Jprojective_coordinates_GFp,
  1393. ec_GFp_simple_get_Jprojective_coordinates_GFp,
  1394. ec_GFp_simple_point_set_affine_coordinates,
  1395. ecp_nistz256_get_affine,
  1396. 0, 0, 0,
  1397. ec_GFp_simple_add,
  1398. ec_GFp_simple_dbl,
  1399. ec_GFp_simple_invert,
  1400. ec_GFp_simple_is_at_infinity,
  1401. ec_GFp_simple_is_on_curve,
  1402. ec_GFp_simple_cmp,
  1403. ec_GFp_simple_make_affine,
  1404. ec_GFp_simple_points_make_affine,
  1405. ecp_nistz256_points_mul, /* mul */
  1406. ecp_nistz256_mult_precompute, /* precompute_mult */
  1407. ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
  1408. ec_GFp_mont_field_mul,
  1409. ec_GFp_mont_field_sqr,
  1410. 0, /* field_div */
  1411. ec_GFp_mont_field_inv,
  1412. ec_GFp_mont_field_encode,
  1413. ec_GFp_mont_field_decode,
  1414. ec_GFp_mont_field_set_to_one,
  1415. ec_key_simple_priv2oct,
  1416. ec_key_simple_oct2priv,
  1417. 0, /* set private */
  1418. ec_key_simple_generate_key,
  1419. ec_key_simple_check_key,
  1420. ec_key_simple_generate_public_key,
  1421. 0, /* keycopy */
  1422. 0, /* keyfinish */
  1423. ecdh_simple_compute_key,
  1424. ecdsa_simple_sign_setup,
  1425. ecdsa_simple_sign_sig,
  1426. ecdsa_simple_verify_sig,
  1427. ecp_nistz256_inv_mod_ord, /* can be #define-d NULL */
  1428. 0, /* blind_coordinates */
  1429. 0, /* ladder_pre */
  1430. 0, /* ladder_step */
  1431. 0 /* ladder_post */
  1432. };
  1433. return &ret;
  1434. }