1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
166
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
...
...
...
#define NX_CRYPTO_SOURCE_CODE
#include "nx_crypto.h"
#include "nx_crypto_hmac_sha2.h"
#if defined(NX_CRYPTO_SELF_TEST) && defined(STM32F469xx)
#define NX_CRYPTO_INTEGRITY_TEST_CHECK(status) \
if(status) \
{ \
_nx_crypto_library_state |= NX_CRYPTO_LIBRARY_STATE_POST_FAILED; \
...}...
#define REG32(x) (*(volatile unsigned int *)(x))
#define STM32F4_RCC 0x40023800
#define STM32F4_RCC_AHB2ENR REG32(STM32F4_RCC + 0x34)
#define STM32F4_RCC_AHB2ENR_RNGEN 0x00000040
#define STM32L4_RCC 0x40021000
#define STM32L4_RCC_AHB2ENR REG32(STM32L4_RCC + 0x4C)
#define STM32L4_RCC_AHB2ENR_RNGEN 0x00040000
#define STM32_RNG 0x50060800
#define STM32_RNG_CR REG32(STM32_RNG + 0x00)
#define STM32_RNG_SR REG32(STM32_RNG + 0x04)
#define STM32_RNG_DR REG32(STM32_RNG + 0x08)
#define STM32_RNG_CR_RNGEN 0x00000004
#define STM32_RNG_CR_IE 0x00000008
#define STM32_RNG_CR_CED 0x00000020
#define STM32_RNG_SR_DRDY 0x00000001
#define STM32_RNG_SR_CECS 0x00000002
#define STM32_RNG_SR_SECS 0x00000004
#define STM32_RNG_SR_CEIS 0x00000020
#define STM32_RNG_SR_SEIS 0x00000040
#define DBGMCU_IDCODE REG32(0xE0042000)
#define DBGMCU_IDCODE_DEV_ID_MASK 0x00000FFF
#define DEV_ID_L47x 0x415
23 defines
const unsigned long long _nx_crypto_module_program_begin @ "NX_CRYPTO_PROGRAM_BEGIN" = 0ull;
const unsigned char _nx_crypto_module_hash_value[32] @ "NX_CRYPTO_PROGRAM_END" =
{
#if (__VER__ == 8030002)
0xe0, 0xa6, 0xf3, 0x19, 0x9f, 0x43, 0x02, 0x4f,
0xe6, 0x69, 0xfb, 0xd3, 0x4c, 0x1b, 0xdd, 0xee,
0x90, 0x3c, 0xff, 0xba, 0x16, 0x56, 0x51, 0x78,
0x57, 0xe6, 0x47, 0xc2, 0xca, 0x83, 0xff, 0xc9/* ... */
#else
0xe8, 0xd9, 0xe8, 0xac, 0xd3, 0x7c, 0x54, 0x7e,
0xd0, 0x72, 0x96, 0x38, 0xde, 0x45, 0x78, 0xff,
0xe1, 0xe4, 0xb2, 0xfc, 0xfd, 0xa6, 0x11, 0xd7,
0x0c, 0xda, 0xc9, 0xc9, 0xf2, 0x95, 0xd2, 0xa4/* ... */
#endif
...};
extern NX_CRYPTO_METHOD crypto_method_hmac_sha256;
extern const CHAR nx_crypto_hash_key[];
extern const UINT nx_crypto_hash_key_size;
static UCHAR nx_crypto_hmac_output[64];
static NX_CRYPTO_SHA256_HMAC nx_crypto_self_test_hmac;
...
NX_CRYPTO_KEEP void _nx_crypto_hardware_rand_initialize_stm32f4(void)
{
unsigned int device_id;
device_id = DBGMCU_IDCODE & DBGMCU_IDCODE_DEV_ID_MASK;
if (device_id == DEV_ID_L47x)
{
STM32L4_RCC_AHB2ENR |= STM32L4_RCC_AHB2ENR_RNGEN;
}if (device_id == DEV_ID_L47x) { ... }
else
{
STM32F4_RCC_AHB2ENR |= STM32F4_RCC_AHB2ENR_RNGEN;
}else { ... }
STM32_RNG_CR = STM32_RNG_CR_RNGEN;
}_nx_crypto_hardware_rand_initialize_stm32f4 (void) { ... }
NX_CRYPTO_KEEP INT nx_crypto_hardware_rand(VOID)
{
while((STM32_RNG_SR & STM32_RNG_SR_DRDY) == 0);
return STM32_RNG_DR;
}nx_crypto_hardware_rand (VOID) { ... }
...
...
NX_CRYPTO_KEEP __nounwind __interwork __softfp __aapcs_core void __aeabi_memcpy (void *dest, const void *src, size_t size)
{
char *from, *to;
unsigned int i;
from = (char*)src;
to = (char*)dest;
for(i = 0; i < size; i++)
{
to[i] = from[i];
}for (i = 0; i < size; i++) { ... }
}__aeabi_memcpy (void *dest, const void *src, size_t size) { ... }
...
...
NX_CRYPTO_KEEP __nounwind __interwork __softfp __aapcs_core void __aeabi_memcpy4 (void *dest, const void *src, size_t size)
{
char *from, *to;
unsigned int i;
from = (char*)src;
to = (char*)dest;
for(i = 0; i < size; i++)
{
to[i] = from[i];
}for (i = 0; i < size; i++) { ... }
}__aeabi_memcpy4 (void *dest, const void *src, size_t size) { ... }
...
...
NX_CRYPTO_KEEP __nounwind __interwork __softfp __aapcs_core void __aeabi_memcpy8 (void *dest, const void *src, size_t size)
{
char *from, *to;
unsigned int i;
from = (char*)src;
to = (char*)dest;
for(i = 0; i < size; i++)
{
to[i] = from[i];
}for (i = 0; i < size; i++) { ... }
}__aeabi_memcpy8 (void *dest, const void *src, size_t size) { ... }
NX_CRYPTO_KEEP UINT _nx_crypto_integrity_test(void)
{
UINT status;
status = _nx_crypto_method_hmac_sha256_operation(NX_CRYPTO_AUTHENTICATE,
(VOID*)&nx_crypto_self_test_hmac,
&crypto_method_hmac_sha256,
(UCHAR*)nx_crypto_hash_key,
nx_crypto_hash_key_size,
(UCHAR*)&_nx_crypto_module_program_begin,
(ULONG)_nx_crypto_module_hash_value - (ULONG)&_nx_crypto_module_program_begin,
NX_CRYPTO_NULL,
nx_crypto_hmac_output, sizeof(nx_crypto_hmac_output),
&nx_crypto_self_test_hmac, sizeof(nx_crypto_self_test_hmac),
NX_CRYPTO_NULL, NX_CRYPTO_NULL);
NX_CRYPTO_INTEGRITY_TEST_CHECK(status)
status = (UINT)NX_CRYPTO_MEMCMP(nx_crypto_hmac_output, _nx_crypto_module_hash_value, 32);
NX_CRYPTO_INTEGRITY_TEST_CHECK(status);
return(status);
}_nx_crypto_integrity_test (void) { ... }
...
/* ... */#endif