Botan 3.14.1
Crypto and TLS for C&
tpm2_ecc.cpp
Go to the documentation of this file.
1/*
2* TPM 2.0 ECC Key Wrappres
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/tpm2_ecc.h>
10
11#include <botan/pk_options_readers.h>
12#include <botan/internal/concat_util.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/pk_options_impl.h>
15#include <botan/internal/tpm2_algo_mappings.h>
16#include <botan/internal/tpm2_pkops.h>
17#include <botan/internal/tpm2_util.h>
18
19#include <tss2/tss2_esys.h>
20
21namespace Botan::TPM2 {
22
23EC_PublicKey::EC_PublicKey(Object handle, SessionBundle sessions, const TPM2B_PUBLIC* public_blob) :
24 EC_PublicKey(std::move(handle), std::move(sessions), ecc_pubkey_from_tss2_public(public_blob)) {}
25
28 const std::pair<EC_Group, EC_AffinePoint>& public_key) :
29 Botan::TPM2::PublicKey(std::move(handle), std::move(sessions)),
30 Botan::EC_PublicKey(public_key.first, public_key.second) {}
31
34 const TPM2B_PUBLIC* public_blob,
35 std::span<const uint8_t> private_blob) :
36 EC_PrivateKey(std::move(handle), std::move(sessions), ecc_pubkey_from_tss2_public(public_blob), private_blob) {}
37
40 const std::pair<EC_Group, EC_AffinePoint>& public_key,
41 std::span<const uint8_t> private_blob) :
42 Botan::TPM2::PrivateKey(std::move(handle), std::move(sessions), private_blob),
43 Botan::EC_PublicKey(public_key.first, public_key.second) {}
44
45std::unique_ptr<Public_Key> EC_PrivateKey::public_key() const {
46 return std::make_unique<Botan::ECDSA_PublicKey>(domain(), public_ec_point());
47}
48
49std::vector<uint8_t> EC_PublicKey::public_key_bits() const {
51}
52
53std::vector<uint8_t> EC_PublicKey::raw_public_key_bits() const {
55}
56
57std::vector<uint8_t> EC_PrivateKey::public_key_bits() const {
59}
60
61std::vector<uint8_t> EC_PrivateKey::raw_public_key_bits() const {
63}
64
65std::unique_ptr<TPM2::PrivateKey> EC_PrivateKey::create_unrestricted_transient(const std::shared_ptr<Context>& ctx,
67 std::span<const uint8_t> auth_value,
68 const TPM2::PrivateKey& parent,
69 const EC_Group& group) {
70 // TODO: Code duplication from RSA_PrivateKey::create_unrestricted_transient
71 BOTAN_ARG_CHECK(parent.is_parent(), "The passed key cannot be used as a parent key");
72
73 const auto curve_id = get_tpm2_curve_id(group.get_curve_oid());
74 if(!curve_id) {
75 throw Invalid_Argument("Unsupported ECC curve");
76 }
77
78 const TPM2B_SENSITIVE_CREATE sensitive_data = {
79 .size = 0, // ignored
80 .sensitive =
81 {
82 .userAuth = copy_into<TPM2B_AUTH>(auth_value),
83
84 // Architecture Document, Section 25.2.3
85 // When an asymmetric key is created, the caller is not allowed to
86 // provide the sensitive data of the key.
88 },
89 };
90
91 const TPMT_PUBLIC key_template = {
92 .type = TPM2_ALG_ECC,
93
94 // This is the algorithm for fingerprinting the newly created public key.
95 // For best compatibility we always use SHA-256.
96 .nameAlg = TPM2_ALG_SHA256,
97
98 // This sets up the key to be both a decryption and a signing key, forbids
99 // its duplication (fixed_tpm, fixed_parent) and ensures that the key's
100 // private portion can be used only by a user with an HMAC or password
101 // session.
102 .objectAttributes = ObjectAttributes::render({
103 .fixed_tpm = true,
104 .fixed_parent = true,
105 .sensitive_data_origin = true,
106 .user_with_auth = true,
107 .decrypt = true, // TODO: Shall we set this?
108 .sign_encrypt = true,
109 }),
110
111 // We currently do not support policy-based authorization
112 .authPolicy = init_empty<TPM2B_DIGEST>(),
113 .parameters =
114 {
115 .eccDetail =
116 {
117 // Structures Document (Part 2), Section 12.2.3.5
118 // If the key is not a restricted decryption key, this field
119 // shall be set to TPM_ALG_NULL.
120 //
121 // TODO: Once we stop supporting TSS < 4.0, we could use
122 // `.keyBits = {.null = {}}, .mode = {.null = {}}`
123 // which better reflects our intention here.
124 .symmetric =
125 {
126 .algorithm = TPM2_ALG_NULL,
127 .keyBits = {.sym = 0},
128 .mode = {.sym = TPM2_ALG_NULL},
129 },
130
131 // Structures Document (Part 2), Section 12.2.3.6
132 // If the decrypt attribute of the key is SET, then this shall be a
133 // valid key exchange scheme or TPM_ALG_NULL
134 //
135 // TODO: Once we stop supporting TSS < 4.0, we could use
136 // `.details = {.null = {}}`
137 // which better reflects our intention here.
138 .scheme =
139 {
140 .scheme = TPM2_ALG_NULL,
141 .details = {.anySig = {.hashAlg = TPM2_ALG_NULL}},
142 },
143 .curveID = curve_id.value(),
144
145 // Structures Document (Part 2), Section 12.2.3.6
146 // If the kdf parameter associated with curveID is not
147 // TPM_ALG_NULL then this is required to be NULL.
148 // NOTE There are currently no commands where this parameter
149 // has effect and, in the reference code, this field needs to
150 // be set to TPM_ALG_NULL
151 // TODO: Easier initialization?
152 .kdf = {.scheme = TPM2_ALG_NULL, .details = {.kdf2 = {.hashAlg = TPM2_ALG_NULL}}},
153 },
154 },
155
156 // For creating an asymmetric key this value is not used.
157 .unique = {.ecc = {}},
158 };
159
161 ctx, sessions, parent.handles().transient_handle(), key_template, sensitive_data);
162}
163
164namespace {
165
166SignatureAlgorithmSelection make_signature_scheme(const PK_Signature_Options_Reader& options) {
167 const std::string& hash_name = options.hash_function_name();
168 return {
169 .signature_scheme =
170 TPMT_SIG_SCHEME{
171 .scheme = TPM2_ALG_ECDSA, // Only support ECDSA
172 .details = {.any = {.hashAlg = get_tpm2_hash_type(hash_name)}},
173 },
174 .hash_name = std::string(hash_name),
175 .padding = std::nullopt,
176 };
177}
178
179size_t signature_length_for_ecdsa_key_handle(const SessionBundle& sessions, const Object& object) {
180 const auto curve_id = object._public_info(sessions, TPM2_ALG_ECDSA).pub->publicArea.parameters.eccDetail.curveID;
181
182 const auto order_bytes = curve_id_order_byte_size(curve_id);
183 if(!order_bytes) {
184 throw Invalid_Argument(Botan::fmt("Unsupported ECC curve: {}", curve_id));
185 };
186 return 2 * order_bytes.value();
187}
188
189class EC_Signature_Operation final : public Signature_Operation {
190 public:
191 EC_Signature_Operation(const Object& object,
192 const SessionBundle& sessions,
193 const PK_Signature_Options_Reader& options) :
194 Signature_Operation(object, sessions, make_signature_scheme(options)) {}
195
196 size_t signature_length() const override {
197 return signature_length_for_ecdsa_key_handle(sessions(), key_handle());
198 }
199
200 AlgorithmIdentifier algorithm_identifier() const override {
201 // Copied from ECDSA
202 const std::string full_name = "ECDSA/" + hash_function();
203 const OID oid = OID::from_string(full_name);
204 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
205 }
206
207 private:
208 std::vector<uint8_t> marshal_signature(const TPMT_SIGNATURE& signature) const override {
209 BOTAN_STATE_CHECK(signature.sigAlg == TPM2_ALG_ECDSA);
210
211 const auto r = as_span(signature.signature.ecdsa.signatureR);
212 const auto s = as_span(signature.signature.ecdsa.signatureS);
213 const auto sig_len = signature_length_for_ecdsa_key_handle(sessions(), key_handle());
214 BOTAN_ASSERT_NOMSG(sig_len % 2 == 0);
215 BOTAN_ASSERT_NOMSG(r.size() == sig_len / 2 && s.size() == sig_len / 2);
216
217 return concat<std::vector<uint8_t>>(r, s);
218 }
219};
220
221class EC_Verification_Operation final : public Verification_Operation {
222 public:
223 EC_Verification_Operation(const Object& object,
224 const SessionBundle& sessions,
225 const PK_Signature_Options_Reader& options) :
226 Verification_Operation(object, sessions, make_signature_scheme(options)) {}
227
228 private:
229 TPMT_SIGNATURE unmarshal_signature(std::span<const uint8_t> sig_data) const override {
230 BOTAN_STATE_CHECK(scheme().scheme == TPM2_ALG_ECDSA);
231
232 const auto sig_len = signature_length_for_ecdsa_key_handle(sessions(), key_handle());
233 BOTAN_ARG_CHECK(sig_data.size() == sig_len, "Invalid signature length");
234 BOTAN_ASSERT_NOMSG(sig_len % 2 == 0);
235
236 return {
237 .sigAlg = TPM2_ALG_ECDSA,
238 .signature =
239 {
240 .ecdsa =
241 {
242 .hash = scheme().details.any.hashAlg,
243 .signatureR = copy_into<TPM2B_ECC_PARAMETER>(sig_data.first(sig_len / 2)),
244 .signatureS = copy_into<TPM2B_ECC_PARAMETER>(sig_data.last(sig_len / 2)),
245 },
246 },
247 };
248 }
249};
250
251} // namespace
252
253std::unique_ptr<PK_Ops::Verification> EC_PublicKey::_create_verification_op(
254 const PK_Signature_Options_Reader& options) const {
255 require_hardware_provider(options, algo_name(), "tpm2");
256 return std::make_unique<EC_Verification_Operation>(handles(), sessions(), options);
257}
258
259std::unique_ptr<PK_Ops::Signature> EC_PrivateKey::_create_signature_op(
261 BOTAN_UNUSED(rng);
262 require_hardware_provider(options, algo_name(), "tpm2");
263 return std::make_unique<EC_Signature_Operation>(handles(), sessions(), options);
264}
265
266} // namespace Botan::TPM2
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
const OID & get_curve_oid() const
Definition ec_group.cpp:805
const EC_Group & domain() const
Definition ecc_key.cpp:76
const EC_AffinePoint & public_ec_point() const
Definition ecc_key.cpp:88
EC_PublicKey(const EC_PublicKey &other)=default
std::vector< uint8_t > raw_public_key_bits() const override
Definition ecc_key.cpp:102
EC_PublicKey()=default
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
const std::string & hash_function_name() const
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_ecc.cpp:61
static std::unique_ptr< TPM2::PrivateKey > create_unrestricted_transient(const std::shared_ptr< Context > &ctx, const SessionBundle &sessions, std::span< const uint8_t > auth_value, const TPM2::PrivateKey &parent, const EC_Group &group)
Definition tpm2_ecc.cpp:65
std::unique_ptr< Public_Key > public_key() const override
Definition tpm2_ecc.cpp:45
std::string algo_name() const override
Definition tpm2_ecc.h:53
EC_PrivateKey(Object handle, SessionBundle sessions, const TPM2B_PUBLIC *public_blob, std::span< const uint8_t > private_blob={})
Definition tpm2_ecc.cpp:32
friend class TPM2::PrivateKey
Definition tpm2_ecc.h:103
std::unique_ptr< PK_Ops::Signature > _create_signature_op(Botan::RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
Definition tpm2_ecc.cpp:259
std::vector< uint8_t > public_key_bits() const override
Definition tpm2_ecc.cpp:57
std::vector< uint8_t > public_key_bits() const override
Definition tpm2_ecc.cpp:49
EC_PublicKey(Object handle, SessionBundle sessions, const TPM2B_PUBLIC *public_blob)
Definition tpm2_ecc.cpp:23
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
Definition tpm2_ecc.cpp:253
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_ecc.cpp:53
std::string algo_name() const override
Definition tpm2_ecc.h:22
friend class TPM2::PublicKey
Definition tpm2_ecc.h:44
ESYS_TR transient_handle() const noexcept
static std::unique_ptr< PrivateKey > create_transient_from_template(const std::shared_ptr< Context > &ctx, const SessionBundle &sessions, ESYS_TR parent, const TPMT_PUBLIC &key_template, const TPM2B_SENSITIVE_CREATE &sensitive_data)
Definition tpm2_key.cpp:224
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_key.cpp:287
const SessionBundle & sessions() const
Definition tpm2_key.h:224
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_key.cpp:161
const Object & handles() const
Definition tpm2_key.h:99
const SessionBundle & sessions() const
Definition tpm2_key.h:101
constexpr T init_empty()
Create an empty TPM2 buffer of the given type.
Definition tpm2_util.h:151
std::optional< size_t > curve_id_order_byte_size(TPMI_ECC_CURVE curve_id)
TPMI_ALG_HASH get_tpm2_hash_type(std::string_view hash_name)
std::optional< TPM2_ECC_CURVE > get_tpm2_curve_id(const OID &curve_oid)
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
Definition tpm2_util.h:116
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
Definition tpm2_util.h:101
void require_hardware_provider(const OptionsT &options, std::string_view algo_name, std::string_view provider_name)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95
static TPMA_OBJECT render(ObjectAttributes attributes)