Botan 3.14.1
Crypto and TLS for C&
Botan::PKCS1v15_Raw_SignaturePaddingScheme Class Referencefinal

#include <pkcs1_sig_padding.h>

Inheritance diagram for Botan::PKCS1v15_Raw_SignaturePaddingScheme:
Botan::SignaturePaddingScheme

Public Member Functions

std::vector< uint8_t > encoding_of (std::span< const uint8_t > msg, size_t output_bits, RandomNumberGenerator &rng) override
std::string hash_function () const override
std::string name () const override
 PKCS1v15_Raw_SignaturePaddingScheme (const PK_Signature_Options_Reader &options)
std::vector< uint8_t > raw_data () override
void update (const uint8_t input[], size_t length) override
bool verify (std::span< const uint8_t > coded, std::span< const uint8_t > raw, size_t key_bits) override

Static Public Member Functions

static std::unique_ptr< SignaturePaddingScheme > create_or_throw (const PK_Signature_Options_Reader &options)

Detailed Description

PKCS1v15_SignaturePaddingScheme_Raw which is PKCS1v15_SignaturePaddingScheme without a hash or digest id (which according to QCA docs is "identical to PKCS#11's CKM_RSA_PKCS mechanism", something I have not confirmed)

Definition at line 55 of file pkcs1_sig_padding.h.

Constructor & Destructor Documentation

◆ PKCS1v15_Raw_SignaturePaddingScheme()

Botan::PKCS1v15_Raw_SignaturePaddingScheme::PKCS1v15_Raw_SignaturePaddingScheme ( const PK_Signature_Options_Reader & options)
explicit

Definition at line 103 of file pkcs1_sig_padding.cpp.

103 {
105
106 BOTAN_ARG_CHECK(options.using_externally_computed_prehash(),
107 "PKCS1v15 raw signing requires an externally computed prehash");
108
109 if(auto hash_algo = externally_computed_prehash_name(options)) {
110 std::unique_ptr<HashFunction> hash(HashFunction::create_or_throw(*hash_algo));
111 m_hash_id = pkcs_hash_id(hash->name());
112 m_hash_name = hash->name();
113 m_hash_output_len = hash->output_length();
114 } else {
115 m_hash_output_len = 0;
116 // m_hash_id, m_hash_name left empty
117 }
118}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
Definition hash_id.cpp:78
std::optional< std::string > externally_computed_prehash_name(const PK_Signature_Options_Reader &options)

References Botan::acknowledge_always_deterministic(), BOTAN_ARG_CHECK, Botan::HashFunction::create_or_throw(), Botan::externally_computed_prehash_name(), Botan::pkcs_hash_id(), and Botan::PK_Signature_Options_Reader::using_externally_computed_prehash().

Member Function Documentation

◆ create_or_throw()

std::unique_ptr< SignaturePaddingScheme > Botan::SignaturePaddingScheme::create_or_throw ( const PK_Signature_Options_Reader & options)
staticinherited

Factory method for SignaturePaddingScheme objects

Parameters
optionsthe algorithm parameters
Returns
pointer to newly allocated object of that type, or throws

Definition at line 36 of file sig_padding.cpp.

37 {
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}
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::HashFunction::create(), Botan::fmt(), Botan::PK_Signature_Options_Reader::hash_function_name(), Botan::PK_Signature_Options_Reader::padding(), Botan::PK_Signature_Options_Reader::to_string(), Botan::PK_Signature_Options_Reader::using_externally_computed_prehash(), Botan::PK_Signature_Options_Reader::using_hash(), and Botan::PK_Signature_Options_Reader::using_padding().

Referenced by Botan::ISO_9796_DS2::ISO_9796_DS2(), Botan::ISO_9796_DS3::ISO_9796_DS3(), Botan::PKCS1v15_SignaturePaddingScheme::PKCS1v15_SignaturePaddingScheme(), Botan::PSS_Raw::PSS_Raw(), Botan::PSSR::PSSR(), Botan::X931_SignaturePadding::X931_SignaturePadding(), and ~SignaturePaddingScheme().

◆ encoding_of()

std::vector< uint8_t > Botan::PKCS1v15_Raw_SignaturePaddingScheme::encoding_of ( std::span< const uint8_t > msg,
size_t output_bits,
RandomNumberGenerator & rng )
overridevirtual

Return the encoding of a message

Parameters
msgthe result of raw_data()
output_bitsthe desired output bit size
Random Number Generatorsa random number generator
Returns
encoded signature

Implements Botan::SignaturePaddingScheme.

Definition at line 142 of file pkcs1_sig_padding.cpp.

144 {
145 return pkcs1v15_sig_encoding(msg, output_bits, m_hash_id);
146}

◆ hash_function()

std::string Botan::PKCS1v15_Raw_SignaturePaddingScheme::hash_function ( ) const
inlineoverridevirtual

Return the hash function being used by this padding scheme

Implements Botan::SignaturePaddingScheme.

Definition at line 69 of file pkcs1_sig_padding.h.

69{ return m_hash_name; }

◆ name()

std::string Botan::PKCS1v15_Raw_SignaturePaddingScheme::name ( ) const
overridevirtual
Returns
the SCAN name of the encoding/padding scheme

Implements Botan::SignaturePaddingScheme.

Definition at line 95 of file pkcs1_sig_padding.cpp.

95 {
96 if(m_hash_name.empty()) {
97 return "PKCS1v15(Raw)";
98 } else {
99 return fmt("PKCS1v15(Raw,{})", m_hash_name);
100 }
101}

References Botan::fmt().

◆ raw_data()

std::vector< uint8_t > Botan::PKCS1v15_Raw_SignaturePaddingScheme::raw_data ( )
overridevirtual
Returns
raw hash

Implements Botan::SignaturePaddingScheme.

Definition at line 131 of file pkcs1_sig_padding.cpp.

131 {
132 std::vector<uint8_t> ret;
133 std::swap(ret, m_message);
134
135 if(m_hash_output_len > 0 && ret.size() != m_hash_output_len) {
136 throw Encoding_Error("PKCS1v15_Raw_SignaturePaddingScheme::encoding_of: Bad input length");
137 }
138
139 return ret;
140}

◆ update()

void Botan::PKCS1v15_Raw_SignaturePaddingScheme::update ( const uint8_t input[],
size_t length )
overridevirtual

Add more data to the signature computation

Parameters
inputsome data
lengthlength of input in bytes

Implements Botan::SignaturePaddingScheme.

Definition at line 120 of file pkcs1_sig_padding.cpp.

120 {
121 if(length > 0) {
122 // A sanity check to prevent someone from accidentally feeding an entire message
123 // into PKCS1v15(Raw), which would have to be buffered in memory
124 if(m_message.size() + length > 16384 / 8) {
125 throw Invalid_Argument("PKCS1v15(Raw) message too long");
126 }
127 m_message.insert(m_message.end(), input, input + length);
128 }
129}

◆ verify()

bool Botan::PKCS1v15_Raw_SignaturePaddingScheme::verify ( std::span< const uint8_t > encoding,
std::span< const uint8_t > raw_hash,
size_t key_bits )
overridevirtual

Verify the encoding

Parameters
encodingthe received (coded) message representative
Raw Hash Functionthe computed (local, uncoded) message representative
key_bitsthe size of the key in bits
Returns
true if coded is a valid encoding of raw, otherwise false

Implements Botan::SignaturePaddingScheme.

Definition at line 148 of file pkcs1_sig_padding.cpp.

150 {
151 if(m_hash_output_len > 0 && raw.size() != m_hash_output_len) {
152 return false;
153 }
154
155 try {
156 const auto pkcs1 = pkcs1v15_sig_encoding(raw, key_bits, m_hash_id);
157 return constant_time_compare(coded, pkcs1);
158 } catch(...) {
159 return false;
160 }
161}
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17

References Botan::constant_time_compare().


The documentation for this class was generated from the following files: