From 693364ddc5a7724a45ba65b17426a30c1e3eca38 Mon Sep 17 00:00:00 2001
From: Laurent <laurent.thomas@open-cells.com>
Date: Sat, 29 Aug 2020 09:37:15 +0200
Subject: [PATCH] merge develop

---
 executables/nr-uesoftmodem.c        |  7 ++---
 openair3/NAS/NR_UE/ue_process_nas.c | 42 ++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/executables/nr-uesoftmodem.c b/executables/nr-uesoftmodem.c
index 6e243ab13d..7af04ffd45 100644
--- a/executables/nr-uesoftmodem.c
+++ b/executables/nr-uesoftmodem.c
@@ -44,6 +44,7 @@
 #include "SCHED/sched_common_vars.h"
 #include "PHY/MODULATION/modulation_vars.h"
 //#include "../../SIMU/USER/init_lte.h"
+#include "PHY/NR_REFSIG/nr_mod_table.h"
 
 #include "LAYER2/MAC/mac_vars.h"
 #include "RRC/LTE/rrc_vars.h"
@@ -764,11 +765,11 @@ int main( int argc, char **argv ) {
   nr_user_nas_t UErrc={0};
   NRUEcontext_t UEnas={0};
   int size=identityRequest((void**)&resp, &UEnas); 
-  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   identity Request:\n" );;
+  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   identity Request:\n" );
   size=identityResponse((void**)&resp, &UErrc); 
-  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   identity Response:\n" );;
+  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   identity Response:\n" );
   size=authenticationRequest((void**)&resp, &UEnas); 
-  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   authentication request:\n" );;
+  log_dump(NAS, resp, size, LOG_DUMP_CHAR,"   authentication request:\n" );
  
   if(IS_SOFTMODEM_DOFORMS) { 
     load_softscope("nr",PHY_vars_UE_g[0][0]);
diff --git a/openair3/NAS/NR_UE/ue_process_nas.c b/openair3/NAS/NR_UE/ue_process_nas.c
index 4d8d6741d0..88ab9456d3 100644
--- a/openair3/NAS/NR_UE/ue_process_nas.c
+++ b/openair3/NAS/NR_UE/ue_process_nas.c
@@ -2,6 +2,41 @@
 #include <openair3/NAS/NR_UE/nr_user_def.h>
 #include <openair3/NAS/COMMON/milenage.h>
 
+void servingNetworkName(uint8_t * msg,nr_user_nas_t *UE) {
+  //SNN-network-identifier in TS 24.501
+  // TS 24.501: If the MNC of the serving PLMN has two digits, then a zero is added at the beginning.
+  const char * format="5G:mnc000.mcc000.3gppnetwork.org";
+  memcpy(msg,format, strlen(format));
+  if (UE->uicc->nmc_size == 2) 
+    memcpy(msg+7, UE->uicc->imsiStr+3, 2);
+  else 
+    memcpy(msg+6, UE->uicc->imsiStr+3, 3);
+  memcpy(msg+13, UE->uicc->imsiStr, 3);
+}
+
+xresToxresStar(uint8_t * msg),nr_user_nas_t *UE {
+  // TS 33.220  annex B.2 => FC=0x6B in TS 33.501 annex A.4
+  //input S to KDF
+  uint8_t S[64]={0};
+  S[0]=0x6B;
+  servingNetworkName(S+1, UE);
+  *(uint16_t*)(msg+strlen(msg))=htons(strlen(msg));
+  msg+=strlen(msg)+sizeof(uint16_t);
+  // add rand
+  memcpy(msg, UE->uicc->rand, sizeof(UE->uicc->rand) ) ;
+  *(uint16_t*)(msg+sizeof(UE->uicc->rand))=htons(sizeof(UE->uicc->rand));
+  msg+=sizeof(UE->uicc->rand)+sizeof(uint16_t);
+  msg+=strlen(msg)+sizeof(uint16_t);
+  // add xres
+  memcpy(msg, UE->uicc->xres, sizeof(UE->uicc->xres) ) ;
+  *(uint16_t*)(msg+sizeof(UE->uicc->xres))=htons(sizeof(UE->uicc->xres));
+  msg+=sizeof(UE->uicc->xres)+sizeof(uint16_t);
+  // S is done
+
+  
+  
+}
+
 void SGSabortNet(void *msg, nr_user_nas_t *UE) {
 }
 
@@ -16,6 +51,9 @@ void SGSauthenticationReq(void *msg, nr_user_nas_t *UE) {
   authenticationrequestHeader_t *amsg=(authenticationrequestHeader_t *) msg;
   arrayCpy(UE->uicc->rand,amsg->RAND);
   arrayCpy(UE->uicc->autn,amsg->AUTN);
+  // AUTHENTICATION REQUEST message that contains a valid ngKSI, SQN and MAC is received
+  // TBD verify ngKSI (we set it as '2', see gNB code)
+  // SQN and MAC are tested in auth resp processing
   nas_schedule();
 }
 
@@ -122,14 +160,16 @@ int authenticationResponse(void **msg,nr_user_nas_t *UE) {
   resp->sh=0;
   resp->mt=Authenticationresponse;
   resp->iei=IEI_AuthenticationResponse;
-  resp->RESlen=sizeof(resp->RES);
+  resp->RESlen=sizeof(resp->RES); // always 16 see TS 24.501 Table聽8.2.2.1.1
   // Verify the AUTN
   uint8_t ik[16], ck[16], res[8], AUTN[16];
   milenage_generate(UE->uicc->opc, UE->uicc->amf, UE->uicc->key,
                     UE->uicc->sqn, UE->uicc->rand, AUTN, ik, ck, res);
+
   
   if ( memcmp(UE->uicc->autn, AUTN, sizeof(AUTN))  ) {
     // prepare and send good answer
+    
   } else {
     // prepare and send autn is not compatible with us
   }
-- 
2.26.2