From: FdaSilvaYY Date: Thu, 1 Aug 2024 20:47:00 +0000 (+0200) Subject: crypto: factorize to hex chars conversion code. X-Git-Tag: openssl-3.4.0-alpha1~214 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ca3c6f38292ab1326af9fa414cfa8de4e30a4e82;p=thirdparty%2Fopenssl.git crypto: factorize to hex chars conversion code. Reviewed-by: Tom Cosgrove Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/24968) --- diff --git a/crypto/asn1/a_strex.c b/crypto/asn1/a_strex.c index 29ea60596e5..89fda41183c 100644 --- a/crypto/asn1/a_strex.c +++ b/crypto/asn1/a_strex.c @@ -234,15 +234,14 @@ static int do_buf(unsigned char *buf, int buflen, static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen) { - static const char hexdig[] = "0123456789ABCDEF"; unsigned char *p, *q; char hextmp[2]; + if (arg) { p = buf; q = buf + buflen; while (p != q) { - hextmp[0] = hexdig[*p >> 4]; - hextmp[1] = hexdig[*p & 0xf]; + ossl_to_hex(hextmp, *p); if (!io_ch(arg, hextmp, 2)) return -1; p++; diff --git a/crypto/asn1/f_int.c b/crypto/asn1/f_int.c index 416c6f7195f..ef0b1e11148 100644 --- a/crypto/asn1/f_int.c +++ b/crypto/asn1/f_int.c @@ -16,7 +16,6 @@ int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) { int i, n = 0; - static const char *h = "0123456789ABCDEF"; char buf[2]; if (a == NULL) @@ -39,8 +38,7 @@ int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) goto err; n += 2; } - buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; - buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; + ossl_to_hex(buf, a->data[i]); if (BIO_write(bp, buf, 2) != 2) goto err; n += 2; diff --git a/crypto/asn1/f_string.c b/crypto/asn1/f_string.c index 1da442a4575..7ecb001869d 100644 --- a/crypto/asn1/f_string.c +++ b/crypto/asn1/f_string.c @@ -16,7 +16,6 @@ int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) { int i, n = 0; - static const char *h = "0123456789ABCDEF"; char buf[2]; if (a == NULL) @@ -33,8 +32,7 @@ int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) goto err; n += 2; } - buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; - buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; + ossl_to_hex(buf, a->data[i]); if (BIO_write(bp, buf, 2) != 2) goto err; n += 2; diff --git a/crypto/bn/bn_conv.c b/crypto/bn/bn_conv.c index 849440e71e7..f8c2cd95adb 100644 --- a/crypto/bn/bn_conv.c +++ b/crypto/bn/bn_conv.c @@ -11,8 +11,6 @@ #include "crypto/ctype.h" #include "bn_local.h" -static const char Hex[] = "0123456789ABCDEF"; - /* Must 'OPENSSL_free' the returned data */ char *BN_bn2hex(const BIGNUM *a) { @@ -33,8 +31,7 @@ char *BN_bn2hex(const BIGNUM *a) /* strip leading zeros */ v = (int)((a->d[i] >> j) & 0xff); if (z || v != 0) { - *p++ = Hex[v >> 4]; - *p++ = Hex[v & 0x0f]; + p += ossl_to_hex(p, v); z = 1; } } diff --git a/crypto/ec/ec_print.c b/crypto/ec/ec_print.c index ffe112052fb..907948f0be3 100644 --- a/crypto/ec/ec_print.c +++ b/crypto/ec/ec_print.c @@ -9,18 +9,17 @@ #include /* strlen */ #include +#include "internal/cryptlib.h" #include "ec_local.h" -static const char *HEX_DIGITS = "0123456789ABCDEF"; - /* the return value must be freed (using OPENSSL_free()) */ char *EC_POINT_point2hex(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, BN_CTX *ctx) { char *ret, *p; - size_t buf_len = 0, i; - unsigned char *buf = NULL, *pbuf; + size_t buf_len, i; + unsigned char *buf = NULL; buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx); @@ -28,21 +27,16 @@ char *EC_POINT_point2hex(const EC_GROUP *group, return NULL; ret = OPENSSL_malloc(buf_len * 2 + 2); - if (ret == NULL) { - OPENSSL_free(buf); - return NULL; - } + if (ret == NULL) + goto err; + p = ret; - pbuf = buf; - for (i = buf_len; i > 0; i--) { - int v = (int)*(pbuf++); - *(p++) = HEX_DIGITS[v >> 4]; - *(p++) = HEX_DIGITS[v & 0x0F]; - } + for (i = 0; i < buf_len; ++i) + p += ossl_to_hex(p, buf[i]); *p = '\0'; + err: OPENSSL_free(buf); - return ret; } diff --git a/crypto/o_str.c b/crypto/o_str.c index ba41d76fc17..93af73561f9 100644 --- a/crypto/o_str.c +++ b/crypto/o_str.c @@ -14,6 +14,7 @@ #include "crypto/ctype.h" #include "internal/cryptlib.h" #include "internal/thread_once.h" +#include "internal/to_hex.h" #define DEFAULT_SEPARATOR ':' #define CH_ZERO '\0' @@ -286,12 +287,9 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, const unsigned char *buf, size_t buflen, const char sep) { - static const char hexdig[] = "0123456789ABCDEF"; - const unsigned char *p; char *q; - size_t i; int has_sep = (sep != CH_ZERO); - size_t len = has_sep ? buflen * 3 : 1 + buflen * 2; + size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2; if (len == 0) ++len; @@ -306,9 +304,8 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, } q = str; - for (i = 0, p = buf; i < buflen; i++, p++) { - *q++ = hexdig[(*p >> 4) & 0xf]; - *q++ = hexdig[*p & 0xf]; + for (i = 0; i < buflen; i++) { + q += ossl_to_hex(q, buf[i]); if (has_sep) *q++ = sep; } @@ -428,3 +425,10 @@ int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) return 0; return 0; } + +size_t ossl_to_hex(char *buf, uint8_t n) +{ + static const char hexdig[] = "0123456789ABCDEF"; + + return to_hex(buf, n, hexdig); +} diff --git a/crypto/x509/x509_obj.c b/crypto/x509/x509_obj.c index 2af7203b012..a34235b7100 100644 --- a/crypto/x509/x509_obj.c +++ b/crypto/x509/x509_obj.c @@ -32,7 +32,6 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) char *p; unsigned char *q; BUF_MEM *b = NULL; - static const char hex[17] = "0123456789ABCDEF"; int gs_doit[4]; char tmp_buf[80]; #ifdef CHARSET_EBCDIC @@ -147,8 +146,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) if ((n < ' ') || (n > '~')) { *(p++) = '\\'; *(p++) = 'x'; - *(p++) = hex[(n >> 4) & 0x0f]; - *(p++) = hex[n & 0x0f]; + p += ossl_to_hex(p, n); } else { if (n == '/' || n == '+') *(p++) = '\\'; @@ -159,8 +157,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) if ((n < os_toascii[' ']) || (n > os_toascii['~'])) { *(p++) = '\\'; *(p++) = 'x'; - *(p++) = hex[(n >> 4) & 0x0f]; - *(p++) = hex[n & 0x0f]; + p += ossl_to_hex(p, n); } else { if (n == os_toascii['/'] || n == os_toascii['+']) *(p++) = '\\'; diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h index 32caabad7e5..65f2b368019 100644 --- a/include/internal/cryptlib.h +++ b/include/internal/cryptlib.h @@ -162,6 +162,12 @@ char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep); unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen, const char sep); +/** + * Writes |n| value in hex format into |buf|, + * and returns the number of bytes written + */ +size_t ossl_to_hex(char *buf, uint8_t n); + STACK_OF(SSL_COMP) *ossl_load_builtin_compressions(void); void ossl_free_compression_methods_int(STACK_OF(SSL_COMP) *methods);