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

randombit / botan / 23111636944

15 Mar 2026 01:45PM UTC coverage: 89.732% (+0.002%) from 89.73%
23111636944

Pull #5446

github

web-flow
Merge 75b20e42b into e9952d62f
Pull Request #5446: Address (or silence) various new warnings from clang-tidy 22

104217 of 116142 relevant lines covered (89.73%)

11426748.42 hits per line

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

89.47
/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
namespace {
20

21
#if defined(BOTAN_HAS_BLOCK_CIPHER)
22

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

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

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

38
         Test::Result result(algo);
17,094✔
39

40
         if(iterations > 1 && run_long_tests() == false) {
17,094✔
41
            return result;
42
         }
43

44
         const std::vector<std::string> providers = possible_providers(algo);
17,094✔
45

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

51
         for(const auto& provider_ask : providers) {
34,188✔
52
            auto cipher = Botan::BlockCipher::create(algo, provider_ask);
17,094✔
53

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

59
            const std::string provider(cipher->provider());
17,094✔
60
            result.test_str_not_empty("provider", provider);
17,094✔
61
            result.test_str_eq(provider, cipher->name(), algo);
17,094✔
62
            result.test_sz_gte(provider, cipher->parallelism(), 1);
17,094✔
63
            result.test_sz_gte(provider, cipher->block_size(), 8);
17,094✔
64
            result.test_sz_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
34,188✔
65

66
            result.test_is_false("no key set", cipher->has_keying_material());
17,094✔
67

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

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

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

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

101
            cipher->set_key(key);
17,094✔
102
            result.test_is_true("key set", cipher->has_keying_material());
17,094✔
103

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

113
            // Test that clone works and does not affect parent object
114
            auto clone = cipher->new_object();
17,094✔
115
            result.test_is_true("Clone has different pointer", cipher.get() != clone.get());
17,094✔
116
            result.test_str_eq("Clone has same name", cipher->name(), clone->name());
17,094✔
117
            clone->set_key(this->rng().random_vec(cipher->maximum_keylength()));
17,094✔
118

119
            // have called set_key on clone: process input values
120
            std::vector<uint8_t> buf = input;
17,094✔
121

122
            for(size_t i = 0; i != iterations; ++i) {
1,034,187✔
123
               cipher->encrypt(buf);
2,034,186✔
124
            }
125

126
            result.test_bin_eq(provider + " encrypt", buf, expected);
17,094✔
127

128
            // always decrypt expected ciphertext vs what we produced above
129
            buf = expected;
17,094✔
130

131
            for(size_t i = 0; i != iterations; ++i) {
1,034,187✔
132
               cipher->decrypt(buf);
2,034,186✔
133
            }
134

135
            result.test_bin_eq(provider + " decrypt", buf, input);
17,094✔
136

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

142
            for(size_t i = 0; i != iterations; ++i) {
1,034,187✔
143
               cipher->encrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,017,093✔
144
            }
145

146
            result.test_bin_eq(provider + " encrypt misaligned", {buf.data() + 1, buf.size() - 1}, expected);
17,094✔
147

148
            // always decrypt expected ciphertext vs what we produced above
149
            Botan::copy_mem(buf.data() + 1, expected.data(), expected.size());
17,094✔
150

151
            for(size_t i = 0; i != iterations; ++i) {
1,034,187✔
152
               cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,017,093✔
153
            }
154

155
            result.test_bin_eq(provider + " decrypt misaligned", std::span{buf.data() + 1, buf.size() - 1}, input);
17,094✔
156

157
            result.test_is_true("key set", cipher->has_keying_material());
17,094✔
158
            cipher->clear();
17,094✔
159
            result.test_is_false("key set", cipher->has_keying_material());
17,094✔
160

161
            try {
17,094✔
162
               std::vector<uint8_t> block(cipher->block_size());
17,094✔
163
               cipher->encrypt(block);
17,094✔
164
               result.test_failure("Was able to encrypt without a key being set");
×
165
            } catch(Botan::Invalid_State&) {
34,188✔
166
               result.test_success("Trying to encrypt with no key set (after clear) fails");
17,094✔
167
            }
17,094✔
168

169
            try {
17,094✔
170
               std::vector<uint8_t> block(cipher->block_size());
17,094✔
171
               cipher->decrypt(block);
17,094✔
172
               result.test_failure("Was able to decrypt without a key being set");
×
173
            } catch(Botan::Invalid_State&) {
34,188✔
174
               result.test_success("Trying to decrypt with no key set (after clear) fails");
17,094✔
175
            }
17,094✔
176
         }
68,376✔
177

178
         return result;
179
      }
85,470✔
180
};
181

182
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
183

184
class BlockCipher_ParallelOp_Test final : public Test {
1✔
185
   public:
186
      std::vector<Test::Result> run() override {
1✔
187
         /*
188
         * This is somewhat intentionally not a list of all ciphers
189
         * but rather those that are or are likely in the future to be
190
         * implemented using some kind of bitslicing or SIMD technique.
191
         */
192
         const std::vector<std::string> ciphers = {"AES-128",
1✔
193
                                                   "AES-192",
194
                                                   "AES-256",
195
                                                   "ARIA-128",
196
                                                   "ARIA-256",
197
                                                   "Camellia-128",
198
                                                   "Camellia-192",
199
                                                   "Camellia-256",
200
                                                   "DES",
201
                                                   "TripleDES",
202
                                                   "IDEA",
203
                                                   "Noekeon",
204
                                                   "SEED",
205
                                                   "Serpent",
206
                                                   "SHACAL2",
207
                                                   "SM4"};
1✔
208

209
         std::vector<Test::Result> results;
1✔
210
         results.reserve(ciphers.size());
1✔
211
         for(const auto& cipher : ciphers) {
17✔
212
            results.push_back(test_parallel_op(cipher));
32✔
213
         }
214
         return results;
1✔
215
      }
1✔
216

217
   private:
218
      Test::Result test_parallel_op(const std::string& cipher_name) const {
16✔
219
         Test::Result result(cipher_name + " parallel operation");
16✔
220

221
         auto cipher = Botan::BlockCipher::create(cipher_name);
16✔
222
         if(cipher == nullptr) {
16✔
223
            result.note_missing(cipher_name);
×
224
            return result;
225
         }
226

227
         result.test_sz_gte("Has non-zero parallelism", cipher->parallelism(), 1);
16✔
228

229
         const size_t block_size = cipher->block_size();
16✔
230

231
         // Chosen to maximize coverage of handling of tail blocks
232
         constexpr size_t test_blocks = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1;
16✔
233

234
         std::vector<uint8_t> input(block_size * test_blocks);
16✔
235
         rng().randomize(input);
16✔
236

237
         cipher->set_key(rng().random_vec(cipher->maximum_keylength()));
16✔
238

239
         // Encrypt the message one block at a time
240
         std::vector<uint8_t> enc_1by1(input);
16✔
241

242
         for(size_t i = 0; i != test_blocks; ++i) {
4,096✔
243
            cipher->encrypt(&enc_1by1[i * block_size], &enc_1by1[i * block_size]);
4,080✔
244
         }
245

246
         // Encrypt the message with all blocks potentially in parallel
247
         std::vector<uint8_t> enc_all(input);
16✔
248

249
         cipher->encrypt(enc_all);
16✔
250

251
         result.test_bin_eq("Same output no matter how encrypted", enc_all, enc_1by1);
16✔
252

253
         // Decrypt the message one block at a time
254
         for(size_t i = 0; i != test_blocks; ++i) {
4,096✔
255
            cipher->decrypt(&enc_1by1[i * block_size], &enc_1by1[i * block_size]);
4,080✔
256
         }
257

258
         // Decrypt the message with all blocks potentially in parallel
259
         cipher->decrypt(enc_all);
16✔
260

261
         result.test_bin_eq("Same output no matter how decrypted", enc_all, enc_1by1);
16✔
262
         result.test_bin_eq("Original input recovered in 1-by-1", enc_1by1, input);
16✔
263
         result.test_bin_eq("Original input recovered in parallel processing", enc_all, input);
16✔
264

265
         return result;
16✔
266
      }
32✔
267
};
268

269
BOTAN_REGISTER_TEST("block", "bc_parop", BlockCipher_ParallelOp_Test);
270

271
#endif
272

273
}  // namespace
274

275
}  // 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