Skip to content

Commit 964001e

Browse files
committed
combine: Support TPMEvent combinations for N images
Signed-off-by: Beñat Gartzia Arruabarrena <[email protected]>
1 parent 81fa7db commit 964001e

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

lib/src/pcrs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const PCR_INIT_VALUE: [u8; 32] = [
1717
mod tests;
1818

1919
#[serde_as]
20-
#[derive(Clone, Serialize, Deserialize)]
21-
#[cfg_attr(test, derive(PartialEq, Debug))]
20+
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
21+
#[cfg_attr(test, derive(Debug))]
2222
pub struct Part {
2323
pub name: String,
2424
#[serde_as(as = "serde_with::hex::Hex")]
2525
pub hash: Vec<u8>,
2626
}
2727

2828
#[serde_as]
29-
#[derive(Clone, Serialize, Deserialize)]
30-
#[cfg_attr(test, derive(PartialEq, Debug))]
29+
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
30+
#[cfg_attr(test, derive(Debug))]
3131
pub struct Pcr {
3232
pub id: u64,
3333
#[serde_as(as = "serde_with::hex::Hex")]

lib/src/tpmevents/combine.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,20 @@ use std::collections::HashMap;
7171
use super::*;
7272
use crate::pcrs::{Pcr, compile_pcrs};
7373

74+
use itertools::Itertools;
75+
7476
#[cfg(test)]
7577
mod tests;
7678

79+
pub fn combine_images(images: &Vec<Vec<TPMEvent>>) -> Vec<Vec<Pcr>> {
80+
images
81+
.iter()
82+
.combinations(2)
83+
.flat_map(|p| combine(p[0], p[1]))
84+
.unique()
85+
.collect()
86+
}
87+
7788
pub fn combine(this: &[TPMEvent], that: &[TPMEvent]) -> Vec<Vec<Pcr>> {
7889
let map_this = tpm_event_id_hashmap(this);
7990
let map_that = tpm_event_id_hashmap(that);

lib/src/tpmevents/combine/tests.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::pcrs::{Pcr, compile_pcrs};
66
use crate::tpmevents::{TPMEvent, TPMEventID};
77

88
use hex::decode;
9-
use std::collections::HashMap;
9+
use std::collections::{HashMap, HashSet};
1010

1111
#[test]
1212
fn test_tpm_event_id_hashmap() {
@@ -1207,3 +1207,68 @@ fn test_pcr7_enable_secureboot() {
12071207

12081208
assert_eq!(res, expected);
12091209
}
1210+
1211+
#[test]
1212+
fn test_image_combinations() {
1213+
let shim1 = TPMEvent {
1214+
name: "shim1".into(),
1215+
pcr: 4,
1216+
hash: decode("f6f919856f814f30c2043b567c9434b73b658f2360175f18e49da81112216be0").unwrap(),
1217+
id: TPMEventID::Pcr4Shim,
1218+
};
1219+
let shim2 = TPMEvent {
1220+
name: "shim2".into(),
1221+
pcr: 4,
1222+
hash: decode("5921135eb8f625f3050a92d66551ef0a6682b8c393af8ef8379a1332f1f1872f").unwrap(),
1223+
id: TPMEventID::Pcr4Shim,
1224+
};
1225+
let kernel1 = TPMEvent {
1226+
name: "kernel1".into(),
1227+
pcr: 4,
1228+
hash: decode("2b1dc59bc61dbbc3db11a6f3b0708c948efd46cceb7f6c8ea2024b8d1b8c829a").unwrap(),
1229+
id: TPMEventID::Pcr4Vmlinuz,
1230+
};
1231+
let kernel2 = TPMEvent {
1232+
name: "kernel2".into(),
1233+
pcr: 4,
1234+
hash: decode("d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35").unwrap(),
1235+
id: TPMEventID::Pcr4Vmlinuz,
1236+
};
1237+
let kernel3 = TPMEvent {
1238+
name: "kernel3".into(),
1239+
pcr: 4,
1240+
hash: decode("4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce").unwrap(),
1241+
id: TPMEventID::Pcr4Vmlinuz,
1242+
};
1243+
let kernel4 = TPMEvent {
1244+
name: "kernel4".into(),
1245+
pcr: 4,
1246+
hash: decode("4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a").unwrap(),
1247+
id: TPMEventID::Pcr4Vmlinuz,
1248+
};
1249+
1250+
let images = vec![
1251+
vec![shim1.clone(), kernel1.clone()],
1252+
vec![shim1.clone(), kernel2.clone()],
1253+
vec![shim2.clone(), kernel3.clone()],
1254+
vec![shim2.clone(), kernel4.clone()],
1255+
];
1256+
1257+
let res = combine_images(&images);
1258+
let expected: Vec<Vec<Pcr>> = vec![
1259+
compile_pcrs(&vec![shim1.clone(), kernel1.clone()]),
1260+
compile_pcrs(&vec![shim1.clone(), kernel2.clone()]),
1261+
compile_pcrs(&vec![shim1.clone(), kernel3.clone()]),
1262+
compile_pcrs(&vec![shim1.clone(), kernel4.clone()]),
1263+
compile_pcrs(&vec![shim2.clone(), kernel1.clone()]),
1264+
compile_pcrs(&vec![shim2.clone(), kernel2.clone()]),
1265+
compile_pcrs(&vec![shim2.clone(), kernel3.clone()]),
1266+
compile_pcrs(&vec![shim2.clone(), kernel4.clone()]),
1267+
];
1268+
1269+
assert_eq!(res.len(), expected.len());
1270+
assert_eq!(
1271+
HashSet::<_>::from_iter(res.iter().flat_map(|e| e.clone())),
1272+
HashSet::<_>::from_iter(expected.iter().flat_map(|e| e.clone())),
1273+
);
1274+
}

0 commit comments

Comments
 (0)