Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/symcrypt/lib/a_dispatch.c
15010 views
1
//
2
// a_dispatch.c Dispatch between different arithmetic format implementations.
3
//
4
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5
//
6
// SymCrypt can have multiple implementations of the arithmetic operations, and these can
7
// have incompatible formats used to store the integers.
8
// This file contains logic to dispatch between these incompatible formats.
9
// Currently all implementations use the default format, or "Fdef".
10
//
11
12
#include "precomp.h"
13
14
//
15
// Define the FDEF dispatch table here.
16
// This should eventually be split out so that different users of the library can use different
17
// table sets & implementation choice functions.
18
//
19
20
21
const SYMCRYPT_MODULAR_FUNCTIONS g_SymCryptModFns[] = {
22
SYMCRYPT_MOD_FUNCTIONS_FDEF_GENERIC, // Handles any type of modulus
23
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY, // Montgomery, only for odd parity-public moduli
24
25
#if 0 && SYMCRYPT_CPU_AMD64
26
27
SYMCRYPT_MOD_FUNCTIONS_FDEF369_MONTGOMERY, // optimized for 384 and 576-bit moduli
28
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULX256, // Special faster code for 256-bit Montgomery moduli, MULX-based code
29
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULXP384, // Special faster code for P-384 field modulus, MULX-based code
30
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULX, // MULX-based code, for any size (digit size = 512 bits)
31
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULX1024, // Special faster code for 1024-bit Montgomery moduli, MULX-based code
32
{NULL,},
33
34
// SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULXP256, // Special faster code for P-256 field modulus, MULX-based code
35
// SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_MULX384, // Special faster code for 384-bit Montgomery moduli, MULX-based code
36
// SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY256, // Special faster code for 256-bit Montgomery moduli
37
// SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY512, // Special faster code for 512-bit Montgomery moduli
38
// SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY1024, // Special faster code for 1024-bit Montgomery moduli
39
40
#elif 0 && SYMCRYPT_CPU_ARM64
41
42
SYMCRYPT_MOD_FUNCTIONS_FDEF369_MONTGOMERY,
43
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_ARM64256,
44
SYMCRYPT_MOD_FUNCTIONS_FDEF_MONTGOMERY_ARM64P384,
45
{NULL,},
46
{NULL,},
47
{NULL,},
48
49
#endif
50
};
51
52
#define SymCryptModLabel(_label) (_label << 16)
53
#define SymCryptModFntableGeneric (SymCryptModLabel('gM') + (0 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
54
#define SymCryptModFntableMontgomery (SymCryptModLabel('mM') + (1 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
55
#define SymCryptModFntable369Montgomery (SymCryptModLabel('9m') + (2 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
56
#define SymCryptModFntableMontgomeryMulx256 (SymCryptModLabel('2x') + (3 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
57
#define SymCryptModFntableMontgomeryMulxP384 (SymCryptModLabel('3n') + (4 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
58
#define SymCryptModFntableMontgomeryMulx (SymCryptModLabel('xM') + (5 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
59
#define SymCryptModFntableMontgomeryMulx1024 (SymCryptModLabel('1x') + (6 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
60
61
#define SymCryptModFntableMontgomeryArm64256 (SymCryptModLabel('2m') + (3 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
62
#define SymCryptModFntableMontgomeryArm64P384 (SymCryptModLabel('3n') + (4 * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
63
64
// #define SymCryptModFntableMontgomeryMulxP256 (SymCryptModLabel('2n') + (xx * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
65
// #define SymCryptModFntableMontgomeryMulx384 (SymCryptModLabel('3x') + (xx * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
66
// #define SymCryptModFntableMontgomery256 (SymCryptModLabel('2m') + (xx * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
67
// #define SymCryptModFntableMontgomery512 (SymCryptModLabel('5m') + (xx * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
68
// #define SymCryptModFntableMontgomery1024 (SymCryptModLabel('1m') + (xx * SYMCRYPT_MODULAR_FUNCTIONS_SIZE))
69
70
C_ASSERT( (sizeof( g_SymCryptModFns ) & (sizeof( g_SymCryptModFns) - 1 )) == 0 ); // size of the table must be a power of 2 to be CFG-safe.
71
72
const UINT32 g_SymCryptModFnsMask = sizeof( g_SymCryptModFns ) - sizeof( g_SymCryptModFns[0] );
73
74
//
75
// Tweaking the selection & function tables allows different tradeoffs of performance vs codesize
76
//
77
const SYMCRYPT_MODULUS_TYPE_SELECTION_ENTRY SymCryptModulusTypeSelections[] =
78
{
79
#if 0 && SYMCRYPT_CPU_AMD64
80
// Mulx used for 0-512 and 577-... bits
81
{SymCryptModFntableMontgomeryMulxP384, SYMCRYPT_CPU_FEATURES_FOR_MULX, 384, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY | SYMCRYPT_MODULUS_FEATURE_NISTP384 },
82
{SymCryptModFntableMontgomeryMulx256, SYMCRYPT_CPU_FEATURES_FOR_MULX, 256, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
83
{SymCryptModFntableMontgomeryMulx, SYMCRYPT_CPU_FEATURES_FOR_MULX, 512, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
84
{SymCryptModFntable369Montgomery, 0, 384, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
85
{SymCryptModFntableMontgomery, 0, 512, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
86
{SymCryptModFntable369Montgomery, 0, 576, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
87
{SymCryptModFntableMontgomeryMulx1024, SYMCRYPT_CPU_FEATURES_FOR_MULX, 1024, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
88
{SymCryptModFntableMontgomeryMulx, SYMCRYPT_CPU_FEATURES_FOR_MULX, 0, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
89
90
#elif 0 && SYMCRYPT_CPU_ARM64
91
92
{SymCryptModFntableMontgomeryArm64P384, 0, 384, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY | SYMCRYPT_MODULUS_FEATURE_NISTP384 },
93
{SymCryptModFntableMontgomeryArm64256, 0, 256, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
94
{SymCryptModFntable369Montgomery, 0, 384, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
95
{SymCryptModFntableMontgomery, 0, 512, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
96
{SymCryptModFntable369Montgomery, 0, 576, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
97
98
#endif
99
100
{SymCryptModFntableMontgomery, 0, 0, SYMCRYPT_MODULUS_FEATURE_MONTGOMERY },
101
{SymCryptModFntableGeneric, 0, 0, 0 },
102
// This last entry always matches, so the code never falls off the end of this table.
103
};
104
105
106
//
107
// At the moment there is only the default number format.
108
//
109
110
UINT32
111
SymCryptDigitsFromBits( UINT32 nBits )
112
{
113
return SymCryptFdefDigitsFromBits( nBits );
114
}
115
116
117
PSYMCRYPT_INT
118
SYMCRYPT_CALL
119
SymCryptIntAllocate( UINT32 nDigits )
120
{
121
return SymCryptFdefIntAllocate( nDigits );
122
}
123
124
VOID
125
SYMCRYPT_CALL
126
SymCryptIntFree( _Out_ PSYMCRYPT_INT piObj )
127
{
128
SymCryptIntWipe( piObj );
129
SymCryptCallbackFree( piObj );
130
}
131
132
UINT32
133
SYMCRYPT_CALL
134
SymCryptSizeofIntFromDigits( UINT32 nDigits )
135
{
136
return SymCryptFdefSizeofIntFromDigits( nDigits );
137
}
138
139
PSYMCRYPT_INT
140
SYMCRYPT_CALL
141
SymCryptIntCreate(
142
_Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer,
143
SIZE_T cbBuffer,
144
UINT32 nDigits )
145
{
146
return SymCryptFdefIntCreate( pbBuffer, cbBuffer, nDigits );
147
}
148
149
VOID
150
SYMCRYPT_CALL
151
SymCryptIntWipe( _Out_ PSYMCRYPT_INT piDst )
152
{
153
SYMCRYPT_CHECK_MAGIC( piDst );
154
155
// Wipe the whole structure in one go;
156
SymCryptWipe( piDst, piDst->cbSize );
157
}
158
159
VOID
160
SYMCRYPT_CALL
161
SymCryptIntCopy(
162
_In_ PCSYMCRYPT_INT piSrc,
163
_Out_ PSYMCRYPT_INT piDst )
164
{
165
SymCryptFdefIntCopy( piSrc, piDst );
166
}
167
168
VOID
169
SYMCRYPT_CALL
170
SymCryptIntMaskedCopy(
171
_In_ PCSYMCRYPT_INT piSrc,
172
_Inout_ PSYMCRYPT_INT piDst,
173
UINT32 mask )
174
{
175
SymCryptFdefIntMaskedCopy( piSrc, piDst, mask );
176
}
177
178
VOID
179
SYMCRYPT_CALL
180
SymCryptIntConditionalCopy(
181
_In_ PCSYMCRYPT_INT piSrc,
182
_Inout_ PSYMCRYPT_INT piDst,
183
UINT32 cond )
184
{
185
SymCryptFdefIntConditionalCopy( piSrc, piDst, cond );
186
}
187
188
VOID
189
SYMCRYPT_CALL
190
SymCryptIntConditionalSwap(
191
_Inout_ PSYMCRYPT_INT piSrc1,
192
_Inout_ PSYMCRYPT_INT piSrc2,
193
UINT32 cond )
194
{
195
SymCryptFdefIntConditionalSwap( piSrc1, piSrc2, cond );
196
}
197
198
UINT32
199
SYMCRYPT_CALL
200
SymCryptIntBitsizeOfObject( _In_ PCSYMCRYPT_INT piSrc )
201
{
202
return SymCryptFdefIntBitsizeOfObject( piSrc );
203
}
204
205
UINT32
206
SYMCRYPT_CALL
207
SymCryptIntDigitsizeOfObject( _In_ PCSYMCRYPT_INT piSrc )
208
{
209
return piSrc->nDigits;
210
}
211
212
SYMCRYPT_ERROR
213
SYMCRYPT_CALL
214
SymCryptIntCopyMixedSize(
215
_In_ PCSYMCRYPT_INT piSrc,
216
_Out_ PSYMCRYPT_INT piDst )
217
{
218
return SymCryptFdefIntCopyMixedSize( piSrc, piDst );
219
}
220
221
UINT32
222
SYMCRYPT_CALL
223
SymCryptIntBitsizeOfValue( _In_ PCSYMCRYPT_INT piSrc )
224
{
225
return SymCryptFdefIntBitsizeOfValue( piSrc );
226
}
227
228
VOID
229
SYMCRYPT_CALL
230
SymCryptIntSetValueUint32(
231
UINT32 u32Src,
232
_Out_ PSYMCRYPT_INT piDst )
233
{
234
SymCryptFdefIntSetValueUint32( u32Src, piDst );
235
}
236
237
VOID
238
SYMCRYPT_CALL
239
SymCryptIntSetValueUint64(
240
UINT64 u64Src,
241
_Out_ PSYMCRYPT_INT piDst )
242
{
243
SymCryptFdefIntSetValueUint64( u64Src, piDst );
244
}
245
246
SYMCRYPT_ERROR
247
SYMCRYPT_CALL
248
SymCryptIntSetValue(
249
_In_reads_bytes_(cbSrc) PCBYTE pbSrc,
250
SIZE_T cbSrc,
251
SYMCRYPT_NUMBER_FORMAT format,
252
_Out_ PSYMCRYPT_INT piDst )
253
{
254
return SymCryptFdefIntSetValue( pbSrc, cbSrc, format, piDst );
255
}
256
257
SYMCRYPT_ERROR
258
SYMCRYPT_CALL
259
SymCryptIntGetValue(
260
_In_ PCSYMCRYPT_INT piSrc,
261
_Out_writes_bytes_( cbDst ) PBYTE pbDst,
262
SIZE_T cbDst,
263
SYMCRYPT_NUMBER_FORMAT format )
264
{
265
return SymCryptFdefIntGetValue( piSrc, pbDst, cbDst, format );
266
}
267
268
UINT32
269
SYMCRYPT_CALL
270
SymCryptIntGetValueLsbits32( _In_ PCSYMCRYPT_INT piSrc )
271
{
272
return SymCryptFdefIntGetValueLsbits32( piSrc );
273
}
274
275
UINT64
276
SYMCRYPT_CALL
277
SymCryptIntGetValueLsbits64( _In_ PCSYMCRYPT_INT piSrc )
278
{
279
return SymCryptFdefIntGetValueLsbits64( piSrc );
280
}
281
282
UINT32
283
SYMCRYPT_CALL
284
SymCryptIntAddUint32(
285
_In_ PCSYMCRYPT_INT piSrc1,
286
UINT32 u32Src2,
287
_Out_ PSYMCRYPT_INT piDst )
288
{
289
return SymCryptFdefIntAddUint32( piSrc1, u32Src2, piDst );
290
}
291
292
UINT32
293
SYMCRYPT_CALL
294
SymCryptIntAddSameSize(
295
_In_ PCSYMCRYPT_INT piSrc1,
296
_In_ PCSYMCRYPT_INT piSrc2,
297
_Out_ PSYMCRYPT_INT piDst )
298
{
299
return SymCryptFdefIntAddSameSize( piSrc1, piSrc2, piDst );
300
}
301
302
UINT32
303
SYMCRYPT_CALL
304
SymCryptIntAddMixedSize(
305
_In_ PCSYMCRYPT_INT piSrc1,
306
_In_ PCSYMCRYPT_INT piSrc2,
307
_Out_ PSYMCRYPT_INT piDst )
308
{
309
return SymCryptFdefIntAddMixedSize( piSrc1, piSrc2, piDst );
310
}
311
312
UINT32
313
SYMCRYPT_CALL
314
SymCryptIntSubUint32(
315
_In_ PCSYMCRYPT_INT piSrc1,
316
UINT32 u32Src2,
317
_Out_ PSYMCRYPT_INT piDst )
318
{
319
return SymCryptFdefIntSubUint32( piSrc1, u32Src2, piDst );
320
}
321
322
UINT32
323
SYMCRYPT_CALL
324
SymCryptIntSubSameSize(
325
_In_ PCSYMCRYPT_INT piSrc1,
326
_In_ PCSYMCRYPT_INT piSrc2,
327
_Out_ PSYMCRYPT_INT piDst )
328
{
329
return SymCryptFdefIntSubSameSize( piSrc1, piSrc2, piDst );
330
}
331
332
UINT32
333
SYMCRYPT_CALL
334
SymCryptIntSubMixedSize(
335
_In_ PCSYMCRYPT_INT piSrc1,
336
_In_ PCSYMCRYPT_INT piSrc2,
337
_Out_ PSYMCRYPT_INT piDst )
338
{
339
return SymCryptFdefIntSubMixedSize( piSrc1, piSrc2, piDst );
340
}
341
342
VOID
343
SYMCRYPT_CALL
344
SymCryptIntNeg(
345
_In_ PCSYMCRYPT_INT piSrc,
346
_Out_ PSYMCRYPT_INT piDst )
347
{
348
SymCryptFdefIntNeg( piSrc, piDst );
349
}
350
351
352
VOID
353
SYMCRYPT_CALL
354
SymCryptIntMulPow2(
355
_In_ PCSYMCRYPT_INT piSrc,
356
SIZE_T exp,
357
_Out_ PSYMCRYPT_INT piDst )
358
{
359
SymCryptFdefIntMulPow2( piSrc, exp, piDst );
360
}
361
362
VOID
363
SYMCRYPT_CALL
364
SymCryptIntDivPow2(
365
_In_ PCSYMCRYPT_INT piSrc,
366
SIZE_T exp,
367
_Out_ PSYMCRYPT_INT piDst )
368
{
369
SymCryptFdefIntDivPow2( piSrc, exp, piDst );
370
}
371
372
VOID
373
SYMCRYPT_CALL
374
SymCryptIntShr1(
375
UINT32 highestBit,
376
_In_ PCSYMCRYPT_INT piSrc,
377
_Out_ PSYMCRYPT_INT piDst )
378
{
379
SymCryptFdefIntShr1( highestBit, piSrc, piDst );
380
}
381
382
VOID
383
SYMCRYPT_CALL
384
SymCryptIntModPow2(
385
_In_ PCSYMCRYPT_INT piSrc,
386
SIZE_T exp,
387
_Out_ PSYMCRYPT_INT piDst )
388
{
389
SymCryptFdefIntModPow2( piSrc, exp, piDst );
390
}
391
392
UINT32
393
SYMCRYPT_CALL
394
SymCryptIntGetBit(
395
_In_ PCSYMCRYPT_INT piSrc,
396
UINT32 iBit )
397
{
398
return SymCryptFdefIntGetBit( piSrc, iBit );
399
}
400
401
UINT32
402
SYMCRYPT_CALL
403
SymCryptIntGetBits(
404
_In_ PCSYMCRYPT_INT piSrc,
405
UINT32 iBit,
406
UINT32 nBits )
407
{
408
return SymCryptFdefIntGetBits( piSrc, iBit, nBits );
409
}
410
411
VOID
412
SYMCRYPT_CALL
413
SymCryptIntSetBits(
414
_In_ PSYMCRYPT_INT piDst,
415
UINT32 value,
416
UINT32 iBit,
417
UINT32 nBits )
418
{
419
SymCryptFdefIntSetBits( piDst, value, iBit, nBits );
420
}
421
422
UINT32
423
SYMCRYPT_CALL
424
SymCryptIntIsEqualUint32(
425
_In_ PCSYMCRYPT_INT piSrc1,
426
_In_ UINT32 u32Src2 )
427
{
428
return SymCryptFdefIntIsEqualUint32( piSrc1, u32Src2 );
429
}
430
431
UINT32
432
SYMCRYPT_CALL
433
SymCryptIntIsEqual(
434
_In_ PCSYMCRYPT_INT piSrc1,
435
_In_ PCSYMCRYPT_INT piSrc2 )
436
{
437
return SymCryptFdefIntIsEqual( piSrc1, piSrc2 );
438
}
439
440
UINT32
441
SYMCRYPT_CALL
442
SymCryptIntIsLessThan(
443
_In_ PCSYMCRYPT_INT piSrc1,
444
_In_ PCSYMCRYPT_INT piSrc2 )
445
{
446
return SymCryptFdefIntIsLessThan( piSrc1, piSrc2 );
447
}
448
449
UINT32
450
SYMCRYPT_CALL
451
SymCryptIntMulUint32(
452
_In_ PCSYMCRYPT_INT piSrc1,
453
UINT32 Src2,
454
_Out_ PSYMCRYPT_INT piDst )
455
{
456
return SymCryptFdefIntMulUint32( piSrc1, Src2, piDst );
457
}
458
459
VOID
460
SYMCRYPT_CALL
461
SymCryptIntMulSameSize(
462
_In_ PCSYMCRYPT_INT piSrc1,
463
_In_ PCSYMCRYPT_INT piSrc2,
464
_Out_ PSYMCRYPT_INT piDst,
465
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
466
SIZE_T cbScratch )
467
{
468
SymCryptFdefIntMulSameSize( piSrc1, piSrc2, piDst, pbScratch, cbScratch );
469
}
470
471
472
VOID
473
SYMCRYPT_CALL
474
SymCryptIntSquare(
475
_In_ PCSYMCRYPT_INT piSrc,
476
_Out_ PSYMCRYPT_INT piDst,
477
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
478
SIZE_T cbScratch )
479
{
480
SymCryptFdefIntSquare( piSrc, piDst, pbScratch, cbScratch );
481
}
482
483
VOID
484
SYMCRYPT_CALL
485
SymCryptIntMulMixedSize(
486
_In_ PCSYMCRYPT_INT piSrc1,
487
_In_ PCSYMCRYPT_INT piSrc2,
488
_Out_ PSYMCRYPT_INT piDst,
489
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
490
SIZE_T cbScratch )
491
{
492
SymCryptFdefIntMulMixedSize( piSrc1, piSrc2, piDst, pbScratch, cbScratch );
493
}
494
495
PSYMCRYPT_DIVISOR
496
SYMCRYPT_CALL
497
SymCryptDivisorAllocate( UINT32 nDigits )
498
{
499
return SymCryptFdefDivisorAllocate( nDigits );
500
}
501
502
VOID
503
SYMCRYPT_CALL
504
SymCryptDivisorFree( _Out_ PSYMCRYPT_DIVISOR pdObj )
505
{
506
SymCryptDivisorWipe( pdObj );
507
SymCryptCallbackFree( pdObj );
508
}
509
510
UINT32
511
SYMCRYPT_CALL
512
SymCryptSizeofDivisorFromDigits( UINT32 nDigits )
513
{
514
return SymCryptFdefSizeofDivisorFromDigits( nDigits );
515
}
516
517
PSYMCRYPT_DIVISOR
518
SYMCRYPT_CALL
519
SymCryptDivisorCreate(
520
_Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer,
521
SIZE_T cbBuffer,
522
UINT32 nDigits )
523
{
524
return SymCryptFdefDivisorCreate( pbBuffer, cbBuffer, nDigits );
525
}
526
527
VOID
528
SYMCRYPT_CALL
529
SymCryptDivisorWipe( _Out_ PSYMCRYPT_DIVISOR pdObj )
530
{
531
SYMCRYPT_CHECK_MAGIC( pdObj );
532
533
SymCryptWipe( pdObj, pdObj->cbSize );
534
}
535
536
VOID
537
SymCryptDivisorCopy(
538
_In_ PCSYMCRYPT_DIVISOR pdSrc,
539
_Out_ PSYMCRYPT_DIVISOR pdDst )
540
{
541
SymCryptFdefDivisorCopy( pdSrc, pdDst );
542
}
543
544
UINT32
545
SYMCRYPT_CALL
546
SymCryptDivisorDigitsizeOfObject( _In_ PCSYMCRYPT_DIVISOR pdSrc )
547
{
548
return pdSrc->nDigits;
549
}
550
551
PSYMCRYPT_INT
552
SYMCRYPT_CALL
553
SymCryptIntFromDivisor( _In_ PSYMCRYPT_DIVISOR pdSrc )
554
{
555
return SymCryptFdefIntFromDivisor( pdSrc );
556
}
557
558
VOID
559
SYMCRYPT_CALL
560
SymCryptIntToDivisor(
561
_In_ PCSYMCRYPT_INT piSrc,
562
_Out_ PSYMCRYPT_DIVISOR pdDst,
563
UINT32 totalOperations,
564
UINT32 flags,
565
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
566
SIZE_T cbScratch )
567
{
568
SymCryptFdefIntToDivisor( piSrc, pdDst, totalOperations, flags, pbScratch, cbScratch );
569
}
570
571
VOID
572
SYMCRYPT_CALL
573
SymCryptIntDivMod(
574
_In_ PCSYMCRYPT_INT piSrc,
575
_In_ PCSYMCRYPT_DIVISOR pdDivisor,
576
_Out_opt_ PSYMCRYPT_INT piQuotient,
577
_Out_opt_ PSYMCRYPT_INT piRemainder,
578
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
579
SIZE_T cbScratch )
580
{
581
SymCryptFdefIntDivMod( piSrc, pdDivisor, piQuotient, piRemainder, pbScratch, cbScratch );
582
}
583
584
585
PSYMCRYPT_MODULUS
586
SYMCRYPT_CALL
587
SymCryptModulusAllocate( UINT32 nDigits )
588
{
589
return SymCryptFdefModulusAllocate( nDigits );
590
}
591
592
VOID
593
SYMCRYPT_CALL
594
SymCryptModulusFree( _Out_ PSYMCRYPT_MODULUS pmObj )
595
{
596
SymCryptFdefModulusFree( pmObj );
597
}
598
599
UINT32
600
SYMCRYPT_CALL
601
SymCryptSizeofModulusFromDigits( UINT32 nDigits )
602
{
603
return SymCryptFdefSizeofModulusFromDigits( nDigits );
604
}
605
606
PSYMCRYPT_MODULUS
607
SYMCRYPT_CALL
608
SymCryptModulusCreate(
609
_Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer,
610
SIZE_T cbBuffer,
611
UINT32 nDigits )
612
{
613
return SymCryptFdefModulusCreate( pbBuffer, cbBuffer, nDigits );
614
}
615
616
VOID
617
SYMCRYPT_CALL
618
SymCryptModulusWipe( _Out_ PSYMCRYPT_MODULUS pmObj )
619
{
620
SYMCRYPT_CHECK_MAGIC( pmObj );
621
622
SymCryptWipe( pmObj, pmObj->cbSize );
623
}
624
625
VOID
626
SymCryptModulusCopy(
627
_In_ PCSYMCRYPT_MODULUS pmSrc,
628
_Out_ PSYMCRYPT_MODULUS pmDst )
629
{
630
SymCryptFdefModulusCopy( pmSrc, pmDst );
631
}
632
633
UINT32
634
SYMCRYPT_CALL
635
SymCryptModulusDigitsizeOfObject( _In_ PCSYMCRYPT_MODULUS pmSrc )
636
{
637
return pmSrc->nDigits;
638
}
639
640
PSYMCRYPT_MODELEMENT
641
SYMCRYPT_CALL
642
SymCryptModElementAllocate( _In_ PCSYMCRYPT_MODULUS pmMod )
643
{
644
return SymCryptFdefModElementAllocate( pmMod );
645
}
646
647
VOID
648
SYMCRYPT_CALL
649
SymCryptModElementFree(
650
_In_ PCSYMCRYPT_MODULUS pmMod,
651
_Out_ PSYMCRYPT_MODELEMENT peObj )
652
{
653
SymCryptFdefModElementFree( pmMod, peObj );
654
}
655
656
UINT32
657
SYMCRYPT_CALL
658
SymCryptSizeofModElementFromModulus( PCSYMCRYPT_MODULUS pmMod )
659
{
660
return SymCryptFdefSizeofModElementFromModulus( pmMod );
661
}
662
663
PSYMCRYPT_MODELEMENT
664
SYMCRYPT_CALL
665
SymCryptModElementCreate(
666
_Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer,
667
SIZE_T cbBuffer,
668
_In_ PCSYMCRYPT_MODULUS pmMod )
669
{
670
return SymCryptFdefModElementCreate( pbBuffer, cbBuffer, pmMod );
671
}
672
673
VOID
674
SYMCRYPT_CALL
675
SymCryptModElementWipe(
676
_In_ PCSYMCRYPT_MODULUS pmMod,
677
_Out_ PSYMCRYPT_MODELEMENT peDst )
678
{
679
SymCryptFdefModElementWipe( pmMod, peDst );
680
}
681
682
VOID
683
SymCryptModElementCopy(
684
_In_ PCSYMCRYPT_MODULUS pmMod,
685
_In_ PCSYMCRYPT_MODELEMENT peSrc,
686
_Out_ PSYMCRYPT_MODELEMENT peDst )
687
{
688
SymCryptFdefModElementCopy( pmMod, peSrc, peDst );
689
}
690
691
VOID
692
SymCryptModElementMaskedCopy(
693
_In_ PCSYMCRYPT_MODULUS pmMod,
694
_In_ PCSYMCRYPT_MODELEMENT peSrc,
695
_Out_ PSYMCRYPT_MODELEMENT peDst,
696
UINT32 mask )
697
{
698
SymCryptFdefModElementMaskedCopy( pmMod, peSrc, peDst, mask );
699
}
700
701
PSYMCRYPT_DIVISOR
702
SYMCRYPT_CALL
703
SymCryptDivisorFromModulus( _In_ PSYMCRYPT_MODULUS pmSrc )
704
{
705
return SymCryptFdefDivisorFromModulus( pmSrc );
706
}
707
708
VOID
709
SymCryptModElementConditionalSwap(
710
_In_ PCSYMCRYPT_MODULUS pmMod,
711
_Inout_ PSYMCRYPT_MODELEMENT peData1,
712
_Inout_ PSYMCRYPT_MODELEMENT peData2,
713
_In_ UINT32 cond )
714
{
715
SymCryptFdefModElementConditionalSwap( pmMod, peData1, peData2, cond );
716
}
717
718
PSYMCRYPT_INT
719
SYMCRYPT_CALL
720
SymCryptIntFromModulus( _In_ PSYMCRYPT_MODULUS pmSrc )
721
{
722
return SymCryptFdefIntFromModulus( pmSrc );
723
}
724
725
VOID
726
SYMCRYPT_CALL
727
SymCryptIntToModulus(
728
_In_ PCSYMCRYPT_INT piSrc,
729
_Out_ PSYMCRYPT_MODULUS pmDst,
730
UINT32 averageOperations,
731
UINT32 flags,
732
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
733
SIZE_T cbScratch )
734
{
735
PSYMCRYPT_INT piSrcTweak = (PSYMCRYPT_INT) piSrc;
736
737
// In CHKed build, we'll verify that the modulus is not prime, or that it is 2 or odd
738
// (Some inversion algorithms fail hard when one input isn't 2 or odd.)
739
// We are constant-time w.r.t. piSrc being odd or =2. We don't hide the size of any input,
740
// but inputs 2 and 3 are handled with the same code path.
741
SYMCRYPT_ASSERT( ((flags & SYMCRYPT_FLAG_MODULUS_PRIME) == 0) ||
742
(((SymCryptIntGetValueLsbits32( piSrc ) & 1) | SymCryptIntIsEqualUint32( piSrc, 2 )) != 0) );
743
744
SymCryptFdefIntToModulus( piSrcTweak, pmDst, averageOperations, flags, pbScratch, cbScratch );
745
}
746
747
VOID
748
SYMCRYPT_CALL
749
SymCryptIntToModElement(
750
_In_ PCSYMCRYPT_INT piSrc,
751
_In_ PCSYMCRYPT_MODULUS pmMod,
752
_Out_ PSYMCRYPT_MODELEMENT peDst,
753
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
754
SIZE_T cbScratch )
755
{
756
SymCryptFdefIntToModElement( piSrc, pmMod, peDst, pbScratch, cbScratch );
757
}
758
759
SYMCRYPT_DISABLE_CFG
760
VOID
761
SYMCRYPT_CALL
762
SymCryptModElementToInt(
763
_In_ PCSYMCRYPT_MODULUS pmMod,
764
_In_ PCSYMCRYPT_MODELEMENT peSrc,
765
_Out_ PSYMCRYPT_INT piDst,
766
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
767
SIZE_T cbScratch )
768
{
769
PCUINT32 pData;
770
771
SYMCRYPT_ASSERT( piDst->nDigits >= pmMod->nDigits );
772
773
pData = SYMCRYPT_MOD_CALL( pmMod ) modPreGet( pmMod, peSrc, pbScratch, cbScratch );
774
775
SymCryptFdefModElementToIntGeneric( pmMod, pData, piDst, pbScratch, cbScratch );
776
}
777
778
SYMCRYPT_DISABLE_CFG
779
SYMCRYPT_ERROR
780
SYMCRYPT_CALL
781
SymCryptModElementSetValue(
782
_In_reads_bytes_( cbSrc ) PCBYTE pbSrc,
783
SIZE_T cbSrc,
784
SYMCRYPT_NUMBER_FORMAT format,
785
PCSYMCRYPT_MODULUS pmMod,
786
_Out_ PSYMCRYPT_MODELEMENT peDst,
787
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
788
SIZE_T cbScratch )
789
{
790
SYMCRYPT_ERROR scError;
791
792
scError = SymCryptFdefModElementSetValueGeneric( pbSrc, cbSrc, format, pmMod, peDst, pbScratch, cbScratch );
793
794
if( scError == SYMCRYPT_NO_ERROR )
795
{
796
SYMCRYPT_MOD_CALL( pmMod ) modSetPost( pmMod, peDst, pbScratch, cbScratch );
797
}
798
799
return scError;
800
}
801
802
SYMCRYPT_ERROR
803
SYMCRYPT_CALL
804
SymCryptModElementGetValue(
805
PCSYMCRYPT_MODULUS pmMod,
806
_In_ PCSYMCRYPT_MODELEMENT peSrc,
807
_Out_writes_bytes_( cbDst ) PBYTE pbDst,
808
SIZE_T cbDst,
809
SYMCRYPT_NUMBER_FORMAT format,
810
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
811
SIZE_T cbScratch )
812
{
813
return SymCryptFdefModElementGetValue( pmMod, peSrc, pbDst, cbDst, format, pbScratch, cbScratch );
814
}
815
816
UINT32
817
SYMCRYPT_CALL
818
SymCryptModElementIsEqual(
819
_In_ PCSYMCRYPT_MODULUS pmMod,
820
_In_ PCSYMCRYPT_MODELEMENT peSrc1,
821
_In_ PCSYMCRYPT_MODELEMENT peSrc2 )
822
{
823
return SymCryptFdefModElementIsEqual( pmMod, peSrc1, peSrc2 );
824
}
825
826
UINT32
827
SYMCRYPT_CALL
828
SymCryptModElementIsZero(
829
_In_ PCSYMCRYPT_MODULUS pmMod,
830
_In_ PCSYMCRYPT_MODELEMENT peSrc )
831
{
832
return SymCryptFdefModElementIsZero( pmMod, peSrc );
833
}
834
835
SYMCRYPT_DISABLE_CFG
836
VOID
837
SYMCRYPT_CALL
838
SymCryptModAdd(
839
_In_ PCSYMCRYPT_MODULUS pmMod,
840
_In_ PCSYMCRYPT_MODELEMENT peSrc1,
841
_In_ PCSYMCRYPT_MODELEMENT peSrc2,
842
_Out_ PSYMCRYPT_MODELEMENT peDst,
843
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
844
SIZE_T cbScratch )
845
{
846
SYMCRYPT_MOD_CALL( pmMod ) modAdd( pmMod, peSrc1, peSrc2, peDst, pbScratch, cbScratch );
847
}
848
849
SYMCRYPT_DISABLE_CFG
850
VOID
851
SYMCRYPT_CALL
852
SymCryptModSub(
853
_In_ PCSYMCRYPT_MODULUS pmMod,
854
_In_ PCSYMCRYPT_MODELEMENT peSrc1,
855
_In_ PCSYMCRYPT_MODELEMENT peSrc2,
856
_Out_ PSYMCRYPT_MODELEMENT peDst,
857
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
858
SIZE_T cbScratch )
859
{
860
SYMCRYPT_MOD_CALL( pmMod ) modSub( pmMod, peSrc1, peSrc2, peDst, pbScratch, cbScratch );
861
}
862
863
864
SYMCRYPT_DISABLE_CFG
865
VOID
866
SYMCRYPT_CALL
867
SymCryptModMul(
868
_In_ PCSYMCRYPT_MODULUS pmMod,
869
_In_ PCSYMCRYPT_MODELEMENT peSrc1,
870
_In_ PCSYMCRYPT_MODELEMENT peSrc2,
871
_Out_ PSYMCRYPT_MODELEMENT peDst,
872
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
873
SIZE_T cbScratch )
874
{
875
SYMCRYPT_MOD_CALL( pmMod ) modMul( pmMod, peSrc1, peSrc2, peDst, pbScratch, cbScratch );
876
}
877
878
SYMCRYPT_DISABLE_CFG
879
VOID
880
SYMCRYPT_CALL
881
SymCryptModSquare(
882
_In_ PCSYMCRYPT_MODULUS pmMod,
883
_In_ PCSYMCRYPT_MODELEMENT peSrc,
884
_Out_ PSYMCRYPT_MODELEMENT peDst,
885
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
886
SIZE_T cbScratch )
887
{
888
SYMCRYPT_MOD_CALL( pmMod ) modSquare( pmMod, peSrc, peDst, pbScratch, cbScratch );
889
}
890
891
892
SYMCRYPT_DISABLE_CFG
893
VOID
894
SYMCRYPT_CALL
895
SymCryptModNeg(
896
_In_ PCSYMCRYPT_MODULUS pmMod,
897
_In_ PCSYMCRYPT_MODELEMENT peSrc,
898
_Out_ PSYMCRYPT_MODELEMENT peDst,
899
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
900
SIZE_T cbScratch )
901
{
902
SYMCRYPT_MOD_CALL( pmMod ) modNeg( pmMod, peSrc, peDst, pbScratch, cbScratch );
903
}
904
905
SYMCRYPT_DISABLE_CFG
906
VOID
907
SYMCRYPT_CALL
908
SymCryptModElementSetValueUint32(
909
UINT32 value,
910
_In_ PCSYMCRYPT_MODULUS pmMod,
911
_Out_ PSYMCRYPT_MODELEMENT peDst,
912
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
913
SIZE_T cbScratch )
914
{
915
SymCryptFdefModElementSetValueUint32Generic( value, pmMod, peDst, pbScratch, cbScratch );
916
917
SYMCRYPT_MOD_CALL( pmMod ) modSetPost( pmMod, peDst, pbScratch, cbScratch );
918
}
919
920
VOID
921
SYMCRYPT_CALL
922
SymCryptModElementSetValueNegUint32(
923
UINT32 value,
924
_In_ PCSYMCRYPT_MODULUS pmMod,
925
_Out_ PSYMCRYPT_MODELEMENT peDst,
926
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
927
SIZE_T cbScratch )
928
{
929
SymCryptFdefModElementSetValueNegUint32( value, pmMod, peDst, pbScratch, cbScratch );
930
}
931
932
VOID
933
SYMCRYPT_CALL
934
SymCryptModDivPow2(
935
_In_ PCSYMCRYPT_MODULUS pmMod,
936
_In_ PCSYMCRYPT_MODELEMENT peSrc,
937
UINT32 exp,
938
_Out_ PSYMCRYPT_MODELEMENT peDst,
939
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
940
SIZE_T cbScratch )
941
{
942
SymCryptFdefModDivPow2( pmMod, peSrc, exp, peDst, pbScratch, cbScratch );
943
}
944
945
SYMCRYPT_DISABLE_CFG
946
SYMCRYPT_ERROR
947
SYMCRYPT_CALL
948
SymCryptModInv(
949
_In_ PCSYMCRYPT_MODULUS pmMod,
950
_In_ PCSYMCRYPT_MODELEMENT peSrc,
951
_Out_ PSYMCRYPT_MODELEMENT peDst,
952
UINT32 flags,
953
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
954
SIZE_T cbScratch )
955
{
956
return SYMCRYPT_MOD_CALL( pmMod ) modInv( pmMod, peSrc, peDst, flags, pbScratch, cbScratch );
957
}
958
959
VOID
960
SYMCRYPT_CALL
961
SymCryptModExp(
962
_In_ PCSYMCRYPT_MODULUS pmMod,
963
_In_ PCSYMCRYPT_MODELEMENT peBase,
964
_In_ PCSYMCRYPT_INT piExp,
965
UINT32 nBitsExp,
966
UINT32 flags,
967
_Out_ PSYMCRYPT_MODELEMENT peDst,
968
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
969
SIZE_T cbScratch )
970
{
971
SymCryptModExpGeneric( pmMod, peBase, piExp, nBitsExp, flags, peDst, pbScratch, cbScratch );
972
}
973
974
SYMCRYPT_ERROR
975
SYMCRYPT_CALL
976
SymCryptModMultiExp(
977
_In_ PCSYMCRYPT_MODULUS pmMod,
978
_In_reads_( nBases ) PCSYMCRYPT_MODELEMENT * peBaseArray,
979
_In_reads_( nBases ) PCSYMCRYPT_INT * piExpArray,
980
UINT32 nBases,
981
UINT32 nBitsExp,
982
UINT32 flags,
983
_Out_ PSYMCRYPT_MODELEMENT peDst,
984
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
985
SIZE_T cbScratch )
986
{
987
return SymCryptModMultiExpGeneric( pmMod, peBaseArray, piExpArray, nBases, nBitsExp, flags, peDst, pbScratch, cbScratch );
988
}
989
990
SYMCRYPT_DISABLE_CFG
991
VOID
992
SYMCRYPT_CALL
993
SymCryptModSetRandom(
994
_In_ PCSYMCRYPT_MODULUS pmMod,
995
_Out_ PSYMCRYPT_MODELEMENT peDst,
996
UINT32 flags,
997
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
998
SIZE_T cbScratch )
999
{
1000
SymCryptFdefModSetRandomGeneric( pmMod, peDst, flags, pbScratch, cbScratch );
1001
1002
SYMCRYPT_MOD_CALL( pmMod ) modSetPost( pmMod, peDst, pbScratch, cbScratch );
1003
}
1004
1005
PCSYMCRYPT_TRIALDIVISION_CONTEXT
1006
SYMCRYPT_CALL
1007
SymCryptCreateTrialDivisionContext( UINT32 nDigits )
1008
{
1009
return SymCryptFdefCreateTrialDivisionContext( nDigits );
1010
}
1011
1012
UINT32
1013
SYMCRYPT_CALL
1014
SymCryptIntFindSmallDivisor(
1015
_In_ PCSYMCRYPT_TRIALDIVISION_CONTEXT pContext,
1016
_In_ PCSYMCRYPT_INT piSrc,
1017
_Out_writes_bytes_( cbScratch ) PBYTE pbScratch,
1018
SIZE_T cbScratch )
1019
{
1020
return SymCryptFdefIntFindSmallDivisor( pContext, piSrc, pbScratch, cbScratch );
1021
}
1022
1023
VOID
1024
SYMCRYPT_CALL
1025
SymCryptFreeTrialDivisionContext( PCSYMCRYPT_TRIALDIVISION_CONTEXT pContext )
1026
{
1027
SymCryptFdefFreeTrialDivisionContext( pContext );
1028
}
1029
1030