Skip to content

SM2#733

Open
karel-m wants to merge 2 commits into
developfrom
pr/sm2
Open

SM2#733
karel-m wants to merge 2 commits into
developfrom
pr/sm2

Conversation

@karel-m
Copy link
Copy Markdown
Member

@karel-m karel-m commented Apr 21, 2026

Added SM2 (Chinese public key algorithm) support.

It depends on the not-yet-merged SM3 hash support.

SM2 uses the existing ECC curve code where possible (it is just another curve). However, its signing/encryption work differently enough, so this patch adds separate SM2-specific functions.

Checklist

  • documentation is added or updated
  • tests are added or updated

@karel-m karel-m requested a review from sjaeckel April 21, 2026 19:00
@sjaeckel sjaeckel changed the base branch from develop to pr/sm3-hash May 19, 2026 11:19
Base automatically changed from pr/sm3-hash to develop May 19, 2026 11:31
Comment thread src/pk/ecc/ecc_sm2.c
LTC_ARGCHK(key != NULL);

if ((err = s_sm2_only_curve(key)) != CRYPT_OK) return err;
if ((hash_idx = s_sm2_hash_idx(hash_idx)) < 0) return hash_idx;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error check won't work.

I'd say we can remove the hash_idx in the API. This also applies to ecc_decrypt_key_sm2().

Comment thread src/headers/tomcrypt_pk.h
Comment on lines +474 to +484
int ecc_sign_sm2(const unsigned char *id, unsigned long idlen,
const unsigned char *msg, unsigned long msglen,
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, int hash_idx,
const ecc_key *key);

int ecc_verify_sm2(const unsigned char *id, unsigned long idlen,
const unsigned char *msg, unsigned long msglen,
const unsigned char *sig, unsigned long siglen,
int hash_idx, int *stat, const ecc_key *key);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it maybe make sense to introduce a new generic API?

Add LTC_ECCSIG_SM2 to ecc_signature_type and hash_idx to ltc_ecc_sig_opts and then have the following:

int ecc_sign_data(const unsigned char    *data,
                           unsigned long     datalen,
                           unsigned char    *sig,
                           unsigned long    *siglen,
                           ltc_ecc_sig_opts *opts,
                     const       ecc_key    *key);

int ecc_verify_data(const unsigned char *sig,
                             unsigned long  siglen,
                       const unsigned char *data,
                             unsigned long  datalen,
                          ltc_ecc_sig_opts *opts,
                                       int *stat,
                       const       ecc_key *key);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants