aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_aead.cpp
blob: 5d7e1094e88d4db6d616787195927380a6dd9603 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_AEAD_MODES)
#include <botan/aead.h>
#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_AEAD_MODES)

class AEAD_Tests : public Text_Based_Test
   {
   public:
      AEAD_Tests() :
         Text_Based_Test("aead", {"Key", "Nonce", "In", "Out"}, {"AD"})
         {}

      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> nonce    = get_opt_bin(vars, "Nonce");
         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> ad       = get_opt_bin(vars, "AD");

         Test::Result result(algo);

         std::unique_ptr<Botan::AEAD_Mode> enc(Botan::get_aead(algo, Botan::ENCRYPTION));
         std::unique_ptr<Botan::AEAD_Mode> dec(Botan::get_aead(algo, Botan::DECRYPTION));

         if(!enc || !dec)
            {
            result.note_missing(algo);
            return result;
            }

         enc->set_key(key);
         enc->set_associated_data_vec(ad);
         enc->start(nonce);

         Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
         // TODO: should first update if possible
         enc->finish(buf);

         result.test_eq("encrypt", buf, expected);

         buf.assign(expected.begin(), expected.end());

         dec->set_key(key);
         dec->set_associated_data_vec(ad);
         dec->start(nonce);
         dec->finish(buf);

         if(enc->authenticated())
            {
            const std::vector<byte> mutated_input = mutate_vec(expected, true);
            buf.assign(mutated_input.begin(), mutated_input.end());

            dec->start(nonce);

            try
               {
               dec->finish(buf);
               result.test_failure("accepted modified message", mutated_input);
               }
            catch(Botan::Integrity_Failure& e)
               {
               result.test_note("correctly rejected modified message");
               }
            catch(std::exception& e)
               {
               result.test_failure("unexpected error while rejecting modified message", e.what());
               }
            }

         if(nonce.size() > 0)
            {
            buf.assign(expected.begin(), expected.end());
            std::vector<byte> bad_nonce = mutate_vec(nonce);

            dec->start(bad_nonce);

            try
               {
               dec->finish(buf);
               result.test_failure("accepted message with modified nonce", bad_nonce);
               }
            catch(Botan::Integrity_Failure& e)
               {
               result.test_note("correctly rejected modified nonce");
               }
            catch(std::exception& e)
               {
               result.test_failure("unexpected error while rejecting modified nonce", e.what());
               }
            }

         const std::vector<byte> bad_ad = mutate_vec(ad, true);

         dec->set_associated_data_vec(bad_ad);

         dec->start(nonce);

         try
            {
            buf.assign(expected.begin(), expected.end());
            dec->finish(buf);
            result.test_failure("accepted message with modified ad", bad_ad);
            }
         catch(Botan::Integrity_Failure& e)
            {
            result.test_note("correctly rejected modified ad");
            }
         catch(std::exception& e)
            {
            result.test_failure("unexpected error while rejecting modified nonce", e.what());
            }

         return result;
         }
   };

BOTAN_REGISTER_TEST("aead", AEAD_Tests);

#endif

}

}