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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

91.09
/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,022✔
20
         return provider_filter(Botan::BlockCipher::providers(algo));
14,022✔
21
      }
22

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

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

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

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

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

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

46
            if(!cipher) {
14,022✔
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,022✔
52
            result.test_is_nonempty("provider", provider);
14,022✔
53
            result.test_eq(provider, cipher->name(), algo);
14,022✔
54
            result.test_gte(provider, cipher->parallelism(), 1);
14,022✔
55
            result.test_gte(provider, cipher->block_size(), 8);
14,022✔
56
            result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
28,044✔
57

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

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

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

77
            // Test to make sure clear() resets what we need it to
78
            cipher->set_key(Test::rng().random_vec(cipher->key_spec().maximum_keylength()));
14,022✔
79
            Botan::secure_vector<uint8_t> garbage = Test::rng().random_vec(cipher->block_size());
14,022✔
80
            cipher->encrypt(garbage);
14,022✔
81
            cipher->clear();
14,022✔
82

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

93
            cipher->set_key(key);
14,022✔
94
            result.test_eq("key set", cipher->has_keying_material(), true);
14,022✔
95

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

105
            // Test that clone works and does not affect parent object
106
            auto clone = cipher->new_object();
14,022✔
107
            result.confirm("Clone has different pointer", cipher.get() != clone.get());
28,044✔
108
            result.test_eq("Clone has same name", cipher->name(), clone->name());
28,079✔
109
            clone->set_key(Test::rng().random_vec(cipher->maximum_keylength()));
14,022✔
110

111
            // have called set_key on clone: process input values
112
            std::vector<uint8_t> buf = input;
14,022✔
113

114
            for(size_t i = 0; i != iterations; ++i) {
1,028,043✔
115
               cipher->encrypt(buf);
2,028,042✔
116
            }
117

118
            result.test_eq(provider, "encrypt", buf, expected);
14,022✔
119

120
            // always decrypt expected ciphertext vs what we produced above
121
            buf = expected;
14,022✔
122

123
            for(size_t i = 0; i != iterations; ++i) {
1,028,043✔
124
               cipher->decrypt(buf);
2,028,042✔
125
            }
126

127
            result.test_eq(provider, "decrypt", buf, input);
14,022✔
128

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

134
            for(size_t i = 0; i != iterations; ++i) {
1,028,043✔
135
               cipher->encrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,014,021✔
136
            }
137

138
            result.test_eq(provider.c_str(),
14,022✔
139
                           "encrypt misaligned",
140
                           buf.data() + 1,
14,022✔
141
                           buf.size() - 1,
14,022✔
142
                           expected.data(),
143
                           expected.size());
144

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

148
            for(size_t i = 0; i != iterations; ++i) {
1,028,043✔
149
               cipher->decrypt_n(buf.data() + 1, buf.data() + 1, blocks);
1,014,021✔
150
            }
151

152
            result.test_eq(
14,022✔
153
               provider.c_str(), "decrypt misaligned", buf.data() + 1, buf.size() - 1, input.data(), input.size());
14,022✔
154

155
            result.test_eq("key set", cipher->has_keying_material(), true);
14,022✔
156
            cipher->clear();
14,022✔
157
            result.test_eq("key set", cipher->has_keying_material(), false);
14,022✔
158

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

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

176
         return result;
177
      }
70,110✔
178
};
179

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

182
}  // namespace Botan_Tests
183

184
#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