Skip to content

Commit a8d4f83

Browse files
committed
feat(fde): support multiple hash algorithms in show-reference-value
The `show-reference-value` command now supports multiple hash algorithms: sha1, sha256, sha384, and sm3. You can specify them using `--hash-algo`, for example: --hash-algo sha256 --hash-algo sm3 Multiple values are allowed; defaults to sha384 and sm3. Added SHA-1 support via the `sha1` crate. Improved CLI documentation to clearly explain which boot components are measured and how the `stage` parameter works. Also added validation to ensure at least one hash algorithm is provided. Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
1 parent 2184d35 commit a8d4f83

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ scopeguard = "1.2.0"
4242
serde = {version = "1.0", features = ["derive"]}
4343
serde_json = {version = "1", features = ["preserve_order"]}
4444
serde_variant = "0.1.3"
45+
sha1 = "0.10.6"
4546
sha2 = {version = "0.10.8"}
4647
shadow-rs = {version = "0.35.2", default-features = false}
4748
sm3 = "0.4.2"

src/cli.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,33 @@ pub struct FdeOptions {
116116

117117
#[derive(Subcommand, Debug)]
118118
pub enum FdeSubcommand {
119-
/// Show claims related to FDE.
119+
/// Display cryptographic reference values (e.g., hashes) of boot-related components for attestation.
120+
///
121+
/// This includes artifacts such as:
122+
/// - GRUB configuration and binaries
123+
/// - Shim and bootloader
124+
/// - Initrd and kernel images
125+
/// - Kernel command line
126+
///
127+
/// For encrypted (FDE) disks, additional values are included:
128+
/// - Root filesystem hash (integrity measurement)
129+
/// - Cryptpilot configuration bundle hash
130+
///
131+
/// Supports both encrypted (FDE) and plain disks. Optionally filtered by stage.
120132
#[command(name = "show-reference-value")]
121133
ShowReferenceValue {
134+
/// Specify one or more hash algorithms to use.
135+
/// Multiple algorithms can be provided (e.g., --hash-algo sha384 --hash-algo sm3).
136+
#[clap(long = "hash-algo", default_value = "sha384,sm3")]
137+
hash_algos: Vec<ShowReferenceValueHashAlgo>,
138+
/// Optional stage filter (e.g., initrd, system). If not provided, all stages are included.
139+
///
140+
/// Used to inject additional reference values for specific boot stages. For example:
141+
/// - When `--stage system` is specified, an extra claim is added:
142+
/// `cryptpilot.alibabacloud.com initrd_switch_root {}`
143+
/// indicating that the system has switched to the new root filesystem (sysroot).
144+
///
145+
/// If omitted, no additional stage-specific claims are generated.
122146
#[clap(long)]
123147
stage: Option<ShowReferenceValueStage>,
124148
},
@@ -128,6 +152,21 @@ pub enum FdeSubcommand {
128152
DumpConfig,
129153
}
130154

155+
#[derive(ValueEnum, Clone, Debug)]
156+
pub enum ShowReferenceValueHashAlgo {
157+
#[clap(name = "sha1")]
158+
Sha1,
159+
160+
#[clap(name = "sha256")]
161+
Sha256,
162+
163+
#[clap(name = "sha384")]
164+
Sha384,
165+
166+
#[clap(name = "sm3")]
167+
Sm3,
168+
}
169+
131170
#[derive(ValueEnum, Clone, Debug)]
132171
pub enum ShowReferenceValueStage {
133172
#[clap(name = "initrd")]

src/cmd/fde/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ impl IntoCommand for crate::cli::FdeOptions {
1010
crate::cli::FdeSubcommand::DumpConfig => {
1111
Box::new(dump_config::ConfigDumpCommand { disk: self.disk })
1212
}
13-
crate::cli::FdeSubcommand::ShowReferenceValue { stage } => {
13+
crate::cli::FdeSubcommand::ShowReferenceValue { stage, hash_algos } => {
1414
Box::new(show_reference_value::ShowReferenceValueCommand {
1515
disk: self.disk,
1616
stage,
17+
hash_algos,
1718
})
1819
}
1920
}

src/cmd/fde/show_reference_value.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::path::PathBuf;
22

3-
use anyhow::Result;
3+
use anyhow::{bail, Result};
44
use async_trait::async_trait;
55
use indexmap::IndexMap;
66

77
use crate::{
8-
cli::ShowReferenceValueStage,
8+
cli::{ShowReferenceValueHashAlgo, ShowReferenceValueStage},
99
cmd::fde::disk::MeasurementedBootComponents,
1010
measure::{
1111
attestation_agent::AAEL_DOMAIN, OPERATION_NAME_FDE_ROOTFS_HASH,
@@ -18,11 +18,16 @@ use super::disk::{FdeDisk, OnCurrentSystemFdeDisk, OnExternalFdeDisk};
1818
pub struct ShowReferenceValueCommand {
1919
pub disk: Option<PathBuf>,
2020
pub stage: Option<ShowReferenceValueStage>,
21+
pub hash_algos: Vec<ShowReferenceValueHashAlgo>,
2122
}
2223

2324
#[async_trait]
2425
impl super::super::Command for ShowReferenceValueCommand {
2526
async fn run(&self) -> Result<()> {
27+
if self.hash_algos.is_empty() {
28+
bail!("No hash algorithm specified");
29+
}
30+
2631
tracing::debug!("Get rootfs reference value");
2732
let mut map = IndexMap::new();
2833

@@ -39,7 +44,10 @@ impl super::super::Command for ShowReferenceValueCommand {
3944
map.insert(aael_key, vec![hash_hex]);
4045
}
4146
Err(error) => {
42-
tracing::warn!(?error, "Failed to load fde config bundle, skip \"{aael_key}\"");
47+
tracing::warn!(
48+
?error,
49+
"Failed to load fde config bundle, skip \"{aael_key}\""
50+
);
4351
}
4452
};
4553
}
@@ -69,8 +77,22 @@ impl super::super::Command for ShowReferenceValueCommand {
6977
let boot_components = fde_disk.get_boot_components().await?;
7078
tracing::debug!("Starting to calculate reference values for boot components");
7179

72-
inseart_with_hash::<sha2::Sha384>(&boot_components, &mut map, "SHA-384")?;
73-
inseart_with_hash::<sm3::Sm3>(&boot_components, &mut map, "SM3")?;
80+
for hash_algo in &self.hash_algos {
81+
match hash_algo {
82+
ShowReferenceValueHashAlgo::Sha1 => {
83+
inseart_with_hash::<sha1::Sha1>(&boot_components, &mut map, "SHA-1")?
84+
}
85+
ShowReferenceValueHashAlgo::Sha256 => {
86+
inseart_with_hash::<sha2::Sha256>(&boot_components, &mut map, "SHA-256")?
87+
}
88+
ShowReferenceValueHashAlgo::Sha384 => {
89+
inseart_with_hash::<sha2::Sha384>(&boot_components, &mut map, "SHA-384")?
90+
}
91+
ShowReferenceValueHashAlgo::Sm3 => {
92+
inseart_with_hash::<sm3::Sm3>(&boot_components, &mut map, "SM3")?
93+
}
94+
}
95+
}
7496

7597
map.insert(
7698
"kernel_cmdline".to_string(),

0 commit comments

Comments
 (0)