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
57impl From<HashScheme> for HmacScheme {
58    fn from(hash_scheme: HashScheme) -> Self {
59        HmacScheme {
60            hashing_algorithm: hash_scheme.hashing_algorithm,
61        }
62    }
63}
64
65impl From<HmacScheme> for HashScheme {
66    fn from(hmac_scheme: HmacScheme) -> Self {
67        HashScheme {
68            hashing_algorithm: hmac_scheme.hashing_algorithm,
69        }
70    }
71}
72
73impl TryFrom<TPMS_SCHEME_HMAC> for HmacScheme {
74    type Error = Error;
75    fn try_from(tpms_scheme_hmac: TPMS_SCHEME_HMAC) -> Result<Self> {
76        Ok(HmacScheme {
77            hashing_algorithm: tpms_scheme_hmac.hashAlg.try_into()?,
78        })
79    }
80}
81
82impl From<HmacScheme> for TPMS_SCHEME_HMAC {
83    fn from(hash_scheme: HmacScheme) -> Self {
84        TPMS_SCHEME_HMAC {
85            hashAlg: hash_scheme.hashing_algorithm.into(),
86        }
87    }
88}
89
90/// Struct for holding the xor scheme
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub struct XorScheme {
93    hashing_algorithm: HashingAlgorithm,
94    key_derivation_function: KeyDerivationFunction,
95}
96
97impl XorScheme {
98    /// Creates a new XorScheme
99    pub const fn new(
100        hashing_algorithm: HashingAlgorithm,
101        key_derivation_function: KeyDerivationFunction,
102    ) -> XorScheme {
103        XorScheme {
104            hashing_algorithm,
105            key_derivation_function,
106        }
107    }
108}
109
110impl TryFrom<TPMS_SCHEME_XOR> for XorScheme {
111    type Error = Error;
112    fn try_from(tpms_scheme_xor: TPMS_SCHEME_XOR) -> Result<Self> {
113        Ok(XorScheme {
114            hashing_algorithm: tpms_scheme_xor.hashAlg.try_into()?,
115            key_derivation_function: tpms_scheme_xor.kdf.try_into()?,
116        })
117    }
118}
119
120impl From<XorScheme> for TPMS_SCHEME_XOR {
121    fn from(xor_scheme: XorScheme) -> Self {
122        TPMS_SCHEME_XOR {
123            hashAlg: xor_scheme.hashing_algorithm.into(),
124            kdf: xor_scheme.key_derivation_function.into(),
125        }
126    }
127}
128
129/// Struct for holding the ECDAA scheme
130///
131/// # Details
132/// This corresponds to the TPMS_SCHEME_ECDAA
133#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub struct EcDaaScheme {
135    hashing_algorithm: HashingAlgorithm,
136    count: u16,
137}
138
139impl EcDaaScheme {
140    /// Function for creating a new ECDAA scheme
141    pub const fn new(hashing_algorithm: HashingAlgorithm, count: u16) -> Self {
142        EcDaaScheme {
143            hashing_algorithm,
144            count,
145        }
146    }
147
148    /// Returns the hashing algorithm of the ECDAA scheme.
149    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
150        self.hashing_algorithm
151    }
152
153    /// Returns the count of the ECDAA.
154    pub const fn count(&self) -> u16 {
155        self.count
156    }
157}
158
159impl From<EcDaaScheme> for TPMS_SCHEME_ECDAA {
160    fn from(ec_daa_scheme: EcDaaScheme) -> Self {
161        TPMS_SCHEME_ECDAA {
162            hashAlg: ec_daa_scheme.hashing_algorithm.into(),
163            count: ec_daa_scheme.count,
164        }
165    }
166}
167
168impl TryFrom<TPMS_SCHEME_ECDAA> for EcDaaScheme {
169    type Error = Error;
170
171    fn try_from(tpms_scheme_ecdaa: TPMS_SCHEME_ECDAA) -> Result<Self> {
172        Ok(EcDaaScheme {
173            hashing_algorithm: tpms_scheme_ecdaa.hashAlg.try_into()?,
174            count: tpms_scheme_ecdaa.count,
175        })
176    }
177}