Windows: Reduce time of mount with PRF auto-detection

This commit is contained in:
Mounir IDRASSI 2021-07-14 23:57:00 +02:00
parent b98606e390
commit fdf7888ab3
No known key found for this signature in database
GPG Key ID: 02C30AE90FAE4A6F
3 changed files with 112 additions and 29 deletions

View File

@ -102,12 +102,22 @@ typedef struct EncryptionThreadPoolWorkItemStruct
int IterationCount; int IterationCount;
TC_EVENT *NoOutstandingWorkItemEvent; TC_EVENT *NoOutstandingWorkItemEvent;
LONG *OutstandingWorkItemCount; LONG *OutstandingWorkItemCount;
char *Password; CRYPTOPP_ALIGN_DATA(16) char Password[MAX_PASSWORD];
int PasswordLength; int PasswordLength;
int Pkcs5Prf; int Pkcs5Prf;
char *Salt; char Salt[PKCS5_SALT_SIZE];
} KeyDerivation; } KeyDerivation;
struct
{
TC_EVENT *KeyDerivationCompletedEvent;
TC_EVENT *NoOutstandingWorkItemEvent;
LONG *outstandingWorkItemCount;
void* keyDerivationWorkItems;
int keyDerivationWorkItemsSize;
} ReadVolumeHeaderFinalization;
}; };
} EncryptionThreadPoolWorkItem; } EncryptionThreadPoolWorkItem;
@ -275,6 +285,25 @@ static TC_THREAD_PROC EncryptionThreadProc (void *threadArg)
TC_SET_EVENT (WorkItemCompletedEvent); TC_SET_EVENT (WorkItemCompletedEvent);
continue; continue;
case ReadVolumeHeaderFinalizationWork:
TC_WAIT_EVENT (*(workItem->ReadVolumeHeaderFinalization.NoOutstandingWorkItemEvent));
if (workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItems)
{
burn (workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItems, workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItemsSize);
TCfree (workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItems);
}
#if !defined(DEVICE_DRIVER)
CloseHandle (*(workItem->ReadVolumeHeaderFinalization.KeyDerivationCompletedEvent));
CloseHandle (*(workItem->ReadVolumeHeaderFinalization.NoOutstandingWorkItemEvent));
#endif
TCfree (workItem->ReadVolumeHeaderFinalization.KeyDerivationCompletedEvent);
TCfree (workItem->ReadVolumeHeaderFinalization.NoOutstandingWorkItemEvent);
TCfree (workItem->ReadVolumeHeaderFinalization.outstandingWorkItemCount);
SetWorkItemState (workItem, WorkItemFree);
TC_SET_EVENT (WorkItemCompletedEvent);
continue;
default: default:
TC_THROW_FATAL_EXCEPTION; TC_THROW_FATAL_EXCEPTION;
} }
@ -515,10 +544,10 @@ void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT
workItem->KeyDerivation.IterationCount = iterationCount; workItem->KeyDerivation.IterationCount = iterationCount;
workItem->KeyDerivation.NoOutstandingWorkItemEvent = noOutstandingWorkItemEvent; workItem->KeyDerivation.NoOutstandingWorkItemEvent = noOutstandingWorkItemEvent;
workItem->KeyDerivation.OutstandingWorkItemCount = outstandingWorkItemCount; workItem->KeyDerivation.OutstandingWorkItemCount = outstandingWorkItemCount;
workItem->KeyDerivation.Password = password; memcpy (workItem->KeyDerivation.Password, password, passwordLength);
workItem->KeyDerivation.PasswordLength = passwordLength; workItem->KeyDerivation.PasswordLength = passwordLength;
workItem->KeyDerivation.Pkcs5Prf = pkcs5Prf; workItem->KeyDerivation.Pkcs5Prf = pkcs5Prf;
workItem->KeyDerivation.Salt = salt; memcpy (workItem->KeyDerivation.Salt, salt, PKCS5_SALT_SIZE);
InterlockedIncrement (outstandingWorkItemCount); InterlockedIncrement (outstandingWorkItemCount);
TC_CLEAR_EVENT (*noOutstandingWorkItemEvent); TC_CLEAR_EVENT (*noOutstandingWorkItemEvent);
@ -528,6 +557,38 @@ void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT
TC_RELEASE_MUTEX (&EnqueueMutex); TC_RELEASE_MUTEX (&EnqueueMutex);
} }
void EncryptionThreadPoolBeginReadVolumeHeaderFinalization (TC_EVENT *keyDerivationCompletedEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG* outstandingWorkItemCount, void* keyDerivationWorkItems, int keyDerivationWorkItemsSize)
{
EncryptionThreadPoolWorkItem *workItem;
if (!ThreadPoolRunning)
TC_THROW_FATAL_EXCEPTION;
TC_ACQUIRE_MUTEX (&EnqueueMutex);
workItem = &WorkItemQueue[EnqueuePosition++];
if (EnqueuePosition >= ThreadQueueSize)
EnqueuePosition = 0;
while (GetWorkItemState (workItem) != WorkItemFree)
{
TC_WAIT_EVENT (WorkItemCompletedEvent);
}
workItem->Type = ReadVolumeHeaderFinalizationWork;
workItem->ReadVolumeHeaderFinalization.NoOutstandingWorkItemEvent = noOutstandingWorkItemEvent;
#if !defined(DEVICE_DRIVER)
workItem->ReadVolumeHeaderFinalization.KeyDerivationCompletedEvent = keyDerivationCompletedEvent;
#endif
workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItems = keyDerivationWorkItems;
workItem->ReadVolumeHeaderFinalization.keyDerivationWorkItemsSize = keyDerivationWorkItemsSize;
workItem->ReadVolumeHeaderFinalization.outstandingWorkItemCount = outstandingWorkItemCount;
SetWorkItemState (workItem, WorkItemReady);
TC_SET_EVENT (WorkItemReadyEvent);
TC_RELEASE_MUTEX (&EnqueueMutex);
}
void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, byte *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo) void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, byte *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo)
{ {

View File

@ -24,7 +24,8 @@ typedef enum
{ {
EncryptDataUnitsWork, EncryptDataUnitsWork,
DecryptDataUnitsWork, DecryptDataUnitsWork,
DeriveKeyWork DeriveKeyWork,
ReadVolumeHeaderFinalizationWork
} EncryptionThreadPoolWorkType; } EncryptionThreadPoolWorkType;
#ifndef DEVICE_DRIVER #ifndef DEVICE_DRIVER
@ -32,6 +33,7 @@ size_t GetCpuCount (WORD* pGroupCount);
#endif #endif
void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG *completionFlag, LONG *outstandingWorkItemCount, int pkcs5Prf, char *password, int passwordLength, char *salt, int iterationCount, char *derivedKey); void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG *completionFlag, LONG *outstandingWorkItemCount, int pkcs5Prf, char *password, int passwordLength, char *salt, int iterationCount, char *derivedKey);
void EncryptionThreadPoolBeginReadVolumeHeaderFinalization (TC_EVENT *keyDerivationCompletedEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG* outstandingWorkItemCount, void* keyDerivationWorkItems, int keyDerivationWorkItemsSize);
void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, byte *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo); void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, byte *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo);
BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount); BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount);
void EncryptionThreadPoolStop (); void EncryptionThreadPoolStop ();

View File

@ -179,12 +179,12 @@ int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int
int primaryKeyOffset; int primaryKeyOffset;
int pkcs5PrfCount = LAST_PRF_ID - FIRST_PRF_ID + 1; int pkcs5PrfCount = LAST_PRF_ID - FIRST_PRF_ID + 1;
#if !defined(_UEFI) #if !defined(_UEFI)
TC_EVENT keyDerivationCompletedEvent; TC_EVENT *keyDerivationCompletedEvent = NULL;
TC_EVENT noOutstandingWorkItemEvent; TC_EVENT *noOutstandingWorkItemEvent = NULL;
KeyDerivationWorkItem *keyDerivationWorkItems = NULL; KeyDerivationWorkItem *keyDerivationWorkItems = NULL;
KeyDerivationWorkItem *item; KeyDerivationWorkItem *item;
size_t encryptionThreadCount = GetEncryptionThreadCount(); size_t encryptionThreadCount = GetEncryptionThreadCount();
LONG outstandingWorkItemCount = 0; LONG *outstandingWorkItemCount = NULL;
int i; int i;
#endif #endif
size_t queuedWorkItems = 0; size_t queuedWorkItems = 0;
@ -218,29 +218,60 @@ int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int
/* use thread pool only if no PRF was specified */ /* use thread pool only if no PRF was specified */
if ((selected_pkcs5_prf == 0) && (encryptionThreadCount > 1)) if ((selected_pkcs5_prf == 0) && (encryptionThreadCount > 1))
{ {
keyDerivationCompletedEvent = TCalloc (sizeof (TC_EVENT));
if (!keyDerivationCompletedEvent)
return ERR_OUTOFMEMORY;
noOutstandingWorkItemEvent = TCalloc (sizeof (TC_EVENT));
if (!noOutstandingWorkItemEvent)
{
TCfree(keyDerivationCompletedEvent);
return ERR_OUTOFMEMORY;
}
outstandingWorkItemCount = TCalloc (sizeof (LONG));
if (!outstandingWorkItemCount)
{
TCfree(keyDerivationCompletedEvent);
TCfree(noOutstandingWorkItemEvent);
return ERR_OUTOFMEMORY;
}
keyDerivationWorkItems = TCalloc (sizeof (KeyDerivationWorkItem) * pkcs5PrfCount); keyDerivationWorkItems = TCalloc (sizeof (KeyDerivationWorkItem) * pkcs5PrfCount);
if (!keyDerivationWorkItems) if (!keyDerivationWorkItems)
{
TCfree(keyDerivationCompletedEvent);
TCfree(noOutstandingWorkItemEvent);
TCfree(outstandingWorkItemCount);
return ERR_OUTOFMEMORY; return ERR_OUTOFMEMORY;
}
for (i = 0; i < pkcs5PrfCount; ++i) for (i = 0; i < pkcs5PrfCount; ++i)
keyDerivationWorkItems[i].Free = TRUE; keyDerivationWorkItems[i].Free = TRUE;
*outstandingWorkItemCount = 0;
#ifdef DEVICE_DRIVER #ifdef DEVICE_DRIVER
KeInitializeEvent (&keyDerivationCompletedEvent, SynchronizationEvent, FALSE); KeInitializeEvent (keyDerivationCompletedEvent, SynchronizationEvent, FALSE);
KeInitializeEvent (&noOutstandingWorkItemEvent, SynchronizationEvent, TRUE); KeInitializeEvent (noOutstandingWorkItemEvent, SynchronizationEvent, TRUE);
#else #else
keyDerivationCompletedEvent = CreateEvent (NULL, FALSE, FALSE, NULL); *keyDerivationCompletedEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
if (!keyDerivationCompletedEvent) if (!*keyDerivationCompletedEvent)
{ {
TCfree (keyDerivationWorkItems); TCfree (keyDerivationWorkItems);
TCfree(keyDerivationCompletedEvent);
TCfree(noOutstandingWorkItemEvent);
TCfree(outstandingWorkItemCount);
return ERR_OUTOFMEMORY; return ERR_OUTOFMEMORY;
} }
noOutstandingWorkItemEvent = CreateEvent (NULL, FALSE, TRUE, NULL); *noOutstandingWorkItemEvent = CreateEvent (NULL, FALSE, TRUE, NULL);
if (!noOutstandingWorkItemEvent) if (!*noOutstandingWorkItemEvent)
{ {
CloseHandle (keyDerivationCompletedEvent); CloseHandle (keyDerivationCompletedEvent);
TCfree (keyDerivationWorkItems); TCfree (keyDerivationWorkItems);
TCfree(keyDerivationCompletedEvent);
TCfree(noOutstandingWorkItemEvent);
TCfree(outstandingWorkItemCount);
return ERR_OUTOFMEMORY; return ERR_OUTOFMEMORY;
} }
#endif #endif
@ -283,8 +314,8 @@ int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int
item->KeyReady = FALSE; item->KeyReady = FALSE;
item->Pkcs5Prf = enqPkcs5Prf; item->Pkcs5Prf = enqPkcs5Prf;
EncryptionThreadPoolBeginKeyDerivation (&keyDerivationCompletedEvent, &noOutstandingWorkItemEvent, EncryptionThreadPoolBeginKeyDerivation (keyDerivationCompletedEvent, noOutstandingWorkItemEvent,
&item->KeyReady, &outstandingWorkItemCount, enqPkcs5Prf, keyInfo.userKey, &item->KeyReady, outstandingWorkItemCount, enqPkcs5Prf, keyInfo.userKey,
keyInfo.keyLength, keyInfo.salt, get_pkcs5_iteration_count (enqPkcs5Prf, pim, truecryptMode, bBoot), item->DerivedKey); keyInfo.keyLength, keyInfo.salt, get_pkcs5_iteration_count (enqPkcs5Prf, pim, truecryptMode, bBoot), item->DerivedKey);
++queuedWorkItems; ++queuedWorkItems;
@ -317,7 +348,7 @@ int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int
} }
if (queuedWorkItems > 0) if (queuedWorkItems > 0)
TC_WAIT_EVENT (keyDerivationCompletedEvent); TC_WAIT_EVENT (*keyDerivationCompletedEvent);
} }
continue; continue;
KeyReady: ; KeyReady: ;
@ -587,18 +618,7 @@ ret:
#if !defined(_UEFI) #if !defined(_UEFI)
if ((selected_pkcs5_prf == 0) && (encryptionThreadCount > 1)) if ((selected_pkcs5_prf == 0) && (encryptionThreadCount > 1))
{ {
TC_WAIT_EVENT (noOutstandingWorkItemEvent); EncryptionThreadPoolBeginReadVolumeHeaderFinalization (keyDerivationCompletedEvent, noOutstandingWorkItemEvent, outstandingWorkItemCount, keyDerivationWorkItems, sizeof (KeyDerivationWorkItem) * pkcs5PrfCount);
if (keyDerivationWorkItems)
{
burn (keyDerivationWorkItems, sizeof (KeyDerivationWorkItem) * pkcs5PrfCount);
TCfree (keyDerivationWorkItems);
}
#if !defined(DEVICE_DRIVER)
CloseHandle (keyDerivationCompletedEvent);
CloseHandle (noOutstandingWorkItemEvent);
#endif
} }
#endif #endif
return status; return status;