Skip to content

Commit 05a25f4

Browse files
committed
fwidgen: Add sha256 support, allow the caller to select the FWID digest.
1 parent 1ea9af9 commit 05a25f4

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fwidgen/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ anyhow.version = "1"
88
clap = { version = "4.5.32", features = ["derive", "env", "wrap_help"] }
99
hex = "0.4.3"
1010
hubtools.workspace = true
11-
sha3 = { version = "0.10.8", default-features = false }
11+
sha2 = "0.10.8"
12+
sha3 = "0.10.8"
1213
toml = { version = "0.7.8", default-features = false, features = ["parse"] }

fwidgen/src/main.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use anyhow::{anyhow, Context, Result};
6-
use clap::Parser;
6+
use clap::{Parser, ValueEnum};
77
use hubtools::RawHubrisArchive;
8-
use sha3::{Digest, Sha3_256};
8+
use sha2::{digest::DynDigest, Digest as _, Sha256};
9+
use sha3::Sha3_256;
910
use std::{ops::Range, str};
1011

1112
pub const LPC55_FLASH_PAGE_SIZE: usize = 512;
@@ -22,11 +23,35 @@ pub const LPC55_FLASH_PAGE_SIZE: usize = 512;
2223
/// of hubris archives
2324
#[derive(Parser, Debug)]
2425
struct Args {
26+
/// Hash algorithm used to generate FWID
27+
#[clap(default_value_t, env = "HUBEDIT_DIGEST", long, value_enum)]
28+
digest: Digest,
29+
2530
/// Hubris archive
2631
#[clap(env = "HUBEDIT_ARCHIVE")]
2732
archive: String,
2833
}
2934

35+
// We provide names explicitly for each variant to map each to the IANA named
36+
// information hash algorithm registry hash name strings.
37+
#[derive(Clone, Debug, Default, ValueEnum)]
38+
enum Digest {
39+
#[clap(name = "sha-256")]
40+
Sha256,
41+
#[clap(name = "sha3-256")]
42+
#[default]
43+
Sha3_256,
44+
}
45+
46+
impl Digest {
47+
fn to_dyn_digest(&self) -> Box<dyn DynDigest> {
48+
match self {
49+
Digest::Sha256 => Box::new(Sha256::new()),
50+
Digest::Sha3_256 => Box::new(Sha3_256::new()),
51+
}
52+
}
53+
}
54+
3055
#[derive(Debug)]
3156
enum Chip {
3257
Lpc55,
@@ -172,9 +197,9 @@ fn main() -> Result<()> {
172197
}
173198
};
174199

175-
let mut digest = Sha3_256::new();
200+
let mut digest = args.digest.to_dyn_digest();
176201
digest.update(&image);
177-
digest.update(vec![0xff; pad]);
202+
digest.update(vec![0xff; pad].as_ref());
178203

179204
let digest = digest.finalize();
180205

0 commit comments

Comments
 (0)