Botan  2.8.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 #include <botan/parsing.h>
12 
13 #if defined(BOTAN_HAS_EME_OAEP)
14 #include <botan/oaep.h>
15 #endif
16 
17 #if defined(BOTAN_HAS_EME_PKCS1v15)
18 #include <botan/eme_pkcs.h>
19 #endif
20 
21 #if defined(BOTAN_HAS_EME_RAW)
22 #include <botan/eme_raw.h>
23 #endif
24 
25 namespace Botan {
26 
27 EME* get_eme(const std::string& algo_spec)
28  {
29 #if defined(BOTAN_HAS_EME_RAW)
30  if(algo_spec == "Raw")
31  return new EME_Raw;
32 #endif
33 
34 #if defined(BOTAN_HAS_EME_PKCS1v15)
35  if(algo_spec == "PKCS1v15" || algo_spec == "EME-PKCS1-v1_5")
36  return new EME_PKCS1v15;
37 #endif
38 
39 #if defined(BOTAN_HAS_EME_OAEP)
40  SCAN_Name req(algo_spec);
41 
42  if(req.algo_name() == "OAEP" ||
43  req.algo_name() == "EME-OAEP" ||
44  req.algo_name() == "EME1")
45  {
46  if(req.arg_count() == 1 ||
47  ((req.arg_count() == 2 || req.arg_count() == 3) && req.arg(1) == "MGF1"))
48  {
49  if(auto hash = HashFunction::create(req.arg(0)))
50  return new OAEP(hash.release(), req.arg(2, ""));
51  }
52  else if(req.arg_count() == 2 || req.arg_count() == 3)
53  {
54  auto mgf_params = parse_algorithm_name(req.arg(1));
55 
56  if(mgf_params.size() == 2 && mgf_params[0] == "MGF1")
57  {
58  auto hash = HashFunction::create(req.arg(0));
59  auto mgf1_hash = HashFunction::create(mgf_params[1]);
60 
61  if(hash && mgf1_hash)
62  {
63  return new OAEP(hash.release(), mgf1_hash.release(), req.arg(2, ""));
64  }
65  }
66  }
67  }
68 #endif
69 
70  throw Algorithm_Not_Found(algo_spec);
71  }
72 
73 /*
74 * Encode a message
75 */
76 secure_vector<uint8_t> EME::encode(const uint8_t msg[], size_t msg_len,
77  size_t key_bits,
78  RandomNumberGenerator& rng) const
79  {
80  return pad(msg, msg_len, key_bits, rng);
81  }
82 
83 /*
84 * Encode a message
85 */
87  size_t key_bits,
88  RandomNumberGenerator& rng) const
89  {
90  return pad(msg.data(), msg.size(), key_bits, rng);
91  }
92 
93 
94 }
EME * get_eme(const std::string &algo_spec)
Definition: eme.cpp:27
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:110
secure_vector< uint8_t > encode(const uint8_t in[], size_t in_length, size_t key_length, RandomNumberGenerator &rng) const
Definition: eme.cpp:76
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition: parsing.cpp:91
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