Botan 3.14.1
Crypto and TLS for C&
pk_ops_impl.h
Go to the documentation of this file.
1
2/*
3* (C) 2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PK_OPERATION_IMPL_H_
9#define BOTAN_PK_OPERATION_IMPL_H_
10
11#include <botan/pk_ops.h>
12
13namespace Botan {
14
15class HashFunction;
16class KDF;
18
19} // namespace Botan
20
21namespace Botan::PK_Ops {
22
23/**
24* Describes the raw shared key produced by a KEM, ie the value returned when
25* no KDF is applied to it.
26*/
27enum class KemSharedKeyQuality : uint8_t {
28 /// The raw output is already a uniformly random key (eg ML-KEM), so it
29 /// is returned directly if the caller does not specify a KDF
31 /// The raw output is not uniform (eg RSA-KEM, or a key agreement used as a
32 /// KEM), so the caller must either specify a KDF or explicitly request the
33 /// raw output
35};
36
37// NOLINTBEGIN(*-special-member-functions)
38
39#if defined(BOTAN_HAS_RSA_ENCRYPTION_PADDING)
40
41class Encryption_with_Padding : public Encryption {
42 public:
43 ~Encryption_with_Padding() override;
44
45 size_t max_input_bits() const override;
46
47 std::vector<uint8_t> encrypt(std::span<const uint8_t> ptext, RandomNumberGenerator& rng) override;
48
49 protected:
50 explicit Encryption_with_Padding(const PK_Encryption_Options_Reader& options);
51
52 private:
53 virtual size_t max_ptext_input_bits() const = 0;
54
55 virtual std::vector<uint8_t> raw_encrypt(std::span<const uint8_t> msg, RandomNumberGenerator& rng) = 0;
56 std::unique_ptr<EncryptionPaddingScheme> m_padding;
57};
58
59class Decryption_with_Padding : public Decryption {
60 public:
61 ~Decryption_with_Padding() override;
62
63 size_t decrypt(uint8_t& valid_mask, std::span<uint8_t> ptext, std::span<const uint8_t> ctext) override;
64
65 protected:
66 explicit Decryption_with_Padding(const PK_Encryption_Options_Reader& options);
67
68 private:
69 virtual secure_vector<uint8_t> raw_decrypt(std::span<const uint8_t> ctext) = 0;
70 std::unique_ptr<EncryptionPaddingScheme> m_padding;
71};
72
73#endif
74
76 public:
78
79 void update(std::span<const uint8_t> input) override;
80 bool is_valid_signature(std::span<const uint8_t> sig) override;
81
82 std::string hash_function() const final;
83
84 protected:
86
87 explicit Verification_with_Hash(const AlgorithmIdentifier& alg_id,
88 std::string_view pk_algo,
89 bool allow_null_parameters = false);
90
91 /**
92 * Perform a signature check operation
93 * @param msg the message
94 * @param sig the signature
95 * @returns if sig is a valid signature for msg
96 */
97 virtual bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) = 0;
98
99 private:
100 std::unique_ptr<HashFunction> m_hash;
101};
102
104 public:
105 void update(std::span<const uint8_t> input) override;
106
107 std::vector<uint8_t> sign(RandomNumberGenerator& rng) override;
108
110
111 protected:
112 explicit Signature_with_Hash(const PK_Signature_Options_Reader& options);
113
114 std::string hash_function() const final;
115
116#if defined(BOTAN_HAS_RFC6979_GENERATOR)
117 std::string rfc6979_hash_function() const;
118#endif
119
120 private:
121 virtual std::vector<uint8_t> raw_sign(std::span<const uint8_t> input, RandomNumberGenerator& rng) = 0;
122
123 std::unique_ptr<HashFunction> m_hash;
124};
125
127 public:
128 secure_vector<uint8_t> agree(size_t key_len,
129 std::span<const uint8_t> other_key,
130 std::span<const uint8_t> salt) override;
131
133
134 protected:
135 /**
136 * @param options must specify either a KDF or the raw shared key,
137 * since the agreed value is never a uniform key
138 */
140
141 private:
142 virtual secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) = 0;
143 std::unique_ptr<KDF> m_kdf;
144};
145
147 public:
148 void kem_encrypt(std::span<uint8_t> out_encapsulated_key,
149 std::span<uint8_t> out_shared_key,
151 size_t desired_shared_key_len,
152 std::span<const uint8_t> salt) final;
153
154 size_t shared_key_length(size_t desired_shared_key_len) const final;
155
157
158 protected:
159 virtual void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
160 std::span<uint8_t> out_raw_shared_key,
161 RandomNumberGenerator& rng) = 0;
162
163 virtual size_t raw_kem_shared_key_length() const = 0;
164
165 /**
166 * @param options the KEM options; if these do not specify a KDF then
167 * whether the raw output can be used directly is decided by raw_key
168 */
170
171 private:
172 std::unique_ptr<KDF> m_kdf;
173};
174
176 public:
177 void kem_decrypt(std::span<uint8_t> out_shared_key,
178 std::span<const uint8_t> encapsulated_key,
179 size_t desired_shared_key_len,
180 std::span<const uint8_t> salt) final;
181
182 size_t shared_key_length(size_t desired_shared_key_len) const final;
183
185
186 protected:
187 virtual void raw_kem_decrypt(std::span<uint8_t> out_raw_shared_key,
188 std::span<const uint8_t> encapsulated_key) = 0;
189
190 virtual size_t raw_kem_shared_key_length() const = 0;
191
192 /**
193 * @param options the KEM options; if these do not specify a KDF then
194 * whether the raw output can be used directly is decided by raw_key
195 */
197
198 private:
199 std::unique_ptr<KDF> m_kdf;
200};
201
202// NOLINTEND(*-special-member-functions)
203
204} // namespace Botan::PK_Ops
205
206#endif
KEM_Decryption_with_KDF(const PK_KEM_Options_Reader &options, KemSharedKeyQuality raw_key)
Definition pk_ops.cpp:292
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
Definition pk_ops.cpp:273
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
Definition pk_ops.cpp:265
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
Definition pk_ops.cpp:219
virtual size_t raw_kem_shared_key_length() const =0
KEM_Encryption_with_KDF(const PK_KEM_Options_Reader &options, KemSharedKeyQuality raw_key)
Definition pk_ops.cpp:259
size_t shared_key_length(size_t desired_shared_key_len) const final
Definition pk_ops.cpp:211
virtual void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_raw_shared_key, RandomNumberGenerator &rng)=0
Key_Agreement_with_KDF(const PK_Key_Agreement_Options_Reader &options)
Definition pk_ops.cpp:77
secure_vector< uint8_t > agree(size_t key_len, std::span< const uint8_t > other_key, std::span< const uint8_t > salt) override
Definition pk_ops.cpp:89
std::vector< uint8_t > sign(RandomNumberGenerator &rng) override
Definition pk_ops.cpp:159
Signature_with_Hash(const PK_Signature_Options_Reader &options)
Definition pk_ops.cpp:136
std::string hash_function() const final
Definition pk_ops.cpp:169
void update(std::span< const uint8_t > input) override
Definition pk_ops.cpp:202
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)
Definition pk_ops.cpp:164
bool is_valid_signature(std::span< const uint8_t > sig) override
Definition pk_ops.cpp:206
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128