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----- 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/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..4ade7e4c2 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 { @@ -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();