Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#include "nx_crypto_gcm.h"
...
...
_nx_crypto_gcm_xor(UCHAR *, UCHAR *, UCHAR *)
...
...
_nx_crypto_gcm_inc32(UCHAR *)
...
...
_nx_crypto_gcm_multi(UCHAR *, UCHAR *, UCHAR *)
...
...
_nx_crypto_gcm_ghash_update(UCHAR *, UCHAR *, UINT, UCHAR *)
...
...
_nx_crypto_gcm_gctr(void *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), UCHAR *, UCHAR *, UINT, UCHAR *)
...
...
_nx_crypto_gcm_encrypt_init(void *, NX_CRYPTO_GCM *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), void *, UINT, UCHAR *, UINT)
...
...
_nx_crypto_gcm_encrypt_update(void *, NX_CRYPTO_GCM *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), UCHAR *, UCHAR *, UINT, UINT)
...
...
_nx_crypto_gcm_encrypt_calculate(void *, NX_CRYPTO_GCM *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), UCHAR *, UINT, UINT)
...
...
_nx_crypto_gcm_decrypt_update(void *, NX_CRYPTO_GCM *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), UCHAR *, UCHAR *, UINT, UINT)
...
_nx_crypto_gcm_decrypt_calculate(void *, NX_CRYPTO_GCM *, UINT (*)(void *, UCHAR *, UCHAR *, UINT), UCHAR *, UINT, UINT)
Files
loading...
SourceVuSTM32 Libraries and Samplesnetxduocrypto_libraries/src/nx_crypto_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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Crypto Component */ /** */ /** GCM Mode */ /** */... /**************************************************************************/ /**************************************************************************/ #include "nx_crypto_gcm.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_xor PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs XOR operation on the output buffer. */ /* */ /* INPUT */ /* */ /* plaintext Pointer to input plantext */ /* key Value to be xor'ed */ /* ciphertext Output buffer of 16 bytes */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_gcm_multi Compute multilication in GF */ /* _nx_crypto_gcm_ghash_update Compute GHASH */ /* _nx_crypto_gcm_gctr Perform GCTR operation */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), disabled */ /* unaligned access by default,*/ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP static VOID _nx_crypto_gcm_xor(UCHAR *plaintext, UCHAR *key, UCHAR *ciphertext) { #ifdef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS UINT *p = (UINT *)plaintext; UINT *c = (UINT *)ciphertext; UINT *k = (UINT *)key; c[0] = p[0] ^ k[0]; c[1] = p[1] ^ k[1]; c[2] = p[2] ^ k[2]; c[3] = p[3] ^ k[3];/* ... */ #else ciphertext[0] = plaintext[0] ^ key[0]; ciphertext[1] = plaintext[1] ^ key[1]; ciphertext[2] = plaintext[2] ^ key[2]; ciphertext[3] = plaintext[3] ^ key[3]; ciphertext[4] = plaintext[4] ^ key[4]; ciphertext[5] = plaintext[5] ^ key[5]; ciphertext[6] = plaintext[6] ^ key[6]; ciphertext[7] = plaintext[7] ^ key[7]; ciphertext[8] = plaintext[8] ^ key[8]; ciphertext[9] = plaintext[9] ^ key[9]; ciphertext[10] = plaintext[10] ^ key[10]; ciphertext[11] = plaintext[11] ^ key[11]; ciphertext[12] = plaintext[12] ^ key[12]; ciphertext[13] = plaintext[13] ^ key[13]; ciphertext[14] = plaintext[14] ^ key[14]; ciphertext[15] = plaintext[15] ^ key[15];/* ... */ #endif }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_inc32 PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function adds one for the last byte. */ /* */ /* INPUT */ /* */ /* counter_block Pointer to counter block */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_gcm_gctr Perform GCTR operation */ /* _nx_crypto_gcm_encrypt Perform GCM encrypt/decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP static VOID _nx_crypto_gcm_inc32(UCHAR *counter_block) { USHORT result; /* Add one for last byte. */ result = (USHORT)(counter_block[15] + 1); counter_block[15] = (UCHAR)(result & 0xFF); /* Handle carry. */ result = (USHORT)((result >> 8) + counter_block[14]); counter_block[14] = (UCHAR)(result & 0xFF); result = (USHORT)((result >> 8) + counter_block[13]); counter_block[13] = (UCHAR)(result & 0xFF); result = (USHORT)((result >> 8) + counter_block[12]); counter_block[12] = (UCHAR)(result & 0xFF); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_multi PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs multiplication in GF(2^128). */ /* */ /* INPUT */ /* */ /* x Pointer to X block */ /* y Pointer to Y block */ /* output Pointer to result block */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_xor Perform XOR operation */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_gcm_ghash_update Compute GHASH */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP static VOID _nx_crypto_gcm_multi(UCHAR *x, UCHAR *y, UCHAR *output) { UINT i; INT j; UCHAR v[NX_CRYPTO_GCM_BLOCK_SIZE]; UCHAR lsb; UCHAR mask; NX_CRYPTO_MEMSET(output, 0, NX_CRYPTO_GCM_BLOCK_SIZE); NX_CRYPTO_MEMCPY(v, y, NX_CRYPTO_GCM_BLOCK_SIZE); /* Use case of memcpy is verified. */ mask = 0x80; for (i = 0; i < NX_CRYPTO_GCM_BLOCK_SIZE_BITS; i++) { /* output = output xor v when the ith bit of x is set. */ if (*x & mask) { _nx_crypto_gcm_xor(output, v, output); }if (*x & mask) { ... } /* Store the LSB before shift right. */ j = NX_CRYPTO_GCM_BLOCK_SIZE - 1; lsb = v[j]; /* v = v >> 1 */ for (; j > 0; j--) { v[j] = (UCHAR)((v[j] >> 1) | (v[j - 1] << 7)); }for (; j > 0; j--) { ... } v[0] = v[0] >> 1; /* v = v xor R when LSB of v is set. */ if (lsb & 1) { v[0] = v[0] ^ 0xe1; }if (lsb & 1) { ... } mask = mask >> 1; if (!mask) { mask = 0x80; x++; }if (!mask) { ... } }for (i = 0; i < NX_CRYPTO_GCM_BLOCK_SIZE_BITS; i++) { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_ghash_update PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function updates GHASH with new input from the caller. The */ /* input is padded so that the length is a multiple of the block size. */ /* */ /* INPUT */ /* */ /* hkey Pointer to hash key */ /* input Pointer to bytes of input */ /* input_length Length of bytes of input */ /* output Pointer to updated hash */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_xor Perform XOR operation */ /* _nx_crypto_gcm_multi Perform multiplication in GF */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_gcm_encrypt Perform GCM encrypt/decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP static VOID _nx_crypto_gcm_ghash_update(UCHAR *hkey, UCHAR *input, UINT input_length, UCHAR *output) { UCHAR tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE]; UINT i, n; n = input_length >> NX_CRYPTO_GCM_BLOCK_SIZE_SHIFT; for (i = 0; i < n; i++) { /* output = (output xor input) multi hkey */ _nx_crypto_gcm_xor(output, input, tmp_block); _nx_crypto_gcm_multi(tmp_block, hkey, output); input += NX_CRYPTO_GCM_BLOCK_SIZE; }for (i = 0; i < n; i++) { ... } input_length -= n << NX_CRYPTO_GCM_BLOCK_SIZE_SHIFT; if (input_length > 0) { /* Pad the block with zeros when the input length is not multiple of the block size. *//* ... */ NX_CRYPTO_MEMCPY(tmp_block, input, input_length); /* Use case of memcpy is verified. */ NX_CRYPTO_MEMSET(&tmp_block[input_length], 0, sizeof(tmp_block) - input_length); _nx_crypto_gcm_xor(output, tmp_block, tmp_block); _nx_crypto_gcm_multi(tmp_block, hkey, output); }if (input_length > 0) { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_gctr PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function performs GCTR mode encryption and decryption. The */ /* counter block is updated after calling this function. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* crypto_function Pointer to crypto function */ /* input Pointer to bytes of input */ /* output Pointer to output buffer */ /* length Length of bytes of input */ /* counter_block Pointer to counter block */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_xor Perform XOR operation */ /* _nx_crypto_gcm_inc32 Increase the counter by one */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_gcm_encrypt Perform GCM encrypt/decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP static VOID _nx_crypto_gcm_gctr(VOID *crypto_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), UCHAR *input, UCHAR *output, UINT length, UCHAR *counter_block) { UCHAR aes_output[NX_CRYPTO_GCM_BLOCK_SIZE]; UINT i, n; n = length >> NX_CRYPTO_GCM_BLOCK_SIZE_SHIFT; for (i = 0; i < n; i++) { /* Encrypt the counter. */ crypto_function(crypto_metadata, counter_block, aes_output, NX_CRYPTO_GCM_BLOCK_SIZE); /* XOR the input with encrypted counter. */ _nx_crypto_gcm_xor(input, aes_output, output); /* Increase the counter block. */ _nx_crypto_gcm_inc32(counter_block); input += NX_CRYPTO_GCM_BLOCK_SIZE; output += NX_CRYPTO_GCM_BLOCK_SIZE; }for (i = 0; i < n; i++) { ... } length -= n << NX_CRYPTO_GCM_BLOCK_SIZE_SHIFT; if (length > 0) { crypto_function(crypto_metadata, counter_block, aes_output, NX_CRYPTO_GCM_BLOCK_SIZE); /* Perform XOR operation on local buffer when remaining input length is smaller than block size. *//* ... */ _nx_crypto_gcm_xor(input, aes_output, aes_output); NX_CRYPTO_MEMCPY(output, aes_output, length); /* Use case of memcpy is verified. */ }if (length > 0) { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_encrypt_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function initialize the GCM mode for encryption. */ /* */ /* Note, the first byte of iv represents the length of IV excluding its*/ /* first byte. For example, 0x0401020304 indicates the length of IV is */ /* 4 bytes and the content of IV is 0x01020304. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* gcm_metadata Pointer to GCM metadata */ /* crypto_function Pointer to crypto function */ /* additional_data Pointer to additional data */ /* additional_len Length of additional data */ /* iv Pointer to Initial Vector */ /* block_size Block size of crypto algorithm*/ /* */ /* OUTPUT */ /* */ /* status */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_ghash_update Update GHASH */ /* _nx_crypto_gcm_inc32 Increase the counter by one */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_aes_gcm_operation Handle AES encrypt or decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_gcm_encrypt_init(VOID *crypto_metadata, NX_CRYPTO_GCM *gcm_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), VOID *additional_data, UINT additional_len, UCHAR *iv, UINT block_size) { UCHAR *hkey = gcm_metadata -> nx_crypto_gcm_hkey; UCHAR *j0 = gcm_metadata -> nx_crypto_gcm_j0; UCHAR *s = gcm_metadata -> nx_crypto_gcm_s; UCHAR *counter = gcm_metadata -> nx_crypto_gcm_counter; UCHAR tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE]; UCHAR iv_len; /* Check the block size. */ if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { return(NX_CRYPTO_PTR_ERROR); }if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { ... } /* Generate hash key by encrypt the zero block. */ NX_CRYPTO_MEMSET(hkey, 0, NX_CRYPTO_GCM_BLOCK_SIZE); crypto_function(crypto_metadata, hkey, hkey, NX_CRYPTO_GCM_BLOCK_SIZE); /* Generate the pre-counter block j0. */ iv_len = iv[0]; iv = iv + 1; if (iv_len == NX_CRYPTO_GCM_BLOCK_SIZE - 4) { /* When the length of IV is 12 then 1 is appended to IV to form j0. */ /* j0 in increased before GCTR. */ NX_CRYPTO_MEMCPY(j0, iv, iv_len); /* Use case of memcpy is verified. */ j0[12] = 0; j0[13] = 0; j0[14] = 0; j0[15] = 1; }if (iv_len == NX_CRYPTO_GCM_BLOCK_SIZE - 4) { ... } else { /* When the length of IV is not 12 then apply GHASH to the IV. */ NX_CRYPTO_MEMSET(j0, 0, NX_CRYPTO_GCM_BLOCK_SIZE); _nx_crypto_gcm_ghash_update(hkey, iv, iv_len, j0); /* Apply GHASH to the length of IV to form j0.*/ NX_CRYPTO_MEMSET(tmp_block, 0, NX_CRYPTO_GCM_BLOCK_SIZE); tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE - 2] = (UCHAR)(((iv_len << 3) & 0xFF00) >> 8); tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE - 1] = (UCHAR)((iv_len << 3) & 0x00FF); _nx_crypto_gcm_ghash_update(hkey, tmp_block, NX_CRYPTO_GCM_BLOCK_SIZE, j0); }else { ... } /* Apply GHASH to the additional authenticated data. */ NX_CRYPTO_MEMSET(s, 0, NX_CRYPTO_GCM_BLOCK_SIZE); _nx_crypto_gcm_ghash_update(hkey, additional_data, additional_len, s); /* Initial counter block for GCTR is j0 + 1. */ NX_CRYPTO_MEMCPY(counter, j0, NX_CRYPTO_GCM_BLOCK_SIZE); /* Use case of memcpy is verified. */ _nx_crypto_gcm_inc32(counter); gcm_metadata -> nx_crypto_gcm_additional_data_len = additional_len; gcm_metadata -> nx_crypto_gcm_input_total_length = 0; #ifdef NX_SECURE_KEY_CLEAR NX_CRYPTO_MEMSET(tmp_block, 0, sizeof(tmp_block)); #endif return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_encrypt_update PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function updates data for GCM encryption. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* gcm_metadata Pointer to GCM metadata */ /* crypto_function Pointer to crypto function */ /* input Pointer to bytes of input */ /* output Pointer to output buffer */ /* length Length of bytes of input */ /* block_size Block size of crypto algorithm*/ /* */ /* OUTPUT */ /* */ /* status */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_gctr Update data for GCM mode */ /* _nx_crypto_gcm_ghash_update Update GHASH */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_aes_gcm_operation Handle AES encrypt or decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_gcm_encrypt_update(VOID *crypto_metadata, NX_CRYPTO_GCM *gcm_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), UCHAR *input, UCHAR *output, UINT length, UINT block_size) { UCHAR *hkey = gcm_metadata -> nx_crypto_gcm_hkey; UCHAR *s = gcm_metadata -> nx_crypto_gcm_s; UCHAR *counter = gcm_metadata -> nx_crypto_gcm_counter; /* Check the block size. */ if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { return(NX_CRYPTO_PTR_ERROR); }if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { ... } /* Invoke GCTR function to encrypt or decrypt the input message. */ _nx_crypto_gcm_gctr(crypto_metadata, crypto_function, input, output, length, counter); /* Apply GHASH to the cipher text. */ _nx_crypto_gcm_ghash_update(hkey, output, length, s); gcm_metadata -> nx_crypto_gcm_input_total_length += length; return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_encrypt_calculate PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function calculates TAG for GCM mode. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* gcm_metadata Pointer to GCM metadata */ /* crypto_function Pointer to crypto function */ /* output Pointer to output buffer */ /* icv_len Length of TAG */ /* block_size Block size of crypto algorithm*/ /* */ /* OUTPUT */ /* */ /* status */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_gctr Update data for GCM mode */ /* _nx_crypto_gcm_ghash_update Update GHASH */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_aes_gcm_operation Handle AES encrypt or decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_gcm_encrypt_calculate(VOID *crypto_metadata, NX_CRYPTO_GCM *gcm_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), UCHAR *output, UINT icv_len, UINT block_size) { UCHAR *hkey = gcm_metadata -> nx_crypto_gcm_hkey; UCHAR *j0 = gcm_metadata -> nx_crypto_gcm_j0; UCHAR *s = gcm_metadata -> nx_crypto_gcm_s; UCHAR tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE]; UINT additional_len = gcm_metadata -> nx_crypto_gcm_additional_data_len; UINT length; /* Check the block size. */ if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { return(NX_CRYPTO_PTR_ERROR); }if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { ... } /* Apply GHASH to the length of additional authenticated data and the length of cipher text. */ length = gcm_metadata -> nx_crypto_gcm_input_total_length; tmp_block[0] = 0; tmp_block[1] = 0; tmp_block[2] = 0; tmp_block[3] = 0; tmp_block[4] = (UCHAR)(((additional_len << 3) & 0xFF000000) >> 24); tmp_block[5] = (UCHAR)(((additional_len << 3) & 0x00FF0000) >> 16); tmp_block[6] = (UCHAR)(((additional_len << 3) & 0x0000FF00) >> 8); tmp_block[7] = (UCHAR)((additional_len << 3) & 0x000000FF); tmp_block[8] = 0; tmp_block[9] = 0; tmp_block[10] = 0; tmp_block[11] = 0; tmp_block[12] = (UCHAR)(((length << 3) & 0xFF000000) >> 24); tmp_block[13] = (UCHAR)(((length << 3) & 0x00FF0000) >> 16); tmp_block[14] = (UCHAR)(((length << 3) & 0x0000FF00) >> 8); tmp_block[15] = (UCHAR)((length << 3) & 0x000000FF); _nx_crypto_gcm_ghash_update(hkey, tmp_block, NX_CRYPTO_GCM_BLOCK_SIZE, s); /* Encrypt the GHASH result using GCTR with j0 as initial counter block. The result is the authentication tag. *//* ... */ _nx_crypto_gcm_gctr(crypto_metadata, crypto_function, s, s, NX_CRYPTO_GCM_BLOCK_SIZE, j0); /* Append authentication tag to the end of the cipher text. */ NX_CRYPTO_MEMCPY(output, s, icv_len); /* Use case of memcpy is verified. */ #ifdef NX_SECURE_KEY_CLEAR NX_CRYPTO_MEMSET(tmp_block, 0, sizeof(tmp_block)); #endif return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_decrypt_update PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function updates data for GCM decryption. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* gcm_metadata Pointer to GCM metadata */ /* crypto_function Pointer to crypto function */ /* input Pointer to bytes of input */ /* output Pointer to output buffer */ /* length Length of bytes of input */ /* block_size Block size of crypto algorithm*/ /* */ /* OUTPUT */ /* */ /* status */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_gctr Update data for GCM mode */ /* _nx_crypto_gcm_ghash_update Update GHASH */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_aes_gcm_operation Handle AES encrypt or decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_gcm_decrypt_update(VOID *crypto_metadata, NX_CRYPTO_GCM *gcm_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), UCHAR *input, UCHAR *output, UINT length, UINT block_size) { UCHAR *hkey = gcm_metadata -> nx_crypto_gcm_hkey; UCHAR *s = gcm_metadata -> nx_crypto_gcm_s; UCHAR *counter = gcm_metadata -> nx_crypto_gcm_counter; /* Check the block size. */ if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { return(NX_CRYPTO_PTR_ERROR); }if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { ... } /* Apply GHASH to the cipher text. */ _nx_crypto_gcm_ghash_update(hkey, input, length, s); /* Invoke GCTR function to encrypt or decrypt the input message. */ _nx_crypto_gcm_gctr(crypto_metadata, crypto_function, input, output, length, counter); gcm_metadata -> nx_crypto_gcm_input_total_length += length; return(NX_CRYPTO_SUCCESS); }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_gcm_decrypt_calculate PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function verifies the TAG for GCM mode. */ /* */ /* INPUT */ /* */ /* crypto_metadata Pointer to crypto metadata */ /* gcm_metadata Pointer to GCM metadata */ /* crypto_function Pointer to crypto function */ /* input Pointer to TAG buffer */ /* icv_len Length of TAG */ /* block_size Block size of crypto algorithm*/ /* */ /* OUTPUT */ /* */ /* status */ /* */ /* CALLS */ /* */ /* _nx_crypto_gcm_gctr Update data for GCM mode */ /* _nx_crypto_gcm_ghash_update Update GHASH */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_aes_gcm_operation Handle AES encrypt or decrypt */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_gcm_decrypt_calculate(VOID *crypto_metadata, NX_CRYPTO_GCM *gcm_metadata, UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), UCHAR *input, UINT icv_len, UINT block_size) { UCHAR *hkey = gcm_metadata -> nx_crypto_gcm_hkey; UCHAR *j0 = gcm_metadata -> nx_crypto_gcm_j0; UCHAR *s = gcm_metadata -> nx_crypto_gcm_s; UCHAR tmp_block[NX_CRYPTO_GCM_BLOCK_SIZE]; UINT additional_len = gcm_metadata -> nx_crypto_gcm_additional_data_len; UINT length; UINT i; /* Check the block size. */ if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { return(NX_CRYPTO_PTR_ERROR); }if (block_size != NX_CRYPTO_GCM_BLOCK_SIZE) { ... } /* Apply GHASH to the length of additional authenticated data and the length of cipher text. */ length = gcm_metadata -> nx_crypto_gcm_input_total_length; tmp_block[0] = 0; tmp_block[1] = 0; tmp_block[2] = 0; tmp_block[3] = 0; tmp_block[4] = (UCHAR)(((additional_len << 3) & 0xFF000000) >> 24); tmp_block[5] = (UCHAR)(((additional_len << 3) & 0x00FF0000) >> 16); tmp_block[6] = (UCHAR)(((additional_len << 3) & 0x0000FF00) >> 8); tmp_block[7] = (UCHAR)((additional_len << 3) & 0x000000FF); tmp_block[8] = 0; tmp_block[9] = 0; tmp_block[10] = 0; tmp_block[11] = 0; tmp_block[12] = (UCHAR)(((length << 3) & 0xFF000000) >> 24); tmp_block[13] = (UCHAR)(((length << 3) & 0x00FF0000) >> 16); tmp_block[14] = (UCHAR)(((length << 3) & 0x0000FF00) >> 8); tmp_block[15] = (UCHAR)((length << 3) & 0x000000FF); _nx_crypto_gcm_ghash_update(hkey, tmp_block, NX_CRYPTO_GCM_BLOCK_SIZE, s); #ifdef NX_SECURE_KEY_CLEAR NX_CRYPTO_MEMSET(tmp_block, 0, sizeof(tmp_block)); #endif /* Encrypt the GHASH result using GCTR with j0 as initial counter block. The result is the authentication tag. *//* ... */ _nx_crypto_gcm_gctr(crypto_metadata, crypto_function, s, s, NX_CRYPTO_GCM_BLOCK_SIZE, j0); for (i = 0; i < icv_len; i++) { if (input[i] != s[i]) { /* Authentication failed. */ return(NX_CRYPTO_AUTHENTICATION_FAILED); }if (input[i] != s[i]) { ... } }for (i = 0; i < icv_len; i++) { ... } return(NX_CRYPTO_SUCCESS); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.