Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
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 ~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 ~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
54 class Verification_with_EMSA : public Verification
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> m_emsa;
121
122 private:
123 const std::string m_hash;
124 bool m_prefix_used;
125 };
126
127 class Signature_with_EMSA : public Signature
128 {
129 public:
130 void update(const uint8_t msg[], size_t msg_len) override;
131
132 secure_vector<uint8_t> sign(RandomNumberGenerator& rng) override;
133 protected:
134 explicit Signature_with_EMSA(const std::string& emsa);
135 ~Signature_with_EMSA() = default;
136
137 std::string hash_for_signature() { return m_hash; }
138
139 /**
140 * @return boolean specifying if this signature scheme uses
141 * a message prefix returned by message_prefix()
142 */
143 virtual bool has_prefix() { return false; }
144
145 /**
146 * @return the message prefix if this signature scheme uses
147 * a message prefix, signaled via has_prefix()
148 */
149 virtual secure_vector<uint8_t> message_prefix() const { throw Exception( "No prefix" ); }
150
151 std::unique_ptr<EMSA> m_emsa;
152 private:
153
154 /**
155 * Get the maximum message size in bits supported by this public key.
156 * @return maximum message in bits
157 */
158 virtual size_t max_input_bits() const = 0;
159
160 bool self_test_signature(const std::vector<uint8_t>& msg,
161 const std::vector<uint8_t>& sig) const;
162
163 virtual secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
164 RandomNumberGenerator& rng) = 0;
165
166 const std::string m_hash;
167 bool m_prefix_used;
168 };
169
170 class Key_Agreement_with_KDF : public Key_Agreement
171 {
172 public:
173 secure_vector<uint8_t> agree(size_t key_len,
174 const uint8_t other_key[], size_t other_key_len,
175 const uint8_t salt[], size_t salt_len) override;
176
177 protected:
178 explicit Key_Agreement_with_KDF(const std::string& kdf);
179 ~Key_Agreement_with_KDF() = default;
180 private:
181 virtual secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) = 0;
182 std::unique_ptr<KDF> m_kdf;
183 };
184
185 class KEM_Encryption_with_KDF : public KEM_Encryption
186 {
187 public:
188 void kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
189 secure_vector<uint8_t>& out_shared_key,
190 size_t desired_shared_key_len,
191 Botan::RandomNumberGenerator& rng,
192 const uint8_t salt[],
193 size_t salt_len) override;
194
195 protected:
196 virtual void raw_kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
197 secure_vector<uint8_t>& raw_shared_key,
198 Botan::RandomNumberGenerator& rng) = 0;
199
200 explicit KEM_Encryption_with_KDF(const std::string& kdf);
201 ~KEM_Encryption_with_KDF() = default;
202 private:
203 std::unique_ptr<KDF> m_kdf;
204 };
205
206 class KEM_Decryption_with_KDF : public KEM_Decryption
207 {
208 public:
209 secure_vector<uint8_t> kem_decrypt(const uint8_t encap_key[],
210 size_t len,
211 size_t desired_shared_key_len,
212 const uint8_t salt[],
213 size_t salt_len) override;
214
215 protected:
216 virtual secure_vector<uint8_t>
217 raw_kem_decrypt(const uint8_t encap_key[], size_t len) = 0;
218
219 explicit KEM_Decryption_with_KDF(const std::string& kdf);
220 ~KEM_Decryption_with_KDF() = default;
221 private:
222 std::unique_ptr<KDF> m_kdf;
223 };
224
225 }
226
227 }
228
229 #endif
Botan::PK_Ops::Encryption_with_EME::encrypt
secure_vector< uint8_t > encrypt(const uint8_t msg[], size_t msg_len, RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:25
Botan::PK_Ops::Verification_with_EMSA::update
void update(const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:104
Botan::PK_Ops::Signature_with_EMSA::~Signature_with_EMSA
~Signature_with_EMSA()=default
Botan::Exception
Definition: exceptn.h:21
Botan::PK_Ops::KEM_Encryption_with_KDF::raw_kem_encrypt
virtual void raw_kem_encrypt(secure_vector< uint8_t > &out_encapsulated_key, secure_vector< uint8_t > &raw_shared_key, Botan::RandomNumberGenerator &rng)=0
Botan::PK_Ops::KEM_Decryption_with_KDF::kem_decrypt
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:154
Botan::PK_Ops::Key_Agreement_with_KDF::Key_Agreement_with_KDF
Key_Agreement_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:49
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::PK_Ops::KEM_Encryption_with_KDF
Definition: pk_ops_impl.h:185
Botan::PK_Ops::KEM_Decryption_with_KDF::~KEM_Decryption_with_KDF
~KEM_Decryption_with_KDF()=default
Botan::PK_Ops::Encryption_with_EME::Encryption_with_EME
Encryption_with_EME(const std::string &eme)
Definition: pk_ops.cpp:13
Botan::PK_Ops::KEM_Decryption_with_KDF::KEM_Decryption_with_KDF
KEM_Decryption_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:167
Botan::PK_Ops::Verification_with_EMSA::do_check
bool do_check(const secure_vector< uint8_t > &msg, const uint8_t sig[], size_t sig_len)
Botan::PK_Ops::Verification_with_EMSA::has_prefix
virtual bool has_prefix()
Definition: pk_ops_impl.h:80
Botan::PK_Ops::Verification_with_EMSA::verify
virtual bool verify(const uint8_t[], size_t, const uint8_t[], size_t)
Definition: pk_ops_impl.h:102
Botan::PK_Ops::Signature
Definition: pk_ops.h:86
Botan::Invalid_State
Definition: exceptn.h:61
Botan::PK_Ops::Verification_with_EMSA::message_prefix
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:86
Botan::PK_Ops::Decryption_with_EME::Decryption_with_EME
Decryption_with_EME(const std::string &eme)
Definition: pk_ops.cpp:33
Botan::PK_Ops::Signature_with_EMSA::Signature_with_EMSA
Signature_with_EMSA(const std::string &emsa)
Definition: pk_ops.cpp:65
Botan::PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF
KEM_Encryption_with_KDF(const std::string &kdf)
Definition: pk_ops.cpp:148
Botan::PK_Ops::KEM_Encryption_with_KDF::~KEM_Encryption_with_KDF
~KEM_Encryption_with_KDF()=default
Botan::PK_Ops::KEM_Decryption
Definition: pk_ops.h:134
Botan::PK_Ops::Key_Agreement_with_KDF::~Key_Agreement_with_KDF
~Key_Agreement_with_KDF()=default
Botan::PK_Ops::KEM_Encryption
Definition: pk_ops.h:121
Botan::PK_Ops::Key_Agreement_with_KDF
Definition: pk_ops_impl.h:170
Botan::PK_Ops::Verification_with_EMSA::m_emsa
std::unique_ptr< EMSA > m_emsa
Definition: pk_ops_impl.h:120
Botan::PK_Ops::Signature_with_EMSA::has_prefix
virtual bool has_prefix()
Definition: pk_ops_impl.h:143
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::PK_Ops::KEM_Encryption_with_KDF::kem_encrypt
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:133
Botan::PK_Ops::KEM_Decryption_with_KDF::raw_kem_decrypt
virtual secure_vector< uint8_t > raw_kem_decrypt(const uint8_t encap_key[], size_t len)=0
Botan::PK_Ops::Verification_with_EMSA::max_input_bits
virtual size_t max_input_bits() const =0
Botan::PK_Ops::Signature_with_EMSA::sign
secure_vector< uint8_t > sign(RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:86
Botan::PK_Ops::Verification_with_EMSA::~Verification_with_EMSA
~Verification_with_EMSA()=default
Botan::PK_Ops::Encryption_with_EME::max_input_bits
size_t max_input_bits() const override
Definition: pk_ops.cpp:20
Botan::PK_Ops::Verification_with_EMSA::Verification_with_EMSA
Verification_with_EMSA(const std::string &emsa)
Definition: pk_ops.cpp:94
Botan::PK_Ops::Signature_with_EMSA::hash_for_signature
std::string hash_for_signature()
Definition: pk_ops_impl.h:137
Botan::PK_Ops::Signature_with_EMSA
Definition: pk_ops_impl.h:127
Botan
Definition: alg_id.cpp:13
Botan::PK_Ops::Encryption_with_EME
Definition: pk_ops_impl.h:20
Botan::PK_Ops::Signature_with_EMSA::message_prefix
virtual secure_vector< uint8_t > message_prefix() const
Definition: pk_ops_impl.h:149
Botan::PK_Ops::Signature_with_EMSA::update
void update(const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:75
Botan::PK_Ops::Key_Agreement
Definition: pk_ops.h:108
Botan::PK_Ops::Verification_with_EMSA::with_recovery
virtual bool with_recovery() const =0
Botan::PK_Ops::Verification_with_EMSA::is_valid_signature
bool is_valid_signature(const uint8_t sig[], size_t sig_len) override
Definition: pk_ops.cpp:115
Botan::PK_Ops::Verification_with_EMSA
Definition: pk_ops_impl.h:54
Botan::PK_Ops::Signature_with_EMSA::m_emsa
std::unique_ptr< EMSA > m_emsa
Definition: pk_ops_impl.h:151
Botan::PK_Ops::Verification_with_EMSA::hash_for_signature
std::string hash_for_signature()
Definition: pk_ops_impl.h:65
Botan::PK_Ops::Verification_with_EMSA::verify_mr
virtual secure_vector< uint8_t > verify_mr(const uint8_t[], size_t)
Definition: pk_ops_impl.h:115
Botan::PK_Ops::Decryption
Definition: pk_ops.h:51
Botan::PK_Ops::Encryption
Definition: pk_ops.h:36
Botan::PK_Ops::Encryption_with_EME::~Encryption_with_EME
~Encryption_with_EME()=default
Botan::PK_Ops::Verification
Definition: pk_ops.h:64
Botan::PK_Ops::Decryption_with_EME::decrypt
secure_vector< uint8_t > decrypt(uint8_t &valid_mask, const uint8_t msg[], size_t msg_len) override
Definition: pk_ops.cpp:41
Botan::PK_Ops::Decryption_with_EME::~Decryption_with_EME
~Decryption_with_EME()=default
Botan::PK_Ops::Decryption_with_EME
Definition: pk_ops_impl.h:39
Botan::PK_Ops::KEM_Decryption_with_KDF
Definition: pk_ops_impl.h:206
Botan::PK_Ops::Key_Agreement_with_KDF::agree
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:55