From 70eb09dc099a13f60d9980a122ecb01730ce5c70 Mon Sep 17 00:00:00 2001 From: Guido Casati Date: Mon, 26 May 2025 16:28:45 +0200 Subject: [PATCH] Add NH derivation function (A.10 3GPP TS 33.501) --- openair3/SECU/key_nas_deriver.c | 24 ++++++++++++++++++++++++ openair3/SECU/key_nas_deriver.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/openair3/SECU/key_nas_deriver.c b/openair3/SECU/key_nas_deriver.c index 3c0b05dc50..a7cf27a1ff 100644 --- a/openair3/SECU/key_nas_deriver.c +++ b/openair3/SECU/key_nas_deriver.c @@ -293,3 +293,27 @@ void derive_skgNB(const uint8_t *keNB, const uint16_t sk_counter, uint8_t *skgNB byte_array_t data = {.buf = s, .len = 5}; kdf(keNB, data, 32, skgNB); } + +/** @brief NH derivation function (A.10 3GPP TS 33.501) + * @param k_amf: input key + * @param sync_input: pointer to the newly derived KgNB for the initial NH derivation, + * and the previous NH for all subsequent derivations + * @param nh: pointer to the derivated NH output */ +void nr_derive_nh(const uint8_t k_amf[SECURITY_KEY_LEN], const uint8_t *sync_input, uint8_t *nh) +{ + uint8_t s[SECURITY_KEY_LEN + 3] = {0}; + // FC + s[0] = FC_NH; + // P0 = SYNC-input + memcpy(&s[1], sync_input, SECURITY_KEY_LEN); + // L0 = length of SYNC-input (i.e. 0x00 0x20) + s[SECURITY_KEY_LEN + 1] = 0x00; + s[SECURITY_KEY_LEN + 2] = 0x20; + + byte_array_t data = {.buf = s, .len = sizeof(s)}; + kdf(k_amf, data, SECURITY_KEY_LEN, nh); + + log_hex_buffer("KDF input (SYNC string)", s, SECURITY_KEY_LEN + 3); + log_hex_buffer("Derived NH", nh, SECURITY_KEY_LEN); + +} diff --git a/openair3/SECU/key_nas_deriver.h b/openair3/SECU/key_nas_deriver.h index 4cb336bce9..0ab14ad51a 100644 --- a/openair3/SECU/key_nas_deriver.h +++ b/openair3/SECU/key_nas_deriver.h @@ -34,4 +34,6 @@ void nr_derive_key(algorithm_type_dist_t alg_type, uint8_t alg_id, const uint8_t void nr_derive_key_ng_ran_star(uint16_t pci, uint64_t nr_arfcn_dl, const uint8_t key[32], uint8_t *key_ng_ran_star); +void nr_derive_nh(const uint8_t k_amf[SECURITY_KEY_LEN], const uint8_t *sync_input, uint8_t *nh); + #endif