Skip to content

Commit 2827a61

Browse files
committed
Add SM2 support
1 parent 4e28919 commit 2827a61

12 files changed

Lines changed: 1234 additions & 30 deletions

src/wh_client_crypto.c

Lines changed: 423 additions & 25 deletions
Large diffs are not rendered by default.

src/wh_client_cryptocb.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,68 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx)
667667

668668
#endif /* WOLFSSL_HAVE_LMS || WOLFSSL_HAVE_XMSS */
669669

670+
#if defined(WOLFSSL_SM2) && defined(HAVE_ECC)
671+
case WC_PK_TYPE_SM2_SIGN: {
672+
ecc_key* key = info->pk.sm2sign.key;
673+
const byte* in = info->pk.sm2sign.in;
674+
word32 in_len = info->pk.sm2sign.inlen;
675+
byte* out = info->pk.sm2sign.out;
676+
word32* out_len = info->pk.sm2sign.outlen;
677+
uint16_t sig_len = 0;
678+
679+
/* Convert to a uint16_t local to support big endian. */
680+
if (out_len != NULL) {
681+
sig_len = (uint16_t)(*out_len);
682+
}
683+
684+
if (in_len > UINT16_MAX) {
685+
ret = WH_ERROR_BADARGS;
686+
}
687+
else {
688+
ret = wh_Client_Sm2Sign(ctx, key, in, (uint16_t)in_len, out,
689+
&sig_len);
690+
/* Propagate the required length on BUFFER_SIZE so the caller
691+
* can retry with a large enough buffer. */
692+
if (((ret == WH_ERROR_OK) || (ret == WH_ERROR_BUFFER_SIZE)) &&
693+
(out_len != NULL)) {
694+
*out_len = sig_len;
695+
}
696+
}
697+
} break;
698+
699+
case WC_PK_TYPE_SM2_VERIFY: {
700+
word32 sig_len = info->pk.sm2verify.siglen;
701+
word32 hash_len = info->pk.sm2verify.hashlen;
702+
703+
if ((sig_len > UINT16_MAX) || (hash_len > UINT16_MAX)) {
704+
ret = WH_ERROR_BADARGS;
705+
}
706+
else {
707+
ret = wh_Client_Sm2Verify(
708+
ctx, info->pk.sm2verify.key, info->pk.sm2verify.sig,
709+
(uint16_t)sig_len, info->pk.sm2verify.hash,
710+
(uint16_t)hash_len, info->pk.sm2verify.res);
711+
}
712+
} break;
713+
714+
case WC_PK_TYPE_SM2_SHARED_SECRET: {
715+
word32* out_len = info->pk.sm2dh.outlen;
716+
uint16_t sec_len = 0;
717+
718+
if (out_len != NULL) {
719+
sec_len = (uint16_t)(*out_len);
720+
}
721+
722+
ret = wh_Client_Sm2SharedSecret(ctx, info->pk.sm2dh.private_key,
723+
info->pk.sm2dh.public_key,
724+
info->pk.sm2dh.out, &sec_len);
725+
if (((ret == WH_ERROR_OK) || (ret == WH_ERROR_BUFFER_SIZE)) &&
726+
(out_len != NULL)) {
727+
*out_len = sec_len;
728+
}
729+
} break;
730+
#endif /* WOLFSSL_SM2 && HAVE_ECC */
731+
670732
#if defined(WOLFSSL_HAVE_MLDSA) || defined(HAVE_FALCON)
671733
case WC_PK_TYPE_PQC_SIG_KEYGEN:
672734
ret = _handlePqcSigKeyGen(ctx, info, 0);

src/wh_message_crypto.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,85 @@ int wh_MessageCrypto_TranslateAesGcmResponse(
190190
WH_T32(magic, dest, src, authTagSz);
191191
return 0;
192192
}
193+
/* SM2 Sign Request translation */
194+
int wh_MessageCrypto_TranslateSm2SignRequest(
195+
uint16_t magic, const whMessageCrypto_Sm2SignRequest* src,
196+
whMessageCrypto_Sm2SignRequest* dest)
197+
{
198+
if ((src == NULL) || (dest == NULL)) {
199+
return WH_ERROR_BADARGS;
200+
}
201+
WH_T32(magic, dest, src, options);
202+
WH_T32(magic, dest, src, keyId);
203+
WH_T32(magic, dest, src, sz);
204+
return 0;
205+
}
206+
207+
/* SM2 Sign Response translation */
208+
int wh_MessageCrypto_TranslateSm2SignResponse(
209+
uint16_t magic, const whMessageCrypto_Sm2SignResponse* src,
210+
whMessageCrypto_Sm2SignResponse* dest)
211+
{
212+
if ((src == NULL) || (dest == NULL)) {
213+
return WH_ERROR_BADARGS;
214+
}
215+
WH_T32(magic, dest, src, sz);
216+
return 0;
217+
}
218+
219+
/* SM2 Verify Request translation */
220+
int wh_MessageCrypto_TranslateSm2VerifyRequest(
221+
uint16_t magic, const whMessageCrypto_Sm2VerifyRequest* src,
222+
whMessageCrypto_Sm2VerifyRequest* dest)
223+
{
224+
if ((src == NULL) || (dest == NULL)) {
225+
return WH_ERROR_BADARGS;
226+
}
227+
WH_T32(magic, dest, src, options);
228+
WH_T32(magic, dest, src, keyId);
229+
WH_T32(magic, dest, src, sigSz);
230+
WH_T32(magic, dest, src, hashSz);
231+
return 0;
232+
}
233+
234+
/* SM2 Verify Response translation */
235+
int wh_MessageCrypto_TranslateSm2VerifyResponse(
236+
uint16_t magic, const whMessageCrypto_Sm2VerifyResponse* src,
237+
whMessageCrypto_Sm2VerifyResponse* dest)
238+
{
239+
if ((src == NULL) || (dest == NULL)) {
240+
return WH_ERROR_BADARGS;
241+
}
242+
WH_T32(magic, dest, src, res);
243+
return 0;
244+
}
245+
246+
/* SM2 Shared Secret Request translation */
247+
int wh_MessageCrypto_TranslateSm2DhRequest(
248+
uint16_t magic, const whMessageCrypto_Sm2DhRequest* src,
249+
whMessageCrypto_Sm2DhRequest* dest)
250+
{
251+
if ((src == NULL) || (dest == NULL)) {
252+
return WH_ERROR_BADARGS;
253+
}
254+
WH_T32(magic, dest, src, options);
255+
WH_T32(magic, dest, src, privateKeyId);
256+
WH_T32(magic, dest, src, publicKeyId);
257+
return 0;
258+
}
259+
260+
/* SM2 Shared Secret Response translation */
261+
int wh_MessageCrypto_TranslateSm2DhResponse(
262+
uint16_t magic, const whMessageCrypto_Sm2DhResponse* src,
263+
whMessageCrypto_Sm2DhResponse* dest)
264+
{
265+
if ((src == NULL) || (dest == NULL)) {
266+
return WH_ERROR_BADARGS;
267+
}
268+
WH_T32(magic, dest, src, sz);
269+
return 0;
270+
}
271+
193272
/* SM4 ECB Request translation */
194273
int wh_MessageCrypto_TranslateSm4EcbRequest(
195274
uint16_t magic, const whMessageCrypto_Sm4EcbRequest* src,

0 commit comments

Comments
 (0)