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

View File

@ -1,4 +1,4 @@
#include <GCrypt/GCryptWrapper.h> #include <GCrypt/GWrapper.h>
#include <GCrypt/Flexblock.h> #include <GCrypt/Flexblock.h>
#include <GCrypt/Util.h> #include <GCrypt/Util.h>
#include "Catch2.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. // 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]") { TEST_CASE(__FILE__"/Encrypting and decrypting strings works", "[Wrapper]") {
// Setup // Setup
const std::string plaintext = "Hello, World!"; const std::string plaintext = "Hello, World!";
const std::string password = "Der Affe will Zucker"; const Key key = Key::FromPassword("Der Affe will Zucker");
std::string ciphertext; std::string ciphertext;
std::string decrypted; std::string decrypted;
// Encryption // Encryption
ciphertext = GCryptWrapper::EncryptString(plaintext, password); ciphertext = GWrapper::EncryptString(plaintext, key);
// Decryption // Decryption
decrypted = GCryptWrapper::DecryptString(ciphertext, password); decrypted = GWrapper::DecryptString(ciphertext, key);
// Assertion // Assertion
REQUIRE(plaintext == decrypted); REQUIRE(plaintext == decrypted);
} }
// Tests that encrypting and decrypting files using the wrapper works. // 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. // 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]") { TEST_CASE(__FILE__"/Encrypting and decrypting files works", "[Wrapper]") {
// Setup // Setup
const std::string testfile_dir = "testAssets/"; const std::string testfile_dir = "testAssets/";
const std::string filename_plain = testfile_dir + "testfile.png"; const std::string filename_plain = testfile_dir + "testfile.png";
const std::string filename_encrypted = testfile_dir + "testfile.png.crypt"; const std::string filename_encrypted = testfile_dir + "testfile.png.crypt";
const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png"; const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png";
const std::string password = "Der Affe will Zucker"; const Key key = Key::FromPassword("Der Affe will Zucker");
// Encryption // Encryption
GCryptWrapper::EncryptFile(filename_plain, filename_encrypted, password); GWrapper::EncryptFile(filename_plain, filename_encrypted, key);
// Decryption // Decryption
GCryptWrapper::DecryptFile(filename_encrypted, filename_decrypted, password); GWrapper::DecryptFile(filename_encrypted, filename_decrypted, key);
// Read in both the base, and the decrypted file // Read in both the base, and the decrypted file
const Flexblock plainfile = ReadFileToBits(filename_plain); const Flexblock plainfile = ReadFileToBits(filename_plain);
const Flexblock decryptfile = ReadFileToBits(filename_decrypted); const Flexblock decryptfile = ReadFileToBits(filename_decrypted);
// Assertion (If this fails, maybe check if the image is even readable by an image viewer) // Assertion (If this fails, maybe check if the image is even readable by an image viewer)
REQUIRE( REQUIRE(
PadStringToLength(plainfile, decryptfile.length(), '0', false) == PadStringToLength(plainfile, decryptfile.length(), '0', false) ==
decryptfile 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 // Already validated range: Password 0 - 1.000.000
TEST_CASE(__FILE__"/Password to key transformation collision resistance", "[Key extrapolation]") { TEST_CASE(__FILE__"/Password to key transformation collision resistance", "[Key extrapolation]") {
// To test resistence set this to a high number around a million. // 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. // 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; 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 // Try NUM_RUN_TESTS passwords
const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (std::size_t i = 0; i < NUM_RUN_TESTS; i++) { for (std::size_t i = 0; i < NUM_RUN_TESTS; i++) {
// Get password // Get password
const std::string password = Base10_2_X(i, charset, 0); const std::string password = Base10_2_X(i, charset, 0);
// Generate key // Generate key
const std::bitset<BLOCK_SIZE> newKey = PasswordToKey(password).Get(); const std::bitset<BLOCK_SIZE> newKey = Key::FromPassword(password).Get();
// Check if this block is already in our map // Check if this block is already in our map
if (keys.find(newKey) != keys.cend()) { if (keys.find(newKey) != keys.cend()) {
std::cout << "Collision found between password \"" std::cout << "Collision found between password \""
<< password << password
<< "\" and \"" << "\" and \""
<< keys[newKey] << keys[newKey]
<< "\". The key is \"" << "\". The key is \""
<< newKey << newKey
<< "\"."; << "\".";
FAIL(); FAIL();
} }
// All good? Insert it into our map // All good? Insert it into our map
keys[newKey] = password; keys[newKey] = password;
} }
return; return;
} }