Windows driver: better randomness for wipe bytes by always using Whirlpool hash of current time and random bytes retrieved using CPU RDRAND/RDSEED if available.

This commit is contained in:
Mounir IDRASSI 2019-01-31 01:10:38 +01:00
parent 61c1baa4bf
commit 5571a8ba6b
No known key found for this signature in database
GPG Key ID: 02C30AE90FAE4A6F
3 changed files with 40 additions and 19 deletions

View File

@ -28,6 +28,7 @@
#include "DriveFilter.h"
#include "Boot/Windows/BootCommon.h"
#include "cpu.h"
#include "rdrand.h"
static BOOL DeviceFilterActive = FALSE;
@ -1521,30 +1522,38 @@ static VOID SetupThreadProc (PVOID threadArg)
// generate real random values for wipeRandChars and
// wipeRandCharsUpdate instead of relying on uninitialized stack memory
LARGE_INTEGER iSeed;
byte digest[WHIRLPOOL_DIGESTSIZE];
WHIRLPOOL_CTX tctx;
#ifndef _WIN64
KFLOATING_SAVE floatingPointState;
NTSTATUS saveStatus = STATUS_INVALID_PARAMETER;
if (HasISSE())
saveStatus = KeSaveFloatingPointState (&floatingPointState);
#endif
KeQuerySystemTime( &iSeed );
if (KeGetCurrentIrql() < DISPATCH_LEVEL)
WHIRLPOOL_init (&tctx);
WHIRLPOOL_add ((unsigned char *) &(iSeed.QuadPart), sizeof(iSeed.QuadPart), &tctx);
// use RDSEED or RDRAND from CPU as source of entropy if present
if ( (HasRDSEED() && RDSEED_getBytes (digest, sizeof (digest)))
|| (HasRDRAND() && RDRAND_getBytes (digest, sizeof (digest)))
)
{
ULONG ulRandom;
ulRandom = RtlRandomEx( &iSeed.LowPart );
memcpy (wipeRandChars, &ulRandom, TC_WIPE_RAND_CHAR_COUNT);
ulRandom = RtlRandomEx( &ulRandom );
memcpy (wipeRandCharsUpdate, &ulRandom, TC_WIPE_RAND_CHAR_COUNT);
burn (&ulRandom, sizeof(ulRandom));
WHIRLPOOL_add (digest, sizeof(digest), &tctx);
}
else
{
byte digest[SHA512_DIGESTSIZE];
sha512_ctx tctx;
sha512_begin (&tctx);
sha512_hash ((unsigned char *) &(iSeed.QuadPart), sizeof(iSeed.QuadPart), &tctx);
sha512_end (digest, &tctx);
WHIRLPOOL_finalize (&tctx, digest);
memcpy (wipeRandChars, digest, TC_WIPE_RAND_CHAR_COUNT);
memcpy (wipeRandCharsUpdate, &digest[SHA512_DIGESTSIZE - TC_WIPE_RAND_CHAR_COUNT], TC_WIPE_RAND_CHAR_COUNT);
#if !defined (_WIN64)
if (NT_SUCCESS (saveStatus))
KeRestoreFloatingPointState (&floatingPointState);
#endif
burn (digest, SHA512_DIGESTSIZE);
burn (&tctx, sizeof (tctx));
}
memcpy (wipeRandChars, digest, TC_WIPE_RAND_CHAR_COUNT);
memcpy (wipeRandCharsUpdate, &digest[WHIRLPOOL_DIGESTSIZE - TC_WIPE_RAND_CHAR_COUNT], TC_WIPE_RAND_CHAR_COUNT);
burn (digest, WHIRLPOOL_DIGESTSIZE);
burn (&tctx, sizeof (tctx));
burn (&iSeed, sizeof(iSeed));

View File

@ -193,6 +193,7 @@ BuildDriver.cmd -rebuild -debug -x64 "$(SolutionDir)\Common" "$(SolutionDir)\Cry
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Crypto\Camellia.c" />
<ClCompile Include="..\Crypto\rdrand.c" />
<ClCompile Include="..\Crypto\SerpentFast.c" />
<ClCompile Include="..\Crypto\SerpentFast_simd.cpp" />
<ClCompile Include="DriveFilter.c" />
@ -225,6 +226,7 @@ BuildDriver.cmd -rebuild -debug -x64 "$(SolutionDir)\Common" "$(SolutionDir)\Cry
<None Include="..\Crypto\Aes_x86.asm" />
<None Include="..\Crypto\Camellia_aesni_x64.S" />
<None Include="..\Crypto\Camellia_x64.S" />
<None Include="..\Crypto\rdrand_ml.asm" />
<None Include="..\Crypto\sha256-x86-nayuki.S">
<FileType>Document</FileType>
</None>
@ -266,6 +268,7 @@ BuildDriver.cmd -rebuild -debug -x64 "$(SolutionDir)\Common" "$(SolutionDir)\Cry
<ClInclude Include="..\Common\Apidrvr.h" />
<ClInclude Include="..\Common\Cache.h" />
<ClInclude Include="..\Common\Common.h" />
<ClInclude Include="..\Crypto\rdrand.h" />
<ClInclude Include="DriveFilter.h" />
<ClInclude Include="DumpFilter.h" />
<ClInclude Include="EncryptedIoQueue.h" />

View File

@ -108,6 +108,9 @@
<ClCompile Include="..\Crypto\SerpentFast_simd.cpp">
<Filter>Source Files\Crypto</Filter>
</ClCompile>
<ClCompile Include="..\Crypto\rdrand.c">
<Filter>Source Files\Crypto</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\Crypto\Aes_hw_cpu.asm">
@ -179,6 +182,9 @@
<None Include="..\Crypto\sha512_avx2_x64.asm">
<Filter>Source Files\Crypto</Filter>
</None>
<None Include="..\Crypto\rdrand_ml.asm">
<Filter>Source Files\Crypto</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Common\Apidrvr.h">
@ -229,6 +235,9 @@
<ClInclude Include="..\Common\Xts.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\Crypto\rdrand.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Driver.rc">