10#include <botan/internal/ffi_pkey.h>
11#include <botan/internal/ffi_rng.h>
12#include <botan/internal/ffi_util.h>
14#if defined(BOTAN_HAS_TPM2)
15 #include <botan/tpm2_context.h>
16 #include <botan/tpm2_key.h>
17 #include <botan/tpm2_rng.h>
18 #include <botan/tpm2_session.h>
20 #if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
21 #include <botan/tpm2_crypto_backend.h>
29#if defined(BOTAN_HAS_TPM2)
35struct botan_tpm2_ctx_wrapper {
36 std::shared_ptr<Botan::TPM2::Context> ctx;
39struct botan_tpm2_session_wrapper {
40 std::shared_ptr<Botan::TPM2::Session> session;
46 #if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
56 (s2 !=
nullptr) ?
safe_get(s2).session :
nullptr,
57 (s3 !=
nullptr) ?
safe_get(s3).session :
nullptr);
67#if defined(BOTAN_HAS_TPM2)
75#if defined(BOTAN_HAS_TPM2)
77 if(ctx_out ==
nullptr) {
80 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
82 auto tcti = [=]() -> std::optional<std::string> {
83 if(tcti_nameconf ==
nullptr) {
86 return std::string(tcti_nameconf);
100#if defined(BOTAN_HAS_TPM2)
102 if(ctx_out ==
nullptr) {
105 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
107 auto tcti_name_str = [=]() -> std::optional<std::string> {
108 if(tcti_name ==
nullptr) {
111 return std::string(tcti_name);
115 auto tcti_conf_str = [=]() -> std::optional<std::string> {
116 if(tcti_conf ==
nullptr) {
119 return std::string(tcti_conf);
133#if defined(BOTAN_HAS_TPM2)
135 if(ctx_out ==
nullptr || esys_ctx ==
nullptr) {
139 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
150#if defined(BOTAN_HAS_TPM2)
151 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
157 std::shared_ptr<Botan::RandomNumberGenerator> rng_ptr(&rng_ref, [](
auto*) {});
158 ctx_wrapper.ctx->use_botan_crypto_backend(rng_ptr);
173#if defined(BOTAN_HAS_TPM2)
182 ESYS_CONTEXT* esys_ctx,
184#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
186 if(cbs_out ==
nullptr || esys_ctx ==
nullptr) {
194 std::shared_ptr<Botan::RandomNumberGenerator> rng_ptr(&rng_ref, [](
auto*) {});
204#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
217#if defined(BOTAN_HAS_TPM2)
218 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
219 if(rng_out ==
nullptr) {
224 rng_out, std::make_unique<Botan::TPM2::RandomNumberGenerator>(ctx_wrapper.ctx, sessions(s1, s2, s3)));
233#if defined(BOTAN_HAS_TPM2)
234 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
235 if(session_out ==
nullptr) {
239 auto session = std::make_unique<botan_tpm2_session_wrapper>();
250#if defined(BOTAN_HAS_TPM2)
static bool supports_botan_crypto_backend() noexcept
static std::shared_ptr< Context > create(const std::string &tcti_nameconf)
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")
struct botan_tpm2_session_struct * botan_tpm2_session_t
struct botan_tpm2_ctx_struct * botan_tpm2_ctx_t
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
struct botan_tpm2_crypto_backend_state_struct * botan_tpm2_crypto_backend_state_t
int botan_tpm2_ctx_enable_crypto_backend(botan_tpm2_ctx_t ctx, botan_rng_t rng)
int botan_tpm2_ctx_init_ex(botan_tpm2_ctx_t *ctx_out, const char *tcti_name, const char *tcti_conf)
int botan_tpm2_ctx_init(botan_tpm2_ctx_t *ctx_out, const char *tcti_nameconf)
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_ctx_from_esys(botan_tpm2_ctx_t *ctx_out, ESYS_CONTEXT *esys_ctx)
int botan_tpm2_ctx_destroy(botan_tpm2_ctx_t ctx)
int botan_tpm2_unauthenticated_session_init(botan_tpm2_session_t *session_out, botan_tpm2_ctx_t ctx)
int botan_tpm2_crypto_backend_state_destroy(botan_tpm2_crypto_backend_state_t cbs)
int botan_tpm2_session_destroy(botan_tpm2_session_t session)
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_supports_crypto_backend()
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
std::unique_ptr< CryptoCallbackState > use_botan_crypto_backend(ESYS_CONTEXT *context, const std::shared_ptr< Botan::RandomNumberGenerator > &rng)
T & safe_get(botan_struct< T, M > *p)
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
int ffi_guard_thunk(const char *func_name, T thunk)