Botan 3.14.1
Crypto and TLS for C&
tpm2_pkops.cpp
Go to the documentation of this file.
1/*
2* TPM 2.0 Public Key Operations
3* (C) 2024 Jack Lloyd
4* (C) 2024 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity GmbH, financed by LANCOM Systems GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/tpm2_pkops.h>
10
11#include <botan/internal/stl_util.h>
12#include <botan/internal/tpm2_hash.h>
13
14namespace Botan::TPM2 {
15
16namespace {
17
18/**
19* Signing with a restricted key requires a validation ticket that is provided
20* when hashing the data to sign on the TPM. Otherwise, it is fine to hash the
21* data in software.
22*
23* @param key_handle the key to create the signature with
24* @param sessions the sessions to use for the TPM operations
25* @param hash_name the name of the hash function to use
26*
27* @return a HashFunction that hashes in hardware if the key is restricted
28*/
29std::unique_ptr<Botan::HashFunction> create_hash_function(const Object& key_handle,
30 const SessionBundle& sessions,
31 std::string_view hash_name) {
32 if(key_handle.attributes(sessions).restricted) {
33 // TODO: this could also be ENDORSEMENT or PLATFORM, and we're not 100% sure
34 // that OWNER is always the right choice here.
35 const TPMI_RH_HIERARCHY hierarchy = ESYS_TR_RH_OWNER;
36 return std::make_unique<HashFunction>(key_handle.context(), hash_name, hierarchy, sessions);
37 } else {
39 }
40}
41
42} // namespace
43
46 const SignatureAlgorithmSelection& algorithms) :
48 object, sessions, algorithms, create_hash_function(object, sessions, algorithms.hash_name)) {}
49
51 BOTAN_UNUSED(rng);
52
53 auto do_sign = [this](const TPM2B_DIGEST& digest, const TPMT_TK_HASHCHECK& validation) {
55 check_rc("Esys_Sign",
56 Esys_Sign(key_handle().context()->esys_context(),
57 key_handle().transient_handle(),
58 sessions()[0],
59 sessions()[1],
60 sessions()[2],
61 &digest,
62 &scheme(),
63 &validation,
64 out_ptr(signature)));
65 BOTAN_ASSERT_NONNULL(signature);
66 BOTAN_ASSERT_NOMSG(signature->sigAlg == scheme().scheme);
67 BOTAN_ASSERT_NOMSG(signature->signature.any.hashAlg == scheme().details.any.hashAlg);
68 return signature;
69 };
70
71 const auto signature = [&] {
72 if(auto* h = dynamic_cast<HashFunction*>(hash())) {
73 // This is a TPM2-based hash object that calculated the digest on
74 // the TPM. We can use the validation ticket to create the signature.
75 auto [digest, validation] = h->final_with_ticket();
77 BOTAN_ASSERT_NONNULL(validation);
78 return do_sign(*digest, *validation);
79 } else {
80 // This is a software hash, so we have to stub the validation ticket
81 // and create the signature without it.
82 TPM2B_DIGEST digest;
83 hash()->final(as_span(digest, hash()->output_length()));
84 return do_sign(digest,
85 TPMT_TK_HASHCHECK{
86 .tag = TPM2_ST_HASHCHECK,
87 .hierarchy = TPM2_RH_NULL,
88 .digest = init_empty<TPM2B_DIGEST>(),
89 });
90 }
91 }();
92
93 return marshal_signature(*signature);
94}
95
98 const SignatureAlgorithmSelection& algorithms) :
99 Signature_Operation_Base<PK_Ops::Verification>(
100 object, sessions, algorithms, Botan::HashFunction::create_or_throw(algorithms.hash_name)) {}
101
102bool Verification_Operation::is_valid_signature(std::span<const uint8_t> sig_data) {
103 TPM2B_DIGEST digest;
104 hash()->final(as_span(digest, hash()->output_length()));
105
106 const auto signature = unmarshal_signature(sig_data);
107
108 // If the signature is not valid, this returns TPM2_RC_SIGNATURE.
109 const auto rc = check_rc_expecting<TPM2_RC_SIGNATURE>("Esys_VerifySignature",
110 Esys_VerifySignature(key_handle().context()->esys_context(),
111 key_handle().transient_handle(),
112 sessions()[0],
113 sessions()[1],
114 sessions()[2],
115 &digest,
116 &signature,
117 nullptr /* validation */));
118
119 return rc == TPM2_RC_SUCCESS;
120}
121
122} // namespace Botan::TPM2
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
void final(uint8_t out[])
Definition buf_comp.h:97
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
Signature_Operation_Base(const Object &object, const SessionBundle &sessions, const SignatureAlgorithmSelection &algorithms, std::unique_ptr< Botan::HashFunction > hash)
Definition tpm2_pkops.h:28
Signature_Operation(const Object &object, const SessionBundle &sessions, const SignatureAlgorithmSelection &algorithms)
virtual std::vector< uint8_t > marshal_signature(const TPMT_SIGNATURE &signature) const =0
std::vector< uint8_t > sign(Botan::RandomNumberGenerator &rng) override
virtual TPMT_SIGNATURE unmarshal_signature(std::span< const uint8_t > sig_data) const =0
Verification_Operation(const Object &object, const SessionBundle &sessions, const SignatureAlgorithmSelection &algorithms)
bool is_valid_signature(std::span< const uint8_t > sig_data) override
constexpr T init_empty()
Create an empty TPM2 buffer of the given type.
Definition tpm2_util.h:151
constexpr void check_rc(std::string_view location, TSS2_RC rc)
Definition tpm2_util.h:53
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
Definition tpm2_util.h:161
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
Definition tpm2_util.h:101
constexpr TSS2_RC check_rc_expecting(std::string_view location, TSS2_RC rc)
Definition tpm2_util.h:71
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:164