• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21780928802

07 Feb 2026 01:36PM UTC coverage: 90.068% (+0.003%) from 90.065%
21780928802

Pull #5295

github

web-flow
Merge cbabeb61a into ebf8f0044
Pull Request #5295: Reduce header dependencies in tests and cli

102234 of 113508 relevant lines covered (90.07%)

11464546.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.13
/src/tests/test_block.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_BLOCK_CIPHER)
10
   #include <botan/block_cipher.h>
11
   #include <botan/exceptn.h>
12
   #include <botan/mem_ops.h>
13
   #include <botan/rng.h>
14
   #include <botan/internal/fmt.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_BLOCK_CIPHER)
20

21
class Block_Cipher_Tests final : public Text_Based_Test {
×
22
   public:
23
      Block_Cipher_Tests() : Text_Based_Test("block", "Key,In,Out", "Tweak,Iterations") {}
2✔
24

25
      std::vector<std::string> possible_providers(const std::string& algo) override {
17,513✔
26
         return provider_filter(Botan::BlockCipher::providers(algo));
17,513✔
27
      }
28

29
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
17,513✔
30
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
17,513✔
31
         const std::vector<uint8_t> input = vars.get_req_bin("In");
17,513✔
32
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
17,513✔
33
         const std::vector<uint8_t> tweak = vars.get_opt_bin("Tweak");
17,513✔
34
         const size_t iterations = vars.get_opt_sz("Iterations", 1);
17,513✔
35

36
         Test::Result result(algo);
35,026✔
37

38
         if(iterations > 1 && run_long_tests() == false) {
17,513✔
39
            return result;
40
         }
41

42
         const std::vector<std::string> providers = possible_providers(algo);
17,513✔
43

44
         if(providers.empty()) {
17,513✔
45
            result.note_missing("block cipher " + algo);
×
46
            return result;
×
47
         }
48

49
         for(const auto& provider_ask : providers) {
35,026✔
50
            auto cipher = Botan::BlockCipher::create(algo, provider_ask);
17,513✔
51

52
            if(!cipher) {
17,513✔
53
               result.test_failure(Botan::fmt("Cipher {} supported by {} but not found", algo, provider_ask));
×
54
               continue;
×
55
            }
56

57
            const std::string provider(cipher->provider());
17,513✔
58
            result.test_is_nonempty("provider", provider);
17,513✔
59
            result.test_eq(provider, cipher->name(), algo);
17,513✔
60
            result.test_gte(provider, cipher->parallelism(), 1);
17,513✔
61
            result.test_gte(provider, cipher->block_size(), 8);
17,513✔
62
            result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
35,026✔
63

64
            result.test_eq("no key set", cipher->has_keying_material(), false);
17,513✔
65

66
            // Test that trying to encrypt or decrypt with no key set throws Botan::Invalid_State
67
            try {
17,513✔
68
               std::vector<uint8_t> block(cipher->block_size());
17,513✔
69
               cipher->encrypt(block);
17,513✔
70
               result.test_failure("Was able to encrypt without a key being set");
×
71
            } catch(Botan::Invalid_State&) {
35,026✔
72
               result.test_success("Trying to encrypt with no key set fails");
17,513✔
73
            }
17,513✔
74

75
            try {
17,513✔
76
               std::vector<uint8_t> block(cipher->block_size());
17,513✔
77
               cipher->decrypt(block);
17,513✔
78
               result.test_failure("Was able to decrypt without a key being set");
×
79
            } catch(Botan::Invalid_State&) {
35,026✔
80
               result.test_success("Trying to encrypt with no key set fails");
17,513✔
81
            }
17,513✔
82

83
            // Test to make sure clear() resets what we need it to
84
            cipher->set_key(this->rng().random_vec(cipher->key_spec().maximum_keylength()));
17,513✔
85
            Botan::secure_vector<uint8_t> garbage = this->rng().random_vec(cipher->block_size());
17,513✔
86
            cipher->encrypt(garbage);
17,513✔
87
            cipher->clear();
17,513✔
88

89
            /*
90
            * Different providers may have additional restrictions on key sizes.
91
            * Avoid testing the cipher with a key size that it does not natively support.
92
            */
93
            if(!cipher->valid_keylength(key.size())) {
17,513✔
94
               result.test_note("Skipping test with provider " + provider + " as it does not support key length " +
×
95
                                std::to_string(key.size()));
×
96
               continue;
×
97
            }
98

99
            cipher->set_key(key);
17,513✔
100
            result.test_eq("key set", cipher->has_keying_material(), true);
17,513✔
101

102
            if(!tweak.empty()) {
17,513✔
103
               Botan::Tweakable_Block_Cipher* tbc = dynamic_cast<Botan::Tweakable_Block_Cipher*>(cipher.get());
2✔
104
               if(tbc == nullptr) {
2✔
105
                  result.test_failure("Tweak set in test data but cipher is not a Tweakable_Block_Cipher");
×
106
               } else {
107
                  tbc->set_tweak(tweak.data(), tweak.size());
2✔
108
               }
109
            }
110

111
            // Test that clone works and does not affect parent object
112
            auto clone = cipher->new_object();
17,513✔
113
            result.confirm("Clone has different pointer", cipher.get() != clone.get());
35,026✔
114
            result.test_eq("Clone has same name", cipher->name(), clone->name());
35,026✔
115
            clone->set_key(this->rng().random_vec(cipher->maximum_keylength()));
17,513✔
116

117
            // have called set_key on clone: process input values
118
            std::vector<uint8_t> buf = input;
17,513✔
119

120
            for(size_t i = 0; i != iterations; ++i) {
1,035,025✔
121
               cipher->encrypt(buf);
2,035,024✔
122
            }
123

124
            result.test_eq(provider, "encrypt", buf, expected);
35,026✔
125

126
            // always decrypt expected ciphertext vs what we produced above
127
            buf = expected;
17,513✔
128

129
            for(size_t i = 0; i != iterations; ++i) {
1,035,025✔
130
               cipher->decrypt(buf);
2,035,024✔
131
            }
132

133
            result.test_eq(provider, "decrypt", buf, input);
35,026✔
134

135
            // Now test misaligned buffers
136
            const size_t blocks = input.size() / cipher->block_size();
17,513✔
137
            buf.resize(input.size() + 1);
17,513✔
138
            Botan::copy_mem(buf.data() + 1, input.data(), input.size());
17,513✔
139

140
            for(size_t i = 0; i != iterations; ++i) {
1,035,025✔
141
               cipher->encrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,017,512✔
142
            }
143

144
            result.test_eq(provider.c_str(),
17,513✔
145
                           "encrypt misaligned",
146
                           buf.data() + 1,
17,513✔
147
                           buf.size() - 1,
17,513✔
148
                           expected.data(),
149
                           expected.size());
150

151
            // always decrypt expected ciphertext vs what we produced above
152
            Botan::copy_mem(buf.data() + 1, expected.data(), expected.size());
17,513✔
153

154
            for(size_t i = 0; i != iterations; ++i) {
1,035,025✔
155
               cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,017,512✔
156
            }
157

158
            result.test_eq(
17,513✔
159
               provider.c_str(), "decrypt misaligned", buf.data() + 1, buf.size() - 1, input.data(), input.size());
17,513✔
160

161
            result.test_eq("key set", cipher->has_keying_material(), true);
17,513✔
162
            cipher->clear();
17,513✔
163
            result.test_eq("key set", cipher->has_keying_material(), false);
17,513✔
164

165
            try {
17,513✔
166
               std::vector<uint8_t> block(cipher->block_size());
17,513✔
167
               cipher->encrypt(block);
17,513✔
168
               result.test_failure("Was able to encrypt without a key being set");
×
169
            } catch(Botan::Invalid_State&) {
35,026✔
170
               result.test_success("Trying to encrypt with no key set (after clear) fails");
17,513✔
171
            }
17,513✔
172

173
            try {
17,513✔
174
               std::vector<uint8_t> block(cipher->block_size());
17,513✔
175
               cipher->decrypt(block);
17,513✔
176
               result.test_failure("Was able to decrypt without a key being set");
×
177
            } catch(Botan::Invalid_State&) {
35,026✔
178
               result.test_success("Trying to decrypt with no key set (after clear) fails");
17,513✔
179
            }
17,513✔
180
         }
70,052✔
181

182
         return result;
183
      }
17,513✔
184
};
185

186
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
187

188
#endif
189

190
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc