Botan 3.0.0-alpha0
Crypto and TLS for C&
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/internal/pk_ops.h>
12#include <botan/internal/eme.h>
13#include <botan/kdf.h>
14#include <botan/internal/emsa.h>
15
16namespace Botan {
17
18namespace 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
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
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
54 {
55 public:
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 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 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 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 {
101 throw Invalid_State("Message recovery required");
102 }
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 {
113 throw Invalid_State("Message recovery not supported");
114 }
115
116 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
125 {
126 public:
127 void update(const uint8_t msg[], size_t msg_len) override;
128
130 protected:
131 explicit Signature_with_EMSA(const std::string& emsa, bool with_message_recovery = false);
132
134
135 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 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 virtual secure_vector<uint8_t> message_prefix() const { throw Invalid_State("No prefix"); }
148
149 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
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);
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
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,
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,
196
197 explicit KEM_Encryption_with_KDF(const std::string& kdf);
199 private:
200 std::unique_ptr<KDF> m_kdf;
201 };
202
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:
214 raw_kem_decrypt(const uint8_t encap_key[], size_t len) = 0;
215
216 explicit KEM_Decryption_with_KDF(const std::string& kdf);
218 private:
219 std::unique_ptr<KDF> m_kdf;
220 };
221
222}
223
224}
225
226#endif
secure_vector< uint8_t > decrypt(uint8_t &valid_mask, const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:38
Decryption_with_EME(const std::string &eme)
Definition: pk_ops.cpp:32
size_t max_input_bits() const override
Definition: pk_ops.cpp:19
secure_vector< uint8_t > encrypt(const uint8_t msg[], size_t msg_len, RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:24
Encryption_with_EME(const std::string &eme)
Definition: pk_ops.cpp:14
virtual secure_vector< uint8_t > raw_kem_decrypt(const uint8_t encap_key[], size_t len)=0
KEM_Decryption_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:175
secure_vector< uint8_t > kem_decrypt(const uint8_t encap_key[], size_t len, size_t desired_shared_key_len, const uint8_t salt[], size_t salt_len) override
Definition: pk_ops.cpp:160
void kem_encrypt(secure_vector< uint8_t > &out_encapsulated_key, secure_vector< uint8_t > &out_shared_key, size_t desired_shared_key_len, Botan::RandomNumberGenerator &rng, const uint8_t salt[], size_t salt_len) override
Definition: pk_ops.cpp:136
KEM_Encryption_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:153
virtual void raw_kem_encrypt(secure_vector< uint8_t > &out_encapsulated_key, secure_vector< uint8_t > &raw_shared_key, Botan::RandomNumberGenerator &rng)=0
Key_Agreement_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:46
secure_vector< uint8_t > agree(size_t key_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[], size_t salt_len) override
Definition: pk_ops.cpp:52
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:147
void update(const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:75
Signature_with_EMSA(const std::string &emsa, bool with_message_recovery=false)
Definition: pk_ops.cpp:62
secure_vector< uint8_t > sign(RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:86
std::unique_ptr< EMSA > clone_emsa() const
Definition: pk_ops_impl.h:149
void update(const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:107
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:82
std::unique_ptr< EMSA > clone_emsa() const
Definition: pk_ops_impl.h:116
virtual bool with_recovery() const =0
bool is_valid_signature(const uint8_t sig[], size_t sig_len) override
Definition: pk_ops.cpp:118
Verification_with_EMSA(const std::string &emsa, bool has_message_recovery=false)
Definition: pk_ops.cpp:94
virtual size_t max_input_bits() const =0
virtual secure_vector< uint8_t > verify_mr(const uint8_t[], size_t)
Definition: pk_ops_impl.h:111
virtual bool verify(const uint8_t[], size_t, const uint8_t[], size_t)
Definition: pk_ops_impl.h:98
Definition: alg_id.cpp:13
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
size_t salt_len
Definition: x509_obj.cpp:25