Botan 2.19.3
Crypto and TLS for C&
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_PKCS1)
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
25namespace Botan {
26
27EME* 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_PKCS1)
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*/
76secure_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}
secure_vector< uint8_t > encode(const uint8_t in[], size_t in_length, size_t key_length, RandomNumberGenerator &rng) const
Definition eme.cpp:76
virtual secure_vector< uint8_t > pad(const uint8_t in[], size_t in_length, size_t key_length, RandomNumberGenerator &rng) const =0
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition hash.cpp:102
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:56
const std::string & algo_name() const
Definition scan_name.h:51
EME * get_eme(const std::string &algo_spec)
Definition eme.cpp:27
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition parsing.cpp:95
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65
MechanismType hash