9#ifndef BOTAN_TPM2_UTIL_H_
10#define BOTAN_TPM2_UTIL_H_
12#include <botan/concepts.h>
13#include <botan/mem_ops.h>
14#include <botan/tpm2_context.h>
15#include <botan/tpm2_error.h>
16#include <botan/tpm2_object.h>
18#include <botan/internal/fmt.h>
24#include <tss2/tss2_esys.h>
25#include <tss2/tss2_rc.h>
34#if defined(TSS2_BASE_RC_CALLBACK_NULL)
36 #define BOTAN_TSS2_SUPPORTS_CRYPTO_CALLBACKS
39 #define BOTAN_TSS2_SUPPORTS_ERROR_DECODING
42#if defined(TPM2_RC_FW_LIMITED)
44 #define BOTAN_TSS2_SUPPORTS_SM4_IN_CRYPTO_CALLBACKS
55 if(rc != TSS2_RC_SUCCESS) {
56 throw Error(location, rc);
70template <
TSS2_RC... expected_errors>
71 requires(
sizeof...(expected_errors) > 0)
74 if(rc == TSS2_RC_SUCCESS) {
82 const bool is_expected_by_caller = ((decoded_rc == expected_errors) || ...);
83 if(is_expected_by_caller) {
97 { t.buffer } -> std::convertible_to<const uint8_t*>;
98 { t.size } -> std::convertible_to<size_t>;
103 return std::span{data.buffer, data.size};
110 data.size =
static_cast<decltype(data.size)
>(length);
116template <tpm2_buffer T>
117constexpr void copy_into(
T& dest, std::span<const uint8_t> data) {
123template <tpm2_buffer T>
132template <concepts::resizable_
byte_buffer OutT>
135 result.resize(data.size);
141template <tpm2_buffer T>
177 m_object(object), m_persistent(persistent), m_handle(persistent ? 0 : ESYS_TR_NONE) {}
196 [[nodiscard]]
constexpr operator uint32_t*() &&
noexcept {
return &m_handle; }
199 constexpr bool was_written()
const {
return m_handle != (m_persistent ? 0 : ESYS_TR_NONE); }
221template <
typename FieldPo
interT, std::
unsigned_
integral MaskT>
222 requires std::is_member_object_pointer_v<FieldPointerT>
228 [[nodiscard]]
constexpr bool&
operator()(
auto&
object)
const noexcept {
return object.*
field; }
231 [[nodiscard]]
constexpr bool operator()(
const auto&
object)
const noexcept {
return object.*
field; }
235template <
typename MaskT,
typename FieldPo
interT>
246template <std::unsigned_integral UnderlyingT,
247 typename AttributeWrapperT,
251 template <std::invocable<const PropMap<
bool AttributeWrapperT::*, UnderlyingT>&> FnT>
252 static constexpr void for_all(FnT&& fn) {
256 static consteval bool all_single_bit_bitmasks() {
258 for_all([&](
auto&& prop) { result = result && (std::popcount(prop.mask) == 1); });
262 static_assert(all_single_bit_bitmasks(),
"props... must contain single-bit flags only");
265 static constexpr UnderlyingT
render(AttributeWrapperT attributes) {
266 UnderlyingT result = 0;
267 for_all([&](
auto&& prop) {
268 if(prop(attributes)) {
275 static constexpr AttributeWrapperT
read(UnderlyingT attributes) {
276 AttributeWrapperT result;
277 for_all([&](
auto&& prop) { prop(result) = (attributes & prop.mask) != 0; });
#define BOTAN_ASSERT_NOMSG(expr)
static constexpr AttributeWrapperT read(UnderlyingT attributes)
static constexpr UnderlyingT render(AttributeWrapperT attributes)
ObjectSetter(const ObjectSetter &)=delete
constexpr ObjectSetter(Object &object, bool persistent=false)
ObjectSetter & operator=(ObjectSetter &&)=delete
ObjectSetter(ObjectSetter &&)=delete
constexpr ~ObjectSetter() noexcept
ObjectSetter & operator=(const ObjectSetter &)=delete
PropMap(MaskT, FieldPointerT) -> PropMap< MaskT, FieldPointerT >
Deduction guide to simplify the creation of PropMap instances.
constexpr T init_empty()
Create an empty TPM2 buffer of the given type.
constexpr void check_rc(std::string_view location, TSS2_RC rc)
constexpr auto out_persistent_handle(Object &object)
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
TSS2_RC get_raw_rc(TSS2_RC rc)
constexpr auto out_transient_handle(Object &object)
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
constexpr TSS2_RC check_rc_expecting(std::string_view location, TSS2_RC rc)
constexpr void copy_mem(T *out, const T *in, size_t n)
std::optional< TPM2_HANDLE > persistent
constexpr bool operator()(const auto &object) const noexcept
Read-only access the boolean member 'field' from the given object.
constexpr bool & operator()(auto &object) const noexcept
Access the boolean member 'field' from the given object.
unique_esys_ptr< TPM2B_PUBLIC > pub
unique_esys_ptr< TPM2B_NAME > qualified_name
unique_esys_ptr< TPM2B_NAME > name
void operator()(void *handle)
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TSS2_RC
Forward declaration of TSS2 type for convenience.