Botan  2.4.0
Crypto and TLS for C++11
eme.cpp
Go to the documentation of this file.
1 /*
2 * EME Base Class
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/eme.h>
9 #include <botan/scan_name.h>
10 #include <botan/exceptn.h>
11 
12 #if defined(BOTAN_HAS_EME_OAEP)
13 #include <botan/oaep.h>
14 #endif
15 
16 #if defined(BOTAN_HAS_EME_PKCS1v15)
17 #include <botan/eme_pkcs.h>
18 #endif
19 
20 #if defined(BOTAN_HAS_EME_RAW)
21 #include <botan/eme_raw.h>
22 #endif
23 
24 namespace Botan {
25 
26 EME* get_eme(const std::string& algo_spec)
27  {
28 #if defined(BOTAN_HAS_EME_RAW)
29  if(algo_spec == "Raw")
30  return new EME_Raw;
31 #endif
32 
33 #if defined(BOTAN_HAS_EME_PKCS1v15)
34  if(algo_spec == "PKCS1v15" || algo_spec == "EME-PKCS1-v1_5")
35  return new EME_PKCS1v15;
36 #endif
37 
38 #if defined(BOTAN_HAS_EME_OAEP)
39  SCAN_Name req(algo_spec);
40 
41  if(req.algo_name() == "OAEP" ||
42  req.algo_name() == "EME-OAEP" ||
43  req.algo_name() == "EME1")
44  {
45  if(req.arg_count() == 1 ||
46  (req.arg_count() == 2 && req.arg(1) == "MGF1"))
47  {
48  if(auto hash = HashFunction::create(req.arg(0)))
49  return new OAEP(hash.release());
50  }
51  }
52 #endif
53 
54  throw Algorithm_Not_Found(algo_spec);
55  }
56 
57 /*
58 * Encode a message
59 */
60 secure_vector<uint8_t> EME::encode(const uint8_t msg[], size_t msg_len,
61  size_t key_bits,
62  RandomNumberGenerator& rng) const
63  {
64  return pad(msg, msg_len, key_bits, rng);
65  }
66 
67 /*
68 * Encode a message
69 */
71  size_t key_bits,
72  RandomNumberGenerator& rng) const
73  {
74  return pad(msg.data(), msg.size(), key_bits, rng);
75  }
76 
77 
78 }
EME * get_eme(const std::string &algo_spec)
Definition: eme.cpp:26
size_t arg_count() const
Definition: scan_name.h:49
virtual secure_vector< uint8_t > pad(const uint8_t in[], size_t in_length, size_t key_length, RandomNumberGenerator &rng) const =0
Definition: eme.h:20
std::string arg(size_t i) const
Definition: scan_name.cpp:122
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
secure_vector< uint8_t > encode(const uint8_t in[], size_t in_length, size_t key_length, RandomNumberGenerator &rng) const
Definition: eme.cpp:60
Definition: alg_id.cpp:13
const std::string & algo_name() const
Definition: scan_name.h:44
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
MechanismType hash