Module: kamailio Branch: master Commit: d48a29cf0c4936953e7a002bedf3ba538c41ff20 URL: https://github.com/kamailio/kamailio/commit/d48a29cf0c4936953e7a002bedf3ba538c41ff20
Author: Xenofon Karamanos <[email protected]> Committer: Henning Westerholt <[email protected]> Date: 2026-01-07T11:58:55+01:00 core/crypto: Add utils for sha3 hex to bytes and vice versa --- Modified: src/core/crypto/shautils.c Modified: src/core/crypto/shautils.h --- Diff: https://github.com/kamailio/kamailio/commit/d48a29cf0c4936953e7a002bedf3ba538c41ff20.diff Patch: https://github.com/kamailio/kamailio/commit/d48a29cf0c4936953e7a002bedf3ba538c41ff20.patch --- diff --git a/src/core/crypto/shautils.c b/src/core/crypto/shautils.c index bea42cdca15..1f632f24bc7 100644 --- a/src/core/crypto/shautils.c +++ b/src/core/crypto/shautils.c @@ -31,6 +31,9 @@ * \ingroup core/crypto * Module: \ref core/crypto */ +#include <ctype.h> +#include <errno.h> +#include <stdlib.h> #include "../../core/crypto/md5.h" #include "../../core/ut.h" @@ -91,3 +94,60 @@ void compute_sha512(char *dst, u_int8_t *src, int src_len) sr_SHA512_Update(&ctx512, src, src_len); sr_SHA512_End(&ctx512, dst); } + + +/** + * Convert hex string to bytes + */ +int hex_to_bytes(const char *hex_str, unsigned char *bytes, int max_bytes) +{ + int len; + int byte_len; + int i; + char *endptr; + long val; + + len = strlen(hex_str); + if(len % 2 != 0) + return -1; /* Invalid hex string */ + + byte_len = len / 2; + if(byte_len > max_bytes) + return -1; /* Too many bytes */ + + for(i = 0; i < byte_len; i++) { + char hex_byte[3] = {hex_str[i * 2], hex_str[i * 2 + 1], '\0'}; + + /* Check for valid hex characters before calling strtol */ + if(!isxdigit((unsigned char)hex_byte[0]) + || !isxdigit((unsigned char)hex_byte[1])) { + return -1; /* Invalid hex character */ + } + + errno = 0; + val = strtol(hex_byte, &endptr, 16); + + /* Check for conversion errors */ + if(errno != 0 || endptr == hex_byte || *endptr != '\0') { + return -1; /* Conversion failed */ + } + + /* Check for out of range values */ + if(val < 0 || val > 255) { + return -1; /* Value out of byte range */ + } + + bytes[i] = (unsigned char)val; + } + + return byte_len; +} + +/* Convert bytes to hex string */ +void bytes_to_hex(const unsigned char *bytes, size_t len, char *hex) +{ + for(size_t i = 0; i < len; i++) { + sprintf(hex + 2 * i, "%02x", bytes[i]); + } + hex[2 * len] = '\0'; +} diff --git a/src/core/crypto/shautils.h b/src/core/crypto/shautils.h index abcddfb8d8b..57e10a9082e 100644 --- a/src/core/crypto/shautils.h +++ b/src/core/crypto/shautils.h @@ -45,4 +45,8 @@ void compute_sha384(char *dst, u_int8_t *src, int src_len); void compute_sha512(char *dst, u_int8_t *src, int src_len); +int hex_to_bytes(const char *hex_str, unsigned char *bytes, int max_bytes); + +void bytes_to_hex(const unsigned char *bytes, size_t len, char *hex); + #endif _______________________________________________ Kamailio - Development Mailing List -- [email protected] To unsubscribe send an email to [email protected] Important: keep the mailing list in the recipients, do not reply only to the sender!
