|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + abstraction::pcr::PcrBank, |
| 6 | + interface_types::algorithm::HashingAlgorithm, |
| 7 | + structures::{Digest, DigestList, PcrSelectionList}, |
| 8 | + tss2_esys::TPML_DIGEST, |
| 9 | + Error, Result, WrapperErrorKind, |
| 10 | +}; |
| 11 | +use log::error; |
| 12 | +/// Struct holding pcr banks and their associated |
| 13 | +/// hashing algorithm |
| 14 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 15 | +pub struct PcrData { |
| 16 | + data: Vec<(HashingAlgorithm, PcrBank)>, |
| 17 | +} |
| 18 | + |
| 19 | +impl PcrData { |
| 20 | + /// Creates new empty PcrData |
| 21 | + pub const fn new() -> Self { |
| 22 | + PcrData { data: Vec::new() } |
| 23 | + } |
| 24 | + |
| 25 | + /// Function for creating PcrData from a pcr selection list and pcr digests list. |
| 26 | + pub fn create( |
| 27 | + pcr_selection_list: &PcrSelectionList, |
| 28 | + digest_list: &DigestList, |
| 29 | + ) -> Result<PcrData> { |
| 30 | + Ok(PcrData { |
| 31 | + data: Self::create_data(pcr_selection_list, digest_list.value().to_vec())?, |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + /// Adds data to the PcrData |
| 36 | + pub fn add( |
| 37 | + &mut self, |
| 38 | + pcr_selection_list: &PcrSelectionList, |
| 39 | + digest_list: &DigestList, |
| 40 | + ) -> Result<()> { |
| 41 | + self.data.append(&mut Self::create_data( |
| 42 | + pcr_selection_list, |
| 43 | + digest_list.value().to_vec(), |
| 44 | + )?); |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | + |
| 48 | + /// Function for turning a pcr selection list and pcr digests values |
| 49 | + /// into the format in which data is stored in PcrData. |
| 50 | + fn create_data( |
| 51 | + pcr_selection_list: &PcrSelectionList, |
| 52 | + mut digests: Vec<Digest>, |
| 53 | + ) -> Result<Vec<(HashingAlgorithm, PcrBank)>> { |
| 54 | + pcr_selection_list |
| 55 | + .get_selections() |
| 56 | + .iter() |
| 57 | + .map(|pcr_selection| { |
| 58 | + let pcr_slots = pcr_selection.selected_pcrs(); |
| 59 | + if pcr_slots.len() > digests.len() { |
| 60 | + error!("More pcr slots in selection then available digests"); |
| 61 | + return Err(Error::local_error(WrapperErrorKind::InconsistentParams)); |
| 62 | + } |
| 63 | + let digests_in_bank = digests.drain(..pcr_slots.len()).collect(); |
| 64 | + Ok(( |
| 65 | + pcr_selection.hashing_algorithm(), |
| 66 | + PcrBank::create(pcr_slots, digests_in_bank)?, |
| 67 | + )) |
| 68 | + }) |
| 69 | + .collect() |
| 70 | + } |
| 71 | + |
| 72 | + /// Function for retrieving the first PCR values associated with hashing_algorithm. |
| 73 | + pub fn pcr_bank(&self, hashing_algorithm: HashingAlgorithm) -> Option<&PcrBank> { |
| 74 | + self.data |
| 75 | + .iter() |
| 76 | + .find(|(alg, _)| alg == &hashing_algorithm) |
| 77 | + .map(|(_, bank)| bank) |
| 78 | + } |
| 79 | + |
| 80 | + /// Function for retrieving the number of banks in the data. |
| 81 | + pub fn len(&self) -> usize { |
| 82 | + self.data.len() |
| 83 | + } |
| 84 | + |
| 85 | + /// Returns true if there are no banks in the data. |
| 86 | + pub fn is_empty(&self) -> bool { |
| 87 | + self.data.is_empty() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<'a> IntoIterator for PcrData { |
| 92 | + type Item = (HashingAlgorithm, PcrBank); |
| 93 | + type IntoIter = ::std::vec::IntoIter<(HashingAlgorithm, PcrBank)>; |
| 94 | + |
| 95 | + fn into_iter(self) -> Self::IntoIter { |
| 96 | + self.data.into_iter() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl From<PcrData> for Vec<TPML_DIGEST> { |
| 101 | + fn from(pcr_data: PcrData) -> Self { |
| 102 | + pcr_data |
| 103 | + .data |
| 104 | + .iter() |
| 105 | + .flat_map(|(_, pcr_bank)| pcr_bank.into_iter()) |
| 106 | + .map(|(_, digest)| digest) |
| 107 | + .collect::<Vec<&Digest>>() |
| 108 | + .chunks(DigestList::MAX_SIZE) |
| 109 | + .map(|digests| { |
| 110 | + let mut tpml_digest: TPML_DIGEST = Default::default(); |
| 111 | + for (index, digest) in digests.iter().enumerate() { |
| 112 | + tpml_digest.count += 1; |
| 113 | + tpml_digest.digests[index].size = digest.len() as u16; |
| 114 | + tpml_digest.digests[index].buffer[..digest.len()] |
| 115 | + .copy_from_slice(digest.value()); |
| 116 | + } |
| 117 | + tpml_digest |
| 118 | + }) |
| 119 | + .collect() |
| 120 | + } |
| 121 | +} |
0 commit comments