Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include "aes/esp_aes.h"
#include "aes/esp_aes_gcm.h"
#include "esp_aes_internal.h"
#include "hal/aes_hal.h"
#include "mbedtls/aes.h"
#include "mbedtls/error.h"
#include "mbedtls/gcm.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "soc/soc_caps.h"
#include "soc/soc_memory_layout.h"
#include "sdkconfig.h"
#include "esp_aes_dma_priv.h"
#define ESP_AES_GCM_AAD_MAX_BYTES
TAG
esp_gcm_derive_J0(esp_gcm_context *)
increment32_j0(esp_gcm_context *, uint8_t *)
xor_data(uint8_t *, const uint8_t *)
gcm_gen_table(esp_gcm_context *)
last4
gcm_mult(esp_gcm_context *, const unsigned char *, unsigned char *)
esp_aes_gcm_setkey(esp_gcm_context *, mbedtls_cipher_id_t, const unsigned char *, unsigned int)
esp_gcm_ghash(esp_gcm_context *, const unsigned char *, size_t, uint8_t *)
esp_aes_gcm_init(esp_gcm_context *)
esp_aes_gcm_free(esp_gcm_context *)
esp_aes_gcm_starts(esp_gcm_context *, int, const unsigned char *, size_t)
esp_aes_gcm_update_ad(esp_gcm_context *, const unsigned char *, size_t)
esp_aes_gcm_update(esp_gcm_context *, const unsigned char *, size_t, unsigned char *, size_t, size_t *)
esp_aes_gcm_finish(esp_gcm_context *, unsigned char *, size_t, size_t *, unsigned char *, size_t)
esp_aes_gcm_crypt_and_tag_partial_hw(esp_gcm_context *, int, size_t, const unsigned char *, size_t, const unsigned char *, size_t, const unsigned char *, unsigned char *, size_t, unsigned char *)
esp_aes_gcm_crypt_and_tag(esp_gcm_context *, int, size_t, const unsigned char *, size_t, const unsigned char *, size_t, const unsigned char *, unsigned char *, size_t, unsigned char *)
esp_aes_gcm_auth_decrypt(esp_gcm_context *, size_t, const unsigned char *, size_t, const unsigned char *, size_t, const unsigned char *, size_t, const unsigned char *, unsigned char *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesmbedTLSport/aes/esp_aes_gcm.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
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
240
241
242
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
299
300
301
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
358
359
360
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * GCM block cipher, ESP DMA hardware accelerated version * Based on mbedTLS FIPS-197 compliant version. * * SPDX-FileCopyrightText: The Mbed TLS Contributors * * SPDX-License-Identifier: Apache-2.0 * * SPDX-FileContributor: 2016-2024 Espressif Systems (Shanghai) CO LTD *//* ... */ /* * The AES block cipher was designed by Vincent Rijmen and Joan Daemen. * * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf *//* ... */ #include <string.h> #include "aes/esp_aes.h" #include "aes/esp_aes_gcm.h" #include "esp_aes_internal.h" #include "hal/aes_hal.h" #include "mbedtls/aes.h" #include "mbedtls/error.h" #include "mbedtls/gcm.h" #include "esp_heap_caps.h" #include "esp_log.h" #include "soc/soc_caps.h" #include "soc/soc_memory_layout.h" #include "sdkconfig.h"13 includes #if SOC_AES_SUPPORT_DMA #include "esp_aes_dma_priv.h" #endif #define ESP_PUT_BE64(a, val) \ do { \ *(uint64_t*)(a) = __builtin_bswap64( (uint64_t)(val) ); \ }{...} while (0)... /* For simplicity limit the maximum amount of aad bytes to a single DMA descriptor This should cover all normal, e.g. mbedtls, use cases *//* ... */ #define ESP_AES_GCM_AAD_MAX_BYTES 4080 static const char *TAG = "esp-aes-gcm"; static void esp_gcm_ghash(esp_gcm_context *ctx, const unsigned char *x, size_t x_len, uint8_t *z); /* * Calculates the Initial Counter Block, J0 * and copies to to the esp_gcm_context *//* ... */ static void esp_gcm_derive_J0(esp_gcm_context *ctx) { uint8_t len_buf[16]; memset(ctx->J0, 0, AES_BLOCK_BYTES); memset(len_buf, 0, AES_BLOCK_BYTES); /* If IV is 96 bits J0 = ( IV || 0^31 || 1 ) */ if (ctx->iv_len == 12) { memcpy(ctx->J0, ctx->iv, ctx->iv_len); ctx->J0[AES_BLOCK_BYTES - 1] |= 1; }{...} else { /* For IV != 96 bit, J0 = GHASH(IV || 0[s+64] || [len(IV)]64) */ /* First calculate GHASH on IV */ esp_gcm_ghash(ctx, ctx->iv, ctx->iv_len, ctx->J0); /* Next create 128 bit block which is equal to 64 bit 0 + iv length truncated to 64 bits *//* ... */ ESP_PUT_BE64(len_buf + 8, ctx->iv_len * 8); /* Calculate GHASH on last block */ esp_gcm_ghash(ctx, len_buf, 16, ctx->J0); }{...} }{ ... } /* * Increment J0 as per GCM spec, by applying the Standard Incrementing Function INC_32 to it. * j is the counter which needs to be incremented which is * copied to ctx->J0 after incrementing *//* ... */ static void increment32_j0(esp_gcm_context *ctx, uint8_t *j) { uint8_t j_len = AES_BLOCK_BYTES; memcpy(j, ctx->J0, AES_BLOCK_BYTES); if (j) { for (uint32_t i = j_len; i > (j_len - 4); i--) { if (++j[i - 1] != 0) { break; }{...} }{...} memcpy(ctx->J0, j, AES_BLOCK_BYTES); }{...} }{ ... } /* Function to xor two data blocks */ static void xor_data(uint8_t *d, const uint8_t *s) { for (int i = 0; i < AES_BLOCK_BYTES; i++) { d[i] ^= s[i]; }{...} }{ ... } /* * 32-bit integer manipulation macros (big endian) *//* ... */ #ifndef GET_UINT32_BE #define GET_UINT32_BE(n,b,i) \ { \ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ | ( (uint32_t) (b)[(i) + 1] << 16 ) \ | ( (uint32_t) (b)[(i) + 2] << 8 ) \ | ( (uint32_t) (b)[(i) + 3] ); \ }{...} ...#endif/* ... */ #ifndef PUT_UINT32_BE #define PUT_UINT32_BE(n,b,i) \ { \ (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ (b)[(i) + 3] = (unsigned char) ( (n) ); \ }{...} ...#endif/* ... */ /* Based on MbedTLS's implementation * * Precompute small multiples of H, that is set * HH[i] || HL[i] = H times i, * where i is seen as a field element as in [MGV], ie high-order bits * correspond to low powers of P. The result is stored in the same way, that * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL * corresponds to P^127. *//* ... */ static int gcm_gen_table( esp_gcm_context *ctx ) { int i, j; uint64_t hi, lo; uint64_t vl, vh; unsigned char *h; h = ctx->H; /* pack h as two 64-bits ints, big-endian */ GET_UINT32_BE( hi, h, 0 ); GET_UINT32_BE( lo, h, 4 ); vh = (uint64_t) hi << 32 | lo; GET_UINT32_BE( hi, h, 8 ); GET_UINT32_BE( lo, h, 12 ); vl = (uint64_t) hi << 32 | lo; /* 8 = 1000 corresponds to 1 in GF(2^128) */ ctx->HL[8] = vl; ctx->HH[8] = vh; /* 0 corresponds to 0 in GF(2^128) */ ctx->HH[0] = 0; ctx->HL[0] = 0; for ( i = 4; i > 0; i >>= 1 ) { uint32_t T = ( vl & 1 ) * 0xe1000000U; vl = ( vh << 63 ) | ( vl >> 1 ); vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32); ctx->HL[i] = vl; ctx->HH[i] = vh; }{...} for ( i = 2; i <= 8; i *= 2 ) { uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i; vh = *HiH; vl = *HiL; for ( j = 1; j < i; j++ ) { HiH[j] = vh ^ ctx->HH[j]; HiL[j] = vl ^ ctx->HL[j]; }{...} }{...} return ( 0 ); }{ ... } /* * Shoup's method for multiplication use this table with * last4[x] = x times P^128 * where x and last4[x] are seen as elements of GF(2^128) as in [MGV] *//* ... */ static const uint32_t last4[16] = { 0x00000000, 0x1c200000, 0x38400000, 0x24600000, 0x70800000, 0x6ca00000, 0x48c00000, 0x54e00000, 0xe1000000, 0xfd200000, 0xd9400000, 0xc5600000, 0x91800000, 0x8da00000, 0xa9c00000, 0xb5e00000 }{...}; /* Based on MbedTLS's implementation * * Sets output to x times H using the precomputed tables. * x and output are seen as elements of GF(2^128) as in [MGV]. *//* ... */ static void gcm_mult( esp_gcm_context *ctx, const unsigned char x[16], unsigned char output[16] ) { int i = 0; unsigned char lo, hi, rem; uint64_t zh, zl; lo = x[15] & 0xf; hi = x[15] >> 4; zh = ctx->HH[lo]; zl = ctx->HL[lo]; rem = (unsigned char) zl & 0xf; zl = ( zh << 60 ) | ( zl >> 4 ); zh = ( zh >> 4 ); zh ^= (uint64_t) last4[rem] << 32; zh ^= ctx->HH[hi]; zl ^= ctx->HL[hi]; for ( i = 14; i >= 0; i-- ) { lo = x[i] & 0xf; hi = x[i] >> 4; rem = (unsigned char) zl & 0xf; zl = ( zh << 60 ) | ( zl >> 4 ); zh = ( zh >> 4 ); zh ^= (uint64_t) last4[rem] << 32; zh ^= ctx->HH[lo]; zl ^= ctx->HL[lo]; rem = (unsigned char) zl & 0xf; zl = ( zh << 60 ) | ( zl >> 4 ); zh = ( zh >> 4 ); zh ^= (uint64_t) last4[rem] << 32; zh ^= ctx->HH[hi]; zl ^= ctx->HL[hi]; }{...} PUT_UINT32_BE( zh >> 32, output, 0 ); PUT_UINT32_BE( zh, output, 4 ); PUT_UINT32_BE( zl >> 32, output, 8 ); PUT_UINT32_BE( zl, output, 12 ); }{ ... } /* Update the key value in gcm context */ int esp_aes_gcm_setkey( esp_gcm_context *ctx, mbedtls_cipher_id_t cipher, const unsigned char *key, unsigned int keybits ) { /* Fallback to software implementation of GCM operation when a non-AES * cipher is selected, as we support hardware acceleration only for a * GCM operation using AES cipher. *//* ... */ #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { mbedtls_gcm_free_soft(ctx->ctx_soft); free(ctx->ctx_soft); ctx->ctx_soft = NULL; }{...} if (cipher != MBEDTLS_CIPHER_ID_AES) { ctx->ctx_soft = (mbedtls_gcm_context_soft*) malloc(sizeof(mbedtls_gcm_context_soft)); if (ctx->ctx_soft == NULL) { return MBEDTLS_ERR_CIPHER_ALLOC_FAILED; }{...} mbedtls_gcm_init_soft(ctx->ctx_soft); return mbedtls_gcm_setkey_soft(ctx->ctx_soft, cipher, key, keybits); }{...} #endif/* ... */ #if !SOC_AES_SUPPORT_AES_192 if (keybits == 192) { return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED; }{...} #endif/* ... */ if (keybits != 128 && keybits != 192 && keybits != 256) { return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; }{...} ctx->aes_ctx.key_bytes = keybits / 8; memcpy(ctx->aes_ctx.key, key, ctx->aes_ctx.key_bytes); return ( 0 ); }{ ... } /* AES-GCM GHASH calculation z = GHASH(x) using h0 hash key *//* ... */ static void esp_gcm_ghash(esp_gcm_context *ctx, const unsigned char *x, size_t x_len, uint8_t *z) { uint8_t tmp[AES_BLOCK_BYTES]; memset(tmp, 0, AES_BLOCK_BYTES); /* GHASH(X) is calculated on input string which is multiple of 128 bits * If input string bit length is not multiple of 128 bits it needs to * be padded by 0 * * Steps: * 1. Let X1, X2, ... , Xm-1, Xm denote the unique sequence of blocks such * that X = X1 || X2 || ... || Xm-1 || Xm. * 2. Let Y0 be the “zero block,” 0128. * 3. Fori=1,...,m,letYi =(Yi-1 ^ Xi)•H. * 4. Return Ym *//* ... */ /* If input bit string is >= 128 bits, process full 128 bit blocks */ while (x_len >= AES_BLOCK_BYTES) { xor_data(z, x); gcm_mult(ctx, z, z); x += AES_BLOCK_BYTES; x_len -= AES_BLOCK_BYTES; }{...} /* If input bit string is not multiple of 128 create last 128 bit * block by padding necessary 0s *//* ... */ if (x_len) { memcpy(tmp, x, x_len); xor_data(z, tmp); gcm_mult(ctx, z, z); }{...} }{ ... } /* Function to init AES GCM context to zero */ void esp_aes_gcm_init( esp_gcm_context *ctx) { if (ctx == NULL) { return; }{...} bzero(ctx, sizeof(esp_gcm_context)); #if SOC_AES_SUPPORT_DMA && CONFIG_MBEDTLS_AES_USE_INTERRUPT esp_aes_intr_alloc(); #endif ctx->gcm_state = ESP_AES_GCM_STATE_INIT; }{ ... } /* Function to clear AES-GCM context */ void esp_aes_gcm_free( esp_gcm_context *ctx) { if (ctx == NULL) { return; }{...} #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { mbedtls_gcm_free_soft(ctx->ctx_soft); free(ctx->ctx_soft); /* Note that the value of ctx->ctx_soft should be NULL'ed out and here it is taken care by the bzero call below *//* ... */ }{...} #endif/* ... */ bzero(ctx, sizeof(esp_gcm_context)); }{ ... } /* Setup AES-GCM */ int esp_aes_gcm_starts( esp_gcm_context *ctx, int mode, const unsigned char *iv, size_t iv_len ) { if (!ctx) { ESP_LOGE(TAG, "No AES context supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_starts_soft(ctx->ctx_soft, mode, iv, iv_len); }{...} #endif/* ... */ /* IV is limited to 2^32 bits, so 2^29 bytes */ /* IV is not allowed to be zero length */ if ( iv_len == 0 || ( (uint32_t) iv_len ) >> 29 != 0 ) { return ( MBEDTLS_ERR_GCM_BAD_INPUT ); }{...} if (!iv) { ESP_LOGE(TAG, "No IV supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} /* Initialize AES-GCM context */ memset(ctx->ghash, 0, sizeof(ctx->ghash)); ctx->data_len = 0; ctx->aad = NULL; ctx->aad_len = 0; ctx->iv = iv; ctx->iv_len = iv_len; ctx->mode = mode; /* H and the lookup table are only generated once per ctx */ if (ctx->gcm_state == ESP_AES_GCM_STATE_INIT) { /* Lock the AES engine to calculate ghash key H in hardware */ #if CONFIG_MBEDTLS_HARDWARE_GCM esp_aes_acquire_hardware(); ctx->aes_ctx.key_in_hardware = aes_hal_setkey(ctx->aes_ctx.key, ctx->aes_ctx.key_bytes, mode); aes_hal_mode_init(ESP_AES_BLOCK_MODE_GCM); aes_hal_gcm_calc_hash(ctx->H); esp_aes_release_hardware();/* ... */ #else memset(ctx->H, 0, sizeof(ctx->H)); int ret = esp_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->H, ctx->H); if (ret != 0) { return ret; }{...} /* ... */#endif gcm_gen_table(ctx); }{...} /* Once H is obtained we need to derive J0 (Initial Counter Block) */ esp_gcm_derive_J0(ctx); /* The initial counter block keeps updating during the esp_gcm_update call * however to calculate final authentication tag T we need original J0 * so we make a copy here *//* ... */ memcpy(ctx->ori_j0, ctx->J0, 16); ctx->gcm_state = ESP_AES_GCM_STATE_START; return ( 0 ); }{ ... } int esp_aes_gcm_update_ad( esp_gcm_context *ctx, const unsigned char *aad, size_t aad_len ) { if (!ctx) { ESP_LOGE(TAG, "No AES context supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_update_ad_soft(ctx->ctx_soft, aad, aad_len); }{...} #endif/* ... */ /* AD are limited to 2^32 bits, so 2^29 bytes */ if ( ( (uint32_t) aad_len ) >> 29 != 0 ) { return ( MBEDTLS_ERR_GCM_BAD_INPUT ); }{...} if ( (aad_len > 0) && !aad) { ESP_LOGE(TAG, "No aad supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} if (ctx->gcm_state != ESP_AES_GCM_STATE_START) { ESP_LOGE(TAG, "AES context in invalid state!"); return -1; }{...} /* Initialise associated data */ ctx->aad = aad; ctx->aad_len = aad_len; esp_gcm_ghash(ctx, ctx->aad, ctx->aad_len, ctx->ghash); return ( 0 ); }{ ... } /* Perform AES-GCM operation */ int esp_aes_gcm_update( esp_gcm_context *ctx, const unsigned char *input, size_t input_length, unsigned char *output, size_t output_size, size_t *output_length ) { if (!ctx) { ESP_LOGE(TAG, "No GCM context supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_update_soft(ctx->ctx_soft, input, input_length, output, output_size, output_length); }{...} #endif/* ... */ size_t nc_off = 0; uint8_t nonce_counter[AES_BLOCK_BYTES] = {0}; uint8_t stream[AES_BLOCK_BYTES] = {0}; if (!output_length) { ESP_LOGE(TAG, "No output length supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} *output_length = input_length; if (!input) { ESP_LOGE(TAG, "No input supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} if (!output) { ESP_LOGE(TAG, "No output supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} if ( output > input && (size_t) ( output - input ) < input_length ) { return ( MBEDTLS_ERR_GCM_BAD_INPUT ); }{...} /* If this is the first time esp_gcm_update is getting called * calculate GHASH on aad and preincrement the ICB *//* ... */ if (ctx->gcm_state == ESP_AES_GCM_STATE_START) { /* Jo needs to be incremented first time, later the CTR * operation will auto update it *//* ... */ increment32_j0(ctx, nonce_counter); ctx->gcm_state = ESP_AES_GCM_STATE_UPDATE; }{...} else if (ctx->gcm_state == ESP_AES_GCM_STATE_UPDATE) { memcpy(nonce_counter, ctx->J0, AES_BLOCK_BYTES); }{...} /* Perform intermediate GHASH on "encrypted" data during decryption */ if (ctx->mode == ESP_AES_DECRYPT) { esp_gcm_ghash(ctx, input, input_length, ctx->ghash); }{...} /* Output = GCTR(J0, Input): Encrypt/Decrypt the input */ int ret = esp_aes_crypt_ctr(&ctx->aes_ctx, input_length, &nc_off, nonce_counter, stream, input, output); if (ret != 0) { return ret; }{...} /* ICB gets auto incremented after GCTR operation here so update the context */ memcpy(ctx->J0, nonce_counter, AES_BLOCK_BYTES); /* Keep updating the length counter for final tag calculation */ ctx->data_len += input_length; /* Perform intermediate GHASH on "encrypted" data during encryption*/ if (ctx->mode == ESP_AES_ENCRYPT) { esp_gcm_ghash(ctx, output, input_length, ctx->ghash); }{...} return 0; }{ ... } /* Function to read the tag value */ int esp_aes_gcm_finish( esp_gcm_context *ctx, unsigned char *output, size_t output_size, size_t *output_length, unsigned char *tag, size_t tag_len ) { #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_finish_soft(ctx->ctx_soft, output, output_size, output_length, tag, tag_len); }{...} #endif/* ... */ size_t nc_off = 0; uint8_t len_block[AES_BLOCK_BYTES] = {0}; uint8_t stream[AES_BLOCK_BYTES] = {0}; if ( tag_len > 16 || tag_len < 4 ) { return ( MBEDTLS_ERR_GCM_BAD_INPUT ); }{...} /* Calculate final GHASH on aad_len, data length */ ESP_PUT_BE64(len_block, ctx->aad_len * 8); ESP_PUT_BE64(len_block + 8, ctx->data_len * 8); esp_gcm_ghash(ctx, len_block, AES_BLOCK_BYTES, ctx->ghash); /* Tag T = GCTR(J0, ) where T is truncated to tag_len */ return esp_aes_crypt_ctr(&ctx->aes_ctx, tag_len, &nc_off, ctx->ori_j0, stream, ctx->ghash, tag); }{ ... } #if CONFIG_MBEDTLS_HARDWARE_GCM /* Due to restrictions in the hardware (e.g. need to do the whole conversion in one go), some combinations of inputs are not supported *//* ... */ static bool esp_aes_gcm_input_support_hw_accel(size_t length, const unsigned char *aad, size_t aad_len, const unsigned char *input, unsigned char *output) { bool support_hw_accel = true; if (aad_len > ESP_AES_GCM_AAD_MAX_BYTES) { support_hw_accel = false; }{...} else if (!esp_ptr_dma_capable(aad) && aad_len > 0) { /* aad in non internal DMA memory */ support_hw_accel = false; }{...} else if (!esp_ptr_dma_capable(input) && length > 0) { /* input in non internal DMA memory */ support_hw_accel = false; }{...} else if (!esp_ptr_dma_capable(output) && length > 0) { /* output in non internal DMA memory */ support_hw_accel = false; }{...} else if (length == 0) { support_hw_accel = false; }{...} return support_hw_accel; }{...} /* ... */#endif static int esp_aes_gcm_crypt_and_tag_partial_hw( esp_gcm_context *ctx, int mode, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *input, unsigned char *output, size_t tag_len, unsigned char *tag ) { int ret = 0; size_t olen; if ( ( ret = esp_aes_gcm_starts( ctx, mode, iv, iv_len ) ) != 0 ) { return ( ret ); }{...} if ( ( ret = esp_aes_gcm_update_ad( ctx, aad, aad_len ) ) != 0 ) { return ( ret ); }{...} if ( ( ret = esp_aes_gcm_update( ctx, input, length, output, 0, &olen ) ) != 0 ) { return ( ret ); }{...} if ( ( ret = esp_aes_gcm_finish( ctx, output, 0, &olen, tag, tag_len ) ) != 0 ) { return ( ret ); }{...} return ret; }{ ... } int esp_aes_gcm_crypt_and_tag( esp_gcm_context *ctx, int mode, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *input, unsigned char *output, size_t tag_len, unsigned char *tag ) { if (!ctx) { ESP_LOGE(TAG, "No AES context supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_crypt_and_tag_soft(ctx->ctx_soft, mode, length, iv, iv_len, aad, aad_len, input, output, tag_len, tag); }{...} #endif/* ... */ #if CONFIG_MBEDTLS_HARDWARE_GCM int ret; size_t remainder_bit; /* Due to hardware limitation only certain cases are fully supported in HW */ if (!esp_aes_gcm_input_support_hw_accel(length, aad, aad_len, input, output)) { return esp_aes_gcm_crypt_and_tag_partial_hw(ctx, mode, length, iv, iv_len, aad, aad_len, input, output, tag_len, tag); }{...} /* Limit aad len to a single DMA descriptor to simplify DMA handling In practice, e.g. with mbedtls the length of aad will always be short the size field has 12 bits, but 0 not for 4096. to avoid possible problem when the size is not word-aligned, we only use 4096-4 per desc. Maximum size of data in the buffer that a DMA descriptor can hold. *//* ... */ if (aad_len > DMA_DESCRIPTOR_BUFFER_MAX_SIZE_4B_ALIGNED) { return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} /* IV and AD are limited to 2^32 bits, so 2^29 bytes */ /* IV is not allowed to be zero length */ if ( iv_len == 0 || ( (uint32_t) iv_len ) >> 29 != 0 || ( (uint32_t) aad_len ) >> 29 != 0 ) { return ( MBEDTLS_ERR_GCM_BAD_INPUT ); }{...} if (!iv) { ESP_LOGE(TAG, "No IV supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} if ( (aad_len > 0) && !aad) { ESP_LOGE(TAG, "No aad supplied"); return MBEDTLS_ERR_GCM_BAD_INPUT; }{...} /* Initialize AES-GCM context */ memset(ctx->ghash, 0, sizeof(ctx->ghash)); ctx->data_len = 0; ctx->iv = iv; ctx->iv_len = iv_len; ctx->aad = aad; ctx->aad_len = aad_len; ctx->mode = mode; esp_aes_acquire_hardware(); ctx->aes_ctx.key_in_hardware = 0; ctx->aes_ctx.key_in_hardware = aes_hal_setkey(ctx->aes_ctx.key, ctx->aes_ctx.key_bytes, mode); aes_hal_mode_init(ESP_AES_BLOCK_MODE_GCM); /* See TRM GCM chapter for description of this calculation */ remainder_bit = (8 * length) % 128; aes_hal_gcm_init( (aad_len + AES_BLOCK_BYTES - 1) / AES_BLOCK_BYTES, remainder_bit); aes_hal_gcm_calc_hash(ctx->H); gcm_gen_table(ctx); esp_gcm_derive_J0(ctx); aes_hal_gcm_set_j0(ctx->J0); ret = esp_aes_process_dma_gcm(&ctx->aes_ctx, input, output, length, aad, aad_len); if (ret != 0) { esp_aes_release_hardware(); return ret; }{...} aes_hal_gcm_read_tag(tag, tag_len); esp_aes_release_hardware(); return ( ret );/* ... */ #else return esp_aes_gcm_crypt_and_tag_partial_hw(ctx, mode, length, iv, iv_len, aad, aad_len, input, output, tag_len, tag); #endif }{ ... } int esp_aes_gcm_auth_decrypt( esp_gcm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *tag, size_t tag_len, const unsigned char *input, unsigned char *output ) { #if defined(MBEDTLS_GCM_NON_AES_CIPHER_SOFT_FALLBACK) if (ctx->ctx_soft != NULL) { return mbedtls_gcm_auth_decrypt_soft(ctx->ctx_soft, length, iv, iv_len, aad, aad_len, tag, tag_len, input, output); }{...} #endif/* ... */ int ret; unsigned char check_tag[16]; size_t i; int diff; if ( ( ret = esp_aes_gcm_crypt_and_tag( ctx, ESP_AES_DECRYPT, length, iv, iv_len, aad, aad_len, input, output, tag_len, check_tag ) ) != 0 ) { return ( ret ); }{...} /* Check tag in "constant-time" */ for ( diff = 0, i = 0; i < tag_len; i++ ) { diff |= tag[i] ^ check_tag[i]; }{...} if ( diff != 0 ) { bzero( output, length ); return ( MBEDTLS_ERR_GCM_AUTH_FAILED ); }{...} return ( 0 ); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.