Added test cases that a Cipher is indeterministic
This commit is contained in:
parent
f18195757c
commit
79283b868b
@ -18,13 +18,30 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CheckEncryptThenDecryptIsIdentity(const Data &plaintext) {
|
void CheckEncryptThenDecryptIsIdentity(const Data &plaintext) {
|
||||||
Data ciphertext(Cipher::ciphertextSize(plaintext.size()));
|
Data ciphertext = Encrypt(plaintext);
|
||||||
Data decrypted(plaintext.size());
|
Data decrypted = Decrypt(ciphertext);
|
||||||
Cipher::encrypt((byte*)plaintext.data(), plaintext.size(), (byte*)ciphertext.data(), this->encKey);
|
EXPECT_EQ(plaintext.size(), decrypted.size());
|
||||||
Cipher::decrypt((byte*)ciphertext.data(), (byte*) decrypted.data(), decrypted.size(), this->encKey);
|
|
||||||
EXPECT_EQ(0, std::memcmp(plaintext.data(), decrypted.data(), plaintext.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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Data Encrypt(const Data &plaintext) {
|
||||||
|
Data ciphertext(Cipher::ciphertextSize(plaintext.size()));
|
||||||
|
Cipher::encrypt((byte*)plaintext.data(), plaintext.size(), (byte*)ciphertext.data(), this->encKey);
|
||||||
|
return ciphertext;
|
||||||
|
}
|
||||||
|
|
||||||
|
Data Decrypt(const Data &ciphertext) {
|
||||||
|
Data decrypted(Cipher::plaintextSize(ciphertext.size()));
|
||||||
|
Cipher::decrypt((byte*)ciphertext.data(), (byte*) decrypted.data(), decrypted.size(), this->encKey);
|
||||||
|
return decrypted;
|
||||||
|
}
|
||||||
|
|
||||||
Data CreateZeroes(unsigned int size) {
|
Data CreateZeroes(unsigned int size) {
|
||||||
Data zeroes(size);
|
Data zeroes(size);
|
||||||
zeroes.FillWithZeroes();
|
zeroes.FillWithZeroes();
|
||||||
@ -64,69 +81,34 @@ TYPED_TEST_P(CipherTest, Size_1048576) {
|
|||||||
EXPECT_EQ(1048576, TypeParam::plaintextSize(TypeParam::ciphertextSize(1048576)));
|
EXPECT_EQ(1048576, TypeParam::plaintextSize(TypeParam::ciphertextSize(1048576)));
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Empty) {
|
constexpr std::initializer_list<unsigned int> SIZES = {0, 1, 100, 1024, 5000, 1048576, 52428800};
|
||||||
Data plaintext = this->CreateZeroes(0);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes) {
|
||||||
|
for (auto size: SIZES) {
|
||||||
|
Data plaintext = this->CreateZeroes(size);
|
||||||
|
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_1) {
|
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data) {
|
||||||
Data plaintext = this->CreateZeroes(1);
|
for (auto size: SIZES) {
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
Data plaintext = this->CreateData(size);
|
||||||
|
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_1) {
|
TYPED_TEST_P(CipherTest, EncryptIsIndeterministic_Zeroes) {
|
||||||
Data plaintext = this->CreateData(1);
|
for (auto size: SIZES) {
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
Data plaintext = this->CreateZeroes(size);
|
||||||
|
this->CheckEncryptIsIndeterministic(plaintext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_100) {
|
TYPED_TEST_P(CipherTest, EncryptIsIndeterministic_Data) {
|
||||||
Data plaintext = this->CreateZeroes(100);
|
for (auto size: SIZES) {
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
Data plaintext = this->CreateData(size);
|
||||||
}
|
this->CheckEncryptIsIndeterministic(plaintext);
|
||||||
|
}
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_100) {
|
|
||||||
Data plaintext = this->CreateData(100);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_1024) {
|
|
||||||
Data plaintext = this->CreateZeroes(1024);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_1024) {
|
|
||||||
Data plaintext = this->CreateData(1024);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_5000) {
|
|
||||||
Data plaintext = this->CreateZeroes(5000);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_5000) {
|
|
||||||
Data plaintext = this->CreateData(5000);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_1MB) {
|
|
||||||
Data plaintext = this->CreateZeroes(1048576);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_1MB) {
|
|
||||||
Data plaintext = this->CreateData(1048576);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Zeroes_50MB) {
|
|
||||||
Data plaintext = this->CreateZeroes(52428800);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
|
||||||
|
|
||||||
TYPED_TEST_P(CipherTest, EncryptThenDecrypt_Data_50MB) {
|
|
||||||
Data plaintext = this->CreateData(52428800);
|
|
||||||
this->CheckEncryptThenDecryptIsIdentity(plaintext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
REGISTER_TYPED_TEST_CASE_P(CipherTest,
|
REGISTER_TYPED_TEST_CASE_P(CipherTest,
|
||||||
@ -135,19 +117,10 @@ REGISTER_TYPED_TEST_CASE_P(CipherTest,
|
|||||||
Size_1024,
|
Size_1024,
|
||||||
Size_4096,
|
Size_4096,
|
||||||
Size_1048576,
|
Size_1048576,
|
||||||
EncryptThenDecrypt_Empty,
|
EncryptThenDecrypt_Zeroes,
|
||||||
EncryptThenDecrypt_Zeroes_1,
|
EncryptThenDecrypt_Data,
|
||||||
EncryptThenDecrypt_Data_1,
|
EncryptIsIndeterministic_Zeroes,
|
||||||
EncryptThenDecrypt_Zeroes_100,
|
EncryptIsIndeterministic_Data
|
||||||
EncryptThenDecrypt_Data_100,
|
|
||||||
EncryptThenDecrypt_Zeroes_1024,
|
|
||||||
EncryptThenDecrypt_Data_1024,
|
|
||||||
EncryptThenDecrypt_Zeroes_5000,
|
|
||||||
EncryptThenDecrypt_Data_5000,
|
|
||||||
EncryptThenDecrypt_Zeroes_1MB,
|
|
||||||
EncryptThenDecrypt_Data_1MB,
|
|
||||||
EncryptThenDecrypt_Zeroes_50MB,
|
|
||||||
EncryptThenDecrypt_Data_50MB
|
|
||||||
);
|
);
|
||||||
|
|
||||||
//TODO For authenticated ciphers, we need test cases checking that authentication fails on manipulations
|
//TODO For authenticated ciphers, we need test cases checking that authentication fails on manipulations
|
||||||
|
Loading…
Reference in New Issue
Block a user