aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_modes.cpp
blob: 2646c90590f73b65060f9a6ec06ab3a4e4e822a1 (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
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_MODES)

#if defined(BOTAN_HAS_FILTERS)

#include <botan/hex.h>
#include <botan/lookup.h>
#include <botan/cipher_mode.h>
#include <botan/filters.h>
#include <iostream>
#include <fstream>
#include <memory>

using namespace Botan;

namespace {

secure_vector<byte> run_mode(const std::string& algo,
                             Cipher_Dir dir,
                             const secure_vector<byte>& pt,
                             const secure_vector<byte>& nonce,
                             const secure_vector<byte>& key)
   {
#if 0
   std::unique_ptr<Cipher_Mode> cipher(get_cipher(algo, dir));

   cipher->set_key(key);
   cipher->start(nonce);

   secure_vector<byte> ct = pt;
   cipher->finish(ct);
#endif

   Pipe pipe(get_cipher(algo, SymmetricKey(key), InitializationVector(nonce), dir));

   pipe.process_msg(pt);

   return pipe.read_all();
   }

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

   size_t fails = 0;

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

   if(ct != ct2)
      {
      std::cout << algo << " got ct " << ct2 << " expected " << ct << std::endl;
      ++fails;
      }

   const std::string pt2 = hex_encode(run_mode(algo,
                                               DECRYPTION,
                                               hex_decode_locked(ct),
                                               nonce,
                                               key));

   if(pt != pt2)
      {
      std::cout << algo << " got pt " << pt2 << " expected " << pt << std::endl;
      ++fails;
      }

   return fails;
   }

}

size_t test_modes()
   {
   auto test = [](const std::string& input)
      {
      std::ifstream vec(input);

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

   return run_tests_in_dir(TEST_DATA_DIR "modes", test);
   }

#else

UNTESTED_WARNING(modes);

#endif // BOTAN_HAS_FILTERS

#else

SKIP_TEST(modes);

#endif // BOTAN_HAS_MODES