/src/botan/src/lib/pk_pad/eme.cpp
Line | Count | Source (jump to first uncovered line) |
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/internal/eme.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/internal/parsing.h> |
12 | | #include <botan/internal/scan_name.h> |
13 | | |
14 | | #if defined(BOTAN_HAS_EME_OAEP) |
15 | | #include <botan/internal/oaep.h> |
16 | | #endif |
17 | | |
18 | | #if defined(BOTAN_HAS_EME_PKCS1) |
19 | | #include <botan/internal/eme_pkcs.h> |
20 | | #endif |
21 | | |
22 | | #if defined(BOTAN_HAS_EME_RAW) |
23 | | #include <botan/internal/eme_raw.h> |
24 | | #endif |
25 | | |
26 | | namespace Botan { |
27 | | |
28 | 0 | std::unique_ptr<EME> EME::create(std::string_view algo_spec) { |
29 | 0 | #if defined(BOTAN_HAS_EME_RAW) |
30 | 0 | if(algo_spec == "Raw") { |
31 | 0 | return std::make_unique<EME_Raw>(); |
32 | 0 | } |
33 | 0 | #endif |
34 | | |
35 | 0 | #if defined(BOTAN_HAS_EME_PKCS1) |
36 | 0 | if(algo_spec == "PKCS1v15" || algo_spec == "EME-PKCS1-v1_5") { |
37 | 0 | return std::make_unique<EME_PKCS1v15>(); |
38 | 0 | } |
39 | 0 | #endif |
40 | | |
41 | 0 | #if defined(BOTAN_HAS_EME_OAEP) |
42 | 0 | SCAN_Name req(algo_spec); |
43 | |
|
44 | 0 | if(req.algo_name() == "OAEP" || req.algo_name() == "EME-OAEP" || req.algo_name() == "EME1") { |
45 | 0 | if(req.arg_count() == 1 || ((req.arg_count() == 2 || req.arg_count() == 3) && req.arg(1) == "MGF1")) { |
46 | 0 | if(auto hash = HashFunction::create(req.arg(0))) { |
47 | 0 | return std::make_unique<OAEP>(std::move(hash), req.arg(2, "")); |
48 | 0 | } |
49 | 0 | } else if(req.arg_count() == 2 || req.arg_count() == 3) { |
50 | 0 | auto mgf_params = parse_algorithm_name(req.arg(1)); |
51 | |
|
52 | 0 | if(mgf_params.size() == 2 && mgf_params[0] == "MGF1") { |
53 | 0 | auto hash = HashFunction::create(req.arg(0)); |
54 | 0 | auto mgf1_hash = HashFunction::create(mgf_params[1]); |
55 | |
|
56 | 0 | if(hash && mgf1_hash) { |
57 | 0 | return std::make_unique<OAEP>(std::move(hash), std::move(mgf1_hash), req.arg(2, "")); |
58 | 0 | } |
59 | 0 | } |
60 | 0 | } |
61 | 0 | } |
62 | 0 | #endif |
63 | | |
64 | 0 | throw Algorithm_Not_Found(algo_spec); |
65 | 0 | } |
66 | | |
67 | | /* |
68 | | * Encode a message |
69 | | */ |
70 | | secure_vector<uint8_t> EME::encode(const uint8_t msg[], |
71 | | size_t msg_len, |
72 | | size_t key_bits, |
73 | 0 | RandomNumberGenerator& rng) const { |
74 | 0 | return pad(msg, msg_len, key_bits, rng); |
75 | 0 | } |
76 | | |
77 | | /* |
78 | | * Encode a message |
79 | | */ |
80 | | secure_vector<uint8_t> EME::encode(const secure_vector<uint8_t>& msg, |
81 | | size_t key_bits, |
82 | 0 | RandomNumberGenerator& rng) const { |
83 | 0 | return pad(msg.data(), msg.size(), key_bits, rng); |
84 | 0 | } |
85 | | |
86 | | } // namespace Botan |