aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_block.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-10-26 20:31:30 -0400
committerJack Lloyd <[email protected]>2017-10-26 22:26:15 -0400
commite6d45052efedfe49e99adb6318aaf56e0a9e8d7b (patch)
treec6c3ccd3cff3d04285940bf1d518c809e0653947 /src/tests/test_block.cpp
parent315b002ecf00f6b6bb0f0d5200d1f39a83527e8f (diff)
Add checks that keyed algorithms are actually keyed before use
Previously calling update or encrypt without calling set_key first would result in invalid outputs or else crashing.
Diffstat (limited to 'src/tests/test_block.cpp')
-rw-r--r--src/tests/test_block.cpp47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/tests/test_block.cpp b/src/tests/test_block.cpp
index 535556a90..02115b66a 100644
--- a/src/tests/test_block.cpp
+++ b/src/tests/test_block.cpp
@@ -53,8 +53,31 @@ class Block_Cipher_Tests final : public Text_Based_Test
result.test_gte(provider, cipher->block_size(), 8);
result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
+ // Test that trying to encrypt or decrypt with now key set throws Botan::Invalid_State
+ try
+ {
+ std::vector<uint8_t> block(cipher->block_size());
+ cipher->encrypt(block);
+ result.test_failure("Was able to encrypt without a key being set");
+ }
+ catch(Botan::Invalid_State&)
+ {
+ result.test_success("Trying to encrypt with no key set fails");
+ }
+
+ try
+ {
+ std::vector<uint8_t> block(cipher->block_size());
+ cipher->decrypt(block);
+ result.test_failure("Was able to decrypt without a key being set");
+ }
+ catch(Botan::Invalid_State&)
+ {
+ result.test_success("Trying to encrypt with no key set fails");
+ }
+
// Test to make sure clear() resets what we need it to
- cipher->set_key(Test::rng().random_vec(cipher->key_spec().minimum_keylength()));
+ cipher->set_key(Test::rng().random_vec(cipher->key_spec().maximum_keylength()));
Botan::secure_vector<uint8_t> garbage = Test::rng().random_vec(cipher->block_size());
cipher->encrypt(garbage);
cipher->clear();
@@ -89,6 +112,28 @@ class Block_Cipher_Tests final : public Text_Based_Test
result.test_eq(provider, "decrypt", buf, input);
+ try
+ {
+ std::vector<uint8_t> block(cipher->block_size());
+ cipher->encrypt(block);
+ result.test_failure("Was able to encrypt without a key being set");
+ }
+ catch(Botan::Invalid_State&)
+ {
+ result.test_success("Trying to encrypt with no key set (after clear) fails");
+ }
+
+ try
+ {
+ std::vector<uint8_t> block(cipher->block_size());
+ cipher->decrypt(block);
+ result.test_failure("Was able to decrypt without a key being set");
+ }
+ catch(Botan::Invalid_State&)
+ {
+ result.test_success("Trying to decrypt with no key set (after clear) fails");
+ }
+
}
return result;