7#include <botan/internal/pk_options_impl.h>
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/algorithm_spec.h>
12#include <botan/internal/fmt.h>
28 if(algo.starts_with(
"Dilithium") || algo.starts_with(
"ML-DSA") || algo ==
"SLH-DSA" || algo ==
"SPHINCS+") {
29 if(!params.empty() && params !=
"Randomized" && params !=
"Deterministic" && params !=
"Pure") {
30 throw Invalid_Argument(
fmt(
"Unexpected parameters '{}' for signing with {}", params, algo));
33 if(params ==
"Deterministic" || params ==
"Pure") {
54 std::string hash =
"SM3";
55 const auto comma = params.find(
',');
56 if(comma == std::string::npos) {
59 userid = params.substr(0, comma);
60 hash = params.substr(comma + 1, std::string::npos);
71 if(algo ==
"Ed25519") {
72 if(params.empty() || params ==
"Identity" || params ==
"Pure") {
74 }
else if(params ==
"Ed25519ph") {
82 if(params.empty() || params ==
"Identity" || params ==
"Pure" || params ==
"Ed448") {
84 }
else if(params ==
"Ed448ph") {
93 const auto canonical_padding = [](std::string_view alg) -> std::string_view {
95 if(alg ==
"EMSA_PKCS1" || alg ==
"EMSA-PKCS1-v1_5" || alg ==
"EMSA3") {
100 if(alg ==
"PSSR_Raw") {
105 if(alg ==
"PSSR" || alg ==
"EMSA-PSS" || alg ==
"PSS-MGF1" || alg ==
"EMSA4") {
110 if(alg ==
"EMSA_X931" || alg ==
"EMSA2" || alg ==
"X9.31") {
120 if(
auto m = req.
match(
"Raw({hash?})")) {
123 std::string(m->str(
"hash")));
128 if(
auto m = req.
match(
"PKCS1v15(Raw,{hash?})")) {
137 if(
auto m = req.
match(
"PKCS1v15({hash})")) {
150 if(
auto m = req.
match(
"PSS|PSS_Raw({hash})")) {
154 if(
auto m = req.
match(
"PSS|PSS_Raw({hash},MGF1,{salt_len:int?})")) {
157 if(m->has(
"salt_len")) {
158 return std::move(pss_opt).with_salt_size(m->integer(
"salt_len"));
163 if(
auto m = req.
match(
"ISO_9796_DS2({hash},{trailer:str=exp},{salt_len:int?})")) {
164 const auto trailer = m->str(
"trailer");
165 if(trailer !=
"imp" && trailer !=
"exp") {
166 throw Invalid_Argument(
fmt(
"Unexpected parameters '{}' for signing with {}", params, algo));
174 if(m->has(
"salt_len")) {
175 return std::move(opt).with_salt_size(m->integer(
"salt_len"));
181 if(
auto m = req.
match(
"ISO_9796_DS3({hash},{trailer:str=exp})")) {
182 const auto trailer = m->str(
"trailer");
183 if(trailer !=
"imp" && trailer !=
"exp") {
184 throw Invalid_Argument(
fmt(
"Unexpected parameters '{}' for signing with {}", params, algo));
193 if(req.
matches(
"X9.31(Raw)")) {
197 if(
auto m = req.
match(
"X9.31({hash})")) {
214 const std::string_view det_suffix =
",Deterministic";
215 const bool deterministic = params.ends_with(det_suffix);
216 const std::string_view hash_params = deterministic ? params.substr(0, params.size() - det_suffix.size()) : params;
219 if(hash_params.starts_with(
"Raw")) {
221 const auto m = req.
match(
"Raw({hash?})");
223 throw Invalid_Argument(
fmt(
"Unexpected parameters '{}' for signing with {}", params, algo));
227 return std::move(raw_opt).with_externally_computed_prehash(std::string(m->str(
"hash")));
232 const auto hash = [&]() -> std::string {
233 if(hash_params.starts_with(
"EMSA1")) {
235 const auto m = req.
match(
"EMSA1({hash})");
237 throw Invalid_Argument(
fmt(
"Unexpected parameters '{}' for signing with {}", params, algo));
239 return std::string(m->str(
"hash"));
241 return std::string(hash_params);
270 if(params ==
"PKCS1v15" || params ==
"EME-PKCS1-v1_5") {
280 if(params.starts_with(
"OAEP") || params.starts_with(
"EME-OAEP") || params.starts_with(
"EME1")) {
285 if(
auto m = req.
match(
"OAEP|EME-OAEP|EME1({hash})")) {
286 return oaep_opt.with_hash(m->str(
"hash"));
289 if(
auto m = req.
match(
"OAEP|EME-OAEP|EME1({hash},MGF1,{label:str=})")) {
290 return oaep_opt.with_hash(m->str(
"hash")).with_context(m->str(
"label"));
293 if(
auto m = req.
match(
"OAEP|EME-OAEP|EME1({hash},MGF1({mgf_hash}),{label:str=})")) {
294 return oaep_opt.with_hash(m->str(
"hash")).with_mgf1_hash(m->str(
"mgf_hash")).with_context(m->str(
"label"));
305 throw Invalid_Argument(
"KEM requires specifying a KDF, or Raw to use the shared key directly");
306 }
else if(params ==
"Raw") {
316 throw Invalid_Argument(
"Key agreement requires specifying a KDF, or Raw to use the agreed value directly");
317 }
else if(params ==
"Raw") {
325 std::string_view algo_name,
326 std::string_view hash_fn) {
329 fmt(
"This {} key can only be used with {}, not {}", algo_name, hash_fn, options.
hash_function_name()));
339 if(prehash.has_value() && hash.has_value() && *prehash != *hash) {
341 fmt(
"Externally computed prehash was named as {} but the hash option specified {}", *prehash, *hash));
344 return prehash.has_value() ? prehash : hash;
#define BOTAN_STATE_CHECK(expr)
AlgorithmSpec with_head(std::string_view head) const
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::string_view head() const
virtual std::string algo_name() const =0
PK_Encryption_Options with_padding(std::string_view padding)
PK_Encryption_Options with_hash(std::string_view hash)
PK_KEM_Options with_kdf(std::string_view kdf)
PK_KEM_Options with_raw_shared_key()
PK_Key_Agreement_Options with_kdf(std::string_view kdf)
PK_Key_Agreement_Options with_raw_shared_key()
const std::optional< std::string > & hash_function() const
bool using_externally_computed_prehash() const
const std::optional< std::string > & externally_computed_prehash_function() const
const std::string & hash_function_name() const
bool using_deterministic_signature() 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_prehash(std::optional< std::string > prehash=std::nullopt)
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)
PK_Encryption_Options parse_legacy_enc_options(const Public_Key &key, std::string_view params)
std::string fmt(std::string_view format, const T &... args)
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
PK_KEM_Options parse_legacy_kem_options(std::string_view params)
PK_Key_Agreement_Options parse_legacy_ka_options(std::string_view params)
void validate_for_hash_based_signature(const PK_Signature_Options_Reader &options, std::string_view algo_name, std::string_view hash_fn)
PK_Signature_Options parse_legacy_sig_options(const Public_Key &key, std::string_view params)
std::optional< std::string > externally_computed_prehash_name(const PK_Signature_Options_Reader &options)