aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_pubkey.h
blob: d43909f1451fb30875c3f968648c74df24ffba8f (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_TEST_PUBKEY_H_
#define BOTAN_TEST_PUBKEY_H_

#include "tests.h"

#include "test_rng.h"

#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)

#include <botan/pubkey.h>

namespace Botan_Tests {

class PK_Test : public Text_Based_Test
   {
   public:
      PK_Test(const std::string& algo,
              const std::string& test_src,
              const std::string& required_keys,
              const std::string& optional_keys = {})
         : Text_Based_Test(test_src, required_keys, optional_keys), m_algo(algo) {}

      std::string algo_name() const
         {
         return m_algo;
         }

   protected:
      std::vector<std::string> possible_providers(const std::string& params) override;

      virtual std::string default_padding(const VarMap&) const
         {
         throw Test_Error("No default padding scheme set for " + algo_name());
         }

      virtual std::string choose_padding(const VarMap& vars,
                                         const std::string& pad_hdr);

   private:
      std::string m_algo;
   };

class PK_Signature_Generation_Test : public PK_Test
   {
   public:
      PK_Signature_Generation_Test(const std::string& algo,
                                   const std::string& test_src,
                                   const std::string& required_keys,
                                   const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}


      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;

      virtual Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const
         {
         return new Fixed_Output_RNG(nonce);
         }

   private:
      Test::Result run_one_test(const std::string&, const VarMap& vars) override final;
   };

class PK_Signature_Verification_Test : public PK_Test
   {
   public:
      PK_Signature_Verification_Test(const std::string& algo,
                                     const std::string& test_src,
                                     const std::string& required_keys,
                                     const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}

      virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
   private:
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override final;
   };

class PK_Signature_NonVerification_Test : public PK_Test
   {
   public:
      PK_Signature_NonVerification_Test(const std::string& algo,
                                        const std::string& test_src,
                                        const std::string& required_keys,
                                        const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}

      bool clear_between_callbacks() const override
         {
         return false;
         }

      virtual std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) = 0;
   private:
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override final;
   };

class PK_Encryption_Decryption_Test : public PK_Test
   {
   public:
      PK_Encryption_Decryption_Test(const std::string& algo,
                                    const std::string& test_src,
                                    const std::string& required_keys,
                                    const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}

      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;

      std::string default_padding(const VarMap&) const override
         {
         return "Raw";
         }

      virtual Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const
         {
         return new Fixed_Output_RNG(nonce);
         }

   private:
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override final;
   };

class PK_Key_Agreement_Test : public PK_Test
   {
   public:
      PK_Key_Agreement_Test(const std::string& algo,
                            const std::string& test_src,
                            const std::string& required_keys,
                            const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}

      virtual std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& header,
            const VarMap& vars) = 0;

      virtual std::vector<uint8_t> load_their_key(const std::string& header,
            const VarMap& vars) = 0;

      virtual std::string default_kdf(const VarMap&) const
         {
         return "Raw";
         }

   private:
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override final;
   };

class PK_KEM_Test : public PK_Test
   {
   public:
      PK_KEM_Test(const std::string& algo,
                  const std::string& test_src,
                  const std::string& required_keys,
                  const std::string& optional_keys = "")
         : PK_Test(algo, test_src, required_keys, optional_keys) {}

      virtual std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) = 0;
   private:
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override final;
   };

class PK_Key_Generation_Test : public Test
   {
   protected:
      std::vector<Test::Result> run() override final;

      virtual std::vector<std::string> keygen_params() const = 0;

      virtual std::string algo_name() const = 0;

      std::vector<std::string> possible_providers(const std::string& params) override;
   };

void check_invalid_signatures(Test::Result& result,
                              Botan::PK_Verifier& verifier,
                              const std::vector<uint8_t>& message,
                              const std::vector<uint8_t>& signature);

void check_invalid_ciphertexts(Test::Result& result,
                               Botan::PK_Decryptor& decryptor,
                               const std::vector<uint8_t>& plaintext,
                               const std::vector<uint8_t>& ciphertext);

}

#endif

#endif