• 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

93.98
/src/tests/test_mac.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
* (C) 2016 René Korthaus
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_MAC)
11
   #include <botan/mac.h>
12
#endif
13

14
namespace Botan_Tests {
15

16
namespace {
17

18
#if defined(BOTAN_HAS_MAC)
19

20
class Message_Auth_Tests final : public Text_Based_Test {
×
21
   public:
22
      Message_Auth_Tests() : Text_Based_Test("mac", "Key,In,Out", "IV") {}
2✔
23

24
      std::vector<std::string> possible_providers(const std::string& algo) override {
587✔
25
         return provider_filter(Botan::MessageAuthenticationCode::providers(algo));
587✔
26
      }
27

28
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
587✔
29
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
587✔
30
         const std::vector<uint8_t> input = vars.get_req_bin("In");
587✔
31
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
587✔
32
         const std::vector<uint8_t> iv = vars.get_opt_bin("IV");
587✔
33

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

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

38
         if(providers.empty()) {
587✔
39
            result.note_missing("MAC " + algo);
×
40
            return result;
×
41
         }
42

43
         for(const auto& provider_ask : providers) {
1,174✔
44
            auto mac = Botan::MessageAuthenticationCode::create(algo, provider_ask);
587✔
45

46
            if(!mac) {
587✔
47
               result.test_failure("MAC " + algo + " supported by " + provider_ask + " but not found");
×
48
               continue;
×
49
            }
50

51
            const std::string provider(mac->provider());
587✔
52

53
            result.test_is_nonempty("provider", provider);
587✔
54
            result.test_eq(provider, mac->name(), algo);
587✔
55

56
            try {
587✔
57
               std::vector<uint8_t> buf(128);
587✔
58
               mac->update(buf.data(), buf.size());
587✔
59
               result.test_failure("Was able to MAC without a key being set");
587✔
60
            } catch(Botan::Invalid_State&) { result.test_success("Trying to MAC with no key set fails"); }
1,174✔
61

62
            result.test_eq("key not set", mac->has_keying_material(), false);
587✔
63
            mac->set_key(key);
587✔
64
            result.test_eq("key set", mac->has_keying_material(), true);
587✔
65
            mac->start(iv);
587✔
66
            mac->update(input);
587✔
67
            result.test_eq(provider, "correct mac", mac->final(), expected);
1,174✔
68

69
            mac->set_key(key);
587✔
70
            mac->start(iv);
587✔
71
            mac->update(input);
587✔
72
            result.test_eq(provider, "correct mac (try 2)", mac->final(), expected);
1,174✔
73

74
            if(!mac->fresh_key_required_per_message()) {
587✔
75
               for(size_t i = 0; i != 3; ++i) {
2,036✔
76
                  mac->start(iv);
1,527✔
77
                  mac->update(input);
1,527✔
78
                  result.test_eq(provider, "correct mac (same key)", mac->final(), expected);
4,581✔
79
               }
80
            }
81

82
            // Test to make sure clear() resets what we need it to
83
            mac->set_key(key);
587✔
84
            mac->start(iv);
587✔
85
            mac->update("some discarded input");
587✔
86
            mac->clear();
587✔
87
            result.test_eq("key not set", mac->has_keying_material(), false);
587✔
88

89
            // do the same to test verify_mac()
90
            mac->set_key(key);
587✔
91
            mac->start(iv);
587✔
92
            mac->update(input);
587✔
93

94
            // Test that clone works and does not affect parent object
95
            auto clone = mac->new_object();
587✔
96
            result.confirm("Clone has different pointer", mac.get() != clone.get());
1,174✔
97
            result.test_eq("Clone has same name", mac->name(), clone->name());
1,214✔
98
            clone->set_key(key);
587✔
99
            clone->start(iv);
587✔
100
            clone->update(Test::rng().random_vec(32));
587✔
101

102
            result.test_eq(provider + " verify mac", mac->verify_mac(expected.data(), expected.size()), true);
587✔
103

104
            if(input.size() > 2) {
587✔
105
               mac->set_key(key);  // Poly1305 requires the re-key
551✔
106
               mac->start(iv);
551✔
107

108
               mac->update(input[0]);
551✔
109
               mac->update(&input[1], input.size() - 2);
551✔
110
               mac->update(input[input.size() - 1]);
551✔
111

112
               result.test_eq(provider, "split mac", mac->final(), expected);
1,102✔
113

114
               // do the same to test verify_mac()
115
               mac->set_key(key);
551✔
116
               mac->start(iv);
551✔
117

118
               mac->update(input[0]);
551✔
119
               mac->update(&input[1], input.size() - 2);
551✔
120
               mac->update(input[input.size() - 1]);
551✔
121

122
               result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true);
1,102✔
123
            }
124

125
            mac->clear();
587✔
126

127
            try {
587✔
128
               std::vector<uint8_t> buf(128);
587✔
129
               mac->update(buf.data(), buf.size());
587✔
130
               result.test_failure("Was able to MAC without a key being set");
587✔
131
            } catch(Botan::Invalid_State&) { result.test_success("Trying to MAC with no key set (after clear) fails"); }
1,174✔
132

133
            try {
587✔
134
               std::vector<uint8_t> buf(mac->output_length());
587✔
135
               mac->final(buf.data());
587✔
136
               result.test_failure("Was able to MAC without a key being set");
587✔
137
            } catch(Botan::Invalid_State&) { result.test_success("Trying to MAC with no key set (after clear) fails"); }
1,174✔
138
         }
1,174✔
139

140
         return result;
141
      }
2,408✔
142
};
143

144
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("mac", "mac_algos", Message_Auth_Tests);
145

146
#endif
147

148
}  // namespace
149

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

© 2025 Coveralls, Inc