8#include <botan/internal/pk_ops_impl.h>
10#include <botan/assert.h>
11#include <botan/hash.h>
13#include <botan/pk_options_readers.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/parsing.h>
18#include <botan/internal/pk_options_impl.h>
20#if defined(BOTAN_HAS_RAW_HASH_FN)
21 #include <botan/internal/raw_hash.h>
24#if defined(BOTAN_HAS_RSA_ENCRYPTION_PADDING)
25 #include <botan/internal/enc_padding.h>
31 throw Not_Implemented(
"This signature scheme does not have an algorithm identifier available");
34#if defined(BOTAN_HAS_RSA_ENCRYPTION_PADDING)
39PK_Ops::Encryption_with_Padding::~Encryption_with_Padding() =
default;
41size_t PK_Ops::Encryption_with_Padding::max_input_bits()
const {
42 return 8 * m_padding->maximum_input_size(max_ptext_input_bits());
45std::vector<uint8_t> PK_Ops::Encryption_with_Padding::encrypt(std::span<const uint8_t> msg,
46 RandomNumberGenerator& rng) {
47 const size_t max_input_bits = max_ptext_input_bits();
48 const size_t max_input_bytes = (max_input_bits + 7) / 8;
52 const size_t written = m_padding->pad(padded_ptext, msg, max_input_bits, rng);
53 return raw_encrypt(std::span{padded_ptext}.first(written), rng);
56PK_Ops::Decryption_with_Padding::Decryption_with_Padding(
const PK_Encryption_Options_Reader& options) :
57 m_padding(EncryptionPaddingScheme::create_or_throw(options)) {}
59PK_Ops::Decryption_with_Padding::~Decryption_with_Padding() =
default;
61size_t PK_Ops::Decryption_with_Padding::decrypt(uint8_t& valid_mask,
62 std::span<uint8_t> ptext,
63 std::span<const uint8_t> ctext) {
67 BOTAN_ARG_CHECK(ptext.size() >= raw.size(),
"Output buffer too small for decryption");
69 const auto len = m_padding->unpad(ptext, raw);
72 return len.value_or(0);
82 "Key agreement does not produce a uniform shared key, so a KDF must be specified (or the raw shared key "
83 "explicitly requested)");
90 std::span<const uint8_t> other_key,
91 std::span<const uint8_t> salt) {
92 if(!salt.empty() && m_kdf ==
nullptr) {
93 throw Invalid_Argument(
"PK_Key_Agreement::derive_key requires a KDF to use a salt");
98 return m_kdf->derive_key(key_len, z, salt.data(), salt.size());
108#if defined(BOTAN_HAS_RAW_HASH_FN)
112 return std::make_unique<RawHashFunction>(
"Raw", 0);
114 throw Lookup_Error(
"Signing an externally computed prehash requires the raw_hash module");
127 throw Invalid_Argument(
"This algorithm does not support prehashing with a different hash");
137 Signature(), m_hash(validate_options_returning_hash(options)) {}
141#if defined(BOTAN_HAS_RFC6979_GENERATOR)
142std::string PK_Ops::Signature_with_Hash::rfc6979_hash_function()
const {
143 std::string hash = m_hash->name();
152 return m_hash->name();
160 const std::vector<uint8_t> msg = m_hash->final_stdvec();
161 return raw_sign(msg, rng);
165 Verification(), m_hash(validate_options_returning_hash(options)) {}
170 return m_hash->name();
174 std::string_view pk_algo,
175 bool allow_null_parameters) {
179 fmt(
"Unexpected AlgorithmIdentifier OID {} in association with {} key", alg_id.
oid(), pk_algo));
182 const auto oid_info =
split_on(*oid_name,
'/');
184 if(oid_info.size() != 2 || oid_info[0] != pk_algo) {
186 fmt(
"Unexpected AlgorithmIdentifier OID {} in association with {} key", alg_id.
oid(), pk_algo));
191 if(!allow_null_parameters) {
192 throw Decoding_Error(
fmt(
"Unexpected NULL AlgorithmIdentifier parameters for {}", pk_algo));
195 throw Decoding_Error(
fmt(
"Unexpected AlgorithmIdentifier parameters for {}", pk_algo));
207 const std::vector<uint8_t> msg = m_hash->final_stdvec();
213 return desired_shared_key_len;
220 std::span<uint8_t> out_shared_key,
222 size_t desired_shared_key_len,
223 std::span<const uint8_t> salt) {
224 BOTAN_ARG_CHECK(salt.empty() || m_kdf,
"PK_KEM_Encryptor::encrypt requires a KDF to use a salt");
229 out_shared_key.size(), desired_shared_key_len,
"KDF output length and shared key length match");
233 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
249 "This KEM does not produce a uniform shared key, so a KDF must be specified (or the raw shared key "
250 "explicitly requested)");
261 m_kdf(kem_kdf_from_options(options, raw_key)) {}
267 return desired_shared_key_len;
274 std::span<const uint8_t> encapsulated_key,
275 size_t desired_shared_key_len,
276 std::span<const uint8_t> salt) {
277 BOTAN_ARG_CHECK(salt.empty() || m_kdf,
"PK_KEM_Decryptor::decrypt requires a KDF to use a salt");
281 out_shared_key.size(), desired_shared_key_len,
"KDF output length and shared key length match");
285 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
294 m_kdf(kem_kdf_from_options(options, raw_key)) {}
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
#define BOTAN_ARG_CHECK(expr, msg)
bool parameters_are_empty() const
bool parameters_are_null() const
static constexpr Mask< T > from_choice(Choice c)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
std::optional< std::string > registered_name() const
bool using_raw_shared_key() const
const std::optional< std::string > & kdf() const
bool using_raw_shared_key() const
const std::optional< std::string > & kdf() const
~KEM_Decryption_with_KDF() override
KEM_Decryption_with_KDF(const PK_KEM_Options_Reader &options, KemSharedKeyQuality raw_key)
void kem_decrypt(std::span< uint8_t > out_shared_key, std::span< const uint8_t > encapsulated_key, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
virtual void raw_kem_decrypt(std::span< uint8_t > out_raw_shared_key, std::span< const uint8_t > encapsulated_key)=0
size_t shared_key_length(size_t desired_shared_key_len) const final
virtual size_t raw_kem_shared_key_length() const =0
void kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
~KEM_Encryption_with_KDF() override
virtual size_t raw_kem_shared_key_length() const =0
KEM_Encryption_with_KDF(const PK_KEM_Options_Reader &options, KemSharedKeyQuality raw_key)
size_t shared_key_length(size_t desired_shared_key_len) const final
virtual void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_raw_shared_key, RandomNumberGenerator &rng)=0
virtual size_t encapsulated_key_length() const =0
~Key_Agreement_with_KDF() override
Key_Agreement_with_KDF(const PK_Key_Agreement_Options_Reader &options)
secure_vector< uint8_t > agree(size_t key_len, std::span< const uint8_t > other_key, std::span< const uint8_t > salt) override
std::vector< uint8_t > sign(RandomNumberGenerator &rng) override
Signature_with_Hash(const PK_Signature_Options_Reader &options)
std::string hash_function() const final
~Signature_with_Hash() override
void update(std::span< const uint8_t > input) override
virtual AlgorithmIdentifier algorithm_identifier() const
virtual void update(std::span< const uint8_t > input)=0
std::string hash_function() const final
void update(std::span< const uint8_t > input) override
~Verification_with_Hash() override
virtual bool verify(std::span< const uint8_t > msg, std::span< const uint8_t > sig)=0
Verification_with_Hash(const PK_Signature_Options_Reader &options)
bool is_valid_signature(std::span< const uint8_t > sig) override
bool using_externally_computed_prehash() const
const std::optional< std::string > & prehash_function() const
const std::string & hash_function_name() const
bool using_prehash() const
std::string fmt(std::string_view format, const T &... args)
std::vector< std::string > split_on(std::string_view str, char delim)
std::optional< std::string > externally_computed_prehash_name(const PK_Signature_Options_Reader &options)
std::vector< T, secure_allocator< T > > secure_vector