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

randombit / botan / 16293079084

15 Jul 2025 12:20PM UTC coverage: 90.627% (+0.003%) from 90.624%
16293079084

push

github

web-flow
Merge pull request #4990 from randombit/jack/string-and-span

Improve string<->span conversions

99640 of 109945 relevant lines covered (90.63%)

12253617.72 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/mem_ops.h>
12
   #include <botan/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_BLOCK_CIPHER)
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

122
            result.test_eq(provider, "encrypt", buf, expected);
35,026✔
123

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

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

131
            result.test_eq(provider, "decrypt", buf, input);
35,026✔
132

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

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

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

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

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

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

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

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

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

180
         return result;
181
      }
17,513✔
182
};
183

184
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
185

186
#endif
187

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