Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fwidgen: allow caller to select the FWID digest, add support for sha256, add IANA string id to output #46

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion fwidgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ anyhow.version = "1"
clap = { version = "4.5.32", features = ["derive", "env", "wrap_help"] }
hex = "0.4.3"
hubtools.workspace = true
sha3 = { version = "0.10.8", default-features = false }
sha2 = "0.10.8"
sha3 = "0.10.8"
toml = { version = "0.7.8", default-features = false, features = ["parse"] }
51 changes: 45 additions & 6 deletions fwidgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use anyhow::{anyhow, Context, Result};
use clap::Parser;
use clap::{Parser, ValueEnum};
use hubtools::RawHubrisArchive;
use sha3::{Digest, Sha3_256};
use std::{ops::Range, str};
use sha2::{digest::DynDigest, Digest as _, Sha256};
use sha3::Sha3_256;
use std::{fmt, ops::Range, str};

pub const LPC55_FLASH_PAGE_SIZE: usize = 512;

Expand All @@ -22,11 +23,46 @@ pub const LPC55_FLASH_PAGE_SIZE: usize = 512;
/// of hubris archives
#[derive(Parser, Debug)]
struct Args {
/// Hash algorithm used to generate FWID
#[clap(default_value_t, env = "HUBEDIT_DIGEST", long, value_enum)]
digest: Digest,

/// Hubris archive
#[clap(env = "HUBEDIT_ARCHIVE")]
archive: String,
}

// We provide names explicitly for each variant to map each to the IANA named
// information hash algorithm registry hash name strings.
#[derive(Clone, Debug, Default, ValueEnum)]
enum Digest {
#[clap(name = "sha-256")]
Sha256,
#[clap(name = "sha3-256")]
#[default]
Sha3_256,
}

// Display string names for digest algorithms from IANA named information
// hash algorithm registry
impl fmt::Display for Digest {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Sha256 => write!(f, "sha-256"),
Self::Sha3_256 => write!(f, "sha3-256"),
}
}
}

impl Digest {
fn to_dyn_digest(&self) -> Box<dyn DynDigest> {
match self {
Digest::Sha256 => Box::new(Sha256::new()),
Digest::Sha3_256 => Box::new(Sha3_256::new()),
}
}
}

#[derive(Debug)]
enum Chip {
Lpc55,
Expand Down Expand Up @@ -172,13 +208,16 @@ fn main() -> Result<()> {
}
};

let mut digest = Sha3_256::new();
let mut digest = args.digest.to_dyn_digest();
digest.update(&image);
digest.update(vec![0xff; pad]);
digest.update(vec![0xff; pad].as_ref());

let digest = digest.finalize();

println!("{}", hex::encode(digest));
// Display FWID as the string name for the digest from IANA registry and
// the output from the selected digest encoded as hex & separated by a
// `;`.
println!("{};{}", args.digest, hex::encode(digest));

Ok(())
}
Loading