From bd1f266ab5426ff39b8b7ca6f5e9d5e119527511 Mon Sep 17 00:00:00 2001 From: Guido Casati Date: Mon, 26 May 2025 16:26:26 +0200 Subject: [PATCH] Add utility to log hex buffer with size len (LOG_D) and remove unused legacy hex_dump * logging buffer is pretty useful for debugging purposes, e.g. derived key values --- common/utils/utils.c | 28 ------------------- common/utils/utils.h | 2 -- openair3/SECU/key_nas_deriver.c | 49 +++++++++++++++++++++------------ openair3/SECU/key_nas_deriver.h | 2 ++ 4 files changed, 33 insertions(+), 48 deletions(-) diff --git a/common/utils/utils.c b/common/utils/utils.c index 377c4512b7..33797b49df 100644 --- a/common/utils/utils.c +++ b/common/utils/utils.c @@ -7,34 +7,6 @@ #include #include "utils.h" - - -const char *hexdump(const void *data, size_t data_len, char *out, size_t out_len) -{ - char *p = out; - char *endp = out + out_len; - const uint8_t *q = data; - snprintf(p, endp - p, "[%zu]", data_len); - p += strlen(p); - for (size_t i = 0; i < data_len; ++i) - { - if (p >= endp) - { - static const char ellipses[] = "..."; - char *s = endp - sizeof(ellipses); - if (s >= p) - { - strcpy(s, ellipses); - } - break; - } - snprintf(p, endp - p, " %02X", *q++); - p += strlen(p); - } - return out; -} - - /**************************************************************************** ** ** ** Name: hex_char_to_hex_value() ** diff --git a/common/utils/utils.h b/common/utils/utils.h index 841cb2af9c..df188b812f 100644 --- a/common/utils/utils.h +++ b/common/utils/utils.h @@ -109,8 +109,6 @@ static inline void *malloc_or_fail(size_t size) #define PAGE_MASK 0xfffff000 #define virt_to_phys(x) (x) -const char *hexdump(const void *data, size_t data_len, char *out, size_t out_len); - // Converts an hexadecimal ASCII coded digit into its value. ** int hex_char_to_hex_value (char c); // Converts an hexadecimal ASCII coded string into its value.** diff --git a/openair3/SECU/key_nas_deriver.c b/openair3/SECU/key_nas_deriver.c index ec6da5e411..3c0b05dc50 100644 --- a/openair3/SECU/key_nas_deriver.c +++ b/openair3/SECU/key_nas_deriver.c @@ -57,14 +57,32 @@ #define NR_FC_ALG_KEY_DER (0x69) #define NR_FC_ALG_KEY_NG_RAN_STAR_DER (0x70) -// #define SECU_DEBUG 1 +static void log_hex_buffer(const char *label, const uint8_t *buf, const int len) +{ + char hex_str[len * 2 + 1]; + for (int i = 0; i < len; i++) { + snprintf(&hex_str[i * 2], 3, "%02x", buf[i]); + } + hex_str[len * 2] = '\0'; + LOG_D(UTIL, "%s: %s\n", label, hex_str); +} + +static const char* alg_type_to_str(algorithm_type_dist_t alg_type) +{ + switch (alg_type) { + case NAS_ENC_ALG: return "NAS_ENC_ALG"; + case NAS_INT_ALG: return "NAS_INT_ALG"; + case RRC_ENC_ALG: return "RRC_ENC_ALG"; + case RRC_INT_ALG: return "RRC_INT_ALG"; + case UP_ENC_ALG: return "UP_ENC_ALG"; + case UP_INT_ALG: return "UP_INT_ALG"; + default: return "UNKNOWN_ALG_TYPE"; + } +} static void derive_key_common(algorithm_type_dist_t alg_type, uint8_t alg_id, const uint8_t kasme[32], uint8_t FC, uint8_t out[32]) { uint8_t s[7]; -#if defined(SECU_DEBUG) - int i; -#endif /* FC */ s[0] = FC; @@ -83,18 +101,11 @@ static void derive_key_common(algorithm_type_dist_t alg_type, uint8_t alg_id, co s[5] = 0x00; s[6] = 0x01; -#if defined(SECU_DEBUG) - printf("%s FC %d nas_alg_type distinguisher %d nas_enc_alg_identity %d\n", __FUNCTION__, FC_ALG_KEY_DER, alg_type, alg_id); - - for (i = 0; i < 7; i ++) { - printf("0x%02x ", s[i]); - } - - printf("\n"); -#endif - byte_array_t data = {.len = 7, .buf = s}; kdf(kasme, data, 32, out); + log_hex_buffer("Input key (kgnb)", kasme, 32); + log_hex_buffer("Derivation string (s)", s, 7); + log_hex_buffer(alg_type_to_str(alg_type), out, 32); } /*! @@ -165,10 +176,9 @@ void derive_kgnb(uint8_t kamf[32], uint32_t count, uint8_t *kgnb) byte_array_t data = {.buf = input, .len = 10}; kdf(kamf, data, 32, kgnb); - printf("kgnb : "); - for (int pp = 0; pp < 32; pp++) - printf("%02x ", kgnb[pp]); - printf("\n"); + log_hex_buffer("KAMF", kamf, 32); + log_hex_buffer("KDF input for kgnb", input, 10); + log_hex_buffer("Derived kgnb", kgnb, 32); } void derive_keNB(const uint8_t kasme[32], const uint32_t nas_count, uint8_t *keNB) @@ -260,6 +270,9 @@ void nr_derive_key_ng_ran_star(uint16_t pci, uint64_t nr_arfcn_dl, const uint8_t byte_array_t data = {.buf = s, .len = 10}; const uint32_t len_key = 32; kdf(key, data, len_key, key_ng_ran_star); + log_hex_buffer("Input key", key, 32); + log_hex_buffer("KDF input for ng_ran*", s, 10); + log_hex_buffer("Derived key_ng_ran*", key_ng_ran_star, 32); } void derive_skgNB(const uint8_t *keNB, const uint16_t sk_counter, uint8_t *skgNB) diff --git a/openair3/SECU/key_nas_deriver.h b/openair3/SECU/key_nas_deriver.h index 99c5b20b51..4cb336bce9 100644 --- a/openair3/SECU/key_nas_deriver.h +++ b/openair3/SECU/key_nas_deriver.h @@ -5,6 +5,8 @@ #include #include +#define SECURITY_KEY_LEN 32 + typedef enum { NAS_ENC_ALG = 0x01, NAS_INT_ALG = 0x02,