bootencryption.cpp
来自「外国开源的硬盘加密软件」· C++ 代码 · 共 2,128 行 · 第 1/5 页
CPP
2,128 行
return sum;
}
void BootEncryption::CreateBootLoaderInMemory (byte *buffer, size_t bufferSize, bool rescueDisk, bool hiddenOSCreation)
{
if (bufferSize < TC_BOOT_LOADER_AREA_SIZE - TC_BOOT_ENCRYPTION_VOLUME_HEADER_SIZE)
throw ParameterIncorrect (SRC_POS);
ZeroMemory (buffer, bufferSize);
int ea = 0;
if (GetStatus().DriveMounted)
{
try
{
GetBootEncryptionAlgorithmNameRequest request;
CallDriver (TC_IOCTL_GET_BOOT_ENCRYPTION_ALGORITHM_NAME, NULL, 0, &request, sizeof (request));
if (_stricmp (request.BootEncryptionAlgorithmName, "AES") == 0)
ea = AES;
else if (_stricmp (request.BootEncryptionAlgorithmName, "Serpent") == 0)
ea = SERPENT;
else if (_stricmp (request.BootEncryptionAlgorithmName, "Twofish") == 0)
ea = TWOFISH;
}
catch (...)
{
try
{
VOLUME_PROPERTIES_STRUCT properties;
GetVolumeProperties (&properties);
ea = properties.ea;
}
catch (...) { }
}
}
else
{
if (SelectedEncryptionAlgorithmId == 0)
throw ParameterIncorrect (SRC_POS);
ea = SelectedEncryptionAlgorithmId;
}
int bootSectorId = rescueDisk ? IDR_RESCUE_BOOT_SECTOR : IDR_BOOT_SECTOR;
int bootLoaderId = rescueDisk ? IDR_RESCUE_LOADER : IDR_BOOT_LOADER;
switch (ea)
{
case AES:
bootSectorId = rescueDisk ? IDR_RESCUE_BOOT_SECTOR_AES : IDR_BOOT_SECTOR_AES;
bootLoaderId = rescueDisk ? IDR_RESCUE_LOADER_AES : IDR_BOOT_LOADER_AES;
break;
case SERPENT:
bootSectorId = rescueDisk ? IDR_RESCUE_BOOT_SECTOR_SERPENT : IDR_BOOT_SECTOR_SERPENT;
bootLoaderId = rescueDisk ? IDR_RESCUE_LOADER_SERPENT : IDR_BOOT_LOADER_SERPENT;
break;
case TWOFISH:
bootSectorId = rescueDisk ? IDR_RESCUE_BOOT_SECTOR_TWOFISH : IDR_BOOT_SECTOR_TWOFISH;
bootLoaderId = rescueDisk ? IDR_RESCUE_LOADER_TWOFISH : IDR_BOOT_LOADER_TWOFISH;
break;
}
// Boot sector
DWORD size;
byte *bootSecResourceImg = MapResource ("BIN", bootSectorId, &size);
if (!bootSecResourceImg || size != SECTOR_SIZE)
throw ParameterIncorrect (SRC_POS);
memcpy (buffer, bootSecResourceImg, size);
*(uint16 *) (buffer + TC_BOOT_SECTOR_VERSION_OFFSET) = BE16 (VERSION_NUM);
if (IsOSAtLeast (WIN_VISTA))
buffer[TC_BOOT_SECTOR_CONFIG_OFFSET] |= TC_BOOT_CFG_FLAG_WINDOWS_VISTA_OR_LATER;
// Checksum of the backup header of the outer volume for the hidden system
if (hiddenOSCreation)
{
Device device (GetSystemDriveConfiguration().DevicePath);
byte headerSector[SECTOR_SIZE];
device.SeekAt (HiddenOSCandidatePartition.Info.StartingOffset.QuadPart + HiddenOSCandidatePartition.Info.PartitionLength.QuadPart - TC_VOLUME_HEADER_GROUP_SIZE + TC_VOLUME_HEADER_EFFECTIVE_SIZE);
device.Read (headerSector, sizeof (headerSector));
*(uint32 *) (buffer + TC_BOOT_SECTOR_OUTER_VOLUME_BAK_HEADER_CRC_OFFSET) = GetCrc32 (headerSector, sizeof (headerSector));
}
// Decompressor
byte *decompressor = MapResource ("BIN", IDR_BOOT_LOADER_DECOMPRESSOR, &size);
if (!decompressor || size > TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT * SECTOR_SIZE)
throw ParameterIncorrect (SRC_POS);
memcpy (buffer + SECTOR_SIZE, decompressor, size);
// Compressed boot loader
byte *bootLoader = MapResource ("BIN", bootLoaderId, &size);
if (!bootLoader || size > TC_MAX_BOOT_LOADER_SECTOR_COUNT * SECTOR_SIZE)
throw ParameterIncorrect (SRC_POS);
memcpy (buffer + SECTOR_SIZE + TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT * SECTOR_SIZE, bootLoader, size);
// Boot loader and decompressor checksum
*(uint16 *) (buffer + TC_BOOT_SECTOR_LOADER_LENGTH_OFFSET) = static_cast <uint16> (size);
*(uint32 *) (buffer + TC_BOOT_SECTOR_LOADER_CHECKSUM_OFFSET) = GetChecksum (buffer + SECTOR_SIZE,
TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT * SECTOR_SIZE + size);
// Backup of decompressor and boot loader
if (size + TC_BOOT_LOADER_DECOMPRESSOR_SECTOR_COUNT * SECTOR_SIZE <= TC_BOOT_LOADER_BACKUP_SECTOR_COUNT * SECTOR_SIZE)
{
memcpy (buffer + SECTOR_SIZE + TC_BOOT_LOADER_BACKUP_SECTOR_COUNT * SECTOR_SIZE,
buffer + SECTOR_SIZE, TC_BOOT_LOADER_BACKUP_SECTOR_COUNT * SECTOR_SIZE);
buffer[TC_BOOT_SECTOR_CONFIG_OFFSET] |= TC_BOOT_CFG_FLAG_BACKUP_LOADER_AVAILABLE;
}
else if (!rescueDisk && bootLoaderId != IDR_BOOT_LOADER)
{
throw ParameterIncorrect (SRC_POS);
}
}
void BootEncryption::ReadBootSectorConfig (byte *config, size_t bufLength, byte *userConfig, string *customUserMessage, uint16 *bootLoaderVersion)
{
if (config && bufLength < TC_BOOT_CFG_FLAG_AREA_SIZE)
throw ParameterIncorrect (SRC_POS);
GetSystemDriveConfigurationRequest request;
_snwprintf (request.DevicePath, array_capacity (request.DevicePath), L"%hs", GetSystemDriveConfiguration().DeviceKernelPath.c_str());
try
{
CallDriver (TC_IOCTL_GET_SYSTEM_DRIVE_CONFIG, &request, sizeof (request), &request, sizeof (request));
if (config)
*config = request.Configuration;
if (userConfig)
*userConfig = request.UserConfiguration;
if (customUserMessage)
{
request.CustomUserMessage[TC_BOOT_SECTOR_USER_MESSAGE_MAX_LENGTH] = 0;
*customUserMessage = request.CustomUserMessage;
}
if (bootLoaderVersion)
*bootLoaderVersion = request.BootLoaderVersion;
}
catch (...)
{
if (config)
*config = 0;
if (userConfig)
*userConfig = 0;
if (customUserMessage)
customUserMessage->clear();
if (bootLoaderVersion)
*bootLoaderVersion = 0;
}
}
void BootEncryption::WriteBootSectorConfig (const byte newConfig[])
{
Device device (GetSystemDriveConfiguration().DevicePath);
byte mbr[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbr, sizeof (mbr));
memcpy (mbr + TC_BOOT_SECTOR_CONFIG_OFFSET, newConfig, TC_BOOT_CFG_FLAG_AREA_SIZE);
device.SeekAt (0);
device.Write (mbr, sizeof (mbr));
byte mbrVerificationBuf[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbrVerificationBuf, sizeof (mbr));
if (memcmp (mbr, mbrVerificationBuf, sizeof (mbr)) != 0)
throw ErrorException ("ERROR_MBR_PROTECTED");
}
void BootEncryption::WriteBootSectorUserConfig (byte userConfig, const string &customUserMessage)
{
Device device (GetSystemDriveConfiguration().DevicePath);
byte mbr[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbr, sizeof (mbr));
if (!BufferContainsString (mbr, sizeof (mbr), TC_APP_NAME)
|| BE16 (*(uint16 *) (mbr + TC_BOOT_SECTOR_VERSION_OFFSET)) > VERSION_NUM)
{
return;
}
mbr[TC_BOOT_SECTOR_USER_CONFIG_OFFSET] = userConfig;
memset (mbr + TC_BOOT_SECTOR_USER_MESSAGE_OFFSET, 0, TC_BOOT_SECTOR_USER_MESSAGE_MAX_LENGTH);
if (!customUserMessage.empty())
{
if (customUserMessage.size() > TC_BOOT_SECTOR_USER_MESSAGE_MAX_LENGTH)
throw ParameterIncorrect (SRC_POS);
memcpy (mbr + TC_BOOT_SECTOR_USER_MESSAGE_OFFSET, customUserMessage.c_str(), customUserMessage.size());
}
device.SeekAt (0);
device.Write (mbr, sizeof (mbr));
byte mbrVerificationBuf[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbrVerificationBuf, sizeof (mbr));
if (memcmp (mbr, mbrVerificationBuf, sizeof (mbr)) != 0)
throw ErrorException ("ERROR_MBR_PROTECTED");
}
unsigned int BootEncryption::GetHiddenOSCreationPhase ()
{
byte configFlags [TC_BOOT_CFG_FLAG_AREA_SIZE];
ReadBootSectorConfig (configFlags, sizeof(configFlags));
return (configFlags[0] & TC_BOOT_CFG_MASK_HIDDEN_OS_CREATION_PHASE);
}
void BootEncryption::SetHiddenOSCreationPhase (unsigned int newPhase)
{
#if TC_BOOT_CFG_FLAG_AREA_SIZE != 1
# error TC_BOOT_CFG_FLAG_AREA_SIZE != 1; revise GetHiddenOSCreationPhase() and SetHiddenOSCreationPhase()
#endif
byte configFlags [TC_BOOT_CFG_FLAG_AREA_SIZE];
ReadBootSectorConfig (configFlags, sizeof(configFlags));
configFlags[0] &= (byte) ~TC_BOOT_CFG_MASK_HIDDEN_OS_CREATION_PHASE;
configFlags[0] |= newPhase;
WriteBootSectorConfig (configFlags);
}
#ifndef SETUP
void BootEncryption::StartDecoyOSWipe (WipeAlgorithmId wipeAlgorithm)
{
if (!IsHiddenOSRunning())
throw ParameterIncorrect (SRC_POS);
WipeDecoySystemRequest request;
ZeroMemory (&request, sizeof (request));
request.WipeAlgorithm = wipeAlgorithm;
if (Randinit() != ERR_SUCCESS)
throw ParameterIncorrect (SRC_POS);
UserEnrichRandomPool (ParentWindow);
if (!RandgetBytes (request.WipeKey, sizeof (request.WipeKey), TRUE))
throw ParameterIncorrect (SRC_POS);
CallDriver (TC_IOCTL_START_DECOY_SYSTEM_WIPE, &request, sizeof (request), NULL, 0);
burn (&request, sizeof (request));
}
void BootEncryption::AbortDecoyOSWipe ()
{
CallDriver (TC_IOCTL_ABORT_DECOY_SYSTEM_WIPE);
}
DecoySystemWipeStatus BootEncryption::GetDecoyOSWipeStatus ()
{
DecoySystemWipeStatus status;
CallDriver (TC_IOCTL_GET_DECOY_SYSTEM_WIPE_STATUS, NULL, 0, &status, sizeof (status));
return status;
}
void BootEncryption::CheckDecoyOSWipeResult ()
{
CallDriver (TC_IOCTL_GET_DECOY_SYSTEM_WIPE_RESULT);
}
void BootEncryption::WipeHiddenOSCreationConfig ()
{
if (IsHiddenOSRunning() || Randinit() != ERR_SUCCESS)
throw ParameterIncorrect (SRC_POS);
Device device (GetSystemDriveConfiguration().DevicePath);
byte mbr[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbr, sizeof (mbr));
finally_do_arg (BootEncryption *, this,
{
try
{
finally_arg->SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_NONE);
} catch (...) { }
});
#if PRAND_DISK_WIPE_PASSES > RNG_POOL_SIZE
# error PRAND_DISK_WIPE_PASSES > RNG_POOL_SIZE
#endif
byte randData[PRAND_DISK_WIPE_PASSES];
if (!RandgetBytes (randData, sizeof (randData), FALSE))
throw ParameterIncorrect (SRC_POS);
for (int wipePass = 0; wipePass < PRAND_DISK_WIPE_PASSES; wipePass++)
{
for (int i = 0; i < TC_BOOT_SECTOR_OUTER_VOLUME_BAK_HEADER_CRC_SIZE; ++i)
{
mbr[TC_BOOT_SECTOR_OUTER_VOLUME_BAK_HEADER_CRC_OFFSET + i] = randData[wipePass];
}
mbr[TC_BOOT_SECTOR_CONFIG_OFFSET] &= (byte) ~TC_BOOT_CFG_MASK_HIDDEN_OS_CREATION_PHASE;
mbr[TC_BOOT_SECTOR_CONFIG_OFFSET] |= randData[wipePass] & TC_BOOT_CFG_MASK_HIDDEN_OS_CREATION_PHASE;
if (wipePass == PRAND_DISK_WIPE_PASSES - 1)
memset (mbr + TC_BOOT_SECTOR_OUTER_VOLUME_BAK_HEADER_CRC_OFFSET, 0, TC_BOOT_SECTOR_OUTER_VOLUME_BAK_HEADER_CRC_SIZE);
device.SeekAt (0);
device.Write (mbr, sizeof (mbr));
}
for (int wipePass = 0; wipePass < PRAND_DISK_WIPE_PASSES/4 + 1; wipePass++)
{
SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_NONE);
SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_CLONING);
SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_WIPING);
SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_WIPED);
}
SetHiddenOSCreationPhase (TC_HIDDEN_OS_CREATION_PHASE_NONE);
}
#endif // !SETUP
void BootEncryption::InstallBootLoader (bool preserveUserConfig, bool hiddenOSCreation)
{
byte bootLoaderBuf[TC_BOOT_LOADER_AREA_SIZE - TC_BOOT_ENCRYPTION_VOLUME_HEADER_SIZE];
CreateBootLoaderInMemory (bootLoaderBuf, sizeof (bootLoaderBuf), false, hiddenOSCreation);
// Write MBR
Device device (GetSystemDriveConfiguration().DevicePath);
byte mbr[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbr, sizeof (mbr));
if (preserveUserConfig && BufferContainsString (mbr, sizeof (mbr), TC_APP_NAME))
{
uint16 version = BE16 (*(uint16 *) (mbr + TC_BOOT_SECTOR_VERSION_OFFSET));
if (version != 0)
{
bootLoaderBuf[TC_BOOT_SECTOR_USER_CONFIG_OFFSET] = mbr[TC_BOOT_SECTOR_USER_CONFIG_OFFSET];
memcpy (bootLoaderBuf + TC_BOOT_SECTOR_USER_MESSAGE_OFFSET, mbr + TC_BOOT_SECTOR_USER_MESSAGE_OFFSET, TC_BOOT_SECTOR_USER_MESSAGE_MAX_LENGTH);
}
}
memcpy (mbr, bootLoaderBuf, TC_MAX_MBR_BOOT_CODE_SIZE);
device.SeekAt (0);
device.Write (mbr, sizeof (mbr));
byte mbrVerificationBuf[SECTOR_SIZE];
device.SeekAt (0);
device.Read (mbrVerificationBuf, sizeof (mbr));
if (memcmp (mbr, mbrVerificationBuf, sizeof (mbr)) != 0)
throw ErrorException ("ERROR_MBR_PROTECTED");
// Write boot loader
device.SeekAt (SECTOR_SIZE);
device.Write (bootLoaderBuf + SECTOR_SIZE, sizeof (bootLoaderBuf) - SECTOR_SIZE);
}
string BootEncryption::GetSystemLoaderBackupPath ()
{
char pathBuf[MAX_PATH];
throw_sys_if (!SUCCEEDED (SHGetFolderPath (NULL, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, pathBuf)));
string path = string (pathBuf) + "\\" TC_APP_NAME;
CreateDirectory (path.c_str(), NULL);
return path + '\\' + TC_SYS_BOOT_LOADER_BACKUP_NAME;
}
void BootEncryption::RenameDeprecatedSystemLoaderBackup ()
{
char pathBuf[MAX_PATH];
if (SUCCEEDED (SHGetFolderPath (NULL, CSIDL_COMMON_APPDATA, NULL, 0, pathBuf)))
{
string path = string (pathBuf) + "\\" TC_APP_NAME + '\\' + TC_SYS_BOOT_LOADER_BACKUP_NAME_LEGACY;
if (FileExists (path.c_str()) && !FileExists (GetSystemLoaderBackupPath().c_str()))
throw_sys_if (rename (path.c_str(), GetSystemLoaderBackupPath().c_str()) != 0);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?