Skip to content

Commit 3f2d530

Browse files
committed
Changed DeriveSessionKey10x and DeriveSessionKey11x functions API
Removed pointer based variable usage.
1 parent f77b876 commit 3f2d530

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

src/mac/LoRaMacCrypto.c

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,8 @@ static LoRaMacCryptoStatus_t GetKeyAddrItem( AddressIdentifier_t addrID, KeyAddr
630630
* \param[IN] deviceNonce - Device nonce
631631
* \retval - Status of the operation
632632
*/
633-
static LoRaMacCryptoStatus_t DeriveSessionKey10x( KeyIdentifier_t keyID, uint8_t* joinNonce, uint8_t* netID, uint8_t* devNonce )
633+
static LoRaMacCryptoStatus_t DeriveSessionKey10x( KeyIdentifier_t keyID, uint32_t joinNonce, uint32_t netID, uint16_t devNonce )
634634
{
635-
if( ( joinNonce == 0 ) || ( netID == 0 ) || ( devNonce == 0 ) )
636-
{
637-
return LORAMAC_CRYPTO_ERROR_NPE;
638-
}
639-
640635
uint8_t compBase[16] = { 0 };
641636

642637
switch( keyID )
@@ -653,9 +648,16 @@ static LoRaMacCryptoStatus_t DeriveSessionKey10x( KeyIdentifier_t keyID, uint8_t
653648
return LORAMAC_CRYPTO_ERROR_INVALID_KEY_ID;
654649
}
655650

656-
memcpy1( compBase + 1, joinNonce, 3 );
657-
memcpy1( compBase + 4, netID, 3 );
658-
memcpy1( compBase + 7, devNonce, 2 );
651+
compBase[1] = ( uint8_t )( ( joinNonce >> 0 ) & 0xFF );
652+
compBase[2] = ( uint8_t )( ( joinNonce >> 8 ) & 0xFF );
653+
compBase[3] = ( uint8_t )( ( joinNonce >> 16 ) & 0xFF );
654+
655+
compBase[4] = ( uint8_t )( ( netID >> 0 ) & 0xFF );
656+
compBase[5] = ( uint8_t )( ( netID >> 8 ) & 0xFF );
657+
compBase[6] = ( uint8_t )( ( netID >> 16 ) & 0xFF );
658+
659+
compBase[7] = ( uint8_t )( ( devNonce >> 0 ) & 0xFF );
660+
compBase[8] = ( uint8_t )( ( devNonce >> 8 ) & 0xFF );
659661

660662
if( SecureElementDeriveAndStoreKey( compBase, NWK_KEY, keyID ) != SECURE_ELEMENT_SUCCESS )
661663
{
@@ -675,9 +677,9 @@ static LoRaMacCryptoStatus_t DeriveSessionKey10x( KeyIdentifier_t keyID, uint8_t
675677
* \param[IN] deviceNonce - Device nonce
676678
* \retval - Status of the operation
677679
*/
678-
static LoRaMacCryptoStatus_t DeriveSessionKey11x( KeyIdentifier_t keyID, uint8_t* joinNonce, uint8_t* joinEUI, uint8_t* devNonce )
680+
static LoRaMacCryptoStatus_t DeriveSessionKey11x( KeyIdentifier_t keyID, uint32_t joinNonce, uint8_t* joinEUI, uint16_t devNonce )
679681
{
680-
if( ( joinNonce == 0 ) || ( joinEUI == 0 ) || ( devNonce == 0 ) )
682+
if( joinEUI == 0 )
681683
{
682684
return LORAMAC_CRYPTO_ERROR_NPE;
683685
}
@@ -704,9 +706,14 @@ static LoRaMacCryptoStatus_t DeriveSessionKey11x( KeyIdentifier_t keyID, uint8_t
704706
return LORAMAC_CRYPTO_ERROR_INVALID_KEY_ID;
705707
}
706708

707-
memcpy1( compBase + 1, joinNonce, 3 );
709+
compBase[1] = ( uint8_t )( ( joinNonce >> 0 ) & 0xFF );
710+
compBase[2] = ( uint8_t )( ( joinNonce >> 8 ) & 0xFF );
711+
compBase[3] = ( uint8_t )( ( joinNonce >> 16 ) & 0xFF );
712+
708713
memcpyr( compBase + 4, joinEUI, 8 );
709-
memcpy1( compBase + 12, devNonce, 2 );
714+
715+
compBase[12] = ( uint8_t )( ( devNonce >> 0 ) & 0xFF );
716+
compBase[13] = ( uint8_t )( ( devNonce >> 8 ) & 0xFF );
710717

711718
if( SecureElementDeriveAndStoreKey( compBase, rootKeyId, keyID ) != SECURE_ELEMENT_SUCCESS )
712719
{
@@ -1235,7 +1242,7 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
12351242
LoRaMacCryptoStatus_t retval = LORAMAC_CRYPTO_ERROR;
12361243
uint8_t decJoinAccept[LORAMAC_JOIN_ACCEPT_FRAME_MAX_SIZE] = { 0 };
12371244
uint8_t versionMinor = 0;
1238-
uint16_t* nonce = &CryptoCtx.NvmCtx->DevNonce;
1245+
uint16_t nonce = CryptoCtx.NvmCtx->DevNonce;
12391246

12401247
// Nonce selection depending on JoinReqType
12411248
// JOIN_REQ : CryptoCtx.NvmCtx->DevNonce
@@ -1252,16 +1259,16 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
12521259
// If Join-accept is a reply to a rejoin, the RJcount(0 or 1) replaces DevNonce in the key derivation process.
12531260
if( ( joinReqType == REJOIN_REQ_0 ) || ( joinReqType == REJOIN_REQ_2 ) )
12541261
{
1255-
nonce = ( uint8_t* )&CryptoCtx.RJcount0;
1262+
nonce = CryptoCtx.RJcount0;
12561263
}
12571264
else
12581265
{
1259-
nonce = ( uint8_t* )&CryptoCtx.NvmCtx->FCntList.RJcount1;
1266+
nonce = CryptoCtx.NvmCtx->FCntList.RJcount1;
12601267
}
12611268
}
12621269
#endif
12631270

1264-
if( SecureElementProcessJoinAccept( joinReqType, joinEUI, *nonce, macMsg->Buffer,
1271+
if( SecureElementProcessJoinAccept( joinReqType, joinEUI, nonce, macMsg->Buffer,
12651272
macMsg->BufSize, decJoinAccept,
12661273
&versionMinor ) != SECURE_ELEMENT_SUCCESS )
12671274
{
@@ -1276,15 +1283,19 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
12761283
return LORAMAC_CRYPTO_ERROR_PARSER;
12771284
}
12781285

1279-
#if( USE_JOIN_NONCE_COUNTER_CHECK == 1 )
1280-
// Check if the JoinNonce is greater as the previous one
1281-
uint32_t currentJoinNonce = 0;
1286+
uint32_t currentJoinNonce;
12821287

12831288
currentJoinNonce = ( uint32_t )macMsg->JoinNonce[0];
12841289
currentJoinNonce |= ( ( uint32_t )macMsg->JoinNonce[1] << 8 );
12851290
currentJoinNonce |= ( ( uint32_t )macMsg->JoinNonce[2] << 16 );
12861291

1292+
#if( USE_JOIN_NONCE_COUNTER_CHECK == 1 )
1293+
// Check if the JoinNonce is greater as the previous one
12871294
if( currentJoinNonce > CryptoCtx.NvmCtx->JoinNonce )
1295+
#else
1296+
// Check if the JoinNonce is different from the previous one
1297+
if( currentJoinNonce != CryptoCtx.NvmCtx->JoinNonce )
1298+
#endif
12881299
{
12891300
CryptoCtx.NvmCtx->JoinNonce = currentJoinNonce;
12901301
CryptoCtx.EventCryptoNvmCtxChanged( );
@@ -1293,7 +1304,6 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
12931304
{
12941305
return LORAMAC_CRYPTO_FAIL_JOIN_NONCE;
12951306
}
1296-
#endif
12971307

12981308
// Derive lifetime keys
12991309
retval = LoRaMacCryptoDeriveMcRootKey( versionMinor, APP_KEY );
@@ -1313,25 +1323,25 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
13131323
{
13141324
// Operating in LoRaWAN 1.1.x mode
13151325

1316-
retval = DeriveSessionKey11x( F_NWK_S_INT_KEY, macMsg->JoinNonce, joinEUI, nonce );
1326+
retval = DeriveSessionKey11x( F_NWK_S_INT_KEY, currentJoinNonce, joinEUI, nonce );
13171327
if( retval != LORAMAC_CRYPTO_SUCCESS )
13181328
{
13191329
return retval;
13201330
}
13211331

1322-
retval = DeriveSessionKey11x( S_NWK_S_INT_KEY, macMsg->JoinNonce, joinEUI, nonce );
1332+
retval = DeriveSessionKey11x( S_NWK_S_INT_KEY, currentJoinNonce, joinEUI, nonce );
13231333
if( retval != LORAMAC_CRYPTO_SUCCESS )
13241334
{
13251335
return retval;
13261336
}
13271337

1328-
retval = DeriveSessionKey11x( NWK_S_ENC_KEY, macMsg->JoinNonce, joinEUI, nonce );
1338+
retval = DeriveSessionKey11x( NWK_S_ENC_KEY, currentJoinNonce, joinEUI, nonce );
13291339
if( retval != LORAMAC_CRYPTO_SUCCESS )
13301340
{
13311341
return retval;
13321342
}
13331343

1334-
retval = DeriveSessionKey11x( APP_S_KEY, macMsg->JoinNonce, joinEUI, nonce );
1344+
retval = DeriveSessionKey11x( APP_S_KEY, currentJoinNonce, joinEUI, nonce );
13351345
if( retval != LORAMAC_CRYPTO_SUCCESS )
13361346
{
13371347
return retval;
@@ -1342,25 +1352,31 @@ LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReq
13421352
{
13431353
// Operating in LoRaWAN 1.0.x mode
13441354

1345-
retval = DeriveSessionKey10x( APP_S_KEY, macMsg->JoinNonce, macMsg->NetID, ( uint8_t* )&CryptoCtx.NvmCtx->DevNonce );
1355+
uint32_t netID;
1356+
1357+
netID = ( uint32_t )macMsg->NetID[0];
1358+
netID |= ( ( uint32_t )macMsg->NetID[1] << 8 );
1359+
netID |= ( ( uint32_t )macMsg->NetID[2] << 16 );
1360+
1361+
retval = DeriveSessionKey10x( APP_S_KEY, currentJoinNonce, netID, nonce );
13461362
if( retval != LORAMAC_CRYPTO_SUCCESS )
13471363
{
13481364
return retval;
13491365
}
13501366

1351-
retval = DeriveSessionKey10x( NWK_S_ENC_KEY, macMsg->JoinNonce, macMsg->NetID, ( uint8_t* )&CryptoCtx.NvmCtx->DevNonce );
1367+
retval = DeriveSessionKey10x( NWK_S_ENC_KEY, currentJoinNonce, netID, nonce );
13521368
if( retval != LORAMAC_CRYPTO_SUCCESS )
13531369
{
13541370
return retval;
13551371
}
13561372

1357-
retval = DeriveSessionKey10x( F_NWK_S_INT_KEY, macMsg->JoinNonce, macMsg->NetID, ( uint8_t* )&CryptoCtx.NvmCtx->DevNonce );
1373+
retval = DeriveSessionKey10x( F_NWK_S_INT_KEY, currentJoinNonce, netID, nonce );
13581374
if( retval != LORAMAC_CRYPTO_SUCCESS )
13591375
{
13601376
return retval;
13611377
}
13621378

1363-
retval = DeriveSessionKey10x( S_NWK_S_INT_KEY, macMsg->JoinNonce, macMsg->NetID, ( uint8_t* )&CryptoCtx.NvmCtx->DevNonce );
1379+
retval = DeriveSessionKey10x( S_NWK_S_INT_KEY, currentJoinNonce, netID, nonce );
13641380
if( retval != LORAMAC_CRYPTO_SUCCESS )
13651381
{
13661382
return retval;

0 commit comments

Comments
 (0)