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

randombit / botan / 23698593421

29 Mar 2026 01:27AM UTC coverage: 89.534% (+0.005%) from 89.529%
23698593421

push

github

web-flow
Merge pull request #5492 from randombit/jack/remove-sm4-iter-test

Remove SM4 iterations test

105450 of 117777 relevant lines covered (89.53%)

11856180.34 hits per line

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

88.98
/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") {}
2✔
26

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

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

37
         Test::Result result(algo);
17,146✔
38

39
         const std::vector<std::string> providers = possible_providers(algo);
17,146✔
40

41
         if(providers.empty()) {
17,146✔
42
            result.note_missing("block cipher " + algo);
×
43
            return result;
×
44
         }
45

46
         for(const auto& provider_ask : providers) {
34,292✔
47
            auto cipher = Botan::BlockCipher::create(algo, provider_ask);
17,146✔
48

49
            if(!cipher) {
17,146✔
50
               result.test_failure(Botan::fmt("Cipher {} supported by {} but not found", algo, provider_ask));
×
51
               continue;
×
52
            }
53

54
            const std::string provider(cipher->provider());
17,146✔
55
            result.test_str_not_empty("provider", provider);
17,146✔
56
            result.test_str_eq(provider, cipher->name(), algo);
17,146✔
57
            result.test_sz_gte(provider, cipher->parallelism(), 1);
17,146✔
58
            result.test_sz_gte(provider, cipher->block_size(), 8);
17,146✔
59
            result.test_sz_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
34,292✔
60

61
            result.test_is_false("no key set", cipher->has_keying_material());
17,146✔
62

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

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

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

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

96
            cipher->set_key(key);
17,146✔
97
            result.test_is_true("key set", cipher->has_keying_material());
17,146✔
98

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

108
            // Test that clone works and does not affect parent object
109
            auto clone = cipher->new_object();
17,146✔
110
            result.test_is_true("Clone has different pointer", cipher.get() != clone.get());
17,146✔
111
            result.test_str_eq("Clone has same name", cipher->name(), clone->name());
17,146✔
112
            clone->set_key(this->rng().random_vec(cipher->maximum_keylength()));
17,146✔
113

114
            // have called set_key on clone: process input values
115
            std::vector<uint8_t> buf = input;
17,146✔
116

117
            cipher->encrypt(buf);
17,146✔
118

119
            result.test_bin_eq(provider + " encrypt", buf, expected);
17,146✔
120

121
            // always decrypt expected ciphertext vs what we produced above
122
            buf = expected;
17,146✔
123

124
            cipher->decrypt(buf);
17,146✔
125

126
            result.test_bin_eq(provider + " decrypt", buf, input);
17,146✔
127

128
            // Now test misaligned buffers
129
            const size_t blocks = input.size() / cipher->block_size();
17,146✔
130
            buf.resize(input.size() + 1);
17,146✔
131
            Botan::copy_mem(buf.data() + 1, input.data(), input.size());
17,146✔
132

133
            cipher->encrypt_n(buf.data() + 1, buf.data() + 1, blocks);
17,146✔
134

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

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

140
            cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
17,146✔
141

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

144
            result.test_is_true("key set", cipher->has_keying_material());
17,146✔
145
            cipher->clear();
17,146✔
146
            result.test_is_false("key set", cipher->has_keying_material());
17,146✔
147

148
            try {
17,146✔
149
               std::vector<uint8_t> block(cipher->block_size());
17,146✔
150
               cipher->encrypt(block);
17,146✔
151
               result.test_failure("Was able to encrypt without a key being set");
×
152
            } catch(Botan::Invalid_State&) {
34,292✔
153
               result.test_success("Trying to encrypt with no key set (after clear) fails");
17,146✔
154
            }
17,146✔
155

156
            try {
17,146✔
157
               std::vector<uint8_t> block(cipher->block_size());
17,146✔
158
               cipher->decrypt(block);
17,146✔
159
               result.test_failure("Was able to decrypt without a key being set");
×
160
            } catch(Botan::Invalid_State&) {
34,292✔
161
               result.test_success("Trying to decrypt with no key set (after clear) fails");
17,146✔
162
            }
17,146✔
163
         }
68,584✔
164

165
         return result;
166
      }
85,730✔
167
};
168

169
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
170

171
class BlockCipher_ParallelOp_Test final : public Test {
1✔
172
   public:
173
      std::vector<Test::Result> run() override {
1✔
174
         /*
175
         * This is somewhat intentionally not a list of all ciphers
176
         * but rather those that are or are likely in the future to be
177
         * implemented using some kind of bitslicing or SIMD technique.
178
         */
179
         const std::vector<std::string> ciphers = {"AES-128",
1✔
180
                                                   "AES-192",
181
                                                   "AES-256",
182
                                                   "ARIA-128",
183
                                                   "ARIA-256",
184
                                                   "Camellia-128",
185
                                                   "Camellia-192",
186
                                                   "Camellia-256",
187
                                                   "DES",
188
                                                   "TripleDES",
189
                                                   "IDEA",
190
                                                   "Noekeon",
191
                                                   "SEED",
192
                                                   "Serpent",
193
                                                   "SHACAL2",
194
                                                   "SM4"};
1✔
195

196
         std::vector<Test::Result> results;
1✔
197
         results.reserve(ciphers.size());
1✔
198
         for(const auto& cipher : ciphers) {
17✔
199
            results.push_back(test_parallel_op(cipher));
32✔
200
         }
201
         return results;
1✔
202
      }
1✔
203

204
   private:
205
      Test::Result test_parallel_op(const std::string& cipher_name) const {
16✔
206
         Test::Result result(cipher_name + " parallel operation");
16✔
207

208
         auto cipher = Botan::BlockCipher::create(cipher_name);
16✔
209
         if(cipher == nullptr) {
16✔
210
            result.note_missing(cipher_name);
×
211
            return result;
212
         }
213

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

216
         const size_t block_size = cipher->block_size();
16✔
217

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

221
         std::vector<uint8_t> input(block_size * test_blocks);
16✔
222
         rng().randomize(input);
16✔
223

224
         cipher->set_key(rng().random_vec(cipher->maximum_keylength()));
16✔
225

226
         // Encrypt the message one block at a time
227
         std::vector<uint8_t> enc_1by1(input);
16✔
228

229
         for(size_t i = 0; i != test_blocks; ++i) {
4,096✔
230
            cipher->encrypt(&enc_1by1[i * block_size], &enc_1by1[i * block_size]);
4,080✔
231
         }
232

233
         // Encrypt the message with all blocks potentially in parallel
234
         std::vector<uint8_t> enc_all(input);
16✔
235

236
         cipher->encrypt(enc_all);
16✔
237

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

240
         // Decrypt the message one block at a time
241
         for(size_t i = 0; i != test_blocks; ++i) {
4,096✔
242
            cipher->decrypt(&enc_1by1[i * block_size], &enc_1by1[i * block_size]);
4,080✔
243
         }
244

245
         // Decrypt the message with all blocks potentially in parallel
246
         cipher->decrypt(enc_all);
16✔
247

248
         result.test_bin_eq("Same output no matter how decrypted", enc_all, enc_1by1);
16✔
249
         result.test_bin_eq("Original input recovered in 1-by-1", enc_1by1, input);
16✔
250
         result.test_bin_eq("Original input recovered in parallel processing", enc_all, input);
16✔
251

252
         return result;
16✔
253
      }
32✔
254
};
255

256
BOTAN_REGISTER_TEST("block", "bc_parop", BlockCipher_ParallelOp_Test);
257

258
#endif
259

260
}  // namespace
261

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