Botan 3.6.0
Crypto and TLS for C&
ffi_tpm2.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_pkey.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_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_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_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_ctx_destroy()

int botan_tpm2_ctx_destroy ( botan_tpm2_ctx_t ctx)

Frees all resouces of a TPM2 context

Parameters
ctxTPM2 context
Returns
0 on success

Definition at line 149 of file ffi_tpm2.cpp.

149 {
150#if defined(BOTAN_HAS_TPM2)
151 return BOTAN_FFI_CHECKED_DELETE(ctx);
152#else
153 BOTAN_UNUSED(ctx);
155#endif
156}
#define BOTAN_UNUSED
Definition assert.h:118
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

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
rngrandom 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, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) -> int {
130
131 // The lifetime of the RNG used for the crypto backend should be managed
132 // by the TPM2::Context. Here, we just need to trust the user that they
133 // keep the passed-in RNG instance intact for the lifetime of the context.
134 std::shared_ptr<Botan::RandomNumberGenerator> rng_ptr(&rng_ref, [](auto*) {});
135 ctx_wrapper.ctx->use_botan_crypto_backend(rng_ptr);
136 return BOTAN_FFI_SUCCESS;
137 });
138#else
139 BOTAN_UNUSED(ctx, rng);
141#endif
142}
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::safe_get().

◆ 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 66 of file ffi_tpm2.cpp.

66 {
67#if defined(BOTAN_HAS_TPM2)
68 return ffi_guard_thunk(__func__, [=]() -> int {
69 if(ctx_out == nullptr) {
71 }
72 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
73
74 auto tcti = [=]() -> std::optional<std::string> {
75 if(tcti_nameconf == nullptr) {
76 return {};
77 } else {
78 return std::string(tcti_nameconf);
79 }
80 }();
81
82 ctx->ctx = Botan::TPM2::Context::create(std::move(tcti));
83 *ctx_out = new botan_tpm2_ctx_struct(std::move(ctx));
84 return BOTAN_FFI_SUCCESS;
85 });
86#else
87 BOTAN_UNUSED(ctx_out, tcti_nameconf);
89#endif
90}
static std::shared_ptr< Context > create(const std::string &tcti_nameconf)
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:118

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

◆ 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 92 of file ffi_tpm2.cpp.

92 {
93#if defined(BOTAN_HAS_TPM2)
94 return ffi_guard_thunk(__func__, [=]() -> int {
95 if(ctx_out == nullptr) {
97 }
98 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
99
100 auto tcti_name_str = [=]() -> std::optional<std::string> {
101 if(tcti_name == nullptr) {
102 return {};
103 } else {
104 return std::string(tcti_name);
105 }
106 }();
107
108 auto tcti_conf_str = [=]() -> std::optional<std::string> {
109 if(tcti_conf == nullptr) {
110 return {};
111 } else {
112 return std::string(tcti_conf);
113 }
114 }();
115
116 ctx->ctx = Botan::TPM2::Context::create(std::move(tcti_name_str), std::move(tcti_conf_str));
117 *ctx_out = new botan_tpm2_ctx_struct(std::move(ctx));
118 return BOTAN_FFI_SUCCESS;
119 });
120#else
121 BOTAN_UNUSED(ctx_out, tcti_name, tcti_conf);
123#endif
124}

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

◆ 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 158 of file ffi_tpm2.cpp.

162 {
163#if defined(BOTAN_HAS_TPM2)
164 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) -> int {
165 if(rng_out == nullptr) {
167 }
168
169 *rng_out = new botan_rng_struct(
170 std::make_unique<Botan::TPM2::RandomNumberGenerator>(ctx_wrapper.ctx, sessions(s1, s2, s3)));
171 return BOTAN_FFI_SUCCESS;
172 });
173#else
174 BOTAN_UNUSED(rng_out, ctx, s1, s2, s3);
176#endif
177}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, and BOTAN_UNUSED.

◆ 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 197 of file ffi_tpm2.cpp.

197 {
198#if defined(BOTAN_HAS_TPM2)
199 return BOTAN_FFI_CHECKED_DELETE(session);
200#else
201 BOTAN_UNUSED(session);
203#endif
204}

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_tpm2_supports_crypto_backend()

int botan_tpm2_supports_crypto_backend ( )

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 58 of file ffi_tpm2.cpp.

58 {
59#if defined(BOTAN_HAS_TPM2)
61#else
62 return 0;
63#endif
64}
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 179 of file ffi_tpm2.cpp.

179 {
180#if defined(BOTAN_HAS_TPM2)
181 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) -> int {
182 if(session_out == nullptr) {
184 }
185
186 auto session = std::make_unique<botan_tpm2_session_wrapper>();
187 session->session = Botan::TPM2::Session::unauthenticated_session(ctx_wrapper.ctx);
188 *session_out = new botan_tpm2_session_struct(std::move(session));
189 return BOTAN_FFI_SUCCESS;
190 });
191#else
192 BOTAN_UNUSED(session_out, ctx);
194#endif
195}
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_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::TPM2::Session::unauthenticated_session().