9#include <botan/internal/tpm2_hash.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/stl_util.h>
13#include <botan/internal/tpm2_algo_mappings.h>
15#include <tss2/tss2_esys.h>
20 std::string_view algorithm,
21 TPMI_RH_HIERARCHY hierarchy,
24 m_hierarchy(hierarchy),
25 m_handle(std::move(ctx)),
26 m_sessions(std::move(sessions)) {
41 case TPM2_ALG_SHA3_256:
42 case TPM2_ALG_SM3_256:
45 case TPM2_ALG_SHA3_384:
48 case TPM2_ALG_SHA3_512:
52 throw Invalid_State(
"TPM 2.0 hash object with unexpected hash type");
61 throw Not_Implemented(
"TPM 2.0 hash functions do not support copy_state");
65 return std::make_unique<HashFunction>(m_handle.
context(),
name(), m_hierarchy, m_sessions);
68void HashFunction::lazy_setup() {
75 Esys_HashSequenceStart(*m_handle.
context(),
83 if(rc == TPM2_RC_HASH) {
88void HashFunction::add_data(std::span<const uint8_t> input) {
91 BufferSlicer slicer(input);
92 while(slicer.remaining() > 0) {
93 const size_t chunk = std::min(slicer.remaining(),
size_t(TPM2_MAX_DIGEST_BUFFER));
96 "Esys_SequenceUpdate",
110 Esys_SequenceComplete(*m_handle.
context(),
128void HashFunction::final_result(std::span<uint8_t> output) {
134 copy_mem(output.first(output.size()),
as_span(*digest_and_ticket.first));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
size_t output_length() const override
std::string name() const override
HashFunction(std::shared_ptr< Context > ctx, std::string_view algorithm, TPMI_RH_HIERARCHY hierarchy=ESYS_TR_RH_NULL, SessionBundle sessions={})
std::unique_ptr< Botan::HashFunction > new_object() const override
std::unique_ptr< Botan::HashFunction > copy_state() const override
std::pair< unique_esys_ptr< TPM2B_DIGEST >, unique_esys_ptr< TPMT_TK_HASHCHECK > > final_with_ticket()
bool has_transient_handle() const
void _reset() noexcept
Flush the object's TPM handles and reset its internal state.
const std::shared_ptr< Context > & context() const
ESYS_TR transient_handle() const noexcept
void _disengage() noexcept
Reset the object's internal state without flushing its TPM handles.
constexpr T init_empty()
Create an empty TPM2 buffer of the given type.
constexpr void check_rc(std::string_view location, TSS2_RC rc)
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
TPMI_ALG_HASH get_tpm2_hash_type(std::string_view hash_name)
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.
std::string get_botan_hash_name(TPM2_ALG_ID hash_id)
constexpr TSS2_RC check_rc_expecting(std::string_view location, TSS2_RC rc)
constexpr auto out_ptr(T &outptr) noexcept
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)