Skip to content

Commit

Permalink
Add OPENSSL_hexstr2buf
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Sep 10, 2024
1 parent 685cc6c commit 4861932
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crypto/crypto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ TEST(CryptoTest, Strndup) {
EXPECT_STREQ("", str.get());
}

TEST(CryptoTest, OPENSSL_hexstr2buf) {
const char *test_cases[][2] = {{"a2", "\xa2"},
{"a213", "\xa2\x13"},
{"ffeedd", "\xff\xee\xdd"},
{"10aab1c2", "\x10\xaa\xb1\xc2"}};

for (auto test_case : test_cases) {
const char *test_value = test_case[0];
const char *expected_answer = test_case[1];
size_t actual_answer_len = 0;
size_t expected_answer_len = OPENSSL_strnlen(test_case[1], 5);
unsigned char *buf = OPENSSL_hexstr2buf(test_value, &actual_answer_len);
EXPECT_EQ(expected_answer_len, actual_answer_len);
EXPECT_EQ(0, OPENSSL_memcmp(buf, expected_answer, expected_answer_len));
OPENSSL_free(buf);
}
}

#if defined(BORINGSSL_FIPS_COUNTERS)
using CounterArray = size_t[fips_counter_max + 1];

Expand Down
30 changes: 30 additions & 0 deletions crypto/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,36 @@ int OPENSSL_fromxdigit(uint8_t *out, int c) {
return 0;
}

uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) {
if (str == NULL || len == NULL) {
return NULL;
}

const size_t slen = strlen(str);
if (slen % 2 != 0) {
return NULL;
}

const size_t buflen = slen / 2;
uint8_t *buf = OPENSSL_zalloc(buflen);
if (buf == NULL) {
return NULL;
}

for (size_t i = 0; i < buflen; i++) {
uint8_t hi, lo;
if (!OPENSSL_fromxdigit(&hi, str[2 * i]) ||
!OPENSSL_fromxdigit(&lo, str[2 * i + 1])) {
OPENSSL_free(buf);
return NULL;
}
buf[i] = (hi << 4) | lo;
}

*len = buflen;
return buf;
}

int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }

int OPENSSL_tolower(int c) {
Expand Down
7 changes: 7 additions & 0 deletions include/openssl/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ OPENSSL_EXPORT int OPENSSL_isxdigit(int c);
// zero is returned.
OPENSSL_EXPORT int OPENSSL_fromxdigit(uint8_t *out, int c);

// OPENSSL_hexstr2buf allocates and returns a buffer containing the bytes
// represented by the hexadecimal string |str|. |str| must be a NULL terminated
// string of hex characters. The length of the buffer is stored in |*len|.
// |len| must not be NULL. The caller must free the returned
// buffer with |OPENSSL_free|. If |str| is malformed, NULL is returned.
OPENSSL_EXPORT uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len);

// OPENSSL_isalnum is a locale-independent, ASCII-only version of isalnum(3), It
// only recognizes what |OPENSSL_isalpha| and |OPENSSL_isdigit| recognize.
OPENSSL_EXPORT int OPENSSL_isalnum(int c);
Expand Down

0 comments on commit 4861932

Please sign in to comment.