Botan 3.10.0
Crypto and TLS for C&
enc_padding.cpp
Go to the documentation of this file.
1/*
2* (C) 1999-2008 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/enc_padding.h>
8
9#include <botan/exceptn.h>
10#include <botan/internal/parsing.h>
11#include <botan/internal/scan_name.h>
12
13#if defined(BOTAN_HAS_EME_OAEP)
14 #include <botan/internal/oaep.h>
15#endif
16
17#if defined(BOTAN_HAS_EME_PKCS1)
18 #include <botan/internal/eme_pkcs.h>
19#endif
20
21#if defined(BOTAN_HAS_EME_RAW)
22 #include <botan/internal/eme_raw.h>
23#endif
24
25namespace Botan {
26
27std::unique_ptr<EncryptionPaddingScheme> EncryptionPaddingScheme::create(std::string_view algo_spec) {
28#if defined(BOTAN_HAS_EME_RAW)
29 if(algo_spec == "Raw") {
30 return std::make_unique<EME_Raw>();
31 }
32#endif
33
34#if defined(BOTAN_HAS_EME_PKCS1)
35 // TODO(Botan4) Remove all but "PKCS1v15"
36 if(algo_spec == "PKCS1v15" || algo_spec == "EME-PKCS1-v1_5") {
37 return std::make_unique<EME_PKCS1v15>();
38 }
39#endif
40
41#if defined(BOTAN_HAS_EME_OAEP)
42 SCAN_Name req(algo_spec);
43
44 // TODO(Botan4) Remove all but "OAEP"
45 if(req.algo_name() == "OAEP" || req.algo_name() == "EME-OAEP" || req.algo_name() == "EME1") {
46 if(req.arg_count() == 1 || ((req.arg_count() == 2 || req.arg_count() == 3) && req.arg(1) == "MGF1")) {
47 if(auto hash = HashFunction::create(req.arg(0))) {
48 return std::make_unique<OAEP>(std::move(hash), req.arg(2, ""));
49 }
50 } else if(req.arg_count() == 2 || req.arg_count() == 3) {
51 auto mgf_params = parse_algorithm_name(req.arg(1));
52
53 if(mgf_params.size() == 2 && mgf_params[0] == "MGF1") {
54 auto hash = HashFunction::create(req.arg(0));
55 auto mgf1_hash = HashFunction::create(mgf_params[1]);
56
57 if(hash && mgf1_hash) {
58 return std::make_unique<OAEP>(std::move(hash), std::move(mgf1_hash), req.arg(2, ""));
59 }
60 }
61 }
62 }
63#endif
64
65 throw Algorithm_Not_Found(algo_spec);
66}
67
69
70} // namespace Botan
static std::unique_ptr< EncryptionPaddingScheme > create(std::string_view algo_spec)
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:111
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:49
const std::string & algo_name() const
Definition scan_name.h:44
std::vector< std::string > parse_algorithm_name(std::string_view namex)
Definition parsing.cpp:57