ecp_nistz256.c 51 KB

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