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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use crate::{
    interface_types::algorithm::HashingAlgorithm,
    structures::{EccParameter, PublicKeyRsa},
    tss2_esys::{TPMS_SIGNATURE_ECC, TPMS_SIGNATURE_RSA},
    Error, Result, WrapperErrorKind,
};
use log::error;
use std::convert::{TryFrom, TryInto};

/// Type holding RSA signature information.
///
/// For more information about the contents of `signature` see Annex B
/// in the Architecture spec.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RsaSignature {
    hashing_algorithm: HashingAlgorithm,
    signature: PublicKeyRsa,
}

impl RsaSignature {
    /// Creates new RSA signature
    ///
    /// # Errors
    /// Using [Null][`HashingAlgorithm::Null`] will cause an error.
    pub fn create(hashing_algorithm: HashingAlgorithm, signature: PublicKeyRsa) -> Result<Self> {
        if hashing_algorithm == HashingAlgorithm::Null {
            error!("Hashing algorithm Null is not allowed in RsaSignature");
            return Err(Error::local_error(WrapperErrorKind::InvalidParam));
        }
        Ok(RsaSignature {
            hashing_algorithm,
            signature,
        })
    }

    /// Returns the hashing algorithm
    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
        self.hashing_algorithm
    }

    /// Returns the signature
    pub const fn signature(&self) -> &PublicKeyRsa {
        &self.signature
    }
}

impl From<RsaSignature> for TPMS_SIGNATURE_RSA {
    fn from(rsa_signature: RsaSignature) -> Self {
        TPMS_SIGNATURE_RSA {
            hash: rsa_signature.hashing_algorithm.into(),
            sig: rsa_signature.signature.into(),
        }
    }
}

impl TryFrom<TPMS_SIGNATURE_RSA> for RsaSignature {
    type Error = Error;

    fn try_from(tpms_signature_rsa: TPMS_SIGNATURE_RSA) -> Result<Self> {
        let hashing_algorithm = tpms_signature_rsa.hash.try_into()?;
        if hashing_algorithm == HashingAlgorithm::Null {
            error!("Received invalid hashing algorithm Null from the tpm in the RSA signature.");
            return Err(Error::local_error(WrapperErrorKind::WrongValueFromTpm));
        }

        Ok(RsaSignature {
            hashing_algorithm,
            signature: tpms_signature_rsa.sig.try_into()?,
        })
    }
}

/// Type holding ECC signature information.
///
/// For more information about the contents of `signature_r` and `signature_s`
/// see Annex B in the Architecture spec (or Annex D for SM2 signatures).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EccSignature {
    hashing_algorithm: HashingAlgorithm,
    signature_r: EccParameter,
    signature_s: EccParameter,
}

impl EccSignature {
    /// Creates new ECC signature
    pub fn create(
        hashing_algorithm: HashingAlgorithm,
        signature_r: EccParameter,
        signature_s: EccParameter,
    ) -> Result<Self> {
        if hashing_algorithm == HashingAlgorithm::Null {
            error!("Hashing algorithm Null is not allowed in RsaSignature");
            return Err(Error::local_error(WrapperErrorKind::InvalidParam));
        }
        Ok(EccSignature {
            hashing_algorithm,
            signature_r,
            signature_s,
        })
    }

    /// Returns the hashing algorithm
    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
        self.hashing_algorithm
    }

    /// Returns signature r
    pub const fn signature_r(&self) -> &EccParameter {
        &self.signature_r
    }

    /// Returns signature s
    pub const fn signature_s(&self) -> &EccParameter {
        &self.signature_s
    }
}

impl From<EccSignature> for TPMS_SIGNATURE_ECC {
    fn from(ecc_signature: EccSignature) -> Self {
        TPMS_SIGNATURE_ECC {
            hash: ecc_signature.hashing_algorithm.into(),
            signatureR: ecc_signature.signature_r.into(),
            signatureS: ecc_signature.signature_s.into(),
        }
    }
}

impl TryFrom<TPMS_SIGNATURE_ECC> for EccSignature {
    type Error = Error;

    fn try_from(tpms_signature_ecc: TPMS_SIGNATURE_ECC) -> Result<Self> {
        let hashing_algorithm = tpms_signature_ecc.hash.try_into()?;
        if hashing_algorithm == HashingAlgorithm::Null {
            error!("Received invalid hashing algorithm Null from the tpm in the ECC signature.");
            return Err(Error::local_error(WrapperErrorKind::WrongValueFromTpm));
        }
        Ok(EccSignature {
            hashing_algorithm,
            signature_r: tpms_signature_ecc.signatureR.try_into()?,
            signature_s: tpms_signature_ecc.signatureS.try_into()?,
        })
    }
}