tss_esapi/structures/ecc/
point.rs

1use tss_esapi_sys::TPM2B_ECC_POINT;
2
3// Copyright 2021 Contributors to the Parsec project.
4// SPDX-License-Identifier: Apache-2.0
5use crate::{structures::EccParameter, tss2_esys::TPMS_ECC_POINT, Error, Result};
6use std::{
7    convert::{TryFrom, TryInto},
8    mem::size_of,
9};
10
11/// Structure holding ecc point information
12///
13/// # Details
14/// This corresponds to TPMS_ECC_POINT
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct EccPoint {
17    x: EccParameter,
18    y: EccParameter,
19}
20
21impl EccPoint {
22    /// Creates a new ecc point
23    pub const fn new(x: EccParameter, y: EccParameter) -> Self {
24        EccPoint { x, y }
25    }
26
27    /// Returns x value as an [EccParameter] reference.
28    pub const fn x(&self) -> &EccParameter {
29        &self.x
30    }
31
32    /// Returns y value as an [EccParameter] reference.
33    pub const fn y(&self) -> &EccParameter {
34        &self.y
35    }
36}
37
38impl Default for EccPoint {
39    fn default() -> Self {
40        EccPoint::new(EccParameter::default(), EccParameter::default())
41    }
42}
43
44impl From<EccPoint> for TPMS_ECC_POINT {
45    fn from(ecc_point: EccPoint) -> Self {
46        TPMS_ECC_POINT {
47            x: ecc_point.x.into(),
48            y: ecc_point.y.into(),
49        }
50    }
51}
52
53impl From<EccPoint> for TPM2B_ECC_POINT {
54    fn from(ecc_point: EccPoint) -> Self {
55        let size = size_of::<u16>() + ecc_point.x().len() + size_of::<u16>() + ecc_point.y().len();
56        TPM2B_ECC_POINT {
57            size: size as u16,
58            point: ecc_point.into(),
59        }
60    }
61}
62
63impl TryFrom<TPMS_ECC_POINT> for EccPoint {
64    type Error = Error;
65
66    fn try_from(tpms_ecc_point: TPMS_ECC_POINT) -> Result<Self> {
67        Ok(EccPoint {
68            x: tpms_ecc_point.x.try_into()?,
69            y: tpms_ecc_point.y.try_into()?,
70        })
71    }
72}