Skip to content

Commit 0729830

Browse files
committed
Add PK_Signature_Options
This allows controlling all details of how signatures are created, without having to stuff values into the single parameters string which was previously available.
1 parent 1eacc5b commit 0729830

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1388
-767
lines changed

src/cli/pubkey.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#include <botan/pkcs8.h>
2222
#include <botan/pubkey.h>
2323
#include <botan/x509_key.h>
24+
#include <botan/internal/pk_options.h>
2425
#include <botan/internal/workfactor.h>
2526

2627
#include <fstream>
27-
#include <sstream>
2828

2929
#if defined(BOTAN_HAS_DL_GROUP)
3030
#include <botan/dl_group.h>
@@ -97,26 +97,17 @@ BOTAN_REGISTER_COMMAND("keygen", PK_Keygen);
9797

9898
namespace {
9999

100-
std::string choose_sig_padding(const std::string& key, const std::string& padding, const std::string& hash) {
101-
if(key == "RSA") {
102-
std::ostringstream oss;
103-
if(padding.empty()) {
104-
oss << "PSS";
105-
} else {
106-
oss << padding;
107-
}
108-
109-
oss << "(" << hash << ")";
110-
return oss.str();
111-
} else if(padding.empty()) {
112-
return hash;
113-
} else if(hash.empty()) {
114-
return padding;
115-
} else {
116-
std::ostringstream oss;
117-
oss << padding << "(" << hash << ")";
118-
return oss.str();
100+
Botan::PK_Signature_Options sig_options(
101+
std::string_view key, std::string_view padding, std::string_view hash, bool use_der, std::string_view provider) {
102+
if(key == "RSA" && padding.empty()) {
103+
return sig_options(key, "PSS", hash, use_der, provider);
119104
}
105+
106+
return Botan::PK_Signature_Options()
107+
.with_hash(hash)
108+
.with_padding(padding)
109+
.with_der_encoded_signature(use_der)
110+
.with_provider(provider);
120111
}
121112

122113
} // namespace
@@ -196,21 +187,14 @@ class PK_Sign final : public Command {
196187
throw CLI_Error_Unsupported("hashing", hash_fn);
197188
}
198189

199-
const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
200-
201-
auto format = Botan::Signature_Format::Standard;
202-
203-
if(flag_set("der-format")) {
204-
if(!key->_signature_element_size_for_DER_encoding()) {
205-
throw CLI_Usage_Error("Key type " + key->algo_name() +
206-
" does not support DER formatting for signatures");
207-
}
208-
format = Botan::Signature_Format::DerSequence;
190+
if(flag_set("der-format") && !key->_signature_element_size_for_DER_encoding()) {
191+
throw CLI_Usage_Error("Key type " + key->algo_name() + " does not support DER formatting for signatures");
209192
}
210193

211-
const std::string provider = get_arg("provider");
194+
const auto options =
195+
sig_options(key->algo_name(), get_arg("padding"), hash_fn, flag_set("der-format"), get_arg("provider"));
212196

213-
Botan::PK_Signer signer(*key, rng(), sig_padding, format, provider);
197+
Botan::PK_Signer signer(*key, rng(), options);
214198

215199
auto onData = [&signer](const uint8_t b[], size_t l) { signer.update(b, l); };
216200
Command::read_file(get_arg("file"), onData);
@@ -254,18 +238,9 @@ class PK_Verify final : public Command {
254238
throw CLI_Error_Unsupported("hashing", hash_fn);
255239
}
256240

257-
const std::string sig_padding = choose_sig_padding(key->algo_name(), get_arg("padding"), hash_fn);
258-
259-
auto format = Botan::Signature_Format::Standard;
260-
if(flag_set("der-format")) {
261-
if(key->message_parts() == 1) {
262-
throw CLI_Usage_Error("Key type " + key->algo_name() +
263-
" does not support DER formatting for signatures");
264-
}
265-
format = Botan::Signature_Format::DerSequence;
266-
}
241+
const auto options = sig_options(key->algo_name(), get_arg("padding"), hash_fn, flag_set("der-format"), "");
267242

268-
Botan::PK_Verifier verifier(*key, sig_padding, format);
243+
Botan::PK_Verifier verifier(*key, options);
269244
auto onData = [&verifier](const uint8_t b[], size_t l) { verifier.update(b, l); };
270245
Command::read_file(get_arg("file"), onData);
271246

src/examples/pkcs10_csr_on_tpm2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <botan/build.h>
44

5-
#if defined(BOTAN_HAS_TPM2)
5+
#if defined(BOTAN_HAS_TPM2) && defined(BOTAN_HAS_TPM2_RSA)
66

77
#include <botan/auto_rng.h>
88
#include <botan/hex.h>

src/lib/pk_pad/sig_padding/emsa_pkcs1/pkcs1_sig_padding.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ PKCS1v15_Raw_SignaturePaddingScheme::PKCS1v15_Raw_SignaturePaddingScheme() : m_h
100100
// m_hash_id, m_hash_name left empty
101101
}
102102

103-
PKCS1v15_Raw_SignaturePaddingScheme::PKCS1v15_Raw_SignaturePaddingScheme(std::string_view hash_algo) {
104-
std::unique_ptr<HashFunction> hash(HashFunction::create_or_throw(hash_algo));
105-
m_hash_id = pkcs_hash_id(hash_algo);
106-
m_hash_name = hash->name();
107-
m_hash_output_len = hash->output_length();
103+
PKCS1v15_Raw_SignaturePaddingScheme::PKCS1v15_Raw_SignaturePaddingScheme(const std::optional<std::string>& hash_algo) {
104+
if(hash_algo) {
105+
std::unique_ptr<HashFunction> hash(HashFunction::create_or_throw(hash_algo.value()));
106+
m_hash_id = pkcs_hash_id(hash->name());
107+
m_hash_name = hash->name();
108+
m_hash_output_len = hash->output_length();
109+
} else {
110+
m_hash_output_len = 0;
111+
// m_hash_id, m_hash_name left empty
112+
}
108113
}
109114

110115
void PKCS1v15_Raw_SignaturePaddingScheme::update(const uint8_t input[], size_t length) {

src/lib/pk_pad/sig_padding/emsa_pkcs1/pkcs1_sig_padding.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include <botan/internal/sig_padding.h>
1212

1313
#include <memory>
14+
#include <optional>
1415
#include <string>
15-
#include <string_view>
1616
#include <vector>
1717

1818
namespace Botan {
@@ -73,7 +73,7 @@ class PKCS1v15_Raw_SignaturePaddingScheme final : public SignaturePaddingScheme
7373
* @param hash_algo the digest id for that hash is included in
7474
* the signature.
7575
*/
76-
explicit PKCS1v15_Raw_SignaturePaddingScheme(std::string_view hash_algo);
76+
explicit PKCS1v15_Raw_SignaturePaddingScheme(const std::optional<std::string>& hash_algo = std::nullopt);
7777

7878
std::string hash_function() const override { return m_hash_name; }
7979

src/lib/pk_pad/sig_padding/emsa_pssr/pssr.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ bool pss_verify(HashFunction& hash,
145145

146146
} // namespace
147147

148-
PSSR::PSSR(std::unique_ptr<HashFunction> hash) :
149-
m_hash(std::move(hash)), m_salt_size(m_hash->output_length()), m_required_salt_len(false) {}
150-
151-
PSSR::PSSR(std::unique_ptr<HashFunction> hash, size_t salt_size) :
152-
m_hash(std::move(hash)), m_salt_size(salt_size), m_required_salt_len(true) {}
148+
PSSR::PSSR(std::unique_ptr<HashFunction> hash, std::optional<size_t> salt_size) :
149+
m_hash(std::move(hash)),
150+
m_salt_size(salt_size.value_or(m_hash->output_length())),
151+
m_required_salt_len(salt_size.has_value()) {}
153152

154153
/*
155154
* PSSR Update Operation
@@ -192,11 +191,10 @@ std::string PSSR::name() const {
192191
return fmt("PSS({},MGF1,{})", m_hash->name(), m_salt_size);
193192
}
194193

195-
PSS_Raw::PSS_Raw(std::unique_ptr<HashFunction> hash) :
196-
m_hash(std::move(hash)), m_salt_size(m_hash->output_length()), m_required_salt_len(false) {}
197-
198-
PSS_Raw::PSS_Raw(std::unique_ptr<HashFunction> hash, size_t salt_size) :
199-
m_hash(std::move(hash)), m_salt_size(salt_size), m_required_salt_len(true) {}
194+
PSS_Raw::PSS_Raw(std::unique_ptr<HashFunction> hash, std::optional<size_t> salt_size) :
195+
m_hash(std::move(hash)),
196+
m_salt_size(salt_size.value_or(m_hash->output_length())),
197+
m_required_salt_len(salt_size.has_value()) {}
200198

201199
/*
202200
* PSS_Raw Update Operation

src/lib/pk_pad/sig_padding/emsa_pssr/pssr.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <botan/internal/sig_padding.h>
1212
#include <memory>
13+
#include <optional>
1314
#include <string>
1415
#include <vector>
1516

@@ -23,16 +24,11 @@ class HashFunction;
2324
*/
2425
class PSSR final : public SignaturePaddingScheme {
2526
public:
26-
/**
27-
* @param hash the hash function to use
28-
*/
29-
explicit PSSR(std::unique_ptr<HashFunction> hash);
30-
3127
/**
3228
* @param hash the hash function to use
3329
* @param salt_size the size of the salt to use in bytes
3430
*/
35-
PSSR(std::unique_ptr<HashFunction> hash, size_t salt_size);
31+
PSSR(std::unique_ptr<HashFunction> hash, std::optional<size_t> salt_size);
3632

3733
std::string name() const override;
3834

@@ -60,16 +56,11 @@ class PSSR final : public SignaturePaddingScheme {
6056
*/
6157
class PSS_Raw final : public SignaturePaddingScheme {
6258
public:
63-
/**
64-
* @param hash the hash function to use
65-
*/
66-
explicit PSS_Raw(std::unique_ptr<HashFunction> hash);
67-
6859
/**
6960
* @param hash the hash function to use
7061
* @param salt_size the size of the salt to use in bytes
7162
*/
72-
PSS_Raw(std::unique_ptr<HashFunction> hash, size_t salt_size);
63+
explicit PSS_Raw(std::unique_ptr<HashFunction> hash, std::optional<size_t> salt_size = std::nullopt);
7364

7465
std::string hash_function() const override;
7566

src/lib/pk_pad/sig_padding/iso9796/iso9796.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ bool iso9796_verification(std::span<const uint8_t> repr,
209209

210210
} // namespace
211211

212+
ISO_9796_DS2::ISO_9796_DS2(std::unique_ptr<HashFunction> hash, bool implicit, std::optional<size_t> salt_size) :
213+
m_hash(std::move(hash)), m_implicit(implicit), m_salt_len(salt_size.value_or(m_hash->output_length())) {}
214+
212215
/*
213216
* ISO-9796-2 signature scheme 2
214217
* DS 2 is probabilistic

src/lib/pk_pad/sig_padding/iso9796/iso9796.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <botan/internal/sig_padding.h>
1212
#include <memory>
13+
#include <optional>
1314
#include <string>
1415
#include <vector>
1516

@@ -27,8 +28,7 @@ class ISO_9796_DS2 final : public SignaturePaddingScheme {
2728
* @param implicit whether or not the trailer is implicit
2829
* @param salt_size size of the salt to use in bytes
2930
*/
30-
ISO_9796_DS2(std::unique_ptr<HashFunction> hash, bool implicit, size_t salt_size) :
31-
m_hash(std::move(hash)), m_implicit(implicit), m_salt_len(salt_size) {}
31+
ISO_9796_DS2(std::unique_ptr<HashFunction> hash, bool implicit, std::optional<size_t> salt_size);
3232

3333
std::string hash_function() const override;
3434

0 commit comments

Comments
 (0)