Skip to main content

tss_esapi/structures/pcr/
select.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    structures::{PcrSelectSize, PcrSlot, PcrSlotCollection},
5    tss2_esys::TPMS_PCR_SELECT,
6    Error, Result,
7};
8
9use std::convert::TryFrom;
10/// This module contains necessary representations
11/// of the items belonging to the TPMS_PCR_SELECT
12/// structure.
13///
14/// The minimum number of octets allowed in a TPMS_PCR_SELECT.sizeOfSelect
15/// is not determined by the number of PCR implemented but by the
16/// number of PCR required by the platform-specific
17/// specification with which the TPM is compliant or by the implementer if
18/// not adhering to a platform-specific specification.
19#[derive(Debug, Copy, Clone, PartialEq, Eq)]
20pub struct PcrSelect {
21    pcr_slot_collection: PcrSlotCollection,
22}
23
24impl PcrSelect {
25    /// Creates a new PcrSelect
26    pub fn create(pcr_select_size: PcrSelectSize, pcr_slots: &[PcrSlot]) -> Result<Self> {
27        PcrSlotCollection::create(pcr_select_size, pcr_slots).map(|pcr_slot_collection| PcrSelect {
28            pcr_slot_collection,
29        })
30    }
31
32    /// Returns the size of the select.
33    ///
34    /// NB! This is not the same as how many [PcrSlot]
35    /// there are in the select but rather how many
36    /// octets that are needed to hold the bit field
37    /// that indicate what slots that are selected.
38    pub fn size_of_select(&self) -> PcrSelectSize {
39        self.pcr_slot_collection.size_of_select()
40    }
41
42    /// Returns the selected PCRs in the select.
43    pub fn selected_pcrs(&self) -> Vec<PcrSlot> {
44        self.pcr_slot_collection.collection()
45    }
46}
47
48impl TryFrom<TPMS_PCR_SELECT> for PcrSelect {
49    type Error = Error;
50    fn try_from(tss_pcr_select: TPMS_PCR_SELECT) -> Result<Self> {
51        PcrSlotCollection::try_from((tss_pcr_select.sizeofSelect, tss_pcr_select.pcrSelect)).map(
52            |pcr_slot_collection| PcrSelect {
53                pcr_slot_collection,
54            },
55        )
56    }
57}
58
59impl From<PcrSelect> for TPMS_PCR_SELECT {
60    fn from(pcr_select: PcrSelect) -> Self {
61        let (size_of_select, pcr_select) = pcr_select.pcr_slot_collection.into();
62        TPMS_PCR_SELECT {
63            sizeofSelect: size_of_select,
64            pcrSelect: pcr_select,
65        }
66    }
67}