Skip to content

Commit

Permalink
ECC SM2 import private key: check less than order-1
Browse files Browse the repository at this point in the history
SM2 curves must have private key less than order-1 instead of order.
  • Loading branch information
SparkiDev committed Oct 19, 2023
1 parent a3bf7a6 commit abd7bb3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions wolfcrypt/src/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10883,6 +10883,13 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
if (ret == 0) {
ret = mp_read_radix(order, key->dp->order, MP_RADIX_HEX);
}
#ifdef WOLFSSL_SM2
/* SM2 curve: private key must be less than order-1. */
if ((ret == 0) && (key->idx != ECC_CUSTOM_IDX) &&
(ecc_sets[key->idx].id == ECC_SM2P256V1)) {
ret = mp_sub_d(order, 1, order);
}
#endif
if ((ret == 0) && (mp_cmp(key->k, order) != MP_LT)) {
ret = ECC_PRIV_KEY_E;
}
Expand Down

0 comments on commit abd7bb3

Please sign in to comment.