tss_esapi/constants/
session_type.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    constants::tss::{TPM2_SE_HMAC, TPM2_SE_POLICY, TPM2_SE_TRIAL},
5    tss2_esys::TPM2_SE,
6    Error, Result, WrapperErrorKind,
7};
8use log::error;
9use num_derive::{FromPrimitive, ToPrimitive};
10use num_traits::{FromPrimitive, ToPrimitive};
11use std::convert::TryFrom;
12
13/// Enum representing the different TPM session types.
14#[derive(FromPrimitive, ToPrimitive, Debug, Copy, Clone, PartialEq, Eq)]
15#[repr(u8)]
16pub enum SessionType {
17    Hmac = TPM2_SE_HMAC,
18    Policy = TPM2_SE_POLICY,
19    Trial = TPM2_SE_TRIAL,
20}
21
22impl From<SessionType> for TPM2_SE {
23    fn from(session_type: SessionType) -> TPM2_SE {
24        // The values are well defined so this cannot fail.
25        session_type.to_u8().unwrap()
26    }
27}
28
29impl TryFrom<TPM2_SE> for SessionType {
30    type Error = Error;
31    fn try_from(tpm_session_type: TPM2_SE) -> Result<SessionType> {
32        SessionType::from_u8(tpm_session_type).ok_or_else(|| {
33            error!(
34                "value = {} did not match any SessionType.",
35                tpm_session_type
36            );
37            Error::local_error(WrapperErrorKind::InvalidParam)
38        })
39    }
40}