ecp_nistz256.c 52 KB

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