ecp_sm2p256.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. *
  9. */
  10. /*
  11. * SM2 low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include <openssl/err.h>
  17. #include "crypto/bn.h"
  18. #include "ec_local.h"
  19. #include "internal/constant_time.h"
  20. #if defined(__GNUC__)
  21. # define ALIGN32 __attribute((aligned(32)))
  22. # define ALIGN64 __attribute((aligned(64)))
  23. #elif defined(_MSC_VER)
  24. # define ALIGN32 __declspec(align(32))
  25. # define ALIGN64 __declspec(align(64))
  26. #else
  27. # define ALIGN32
  28. # define ALIGN64
  29. #endif
  30. #define P256_LIMBS (256 / BN_BITS2)
  31. #if !defined(OPENSSL_NO_SM2_PRECOMP)
  32. extern const BN_ULONG ecp_sm2p256_precomputed[8 * 32 * 256];
  33. #endif
  34. typedef struct {
  35. BN_ULONG X[P256_LIMBS];
  36. BN_ULONG Y[P256_LIMBS];
  37. BN_ULONG Z[P256_LIMBS];
  38. } P256_POINT;
  39. typedef struct {
  40. BN_ULONG X[P256_LIMBS];
  41. BN_ULONG Y[P256_LIMBS];
  42. } P256_POINT_AFFINE;
  43. #if !defined(OPENSSL_NO_SM2_PRECOMP)
  44. /* Coordinates of G, for which we have precomputed tables */
  45. static const BN_ULONG def_xG[P256_LIMBS] ALIGN32 = {
  46. 0x715a4589334c74c7, 0x8fe30bbff2660be1,
  47. 0x5f9904466a39c994, 0x32c4ae2c1f198119
  48. };
  49. static const BN_ULONG def_yG[P256_LIMBS] ALIGN32 = {
  50. 0x02df32e52139f0a0, 0xd0a9877cc62a4740,
  51. 0x59bdcee36b692153, 0xbc3736a2f4f6779c,
  52. };
  53. #endif
  54. /* p and order for SM2 according to GB/T 32918.5-2017 */
  55. static const BN_ULONG def_p[P256_LIMBS] ALIGN32 = {
  56. 0xffffffffffffffff, 0xffffffff00000000,
  57. 0xffffffffffffffff, 0xfffffffeffffffff
  58. };
  59. static const BN_ULONG def_ord[P256_LIMBS] ALIGN32 = {
  60. 0x53bbf40939d54123, 0x7203df6b21c6052b,
  61. 0xffffffffffffffff, 0xfffffffeffffffff
  62. };
  63. static const BN_ULONG ONE[P256_LIMBS] ALIGN32 = {1, 0, 0, 0};
  64. /* Functions implemented in assembly */
  65. /*
  66. * Most of below mentioned functions *preserve* the property of inputs
  67. * being fully reduced, i.e. being in [0, modulus) range. Simply put if
  68. * inputs are fully reduced, then output is too.
  69. */
  70. /* Right shift: a >> 1 */
  71. void bn_rshift1(BN_ULONG *a);
  72. /* Sub: r = a - b */
  73. void bn_sub(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
  74. /* Modular div by 2: r = a / 2 mod p */
  75. void ecp_sm2p256_div_by_2(BN_ULONG *r, const BN_ULONG *a);
  76. /* Modular div by 2: r = a / 2 mod n, where n = ord(p) */
  77. void ecp_sm2p256_div_by_2_mod_ord(BN_ULONG *r, const BN_ULONG *a);
  78. /* Modular add: r = a + b mod p */
  79. void ecp_sm2p256_add(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
  80. /* Modular sub: r = a - b mod p */
  81. void ecp_sm2p256_sub(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
  82. /* Modular sub: r = a - b mod n, where n = ord(p) */
  83. void ecp_sm2p256_sub_mod_ord(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
  84. /* Modular mul by 3: out = 3 * a mod p */
  85. void ecp_sm2p256_mul_by_3(BN_ULONG *r, const BN_ULONG *a);
  86. /* Modular mul: r = a * b mod p */
  87. void ecp_sm2p256_mul(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b);
  88. /* Modular sqr: r = a ^ 2 mod p */
  89. void ecp_sm2p256_sqr(BN_ULONG *r, const BN_ULONG *a);
  90. static ossl_inline BN_ULONG is_zeros(const BN_ULONG *a)
  91. {
  92. BN_ULONG res;
  93. res = a[0] | a[1] | a[2] | a[3];
  94. return constant_time_is_zero_64(res);
  95. }
  96. static ossl_inline int is_equal(const BN_ULONG *a, const BN_ULONG *b)
  97. {
  98. BN_ULONG res;
  99. res = a[0] ^ b[0];
  100. res |= a[1] ^ b[1];
  101. res |= a[2] ^ b[2];
  102. res |= a[3] ^ b[3];
  103. return constant_time_is_zero_64(res);
  104. }
  105. static ossl_inline int is_greater(const BN_ULONG *a, const BN_ULONG *b)
  106. {
  107. int i;
  108. for (i = P256_LIMBS - 1; i >= 0; --i) {
  109. if (a[i] > b[i])
  110. return 1;
  111. if (a[i] < b[i])
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. #define is_one(a) is_equal(a, ONE)
  117. #define is_even(a) !(a[0] & 1)
  118. #define is_point_equal(a, b) \
  119. is_equal(a->X, b->X) && \
  120. is_equal(a->Y, b->Y) && \
  121. is_equal(a->Z, b->Z)
  122. /* Bignum and field elements conversion */
  123. #define ecp_sm2p256_bignum_field_elem(out, in) \
  124. bn_copy_words(out, in, P256_LIMBS)
  125. /* Binary algorithm for inversion in Fp */
  126. #define BN_MOD_INV(out, in, mod_div, mod_sub, mod) \
  127. do { \
  128. BN_ULONG u[4] ALIGN32; \
  129. BN_ULONG v[4] ALIGN32; \
  130. BN_ULONG x1[4] ALIGN32 = {1, 0, 0, 0}; \
  131. BN_ULONG x2[4] ALIGN32 = {0}; \
  132. \
  133. if (is_zeros(in)) \
  134. return; \
  135. memcpy(u, in, 32); \
  136. memcpy(v, mod, 32); \
  137. while (!is_one(u) && !is_one(v)) { \
  138. while (is_even(u)) { \
  139. bn_rshift1(u); \
  140. mod_div(x1, x1); \
  141. } \
  142. while (is_even(v)) { \
  143. bn_rshift1(v); \
  144. mod_div(x2, x2); \
  145. } \
  146. if (is_greater(u, v) == 1) { \
  147. bn_sub(u, u, v); \
  148. mod_sub(x1, x1, x2); \
  149. } else { \
  150. bn_sub(v, v, u); \
  151. mod_sub(x2, x2, x1); \
  152. } \
  153. } \
  154. if (is_one(u)) \
  155. memcpy(out, x1, 32); \
  156. else \
  157. memcpy(out, x2, 32); \
  158. } while (0)
  159. /* Modular inverse |out| = |in|^(-1) mod |p|. */
  160. static ossl_inline void ecp_sm2p256_mod_inverse(BN_ULONG* out,
  161. const BN_ULONG* in) {
  162. BN_MOD_INV(out, in, ecp_sm2p256_div_by_2, ecp_sm2p256_sub, def_p);
  163. }
  164. /* Modular inverse mod order |out| = |in|^(-1) % |ord|. */
  165. static ossl_inline void ecp_sm2p256_mod_ord_inverse(BN_ULONG* out,
  166. const BN_ULONG* in) {
  167. BN_MOD_INV(out, in, ecp_sm2p256_div_by_2_mod_ord, ecp_sm2p256_sub_mod_ord,
  168. def_ord);
  169. }
  170. /* Point double: R <- P + P */
  171. static void ecp_sm2p256_point_double(P256_POINT *R, const P256_POINT *P)
  172. {
  173. unsigned int i;
  174. BN_ULONG tmp0[P256_LIMBS] ALIGN32;
  175. BN_ULONG tmp1[P256_LIMBS] ALIGN32;
  176. BN_ULONG tmp2[P256_LIMBS] ALIGN32;
  177. /* zero-check P->Z */
  178. if (is_zeros(P->Z)) {
  179. for (i = 0; i < P256_LIMBS; ++i)
  180. R->Z[i] = 0;
  181. return;
  182. }
  183. ecp_sm2p256_sqr(tmp0, P->Z);
  184. ecp_sm2p256_sub(tmp1, P->X, tmp0);
  185. ecp_sm2p256_add(tmp0, P->X, tmp0);
  186. ecp_sm2p256_mul(tmp1, tmp1, tmp0);
  187. ecp_sm2p256_mul_by_3(tmp1, tmp1);
  188. ecp_sm2p256_add(R->Y, P->Y, P->Y);
  189. ecp_sm2p256_mul(R->Z, R->Y, P->Z);
  190. ecp_sm2p256_sqr(R->Y, R->Y);
  191. ecp_sm2p256_mul(tmp2, R->Y, P->X);
  192. ecp_sm2p256_sqr(R->Y, R->Y);
  193. ecp_sm2p256_div_by_2(R->Y, R->Y);
  194. ecp_sm2p256_sqr(R->X, tmp1);
  195. ecp_sm2p256_add(tmp0, tmp2, tmp2);
  196. ecp_sm2p256_sub(R->X, R->X, tmp0);
  197. ecp_sm2p256_sub(tmp0, tmp2, R->X);
  198. ecp_sm2p256_mul(tmp0, tmp0, tmp1);
  199. ecp_sm2p256_sub(tmp1, tmp0, R->Y);
  200. memcpy(R->Y, tmp1, 32);
  201. }
  202. /* Point add affine: R <- P + Q */
  203. static void ecp_sm2p256_point_add_affine(P256_POINT *R, const P256_POINT *P,
  204. const P256_POINT_AFFINE *Q)
  205. {
  206. unsigned int i;
  207. BN_ULONG tmp0[P256_LIMBS] ALIGN32 = {0};
  208. BN_ULONG tmp1[P256_LIMBS] ALIGN32 = {0};
  209. BN_ULONG tmp2[P256_LIMBS] ALIGN32 = {0};
  210. BN_ULONG tmp3[P256_LIMBS] ALIGN32 = {0};
  211. /* zero-check P->Z */
  212. if (is_zeros(P->Z)) {
  213. for (i = 0; i < P256_LIMBS; ++i) {
  214. R->X[i] = Q->X[i];
  215. R->Y[i] = Q->Y[i];
  216. R->Z[i] = 0;
  217. }
  218. R->Z[0] = 1;
  219. return;
  220. }
  221. ecp_sm2p256_sqr(tmp0, P->Z);
  222. ecp_sm2p256_mul(tmp1, tmp0, P->Z);
  223. ecp_sm2p256_mul(tmp0, tmp0, Q->X);
  224. ecp_sm2p256_mul(tmp1, tmp1, Q->Y);
  225. ecp_sm2p256_sub(tmp0, tmp0, P->X);
  226. ecp_sm2p256_sub(tmp1, tmp1, P->Y);
  227. /* zero-check tmp0, tmp1 */
  228. if (is_zeros(tmp0)) {
  229. if (is_zeros(tmp1)) {
  230. P256_POINT K;
  231. for (i = 0; i < P256_LIMBS; ++i) {
  232. K.X[i] = Q->X[i];
  233. K.Y[i] = Q->Y[i];
  234. K.Z[i] = 0;
  235. }
  236. K.Z[0] = 1;
  237. ecp_sm2p256_point_double(R, &K);
  238. } else {
  239. for (i = 0; i < P256_LIMBS; ++i)
  240. R->Z[i] = 0;
  241. }
  242. return;
  243. }
  244. ecp_sm2p256_mul(R->Z, P->Z, tmp0);
  245. ecp_sm2p256_sqr(tmp2, tmp0);
  246. ecp_sm2p256_mul(tmp3, tmp2, tmp0);
  247. ecp_sm2p256_mul(tmp2, tmp2, P->X);
  248. ecp_sm2p256_add(tmp0, tmp2, tmp2);
  249. ecp_sm2p256_sqr(R->X, tmp1);
  250. ecp_sm2p256_sub(R->X, R->X, tmp0);
  251. ecp_sm2p256_sub(R->X, R->X, tmp3);
  252. ecp_sm2p256_sub(tmp2, tmp2, R->X);
  253. ecp_sm2p256_mul(tmp2, tmp2, tmp1);
  254. ecp_sm2p256_mul(tmp3, tmp3, P->Y);
  255. ecp_sm2p256_sub(R->Y, tmp2, tmp3);
  256. }
  257. /* Point add: R <- P + Q */
  258. static void ecp_sm2p256_point_add(P256_POINT *R, const P256_POINT *P,
  259. const P256_POINT *Q)
  260. {
  261. unsigned int i;
  262. BN_ULONG tmp0[P256_LIMBS] ALIGN32 = {0};
  263. BN_ULONG tmp1[P256_LIMBS] ALIGN32 = {0};
  264. BN_ULONG tmp2[P256_LIMBS] ALIGN32 = {0};
  265. /* zero-check P | Q ->Z */
  266. if (is_zeros(P->Z)) {
  267. for (i = 0; i < P256_LIMBS; ++i) {
  268. R->X[i] = Q->X[i];
  269. R->Y[i] = Q->Y[i];
  270. R->Z[i] = Q->Z[i];
  271. }
  272. return;
  273. } else if (is_zeros(Q->Z)) {
  274. for (i = 0; i < P256_LIMBS; ++i) {
  275. R->X[i] = P->X[i];
  276. R->Y[i] = P->Y[i];
  277. R->Z[i] = P->Z[i];
  278. }
  279. return;
  280. } else if (is_point_equal(P, Q)) {
  281. ecp_sm2p256_point_double(R, Q);
  282. return;
  283. }
  284. ecp_sm2p256_sqr(tmp0, P->Z);
  285. ecp_sm2p256_mul(tmp1, tmp0, P->Z);
  286. ecp_sm2p256_mul(tmp0, tmp0, Q->X);
  287. ecp_sm2p256_mul(tmp1, tmp1, Q->Y);
  288. ecp_sm2p256_mul(R->Y, P->Y, Q->Z);
  289. ecp_sm2p256_mul(R->Z, Q->Z, P->Z);
  290. ecp_sm2p256_sqr(tmp2, Q->Z);
  291. ecp_sm2p256_mul(R->Y, tmp2, R->Y);
  292. ecp_sm2p256_mul(R->X, tmp2, P->X);
  293. ecp_sm2p256_sub(tmp0, tmp0, R->X);
  294. ecp_sm2p256_mul(R->Z, tmp0, R->Z);
  295. ecp_sm2p256_sub(tmp1, tmp1, R->Y);
  296. ecp_sm2p256_sqr(tmp2, tmp0);
  297. ecp_sm2p256_mul(tmp0, tmp0, tmp2);
  298. ecp_sm2p256_mul(tmp2, tmp2, R->X);
  299. ecp_sm2p256_sqr(R->X, tmp1);
  300. ecp_sm2p256_sub(R->X, R->X, tmp2);
  301. ecp_sm2p256_sub(R->X, R->X, tmp2);
  302. ecp_sm2p256_sub(R->X, R->X, tmp0);
  303. ecp_sm2p256_sub(tmp2, tmp2, R->X);
  304. ecp_sm2p256_mul(tmp2, tmp1, tmp2);
  305. ecp_sm2p256_mul(tmp0, tmp0, R->Y);
  306. ecp_sm2p256_sub(R->Y, tmp2, tmp0);
  307. }
  308. #if !defined(OPENSSL_NO_SM2_PRECOMP)
  309. /* Base point mul by scalar: k - scalar, G - base point */
  310. static void ecp_sm2p256_point_G_mul_by_scalar(P256_POINT *R, const BN_ULONG *k)
  311. {
  312. unsigned int i, index, mask = 0xff;
  313. P256_POINT_AFFINE Q;
  314. memset(R, 0, sizeof(P256_POINT));
  315. if (is_zeros(k))
  316. return;
  317. index = k[0] & mask;
  318. if (index) {
  319. index = index * 8;
  320. memcpy(R->X, ecp_sm2p256_precomputed + index, 32);
  321. memcpy(R->Y, ecp_sm2p256_precomputed + index + P256_LIMBS, 32);
  322. R->Z[0] = 1;
  323. }
  324. for (i = 1; i < 32; ++i) {
  325. index = (k[i / 8] >> (8 * (i % 8))) & mask;
  326. if (index) {
  327. index = index + i * 256;
  328. index = index * 8;
  329. memcpy(Q.X, ecp_sm2p256_precomputed + index, 32);
  330. memcpy(Q.Y, ecp_sm2p256_precomputed + index + P256_LIMBS, 32);
  331. ecp_sm2p256_point_add_affine(R, R, &Q);
  332. }
  333. }
  334. }
  335. #endif
  336. /*
  337. * Affine point mul by scalar: k - scalar, P - affine point
  338. */
  339. static void ecp_sm2p256_point_P_mul_by_scalar(P256_POINT *R, const BN_ULONG *k,
  340. P256_POINT_AFFINE P)
  341. {
  342. int i, init = 0;
  343. unsigned int index, mask = 0x0f;
  344. P256_POINT precomputed[16] ALIGN64;
  345. memset(R, 0, sizeof(P256_POINT));
  346. if (is_zeros(k))
  347. return;
  348. /* The first value of the precomputed table is P. */
  349. memcpy(precomputed[1].X, P.X, 32);
  350. memcpy(precomputed[1].Y, P.Y, 32);
  351. precomputed[1].Z[0] = 1;
  352. precomputed[1].Z[1] = 0;
  353. precomputed[1].Z[2] = 0;
  354. precomputed[1].Z[3] = 0;
  355. /* The second value of the precomputed table is 2P. */
  356. ecp_sm2p256_point_double(&precomputed[2], &precomputed[1]);
  357. /* The subsequent elements are 3P, 4P, and so on. */
  358. for (i = 3; i < 16; ++i)
  359. ecp_sm2p256_point_add_affine(&precomputed[i], &precomputed[i - 1], &P);
  360. for (i = 64 - 1; i >= 0; --i) {
  361. index = (k[i / 16] >> (4 * (i % 16))) & mask;
  362. if (init == 0) {
  363. if (index) {
  364. memcpy(R, &precomputed[index], sizeof(P256_POINT));
  365. init = 1;
  366. }
  367. } else {
  368. ecp_sm2p256_point_double(R, R);
  369. ecp_sm2p256_point_double(R, R);
  370. ecp_sm2p256_point_double(R, R);
  371. ecp_sm2p256_point_double(R, R);
  372. if (index)
  373. ecp_sm2p256_point_add(R, R, &precomputed[index]);
  374. }
  375. }
  376. }
  377. /* Get affine point */
  378. static void ecp_sm2p256_point_get_affine(P256_POINT_AFFINE *R,
  379. const P256_POINT *P)
  380. {
  381. BN_ULONG z_inv3[P256_LIMBS] ALIGN32 = {0};
  382. BN_ULONG z_inv2[P256_LIMBS] ALIGN32 = {0};
  383. if (is_one(P->Z)) {
  384. memcpy(R->X, P->X, 32);
  385. memcpy(R->Y, P->Y, 32);
  386. return;
  387. }
  388. ecp_sm2p256_mod_inverse(z_inv3, P->Z);
  389. ecp_sm2p256_sqr(z_inv2, z_inv3);
  390. ecp_sm2p256_mul(R->X, P->X, z_inv2);
  391. ecp_sm2p256_mul(z_inv3, z_inv3, z_inv2);
  392. ecp_sm2p256_mul(R->Y, P->Y, z_inv3);
  393. }
  394. #if !defined(OPENSSL_NO_SM2_PRECOMP)
  395. static int ecp_sm2p256_is_affine_G(const EC_POINT *generator)
  396. {
  397. return (bn_get_top(generator->X) == P256_LIMBS)
  398. && (bn_get_top(generator->Y) == P256_LIMBS)
  399. && is_equal(bn_get_words(generator->X), def_xG)
  400. && is_equal(bn_get_words(generator->Y), def_yG)
  401. && (generator->Z_is_one == 1);
  402. }
  403. #endif
  404. /*
  405. * Convert Jacobian coordinate point into affine coordinate (x,y)
  406. */
  407. static int ecp_sm2p256_get_affine(const EC_GROUP *group,
  408. const EC_POINT *point,
  409. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  410. {
  411. BN_ULONG z_inv2[P256_LIMBS] ALIGN32 = {0};
  412. BN_ULONG z_inv3[P256_LIMBS] ALIGN32 = {0};
  413. BN_ULONG x_aff[P256_LIMBS] ALIGN32 = {0};
  414. BN_ULONG y_aff[P256_LIMBS] ALIGN32 = {0};
  415. BN_ULONG point_x[P256_LIMBS] ALIGN32 = {0};
  416. BN_ULONG point_y[P256_LIMBS] ALIGN32 = {0};
  417. BN_ULONG point_z[P256_LIMBS] ALIGN32 = {0};
  418. if (EC_POINT_is_at_infinity(group, point)) {
  419. ECerr(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
  420. return 0;
  421. }
  422. if (ecp_sm2p256_bignum_field_elem(point_x, point->X) <= 0
  423. || ecp_sm2p256_bignum_field_elem(point_y, point->Y) <= 0
  424. || ecp_sm2p256_bignum_field_elem(point_z, point->Z) <= 0) {
  425. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  426. return 0;
  427. }
  428. ecp_sm2p256_mod_inverse(z_inv3, point_z);
  429. ecp_sm2p256_sqr(z_inv2, z_inv3);
  430. if (x != NULL) {
  431. ecp_sm2p256_mul(x_aff, point_x, z_inv2);
  432. if (!bn_set_words(x, x_aff, P256_LIMBS))
  433. return 0;
  434. }
  435. if (y != NULL) {
  436. ecp_sm2p256_mul(z_inv3, z_inv3, z_inv2);
  437. ecp_sm2p256_mul(y_aff, point_y, z_inv3);
  438. if (!bn_set_words(y, y_aff, P256_LIMBS))
  439. return 0;
  440. }
  441. return 1;
  442. }
  443. /* r = sum(scalar[i]*point[i]) */
  444. static int ecp_sm2p256_windowed_mul(const EC_GROUP *group,
  445. P256_POINT *r,
  446. const BIGNUM **scalar,
  447. const EC_POINT **point,
  448. size_t num, BN_CTX *ctx)
  449. {
  450. unsigned int i;
  451. int ret = 0;
  452. const BIGNUM **scalars = NULL;
  453. BN_ULONG k[P256_LIMBS] ALIGN32 = {0};
  454. P256_POINT kP;
  455. ALIGN32 union {
  456. P256_POINT p;
  457. P256_POINT_AFFINE a;
  458. } t, p;
  459. if (num > OPENSSL_MALLOC_MAX_NELEMS(P256_POINT)
  460. || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
  461. ECerr(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  462. goto err;
  463. }
  464. memset(r, 0, sizeof(P256_POINT));
  465. for (i = 0; i < num; i++) {
  466. if (EC_POINT_is_at_infinity(group, point[i]))
  467. continue;
  468. if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
  469. BIGNUM *tmp;
  470. if ((tmp = BN_CTX_get(ctx)) == NULL)
  471. goto err;
  472. if (!BN_nnmod(tmp, scalar[i], group->order, ctx)) {
  473. ECerr(ERR_LIB_EC, ERR_R_BN_LIB);
  474. goto err;
  475. }
  476. scalars[i] = tmp;
  477. } else {
  478. scalars[i] = scalar[i];
  479. }
  480. if (ecp_sm2p256_bignum_field_elem(k, scalars[i]) <= 0
  481. || ecp_sm2p256_bignum_field_elem(p.p.X, point[i]->X) <= 0
  482. || ecp_sm2p256_bignum_field_elem(p.p.Y, point[i]->Y) <= 0
  483. || ecp_sm2p256_bignum_field_elem(p.p.Z, point[i]->Z) <= 0) {
  484. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  485. goto err;
  486. }
  487. ecp_sm2p256_point_get_affine(&t.a, &p.p);
  488. ecp_sm2p256_point_P_mul_by_scalar(&kP, k, t.a);
  489. ecp_sm2p256_point_add(r, r, &kP);
  490. }
  491. ret = 1;
  492. err:
  493. OPENSSL_free(scalars);
  494. return ret;
  495. }
  496. /* r = scalar*G + sum(scalars[i]*points[i]) */
  497. static int ecp_sm2p256_points_mul(const EC_GROUP *group,
  498. EC_POINT *r,
  499. const BIGNUM *scalar,
  500. size_t num,
  501. const EC_POINT *points[],
  502. const BIGNUM *scalars[], BN_CTX *ctx)
  503. {
  504. int ret = 0, p_is_infinity = 0;
  505. const EC_POINT *generator = NULL;
  506. BN_ULONG k[P256_LIMBS] ALIGN32 = {0};
  507. ALIGN32 union {
  508. P256_POINT p;
  509. P256_POINT_AFFINE a;
  510. } t, p;
  511. if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
  512. ECerr(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  513. goto err;
  514. }
  515. BN_CTX_start(ctx);
  516. if (scalar) {
  517. generator = EC_GROUP_get0_generator(group);
  518. if (generator == NULL) {
  519. ECerr(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
  520. goto err;
  521. }
  522. if (!ecp_sm2p256_bignum_field_elem(k, scalar)) {
  523. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  524. goto err;
  525. }
  526. #if !defined(OPENSSL_NO_SM2_PRECOMP)
  527. if (ecp_sm2p256_is_affine_G(generator)) {
  528. ecp_sm2p256_point_G_mul_by_scalar(&p.p, k);
  529. } else
  530. #endif
  531. {
  532. /* if no precomputed table */
  533. const EC_POINT *new_generator[1];
  534. const BIGNUM *g_scalars[1];
  535. new_generator[0] = generator;
  536. g_scalars[0] = scalar;
  537. if (!ecp_sm2p256_windowed_mul(group, &p.p, g_scalars, new_generator,
  538. (new_generator[0] != NULL
  539. && g_scalars[0] != NULL), ctx))
  540. goto err;
  541. }
  542. } else {
  543. p_is_infinity = 1;
  544. }
  545. if (num) {
  546. P256_POINT *out = &t.p;
  547. if (p_is_infinity)
  548. out = &p.p;
  549. if (!ecp_sm2p256_windowed_mul(group, out, scalars, points, num, ctx))
  550. goto err;
  551. if (!p_is_infinity)
  552. ecp_sm2p256_point_add(&p.p, &p.p, out);
  553. }
  554. /* Not constant-time, but we're only operating on the public output. */
  555. if (!bn_set_words(r->X, p.p.X, P256_LIMBS)
  556. || !bn_set_words(r->Y, p.p.Y, P256_LIMBS)
  557. || !bn_set_words(r->Z, p.p.Z, P256_LIMBS))
  558. goto err;
  559. r->Z_is_one = is_equal(bn_get_words(r->Z), ONE) & 1;
  560. ret = 1;
  561. err:
  562. BN_CTX_end(ctx);
  563. return ret;
  564. }
  565. static int ecp_sm2p256_field_mul(const EC_GROUP *group, BIGNUM *r,
  566. const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  567. {
  568. BN_ULONG a_fe[P256_LIMBS] ALIGN32 = {0};
  569. BN_ULONG b_fe[P256_LIMBS] ALIGN32 = {0};
  570. BN_ULONG r_fe[P256_LIMBS] ALIGN32 = {0};
  571. if (a == NULL || b == NULL || r == NULL)
  572. return 0;
  573. if (!ecp_sm2p256_bignum_field_elem(a_fe, a)
  574. || !ecp_sm2p256_bignum_field_elem(b_fe, b)) {
  575. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  576. return 0;
  577. }
  578. ecp_sm2p256_mul(r_fe, a_fe, b_fe);
  579. if (!bn_set_words(r, r_fe, P256_LIMBS))
  580. return 0;
  581. return 1;
  582. }
  583. static int ecp_sm2p256_field_sqr(const EC_GROUP *group, BIGNUM *r,
  584. const BIGNUM *a, BN_CTX *ctx)
  585. {
  586. BN_ULONG a_fe[P256_LIMBS] ALIGN32 = {0};
  587. BN_ULONG r_fe[P256_LIMBS] ALIGN32 = {0};
  588. if (a == NULL || r == NULL)
  589. return 0;
  590. if (!ecp_sm2p256_bignum_field_elem(a_fe, a)) {
  591. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  592. return 0;
  593. }
  594. ecp_sm2p256_sqr(r_fe, a_fe);
  595. if (!bn_set_words(r, r_fe, P256_LIMBS))
  596. return 0;
  597. return 1;
  598. }
  599. static int ecp_sm2p256_inv_mod_ord(const EC_GROUP *group, BIGNUM *r,
  600. const BIGNUM *x, BN_CTX *ctx)
  601. {
  602. int ret = 0;
  603. BN_ULONG t[P256_LIMBS] ALIGN32 = {0};
  604. BN_ULONG out[P256_LIMBS] ALIGN32 = {0};
  605. if (bn_wexpand(r, P256_LIMBS) == NULL) {
  606. ECerr(ERR_LIB_EC, ERR_R_BN_LIB);
  607. goto err;
  608. }
  609. if ((BN_num_bits(x) > 256) || BN_is_negative(x)) {
  610. BIGNUM *tmp;
  611. if ((tmp = BN_CTX_get(ctx)) == NULL
  612. || !BN_nnmod(tmp, x, group->order, ctx)) {
  613. ECerr(ERR_LIB_EC, ERR_R_BN_LIB);
  614. goto err;
  615. }
  616. x = tmp;
  617. }
  618. if (!ecp_sm2p256_bignum_field_elem(t, x)) {
  619. ECerr(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
  620. goto err;
  621. }
  622. ecp_sm2p256_mod_ord_inverse(out, t);
  623. if (!bn_set_words(r, out, P256_LIMBS))
  624. goto err;
  625. ret = 1;
  626. err:
  627. return ret;
  628. }
  629. const EC_METHOD *EC_GFp_sm2p256_method(void)
  630. {
  631. static const EC_METHOD ret = {
  632. EC_FLAGS_DEFAULT_OCT,
  633. NID_X9_62_prime_field,
  634. ossl_ec_GFp_simple_group_init,
  635. ossl_ec_GFp_simple_group_finish,
  636. ossl_ec_GFp_simple_group_clear_finish,
  637. ossl_ec_GFp_simple_group_copy,
  638. ossl_ec_GFp_simple_group_set_curve,
  639. ossl_ec_GFp_simple_group_get_curve,
  640. ossl_ec_GFp_simple_group_get_degree,
  641. ossl_ec_group_simple_order_bits,
  642. ossl_ec_GFp_simple_group_check_discriminant,
  643. ossl_ec_GFp_simple_point_init,
  644. ossl_ec_GFp_simple_point_finish,
  645. ossl_ec_GFp_simple_point_clear_finish,
  646. ossl_ec_GFp_simple_point_copy,
  647. ossl_ec_GFp_simple_point_set_to_infinity,
  648. ossl_ec_GFp_simple_point_set_affine_coordinates,
  649. ecp_sm2p256_get_affine,
  650. 0, 0, 0,
  651. ossl_ec_GFp_simple_add,
  652. ossl_ec_GFp_simple_dbl,
  653. ossl_ec_GFp_simple_invert,
  654. ossl_ec_GFp_simple_is_at_infinity,
  655. ossl_ec_GFp_simple_is_on_curve,
  656. ossl_ec_GFp_simple_cmp,
  657. ossl_ec_GFp_simple_make_affine,
  658. ossl_ec_GFp_simple_points_make_affine,
  659. ecp_sm2p256_points_mul, /* mul */
  660. 0 /* precompute_mult */,
  661. 0 /* have_precompute_mult */,
  662. ecp_sm2p256_field_mul,
  663. ecp_sm2p256_field_sqr,
  664. 0 /* field_div */,
  665. 0 /* field_inv */,
  666. 0 /* field_encode */,
  667. 0 /* field_decode */,
  668. 0 /* field_set_to_one */,
  669. ossl_ec_key_simple_priv2oct,
  670. ossl_ec_key_simple_oct2priv,
  671. 0, /* set private */
  672. ossl_ec_key_simple_generate_key,
  673. ossl_ec_key_simple_check_key,
  674. ossl_ec_key_simple_generate_public_key,
  675. 0, /* keycopy */
  676. 0, /* keyfinish */
  677. ossl_ecdh_simple_compute_key,
  678. ossl_ecdsa_simple_sign_setup,
  679. ossl_ecdsa_simple_sign_sig,
  680. ossl_ecdsa_simple_verify_sig,
  681. ecp_sm2p256_inv_mod_ord,
  682. 0, /* blind_coordinates */
  683. 0, /* ladder_pre */
  684. 0, /* ladder_step */
  685. 0 /* ladder_post */
  686. };
  687. return &ret;
  688. }