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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

90.72
/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

11
   #include <botan/block_cipher.h>
12

13
namespace Botan_Tests {
14

15
class Block_Cipher_Tests final : public Text_Based_Test {
×
16
   public:
17
      Block_Cipher_Tests() : Text_Based_Test("block", "Key,In,Out", "Tweak,Iterations") {}
3✔
18

19
      std::vector<std::string> possible_providers(const std::string& algo) override {
14,019✔
20
         return provider_filter(Botan::BlockCipher::providers(algo));
14,019✔
21
      }
22

23
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
14,019✔
24
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
14,019✔
25
         const std::vector<uint8_t> input = vars.get_req_bin("In");
14,019✔
26
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
14,019✔
27
         const std::vector<uint8_t> tweak = vars.get_opt_bin("Tweak");
14,019✔
28
         const size_t iterations = vars.get_opt_sz("Iterations", 1);
14,019✔
29

30
         Test::Result result(algo);
28,038✔
31

32
         if(iterations > 1 && run_long_tests() == false) {
14,019✔
33
            return result;
34
         }
35

36
         const std::vector<std::string> providers = possible_providers(algo);
14,019✔
37

38
         if(providers.empty()) {
14,019✔
39
            result.note_missing("block cipher " + algo);
×
40
            return result;
×
41
         }
42

43
         for(const auto& provider_ask : providers) {
28,038✔
44
            auto cipher = Botan::BlockCipher::create(algo, provider_ask);
14,019✔
45

46
            if(!cipher) {
14,019✔
47
               result.test_failure("Cipher " + algo + " supported by " + provider_ask + " but not found");
×
48
               continue;
×
49
            }
50

51
            const std::string provider(cipher->provider());
14,019✔
52
            result.test_is_nonempty("provider", provider);
14,019✔
53
            result.test_eq(provider, cipher->name(), algo);
14,019✔
54
            result.test_gte(provider, cipher->parallelism(), 1);
14,019✔
55
            result.test_gte(provider, cipher->block_size(), 8);
14,019✔
56
            result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
28,038✔
57

58
            result.test_eq("no key set", cipher->has_keying_material(), false);
14,019✔
59

60
            // Test that trying to encrypt or decrypt with no key set throws Botan::Invalid_State
61
            try {
14,019✔
62
               std::vector<uint8_t> block(cipher->block_size());
14,019✔
63
               cipher->encrypt(block);
14,019✔
64
               result.test_failure("Was able to encrypt without a key being set");
14,019✔
65
            } catch(Botan::Invalid_State&) { result.test_success("Trying to encrypt with no key set fails"); }
28,038✔
66

67
            try {
14,019✔
68
               std::vector<uint8_t> block(cipher->block_size());
14,019✔
69
               cipher->decrypt(block);
14,019✔
70
               result.test_failure("Was able to decrypt without a key being set");
14,019✔
71
            } catch(Botan::Invalid_State&) { result.test_success("Trying to encrypt with no key set fails"); }
28,038✔
72

73
            // Test to make sure clear() resets what we need it to
74
            cipher->set_key(Test::rng().random_vec(cipher->key_spec().maximum_keylength()));
14,019✔
75
            Botan::secure_vector<uint8_t> garbage = Test::rng().random_vec(cipher->block_size());
14,019✔
76
            cipher->encrypt(garbage);
14,019✔
77
            cipher->clear();
14,019✔
78

79
            /*
80
            * Different providers may have additional restrictions on key sizes.
81
            * Avoid testing the cipher with a key size that it does not natively support.
82
            */
83
            if(!cipher->valid_keylength(key.size())) {
14,019✔
84
               result.test_note("Skipping test with provider " + provider + " as it does not support key length " +
×
85
                                std::to_string(key.size()));
×
86
               continue;
×
87
            }
88

89
            cipher->set_key(key);
14,019✔
90
            result.test_eq("key set", cipher->has_keying_material(), true);
14,019✔
91

92
            if(!tweak.empty()) {
14,019✔
93
               Botan::Tweakable_Block_Cipher* tbc = dynamic_cast<Botan::Tweakable_Block_Cipher*>(cipher.get());
2✔
94
               if(tbc == nullptr)
2✔
95
                  result.test_failure("Tweak set in test data but cipher is not a Tweakable_Block_Cipher");
×
96
               else
97
                  tbc->set_tweak(tweak.data(), tweak.size());
2✔
98
            }
99

100
            // Test that clone works and does not affect parent object
101
            auto clone = cipher->new_object();
14,019✔
102
            result.confirm("Clone has different pointer", cipher.get() != clone.get());
28,038✔
103
            result.test_eq("Clone has same name", cipher->name(), clone->name());
28,073✔
104
            clone->set_key(Test::rng().random_vec(cipher->maximum_keylength()));
14,019✔
105

106
            // have called set_key on clone: process input values
107
            std::vector<uint8_t> buf = input;
14,019✔
108

109
            for(size_t i = 0; i != iterations; ++i) {
1,028,037✔
110
               cipher->encrypt(buf);
2,028,036✔
111
            }
112

113
            result.test_eq(provider, "encrypt", buf, expected);
14,019✔
114

115
            // always decrypt expected ciphertext vs what we produced above
116
            buf = expected;
14,019✔
117

118
            for(size_t i = 0; i != iterations; ++i) {
1,028,037✔
119
               cipher->decrypt(buf);
2,028,036✔
120
            }
121

122
            result.test_eq(provider, "decrypt", buf, input);
14,019✔
123

124
            // Now test misaligned buffers
125
            const size_t blocks = input.size() / cipher->block_size();
14,019✔
126
            buf.resize(input.size() + 1);
14,019✔
127
            Botan::copy_mem(buf.data() + 1, input.data(), input.size());
14,019✔
128

129
            for(size_t i = 0; i != iterations; ++i) {
1,028,037✔
130
               cipher->encrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,014,018✔
131
            }
132

133
            result.test_eq(provider.c_str(),
14,019✔
134
                           "encrypt misaligned",
135
                           buf.data() + 1,
14,019✔
136
                           buf.size() - 1,
14,019✔
137
                           expected.data(),
138
                           expected.size());
139

140
            // always decrypt expected ciphertext vs what we produced above
141
            Botan::copy_mem(buf.data() + 1, expected.data(), expected.size());
14,019✔
142

143
            for(size_t i = 0; i != iterations; ++i) {
1,028,037✔
144
               cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,014,018✔
145
            }
146

147
            result.test_eq(
14,019✔
148
               provider.c_str(), "decrypt misaligned", buf.data() + 1, buf.size() - 1, input.data(), input.size());
14,019✔
149

150
            result.test_eq("key set", cipher->has_keying_material(), true);
14,019✔
151
            cipher->clear();
14,019✔
152
            result.test_eq("key set", cipher->has_keying_material(), false);
14,019✔
153

154
            try {
14,019✔
155
               std::vector<uint8_t> block(cipher->block_size());
14,019✔
156
               cipher->encrypt(block);
14,019✔
157
               result.test_failure("Was able to encrypt without a key being set");
14,019✔
158
            } catch(Botan::Invalid_State&) {
14,019✔
159
               result.test_success("Trying to encrypt with no key set (after clear) fails");
14,019✔
160
            }
14,019✔
161

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

171
         return result;
172
      }
70,095✔
173
};
174

175
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("block", "block_ciphers", Block_Cipher_Tests);
176

177
}  // namespace Botan_Tests
178

179
#endif
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

© 2025 Coveralls, Inc