tss_esapi/constants/
ecc.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    constants::tss::{
5        TPM2_ECC_BN_P256, TPM2_ECC_BN_P638, TPM2_ECC_NIST_P192, TPM2_ECC_NIST_P224,
6        TPM2_ECC_NIST_P256, TPM2_ECC_NIST_P384, TPM2_ECC_NIST_P521, TPM2_ECC_SM2_P256,
7    },
8    tss2_esys::TPM2_ECC_CURVE,
9    Error, Result, WrapperErrorKind,
10};
11use log::error;
12use num_derive::{FromPrimitive, ToPrimitive};
13use num_traits::{FromPrimitive, ToPrimitive};
14use std::convert::TryFrom;
15/// Enum that contains the constants for the
16/// implemented elliptic curves.
17///
18/// # Details
19/// This corresponds to `TPM2_ECC_CURVE`
20#[derive(FromPrimitive, ToPrimitive, Debug, Copy, Clone, PartialEq, Eq, Hash)]
21#[repr(u16)]
22pub enum EccCurveIdentifier {
23    NistP192 = TPM2_ECC_NIST_P192,
24    NistP224 = TPM2_ECC_NIST_P224,
25    NistP256 = TPM2_ECC_NIST_P256,
26    NistP384 = TPM2_ECC_NIST_P384,
27    NistP521 = TPM2_ECC_NIST_P521,
28    BnP256 = TPM2_ECC_BN_P256,
29    BnP638 = TPM2_ECC_BN_P638,
30    Sm2P256 = TPM2_ECC_SM2_P256,
31}
32
33impl From<EccCurveIdentifier> for TPM2_ECC_CURVE {
34    fn from(curve: EccCurveIdentifier) -> Self {
35        // The values are well defined so this cannot fail.
36        curve.to_u16().unwrap()
37    }
38}
39
40impl TryFrom<TPM2_ECC_CURVE> for EccCurveIdentifier {
41    type Error = Error;
42
43    fn try_from(tpm2_ecc_curve: TPM2_ECC_CURVE) -> Result<Self> {
44        EccCurveIdentifier::from_u16(tpm2_ecc_curve).ok_or_else(|| {
45            error!("Value = {} did not match any ecc curve.", tpm2_ecc_curve);
46            Error::local_error(WrapperErrorKind::InvalidParam)
47        })
48    }
49}