From f0fb76dca185086bc7e043affa8e0742cf22d39c Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 4 Feb 2026 02:50:08 +0800 Subject: [PATCH 1/4] add ability to generate and install testing keys for third party images --- tools/src/bin/xous-app-uf2.rs | 1 + tools/src/sign_image.rs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/src/bin/xous-app-uf2.rs b/tools/src/bin/xous-app-uf2.rs index bf5f5392f..82dd7a4c3 100644 --- a/tools/src/bin/xous-app-uf2.rs +++ b/tools/src/bin/xous-app-uf2.rs @@ -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> { diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index fb7fe48c1..aeb0b846c 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -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 { @@ -253,6 +249,15 @@ pub fn sign_image( 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 first random data with the derived public key of the fake signer + header.sealed_data.pubkeys[0].pk.copy_from_slice(derived_public_key.as_ref()); } let mut protected = Vec::new(); From 0caf0bb2d79bdc346f213c10161e5e57a73d3d7d Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 4 Feb 2026 02:50:24 +0800 Subject: [PATCH 2/4] add a testing key for third party images just some fake base64 data to make a unique key that we can use to "sign" third party images --- devkey/testing.key | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 devkey/testing.key diff --git a/devkey/testing.key b/devkey/testing.key new file mode 100644 index 000000000..f4330038e --- /dev/null +++ b/devkey/testing.key @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEIKindlyNoteThisIsTestKeyDontUseForProduction +-----END PRIVATE KEY----- From cf267f096c8047de2f15ccbb0fb42ad212aa1032 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 4 Feb 2026 03:07:38 +0800 Subject: [PATCH 3/4] generate actual private keys instead of using random data by doing this we make this safer by ensuring that the public keys are valid public keys, allowing us to sign these images with production keys and not accidentally signing a weak public key. --- tools/Cargo.toml | 2 +- tools/src/sign_image.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/Cargo.toml b/tools/Cargo.toml index edf5c3f59..a77214a58 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -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" diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index aeb0b846c..42ea01231 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -242,10 +242,20 @@ 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); } From f17b67c16df16e1a57a412ed67389bfe178aad16 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 4 Feb 2026 03:14:29 +0800 Subject: [PATCH 4/4] put the test key into the devkey slot i feel a little more comfortable with this because there are checks that *any* key in that slot would be treated as a devkey and would lead to key erasure. so just in case i messed up something in the other checks, this would trigger a key erasure. I think there is no way around signing at least a beta image containing the third party keys if i truly want to check all the paths, so trying to isolate what it can do is important. --- tools/src/sign_image.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index 42ea01231..4ade7e4c2 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -266,8 +266,8 @@ pub fn sign_image( .map_err(|e| format!("{}", e))?; let derived_public_key = sk.public_key(); - // replace first random data with the derived public key of the fake signer - header.sealed_data.pubkeys[0].pk.copy_from_slice(derived_public_key.as_ref()); + // 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();