Merge pull request #805 from betrusted-io/antirollback-ci

Add ability to simulate third party images
This commit is contained in:
bunnie 2026-02-04 03:16:04 +08:00 committed by GitHub
commit 0ae03c6354
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 10 deletions

3
devkey/testing.key Normal file
View file

@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIKindlyNoteThisIsTestKeyDontUseForProduction
-----END PRIVATE KEY-----

View file

@ -22,7 +22,7 @@ pem = "0.8.3"
svd2utra = "0.1.25"
xmas-elf = "0.9.0"
xous-semver = "0.1.5"
ed25519-dalek = { version = "2.1.0", features = ["digest"] }
ed25519-dalek = { version = "2.1.0", features = ["digest", "rand_core"] }
sha2 = { version = "0.10.8" }
pkcs8 = { version = "0.8.0", features = ["pem"] }
base64 = "0.20.0"

View file

@ -19,6 +19,7 @@ use xous_tools::utils::parse_u32;
use xous_tools::xous_arguments::XousArgumentCode;
use xous_tools::xous_arguments::XousArguments;
// this must match exactly what's in devkey/dev.key
const DEV_KEY_PEM: &'static str = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIKindlyNoteThisIsADevKeyDontUseForProduction\n-----END PRIVATE KEY-----";
fn main() -> Result<(), Box<dyn std::error::Error>> {

View file

@ -13,6 +13,9 @@ use rand::RngCore;
use ring::signature::{Ed25519KeyPair, KeyPair};
use sha2::{Digest, Sha512};
// this must match exactly what's in devkey/testing.key
const TEST_KEY_PEM: &'static str = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIKindlyNoteThisIsTestKeyDontUseForProduction\n-----END PRIVATE KEY-----";
#[repr(u32)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum Version {
@ -201,13 +204,6 @@ pub fn sign_image(
// Now we can use the private key data.
let signing_key = SigningKey::from_bytes(&secbytes);
// This is handy code to remember - a quick way to get the public key from the private key. Just
// in case I need this in the future to sanity check some values.
/*
let sk = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&private_key.contents)
.map_err(|e| format!("{}", e))?;
let pubkey = sk.public_key();
*/
let anti_rollback = if let Some(code) = anti_rollback_manual {
code as u32
} else {
@ -246,13 +242,32 @@ pub fn sign_image(
dst.populate_from(src);
}
} else {
// populate with fake, random data. The data changes every time.
// populate with a random key. Use actual key generation to ensure that the committed
// public key is actually a valid public key.
for dst in header.sealed_data.pubkeys.iter_mut() {
use ed25519_dalek::VerifyingKey;
use rand::rngs::OsRng;
let mut fake_pk = Pubkey::default();
rand::rngs::OsRng.fill_bytes(&mut fake_pk.pk);
// Generate a random signing (private) key
let signing_key = SigningKey::generate(&mut OsRng);
// Derive the verifying (public) key
let verifying_key: VerifyingKey = signing_key.verifying_key();
let public_bytes = verifying_key.to_bytes();
fake_pk.pk.copy_from_slice(&public_bytes);
rand::rngs::OsRng.fill_bytes(&mut fake_pk.tag);
dst.populate_from(&fake_pk);
}
let testing_key = pem::parse(TEST_KEY_PEM)?;
// derive the public key for use in "fake keys" routines
let sk = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&testing_key.contents)
.map_err(|e| format!("{}", e))?;
let derived_public_key = sk.public_key();
// replace devkey slot with the derived public key of the fake signer
header.sealed_data.pubkeys[3].pk.copy_from_slice(derived_public_key.as_ref());
}
let mut protected = Vec::new();