9#include <botan/internal/tpm2_algo_mappings.h>
11#include <botan/asn1_obj.h>
12#include <botan/exceptn.h>
13#include <botan/internal/algorithm_spec.h>
14#include <botan/internal/fmt.h>
19 if(algo_name ==
"RSA") {
21 }
else if(algo_name ==
"ECC") {
23 }
else if(algo_name ==
"ECDSA") {
24 return TPM2_ALG_ECDSA;
25 }
else if(algo_name ==
"ECDH") {
27 }
else if(algo_name ==
"ECDAA") {
28 return TPM2_ALG_ECDAA;
35 if(hash_name ==
"SHA-1") {
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;
58 return hash_id.value();
74 case TPM2_ALG_SHA3_256:
76 case TPM2_ALG_SHA3_384:
78 case TPM2_ALG_SHA3_512:
80 case TPM2_ALG_SM3_256:
89 return hash_name.value();
92 throw Invalid_State(
"TPM 2.0 hash object with unexpected hash type");
100 }
else if(key_bits == 192) {
102 }
else if(key_bits == 256) {
108 if(key_bits == 128) {
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";
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};
178 case TPM2_ECC_NIST_P192:
180 case TPM2_ECC_NIST_P224:
182 case TPM2_ECC_NIST_P256:
184 case TPM2_ECC_NIST_P384:
186 case TPM2_ECC_NIST_P521:
188 case TPM2_ECC_SM2_P256:
197 case TPM2_ECC_NIST_P192:
199 case TPM2_ECC_NIST_P224:
201 case TPM2_ECC_NIST_P256:
203 case TPM2_ECC_NIST_P384:
205 case TPM2_ECC_NIST_P521:
207 case TPM2_ECC_SM2_P256:
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;
237 if(mode_name ==
"CFB") {
239 }
else if(mode_name ==
"CBC") {
241 }
else if(mode_name ==
"ECB") {
243 }
else if(mode_name ==
"OFB") {
245 }
else if(mode_name ==
"CTR" || mode_name ==
"CTR-BE") {
256 if(mode_name && cipher_name) {
257 return Botan::fmt(
"{}({})", *mode_name, *cipher_name);
265 const auto m = spec.
match(
"*({cipher})");
273 if(!cipher || !mode) {
278 .algorithm = cipher->first,
279 .keyBits = {.sym = cipher->second},
280 .mode = {.sym = mode.value()},
286 return cipher.value();
289 throw Lookup_Error(
"TPM 2.0 Symmetric Cipher Spec", algo_name);
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;
309 const auto m = req.
match(
"*({hash})");
316 if(!scheme || !hash) {
320 return TPMT_SIG_SCHEME{
321 .scheme = scheme.value(),
322 .details = {.any = {.hashAlg = hash.value()}},
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;
341 if(
const auto m = req.
match(
"OAEP|EME-OAEP|EME1({hash})")) {
347 return TPMT_RSA_DECRYPT{
348 .scheme = TPM2_ALG_OAEP,
349 .details = {.oaep = {.hashAlg = hash.value()}},
353 if(req.
matches(
"PKCS1v15|EME-PKCS1-v1_5")) {
354 return TPMT_RSA_DECRYPT{
355 .scheme = TPM2_ALG_RSAES,
356 .details = {.rsaes = {}},
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::optional< std::string > registered_name() const
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)