Skip to content

Commit 9e27cb8

Browse files
committed
LMIC should init secure element; add missing APIs
1 parent 6e75e1a commit 9e27cb8

File tree

4 files changed

+173
-9
lines changed

4 files changed

+173
-9
lines changed

src/lmic/lmic.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2728,8 +2728,24 @@ void LMIC_reset (void) {
27282728
LMIC.netDeviceTime = 0; // the "invalid" time.
27292729
LMIC.netDeviceTimeFrac = 0;
27302730
#endif // LMIC_ENABLE_DeviceTimeReq
2731-
}
27322731

2732+
// finally, initialize the secure element.
2733+
// As a temporary measure for testing, we grab the keys from the client using `os_getDevKey()`
2734+
// and so forth. This needs rework for ABP and really isn't right for the long run,
2735+
// but OTAA will work.
2736+
if (LMIC_SecureElement_initialize() == LMIC_SecureElement_Error_OK) {
2737+
// send down the keys:
2738+
LMIC_SecureElement_Aes128Key_t appKey;
2739+
os_getDevKey(appKey.bytes);
2740+
LMIC_SecureElement_setAppKey(&appKey);
2741+
2742+
LMIC_SecureElement_EUI_t devEui, appEui;
2743+
os_getDevEui(devEui.bytes);
2744+
os_getArtEui(appEui.bytes);
2745+
LMIC_SecureElement_setDevEUI(&devEui);
2746+
LMIC_SecureElement_setAppEUI(&appEui);
2747+
}
2748+
}
27332749

27342750
void LMIC_init (void) {
27352751
LMIC.opmode = OP_SHUTDOWN;

src/se/drivers/default/lmic_se_default.c

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,31 @@
3737

3838
#include "../../../lmic/lmic.h"
3939

40+
static LMIC_SecureElement_EUI_t s_devEUI;
41+
static LMIC_SecureElement_EUI_t s_appEUI;
42+
static LMIC_SecureElement_Aes128Key_t s_appKey;
43+
4044
static LMIC_SecureElement_Aes128Key_t s_nwkSKey;
4145
static LMIC_SecureElement_Aes128Key_t s_appSKey;
4246

47+
static inline
48+
uint8_t *
49+
LMIC_SecureElement_DefaultI_GetAppEUIRef(void) {
50+
return s_appEUI.bytes;
51+
}
52+
53+
static inline
54+
uint8_t *
55+
LMIC_SecureElement_DefaultI_GetDevEUIRef(void) {
56+
return s_devEUI.bytes;
57+
}
58+
59+
static inline
60+
uint8_t *
61+
LMIC_SecureElement_DefaultI_GetAppKeyRef(void) {
62+
return s_appKey.bytes;
63+
}
64+
4365
static inline
4466
uint8_t *
4567
LMIC_SecureElement_DefaultI_GetNwkSKeyRef(void) {
@@ -52,6 +74,12 @@ LMIC_SecureElement_DefaultI_GetAppSKeyRef(void) {
5274
return s_appSKey.bytes;
5375
}
5476

77+
78+
static void appKeyToAESkey(void) {
79+
memcpy(AESkey, s_appKey.bytes, sizeof(s_appKey.bytes));
80+
}
81+
82+
5583
/*!
5684
5785
\copydoc LMIC_SecureElement_initialize_t
@@ -69,6 +97,51 @@ LMIC_SecureElement_Default_initialize(void) {
6997
return LMIC_SecureElement_Error_OK;
7098
}
7199

100+
/*!
101+
102+
\copydoc LMIC_SecureElement_setDevEUI_t
103+
104+
\par "Implementation Notes"
105+
In the default secure element, the device EUI is stored in a static variable without obfuscation.
106+
107+
*/
108+
109+
LMIC_SecureElement_Error_t
110+
LMIC_SecureElement_Default_setDevEUI(const LMIC_SecureElement_EUI_t *pDevEUI) {
111+
s_devEUI = *pDevEUI;
112+
return LMIC_SecureElement_Error_OK;
113+
}
114+
115+
/*!
116+
117+
\copydoc LMIC_SecureElement_setAppEUI_t
118+
119+
\par "Implementation Notes"
120+
In the default secure element, the app EUI is stored in a static variable without obfuscation.
121+
122+
*/
123+
124+
LMIC_SecureElement_Error_t
125+
LMIC_SecureElement_Default_setAppEUI(const LMIC_SecureElement_EUI_t *pAppEUI) {
126+
s_appEUI = *pAppEUI;
127+
return LMIC_SecureElement_Error_OK;
128+
}
129+
130+
/*!
131+
132+
\copydoc LMIC_SecureElement_setAppKey_t
133+
134+
\par "Implementation Notes"
135+
In the default secure element, the appkey is stored in a static variable without obfuscation.
136+
137+
*/
138+
139+
LMIC_SecureElement_Error_t
140+
LMIC_SecureElement_Default_setAppKey(const LMIC_SecureElement_Aes128Key_t *pAppKey) {
141+
s_appKey = *pAppKey;
142+
return LMIC_SecureElement_Error_OK;
143+
}
144+
72145
// ================================================================================
73146
// BEG AES
74147

@@ -259,7 +332,7 @@ LMIC_SecureElement_Default_decodeMessage(
259332

260333

261334
static void aes_appendMic0 (xref2u1_t pdu, int len) {
262-
os_getDevKey(AESkey);
335+
appKeyToAESkey();
263336
os_wmsbf4(pdu+len, os_aes(AES_MIC|AES_MICNOAUX, pdu, len)); // MSB because of internal structure of AES
264337
}
265338

@@ -286,8 +359,8 @@ LMIC_SecureElement_Default_createJoinRequest(
286359

287360
xref2u1_t d = pJoinRequestBytes;
288361
d[OFF_JR_HDR] = ftype;
289-
os_getArtEui(d + OFF_JR_ARTEUI);
290-
os_getDevEui(d + OFF_JR_DEVEUI);
362+
memcpy(d + OFF_JR_ARTEUI, s_appEUI.bytes, sizeof(s_appEUI.bytes));
363+
memcpy(d + OFF_JR_DEVEUI, s_devEUI.bytes, sizeof(s_devEUI.bytes));
291364
os_wlsbf2(d + OFF_JR_DEVNONCE, LMIC.devNonce);
292365
aes_appendMic0(d, OFF_JR_MIC);
293366

@@ -296,16 +369,15 @@ LMIC_SecureElement_Default_createJoinRequest(
296369
return LMIC_SecureElement_Error_OK;
297370
}
298371

299-
300372
static int aes_verifyMic0 (xref2u1_t pdu, int len) {
301-
os_getDevKey(AESkey);
373+
appKeyToAESkey();
302374
return os_aes(AES_MIC|AES_MICNOAUX, pdu, len) == os_rmsbf4(pdu+len);
303375
}
304376

305377

306378

307379
static void aes_encrypt (xref2u1_t pdu, int len) {
308-
os_getDevKey(AESkey);
380+
appKeyToAESkey();
309381
os_aes(AES_ENC, pdu, len);
310382
}
311383

@@ -319,9 +391,9 @@ static void aes_sessKeys (u2_t devnonce, xref2cu1_t artnonce, xref2u1_t nwkkey,
319391
os_copyMem(artkey, nwkkey, 16);
320392
artkey[0] = 0x02;
321393

322-
os_getDevKey(AESkey);
394+
appKeyToAESkey();
323395
os_aes(AES_ENC, nwkkey, 16);
324-
os_getDevKey(AESkey);
396+
appKeyToAESkey();
325397
os_aes(AES_ENC, artkey, 16);
326398
}
327399

src/se/i/lmic_secure_element_api.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ LMIC_SecureElement_getRandomU2_t LMIC_SecureElement_getRandomU2;
8383
LMIC_SecureElement_fillRandomBuffer_t LMIC_SecureElement_fillRandomBuffer;
8484
LMIC_SecureElement_setAppKey_t LMIC_SecureElement_setAppKey;
8585
LMIC_SecureElement_getAppKey_t LMIC_SecureElement_getAppKey;
86+
LMIC_SecureElement_setAppEUI_t LMIC_SecureElement_setAppEUI;
87+
LMIC_SecureElement_getAppEUI_t LMIC_SecureElement_getAppEUI;
88+
LMIC_SecureElement_setDevEUI_t LMIC_SecureElement_setDevEUI;
89+
LMIC_SecureElement_getDevEUI_t LMIC_SecureElement_getDevEUI;
8690
LMIC_SecureElement_setNwkSKey_t LMIC_SecureElement_setNwkSKey;
8791
LMIC_SecureElement_getNwkSKey_t LMIC_SecureElement_getNwkSKey;
8892
LMIC_SecureElement_setAppSKey_t LMIC_SecureElement_setAppSKey;
@@ -200,6 +204,34 @@ LMIC_SecureElement_getAppKey(LMIC_SecureElement_Aes128Key_t *pAppKey) {
200204
return LMIC_SecureElement_METHOD(LMIC_CFG_SecureElement_DRIVER, getAppKey)(pAppKey);
201205
}
202206

207+
/// \copydoc LMIC_SecureElement_setAppEUI_t
208+
static inline
209+
LMIC_SecureElement_Error_t LMIC_ABI_STD
210+
LMIC_SecureElement_setAppEUI(const LMIC_SecureElement_EUI_t *pAppEUI) {
211+
return LMIC_SecureElement_METHOD(LMIC_CFG_SecureElement_DRIVER, setAppEUI)(pAppEUI);
212+
}
213+
214+
/// \copydoc LMIC_SecureElement_getAppEUI_t
215+
static inline
216+
LMIC_SecureElement_Error_t LMIC_ABI_STD
217+
LMIC_SecureElement_getAppEUI(LMIC_SecureElement_EUI_t *pAppEUI) {
218+
return LMIC_SecureElement_METHOD(LMIC_CFG_SecureElement_DRIVER, getAppEUI)(pAppEUI);
219+
}
220+
221+
/// \copydoc LMIC_SecureElement_setDevEUI_t
222+
static inline
223+
LMIC_SecureElement_Error_t LMIC_ABI_STD
224+
LMIC_SecureElement_setDevEUI(const LMIC_SecureElement_EUI_t *pDevEUI) {
225+
return LMIC_SecureElement_METHOD(LMIC_CFG_SecureElement_DRIVER, setDevEUI)(pDevEUI);
226+
}
227+
228+
/// \copydoc LMIC_SecureElement_getDevEUI_t
229+
static inline
230+
LMIC_SecureElement_Error_t LMIC_ABI_STD
231+
LMIC_SecureElement_getDevEUI(LMIC_SecureElement_EUI_t *pDevEUI) {
232+
return LMIC_SecureElement_METHOD(LMIC_CFG_SecureElement_DRIVER, getDevEUI)(pDevEUI);
233+
}
234+
203235
/// \copydoc LMIC_SecureElement_setNwkSKey_t
204236
static inline
205237
LMIC_SecureElement_Error_t LMIC_ABI_STD

src/se/i/lmic_secure_element_interface.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,46 @@ LMIC_SecureElement_setAppSKey_t(const LMIC_SecureElement_Aes128Key_t *pAppSKey,
280280
typedef LMIC_SecureElement_Error_t LMIC_ABI_STD
281281
LMIC_SecureElement_getAppSKey_t(LMIC_SecureElement_Aes128Key_t *pAppSKey, LMIC_SecureElement_KeySelector_t iKey);
282282

283+
/// \brief Get device EUI.
284+
///
285+
/// \param pDevEUI [out] device EUI.
286+
///
287+
/// \returns \ref LMIC_SecureElement_Error_OK for success, some other code for failure.
288+
/// Many secure elements will fail this request, because their purpose in life
289+
/// is guarding keys and preventing tampering.
290+
///
291+
typedef LMIC_SecureElement_Error_t LMIC_ABI_STD
292+
LMIC_SecureElement_getDevEUI_t(LMIC_SecureElement_EUI_t *pDevEUI);
293+
294+
/// \brief Set device EUI.
295+
///
296+
/// \param pDevEUI [in] Device key for this secure element.
297+
///
298+
/// \returns \ref LMIC_SecureElement_Error_OK for success, some other code for failure.
299+
///
300+
typedef LMIC_SecureElement_Error_t LMIC_ABI_STD
301+
LMIC_SecureElement_setDevEUI_t(const LMIC_SecureElement_EUI_t *pDevEUI);
302+
303+
/// \brief Get application EUI.
304+
///
305+
/// \param pAppEUI [out] application EUI for this secure element.
306+
///
307+
/// \returns \ref LMIC_SecureElement_Error_OK for success, some other code for failure.
308+
/// Many secure elements will fail this request, because their purpose in life
309+
/// is guarding keys and preventing tampering.
310+
///
311+
typedef LMIC_SecureElement_Error_t LMIC_ABI_STD
312+
LMIC_SecureElement_getAppEUI_t(LMIC_SecureElement_EUI_t *pAppEUI);
313+
314+
/// \brief Set application EUI.
315+
///
316+
/// \param pAppEUI [in] application EUI for this secure element.
317+
///
318+
/// \returns \ref LMIC_SecureElement_Error_OK for success, some other code for failure.
319+
///
320+
typedef LMIC_SecureElement_Error_t LMIC_ABI_STD
321+
LMIC_SecureElement_setAppEUI_t(const LMIC_SecureElement_EUI_t *pAppEUI);
322+
283323
/// \brief Create a join request.
284324
///
285325
/// \param pJoinRequestBytes [out] Buffer to be filled with the join request.
@@ -461,6 +501,10 @@ LMIC_SecureElement_aes128Encrypt_t(const uint8_t *pKey, const uint8_t *pInput, u
461501
LMIC_SecureElement_fillRandomBuffer_t LMIC_SecureElement_##a_driver##_fillRandomBuffer; \
462502
LMIC_SecureElement_setAppKey_t LMIC_SecureElement_##a_driver##_setAppKey; \
463503
LMIC_SecureElement_getAppKey_t LMIC_SecureElement_##a_driver##_getAppKey; \
504+
LMIC_SecureElement_setAppEUI_t LMIC_SecureElement_##a_driver##_setAppEUI; \
505+
LMIC_SecureElement_getAppEUI_t LMIC_SecureElement_##a_driver##_getAppEUI; \
506+
LMIC_SecureElement_setDevEUI_t LMIC_SecureElement_##a_driver##_setDevEUI; \
507+
LMIC_SecureElement_getDevEUI_t LMIC_SecureElement_##a_driver##_getDevEUI; \
464508
LMIC_SecureElement_setNwkSKey_t LMIC_SecureElement_##a_driver##_setNwkSKey; \
465509
LMIC_SecureElement_getNwkSKey_t LMIC_SecureElement_##a_driver##_getNwkSKey; \
466510
LMIC_SecureElement_setAppSKey_t LMIC_SecureElement_##a_driver##_setAppSKey; \

0 commit comments

Comments
 (0)