~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/tests/test_mac.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   {
 
22
   public:
 
23
      Message_Auth_Tests() : Text_Based_Test("mac", "Key,In,Out", "IV") {}
 
24
 
 
25
      std::vector<std::string> possible_providers(const std::string& algo) override
 
26
         {
 
27
         return provider_filter(Botan::MessageAuthenticationCode::providers(algo));
 
28
         }
 
29
 
 
30
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
 
31
         {
 
32
         const std::vector<uint8_t> key      = get_req_bin(vars, "Key");
 
33
         const std::vector<uint8_t> input    = get_req_bin(vars, "In");
 
34
         const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
 
35
         const std::vector<uint8_t> iv       = get_opt_bin(vars, "IV");
 
36
 
 
37
         Test::Result result(algo);
 
38
 
 
39
         const std::vector<std::string> providers = possible_providers(algo);
 
40
 
 
41
         if(providers.empty())
 
42
            {
 
43
            result.note_missing("block cipher " + algo);
 
44
            return result;
 
45
            }
 
46
 
 
47
         for(auto const& provider_ask : providers)
 
48
            {
 
49
            std::unique_ptr<Botan::MessageAuthenticationCode> mac(Botan::MessageAuthenticationCode::create(algo, provider_ask));
 
50
 
 
51
            if(!mac)
 
52
               {
 
53
               result.test_failure("MAC " + algo + " supported by " + provider_ask + " but not found");
 
54
               continue;
 
55
               }
 
56
 
 
57
            const std::string provider(mac->provider());
 
58
 
 
59
            result.test_is_nonempty("provider", provider);
 
60
            result.test_eq(provider, mac->name(), algo);
 
61
 
 
62
            try
 
63
               {
 
64
               std::vector<uint8_t> buf(128);
 
65
               mac->update(buf.data(), buf.size());
 
66
               result.test_failure("Was able to MAC without a key being set");
 
67
               }
 
68
            catch(Botan::Invalid_State&)
 
69
               {
 
70
               result.test_success("Trying to MAC with no key set fails");
 
71
               }
 
72
 
 
73
            mac->set_key(key);
 
74
            mac->start(iv);
 
75
 
 
76
            mac->update(input);
 
77
 
 
78
            result.test_eq(provider, "correct mac", mac->final(), expected);
 
79
 
 
80
            // Test to make sure clear() resets what we need it to
 
81
            mac->set_key(key);
 
82
            mac->start(iv);
 
83
            mac->update("some discarded input");
 
84
            mac->clear();
 
85
 
 
86
            // do the same to test verify_mac()
 
87
            mac->set_key(key);
 
88
            mac->start(iv);
 
89
            mac->update(input);
 
90
 
 
91
            // Test that clone works and does not affect parent object
 
92
            std::unique_ptr<Botan::MessageAuthenticationCode> clone(mac->clone());
 
93
            result.confirm("Clone has different pointer", mac.get() != clone.get());
 
94
            result.test_eq("Clone has same name", mac->name(), clone->name());
 
95
            clone->set_key(key);
 
96
            clone->start(iv);
 
97
            clone->update(Test::rng().random_vec(32));
 
98
 
 
99
            result.test_eq(provider + " verify mac", mac->verify_mac(expected.data(), expected.size()), true);
 
100
 
 
101
            if(input.size() > 2)
 
102
               {
 
103
               mac->set_key(key); // Poly1305 requires the re-key
 
104
               mac->start(iv);
 
105
 
 
106
               mac->update(input[0]);
 
107
               mac->update(&input[1], input.size() - 2);
 
108
               mac->update(input[input.size() - 1]);
 
109
 
 
110
               result.test_eq(provider, "split mac", mac->final(), expected);
 
111
 
 
112
               // do the same to test verify_mac()
 
113
               mac->set_key(key);
 
114
               mac->start(iv);
 
115
 
 
116
               mac->update(input[ 0 ]);
 
117
               mac->update(&input[ 1 ], input.size() - 2);
 
118
               mac->update(input[ input.size() - 1 ]);
 
119
 
 
120
               result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true);
 
121
               }
 
122
 
 
123
            mac->clear();
 
124
 
 
125
            try
 
126
               {
 
127
               std::vector<uint8_t> buf(128);
 
128
               mac->update(buf.data(), buf.size());
 
129
               result.test_failure("Was able to MAC without a key being set");
 
130
               }
 
131
            catch(Botan::Invalid_State&)
 
132
               {
 
133
               result.test_success("Trying to MAC with no key set (after clear) fails");
 
134
               }
 
135
 
 
136
            try
 
137
               {
 
138
               std::vector<uint8_t> buf(mac->output_length());
 
139
               mac->final(buf.data());
 
140
               result.test_failure("Was able to MAC without a key being set");
 
141
               }
 
142
            catch(Botan::Invalid_State&)
 
143
               {
 
144
               result.test_success("Trying to MAC with no key set (after clear) fails");
 
145
               }
 
146
            }
 
147
 
 
148
         return result;
 
149
         }
 
150
   };
 
151
 
 
152
BOTAN_REGISTER_TEST("mac", Message_Auth_Tests);
 
153
 
 
154
#endif
 
155
 
 
156
}
 
157
 
 
158
}