freeotfecypheraes_ltc.c
来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 371 行
C
371 行
// Description:
// By Sarah Dean
// Email: sdean12@sdean12.org
// WWW: http://www.FreeOTFE.org/
//
// -----------------------------------------------------------------------------
//
#ifndef WINCE
#include <ntddk.h>
#endif
#include "FreeOTFECypherAES_ltc.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 libtomcrypt AES library...
//#define CBC 1
//#include <mycrypt.h>
//#include <mycrypt_cipher.h>
#include <mycrypt_custom.h>
// =========================================================================
// Cypher driver init function
// driverInfo - The structure to be initializedNTSTATUS
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;
// -- AES-128 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_AES_128,
strlen(DRIVER_CIPHER_TITLE_AES_128)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 128;
driverInfo->CypherDetails[idx].BlockSize = (aes_desc.block_length * 8);
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_AES_128;
// -- AES-192 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_AES_192,
strlen(DRIVER_CIPHER_TITLE_AES_192)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 192;
driverInfo->CypherDetails[idx].BlockSize = (aes_desc.block_length * 8);
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_AES_192;
// -- AES-256 --
idx++;
FREEOTFE_MEMZERO(driverInfo->CypherDetails[idx].Title, MAX_CYPHER_TITLE);
FREEOTFE_MEMCPY(
driverInfo->CypherDetails[idx].Title,
DRIVER_CIPHER_TITLE_AES_256,
strlen(DRIVER_CIPHER_TITLE_AES_256)
);
driverInfo->CypherDetails[idx].Mode = CYPHER_MODE_CBC;
driverInfo->CypherDetails[idx].KeySize = 256;
driverInfo->CypherDetails[idx].BlockSize = (aes_desc.block_length * 8);
driverInfo->CypherDetails[idx].VersionID = DRIVER_CYPHER_IMPL_VERSION;
driverInfo->CypherDetails[idx].CypherGUID = CIPHER_GUID_AES_256;
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherDriverExtDetailsInit\n")));
return status;
}
// =========================================================================
// Cypher driver cleardown function
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;
// libtomcrypt can't handle NULL IVs in CBC mode - it ASSERTs that IV != NULL
char ltcNullIV[FREEOTFE_MAX_CYPHER_BLOCKSIZE];
int cipher;
unsigned int i;
symmetric_CBC *cbc;
int errnum;
unsigned char* inBufPtr;
unsigned char* outBufPtr;
unsigned int x;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherEncryptData\n")));
if (!(
(IsEqualGUID(&CIPHER_GUID_AES_128, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_AES_192, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_AES_256, CypherGUID))
))
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unsupported cipher GUID passed in.\n")));
status = STATUS_INVALID_PARAMETER;
}
// libtomcrypt can't handle NULL IVs in CBC mode - it ASSERTs that IV != NULL
if ( (IVLength == 0) || (IV == NULL) )
{
FREEOTFE_MEMZERO(<cNullIV, sizeof(ltcNullIV));
IV = (char*)<cNullIV;
}
cbc = FREEOTFE_MEMALLOC(sizeof(symmetric_CBC));
FREEOTFE_MEMZERO(cbc, sizeof(symmetric_CBC));
if NT_SUCCESS(status)
{
status = InitLTCCypher(&cipher);
}
if NT_SUCCESS(status)
{
// Start a CBC session
if ((errnum = cbc_start(cipher, IV, Key, (KeyLength/8), 0, cbc)) != CRYPT_OK)
{
status = STATUS_UNSUCCESSFUL;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unable to start CBC session (errnum: %d)\n"), errnum));
}
}
if NT_SUCCESS(status)
{
x = (unsigned int)(int)(PlaintextLength / aes_desc.block_length);
inBufPtr = PlaintextData;
outBufPtr = CyphertextData;
for (i = 0; i < x; i++)
{
if ((errnum = cbc_encrypt(inBufPtr, outBufPtr, cbc)) != CRYPT_OK)
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unable to encrypt/decrypt block (errnum: %d)\n"), errnum));
status = STATUS_UNSUCCESSFUL;
}
inBufPtr += aes_desc.block_length;
outBufPtr += aes_desc.block_length;
}
}
SecZeroMemory(cbc, sizeof(symmetric_CBC));
FREEOTFE_FREE(cbc);
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;
// libtomcrypt can't handle NULL IVs in CBC mode - it ASSERTs that IV != NULL
char ltcNullIV[FREEOTFE_MAX_CYPHER_BLOCKSIZE];
int cipher;
unsigned int i;
symmetric_CBC *cbc;
int errnum;
unsigned char* inBufPtr;
unsigned char* outBufPtr;
unsigned int x;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("ImpCypherDecryptData\n")));
if (!(
(IsEqualGUID(&CIPHER_GUID_AES_128, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_AES_192, CypherGUID)) ||
(IsEqualGUID(&CIPHER_GUID_AES_256, CypherGUID))
))
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unsupported cipher GUID passed in.\n")));
status = STATUS_INVALID_PARAMETER;
}
// libtomcrypt can't handle NULL IVs in CBC mode - it ASSERTs that IV != NULL
if ( (IVLength == 0) || (IV == NULL) )
{
FREEOTFE_MEMZERO(<cNullIV, sizeof(ltcNullIV));
IV = (char*)<cNullIV;
}
cbc = FREEOTFE_MEMALLOC(sizeof(symmetric_CBC));
FREEOTFE_MEMZERO(cbc, sizeof(symmetric_CBC));
if NT_SUCCESS(status)
{
status = InitLTCCypher(&cipher);
}
if NT_SUCCESS(status)
{
// Start a CBC session
if ((errnum = cbc_start(cipher, IV, Key, (KeyLength/8), 0, cbc)) != CRYPT_OK)
{
status = STATUS_UNSUCCESSFUL;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unable to start CBC session (errnum: %d)\n"), errnum));
}
}
if NT_SUCCESS(status)
{
x = (unsigned int)(int)(CyphertextLength / aes_desc.block_length);
inBufPtr = CyphertextData;
outBufPtr = PlaintextData;
for (i = 0; i < x; i++)
{
if ((errnum = cbc_decrypt(inBufPtr, outBufPtr, cbc)) != CRYPT_OK)
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Unable to encrypt/decrypt block (errnum: %d)\n"), errnum));
status = STATUS_UNSUCCESSFUL;
}
inBufPtr += aes_desc.block_length;
outBufPtr += aes_desc.block_length;
}
}
SecZeroMemory(cbc, sizeof(symmetric_CBC));
FREEOTFE_FREE(cbc);
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("ImpCypherDecryptData\n")));
return status;
}
// =========================================================================
// Initialize libtomcrypt cypher
NTSTATUS
InitLTCCypher(
OUT int *cipher
)
{
NTSTATUS status = STATUS_CRYPTO_SYSTEM_INVALID;
DEBUGOUTCYPHERIMPL(DEBUGLEV_ENTER, (TEXT("InitLTCCypher\n")));
// Initialize cipher
*cipher = register_cipher(&aes_desc);
if (*cipher == -1)
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Could not register cipher\n")));
}
else
{
*cipher = find_cipher("aes");
if (*cipher == -1)
{
DEBUGOUTCYPHERIMPL(DEBUGLEV_ERROR, (TEXT("Could not find cipher\n")));
}
else
{
status = STATUS_SUCCESS;
}
}
DEBUGOUTCYPHERIMPL(DEBUGLEV_EXIT, (TEXT("InitLTCCypher\n")));
return status;
}
// =========================================================================
// =========================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?