Skip to content

Commit 2b531dd

Browse files
committed
Windows: Add an SDK for VeraCrypt Format that allows third-party application to create volumes
1 parent 1ed5225 commit 2b531dd

File tree

17 files changed

+1241
-139
lines changed

17 files changed

+1241
-139
lines changed

src/Common/BootEncryption.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static BOOL IsWindowsMBR (const uint8 *buffer, size_t bufferSize)
298298

299299
namespace VeraCrypt
300300
{
301-
#if !defined (SETUP)
301+
#if !defined (SETUP) && !defined (VCSDK_DLL)
302302

303303
class Elevator
304304
{
@@ -755,6 +755,12 @@ namespace VeraCrypt
755755
static void WriteEfiBootSectorUserConfig (uint8 userConfig, const string &customUserMessage, int pim, int hashAlg) { throw ParameterIncorrect (SRC_POS); }
756756
static void UpdateSetupConfigFile (bool bForInstall) { throw ParameterIncorrect (SRC_POS); }
757757
static void GetSecureBootConfig (BOOL* pSecureBootEnabled, BOOL *pVeraCryptKeysLoaded) { throw ParameterIncorrect (SRC_POS); }
758+
static void RegisterSystemFavoritesService(BOOL registerService) { throw ParameterIncorrect(SRC_POS); }
759+
static BOOL IsPagingFileActive(BOOL checkNonWindowsPartitionsOnly) { throw ParameterIncorrect(SRC_POS); }
760+
static void WriteLocalMachineRegistryDwordValue(wchar_t* keyPath, wchar_t* valueName, DWORD value) { throw ParameterIncorrect(SRC_POS); }
761+
static void NotifyService(DWORD dwNotifyCmd) { throw ParameterIncorrect(SRC_POS); }
762+
static void CopyFile(const wstring& sourceFile, const wstring& destinationFile) { throw ParameterIncorrect(SRC_POS); }
763+
static void DeleteFile(const wstring& file) { throw ParameterIncorrect(SRC_POS); }
758764
};
759765

760766
#endif // SETUP

src/Common/Dlgcode.c

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ BOOL VerifyModuleSignature (const wchar_t* path)
10121012

10131013
DWORD handleWin32Error (HWND hwndDlg, const char* srcPos)
10141014
{
1015-
#ifndef VC_COMREG
1015+
#if !defined(VC_COMREG) && !defined(VCSDK_DLL)
10161016
PWSTR lpMsgBuf;
10171017
DWORD dwError = GetLastError ();
10181018
wchar_t szErrorValue[32];
@@ -3675,7 +3675,7 @@ void DoPostInstallTasks (HWND hwndDlg)
36753675
SavePostInstallTasksSettings (TC_POST_INSTALL_CFG_REMOVE_ALL);
36763676
}
36773677

3678-
#ifndef SETUP_DLL
3678+
#if !defined(SETUP_DLL) && !defined(VCSDK_DLL)
36793679
// Use an idea proposed in https://medium.com/@1ndahous3/safe-code-pitfalls-dll-side-loading-winapi-and-c-73baaf48bdf5
36803680
// it allows to set safe DLL search mode for the entire process very early on, before even the CRT is initialized and global constructors are called
36813681
#pragma comment(linker, "/ENTRY:CustomMainCrtStartup")
@@ -3715,13 +3715,14 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
37153715
WNDCLASSW wc;
37163716
char langId[6];
37173717
SetDefaultDllDirectoriesPtr SetDefaultDllDirectoriesFn = NULL;
3718-
#if !defined(SETUP)
3718+
#if !defined(SETUP) && !defined(VCSDK_DLL)
37193719
wchar_t modPath[MAX_PATH];
37203720
#endif
37213721
INITCOMMONCONTROLSEX InitCtrls;
37223722

37233723
InitOSVersionInfo();
37243724

3725+
#ifndef VCSDK_DLL
37253726
if (!IsWin10BuildAtLeast(WIN_10_1809_BUILD))
37263727
{
37273728
// abort using a message that says that VeraCrypt can run only on Windows 10 version 1809 or later
@@ -3740,6 +3741,7 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
37403741
// This can happen only if KB2533623 is missing from Windows 7
37413742
AbortProcessDirect(L"VeraCrypt requires KB2533623 to be installed on Windows 7 and Windows Server 2008 R2 in order to run.");
37423743
}
3744+
#endif
37433745

37443746
VirtualLock (&CmdTokenPin, sizeof (CmdTokenPin));
37453747

@@ -3753,13 +3755,11 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
37533755
// Load RichEdit library in order to be able to use RichEdit20W class
37543756
LoadLibraryEx (L"Riched20.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
37553757

3756-
#if !defined(SETUP)
3758+
#if !defined(SETUP) && !defined(VCSDK_DLL)
37573759
GetModuleFileNameW (NULL, modPath, ARRAYSIZE (modPath));
37583760
if (!VerifyModuleSignature (modPath))
37593761
AbortProcessDirect (L"This distribution package is damaged. Please try downloading it again (preferably from the official VeraCrypt website at https://veracrypt.jp).");
3760-
#endif
37613762

3762-
#ifndef SETUP
37633763
/* enable drag-n-drop when we are running elevated */
37643764
AllowMessageInUIPI (WM_DROPFILES);
37653765
AllowMessageInUIPI (WM_COPYDATA);
@@ -3772,7 +3772,7 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
37723772
SetErrorMode (SetErrorMode (0) | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
37733773
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
37743774

3775-
#ifndef SETUP
3775+
#if !defined(SETUP) && !defined(VCSDK_DLL)
37763776
// Application ID
37773777
SetCurrentProcessExplicitAppUserModelID (TC_APPLICATION_ID);
37783778
#endif
@@ -3781,7 +3781,7 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
37813781
langId[0] = 0;
37823782
SetPreferredLangId (ConfigReadString ("Language", "", langId, sizeof (langId)));
37833783

3784-
#ifndef SETUP
3784+
#if !defined(SETUP) && !defined(VCSDK_DLL)
37853785
if (langId[0] == 0)
37863786
{
37873787
// check if user selected a language during installation
@@ -3820,7 +3820,7 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
38203820

38213821
LoadLanguageFile ();
38223822

3823-
#ifndef SETUP
3823+
#if !defined(SETUP) && !defined(VCSDK_DLL)
38243824
// UAC elevation moniker cannot be used in portable mode.
38253825
// A new instance of the application must be created with elevated privileges.
38263826
if (IsNonInstallMode () && !IsAdmin () && IsUacSupported ())
@@ -3918,7 +3918,7 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
39183918

39193919
InitHelpFileName ();
39203920

3921-
#ifndef SETUP
3921+
#if !defined(SETUP) && !defined(VCSDK_DLL)
39223922

39233923
EnableRamEncryption ((ReadDriverConfigurationFlags() & VC_DRIVER_CONFIG_ENABLE_RAM_ENCRYPTION) ? TRUE : FALSE);
39243924
if (IsRamEncryptionEnabled())
@@ -10226,7 +10226,7 @@ void CleanLastVisitedMRU (void)
1022610226
}
1022710227

1022810228

10229-
#ifndef SETUP
10229+
#if !defined(SETUP) && !defined(VCSDK_DLL)
1023010230
void ClearHistory (HWND hwndDlgItem)
1023110231
{
1023210232
ArrowWaitCursor ();
@@ -12582,7 +12582,7 @@ extern "C" BOOL IsThreadInSecureDesktop(DWORD dwThreadID)
1258212582
return bRet;
1258312583
}
1258412584

12585-
12585+
#ifndef VCSDK_DLL
1258612586
BOOL InitSecurityTokenLibrary (HWND hwndDlg)
1258712587
{
1258812588
if (SecurityTokenLibraryPath[0] == 0)
@@ -12648,7 +12648,7 @@ BOOL InitSecurityTokenLibrary (HWND hwndDlg)
1264812648

1264912649
return TRUE;
1265012650
}
12651-
12651+
#endif
1265212652
std::vector <HostDevice> GetAvailableHostDevices (bool noDeviceProperties, bool singleList, bool noFloppy, bool detectUnencryptedFilesystems)
1265312653
{
1265412654
vector <HostDevice> devices;
@@ -15032,7 +15032,7 @@ void SafeOpenURL (LPCWSTR szUrl)
1503215032
}
1503315033
}
1503415034

15035-
#if !defined(SETUP)
15035+
#if !defined(SETUP) && !defined(VCSDK_DLL)
1503615036

1503715037
#define RtlGenRandom SystemFunction036
1503815038
extern "C" BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
@@ -16179,7 +16179,7 @@ BOOL ModifyFileSecurityPermissions(const wchar_t* filePath, PSECURITY_INFO_BACKU
1617916179
}
1618016180
#endif
1618116181

16182-
#if !defined(SETUP) && !defined(VC_COMREG)
16182+
#if !defined(SETUP) && !defined(VC_COMREG) && !defined(VCSDK_DLL)
1618316183

1618416184
/*
1618516185
* Screen Protection Functions
@@ -16404,4 +16404,37 @@ BOOL AttachProtectionToCurrentThread(HWND hwnd)
1640416404
void DetachProtectionFromCurrentThread()
1640516405
{
1640616406
}
16407-
#endif
16407+
#endif
16408+
16409+
// This function moves the file pointer to the given offset. It first retrieves the current
16410+
// file position using SetFilePointerEx() with FILE_CURRENT as the reference point, and then
16411+
// calculates the difference between the current position and the desired position. Subsequently,
16412+
// it moves the file pointer by the difference calculated using SetFilePointerEx() again.
16413+
//
16414+
// This approach of moving the file pointer relatively (instead of absolutely) was implemented
16415+
// as a workaround to address the performance issues related to in-place encryption. When using
16416+
// SetFilePointerEx() with FILE_BEGIN as the reference point, reaching the end of large drives
16417+
// during in-place encryption can cause significant slowdowns. By moving the file pointer
16418+
// relatively, these performance issues are mitigated.
16419+
//
16420+
// We fall back to absolute positioning if the relative positioning fails.
16421+
BOOL MoveFilePointer(HANDLE dev, LARGE_INTEGER offset)
16422+
{
16423+
LARGE_INTEGER currOffset;
16424+
LARGE_INTEGER diffOffset;
16425+
16426+
currOffset.QuadPart = 0;
16427+
if (SetFilePointerEx(dev, currOffset, &currOffset, FILE_CURRENT))
16428+
{
16429+
diffOffset.QuadPart = offset.QuadPart - currOffset.QuadPart;
16430+
if (diffOffset.QuadPart == 0)
16431+
return TRUE;
16432+
16433+
// Moves the file pointer by the difference between current and desired positions
16434+
if (SetFilePointerEx(dev, diffOffset, NULL, FILE_CURRENT))
16435+
return TRUE;
16436+
}
16437+
16438+
// An error occurred, fallback to absolute positioning
16439+
return SetFilePointerEx(dev, offset, NULL, FILE_BEGIN);
16440+
}

src/Common/Dlgcode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,13 @@ BitLockerEncryptionStatus GetBitLockerEncryptionStatus(WCHAR driveLetter);
600600
BOOL IsTestSigningModeEnabled ();
601601
DWORD SendServiceNotification (DWORD dwNotificationCmd);
602602
DWORD FastResizeFile (const wchar_t* filePath, __int64 fileSize);
603-
#if !defined(SETUP)
603+
#if !defined(SETUP) && !defined(VCSDK_DLL)
604604
void GetAppRandomSeed (unsigned char* pbRandSeed, size_t cbRandSeed);
605605
#endif
606606
BOOL IsInternetConnected();
607607
BOOL AttachProtectionToCurrentThread(HWND hwnd);
608608
void DetachProtectionFromCurrentThread();
609+
BOOL MoveFilePointer(HANDLE dev, LARGE_INTEGER offset);
609610

610611
#if defined(SETUP) && !defined (PORTABLE)
611612
typedef struct _SECURITY_INFO_BACKUP {

src/Common/Fat.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Progress.h"
2525
#include "Random.h"
2626
#include "Volumes.h"
27+
#include "Dlgcode.h"
2728

2829
void
2930
GetFatParams (fatparams * ft)
@@ -255,7 +256,7 @@ static void PutFSInfo (unsigned char *sector, fatparams *ft)
255256

256257

257258
int
258-
FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void * dev, PCRYPTO_INFO cryptoInfo, BOOL quickFormat, BOOL bDevice)
259+
FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void * dev, PCRYPTO_INFO cryptoInfo, volatile void *volParamsArg)
259260
{
260261
int write_buf_cnt = 0;
261262
char sector[TC_MAX_VOLUME_SECTOR_SIZE], *write_buf;
@@ -267,6 +268,9 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
267268
int retVal;
268269
CRYPTOPP_ALIGN_DATA(16) char temporaryKey[MASTER_KEYDATA_SIZE];
269270
HWND hwndDlg = (HWND) hwndDlgPtr;
271+
volatile FORMAT_VOL_PARAMETERS* volParams = (volatile FORMAT_VOL_PARAMETERS*)volParamsArg;
272+
BOOL quickFormat = volParams->quickFormat;
273+
BOOL bDevice = volParams->bDevice;
270274

271275
LARGE_INTEGER startOffset;
272276
LARGE_INTEGER newOffset;
@@ -292,7 +296,7 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
292296

293297
PutBoot (ft, (unsigned char *) sector);
294298
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
295-
cryptoInfo) == FALSE)
299+
cryptoInfo, volParams) == FALSE)
296300
goto fail;
297301

298302
/* fat32 boot area */
@@ -301,7 +305,7 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
301305
/* fsinfo */
302306
PutFSInfo((unsigned char *) sector, ft);
303307
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
304-
cryptoInfo) == FALSE)
308+
cryptoInfo, volParams) == FALSE)
305309
goto fail;
306310

307311
/* reserved */
@@ -311,20 +315,20 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
311315
sector[508+3]=0xaa; /* TrailSig */
312316
sector[508+2]=0x55;
313317
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
314-
cryptoInfo) == FALSE)
318+
cryptoInfo, volParams) == FALSE)
315319
goto fail;
316320
}
317321

318322
/* bootsector backup */
319323
memset (sector, 0, ft->sector_size);
320324
PutBoot (ft, (unsigned char *) sector);
321325
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
322-
cryptoInfo) == FALSE)
326+
cryptoInfo, volParams) == FALSE)
323327
goto fail;
324328

325329
PutFSInfo((unsigned char *) sector, ft);
326330
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
327-
cryptoInfo) == FALSE)
331+
cryptoInfo, volParams) == FALSE)
328332
goto fail;
329333
}
330334

@@ -333,7 +337,7 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
333337
{
334338
memset (sector, 0, ft->sector_size);
335339
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
336-
cryptoInfo) == FALSE)
340+
cryptoInfo, volParams) == FALSE)
337341
goto fail;
338342
}
339343

@@ -377,7 +381,7 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
377381
}
378382

379383
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
380-
cryptoInfo) == FALSE)
384+
cryptoInfo, volParams) == FALSE)
381385
goto fail;
382386
}
383387
}
@@ -388,7 +392,7 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
388392
{
389393
memset (sector, 0, ft->sector_size);
390394
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
391-
cryptoInfo) == FALSE)
395+
cryptoInfo, volParams) == FALSE)
392396
goto fail;
393397

394398
}
@@ -452,10 +456,19 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
452456
while (x--)
453457
{
454458
if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo, startSector,
455-
cryptoInfo) == FALSE)
459+
cryptoInfo, volParams) == FALSE)
456460
goto fail;
457461
}
458-
UpdateProgressBar ((nSecNo - startSector) * ft->sector_size);
462+
463+
if (volParams->progress_callback)
464+
{
465+
// Call the progress callback function if it is set
466+
volParams->progress_callback ((nSecNo - startSector) * ft->sector_size, volParams->progress_callback_user_data);
467+
}
468+
else
469+
{
470+
UpdateProgressBar ((nSecNo - startSector) * ft->sector_size);
471+
}
459472

460473
if (!FlushFormatWriteBuffer (dev, write_buf, &write_buf_cnt, &nSecNo, cryptoInfo))
461474
{
@@ -498,16 +511,44 @@ FormatFat (void* hwndDlgPtr, unsigned __int64 startSector, fatparams * ft, void
498511
nSecNo++;
499512
num_sectors -= nSkipSectors;
500513

501-
if (UpdateProgressBar ((nSecNo - startSector)* ft->sector_size))
502-
goto fail;
514+
if (volParams->progress_callback)
515+
{
516+
// Call the progress callback function if it is set
517+
if (!volParams->progress_callback ((nSecNo - startSector) * ft->sector_size, volParams->progress_callback_user_data))
518+
{
519+
goto fail;
520+
}
521+
}
522+
else
523+
{
524+
if (UpdateProgressBar ((nSecNo - startSector)* ft->sector_size))
525+
goto fail;
526+
}
527+
503528
}
504529

505530
nSecNo += num_sectors;
506-
UpdateProgressBar ((nSecNo - startSector)* ft->sector_size);
531+
if (volParams->progress_callback)
532+
{
533+
// Call the progress callback function if it is set
534+
volParams->progress_callback ((nSecNo - startSector) * ft->sector_size, volParams->progress_callback_user_data);
535+
}
536+
else
537+
{
538+
UpdateProgressBar ((nSecNo - startSector)* ft->sector_size);
539+
}
507540
}
508541
else
509542
{
510-
UpdateProgressBar ((uint64) ft->num_sectors * ft->sector_size);
543+
if (volParams->progress_callback)
544+
{
545+
// Call the progress callback function if it is set
546+
volParams->progress_callback ((uint64) ft->num_sectors * ft->sector_size, volParams->progress_callback_user_data);
547+
}
548+
else
549+
{
550+
UpdateProgressBar ((uint64) ft->num_sectors * ft->sector_size);
551+
}
511552

512553
if (!FlushFormatWriteBuffer (dev, write_buf, &write_buf_cnt, &nSecNo, cryptoInfo))
513554
goto fail;

src/Common/Fat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ struct msdos_boot_sector
6666

6767
void GetFatParams ( fatparams *ft );
6868
void PutBoot ( fatparams *ft , unsigned char *boot );
69-
int FormatFat (void* hwndDlg, unsigned __int64 startSector, fatparams * ft, void * dev, PCRYPTO_INFO cryptoInfo, BOOL quickFormat, BOOL bDevice);
69+
int FormatFat (void* hwndDlg, unsigned __int64 startSector, fatparams * ft, void * dev, PCRYPTO_INFO cryptoInfo, volatile void *);

0 commit comments

Comments
 (0)