2017-11-03 13:57:58 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Block Cipher
|
|
|
|
***********************************************************************************************************************************/
|
2018-11-28 12:42:36 -05:00
|
|
|
#include "common/io/io.h"
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Data for testing
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#define TEST_CIPHER "aes-256-cbc"
|
|
|
|
#define TEST_PASS "areallybadpassphrase"
|
|
|
|
#define TEST_PASS_SIZE strlen(TEST_PASS)
|
|
|
|
#define TEST_PLAINTEXT "plaintext"
|
|
|
|
#define TEST_BUFFER_SIZE 256
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Run
|
|
|
|
***********************************************************************************************************************************/
|
2018-01-31 18:22:25 -05:00
|
|
|
void
|
2018-08-03 19:19:14 -04:00
|
|
|
testRun(void)
|
2017-11-03 13:57:58 -04:00
|
|
|
{
|
2018-05-18 11:57:32 -04:00
|
|
|
FUNCTION_HARNESS_VOID();
|
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
const Buffer *testPass = bufNewStr(strNew(TEST_PASS));
|
|
|
|
const Buffer *testPlainText = bufNewStr(strNew(TEST_PLAINTEXT));
|
|
|
|
|
2018-09-16 14:15:21 -04:00
|
|
|
// *****************************************************************************************************************************
|
2017-11-03 13:57:58 -04:00
|
|
|
if (testBegin("blockCipherNew() and blockCipherFree()"))
|
|
|
|
{
|
|
|
|
// Cipher and digest errors
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ERROR(
|
2018-11-27 22:49:24 -05:00
|
|
|
cipherBlockNewC(
|
2017-11-06 22:55:34 -05:00
|
|
|
cipherModeEncrypt, BOGUS_STR, (unsigned char *)TEST_PASS, TEST_PASS_SIZE, NULL), AssertError,
|
|
|
|
"unable to load cipher 'BOGUS'");
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_ERROR(
|
2018-11-28 12:42:36 -05:00
|
|
|
cipherBlockNew(
|
|
|
|
cipherModeEncrypt, cipherTypeAes256Cbc, testPass, strNew(BOGUS_STR)), AssertError, "unable to load digest 'BOGUS'");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
|
|
|
// Initialization of object
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
CipherBlock *cipherBlock = cipherBlockNewC(
|
|
|
|
cipherModeEncrypt, TEST_CIPHER, (unsigned char *)TEST_PASS, TEST_PASS_SIZE, NULL);
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_STR(memContextName(cipherBlock->memContext), "cipherBlock", "mem context name is valid");
|
|
|
|
TEST_RESULT_INT(cipherBlock->mode, cipherModeEncrypt, "mode is valid");
|
|
|
|
TEST_RESULT_INT(cipherBlock->passSize, TEST_PASS_SIZE, "passphrase size is valid");
|
|
|
|
TEST_RESULT_BOOL(memcmp(cipherBlock->pass, TEST_PASS, TEST_PASS_SIZE) == 0, true, "passphrase is valid");
|
|
|
|
TEST_RESULT_BOOL(cipherBlock->saltDone, false, "salt done is false");
|
|
|
|
TEST_RESULT_BOOL(cipherBlock->processDone, false, "process done is false");
|
|
|
|
TEST_RESULT_INT(cipherBlock->headerSize, 0, "header size is 0");
|
|
|
|
TEST_RESULT_PTR_NE(cipherBlock->cipher, NULL, "cipher is set");
|
|
|
|
TEST_RESULT_PTR_NE(cipherBlock->digest, NULL, "digest is set");
|
|
|
|
TEST_RESULT_PTR(cipherBlock->cipherContext, NULL, "cipher context is not set");
|
2018-11-27 22:49:24 -05:00
|
|
|
|
|
|
|
TEST_RESULT_VOID(cipherBlockFree(cipherBlock), "free cipher block");
|
|
|
|
TEST_RESULT_VOID(cipherBlockFree(NULL), "free null cipher block");
|
2017-11-03 13:57:58 -04:00
|
|
|
}
|
|
|
|
|
2018-09-16 14:15:21 -04:00
|
|
|
// *****************************************************************************************************************************
|
2017-11-03 13:57:58 -04:00
|
|
|
if (testBegin("Encrypt and Decrypt"))
|
|
|
|
{
|
|
|
|
// Encrypt
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
Buffer *encryptBuffer = bufNew(TEST_BUFFER_SIZE);
|
|
|
|
|
|
|
|
CipherBlock *blockEncrypt = cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
IoFilter *blockEncryptFilter = cipherBlockFilter(blockEncrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-11-06 22:55:34 -05:00
|
|
|
TEST_RESULT_INT(
|
2018-11-27 22:49:24 -05:00
|
|
|
cipherBlockProcessSizeC(blockEncrypt, strlen(TEST_PLAINTEXT)),
|
2017-11-06 22:55:34 -05:00
|
|
|
strlen(TEST_PLAINTEXT) + EVP_MAX_BLOCK_LENGTH + CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN, "check process size");
|
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufLimitSet(encryptBuffer, CIPHER_BLOCK_MAGIC_SIZE);
|
|
|
|
ioFilterProcessInOut(blockEncryptFilter, testPlainText, encryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(encryptBuffer), CIPHER_BLOCK_MAGIC_SIZE, "cipher size is magic size");
|
|
|
|
TEST_RESULT_BOOL(ioFilterInputSame(blockEncryptFilter), true, "filter needs same input");
|
|
|
|
|
|
|
|
bufLimitSet(encryptBuffer, CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN);
|
|
|
|
ioFilterProcessInOut(blockEncryptFilter, testPlainText, encryptBuffer);
|
|
|
|
TEST_RESULT_BOOL(ioFilterInputSame(blockEncryptFilter), false, "filter does not need same input");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
|
|
|
TEST_RESULT_BOOL(blockEncrypt->saltDone, true, "salt done is true");
|
|
|
|
TEST_RESULT_BOOL(blockEncrypt->processDone, true, "process done is true");
|
|
|
|
TEST_RESULT_INT(blockEncrypt->headerSize, 0, "header size is 0");
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_RESULT_INT(bufUsed(encryptBuffer), CIPHER_BLOCK_HEADER_SIZE, "cipher size is header len");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
|
|
|
TEST_RESULT_INT(
|
2018-11-27 22:49:24 -05:00
|
|
|
cipherBlockProcessSizeC(blockEncrypt, strlen(TEST_PLAINTEXT)),
|
2017-11-06 22:55:34 -05:00
|
|
|
strlen(TEST_PLAINTEXT) + EVP_MAX_BLOCK_LENGTH, "check process size");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufLimitSet(
|
|
|
|
encryptBuffer, CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN + (size_t)EVP_CIPHER_block_size(blockEncrypt->cipher) / 2);
|
|
|
|
ioFilterProcessInOut(blockEncryptFilter, testPlainText, encryptBuffer);
|
|
|
|
bufLimitSet(
|
|
|
|
encryptBuffer, CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN + (size_t)EVP_CIPHER_block_size(blockEncrypt->cipher));
|
|
|
|
ioFilterProcessInOut(blockEncryptFilter, testPlainText, encryptBuffer);
|
|
|
|
bufLimitClear(encryptBuffer);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_INT(
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsed(encryptBuffer), CIPHER_BLOCK_HEADER_SIZE + (size_t)EVP_CIPHER_block_size(blockEncrypt->cipher),
|
2017-11-03 13:57:58 -04:00
|
|
|
"cipher size increases by one block");
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_RESULT_BOOL(ioFilterDone(blockEncryptFilter), false, "filter is not done");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockEncryptFilter, NULL, encryptBuffer);
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_INT(
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsed(encryptBuffer), CIPHER_BLOCK_HEADER_SIZE + (size_t)(EVP_CIPHER_block_size(blockEncrypt->cipher) * 2),
|
2017-11-03 13:57:58 -04:00
|
|
|
"cipher size increases by one block on flush");
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_RESULT_BOOL(ioFilterDone(blockEncryptFilter), true, "filter is done");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
|
|
|
cipherBlockFree(blockEncrypt);
|
|
|
|
|
|
|
|
// Decrypt in one pass
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
Buffer *decryptBuffer = bufNew(TEST_BUFFER_SIZE);
|
|
|
|
|
|
|
|
CipherBlock *blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
IoFilter *blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
2017-11-06 22:55:34 -05:00
|
|
|
|
|
|
|
TEST_RESULT_INT(
|
2018-11-28 12:42:36 -05:00
|
|
|
cipherBlockProcessSizeC(blockDecrypt, bufUsed(encryptBuffer)), bufUsed(encryptBuffer) + EVP_MAX_BLOCK_LENGTH,
|
|
|
|
"check process size");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, encryptBuffer, decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), EVP_CIPHER_block_size(blockDecrypt->cipher), "decrypt size is one block");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), strlen(TEST_PLAINTEXT) * 2, "check final decrypt size");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(decryptBuffer)), TEST_PLAINTEXT TEST_PLAINTEXT, "check final decrypt buffer");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Decrypt in small chunks to test buffering
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsedZero(decryptBuffer);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewC(CIPHER_BLOCK_MAGIC_SIZE, bufPtr(encryptBuffer)), decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), 0, "no decrypt since header read is not complete");
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_BOOL(blockDecrypt->saltDone, false, "salt done is false");
|
|
|
|
TEST_RESULT_BOOL(blockDecrypt->processDone, false, "process done is false");
|
|
|
|
TEST_RESULT_INT(blockDecrypt->headerSize, CIPHER_BLOCK_MAGIC_SIZE, "check header size");
|
|
|
|
TEST_RESULT_BOOL(
|
|
|
|
memcmp(blockDecrypt->header, CIPHER_BLOCK_MAGIC, CIPHER_BLOCK_MAGIC_SIZE) == 0, true, "check header magic");
|
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(
|
|
|
|
blockDecryptFilter, bufNewC(PKCS5_SALT_LEN, bufPtr(encryptBuffer) + CIPHER_BLOCK_MAGIC_SIZE), decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), 0, "no decrypt since no data processed yet");
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_BOOL(blockDecrypt->saltDone, true, "salt done is true");
|
|
|
|
TEST_RESULT_BOOL(blockDecrypt->processDone, false, "process done is false");
|
|
|
|
TEST_RESULT_INT(blockDecrypt->headerSize, CIPHER_BLOCK_MAGIC_SIZE, "check header size (not increased)");
|
|
|
|
TEST_RESULT_BOOL(
|
|
|
|
memcmp(
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt->header + CIPHER_BLOCK_MAGIC_SIZE, bufPtr(encryptBuffer) + CIPHER_BLOCK_MAGIC_SIZE,
|
2017-11-03 13:57:58 -04:00
|
|
|
PKCS5_SALT_LEN) == 0,
|
|
|
|
true, "check header salt");
|
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(
|
|
|
|
blockDecryptFilter,
|
|
|
|
bufNewC(bufUsed(encryptBuffer) - CIPHER_BLOCK_HEADER_SIZE, bufPtr(encryptBuffer) + CIPHER_BLOCK_HEADER_SIZE),
|
|
|
|
decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), EVP_CIPHER_block_size(blockDecrypt->cipher), "decrypt size is one block");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), strlen(TEST_PLAINTEXT) * 2, "check final decrypt size");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(decryptBuffer)), TEST_PLAINTEXT TEST_PLAINTEXT, "check final decrypt buffer");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Encrypt zero byte file and decrypt it
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockEncrypt = cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockEncryptFilter = cipherBlockFilter(blockEncrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsedZero(encryptBuffer);
|
|
|
|
|
|
|
|
ioFilterProcessInOut(blockEncryptFilter, NULL, encryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(encryptBuffer), 32, "check remaining size");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockEncrypt);
|
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
|
|
|
|
|
|
|
bufUsedZero(decryptBuffer);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, encryptBuffer, decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), 0, "0 bytes processed");
|
|
|
|
ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer);
|
|
|
|
TEST_RESULT_INT(bufUsed(decryptBuffer), 0, "0 bytes on flush");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Invalid cipher header
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-11-06 22:55:34 -05:00
|
|
|
TEST_ERROR(
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewStr(strNew("1234567890123456")), decryptBuffer), CryptoError,
|
|
|
|
"cipher header invalid");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Invalid encrypted data cannot be flushed
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsedZero(decryptBuffer);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewStr(strNew(CIPHER_BLOCK_MAGIC "12345678")), decryptBuffer);
|
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewStr(strNew("1234567890123456")), decryptBuffer);
|
|
|
|
|
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "unable to flush");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// File with no header should not flush
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
|
|
|
|
|
|
|
bufUsedZero(decryptBuffer);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNew(0), decryptBuffer);
|
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "cipher header missing");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-12-22 18:36:36 -05:00
|
|
|
cipherBlockFree(blockDecrypt);
|
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// File with header only should error
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
blockDecrypt = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecryptFilter = cipherBlockFilter(blockDecrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsedZero(decryptBuffer);
|
|
|
|
|
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewStr(strNew(CIPHER_BLOCK_MAGIC "12345678")), decryptBuffer);
|
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "unable to flush");
|
2017-12-22 18:36:36 -05:00
|
|
|
|
|
|
|
cipherBlockFree(blockDecrypt);
|
2017-11-03 13:57:58 -04:00
|
|
|
}
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2018-05-18 11:57:32 -04:00
|
|
|
FUNCTION_HARNESS_RESULT_VOID();
|
2017-11-03 13:57:58 -04:00
|
|
|
}
|