1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use crate::{
    interface_types::algorithm::PublicAlgorithm,
    structures::{
        PublicEccParameters, PublicKeyedHashParameters, PublicRsaParameters,
        SymmetricCipherParameters,
    },
    tss2_esys::{TPMT_PUBLIC_PARMS, TPMU_PUBLIC_PARMS},
    Error, Result,
};
use std::convert::{TryFrom, TryInto};
/// Enum representing the public parameters structure.
///
/// # Details
/// This corresponds to TPMT_PUBLIC_PARMS
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PublicParameters {
    Rsa(PublicRsaParameters),
    KeyedHash(PublicKeyedHashParameters),
    Ecc(PublicEccParameters),
    SymCipher(SymmetricCipherParameters),
}

impl PublicParameters {
    /// Returns the algorithm
    pub fn algorithm(&self) -> PublicAlgorithm {
        match self {
            PublicParameters::Rsa(_) => PublicAlgorithm::Rsa,
            PublicParameters::KeyedHash(_) => PublicAlgorithm::KeyedHash,
            PublicParameters::Ecc(_) => PublicAlgorithm::Ecc,
            PublicParameters::SymCipher(_) => PublicAlgorithm::SymCipher,
        }
    }
}

impl From<PublicParameters> for TPMT_PUBLIC_PARMS {
    fn from(public_parameters: PublicParameters) -> TPMT_PUBLIC_PARMS {
        let algorithm = public_parameters.algorithm();
        match public_parameters {
            PublicParameters::Rsa(parameters) => TPMT_PUBLIC_PARMS {
                type_: algorithm.into(),
                parameters: TPMU_PUBLIC_PARMS {
                    rsaDetail: parameters.into(),
                },
            },
            PublicParameters::KeyedHash(parameters) => TPMT_PUBLIC_PARMS {
                type_: algorithm.into(),
                parameters: TPMU_PUBLIC_PARMS {
                    keyedHashDetail: parameters.into(),
                },
            },
            PublicParameters::Ecc(parameters) => TPMT_PUBLIC_PARMS {
                type_: algorithm.into(),
                parameters: TPMU_PUBLIC_PARMS {
                    eccDetail: parameters.into(),
                },
            },
            PublicParameters::SymCipher(parameters) => TPMT_PUBLIC_PARMS {
                type_: algorithm.into(),
                parameters: TPMU_PUBLIC_PARMS {
                    symDetail: parameters.into(),
                },
            },
        }
    }
}

impl TryFrom<TPMT_PUBLIC_PARMS> for PublicParameters {
    type Error = Error;

    fn try_from(tpmt_public_parms: TPMT_PUBLIC_PARMS) -> Result<Self> {
        match PublicAlgorithm::try_from(tpmt_public_parms.type_)? {
            PublicAlgorithm::Rsa => Ok(PublicParameters::Rsa(
                unsafe { tpmt_public_parms.parameters.rsaDetail }.try_into()?,
            )),
            PublicAlgorithm::KeyedHash => Ok(PublicParameters::KeyedHash(
                unsafe { tpmt_public_parms.parameters.keyedHashDetail }.try_into()?,
            )),
            PublicAlgorithm::Ecc => Ok(PublicParameters::Ecc(
                unsafe { tpmt_public_parms.parameters.eccDetail }.try_into()?,
            )),
            PublicAlgorithm::SymCipher => Ok(PublicParameters::SymCipher(
                unsafe { tpmt_public_parms.parameters.symDetail }.try_into()?,
            )),
        }
    }
}