Botan 3.14.1
Crypto and TLS for C&
pk_options_impl.h
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_PK_OPTIONS_IMPL_H_
8#define BOTAN_PK_OPTIONS_IMPL_H_
9
10#include <botan/exceptn.h>
11#include <botan/pk_options_readers.h>
12#include <optional>
13#include <string>
14#include <string_view>
15
16namespace Botan {
17
18class Public_Key;
19
20PK_Signature_Options parse_legacy_sig_options(const Public_Key& key, std::string_view params);
21
22PK_Encryption_Options parse_legacy_enc_options(const Public_Key& key, std::string_view params);
23
24PK_KEM_Options parse_legacy_kem_options(std::string_view params);
25
27
28/**
29* Creates option readers for library code which builds its own options,
30* for example when verifying an X.509 signature
31*/
33 public:
35 return PK_Signature_Options_Reader(options, PK_Signature_Options_Reader::Usage::Signing);
36 }
37
39 return PK_Signature_Options_Reader(options, PK_Signature_Options_Reader::Usage::Verification);
40 }
41
45
46 static PK_KEM_Options_Reader read(const PK_KEM_Options& options) { return PK_KEM_Options_Reader(options); }
47
51
52 template <typename ReaderT>
53 static void throw_if_unexamined(const ReaderT& reader, std::string_view algo_name) {
54 reader.throw_if_unexamined(algo_name);
55 }
56};
57
58/**
59* For schemes where the hash function is fixed by the key (XMSS, SLH-DSA, ...)
60*
61* Accepts the hash option only if it names the hash the key already uses.
62*/
63void validate_for_hash_based_signature(const PK_Signature_Options_Reader& options,
64 std::string_view algo_name,
65 std::string_view hash_fn);
66
67/**
68* For schemes which can sign an externally computed prehash
69*
70* Returns the name of the hash function the caller used, if it was named
71* (either in the prehash option or via with_hash; if both were given they
72* must agree), or nullopt if the input is an unidentified digest.
73*
74* Must only be called if using_externally_computed_prehash() is true.
75*/
76std::optional<std::string> externally_computed_prehash_name(const PK_Signature_Options_Reader& options);
77
78/**
79* For keys implemented in software
80*
81* Such keys have only the "base" provider, so any other provider request is
82* rejected. Reading the option here also acknowledges it.
83*
84* @throws Provider_Not_Found if a different provider was requested
85*/
86template <typename OptionsT>
87void require_software_provider(const OptionsT& options, std::string_view algo_name) {
88 if(options.using_provider()) {
89 throw Provider_Not_Found(algo_name, options.provider().value());
90 }
91}
92
93/**
94* For keys held in hardware (PKCS #11, TPM)
95*
96* Such keys have exactly one implementation, so the provider option must be
97* unset or name that provider. In particular "base", which names the software
98* implementation, is not available for them.
99*
100* @throws Provider_Not_Found otherwise
101*/
102template <typename OptionsT>
103void require_hardware_provider(const OptionsT& options, std::string_view algo_name, std::string_view provider_name) {
104 const auto& provider = options.provider();
105 if(provider.has_value() && *provider != provider_name) {
106 throw Provider_Not_Found(algo_name, *provider);
107 }
108}
109
110/**
111* For schemes whose signatures are always deterministic
112*
113* Any request for a deterministic signature is trivially satisfied, so this
114* just examines (and thereby acknowledges) the option.
115*/
116void acknowledge_always_deterministic(const PK_Signature_Options_Reader& options);
117
118} // namespace Botan
119
120#endif
static PK_Key_Agreement_Options_Reader read(const PK_Key_Agreement_Options &options)
static PK_KEM_Options_Reader read(const PK_KEM_Options &options)
static void throw_if_unexamined(const ReaderT &reader, std::string_view algo_name)
static PK_Signature_Options_Reader for_signing(const PK_Signature_Options &options)
static PK_Signature_Options_Reader for_verification(const PK_Signature_Options &options)
static PK_Encryption_Options_Reader read(const PK_Encryption_Options &options)
void require_hardware_provider(const OptionsT &options, std::string_view algo_name, std::string_view provider_name)
PK_Encryption_Options parse_legacy_enc_options(const Public_Key &key, std::string_view params)
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 require_software_provider(const OptionsT &options, std::string_view algo_name)
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)