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
This commit is contained in:
Guido Casati
2025-06-09 20:10:01 +02:00
parent d06790fcf2
commit bd1f266ab5
4 changed files with 33 additions and 48 deletions
-28
View File
@@ -7,34 +7,6 @@
#include <errno.h>
#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() **
-2
View File
@@ -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.**
+31 -18
View File
@@ -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)
+2
View File
@@ -5,6 +5,8 @@
#include <stdlib.h>
#include <stdint.h>
#define SECURITY_KEY_LEN 32
typedef enum {
NAS_ENC_ALG = 0x01,
NAS_INT_ALG = 0x02,