2
* (C) 2014,2015 Jack Lloyd
4
* Botan is released under the Simplified BSD License (see license.txt)
8
#include <botan/block_cipher.h>
10
namespace Botan_Tests {
12
class Block_Cipher_Tests final : public Text_Based_Test
15
Block_Cipher_Tests() : Text_Based_Test("block", "Key,In,Out", "Iterations") {}
17
std::vector<std::string> possible_providers(const std::string& algo) override
19
return provider_filter(Botan::BlockCipher::providers(algo));
22
Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
24
const std::vector<uint8_t> key = get_req_bin(vars, "Key");
25
const std::vector<uint8_t> input = get_req_bin(vars, "In");
26
const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
27
const size_t iterations = get_opt_sz(vars, "Iterations", 1);
29
Test::Result result(algo);
31
const std::vector<std::string> providers = possible_providers(algo);
35
result.note_missing("block cipher " + algo);
39
for(auto const& provider_ask : providers)
41
std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(algo, provider_ask));
45
result.test_failure("Cipher " + algo + " supported by " + provider_ask + " but not found");
49
const std::string provider(cipher->provider());
50
result.test_is_nonempty("provider", provider);
51
result.test_eq(provider, cipher->name(), algo);
52
result.test_gte(provider, cipher->parallelism(), 1);
53
result.test_gte(provider, cipher->block_size(), 8);
54
result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
56
// Test that trying to encrypt or decrypt with now key set throws Botan::Invalid_State
59
std::vector<uint8_t> block(cipher->block_size());
60
cipher->encrypt(block);
61
result.test_failure("Was able to encrypt without a key being set");
63
catch(Botan::Invalid_State&)
65
result.test_success("Trying to encrypt with no key set fails");
70
std::vector<uint8_t> block(cipher->block_size());
71
cipher->decrypt(block);
72
result.test_failure("Was able to decrypt without a key being set");
74
catch(Botan::Invalid_State&)
76
result.test_success("Trying to encrypt with no key set fails");
79
// Test to make sure clear() resets what we need it to
80
cipher->set_key(Test::rng().random_vec(cipher->key_spec().maximum_keylength()));
81
Botan::secure_vector<uint8_t> garbage = Test::rng().random_vec(cipher->block_size());
82
cipher->encrypt(garbage);
87
// Test that clone works and does not affect parent object
88
std::unique_ptr<Botan::BlockCipher> clone(cipher->clone());
89
result.confirm("Clone has different pointer", cipher.get() != clone.get());
90
result.test_eq("Clone has same name", cipher->name(), clone->name());
91
clone->set_key(Test::rng().random_vec(cipher->maximum_keylength()));
93
// have called set_key on clone: process input values
94
std::vector<uint8_t> buf = input;
96
for(size_t i = 0; i != iterations; ++i)
101
result.test_eq(provider, "encrypt", buf, expected);
103
// always decrypt expected ciphertext vs what we produced above
106
for(size_t i = 0; i != iterations; ++i)
108
cipher->decrypt(buf);
113
result.test_eq(provider, "decrypt", buf, input);
117
std::vector<uint8_t> block(cipher->block_size());
118
cipher->encrypt(block);
119
result.test_failure("Was able to encrypt without a key being set");
121
catch(Botan::Invalid_State&)
123
result.test_success("Trying to encrypt with no key set (after clear) fails");
128
std::vector<uint8_t> block(cipher->block_size());
129
cipher->decrypt(block);
130
result.test_failure("Was able to decrypt without a key being set");
132
catch(Botan::Invalid_State&)
134
result.test_success("Trying to decrypt with no key set (after clear) fails");
144
BOTAN_REGISTER_TEST("block", Block_Cipher_Tests);