Coverage Report

Created: 2022-06-23 06:44

/src/botan/build/include/botan/internal/pk_ops_impl.h
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
* (C) 2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_PK_OPERATION_IMPL_H_
9
#define BOTAN_PK_OPERATION_IMPL_H_
10
11
#include <botan/internal/pk_ops.h>
12
#include <botan/internal/eme.h>
13
#include <botan/kdf.h>
14
#include <botan/internal/emsa.h>
15
16
namespace Botan {
17
18
namespace PK_Ops {
19
20
class Encryption_with_EME : public Encryption
21
   {
22
   public:
23
      size_t max_input_bits() const override;
24
25
      secure_vector<uint8_t> encrypt(const uint8_t msg[], size_t msg_len,
26
                                  RandomNumberGenerator& rng) override;
27
28
0
      ~Encryption_with_EME() = default;
29
   protected:
30
      explicit Encryption_with_EME(const std::string& eme);
31
   private:
32
      virtual size_t max_raw_input_bits() const = 0;
33
34
      virtual secure_vector<uint8_t> raw_encrypt(const uint8_t msg[], size_t len,
35
                                              RandomNumberGenerator& rng) = 0;
36
      std::unique_ptr<EME> m_eme;
37
   };
38
39
class Decryption_with_EME : public Decryption
40
   {
41
   public:
42
      secure_vector<uint8_t> decrypt(uint8_t& valid_mask,
43
                                  const uint8_t msg[], size_t msg_len) override;
44
45
0
      ~Decryption_with_EME() = default;
46
   protected:
47
      explicit Decryption_with_EME(const std::string& eme);
48
   private:
49
      virtual secure_vector<uint8_t> raw_decrypt(const uint8_t msg[], size_t len) = 0;
50
      std::unique_ptr<EME> m_eme;
51
   };
52
53
class Verification_with_EMSA : public Verification
54
   {
55
   public:
56
6.79k
      ~Verification_with_EMSA() = default;
57
58
      void update(const uint8_t msg[], size_t msg_len) override;
59
      bool is_valid_signature(const uint8_t sig[], size_t sig_len) override;
60
61
   protected:
62
      explicit Verification_with_EMSA(const std::string& emsa, bool has_message_recovery = false);
63
64
0
      std::string hash_for_signature() { return m_hash; }
65
66
      /**
67
      * Get the maximum message size in bits supported by this public key.
68
      * @return maximum message in bits
69
      */
70
      virtual size_t max_input_bits() const = 0;
71
72
      /**
73
      * @return boolean specifying if this signature scheme uses
74
      * a message prefix returned by message_prefix()
75
      */
76
6.79k
      virtual bool has_prefix() { return false; }
77
78
      /**
79
      * @return the message prefix if this signature scheme uses
80
      * a message prefix, signaled via has_prefix()
81
      */
82
0
      virtual secure_vector<uint8_t> message_prefix() const { throw Invalid_State("No prefix"); }
83
84
      /**
85
      * @return boolean specifying if this key type supports message
86
      * recovery and thus if you need to call verify() or verify_mr()
87
      */
88
      virtual bool with_recovery() const = 0;
89
90
      /*
91
      * Perform a signature check operation
92
      * @param msg the message
93
      * @param msg_len the length of msg in bytes
94
      * @param sig the signature
95
      * @param sig_len the length of sig in bytes
96
      * @returns if signature is a valid one for message
97
      */
98
      virtual bool verify(const uint8_t[], size_t,
99
                          const uint8_t[], size_t)
100
0
         {
101
0
         throw Invalid_State("Message recovery required");
102
0
         }
103
104
      /*
105
      * Perform a signature operation (with message recovery)
106
      * Only call this if with_recovery() returns true
107
      * @param msg the message
108
      * @param msg_len the length of msg in bytes
109
      * @returns recovered message
110
      */
111
      virtual secure_vector<uint8_t> verify_mr(const uint8_t[], size_t)
112
0
         {
113
0
         throw Invalid_State("Message recovery not supported");
114
0
         }
115
116
0
      std::unique_ptr<EMSA> clone_emsa() const { return m_emsa->new_object(); }
117
118
   private:
119
      std::unique_ptr<EMSA> m_emsa;
120
      const std::string m_hash;
121
      bool m_prefix_used;
122
   };
123
124
class Signature_with_EMSA : public Signature
125
   {
126
   public:
127
      void update(const uint8_t msg[], size_t msg_len) override;
128
129
      secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
130
   protected:
131
      explicit Signature_with_EMSA(const std::string& emsa, bool with_message_recovery = false);
132
133
0
      ~Signature_with_EMSA() = default;
134
135
0
      std::string hash_for_signature() { return m_hash; }
136
137
      /**
138
      * @return boolean specifying if this signature scheme uses
139
      * a message prefix returned by message_prefix()
140
      */
141
0
      virtual bool has_prefix() { return false; }
142
143
      /**
144
      * @return the message prefix if this signature scheme uses
145
      * a message prefix, signaled via has_prefix()
146
      */
147
0
      virtual secure_vector<uint8_t> message_prefix() const { throw Invalid_State("No prefix"); }
148
149
0
      std::unique_ptr<EMSA> clone_emsa() const { return m_emsa->new_object(); }
150
151
   private:
152
153
      /**
154
      * Get the maximum message size in bits supported by this public key.
155
      * @return maximum message in bits
156
      */
157
      virtual size_t max_input_bits() const = 0;
158
159
      virtual secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
160
                                           RandomNumberGenerator& rng) = 0;
161
162
      std::unique_ptr<EMSA> m_emsa;
163
      const std::string m_hash;
164
      bool m_prefix_used;
165
   };
166
167
class Key_Agreement_with_KDF : public Key_Agreement
168
   {
169
   public:
170
      secure_vector<uint8_t> agree(size_t key_len,
171
                                const uint8_t other_key[], size_t other_key_len,
172
                                const uint8_t salt[], size_t salt_len) override;
173
174
   protected:
175
      explicit Key_Agreement_with_KDF(const std::string& kdf);
176
13.6k
      ~Key_Agreement_with_KDF() = default;
177
   private:
178
      virtual secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) = 0;
179
      std::unique_ptr<KDF> m_kdf;
180
   };
181
182
class KEM_Encryption_with_KDF : public KEM_Encryption
183
   {
184
   public:
185
      void kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
186
                       secure_vector<uint8_t>& out_shared_key,
187
                       size_t desired_shared_key_len,
188
                       Botan::RandomNumberGenerator& rng,
189
                       const uint8_t salt[],
190
                       size_t salt_len) override;
191
192
   protected:
193
      virtual void raw_kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
194
                                   secure_vector<uint8_t>& raw_shared_key,
195
                                   Botan::RandomNumberGenerator& rng) = 0;
196
197
      explicit KEM_Encryption_with_KDF(const std::string& kdf);
198
0
      ~KEM_Encryption_with_KDF() = default;
199
   private:
200
      std::unique_ptr<KDF> m_kdf;
201
   };
202
203
class KEM_Decryption_with_KDF : public KEM_Decryption
204
   {
205
   public:
206
      secure_vector<uint8_t> kem_decrypt(const uint8_t encap_key[],
207
                                      size_t len,
208
                                      size_t desired_shared_key_len,
209
                                      const uint8_t salt[],
210
                                      size_t salt_len) override;
211
212
   protected:
213
      virtual secure_vector<uint8_t>
214
      raw_kem_decrypt(const uint8_t encap_key[], size_t len) = 0;
215
216
      explicit KEM_Decryption_with_KDF(const std::string& kdf);
217
0
      ~KEM_Decryption_with_KDF() = default;
218
   private:
219
      std::unique_ptr<KDF> m_kdf;
220
   };
221
222
}
223
224
}
225
226
#endif