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>
25#if defined(BOTAN_HAS_TPM2)
31struct botan_tpm2_ctx_wrapper {
32 std::shared_ptr<Botan::TPM2::Context> ctx;
35struct botan_tpm2_session_wrapper {
36 std::shared_ptr<Botan::TPM2::Session> session;
48 (s2 !=
nullptr) ?
safe_get(s2).session :
nullptr,
49 (s3 !=
nullptr) ?
safe_get(s3).session :
nullptr);
59#if defined(BOTAN_HAS_TPM2)
67#if defined(BOTAN_HAS_TPM2)
69 if(ctx_out ==
nullptr) {
72 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
74 auto tcti = [=]() -> std::optional<std::string> {
75 if(tcti_nameconf ==
nullptr) {
78 return std::string(tcti_nameconf);
83 *ctx_out =
new botan_tpm2_ctx_struct(std::move(ctx));
93#if defined(BOTAN_HAS_TPM2)
95 if(ctx_out ==
nullptr) {
98 auto ctx = std::make_unique<botan_tpm2_ctx_wrapper>();
100 auto tcti_name_str = [=]() -> std::optional<std::string> {
101 if(tcti_name ==
nullptr) {
104 return std::string(tcti_name);
108 auto tcti_conf_str = [=]() -> std::optional<std::string> {
109 if(tcti_conf ==
nullptr) {
112 return std::string(tcti_conf);
117 *ctx_out =
new botan_tpm2_ctx_struct(std::move(ctx));
127#if defined(BOTAN_HAS_TPM2)
128 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
134 std::shared_ptr<Botan::RandomNumberGenerator> rng_ptr(&rng_ref, [](
auto*) {});
135 ctx_wrapper.ctx->use_botan_crypto_backend(rng_ptr);
150#if defined(BOTAN_HAS_TPM2)
163#if defined(BOTAN_HAS_TPM2)
164 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
165 if(rng_out ==
nullptr) {
169 *rng_out =
new botan_rng_struct(
170 std::make_unique<Botan::TPM2::RandomNumberGenerator>(ctx_wrapper.ctx, sessions(s1, s2, s3)));
180#if defined(BOTAN_HAS_TPM2)
181 return BOTAN_FFI_VISIT(ctx, [=](botan_tpm2_ctx_wrapper& ctx_wrapper) ->
int {
182 if(session_out ==
nullptr) {
186 auto session = std::make_unique<botan_tpm2_session_wrapper>();
188 *session_out =
new botan_tpm2_session_struct(std::move(session));
198#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
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_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_session_destroy(botan_tpm2_session_t session)
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)
T & safe_get(botan_struct< T, M > *p)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)