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

randombit / botan / 23700909588

29 Mar 2026 01:27AM UTC coverage: 91.923% (+2.4%) from 89.529%
23700909588

push

github

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

Remove SM4 iterations test

108264 of 117777 relevant lines covered (91.92%)

11240399.19 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 {
20,029✔
28
         return provider_filter(Botan::BlockCipher::providers(algo));
20,029✔
29
      }
30

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

37
         Test::Result result(algo);
20,029✔
38

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

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

46
         for(const auto& provider_ask : providers) {
40,058✔
47
            auto cipher = Botan::BlockCipher::create(algo, provider_ask);
20,029✔
48

49
            if(!cipher) {
20,029✔
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());
20,029✔
55
            result.test_str_not_empty("provider", provider);
20,029✔
56
            result.test_str_eq(provider, cipher->name(), algo);
20,029✔
57
            result.test_sz_gte(provider, cipher->parallelism(), 1);
20,029✔
58
            result.test_sz_gte(provider, cipher->block_size(), 8);
20,029✔
59
            result.test_sz_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
40,058✔
60

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

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

72
            try {
20,029✔
73
               std::vector<uint8_t> block(cipher->block_size());
20,029✔
74
               cipher->decrypt(block);
20,029✔
75
               result.test_failure("Was able to decrypt without a key being set");
×
76
            } catch(Botan::Invalid_State&) {
40,058✔
77
               result.test_success("Trying to encrypt with no key set fails");
20,029✔
78
            }
20,029✔
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()));
20,029✔
82
            Botan::secure_vector<uint8_t> garbage = this->rng().random_vec(cipher->block_size());
20,029✔
83
            cipher->encrypt(garbage);
20,029✔
84
            cipher->clear();
20,029✔
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())) {
20,029✔
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);
20,029✔
97
            result.test_is_true("key set", cipher->has_keying_material());
20,029✔
98

99
            if(!tweak.empty()) {
20,029✔
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();
20,029✔
110
            result.test_is_true("Clone has different pointer", cipher.get() != clone.get());
20,029✔
111
            result.test_str_eq("Clone has same name", cipher->name(), clone->name());
20,029✔
112
            clone->set_key(this->rng().random_vec(cipher->maximum_keylength()));
20,029✔
113

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

117
            cipher->encrypt(buf);
20,029✔
118

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

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

124
            cipher->decrypt(buf);
20,029✔
125

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

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

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

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

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

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

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

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

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

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

165
         return result;
166
      }
100,145✔
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