Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/ec/ecp_sm2p256.c

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