diff --git a/src/mac/LoRaMac.c b/src/mac/LoRaMac.c index 198bf67bb..4cbef9dcd 100644 --- a/src/mac/LoRaMac.c +++ b/src/mac/LoRaMac.c @@ -201,14 +201,6 @@ typedef struct sLoRaMacNvmCtx typedef struct sLoRaMacCtx { - /* - * Device IEEE EUI - */ - uint8_t* DevEui; - /* - * Join IEEE EUI - */ - uint8_t* JoinEui; /* * Length of packet in PktBuffer */ @@ -1012,7 +1004,7 @@ static void ProcessRadioRxDone( void ) PrepareRxDoneAbort( ); return; } - macCryptoStatus = LoRaMacCryptoHandleJoinAccept( JOIN_REQ, MacCtx.JoinEui, &macMsgJoinAccept ); + macCryptoStatus = LoRaMacCryptoHandleJoinAccept( JOIN_REQ, SecureElementGetJoinEui( ), &macMsgJoinAccept ); if( LORAMAC_CRYPTO_SUCCESS == macCryptoStatus ) { @@ -2350,8 +2342,8 @@ LoRaMacStatus_t SendReJoinReq( JoinReqIdentifier_t joinReqType ) macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ; MacCtx.TxMsg.Message.JoinReq.MHDR.Value = macHdr.Value; - memcpy1( MacCtx.TxMsg.Message.JoinReq.JoinEUI, MacCtx.JoinEui, LORAMAC_JOIN_EUI_FIELD_SIZE ); - memcpy1( MacCtx.TxMsg.Message.JoinReq.DevEUI, MacCtx.DevEui, LORAMAC_DEV_EUI_FIELD_SIZE ); + memcpy1( MacCtx.TxMsg.Message.JoinReq.JoinEUI, SecureElementGetJoinEui( ), LORAMAC_JOIN_EUI_FIELD_SIZE ); + memcpy1( MacCtx.TxMsg.Message.JoinReq.DevEUI, SecureElementGetDevEui( ), LORAMAC_DEV_EUI_FIELD_SIZE ); allowDelayedTx = false; @@ -4455,8 +4447,8 @@ LoRaMacStatus_t LoRaMacMlmeRequest( MlmeReq_t* mlmeRequest ) ResetMacParameters( ); - MacCtx.DevEui = mlmeRequest->Req.Join.DevEui; - MacCtx.JoinEui = mlmeRequest->Req.Join.JoinEui; + SecureElementSetDevEui( mlmeRequest->Req.Join.DevEui ); + SecureElementSetJoinEui( mlmeRequest->Req.Join.JoinEui ); MacCtx.NvmCtx->MacParams.ChannelsDatarate = RegionAlternateDr( MacCtx.NvmCtx->Region, mlmeRequest->Req.Join.Datarate, ALTERNATE_DR ); diff --git a/src/mac/secure-element.h b/src/mac/secure-element.h index c06b4e726..8eb07ee2a 100644 --- a/src/mac/secure-element.h +++ b/src/mac/secure-element.h @@ -37,6 +37,8 @@ #include #include "LoRaMacCrypto.h" +#define SE_EUI_SIZE 16 + /*! * Return values. */ @@ -166,4 +168,34 @@ SecureElementStatus_t SecureElementDeriveAndStoreKey( Version_t version, uint8_t */ SecureElementStatus_t SecureElementRandomNumber( uint32_t* randomNum ); +/*! + * Sets the DevEUI + * + * \param[IN] devEui - Pointer to the 16-byte devEUI + * \retval - Status of the operation + */ +SecureElementStatus_t SecureElementSetDevEui( uint8_t* devEui ); + +/*! + * Gets the DevEUI + * + * \retval - Pointer to the 16-byte devEUI + */ +uint8_t* SecureElementGetDevEui( void ); + +/*! + * Sets the JoinEUI + * + * \param[IN] joinEui - Pointer to the 16-byte joinEui + * \retval - Status of the operation + */ +SecureElementStatus_t SecureElementSetJoinEui( uint8_t* joinEui ); + +/*! + * Gets the DevEUI + * + * \retval - Pointer to the 16-byte joinEui + */ +uint8_t* SecureElementGetJoinEui( void ); + #endif // __SECURE_ELEMENT_H__ diff --git a/src/peripherals/soft-se/soft-se.c b/src/peripherals/soft-se/soft-se.c index 26df71e3c..d170ac06b 100644 --- a/src/peripherals/soft-se/soft-se.c +++ b/src/peripherals/soft-se/soft-se.c @@ -52,6 +52,14 @@ typedef struct sKey */ typedef struct sSecureElementNvCtx { + /* + * DevEUI storage + */ + uint8_t DevEui[SE_EUI_SIZE]; + /* + * Join EUI storage + */ + uint8_t JoinEui[SE_EUI_SIZE]; /* * AES computation context variable */ @@ -191,6 +199,9 @@ SecureElementStatus_t SecureElementInit( SecureElementNvmEvent seNvmCtxChanged ) // Set standard keys memcpy1( SeNvmCtx.KeyList[itr].KeyValue, zeroKey, KEY_SIZE ); + memset1( SeNvmCtx.DevEui, 0, SE_EUI_SIZE ); + memset1( SeNvmCtx.JoinEui, 0, SE_EUI_SIZE ); + // Assign callback if( seNvmCtxChanged != 0 ) { @@ -372,3 +383,35 @@ SecureElementStatus_t SecureElementRandomNumber( uint32_t* randomNum ) *randomNum = Radio.Random( ); return SECURE_ELEMENT_SUCCESS; } + +SecureElementStatus_t SecureElementSetDevEui( uint8_t* devEui ) +{ + if( devEui == NULL ) + { + return SECURE_ELEMENT_ERROR_NPE; + } + memcpy1( SeNvmCtx.DevEui, devEui, SE_EUI_SIZE ); + SeNvmCtxChanged( ); + return SECURE_ELEMENT_SUCCESS; +} + +uint8_t* SecureElementGetDevEui( void ) +{ + return SeNvmCtx.DevEui; +} + +SecureElementStatus_t SecureElementSetJoinEui( uint8_t* joinEui ) +{ + if( joinEui == NULL ) + { + return SECURE_ELEMENT_ERROR_NPE; + } + memcpy1( SeNvmCtx.JoinEui, joinEui, SE_EUI_SIZE ); + SeNvmCtxChanged( ); + return SECURE_ELEMENT_SUCCESS; +} + +uint8_t* SecureElementGetJoinEui( void ) +{ + return SeNvmCtx.JoinEui; +}