9#ifndef BOTAN_TPM2_ALGORITHM_MAPPINGS_H_
10#define BOTAN_TPM2_ALGORITHM_MAPPINGS_H_
12#include <botan/asn1_obj.h>
13#include <botan/exceptn.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/scan_name.h>
22#include <tss2/tss2_tpm2_types.h>
27 std::string_view algo_name)
noexcept {
28 if(algo_name ==
"RSA") {
30 }
else if(algo_name ==
"ECC") {
32 }
else if(algo_name ==
"ECDSA") {
33 return TPM2_ALG_ECDSA;
34 }
else if(algo_name ==
"ECDH") {
36 }
else if(algo_name ==
"ECDAA") {
37 return TPM2_ALG_ECDAA;
48 if(hash_name ==
"SHA-1") {
50 }
else if(hash_name ==
"SHA-256") {
51 return TPM2_ALG_SHA256;
52 }
else if(hash_name ==
"SHA-384") {
53 return TPM2_ALG_SHA384;
54 }
else if(hash_name ==
"SHA-512") {
55 return TPM2_ALG_SHA512;
56 }
else if(hash_name ==
"SHA-3(256)") {
57 return TPM2_ALG_SHA3_256;
58 }
else if(hash_name ==
"SHA-3(384)") {
59 return TPM2_ALG_SHA3_384;
60 }
else if(hash_name ==
"SHA-3(512)") {
61 return TPM2_ALG_SHA3_512;
62 }
else if(hash_name ==
"SM3") {
63 return TPM2_ALG_SM3_256;
75 return hash_id.value();
95 case TPM2_ALG_SHA3_256:
97 case TPM2_ALG_SHA3_384:
99 case TPM2_ALG_SHA3_512:
101 case TPM2_ALG_SM3_256:
114 return hash_name.value();
117 throw Invalid_State(
"TPM 2.0 hash object with unexpected hash type");
121 TPM2_KEY_BITS key_bits)
noexcept {
124 if(key_bits == 128) {
126 }
else if(key_bits == 192) {
128 }
else if(key_bits == 256) {
134 if(key_bits == 128) {
139 case TPM2_ALG_CAMELLIA:
140 if(key_bits == 128) {
141 return "Camellia-128";
142 }
else if(key_bits == 192) {
143 return "Camellia-192";
144 }
else if(key_bits == 256) {
145 return "Camellia-256";
160 std::string_view cipher_name)
noexcept {
161 if(cipher_name ==
"AES-128") {
162 return std::pair{TPM2_ALG_AES, 128};
163 }
else if(cipher_name ==
"AES-192") {
164 return std::pair{TPM2_ALG_AES, 192};
165 }
else if(cipher_name ==
"AES-256") {
166 return std::pair{TPM2_ALG_AES, 256};
167 }
else if(cipher_name ==
"SM4") {
168 return std::pair{TPM2_ALG_SM4, 128};
169 }
else if(cipher_name ==
"Camellia-128") {
170 return std::pair{TPM2_ALG_CAMELLIA, 128};
171 }
else if(cipher_name ==
"Camellia-192") {
172 return std::pair{TPM2_ALG_CAMELLIA, 192};
173 }
else if(cipher_name ==
"Camellia-256") {
174 return std::pair{TPM2_ALG_CAMELLIA, 256};
175 }
else if(cipher_name ==
"3DES") {
176 return std::pair{TPM2_ALG_TDES, 168};
204 case TPM2_ECC_NIST_P192:
206 case TPM2_ECC_NIST_P224:
208 case TPM2_ECC_NIST_P256:
210 case TPM2_ECC_NIST_P384:
212 case TPM2_ECC_NIST_P521:
214 case TPM2_ECC_SM2_P256:
223 case TPM2_ECC_NIST_P192:
225 case TPM2_ECC_NIST_P224:
227 case TPM2_ECC_NIST_P256:
229 case TPM2_ECC_NIST_P384:
231 case TPM2_ECC_NIST_P521:
233 case TPM2_ECC_SM2_P256:
245 if(curve_name ==
"secp192r1") {
246 return TPM2_ECC_NIST_P192;
247 }
else if(curve_name ==
"secp224r1") {
248 return TPM2_ECC_NIST_P224;
249 }
else if(curve_name ==
"secp256r1") {
250 return TPM2_ECC_NIST_P256;
251 }
else if(curve_name ==
"secp384r1") {
252 return TPM2_ECC_NIST_P384;
253 }
else if(curve_name ==
"secp521r1") {
254 return TPM2_ECC_NIST_P521;
255 }
else if(curve_name ==
"sm2p256v1") {
256 return TPM2_ECC_SM2_P256;
263 if(mode_name ==
"CFB") {
265 }
else if(mode_name ==
"CBC") {
267 }
else if(mode_name ==
"ECB") {
269 }
else if(mode_name ==
"OFB") {
271 }
else if(mode_name ==
"CTR" || mode_name ==
"CTR-BE") {
293 return Botan::fmt(
"{}({})", mode_name.value(), cipher_name.value());
305 if(!cipher || !mode) {
310 .algorithm = cipher->first,
311 .keyBits = {.sym = cipher->second},
312 .mode = {.sym = mode.value()},
318 return cipher.value();
321 throw Lookup_Error(
"TPM 2.0 Symmetric Cipher Spec", algo_name);
325 std::string_view padding_name)
noexcept {
326 if(padding_name ==
"EMSA_PKCS1" || padding_name ==
"PKCS1v15" || padding_name ==
"EMSA-PKCS1-v1_5" ||
327 padding_name ==
"EMSA3") {
328 return TPM2_ALG_RSASSA;
329 }
else if(padding_name ==
"PSS" || padding_name ==
"PSSR" || padding_name ==
"EMSA-PSS" ||
330 padding_name ==
"PSS-MGF1" || padding_name ==
"EMSA4") {
331 return TPM2_ALG_RSAPSS;
345 if(!scheme || !hash) {
349 if(scheme.value() == TPM2_ALG_RSAPSS && req.
arg_count() != 1) {
354 return TPMT_SIG_SCHEME{
355 .scheme = scheme.value(),
356 .details = {.any = {.hashAlg = hash.value()}},
361 std::string_view
name)
noexcept {
362 if(
name ==
"OAEP" ||
name ==
"EME-OAEP" ||
name ==
"EME1") {
363 return TPM2_ALG_OAEP;
364 }
else if(
name ==
"PKCS1v15" ||
name ==
"EME-PKCS1-v1_5") {
365 return TPM2_ALG_RSAES;
366 }
else if(
name ==
"Raw") {
367 return TPM2_ALG_NULL;
380 if(scheme.value() == TPM2_ALG_OAEP) {
390 return TPMT_RSA_DECRYPT{
391 .scheme = scheme.value(),
392 .details = {.oaep = {.hashAlg = hash.value()}},
394 }
else if(scheme.value() == TPM2_ALG_RSAES) {
395 return TPMT_RSA_DECRYPT{
396 .scheme = scheme.value(),
397 .details = {.rsaes = {}},
std::string to_formatted_string() const
std::string arg(size_t i) const
const std::string & algo_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 > block_cipher_tss2_to_botan(TPMI_ALG_SYM cipher_id, TPM2_KEY_BITS key_bits) noexcept
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< 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< std::string > hash_algo_tss2_to_botan(TPMI_ALG_HASH hash_id) noexcept
std::optional< TPMI_ALG_SYM_MODE > cipher_mode_botan_to_tss2(std::string_view mode_name) noexcept
std::optional< std::string > cipher_tss2_to_botan(TPMT_SYM_DEF cipher_def) noexcept
std::string get_botan_hash_name(TPM2_ALG_ID hash_id)
std::string fmt(std::string_view format, const T &... args)