forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcm.c
904 lines (745 loc) · 29.9 KB
/
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
//
// gcm.c Implementation of the GCM block cipher mode
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
#include "precomp.h"
#define GCM_MIN_NONCE_SIZE (1)
#define GCM_MIN_TAG_SIZE (12)
#define GCM_MAX_TAG_SIZE (16)
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptGcmValidateParameters(
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_ SIZE_T cbNonce,
_In_ UINT64 cbAssociatedData,
_In_ UINT64 cbData,
_In_ SIZE_T cbTag )
{
if( pBlockCipher->blockSize != SYMCRYPT_GCM_BLOCK_SIZE )
{
return SYMCRYPT_WRONG_BLOCK_SIZE;
}
//
// SP800-38D specifies that the nonce must be at least one bit, but we operate on bytes,
// so the minimum is one byte.
//
if( cbNonce < GCM_MIN_NONCE_SIZE )
{
return SYMCRYPT_WRONG_NONCE_SIZE;
}
//
// cbAssociatedData is limited to <2^61 bytes
//
if( (cbAssociatedData >> 61) > 0 )
{
return SYMCRYPT_WRONG_DATA_SIZE;
}
//
// per SP800-38D cbData is limited to 2^36 - 32 bytes
//
if( cbData > SYMCRYPT_GCM_MAX_DATA_SIZE )
{
return SYMCRYPT_WRONG_DATA_SIZE;
}
if( cbTag < GCM_MIN_TAG_SIZE || cbTag > GCM_MAX_TAG_SIZE )
{
return SYMCRYPT_WRONG_TAG_SIZE;
}
return SYMCRYPT_NO_ERROR;
}
VOID
SYMCRYPT_CALL
SymCryptGcmAddMacData(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_opt_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
SIZE_T bytesToProcess;
if( pState->bytesInMacBlock > 0 )
{
bytesToProcess = SYMCRYPT_MIN( cbData, SYMCRYPT_GCM_BLOCK_SIZE - pState->bytesInMacBlock );
memcpy( &pState->macBlock[pState->bytesInMacBlock], pbData, bytesToProcess );
pbData += bytesToProcess;
cbData -= bytesToProcess;
pState->bytesInMacBlock += bytesToProcess;
if( pState->bytesInMacBlock == SYMCRYPT_GCM_BLOCK_SIZE )
{
SymCryptGHashAppendData( &pState->pKey->ghashKey,
&pState->ghashState,
&pState->macBlock[0],
SYMCRYPT_GCM_BLOCK_SIZE );
pState->bytesInMacBlock = 0;
}
}
if( cbData >= SYMCRYPT_GCM_BLOCK_SIZE )
{
bytesToProcess = cbData & SYMCRYPT_GCM_BLOCK_ROUND_MASK;
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, pbData, bytesToProcess );
pbData += bytesToProcess;
cbData -= bytesToProcess;
}
if( cbData > 0 )
{
memcpy( &pState->macBlock[0], pbData, cbData );
pState->bytesInMacBlock = cbData;
}
}
VOID
SYMCRYPT_CALL
SymCryptGcmPadMacData( _Inout_ PSYMCRYPT_GCM_STATE pState )
{
SIZE_T nBytes;
//
// Pad the MAC data with zeroes until we hit the block size.
//
nBytes = pState->bytesInMacBlock;
if( nBytes > 0 )
{
SymCryptWipe( &pState->macBlock[nBytes], SYMCRYPT_GCM_BLOCK_SIZE - nBytes );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &pState->macBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
pState->bytesInMacBlock = 0;
}
}
VOID
SYMCRYPT_CALL
SymCryptGcmEncryptDecryptPart(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
SIZE_T bytesToProcess;
SIZE_T bytesUsedInKeyStreamBuffer;
bytesUsedInKeyStreamBuffer = (SIZE_T) (pState->cbData & SYMCRYPT_GCM_BLOCK_MOD_MASK);
//
// We update pState->cbData once before we modify cbData.
// pState->cbData is not used in the rest of this function
//
SYMCRYPT_ASSERT( pState->cbData + cbData <= SYMCRYPT_GCM_MAX_DATA_SIZE );
pState->cbData += cbData;
if( bytesUsedInKeyStreamBuffer != 0 )
{
bytesToProcess = SYMCRYPT_MIN( cbData, SYMCRYPT_GCM_BLOCK_SIZE - bytesUsedInKeyStreamBuffer );
SymCryptXorBytes( pbSrc, &pState->keystreamBlock[bytesUsedInKeyStreamBuffer], pbDst, bytesToProcess );
pbSrc += bytesToProcess;
pbDst += bytesToProcess;
cbData -= bytesToProcess;
//
// If there are bytes left in the key stream buffer, then cbData == 0 and we're done.
// If we used up all the bytes, then we are fine, no need to compute the next key stream block
//
}
if( cbData >= SYMCRYPT_GCM_BLOCK_SIZE )
{
bytesToProcess = cbData & SYMCRYPT_GCM_BLOCK_ROUND_MASK;
SYMCRYPT_ASSERT( pState->pKey->pBlockCipher->blockSize == SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptCtrMsb32( pState->pKey->pBlockCipher,
&pState->pKey->blockcipherKey,
&pState->counterBlock[0],
pbSrc,
pbDst,
bytesToProcess );
pbSrc += bytesToProcess;
pbDst += bytesToProcess;
cbData -= bytesToProcess;
}
if( cbData > 0 )
{
SymCryptWipeKnownSize( &pState->keystreamBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
SYMCRYPT_ASSERT( pState->pKey->pBlockCipher->blockSize == SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptCtrMsb32( pState->pKey->pBlockCipher,
&pState->pKey->blockcipherKey,
&pState->counterBlock[0],
&pState->keystreamBlock[0],
&pState->keystreamBlock[0],
SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptXorBytes( &pState->keystreamBlock[0], pbSrc, pbDst, cbData );
//
// pState->cbData contains the data length after this call already, so it knows how many
// bytes are left in the keystream block
//
}
}
FORCEINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmResetCounterBlock(
_Inout_ PSYMCRYPT_GCM_STATE pState )
{
// Computing the tag for GCM requires invoking the GCTR function with the pre-counter
// block which was computed when the nonce was set. Historically, we only supported 12-byte
// nonces, so we could trivially reset the counter block by just setting the last 4 bytes to
// (DWORD) 1. With support for larger IVs, the pre-counter block is computed from a GHash of
// the nonce, and we don't store the value. Adding a field in the GCM struct to store the value
// would be ABI-breaking, so instead we can recompute the value by decrementing the last 32 bits
// of the counter block by the number of blocks that have been processed (since the counter is
// incremented once per block), plus one for the initial increment.
UINT32 preCounter32 = SYMCRYPT_LOAD_MSBFIRST32(&pState->counterBlock[12]) -
(UINT32) ((pState->cbData + SYMCRYPT_GCM_BLOCK_SIZE - 1) / SYMCRYPT_GCM_BLOCK_SIZE) - 1;
SYMCRYPT_STORE_MSBFIRST32(&pState->counterBlock[12], preCounter32);
}
VOID
SYMCRYPT_CALL
SymCryptGcmComputeTag(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_Out_writes_( SYMCRYPT_GCM_BLOCK_SIZE ) PBYTE pbTag )
{
SYMCRYPT_ALIGN BYTE buf[2 * SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_STORE_MSBFIRST64( &buf[16], pState->cbAuthData * 8 );
SYMCRYPT_STORE_MSBFIRST64( &buf[24], pState->cbData * 8 );
if( pState->bytesInMacBlock > 0 )
{
//
// Pad the MAC data with zeroes until we hit the block size
//
SymCryptWipeKnownSize( &buf[0], SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( buf, &pState->macBlock[0], pState->bytesInMacBlock );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[0], 2 * SYMCRYPT_GCM_BLOCK_SIZE );
}
else
{
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[16], SYMCRYPT_GCM_BLOCK_SIZE );
}
SymCryptGcmResetCounterBlock(pState);
//
// Convert the GHash state to an array of bytes
//
SYMCRYPT_STORE_MSBFIRST64( &buf[0], pState->ghashState.ull[1] );
SYMCRYPT_STORE_MSBFIRST64( &buf[8], pState->ghashState.ull[0] );
SYMCRYPT_ASSERT( pState->pKey->pBlockCipher->blockSize == SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptCtrMsb32( pState->pKey->pBlockCipher,
&pState->pKey->blockcipherKey,
&pState->counterBlock[0],
buf,
pbTag,
SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptWipeKnownSize( buf, sizeof( buf ) );
}
SYMCRYPT_NOINLINE
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptGcmExpandKey(
_Out_ PSYMCRYPT_GCM_EXPANDED_KEY pExpandedKey,
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_reads_( cbKey ) PCBYTE pbKey,
SIZE_T cbKey )
{
SYMCRYPT_ALIGN BYTE H[SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_ERROR status = SYMCRYPT_NO_ERROR;
if( cbKey > SYMCRYPT_GCM_MAX_KEY_SIZE )
{
status = SYMCRYPT_WRONG_KEY_SIZE;
goto cleanup;
}
//
// Perform the Block cipher key expansion first
//
pExpandedKey->pBlockCipher = pBlockCipher;
status = pBlockCipher->expandKeyFunc( &pExpandedKey->blockcipherKey, pbKey, cbKey );
if( status != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
//
// We keep a copy of the key to make it easy to
// implement the SymCryptGcmKeyCopy function
//
pExpandedKey->cbKey = cbKey;
memcpy( &pExpandedKey->abKey[0], pbKey, cbKey );
//
// Compute H and the GHASH expanded key
//
SymCryptWipeKnownSize( H, sizeof( H ) );
pBlockCipher->encryptFunc( &pExpandedKey->blockcipherKey, H, H );
SymCryptGHashExpandKey( &pExpandedKey->ghashKey, H );
SYMCRYPT_SET_MAGIC( pExpandedKey );
SymCryptWipeKnownSize( H, sizeof( H ) );
cleanup:
return status;
}
VOID
SYMCRYPT_CALL
SymCryptGcmKeyCopy( _In_ PCSYMCRYPT_GCM_EXPANDED_KEY pSrc, _Out_ PSYMCRYPT_GCM_EXPANDED_KEY pDst )
{
SYMCRYPT_ERROR status;
SYMCRYPT_CHECK_MAGIC( pSrc );
status = SymCryptGcmExpandKey( pDst, pSrc->pBlockCipher, &pSrc->abKey[0], pSrc->cbKey );
SYMCRYPT_ASSERT( status == SYMCRYPT_NO_ERROR );
}
VOID
SYMCRYPT_CALL
SymCryptGcmSetNonce(
_Out_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce )
{
SYMCRYPT_ASSERT( cbNonce >= GCM_MIN_NONCE_SIZE );
// Handle the nonce depending on its size, as specified in NIST SP800-38D
if( cbNonce == 12 )
{
// If len(nonce) = 96 bits (12 bytes), pre-counter block = nonce || (DWORD) 1
memcpy( &pState->counterBlock[0], pbNonce, cbNonce );
SymCryptWipeKnownSize( &pState->counterBlock[12], 4 );
pState->counterBlock[15] = 1;
}
else
{
// If len(nonce) != 96 bits (12 bytes),
// pre-counter block = GHASH(nonce padded to a multiple of 128 bits || (QWORD) len(nonce))
BYTE buf[SYMCRYPT_GF128_BLOCK_SIZE];
SIZE_T cbNonceRemainder = cbNonce & (SYMCRYPT_GF128_BLOCK_SIZE - 1);
// Process all full blocks of the nonce, i.e. all nonce bytes up to a multiple of
// SYMCRYPT_GF128_BLOCK_SIZE. SymCryptGHashAppendData ignores additional data that are
// not a multiple of the block size. We will handle any such remaining data below.
// (This also works if the nonce is less than the block size.)
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, pbNonce, cbNonce );
// If the nonce length is not a multiple of SYMCRYPT_GF128_BLOCK_SIZE, we need to pad any
// remaining data to a multiple of the block size.
if(cbNonceRemainder > 0)
{
SymCryptWipeKnownSize( buf, sizeof(buf) );
memcpy(buf, pbNonce + cbNonce - cbNonceRemainder, cbNonceRemainder);
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, buf, sizeof(buf) );
}
// Now we append the length of the nonce in bits. We take the length as a 64-bit integer,
// but it too must be padded to 128 bits for use in GHASH.
SymCryptWipeKnownSize( buf, 8 );
SYMCRYPT_STORE_MSBFIRST64( &buf[8], cbNonce * 8 );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, buf, sizeof(buf) );
SymCryptGHashResult( &pState->ghashState, pState->counterBlock );
SymCryptWipeKnownSize( &pState->ghashState, sizeof( pState->ghashState ) );
}
// Increment the last 32 bits of the counter. We'll recalculate the pre-counter block later
// when computing the tag.
SYMCRYPT_STORE_MSBFIRST32(
&pState->counterBlock[12],
1 + SYMCRYPT_LOAD_MSBFIRST32( &pState->counterBlock[12] ) );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmInit(
_Out_ PSYMCRYPT_GCM_STATE pState,
_In_ PCSYMCRYPT_GCM_EXPANDED_KEY pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce )
{
UNREFERENCED_PARAMETER( cbNonce ); // It is used in an ASSERT, but only in CHKed builds.
SYMCRYPT_CHECK_MAGIC( pExpandedKey );
pState->pKey = pExpandedKey;
pState->cbData = 0;
pState->cbAuthData = 0;
pState->bytesInMacBlock = 0;
SymCryptWipeKnownSize( &pState->ghashState, sizeof( pState->ghashState ) );
SymCryptGcmSetNonce(pState, pbNonce, cbNonce);
SYMCRYPT_SET_MAGIC( pState );
}
VOID
SYMCRYPT_CALL
SymCryptGcmStateCopy(
_In_ PCSYMCRYPT_GCM_STATE pSrc,
_In_opt_ PCSYMCRYPT_GCM_EXPANDED_KEY pExpandedKeyCopy,
_Out_ PSYMCRYPT_GCM_STATE pDst )
{
SYMCRYPT_CHECK_MAGIC( pSrc );
*pDst = *pSrc;
if( pExpandedKeyCopy != NULL )
{
pDst->pKey = pExpandedKeyCopy;
}
SYMCRYPT_SET_MAGIC( pDst );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmAuthPart(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_opt_( cbData ) PCBYTE pbAuthData,
SIZE_T cbData )
{
SYMCRYPT_CHECK_MAGIC( pState );
SYMCRYPT_ASSERT( pState->cbData == 0 );
SymCryptGcmAddMacData( pState, pbAuthData, cbData );
pState->cbAuthData += cbData;
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmEncryptPart(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
if( pState->cbData == 0 )
{
//
// This is the first actual encryption data, pad the Auth data with zeroes if needed.
//
SymCryptGcmPadMacData( pState );
}
if ( pState->pKey->pBlockCipher->gcmEncryptPartFunc != NULL )
{
//
// Use optimized implementation if available
//
(*pState->pKey->pBlockCipher->gcmEncryptPartFunc) ( pState, pbSrc, pbDst, cbData );
SYMCRYPT_ASSERT( pState->bytesInMacBlock <= 15 );
}
else
{
SymCryptGcmEncryptPartTwoPass( pState, pbSrc, pbDst, cbData );
}
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmEncryptPartTwoPass(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
//
// Do the actual encryption
//
SymCryptGcmEncryptDecryptPart( pState, pbSrc, pbDst, cbData );
//
// We break the read-once/write once rule here by reading the pbDst data back.
// In this particular situation this is safe, and avoiding it is expensive as it
// requires an extra copy and an extra memory buffer.
// The first write exposes the GCM key stream, independent of the underlying data that
// we are processing. From an attacking point of view we can think of this as literally
// handing over the key stream. So encryption consists of two steps:
// - hand over the key stream
// - MAC some ciphertext
// In this view (which has equivalent security properties to GCM) is obviously doesn't
// matter that we read pbDst back.
//
SymCryptGcmAddMacData( pState, pbDst, cbData );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmDecryptPart(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
if( pState->cbData == 0 )
{
//
// This is the first actual encryption data, pad the Auth data with zeroes if needed.
//
SymCryptGcmPadMacData( pState );
}
if ( pState->pKey->pBlockCipher->gcmDecryptPartFunc != NULL )
{
//
// Use optimized implementation if available
//
(*pState->pKey->pBlockCipher->gcmDecryptPartFunc) ( pState, pbSrc, pbDst, cbData );
SYMCRYPT_ASSERT( pState->bytesInMacBlock <= 15 );
}
else
{
SymCryptGcmDecryptPartTwoPass( pState, pbSrc, pbDst, cbData );
}
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmDecryptPartTwoPass(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
SymCryptGcmAddMacData( pState, pbSrc, cbData );
//
// Do the actual decryption
// This violates the read-once rule, but it is safe for the same reasons as above
// in the encryption case.
//
SymCryptGcmEncryptDecryptPart( pState, pbSrc, pbDst, cbData );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmEncryptFinal(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_Out_writes_( cbTag ) PBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_ALIGN BYTE buf[SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_ASSERT( cbTag >= GCM_MIN_TAG_SIZE && cbTag <= GCM_MAX_TAG_SIZE );
SymCryptGcmComputeTag( pState, &buf[0] );
memcpy( pbTag, buf, cbTag );
SymCryptWipeKnownSize( buf, sizeof( buf ) );
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
SYMCRYPT_ASSERT( pState->bytesInMacBlock == 0 );
}
SYMCRYPT_NOINLINE
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptGcmDecryptFinal(
_Inout_ PSYMCRYPT_GCM_STATE pState,
_In_reads_( cbTag ) PCBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_ALIGN BYTE buf[SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_ERROR status;
SYMCRYPT_ASSERT( cbTag >= GCM_MIN_TAG_SIZE && cbTag <= GCM_MAX_TAG_SIZE );
SymCryptGcmComputeTag( pState, &buf[0] );
if( !SymCryptEqual( pbTag, buf, cbTag ) )
{
status = SYMCRYPT_AUTHENTICATION_FAILURE;
}
else
{
status = SYMCRYPT_NO_ERROR;
}
SymCryptWipeKnownSize( buf, sizeof( buf ) );
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
SYMCRYPT_ASSERT( pState->bytesInMacBlock == 0 );
return status;
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptGcmEncrypt(
_In_ PCSYMCRYPT_GCM_EXPANDED_KEY pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce,
_In_reads_opt_( cbAuthData ) PCBYTE pbAuthData,
SIZE_T cbAuthData,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData,
_Out_writes_( cbTag ) PBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_ALIGN BYTE buf[2 * SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_GCM_STATE state;
PSYMCRYPT_GCM_STATE pState = &state;
// SymCryptGcmInit( &state, pExpandedKey, pbNonce, cbNonce );
UNREFERENCED_PARAMETER( cbNonce ); // It is used in an ASSERT, but only in CHKed builds.
SYMCRYPT_ASSERT( cbNonce >= GCM_MIN_NONCE_SIZE );
SYMCRYPT_ASSERT( cbTag >= GCM_MIN_TAG_SIZE && cbTag <= GCM_MAX_TAG_SIZE );
SYMCRYPT_CHECK_MAGIC( pExpandedKey );
pState->pKey = pExpandedKey;
pState->cbData = 0;
pState->cbAuthData = 0;
pState->bytesInMacBlock = 0;
SymCryptWipeKnownSize( &pState->ghashState, sizeof( pState->ghashState ) );
SymCryptGcmSetNonce( pState, pbNonce, cbNonce );
// SymCryptGcmAuthPart( &state, pbAuthData, cbAuthData );
pState->cbAuthData += cbAuthData;
if( cbAuthData >= SYMCRYPT_GCM_BLOCK_SIZE )
{
SIZE_T bytesToDo = cbAuthData & SYMCRYPT_GCM_BLOCK_ROUND_MASK;
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, pbAuthData, bytesToDo );
pbAuthData += bytesToDo;
cbAuthData -= bytesToDo;
}
if( cbAuthData > 0 )
{
//
// Pad the MAC data with zeroes until we hit the block size.
//
SymCryptWipeKnownSize( &pState->macBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( &pState->macBlock[0], pbAuthData, cbAuthData );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &pState->macBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
}
// SymCryptGcmEncryptPart( &state, pbSrc, pbDst, cbData );
if ( pState->pKey->pBlockCipher->gcmEncryptPartFunc != NULL )
{
//
// Use optimized implementation if available
//
(*pState->pKey->pBlockCipher->gcmEncryptPartFunc) ( pState, pbSrc, pbDst, cbData );
}
else
{
SymCryptGcmEncryptPartTwoPass( pState, pbSrc, pbDst, cbData );
}
// SymCryptGcmEncryptFinal( &state, pbTag, cbTag );
SYMCRYPT_STORE_MSBFIRST64( &buf[16], pState->cbAuthData * 8 );
SYMCRYPT_STORE_MSBFIRST64( &buf[24], pState->cbData * 8 );
if( pState->bytesInMacBlock > 0 )
{
//
// Pad the MAC data with zeroes until we hit the block size
//
SymCryptWipeKnownSize( &buf[0], SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( buf, &pState->macBlock[0], pState->bytesInMacBlock );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[0], 2 * SYMCRYPT_GCM_BLOCK_SIZE );
}
else
{
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[16], SYMCRYPT_GCM_BLOCK_SIZE );
}
// Reset the counter block prior to computing the tag
SymCryptGcmResetCounterBlock( pState );
//
// Convert the GHash state to an array of bytes
//
SYMCRYPT_STORE_MSBFIRST64( &buf[0], pState->ghashState.ull[1] );
SYMCRYPT_STORE_MSBFIRST64( &buf[8], pState->ghashState.ull[0] );
SYMCRYPT_ASSERT( pState->pKey->pBlockCipher->blockSize == SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptCtrMsb32( pState->pKey->pBlockCipher,
&pState->pKey->blockcipherKey,
&pState->counterBlock[0],
buf,
buf,
SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( pbTag, buf, cbTag );
SymCryptWipeKnownSize( buf, sizeof( buf ) );
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
}
SYMCRYPT_NOINLINE
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptGcmDecrypt(
_In_ PCSYMCRYPT_GCM_EXPANDED_KEY pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce,
_In_reads_opt_( cbAuthData ) PCBYTE pbAuthData,
SIZE_T cbAuthData,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData,
_In_reads_( cbTag ) PCBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_ERROR status;
SYMCRYPT_ALIGN BYTE buf[2 * SYMCRYPT_GCM_BLOCK_SIZE];
SYMCRYPT_GCM_STATE state;
PSYMCRYPT_GCM_STATE pState = &state;
// SymCryptGcmInit( &state, pExpandedKey, pbNonce, cbNonce );
UNREFERENCED_PARAMETER( cbNonce ); // It is used in an ASSERT, but only in CHKed builds.
SYMCRYPT_ASSERT( cbNonce >= GCM_MIN_NONCE_SIZE );
SYMCRYPT_ASSERT( cbTag >= GCM_MIN_TAG_SIZE && cbTag <= GCM_MAX_TAG_SIZE );
SYMCRYPT_CHECK_MAGIC( pExpandedKey );
pState->pKey = pExpandedKey;
pState->cbData = 0;
pState->cbAuthData = 0;
pState->bytesInMacBlock = 0;
SymCryptWipeKnownSize( &pState->ghashState, sizeof( pState->ghashState ) );
SymCryptGcmSetNonce( pState, pbNonce, cbNonce );
// SymCryptGcmAuthPart( &state, pbAuthData, cbAuthData );
pState->cbAuthData += cbAuthData;
if( cbAuthData >= SYMCRYPT_GCM_BLOCK_SIZE )
{
SIZE_T bytesToDo = cbAuthData & SYMCRYPT_GCM_BLOCK_ROUND_MASK;
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, pbAuthData, bytesToDo );
pbAuthData += bytesToDo;
cbAuthData -= bytesToDo;
}
if( cbAuthData > 0 )
{
//
// Pad the MAC data with zeroes until we hit the block size.
//
SymCryptWipeKnownSize( &pState->macBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( &pState->macBlock[0], pbAuthData, cbAuthData );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &pState->macBlock[0], SYMCRYPT_GCM_BLOCK_SIZE );
}
// SymCryptGcmDecryptPart( &state, pbSrc, pbDst, cbData );
if ( pState->pKey->pBlockCipher->gcmDecryptPartFunc != NULL )
{
//
// Use optimized implementation if available
//
(*pState->pKey->pBlockCipher->gcmDecryptPartFunc) ( pState, pbSrc, pbDst, cbData );
}
else
{
SymCryptGcmDecryptPartTwoPass( pState, pbSrc, pbDst, cbData );
}
//status = SymCryptGcmDecryptFinal( &state, pbTag, cbTag );
SYMCRYPT_STORE_MSBFIRST64( &buf[16], pState->cbAuthData * 8 );
SYMCRYPT_STORE_MSBFIRST64( &buf[24], pState->cbData * 8 );
if( pState->bytesInMacBlock > 0 )
{
//
// Pad the MAC data with zeroes until we hit the block size
//
SymCryptWipeKnownSize( &buf[0], SYMCRYPT_GCM_BLOCK_SIZE );
memcpy( buf, &pState->macBlock[0], pState->bytesInMacBlock );
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[0], 2 * SYMCRYPT_GCM_BLOCK_SIZE );
}
else
{
SymCryptGHashAppendData( &pState->pKey->ghashKey, &pState->ghashState, &buf[16], SYMCRYPT_GCM_BLOCK_SIZE );
}
SymCryptGcmResetCounterBlock( pState );
//
// Convert the GHash state to an array of bytes
//
SYMCRYPT_STORE_MSBFIRST64( &buf[0], pState->ghashState.ull[1] );
SYMCRYPT_STORE_MSBFIRST64( &buf[8], pState->ghashState.ull[0] );
SYMCRYPT_ASSERT( pState->pKey->pBlockCipher->blockSize == SYMCRYPT_GCM_BLOCK_SIZE );
SymCryptCtrMsb32( pState->pKey->pBlockCipher,
&pState->pKey->blockcipherKey,
&pState->counterBlock[0],
buf,
buf,
SYMCRYPT_GCM_BLOCK_SIZE );
if( !SymCryptEqual( pbTag, buf, cbTag ) )
{
status = SYMCRYPT_AUTHENTICATION_FAILURE;
}
else
{
status = SYMCRYPT_NO_ERROR;
}
SymCryptWipeKnownSize( buf, sizeof( buf ) );
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
if( status != SYMCRYPT_NO_ERROR )
{
SymCryptWipe( pbDst, cbData );
}
return status;
}
static const BYTE SymCryptGcmSelftestResult[3 + SYMCRYPT_AES_BLOCK_SIZE ] =
{
0xa5, 0x4c, 0x60,
0x80, 0xb0, 0x48, 0x6d, 0x03, 0x9f, 0xea, 0xc3, 0x3c, 0x28, 0x96, 0x3f, 0x99, 0x8a, 0x77, 0x43,
};
VOID
SYMCRYPT_CALL
SymCryptGcmSelftest(void)
{
BYTE buf[ 3 + SYMCRYPT_AES_BLOCK_SIZE ];
SYMCRYPT_GCM_EXPANDED_KEY key;
SYMCRYPT_ERROR err;
if( SymCryptGcmExpandKey( &key, SymCryptAesBlockCipher, SymCryptTestKey32, 16 ) != SYMCRYPT_NO_ERROR )
{
SymCryptFatal( 'gcm0' );
}
SymCryptGcmEncrypt( &key,
&SymCryptTestKey32[16], 12,
NULL, 0,
&SymCryptTestMsg3[0], buf, 3,
&buf[3], SYMCRYPT_AES_BLOCK_SIZE );
SymCryptInjectError( buf, sizeof( buf ) );
if( memcmp( buf, SymCryptGcmSelftestResult, sizeof( buf ) ) != 0 )
{
SymCryptFatal( 'gcm1' );
}
// inject error into the ciphertext or tag
SymCryptInjectError( buf, sizeof( buf ) );
err = SymCryptGcmDecrypt( &key,
&SymCryptTestKey32[16], 12,
NULL, 0,
buf, buf, 3,
&buf[3], SYMCRYPT_AES_BLOCK_SIZE );
SymCryptInjectError( buf, 3 );
if( err != SYMCRYPT_NO_ERROR || memcmp( buf, SymCryptTestMsg3, 3 ) != 0 )
{
SymCryptFatal( 'gcm2' );
}
}