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
|
/** @file
Interface for DCS
Copyright (c) 2016. Disk Cryptography Services for EFI (DCS), Alex Kolotnikov
Copyright (c) 2016. VeraCrypt, Mounir IDRASSI
This program and the accompanying materials
are licensed and made available under the terms and conditions
of the Apache License, Version 2.0.
The full text of the license may be found at
https://opensource.org/licenses/Apache-2.0
**/
#include <DcsVeraCrypt.h>
#include <Uefi.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/PrintLib.h>
#include <Library/CommonLib.h>
#include <Library/GraphLib.h>
#include <Library/PasswordLib.h>
#include <Library/DcsCfgLib.h>
#include <common/Password.h>
#include "common/Crypto.h"
#include "common/Crc.h"
#include "BootCommon.h"
#include "Library/DcsTpmLib.h"
#include <DcsConfig.h>
///////////////////////////////////////////////////////////////////////////
// Globals
//////////////////////////////////////////////////////////////////////////
#define MAX_MSG 256
int gAuthPasswordType = 0;
char* gAuthPasswordMsg = NULL;
Password gAuthPassword;
UINT8 gAutoLogin = 0;
char* gAutoPassword = NULL;
char* gAuthPimMsg = NULL;
int gAuthPimRqt = 1;
int gAuthPim = 0;
int gAuthTcRqt = 0;
int gAuthTc = 0;
char *gAuthHashMsg = NULL;
int gAuthHashRqt = 1;
int gAuthHash = 0;
int gAuthBootRqt = 0;
int gAuthBoot = 1;
int gAuthRetry = 10;
int gAuthPwdCode = 1;
int gRndDefault = 0;
char* gAuthErrorMsg = NULL;
char* gAuthStartMsg = NULL;
INT32 gRUD = 0;
int gAuthSecRegionSearch = 0;
int gSecRegionInfoDelay = 0;
CHAR8* gPlatformKeyFile = NULL;
UINTN gPlatformKeyFileSize = 0;
EFI_GUID *gPartitionGuidOS = NULL;
int gDcsBootForce = 1;
char* gForcePasswordMsg = NULL;
int gForcePasswordType = 0;
UINT8 gForcePasswordProgress = 1;
CHAR8* gOnExitFailed = NULL;
CHAR8* gOnExitSuccess = NULL;
CHAR8* gOnExitNotFound = NULL;
//////////////////////////////////////////////////////////////////////////
// Authorize
/////////////////////////////////////////////////////////////////////////
#define VCCONFIG_ALLOC(data, size) \
if(data == NULL) MEM_FREE(data); \
data = MEM_ALLOC(size);
VOID
VCAuthLoadConfig()
{
int tmp;
char* strTemp = NULL;
if (gAuthPasswordMsg != NULL) return; // Already loaded
SetMem(&gAuthPassword, sizeof(gAuthPassword), 0);
strTemp = MEM_ALLOC(MAX_MSG);
VCCONFIG_ALLOC(gPasswordPictureFileName, MAX_MSG * 2);
ConfigReadString("PasswordPicture", "\\EFI\\VeraCrypt\\login.bmp", strTemp, MAX_MSG);
AsciiStrToUnicodeStr(strTemp, gPasswordPictureFileName);
MEM_FREE(strTemp);
VCCONFIG_ALLOC(gPasswordPictureChars, MAX_MSG);
ConfigReadString("PictureChars", gPasswordPictureCharsDefault, gPasswordPictureChars, MAX_MSG);
gPasswordPictureCharsLen = strlen(gPasswordPictureChars);
gAuthPasswordType = ConfigReadInt("PasswordType", 0);
VCCONFIG_ALLOC(gAuthPasswordMsg, MAX_MSG);
ConfigReadString("PasswordMsg", "Password:", gAuthPasswordMsg, MAX_MSG);
gAutoLogin = (UINT8)ConfigReadInt("AutoLogin", 0);
VCCONFIG_ALLOC(gAutoPassword, MAX_PASSWORD);
ConfigReadString("AutoPassword", "", gAutoPassword, MAX_PASSWORD);
VCCONFIG_ALLOC(gAuthPimMsg, MAX_MSG);
gAuthPimRqt = ConfigReadInt("PimRqt", 1);
gAuthPim = ConfigReadInt("Pim", 0);
ConfigReadString("PimMsg", "Pim:", gAuthPimMsg, MAX_MSG);
VCCONFIG_ALLOC(gAuthHashMsg, MAX_MSG);
gAuthHashRqt = ConfigReadInt("HashRqt", 1);
gAuthHash = ConfigReadInt("Hash", 0);
strTemp = MEM_ALLOC(MAX_MSG);
tmp = 1;
AsciiSPrint(strTemp, MAX_MSG, "(0) TEST ALL");
while (HashGetName(tmp) != NULL && *HashGetName(tmp) != 0)
{
AsciiSPrint(strTemp, MAX_MSG, "%a (%d) %s", strTemp, tmp, HashGetName(tmp));
++tmp;
};
AsciiSPrint(strTemp, MAX_MSG, "%a \n\rHash:", strTemp);
ConfigReadString("HashMsg", strTemp, gAuthHashMsg, MAX_MSG);
MEM_FREE(strTemp);
gAuthBootRqt = ConfigReadInt("BootRqt", 0);
gAuthTcRqt = ConfigReadInt("TcRqt", 0);
gPasswordProgress = (UINT8)ConfigReadInt("AuthorizeProgress", 1); // print "*"
gPasswordVisible = (UINT8)ConfigReadInt("AuthorizeVisible", 0); // show chars
gPasswordShowMark = ConfigReadInt("AuthorizeMarkTouch", 1); // show touch points
gPasswordTimeout = (UINT8)ConfigReadInt("PasswordTimeout", 0); // If no password for <seconds> => <ESC>
gDcsBootForce = ConfigReadInt("DcsBootForce", 1); // Ask password even if no USB marked found.
VCCONFIG_ALLOC(gForcePasswordMsg, MAX_MSG);
ConfigReadString("ForcePasswordMsg", gAuthPasswordMsg, gForcePasswordMsg, MAX_MSG);
gForcePasswordType = ConfigReadInt("ForcePasswordType", gAuthPasswordType);
gForcePasswordProgress = (UINT8)ConfigReadInt("ForcePasswordProgress", gPasswordProgress);
gAuthRetry = ConfigReadInt("AuthorizeRetry", 10);
VCCONFIG_ALLOC(gAuthStartMsg, MAX_MSG);
ConfigReadString("AuthStartMsg", "Authorizing...\n\r", gAuthStartMsg, MAX_MSG);
VCCONFIG_ALLOC(gAuthErrorMsg, MAX_MSG);
ConfigReadString("AuthErrorMsg", "Authorization failed. Wrong password, PIM or hash.\n\r", gAuthErrorMsg, MAX_MSG);
gRUD = ConfigReadInt("RUD", 0);
gRndDefault = ConfigReadInt("Random", 0);
gAuthSecRegionSearch = ConfigReadInt("SecRegionSearch", 0);
gSecRegionInfoDelay = ConfigReadInt("SecRegionInfoDelay", 0);
gPlatformLocked = ConfigReadInt("PlatformLocked", 0);
gTPMLocked = ConfigReadInt("TPMLocked", 0);
gTPMLockedInfoDelay = ConfigReadInt("TPMLockedInfoDelay", 9);
gSCLocked = ConfigReadInt("SCLocked", 0);
// Actions for DcsInt
VCCONFIG_ALLOC(gOnExitSuccess, MAX_MSG);
ConfigReadString("ActionSuccess", "Exit", gOnExitSuccess, MAX_MSG);
VCCONFIG_ALLOC(gOnExitNotFound, MAX_MSG);
ConfigReadString("ActionNotFound", "Exit", gOnExitNotFound, MAX_MSG);
VCCONFIG_ALLOC(gOnExitFailed, MAX_MSG);
ConfigReadString("ActionFailed", "Exit", gOnExitFailed, MAX_MSG);
strTemp = MEM_ALLOC(MAX_MSG);
ConfigReadString("PartitionGuidOS", "", strTemp, MAX_MSG);
if (strTemp[0] != 0) {
EFI_GUID g;
if (AsciiStrToGuid(&g, strTemp)) {
VCCONFIG_ALLOC(gPartitionGuidOS, sizeof(EFI_GUID));
if (gPartitionGuidOS != NULL) {
memcpy(gPartitionGuidOS, &g, sizeof(g));
}
}
}
MEM_FREE(strTemp);
// touch
tmp = ConfigReadInt("TouchDevice", -1);
if (tmp == -1) InitTouch();
if (tmp >= 0) {
if (gTouchCount == 0) InitTouch();
if (tmp < (int)gTouchCount) {
TouchGetIO(gTouchHandles[tmp], &gTouchPointer);
}
}
gTouchSimulate = ConfigReadInt("TouchSimulate", 0);
// Graph
tmp = ConfigReadInt("GraphDevice", -1);
if (tmp == -1) InitGraph();
if (tmp >= 0) {
if (gGraphCount == 0) InitGraph();
if (tmp < (int)gGraphCount) {
GraphGetIO(gGraphHandles[tmp], &gGraphOut);
}
}
if (gGraphOut != NULL) {
tmp = ConfigReadInt("GraphMode", -1);
if (tmp >= 0 && tmp <= (int)gGraphOut->Mode->MaxMode) {
gGraphOut->SetMode(gGraphOut, tmp);
}
}
// Beep
gBeepEnabled = ConfigReadInt("Beep", 0);
if (gBeepEnabled) {
gBeepNumberDefault = ConfigReadInt("BeepNumber", 1);
gBeepDurationDefault = ConfigReadInt("BeepDuration", 100);
gBeepIntervalDefault = ConfigReadInt("BeepInterval", 0);
gBeepToneDefault = ConfigReadInt("BeepTone", 0x500);
gBeepControlEnabled = ConfigReadInt("BeepControl", 1) != 0;
tmp = ConfigReadInt("BeepDevice", -1);
if (tmp == -1) InitSpeaker();
if (tmp >= 0) {
if (gSpeakerCount == 0) InitSpeaker();
if (tmp < (int)gSpeakerCount) {
SpeakerSelect(tmp);
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// Configuration menu
//////////////////////////////////////////////////////////////////////////
PMENU_ITEM gCfgMenu = NULL;
BOOLEAN gCfgMenuContinue = TRUE;
EFI_STATUS
ActionCfgReboot(IN VOID *ctx) {
gST->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
return EFI_DEVICE_ERROR;
}
EFI_STATUS
ActionCfgTpm(IN VOID *ctx) {
return gTpm->Configure(gTpm);
}
EFI_STATUS
ActionBoot(IN VOID *ctx) {
gCfgMenuContinue = FALSE;
gAuthPwdCode = AskPwdRetCancel;
return EFI_SUCCESS;
}
EFI_STATUS
ActionNewPassword(IN VOID *ctx) {
gCfgMenuContinue = FALSE;
gAuthPwdCode = AskPwdRetLogin;
return EFI_SUCCESS;
}
VOID
CfgMenuCreate() {
PMENU_ITEM item = NULL;
item = DcsMenuAppend(item, L"Boot", 'b', ActionBoot, NULL);
gCfgMenu = item;
item = DcsMenuAppend(item, L"Hard reset", 'r', ActionCfgReboot, NULL);
item = DcsMenuAppend(item, L"New password", 'n', ActionNewPassword, NULL);
if (gTpm != NULL) {
item = DcsMenuAppend(item, L"Configure TPM", 't', ActionCfgTpm, NULL);
}
}
VOID
VCAskPwd(
IN UINTN pwdType,
OUT Password* vcPwd) {
BOOL pwdReady;
if (gAuthPasswordMsg == NULL) VCAuthLoadConfig();
do {
pwdReady = TRUE;
if (pwdType == AskPwdNew) {
EFI_INPUT_KEY key;
key = KeyWait(L"Press 'c' to configure, others to skip %1d\r", 9, 0, 0);
if (key.UnicodeChar == 'c') {
PMENU_ITEM item = NULL;
EFI_STATUS res;
OUT_PRINT(L"\n%V%a %a configuration%N\n", TC_APP_NAME, VERSION_STRING);
if (gCfgMenu == NULL) CfgMenuCreate();
do {
DcsMenuPrint(gCfgMenu);
item = NULL;
key.UnicodeChar = 0;
while (item == NULL) {
item = gCfgMenu;
key = GetKey();
while (item != NULL) {
if (item->Select == key.UnicodeChar) break;
item = item->Next;
}
}
OUT_PRINT(L"%c\n", key.UnicodeChar);
res = item->Action(item->Context);
if (EFI_ERROR(res)) {
ERR_PRINT(L"%r\n", res);
}
} while (gCfgMenuContinue);
if (gAuthPwdCode == AskPwdRetCancel) {
return;
}
}
}
if (gAutoLogin) {
gAutoLogin = 0;
gAuthPwdCode = AskPwdRetLogin;
vcPwd->Length = (unsigned int)strlen(gAutoPassword);
strcpy(vcPwd->Text, gAutoPassword);
}
else {
if (gAuthPasswordType == 1 &&
gGraphOut != NULL &&
((gTouchPointer != NULL) || (gTouchSimulate != 0))) {
AskPictPwdInt(pwdType, sizeof(vcPwd->Text), vcPwd->Text, &vcPwd->Length, &gAuthPwdCode);
}
else {
switch (pwdType) {
case AskPwdNew:
OUT_PRINT(L"New password:");
break;
case AskPwdConfirm:
OUT_PRINT(L"Confirm password:");
break;
case AskPwdLogin:
default:
OUT_PRINT(L"%a", gAuthPasswordMsg);
break;
}
AskConsolePwdInt(&vcPwd->Length, vcPwd->Text, &gAuthPwdCode, sizeof(vcPwd->Text), gPasswordVisible);
}
if (gAuthPwdCode == AskPwdRetCancel) {
return;
}
}
if (gSCLocked) {
ERR_PRINT(L"Smart card is not configured\n");
}
if (gPlatformLocked) {
if (gPlatformKeyFile == NULL) {
ERR_PRINT(L"Platform key file is absent\n");
}
else {
ApplyKeyFile(vcPwd, gPlatformKeyFile, gPlatformKeyFileSize);
}
}
if (gTPMLocked) {
if (gTpm != NULL) {
pwdReady = !EFI_ERROR(gTpm->Apply(gTpm, vcPwd));
if (!pwdReady) {
ERR_PRINT(L"TPM error: DCS configuration ");
if (!gTpm->IsConfigured(gTpm)) {
ERR_PRINT(L"absent\n");
}
else {
ERR_PRINT(L"locked\n");
}
}
} else {
ERR_PRINT(L"No TPM found\n");
}
}
} while (!pwdReady);
}
VOID
VCAuthAsk()
{
VCAskPwd(AskPwdLogin, &gAuthPassword);
if (gAuthPwdCode == AskPwdRetCancel) {
return;
}
if (gAuthPimRqt) {
gAuthPim = AskInt(gAuthPimMsg, gPasswordVisible);
}
if (gAuthTcRqt) {
gAuthTc = AskConfirm("True crypt mode [N]?", gPasswordVisible);
}
if (gAuthBootRqt) {
gAuthBoot = AskConfirm("Boot mount mode [N]?", gPasswordVisible);
}
if (gAuthHashRqt) {
do {
gAuthHash = AskInt(gAuthHashMsg, gPasswordVisible);
} while (gAuthHash < 0 || gAuthHash > 4);
}
}
//////////////////////////////////////////////////////////////////////////
// VeraCrypt helpers
//////////////////////////////////////////////////////////////////////////
void* VeraCryptMemAlloc(IN UINTN size) {
return MEM_ALLOC(size);
}
void VeraCryptMemFree(IN VOID* ptr) {
MEM_FREE(ptr);
}
void ThrowFatalException(int line) {
ERR_PRINT(L"Fatal %d\n", line);
}
//////////////////////////////////////////////////////////////////////////
// Random data
//////////////////////////////////////////////////////////////////////////
BOOL
RandgetBytes(unsigned char *buf, int len, BOOL forceSlowPoll) {
EFI_STATUS res;
res = RndGetBytes(buf, len);
return !EFI_ERROR(res);
}
//////////////////////////////////////////////////////////////////////////
// Key file
//////////////////////////////////////////////////////////////////////////
#define KEYFILE_POOL_SIZE 64
#define KEYFILE_MAX_READ_LEN (1024*1024)
VOID
ApplyKeyFile(
IN OUT Password* password,
IN CHAR8* keyfileData,
IN UINTN keyfileDataSize
)
{
unsigned __int32 crc = 0xffffffff;
int writePos = 0;
size_t totalRead = 0;
size_t i;
CHAR8 keyPool[KEYFILE_POOL_SIZE];
ZeroMem(keyPool, sizeof(keyPool));
for (i = 0; i < keyfileDataSize; i++)
{
crc = UPDC32(keyfileData[i], crc);
keyPool[writePos++] += (unsigned __int8)(crc >> 24);
keyPool[writePos++] += (unsigned __int8)(crc >> 16);
keyPool[writePos++] += (unsigned __int8)(crc >> 8);
keyPool[writePos++] += (unsigned __int8)crc;
if (writePos >= KEYFILE_POOL_SIZE)
writePos = 0;
if (++totalRead >= KEYFILE_MAX_READ_LEN)
break;
}
for (i = 0; i < sizeof(keyPool); i++)
{
if (i < password->Length)
password->Text[i] += keyPool[i];
else
password->Text[i] = keyPool[i];
}
if (password->Length < (int)sizeof(keyPool))
password->Length = sizeof(keyPool);
burn (keyPool, sizeof(keyPool));
}
|