7#include <botan/pubkey.h>
9#include <botan/ber_dec.h>
10#include <botan/bigint.h>
11#include <botan/der_enc.h>
12#include <botan/mem_ops.h>
13#include <botan/pk_ops.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/parsing.h>
18#include <botan/internal/pss_params.h>
19#include <botan/internal/stl_util.h>
24 uint8_t valid_mask = 0;
29 throw Decoding_Error(
"Invalid public key ciphertext, cannot decrypt");
37 size_t expected_pt_len,
39 const uint8_t required_content_bytes[],
40 const uint8_t required_content_offsets[],
41 size_t required_contents_length)
const {
44 uint8_t decrypt_valid = 0;
50 decoded.resize(expected_pt_len);
52 for(
size_t i = 0; i != required_contents_length; ++i) {
64 const uint8_t exp = required_content_bytes[i];
65 const uint8_t off = required_content_offsets[i];
67 BOTAN_ASSERT(off < expected_pt_len,
"Offset in range of plaintext");
75 valid_mask.select_n(decoded.data(), decoded.data(), fake_pms.data(), expected_pt_len);
82 size_t expected_pt_len,
89 std::string_view padding,
90 std::string_view provider) {
106std::vector<uint8_t> PK_Encryptor_EME::enc(
const uint8_t in[],
size_t length,
RandomNumberGenerator& rng)
const {
107 return m_op->encrypt(std::span{in, length}, rng);
111 return m_op->max_input_bits() / 8;
116 std::string_view padding,
117 std::string_view provider) {
133secure_vector<uint8_t> PK_Decryptor_EME::do_decrypt(uint8_t& valid_mask,
const uint8_t in[],
size_t in_len)
const {
134 return m_op->decrypt(valid_mask, {in, in_len});
154 return m_op->encapsulated_key_length();
158 std::span<uint8_t> out_shared_key,
160 size_t desired_shared_key_len,
161 std::span<const uint8_t> salt) {
164 "not enough space for shared key");
165 m_op->kem_encrypt(out_encapsulated_key, out_shared_key, rng, desired_shared_key_len, salt);
169 return m_op->shared_key_length(desired_shared_key_len);
173 return m_op->encapsulated_key_length();
178 std::string_view param,
179 std::string_view provider) {
192 std::span<const uint8_t> encap_key,
193 size_t desired_shared_key_len,
194 std::span<const uint8_t> salt) {
195 BOTAN_ARG_CHECK(out_shared_key.size() == shared_key_length(desired_shared_key_len),
196 "inconsistent size of shared key output buffer");
197 m_op->kem_decrypt(out_shared_key, encap_key, desired_shared_key_len, salt);
202 std::string_view kdf,
203 std::string_view provider) {
222 std::string_view params)
const {
227 const std::span<const uint8_t> in,
228 std::string_view params)
const {
233 size_t key_len,
const uint8_t in[],
size_t in_len,
const uint8_t salt[],
size_t salt_len)
const {
234 return SymmetricKey(m_op->agree(key_len, {in, in_len}, {salt, salt_len}));
249 std::string_view emsa,
251 std::string_view provider) {
256 m_sig_format = format;
259 check_der_format_supported(format, m_parts);
263 return m_op->algorithm_identifier();
267 return m_op->hash_function();
280 m_op->update({in, length});
285std::vector<uint8_t> der_encode_signature(std::span<const uint8_t> sig,
size_t parts,
size_t part_size) {
286 if(sig.size() % parts != 0 || sig.size() != parts * part_size) {
290 BufferSlicer bs_sig(sig);
291 std::vector<BigInt> sig_parts;
292 sig_parts.reserve(parts);
293 for(
size_t i = 0; i != parts; ++i) {
297 std::vector<uint8_t> output;
298 DER_Encoder(output).start_sequence().encode_list(sig_parts).end_cons();
306 return m_op->signature_length();
310 return m_op->signature_length() + (8 + 4 * m_parts);
317 std::vector<uint8_t> sig = m_op->sign(rng);
322 return der_encode_signature(sig, m_parts, m_part_size);
329 std::string_view emsa,
331 std::string_view provider) {
336 m_sig_format = format;
339 check_der_format_supported(format, m_parts);
344 std::string_view provider) {
354 check_der_format_supported(m_sig_format, m_parts);
367 check_der_format_supported(format, m_parts);
368 m_sig_format = format;
381 m_op->update({in, length});
386std::vector<uint8_t> decode_der_signature(
const uint8_t sig[],
size_t length,
size_t sig_parts,
size_t sig_part_size) {
387 std::vector<uint8_t> real_sig;
398 real_sig += sig_part.
serialize(sig_part_size);
402 if(count != sig_parts) {
403 throw Decoding_Error(
"PK_Verifier: signature size invalid");
406 const std::vector<uint8_t> reencoded = der_encode_signature(real_sig, sig_parts, sig_part_size);
408 if(reencoded.size() != length ||
CT::is_equal(reencoded.data(), sig, reencoded.size()).as_bool() ==
false) {
409 throw Decoding_Error(
"PK_Verifier: signature is not the canonical DER encoding");
419 return m_op->is_valid_signature({sig, length});
421 bool decoding_success =
false;
422 std::vector<uint8_t> real_sig;
425 real_sig = decode_der_signature(sig, length, m_parts, m_part_size);
426 decoding_success =
true;
429 bool accept = m_op->is_valid_signature(real_sig);
431 return accept && decoding_success;
433 throw Internal_Error(
"PK_Verifier: Invalid signature format enum");
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
virtual std::string algo_name() const =0
BER_Decoder & decode(bool &out)
BER_Decoder start_sequence()
static BigInt from_bytes(std::span< const uint8_t > bytes)
T serialize(size_t len) const
static constexpr Mask< T > is_equal(T x, T y)
PK_Decryptor_EME(const Private_Key &key, RandomNumberGenerator &rng, std::string_view eme, std::string_view provider="")
~PK_Decryptor_EME() override
size_t plaintext_length(size_t ptext_len) const override
secure_vector< uint8_t > decrypt_or_random(const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
~PK_Encryptor_EME() override
PK_Encryptor_EME(const Public_Key &key, RandomNumberGenerator &rng, std::string_view padding, std::string_view provider="")
size_t maximum_input_size() const override
size_t ciphertext_length(size_t ptext_len) const override
size_t encapsulated_key_length() const
PK_KEM_Decryptor(const Private_Key &key, RandomNumberGenerator &rng, std::string_view kem_param="", std::string_view provider="")
size_t shared_key_length(size_t desired_shared_key_len) const
PK_KEM_Encryptor(const Public_Key &key, std::string_view kem_param="", std::string_view provider="")
KEM_Encapsulation encrypt(RandomNumberGenerator &rng, size_t desired_shared_key_len=32, std::span< const uint8_t > salt={})
size_t shared_key_length(size_t desired_shared_key_len) const
size_t encapsulated_key_length() const
size_t agreed_value_size() const
PK_Key_Agreement(const Private_Key &key, RandomNumberGenerator &rng, std::string_view kdf, std::string_view provider="")
SymmetricKey derive_key(size_t key_len, const uint8_t in[], size_t in_len, const uint8_t params[], size_t params_len) const
PK_Signer(const Private_Key &key, RandomNumberGenerator &rng, std::string_view padding, Signature_Format format=Signature_Format::Standard, std::string_view provider="")
size_t signature_length() const
std::vector< uint8_t > signature(RandomNumberGenerator &rng)
std::string hash_function() const
AlgorithmIdentifier algorithm_identifier() const
void set_input_format(Signature_Format format)
bool verify_message(const uint8_t msg[], size_t msg_length, const uint8_t sig[], size_t sig_length)
std::string hash_function() const
PK_Verifier(const Public_Key &pub_key, std::string_view padding, Signature_Format format=Signature_Format::Standard, std::string_view provider="")
bool check_signature(const uint8_t sig[], size_t length)
virtual std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
virtual Signature_Format default_x509_signature_format() const
virtual std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const
virtual std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const
virtual size_t message_part_size() const
virtual size_t message_parts() const
void random_vec(std::span< uint8_t > v)
int(* update)(CTX *, const void *, CC_LONG len)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
std::string fmt(std::string_view format, const T &... args)
std::vector< T, secure_allocator< T > > secure_vector
const uint8_t * cast_char_ptr_to_uint8(const char *s)