-
Notifications
You must be signed in to change notification settings - Fork 8
/
ldl_sm.c
177 lines (144 loc) · 4.82 KB
/
ldl_sm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright (c) 2019-2020 Cameron Harper
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* */
#include "ldl_sm.h"
#include "ldl_ops.h"
#include "ldl_aes.h"
#include "ldl_cmac.h"
#include "ldl_ctr.h"
#include "ldl_debug.h"
#include "ldl_internal.h"
#include <string.h>
static void *getKey(struct ldl_sm *self, enum ldl_sm_key desc);
static const struct ldl_sm_interface interface = {
.update_session_key = LDL_SM_updateSessionKey,
.mic = LDL_SM_mic,
.ecb = LDL_SM_ecb,
.ctr = LDL_SM_ctr
};
/* functions **********************************************************/
#if defined(LDL_ENABLE_L2_1_0_3) || defined(LDL_ENABLE_L2_1_0_4)
/* LoRaWAN 1.0.x */
void LDL_SM_init(struct ldl_sm *self, const void *appKey)
{
(void)memset(self, 0, sizeof(*self));
if(appKey != NULL){
/* not a mistake, internally we call this LDL_SM_KEY_NWK */
(void)memcpy(getKey(self, LDL_SM_KEY_NWK), appKey, LDL_KEY_SIZE);
}
}
#endif
#if defined(LDL_ENABLE_L2_1_1)
/* LoRaWAN 1.1 */
void LDL_SM_init(struct ldl_sm *self, const void *appKey, const void *nwkKey)
{
(void)memset(self, 0, sizeof(*self));
if(appKey != NULL){
(void)memcpy(getKey(self, LDL_SM_KEY_APP), appKey, LDL_KEY_SIZE);
}
if(nwkKey != NULL){
(void)memcpy(getKey(self, LDL_SM_KEY_NWK), nwkKey, LDL_KEY_SIZE);
}
}
#endif
const struct ldl_sm_interface *LDL_SM_getInterface(void)
{
return &interface;
}
void LDL_SM_updateSessionKey(struct ldl_sm *self, enum ldl_sm_key keyDesc, enum ldl_sm_key rootDesc, const void *iv)
{
struct ldl_aes_ctx ctx;
switch(keyDesc){
case LDL_SM_KEY_FNWKSINT:
case LDL_SM_KEY_APPS:
case LDL_SM_KEY_SNWKSINT:
case LDL_SM_KEY_NWKSENC:
case LDL_SM_KEY_JSINT:
case LDL_SM_KEY_JSENC:
LDL_AES_init(&ctx, getKey(self, rootDesc));
(void)memcpy(getKey(self, keyDesc), iv, LDL_KEY_SIZE);
LDL_AES_encrypt(&ctx, getKey(self, keyDesc));
break;
default:
/* not a session key*/
break;
}
}
uint32_t LDL_SM_mic(struct ldl_sm *self, enum ldl_sm_key desc, const void *hdr, uint8_t hdrLen, const void *data, uint8_t dataLen)
{
uint32_t retval;
uint8_t mic[sizeof(retval)];
struct ldl_aes_ctx aes_ctx;
struct ldl_cmac_ctx ctx;
LDL_AES_init(&aes_ctx, getKey(self, desc));
LDL_CMAC_init(&ctx, &aes_ctx);
LDL_CMAC_update(&ctx, hdr, hdrLen);
LDL_CMAC_update(&ctx, data, dataLen);
LDL_CMAC_finish(&ctx, &mic, U8(sizeof(mic)));
/* intepret the 4th byte as most significant */
retval = mic[3];
retval <<= 8;
retval |= mic[2];
retval <<= 8;
retval |= mic[1];
retval <<= 8;
retval |= mic[0];
/* LoRaWAN will encode this least significant byte first */
return retval;
}
void LDL_SM_ecb(struct ldl_sm *self, enum ldl_sm_key desc, void *b)
{
struct ldl_aes_ctx ctx;
LDL_AES_init(&ctx, getKey(self, desc));
LDL_AES_encrypt(&ctx, b);
}
void LDL_SM_ctr(struct ldl_sm *self, enum ldl_sm_key desc, const void *iv, void *data, uint8_t len)
{
struct ldl_aes_ctx ctx;
LDL_AES_init(&ctx, getKey(self, desc));
LDL_CTR_encrypt(&ctx, iv, data, data, len);
}
/* static functions ***************************************************/
static void *getKey(struct ldl_sm *self, enum ldl_sm_key desc)
{
size_t i = (size_t)desc;
#if defined(LDL_ENABLE_L2_1_0_3) || defined(LDL_ENABLE_L2_1_0_4)
/* map 1.1.x key set to 1.0.x key set */
switch(desc){
case LDL_SM_KEY_APP:
case LDL_SM_KEY_NWK:
i = 0;
break;
case LDL_SM_KEY_FNWKSINT:
case LDL_SM_KEY_SNWKSINT:
case LDL_SM_KEY_NWKSENC:
case LDL_SM_KEY_JSINT:
case LDL_SM_KEY_JSENC:
i = 1;
break;
case LDL_SM_KEY_APPS:
default:
i = 2;
break;
}
#endif
LDL_PEDANTIC(i < sizeof(self->keys)/sizeof(*self->keys))
return self->keys[i].value;
}