@@ -128,7 +128,7 @@ int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size,
128
128
}
129
129
130
130
ret = mbedtls_cipher_cmac_starts (aes_cmac_ctx, key, APPKEY_KEY_LENGTH);
131
- if (0 != ret)
131
+ if (0 != ret) {
132
132
goto exit;
133
133
}
134
134
@@ -169,6 +169,7 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
169
169
uint16_t i;
170
170
uint8_t bufferIndex = 0 ;
171
171
int ret = 0 ;
172
+ uint16_t ctr = 1 ;
172
173
uint8_t a_block[16 ] = {0 };
173
174
uint8_t s_block[16 ] = {0 };
174
175
const uint8_t *key;
@@ -183,7 +184,7 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
183
184
mbedtls_aes_init (&aes_ctx);
184
185
185
186
ret = mbedtls_aes_setkey_enc (&aes_ctx, key, APPKEY_KEY_LENGTH);
186
- if (0 != ret)
187
+ if (0 != ret) {
187
188
goto exit;
188
189
}
189
190
@@ -212,7 +213,8 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
212
213
a_block[15 ] = 0x01 ;
213
214
214
215
while (size >= 16 ) {
215
- a_block[15 ]++;
216
+ a_block[15 ] = ((ctr) & 0xFF );
217
+ ctr++;
216
218
217
219
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block,
218
220
s_block);
@@ -228,6 +230,7 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
228
230
}
229
231
230
232
if (size > 0 ) {
233
+ a_block[15 ] = ((ctr) & 0xFF );
231
234
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block,
232
235
s_block);
233
236
if (0 != ret) {
@@ -281,7 +284,7 @@ int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
281
284
}
282
285
283
286
ret = mbedtls_cipher_cmac_starts (aes_cmac_ctx, key, APPKEY_KEY_LENGTH);
284
- if (0 != ret)
287
+ if (0 != ret) {
285
288
goto exit;
286
289
}
287
290
@@ -322,7 +325,7 @@ int LoRaMacCrypto::decrypt_join_frame(const uint8_t *buffer, uint16_t size,
322
325
}
323
326
324
327
ret = mbedtls_aes_setkey_enc (&aes_ctx, key, APPKEY_KEY_LENGTH);
325
- if (0 != ret)
328
+ if (0 != ret) {
326
329
goto exit;
327
330
}
328
331
@@ -365,22 +368,23 @@ int LoRaMacCrypto::compute_skeys_for_join_frame(const uint8_t *args, uint8_t arg
365
368
nonce[0 ] = 0x02 ;
366
369
memcpy (nonce + 1 , args, args_size);
367
370
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.app_skey );
368
- if (0 != ret)
371
+ if (0 != ret) {
369
372
goto exit;
373
+ }
370
374
371
375
mbedtls_aes_free (&aes_ctx);
372
376
mbedtls_aes_init (&aes_ctx);
373
377
374
378
ret = mbedtls_aes_setkey_enc (&aes_ctx, _keys.nwk_key , APPKEY_KEY_LENGTH);
375
- if (0 != ret)
379
+ if (0 != ret) {
376
380
goto exit;
377
381
}
378
382
379
383
memset (nonce, 0 , sizeof (nonce));
380
384
nonce[0 ] = 0x01 ;
381
385
memcpy (nonce + 1 , args, args_size);
382
386
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.nwk_skey );
383
- if (0 != ret)
387
+ if (0 != ret) {
384
388
goto exit;
385
389
}
386
390
@@ -392,15 +396,17 @@ int LoRaMacCrypto::compute_skeys_for_join_frame(const uint8_t *args, uint8_t arg
392
396
nonce[0 ] = 0x03 ;
393
397
memcpy (nonce + 1 , args, args_size);
394
398
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.snwk_sintkey );
395
- if (0 != ret)
399
+ if (0 != ret) {
396
400
goto exit;
401
+ }
397
402
398
403
memset (nonce, 0 , sizeof (nonce));
399
404
nonce[0 ] = 0x04 ;
400
405
memcpy (nonce + 1 , args, args_size);
401
406
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.nwk_senckey );
402
- if (0 != ret)
407
+ if (0 != ret) {
403
408
goto exit;
409
+ }
404
410
}
405
411
406
412
exit: mbedtls_aes_free (&aes_ctx);
@@ -428,15 +434,17 @@ int LoRaMacCrypto::compute_join_server_keys(const uint8_t *eui)
428
434
nonce[0 ] = 0x05 ;
429
435
memcpy (nonce + 1 , eui, 8 );
430
436
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.js_enckey );
431
- if (0 != ret)
437
+ if (0 != ret) {
432
438
goto exit;
439
+ }
433
440
434
441
memset (nonce, 0 , sizeof (nonce));
435
442
nonce[0 ] = 0x06 ;
436
443
memcpy (nonce + 1 , eui, 8 );
437
444
ret = mbedtls_aes_crypt_ecb (&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, _keys.js_intkey );
438
- if (0 != ret)
445
+ if (0 != ret) {
439
446
goto exit;
447
+ }
440
448
441
449
exit:
442
450
mbedtls_aes_free (&aes_ctx);
0 commit comments