1
1
/*
2
- * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
2
+ * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.
3
3
*
4
4
* Licensed under the Apache License 2.0 (the "License"). You may not use
5
5
* this file except in compliance with the License. You can obtain a copy
@@ -56,10 +56,6 @@ ALIGN32 static const BN_ULONG def_p[P256_LIMBS] = {
56
56
0xffffffffffffffff , 0xffffffff00000000 ,
57
57
0xffffffffffffffff , 0xfffffffeffffffff
58
58
};
59
- ALIGN32 static const BN_ULONG def_ord [P256_LIMBS ] = {
60
- 0x53bbf40939d54123 , 0x7203df6b21c6052b ,
61
- 0xffffffffffffffff , 0xfffffffeffffffff
62
- };
63
59
64
60
ALIGN32 static const BN_ULONG ONE [P256_LIMBS ] = {1 , 0 , 0 , 0 };
65
61
@@ -177,13 +173,6 @@ static ossl_inline void ecp_sm2p256_mod_inverse(BN_ULONG* out,
177
173
BN_MOD_INV (out , in , ecp_sm2p256_div_by_2 , ecp_sm2p256_sub , def_p );
178
174
}
179
175
180
- /* Modular inverse mod order |out| = |in|^(-1) % |ord|. */
181
- static ossl_inline void ecp_sm2p256_mod_ord_inverse (BN_ULONG * out ,
182
- const BN_ULONG * in ) {
183
- BN_MOD_INV (out , in , ecp_sm2p256_div_by_2_mod_ord , ecp_sm2p256_sub_mod_ord ,
184
- def_ord );
185
- }
186
-
187
176
/* Point double: R <- P + P */
188
177
static void ecp_sm2p256_point_double (P256_POINT * R , const P256_POINT * P )
189
178
{
@@ -454,52 +443,6 @@ static int ecp_sm2p256_is_affine_G(const EC_POINT *generator)
454
443
}
455
444
#endif
456
445
457
- /*
458
- * Convert Jacobian coordinate point into affine coordinate (x,y)
459
- */
460
- static int ecp_sm2p256_get_affine (const EC_GROUP * group ,
461
- const EC_POINT * point ,
462
- BIGNUM * x , BIGNUM * y , BN_CTX * ctx )
463
- {
464
- ALIGN32 BN_ULONG z_inv2 [P256_LIMBS ] = {0 };
465
- ALIGN32 BN_ULONG z_inv3 [P256_LIMBS ] = {0 };
466
- ALIGN32 BN_ULONG x_aff [P256_LIMBS ] = {0 };
467
- ALIGN32 BN_ULONG y_aff [P256_LIMBS ] = {0 };
468
- ALIGN32 BN_ULONG point_x [P256_LIMBS ] = {0 };
469
- ALIGN32 BN_ULONG point_y [P256_LIMBS ] = {0 };
470
- ALIGN32 BN_ULONG point_z [P256_LIMBS ] = {0 };
471
-
472
- if (EC_POINT_is_at_infinity (group , point )) {
473
- ECerr (ERR_LIB_EC , EC_R_POINT_AT_INFINITY );
474
- return 0 ;
475
- }
476
-
477
- if (ecp_sm2p256_bignum_field_elem (point_x , point -> X ) <= 0
478
- || ecp_sm2p256_bignum_field_elem (point_y , point -> Y ) <= 0
479
- || ecp_sm2p256_bignum_field_elem (point_z , point -> Z ) <= 0 ) {
480
- ECerr (ERR_LIB_EC , EC_R_COORDINATES_OUT_OF_RANGE );
481
- return 0 ;
482
- }
483
-
484
- ecp_sm2p256_mod_inverse (z_inv3 , point_z );
485
- ecp_sm2p256_sqr (z_inv2 , z_inv3 );
486
-
487
- if (x != NULL ) {
488
- ecp_sm2p256_mul (x_aff , point_x , z_inv2 );
489
- if (!bn_set_words (x , x_aff , P256_LIMBS ))
490
- return 0 ;
491
- }
492
-
493
- if (y != NULL ) {
494
- ecp_sm2p256_mul (z_inv3 , z_inv3 , z_inv2 );
495
- ecp_sm2p256_mul (y_aff , point_y , z_inv3 );
496
- if (!bn_set_words (y , y_aff , P256_LIMBS ))
497
- return 0 ;
498
- }
499
-
500
- return 1 ;
501
- }
502
-
503
446
/* r = sum(scalar[i]*point[i]) */
504
447
static int ecp_sm2p256_windowed_mul (const EC_GROUP * group ,
505
448
P256_POINT * r ,
@@ -689,44 +632,6 @@ static int ecp_sm2p256_field_sqr(const EC_GROUP *group, BIGNUM *r,
689
632
return 1 ;
690
633
}
691
634
692
- static int ecp_sm2p256_inv_mod_ord (const EC_GROUP * group , BIGNUM * r ,
693
- const BIGNUM * x , BN_CTX * ctx )
694
- {
695
- int ret = 0 ;
696
- ALIGN32 BN_ULONG t [P256_LIMBS ] = {0 };
697
- ALIGN32 BN_ULONG out [P256_LIMBS ] = {0 };
698
-
699
- if (bn_wexpand (r , P256_LIMBS ) == NULL ) {
700
- ECerr (ERR_LIB_EC , ERR_R_BN_LIB );
701
- goto err ;
702
- }
703
-
704
- if ((BN_num_bits (x ) > 256 ) || BN_is_negative (x )) {
705
- BIGNUM * tmp ;
706
-
707
- if ((tmp = BN_CTX_get (ctx )) == NULL
708
- || !BN_nnmod (tmp , x , group -> order , ctx )) {
709
- ECerr (ERR_LIB_EC , ERR_R_BN_LIB );
710
- goto err ;
711
- }
712
- x = tmp ;
713
- }
714
-
715
- if (!ecp_sm2p256_bignum_field_elem (t , x )) {
716
- ECerr (ERR_LIB_EC , EC_R_COORDINATES_OUT_OF_RANGE );
717
- goto err ;
718
- }
719
-
720
- ecp_sm2p256_mod_ord_inverse (out , t );
721
-
722
- if (!bn_set_words (r , out , P256_LIMBS ))
723
- goto err ;
724
-
725
- ret = 1 ;
726
- err :
727
- return ret ;
728
- }
729
-
730
635
const EC_METHOD * EC_GFp_sm2p256_method (void )
731
636
{
732
637
static const EC_METHOD ret = {
@@ -747,7 +652,7 @@ const EC_METHOD *EC_GFp_sm2p256_method(void)
747
652
ossl_ec_GFp_simple_point_copy ,
748
653
ossl_ec_GFp_simple_point_set_to_infinity ,
749
654
ossl_ec_GFp_simple_point_set_affine_coordinates ,
750
- ecp_sm2p256_get_affine ,
655
+ ossl_ec_GFp_simple_point_get_affine_coordinates ,
751
656
0 , 0 , 0 ,
752
657
ossl_ec_GFp_simple_add ,
753
658
ossl_ec_GFp_simple_dbl ,
@@ -763,7 +668,7 @@ const EC_METHOD *EC_GFp_sm2p256_method(void)
763
668
ecp_sm2p256_field_mul ,
764
669
ecp_sm2p256_field_sqr ,
765
670
0 /* field_div */ ,
766
- 0 /* field_inv */ ,
671
+ ossl_ec_GFp_simple_field_inv ,
767
672
0 /* field_encode */ ,
768
673
0 /* field_decode */ ,
769
674
0 /* field_set_to_one */ ,
@@ -779,7 +684,7 @@ const EC_METHOD *EC_GFp_sm2p256_method(void)
779
684
ossl_ecdsa_simple_sign_setup ,
780
685
ossl_ecdsa_simple_sign_sig ,
781
686
ossl_ecdsa_simple_verify_sig ,
782
- ecp_sm2p256_inv_mod_ord ,
687
+ 0 , /* use constant‑time fallback for inverse mod order */
783
688
0 , /* blind_coordinates */
784
689
0 , /* ladder_pre */
785
690
0 , /* ladder_step */
0 commit comments