Skip to main content

tss_esapi/abstraction/
mod.rs

1// Copyright 2019 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod ak;
5pub mod cipher;
6pub mod ek;
7pub mod no_tpm;
8pub mod nv;
9pub mod pcr;
10pub mod public;
11pub mod transient;
12
13mod hashing;
14mod signatures;
15mod signer;
16pub use hashing::AssociatedHashingAlgorithm;
17pub use signer::EcSigner;
18#[cfg(feature = "rsa")]
19pub use signer::{RsaPkcsSigner, RsaPssSigner};
20
21use std::convert::TryFrom;
22
23use crate::{
24    attributes::ObjectAttributesBuilder,
25    interface_types::{algorithm::AsymmetricAlgorithm, ecc::EccCurve, key_bits::RsaKeyBits},
26    structures::PublicBuilder,
27    Error, WrapperErrorKind,
28};
29
30/// KeyCustomizaion allows to adjust how a key is going to be created
31pub trait KeyCustomization {
32    /// Alter the attributes used on key creation
33    fn attributes(&self, attributes_builder: ObjectAttributesBuilder) -> ObjectAttributesBuilder {
34        attributes_builder
35    }
36
37    /// Alter the key template used on key creation
38    fn template(&self, template_builder: PublicBuilder) -> PublicBuilder {
39        template_builder
40    }
41}
42
43/// IntoKeyCustomization transforms a type into a type that support KeyCustomization
44pub trait IntoKeyCustomization {
45    type T: KeyCustomization;
46
47    fn into_key_customization(self) -> Option<Self::T>;
48}
49
50impl<T: KeyCustomization> IntoKeyCustomization for T {
51    type T = T;
52
53    fn into_key_customization(self) -> Option<Self::T> {
54        Some(self)
55    }
56}
57
58#[derive(Debug, Copy, Clone)]
59pub struct DefaultKey;
60#[derive(Debug, Copy, Clone)]
61pub struct DefaultKeyImpl;
62impl KeyCustomization for DefaultKeyImpl {}
63
64impl IntoKeyCustomization for DefaultKey {
65    type T = DefaultKeyImpl;
66
67    fn into_key_customization(self) -> Option<Self::T> {
68        None
69    }
70}
71
72impl IntoKeyCustomization for Option<DefaultKey> {
73    type T = DefaultKeyImpl;
74
75    fn into_key_customization(self) -> Option<Self::T> {
76        None
77    }
78}
79
80/// Enum representing the asymmetric algorithm interface type with specific properties.
81///
82/// # Details
83/// Use this instead of [AsymmetricAlgorithm].
84#[derive(Copy, Clone, Debug, PartialEq, Eq)]
85pub enum AsymmetricAlgorithmSelection {
86    Rsa(RsaKeyBits),
87    Ecc(EccCurve),
88}
89
90/// The conversion assumes for RSA 2048 bit size and for ECC the Nist P256 curve,
91/// which matches the defaults in tpm2-tools.
92impl TryFrom<AsymmetricAlgorithm> for AsymmetricAlgorithmSelection {
93    type Error = Error;
94
95    fn try_from(value: AsymmetricAlgorithm) -> Result<Self, Self::Error> {
96        match value {
97            AsymmetricAlgorithm::Rsa => Ok(AsymmetricAlgorithmSelection::Rsa(RsaKeyBits::Rsa2048)),
98            AsymmetricAlgorithm::Ecc => Ok(AsymmetricAlgorithmSelection::Ecc(EccCurve::NistP256)),
99            AsymmetricAlgorithm::Null => {
100                Err(Error::local_error(WrapperErrorKind::UnsupportedParam))
101            }
102        }
103    }
104}