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