VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Crypto.c
blob: 5856df48881d1d95f0617269bfe0fe0be855fdbf (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
/*
 Legal Notice: Some portions of the source code contained in this file were
 derived from the source code of TrueCrypt 7.1a, which is 
 Copyright (c) 2003-2012 TrueCrypt Developers Association and which is 
 governed by the TrueCrypt License 3.0, also from the source code of
 Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
 and which is governed by the 'License Agreement for Encryption for the Masses' 
 Modifications and additions to the original source code (contained in this file) 
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages. */

#include "Tcdefs.h"
#include "Crypto.h"
#include "Xts.h"
#include "Crc.h"
#include "Common/Endian.h"
#if !defined(_UEFI)
#include <string.h>
#ifndef TC_WINDOWS_BOOT
#ifdef TC_WINDOWS_DRIVER
#include <ntstrsafe.h>
#define StringCchCatW	RtlStringCchCatW
#define StringCchCopyW	RtlStringCchCopyW
#else
#include <strsafe.h>
#endif
#include "EncryptionThreadPool.h"
#endif
#endif
#include "Volumes.h"
#include "cpu.h"

#pragma warning (disable:4706) // assignment within conditional expression
/* Update the following when adding a new cipher or EA:

   Crypto.h:
     ID #define
     MAX_EXPANDED_KEY #define

   Crypto.c:
     Ciphers[]
     EncryptionAlgorithms[]
     CipherInit()
     EncipherBlock()
     DecipherBlock()

*/

#ifndef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE

// Cipher configuration
static Cipher Ciphers[] =
{
//								Block Size	Key Size	Key Schedule Size
//	  ID		Name			(Bytes)		(Bytes)		(Bytes)
#ifdef TC_WINDOWS_BOOT
	{ AES,		"AES",			16,			32,			AES_KS				},
#ifndef WOLFCRYPT_BACKEND
        { SERPENT,	"Serpent",		16,			32,			140*4				},
	{ TWOFISH,	"Twofish",		16,			32,			TWOFISH_KS			},
#endif
#else
	{ AES,		L"AES",			16,			32,			AES_KS				},
#ifndef WOLFCRYPT_BACKEND
	{ SERPENT,	L"Serpent",		16,			32,			140*4				},
	{ TWOFISH,	L"Twofish",		16,			32,			TWOFISH_KS			},
	{ CAMELLIA,	L"Camellia",	16,			32,			CAMELLIA_KS			},
	{ KUZNYECHIK,	L"Kuznyechik",16,		32,			KUZNYECHIK_KS },
#endif
#endif
	{ 0,		0,				0,			0,			0					}
};


// Encryption algorithm configuration
static EncryptionAlgorithm EncryptionAlgorithms[] =
{
	//  Cipher(s)                     Modes						FormatEnabled

#ifndef TC_WINDOWS_BOOT

	{ { 0,							0 }, { 0, 0},		0, 0 },	// Must be all-zero
	{ { AES,							0 }, { XTS, 0 },	1, 1 },
#ifndef WOLFCRYPT_BACKEND
	{ { SERPENT,					0 }, { XTS, 0 },	1, 1 },
	{ { TWOFISH,					0 }, { XTS, 0 },	1, 1 },
	{ { CAMELLIA,					0 }, { XTS, 0 },	1, 1 },
	{ { KUZNYECHIK,				0 }, { XTS, 0 },	0, 1 },
	{ { TWOFISH, AES,				0 }, { XTS, 0 },	1, 1 },
	{ { SERPENT, TWOFISH, AES,	0 }, { XTS, 0 },	1, 1 },
	{ { AES, SERPENT,				0 }, { XTS, 0 },	1, 1 },
	{ { AES, TWOFISH, SERPENT,	0 }, { XTS, 0 },	1, 1 },
	{ { SERPENT, TWOFISH,		0 }, { XTS, 0 },	1, 1 },
	{ { KUZNYECHIK, CAMELLIA,		0 }, { XTS, 0 },	0, 1 },
	{ { TWOFISH, KUZNYECHIK,		0 }, { XTS, 0 },	0, 1 },
	{ { SERPENT, CAMELLIA,		0 }, { XTS, 0 },	0, 1 },
	{ { AES, KUZNYECHIK,		0 }, { XTS, 0 },	0, 1 },
	{ { CAMELLIA, SERPENT, KUZNYECHIK,	0 }, { XTS, 0 },	0, 1 },
#endif
	{ { 0,							0 }, { 0,    0},	0, 0 }		// Must be all-zero

#else // TC_WINDOWS_BOOT

	// Encryption algorithms available for boot drive encryption
	{ { 0,						0 }, { 0, 0 },		0 },	// Must be all-zero
	{ { AES,					0 }, { XTS, 0 },	1 },
#ifndef WOLFCRYPT_BACKEND
	{ { SERPENT,				0 }, { XTS, 0 },	1 },
	{ { TWOFISH,				0 }, { XTS, 0 },	1 },
	{ { TWOFISH, AES,			0 }, { XTS, 0 },	1 },
	{ { SERPENT, TWOFISH, AES,	0 }, { XTS, 0 },	1 },
	{ { AES, SERPENT,			0 }, { XTS, 0 },	1 },
	{ { AES, TWOFISH, SERPENT,	0 }, { XTS, 0 },	1 },
	{ { SERPENT, TWOFISH,		0 }, { XTS, 0 },	1 },
#endif
	{ { 0,						0 }, { 0, 0 },		0 },	// Must be all-zero

#endif

};


#ifndef TC_WINDOWS_BOOT
// Hash algorithms
static Hash Hashes[] =
{	// ID				Name					Deprecated	System Encryption
	{ SHA512,		L"SHA-512",				FALSE,	FALSE },
	{ SHA256,		L"SHA-256",				FALSE,	TRUE },
    #ifndef WOLFCRYPT_BACKEND
        { BLAKE2S,		L"BLAKE2s-256",				FALSE,	TRUE },
        { WHIRLPOOL,	L"Whirlpool",			FALSE,	FALSE },
	{ STREEBOG,		L"Streebog",	FALSE,	FALSE },
    #endif
        { 0, 0, 0 }
};
#endif

/* Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal) */
int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks)
{
	int retVal = ERR_SUCCESS;

	switch (cipher)
	{
	case AES:
#ifndef TC_WINDOWS_BOOT
		if (aes_encrypt_key256 (key, (aes_encrypt_ctx *) ks) != EXIT_SUCCESS)
			return ERR_CIPHER_INIT_FAILURE;

		if (aes_decrypt_key256 (key, (aes_decrypt_ctx *) (ks + sizeof(aes_encrypt_ctx))) != EXIT_SUCCESS)
			return ERR_CIPHER_INIT_FAILURE;
#else
		if (aes_set_key (key, (length_type) CipherGetKeySize(AES), (aes_context *) ks) != 0)
			return ERR_CIPHER_INIT_FAILURE;
#endif
		break;

#ifndef WOLFCRYPT_BACKEND	
	case SERPENT:
		serpent_set_key (key, ks);
		break;
		
	case TWOFISH:
		twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key);
		break;

#if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_CAMELLIA)
	case CAMELLIA:
		camellia_set_key (key, ks);
		break;
#endif

#if !defined(TC_WINDOWS_BOOT) 
	case KUZNYECHIK:
		kuznyechik_set_key(key, (kuznyechik_kds*)ks);
		break;
#endif // !defined(TC_WINDOWS_BOOT)

#endif
	default:
		// Unknown/wrong cipher ID
		return ERR_CIPHER_INIT_FAILURE;
	}

	return retVal;
}

void EncipherBlock(int cipher, void *data, void *ks)
{
	switch (cipher)
	{
	case AES:	
		// In 32-bit kernel mode, due to KeSaveFloatingPointState() overhead, AES instructions can be used only when processing the whole data unit.
#if (defined (_WIN64) || !defined (TC_WINDOWS_DRIVER)) && !defined (TC_WINDOWS_BOOT)
		if (IsAesHwCpuSupported())
			aes_hw_cpu_encrypt (ks, data);
		else
#endif
			aes_encrypt (data, data, ks);
		break;

#ifndef WOLFCRYPT_BACKEND
	case TWOFISH:		twofish_encrypt (ks, data, data); break;
	case SERPENT:		serpent_encrypt (data, data, ks); break;
#if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_CAMELLIA)
	case CAMELLIA:		camellia_encrypt (data, data, ks); break;
#endif
#if !defined(TC_WINDOWS_BOOT)
	case KUZNYECHIK:		kuznyechik_encrypt_block(data, data, ks); break;
#endif // !defined(TC_WINDOWS_BOOT) 
#endif
	default:			TC_THROW_FATAL_EXCEPTION;	// Unknown/wrong ID
	}
}

#ifndef TC_WINDOWS_BOOT

void EncipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount)
{
	uint8 *data = dataPtr;
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
	KFLOATING_SAVE floatingPointState;
#endif

	if (cipher == AES
		&& (blockCount & (32 - 1)) == 0
		&& IsAesHwCpuSupported()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		&& NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		while (blockCount > 0)
		{
			aes_hw_cpu_encrypt_32_blocks (ks, data);

			data += 32 * 16;
			blockCount -= 32;
		}

#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#ifndef WOLFCRYPT_BACKEND	
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
	else if (cipher == SERPENT
			&& (blockCount >= 4)
			&& HasSSE2()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
			&& NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		serpent_encrypt_blocks (data, data, blockCount, ks);
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#endif
#if CRYPTOPP_BOOL_X64 && !defined(CRYPTOPP_DISABLE_ASM)
   else if (cipher == TWOFISH)	{
			twofish_encrypt_blocks(ks, data, data, (uint32) blockCount);
	}
	else if (cipher == CAMELLIA)	{
			camellia_encrypt_blocks(ks, data, data, (uint32) blockCount);
	}
#endif
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
	else if (cipher == KUZNYECHIK
			&& HasSSE2()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
			&& (blockCount >= 4) && NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		kuznyechik_encrypt_blocks (data, data, blockCount, ks);
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#endif
#endif
	else
	{
		size_t blockSize = CipherGetBlockSize (cipher);
		while (blockCount-- > 0)
		{
			EncipherBlock (cipher, data, ks);
			data += blockSize;
		}
	}
}

#endif // !TC_WINDOWS_BOOT

void DecipherBlock(int cipher, void *data, void *ks)
{
	switch (cipher)
	{
#ifndef WOLFCRYPT_BACKEND	
	case SERPENT:	serpent_decrypt (data, data, ks); break;
	case TWOFISH:	twofish_decrypt (ks, data, data); break;
#if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_CAMELLIA)
	case CAMELLIA:	camellia_decrypt (data, data, ks); break;
#endif
#if !defined(TC_WINDOWS_BOOT)
	case KUZNYECHIK:	kuznyechik_decrypt_block(data, data, ks); break;
#endif // !defined(TC_WINDOWS_BOOT)
#endif


#ifndef TC_WINDOWS_BOOT

	case AES:
#if defined (_WIN64) || !defined (TC_WINDOWS_DRIVER)
		if (IsAesHwCpuSupported())
			aes_hw_cpu_decrypt ((uint8 *) ks + sizeof (aes_encrypt_ctx), data);
		else
#endif
			aes_decrypt (data, data, (void *) ((char *) ks + sizeof(aes_encrypt_ctx)));
		break;

#else
	case AES:		aes_decrypt (data, data, ks); break;
#endif
	default:		TC_THROW_FATAL_EXCEPTION;	// Unknown/wrong ID
	}
}

#ifndef TC_WINDOWS_BOOT

void DecipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount)
{
	uint8 *data = dataPtr;
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
	KFLOATING_SAVE floatingPointState;
#endif

	if (cipher == AES
		&& (blockCount & (32 - 1)) == 0
		&& IsAesHwCpuSupported()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		&& NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		while (blockCount > 0)
		{
			aes_hw_cpu_decrypt_32_blocks ((uint8 *) ks + sizeof (aes_encrypt_ctx), data);

			data += 32 * 16;
			blockCount -= 32;
		}

#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#ifndef WOLFCRYPT_BACKEND	
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
	else if (cipher == SERPENT
			&& (blockCount >= 4)
			&& HasSSE2()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
			&& NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		serpent_decrypt_blocks (data, data, blockCount, ks);
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#endif
#if CRYPTOPP_BOOL_X64 && !defined(CRYPTOPP_DISABLE_ASM)
   else if (cipher == TWOFISH)	{
			twofish_decrypt_blocks(ks, data, data, (uint32) blockCount);
	}
	else if (cipher == CAMELLIA)	{
			camellia_decrypt_blocks(ks, data, data, (uint32) blockCount);
	}
#endif
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
	else if (cipher == KUZNYECHIK			
			&& HasSSE2()
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
			&& (blockCount >= 4) && NT_SUCCESS (KeSaveFloatingPointState (&floatingPointState))
#endif
		)
	{
		kuznyechik_decrypt_blocks (data, data, blockCount, ks);
#if defined (TC_WINDOWS_DRIVER) && !defined (_WIN64)
		KeRestoreFloatingPointState (&floatingPointState);
#endif
	}
#endif
#endif
	else
	{
		size_t blockSize = CipherGetBlockSize (cipher);
		while (blockCount-- > 0)
		{
			DecipherBlock (cipher, data, ks);
			data += blockSize;
		}
	}
}

#endif // !TC_WINDOWS_BOOT


// Ciphers support

Cipher *CipherGet (int id)
{
	int i;
	for (i = 0; Ciphers[i].Id != 0; i++)
		if (Ciphers[i].Id == id)
			return &Ciphers[i];

	return NULL;
}

#ifndef TC_WINDOWS_BOOT
const wchar_t *CipherGetName (int cipherId)
{
   Cipher* pCipher = CipherGet (cipherId);
   return  pCipher? pCipher -> Name : L"";
}

int CipherGetBlockSize (int cipherId)
{
   Cipher* pCipher = CipherGet (cipherId);
   return pCipher? pCipher -> BlockSize : 0;
}
#endif

int CipherGetKeySize (int cipherId)
{
#ifdef TC_WINDOWS_BOOT
	return CipherGet (cipherId) -> KeySize;
#else
   Cipher* pCipher = CipherGet (cipherId);
   return pCipher? pCipher -> KeySize : 0;
#endif
}

int CipherGetKeyScheduleSize (int cipherId)
{
#ifdef TC_WINDOWS_BOOT
	return CipherGet (cipherId) -> KeyScheduleSize;
#else
   Cipher* pCipher = CipherGet (cipherId);
   return pCipher? pCipher -> KeyScheduleSize : 0;
#endif
}

#ifndef TC_WINDOWS_BOOT

BOOL CipherSupportsIntraDataUnitParallelization (int cipher)
{
	return (cipher == AES && IsAesHwCpuSupported()) 
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
		|| (cipher == SERPENT && HasSSE2())
		|| (cipher == KUZNYECHIK && HasSSE2())
#endif
#if CRYPTOPP_BOOL_X64 && !defined(CRYPTOPP_DISABLE_ASM)
		|| (cipher == TWOFISH)
		|| (cipher == CAMELLIA)
#endif
		;
}

#endif


// Encryption algorithms support

int EAGetFirst ()
{
	return 1;
}

#ifndef TC_WINDOWS_BOOT
// Returns number of EAs
int EAGetCount (void)
{
	int ea, count = 0;

	for (ea = EAGetFirst (); ea != 0; ea = EAGetNext (ea))
	{
		count++;
	}
	return count;
}
#endif

int EAGetNext (int previousEA)
{
	int id = previousEA + 1;
	if (EncryptionAlgorithms[id].Ciphers[0] != 0) return id;
	return 0;
}


// Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal)
int EAInit (int ea, unsigned char *key, unsigned __int8 *ks)
{
	int c, retVal = ERR_SUCCESS;

	if (ea == 0)
		return ERR_CIPHER_INIT_FAILURE;

	for (c = EAGetFirstCipher (ea); c != 0; c = EAGetNextCipher (ea, c))
	{
		switch (CipherInit (c, key, ks))
		{
		case ERR_CIPHER_INIT_FAILURE:
			return ERR_CIPHER_INIT_FAILURE;

		case ERR_CIPHER_INIT_WEAK_KEY:
			retVal = ERR_CIPHER_INIT_WEAK_KEY;		// Non-fatal error
			break;
		}

		key += CipherGetKeySize (c);
		ks += CipherGetKeyScheduleSize (c);
	}
	return retVal;
}


#ifndef TC_WINDOWS_BOOT

BOOL EAInitMode (PCRYPTO_INFO ci, unsigned char* key2)
{
	switch (ci->mode)
	{
	case XTS:
		// Secondary key schedule
		if (EAInit (ci->ea, key2, ci->ks2) != ERR_SUCCESS)
			return FALSE;
                
            #ifdef WOLFCRYPT_BACKEND
                if (xts_encrypt_key256 (key2, (aes_encrypt_ctx *) ci->ks) != EXIT_SUCCESS)
			return ERR_CIPHER_INIT_FAILURE;

		if (xts_decrypt_key256 (key2, (aes_decrypt_ctx *) (ci->ks + sizeof(aes_encrypt_ctx))) != EXIT_SUCCESS)
			return ERR_CIPHER_INIT_FAILURE;
            #endif

                /* Note: XTS mode could potentially be initialized with a weak key causing all blocks in one data unit
		on the volume to be tweaked with zero tweaks (i.e. 512 bytes of the volume would be encrypted in ECB
		mode). However, to create a TrueCrypt volume with such a weak key, each human being on Earth would have
		to create approximately 11,378,125,361,078,862 (about eleven quadrillion) TrueCrypt volumes (provided 
		that the size of each of the volumes is 1024 terabytes). */
		break;

	default:		
		// Unknown/wrong ID
		TC_THROW_FATAL_EXCEPTION;
	}
	return TRUE;
}

static void EAGetDisplayName(wchar_t *buf, size_t bufLen, int ea, int i)
{
	StringCchCopyW (buf, bufLen, CipherGetName (i));
	if (i = EAGetPreviousCipher(ea, i))
	{
		size_t curLen;
		StringCchCatW (buf, bufLen, L"(");
		curLen = wcslen(buf);
		EAGetDisplayName (&buf[curLen], bufLen - curLen, ea, i);
		StringCchCatW (buf, bufLen, L")");
	}
}

// Returns name of EA, cascaded cipher names are separated by hyphens
wchar_t *EAGetName (wchar_t *buf, size_t bufLen, int ea, int guiDisplay)
{
	if (guiDisplay)
	{
		EAGetDisplayName (buf, bufLen, ea, EAGetLastCipher(ea));
	}
	else
	{
		int i = EAGetLastCipher(ea);
		StringCchCopyW (buf, bufLen, (i != 0) ? CipherGetName (i) : L"?");

		while (i = EAGetPreviousCipher(ea, i))
		{
			StringCchCatW (buf, bufLen, L"-");
			StringCchCatW (buf, bufLen, CipherGetName (i));
		}
	}
	return buf;
}


int EAGetByName (wchar_t *name)
{
	int ea = EAGetFirst ();
	wchar_t n[128];

	do
	{
		EAGetName(n, 128, ea, 1);
#if defined(_UEFI)
		if (wcscmp(n, name) == 0)
#else
		if (_wcsicmp(n, name) == 0)
#endif
			return ea;
	}
	while (ea = EAGetNext (ea));

	return 0;
}

#endif // TC_WINDOWS_BOOT

// Returns sum of key sizes of all ciphers of the EA (in bytes)
int EAGetKeySize (int ea)
{
	int i = EAGetFirstCipher (ea);
	int size = CipherGetKeySize (i);

	while (i = EAGetNextCipher (ea, i))
	{
		size += CipherGetKeySize (i);
	}

	return size;
}


#ifndef TC_WINDOWS_BOOT

// Returns the first mode of operation of EA
int EAGetFirstMode (int ea)
{
	return (EncryptionAlgorithms[ea].Modes[0]);
}


int EAGetNextMode (int ea, int previousModeId)
{
	int c, i = 0;
	while (c = EncryptionAlgorithms[ea].Modes[i++])
	{
		if (c == previousModeId) 
			return EncryptionAlgorithms[ea].Modes[i];
	}

	return 0;
}

// Returns the name of the mode of operation
const wchar_t *EAGetModeName (int mode)
{
	switch (mode)
	{
	case XTS:

		return L"XTS";

	}
	return L"[UNKNOWN]";
}

#endif // TC_WINDOWS_BOOT


// Returns sum of key schedule sizes of all ciphers of the EA
int EAGetKeyScheduleSize (int ea)
{
	int i = EAGetFirstCipher(ea);
	int size = CipherGetKeyScheduleSize (i);

	while (i = EAGetNextCipher(ea, i))
	{
		size += CipherGetKeyScheduleSize (i);
	}

	return size;
}

#ifndef TC_WINDOWS_BOOT

// Returns number of ciphers in EA
int EAGetCipherCount (int ea)
{
	int i = 0;
	while (EncryptionAlgorithms[ea].Ciphers[i++]);

	return i - 1;
}

#endif

int EAGetFirstCipher (int ea)
{
	return EncryptionAlgorithms[ea].Ciphers[0];
}


int EAGetLastCipher (int ea)
{
	int c, i = 0;
	while (c = EncryptionAlgorithms[ea].Ciphers[i++]);

	return EncryptionAlgorithms[ea].Ciphers[i - 2];
}


int EAGetNextCipher (int ea, int previousCipherId)
{
	int c, i = 0;
	while (c = EncryptionAlgorithms[ea].Ciphers[i++])
	{
		if (c == previousCipherId) 
			return EncryptionAlgorithms[ea].Ciphers[i];
	}

	return 0;
}


int EAGetPreviousCipher (int ea, int previousCipherId)
{
	int c, i = 0;

	if (EncryptionAlgorithms[ea].Ciphers[i++] == previousCipherId)
		return 0;

	while (c = EncryptionAlgorithms[ea].Ciphers[i++])
	{
		if (c == previousCipherId) 
			return EncryptionAlgorithms[ea].Ciphers[i - 2];
	}

	return 0;
}

#ifndef TC_WINDOWS_BOOT
int EAIsFormatEnabled (int ea)
{
	return EncryptionAlgorithms[ea].FormatEnabled;
}

int EAIsMbrSysEncEnabled (int ea)
{
	return EncryptionAlgorithms[ea].MbrSysEncEnabled;
}

// Returns TRUE if the mode of operation is supported for the encryption algorithm
BOOL EAIsModeSupported (int ea, int testedMode)
{
	int mode;

	for (mode = EAGetFirstMode (ea); mode != 0; mode = EAGetNextMode (ea, mode))
	{
		if (mode == testedMode)
			return TRUE;
	}
	return FALSE;
}

Hash *HashGet (int id)
{
	int i;
	for (i = 0; Hashes[i].Id != 0; i++)
		if (Hashes[i].Id == id)
			return &Hashes[i];

	return 0;
}

#ifdef _WIN32
int HashGetIdByName (wchar_t *name)
{
	int i;
	for (i = 0; Hashes[i].Id != 0; i++)
		if (_wcsicmp (Hashes[i].Name, name) == 0)
			return Hashes[i].Id;

	return 0;
}
#endif

const wchar_t *HashGetName (int hashId)
{
   Hash* pHash = HashGet(hashId);
   return pHash? pHash -> Name : L"";
}

void HashGetName2 (wchar_t *buf, size_t bufLen, int hashId)
{
   Hash* pHash = HashGet(hashId);
   if (pHash)
		StringCchCopyW (buf, bufLen, pHash -> Name);
	else
		buf[0] = L'\0';
}

BOOL HashIsDeprecated (int hashId)
{
   Hash* pHash = HashGet(hashId);
   return pHash? pHash -> Deprecated : FALSE;

}

BOOL HashForSystemEncryption (int hashId)
{
   Hash* pHash = HashGet(hashId);
   return pHash? pHash -> SystemEncryption : FALSE;

}

// Returns the largest key size needed by an EA for the specified mode of operation
int EAGetLargestKeyForMode (int mode)
{
	int ea, key = 0;

	for (ea = EAGetFirst (); ea != 0; ea = EAGetNext (ea))
	{
		if (!EAIsModeSupported (ea, mode))
			continue;

		if (EAGetKeySize (ea) >= key)
			key = EAGetKeySize (ea);
	}
	return key;
}

// Returns the maximum number of bytes necessary to be generated by the PBKDF2 (PKCS #5)
int GetMaxPkcs5OutSize (void)
{
	int size = 32;

	size = VC_MAX (size, EAGetLargestKeyForMode (XTS) * 2);	// Sizes of primary + secondary keys

	return size;
}

#endif


#endif // TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE


#ifdef TC_WINDOWS_BOOT

static uint8 CryptoInfoBufferInUse = 0;
CRYPTO_INFO CryptoInfoBuffer;

#endif

PCRYPTO_INFO crypto_open ()
{
#ifndef TC_WINDOWS_BOOT

	/* Do the crt allocation */
	PCRYPTO_INFO cryptoInfo = (PCRYPTO_INFO) TCalloc (sizeof (CRYPTO_INFO));
	if (cryptoInfo == NULL)
		return NULL;

	memset (cryptoInfo, 0, sizeof (CRYPTO_INFO));

#if !defined(DEVICE_DRIVER) && !defined(_UEFI)
	VirtualLock (cryptoInfo, sizeof (CRYPTO_INFO));
#endif

	cryptoInfo->ea = -1;
	return cryptoInfo;

#else // TC_WINDOWS_BOOT

#if 0
	if (CryptoInfoBufferInUse)
		TC_THROW_FATAL_EXCEPTION;
#endif
	CryptoInfoBufferInUse = 1;
	return &CryptoInfoBuffer;

#endif // TC_WINDOWS_BOOT
}

#ifndef TC_WINDOWS_BOOT
void crypto_loadkey (PKEY_INFO keyInfo, unsigned char *lpszUserKey, int nUserKeyLen)
{
	keyInfo->keyLength = nUserKeyLen;
	burn (keyInfo->userKey, sizeof (keyInfo->userKey));
	memcpy (keyInfo->userKey, lpszUserKey, nUserKeyLen);
}

void crypto_eraseKeys (PCRYPTO_INFO cryptoInfo)
{
	burn (cryptoInfo->ks, sizeof (cryptoInfo->ks));
	burn (cryptoInfo->ks2, sizeof (cryptoInfo->ks2));
#ifdef TC_WINDOWS_DRIVER
	burn (cryptoInfo->master_keydata_hash, sizeof (cryptoInfo->master_keydata_hash));
#else
	burn (cryptoInfo->master_keydata, sizeof (cryptoInfo->master_keydata));
	burn (cryptoInfo->k2, sizeof (cryptoInfo->k2));
#endif
	burn (&cryptoInfo->noIterations, sizeof (cryptoInfo->noIterations));
	burn (&cryptoInfo->volumePim, sizeof (cryptoInfo->volumePim));
}
#endif

void crypto_close (PCRYPTO_INFO cryptoInfo)
{
#ifndef TC_WINDOWS_BOOT

	if (cryptoInfo != NULL)
	{
		burn (cryptoInfo, sizeof (CRYPTO_INFO));
#if !defined(DEVICE_DRIVER) && !defined(_UEFI)
		VirtualUnlock (cryptoInfo, sizeof (CRYPTO_INFO));
#endif
		TCfree (cryptoInfo);
	}

#else // TC_WINDOWS_BOOT

	burn (&CryptoInfoBuffer, sizeof (CryptoInfoBuffer));
	CryptoInfoBufferInUse = FALSE;

#endif // TC_WINDOWS_BOOT
}


#ifndef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE



// EncryptBuffer
//
// buf:  data to be encrypted; the start of the buffer is assumed to be aligned with the start of a data unit.
// len:  number of bytes to encrypt; must be divisible by the block size (for cascaded ciphers, divisible 
//       by the largest block size used within the cascade)
void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo)
{
	switch (cryptoInfo->mode)
	{
	case XTS:
		{
			unsigned __int8 *ks = cryptoInfo->ks;
			unsigned __int8 *ks2 = cryptoInfo->ks2;
			UINT64_STRUCT dataUnitNo;
			int cipher;

			// When encrypting/decrypting a buffer (typically a volume header) the sequential number
			// of the first XTS data unit in the buffer is always 0 and the start of the buffer is
			// always assumed to be aligned with the start of a data unit.
			dataUnitNo.LowPart = 0;
			dataUnitNo.HighPart = 0;

			for (cipher = EAGetFirstCipher (cryptoInfo->ea);
				cipher != 0;
				cipher = EAGetNextCipher (cryptoInfo->ea, cipher))
			{
				EncryptBufferXTS (buf, len, &dataUnitNo, 0, ks, ks2, cipher);

				ks += CipherGetKeyScheduleSize (cipher);
				ks2 += CipherGetKeyScheduleSize (cipher);
			}
		}
		break;

	default:		
		// Unknown/wrong ID
		TC_THROW_FATAL_EXCEPTION;
	}
}


// buf:			data to be encrypted
// unitNo:		sequential number of the data unit with which the buffer starts
// nbrUnits:	number of data units in the buffer
void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, uint32 nbrUnits, PCRYPTO_INFO ci)
#if !defined(TC_WINDOWS_BOOT) && !defined(_UEFI)
{
	EncryptionThreadPoolDoWork (EncryptDataUnitsWork, buf, structUnitNo, nbrUnits, ci);
}

void EncryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci)
#endif // !TC_WINDOWS_BOOT
{
	int ea = ci->ea;
	unsigned __int8 *ks = ci->ks;
	unsigned __int8 *ks2 = ci->ks2;
	int cipher;

	switch (ci->mode)
	{
	case XTS:
		for (cipher = EAGetFirstCipher (ea); cipher != 0; cipher = EAGetNextCipher (ea, cipher))
		{
			EncryptBufferXTS (buf,
				nbrUnits * ENCRYPTION_DATA_UNIT_SIZE,
				structUnitNo,
				0,
				ks,
				ks2,
				cipher);

			ks += CipherGetKeyScheduleSize (cipher);
			ks2 += CipherGetKeyScheduleSize (cipher);
		}
		break;

	default:		
		// Unknown/wrong ID
		TC_THROW_FATAL_EXCEPTION;
	}
}

// DecryptBuffer
//
// buf:  data to be decrypted; the start of the buffer is assumed to be aligned with the start of a data unit.
// len:  number of bytes to decrypt; must be divisible by the block size (for cascaded ciphers, divisible 
//       by the largest block size used within the cascade)
void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo)
{
	switch (cryptoInfo->mode)
	{
	case XTS:
		{
			unsigned __int8 *ks = cryptoInfo->ks + EAGetKeyScheduleSize (cryptoInfo->ea);
			unsigned __int8 *ks2 = cryptoInfo->ks2 + EAGetKeyScheduleSize (cryptoInfo->ea);
			UINT64_STRUCT dataUnitNo;
			int cipher;

			// When encrypting/decrypting a buffer (typically a volume header) the sequential number
			// of the first XTS data unit in the buffer is always 0 and the start of the buffer is
			// always assumed to be aligned with the start of the data unit 0.
			dataUnitNo.LowPart = 0;
			dataUnitNo.HighPart = 0;

			for (cipher = EAGetLastCipher (cryptoInfo->ea);
				cipher != 0;
				cipher = EAGetPreviousCipher (cryptoInfo->ea, cipher))
			{
				ks -= CipherGetKeyScheduleSize (cipher);
				ks2 -= CipherGetKeyScheduleSize (cipher);

				DecryptBufferXTS (buf, len, &dataUnitNo, 0, ks, ks2, cipher);
			}
		}
		break;

	default:		
		// Unknown/wrong ID
		TC_THROW_FATAL_EXCEPTION;
	}
}

// buf:			data to be decrypted
// unitNo:		sequential number of the data unit with which the buffer starts
// nbrUnits:	number of data units in the buffer
void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, uint32 nbrUnits, PCRYPTO_INFO ci)
#if !defined(TC_WINDOWS_BOOT) && !defined(_UEFI)
{
	EncryptionThreadPoolDoWork (DecryptDataUnitsWork, buf, structUnitNo, nbrUnits, ci);
}

void DecryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci)
#endif // !TC_WINDOWS_BOOT
{
	int ea = ci->ea;
	unsigned __int8 *ks = ci->ks;
	unsigned __int8 *ks2 = ci->ks2;
	int cipher;


	switch (ci->mode)
	{
	case XTS:
		ks += EAGetKeyScheduleSize (ea);
		ks2 += EAGetKeyScheduleSize (ea);

		for (cipher = EAGetLastCipher (ea); cipher != 0; cipher = EAGetPreviousCipher (ea, cipher))
		{
			ks -= CipherGetKeyScheduleSize (cipher);
			ks2 -= CipherGetKeyScheduleSize (cipher);

			DecryptBufferXTS (buf,
				nbrUnits * ENCRYPTION_DATA_UNIT_SIZE,
				structUnitNo,
				0,
				ks,
				ks2,
				cipher);
		}
		break;

	default:		
		// Unknown/wrong ID
		TC_THROW_FATAL_EXCEPTION;
	}
}


#else // TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE


#if !defined (TC_WINDOWS_BOOT_AES) && !defined (TC_WINDOWS_BOOT_SERPENT) && !defined (TC_WINDOWS_BOOT_TWOFISH) && !defined (TC_WINDOWS_BOOT_CAMELLIA)
#error No cipher defined
#endif

void EncipherBlock(int cipher, void *data, void *ks)
{
#ifdef TC_WINDOWS_BOOT_AES
	if (IsAesHwCpuSupported())
		aes_hw_cpu_encrypt ((uint8 *) ks, data);
	else
		aes_encrypt (data, data, ks); 
#elif defined (TC_WINDOWS_BOOT_SERPENT) && !defined (WOLFCRYPT_BACKEND)
	serpent_encrypt (data, data, ks);
#elif defined (TC_WINDOWS_BOOT_TWOFISH) && !defined (WOLFCRYPT_BACKEND)
	twofish_encrypt (ks, data, data);
#elif defined (TC_WINDOWS_BOOT_CAMELLIA) && !defined (WOLFCRYPT_BACKEND)
	camellia_encrypt (data, data, ks);
#endif
}

void DecipherBlock(int cipher, void *data, void *ks)
{
#ifdef TC_WINDOWS_BOOT_AES
	if (IsAesHwCpuSupported())
		aes_hw_cpu_decrypt ((uint8 *) ks + sizeof (aes_encrypt_ctx) + 14 * 16, data);
	else
		aes_decrypt (data, data, (aes_decrypt_ctx *) ((uint8 *) ks + sizeof(aes_encrypt_ctx))); 
#elif defined (TC_WINDOWS_BOOT_SERPENT) && !defined (WOLFCRYPT_BACKEND)
	serpent_decrypt (data, data, ks);
#elif defined (TC_WINDOWS_BOOT_TWOFISH) && !defined (WOLFCRYPT_BACKEND)
	twofish_decrypt (ks, data, data);
#elif defined (TC_WINDOWS_BOOT_CAMELLIA) && !defined (WOLFCRYPT_BACKEND)
	camellia_decrypt (data, data, ks);
#endif
}


#ifdef TC_WINDOWS_BOOT_AES

int EAInit (unsigned char *key, unsigned __int8 *ks)
{
	aes_init();

	if (aes_encrypt_key256 (key, (aes_encrypt_ctx *) ks) != EXIT_SUCCESS)
		return ERR_CIPHER_INIT_FAILURE;
	if (aes_decrypt_key256 (key, (aes_decrypt_ctx *) (ks + sizeof (aes_encrypt_ctx))) != EXIT_SUCCESS)
		return ERR_CIPHER_INIT_FAILURE;

	return ERR_SUCCESS;
}

#endif


void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo)
{
	UINT64_STRUCT dataUnitNo;
	dataUnitNo.LowPart = 0; dataUnitNo.HighPart = 0;
	EncryptBufferXTS (buf, len, &dataUnitNo, 0, cryptoInfo->ks, cryptoInfo->ks2, 1);
}

void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci)
{
	EncryptBufferXTS (buf, nbrUnits * ENCRYPTION_DATA_UNIT_SIZE, structUnitNo, 0, ci->ks, ci->ks2, 1);
}

void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo)
{
	UINT64_STRUCT dataUnitNo;
	dataUnitNo.LowPart = 0; dataUnitNo.HighPart = 0;
	DecryptBufferXTS (buf, len, &dataUnitNo, 0, cryptoInfo->ks, cryptoInfo->ks2, 1);
}

void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci)
{
	DecryptBufferXTS (buf, nbrUnits * ENCRYPTION_DATA_UNIT_SIZE, structUnitNo, 0, ci->ks, ci->ks2, 1);
}

#endif // TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE


#if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)

static BOOL HwEncryptionDisabled = FALSE;

BOOL IsAesHwCpuSupported ()
{
#ifdef TC_WINDOWS_BOOT_AES
	static BOOL state = FALSE;
	static BOOL stateValid = FALSE;

	if (!stateValid)
	{
		state = is_aes_hw_cpu_supported() ? TRUE : FALSE;
		stateValid = TRUE;
	}

	return state && !HwEncryptionDisabled;
#elif defined (_M_ARM64) || defined(__arm__) || defined (__arm64__) || defined (__aarch64__)
	return 0;
#else
	return (HasAESNI() && !HwEncryptionDisabled)? TRUE : FALSE;
#endif
}

void EnableHwEncryption (BOOL enable)
{
#if defined (TC_WINDOWS_BOOT)
	if (enable)
		aes_hw_cpu_enable_sse();
#endif

	HwEncryptionDisabled = !enable;
}

BOOL IsHwEncryptionEnabled ()
{
	return !HwEncryptionDisabled;
}

#endif // !TC_WINDOWS_BOOT

#if !defined (TC_WINDOWS_BOOT) && !defined (_UEFI)

static BOOL CpuRngDisabled = TRUE;
static BOOL RamEncryptionEnabled = FALSE;

BOOL IsCpuRngSupported ()
{
#ifndef _M_ARM64
	if (HasRDSEED() || HasRDRAND())
		return TRUE;
	else
#endif
		return FALSE;
}

void EnableCpuRng (BOOL enable)
{
	CpuRngDisabled = !enable;
}

BOOL IsCpuRngEnabled ()
{
	return !CpuRngDisabled;
}

BOOL IsRamEncryptionSupported ()
{
#ifdef _WIN64
	if (t1ha_selfcheck__t1ha2() == 0)
		return TRUE;
	else
		return FALSE;
#else
	return FALSE;
#endif
}

void EnableRamEncryption (BOOL enable)
{
	RamEncryptionEnabled = enable;
}

BOOL IsRamEncryptionEnabled ()
{
	return RamEncryptionEnabled;
}

/* masking for random index to remove bias */
uint8 GetRngMask (uint8 count)
{
	if (count >= 128)
		return 0xFF;
	if (count >= 64)
		return 0x7F;
	if (count >= 32)
		return 0x3F;
	if (count >= 16)
		return 0x1F;
	if (count >= 8)
		return 0x0F;
	if (count >= 4)
		return 0x07;
	if (count >= 2)
		return 0x03;
	return 1;
}

uint8 GetRandomIndex (ChaCha20RngCtx* pCtx, uint8 elementsCount)
{
	uint8 index = 0;
	uint8 mask = GetRngMask (elementsCount);

	while (TRUE)
	{
		ChaCha20RngGetBytes (pCtx, &index, 1);
		index &= mask;
		if (index < elementsCount)
			break;
	}

	return index;
}

#if !defined (_UEFI)
/* declaration of variables and functions used for RAM encryption on 64-bit build */
static uint8* pbKeyDerivationArea = NULL;
static ULONG cbKeyDerivationArea = 0;

static uint64 HashSeedMask = 0;
static uint64 CipherIVMask = 0;
#ifdef TC_WINDOWS_DRIVER
ULONG AllocTag = 'MMCV';
#endif

#if !defined(PAGE_SIZE)
#define PAGE_SIZE 4096
#endif

BOOL InitializeSecurityParameters(GetRandSeedFn rngCallback)
{
	ChaCha20RngCtx ctx;
	uint8 pbSeed[CHACHA20RNG_KEYSZ + CHACHA20RNG_IVSZ];
#ifdef TC_WINDOWS_DRIVER
	uint8 i;
	char randomStr[4];
	Dump ("InitializeSecurityParameters BEGIN\n");
#endif

	rngCallback (pbSeed, sizeof (pbSeed));

	ChaCha20RngInit (&ctx, pbSeed, rngCallback, 0);

#ifdef TC_WINDOWS_DRIVER

	/* Generate random value for tag that is similar to pool tag values used by Windows kernel.
	 * Fully random tag would be too suspicious and outstanding.
     * First character is always a capital letter.
     * Second character is a letter, lowercase or uppercase.
     * Third character is a letter, lowercase or uppercase.
     * Fourth character is a letter or a digit.
	 */

    /* 1. First character (Capital Letter) */
    randomStr[0] = 'A' + GetRandomIndex(&ctx, 26);

    /* 2. Second character (Letter) */
    i = GetRandomIndex(&ctx, 52);
    if (i < 26)
        randomStr[1] = 'A' + i;
    else
        randomStr[1] = 'a' + (i - 26);

    /* 3. Third character (Letter) */
    i = GetRandomIndex(&ctx, 52);
    if (i < 26)
        randomStr[2] = 'A' + i;
    else
        randomStr[2] = 'a' + (i - 26);

    /* 4. Fourth character (Letter or Digit) */
    i = GetRandomIndex(&ctx, 62);
    if (i < 26)
        randomStr[3] = 'A' + i;
    else if (i < 52)
        randomStr[3] = 'a' + (i - 26);
    else
        randomStr[3] = '0' + (i - 52);

	/* combine all characters in reverse order as explained in MSDN */
	AllocTag = 0;
	for (i = 0; i < 4; i++)
	{
		AllocTag = (AllocTag << 8) + randomStr[3-i];
	}

#endif

	cbKeyDerivationArea = 1024 * 1024;
	do
	{
		pbKeyDerivationArea = (uint8*) TCalloc(cbKeyDerivationArea);
		if (!pbKeyDerivationArea)
			cbKeyDerivationArea >>= 1;
	} while (!pbKeyDerivationArea && (cbKeyDerivationArea >= (2*PAGE_SIZE)));

	if (!pbKeyDerivationArea)
	{
		cbKeyDerivationArea = 0;
		Dump ("InitializeSecurityParameters return=FALSE END\n");
		return FALSE;
	}

	/* fill key derivation area with random bytes */
	ChaCha20RngGetBytes (&ctx, pbKeyDerivationArea, cbKeyDerivationArea);

	/* generate hash seed mask */
	ChaCha20RngGetBytes(&ctx, (unsigned char*) &HashSeedMask, sizeof (HashSeedMask));	

	/* generate IV mask */
	ChaCha20RngGetBytes(&ctx, (unsigned char*) &CipherIVMask, sizeof (CipherIVMask));	

	FAST_ERASE64 (pbSeed, sizeof (pbSeed));
	burn (&ctx, sizeof (ctx));
#ifdef TC_WINDOWS_DRIVER
	burn (randomStr, sizeof(randomStr));

	Dump ("InitializeSecurityParameters return=TRUE END\n");
#endif
	return TRUE;
}

void ClearSecurityParameters()
{
	Dump ("ClearSecurityParameters BEGIN\n");
	if (pbKeyDerivationArea)
	{
		FAST_ERASE64 (pbKeyDerivationArea, cbKeyDerivationArea);
		TCfree (pbKeyDerivationArea);
		pbKeyDerivationArea =NULL;
		cbKeyDerivationArea = 0;
	}

	FAST_ERASE64 (&HashSeedMask, 8);
	FAST_ERASE64 (&CipherIVMask, 8);
#ifdef TC_WINDOWS_DRIVER
	burn (&AllocTag, sizeof (AllocTag));
#endif
	Dump ("ClearSecurityParameters END\n");
}

#ifdef TC_WINDOWS_DRIVER
void VcProtectMemory (uint64 encID, unsigned char* pbData, size_t cbData, unsigned char* pbData2, size_t cbData2)
#else
void VcProtectMemory (uint64 encID, unsigned char* pbData, size_t cbData, 
							unsigned char* pbData2, size_t cbData2,
							unsigned char* pbData3, size_t cbData3,
							unsigned char* pbData4, size_t cbData4)
#endif
{
	if (pbKeyDerivationArea)
	{
		uint64 hashLow, hashHigh, hashSeed, cipherIV;
		uint64 pbKey[4];
		ChaCha256Ctx ctx;

		hashSeed = (((uint64) pbKeyDerivationArea) + encID) ^ HashSeedMask;
		hashLow = t1ha2_atonce128(&hashHigh, pbKeyDerivationArea, cbKeyDerivationArea, hashSeed);

		/* set the key to the hash result */
		pbKey[0] = hashLow;
		pbKey[1] = hashHigh;
		/* we now have a 128-bit key and we will expand it to 256-bit by using ChaCha12 cipher */
		/* first we need to generate a the other 128-bit half of the key */
		pbKey[2] = hashLow ^ hashHigh;
		pbKey[3] = hashLow + hashHigh;

		/* Initialize ChaCha12 cipher */
		ChaCha256Init (&ctx, (unsigned char*) pbKey, (unsigned char*) &hashSeed, 12);
		/* encrypt the key by itself */
		ChaCha256Encrypt (&ctx, (unsigned char*) pbKey, sizeof(pbKey), (unsigned char*) pbKey);

		/* Initialize ChaCha12 cipher */
		cipherIV = (((uint64) pbKeyDerivationArea) + encID) ^ CipherIVMask;
		ChaCha256Init (&ctx, (unsigned char*) pbKey, (unsigned char*) &cipherIV, 12);

		ChaCha256Encrypt (&ctx, pbData, cbData, pbData);
		ChaCha256Encrypt (&ctx, pbData2, cbData2, pbData2);
#ifndef TC_WINDOWS_DRIVER
		ChaCha256Encrypt (&ctx, pbData3, cbData3, pbData3);
		ChaCha256Encrypt (&ctx, pbData4, cbData4, pbData4);
#endif
		FAST_ERASE64 (pbKey, sizeof(pbKey));
		FAST_ERASE64 (&hashLow, 8);
		FAST_ERASE64 (&hashHigh, 8);
		FAST_ERASE64 (&hashSeed, 8);
		FAST_ERASE64 (&cipherIV, 8);
		burn (&ctx, sizeof (ctx));
	}
}

uint64 VcGetEncryptionID (PCRYPTO_INFO pCryptoInfo)
{
	return ((uint64) pCryptoInfo->ks) + ((uint64) pCryptoInfo->ks2)
#ifndef TC_WINDOWS_DRIVER
		+ ((uint64) pCryptoInfo->master_keydata) + ((uint64) pCryptoInfo->k2)
#endif
		;
}

static void VcInternalProtectKeys (PCRYPTO_INFO pCryptoInfo, uint64 encID)
{
#ifdef TC_WINDOWS_DRIVER
	VcProtectMemory (encID, pCryptoInfo->ks, MAX_EXPANDED_KEY, pCryptoInfo->ks2, MAX_EXPANDED_KEY);
#else
	VcProtectMemory (encID, pCryptoInfo->ks, MAX_EXPANDED_KEY,
					pCryptoInfo->ks2, MAX_EXPANDED_KEY,
					pCryptoInfo->master_keydata, MASTER_KEYDATA_SIZE,
					pCryptoInfo->k2, MASTER_KEYDATA_SIZE);
#endif

}

void VcProtectKeys (PCRYPTO_INFO pCryptoInfo, uint64 encID)
{
	Dump ("VcProtectKeys BEGIN\n");
	VcInternalProtectKeys (pCryptoInfo, encID);
	Dump ("VcProtectKeys END\n");
}

void VcUnprotectKeys (PCRYPTO_INFO pCryptoInfo, uint64 encID)
{
	Dump ("VcUnprotectKeys BEGIN\n");
	VcInternalProtectKeys (pCryptoInfo, encID);
	Dump ("VcUnprotectKeys END\n");
}
#endif

#endif

#if defined(_M_ARM64) || defined(__arm__) || defined (__arm64__) || defined (__aarch64__)
/* dummy implementation that should never be called */
void aes_hw_cpu_decrypt(const uint8* ks, uint8* data)
{
	ks = ks;
	data = data;
}

void aes_hw_cpu_decrypt_32_blocks(const uint8* ks, uint8* data)
{
	ks = ks;
	data = data;
}

void aes_hw_cpu_encrypt(const uint8* ks, uint8* data)
{
	ks = ks;
	data = data;
}

void aes_hw_cpu_encrypt_32_blocks(const uint8* ks, uint8* data)
{
	ks = ks;
	data = data;
}
#endif