From be33e69b227d1e647067fde1f95f4eee281fd386 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 14 Aug 2018 12:17:20 -0600 Subject: [PATCH] Refactor to rename `wc_ecc_export_int` to `wc_export_int` for generic API for exporting an `mp_int` as hex string or unsigned bin. --- wolfcrypt/src/ecc.c | 53 +++++++++--------------------------- wolfcrypt/src/wolfmath.c | 30 ++++++++++++++++++++ wolfssl/wolfcrypt/ecc.h | 7 ----- wolfssl/wolfcrypt/wolfmath.h | 8 ++++++ 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index c0c32ea2c..380cc6856 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -6400,35 +6400,8 @@ int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key) #ifdef HAVE_ECC_KEY_EXPORT -/* export an mp_int as unsigned char or hex string - * encType is ECC_TYPE_UNSIGNED_BIN or ECC_TYPE_HEX_STR - * return MP_OKAY on success */ -int wc_ecc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz, - int encType) -{ - int err; - - /* check buffer size */ - if (*len < keySz) { - *len = keySz; - return BUFFER_E; - } - - *len = keySz; - XMEMSET(buf, 0, *len); - - if (encType == ECC_TYPE_HEX_STR) { - err = mp_tohex(mp, (char*)buf); - } - else { - err = mp_to_unsigned_bin(mp, buf + (keySz - mp_unsigned_bin_size(mp))); - } - - return err; -} - /* export ecc key to component form, d is optional if only exporting public - * encType is ECC_TYPE_UNSIGNED_BIN or ECC_TYPE_HEX_STR + * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR * return MP_OKAY on success */ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen, byte* qy, word32* qyLen, byte* d, word32* dLen, int encType) @@ -6454,7 +6427,7 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen, /* Hardware cannot export private portion */ return BAD_COND_E; #else - err = wc_ecc_export_int(&key->k, d, dLen, keySz, encType); + err = wc_export_int(&key->k, d, dLen, keySz, encType); if (err != MP_OKAY) return err; #endif @@ -6465,7 +6438,7 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen, if (qxLen == NULL || key->type == ECC_PRIVATEKEY_ONLY) return BAD_FUNC_ARG; - err = wc_ecc_export_int(key->pubkey.x, qx, qxLen, keySz, encType); + err = wc_export_int(key->pubkey.x, qx, qxLen, keySz, encType); if (err != MP_OKAY) return err; } @@ -6475,7 +6448,7 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen, if (qyLen == NULL || key->type == ECC_PRIVATEKEY_ONLY) return BAD_FUNC_ARG; - err = wc_ecc_export_int(key->pubkey.y, qy, qyLen, keySz, encType); + err = wc_export_int(key->pubkey.y, qy, qyLen, keySz, encType); if (err != MP_OKAY) return err; } @@ -6493,7 +6466,7 @@ int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen) } return wc_ecc_export_ex(key, NULL, NULL, NULL, NULL, out, outLen, - ECC_TYPE_UNSIGNED_BIN); + WC_TYPE_UNSIGNED_BIN); } /* export public key to raw elements including public (Qx,Qy) as unsigned bin @@ -6506,7 +6479,7 @@ int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen, } return wc_ecc_export_ex(key, qx, qxLen, qy, qyLen, NULL, NULL, - ECC_TYPE_UNSIGNED_BIN); + WC_TYPE_UNSIGNED_BIN); } /* export ecc key to raw elements including public (Qx,Qy) and @@ -6516,7 +6489,7 @@ int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen, byte* qy, word32* qyLen, byte* d, word32* dLen) { return wc_ecc_export_ex(key, qx, qxLen, qy, qyLen, d, dLen, - ECC_TYPE_UNSIGNED_BIN); + WC_TYPE_UNSIGNED_BIN); } #endif /* HAVE_ECC_KEY_EXPORT */ @@ -6834,7 +6807,7 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, /* read Qx */ if (err == MP_OKAY) { - if (encType == ECC_TYPE_HEX_STR) + if (encType == WC_TYPE_HEX_STR) err = mp_read_radix(key->pubkey.x, qx, MP_RADIX_HEX); else err = mp_read_unsigned_bin(key->pubkey.x, (const byte*)qx, @@ -6843,7 +6816,7 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, /* read Qy */ if (err == MP_OKAY) { - if (encType == ECC_TYPE_HEX_STR) + if (encType == WC_TYPE_HEX_STR) err = mp_read_radix(key->pubkey.y, qy, MP_RADIX_HEX); else err = mp_read_unsigned_bin(key->pubkey.y, (const byte*)qy, @@ -6859,7 +6832,7 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, if (d != NULL) { key->type = ECC_PRIVATEKEY; - if (encType == ECC_TYPE_HEX_STR) + if (encType == WC_TYPE_HEX_STR) err = mp_read_radix(&key->k, d, MP_RADIX_HEX); else err = mp_read_unsigned_bin(&key->k, (const byte*)d, @@ -6900,7 +6873,7 @@ int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy, const char* d, int curve_id) { return wc_ecc_import_raw_private(key, qx, qy, d, curve_id, - ECC_TYPE_HEX_STR); + WC_TYPE_HEX_STR); } @@ -6909,7 +6882,7 @@ int wc_ecc_import_unsigned(ecc_key* key, byte* qx, byte* qy, byte* d, int curve_id) { return wc_ecc_import_raw_private(key, (const char*)qx, (const char*)qy, - (const char*)d, curve_id, ECC_TYPE_UNSIGNED_BIN); + (const char*)d, curve_id, WC_TYPE_UNSIGNED_BIN); } /** @@ -6945,7 +6918,7 @@ int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy, err = ASN_PARSE_E; } else { return wc_ecc_import_raw_private(key, qx, qy, d, ecc_sets[x].id, - ECC_TYPE_HEX_STR); + WC_TYPE_HEX_STR); } return err; diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c index 0c81427d4..5836f011a 100644 --- a/wolfcrypt/src/wolfmath.c +++ b/wolfcrypt/src/wolfmath.c @@ -150,6 +150,36 @@ exit: } #endif /* WC_RSA_BLINDING */ +/* export an mp_int as unsigned char or hex string + * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR + * return MP_OKAY on success */ +int wc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz, + int encType) +{ + int err; + + if (mp == NULL) + return BAD_FUNC_ARG; + + /* check buffer size */ + if (*len < keySz) { + *len = keySz; + return BUFFER_E; + } + + *len = keySz; + XMEMSET(buf, 0, *len); + + if (encType == WC_TYPE_HEX_STR) { + err = mp_tohex(mp, (char*)buf); + } + else { + err = mp_to_unsigned_bin(mp, buf + (keySz - mp_unsigned_bin_size(mp))); + } + + return err; +} + #ifdef HAVE_WOLF_BIGINT void wc_bigint_init(WC_BIGINT* a) diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 4d4c3fd63..3a3105e7f 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -127,10 +127,6 @@ enum { ECC_MAX_CRYPTO_HW_SIZE = 32, #endif - /* point encoding type */ - ECC_TYPE_HEX_STR = 1, - ECC_TYPE_UNSIGNED_BIN = 2, - /* point compression type */ ECC_POINT_COMP_EVEN = 0x02, ECC_POINT_COMP_ODD = 0x03, @@ -559,9 +555,6 @@ int wc_ecc_import_unsigned(ecc_key* key, byte* qx, byte* qy, #endif /* HAVE_ECC_KEY_IMPORT */ #ifdef HAVE_ECC_KEY_EXPORT -WOLFSSL_API -int wc_ecc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz, - int encType); WOLFSSL_API int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen, byte* qy, word32* qyLen, byte* d, word32* dLen, diff --git a/wolfssl/wolfcrypt/wolfmath.h b/wolfssl/wolfcrypt/wolfmath.h index 8d0ab6d81..fbc3f1266 100644 --- a/wolfssl/wolfcrypt/wolfmath.h +++ b/wolfssl/wolfcrypt/wolfmath.h @@ -51,6 +51,14 @@ int get_rand_digit(WC_RNG* rng, mp_digit* d); int mp_rand(mp_int* a, int digits, WC_RNG* rng); + enum { + /* format type */ + WC_TYPE_HEX_STR = 1, + WC_TYPE_UNSIGNED_BIN = 2, + }; + + WOLFSSL_API int wc_export_int(mp_int* mp, byte* buf, word32* len, + word32 keySz, int encType); #ifdef HAVE_WOLF_BIGINT void wc_bigint_init(WC_BIGINT* a);