freeotfecyphertwofish_gladman.c
来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 293 行
C
293 行
// Description:
// By Sarah Dean
// Email: sdean12@sdean12.org
// WWW: http://www.FreeOTFE.org/
//
// -----------------------------------------------------------------------------
//
#ifndef WINCE
#include <ntddk.h>
#endif
#include "FreeOTFECypherTwofish_Gladman.h"
#include "FreeOTFECypherImpl.h"
#include "FreeOTFECypherAPICommon.h"
#ifdef WINCE
#include "FreeOTFE4PDACypherDriver.h" // Required for DRIVER_CYPHER_VERSION
#else
#include "FreeOTFECypherDriver.h" // Required for DRIVER_CYPHER_VERSION
#endif
#include "FreeOTFEPlatform.h"
#include "FreeOTFEDebug.h"
#include "FreeOTFElib.h"
// Include Gladman library...
#include <aes_defs.h>
#include <twofish.h>
// =========================================================================
// Cypher driver init function
// driverInfo - The structure to be initialized
NTSTATUS
ImpCypherDriverExtDetailsInit(
IN OUT CYPHER_DRIVER_INFO* driverInfo
)
{
NTSTATUS status = STATUS_SUCCESS;
int idx = -1;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherDriverExtDetailsInit\n")));
// -- POPULATE DRIVER IDENTIFICATION --
FREEOTFE_MEMZERO(driverInfo->DriverTitle, sizeof(driverInfo->DriverTitle));
FREEOTFE_MEMCPY(
driverInfo->DriverTitle,
DRIVER_TITLE,
strlen(DRIVER_TITLE)
);
driverInfo->DriverGUID = DRIVER_GUID;
driverInfo->DriverVersionID = DRIVER_CYPHER_VERSION;
// -- POPULATE cyphers SUPPORTED --
driverInfo->CypherDetails = FREEOTFE_MEMALLOC((sizeof(CYPHER) * CYPHERS_SUPPORTED));
driverInfo->CypherCount = CYPHERS_SUPPORTED;
// -- Twofish-128 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_TWOFISH_128,
strlen(DRIVER_CIPHER_TITLE_TWOFISH_128)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 128;
driverInfo->CypherDetails[idx].BlockSize = TWOFISH_BLOCK_SIZE;
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_TWOFISH_128;
// -- Twofish-192 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_TWOFISH_192,
strlen(DRIVER_CIPHER_TITLE_TWOFISH_192)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 192;
driverInfo->CypherDetails[idx].BlockSize = TWOFISH_BLOCK_SIZE;
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_TWOFISH_192;
// -- Twofish-256 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_TWOFISH_256,
strlen(DRIVER_CIPHER_TITLE_TWOFISH_256)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 256;
driverInfo->CypherDetails[idx].BlockSize = TWOFISH_BLOCK_SIZE;
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_TWOFISH_256;
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherDriverExtDetailsInit\n")));
return status;
}
// =========================================================================
// Cypher driver cleardown function
// driverInfo - The structure to be cleared down
NTSTATUS
ImpCypherDriverExtDetailsCleardown(
IN OUT CYPHER_DRIVER_INFO* driverInfo
)
{
NTSTATUS status = STATUS_SUCCESS;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherDriverExtDetailsCleardown\n")));
if (driverInfo->CypherDetails != NULL)
{
FREEOTFE_FREE(driverInfo->CypherDetails);
}
driverInfo->CypherDetails = NULL;
driverInfo->CypherCount = 0;
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherDriverExtDetailsCleardown\n")));
return status;
}
// =========================================================================
// Encryption function
// Note: CyphertextLength must be set to the size of the CyphertextData buffer on
// entry; on exit, this will be set to the size of the buffer used.
NTSTATUS
ImpCypherEncryptData(
IN GUID* CypherGUID,
IN int KeyLength, // In bits
IN char *Key,
IN char *KeyASCII, // ASCII representation of "Key"
IN int IVLength, // In bits
IN char *IV,
IN int PlaintextLength, // In bytes
IN char *PlaintextData,
OUT char *CyphertextData
)
{
NTSTATUS status = STATUS_SUCCESS;
// Null IV in case we're not given an IV
char zeroIV[FREEOTFE_MAX_CYPHER_BLOCKSIZE];
char tmpBuffer[(TWOFISH_BLOCK_SIZE / 8)];
int i;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherEncryptData\n")));
if (!(
(IsEqualGUID(&CIPHER_GUID_TWOFISH_128, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_TWOFISH_192, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_TWOFISH_256, CypherGUID))
))
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unsupported cipher GUID passed in.\n")));
status = STATUS_INVALID_PARAMETER;
}
else
{
// Handle situation where a NULL/zero length IV is passed in
if ( (IVLength == 0) || (IV == NULL) )
{
FREEOTFE_MEMZERO(&zeroIV, sizeof(zeroIV));
IV = (char*)&zeroIV;
}
// Process data using CBC mode
//
// CBC Mode ENCRYPT:
// *) Setup key
// *) For each block...
// *) XOR plaintext with IV
// *) Encrypt XOR'd plaintext with key
// *) IV = The just encrypted data
// Note: "enc" specified; we're ENCRYPTING
// Note: We use a temp buffer (tmpBuffer) because we can't write to
// PlaintextData
TWOFISH(set_key(Key, KeyLength, enc));
for (i=0; i < PlaintextLength; i += (TWOFISH_BLOCK_SIZE / 8))
{
XORBlock(PlaintextData+i, IV, tmpBuffer, (TWOFISH_BLOCK_SIZE / 8));
TWOFISH(encrypt(tmpBuffer, CyphertextData+i));
IV = CyphertextData+i;
}
SecZeroMemory(tmpBuffer, sizeof(tmpBuffer));
}
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherEncryptData\n")));
return status;
}
// =========================================================================
// Decryption function
// Note: PlaintextLength must be set to the size of the PlaintextData buffer on
// entry; on exit, this will be set to the size of the buffer used.
NTSTATUS
ImpCypherDecryptData(
IN GUID* CypherGUID,
IN int KeyLength, // In bits
IN char *Key,
IN char *KeyASCII, // ASCII representation of "Key"
IN int IVLength, // In bits
IN char *IV,
IN int CyphertextLength, // In bytes
IN char *CyphertextData,
OUT char *PlaintextData
)
{
NTSTATUS status = STATUS_SUCCESS;
// Null IV in case we're not given an IV
char zeroIV[FREEOTFE_MAX_CYPHER_BLOCKSIZE];
int i;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherDecryptData\n")));
if (!(
(IsEqualGUID(&CIPHER_GUID_TWOFISH_128, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_TWOFISH_192, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_TWOFISH_256, CypherGUID))
))
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unsupported cipher GUID passed in.\n")));
status = STATUS_INVALID_PARAMETER;
}
else
{
// Handle situation where a NULL/zero length IV is passed in
if ( (IVLength == 0) || (IV == NULL) )
{
FREEOTFE_MEMZERO(&zeroIV, sizeof(zeroIV));
IV = (char*)&zeroIV;
}
// Process data using CBC mode
//
// CBC Mode DECRYPT:
// *) Setup key
// *) For each block...
// *) Decrypt cyphertext with key
// *) XOR output plaintext with IV
// *) IV = Cyphertext data
// Note: "dec" specified; we're DECRYPTING
TWOFISH(set_key(Key, KeyLength, dec));
for (i=0; i < CyphertextLength; i += (TWOFISH_BLOCK_SIZE / 8))
{
TWOFISH(decrypt(CyphertextData+i, PlaintextData+i));
XORBlock(PlaintextData+i, IV, PlaintextData+i, (TWOFISH_BLOCK_SIZE / 8));
IV = CyphertextData+i;
}
}
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherDecryptData\n")));
return status;
}
// =========================================================================
// =========================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?