diff --git a/src/ssl.c b/src/ssl.c index 11401973d..9e679f8ed 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -33495,19 +33495,7 @@ void *wolfSSL_OPENSSL_malloc(size_t a) int wolfSSL_OPENSSL_hexchar2int(unsigned char c) { - int ret = -1; - - if ('0' <= c && c <= '9') { - ret = c - '0'; - } - else if ('a' <= c && c <= 'f') { - ret = c - 'a' + 0x0a; - } - else if ('A' <= c && c <= 'F') { - ret = c - 'A' + 0x0a; - } - - return ret; + return (int)HexCharToByte((char)c); } unsigned char *wolfSSL_OPENSSL_hexstr2buf(const char *str, long *len) @@ -50956,9 +50944,6 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str, /* dump hex */ if (flags & ASN1_STRFLGS_DUMP_ALL){ - static const char hex_char[] = { '0', '1', '2', '3', '4', '5', '6', - '7','8', '9', 'A', 'B', 'C', 'D', - 'E', 'F' }; char hex_tmp[4]; char *str_ptr, *str_end; @@ -50974,10 +50959,8 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str, } str_len++; if (flags & ASN1_STRFLGS_DUMP_DER){ - hex_tmp[0] = hex_char[(str->type & 0xf0) >> 4]; - hex_tmp[1] = hex_char[(str->type & 0x0f)]; - hex_tmp[2] = hex_char[(str->length & 0xf0) >> 4]; - hex_tmp[3] = hex_char[(str->length & 0x0f)]; + ByteToHexStr((byte)str->type, &hex_tmp[0]); + ByteToHexStr((byte)str->length, &hex_tmp[2]); if (wolfSSL_BIO_write(out, hex_tmp, 4) != 4){ goto err_exit; } @@ -50988,8 +50971,7 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str, str_ptr = str->data; str_end = str->data + str->length; while (str_ptr < str_end){ - hex_tmp[0] = hex_char[*str_ptr >> 4]; - hex_tmp[1] = hex_char[*str_ptr & 0xf]; + ByteToHexStr((byte)*str_ptr, &hex_tmp[0]); if (wolfSSL_BIO_write(out, hex_tmp, 2) != 2){ goto err_exit; } diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index b5a8e6117..0836c391c 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -12162,7 +12162,7 @@ static mp_int* GetRsaInt(RsaKey* key, int idx) int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen) { int ret = 0, i, j, outLen = 0, mpSz; - word32 seqSz = 0, verSz, rawLen, intTotalLen = 0; + word32 seqSz = 0, verSz = 0, rawLen, intTotalLen = 0; word32 sizes[RSA_INTS]; byte seq[MAX_SEQ_SZ]; byte ver[MAX_VERSION_SZ]; @@ -16487,15 +16487,6 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, #ifdef WOLFSSL_CUSTOM_CURVES -static void ByteToHex(byte n, char* str) -{ - const char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - - str[0] = hexChar[n >> 4]; - str[1] = hexChar[n & 0xf]; -} - /* returns 0 on success */ static int ASNToHexString(const byte* input, word32* inOutIdx, char** out, word32 inSz, void* heap, int heapType) @@ -16526,7 +16517,7 @@ static int ASNToHexString(const byte* input, word32* inOutIdx, char** out, } for (i=0; i= '0' && ch <= '9') + ch -= '0'; + else if (ch >= 'A' && ch <= 'F') + ch -= 'A' - 10; + else if (ch >= 'a' && ch <= 'f') + ch -= 'a' - 10; + else + ch = -1; /* error case - return code must be signed */ + return ch; +} + +WC_STATIC WC_INLINE char ByteToHex(byte in) +{ + static const char kHexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + return (char)(kHexChar[in & 0xF]); +} + +WC_STATIC WC_INLINE int ByteToHexStr(byte in, char* out) +{ + if (out == NULL) + return -1; + + out[0] = ByteToHex(in >> 4); + out[1] = ByteToHex(in & 0xf); + return 0; +} #ifndef WOLFSSL_NO_CT_OPS /* Constant time - mask set when a > b. */ @@ -516,7 +545,6 @@ WC_STATIC WC_INLINE byte ctSetLTE(int a, int b) } #endif - #undef WC_STATIC #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */ diff --git a/wolfcrypt/src/sp_arm32.c b/wolfcrypt/src/sp_arm32.c index fe6b5aa95..bb866cd6a 100644 --- a/wolfcrypt/src/sp_arm32.c +++ b/wolfcrypt/src/sp_arm32.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef WOLFSSL_SP_SMALL #define WOLFSSL_SP_SMALL diff --git a/wolfcrypt/src/sp_arm64.c b/wolfcrypt/src/sp_arm64.c index 1e321fdde..57b9a25f7 100644 --- a/wolfcrypt/src/sp_arm64.c +++ b/wolfcrypt/src/sp_arm64.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef WOLFSSL_SP_SMALL #define WOLFSSL_SP_SMALL diff --git a/wolfcrypt/src/sp_armthumb.c b/wolfcrypt/src/sp_armthumb.c index cecf564b0..cde9ded9f 100644 --- a/wolfcrypt/src/sp_armthumb.c +++ b/wolfcrypt/src/sp_armthumb.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef WOLFSSL_SP_SMALL #define WOLFSSL_SP_SMALL diff --git a/wolfcrypt/src/sp_c32.c b/wolfcrypt/src/sp_c32.c index 47a836c13..af9ccc0e1 100644 --- a/wolfcrypt/src/sp_c32.c +++ b/wolfcrypt/src/sp_c32.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef SP_RSA_PRIVATE_EXP_D #define SP_RSA_PRIVATE_EXP_D diff --git a/wolfcrypt/src/sp_c64.c b/wolfcrypt/src/sp_c64.c index 28e06c76f..63f481b17 100644 --- a/wolfcrypt/src/sp_c64.c +++ b/wolfcrypt/src/sp_c64.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef SP_RSA_PRIVATE_EXP_D #define SP_RSA_PRIVATE_EXP_D diff --git a/wolfcrypt/src/sp_cortexm.c b/wolfcrypt/src/sp_cortexm.c index 334edb9f2..f147dcf13 100644 --- a/wolfcrypt/src/sp_cortexm.c +++ b/wolfcrypt/src/sp_cortexm.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef WOLFSSL_SP_SMALL #define WOLFSSL_SP_SMALL diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index c4e9c421c..6070faaa9 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -31,6 +31,9 @@ This library provides single precision (SP) integer math functions. #endif #include + +#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) + #include #ifdef NO_INLINE #include @@ -85,8 +88,6 @@ This library provides single precision (SP) integer math functions. * WOLFSSL_SP_FAST_MODEXP Allow fast mod_exp with small C code */ -#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) - #include /* DECL_SP_INT: Declare one variable of type 'sp_int'. */ @@ -12448,17 +12449,8 @@ static int _sp_read_radix_16(sp_int* a, const char* in) a->dp[0] = 0; for (i = (int)(XSTRLEN(in) - 1); i >= 0; i--) { - char ch = in[i]; - if ((ch >= '0') && (ch <= '9')) { - ch -= '0'; - } - else if ((ch >= 'A') && (ch <= 'F')) { - ch -= 'A' - 10; - } - else if ((ch >= 'a') && (ch <= 'f')) { - ch -= 'a' - 10; - } - else { + int ch = (int)HexCharToByte(in[i]); + if (ch < 0) { err = MP_VAL; break; } @@ -12597,11 +12589,6 @@ int sp_read_radix(sp_int* a, const char* in, int radix) #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \ defined(WC_MP_TO_RADIX) -/* Hex string characters. */ -static const char sp_hex_char[16] = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' -}; /* Put the big-endian, hex string encoding of a into str. * @@ -12668,11 +12655,11 @@ int sp_tohex(sp_int* a, char* str) #endif /* WC_DISABLE_RADIX_ZERO_PAD */ /* Most-significant word. */ for (; j >= 0; j -= 4) { - *(str++) = sp_hex_char[(a->dp[i] >> j) & 0xf]; + *(str++) = ByteToHex(a->dp[i] >> j); } for (--i; i >= 0; i--) { for (j = SP_WORD_SIZE - 4; j >= 0; j -= 4) { - *(str++) = sp_hex_char[(a->dp[i] >> j) & 0xf]; + *(str++) = ByteToHex(a->dp[i] >> j); } } *str = '\0'; diff --git a/wolfcrypt/src/sp_x86_64.c b/wolfcrypt/src/sp_x86_64.c index 7b23b1f5c..82f1c9ef7 100644 --- a/wolfcrypt/src/sp_x86_64.c +++ b/wolfcrypt/src/sp_x86_64.c @@ -26,6 +26,10 @@ #endif #include + +#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ + defined(WOLFSSL_HAVE_SP_ECC) + #include #include #ifdef NO_INLINE @@ -35,9 +39,6 @@ #include #endif -#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \ - defined(WOLFSSL_HAVE_SP_ECC) - #ifdef RSA_LOW_MEM #ifndef WOLFSSL_SP_SMALL #define WOLFSSL_SP_SMALL diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index ac8725727..13108308c 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -37,6 +37,9 @@ /* in case user set USE_FAST_MATH there */ #include + +#ifdef USE_FAST_MATH + #ifdef NO_INLINE #include #else @@ -44,8 +47,6 @@ #include #endif -#ifdef USE_FAST_MATH - #include #include #include /* will define asm MACROS or C ones */ @@ -5418,7 +5419,7 @@ static wcchar fp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" static int fp_read_radix_16(fp_int *a, const char *str) { int i, j, k, neg; - char ch; + int ch; /* if the leading digit is a * minus set the sign to negative. @@ -5433,15 +5434,10 @@ static int fp_read_radix_16(fp_int *a, const char *str) j = 0; k = 0; for (i = (int)(XSTRLEN(str) - 1); i >= 0; i--) { - ch = str[i]; - if (ch >= '0' && ch <= '9') - ch -= (char)'0'; - else if (ch >= 'A' && ch <= 'F') - ch -= (char)'A' - 10; - else if (ch >= 'a' && ch <= 'f') - ch -= (char)'a' - 10; - else - return FP_VAL; + ch = (int)HexCharToByte(str[i]); + if (ch < 0) { + return FP_VAL; + } k += j == DIGIT_BIT; j &= DIGIT_BIT - 1; diff --git a/wolfssl/wolfcrypt/misc.h b/wolfssl/wolfcrypt/misc.h index 748688fe9..2b0bc06e3 100644 --- a/wolfssl/wolfcrypt/misc.h +++ b/wolfssl/wolfcrypt/misc.h @@ -107,6 +107,9 @@ void ato24(const byte* c, word32* u24); void ato32(const byte* c, word32* u32); word32 btoi(byte b); +WOLFSSL_LOCAL char HexCharToByte(char ch); +WOLFSSL_LOCAL char ByteToHex(byte in); +WOLFSSL_LOCAL int ByteToHexStr(byte in, char* out); WOLFSSL_LOCAL byte ctMaskGT(int a, int b); WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);