]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
crypto: factorize to hex chars conversion code.
authorFdaSilvaYY <fdasilvayy@gmail.com>
Thu, 1 Aug 2024 20:47:00 +0000 (22:47 +0200)
committerTomas Mraz <tomas@openssl.org>
Wed, 7 Aug 2024 17:25:10 +0000 (19:25 +0200)
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24968)

crypto/asn1/a_strex.c
crypto/asn1/f_int.c
crypto/asn1/f_string.c
crypto/bn/bn_conv.c
crypto/ec/ec_print.c
crypto/o_str.c
crypto/x509/x509_obj.c
include/internal/cryptlib.h

index 29ea60596e56ac9521d8e2a274a1a2376348ed0e..89fda41183c8e8878f26f7c807ee5d6480fac451 100644 (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,
                        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++;
index 416c6f7195fa2a57b8404d1a64c229725002b1c4..ef0b1e1114868a4082df6b940aa9e937e4e6650e 100644 (file)
@@ -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;
index 1da442a4575c971d70963a920959ca658e4cff83..7ecb001869d569d0e8b5d983679b3ffe7516cffd 100644 (file)
@@ -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;
index 849440e71e773e9a8412355bc5d59db2c5d625c8..f8c2cd95adb5bfbc080af2c4482811794b432a51 100644 (file)
@@ -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;
             }
         }
index ffe112052fbd620912f56958067dfac1db191975..907948f0be3e35229a4c1b3b36067f3fde82375c 100644 (file)
@@ -9,18 +9,17 @@
 
 #include <string.h> /* strlen */
 #include <openssl/crypto.h>
+#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;
 }
 
index ba41d76fc17212fa594f62dc2b03da36a3b65569..93af73561f97916f44e66dc8d6d0f8529d4d5cc7 100644 (file)
@@ -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);
+}
index 2af7203b01262e7e7f71fa08910fec89e5df65f3..a34235b7100bd07709ff61fb836321d7c4f44138 100644 (file)
@@ -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++) = '\\';
index 32caabad7e5b07463f36956b8ce76b9212e39a22..65f2b368019b568e03c9e012c5c45289278e013a 100644 (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,
                                    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);