aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_mac.cpp
blob: 10236dfff4d6f64512c33fefd62f4694323f1340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* (C) 2014,2015 Jack Lloyd
* (C) 2016 René Korthaus
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_MAC)
  #include <botan/mac.h>
#endif

#if defined(BOTAN_HAS_GMAC)
  #include <botan/gmac.h>
#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_MAC)

class Message_Auth_Tests : public Text_Based_Test
   {
   public:
      Message_Auth_Tests() :
         Text_Based_Test("mac", {"Key", "In", "Out"}, {"IV"}) {}

      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
         {
         const std::vector<uint8_t> key      = get_req_bin(vars, "Key");
         const std::vector<uint8_t> input    = get_req_bin(vars, "In");
         const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
         const std::vector<uint8_t> iv       = get_opt_bin(vars, "IV");

         Test::Result result(algo);

         const std::vector<std::string> providers = Botan::MessageAuthenticationCode::providers(algo);

         if(providers.empty())
            {
            result.note_missing("block cipher " + algo);
            return result;
            }

         for(auto&& provider_ask : providers)
            {
            std::unique_ptr<Botan::MessageAuthenticationCode> mac(Botan::MessageAuthenticationCode::create(algo, provider_ask));

            if(!mac)
               {
               result.note_missing(algo + " from " + provider_ask);
               continue;
               }

            const std::string provider(mac->provider());

            result.test_is_nonempty("provider", provider);
            result.test_eq(provider, mac->name(), algo);

            mac->set_key(key);

#if defined(BOTAN_HAS_GMAC)
            // GMAC needs to set an IV
            if(Botan::GMAC* gmac = dynamic_cast<Botan::GMAC*>(mac.get()))
               {
               gmac->start(iv);
               }
#endif

            // do the same to test verify_mac()
            mac->set_key(key);
            mac->update(input);

            result.test_eq(provider + " correct mac", mac->verify_mac(expected.data(), expected.size()), true);

            if(input.size() > 2)
               {
               mac->set_key(key); // Poly1305 requires the re-key
               mac->update(input[0]);
               mac->update(&input[1], input.size() - 2);
               mac->update(input[input.size()-1]);

               result.test_eq(provider, "split mac", mac->final(), expected);

               // do the same to test verify_mac()
               mac->set_key(key);
               mac->update(input[ 0 ]);
               mac->update(&input[ 1 ], input.size() - 2);
               mac->update(input[ input.size() - 1 ]);

               result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true);
               }
            }

         return result;
         }
   };

BOTAN_REGISTER_TEST("mac", Message_Auth_Tests);

#endif

}

}