Skip to main content

tss_esapi/structures/
schemes.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    interface_types::algorithm::{HashingAlgorithm, KeyDerivationFunction},
5    tss2_esys::{TPMS_SCHEME_ECDAA, TPMS_SCHEME_HASH, TPMS_SCHEME_HMAC, TPMS_SCHEME_XOR},
6    Error, Result,
7};
8use std::convert::{TryFrom, TryInto};
9/// Struct for holding the hash scheme
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct HashScheme {
12    hashing_algorithm: HashingAlgorithm,
13}
14
15impl HashScheme {
16    /// Creates a new HashScheme
17    pub const fn new(hashing_algorithm: HashingAlgorithm) -> HashScheme {
18        HashScheme { hashing_algorithm }
19    }
20
21    /// Returns the hashing algorithm
22    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
23        self.hashing_algorithm
24    }
25}
26
27impl TryFrom<TPMS_SCHEME_HASH> for HashScheme {
28    type Error = Error;
29    fn try_from(tpms_scheme_hash: TPMS_SCHEME_HASH) -> Result<Self> {
30        Ok(HashScheme {
31            hashing_algorithm: tpms_scheme_hash.hashAlg.try_into()?,
32        })
33    }
34}
35
36impl From<HashScheme> for TPMS_SCHEME_HASH {
37    fn from(hash_scheme: HashScheme) -> Self {
38        TPMS_SCHEME_HASH {
39            hashAlg: hash_scheme.hashing_algorithm.into(),
40        }
41    }
42}
43
44/// Struct for holding HMAC scheme.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46pub struct HmacScheme {
47    hashing_algorithm: HashingAlgorithm,
48}
49
50impl HmacScheme {
51    /// Creates a new HmacScheme
52    pub const fn new(hashing_algorithm: HashingAlgorithm) -> HmacScheme {
53        HmacScheme { hashing_algorithm }
54    }
55
56    /// Returns the hashing algorithm
57    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
58        self.hashing_algorithm
59    }
60}
61
62impl From<HashScheme> for HmacScheme {
63    fn from(hash_scheme: HashScheme) -> Self {
64        HmacScheme {
65            hashing_algorithm: hash_scheme.hashing_algorithm,
66        }
67    }
68}
69
70impl From<HmacScheme> for HashScheme {
71    fn from(hmac_scheme: HmacScheme) -> Self {
72        HashScheme {
73            hashing_algorithm: hmac_scheme.hashing_algorithm,
74        }
75    }
76}
77
78impl TryFrom<TPMS_SCHEME_HMAC> for HmacScheme {
79    type Error = Error;
80    fn try_from(tpms_scheme_hmac: TPMS_SCHEME_HMAC) -> Result<Self> {
81        Ok(HmacScheme {
82            hashing_algorithm: tpms_scheme_hmac.hashAlg.try_into()?,
83        })
84    }
85}
86
87impl From<HmacScheme> for TPMS_SCHEME_HMAC {
88    fn from(hash_scheme: HmacScheme) -> Self {
89        TPMS_SCHEME_HMAC {
90            hashAlg: hash_scheme.hashing_algorithm.into(),
91        }
92    }
93}
94
95/// Struct for holding the xor scheme
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97pub struct XorScheme {
98    hashing_algorithm: HashingAlgorithm,
99    key_derivation_function: KeyDerivationFunction,
100}
101
102impl XorScheme {
103    /// Creates a new XorScheme
104    pub const fn new(
105        hashing_algorithm: HashingAlgorithm,
106        key_derivation_function: KeyDerivationFunction,
107    ) -> XorScheme {
108        XorScheme {
109            hashing_algorithm,
110            key_derivation_function,
111        }
112    }
113}
114
115impl TryFrom<TPMS_SCHEME_XOR> for XorScheme {
116    type Error = Error;
117    fn try_from(tpms_scheme_xor: TPMS_SCHEME_XOR) -> Result<Self> {
118        Ok(XorScheme {
119            hashing_algorithm: tpms_scheme_xor.hashAlg.try_into()?,
120            key_derivation_function: tpms_scheme_xor.kdf.try_into()?,
121        })
122    }
123}
124
125impl From<XorScheme> for TPMS_SCHEME_XOR {
126    fn from(xor_scheme: XorScheme) -> Self {
127        TPMS_SCHEME_XOR {
128            hashAlg: xor_scheme.hashing_algorithm.into(),
129            kdf: xor_scheme.key_derivation_function.into(),
130        }
131    }
132}
133
134/// Struct for holding the ECDAA scheme
135///
136/// # Details
137/// This corresponds to the TPMS_SCHEME_ECDAA
138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub struct EcDaaScheme {
140    hashing_algorithm: HashingAlgorithm,
141    count: u16,
142}
143
144impl EcDaaScheme {
145    /// Function for creating a new ECDAA scheme
146    pub const fn new(hashing_algorithm: HashingAlgorithm, count: u16) -> Self {
147        EcDaaScheme {
148            hashing_algorithm,
149            count,
150        }
151    }
152
153    /// Returns the hashing algorithm of the ECDAA scheme.
154    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
155        self.hashing_algorithm
156    }
157
158    /// Returns the count of the ECDAA.
159    pub const fn count(&self) -> u16 {
160        self.count
161    }
162}
163
164impl From<EcDaaScheme> for TPMS_SCHEME_ECDAA {
165    fn from(ec_daa_scheme: EcDaaScheme) -> Self {
166        TPMS_SCHEME_ECDAA {
167            hashAlg: ec_daa_scheme.hashing_algorithm.into(),
168            count: ec_daa_scheme.count,
169        }
170    }
171}
172
173impl TryFrom<TPMS_SCHEME_ECDAA> for EcDaaScheme {
174    type Error = Error;
175
176    fn try_from(tpms_scheme_ecdaa: TPMS_SCHEME_ECDAA) -> Result<Self> {
177        Ok(EcDaaScheme {
178            hashing_algorithm: tpms_scheme_ecdaa.hashAlg.try_into()?,
179            count: tpms_scheme_ecdaa.count,
180        })
181    }
182}