Botan 3.6.0
Crypto and TLS for C&
Botan::TPM2 Namespace Reference

Namespaces

namespace  detail
 

Classes

class  AttributeWrapper
 
class  Context
 
struct  CryptoCallbackState
 
class  EC_PrivateKey
 
class  EC_PublicKey
 
class  Error
 
struct  esys_liberator
 
class  HashFunction
 
class  Object
 
struct  ObjectAttributes
 
struct  ObjectHandles
 
class  ObjectSetter
 
class  PrivateKey
 
struct  PropMap
 
struct  PublicInfo
 
class  PublicKey
 
class  RandomNumberGenerator
 
class  RSA_PrivateKey
 
class  RSA_PublicKey
 
class  Session
 
struct  SessionAttributes
 
class  SessionBundle
 
class  Signature_Operation
 
class  Signature_Operation_Base
 
struct  SignatureAlgorithmSelection
 
class  Verification_Operation
 

Concepts

concept  tpm2_buffer
 

Typedefs

using TPMA_SESSION = uint8_t
 
template<typename T >
using unique_esys_ptr = std::unique_ptr<T, esys_liberator>
 A unique pointer type for ESYS handles that automatically frees the handle.
 

Functions

constexpr auto as_span (tpm2_buffer auto &data)
 Construct a std::span as a view into a TPM2 buffer.
 
constexpr auto as_span (tpm2_buffer auto &data, size_t length)
 
std::optional< TPM2_ALG_ID > asymmetric_algorithm_botan_to_tss2 (std::string_view algo_name) noexcept
 
std::optional< std::pair< TPMI_ALG_SYM, TPM2_KEY_BITS > > block_cipher_botan_to_tss2 (std::string_view cipher_name) noexcept
 
std::optional< std::string > block_cipher_tss2_to_botan (TPMI_ALG_SYM cipher_id, TPM2_KEY_BITS key_bits) noexcept
 
constexpr void check_rc (std::string_view location, TSS2_RC rc)
 
template<TSS2_RC... expected_errors>
requires (sizeof...(expected_errors) > 0)
constexpr TSS2_RC check_rc_expecting (std::string_view location, TSS2_RC rc)
 
std::optional< TPMT_SYM_DEF > cipher_botan_to_tss2 (std::string_view algo_name)
 
std::optional< TPMI_ALG_SYM_MODE > cipher_mode_botan_to_tss2 (std::string_view mode_name) noexcept
 
std::optional< std::string > cipher_mode_tss2_to_botan (TPMI_ALG_SYM_MODE mode_id)
 
std::optional< std::string > cipher_tss2_to_botan (TPMT_SYM_DEF cipher_def) noexcept
 
template<concepts::resizable_byte_buffer OutT>
constexpr OutT copy_into (const tpm2_buffer auto &data)
 
template<tpm2_buffer T>
constexpr T copy_into (std::span< const uint8_t > data)
 
template<tpm2_buffer T>
constexpr void copy_into (T &dest, std::span< const uint8_t > data)
 
std::optional< size_t > curve_id_order_byte_size (TPMI_ECC_CURVE curve_id)
 
std::optional< std::string > curve_id_tss2_to_botan (TPMI_ECC_CURVE mode_id)
 
void enable_crypto_callbacks (const std::shared_ptr< Context > &ctx)
 
std::string get_botan_hash_name (TPM2_ALG_ID hash_id)
 
TSS2_RC get_raw_rc (TSS2_RC rc)
 
std::optional< TPM2_ECC_CURVE > get_tpm2_curve_id (const OID &curve_oid)
 
TPMI_ALG_HASH get_tpm2_hash_type (std::string_view hash_name)
 
TPMT_SYM_DEF get_tpm2_sym_cipher_spec (std::string_view algo_name)
 
std::optional< TPMI_ALG_HASH > hash_algo_botan_to_tss2 (std::string_view hash_name) noexcept
 
std::optional< std::string > hash_algo_tss2_to_botan (TPMI_ALG_HASH hash_id) noexcept
 
template<tpm2_buffer T>
constexpr T init_empty ()
 Create an empty TPM2 buffer of the given type.
 
constexpr auto out_persistent_handle (Object &object)
 
constexpr auto out_transient_handle (Object &object)
 
template<typename MaskT , typename FieldPointerT >
 PropMap (MaskT, FieldPointerT) -> PropMap< MaskT, FieldPointerT >
 Deduction guide to simplify the creation of PropMap instances.
 
std::optional< TPMI_ALG_ASYM_SCHEME > rsa_encryption_padding_botan_to_tss2 (std::string_view name) noexcept
 
std::optional< TPMT_RSA_DECRYPT > rsa_encryption_scheme_botan_to_tss2 (std::string_view padding)
 
std::optional< TPMI_ALG_SIG_SCHEME > rsa_signature_padding_botan_to_tss2 (std::string_view padding_name) noexcept
 
std::optional< TPMT_SIG_SCHEME > rsa_signature_scheme_botan_to_tss2 (std::string_view name)
 

Typedef Documentation

◆ TPMA_SESSION

using Botan::TPM2::TPMA_SESSION = uint8_t

Definition at line 20 of file tpm2_session.h.

◆ unique_esys_ptr

template<typename T >
using Botan::TPM2::unique_esys_ptr = std::unique_ptr<T, esys_liberator>

A unique pointer type for ESYS handles that automatically frees the handle.

Definition at line 154 of file tpm2_util.h.

Function Documentation

◆ as_span() [1/2]

auto Botan::TPM2::as_span ( tpm2_buffer auto & data)
constexpr

Construct a std::span as a view into a TPM2 buffer.

Definition at line 102 of file tpm2_util.h.

102 {
103 return std::span{data.buffer, data.size};
104}

Referenced by as_span(), copy_into(), copy_into(), Botan::TPM2::PrivateKey::create_transient_from_template(), Botan::TPM2::Verification_Operation::is_valid_signature(), and Botan::TPM2::Signature_Operation::sign().

◆ as_span() [2/2]

auto Botan::TPM2::as_span ( tpm2_buffer auto & data,
size_t length )
constexpr

Set the size of data to length and construct a std::span as a view into data

Definition at line 108 of file tpm2_util.h.

108 {
109 BOTAN_ASSERT_NOMSG(length <= sizeof(data.buffer));
110 data.size = static_cast<decltype(data.size)>(length);
111 return as_span(data);
112}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
Definition tpm2_util.h:102

References as_span(), and BOTAN_ASSERT_NOMSG.

◆ asymmetric_algorithm_botan_to_tss2()

std::optional< TPM2_ALG_ID > Botan::TPM2::asymmetric_algorithm_botan_to_tss2 ( std::string_view algo_name)
inlinenodiscardnoexcept

Definition at line 26 of file tpm2_algo_mappings.h.

27 {
28 if(algo_name == "RSA") {
29 return TPM2_ALG_RSA;
30 } else if(algo_name == "ECC") {
31 return TPM2_ALG_ECC;
32 } else if(algo_name == "ECDSA") {
33 return TPM2_ALG_ECDSA;
34 } else if(algo_name == "ECDH") {
35 return TPM2_ALG_ECDH;
36 } else if(algo_name == "ECDAA") {
37 return TPM2_ALG_ECDAA;
38 } else {
39 return std::nullopt;
40 }
41}

Referenced by Botan::TPM2::Context::supports_algorithm().

◆ block_cipher_botan_to_tss2()

std::optional< std::pair< TPMI_ALG_SYM, TPM2_KEY_BITS > > Botan::TPM2::block_cipher_botan_to_tss2 ( std::string_view cipher_name)
inlinenodiscardnoexcept

Definition at line 159 of file tpm2_algo_mappings.h.

160 {
161 if(cipher_name == "AES-128") {
162 return std::pair{TPM2_ALG_AES, 128};
163 } else if(cipher_name == "AES-192") {
164 return std::pair{TPM2_ALG_AES, 192};
165 } else if(cipher_name == "AES-256") {
166 return std::pair{TPM2_ALG_AES, 256};
167 } else if(cipher_name == "SM4") {
168 return std::pair{TPM2_ALG_SM4, 128};
169 } else if(cipher_name == "Camellia-128") {
170 return std::pair{TPM2_ALG_CAMELLIA, 128};
171 } else if(cipher_name == "Camellia-192") {
172 return std::pair{TPM2_ALG_CAMELLIA, 192};
173 } else if(cipher_name == "Camellia-256") {
174 return std::pair{TPM2_ALG_CAMELLIA, 256};
175 } else if(cipher_name == "3DES") {
176 return std::pair{TPM2_ALG_TDES, 168};
177 } else {
178 return {};
179 }
180}

Referenced by cipher_botan_to_tss2(), and Botan::TPM2::Context::supports_algorithm().

◆ block_cipher_tss2_to_botan()

std::optional< std::string > Botan::TPM2::block_cipher_tss2_to_botan ( TPMI_ALG_SYM cipher_id,
TPM2_KEY_BITS key_bits )
inlinenodiscardnoexcept

Definition at line 120 of file tpm2_algo_mappings.h.

121 {
122 switch(cipher_id) {
123 case TPM2_ALG_AES:
124 if(key_bits == 128) {
125 return "AES-128";
126 } else if(key_bits == 192) {
127 return "AES-192";
128 } else if(key_bits == 256) {
129 return "AES-256";
130 }
131 break;
132
133 case TPM2_ALG_SM4:
134 if(key_bits == 128) {
135 return "SM4";
136 }
137 break;
138
139 case TPM2_ALG_CAMELLIA:
140 if(key_bits == 128) {
141 return "Camellia-128";
142 } else if(key_bits == 192) {
143 return "Camellia-192";
144 } else if(key_bits == 256) {
145 return "Camellia-256";
146 }
147 break;
148
149 case TPM2_ALG_TDES:
150 return "3DES";
151
152 default:
153 break;
154 }
155
156 return std::nullopt;
157}

Referenced by cipher_tss2_to_botan().

◆ check_rc()

◆ check_rc_expecting()

template<TSS2_RC... expected_errors>
requires (sizeof...(expected_errors) > 0)
TSS2_RC Botan::TPM2::check_rc_expecting ( std::string_view location,
TSS2_RC rc )
nodiscardconstexpr

Check the return code and throw an exception if an unexpected error occured.

Errors that are listed in the expected_errors parameter are considered expected and will not cause an exception to be thrown. Instead the error code is decoded and returned to the caller for further processing.

Exceptions
TPM2::Errorif an unexpected error occured.
Returns
TSS2_RC_SUCCESS or one of the expected error codes.

Definition at line 72 of file tpm2_util.h.

72 {
73 // If the RC is success, we can return early and avoid the decoding.
74 if(rc == TSS2_RC_SUCCESS) {
75 return rc;
76 }
77
78 // An error occured, we need to decode it to check if it was expected.
79 const TSS2_RC decoded_rc = get_raw_rc(rc);
80
81 // Check if the error is one of the expected and return those to the caller.
82 const bool is_expected_by_caller = ((decoded_rc == expected_errors) || ...);
83 if(is_expected_by_caller) {
84 return decoded_rc;
85 }
86
87 // The error was not expected, so call the normal error handling which
88 // will throw an exception.
89 check_rc(location, rc);
90
91 // We know, rc is not 'success', so this won't ever be reached.
92 return rc;
93}
uint32_t TSS2_RC
Forward declaration of TSS2 type for convenience.
Definition tpm2_error.h:15

References check_rc(), and get_raw_rc().

Referenced by Botan::TPM2::Verification_Operation::is_valid_signature().

◆ cipher_botan_to_tss2()

std::optional< TPMT_SYM_DEF > Botan::TPM2::cipher_botan_to_tss2 ( std::string_view algo_name)
inlinenodiscard

Definition at line 296 of file tpm2_algo_mappings.h.

296 {
297 SCAN_Name spec(algo_name);
298 if(spec.arg_count() == 0) {
299 return std::nullopt;
300 }
301
302 const auto cipher = block_cipher_botan_to_tss2(spec.arg(0));
303 const auto mode = cipher_mode_botan_to_tss2(spec.algo_name());
304
305 if(!cipher || !mode) {
306 return std::nullopt;
307 }
308
309 return TPMT_SYM_DEF{
310 .algorithm = cipher->first,
311 .keyBits = {.sym = cipher->second},
312 .mode = {.sym = mode.value()},
313 };
314}

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), block_cipher_botan_to_tss2(), and cipher_mode_botan_to_tss2().

Referenced by get_tpm2_sym_cipher_spec(), and Botan::TPM2::Context::supports_algorithm().

◆ cipher_mode_botan_to_tss2()

std::optional< TPMI_ALG_SYM_MODE > Botan::TPM2::cipher_mode_botan_to_tss2 ( std::string_view mode_name)
inlinenodiscardnoexcept

Definition at line 262 of file tpm2_algo_mappings.h.

262 {
263 if(mode_name == "CFB") {
264 return TPM2_ALG_CFB;
265 } else if(mode_name == "CBC") {
266 return TPM2_ALG_CBC;
267 } else if(mode_name == "ECB") {
268 return TPM2_ALG_ECB;
269 } else if(mode_name == "OFB") {
270 return TPM2_ALG_OFB;
271 } else if(mode_name == "CTR" || mode_name == "CTR-BE") {
272 return TPM2_ALG_CTR;
273 } else {
274 return std::nullopt;
275 }
276}

Referenced by cipher_botan_to_tss2(), and Botan::TPM2::Context::supports_algorithm().

◆ cipher_mode_tss2_to_botan()

std::optional< std::string > Botan::TPM2::cipher_mode_tss2_to_botan ( TPMI_ALG_SYM_MODE mode_id)
inlinenodiscard

Definition at line 182 of file tpm2_algo_mappings.h.

182 {
183 switch(mode_id) {
184 case TPM2_ALG_CFB:
185 return "CFB";
186 case TPM2_ALG_CBC:
187 return "CBC";
188 case TPM2_ALG_ECB:
189 return "ECB";
190 case TPM2_ALG_OFB:
191 return "OFB";
192 case TPM2_ALG_CTR:
193 return "CTR";
194 default: // TPMI_ALG_SYM_MODE is not an enum
195 return std::nullopt;
196 }
197}

Referenced by cipher_tss2_to_botan().

◆ cipher_tss2_to_botan()

std::optional< std::string > Botan::TPM2::cipher_tss2_to_botan ( TPMT_SYM_DEF cipher_def)
inlinenodiscardnoexcept
Returns
a Botan cipher mode name string if the cipher_id, key_bits and mode_name are known, otherwise std::nullopt

Definition at line 282 of file tpm2_algo_mappings.h.

282 {
283 const auto cipher_name = block_cipher_tss2_to_botan(cipher_def.algorithm, cipher_def.keyBits.sym);
284 if(!cipher_name) {
285 return std::nullopt;
286 }
287
288 const auto mode_name = cipher_mode_tss2_to_botan(cipher_def.mode.sym);
289 if(!mode_name) {
290 return std::nullopt;
291 }
292
293 return Botan::fmt("{}({})", mode_name.value(), cipher_name.value());
294}
std::optional< std::string > block_cipher_tss2_to_botan(TPMI_ALG_SYM cipher_id, TPM2_KEY_BITS key_bits) noexcept
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References block_cipher_tss2_to_botan(), cipher_mode_tss2_to_botan(), and Botan::fmt().

◆ copy_into() [1/3]

template<concepts::resizable_byte_buffer OutT>
OutT Botan::TPM2::copy_into ( const tpm2_buffer auto & data)
constexpr

Copy the content of the TPM2 buffer data into a new resizable byte buffer of the user's choosing.

Definition at line 133 of file tpm2_util.h.

133 {
134 OutT result;
135 result.resize(data.size);
136 copy_mem(result, as_span(data));
137 return result;
138}
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References as_span(), and Botan::copy_mem().

◆ copy_into() [2/3]

template<tpm2_buffer T>
T Botan::TPM2::copy_into ( std::span< const uint8_t > data)
constexpr

Create a TPM2 buffer from the provided data, assuming that the provided data is not larger than the capacity of the buffer type.

Definition at line 124 of file tpm2_util.h.

124 {
125 T result;
126 copy_into(result, data);
127 return result;
128}
FE_25519 T
Definition ge.cpp:34
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
Definition tpm2_util.h:117

References copy_into(), and T.

◆ copy_into() [3/3]

template<tpm2_buffer T>
void Botan::TPM2::copy_into ( T & dest,
std::span< const uint8_t > data )
constexpr

Copy the data into the TPM2 buffer dest, assuming that the provided data is not larger than the capacity of the buffer.

Definition at line 117 of file tpm2_util.h.

117 {
118 copy_mem(as_span(dest, data.size()), data);
119}

References as_span(), and Botan::copy_mem().

Referenced by copy_into(), Botan::TPM2::EC_PrivateKey::create_unrestricted_transient(), Botan::TPM2::RSA_PrivateKey::create_unrestricted_transient(), Botan::TPM2::PrivateKey::load_transient(), Botan::TPM2::Context::persist(), and Botan::TPM2::Session::tpm_nonce().

◆ curve_id_order_byte_size()

std::optional< size_t > Botan::TPM2::curve_id_order_byte_size ( TPMI_ECC_CURVE curve_id)
inlinenodiscard

Definition at line 221 of file tpm2_algo_mappings.h.

221 {
222 switch(curve_id) {
223 case TPM2_ECC_NIST_P192:
224 return 24;
225 case TPM2_ECC_NIST_P224:
226 return 28;
227 case TPM2_ECC_NIST_P256:
228 return 32;
229 case TPM2_ECC_NIST_P384:
230 return 48;
231 case TPM2_ECC_NIST_P521:
232 return 66; // Rounded up to the next full byte
233 case TPM2_ECC_SM2_P256:
234 return 32;
235 default:
236 return std::nullopt;
237 }
238}

◆ curve_id_tss2_to_botan()

std::optional< std::string > Botan::TPM2::curve_id_tss2_to_botan ( TPMI_ECC_CURVE mode_id)
inlinenodiscard

Definition at line 199 of file tpm2_algo_mappings.h.

199 {
200 // Currently, tpm2-tss does not include support for Brainpool curves or 25519/448.
201 // Once the corresponding PR (https://github.com/tpm2-software/tpm2-tss/pull/2897) is merged and released,
202 // this function should be updated.
203 switch(mode_id) {
204 case TPM2_ECC_NIST_P192:
205 return "secp192r1";
206 case TPM2_ECC_NIST_P224:
207 return "secp224r1";
208 case TPM2_ECC_NIST_P256:
209 return "secp256r1";
210 case TPM2_ECC_NIST_P384:
211 return "secp384r1";
212 case TPM2_ECC_NIST_P521:
213 return "secp521r1";
214 case TPM2_ECC_SM2_P256:
215 return "sm2p256v1";
216 default:
217 return std::nullopt;
218 }
219}

◆ enable_crypto_callbacks()

void Botan::TPM2::enable_crypto_callbacks ( const std::shared_ptr< Context > & ctx)

Enable the Botan crypto callbacks for the given ESYS context.

The callbacks may maintain two types of state:

  • 'userdata' is a pointer to a CryptoCallbackState object that is passed to all callback functions. This provides access to a random number generator specified by the user. The lifetime of this object is bound to the TPM2::Context.
  • 'context' is a pointer to a DigestCallbackState object that contains either a HashFunction or a MessageAuthenticationCode object. This holds the hash state between update callback invocations. The lifetime of this object is bound to the digest callbacks, hence *_finish() and *_abort() will delete the object.

The runtime crypto backend is available since TSS2 4.0.0 and later. Explicit support for SM4 was added in TSS2 4.1.0.

Note that the callback implementations should be defensive in regard to the input parameters. All pointers should be checked for nullptr before being dereferenced. Some output parameters (e.g. out-buffer lengths) may be regarded as optional, and should be checked for nullptr before being written to.

Error code conventions:

  • TSS2_ESYS_RC_BAD_REFERENCE: reference (typically userdata) invalid
  • TSS2_ESYS_RC_BAD_VALUE: invalid input (e.g. size != 0 w/ nullptr buffer)
  • TSS2_ESYS_RC_NOT_SUPPORTED: algorithm identifier not mapped to Botan
  • TSS2_ESYS_RC_NOT_IMPLEMENTED: algorithm not available (e.g. disabled)

Enable Botan's crypto callbacks in the TPM2-TSS for the given context.

Exceptions
Not_Implementedif the TPM2-TSS does not support crypto callbacks.

Definition at line 867 of file tpm2_crypto_backend.cpp.

867 {
868#if defined(BOTAN_TSS2_SUPPORTS_CRYPTO_CALLBACKS)
870
871 // clang-format off
872 ESYS_CRYPTO_CALLBACKS callbacks{
873 .rsa_pk_encrypt = &rsa_pk_encrypt,
874 .hash_start = &hash_start,
875 .hash_update = &hash_update,
876 .hash_finish = &hash_finish,
877 .hash_abort = &hash_abort,
878 .hmac_start = &hmac_start,
879 .hmac_update = &hmac_update,
880 .hmac_finish = &hmac_finish,
881 .hmac_abort = &hmac_abort,
882 .get_random2b = &get_random2b,
883 .get_ecdh_point = &get_ecdh_point,
884 .aes_encrypt = &aes_encrypt,
885 .aes_decrypt = &aes_decrypt,
886 .init = &init,
887 .userdata = &ctx->crypto_callback_state(),
888#if defined(BOTAN_TSS2_SUPPORTS_SM4_IN_CRYPTO_CALLBACKS)
889 .sm4_encrypt = &sm4_encrypt,
890 .sm4_decrypt = &sm4_decrypt,
891#endif
892 };
893 // clang-format on
894
895 check_rc("Esys_SetCryptoCallbacks", Esys_SetCryptoCallbacks(*ctx, &callbacks));
896#else
897 BOTAN_UNUSED(ctx);
898 throw Not_Implemented(
899 "This build of botan was compiled with a TSS2 version lower than 4.0.0, "
900 "which does not support custom runtime crypto backends");
901#endif
902}
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
int(* init)(CTX *)

References BOTAN_ASSERT_NONNULL, BOTAN_UNUSED, check_rc(), and init.

Referenced by Botan::TPM2::Context::use_botan_crypto_backend().

◆ get_botan_hash_name()

std::string Botan::TPM2::get_botan_hash_name ( TPM2_ALG_ID hash_id)
inlinenodiscard
Returns
a Botan hash name string if the hash_id value is known, otherwise throws Invalid_State

Definition at line 112 of file tpm2_algo_mappings.h.

112 {
113 if(auto hash_name = hash_algo_tss2_to_botan(hash_id)) {
114 return hash_name.value();
115 }
116
117 throw Invalid_State("TPM 2.0 hash object with unexpected hash type");
118}
std::optional< std::string > hash_algo_tss2_to_botan(TPMI_ALG_HASH hash_id) noexcept

References hash_algo_tss2_to_botan().

Referenced by Botan::TPM2::HashFunction::name().

◆ get_raw_rc()

TSS2_RC Botan::TPM2::get_raw_rc ( TSS2_RC rc)

Definition at line 18 of file tpm2_error.cpp.

18 {
19#if defined(BOTAN_TSS2_SUPPORTS_ERROR_DECODING)
20 TSS2_RC_INFO info;
21 const TSS2_RC decoding_rc = Tss2_RC_DecodeInfo(rc, &info);
22 if(decoding_rc != TSS2_RC_SUCCESS) [[unlikely]] {
23 throw Error(fmt("Decoding RC failed (was: {})", rc), decoding_rc);
24 }
25 return info.error;
26#else
27 // This fallback implementation is derived from the implementation of
28 // Tss2_RC_DecodeInfo in tpm2-tss 4.0.0.
29 const bool formatted = (rc & (1 << 7)) != 0;
30 if(formatted) {
31 return (rc & 0x3F) | TPM2_RC_FMT1;
32 } else {
33 return rc & 0xFFFF;
34 }
35#endif
36}

References Botan::fmt().

Referenced by check_rc_expecting().

◆ get_tpm2_curve_id()

std::optional< TPM2_ECC_CURVE > Botan::TPM2::get_tpm2_curve_id ( const OID & curve_oid)
inlinenodiscard

Definition at line 240 of file tpm2_algo_mappings.h.

240 {
241 // Currently, tpm2-tss does not include support for Brainpool curves or 25519/448.
242 // Once the corresponding PR (https://github.com/tpm2-software/tpm2-tss/pull/2897) is merged and released,
243 // this function should be updated.
244 const std::string curve_name = curve_oid.to_formatted_string();
245 if(curve_name == "secp192r1") {
246 return TPM2_ECC_NIST_P192;
247 } else if(curve_name == "secp224r1") {
248 return TPM2_ECC_NIST_P224;
249 } else if(curve_name == "secp256r1") {
250 return TPM2_ECC_NIST_P256;
251 } else if(curve_name == "secp384r1") {
252 return TPM2_ECC_NIST_P384;
253 } else if(curve_name == "secp521r1") {
254 return TPM2_ECC_NIST_P521;
255 } else if(curve_name == "sm2p256v1") {
256 return TPM2_ECC_SM2_P256;
257 } else {
258 return std::nullopt;
259 }
260}
std::string to_formatted_string() const
Definition asn1_oid.cpp:139

References Botan::OID::to_formatted_string().

Referenced by Botan::TPM2::EC_PrivateKey::create_unrestricted_transient().

◆ get_tpm2_hash_type()

TPMI_ALG_HASH Botan::TPM2::get_tpm2_hash_type ( std::string_view hash_name)
inlinenodiscard
Returns
a TPMI_ALG_HASH value if the hash_name is known, otherwise throws Lookup_Error

Definition at line 73 of file tpm2_algo_mappings.h.

73 {
74 if(auto hash_id = hash_algo_botan_to_tss2(hash_name)) {
75 return hash_id.value();
76 }
77
78 throw Lookup_Error("TPM 2.0 Hash", hash_name);
79}
std::optional< TPMI_ALG_HASH > hash_algo_botan_to_tss2(std::string_view hash_name) noexcept

References hash_algo_botan_to_tss2().

Referenced by Botan::TPM2::Session::authenticated_session(), and Botan::TPM2::Session::unauthenticated_session().

◆ get_tpm2_sym_cipher_spec()

TPMT_SYM_DEF Botan::TPM2::get_tpm2_sym_cipher_spec ( std::string_view algo_name)
inlinenodiscard

Definition at line 316 of file tpm2_algo_mappings.h.

316 {
317 if(auto cipher = cipher_botan_to_tss2(algo_name)) {
318 return cipher.value();
319 }
320
321 throw Lookup_Error("TPM 2.0 Symmetric Cipher Spec", algo_name);
322}
std::optional< TPMT_SYM_DEF > cipher_botan_to_tss2(std::string_view algo_name)

References cipher_botan_to_tss2().

Referenced by Botan::TPM2::Session::authenticated_session(), and Botan::TPM2::Session::unauthenticated_session().

◆ hash_algo_botan_to_tss2()

std::optional< TPMI_ALG_HASH > Botan::TPM2::hash_algo_botan_to_tss2 ( std::string_view hash_name)
inlinenodiscardnoexcept
Returns
a TPMI_ALG_HASH value if the hash_name is known, otherwise std::nullopt

Definition at line 47 of file tpm2_algo_mappings.h.

47 {
48 if(hash_name == "SHA-1") {
49 return TPM2_ALG_SHA1;
50 } else if(hash_name == "SHA-256") {
51 return TPM2_ALG_SHA256;
52 } else if(hash_name == "SHA-384") {
53 return TPM2_ALG_SHA384;
54 } else if(hash_name == "SHA-512") {
55 return TPM2_ALG_SHA512;
56 } else if(hash_name == "SHA-3(256)") {
57 return TPM2_ALG_SHA3_256;
58 } else if(hash_name == "SHA-3(384)") {
59 return TPM2_ALG_SHA3_384;
60 } else if(hash_name == "SHA-3(512)") {
61 return TPM2_ALG_SHA3_512;
62 } else if(hash_name == "SM3") {
63 return TPM2_ALG_SM3_256;
64 } else {
65 return std::nullopt;
66 }
67}

Referenced by get_tpm2_hash_type(), rsa_encryption_scheme_botan_to_tss2(), rsa_signature_scheme_botan_to_tss2(), and Botan::TPM2::Context::supports_algorithm().

◆ hash_algo_tss2_to_botan()

std::optional< std::string > Botan::TPM2::hash_algo_tss2_to_botan ( TPMI_ALG_HASH hash_id)
inlinenodiscardnoexcept
Returns
a Botan hash name string if the hash_id value is known, otherwise std::nullopt

Definition at line 85 of file tpm2_algo_mappings.h.

85 {
86 switch(hash_id) {
87 case TPM2_ALG_SHA1:
88 return "SHA-1";
89 case TPM2_ALG_SHA256:
90 return "SHA-256";
91 case TPM2_ALG_SHA384:
92 return "SHA-384";
93 case TPM2_ALG_SHA512:
94 return "SHA-512";
95 case TPM2_ALG_SHA3_256:
96 return "SHA-3(256)";
97 case TPM2_ALG_SHA3_384:
98 return "SHA-3(384)";
99 case TPM2_ALG_SHA3_512:
100 return "SHA-3(512)";
101 case TPM2_ALG_SM3_256:
102 return "SM3";
103 default: // TPMI_ALG_HASH is not an enum
104 return std::nullopt;
105 }
106}

Referenced by get_botan_hash_name().

◆ init_empty()

template<tpm2_buffer T>
T Botan::TPM2::init_empty ( )
constexpr

Create an empty TPM2 buffer of the given type.

Definition at line 142 of file tpm2_util.h.

142 {
143 T result;
144 result.size = 0;
145 return result;
146}

References T.

Referenced by Botan::TPM2::EC_PrivateKey::create_unrestricted_transient(), Botan::TPM2::RSA_PrivateKey::create_unrestricted_transient(), Botan::TPM2::HashFunction::final_with_ticket(), and Botan::TPM2::Signature_Operation::sign().

◆ out_persistent_handle()

auto Botan::TPM2::out_persistent_handle ( Object & object)
constexpr

Helper to set the persistent handle of an object from a TSS2 library function's out parameter.

Definition at line 215 of file tpm2_util.h.

215 {
216 return ObjectSetter{object, true};
217}

Referenced by Botan::TPM2::Context::persist().

◆ out_transient_handle()

auto Botan::TPM2::out_transient_handle ( Object & object)
constexpr

Helper to set the transient handle of an object from a TSS2 library function's out parameter.

Definition at line 209 of file tpm2_util.h.

209 {
210 return ObjectSetter{object, false};
211}

Referenced by Botan::TPM2::Session::authenticated_session(), Botan::TPM2::PrivateKey::create_transient_from_template(), Botan::TPM2::PrivateKey::load_transient(), Botan::TPM2::PublicKey::load_transient(), Botan::TPM2::Context::persist(), and Botan::TPM2::Session::unauthenticated_session().

◆ PropMap()

template<typename MaskT , typename FieldPointerT >
Botan::TPM2::PropMap ( MaskT ,
FieldPointerT  ) -> PropMap< MaskT, FieldPointerT >

Deduction guide to simplify the creation of PropMap instances.

◆ rsa_encryption_padding_botan_to_tss2()

std::optional< TPMI_ALG_ASYM_SCHEME > Botan::TPM2::rsa_encryption_padding_botan_to_tss2 ( std::string_view name)
inlinenodiscardnoexcept

Definition at line 360 of file tpm2_algo_mappings.h.

361 {
362 if(name == "OAEP" || name == "EME-OAEP" || name == "EME1") {
363 return TPM2_ALG_OAEP;
364 } else if(name == "PKCS1v15" || name == "EME-PKCS1-v1_5") {
365 return TPM2_ALG_RSAES;
366 } else if(name == "Raw") {
367 return TPM2_ALG_NULL;
368 } else {
369 return std::nullopt;
370 }
371}
std::string name

References name.

Referenced by rsa_encryption_scheme_botan_to_tss2(), and Botan::TPM2::Context::supports_algorithm().

◆ rsa_encryption_scheme_botan_to_tss2()

std::optional< TPMT_RSA_DECRYPT > Botan::TPM2::rsa_encryption_scheme_botan_to_tss2 ( std::string_view padding)
inlinenodiscard

Definition at line 373 of file tpm2_algo_mappings.h.

373 {
374 const SCAN_Name req(padding);
375 const auto scheme = rsa_encryption_padding_botan_to_tss2(req.algo_name());
376 if(!scheme) {
377 return std::nullopt;
378 }
379
380 if(scheme.value() == TPM2_ALG_OAEP) {
381 if(req.arg_count() < 1) {
382 return std::nullopt;
383 }
384
385 const auto hash = hash_algo_botan_to_tss2(req.arg(0));
386 if(!hash) {
387 return std::nullopt;
388 }
389
390 return TPMT_RSA_DECRYPT{
391 .scheme = scheme.value(),
392 .details = {.oaep = {.hashAlg = hash.value()}},
393 };
394 } else if(scheme.value() == TPM2_ALG_RSAES) {
395 return TPMT_RSA_DECRYPT{
396 .scheme = scheme.value(),
397 .details = {.rsaes = {}},
398 };
399 } else {
400 return std::nullopt;
401 }
402}
std::optional< TPMI_ALG_ASYM_SCHEME > rsa_encryption_padding_botan_to_tss2(std::string_view name) noexcept

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), hash_algo_botan_to_tss2(), and rsa_encryption_padding_botan_to_tss2().

Referenced by Botan::TPM2::Context::supports_algorithm().

◆ rsa_signature_padding_botan_to_tss2()

std::optional< TPMI_ALG_SIG_SCHEME > Botan::TPM2::rsa_signature_padding_botan_to_tss2 ( std::string_view padding_name)
inlinenodiscardnoexcept

Definition at line 324 of file tpm2_algo_mappings.h.

325 {
326 if(padding_name == "EMSA_PKCS1" || padding_name == "PKCS1v15" || padding_name == "EMSA-PKCS1-v1_5" ||
327 padding_name == "EMSA3") {
328 return TPM2_ALG_RSASSA;
329 } else if(padding_name == "PSS" || padding_name == "PSSR" || padding_name == "EMSA-PSS" ||
330 padding_name == "PSS-MGF1" || padding_name == "EMSA4") {
331 return TPM2_ALG_RSAPSS;
332 } else {
333 return std::nullopt;
334 }
335}

Referenced by rsa_signature_scheme_botan_to_tss2(), and Botan::TPM2::Context::supports_algorithm().

◆ rsa_signature_scheme_botan_to_tss2()

std::optional< TPMT_SIG_SCHEME > Botan::TPM2::rsa_signature_scheme_botan_to_tss2 ( std::string_view name)
inlinenodiscard

Definition at line 337 of file tpm2_algo_mappings.h.

337 {
338 const SCAN_Name req(name);
339 if(req.arg_count() == 0) {
340 return std::nullopt;
341 }
342
343 const auto scheme = rsa_signature_padding_botan_to_tss2(req.algo_name());
344 const auto hash = hash_algo_botan_to_tss2(req.arg(0));
345 if(!scheme || !hash) {
346 return std::nullopt;
347 }
348
349 if(scheme.value() == TPM2_ALG_RSAPSS && req.arg_count() != 1) {
350 // RSA signing using PSS with MGF1
351 return std::nullopt;
352 }
353
354 return TPMT_SIG_SCHEME{
355 .scheme = scheme.value(),
356 .details = {.any = {.hashAlg = hash.value()}},
357 };
358}

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), hash_algo_botan_to_tss2(), name, and rsa_signature_padding_botan_to_tss2().

Referenced by Botan::TPM2::Context::supports_algorithm().