9#include <botan/tpm2_rsa.h>
11#include <botan/mem_ops.h>
12#include <botan/pk_options_readers.h>
13#include <botan/pss_params.h>
15#include <botan/internal/pk_options_impl.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/fmt.h>
19#include <botan/internal/sig_padding.h>
20#include <botan/internal/stl_util.h>
21#include <botan/internal/tpm2_algo_mappings.h>
22#include <botan/internal/tpm2_pkops.h>
23#include <botan/internal/tpm2_util.h>
25#include <tss2/tss2_esys.h>
31 std::move(handle), std::move(session_bundle), rsa_pubkey_components_from_tss2_public(public_blob)) {}
41 const TPM2B_PUBLIC* public_blob,
42 std::span<const uint8_t> private_blob) :
44 std::move(session_bundle),
45 rsa_pubkey_components_from_tss2_public(public_blob),
50 const std::pair<BigInt, BigInt>& pubkey,
51 std::span<const uint8_t> private_blob) :
59 std::span<const uint8_t> auth_value,
62 std::optional<uint32_t> exponent) {
65 const TPM2B_SENSITIVE_CREATE sensitive_data = {
78 const TPMT_PUBLIC key_template = {
83 .nameAlg = TPM2_ALG_SHA256,
92 .sensitive_data_origin =
true,
93 .user_with_auth =
true,
113 .algorithm = TPM2_ALG_NULL,
114 .keyBits = {.sym = 0},
115 .mode = {.sym = TPM2_ALG_NULL},
127 .scheme = TPM2_ALG_NULL,
128 .details = {.anySig = {.hashAlg = TPM2_ALG_NULL}},
130 .keyBits = keylength,
131 .exponent = exponent.value_or(0 ),
147 throw Not_Implemented(
"TPM2 RSA signing without padding is not supported");
151 throw Invalid_Argument(
"RSA signing padding scheme must at least specify a hash function");
156 throw Not_Implemented(
"TPM2 RSA signing does not support specifying the salt size");
160 const std::string padding =
fmt(
"{}({})", options.
padding().value(), hash_name);
164 throw Invalid_Argument(
fmt(
"TPM2 RSA signing with {} cannot produce deterministic signatures", padding));
169 throw Not_Implemented(
Botan::fmt(
"RSA signing with padding scheme {}", padding));
173 .signature_scheme = sig_scheme.value(),
174 .hash_name = hash_name,
179size_t signature_length_for_rsa_key_handle(
const SessionBundle& sessions,
const Object& key_handle) {
180 return key_handle._public_info(sessions, TPM2_ALG_RSA).pub->publicArea.parameters.rsaDetail.keyBits / 8;
185 RSA_Signature_Operation(
const Object&
object,
186 const SessionBundle& sessions,
187 const PK_Signature_Options_Reader& options) :
188 Signature_Operation(object, sessions, select_signature_algorithms(options)),
191 m_padding_name(SignaturePaddingScheme::create_or_throw(options)->
name()) {}
193 size_t signature_length()
const override {
return signature_length_for_rsa_key_handle(sessions(), key_handle()); }
195 AlgorithmIdentifier algorithm_identifier()
const override {
203 const std::string full_name =
"RSA/" + m_padding_name;
206 }
catch(Lookup_Error&) {}
208 if(m_padding_name.starts_with(
"PSS(")) {
210 return AlgorithmIdentifier(
"RSA/PSS", parameters);
213 throw Invalid_Argument(
fmt(
"Signatures using RSA/{} are not supported", m_padding_name));
217 std::vector<uint8_t> marshal_signature(
const TPMT_SIGNATURE& signature)
const override {
218 const auto& sig = [&] {
219 if(signature.sigAlg == TPM2_ALG_RSASSA) {
220 return signature.signature.rsassa;
221 }
else if(signature.sigAlg == TPM2_ALG_RSAPSS) {
222 return signature.signature.rsapss;
224 throw Invalid_State(
fmt(
"TPM2 returned an unexpected signature scheme {}", signature.sigAlg));
231 std::string m_padding_name;
236 RSA_Verification_Operation(
const Object&
object,
237 const SessionBundle& sessions,
238 const PK_Signature_Options_Reader& options) :
239 Verification_Operation(object, sessions, select_signature_algorithms(options)) {}
242 TPMT_SIGNATURE unmarshal_signature(std::span<const uint8_t> signature)
const override {
243 BOTAN_ARG_CHECK(signature.size() == signature_length_for_rsa_key_handle(sessions(), key_handle()),
244 "Unexpected signature byte length");
247 sig.sigAlg = scheme().scheme;
249 auto& sig_data = [&]() -> TPMS_SIGNATURE_RSA& {
250 if(sig.sigAlg == TPM2_ALG_RSASSA) {
251 return sig.signature.rsassa;
252 }
else if(sig.sigAlg == TPM2_ALG_RSAPSS) {
253 return sig.signature.rsapss;
255 throw Invalid_State(
fmt(
"Requested an unexpected signature scheme {}", sig.sigAlg));
258 sig_data.hash = scheme().details.any.hashAlg;
264TPMT_RSA_DECRYPT select_encryption_algorithms(
const PK_Encryption_Options_Reader& options) {
265 if(!options.using_padding()) {
266 throw Lookup_Error(
"TPM2 RSA encryption requires specifying a padding scheme");
269 std::string padding = options.padding().value();
271 if(padding ==
"OAEP") {
272 if(!options.using_hash()) {
273 throw Lookup_Error(
"TPM2 RSA OAEP requires specifying a hash function");
275 const std::string& hash_name = options.hash_function_name();
278 if(options.using_mgf1_hash() && options.mgf1_hash_function().value() != hash_name) {
279 throw Not_Implemented(
"TPM2 RSA OAEP does not support a distinct MGF1 hash function");
286 padding =
fmt(
"OAEP({})", hash_name);
291 throw Not_Implemented(
Botan::fmt(
"RSA encryption with padding scheme {}", padding));
293 return scheme.value();
296class RSA_Encryption_Operation final :
public PK_Ops::Encryption {
298 RSA_Encryption_Operation(
const Object&
object,
299 const SessionBundle& sessions,
300 const PK_Encryption_Options_Reader& options) :
301 m_key_handle(object), m_sessions(sessions), m_scheme(select_encryption_algorithms(options)) {}
303 std::vector<uint8_t>
encrypt(std::span<const uint8_t> msg, Botan::RandomNumberGenerator& )
override {
311 Esys_RSA_Encrypt(m_key_handle.context()->esys_context(),
312 m_key_handle.transient_handle(),
329 size_t max_input_bits()
const override {
330 const auto max_ptext_bytes =
331 (m_key_handle._public_info(m_sessions, TPM2_ALG_RSA).pub->publicArea.parameters.rsaDetail.keyBits - 1) / 8;
332 auto hash_output_bytes = [](TPM2_ALG_ID hash) ->
size_t {
336 case TPM2_ALG_SHA256:
337 case TPM2_ALG_SHA3_256:
339 case TPM2_ALG_SHA384:
340 case TPM2_ALG_SHA3_384:
342 case TPM2_ALG_SHA512:
343 case TPM2_ALG_SHA3_512:
346 throw Invalid_State(
"Unexpected hash algorithm");
350 const auto max_input_bytes = [&]() ->
size_t {
351 switch(m_scheme.scheme) {
353 return max_ptext_bytes - 10;
355 return max_ptext_bytes - 2 * hash_output_bytes(m_scheme.details.oaep.hashAlg) - 1;
357 return max_ptext_bytes;
359 throw Invalid_State(
"Unexpected RSA encryption scheme");
363 return max_input_bytes * 8;
366 size_t ciphertext_length(
size_t )
const override {
367 return m_key_handle._public_info(m_sessions, TPM2_ALG_RSA).pub->publicArea.parameters.rsaDetail.keyBits - 1;
371 const Object& m_key_handle;
372 const SessionBundle& m_sessions;
373 TPMT_RSA_DECRYPT m_scheme;
376class RSA_Decryption_Operation final :
public PK_Ops::Decryption {
378 RSA_Decryption_Operation(
const Object&
object,
379 const SessionBundle& sessions,
380 const PK_Encryption_Options_Reader& options) :
381 m_key_handle(object), m_sessions(sessions), m_scheme(select_encryption_algorithms(options)) {}
383 size_t decrypt(uint8_t& valid_mask, std::span<uint8_t> ptext, std::span<const uint8_t> input)
override {
396 const auto rc = Esys_RSA_Decrypt(m_key_handle.context()->esys_context(),
397 m_key_handle.transient_handle(),
411 constexpr size_t default_plaintext_length = 32;
419 auto* out = &dummy_plaintext;
420 auto* maybe_plaintext = plaintext.get();
424 const auto decrypted =
as_span(*out);
425 BOTAN_ARG_CHECK(ptext.size() >= decrypted.size(),
"Output buffer too small for decryption");
426 copy_mem(ptext.first(decrypted.size()), decrypted);
427 return decrypted.size();
430 size_t plaintext_length(
size_t )
const override {
431 return m_key_handle._public_info(m_sessions, TPM2_ALG_RSA).pub->publicArea.parameters.rsaDetail.keyBits / 8;
434 size_t ciphertext_length(
size_t )
const override {
435 return m_key_handle._public_info(m_sessions, TPM2_ALG_RSA).pub->publicArea.parameters.rsaDetail.keyBits / 8;
439 const Object& m_key_handle;
440 const SessionBundle& m_sessions;
441 TPMT_RSA_DECRYPT m_scheme;
449 return std::make_unique<RSA_Verification_Operation>(
handles(),
sessions(), options);
456 return std::make_unique<RSA_Signature_Operation>(
handles(),
sessions(), options);
463 return std::make_unique<RSA_Encryption_Operation>(
handles(),
sessions(), options);
470 return std::make_unique<RSA_Decryption_Operation>(
handles(),
sessions(), options);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ARG_CHECK(expr, msg)
static constexpr Mask< T > from_choice(Choice c)
static constexpr Mask< T > is_equal(T x, T y)
static OID from_string(std::string_view str)
const std::optional< std::string > & padding() const
bool using_padding() const
const std::string & hash_function_name() const
bool using_salt_size() const
bool using_deterministic_signature() const
static PSS_Params from_padding_name(std::string_view padding_name)
std::vector< uint8_t > serialize() const
std::string algo_name() const override
ESYS_TR transient_handle() const noexcept
static std::unique_ptr< PrivateKey > create_transient_from_template(const std::shared_ptr< Context > &ctx, const SessionBundle &sessions, ESYS_TR parent, const TPMT_PUBLIC &key_template, const TPM2B_SENSITIVE_CREATE &sensitive_data)
const SessionBundle & sessions() const
const Object & handles() const
const SessionBundle & sessions() const
RSA_PrivateKey(Object handle, SessionBundle sessions, const TPM2B_PUBLIC *public_blob, std::span< const uint8_t > private_blob={})
std::unique_ptr< PK_Ops::Signature > _create_signature_op(Botan::RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
static std::unique_ptr< TPM2::PrivateKey > create_unrestricted_transient(const std::shared_ptr< Context > &ctx, const SessionBundle &sessions, std::span< const uint8_t > auth_value, const TPM2::PrivateKey &parent, uint16_t keylength, std::optional< uint32_t > exponent={})
std::unique_ptr< PK_Ops::Decryption > _create_decryption_op(Botan::RandomNumberGenerator &rng, const PK_Encryption_Options_Reader &options) const override
std::unique_ptr< PK_Ops::Encryption > _create_encryption_op(Botan::RandomNumberGenerator &rng, const PK_Encryption_Options_Reader &options) const override
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
RSA_PublicKey(Object handle, SessionBundle sessions, const TPM2B_PUBLIC *public_blob)
constexpr void conditional_swap_ptr(bool cnd, T &x, T &y)
std::string decrypt(std::span< const uint8_t > input, std::string_view passphrase)
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)
std::optional< TPMT_SIG_SCHEME > rsa_signature_scheme_botan_to_tss2(std::string_view name)
constexpr T init_empty()
Create an empty TPM2 buffer of the given type.
constexpr void check_rc(std::string_view location, TSS2_RC rc)
std::optional< TPMT_RSA_DECRYPT > rsa_encryption_scheme_botan_to_tss2(std::string_view padding)
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
constexpr T init_with_size(size_t length)
Create a TPM2 buffer of a given type and length.
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
void require_hardware_provider(const OptionsT &options, std::string_view algo_name, std::string_view provider_name)
constexpr auto out_ptr(T &outptr) noexcept
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
static TPMA_OBJECT render(ObjectAttributes attributes)