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,10 +24,10 @@ 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(
@ -41,8 +40,7 @@ TEST_CASE(__FILE__"/SingleBlock_NoPadding", "[Encryption/Decryption consistency]
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,10 +63,10 @@ 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(
@ -81,8 +79,7 @@ TEST_CASE(__FILE__"/SingleBlock_Padding", "[Encryption/Decryption consistency]")
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,10 +117,10 @@ 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(
@ -136,8 +133,7 @@ TEST_CASE(__FILE__"MultiBlock_NoPadding/", "[Encryption/Decryption consistency]"
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,10 +204,10 @@ 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(

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"
@ -11,16 +11,16 @@ TEST_CASE(__FILE__"/Encrypting and decrypting strings works", "[Wrapper]") {
// Setup
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 decrypted;
// Encryption
ciphertext = GCryptWrapper::EncryptString(plaintext, password);
ciphertext = GWrapper::EncryptString(plaintext, key);
// Decryption
decrypted = GCryptWrapper::DecryptString(ciphertext, password);
decrypted = GWrapper::DecryptString(ciphertext, key);
// Assertion
REQUIRE(plaintext == decrypted);
@ -36,14 +36,14 @@ TEST_CASE(__FILE__"/Encrypting and decrypting files works", "[Wrapper]") {
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 Key key = Key::FromPassword("Der Affe will Zucker");
// Encryption
GCryptWrapper::EncryptFile(filename_plain, filename_encrypted, password);
GWrapper::EncryptFile(filename_plain, filename_encrypted, key);
// Decryption
GCryptWrapper::DecryptFile(filename_encrypted, filename_decrypted, password);
GWrapper::DecryptFile(filename_encrypted, filename_decrypted, key);
// Read in both the base, and the decrypted file
const Flexblock plainfile = ReadFileToBits(filename_plain);

View File

@ -57,7 +57,7 @@ TEST_CASE(__FILE__"/Password to key transformation collision resistance", "[Key
// 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;
constexpr std::size_t NUM_RUN_TESTS = 10;
std::unordered_map<std::bitset<BLOCK_SIZE>, std::string> keys; // <key, password>
@ -69,7 +69,7 @@ TEST_CASE(__FILE__"/Password to key transformation collision resistance", "[Key
const std::string password = Base10_2_X(i, charset, 0);
// 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
if (keys.find(newKey) != keys.cend()) {