Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

pk_ops.cpp
Go to the documentation of this file.
1 /*
2 * PK Operation Types
3 * (C) 2010,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/internal/pk_ops_impl.h>
9 #include <botan/internal/bit_ops.h>
10
11 namespace Botan {
12
13 PK_Ops::Encryption_with_EME::Encryption_with_EME(const std::string& eme)
14 {
15 m_eme.reset(get_eme(eme));
16 if(!m_eme.get())
17 throw Algorithm_Not_Found(eme);
18 }
19
20 size_t PK_Ops::Encryption_with_EME::max_input_bits() const
21 {
22 return m_eme->maximum_input_size(max_raw_input_bits());
23 }
24
25 secure_vector<uint8_t> PK_Ops::Encryption_with_EME::encrypt(const uint8_t msg[], size_t msg_len,
26 RandomNumberGenerator& rng)
27 {
28 const size_t max_raw = max_raw_input_bits();
29 const std::vector<uint8_t> encoded = unlock(m_eme->encode(msg, msg_len, max_raw, rng));
30 return raw_encrypt(encoded.data(), encoded.size(), rng);
31 }
32
33 PK_Ops::Decryption_with_EME::Decryption_with_EME(const std::string& eme)
34 {
35 m_eme.reset(get_eme(eme));
36 if(!m_eme.get())
37 throw Algorithm_Not_Found(eme);
38 }
39
40 secure_vector<uint8_t>
41 PK_Ops::Decryption_with_EME::decrypt(uint8_t& valid_mask,
42 const uint8_t ciphertext[],
43 size_t ciphertext_len)
44 {
45 const secure_vector<uint8_t> raw = raw_decrypt(ciphertext, ciphertext_len);
46 return m_eme->unpad(valid_mask, raw.data(), raw.size());
47 }
48
49 PK_Ops::Key_Agreement_with_KDF::Key_Agreement_with_KDF(const std::string& kdf)
50 {
51 if(kdf != "Raw")
52 m_kdf.reset(get_kdf(kdf));
53 }
54
55 secure_vector<uint8_t> PK_Ops::Key_Agreement_with_KDF::agree(size_t key_len,
56 const uint8_t w[], size_t w_len,
57 const uint8_t salt[], size_t salt_len)
58 {
59 secure_vector<uint8_t> z = raw_agree(w, w_len);
60 if(m_kdf)
61 return m_kdf->derive_key(key_len, z, salt, salt_len);
62 return z;
63 }
64
65 PK_Ops::Signature_with_EMSA::Signature_with_EMSA(const std::string& emsa) :
66 Signature(),
67 m_emsa(get_emsa(emsa)),
68 m_hash(hash_for_emsa(emsa)),
69 m_prefix_used(false)
70 {
71 if(!m_emsa)
72 throw Algorithm_Not_Found(emsa);
73 }
74
75 void PK_Ops::Signature_with_EMSA::update(const uint8_t msg[], size_t msg_len)
76 {
77 if(has_prefix() && !m_prefix_used)
78 {
79 m_prefix_used = true;
80 secure_vector<uint8_t> prefix = message_prefix();
81 m_emsa->update(prefix.data(), prefix.size());
82 }
83 m_emsa->update(msg, msg_len);
84 }
85
86 secure_vector<uint8_t> PK_Ops::Signature_with_EMSA::sign(RandomNumberGenerator& rng)
87 {
88 m_prefix_used = false;
89 const secure_vector<uint8_t> msg = m_emsa->raw_data();
90 const auto padded = m_emsa->encoding_of(msg, this->max_input_bits(), rng);
91 return raw_sign(padded.data(), padded.size(), rng);
92 }
93
94 PK_Ops::Verification_with_EMSA::Verification_with_EMSA(const std::string& emsa) :
95 Verification(),
96 m_emsa(get_emsa(emsa)),
97 m_hash(hash_for_emsa(emsa)),
98 m_prefix_used(false)
99 {
100 if(!m_emsa)
101 throw Algorithm_Not_Found(emsa);
102 }
103
104 void PK_Ops::Verification_with_EMSA::update(const uint8_t msg[], size_t msg_len)
105 {
106 if(has_prefix() && !m_prefix_used)
107 {
108 m_prefix_used = true;
109 secure_vector<uint8_t> prefix = message_prefix();
110 m_emsa->update(prefix.data(), prefix.size());
111 }
112 m_emsa->update(msg, msg_len);
113 }
114
115 bool PK_Ops::Verification_with_EMSA::is_valid_signature(const uint8_t sig[], size_t sig_len)
116 {
117 m_prefix_used = false;
118 const secure_vector<uint8_t> msg = m_emsa->raw_data();
119
120 if(with_recovery())
121 {
122 secure_vector<uint8_t> output_of_key = verify_mr(sig, sig_len);
123 return m_emsa->verify(output_of_key, msg, max_input_bits());
124 }
125 else
126 {
127 Null_RNG rng;
128 secure_vector<uint8_t> encoded = m_emsa->encoding_of(msg, max_input_bits(), rng);
129 return verify(encoded.data(), encoded.size(), sig, sig_len);
130 }
131 }
132
133 void PK_Ops::KEM_Encryption_with_KDF::kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
134 secure_vector<uint8_t>& out_shared_key,
135 size_t desired_shared_key_len,
136 Botan::RandomNumberGenerator& rng,
137 const uint8_t salt[],
138 size_t salt_len)
139 {
140 secure_vector<uint8_t> raw_shared;
141 this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
142
143 out_shared_key = m_kdf->derive_key(desired_shared_key_len,
144 raw_shared.data(), raw_shared.size(),
145 salt, salt_len);
146 }
147
148 PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF(const std::string& kdf)
149 {
150 m_kdf.reset(get_kdf(kdf));
151 }
152
153 secure_vector<uint8_t>
154 PK_Ops::KEM_Decryption_with_KDF::kem_decrypt(const uint8_t encap_key[],
155 size_t len,
156 size_t desired_shared_key_len,
157 const uint8_t salt[],
158 size_t salt_len)
159 {
160 secure_vector<uint8_t> raw_shared = this->raw_kem_decrypt(encap_key, len);
161
162 return m_kdf->derive_key(desired_shared_key_len,
163 raw_shared.data(), raw_shared.size(),
164 salt, salt_len);
165 }
166
167 PK_Ops::KEM_Decryption_with_KDF::KEM_Decryption_with_KDF(const std::string& kdf)
168 {
169 m_kdf.reset(get_kdf(kdf));
170 }
171
172 }
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::hash_for_emsa
std::string hash_for_emsa(const std::string &algo_spec)
Definition: emsa.cpp:142
Botan::get_emsa
EMSA * get_emsa(const std::string &algo_spec)
Definition: emsa.cpp:36
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::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::Signature
Definition: pk_ops.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::Verification_with_EMSA::m_emsa
std::unique_ptr< EMSA > m_emsa
Definition: pk_ops_impl.h:120
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
m_emsa
std::string m_emsa
Definition: dsa.cpp:100
Botan::PK_Ops::Signature_with_EMSA::sign
secure_vector< uint8_t > sign(RandomNumberGenerator &rng) override
Definition: pk_ops.cpp:86
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
Definition: alg_id.cpp:13
Botan::Null_RNG
Definition: rng.h:188
Botan::get_eme
EME * get_eme(const std::string &algo_spec)
Definition: eme.cpp:25
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::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::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
Botan::PK_Ops::Signature_with_EMSA::m_emsa
std::unique_ptr< EMSA > m_emsa
Definition: pk_ops_impl.h:151
Botan::get_kdf
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:226
m_hash
std::unique_ptr< HashFunction > m_hash
Definition: tpm.cpp:439
Botan::Algorithm_Not_Found
Definition: exceptn.h:140
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::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