7#include <botan/pk_options_readers.h>
9#include <botan/assert.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/mem_utils.h>
13#include <initializer_list>
22 BOTAN_STATE_CHECK_MSG(!m_hash_fn.has_value(),
"PK_Signature_Options::with_hash cannot specify hash twice");
25 next.m_hash_fn = hash;
31 BOTAN_STATE_CHECK_MSG(!m_padding.has_value(),
"PK_Signature_Options::with_padding cannot specify padding twice");
33 if(!padding.empty()) {
34 next.m_padding = padding;
40 BOTAN_STATE_CHECK_MSG(!m_using_prehash,
"PK_Signature_Options::with_prehash cannot specify prehash twice");
42 "PK_Signature_Options::with_prehash cannot be combined with an externally computed prehash");
47 next.m_using_prehash =
true;
48 next.m_prehash = std::move(prehash_function);
54 !m_using_external_prehash,
55 "PK_Signature_Options::with_externally_computed_prehash cannot specify external prehash twice");
57 "PK_Signature_Options::with_externally_computed_prehash cannot be combined with a prehash");
62 next.m_using_external_prehash =
true;
63 next.m_external_prehash = std::move(hash);
69 "PK_Signature_Options::with_provider cannot specify provider twice");
71 if(!provider.empty()) {
72 next.m_provider = provider;
78 BOTAN_STATE_CHECK_MSG(!m_context.has_value(),
"PK_Signature_Options::with_context cannot specify context twice");
80 next.m_context = std::vector<uint8_t>(context.begin(), context.end());
85 BOTAN_STATE_CHECK_MSG(!m_context.has_value(),
"PK_Signature_Options::with_context cannot specify context twice");
88 next.m_context = std::vector<uint8_t>(contextb.begin(), contextb.end());
94 "PK_Signature_Options::with_salt_size cannot specify salt size twice");
97 next.m_salt_size = salt_size;
103 !m_deterministic_sig,
104 "PK_Signature_Options::with_deterministic_signature cannot specify deterministic signature twice");
106 next.m_deterministic_sig = deterministic;
112 "PK_Signature_Options::with_der_encoded_signature cannot specify DER encoding twice");
114 next.m_use_der = der;
120 !m_explicit_trailer_field,
121 "PK_Signature_Options::with_explicit_trailer_field cannot specify explicit trailer field twice");
123 next.m_explicit_trailer_field = trailer;
129bool provider_is_in_use(
const std::optional<std::string>& provider) {
130 return provider.has_value() && !provider->empty() && *provider !=
"base";
136 note_examined(Option::Provider);
137 return provider_is_in_use(m_options.m_provider);
141 note_examined(Option::Hash);
143 if(m_options.m_hash_fn.has_value()) {
144 return m_options.m_hash_fn.value();
147 throw Invalid_State(
"This signature scheme requires specifying a hash function");
150uint32_t PK_Signature_Options_Reader::options_in_use()
const {
153 const auto set_if = [&](
bool cond, Option option) {
155 in_use |=
static_cast<uint32_t
>(option);
159 const auto& o = m_options;
161 set_if(o.m_hash_fn.has_value(), Option::Hash);
162 set_if(o.m_using_prehash, Option::Prehash);
163 set_if(o.m_using_external_prehash, Option::ExternalPrehash);
164 set_if(o.m_padding.has_value(), Option::Padding);
165 set_if(o.m_context.has_value(), Option::Context);
166 set_if(provider_is_in_use(o.m_provider), Option::Provider);
167 set_if(o.m_salt_size.has_value(), Option::SaltSize);
168 set_if(o.m_use_der, Option::DerEncoded);
169 set_if(o.m_deterministic_sig && m_usage == Usage::Signing, Option::Deterministic);
170 set_if(o.m_explicit_trailer_field, Option::ExplicitTrailer);
181template <
typename OptionEnum>
182void throw_for_unexamined_options(std::string_view algo_name,
183 std::string_view option_kind,
185 std::initializer_list<std::pair<OptionEnum, std::string_view>> option_names) {
186 if(unexamined == 0) {
191 for(
const auto& [option,
name] : option_names) {
192 if((unexamined &
static_cast<uint32_t
>(option)) != 0) {
200 throw Invalid_Argument(
fmt(
"{} does not support the {} option(s): {}", algo_name, option_kind, names));
205void PK_Signature_Options_Reader::throw_if_unexamined(std::string_view algo_name)
const {
206 const uint32_t unexamined = options_in_use() & ~m_examined;
208 if(unexamined != 0) {
209 throw_for_unexamined_options<Option>(algo_name,
213 {Option::Hash,
"hash"},
214 {Option::Prehash,
"prehash"},
215 {Option::Padding,
"padding"},
216 {Option::Context,
"context"},
217 {Option::Provider,
"provider"},
218 {Option::SaltSize,
"salt size"},
219 {Option::DerEncoded,
"DER encoding"},
220 {Option::Deterministic,
"deterministic"},
221 {Option::ExplicitTrailer,
"explicit trailer field"},
222 {Option::ExternalPrehash,
"externally computed prehash"},
228 std::ostringstream out;
230 const auto print_str = [&](std::string_view
name, std::optional<std::string> val) {
231 if(val.has_value()) {
232 out <<
name <<
"='" << val.value() <<
"' ";
236 print_str(
"Hash", m_hash_fn);
237 print_str(
"Padding", m_padding);
238 print_str(
"Provider", m_provider);
240 if(m_using_prehash) {
241 out <<
"Prehash=" << m_prehash.value_or(
"default") <<
" ";
243 if(m_using_external_prehash) {
244 out <<
"ExternalPrehash=" << m_external_prehash.value_or(
"unspecified") <<
" ";
248 out <<
"Context=" <<
hex_encode(*m_context) <<
" ";
252 out <<
"SaltLen=" << *m_salt_size <<
" ";
255 out <<
"DerSignature ";
257 if(m_deterministic_sig) {
258 out <<
"Deterministic ";
260 if(m_explicit_trailer_field) {
261 out <<
"ExplicitTrailer ";
272 BOTAN_STATE_CHECK_MSG(!m_padding.has_value(),
"PK_Encryption_Options::with_padding cannot specify padding twice");
274 if(!padding.empty()) {
275 next.m_padding = padding;
281 BOTAN_STATE_CHECK_MSG(!m_hash_fn.has_value(),
"PK_Encryption_Options::with_hash cannot specify hash twice");
284 next.m_hash_fn = hash;
291 "PK_Encryption_Options::with_mgf1_hash cannot specify MGF1 hash twice");
294 next.m_mgf1_hash_fn = hash;
300 BOTAN_STATE_CHECK_MSG(!m_context.has_value(),
"PK_Encryption_Options::with_context cannot specify context twice");
302 next.m_context = std::vector<uint8_t>(context.begin(), context.end());
312 "PK_Encryption_Options::with_provider cannot specify provider twice");
314 if(!provider.empty()) {
315 next.m_provider = provider;
321 note_examined(Option::Provider);
322 return provider_is_in_use(m_options.m_provider);
326 note_examined(Option::Hash);
328 if(m_options.m_hash_fn.has_value()) {
329 return m_options.m_hash_fn.value();
332 throw Invalid_State(
"This encryption scheme requires specifying a hash function");
335uint32_t PK_Encryption_Options_Reader::options_in_use()
const {
338 const auto set_if = [&](
bool cond, Option option) {
340 in_use |=
static_cast<uint32_t
>(option);
344 const auto& o = m_options;
346 set_if(o.m_padding.has_value(), Option::Padding);
347 set_if(o.m_hash_fn.has_value(), Option::Hash);
348 set_if(o.m_mgf1_hash_fn.has_value(), Option::Mgf1Hash);
349 set_if(o.m_context.has_value(), Option::Context);
350 set_if(provider_is_in_use(o.m_provider), Option::Provider);
355void PK_Encryption_Options_Reader::throw_if_unexamined(std::string_view algo_name)
const {
356 const uint32_t unexamined = options_in_use() & ~m_examined;
358 if(unexamined != 0) {
359 throw_for_unexamined_options<Option>(algo_name,
363 {Option::Padding,
"padding"},
364 {Option::Hash,
"hash"},
365 {Option::Mgf1Hash,
"MGF1 hash"},
366 {Option::Context,
"context"},
367 {Option::Provider,
"provider"},
373 std::ostringstream out;
375 const auto print_str = [&](std::string_view
name,
const std::optional<std::string>& val) {
376 if(val.has_value()) {
377 out <<
name <<
"='" << val.value() <<
"' ";
381 print_str(
"Padding", m_padding);
382 print_str(
"Hash", m_hash_fn);
383 print_str(
"MGF1Hash", m_mgf1_hash_fn);
384 print_str(
"Provider", m_provider);
387 out <<
"Context=" <<
hex_encode(*m_context) <<
" ";
399 BOTAN_STATE_CHECK_MSG(!m_raw_shared_key,
"PK_KEM_Options::with_kdf cannot be combined with with_raw_shared_key");
401 BOTAN_ARG_CHECK(!kdf.empty(),
"PK_KEM_Options::with_kdf requires a KDF name");
408 BOTAN_STATE_CHECK_MSG(!m_raw_shared_key,
"PK_KEM_Options::with_raw_shared_key cannot be specified twice");
409 BOTAN_STATE_CHECK_MSG(!m_kdf.has_value(),
"PK_KEM_Options::with_raw_shared_key cannot be combined with with_kdf");
411 next.m_raw_shared_key =
true;
417 "PK_KEM_Options::with_provider cannot specify provider twice");
419 if(!provider.empty()) {
420 next.m_provider = provider;
426 note_examined(Option::Provider);
427 return provider_is_in_use(m_options.m_provider);
430uint32_t PK_KEM_Options_Reader::options_in_use()
const {
433 const auto set_if = [&](
bool cond, Option option) {
435 in_use |=
static_cast<uint32_t
>(option);
439 set_if(m_options.m_kdf.has_value(), Option::Kdf);
440 set_if(m_options.m_raw_shared_key, Option::RawSharedKey);
441 set_if(provider_is_in_use(m_options.m_provider), Option::Provider);
446void PK_KEM_Options_Reader::throw_if_unexamined(std::string_view algo_name)
const {
447 const uint32_t unexamined = options_in_use() & ~m_examined;
449 if(unexamined != 0) {
450 throw_for_unexamined_options<Option>(algo_name,
454 {Option::Kdf,
"KDF"},
455 {Option::RawSharedKey,
"raw shared key"},
456 {Option::Provider,
"provider"},
462 std::ostringstream out;
465 out <<
"KDF='" << *m_kdf <<
"' ";
467 if(m_raw_shared_key) {
468 out <<
"RawSharedKey ";
471 out <<
"Provider='" << *m_provider <<
"' ";
482 BOTAN_STATE_CHECK_MSG(!m_kdf.has_value(),
"PK_Key_Agreement_Options::with_kdf cannot specify KDF twice");
484 "PK_Key_Agreement_Options::with_kdf cannot be combined with with_raw_shared_key");
486 BOTAN_ARG_CHECK(!kdf.empty(),
"PK_Key_Agreement_Options::with_kdf requires a KDF name");
493 BOTAN_STATE_CHECK_MSG(!m_raw_shared_key,
"PK_Key_Agreement_Options::with_raw_shared_key cannot be specified twice");
495 "PK_Key_Agreement_Options::with_raw_shared_key cannot be combined with with_kdf");
497 next.m_raw_shared_key =
true;
503 "PK_Key_Agreement_Options::with_provider cannot specify provider twice");
505 if(!provider.empty()) {
506 next.m_provider = provider;
512 note_examined(Option::Provider);
513 return provider_is_in_use(m_options.m_provider);
516uint32_t PK_Key_Agreement_Options_Reader::options_in_use()
const {
519 const auto set_if = [&](
bool cond, Option option) {
521 in_use |=
static_cast<uint32_t
>(option);
525 set_if(m_options.m_kdf.has_value(), Option::Kdf);
526 set_if(m_options.m_raw_shared_key, Option::RawSharedKey);
527 set_if(provider_is_in_use(m_options.m_provider), Option::Provider);
532void PK_Key_Agreement_Options_Reader::throw_if_unexamined(std::string_view algo_name)
const {
533 const uint32_t unexamined = options_in_use() & ~m_examined;
535 if(unexamined != 0) {
536 throw_for_unexamined_options<Option>(algo_name,
540 {Option::Kdf,
"KDF"},
541 {Option::RawSharedKey,
"raw shared key"},
542 {Option::Provider,
"provider"},
548 std::ostringstream out;
551 out <<
"KDF='" << *m_kdf <<
"' ";
553 if(m_raw_shared_key) {
554 out <<
"RawSharedKey ";
557 out <<
"Provider='" << *m_provider <<
"' ";
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_STATE_CHECK_MSG(expr, msg)
bool using_provider() const
const std::string & hash_function_name() const
PK_Encryption_Options with_padding(std::string_view padding)
PK_Encryption_Options()=default
PK_Encryption_Options with_hash(std::string_view hash)
PK_Encryption_Options with_mgf1_hash(std::string_view hash)
PK_Encryption_Options with_context(std::span< const uint8_t > context)
std::string to_string() const
PK_Encryption_Options with_provider(std::string_view provider)
bool using_provider() const
PK_KEM_Options with_provider(std::string_view provider)
PK_KEM_Options with_kdf(std::string_view kdf)
std::string to_string() const
PK_KEM_Options with_raw_shared_key()
bool using_provider() const
~PK_Key_Agreement_Options()
std::string to_string() const
PK_Key_Agreement_Options with_provider(std::string_view provider)
PK_Key_Agreement_Options with_kdf(std::string_view kdf)
PK_Key_Agreement_Options()=default
PK_Key_Agreement_Options with_raw_shared_key()
bool using_provider() const
const std::string & hash_function_name() const
PK_Signature_Options with_explicit_trailer_field(bool trailer=true)
PK_Signature_Options with_deterministic_signature(bool deterministic=true)
PK_Signature_Options with_padding(std::string_view padding)
PK_Signature_Options with_hash(std::string_view hash)
PK_Signature_Options with_der_encoded_signature(bool der=true)
PK_Signature_Options with_salt_size(size_t salt_size)
PK_Signature_Options with_prehash(std::optional< std::string > prehash=std::nullopt)
std::string to_string() const
PK_Signature_Options()=default
PK_Signature_Options with_provider(std::string_view provider)
PK_Signature_Options with_context(std::span< const uint8_t > context)
PK_Signature_Options with_externally_computed_prehash(std::optional< std::string > hash=std::nullopt)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
std::string fmt(std::string_view format, const T &... args)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)