crypto: factorize to hex chars conversion code.

Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24968)
This commit is contained in:
FdaSilvaYY 2024-08-01 22:47:00 +02:00 committed by Tomas Mraz
parent 668fdb593a
commit ca3c6f3829
8 changed files with 33 additions and 40 deletions

View file

@ -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, static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
int buflen) int buflen)
{ {
static const char hexdig[] = "0123456789ABCDEF";
unsigned char *p, *q; unsigned char *p, *q;
char hextmp[2]; char hextmp[2];
if (arg) { if (arg) {
p = buf; p = buf;
q = buf + buflen; q = buf + buflen;
while (p != q) { while (p != q) {
hextmp[0] = hexdig[*p >> 4]; ossl_to_hex(hextmp, *p);
hextmp[1] = hexdig[*p & 0xf];
if (!io_ch(arg, hextmp, 2)) if (!io_ch(arg, hextmp, 2))
return -1; return -1;
p++; p++;

View file

@ -16,7 +16,6 @@
int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
{ {
int i, n = 0; int i, n = 0;
static const char *h = "0123456789ABCDEF";
char buf[2]; char buf[2];
if (a == NULL) if (a == NULL)
@ -39,8 +38,7 @@ int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
goto err; goto err;
n += 2; n += 2;
} }
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; ossl_to_hex(buf, a->data[i]);
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
if (BIO_write(bp, buf, 2) != 2) if (BIO_write(bp, buf, 2) != 2)
goto err; goto err;
n += 2; n += 2;

View file

@ -16,7 +16,6 @@
int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
{ {
int i, n = 0; int i, n = 0;
static const char *h = "0123456789ABCDEF";
char buf[2]; char buf[2];
if (a == NULL) if (a == NULL)
@ -33,8 +32,7 @@ int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
goto err; goto err;
n += 2; n += 2;
} }
buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; ossl_to_hex(buf, a->data[i]);
buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
if (BIO_write(bp, buf, 2) != 2) if (BIO_write(bp, buf, 2) != 2)
goto err; goto err;
n += 2; n += 2;

View file

@ -11,8 +11,6 @@
#include "crypto/ctype.h" #include "crypto/ctype.h"
#include "bn_local.h" #include "bn_local.h"
static const char Hex[] = "0123456789ABCDEF";
/* Must 'OPENSSL_free' the returned data */ /* Must 'OPENSSL_free' the returned data */
char *BN_bn2hex(const BIGNUM *a) char *BN_bn2hex(const BIGNUM *a)
{ {
@ -33,8 +31,7 @@ char *BN_bn2hex(const BIGNUM *a)
/* strip leading zeros */ /* strip leading zeros */
v = (int)((a->d[i] >> j) & 0xff); v = (int)((a->d[i] >> j) & 0xff);
if (z || v != 0) { if (z || v != 0) {
*p++ = Hex[v >> 4]; p += ossl_to_hex(p, v);
*p++ = Hex[v & 0x0f];
z = 1; z = 1;
} }
} }

View file

@ -9,18 +9,17 @@
#include <string.h> /* strlen */ #include <string.h> /* strlen */
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include "internal/cryptlib.h"
#include "ec_local.h" #include "ec_local.h"
static const char *HEX_DIGITS = "0123456789ABCDEF";
/* the return value must be freed (using OPENSSL_free()) */ /* the return value must be freed (using OPENSSL_free()) */
char *EC_POINT_point2hex(const EC_GROUP *group, char *EC_POINT_point2hex(const EC_GROUP *group,
const EC_POINT *point, const EC_POINT *point,
point_conversion_form_t form, BN_CTX *ctx) point_conversion_form_t form, BN_CTX *ctx)
{ {
char *ret, *p; char *ret, *p;
size_t buf_len = 0, i; size_t buf_len, i;
unsigned char *buf = NULL, *pbuf; unsigned char *buf = NULL;
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx); buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
@ -28,21 +27,16 @@ char *EC_POINT_point2hex(const EC_GROUP *group,
return NULL; return NULL;
ret = OPENSSL_malloc(buf_len * 2 + 2); ret = OPENSSL_malloc(buf_len * 2 + 2);
if (ret == NULL) { if (ret == NULL)
OPENSSL_free(buf); goto err;
return NULL;
}
p = ret; p = ret;
pbuf = buf; for (i = 0; i < buf_len; ++i)
for (i = buf_len; i > 0; i--) { p += ossl_to_hex(p, buf[i]);
int v = (int)*(pbuf++);
*(p++) = HEX_DIGITS[v >> 4];
*(p++) = HEX_DIGITS[v & 0x0F];
}
*p = '\0'; *p = '\0';
err:
OPENSSL_free(buf); OPENSSL_free(buf);
return ret; return ret;
} }

View file

@ -14,6 +14,7 @@
#include "crypto/ctype.h" #include "crypto/ctype.h"
#include "internal/cryptlib.h" #include "internal/cryptlib.h"
#include "internal/thread_once.h" #include "internal/thread_once.h"
#include "internal/to_hex.h"
#define DEFAULT_SEPARATOR ':' #define DEFAULT_SEPARATOR ':'
#define CH_ZERO '\0' #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 unsigned char *buf, size_t buflen,
const char sep) const char sep)
{ {
static const char hexdig[] = "0123456789ABCDEF";
const unsigned char *p;
char *q; char *q;
size_t i;
int has_sep = (sep != CH_ZERO); 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) if (len == 0)
++len; ++len;
@ -306,9 +304,8 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
} }
q = str; q = str;
for (i = 0, p = buf; i < buflen; i++, p++) { for (i = 0; i < buflen; i++) {
*q++ = hexdig[(*p >> 4) & 0xf]; q += ossl_to_hex(q, buf[i]);
*q++ = hexdig[*p & 0xf];
if (has_sep) if (has_sep)
*q++ = sep; *q++ = sep;
} }
@ -428,3 +425,10 @@ int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
return 0; return 0;
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);
}

View file

@ -32,7 +32,6 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
char *p; char *p;
unsigned char *q; unsigned char *q;
BUF_MEM *b = NULL; BUF_MEM *b = NULL;
static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4]; int gs_doit[4];
char tmp_buf[80]; char tmp_buf[80];
#ifdef CHARSET_EBCDIC #ifdef CHARSET_EBCDIC
@ -147,8 +146,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
if ((n < ' ') || (n > '~')) { if ((n < ' ') || (n > '~')) {
*(p++) = '\\'; *(p++) = '\\';
*(p++) = 'x'; *(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f]; p += ossl_to_hex(p, n);
*(p++) = hex[n & 0x0f];
} else { } else {
if (n == '/' || n == '+') if (n == '/' || n == '+')
*(p++) = '\\'; *(p++) = '\\';
@ -159,8 +157,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) { if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\'; *(p++) = '\\';
*(p++) = 'x'; *(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f]; p += ossl_to_hex(p, n);
*(p++) = hex[n & 0x0f];
} else { } else {
if (n == os_toascii['/'] || n == os_toascii['+']) if (n == os_toascii['/'] || n == os_toascii['+'])
*(p++) = '\\'; *(p++) = '\\';

View file

@ -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, unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
const char sep); 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); STACK_OF(SSL_COMP) *ossl_load_builtin_compressions(void);
void ossl_free_compression_methods_int(STACK_OF(SSL_COMP) *methods); void ossl_free_compression_methods_int(STACK_OF(SSL_COMP) *methods);