forked from wolfSSL/wolfssl
Refactor hex char to byte conversions.
This commit is contained in:
26
src/ssl.c
26
src/ssl.c
@ -33495,19 +33495,7 @@ void *wolfSSL_OPENSSL_malloc(size_t a)
|
|||||||
|
|
||||||
int wolfSSL_OPENSSL_hexchar2int(unsigned char c)
|
int wolfSSL_OPENSSL_hexchar2int(unsigned char c)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
return (int)HexCharToByte((char)c);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *wolfSSL_OPENSSL_hexstr2buf(const char *str, long *len)
|
unsigned char *wolfSSL_OPENSSL_hexstr2buf(const char *str, long *len)
|
||||||
@ -50959,9 +50947,6 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str,
|
|||||||
|
|
||||||
/* dump hex */
|
/* dump hex */
|
||||||
if (flags & ASN1_STRFLGS_DUMP_ALL){
|
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 hex_tmp[4];
|
||||||
char *str_ptr, *str_end;
|
char *str_ptr, *str_end;
|
||||||
|
|
||||||
@ -50977,10 +50962,8 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str,
|
|||||||
}
|
}
|
||||||
str_len++;
|
str_len++;
|
||||||
if (flags & ASN1_STRFLGS_DUMP_DER){
|
if (flags & ASN1_STRFLGS_DUMP_DER){
|
||||||
hex_tmp[0] = hex_char[(str->type & 0xf0) >> 4];
|
ByteToHexStr((byte)str->type, &hex_tmp[0]);
|
||||||
hex_tmp[1] = hex_char[(str->type & 0x0f)];
|
ByteToHexStr((byte)str->length, &hex_tmp[2]);
|
||||||
hex_tmp[2] = hex_char[(str->length & 0xf0) >> 4];
|
|
||||||
hex_tmp[3] = hex_char[(str->length & 0x0f)];
|
|
||||||
if (wolfSSL_BIO_write(out, hex_tmp, 4) != 4){
|
if (wolfSSL_BIO_write(out, hex_tmp, 4) != 4){
|
||||||
goto err_exit;
|
goto err_exit;
|
||||||
}
|
}
|
||||||
@ -50991,8 +50974,7 @@ int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str,
|
|||||||
str_ptr = str->data;
|
str_ptr = str->data;
|
||||||
str_end = str->data + str->length;
|
str_end = str->data + str->length;
|
||||||
while (str_ptr < str_end){
|
while (str_ptr < str_end){
|
||||||
hex_tmp[0] = hex_char[*str_ptr >> 4];
|
ByteToHexStr((byte)*str_ptr, &hex_tmp[0]);
|
||||||
hex_tmp[1] = hex_char[*str_ptr & 0xf];
|
|
||||||
if (wolfSSL_BIO_write(out, hex_tmp, 2) != 2){
|
if (wolfSSL_BIO_write(out, hex_tmp, 2) != 2){
|
||||||
goto err_exit;
|
goto err_exit;
|
||||||
}
|
}
|
||||||
|
@ -12171,7 +12171,7 @@ static mp_int* GetRsaInt(RsaKey* key, int idx)
|
|||||||
int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
|
int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
|
||||||
{
|
{
|
||||||
int ret = 0, i, j, outLen = 0, mpSz;
|
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];
|
word32 sizes[RSA_INTS];
|
||||||
byte seq[MAX_SEQ_SZ];
|
byte seq[MAX_SEQ_SZ];
|
||||||
byte ver[MAX_VERSION_SZ];
|
byte ver[MAX_VERSION_SZ];
|
||||||
@ -16500,15 +16500,6 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
|
|||||||
|
|
||||||
|
|
||||||
#ifdef WOLFSSL_CUSTOM_CURVES
|
#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 */
|
/* returns 0 on success */
|
||||||
static int ASNToHexString(const byte* input, word32* inOutIdx, char** out,
|
static int ASNToHexString(const byte* input, word32* inOutIdx, char** out,
|
||||||
word32 inSz, void* heap, int heapType)
|
word32 inSz, void* heap, int heapType)
|
||||||
@ -16539,7 +16530,7 @@ static int ASNToHexString(const byte* input, word32* inOutIdx, char** out,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<len; i++)
|
for (i=0; i<len; i++)
|
||||||
ByteToHex(input[*inOutIdx + i], str + i*2);
|
ByteToHexStr(input[*inOutIdx + i], str + i*2);
|
||||||
str[len*2] = '\0';
|
str[len*2] = '\0';
|
||||||
|
|
||||||
*inOutIdx += len;
|
*inOutIdx += len;
|
||||||
|
@ -422,6 +422,35 @@ WC_STATIC WC_INLINE word32 btoi(byte b)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
WC_STATIC WC_INLINE char HexCharToByte(char ch)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
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
|
#ifndef WOLFSSL_NO_CT_OPS
|
||||||
/* Constant time - mask set when a > b. */
|
/* Constant time - mask set when a > b. */
|
||||||
@ -516,7 +545,6 @@ WC_STATIC WC_INLINE byte ctSetLTE(int a, int b)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#undef WC_STATIC
|
#undef WC_STATIC
|
||||||
|
|
||||||
#endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
|
#endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
|
||||||
|
@ -12448,17 +12448,8 @@ static int _sp_read_radix_16(sp_int* a, const char* in)
|
|||||||
|
|
||||||
a->dp[0] = 0;
|
a->dp[0] = 0;
|
||||||
for (i = (int)(XSTRLEN(in) - 1); i >= 0; i--) {
|
for (i = (int)(XSTRLEN(in) - 1); i >= 0; i--) {
|
||||||
char ch = in[i];
|
int ch = (int)HexCharToByte(in[i]);
|
||||||
if ((ch >= '0') && (ch <= '9')) {
|
if (ch < 0) {
|
||||||
ch -= '0';
|
|
||||||
}
|
|
||||||
else if ((ch >= 'A') && (ch <= 'F')) {
|
|
||||||
ch -= 'A' - 10;
|
|
||||||
}
|
|
||||||
else if ((ch >= 'a') && (ch <= 'f')) {
|
|
||||||
ch -= 'a' - 10;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -12597,11 +12588,6 @@ int sp_read_radix(sp_int* a, const char* in, int radix)
|
|||||||
|
|
||||||
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
|
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
|
||||||
defined(WC_MP_TO_RADIX)
|
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.
|
/* Put the big-endian, hex string encoding of a into str.
|
||||||
*
|
*
|
||||||
@ -12668,11 +12654,11 @@ int sp_tohex(sp_int* a, char* str)
|
|||||||
#endif /* WC_DISABLE_RADIX_ZERO_PAD */
|
#endif /* WC_DISABLE_RADIX_ZERO_PAD */
|
||||||
/* Most-significant word. */
|
/* Most-significant word. */
|
||||||
for (; j >= 0; j -= 4) {
|
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 (--i; i >= 0; i--) {
|
||||||
for (j = SP_WORD_SIZE - 4; j >= 0; j -= 4) {
|
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';
|
*str = '\0';
|
||||||
|
@ -5418,7 +5418,7 @@ static wcchar fp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|||||||
static int fp_read_radix_16(fp_int *a, const char *str)
|
static int fp_read_radix_16(fp_int *a, const char *str)
|
||||||
{
|
{
|
||||||
int i, j, k, neg;
|
int i, j, k, neg;
|
||||||
char ch;
|
int ch;
|
||||||
|
|
||||||
/* if the leading digit is a
|
/* if the leading digit is a
|
||||||
* minus set the sign to negative.
|
* minus set the sign to negative.
|
||||||
@ -5433,15 +5433,10 @@ static int fp_read_radix_16(fp_int *a, const char *str)
|
|||||||
j = 0;
|
j = 0;
|
||||||
k = 0;
|
k = 0;
|
||||||
for (i = (int)(XSTRLEN(str) - 1); i >= 0; i--) {
|
for (i = (int)(XSTRLEN(str) - 1); i >= 0; i--) {
|
||||||
ch = str[i];
|
ch = (int)HexCharToByte(str[i]);
|
||||||
if (ch >= '0' && ch <= '9')
|
if (ch < 0) {
|
||||||
ch -= (char)'0';
|
return FP_VAL;
|
||||||
else if (ch >= 'A' && ch <= 'F')
|
}
|
||||||
ch -= (char)'A' - 10;
|
|
||||||
else if (ch >= 'a' && ch <= 'f')
|
|
||||||
ch -= (char)'a' - 10;
|
|
||||||
else
|
|
||||||
return FP_VAL;
|
|
||||||
|
|
||||||
k += j == DIGIT_BIT;
|
k += j == DIGIT_BIT;
|
||||||
j &= DIGIT_BIT - 1;
|
j &= DIGIT_BIT - 1;
|
||||||
|
@ -107,6 +107,9 @@ void ato24(const byte* c, word32* u24);
|
|||||||
void ato32(const byte* c, word32* u32);
|
void ato32(const byte* c, word32* u32);
|
||||||
word32 btoi(byte b);
|
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 ctMaskGT(int a, int b);
|
||||||
WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);
|
WOLFSSL_LOCAL byte ctMaskGTE(int a, int b);
|
||||||
|
Reference in New Issue
Block a user