|
| 1 | +// Copyright 2021 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + structures::{Digest, PcrSlot}, |
| 6 | + Error, Result, WrapperErrorKind, |
| 7 | +}; |
| 8 | +use log::error; |
| 9 | +use std::collections::BTreeMap; |
| 10 | + |
| 11 | +/// Struct for holding PcrSlots and their |
| 12 | +/// corresponding values. |
| 13 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 14 | +pub struct PcrBank { |
| 15 | + bank: BTreeMap<PcrSlot, Digest>, |
| 16 | +} |
| 17 | + |
| 18 | +impl PcrBank { |
| 19 | + /// Function that creates PcrBank from a vector of pcr slots and |
| 20 | + /// a vector of pcr digests. |
| 21 | + /// |
| 22 | + /// # Details |
| 23 | + /// The order of pcr slots are assumed to match the order of the Digests. |
| 24 | + /// |
| 25 | + /// # Error |
| 26 | + /// - If number of pcr slots does not match the number of pcr digests |
| 27 | + /// InconsistentParams error is returned. |
| 28 | + /// |
| 29 | + /// - If the vector of pcr slots contains duplicates then |
| 30 | + /// InconsistentParams error is returned. |
| 31 | + pub fn create(mut pcr_slots: Vec<PcrSlot>, mut digests: Vec<Digest>) -> Result<PcrBank> { |
| 32 | + if pcr_slots.len() != digests.len() { |
| 33 | + error!( |
| 34 | + "Number of PcrSlots does not match the number of PCR digests. ({} != {})", |
| 35 | + pcr_slots.len(), |
| 36 | + digests.len() |
| 37 | + ); |
| 38 | + return Err(Error::local_error(WrapperErrorKind::InconsistentParams)); |
| 39 | + } |
| 40 | + pcr_slots |
| 41 | + .drain(..) |
| 42 | + .zip(digests.drain(..)) |
| 43 | + .try_fold(BTreeMap::<PcrSlot, Digest>::new(), |mut data, (pcr_slot, digest)| { |
| 44 | + if data.insert(pcr_slot, digest).is_none() { |
| 45 | + Ok(data) |
| 46 | + } else { |
| 47 | + error!("Error trying to insert data into PcrSlot {:?} where data have already been inserted", pcr_slot); |
| 48 | + Err(Error::local_error(WrapperErrorKind::InconsistentParams)) |
| 49 | + } |
| 50 | + }) |
| 51 | + .map(|bank| PcrBank { bank }) |
| 52 | + } |
| 53 | + |
| 54 | + /// Retrieves reference to a [Digest] associated with the provided [PcrSlot]. |
| 55 | + /// |
| 56 | + /// # Details |
| 57 | + /// Returns a reference to a [Digest] associated with the provided [PcrSlot] |
| 58 | + /// if one exists else returns None. |
| 59 | + pub fn get_digest(&self, pcr_slot: PcrSlot) -> Option<&Digest> { |
| 60 | + self.bank.get(&pcr_slot) |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns true if the [PcrBank] contains a digest |
| 64 | + /// for the provided [PcrSlot]. |
| 65 | + pub fn has_digest(&self, pcr_slot: PcrSlot) -> bool { |
| 66 | + self.bank.contains_key(&pcr_slot) |
| 67 | + } |
| 68 | + |
| 69 | + /// Number of digests in the [PcrBank]] |
| 70 | + pub fn len(&self) -> usize { |
| 71 | + self.bank.len() |
| 72 | + } |
| 73 | + |
| 74 | + /// Returns true if the [PcrBank] is empty |
| 75 | + pub fn is_empty(&self) -> bool { |
| 76 | + self.bank.is_empty() |
| 77 | + } |
| 78 | + |
| 79 | + /// Removees the [Digest] associated with the [PcrSlot] and |
| 80 | + /// returns it. |
| 81 | + /// |
| 82 | + /// # Details |
| 83 | + /// Removes the [Digest] associated with the provided [PcrSlot] |
| 84 | + /// out of the bank and returns it if it exists else returns None. |
| 85 | + pub fn remove_digest(&mut self, pcr_slot: PcrSlot) -> Option<Digest> { |
| 86 | + self.bank.remove(&pcr_slot) |
| 87 | + } |
| 88 | + |
| 89 | + /// Inserts [Digest] value associated with a [PcrSlot] into the bank. |
| 90 | + /// |
| 91 | + /// # Error |
| 92 | + /// Returns an error if a [Digest] is already associated with the |
| 93 | + /// provided [PcrSlot]. |
| 94 | + pub fn insert_digest(&mut self, pcr_slot: PcrSlot, digest: Digest) -> Result<()> { |
| 95 | + self.ensure_non_existing(pcr_slot, "Failed to insert")?; |
| 96 | + let _ = self.bank.insert(pcr_slot, digest); |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | + |
| 100 | + /// Attempts to extend the [PcrBank] with `other`. |
| 101 | + /// |
| 102 | + /// # Error |
| 103 | + /// Returns an error if the a value in `other`already |
| 104 | + /// exists. |
| 105 | + pub fn try_extend(&mut self, other: PcrBank) -> Result<()> { |
| 106 | + other |
| 107 | + .bank |
| 108 | + .keys() |
| 109 | + .try_for_each(|&pcr_slot| self.ensure_non_existing(pcr_slot, "Failed to extend"))?; |
| 110 | + self.bank.extend(other.bank); |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | + |
| 114 | + /// Returns an error if a [Digest] for [PcrSlot] already exists in the bank |
| 115 | + fn ensure_non_existing(&self, pcr_slot: PcrSlot, error_msg: &str) -> Result<()> { |
| 116 | + if self.has_digest(pcr_slot) { |
| 117 | + error!( |
| 118 | + "{}, a digest already for PcrSlot {:?} exists in the bank", |
| 119 | + error_msg, pcr_slot |
| 120 | + ); |
| 121 | + return Err(Error::local_error(WrapperErrorKind::InvalidParam)); |
| 122 | + } |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<'a> IntoIterator for &'a PcrBank { |
| 128 | + type Item = (&'a PcrSlot, &'a Digest); |
| 129 | + type IntoIter = ::std::collections::btree_map::Iter<'a, PcrSlot, Digest>; |
| 130 | + |
| 131 | + fn into_iter(self) -> Self::IntoIter { |
| 132 | + self.bank.iter() |
| 133 | + } |
| 134 | +} |
0 commit comments