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

randombit / botan / 22038388571

15 Feb 2026 03:39PM UTC coverage: 90.056% (-0.003%) from 90.059%
22038388571

Pull #5341

github

web-flow
Merge e5a537aa4 into 76dfe61e9
Pull Request #5341: Cleanup test predicates on binary strings

102343 of 113644 relevant lines covered (90.06%)

11670901.95 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
#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,538✔
26
         return provider_filter(Botan::BlockCipher::providers(algo));
17,538✔
27
      }
28

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

36
         Test::Result result(algo);
17,538✔
37

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

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

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

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

52
            if(!cipher) {
17,538✔
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,538✔
58
            result.test_str_not_empty("provider", provider);
17,538✔
59
            result.test_str_eq(provider, cipher->name(), algo);
17,538✔
60
            result.test_sz_gte(provider, cipher->parallelism(), 1);
17,538✔
61
            result.test_sz_gte(provider, cipher->block_size(), 8);
17,538✔
62
            result.test_sz_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
35,076✔
63

64
            result.test_is_false("no key set", cipher->has_keying_material());
17,538✔
65

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

75
            try {
17,538✔
76
               std::vector<uint8_t> block(cipher->block_size());
17,538✔
77
               cipher->decrypt(block);
17,538✔
78
               result.test_failure("Was able to decrypt without a key being set");
×
79
            } catch(Botan::Invalid_State&) {
35,076✔
80
               result.test_success("Trying to encrypt with no key set fails");
17,538✔
81
            }
17,538✔
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,538✔
85
            Botan::secure_vector<uint8_t> garbage = this->rng().random_vec(cipher->block_size());
17,538✔
86
            cipher->encrypt(garbage);
17,538✔
87
            cipher->clear();
17,538✔
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,538✔
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,538✔
100
            result.test_is_true("key set", cipher->has_keying_material());
17,538✔
101

102
            if(!tweak.empty()) {
17,538✔
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,538✔
113
            result.test_is_true("Clone has different pointer", cipher.get() != clone.get());
17,538✔
114
            result.test_str_eq("Clone has same name", cipher->name(), clone->name());
17,538✔
115
            clone->set_key(this->rng().random_vec(cipher->maximum_keylength()));
17,538✔
116

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

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

124
            result.test_bin_eq(provider + " encrypt", buf, expected);
17,538✔
125

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

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

133
            result.test_bin_eq(provider + " decrypt", buf, input);
17,538✔
134

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

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

144
            result.test_bin_eq(provider + " encrypt misaligned", {buf.data() + 1, buf.size() - 1}, expected);
17,538✔
145

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

149
            for(size_t i = 0; i != iterations; ++i) {
1,035,075✔
150
               cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,017,537✔
151
            }
152

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

155
            result.test_is_true("key set", cipher->has_keying_material());
17,538✔
156
            cipher->clear();
17,538✔
157
            result.test_is_false("key set", cipher->has_keying_material());
17,538✔
158

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

167
            try {
17,538✔
168
               std::vector<uint8_t> block(cipher->block_size());
17,538✔
169
               cipher->decrypt(block);
17,538✔
170
               result.test_failure("Was able to decrypt without a key being set");
×
171
            } catch(Botan::Invalid_State&) {
35,076✔
172
               result.test_success("Trying to decrypt with no key set (after clear) fails");
17,538✔
173
            }
17,538✔
174
         }
70,152✔
175

176
         return result;
177
      }
87,690✔
178
};
179

180
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
181

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

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

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

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

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

227
         const size_t block_size = cipher->block_size();
16✔
228

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

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

235
         cipher->set_key(rng().random_vec(cipher->maximum_keylength()));
16✔
236

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

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

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

247
         cipher->encrypt(enc_all);
16✔
248

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

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

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

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

263
         return result;
16✔
264
      }
32✔
265
};
266

267
BOTAN_REGISTER_TEST("block", "bc_parop", BlockCipher_ParallelOp_Test);
268

269
#endif
270

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