Skip to content

Commit 21f662c

Browse files
committed
ASN template: StoreECC_DSA_Sig_Bin
Strip leading zeros from R and S before encoding in ASN.1.
1 parent 6125e59 commit 21f662c

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32062,6 +32062,14 @@ int StoreECC_DSA_Sig_Bin(byte* out, word32* outLen, const byte* r, word32 rLen,
3206232062

3206332063
/* Clear dynamic data and set buffers for r and s */
3206432064
XMEMSET(dataASN, 0, sizeof(dataASN));
32065+
while ((rLen > 1) && (r[0] == 0)) {
32066+
rLen--;
32067+
r++;
32068+
}
32069+
while ((sLen > 1) && (s[0] == 0)) {
32070+
sLen--;
32071+
s++;
32072+
}
3206532073
SetASN_Buffer(&dataASN[DSASIGASN_IDX_R], r, rLen);
3206632074
SetASN_Buffer(&dataASN[DSASIGASN_IDX_S], s, sLen);
3206732075

wolfcrypt/test/test.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29898,6 +29898,58 @@ static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
2989829898
}
2989929899
#endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
2990029900

29901+
#if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
29902+
(!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
29903+
(HAVE_FIPS_VERSION > 2)))
29904+
static int ecc_test_raw_enc_dec(void)
29905+
{
29906+
int ret;
29907+
unsigned char r[1];
29908+
word32 rSz;
29909+
unsigned char s[1];
29910+
word32 sSz;
29911+
unsigned char rZero[] = { 0, 0, 0, 0 };
29912+
unsigned char sOne[] = { 0, 0, 1 };
29913+
unsigned char sigRaw[32];
29914+
word32 sigRawSz;
29915+
unsigned char expSig[] = { 0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01 };
29916+
29917+
sigRawSz = sizeof(sigRaw);
29918+
ret = wc_ecc_rs_raw_to_sig(rZero, sizeof(rZero), sOne, sizeof(sOne),
29919+
sigRaw, &sigRawSz);
29920+
if (ret != 0) {
29921+
return WC_TEST_RET_ENC_EC(ret);
29922+
}
29923+
if (sigRawSz != sizeof(expSig)) {
29924+
return WC_TEST_RET_ENC_EC((int)sigRawSz);
29925+
}
29926+
if (XMEMCMP(sigRaw, expSig, sizeof(expSig)) != 0) {
29927+
return WC_TEST_RET_ENC_NC;
29928+
}
29929+
29930+
rSz = sizeof(r);
29931+
sSz = sizeof(s);
29932+
ret = wc_ecc_sig_to_rs(sigRaw, sigRawSz, r, &rSz, s, &sSz);
29933+
if (ret != 0) {
29934+
return WC_TEST_RET_ENC_EC(ret);
29935+
}
29936+
if (rSz != 1) {
29937+
return WC_TEST_RET_ENC_EC((int)rSz);
29938+
}
29939+
if (sSz != 1) {
29940+
return WC_TEST_RET_ENC_EC((int)sSz);
29941+
}
29942+
if (r[0] != 0) {
29943+
return WC_TEST_RET_ENC_EC(r[0]);
29944+
}
29945+
if (s[0] != 1) {
29946+
return WC_TEST_RET_ENC_EC(s[0]);
29947+
}
29948+
29949+
return ret;
29950+
}
29951+
#endif
29952+
2990129953
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
2990229954
{
2990329955
wc_test_ret_t ret;
@@ -30022,6 +30074,16 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
3002230074
}
3002330075
#endif
3002430076

30077+
#if !defined(NO_ASN) && !defined(HAVE_SELFTEST) && \
30078+
(!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
30079+
(HAVE_FIPS_VERSION > 2)))
30080+
ret = ecc_test_raw_enc_dec();
30081+
if (ret != 0) {
30082+
printf("raw sig encode/decode\n");
30083+
goto done;
30084+
}
30085+
#endif
30086+
3002530087
#if defined(WOLFSSL_CUSTOM_CURVES)
3002630088
ret = ecc_test_custom_curves(&rng);
3002730089
if (ret != 0) {

0 commit comments

Comments
 (0)