Botan 3.14.1
Crypto and TLS for C&
sig_padding.cpp
Go to the documentation of this file.
1/*
2* (C) 2015 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/sig_padding.h>
8
9#include <botan/exceptn.h>
10#include <botan/hash.h>
11#include <botan/pk_options_readers.h>
12#include <botan/internal/fmt.h>
13
14#if defined(BOTAN_HAS_X931_SIGNATURE_PADDING)
15 #include <botan/internal/x931_sig_padding.h>
16#endif
17
18#if defined(BOTAN_HAS_PKCSV15_SIGNATURE_PADDING)
19 #include <botan/internal/pkcs1_sig_padding.h>
20#endif
21
22#if defined(BOTAN_HAS_PSS)
23 #include <botan/internal/pssr.h>
24#endif
25
26#if defined(BOTAN_HAS_RAW_SIGNATURE_PADDING)
27 #include <botan/internal/raw_sig_padding.h>
28#endif
29
30#if defined(BOTAN_HAS_ISO_9796)
31 #include <botan/internal/iso9796.h>
32#endif
33
34namespace Botan {
35
36std::unique_ptr<SignaturePaddingScheme> SignaturePaddingScheme::create_or_throw(
37 const PK_Signature_Options_Reader& options) {
38 // Signing without padding is dangerous, so it must be requested explicitly
39 if(!options.using_padding()) {
40 throw Lookup_Error("RSA signatures require specifying a padding scheme");
41 }
42
43 const std::string padding = options.padding().value();
44
45 if(padding == "Raw") {
46#if defined(BOTAN_HAS_EMSA_RAW)
47 // Raw padding signs the input directly, so no hash function is involved
48 return std::make_unique<SignRawBytes>(options);
49#endif
50 } else {
51#if defined(BOTAN_HAS_EMSA_PKCS1)
52 // PKCS1v15 can also sign a digest the caller computed
53 if(padding == "PKCS1v15" && options.using_externally_computed_prehash()) {
54 return std::make_unique<PKCS1v15_Raw_SignaturePaddingScheme>(options);
55 }
56#endif
57
58 if(!options.using_hash()) {
59 throw Lookup_Error(fmt("RSA/{} signatures require specifying a hash function", padding));
60 }
61
62 const std::string& hash_fn = options.hash_function_name();
63 const bool hash_available = HashFunction::create(hash_fn) != nullptr;
64
65#if defined(BOTAN_HAS_EMSA_PKCS1)
66 if(padding == "PKCS1v15" && hash_available) {
67 return std::make_unique<PKCS1v15_SignaturePaddingScheme>(options);
68 }
69#endif
70
71#if defined(BOTAN_HAS_PSS)
72 if(padding == "PSS_Raw" && hash_available) {
73 return std::make_unique<PSS_Raw>(options);
74 }
75
76 if(padding == "PSS" && hash_available) {
77 return std::make_unique<PSSR>(options);
78 }
79#endif
80
81#if defined(BOTAN_HAS_ISO_9796)
82 if(padding == "ISO_9796_DS2" && hash_available) {
83 return std::make_unique<ISO_9796_DS2>(options);
84 }
85
86 if(padding == "ISO_9796_DS3" && hash_available) {
87 return std::make_unique<ISO_9796_DS3>(options);
88 }
89#endif
90
91#if defined(BOTAN_HAS_EMSA_X931)
92 if(padding == "X9.31" && hash_available) {
93 return std::make_unique<X931_SignaturePadding>(options);
94 }
95#endif
96 }
97
98 throw Lookup_Error("Invalid or unavailable signature padding scheme " + options.to_string());
99}
100
101} // namespace Botan
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
const std::optional< std::string > & padding() const
const std::string & hash_function_name() const
std::string to_string() const
Format the options as a string, for error messages.
static std::unique_ptr< SignaturePaddingScheme > create_or_throw(const PK_Signature_Options_Reader &options)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53