aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/kat_aead.cpp
blob: 0bc4a47c5f70abef551b7e9ab9c849a9aadfcca7 (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
#include "tests.h"

#include <botan/hex.h>
#include <botan/aead.h>
#include <iostream>
#include <fstream>
#include <memory>

using namespace Botan;

namespace {

secure_vector<byte> aead(const std::string& algo,
                         Cipher_Dir dir,
                         const secure_vector<byte>& pt,
                         const secure_vector<byte>& nonce,
                         const secure_vector<byte>& ad,
                         const secure_vector<byte>& key)
   {
   std::unique_ptr<AEAD_Mode> aead(get_aead(algo, dir));

   aead->set_key(key);
   aead->set_associated_data_vec(ad);
   aead->start_vec(nonce);

   secure_vector<byte> ct = pt;
   aead->finish(ct);

   return ct;
   }

size_t aead_test(const std::string& algo,
                 const std::string& pt,
                 const std::string& ct,
                 const std::string& nonce_hex,
                 const std::string& ad_hex,
                 const std::string& key_hex)
   {
   auto nonce = hex_decode_locked(nonce_hex);
   auto ad = hex_decode_locked(ad_hex);
   auto key = hex_decode_locked(key_hex);

   size_t fail = 0;

   //std::cout << algo << " pt=" << pt << " ct=" << ct << " key=" << key_hex << " nonce=" << nonce_hex << " ad=" << ad_hex << "\n";

   const std::string ct2 = hex_encode(aead(algo,
                                           ENCRYPTION,
                                           hex_decode_locked(pt),
                                           nonce,
                                           ad,
                                           key));

   if(ct != ct2)
      {
      std::cout << algo << " got ct " << ct2 << " expected " << ct << "\n";
      ++fail;
      }

   const std::string pt2 = hex_encode(aead(algo,
                                           DECRYPTION,
                                           hex_decode_locked(ct2),
                                           nonce,
                                           ad,
                                           key));

   if(pt != pt2)
      {
      std::cout << algo << " got pt " << pt2 << " expected " << pt << "\n";
      ++fail;
      }

   return fail;
   }

}

size_t test_aead()
   {
   std::ifstream vec(TEST_DATA_DIR "/aead.vec");

   return run_tests_bb(vec, "AEAD", "Out", true,
             [](std::map<std::string, std::string> m)
             {
             return aead_test(m["AEAD"], m["In"], m["Out"],
                              m["Nonce"], m["AD"], m["Key"]);
             });
   }