tss_esapi/structures/property/
algorithm_property.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    attributes::AlgorithmAttributes, constants::AlgorithmIdentifier, tss2_esys::TPMS_ALG_PROPERTY,
6    Error, Result,
7};
8use std::convert::{TryFrom, TryInto};
9
10/// Strucutre for holding information describing an
11/// algorithm.
12///
13/// # Details
14/// This corresponds to the TPMS_ALG_PROPERTY
15/// structure.
16#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17pub struct AlgorithmProperty {
18    algorithm_identifier: AlgorithmIdentifier,
19    algorithm_properties: AlgorithmAttributes,
20}
21
22impl AlgorithmProperty {
23    /// Creates a new AlgorithmProperty with the
24    /// given parameters.
25    pub const fn new(
26        algorithm_identifier: AlgorithmIdentifier,
27        algorithm_properties: AlgorithmAttributes,
28    ) -> Self {
29        AlgorithmProperty {
30            algorithm_identifier,
31            algorithm_properties,
32        }
33    }
34
35    /// Returns the algorithm identifier
36    pub const fn algorithm_identifier(&self) -> AlgorithmIdentifier {
37        self.algorithm_identifier
38    }
39
40    /// Returns the algorithm properties
41    pub const fn algorithm_properties(&self) -> AlgorithmAttributes {
42        self.algorithm_properties
43    }
44}
45
46impl TryFrom<TPMS_ALG_PROPERTY> for AlgorithmProperty {
47    type Error = Error;
48
49    fn try_from(tpms_algorithm_description: TPMS_ALG_PROPERTY) -> Result<Self> {
50        Ok(AlgorithmProperty {
51            algorithm_identifier: tpms_algorithm_description.alg.try_into()?,
52            algorithm_properties: tpms_algorithm_description.algProperties.into(),
53        })
54    }
55}
56
57impl From<AlgorithmProperty> for TPMS_ALG_PROPERTY {
58    fn from(algorithm_description: AlgorithmProperty) -> Self {
59        TPMS_ALG_PROPERTY {
60            alg: algorithm_description.algorithm_identifier.into(),
61            algProperties: algorithm_description.algorithm_properties.into(),
62        }
63    }
64}