Botan 3.14.1
Crypto and TLS for C&
ffi_tpm2.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

int botan_tpm2_crypto_backend_state_destroy (botan_tpm2_crypto_backend_state_t cbs)
int botan_tpm2_ctx_destroy (botan_tpm2_ctx_t ctx)
int botan_tpm2_ctx_enable_crypto_backend (botan_tpm2_ctx_t ctx, botan_rng_t rng)
int botan_tpm2_ctx_from_esys (botan_tpm2_ctx_t *ctx_out, ESYS_CONTEXT *esys_ctx)
int botan_tpm2_ctx_init (botan_tpm2_ctx_t *ctx_out, const char *tcti_nameconf)
int botan_tpm2_ctx_init_ex (botan_tpm2_ctx_t *ctx_out, const char *tcti_name, const char *tcti_conf)
int botan_tpm2_enable_crypto_backend (botan_tpm2_crypto_backend_state_t *cbs_out, ESYS_CONTEXT *esys_ctx, botan_rng_t rng)
int botan_tpm2_rng_init (botan_rng_t *rng_out, botan_tpm2_ctx_t ctx, botan_tpm2_session_t s1, botan_tpm2_session_t s2, botan_tpm2_session_t s3)
int botan_tpm2_session_destroy (botan_tpm2_session_t session)
int botan_tpm2_supports_crypto_backend ()
int botan_tpm2_unauthenticated_session_init (botan_tpm2_session_t *session_out, botan_tpm2_ctx_t ctx)

Function Documentation

◆ botan_tpm2_crypto_backend_state_destroy()

int botan_tpm2_crypto_backend_state_destroy ( botan_tpm2_crypto_backend_state_t cbs)

Frees all resources of a TPM2 Crypto Callback State Note that this does not attempt to de-register the crypto backend, it just frees the resource pointed to by cbs. Use the ESAPI function Esys_SetCryptoCallbacks(ctx, nullptr) to deregister manually.

Parameters
cbsTPM2 Crypto Callback State
Returns
0 on success

Definition at line 166 of file ffi_tpm2.cpp.

166 {
167#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
168 return BOTAN_FFI_CHECKED_DELETE(cbs);
169#else
170 BOTAN_UNUSED(cbs);
172#endif
173}
#define BOTAN_UNUSED
Definition assert.h:151
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_tpm2_ctx_destroy()

int botan_tpm2_ctx_destroy ( botan_tpm2_ctx_t ctx)

Frees all resources of a TPM2 context

Parameters
ctxTPM2 context
Returns
0 on success

Definition at line 140 of file ffi_tpm2.cpp.

140 {
141#if defined(BOTAN_HAS_TPM2)
142 return BOTAN_FFI_CHECKED_DELETE(ctx);
143#else
144 BOTAN_UNUSED(ctx);
146#endif
147}

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_tpm2_ctx_enable_crypto_backend()

int botan_tpm2_ctx_enable_crypto_backend ( botan_tpm2_ctx_t ctx,
botan_rng_t rng )

Enable Botan's TSS2 crypto backend that replaces the cryptographic functions required for the communication with the TPM with implementations provided by Botan instead of using TSS' defaults OpenSSL or mbedTLS. Note that the provided rng should not be dependent on the TPM and the caller must ensure that it remains usable for the lifetime of the ctx.

Parameters
ctxTPM2 context
Random Number Generatorsrandom number generator to be used by the crypto backend

Definition at line 126 of file ffi_tpm2.cpp.

126 {
127#if defined(BOTAN_HAS_TPM2)
128 return BOTAN_FFI_VISIT(ctx, [=](auto& tpm2_ctx) { tpm2_ctx.use_botan_crypto_backend(safe_get_sp(rng)); });
129#else
130 BOTAN_UNUSED(ctx, rng);
132#endif
133}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
auto safe_get_sp(T *p) -> const typename T::smart_pointer_type &
Definition ffi_util.h:212

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::safe_get_sp().

◆ botan_tpm2_ctx_from_esys()

int botan_tpm2_ctx_from_esys ( botan_tpm2_ctx_t * ctx_out,
struct ESYS_CONTEXT * esys_ctx )

Wrap an existing ESYS_CONTEXT for use in Botan. Note that destroying the created botan_tpm2_ctx_t won't finalize esys_ctx

Parameters
ctx_outoutput TPM2 context
esys_ctxESYS_CONTEXT to wrap
Returns
0 on success

Definition at line 110 of file ffi_tpm2.cpp.

110 {
111#if defined(BOTAN_HAS_TPM2)
112 return ffi_guard_thunk(__func__, [=]() -> int {
113 if(any_null_pointers(ctx_out, esys_ctx)) {
115 }
116
117 auto ctx = Botan::TPM2::Context::create(esys_ctx);
118 return ffi_new_object(ctx_out, std::move(ctx));
119 });
120#else
121 BOTAN_UNUSED(ctx_out, esys_ctx);
123#endif
124}
static std::shared_ptr< Context > create(const std::string &tcti_nameconf)
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::TPM2::Context::create(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_tpm2_ctx_init()

int botan_tpm2_ctx_init ( botan_tpm2_ctx_t * ctx_out,
const char * tcti_nameconf )

Initialize a TPM2 context

Parameters
ctx_outoutput TPM2 context
tcti_nameconfTCTI config (may be nullptr)
Returns
0 on success

Definition at line 61 of file ffi_tpm2.cpp.

61 {
62#if defined(BOTAN_HAS_TPM2)
63 return ffi_guard_thunk(__func__, [=]() -> int {
64 if(ctx_out == nullptr) {
66 }
67
68 auto ctx = (tcti_nameconf == nullptr) ? Botan::TPM2::Context::create()
69 : Botan::TPM2::Context::create(std::string(tcti_nameconf));
70 return ffi_new_object(ctx_out, std::move(ctx));
71 });
72#else
73 BOTAN_UNUSED(ctx_out, tcti_nameconf);
75#endif
76}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::TPM2::Context::create(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_tpm2_ctx_init_ex()

int botan_tpm2_ctx_init_ex ( botan_tpm2_ctx_t * ctx_out,
const char * tcti_name,
const char * tcti_conf )

Initialize a TPM2 context

Parameters
ctx_outoutput TPM2 context
tcti_nameTCTI name (may be nullptr)
tcti_confTCTI config (may be nullptr)
Returns
0 on success

Definition at line 78 of file ffi_tpm2.cpp.

78 {
79#if defined(BOTAN_HAS_TPM2)
80 return ffi_guard_thunk(__func__, [=]() -> int {
81 if(ctx_out == nullptr) {
83 }
84
85 auto tcti_name_str = [=]() -> std::optional<std::string> {
86 if(tcti_name == nullptr) {
87 return {};
88 } else {
89 return std::string(tcti_name);
90 }
91 }();
92
93 auto tcti_conf_str = [=]() -> std::optional<std::string> {
94 if(tcti_conf == nullptr) {
95 return {};
96 } else {
97 return std::string(tcti_conf);
98 }
99 }();
100
101 auto ctx = Botan::TPM2::Context::create(std::move(tcti_name_str), std::move(tcti_conf_str));
102 return ffi_new_object(ctx_out, std::move(ctx));
103 });
104#else
105 BOTAN_UNUSED(ctx_out, tcti_name, tcti_conf);
107#endif
108}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::TPM2::Context::create(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_tpm2_enable_crypto_backend()

int botan_tpm2_enable_crypto_backend ( botan_tpm2_crypto_backend_state_t * cbs_out,
struct ESYS_CONTEXT * esys_ctx,
botan_rng_t rng )

Use this if you just need Botan's crypto backend but do not want to wrap any other ESYS functionality using Botan's TPM2 wrapper. A Crypto Backend State is created that the user needs to keep alive for as long as the crypto backend is used and needs to be destroyed after. Note that the provided rng should not be dependent on the TPM and the caller must ensure that it remains usable for the lifetime of the esys_ctx.

Parameters
cbs_outTo be created Crypto Backend State
esys_ctxTPM2 context
Random Number Generatorsrandom number generator to be used by the crypto backend

Definition at line 149 of file ffi_tpm2.cpp.

151 {
152#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
153 return ffi_guard_thunk(__func__, [=]() -> int {
154 if(any_null_pointers(cbs_out, esys_ctx)) {
156 }
157
159 });
160#else
161 BOTAN_UNUSED(cbs_out, esys_ctx, rng);
163#endif
164}
std::unique_ptr< CryptoCallbackState > use_botan_crypto_backend(ESYS_CONTEXT *context, const std::shared_ptr< Botan::RandomNumberGenerator > &rng)

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get_sp(), and Botan::TPM2::use_botan_crypto_backend().

◆ botan_tpm2_rng_init()

int botan_tpm2_rng_init ( botan_rng_t * rng_out,
botan_tpm2_ctx_t ctx,
botan_tpm2_session_t s1,
botan_tpm2_session_t s2,
botan_tpm2_session_t s3 )

Initialize a random number generator object via TPM2

Parameters
rng_outrng object to create
ctxTPM2 context
s1the first session to use (optional, may be nullptr)
s2the second session to use (optional, may be nullptr)
s3the third session to use (optional, may be nullptr)

Definition at line 175 of file ffi_tpm2.cpp.

179 {
180#if defined(BOTAN_HAS_TPM2)
181 return BOTAN_FFI_VISIT(ctx, [=](const std::shared_ptr<Botan::TPM2::Context>& tpm2_ctx) -> int {
182 if(rng_out == nullptr) {
184 }
185
186 return ffi_new_object(rng_out,
187 std::make_unique<Botan::TPM2::RandomNumberGenerator>(tpm2_ctx, sessions(s1, s2, s3)));
188 });
189#else
190 BOTAN_UNUSED(rng_out, ctx, s1, s2, s3);
192#endif
193}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::ffi_new_object().

◆ botan_tpm2_session_destroy()

int botan_tpm2_session_destroy ( botan_tpm2_session_t session)

Create an unauthenticated session for use with TPM2

Parameters
sessionthe session object to destroy

Definition at line 210 of file ffi_tpm2.cpp.

210 {
211#if defined(BOTAN_HAS_TPM2)
212 return BOTAN_FFI_CHECKED_DELETE(session);
213#else
214 BOTAN_UNUSED(session);
216#endif
217}

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_tpm2_supports_crypto_backend()

int botan_tpm2_supports_crypto_backend ( void )

Checks if Botan's TSS2 crypto backend can be used in this build

Returns
1 if the crypto backend can be enabled

Definition at line 53 of file ffi_tpm2.cpp.

53 {
54#if defined(BOTAN_HAS_TPM2)
56#else
57 return 0;
58#endif
59}
static bool supports_botan_crypto_backend() noexcept

References Botan::TPM2::Context::supports_botan_crypto_backend().

◆ botan_tpm2_unauthenticated_session_init()

int botan_tpm2_unauthenticated_session_init ( botan_tpm2_session_t * session_out,
botan_tpm2_ctx_t ctx )

Create an unauthenticated session for use with TPM2

Parameters
session_outthe session object to create
ctxTPM2 context

Definition at line 195 of file ffi_tpm2.cpp.

195 {
196#if defined(BOTAN_HAS_TPM2)
197 return BOTAN_FFI_VISIT(ctx, [=](const std::shared_ptr<Botan::TPM2::Context>& tpm2_ctx) -> int {
198 if(session_out == nullptr) {
200 }
201
203 });
204#else
205 BOTAN_UNUSED(session_out, ctx);
207#endif
208}
static std::shared_ptr< Session > unauthenticated_session(const std::shared_ptr< Context > &ctx, std::string_view sym_algo="CFB(AES-256)", std::string_view hash_algo="SHA-256")

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, BOTAN_UNUSED, Botan_FFI::ffi_new_object(), and Botan::TPM2::Session::unauthenticated_session().