From d06790fcf2ff7ec048b39071a6b04140cd55d99e Mon Sep 17 00:00:00 2001 From: Guido Casati Date: Tue, 29 Apr 2025 22:50:04 +0200 Subject: [PATCH] Move derive_kgnb to key_nas_deriver lib --- openair3/NAS/NR_UE/nr_nas_msg.c | 36 +-------------------------------- openair3/SECU/key_nas_deriver.c | 32 +++++++++++++++++++++++++++++ openair3/SECU/key_nas_deriver.h | 2 ++ 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/openair3/NAS/NR_UE/nr_nas_msg.c b/openair3/NAS/NR_UE/nr_nas_msg.c index d152a5d4a3..4c5d6fb717 100644 --- a/openair3/NAS/NR_UE/nr_nas_msg.c +++ b/openair3/NAS/NR_UE/nr_nas_msg.c @@ -71,6 +71,7 @@ #include "fgmm_service_accept.h" #include "fgmm_service_reject.h" #include "ds/byte_array.h" +#include "key_nas_deriver.h" #define MAX_NAS_UE 4 @@ -444,41 +445,6 @@ void derive_knas(algorithm_type_dist_t nas_alg_type, uint8_t nas_alg_id, uint8_t memcpy(knas, out + 16, 16); } -static void derive_kgnb(uint8_t kamf[32], uint32_t count, uint8_t *kgnb) -{ - /* Compute the KDF input parameter - * S = FC(0x6E) || UL NAS Count || 0x00 0x04 || 0x01 || 0x00 0x01 - */ - uint8_t input[32] = {0}; - // uint16_t length = 4; - // int offset = 0; - - LOG_TRACE(INFO, "%s with count= %d", __FUNCTION__, count); - memset(input, 0, 32); - input[0] = 0x6E; - // P0 - input[1] = count >> 24; - input[2] = (uint8_t)(count >> 16); - input[3] = (uint8_t)(count >> 8); - input[4] = (uint8_t)count; - // L0 - input[5] = 0; - input[6] = 4; - // P1 - input[7] = 0x01; - // L1 - input[8] = 0; - input[9] = 1; - - 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"); -} - static void derive_ue_keys(uint8_t *buf, nr_ue_nas_t *nas) { uint8_t ak[6]; diff --git a/openair3/SECU/key_nas_deriver.c b/openair3/SECU/key_nas_deriver.c index 1011eeb357..ec6da5e411 100644 --- a/openair3/SECU/key_nas_deriver.c +++ b/openair3/SECU/key_nas_deriver.c @@ -139,6 +139,38 @@ void nr_derive_key(algorithm_type_dist_t alg_type, uint8_t alg_id, const uint8_t * NOTE: knas is dynamically allocated by the KDF function */ +/** @brief KgNB derivation (A.9 3GPP TS 33.501) */ +void derive_kgnb(uint8_t kamf[32], uint32_t count, uint8_t *kgnb) +{ + /* Compute the KDF input parameter + * S = FC(0x6E) || UL NAS Count || 0x00 0x04 || 0x01 || 0x00 0x01 + */ + uint8_t input[32] = {0}; + + input[0] = 0x6E; + // P0 + input[1] = count >> 24; + input[2] = (uint8_t)(count >> 16); + input[3] = (uint8_t)(count >> 8); + input[4] = (uint8_t)count; + // L0 + input[5] = 0; + input[6] = 4; + // P1 + input[7] = 0x01; + // L1 + input[8] = 0; + input[9] = 1; + + 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"); +} + void derive_keNB(const uint8_t kasme[32], const uint32_t nas_count, uint8_t *keNB) { uint8_t s[7] = {0}; diff --git a/openair3/SECU/key_nas_deriver.h b/openair3/SECU/key_nas_deriver.h index 4e84c5f3be..99c5b20b51 100644 --- a/openair3/SECU/key_nas_deriver.h +++ b/openair3/SECU/key_nas_deriver.h @@ -14,6 +14,8 @@ typedef enum { UP_INT_ALG = 0x06 } algorithm_type_dist_t; +void derive_kgnb(uint8_t kamf[32], uint32_t count, uint8_t *kgnb); + void derive_keNB(const uint8_t kasme[32], const uint32_t nas_count, uint8_t *keNB); void derive_keNB_star(const uint8_t *kenb_32,