forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgroup.c
2017 lines (1690 loc) · 67.8 KB
/
dlgroup.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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// dlgroup.c Dlgroup functions
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
//
#include "precomp.h"
// Miller-Rabin iterarions for prime generation
#define DLGROUP_MR_ITERATIONS (64)
// Default size for Q according to FIPS 186-3
static const struct _DSA_NBITSOFQ_CUTOFFS {
UINT32 nBitsOfP;
UINT32 nBitsOfQ;
} g_nBitsOfQ_Cutoffs[] = {
{ 1024, 160 },
{ 2048, 256 },
{ UINT32_MAX, 256 },
};
// Const label for the generation of generator G according to FIPS 186-3
static const BYTE ggen[] = { 'g', 'g', 'e', 'n' };
UINT32
SYMCRYPT_CALL
SymCryptDlgroupCalculateBitsizeOfQ( UINT32 nBitsOfP )
{
UINT32 i = 0;
while ( (i<SYMCRYPT_ARRAY_SIZE(g_nBitsOfQ_Cutoffs) - 1) &&
(g_nBitsOfQ_Cutoffs[i].nBitsOfP < nBitsOfP) )
{
i++;
};
return g_nBitsOfQ_Cutoffs[i].nBitsOfQ;
}
PSYMCRYPT_DLGROUP
SYMCRYPT_CALL
SymCryptDlgroupAllocate( UINT32 nBitsOfP, UINT32 nBitsOfQ )
{
PVOID p;
SIZE_T cb;
PSYMCRYPT_DLGROUP res = NULL;
// Invalid parameters
if ( (nBitsOfP < SYMCRYPT_DLGROUP_MIN_BITSIZE_P) ||
((nBitsOfQ > 0) && (nBitsOfQ < SYMCRYPT_DLGROUP_MIN_BITSIZE_Q)) ||
(nBitsOfP < nBitsOfQ) )
{
goto cleanup;
}
cb = SymCryptSizeofDlgroupFromBitsizes( nBitsOfP, nBitsOfQ );
p = SymCryptCallbackAlloc( cb );
if ( p==NULL )
{
goto cleanup;
}
res = SymCryptDlgroupCreate( p, cb, nBitsOfP, nBitsOfQ );
cleanup:
return res;
}
VOID
SYMCRYPT_CALL
SymCryptDlgroupFree( _Out_ PSYMCRYPT_DLGROUP pgObj )
{
SYMCRYPT_CHECK_MAGIC( pgObj );
SymCryptDlgroupWipe( pgObj );
SymCryptCallbackFree( pgObj );
}
UINT32
SYMCRYPT_CALL
SymCryptSizeofDlgroupFromBitsizes( UINT32 nBitsOfP, UINT32 nBitsOfQ )
{
UINT32 cbSeed = 0;
if (nBitsOfQ == 0)
{
nBitsOfQ = nBitsOfP-1; // Default to the maximum possible size for Q
}
// Invalid parameters
if ( (nBitsOfP < SYMCRYPT_DLGROUP_MIN_BITSIZE_P) ||
(nBitsOfQ < SYMCRYPT_DLGROUP_MIN_BITSIZE_Q) ||
(nBitsOfP < nBitsOfQ) )
{
return 0;
}
if ( nBitsOfP == nBitsOfQ )
{
nBitsOfQ--;
}
// Calculate the (tight) bytesize of the seed
cbSeed = (nBitsOfQ+7)/8;
return sizeof(SYMCRYPT_DLGROUP) +
SYMCRYPT_SIZEOF_MODULUS_FROM_BITS( nBitsOfP ) +
SYMCRYPT_SIZEOF_MODULUS_FROM_BITS( nBitsOfQ ) +
SYMCRYPT_SIZEOF_MODELEMENT_FROM_BITS( nBitsOfP ) +
((cbSeed + SYMCRYPT_ASYM_ALIGN_VALUE - 1)/SYMCRYPT_ASYM_ALIGN_VALUE)*SYMCRYPT_ASYM_ALIGN_VALUE; // Make sure that the entire structure is ASYM_ALIGNED.
}
PSYMCRYPT_DLGROUP
SYMCRYPT_CALL
SymCryptDlgroupCreate(
_Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer,
SIZE_T cbBuffer,
UINT32 nBitsOfP,
UINT32 nBitsOfQ )
{
PSYMCRYPT_DLGROUP pDlgroup = NULL;
UINT32 cbModP;
UINT32 cbModQ;
UINT32 cbModElement;
SYMCRYPT_ASSERT( cbBuffer >= SymCryptSizeofDlgroupFromBitsizes( nBitsOfP, nBitsOfQ ) );
UNREFERENCED_PARAMETER( cbBuffer ); // only referenced in ASSERTs...
SYMCRYPT_ASSERT_ASYM_ALIGNED( pbBuffer );
// Invalid parameters
if ( (nBitsOfP < SYMCRYPT_DLGROUP_MIN_BITSIZE_P) ||
((nBitsOfQ > 0) && (nBitsOfQ < SYMCRYPT_DLGROUP_MIN_BITSIZE_Q)) ||
(nBitsOfP < nBitsOfQ) )
{
goto cleanup;
}
if ( nBitsOfP == nBitsOfQ )
{
nBitsOfQ--;
}
pDlgroup = (PSYMCRYPT_DLGROUP) pbBuffer;
SYMCRYPT_ASSERT( cbBuffer > sizeof(SYMCRYPT_DLGROUP) );
// DLGROUP parameters
pDlgroup->cbTotalSize = SymCryptSizeofDlgroupFromBitsizes( nBitsOfP, nBitsOfQ );
pDlgroup->fHasPrimeQ = FALSE;
pDlgroup->nBitsOfP = nBitsOfP;
pDlgroup->cbPrimeP = (nBitsOfP+7)/8;
pDlgroup->nDigitsOfP = SymCryptDigitsFromBits( nBitsOfP );
pDlgroup->nMaxBitsOfP = nBitsOfP;
pDlgroup->nBitsOfQ = nBitsOfQ; // 0 value possible
pDlgroup->cbPrimeQ = (nBitsOfQ+7)/8; // 0 value possible
pDlgroup->nDigitsOfQ = (nBitsOfQ>0)?SymCryptDigitsFromBits( nBitsOfQ ):0; // 0 value possible
pDlgroup->nMaxBitsOfQ = (nBitsOfQ==0)?(nBitsOfP-1):nBitsOfQ;
pDlgroup->isSafePrimeGroup = FALSE;
pDlgroup->nMinBitsPriv = 0;
pDlgroup->nDefaultBitsPriv = nBitsOfQ; // 0 value possible
pDlgroup->nBitsOfSeed = nBitsOfQ; // 0 value possible
pDlgroup->cbSeed = (pDlgroup->nBitsOfSeed+7)/8; // 0 value possible
pDlgroup->eFipsStandard = SYMCRYPT_DLGROUP_FIPS_NONE; // This will be set either on generate or import
pDlgroup->pHashAlgorithm = NULL; // Like-wise
pDlgroup->dwGenCounter = 0; // Like-wise
pDlgroup->bIndexGenG = 1; // Default: 1
// Create SymCrypt objects
pbBuffer += sizeof(SYMCRYPT_DLGROUP);
cbModP = SymCryptSizeofModulusFromDigits( pDlgroup->nDigitsOfP );
SYMCRYPT_ASSERT( cbBuffer > sizeof(SYMCRYPT_DLGROUP) + cbModP );
pDlgroup->pmP = SymCryptModulusCreate( pbBuffer, cbModP, pDlgroup->nDigitsOfP );
pbBuffer += cbModP;
//
// **** Always defer the creation of the Q modulus until the group generation or
// import of the modulus. This way it is always the fastest possible even when the caller
// specified nBitsOfQ = 0.
//
if (nBitsOfQ>0)
{
cbModQ = SymCryptSizeofModulusFromDigits( pDlgroup->nDigitsOfQ );
}
else
{
cbModQ = cbModP;
}
SYMCRYPT_ASSERT( cbBuffer > sizeof(SYMCRYPT_DLGROUP) + cbModP + cbModQ );
pDlgroup->pbQ = pbBuffer; // Set the aligned buffer
pDlgroup->pmQ = NULL;
pbBuffer += cbModQ;
cbModElement = SymCryptSizeofModElementFromModulus( pDlgroup->pmP );
SYMCRYPT_ASSERT( cbBuffer > sizeof(SYMCRYPT_DLGROUP) + cbModP + cbModQ + cbModElement );
pDlgroup->peG = SymCryptModElementCreate( pbBuffer, cbModElement, pDlgroup->pmP );
pbBuffer += cbModElement;
pDlgroup->pbSeed = pbBuffer;
// Setting the magic
SYMCRYPT_SET_MAGIC( pDlgroup );
cleanup:
return pDlgroup;
}
VOID
SYMCRYPT_CALL
SymCryptDlgroupWipe( _Out_ PSYMCRYPT_DLGROUP pgDst )
{
SymCryptWipe( (PBYTE) pgDst, pgDst->cbTotalSize );
}
VOID
SYMCRYPT_CALL
SymCryptDlgroupCopy(
_In_ PCSYMCRYPT_DLGROUP pgSrc,
_Out_ PSYMCRYPT_DLGROUP pgDst )
{
//
// in-place copy is somewhat common...
//
if( pgSrc != pgDst )
{
pgDst->cbTotalSize = pgSrc->cbTotalSize;
pgDst->fHasPrimeQ = pgSrc->fHasPrimeQ;
pgDst->nBitsOfP = pgSrc->nBitsOfP;
pgDst->cbPrimeP = pgSrc->cbPrimeP;
pgDst->nDigitsOfP = pgSrc->nDigitsOfP;
pgDst->nMaxBitsOfP = pgSrc->nMaxBitsOfP;
pgDst->nBitsOfQ = pgSrc->nBitsOfQ;
pgDst->cbPrimeQ = pgSrc->cbPrimeQ;
pgDst->nDigitsOfQ = pgSrc->nDigitsOfQ;
pgDst->nMaxBitsOfQ = pgSrc->nMaxBitsOfQ;
pgDst->isSafePrimeGroup = pgSrc->isSafePrimeGroup;
pgDst->nMinBitsPriv = pgSrc->nMinBitsPriv;
pgDst->nDefaultBitsPriv = pgSrc->nDefaultBitsPriv;
pgDst->nBitsOfSeed = pgSrc->nBitsOfSeed;
pgDst->cbSeed = pgSrc->cbSeed;
pgDst->eFipsStandard = pgSrc->eFipsStandard;
pgDst->pHashAlgorithm = pgSrc->pHashAlgorithm;
pgDst->dwGenCounter = pgSrc->dwGenCounter;
pgDst->bIndexGenG = pgSrc->bIndexGenG;
pgDst->pbQ = pgSrc->pbQ;
memcpy( (PBYTE)pgDst + sizeof(SYMCRYPT_DLGROUP), (PCBYTE)pgSrc + sizeof(SYMCRYPT_DLGROUP), pgSrc->cbTotalSize - sizeof(SYMCRYPT_DLGROUP) );
}
}
// DLGROUP-specific functions
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptDlgroupGeneratePrimeQ_FIPS(
_In_ PSYMCRYPT_DLGROUP pDlgroup,
_In_ PCSYMCRYPT_TRIALDIVISION_CONTEXT
pTrialDivisionContext,
_Out_ PUINT32 pfPrimeQFound,
_Out_ PSYMCRYPT_INT piQ,
_Out_ PSYMCRYPT_DIVISOR pdDivTwoQ,
_Out_writes_bytes_( cbScratch )
PBYTE pbScratch,
SIZE_T cbScratch )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
PCSYMCRYPT_HASH hashAlgorithm = pDlgroup->pHashAlgorithm;
UINT32 nBitsOfQ = pDlgroup->nBitsOfQ;
UINT32 cbPrimeQ = pDlgroup->cbPrimeQ;
PBYTE pbSeed = pDlgroup->pbSeed;
UINT32 cbSeed = pDlgroup->cbSeed;
PSYMCRYPT_INT piDivTwoQ = SymCryptIntFromDivisor(pdDivTwoQ);
SIZE_T cbHash = SymCryptHashResultSize( hashAlgorithm );
PBYTE pbTrHash = NULL; // Pointer to the truncated hash value
PBYTE pbHashExtra = NULL; // Needed as temp buffer for 186-2
UINT32 dwShiftBits = (8-nBitsOfQ%8)%8; // When nBitsOfQ is a multiple of 8 -> dwShiftBits = 0;
UINT32 carry = 0;
UNREFERENCED_PARAMETER( cbScratch );
SYMCRYPT_ASSERT( cbScratch >= SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_TO_DIVISOR(SymCryptDigitsFromBits(nBitsOfQ+1)),
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_IS_PRIME(pDlgroup->nDigitsOfQ),
2 * cbHash )) );
SYMCRYPT_ASSERT( cbHash >= cbPrimeQ );
// Hash the seed according to the standard specified
if (pDlgroup->eFipsStandard == SYMCRYPT_DLGROUP_FIPS_186_2)
{
SYMCRYPT_ASSERT( hashAlgorithm == SymCryptSha1Algorithm );
SYMCRYPT_ASSERT( cbScratch >= SYMCRYPT_MAX(2*cbHash, cbSeed) );
// Hash buffers
pbTrHash = pbScratch;
pbHashExtra = pbTrHash + cbHash;
// Prepare an int for SEED + 1
scError = SymCryptIntSetValue( pbSeed, cbSeed, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST, piDivTwoQ );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Add 1
carry = SymCryptIntAddUint32( piDivTwoQ, 1, piDivTwoQ );
if (carry > 0)
{
// This should never happen as the size of piDivTwoQ is at least one bit bigger than Q
scError = SYMCRYPT_FIPS_FAILURE;
goto cleanup;
}
// (SEED+1) Mod 2^nBitsOfSeed
SymCryptIntModPow2( piDivTwoQ, nBitsOfQ, piDivTwoQ );
// Get the value into pbTrHash (Notice the cbSeed size)
scError = SymCryptIntGetValue( piDivTwoQ, pbTrHash, cbSeed, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Hash it into pbHashExtra
SymCryptHash( hashAlgorithm, pbTrHash, cbPrimeQ, pbHashExtra, cbHash );
// Hash the seed
SymCryptHash( hashAlgorithm, pbSeed, cbSeed, pbTrHash, cbHash );
// Xor the two
SymCryptXorBytes( pbTrHash, pbHashExtra, pbTrHash, cbHash );
}
else if (pDlgroup->eFipsStandard == SYMCRYPT_DLGROUP_FIPS_186_3)
{
SYMCRYPT_ASSERT( cbScratch >= cbHash );
pbTrHash = pbScratch;
SymCryptHash( hashAlgorithm, pbSeed, cbSeed, pbTrHash, cbHash );
}
else
{
scError = SYMCRYPT_FIPS_FAILURE;
goto cleanup;
}
// Convert it to (2^{N-1} + (Hash mod 2^{N-1})) | 1
pbTrHash += (cbHash-cbPrimeQ); // Skip any leading zero bytes
pbTrHash[0] &= ((BYTE)0xff >> (dwShiftBits)); // Cut off top bits in the most significant byte
pbTrHash[0] |= ((BYTE)0x01 << (7 - dwShiftBits)); // Set the (N-1)-th bit
pbTrHash[cbPrimeQ-1] |= ((BYTE)0x01); // Make the entire number odd
// Set the value
scError = SymCryptIntSetValue( pbTrHash, cbPrimeQ, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST, piQ );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Assume not a prime
*pfPrimeQFound = 0;
// Fast compositeness check
if (SymCryptIntFindSmallDivisor( pTrialDivisionContext, piQ, NULL, 0 ))
{
goto cleanup;
}
// IntMillerRabinPrimalityTest requirement:
// piQ > 3 since nBitsOfQ is bounded by SYMCRYPT_DLGROUP_MIN_BITSIZE_Q
*pfPrimeQFound = SymCryptIntMillerRabinPrimalityTest(
piQ,
nBitsOfQ,
DLGROUP_MR_ITERATIONS,
SYMCRYPT_FLAG_DATA_PUBLIC, // q and p will be public
pbScratch,
cbScratch );
// Set pdDivTwoQ
if (*pfPrimeQFound)
{
scError = SymCryptIntCopyMixedSize( piQ, piDivTwoQ );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
SymCryptIntMulPow2( piDivTwoQ, 1, piDivTwoQ );
// IntToDivisor requirement:
// Q is non-zero as prime --> 2*Q != 0
SymCryptIntToDivisor(
piDivTwoQ,
pdDivTwoQ,
4*pDlgroup->nBitsOfP, // 4*L
SYMCRYPT_FLAG_DATA_PUBLIC,
pbScratch,
cbScratch );
}
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptDlgroupGeneratePrimeP_FIPS(
_In_ PSYMCRYPT_DLGROUP pDlgroup,
_In_ PSYMCRYPT_DIVISOR pdDivTwoQ,
_In_ UINT32 dwMaxCounter, // Maximum value of counter (used in validation)
_In_ PCSYMCRYPT_TRIALDIVISION_CONTEXT
pTrialDivisionContext,
_Out_ PUINT32 pfPrimePFound,
_Out_ PSYMCRYPT_INT piP,
_Out_ PUINT32 pdwCounter,
_Out_writes_bytes_( cbScratch )
PBYTE pbScratch,
SIZE_T cbScratch )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
PCSYMCRYPT_HASH hashAlgorithm = pDlgroup->pHashAlgorithm;
UINT32 nBitsOfP = pDlgroup->nBitsOfP;
PBYTE pbSeed = pDlgroup->pbSeed;
UINT32 cbSeed = pDlgroup->cbSeed;
UINT32 nBitsOfSeed = pDlgroup->nBitsOfSeed;
SIZE_T cbHash = SymCryptHashResultSize( hashAlgorithm );
UINT32 counter = 0;
UINT32 ndDivTwoQ = SymCryptDivisorDigitsizeOfObject( pdDivTwoQ );
UINT32 cbIntTwoQ = SymCryptSizeofIntFromDigits( ndDivTwoQ );
PSYMCRYPT_INT piPersistent = NULL;
PSYMCRYPT_INT piRemainder = NULL;
PBYTE pbHashOutput = NULL;
PBYTE pbTempSeed = NULL;
PBYTE pbW = NULL;
UINT32 cbW = pDlgroup->cbPrimeP;
PBYTE pbWCurr = NULL;
SIZE_T cbWBytesLeft = 0;
UINT32 carry = 0;
// We will use internal scratch space at the start of pbScratch
// because cbHash, cbSeed and cbW are not necessarily aligned according
// to SYMCRYPT_ASYM_ALIGN_VALUE
PBYTE pbScratchInternal = 0;
SIZE_T cbScratchInternal = 0;
UNREFERENCED_PARAMETER( cbScratch );
SYMCRYPT_ASSERT( cbScratch >= 2*cbIntTwoQ + cbHash + cbSeed + cbW +
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_DIVMOD( pDlgroup->nDigitsOfP, ndDivTwoQ ),
SYMCRYPT_SCRATCH_BYTES_FOR_INT_IS_PRIME( pDlgroup->nDigitsOfP )) );
// Create temporaries
pbScratchInternal = pbScratch;
cbScratchInternal = SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_DIVMOD( pDlgroup->nDigitsOfP, ndDivTwoQ ),
SYMCRYPT_SCRATCH_BYTES_FOR_INT_IS_PRIME( pDlgroup->nDigitsOfP ) );
pbScratch += cbScratchInternal;
piPersistent = SymCryptIntCreate( pbScratch, cbIntTwoQ, ndDivTwoQ );
pbScratch += cbIntTwoQ;
piRemainder = SymCryptIntCreate( pbScratch, cbIntTwoQ, ndDivTwoQ );
pbScratch += cbIntTwoQ;
pbHashOutput = pbScratch;
pbScratch += cbHash;
pbTempSeed = pbScratch;
pbScratch += cbSeed;
pbW = pbScratch;
// Set the value for the expression "domain_parameter_seed + offset + j"
scError = SymCryptIntSetValue( pbSeed, cbSeed, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST, piPersistent );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// If the standard is 186-2 add 1 since the strating offset is 2
if (pDlgroup->eFipsStandard == SYMCRYPT_DLGROUP_FIPS_186_2)
{
carry = SymCryptIntAddUint32( piPersistent, 1, piPersistent );
if (carry!=0)
{
// This should never happen as piPersistent has at least one more bit than
// seedLen == nBitsOfQ
scError = SYMCRYPT_FIPS_FAILURE;
goto cleanup;
}
// Mod 2^seedlen
SymCryptIntModPow2( piPersistent, nBitsOfSeed, piPersistent );
}
*pfPrimePFound = 0;
for (counter = 0; counter < dwMaxCounter+1; counter++)
{
cbWBytesLeft = cbW; // Bytes left to write
pbWCurr = pbW + cbW - SYMCRYPT_MIN(cbW,cbHash); // Position of the first hash chunk to write (if cbW < cbHash then we write only 1 chunk)
while (cbWBytesLeft > 0)
{
// Add 1 to piPersistent
// This can never generate a carry as piPersistent has at least one more bit than
// seedLen == nBitsOfQ and in the next step we always do mod 2^seedlen.
carry = SymCryptIntAddUint32( piPersistent, 1, piPersistent );
if (carry!=0)
{
scError = SYMCRYPT_FIPS_FAILURE;
goto cleanup;
}
// Mod 2^seedlen
SymCryptIntModPow2( piPersistent, nBitsOfSeed, piPersistent );
// Extract piPersistent into a byte array (this will always be equal to domain_parameter_seed + offset + j)
scError = SymCryptIntGetValue( piPersistent, pbTempSeed, cbSeed, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Hash it
SymCryptHash( hashAlgorithm, pbTempSeed, cbSeed, pbHashOutput, cbHash );
if (cbWBytesLeft >= cbHash)
{
// Move the entire hash output to the correct location in the pbW buffer
memcpy(pbWCurr, pbHashOutput, cbHash );
}
else
{
// Move only the last bytes of the hash output
memcpy(pbWCurr, pbHashOutput + cbHash - cbWBytesLeft, cbWBytesLeft );
}
// Update the positions on the W buffer
cbWBytesLeft -= SYMCRYPT_MIN(cbHash,cbWBytesLeft);
pbWCurr -= SYMCRYPT_MIN(cbHash,cbWBytesLeft);
}
// Import the W buffer into P
scError = SymCryptIntSetValue( pbW, cbW, SYMCRYPT_NUMBER_FORMAT_MSB_FIRST, piP );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// Zero-out the top bits of the integer
SymCryptIntModPow2( piP, nBitsOfP, piP );
// Set the most significant bit
SymCryptIntSetBits( piP, 1, nBitsOfP-1, 1);
// At this point piP = X = W + 2^{L-1}
// Calculate c = X mod 2Q
SymCryptIntDivMod( piP, pdDivTwoQ, NULL, piRemainder, pbScratchInternal, cbScratchInternal );
if (SymCryptIntIsEqualUint32(piRemainder, 0))
{
// Just add one to X
// We can never get a carry here because the remainder X mod 2Q
// is 0. Therefore X is even.
carry = SymCryptIntAddUint32( piP, 1, piP );
SYMCRYPT_ASSERT( carry==0 );
}
else
{
// Subtract 1 from c
// We can never get a borrow here because the remainder is not 0.
carry = SymCryptIntSubUint32( piRemainder, 1, piRemainder );
SYMCRYPT_ASSERT( carry==0 );
// X-(c-1)
// We can never get a borrow here because c is smaller
// or equal to X.
carry = SymCryptIntSubMixedSize( piP, piRemainder, piP );
SYMCRYPT_ASSERT( carry==0 );
}
// Check if smaller than 2^{L-1} by checking the L-1 bit
if (SymCryptIntGetBit( piP, nBitsOfP-1 ) == 0)
{
continue;
}
// Fast compositeness check
if (SymCryptIntFindSmallDivisor( pTrialDivisionContext, piP, NULL, 0 ))
{
continue;
}
// IntMillerRabinPrimalityTest requirement:
// piP > 3 since nBitsOfP is bounded by SYMCRYPT_DLGROUP_MIN_BITSIZE_P
*pfPrimePFound = SymCryptIntMillerRabinPrimalityTest(
piP,
nBitsOfP,
DLGROUP_MR_ITERATIONS,
SYMCRYPT_FLAG_DATA_PUBLIC, // q and p will be public
pbScratchInternal,
cbScratchInternal );
if (*pfPrimePFound)
{
*pdwCounter = counter;
break;
}
}
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptDlgroupGenerateGenG_FIPS(
_In_ PSYMCRYPT_DLGROUP pDlgroup,
_Out_ PSYMCRYPT_MODELEMENT peG,
_Out_writes_bytes_( cbScratch )
PBYTE pbScratch,
SIZE_T cbScratch )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
PCSYMCRYPT_HASH hashAlgorithm = pDlgroup->pHashAlgorithm;
PCSYMCRYPT_MODULUS pmP = pDlgroup->pmP;
UINT32 nDigitsOfP = pDlgroup->nDigitsOfP;
UINT32 nBitsOfP = pDlgroup->nBitsOfP;
PCSYMCRYPT_MODULUS pmQ = pDlgroup->pmQ;
UINT32 nDigitsOfQ = pDlgroup->nDigitsOfQ;
PBYTE pbSeed = pDlgroup->pbSeed;
UINT32 cbSeed = pDlgroup->cbSeed;
BYTE bIndexGenG = pDlgroup->bIndexGenG;
SIZE_T cbHash = SymCryptHashResultSize( hashAlgorithm );
SYMCRYPT_ASSERT( cbHash == hashAlgorithm->resultSize );
SIZE_T cbState = SymCryptHashStateSize( hashAlgorithm );
SYMCRYPT_ASSERT( cbState == hashAlgorithm->stateSize );
UINT16 count = 0;
BYTE bTmp = 0;
PSYMCRYPT_INT piExp = NULL;
PSYMCRYPT_INT piRem = NULL;
PSYMCRYPT_MODELEMENT peOne = NULL;
PBYTE pbState = NULL;
PBYTE pbW = NULL;
UINT32 cbExp = SymCryptSizeofIntFromDigits( nDigitsOfP );
UINT32 cbRem = SymCryptSizeofIntFromDigits( nDigitsOfQ );
UINT32 cbModElement = SymCryptSizeofModElementFromModulus( pmP );
UINT32 borrow = 0;
// We will use internal scratch space at the start of pbScratch
// because cbHash is not necessarily aligned according
// to SYMCRYPT_ASYM_ALIGN_VALUE
PBYTE pbScratchInternal = 0;
SIZE_T cbScratchInternal = 0;
UNREFERENCED_PARAMETER( cbScratch );
UNREFERENCED_PARAMETER( nDigitsOfQ );
// Create temporaries
pbScratchInternal = pbScratch;
cbScratchInternal = SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_MODEXP( nDigitsOfP ),
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_DIVMOD( nDigitsOfP, nDigitsOfQ ),
SYMCRYPT_SCRATCH_BYTES_FOR_COMMON_MOD_OPERATIONS( nDigitsOfP ) ));
SYMCRYPT_ASSERT( cbScratch >= cbScratchInternal + cbExp + cbRem );
SYMCRYPT_ASSERT( cbScratch >= cbScratchInternal + cbExp + cbModElement + cbHash + cbState );
pbScratch += cbScratchInternal;
piExp = SymCryptIntCreate( pbScratch, cbExp, nDigitsOfP );
pbScratch += cbExp;
piRem = SymCryptIntCreate( pbScratch, cbRem, nDigitsOfQ );
// Calculate the exponent e = (p-1)/q
borrow = SymCryptIntSubUint32( SymCryptIntFromModulus((PSYMCRYPT_MODULUS)pmP), 1, piExp );
if (borrow!=0)
{
// The only way to get a borrow here is if the imported prime P
// is zero and we generate a G from P and Q.
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
SymCryptIntDivMod(
piExp,
SymCryptDivisorFromModulus( (PSYMCRYPT_MODULUS)pmQ ),
piExp,
piRem,
pbScratchInternal,
cbScratchInternal );
if ( !SymCryptIntIsEqualUint32(piRem, 0) )
{
// The only way to get a non-zero remainder is if Q does not divide P-1
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
// To reach here we have guaranteed that P and Q are odd, with bitlength >= 32b, and Q divides P-1.
// It follows that piExp >= 2, as it must be even and non-zero.
peOne = SymCryptModElementCreate( pbScratch, cbModElement, pmP);
pbScratch += cbModElement;
pbState = pbScratch;
pbScratch += cbState;
pbW = pbScratch;
// Initialize the hash state
SymCryptHashInit( hashAlgorithm, pbState );
// Set the modelement equal to one
SymCryptModElementSetValueUint32( 1, pmP, peOne, pbScratchInternal, cbScratchInternal );
do
{
count += 1;
if (count == 0)
{
scError = SYMCRYPT_FIPS_FAILURE;
goto cleanup;
}
// Hash the seed
SymCryptHashAppend( hashAlgorithm, pbState, pbSeed, cbSeed );
// Hash the "ggen" string
SymCryptHashAppend( hashAlgorithm, pbState, ggen, sizeof(ggen) );
// Hash the index
SymCryptHashAppend( hashAlgorithm, pbState, &bIndexGenG, sizeof(bIndexGenG) );
// Hash the count (in MSB)
bTmp = (BYTE)(count >> 8);
SymCryptHashAppend( hashAlgorithm, pbState, &bTmp, sizeof(bTmp) );
bTmp = (BYTE)count;
SymCryptHashAppend( hashAlgorithm, pbState, &bTmp, sizeof(bTmp) );
// Result into W
SymCryptHashResult( hashAlgorithm, pbState, pbW, cbHash );
// Set this into G
scError = SymCryptModElementSetValue(
pbW,
cbHash,
SYMCRYPT_NUMBER_FORMAT_MSB_FIRST,
pmP,
peG,
pbScratchInternal,
cbScratchInternal );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}
// ModExp G in place
SymCryptModExp(
pmP,
peG,
piExp,
nBitsOfP,
SYMCRYPT_FLAG_DATA_PUBLIC,
peG,
pbScratchInternal,
cbScratchInternal );
} while (SymCryptModElementIsZero( pmP, peG ) || SymCryptModElementIsEqual( pmP, peG, peOne ));
cleanup:
return scError;
}
// Scratch space requirements for the entire FIPS standards generation of P,Q,G
UINT32
SYMCRYPT_CALL
SymCryptDlgroupScratchSpace_FIPS( UINT32 nBitsOfP, UINT32 nBitsOfQ, PCSYMCRYPT_HASH pHashAlgorithm )
{
UINT32 nDigitsOfP = SymCryptDigitsFromBits( nBitsOfP );
UINT32 nDigitsOfQ = SymCryptDigitsFromBits( nBitsOfQ );
UINT32 ndDivTwoQ = SymCryptDigitsFromBits(nBitsOfQ + 1);
UINT32 cbPrimeP = (nBitsOfP+7)/8; // Note: The upper bound for nBitsOfP is enforced by SymCryptDigitsFromBits
UINT32 cbDivTwoQ = SymCryptSizeofDivisorFromDigits(ndDivTwoQ);
UINT32 cbIntTwoQ = SymCryptSizeofIntFromDigits( ndDivTwoQ );
UINT32 cbSeed = (nBitsOfQ+7)/8; // Note: The upper bound for nBitsOfP is enforced by SymCryptDigitsFromBits
UINT32 cbExp = SymCryptSizeofIntFromDigits( nDigitsOfP );
UINT32 cbRem = SymCryptSizeofIntFromDigits( nDigitsOfQ );
UINT32 cbModElement = SYMCRYPT_SIZEOF_MODELEMENT_FROM_BITS( nBitsOfP );
UINT32 cbHash = (UINT32)SymCryptHashResultSize( pHashAlgorithm );
UINT32 cbState = (UINT32) SymCryptHashStateSize( pHashAlgorithm );
//
// From symcrypt_internal.h we have:
// - sizeof results are upper bounded by 2^19
// - SYMCRYPT_SCRATCH_BYTES results are upper bounded by 2^27 (including RSA and ECURVE)
// Thus the following calculation does not overflow the result and is bounded by 2^28.
//
return SYMCRYPT_MAX( cbDivTwoQ + SYMCRYPT_MAX(
// Generate Q
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_TO_DIVISOR( ndDivTwoQ ),
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_IS_PRIME( nDigitsOfQ ),
2 * cbHash)),
// Generate P
2*cbIntTwoQ + cbHash + cbSeed + cbPrimeP +
SYMCRYPT_MAX( SYMCRYPT_SCRATCH_BYTES_FOR_INT_DIVMOD( nDigitsOfP, ndDivTwoQ ),
SYMCRYPT_SCRATCH_BYTES_FOR_INT_IS_PRIME( nDigitsOfP )) ),
SYMCRYPT_MAX(
// Convert P and Q to moduli
SYMCRYPT_SCRATCH_BYTES_FOR_INT_TO_MODULUS( nDigitsOfP ),
// Generate GenG
cbExp + SYMCRYPT_MAX(cbRem, cbModElement + cbState + cbHash) +
SYMCRYPT_MAX(SYMCRYPT_SCRATCH_BYTES_FOR_MODEXP( nDigitsOfP ),
SYMCRYPT_MAX(SYMCRYPT_SCRATCH_BYTES_FOR_INT_DIVMOD( nDigitsOfP, nDigitsOfQ ),
SYMCRYPT_SCRATCH_BYTES_FOR_COMMON_MOD_OPERATIONS( nDigitsOfP ) )) ));
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptDlgroupGenerate(
_In_ PCSYMCRYPT_HASH hashAlgorithm,
_In_ SYMCRYPT_DLGROUP_FIPS fipsStandard,
_Inout_ PSYMCRYPT_DLGROUP pDlgroup )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
PBYTE pbScratch = NULL;
SIZE_T cbScratch = 0;
PBYTE pbScratchInternal = NULL;
SIZE_T cbScratchInternal = 0;
UINT32 fPrimeQFound = 0;
UINT32 fPrimePFound = 0;
// A divisor equal to 2*Q will be needed for the generation of P
PSYMCRYPT_DIVISOR pdDivTwoQ = NULL;
UINT32 cbDivTwoQ = 0;
UINT32 ndDivTwoQ = 0;
UINT32 nBitsOfP = 0;
UINT32 nDigitsOfP = 0;
UINT32 nBitsOfQ = 0;
UINT32 nDigitsOfQ = 0;
PCSYMCRYPT_TRIALDIVISION_CONTEXT pTrialDivisionContext = NULL;
if (fipsStandard == SYMCRYPT_DLGROUP_FIPS_NONE)
{
fipsStandard = SYMCRYPT_DLGROUP_FIPS_LATEST;
}
// Numbered comments refer to the steps in the FIPS standard
// 1. Check that L,N is in the list of acceptable pairs
// => Skipped as SymCrypt supports more sizes
// 2. Check that seedlen >= N
// => Skipped as we always have seedlen == N (see below)
// Make sure that a hash algorithm is passed (if needed)
// and set the FIPS standard
if (fipsStandard == SYMCRYPT_DLGROUP_FIPS_186_2)
{
if (hashAlgorithm != NULL)
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
pDlgroup->eFipsStandard = fipsStandard;
hashAlgorithm = SymCryptSha1Algorithm;
}
else
{
if (hashAlgorithm == NULL)
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
pDlgroup->eFipsStandard = fipsStandard;
}
// If during allocation the caller didn't know the size of Q
// and set it to 0, pick the default bitsize here
// and fix all the zero parameters.
if (pDlgroup->nBitsOfQ == 0)
{
pDlgroup->nBitsOfQ = SymCryptDlgroupCalculateBitsizeOfQ(pDlgroup->nBitsOfP);
if (pDlgroup->nBitsOfQ > pDlgroup->nMaxBitsOfQ)
{
scError = SYMCRYPT_FIPS_FAILURE; // This hits when nMaxBitsOfQ = (nBitsOfP-1) <= 160
goto cleanup;
}
pDlgroup->cbPrimeQ = (pDlgroup->nBitsOfQ + 7)/8;
pDlgroup->nDigitsOfQ = SymCryptDigitsFromBits( pDlgroup->nBitsOfQ );
pDlgroup->nDefaultBitsPriv = pDlgroup->nBitsOfQ;
pDlgroup->nBitsOfSeed = pDlgroup->nBitsOfQ;
pDlgroup->cbSeed = (pDlgroup->nBitsOfSeed+7)/8;
}
// Helper variables
nBitsOfP = pDlgroup->nBitsOfP;
nDigitsOfP = pDlgroup->nDigitsOfP;
nBitsOfQ = pDlgroup->nBitsOfQ;
nDigitsOfQ = pDlgroup->nDigitsOfQ;
// Create the modulus Q
pDlgroup->pmQ = SymCryptModulusCreate( pDlgroup->pbQ, SymCryptSizeofModulusFromDigits( nDigitsOfQ ), nDigitsOfQ );
// Conditions on the hash function output size
// The second condition is needed for generation of G in SymCrypt
// since it allows even very small sizes of P.
if ( (8*((UINT32)SymCryptHashResultSize( hashAlgorithm )) < nBitsOfQ) ||
(8*((UINT32)SymCryptHashResultSize( hashAlgorithm )) > nBitsOfP) )
{
scError = SYMCRYPT_INVALID_ARGUMENT;
goto cleanup;
}
// Set the group's hash algorithm
pDlgroup->pHashAlgorithm = hashAlgorithm;
// Calculate sizes for the 2*Q divisor
ndDivTwoQ = SymCryptDigitsFromBits(nBitsOfQ + 1);
cbDivTwoQ = SymCryptSizeofDivisorFromDigits(ndDivTwoQ);
// Scratch space
//
// From symcrypt_internal.h we have:
// - sizeof results are upper bounded by 2^19
// - SYMCRYPT_SCRATCH_BYTES results are upper bounded by 2^27 (including RSA and ECURVE)
// - SymCryptDlgroupScratchSpace_FIPS is bounded by 2^28.
//
// Thus the following calculation does not overflow cbScratch.
//
cbScratch = SymCryptDlgroupScratchSpace_FIPS( nBitsOfP, nBitsOfQ, hashAlgorithm );
pbScratch = SymCryptCallbackAlloc(cbScratch);
if (pbScratch==NULL)
{
scError = SYMCRYPT_MEMORY_ALLOCATION_FAILURE;
goto cleanup;
}
// Create a divisor 2*Q (needed for the generation of P)
pdDivTwoQ = SymCryptDivisorCreate( pbScratch, cbDivTwoQ, ndDivTwoQ );
pbScratchInternal = pbScratch + cbDivTwoQ;
cbScratchInternal = cbScratch - cbDivTwoQ;
// Create a trial division context for both P and Q
pTrialDivisionContext = SymCryptCreateTrialDivisionContext( pDlgroup->nDigitsOfP );
if (pTrialDivisionContext == NULL)
{
scError = SYMCRYPT_MEMORY_ALLOCATION_FAILURE;
goto cleanup;
}
do
{
do
{
// Fill the seed buffer in the DLGroup with seedlen bits
scError = SymCryptCallbackRandom( pDlgroup->pbSeed, pDlgroup->cbSeed );
if (scError != SYMCRYPT_NO_ERROR)
{
goto cleanup;
}