-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! SFT-3538: Add foundation-firmware crate.
- Loading branch information
Showing
4 changed files
with
212 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use anyhow::{anyhow, bail, Context, Result}; | ||
use bitcoin_hashes::{sha256d, Hash, HashEngine}; | ||
use nom::Finish; | ||
use secp256k1::global::SECP256K1; | ||
use std::{fs, io::Read}; | ||
|
||
fn main() -> Result<()> { | ||
let file_name = std::env::args_os() | ||
.nth(1) | ||
.ok_or_else(|| anyhow!("Please provide a file name."))?; | ||
|
||
let mut file = fs::File::open(file_name).context("Failed to open firmware.")?; | ||
|
||
let header_len = usize::try_from(foundation_firmware::HEADER_LEN).unwrap(); | ||
let mut header_buf = vec![0; header_len]; | ||
|
||
file.read(&mut header_buf) | ||
.context("Failed to read firmware header.")?; | ||
|
||
let header = match foundation_firmware::header(&header_buf).finish() { | ||
Ok((_, hdr)) => hdr, | ||
Err(_) => bail!("Failed to parse firmware header."), | ||
}; | ||
|
||
header.verify().context("Header verification failed.")?; | ||
|
||
println!("Firmware:"); | ||
println!("- Magic: {:#08X}", header.information.magic); | ||
println!("- Timestamp: {}", header.information.timestamp); | ||
println!("- Date: {}", header.information.date); | ||
println!("- Version: {}", header.information.version); | ||
println!("- Length: {} bytes", header.information.length); | ||
println!(); | ||
|
||
let firmware_len = | ||
usize::try_from(header.information.length - foundation_firmware::HEADER_LEN).unwrap(); | ||
let mut firmware_buf = vec![0; firmware_len]; | ||
file.read(&mut firmware_buf) | ||
.context("Failed to read firmware contents.")?; | ||
|
||
let mut engine = sha256d::Hash::engine(); | ||
engine.input(&header.information.serialize()); | ||
engine.input(&firmware_buf); | ||
let hash = sha256d::Hash::from_engine(engine); | ||
|
||
foundation_firmware::verify_signature(&SECP256K1, &header, &hash, None) | ||
.context("Firmware signature verification failed.")?; | ||
|
||
println!("Firmware signature is valid!"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters