Botan  2.4.0
Crypto and TLS for C++11
pk_ops_impl.h
Go to the documentation of this file.
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/pk_ops.h>
12 #include <botan/eme.h>
13 #include <botan/kdf.h>
14 #include <botan/emsa.h>
15 
16 namespace Botan {
17 
18 namespace PK_Ops {
19 
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  ~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 
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  ~Decryption_with_EME() = default;
46  protected:
47  explicit Decryption_with_EME(const std::string& eme);
48  private:
49  virtual size_t max_raw_input_bits() const = 0;
50  virtual secure_vector<uint8_t> raw_decrypt(const uint8_t msg[], size_t len) = 0;
51  std::unique_ptr<EME> m_eme;
52  };
53 
55  {
56  public:
57  ~Verification_with_EMSA() = default;
58 
59  void update(const uint8_t msg[], size_t msg_len) override;
60  bool is_valid_signature(const uint8_t sig[], size_t sig_len) override;
61 
62  bool do_check(const secure_vector<uint8_t>& msg,
63  const uint8_t sig[], size_t sig_len);
64 
65  std::string hash_for_signature() { return m_hash; }
66 
67  protected:
68  explicit Verification_with_EMSA(const std::string& emsa);
69 
70  /**
71  * Get the maximum message size in bits supported by this public key.
72  * @return maximum message in bits
73  */
74  virtual size_t max_input_bits() const = 0;
75 
76  /**
77  * @return boolean specifying if this signature scheme uses
78  * a message prefix returned by message_prefix()
79  */
80  virtual bool has_prefix() { return false; }
81 
82  /**
83  * @return the message prefix if this signature scheme uses
84  * a message prefix, signaled via has_prefix()
85  */
86  virtual secure_vector<uint8_t> message_prefix() const { throw Exception( "No prefix" ); }
87 
88  /**
89  * @return boolean specifying if this key type supports message
90  * recovery and thus if you need to call verify() or verify_mr()
91  */
92  virtual bool with_recovery() const = 0;
93 
94  /*
95  * Perform a signature check operation
96  * @param msg the message
97  * @param msg_len the length of msg in bytes
98  * @param sig the signature
99  * @param sig_len the length of sig in bytes
100  * @returns if signature is a valid one for message
101  */
102  virtual bool verify(const uint8_t[], size_t,
103  const uint8_t[], size_t)
104  {
105  throw Invalid_State("Message recovery required");
106  }
107 
108  /*
109  * Perform a signature operation (with message recovery)
110  * Only call this if with_recovery() returns true
111  * @param msg the message
112  * @param msg_len the length of msg in bytes
113  * @returns recovered message
114  */
115  virtual secure_vector<uint8_t> verify_mr(const uint8_t[], size_t)
116  {
117  throw Invalid_State("Message recovery not supported");
118  }
119 
120  std::unique_ptr<EMSA> clone_emsa() const { return std::unique_ptr<EMSA>(m_emsa->clone()); }
121 
122  private:
123  std::unique_ptr<EMSA> m_emsa;
124  const std::string m_hash;
125  bool m_prefix_used;
126  };
127 
129  {
130  public:
131  void update(const uint8_t msg[], size_t msg_len) override;
132 
133  secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
134  protected:
135  explicit Signature_with_EMSA(const std::string& emsa);
136  ~Signature_with_EMSA() = default;
137 
138  std::string hash_for_signature() { return m_hash; }
139 
140  /**
141  * @return boolean specifying if this signature scheme uses
142  * a message prefix returned by message_prefix()
143  */
144  virtual bool has_prefix() { return false; }
145 
146  /**
147  * @return the message prefix if this signature scheme uses
148  * a message prefix, signaled via has_prefix()
149  */
150  virtual secure_vector<uint8_t> message_prefix() const { throw Exception( "No prefix" ); }
151 
152  std::unique_ptr<EMSA> clone_emsa() const { return std::unique_ptr<EMSA>(m_emsa->clone()); }
153 
154  private:
155 
156  /**
157  * Get the maximum message size in bits supported by this public key.
158  * @return maximum message in bits
159  */
160  virtual size_t max_input_bits() const = 0;
161 
162  bool self_test_signature(const std::vector<uint8_t>& msg,
163  const std::vector<uint8_t>& sig) const;
164 
165  virtual secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
166  RandomNumberGenerator& rng) = 0;
167 
168  std::unique_ptr<EMSA> m_emsa;
169  const std::string m_hash;
170  bool m_prefix_used;
171  };
172 
174  {
175  public:
176  secure_vector<uint8_t> agree(size_t key_len,
177  const uint8_t other_key[], size_t other_key_len,
178  const uint8_t salt[], size_t salt_len) override;
179 
180  protected:
181  explicit Key_Agreement_with_KDF(const std::string& kdf);
182  ~Key_Agreement_with_KDF() = default;
183  private:
184  virtual secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) = 0;
185  std::unique_ptr<KDF> m_kdf;
186  };
187 
189  {
190  public:
191  void kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
192  secure_vector<uint8_t>& out_shared_key,
193  size_t desired_shared_key_len,
195  const uint8_t salt[],
196  size_t salt_len) override;
197 
198  protected:
199  virtual void raw_kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
200  secure_vector<uint8_t>& raw_shared_key,
202 
203  explicit KEM_Encryption_with_KDF(const std::string& kdf);
204  ~KEM_Encryption_with_KDF() = default;
205  private:
206  std::unique_ptr<KDF> m_kdf;
207  };
208 
210  {
211  public:
212  secure_vector<uint8_t> kem_decrypt(const uint8_t encap_key[],
213  size_t len,
214  size_t desired_shared_key_len,
215  const uint8_t salt[],
216  size_t salt_len) override;
217 
218  protected:
219  virtual secure_vector<uint8_t>
220  raw_kem_decrypt(const uint8_t encap_key[], size_t len) = 0;
221 
222  explicit KEM_Decryption_with_KDF(const std::string& kdf);
223  ~KEM_Decryption_with_KDF() = default;
224  private:
225  std::unique_ptr<KDF> m_kdf;
226  };
227 
228 }
229 
230 }
231 
232 #endif
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:86
secure_vector< uint8_t > encrypt(const uint8_t msg[], size_t msg_len, RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:26
Encryption_with_EME(const std::string &eme)
Definition: pk_ops.cpp:14
std::unique_ptr< EMSA > clone_emsa() const
Definition: pk_ops_impl.h:152
virtual bool verify(const uint8_t[], size_t, const uint8_t[], size_t)
Definition: pk_ops_impl.h:102
std::unique_ptr< EMSA > clone_emsa() const
Definition: pk_ops_impl.h:120
std::string decrypt(const uint8_t input[], size_t input_len, const std::string &passphrase)
Definition: cryptobox.cpp:162
size_t max_input_bits() const override
Definition: pk_ops.cpp:21
size_t salt_len
Definition: x509_obj.cpp:25
Definition: alg_id.cpp:13
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:150
virtual secure_vector< uint8_t > verify_mr(const uint8_t[], size_t)
Definition: pk_ops_impl.h:115