Botan 3.14.1
Crypto and TLS for C&
tpm2_algo_mappings.cpp
Go to the documentation of this file.
1/*
2* TPM 2 algorithm mappings
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_algo_mappings.h>
10
11#include <botan/asn1_obj.h>
12#include <botan/exceptn.h>
13#include <botan/internal/algorithm_spec.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan::TPM2 {
17
18std::optional<TPM2_ALG_ID> asymmetric_algorithm_botan_to_tss2(std::string_view algo_name) noexcept {
19 if(algo_name == "RSA") {
20 return TPM2_ALG_RSA;
21 } else if(algo_name == "ECC") {
22 return TPM2_ALG_ECC;
23 } else if(algo_name == "ECDSA") {
24 return TPM2_ALG_ECDSA;
25 } else if(algo_name == "ECDH") {
26 return TPM2_ALG_ECDH;
27 } else if(algo_name == "ECDAA") {
28 return TPM2_ALG_ECDAA;
29 } else {
30 return std::nullopt;
31 }
32}
33
34std::optional<TPMI_ALG_HASH> hash_algo_botan_to_tss2(std::string_view hash_name) noexcept {
35 if(hash_name == "SHA-1") {
36 return TPM2_ALG_SHA1;
37 } else if(hash_name == "SHA-256") {
38 return TPM2_ALG_SHA256;
39 } else if(hash_name == "SHA-384") {
40 return TPM2_ALG_SHA384;
41 } else if(hash_name == "SHA-512") {
42 return TPM2_ALG_SHA512;
43 } else if(hash_name == "SHA-3(256)") {
44 return TPM2_ALG_SHA3_256;
45 } else if(hash_name == "SHA-3(384)") {
46 return TPM2_ALG_SHA3_384;
47 } else if(hash_name == "SHA-3(512)") {
48 return TPM2_ALG_SHA3_512;
49 } else if(hash_name == "SM3") {
50 return TPM2_ALG_SM3_256;
51 } else {
52 return std::nullopt;
53 }
54}
55
56TPMI_ALG_HASH get_tpm2_hash_type(std::string_view hash_name) {
57 if(auto hash_id = hash_algo_botan_to_tss2(hash_name)) {
58 return hash_id.value();
59 }
60
61 throw Lookup_Error("TPM 2.0 Hash", hash_name);
62}
63
64std::optional<std::string> hash_algo_tss2_to_botan(TPMI_ALG_HASH hash_id) {
65 switch(hash_id) {
66 case TPM2_ALG_SHA1:
67 return "SHA-1";
68 case TPM2_ALG_SHA256:
69 return "SHA-256";
70 case TPM2_ALG_SHA384:
71 return "SHA-384";
72 case TPM2_ALG_SHA512:
73 return "SHA-512";
74 case TPM2_ALG_SHA3_256:
75 return "SHA-3(256)";
76 case TPM2_ALG_SHA3_384:
77 return "SHA-3(384)";
78 case TPM2_ALG_SHA3_512:
79 return "SHA-3(512)";
80 case TPM2_ALG_SM3_256:
81 return "SM3";
82 default: // TPMI_ALG_HASH is not an enum
83 return std::nullopt;
84 }
85}
86
87std::string get_botan_hash_name(TPM2_ALG_ID hash_id) {
88 if(auto hash_name = hash_algo_tss2_to_botan(hash_id)) {
89 return hash_name.value();
90 }
91
92 throw Invalid_State("TPM 2.0 hash object with unexpected hash type");
93}
94
95std::optional<std::string> block_cipher_tss2_to_botan(TPMI_ALG_SYM cipher_id, TPM2_KEY_BITS key_bits) {
96 switch(cipher_id) {
97 case TPM2_ALG_AES:
98 if(key_bits == 128) {
99 return "AES-128";
100 } else if(key_bits == 192) {
101 return "AES-192";
102 } else if(key_bits == 256) {
103 return "AES-256";
104 }
105 break;
106
107 case TPM2_ALG_SM4:
108 if(key_bits == 128) {
109 return "SM4";
110 }
111 break;
112
113 case TPM2_ALG_CAMELLIA:
114 if(key_bits == 128) {
115 return "Camellia-128";
116 } else if(key_bits == 192) {
117 return "Camellia-192";
118 } else if(key_bits == 256) {
119 return "Camellia-256";
120 }
121 break;
122
123 case TPM2_ALG_TDES:
124 return "3DES";
125
126 default:
127 break;
128 }
129
130 return std::nullopt;
131}
132
133std::optional<std::pair<TPMI_ALG_SYM, TPM2_KEY_BITS>> block_cipher_botan_to_tss2(
134 std::string_view cipher_name) noexcept {
135 if(cipher_name == "AES-128") {
136 return std::pair{TPM2_ALG_AES, 128};
137 } else if(cipher_name == "AES-192") {
138 return std::pair{TPM2_ALG_AES, 192};
139 } else if(cipher_name == "AES-256") {
140 return std::pair{TPM2_ALG_AES, 256};
141 } else if(cipher_name == "SM4") {
142 return std::pair{TPM2_ALG_SM4, 128};
143 } else if(cipher_name == "Camellia-128") {
144 return std::pair{TPM2_ALG_CAMELLIA, 128};
145 } else if(cipher_name == "Camellia-192") {
146 return std::pair{TPM2_ALG_CAMELLIA, 192};
147 } else if(cipher_name == "Camellia-256") {
148 return std::pair{TPM2_ALG_CAMELLIA, 256};
149 } else if(cipher_name == "3DES") {
150 return std::pair{TPM2_ALG_TDES, 168};
151 } else {
152 return {};
153 }
154}
155
156std::optional<std::string> cipher_mode_tss2_to_botan(TPMI_ALG_SYM_MODE mode_id) {
157 switch(mode_id) {
158 case TPM2_ALG_CFB:
159 return "CFB";
160 case TPM2_ALG_CBC:
161 return "CBC";
162 case TPM2_ALG_ECB:
163 return "ECB";
164 case TPM2_ALG_OFB:
165 return "OFB";
166 case TPM2_ALG_CTR:
167 return "CTR";
168 default: // TPMI_ALG_SYM_MODE is not an enum
169 return std::nullopt;
170 }
171}
172
173std::optional<std::string> curve_id_tss2_to_botan(TPMI_ECC_CURVE mode_id) {
174 // Currently, tpm2-tss does not include support for Brainpool curves or 25519/448.
175 // Once the corresponding PR (https://github.com/tpm2-software/tpm2-tss/pull/2897) is merged and released,
176 // this function should be updated.
177 switch(mode_id) {
178 case TPM2_ECC_NIST_P192:
179 return "secp192r1";
180 case TPM2_ECC_NIST_P224:
181 return "secp224r1";
182 case TPM2_ECC_NIST_P256:
183 return "secp256r1";
184 case TPM2_ECC_NIST_P384:
185 return "secp384r1";
186 case TPM2_ECC_NIST_P521:
187 return "secp521r1";
188 case TPM2_ECC_SM2_P256:
189 return "sm2p256v1";
190 default:
191 return std::nullopt;
192 }
193}
194
195std::optional<size_t> curve_id_order_byte_size(TPMI_ECC_CURVE curve_id) {
196 switch(curve_id) {
197 case TPM2_ECC_NIST_P192:
198 return 24;
199 case TPM2_ECC_NIST_P224:
200 return 28;
201 case TPM2_ECC_NIST_P256:
202 return 32;
203 case TPM2_ECC_NIST_P384:
204 return 48;
205 case TPM2_ECC_NIST_P521:
206 return 66; // Rounded up to the next full byte
207 case TPM2_ECC_SM2_P256:
208 return 32;
209 default:
210 return std::nullopt;
211 }
212}
213
214std::optional<TPM2_ECC_CURVE> get_tpm2_curve_id(const OID& curve_oid) {
215 // Currently, tpm2-tss does not include support for Brainpool curves or 25519/448.
216 // Once the corresponding PR (https://github.com/tpm2-software/tpm2-tss/pull/2897) is merged and released,
217 // this function should be updated.
218 const auto curve_name = curve_oid.registered_name();
219 if(curve_name == "secp192r1") {
220 return TPM2_ECC_NIST_P192;
221 } else if(curve_name == "secp224r1") {
222 return TPM2_ECC_NIST_P224;
223 } else if(curve_name == "secp256r1") {
224 return TPM2_ECC_NIST_P256;
225 } else if(curve_name == "secp384r1") {
226 return TPM2_ECC_NIST_P384;
227 } else if(curve_name == "secp521r1") {
228 return TPM2_ECC_NIST_P521;
229 } else if(curve_name == "sm2p256v1") {
230 return TPM2_ECC_SM2_P256;
231 } else {
232 return std::nullopt;
233 }
234}
235
236std::optional<TPMI_ALG_SYM_MODE> cipher_mode_botan_to_tss2(std::string_view mode_name) noexcept {
237 if(mode_name == "CFB") {
238 return TPM2_ALG_CFB;
239 } else if(mode_name == "CBC") {
240 return TPM2_ALG_CBC;
241 } else if(mode_name == "ECB") {
242 return TPM2_ALG_ECB;
243 } else if(mode_name == "OFB") {
244 return TPM2_ALG_OFB;
245 } else if(mode_name == "CTR" || mode_name == "CTR-BE") {
246 return TPM2_ALG_CTR;
247 } else {
248 return std::nullopt;
249 }
250}
251
252std::optional<std::string> cipher_tss2_to_botan(TPMT_SYM_DEF cipher_def) {
253 const auto cipher_name = block_cipher_tss2_to_botan(cipher_def.algorithm, cipher_def.keyBits.sym);
254 const auto mode_name = cipher_mode_tss2_to_botan(cipher_def.mode.sym);
255
256 if(mode_name && cipher_name) {
257 return Botan::fmt("{}({})", *mode_name, *cipher_name);
258 } else {
259 return std::nullopt;
260 }
261}
262
263std::optional<TPMT_SYM_DEF> cipher_botan_to_tss2(std::string_view algo_name) {
264 const AlgorithmSpec spec(algo_name);
265 const auto m = spec.match("*({cipher})");
266 if(!m) {
267 return std::nullopt;
268 }
269
270 const auto cipher = block_cipher_botan_to_tss2(m->str("cipher"));
271 const auto mode = cipher_mode_botan_to_tss2(m->head());
272
273 if(!cipher || !mode) {
274 return std::nullopt;
275 }
276
277 return TPMT_SYM_DEF{
278 .algorithm = cipher->first,
279 .keyBits = {.sym = cipher->second},
280 .mode = {.sym = mode.value()},
281 };
282}
283
284TPMT_SYM_DEF get_tpm2_sym_cipher_spec(std::string_view algo_name) {
285 if(auto cipher = cipher_botan_to_tss2(algo_name)) {
286 return cipher.value();
287 }
288
289 throw Lookup_Error("TPM 2.0 Symmetric Cipher Spec", algo_name);
290}
291
292std::optional<TPMI_ALG_SIG_SCHEME> rsa_signature_padding_botan_to_tss2(std::string_view padding_name) noexcept {
293 // TODO(Botan4) remove the deprecated aliases
294 if(padding_name == "EMSA_PKCS1" || padding_name == "PKCS1v15" || padding_name == "EMSA-PKCS1-v1_5" ||
295 padding_name == "EMSA3") {
296 return TPM2_ALG_RSASSA;
297 } else if(padding_name == "PSS" || padding_name == "PSSR" || padding_name == "EMSA-PSS" ||
298 padding_name == "PSS-MGF1" || padding_name == "EMSA4") {
299 return TPM2_ALG_RSAPSS;
300 } else {
301 return std::nullopt;
302 }
303}
304
305std::optional<TPMT_SIG_SCHEME> rsa_signature_scheme_botan_to_tss2(std::string_view name) {
306 const AlgorithmSpec req(name);
307
308 // PSS with an explicit MGF1 or salt size is not expressible
309 const auto m = req.match("*({hash})");
310 if(!m) {
311 return std::nullopt;
312 }
313
314 const auto scheme = rsa_signature_padding_botan_to_tss2(m->head());
315 const auto hash = hash_algo_botan_to_tss2(m->str("hash"));
316 if(!scheme || !hash) {
317 return std::nullopt;
318 }
319
320 return TPMT_SIG_SCHEME{
321 .scheme = scheme.value(),
322 .details = {.any = {.hashAlg = hash.value()}},
323 };
324}
325
326std::optional<TPMI_ALG_ASYM_SCHEME> rsa_encryption_padding_botan_to_tss2(std::string_view name) noexcept {
327 if(name == "OAEP" || name == "EME-OAEP" || name == "EME1") {
328 return TPM2_ALG_OAEP;
329 } else if(name == "PKCS1v15" || name == "EME-PKCS1-v1_5") {
330 return TPM2_ALG_RSAES;
331 } else if(name == "Raw") {
332 return TPM2_ALG_NULL;
333 } else {
334 return std::nullopt;
335 }
336}
337
338std::optional<TPMT_RSA_DECRYPT> rsa_encryption_scheme_botan_to_tss2(std::string_view padding) {
339 const AlgorithmSpec req(padding);
340
341 if(const auto m = req.match("OAEP|EME-OAEP|EME1({hash})")) {
342 const auto hash = hash_algo_botan_to_tss2(m->str("hash"));
343 if(!hash) {
344 return std::nullopt;
345 }
346
347 return TPMT_RSA_DECRYPT{
348 .scheme = TPM2_ALG_OAEP,
349 .details = {.oaep = {.hashAlg = hash.value()}},
350 };
351 }
352
353 if(req.matches("PKCS1v15|EME-PKCS1-v1_5")) {
354 return TPMT_RSA_DECRYPT{
355 .scheme = TPM2_ALG_RSAES,
356 .details = {.rsaes = {}},
357 };
358 }
359
360 return std::nullopt;
361}
362
363} // namespace Botan::TPM2
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::optional< std::string > registered_name() const
Definition asn1_oid.cpp:149
const char * name
Definition ffi_tls.h:59
std::optional< TPMT_SIG_SCHEME > rsa_signature_scheme_botan_to_tss2(std::string_view name)
std::optional< TPMI_ALG_SIG_SCHEME > rsa_signature_padding_botan_to_tss2(std::string_view padding_name) noexcept
std::optional< TPMI_ALG_ASYM_SCHEME > rsa_encryption_padding_botan_to_tss2(std::string_view name) noexcept
std::optional< std::pair< TPMI_ALG_SYM, TPM2_KEY_BITS > > block_cipher_botan_to_tss2(std::string_view cipher_name) noexcept
std::optional< std::string > cipher_mode_tss2_to_botan(TPMI_ALG_SYM_MODE mode_id)
std::optional< size_t > curve_id_order_byte_size(TPMI_ECC_CURVE curve_id)
std::optional< TPMT_SYM_DEF > cipher_botan_to_tss2(std::string_view algo_name)
std::optional< TPMT_RSA_DECRYPT > rsa_encryption_scheme_botan_to_tss2(std::string_view padding)
std::optional< std::string > hash_algo_tss2_to_botan(TPMI_ALG_HASH hash_id)
std::optional< std::string > curve_id_tss2_to_botan(TPMI_ECC_CURVE mode_id)
std::optional< TPMI_ALG_HASH > hash_algo_botan_to_tss2(std::string_view hash_name) noexcept
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)
std::optional< std::string > block_cipher_tss2_to_botan(TPMI_ALG_SYM cipher_id, TPM2_KEY_BITS key_bits)
std::optional< std::string > cipher_tss2_to_botan(TPMT_SYM_DEF cipher_def)
std::optional< TPM2_ALG_ID > asymmetric_algorithm_botan_to_tss2(std::string_view algo_name) noexcept
TPMT_SYM_DEF get_tpm2_sym_cipher_spec(std::string_view algo_name)
std::optional< TPMI_ALG_SYM_MODE > cipher_mode_botan_to_tss2(std::string_view mode_name) noexcept
std::string get_botan_hash_name(TPM2_ALG_ID hash_id)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53