monotone

monotone Mtn Source Tree

Root/botan/pubkey.cpp

1/*************************************************
2* Public Key Base Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/pubkey.h>
7#include <botan/parsing.h>
8#include <botan/bit_ops.h>
9#include <botan/lookup.h>
10#include <botan/asn1.h>
11#include <memory>
12
13namespace Botan {
14
15/*************************************************
16* Encrypt a message *
17*************************************************/
18SecureVector<byte> PK_Encryptor::encrypt(const byte in[], u32bit len) const
19 {
20 return enc(in, len);
21 }
22
23/*************************************************
24* Encrypt a message *
25*************************************************/
26SecureVector<byte> PK_Encryptor::encrypt(const MemoryRegion<byte>& in) const
27 {
28 return enc(in.begin(), in.size());
29 }
30
31/*************************************************
32* Decrypt a message *
33*************************************************/
34SecureVector<byte> PK_Decryptor::decrypt(const byte in[], u32bit len) const
35 {
36 return dec(in, len);
37 }
38
39/*************************************************
40* Decrypt a message *
41*************************************************/
42SecureVector<byte> PK_Decryptor::decrypt(const MemoryRegion<byte>& in) const
43 {
44 return dec(in.begin(), in.size());
45 }
46
47/*************************************************
48* PK_Encryptor_MR_with_EME Constructor *
49*************************************************/
50PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& k,
51 const std::string& eme) :
52 key(k), encoder((eme == "Raw") ? 0 : get_eme(eme))
53 {
54 }
55
56/*************************************************
57* Encrypt a message *
58*************************************************/
59SecureVector<byte> PK_Encryptor_MR_with_EME::enc(const byte msg[],
60 u32bit length) const
61 {
62 SecureVector<byte> message;
63 if(encoder) message = encoder->encode(msg, length, key.max_input_bits());
64 else message.set(msg, length);
65
66 if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits())
67 throw Exception("PK_Encryptor_MR_with_EME: Input is too large");
68
69 return key.encrypt(message, message.size());
70 }
71
72/*************************************************
73* Return the max size, in bytes, of a message *
74*************************************************/
75u32bit PK_Encryptor_MR_with_EME::maximum_input_size() const
76 {
77 if(!encoder)
78 return (key.max_input_bits() / 8);
79 else
80 return encoder->maximum_input_size(key.max_input_bits());
81 }
82
83/*************************************************
84* PK_Decryptor_MR_with_EME Constructor *
85*************************************************/
86PK_Decryptor_MR_with_EME::PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& k,
87 const std::string& eme) :
88 key(k), encoder((eme == "Raw") ? 0 : get_eme(eme))
89 {
90 }
91
92/*************************************************
93* Decrypt a message *
94*************************************************/
95SecureVector<byte> PK_Decryptor_MR_with_EME::dec(const byte msg[],
96 u32bit length) const
97 {
98 try {
99 SecureVector<byte> decrypted = key.decrypt(msg, length);
100 if(encoder)
101 return encoder->decode(decrypted, key.max_input_bits());
102 else
103 return decrypted;
104 }
105 catch(Invalid_Argument)
106 {
107 throw Exception("PK_Decryptor_MR_with_EME: Input is invalid");
108 }
109 catch(Decoding_Error)
110 {
111 throw Exception("PK_Decryptor_MR_with_EME: Input is invalid");
112 }
113 }
114
115/*************************************************
116* PK_Signer Constructor *
117*************************************************/
118PK_Signer::PK_Signer(const PK_Signing_Key& k, const std::string& emsa_name) :
119 key(k), emsa(get_emsa(emsa_name))
120 {
121 sig_format = IEEE_1363;
122 }
123
124/*************************************************
125* Set the signature format *
126*************************************************/
127void PK_Signer::set_output_format(Signature_Format format)
128 {
129 if(key.message_parts() == 1 && format != IEEE_1363)
130 throw Invalid_State("PK_Signer: Cannot set the output format for " +
131 key.algo_name() + " keys");
132 sig_format = format;
133 }
134
135/*************************************************
136* Sign a message *
137*************************************************/
138SecureVector<byte> PK_Signer::sign_message(const byte msg[], u32bit length)
139 {
140 update(msg, length);
141 return signature();
142 }
143
144/*************************************************
145* Sign a message *
146*************************************************/
147SecureVector<byte> PK_Signer::sign_message(const MemoryRegion<byte>& msg)
148 {
149 return sign_message(msg, msg.size());
150 }
151
152/*************************************************
153* Add more to the message to be signed *
154*************************************************/
155void PK_Signer::update(const byte in[], u32bit length)
156 {
157 emsa->update(in, length);
158 }
159
160/*************************************************
161* Add more to the message to be signed *
162*************************************************/
163void PK_Signer::update(byte in)
164 {
165 update(&in, 1);
166 }
167
168/*************************************************
169* Add more to the message to be signed *
170*************************************************/
171void PK_Signer::update(const MemoryRegion<byte>& in)
172 {
173 update(in, in.size());
174 }
175
176/*************************************************
177* Create a signature *
178*************************************************/
179SecureVector<byte> PK_Signer::signature()
180 {
181 SecureVector<byte> encoded = emsa->encoding_of(emsa->raw_data(),
182 key.max_input_bits());
183 SecureVector<byte> plain_sig = key.sign(encoded, encoded.size());
184
185 if(key.message_parts() == 1 || sig_format == IEEE_1363)
186 return plain_sig;
187
188 if(sig_format == DER_SEQUENCE)
189 {
190 if(plain_sig.size() % key.message_parts())
191 throw Encoding_Error("PK_Signer: strange signature size found");
192 const u32bit SIZE_OF_PART = plain_sig.size() / key.message_parts();
193
194 std::vector<BigInt> sig_parts(key.message_parts());
195 for(u32bit j = 0; j != sig_parts.size(); j++)
196 sig_parts[j].binary_decode(plain_sig + SIZE_OF_PART*j, SIZE_OF_PART);
197
198 DER_Encoder der_sig;
199 der_sig.start_sequence();
200 for(u32bit j = 0; j != sig_parts.size(); j++)
201 DER::encode(der_sig, sig_parts[j]);
202 der_sig.end_sequence();
203
204 return der_sig.get_contents();
205 }
206 else
207 throw Encoding_Error("PK_Signer: Unknown signature format " +
208 to_string(sig_format));
209 }
210
211/*************************************************
212* PK_Verifier Constructor *
213*************************************************/
214PK_Verifier::PK_Verifier(const PK_Key& k, const std::string& emsa_name) :
215 emsa(get_emsa(emsa_name)), key(k)
216 {
217 sig_format = IEEE_1363;
218 }
219
220/*************************************************
221* Set the signature format *
222*************************************************/
223void PK_Verifier::set_input_format(Signature_Format format)
224 {
225 if(key.message_parts() == 1 && format != IEEE_1363)
226 throw Invalid_State("PK_Verifier: This algorithm always uses IEEE 1363");
227 sig_format = format;
228 }
229
230/*************************************************
231* Verify a message *
232*************************************************/
233bool PK_Verifier::verify_message(const MemoryRegion<byte>& msg,
234 const MemoryRegion<byte>& sig)
235 {
236 return verify_message(msg, msg.size(), sig, sig.size());
237 }
238
239/*************************************************
240* Verify a message *
241*************************************************/
242bool PK_Verifier::verify_message(const byte msg[], u32bit msg_length,
243 const byte sig[], u32bit sig_length)
244 {
245 update(msg, msg_length);
246 return check_signature(sig, sig_length);
247 }
248
249/*************************************************
250* Append to the message *
251*************************************************/
252void PK_Verifier::update(const byte in[], u32bit length)
253 {
254 emsa->update(in, length);
255 }
256
257/*************************************************
258* Append to the message *
259*************************************************/
260void PK_Verifier::update(byte in)
261 {
262 update(&in, 1);
263 }
264
265/*************************************************
266* Append to the message *
267*************************************************/
268void PK_Verifier::update(const MemoryRegion<byte>& in)
269 {
270 update(in, in.size());
271 }
272
273/*************************************************
274* Check a signature *
275*************************************************/
276bool PK_Verifier::check_signature(const MemoryRegion<byte>& sig)
277 {
278 return check_signature(sig, sig.size());
279 }
280
281/*************************************************
282* Check a signature *
283*************************************************/
284bool PK_Verifier::check_signature(const byte sig[], u32bit length)
285 {
286 try {
287 if(sig_format == IEEE_1363)
288 return validate_signature(emsa->raw_data(), sig, length);
289 else if(sig_format == DER_SEQUENCE)
290 {
291 BER_Decoder decoder(sig, length);
292 BER_Decoder ber_sig = BER::get_subsequence(decoder);
293
294 u32bit count = 0;
295 SecureVector<byte> real_sig;
296 while(ber_sig.more_items())
297 {
298 BigInt sig_part;
299 BER::decode(ber_sig, sig_part);
300 real_sig.append(BigInt::encode_1363(sig_part,
301 key.message_part_size()));
302 count++;
303 }
304 if(count != key.message_parts())
305 throw Decoding_Error("PK_Verifier: signature size invalid");
306
307 return validate_signature(emsa->raw_data(),
308 real_sig, real_sig.size());
309 }
310 else
311 throw Decoding_Error("PK_Verifier: Unknown signature format " +
312 to_string(sig_format));
313 }
314 catch(Invalid_Argument) { return false; }
315 catch(Decoding_Error) { return false; }
316 }
317
318/*************************************************
319* PK_Verifier_with_MR Constructor *
320*************************************************/
321PK_Verifier_with_MR::PK_Verifier_with_MR(const PK_Verifying_with_MR_Key& k,
322 const std::string& emsa_name) :
323 PK_Verifier(k, emsa_name), key(k)
324 {
325 }
326
327/*************************************************
328* Verify a signature *
329*************************************************/
330bool PK_Verifier_with_MR::validate_signature(const MemoryRegion<byte>& msg,
331 const byte sig[], u32bit sig_len)
332 {
333 SecureVector<byte> output_of_key = key.verify(sig, sig_len);
334 return emsa->verify(output_of_key, msg, key.max_input_bits());
335 }
336
337/*************************************************
338* PK_Verifier_wo_MR Constructor *
339*************************************************/
340PK_Verifier_wo_MR::PK_Verifier_wo_MR(const PK_Verifying_wo_MR_Key& k,
341 const std::string& emsa_name) :
342 PK_Verifier(k, emsa_name), key(k)
343 {
344 }
345
346/*************************************************
347* Verify a signature *
348*************************************************/
349bool PK_Verifier_wo_MR::validate_signature(const MemoryRegion<byte>& msg,
350 const byte sig[], u32bit sig_len)
351 {
352 SecureVector<byte> encoded = emsa->encoding_of(msg, key.max_input_bits());
353 return key.verify(encoded, encoded.size(), sig, sig_len);
354 }
355
356/*************************************************
357* PK_Key_Agreement Constructor *
358*************************************************/
359PK_Key_Agreement::PK_Key_Agreement(const PK_Key_Agreement_Key& k,
360 const std::string& k_name) :
361 key(k), kdf_name(k_name)
362 {
363 }
364
365/*************************************************
366* Perform Key Agreement Operation *
367*************************************************/
368SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len,
369 const byte in[], u32bit in_len,
370 const std::string& params) const
371 {
372 return derive_key(key_len, in, in_len,
373 (const byte*)params.c_str(), params.length());
374 }
375
376/*************************************************
377* Perform Key Agreement Operation *
378*************************************************/
379SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len, const byte in[],
380 u32bit in_len, const byte params[],
381 u32bit params_len) const
382 {
383 std::auto_ptr<KDF> kdf((kdf_name == "Raw") ? 0 : get_kdf(kdf_name));
384 OctetString z = key.derive_key(in, in_len);
385
386 if(kdf.get())
387 z = kdf->derive_key(key_len, z.bits_of(), params, params_len);
388
389 return z;
390 }
391
392}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status