diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index f0ff1b626..4e45132f7 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -5580,6 +5580,48 @@ int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen) return err; } +/** + Convert ECC R,S raw unsigned bin to signature + r R component of signature + rSz R size + s S component of signature + sSz S size + out DER-encoded ECDSA signature + outlen [in/out] output buffer size, output signature size + return MP_OKAY on success +*/ +int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz, + byte* out, word32* outlen) +{ + int err; + mp_int rtmp; + mp_int stmp; + + if (r == NULL || s == NULL || out == NULL || outlen == NULL) + return ECC_BAD_ARG_E; + + err = mp_init_multi(&rtmp, &stmp, NULL, NULL, NULL, NULL); + if (err != MP_OKAY) + return err; + + err = mp_read_unsigned_bin(&rtmp, r, rSz); + if (err == MP_OKAY) + err = mp_read_unsigned_bin(&stmp, s, sSz); + + /* convert mp_ints to ECDSA sig, initializes rtmp and stmp internally */ + if (err == MP_OKAY) + err = StoreECC_DSA_Sig(out, outlen, &rtmp, &stmp); + + if (err == MP_OKAY) { + if (mp_iszero(&rtmp) == MP_YES || mp_iszero(&stmp) == MP_YES) + err = MP_ZERO_E; + } + + mp_clear(&rtmp); + mp_clear(&stmp); + + return err; +} /** Convert ECC signature to R,S diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index b8033fc68..9def6519c 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -487,6 +487,9 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, WOLFSSL_API int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen); WOLFSSL_API +int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz, + byte* out, word32* outlen); +WOLFSSL_API int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen, byte* s, word32* sLen); WOLFSSL_API