Adjusted tests

This commit is contained in:
Leonetienne 2022-05-22 14:26:23 +02:00
parent 967eba2f03
commit fe6ec11672
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
3 changed files with 80 additions and 84 deletions

View File

@ -1,4 +1,4 @@
#include <GCrypt/Cipher.h>
#include <GCrypt/GWrapper.h>
#include <GCrypt/Util.h>
#include "Catch2.h"
@ -10,8 +10,7 @@ using namespace Leonetienne::GCrypt;
TEST_CASE(__FILE__"/SingleBlock_NoPadding", "[Encryption/Decryption consistency]") {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey("1234");
const Cipher cipher(key, Cipher::CIPHER_DIRECTION::Encryption);
const Key key = Key::FromPassword("1234");
// Recode the ascii-string to bits
const Flexblock cleartext_bits =
@ -25,24 +24,23 @@ TEST_CASE(__FILE__"/SingleBlock_NoPadding", "[Encryption/Decryption consistency]
"0000011110010110111010110110111110011110011010001100100111110101";
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
const Flexblock ciphertext_bits = GWrapper::CipherFlexblock(cleartext_bits, key, GCipher::DIRECTION::ENCIPHER);
// Decipher it again
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
const Flexblock decryptedBits = GWrapper::CipherFlexblock(ciphertext_bits, key, GCipher::DIRECTION::DECIPHER);
// Assert that the decrypted text equals the plaintext
REQUIRE(
cleartext_bits ==
decryptedBits
);
cleartext_bits ==
decryptedBits
);
}
// Tests that encrypting a message of less than BLOCK_SIZE yields the exact message plus zero-padding back
TEST_CASE(__FILE__"/SingleBlock_Padding", "[Encryption/Decryption consistency]") {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey("1234");
const Cipher cipher(key);
const Key key = Key::FromPassword("1234");
// Recode the ascii-string to bits
const Flexblock cleartext_bits =
@ -65,24 +63,23 @@ TEST_CASE(__FILE__"/SingleBlock_Padding", "[Encryption/Decryption consistency]")
"0000000000000000000000000000000000000000000000000000000000000000";
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
const Flexblock ciphertext_bits = GWrapper::CipherFlexblock(cleartext_bits, key, GCipher::DIRECTION::ENCIPHER);
// Decipher it again
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
const Flexblock decryptedBits = GWrapper::CipherFlexblock(ciphertext_bits, key, GCipher::DIRECTION::DECIPHER);
// Assert that the decrypted text equals the plaintext
REQUIRE(
cleartext_bits_EXPECTED_RESULT ==
decryptedBits
);
cleartext_bits_EXPECTED_RESULT ==
decryptedBits
);
}
// Tests that a decrypted ciphertext equals its plaintrext version, using a cleartext that requires A LOT of blocks
TEST_CASE(__FILE__"MultiBlock_NoPadding/", "[Encryption/Decryption consistency]") {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey("1234");
const Cipher cipher(key);
const Key key = Key::FromPassword("1234");
// Recode the ascii-string to bits
const Flexblock cleartext_bits =
@ -120,24 +117,23 @@ TEST_CASE(__FILE__"MultiBlock_NoPadding/", "[Encryption/Decryption consistency]"
"0101110011001101010110010100011001110110000110010001100110011111";
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
const Flexblock ciphertext_bits = GWrapper::CipherFlexblock(cleartext_bits, key, GCipher::DIRECTION::ENCIPHER);
// Decipher it again
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
const Flexblock decryptedBits = GWrapper::CipherFlexblock(ciphertext_bits, key, GCipher::DIRECTION::DECIPHER);
// Assert that the decrypted text equals the plaintext
REQUIRE(
cleartext_bits ==
decryptedBits
);
cleartext_bits ==
decryptedBits
);
}
// Tests that a decrypted ciphertext equals its plaintrext version, using a cleartext that requires A LOT of blocks
TEST_CASE(__FILE__"MultiBlock_Padding/", "[Encryption/Decryption consistency]") {
// Instanciate our cipher and supply a key
const Block key = PasswordToKey("1234");
const Cipher cipher(key);
const Key key = Key::FromPassword("1234");
// Recode the ascii-string to bits
const Flexblock cleartext_bits =
@ -208,15 +204,15 @@ TEST_CASE(__FILE__"MultiBlock_Padding/", "[Encryption/Decryption consistency]")
"0000000000000000000000000000000000000000000000000000000000000000";
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
const Flexblock ciphertext_bits = GWrapper::CipherFlexblock(cleartext_bits, key, GCipher::DIRECTION::ENCIPHER);
// Decipher it again
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
const Flexblock decryptedBits = GWrapper::CipherFlexblock(ciphertext_bits, key, GCipher::DIRECTION::DECIPHER);
// Assert that the decrypted text equals the plaintext
REQUIRE(
cleartext_bits_EXPECTED_RESULT ==
decryptedBits
);
cleartext_bits_EXPECTED_RESULT ==
decryptedBits
);
}

View File

@ -1,4 +1,4 @@
#include <GCrypt/GCryptWrapper.h>
#include <GCrypt/GWrapper.h>
#include <GCrypt/Flexblock.h>
#include <GCrypt/Util.h>
#include "Catch2.h"
@ -9,50 +9,50 @@ using namespace Leonetienne::GCrypt;
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
TEST_CASE(__FILE__"/Encrypting and decrypting strings works", "[Wrapper]") {
// Setup
const std::string plaintext = "Hello, World!";
const std::string password = "Der Affe will Zucker";
// Setup
const std::string plaintext = "Hello, World!";
const Key key = Key::FromPassword("Der Affe will Zucker");
std::string ciphertext;
std::string decrypted;
std::string ciphertext;
std::string decrypted;
// Encryption
ciphertext = GCryptWrapper::EncryptString(plaintext, password);
// Encryption
ciphertext = GWrapper::EncryptString(plaintext, key);
// Decryption
decrypted = GCryptWrapper::DecryptString(ciphertext, password);
// Decryption
decrypted = GWrapper::DecryptString(ciphertext, key);
// Assertion
REQUIRE(plaintext == decrypted);
// Assertion
REQUIRE(plaintext == decrypted);
}
// Tests that encrypting and decrypting files using the wrapper works.
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
TEST_CASE(__FILE__"/Encrypting and decrypting files works", "[Wrapper]") {
// Setup
const std::string testfile_dir = "testAssets/";
// Setup
const std::string testfile_dir = "testAssets/";
const std::string filename_plain = testfile_dir + "testfile.png";
const std::string filename_encrypted = testfile_dir + "testfile.png.crypt";
const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png";
const std::string password = "Der Affe will Zucker";
const std::string filename_plain = testfile_dir + "testfile.png";
const std::string filename_encrypted = testfile_dir + "testfile.png.crypt";
const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png";
const Key key = Key::FromPassword("Der Affe will Zucker");
// Encryption
GCryptWrapper::EncryptFile(filename_plain, filename_encrypted, password);
// Encryption
GWrapper::EncryptFile(filename_plain, filename_encrypted, key);
// Decryption
GCryptWrapper::DecryptFile(filename_encrypted, filename_decrypted, password);
// Decryption
GWrapper::DecryptFile(filename_encrypted, filename_decrypted, key);
// Read in both the base, and the decrypted file
const Flexblock plainfile = ReadFileToBits(filename_plain);
const Flexblock decryptfile = ReadFileToBits(filename_decrypted);
// Read in both the base, and the decrypted file
const Flexblock plainfile = ReadFileToBits(filename_plain);
const Flexblock decryptfile = ReadFileToBits(filename_decrypted);
// Assertion (If this fails, maybe check if the image is even readable by an image viewer)
REQUIRE(
PadStringToLength(plainfile, decryptfile.length(), '0', false) ==
decryptfile
// Assertion (If this fails, maybe check if the image is even readable by an image viewer)
REQUIRE(
PadStringToLength(plainfile, decryptfile.length(), '0', false) ==
decryptfile
);
}

View File

@ -55,39 +55,39 @@ inline std::string Base10_2_X(const unsigned long long int i, const std::string
// Already validated range: Password 0 - 1.000.000
TEST_CASE(__FILE__"/Password to key transformation collision resistance", "[Key extrapolation]") {
// To test resistence set this to a high number around a million.
// This will take a LONG while to execute though (about 2.5hrs on my machine), hence why it's set so low.
constexpr std::size_t NUM_RUN_TESTS = 1000;
// To test resistence set this to a high number around a million.
// This will take a LONG while to execute though (about 2.5hrs on my machine), hence why it's set so low.
constexpr std::size_t NUM_RUN_TESTS = 10;
std::unordered_map<std::bitset<BLOCK_SIZE>, std::string> keys; // <key, password>
std::unordered_map<std::bitset<BLOCK_SIZE>, std::string> keys; // <key, password>
// Try NUM_RUN_TESTS passwords
const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Try NUM_RUN_TESTS passwords
const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (std::size_t i = 0; i < NUM_RUN_TESTS; i++) {
// Get password
const std::string password = Base10_2_X(i, charset, 0);
for (std::size_t i = 0; i < NUM_RUN_TESTS; i++) {
// Get password
const std::string password = Base10_2_X(i, charset, 0);
// Generate key
const std::bitset<BLOCK_SIZE> newKey = PasswordToKey(password).Get();
// Generate key
const std::bitset<BLOCK_SIZE> newKey = Key::FromPassword(password).Get();
// Check if this block is already in our map
if (keys.find(newKey) != keys.cend()) {
std::cout << "Collision found between password \""
<< password
<< "\" and \""
<< keys[newKey]
<< "\". The key is \""
<< newKey
<< "\".";
// Check if this block is already in our map
if (keys.find(newKey) != keys.cend()) {
std::cout << "Collision found between password \""
<< password
<< "\" and \""
<< keys[newKey]
<< "\". The key is \""
<< newKey
<< "\".";
FAIL();
}
FAIL();
}
// All good? Insert it into our map
keys[newKey] = password;
}
// All good? Insert it into our map
keys[newKey] = password;
}
return;
return;
}