tss_esapi/constants/
ecc.rs1use 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#[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 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}