forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmain.c
568 lines (479 loc) · 12.6 KB
/
libmain.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
//
// libmain.c
// General routines for the SymCrypt library
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
#include "precomp.h"
#include "C_asm_shared.inc"
#include "buildInfo.h"
// The following global g_SymCryptFlags has to be at least 32
// bits because the iOS environment has interlocked function
// support for variables of size at least 32 bits.
// The relevant function is OSAtomicOr32Barrier.
UINT32 g_SymCryptFlags = 0;
SYMCRYPT_CPU_FEATURES g_SymCryptCpuFeaturesNotPresent = (SYMCRYPT_CPU_FEATURES) ~0;
SYMCRYPT_CPU_FEATURES g_SymCryptCpuFeaturesPresentCheck = 0;
#if SYMCRYPT_DEBUG
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptLibraryWasNotInitialized(void)
{
SymCryptFatal( 'init' ); // Function name helps figure out what the problem is.
}
#endif
const CHAR * const SymCryptBuildString =
"v" SYMCRYPT_BUILD_INFO_VERSION
"_" SYMCRYPT_BUILD_INFO_BRANCH
"_" SYMCRYPT_BUILD_INFO_COMMIT
"_" SYMCRYPT_BUILD_INFO_TIMESTAMP;
VOID
SYMCRYPT_CALL
SymCryptInitEnvCommon( UINT32 version )
// Returns TRUE if the initialization steps have to be performed.
{
UINT32 tmp;
const CHAR * p;
// Assertion that verifies that the calling application was compiled with
// the same version header files as the library.
if( version != SYMCRYPT_API_VERSION )
{
SymCryptFatal( 'apiv' );
}
//
// Use an interlocked to set the flag in case we add other flags
// that are modified by different threads.
//
SYMCRYPT_ATOMIC_OR32_PRE_RELAXED( &g_SymCryptFlags, SYMCRYPT_FLAG_LIB_INITIALIZED );
//
// Do a forced write of our code version. This ensures that the code
// version is part of the binary, so we can look at a binary and figure
// out which version of SymCrypt it was linked with.
//
SYMCRYPT_FORCE_WRITE32( &tmp, SYMCRYPT_API_VERSION );
//
// Force the build string to be in memory, because otherwise the
// compiler might get smart and remove it.
// This ensures we can always track back to the SymCrypt source code from
// any binary that links this library
//
for( p = SymCryptBuildString; *p!=0; p++ )
{
SYMCRYPT_FORCE_WRITE8( (PBYTE) &tmp, *p );
}
//
// Make an inverted copy of the CPU detection results.
// This helps us diagnose corruption of our flags
// Force-write otherwise the compiler optimizes it away
//
SYMCRYPT_FORCE_WRITE32( &g_SymCryptCpuFeaturesPresentCheck, ~g_SymCryptCpuFeaturesNotPresent );
//
// Test that the C and assembler code agree on the various structure member offsets.
// This gets optimized away in FRE builds as all the values are compile-time computable.
//
#define SYMCRYPT_CHECK_ASM_OFFSET( a, b ) if( (a) != (b) ) {SymCryptFatal( b );}
SYMCRYPT_CHECK_ASM_OFFSETS;
#undef SYMCRYPT_CHECK_ASM_OFFSET
}
_Analysis_noreturn_
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptFatalHang( UINT32 fatalCode )
//
// This function is used by the environment-specific fatal code
// as a last resort when none of the other fatal methods work.
//
{
UINT32 fcode;
//
// Put the fatal code in a location we can find
//
SYMCRYPT_FORCE_WRITE32( &fcode, fatalCode );
fatalInfiniteLoop:
goto fatalInfiniteLoop;
}
#if SYMCRYPT_CPU_X86 | SYMCRYPT_CPU_AMD64 | SYMCRYPT_CPU_ARM | SYMCRYPT_CPU_ARM64
VOID
SYMCRYPT_CALL
SymCryptWipeAsm( _Out_writes_bytes_( cbData ) PVOID pbData, SIZE_T cbData );
VOID
SYMCRYPT_CALL
SymCryptWipe( _Out_writes_bytes_( cbData ) PVOID pbData, SIZE_T cbData )
{
SymCryptWipeAsm( pbData, cbData );
}
#else
//
// Generic but slow wipe routine.
//
VOID
SYMCRYPT_CALL
SymCryptWipe( _Out_writes_bytes_( cbData ) PVOID pbData, SIZE_T cbData )
{
volatile BYTE * p = (volatile BYTE *) pbData;
SIZE_T i;
for( i=0; i<cbData; i++ ){
p[i] = 0;
}
}
#endif
#if SYMCRYPT_CPU_X86 | SYMCRYPT_CPU_ARM
VOID
SYMCRYPT_CALL
SymCryptXorBytes(
_In_reads_( cbBytes ) PCBYTE pbSrc1,
_In_reads_( cbBytes ) PCBYTE pbSrc2,
_Out_writes_( cbBytes ) PBYTE pbResult,
SIZE_T cbBytes )
{
SIZE_T i;
if( cbBytes == 16 )
{
PCUINT32 s1 = (PCUINT32) pbSrc1;
PCUINT32 s2 = (PCUINT32) pbSrc2;
PUINT32 d = (PUINT32) pbResult;
d[0] = s1[0] ^ s2[0];
d[1] = s1[1] ^ s2[1];
d[2] = s1[2] ^ s2[2];
d[3] = s1[3] ^ s2[3];
}
else
{
i = 0;
while( i + 3 < cbBytes )
{
*(UINT32 *)&pbResult[i] = *(UINT32 *)&pbSrc1[i] ^ *(UINT32 *)&pbSrc2[i];
i += 4;
}
while( i < cbBytes )
{
pbResult[i] = pbSrc1[i] ^ pbSrc2[i];
i++;
}
}
}
#elif SYMCRYPT_CPU_AMD64 | SYMCRYPT_CPU_ARM64
VOID
SYMCRYPT_CALL
SymCryptXorBytes(
_In_reads_( cbBytes ) PCBYTE pbSrc1,
_In_reads_( cbBytes ) PCBYTE pbSrc2,
_Out_writes_( cbBytes ) PBYTE pbResult,
SIZE_T cbBytes )
{
if( cbBytes == 16 )
{
PCUINT64 s1 = (PCUINT64) pbSrc1;
PCUINT64 s2 = (PCUINT64) pbSrc2;
PUINT64 d = (PUINT64) pbResult;
d[0] = s1[0] ^ s2[0];
d[1] = s1[1] ^ s2[1];
}
else
{
while( cbBytes >= 8 )
__CPROVER_assigns(pbResult, pbSrc1, pbSrc2, cbBytes)
__CPROVER_loop_invariant( cbBytes >= 0 )
__CPROVER_decreases( cbBytes )
{
*(UINT64 *)pbResult = *(UINT64 *)pbSrc1 ^ *(UINT64 *)pbSrc2;
pbSrc1 += 8;
pbSrc2 += 8;
pbResult += 8;
cbBytes -= 8;
}
while( cbBytes > 0 )
__CPROVER_assigns(pbResult, pbSrc1, pbSrc2, cbBytes)
__CPROVER_loop_invariant( cbBytes >= 0 )
__CPROVER_decreases( cbBytes )
{
*pbResult = *pbSrc1 ^ *pbSrc2;
pbResult++;
pbSrc1++;
pbSrc2++;
cbBytes--;
}
}
}
#else
//
// Generic code
//
VOID
SYMCRYPT_CALL
SymCryptXorBytes(
_In_reads_( cbBytes ) PCBYTE pbSrc1,
_In_reads_( cbBytes ) PCBYTE pbSrc2,
_Out_writes_( cbBytes ) PBYTE pbResult,
SIZE_T cbBytes )
{
SIZE_T i;
for( i=0; i<cbBytes; i++ )
{
pbResult[i] = pbSrc1[i] ^ pbSrc2[i];
}
}
#endif
//
// Generic LSB/MSBfirst load/store code for variable-sized buffers.
// These implementations are inefficient and not side-channel safe.
// This is sufficient for the current usage (typically to allow
// callers to read/write RSA public exponents from/to variable-sized
// buffers).
// Consider upgrading them in future.
//
UINT32
SymCryptUint32Bitsize( UINT32 value )
//
// Some CPUs/compilers have intrinsics for this,
// but this is portable and works everywhere.
//
{
UINT32 res;
res = 0;
while( value != 0 )
{
res += 1;
value >>= 1;
}
return res;
}
UINT32
SymCryptUint64Bitsize( UINT64 value )
{
UINT32 res;
UINT32 upper;
upper = (UINT32)(value >> 32);
if( upper == 0 )
{
res = SymCryptUint32Bitsize( (UINT32) value );
} else {
res = 32 + SymCryptUint32Bitsize( upper );
}
return res;
}
UINT32
SymCryptUint32Bytesize( UINT32 value )
{
if( value == 0 )
{
return 0;
}
if( value < 0x100 )
{
return 1;
}
if( value < 0x10000 )
{
return 2;
}
if( value < 0x1000000 )
{
return 3;
}
return 4;
}
UINT32
SymCryptUint64Bytesize( UINT64 value )
{
UINT32 res;
UINT32 upper;
upper = (UINT32)(value >> 32);
if( upper == 0 )
{
res = SymCryptUint32Bytesize( (UINT32) value );
} else {
res = 4 + SymCryptUint32Bytesize( upper );
}
return res;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptLoadLsbFirstUint32(
_In_reads_( cbSrc ) PCBYTE pbSrc,
SIZE_T cbSrc,
_Out_ PUINT32 pDst )
{
UINT64 v64;
UINT32 v32;
SYMCRYPT_ERROR scError;
scError = SymCryptLoadLsbFirstUint64( pbSrc, cbSrc, &v64 );
if( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
v32 = (UINT32) v64;
if( v32 != v64 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
*pDst = v32;
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptLoadLsbFirstUint64(
_In_reads_( cbSrc ) PCBYTE pbSrc,
SIZE_T cbSrc,
_Out_ PUINT64 pDst )
{
UINT64 v;
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
v = 0;
pbSrc += cbSrc;
while( cbSrc > 8 )
__CPROVER_assigns(cbSrc, scError)
__CPROVER_loop_invariant(cbSrc <= __CPROVER_loop_entry(cbSrc))
__CPROVER_decreases(cbSrc)
{
if( *--pbSrc != 0 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
cbSrc--;
}
while( cbSrc > 0 )
__CPROVER_assigns(cbSrc, v)
__CPROVER_loop_invariant(cbSrc <= __CPROVER_loop_entry(cbSrc))
__CPROVER_decreases(cbSrc)
{
v = (v << 8) | *--pbSrc;
cbSrc--;
}
*pDst = v;
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptLoadMsbFirstUint32(
_In_reads_( cbSrc ) PCBYTE pbSrc,
SIZE_T cbSrc,
_Out_ PUINT32 pDst )
{
UINT64 v64;
UINT32 v32;
SYMCRYPT_ERROR scError;
scError = SymCryptLoadMsbFirstUint64( pbSrc, cbSrc, &v64 );
if( scError != SYMCRYPT_NO_ERROR )
{
goto cleanup;
}
v32 = (UINT32) v64;
if( v32 != v64 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
*pDst = v32;
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptLoadMsbFirstUint64(
_In_reads_( cbSrc ) PCBYTE pbSrc,
SIZE_T cbSrc,
_Out_ PUINT64 pDst )
{
UINT64 v;
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
v = 0;
while( cbSrc > 8 )
__CPROVER_assigns(cbSrc, v)
__CPROVER_loop_invariant(cbSrc >= 8 && cbSrc <= __CPROVER_loop_entry(cbSrc))
__CPROVER_decreases(cbSrc)
{
if( *pbSrc++ != 0 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
cbSrc--;
}
while( cbSrc > 0 )
__CPROVER_assigns(cbSrc, v)
__CPROVER_loop_invariant(cbSrc >= 0 && cbSrc <= __CPROVER_loop_entry(cbSrc))
__CPROVER_decreases(cbSrc)
{
v = (v << 8) | *pbSrc++;
cbSrc--;
}
*pDst = v;
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptStoreLsbFirstUint32(
UINT32 src,
_Out_writes_( cbDst ) PBYTE pbDst,
SIZE_T cbDst )
{
return SymCryptStoreLsbFirstUint64( src, pbDst, cbDst );
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptStoreLsbFirstUint64(
UINT64 src,
_Out_writes_( cbDst ) PBYTE pbDst,
SIZE_T cbDst )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
while( cbDst > 0 )
__CPROVER_assigns(cbDst, pbDst)
__CPROVER_loop_invariant(cbDst <= __CPROVER_loop_entry(cbDst))
__CPROVER_decreases(cbDst)
{
*pbDst++ = (BYTE) src;
src >>= 8;
cbDst--;
}
if( src != 0 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
cleanup:
return scError;
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptStoreMsbFirstUint32(
UINT32 src,
_Out_writes_( cbDst ) PBYTE pbDst,
SIZE_T cbDst )
{
return SymCryptStoreMsbFirstUint64( src, pbDst, cbDst );
}
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptStoreMsbFirstUint64(
UINT64 src,
_Out_writes_( cbDst ) PBYTE pbDst,
SIZE_T cbDst )
{
SYMCRYPT_ERROR scError = SYMCRYPT_NO_ERROR;
pbDst += cbDst;
while( cbDst > 0 )
__CPROVER_assigns(cbDst, pbDst, src)
__CPROVER_loop_invariant(cbDst >= 0 && cbDst <= __CPROVER_loop_entry(cbDst))
__CPROVER_decreases(cbDst)
{
*--pbDst = (BYTE) src;
src >>= 8;
cbDst--;
}
if( src != 0 )
{
scError = SYMCRYPT_VALUE_TOO_LARGE;
goto cleanup;
}
cleanup:
return scError;
}