horcrux/test/crypto-test.cpp

114 lines
4.1 KiB
C++

#include <string>
#include "gtest/gtest.h"
#include "utils.h"
#include "crypto.h"
/* test command:
echo "ditemi perche' se la mucca fa mu il merlo non fa me" |
scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF |
xxd -i
*/
const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me";
const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF";
const std::string test1_iv = "0123456789ABCDEF";
const std::vector<unsigned char> test1_enc {
0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f,
0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d,
0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81,
0x12, 0x96, 0x50, 0xd6, 0x5c, 0xe2, 0xc1, 0x99, 0xe3, 0x38, 0x39, 0x8e,
0x55, 0xd2, 0x04, 0x73, 0x16, 0x39, 0xc7, 0x6a, 0xd3, 0x61, 0x2c, 0x22,
0x59, 0x25, 0xa6, 0x20 };
TEST(CryptoTest, encrypt1){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = plaintext.size() + 16;
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
a.encrypt(key, iv, plaintext, output);
EXPECT_EQ(test1_enc, output);
}
TEST(CryptoTest, encrypt2){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
auto output = a.encrypt(key, iv, plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
EXPECT_EQ(temp.size(), output.size());
EXPECT_EQ(temp, output);
}
TEST(CryptoTest, encrypt3){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a(key);
auto output = a.encrypt(plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
EXPECT_EQ(temp.size(), output.size());
}
TEST(CryptoTest, decrypt1){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
a.decrypt(key, iv, test1_enc, output);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, decrypt2){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
AES256_CBC a;
auto output = a.decrypt(key, input);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, decrypt3){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
AES256_CBC a(key);
auto output = a.decrypt(input);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, endToEnd){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
AES256_CBC a;
auto crypto = a.encrypt(plaintext);
EXPECT_GT(crypto.size(), plaintext.size());
auto decrypted = a.decrypt(crypto);
EXPECT_EQ(plaintext, decrypted);
}