2015-04-24 18:58:42 +02:00
|
|
|
#include <google/gtest/gtest.h>
|
|
|
|
#include "../../../implementations/encrypted/ciphers/AES256_CFB.h"
|
2015-04-24 21:30:54 +02:00
|
|
|
#include "../../../implementations/encrypted/ciphers/Cipher.h"
|
2015-04-24 18:58:42 +02:00
|
|
|
|
|
|
|
#include "../../testutils/DataBlockFixture.h"
|
|
|
|
#include "../../../utils/Data.h"
|
|
|
|
|
|
|
|
using namespace blockstore::encrypted;
|
|
|
|
using blockstore::Data;
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
class CipherTest: public ::testing::Test {
|
|
|
|
public:
|
2015-04-24 21:30:54 +02:00
|
|
|
BOOST_CONCEPT_ASSERT((CipherConcept<Cipher>));
|
2015-04-24 18:58:42 +02:00
|
|
|
typename Cipher::EncryptionKey encKey = createRandomKey();
|
|
|
|
|
|
|
|
static typename Cipher::EncryptionKey createRandomKey(int seed = 0) {
|
|
|
|
DataBlockFixture data(Cipher::EncryptionKey::BINARY_LENGTH, seed);
|
|
|
|
return Cipher::EncryptionKey::FromBinary(data.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckEncryptThenDecryptIsIdentity(const Data &plaintext) {
|
2015-04-24 19:13:56 +02:00
|
|
|
Data ciphertext = Encrypt(plaintext);
|
|
|
|
Data decrypted = Decrypt(ciphertext);
|
|
|
|
EXPECT_EQ(plaintext.size(), decrypted.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(plaintext.data(), decrypted.data(), plaintext.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckEncryptIsIndeterministic(const Data &plaintext) {
|
|
|
|
Data ciphertext = Encrypt(plaintext);
|
|
|
|
Data ciphertext2 = Encrypt(plaintext);
|
|
|
|
EXPECT_NE(0, std::memcmp(ciphertext.data(), ciphertext2.data(), ciphertext.size()));
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:08:36 +02:00
|
|
|
void CheckEncryptedSize(const Data &plaintext) {
|
|
|
|
Data ciphertext = Encrypt(plaintext);
|
|
|
|
EXPECT_EQ(Cipher::ciphertextSize(plaintext.size()), ciphertext.size());
|
|
|
|
}
|
|
|
|
|
2015-04-24 19:13:56 +02:00
|
|
|
Data Encrypt(const Data &plaintext) {
|
2015-04-24 21:08:36 +02:00
|
|
|
return Cipher::encrypt((byte*)plaintext.data(), plaintext.size(), this->encKey);
|
2015-04-24 19:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Data Decrypt(const Data &ciphertext) {
|
2015-04-24 21:08:36 +02:00
|
|
|
return Cipher::decrypt((byte*)ciphertext.data(), ciphertext.size(), this->encKey).value();
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Data CreateZeroes(unsigned int size) {
|
|
|
|
Data zeroes(size);
|
|
|
|
zeroes.FillWithZeroes();
|
|
|
|
return zeroes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data CreateData(unsigned int size, unsigned int seed = 0) {
|
|
|
|
DataBlockFixture data(size, seed);
|
|
|
|
Data result(size);
|
|
|
|
std::memcpy(result.data(), data.data(), size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TYPED_TEST_CASE_P(CipherTest);
|
|
|
|
|
2015-04-24 21:33:25 +02:00
|
|
|
constexpr std::initializer_list<unsigned int> SIZES = {0, 1, 100, 1024, 5000, 1048576, 20971520};
|
2015-04-24 18:58:42 +02:00
|
|
|
|
2015-04-24 21:33:25 +02:00
|
|
|
TYPED_TEST_P(CipherTest, Size) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
EXPECT_EQ(size, TypeParam::ciphertextSize(TypeParam::plaintextSize(size)));
|
|
|
|
EXPECT_EQ(size, TypeParam::plaintextSize(TypeParam::ciphertextSize(size)));
|
|
|
|
}
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:13:56 +02:00
|
|
|
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
Data plaintext = this->CreateZeroes(size);
|
|
|
|
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
|
|
}
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:13:56 +02:00
|
|
|
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
Data plaintext = this->CreateData(size);
|
|
|
|
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
|
|
}
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:13:56 +02:00
|
|
|
TYPED_TEST_P(CipherTest, EncryptIsIndeterministic_Zeroes) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
Data plaintext = this->CreateZeroes(size);
|
|
|
|
this->CheckEncryptIsIndeterministic(plaintext);
|
|
|
|
}
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:13:56 +02:00
|
|
|
TYPED_TEST_P(CipherTest, EncryptIsIndeterministic_Data) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
Data plaintext = this->CreateData(size);
|
|
|
|
this->CheckEncryptIsIndeterministic(plaintext);
|
|
|
|
}
|
2015-04-24 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 21:08:36 +02:00
|
|
|
TYPED_TEST_P(CipherTest, EncryptedSize) {
|
|
|
|
for (auto size: SIZES) {
|
|
|
|
Data plaintext = this->CreateData(size);
|
|
|
|
this->CheckEncryptedSize(plaintext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:58:42 +02:00
|
|
|
REGISTER_TYPED_TEST_CASE_P(CipherTest,
|
2015-04-24 21:33:25 +02:00
|
|
|
Size,
|
2015-04-24 19:13:56 +02:00
|
|
|
EncryptThenDecrypt_Zeroes,
|
|
|
|
EncryptThenDecrypt_Data,
|
|
|
|
EncryptIsIndeterministic_Zeroes,
|
2015-04-24 21:08:36 +02:00
|
|
|
EncryptIsIndeterministic_Data,
|
|
|
|
EncryptedSize
|
2015-04-24 18:58:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//TODO For authenticated ciphers, we need test cases checking that authentication fails on manipulations
|
|
|
|
|
|
|
|
INSTANTIATE_TYPED_TEST_CASE_P(AES256_CFB, CipherTest, AES256_CFB);
|