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_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();
|
|
|
|
|
2019-04-20 08:16:17 -04:00
|
|
|
const Buffer *testPass = BUFSTRDEF(TEST_PASS);
|
|
|
|
const Buffer *testPlainText = BUFSTRDEF(TEST_PLAINTEXT);
|
2018-11-28 12:42:36 -05:00
|
|
|
|
2018-09-16 14:15:21 -04:00
|
|
|
// *****************************************************************************************************************************
|
2019-03-10 13:27:30 +02:00
|
|
|
if (testBegin("Common"))
|
|
|
|
{
|
|
|
|
TEST_RESULT_BOOL(cryptoIsInit(), false, "crypto is not initialized");
|
|
|
|
TEST_RESULT_VOID(cryptoInit(), "initialize crypto");
|
|
|
|
TEST_RESULT_BOOL(cryptoIsInit(), true, "crypto is initialized");
|
|
|
|
TEST_RESULT_VOID(cryptoInit(), "initialize crypto again");
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
cryptoInit();
|
|
|
|
|
|
|
|
TEST_RESULT_VOID(cryptoError(false, "no error here"), "no error");
|
|
|
|
|
|
|
|
EVP_MD_CTX *context = EVP_MD_CTX_create();
|
|
|
|
TEST_ERROR(
|
|
|
|
cryptoError(EVP_DigestInit_ex(context, NULL, NULL) != 1, "unable to initialize hash context"), CryptoError,
|
|
|
|
"unable to initialize hash context: [101187723] no digest set");
|
|
|
|
EVP_MD_CTX_destroy(context);
|
|
|
|
|
|
|
|
TEST_ERROR(cryptoError(true, "no error"), CryptoError, "no error: [0] no details available");
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ERROR(cipherType(strNew(BOGUS_STR)), AssertError, "invalid cipher name 'BOGUS'");
|
|
|
|
TEST_RESULT_UINT(cipherType(strNew("none")), cipherTypeNone, "none type");
|
|
|
|
TEST_RESULT_UINT(cipherType(strNew("aes-256-cbc")), cipherTypeAes256Cbc, "aes-256-cbc type");
|
|
|
|
|
|
|
|
TEST_ERROR(cipherTypeName((CipherType)2), AssertError, "invalid cipher type 2");
|
|
|
|
TEST_RESULT_STR(strPtr(cipherTypeName(cipherTypeNone)), "none", "none name");
|
|
|
|
TEST_RESULT_STR(strPtr(cipherTypeName(cipherTypeAes256Cbc)), "aes-256-cbc", "aes-256-cbc name");
|
|
|
|
|
|
|
|
// Test if the buffer was overrun
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
size_t bufferSize = 256;
|
|
|
|
unsigned char *buffer = memNew(bufferSize + 1);
|
|
|
|
|
|
|
|
cryptoRandomBytes(buffer, bufferSize);
|
|
|
|
TEST_RESULT_BOOL(buffer[bufferSize] == 0, true, "check that buffer did not overrun (though random byte could be 0)");
|
|
|
|
|
|
|
|
// Count bytes that are not zero (there shouldn't be all zeroes)
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
int nonZeroTotal = 0;
|
|
|
|
|
|
|
|
for (unsigned int charIdx = 0; charIdx < bufferSize; charIdx++)
|
2019-05-11 14:51:51 -04:00
|
|
|
if (buffer[charIdx] != 0) // {uncoverable_branch - ok if there are no zeros}
|
2019-03-10 13:27:30 +02:00
|
|
|
nonZeroTotal++;
|
|
|
|
|
|
|
|
TEST_RESULT_INT_NE(nonZeroTotal, 0, "check that there are non-zero values in the buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
|
|
|
if (testBegin("CipherBlock"))
|
2017-11-03 13:57:58 -04:00
|
|
|
{
|
|
|
|
// Cipher and digest errors
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ERROR(
|
2019-05-02 17:52:24 -04:00
|
|
|
cipherBlockNew(
|
|
|
|
cipherModeEncrypt, cipherTypeNone, BUFSTRZ(TEST_PASS), NULL), AssertError,
|
|
|
|
"unable to load cipher 'none'");
|
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
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
CipherBlock *cipherBlock = (CipherBlock *)ioFilterDriver(
|
|
|
|
cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, BUFSTRZ(TEST_PASS), NULL));
|
|
|
|
TEST_RESULT_STR(memContextName(cipherBlock->memContext), "CipherBlock", "mem context name is valid");
|
2017-11-03 13:57:58 -04:00
|
|
|
TEST_RESULT_INT(cipherBlock->mode, cipherModeEncrypt, "mode is valid");
|
2019-05-02 17:52:24 -04:00
|
|
|
TEST_RESULT_INT(cipherBlock->passSize, strlen(TEST_PASS), "passphrase size is valid");
|
|
|
|
TEST_RESULT_BOOL(memcmp(cipherBlock->pass, TEST_PASS, strlen(TEST_PASS)) == 0, true, "passphrase is valid");
|
2017-11-03 13:57:58 -04:00
|
|
|
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
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Encrypt
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
Buffer *encryptBuffer = bufNew(TEST_BUFFER_SIZE);
|
|
|
|
|
2019-05-02 17:52:24 -04:00
|
|
|
IoFilter *blockEncryptFilter = cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
CipherBlock *blockEncrypt = (CipherBlock *)ioFilterDriver(blockEncryptFilter);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-11-06 22:55:34 -05:00
|
|
|
TEST_RESULT_INT(
|
2019-05-02 17:52:24 -04:00
|
|
|
cipherBlockProcessSize(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(
|
2019-05-02 17:52:24 -04:00
|
|
|
cipherBlockProcessSize(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
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockEncryptFilter);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
|
|
|
// Decrypt in one pass
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-11-28 12:42:36 -05:00
|
|
|
Buffer *decryptBuffer = bufNew(TEST_BUFFER_SIZE);
|
|
|
|
|
2019-05-02 17:52:24 -04:00
|
|
|
IoFilter *blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
CipherBlock *blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
2017-11-06 22:55:34 -05:00
|
|
|
|
|
|
|
TEST_RESULT_INT(
|
2019-05-02 17:52:24 -04:00
|
|
|
cipherBlockProcessSize(blockDecrypt, bufUsed(encryptBuffer)), bufUsed(encryptBuffer) + EVP_MAX_BLOCK_LENGTH,
|
2018-11-28 12:42:36 -05:00
|
|
|
"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
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Decrypt in small chunks to test buffering
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
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
|
|
|
|
2019-05-02 12:43:09 -04:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, bufNewC(bufPtr(encryptBuffer), CIPHER_BLOCK_MAGIC_SIZE), decryptBuffer);
|
2018-11-28 12:42:36 -05:00
|
|
|
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(
|
2019-05-02 12:43:09 -04:00
|
|
|
blockDecryptFilter, bufNewC(bufPtr(encryptBuffer) + CIPHER_BLOCK_MAGIC_SIZE, PKCS5_SALT_LEN), decryptBuffer);
|
2018-11-28 12:42:36 -05:00
|
|
|
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,
|
2019-05-02 12:43:09 -04:00
|
|
|
bufNewC(bufPtr(encryptBuffer) + CIPHER_BLOCK_HEADER_SIZE, bufUsed(encryptBuffer) - CIPHER_BLOCK_HEADER_SIZE),
|
2018-11-28 12:42:36 -05:00
|
|
|
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
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Encrypt zero byte file and decrypt it
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockEncryptFilter = cipherBlockNew(cipherModeEncrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockEncrypt = (CipherBlock *)ioFilterDriver(blockEncryptFilter);
|
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
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockEncryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
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, 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
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Invalid cipher header
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2017-11-06 22:55:34 -05:00
|
|
|
TEST_ERROR(
|
2019-04-20 08:16:17 -04:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, BUFSTRDEF("1234567890123456"), decryptBuffer), CryptoError,
|
2018-11-28 12:42:36 -05:00
|
|
|
"cipher header invalid");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// Invalid encrypted data cannot be flushed
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
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
|
|
|
|
2019-04-20 08:16:17 -04:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, BUFSTRDEF(CIPHER_BLOCK_MAGIC "12345678"), decryptBuffer);
|
|
|
|
ioFilterProcessInOut(blockDecryptFilter, BUFSTRDEF("1234567890123456"), decryptBuffer);
|
2018-11-28 12:42:36 -05:00
|
|
|
|
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "unable to flush");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// File with no header should not flush
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
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
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "cipher header missing");
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2017-11-03 13:57:58 -04:00
|
|
|
// File with header only should error
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2019-05-02 17:52:24 -04:00
|
|
|
blockDecryptFilter = cipherBlockNew(cipherModeDecrypt, cipherTypeAes256Cbc, testPass, NULL);
|
|
|
|
blockDecrypt = (CipherBlock *)ioFilterDriver(blockDecryptFilter);
|
2017-11-03 13:57:58 -04:00
|
|
|
|
2018-11-28 12:42:36 -05:00
|
|
|
bufUsedZero(decryptBuffer);
|
|
|
|
|
2019-04-20 08:16:17 -04:00
|
|
|
ioFilterProcessInOut(blockDecryptFilter, BUFSTRDEF(CIPHER_BLOCK_MAGIC "12345678"), decryptBuffer);
|
2018-11-28 12:42:36 -05:00
|
|
|
TEST_ERROR(ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer), CryptoError, "unable to flush");
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2019-05-03 18:52:54 -04:00
|
|
|
ioFilterFree(blockDecryptFilter);
|
2017-11-03 13:57:58 -04:00
|
|
|
}
|
2017-12-22 18:36:36 -05:00
|
|
|
|
2019-03-10 13:27:30 +02:00
|
|
|
// *****************************************************************************************************************************
|
|
|
|
if (testBegin("CryptoHash"))
|
|
|
|
{
|
2019-05-02 17:52:24 -04:00
|
|
|
IoFilter *hash = NULL;
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
TEST_ERROR(cryptoHashNew(strNew(BOGUS_STR)), AssertError, "unable to load hash 'BOGUS'");
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA1)), "create sha1 hash");
|
2019-05-02 17:52:24 -04:00
|
|
|
TEST_RESULT_VOID(ioFilterFree(hash), " free hash");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA1)), "create sha1 hash");
|
2019-05-02 17:52:24 -04:00
|
|
|
TEST_RESULT_STR(
|
|
|
|
strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
|
|
" check empty hash");
|
|
|
|
TEST_RESULT_STR(
|
|
|
|
strPtr(bufHex(cryptoHash((CryptoHash *)ioFilterDriver(hash)))), "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
|
|
" check empty hash again");
|
|
|
|
TEST_RESULT_VOID(ioFilterFree(hash), " free hash");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA1)), "create sha1 hash");
|
2019-05-02 17:52:24 -04:00
|
|
|
TEST_RESULT_VOID(ioFilterProcessIn(hash, BUFSTRZ("1")), " add 1");
|
|
|
|
TEST_RESULT_VOID(ioFilterProcessIn(hash, BUFSTR(strNew("2"))), " add 2");
|
|
|
|
TEST_RESULT_VOID(ioFilterProcessIn(hash, BUFSTRDEF("3")), " add 3");
|
|
|
|
TEST_RESULT_VOID(ioFilterProcessIn(hash, BUFSTRDEF("4")), " add 4");
|
|
|
|
TEST_RESULT_VOID(ioFilterProcessIn(hash, BUFSTRDEF("5")), " add 5");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
TEST_RESULT_STR(
|
2019-05-02 17:52:24 -04:00
|
|
|
strPtr(varStr(ioFilterResult(hash))), "8cb2237d0679ca88db6464eac60da96345513964", " check small hash");
|
|
|
|
TEST_RESULT_VOID(ioFilterFree(hash), " free hash");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_MD5)), "create md5 hash");
|
2019-05-02 17:52:24 -04:00
|
|
|
TEST_RESULT_STR(strPtr(varStr(ioFilterResult(hash))), "d41d8cd98f00b204e9800998ecf8427e", " check empty hash");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_ASSIGN(hash, cryptoHashNew(strNew(HASH_TYPE_SHA256)), "create sha256 hash");
|
|
|
|
TEST_RESULT_STR(
|
2019-05-02 17:52:24 -04:00
|
|
|
strPtr(varStr(ioFilterResult(hash))), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
2019-03-10 13:27:30 +02:00
|
|
|
" check empty hash");
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_RESULT_STR(
|
2019-04-20 08:16:17 -04:00
|
|
|
strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), BUFSTRDEF("12345")))), "8cb2237d0679ca88db6464eac60da96345513964",
|
2019-03-10 13:27:30 +02:00
|
|
|
" check small hash");
|
|
|
|
TEST_RESULT_STR(
|
2019-05-02 17:52:24 -04:00
|
|
|
strPtr(bufHex(cryptoHashOne(strNew(HASH_TYPE_SHA1), BUFSTRDEF("")))), "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
|
|
" check empty hash");
|
2019-03-10 13:27:30 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TEST_RESULT_STR(
|
|
|
|
strPtr(
|
|
|
|
bufHex(
|
|
|
|
cryptoHmacOne(
|
|
|
|
strNew(HASH_TYPE_SHA256),
|
2019-04-20 08:16:17 -04:00
|
|
|
BUFSTRDEF("AWS4wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"),
|
|
|
|
BUFSTRDEF("20170412")))),
|
2019-03-10 13:27:30 +02:00
|
|
|
"8b05c497afe9e1f42c8ada4cb88392e118649db1e5c98f0f0fb0a158bdd2dd76",
|
|
|
|
" check hmac");
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:57:32 -04:00
|
|
|
FUNCTION_HARNESS_RESULT_VOID();
|
2017-11-03 13:57:58 -04:00
|
|
|
}
|