Repository: chipsalliance/caliptra-sw (rust)

Issue:
Test failure: warm_reset_basic
This test is failing on both FPGA and verilator models:

```
cargo test -p caliptra-test --release --features=fpga_realtime warm_reset_basic -- --nocapture
test warm_reset_basic ...           0 new_unbooted
          1 ready_for_fuses is high
          1 writing to cptra_bootfsm_go
         10 UART: 
         10 UART: Running Caliptra ROM ...
         11 UART: 
         11 UART: [state] CFI Enabled
         11 UART: [state] LifecycleState = Production
         11 UART: [state] DebugLocked = Yes
         11 UART: [state] Starting the Watchdog Timer
         18 UART: ROM Digest: B2420B162026FE80BFF68735DC311124219ACA4279F96FC13507B26B7699A06D
         19 UART: [kat] ++
         19 UART: [kat] sha1
         19 UART: [kat] SHA2-256
         19 UART: [kat] SHA2-384
         20 UART: [kat] SHA2-384-ACC
         20 UART: [kat] ECC-384
        134 UART: [kat] HMAC-384
        134 UART: [kat] LMS
        186 UART: [kat] --
        186 UART: [cold-reset] ++
        186 UART: [fht] Storing FHT @ 0x50003400
        187 UART: [idev] ++
        187 UART: [idev] CDI.KEYID = 6
        187 UART: [idev] SUBJECT.KEYID = 7
        187 UART: [idev] UDS.KEYID = 0
        188 UART: [idev] Erasing UDS.KEYID = 0
        351 UART: [idev] Using Sha1 for KeyId Algorithm
        352 UART: [idev] --
        352 UART: [ldev] ++
        352 UART: [ldev] CDI.KEYID = 6
        353 UART: [ldev] SUBJECT.KEYID = 5
        353 UART: [ldev] AUTHORITY.KEYID = 7
        353 UART: [ldev] FE.KEYID = 1
        353 UART: [ldev] Erasing FE.KEYID = 1
        517 UART: [ldev] Signing Cert with AUTHORITY.KEYID = 7
        632 UART: [ldev] PUB.X = 842C00AF05ACCCEB14514E2D37B0C3AAA218F15057F1DCB824A214980B744688A0888A0297FA7DC5E1EAD8CA1291DB22
        633 UART: [ldev] PUB.Y = 9C28EB8678BCE800822C07228F416AE49D218E5DA2F2D1A8A27DC19ADF668A74628999D222B40159D8076FAFBB8C5EDB
        634 UART: [ldev] SIG.R = D59DFC464DBF6A859A598FACE430319C0456AD3784693D039EA59877710BAAB4D1105C5BC6AB244CCBD05EFFA316C786
        634 UART: [ldev] SIG.S = 28889D96A9967976EB3B5B5773601DB6F09B18C8456298C04C2C853CA55A78D3D84E15465CAE31763536B45DD94768F4
        635 UART: [ldev] --
        636 ready_for_fw is high
        636 <<< Executing mbox cmd 0x46574c44 (67156 bytes) from SoC
        640 UART: [fwproc] Waiting for Commands...
        640 UART: [fwproc] Received command 0x46574c44
        640 UART: [fwproc] Received Image of size 67156 bytes
        812 UART: [fwproc] Image verified using Vendor ECC Key Index 0
        816 UART: [fwproc] Loading FMC at address 0x40000000 len 12688
        820 UART: [fwproc] Loading Runtime at address 0x40004000 len 48584
        835 >>> mbox cmd response: success
        835 UART: [afmc] ++
        835 UART: [afmc] CDI.KEYID = 6
        835 UART: [afmc] SUBJECT.KEYID = 7
        835 UART: [afmc] AUTHORITY.KEYID = 5
      1,001 UART: [afmc] Signing Cert with AUTHORITY.KEYID = 5
      1,115 UART: [afmc] Erasing AUTHORITY.KEYID = 5
      1,115 UART: [afmc] PUB.X = 0E99F941C037B07D814B5B7BFBE087E99589D2E148C74BDE9F3322E0B375688874ED0A13BCD958564377973671DF5481
      1,116 UART: [afmc] PUB.Y = 52F5064E7D6183C0835098CE18E45933C92F86557394B7DD761F42B0892403567EF13042EFB22180701A5A9AD5EB8CEE
      1,117 UART: [afmc] SIG.R = 47A7CB35E14C7DF5EEF10F7FDD6BC6EE7438EBD76D834FE004FD54A8E635A180CBF4ABA6C8D9D20F2E013B86C48A17DC
      1,118 UART: [afmc] SIG.S = 5AC72C46CE4887125D2DF7B713DE09E6E7C30CABEEEE162070B1853FE1C4C5CFCE4F3B88B08B69281575F6F0C7DB900D
      1,119 UART: [afmc] --
      1,119 UART: [cold-reset] --
      1,120 UART: [state] Stopping the Watchdog Timer
      1,120 UART: [state] Locking Datavault
      1,120 UART: [state] Locking PCR0, PCR1 and PCR31
      1,120 UART: [state] Locking ICCM
      1,120 UART: [exit] Launching FMC @ 0x40000134
      1,121 UART: 
      1,121 UART: Running Caliptra FMC ...
      1,121 UART: 
      1,121 UART: [alias rt] Extend RT PCRs
      1,123 UART: [alias rt] Extend RT PCRs Done
      1,123 UART: [alias rt] Lock RT PCRs
      1,123 UART: [alias rt] Lock RT PCRs Done
      1,123 UART: [alias rt] Populate DV
      1,123 UART: [alias rt] Populate DV Done
      1,123 UART: Handoff : FMC CDI: 6
      1,123 UART: FMC Alias Private Key: 7
      1,123 UART: [alias rt] Derive CDI
      1,123 UART: [alias rt] Store in in slot 0x4
      1,125 UART: [alias rt] Derive Key Pair
      1,125 UART: [alias rt] Store priv key in slot 0x5
      1,287 UART: [alias rt] Derive Key Pair - Done
      1,288 UART: [alias rt] Signing Cert with AUTHO
      1,288 UART:             RITY.KEYID = 7
      1,402 UART: [alias rt] Erasing AUTHORITY.KEYID = 7
      1,402 UART: [alias rt] PUB.X = CB81F609B98074BCAD4702CE0A1641DEC352CDC7D02029A80999967368D44024116734C097EEF7B48A9EE7CEEFFC7B51
      1,402 UART: [alias rt] PUB.Y = 25BBADB86E0EDB97EA7A2C3A28974F7802F8D8A93E5A70874B22B16A8DB21CFAB6AA348EE3304281B93497D3CA44A6D3
      1,403 UART: [alias rt] SIG.R = F6C2DE4D5FC2D56B0452ADD6E6887961F60FC57C93D831E637CE2B03027D947B4F731A4995E71028EDED53678D507C96
      1,403 UART: [alias rt] SIG.S = E546EA80E90A834DBEE4F867FF6A8ADCBC52FA6C2BF1CE753A5DB9B90BCA46ECEF548F6583BDCA9F8C50BB333FEE8EE0
      1,468 UART: 
      1,468 UART:   ____      _ _       _               ____ _____
      1,468 UART:  / ___|__ _| (_)_ __ | |_ _ __ __ _  |  _ \_   _|
      1,468 UART: | |   / _` | | | '_ \| __| '__/ _` | | |_) || |
      1,468 UART: | |__| (_| | | | |_) | |_| | | (_| | |  _ < | |
      1,468 UART:  \____\__,_|_|_| .__/ \__|_|  \__,_| |_| \_\|_|
      1,469 UART:                |_|
      1,469 UART: 
      1,473 UART: Caliptra RT listening for mailbox commands...
      1,473 UART: 
      1,473 UART: Running Caliptra ROM ...
      1,473 UART: 
      1,473 UART: [state] CFI Enabled
      1,473 UART: [state] LifecycleState = Production
      1,473 UART: [state] DebugLocked = Yes
      1,473 UART: [state] Starting the Watchdog Timer
      1,481 UART: ROM Digest: B2420B162026FE80BFF68735DC311124219ACA4279F96FC13507B26B7699A06D
      1,481 UART: [kat] ++
      1,481 UART: [kat] sha1
      1,482 UART: [kat] SHA2-256
      1,482 UART: [kat] SHA2-384
      1,482 UART: [kat] SHA2-384-ACC
      1,482 UART: [kat] ECC-384
      1,596 UART: [kat] HMAC-384
      1,597 UART: [kat] LMS
      1,649 UART: [kat] --
      1,649 UART: [warm-reset] ++
      1,649 UART: [warm-reset] --
      1,649 UART: [state] Stopping the Watchdog Timer
      1,649 UART: [state] Locking Datavault
      1,649 UART: [state] Locking PCR0, PCR1 and PCR31
      1,649 UART: [state] Locking ICCM
      1,650 UART: [exit] Launching FMC @ 0x40000134
      1,650 UART: 
      1,650 UART: Running Caliptra FMC ...
      1,650 UART: 
      1,650 UART: [alias rt] Extend RT PCRs
      1,650 UART: [alias rt : skip pcr extension
      1,650 UART: [alias rt] Extend RT PCRs Done
      1,650 UART: [alias rt] Lock RT PCRs
      1,650 UART: [alias rt] Lock RT PCRs Done
      1,650 UART: [alias rt] Populate DV
      1,650 UART: [alias rt] Populate DV Done
      1,651 UART: Handoff : FMC CDI: 6
      1,651 UART: FMC Alias Private Key: 7
      1,651 UART: [alias rt] Derive CDI
      1,651 UART: [alias rt] Store in in slot 0x4
      1,653 UART: [alias rt] Derive Key Pair
      1,653 UART: [alias rt] Store priv key in slot 0x5
      1,815 UART: [alias rt] Derive Key Pair - Done
      1,815 UART: [alias rt] Signing Cert with AUTHO
      1,815 UART:             RITY.KEYID = 7
      1,929 UART: [alias rt] Erasing AUTHORITY.KEYID = 7
      1,929 UART: [alias rt] PUB.X = CB81F609B98074BCAD4702CE0A1641DEC352CDC7D02029A80999967368D44024116734C097EEF7B48A9EE7CEEFFC7B51
      1,930 UART: [alias rt] PUB.Y = 25BBADB86E0EDB97EA7A2C3A28974F7802F8D8A93E5A70874B22B16A8DB21CFAB6AA348EE3304281B93497D3CA44A6D3
      1,930 UART: [alias rt] SIG.R = F6C2DE4D5FC2D56B0452ADD6E6887961F60FC57C93D831E637CE2B03027D947B4F731A4995E71028EDED53678D507C96
      1,930 UART: [alias rt] SIG.S = E546EA80E90A834DBEE4F867FF6A8ADCBC52FA6C2BF1CE753A5DB9B90BCA46ECEF548F6583BDCA9F8C50BB333FEE8EE0
      1,996 UART: 
      1,996 UART:   ____      _ _       _               ____ _____
      1,996 UART:  / ___|__ _| (_)_ __ | |_ _ __ __ _  |  _ \_   _|
      1,996 UART: | |   / _` | | | '_ \| __| '__/ _` | | |_) || |
      1,996 UART: | |__| (_| | | | |_) | |_| | | (_| | |  _ < | |
      1,996 UART:  \____\__,_|_|_| .__/ \__|_|  \__,_| |_| \_\|_|
      1,996 UART:                |_|
      1,996 UART: 
      1,998 UART: 262145
      1,998 UART: Handoff Error: Runtime can't load drivers 0x000E000C
```

As best as I can tell, the runtime firmware is trying to disable attestation at https://github.com/chipsalliance/caliptra-sw/blob/da0cf516fdcea1b6f388567927f33e9f0c9f65da/runtime/src/drivers.rs#L216-L218, but that is failing with error code DRIVER_HMAC384_READ_KEY_KV_READ for key id KEY_ID_RT_CDI. I'm guessing this is a bug because KEY_ID_RT_CDI was just erased at https://github.com/chipsalliance/caliptra-sw/blob/da0cf516fdcea1b6f388567927f33e9f0c9f65da/runtime/src/disable.rs#L13-L16, so why is the code trying to use it?


Pull request description:
Reinitialize RT CDI to an HMAC of a buffer of 0s in disable attestation
This is necessary so that we can use the RT CDI to generate a DICE key. We cannot simply erase a key vault slot and then reuse it.

fixes #932 

Patch:
diff --git a/builder/src/firmware.rs b/builder/src/firmware.rs
index 4574a9b1..1dc6b129 100644
--- a/builder/src/firmware.rs
+++ b/builder/src/firmware.rs
@@ -313,11 +313,6 @@ pub mod runtime_tests {
         ..RUNTIME_TEST_FWID_BASE
     };
 
-    pub const KEYVAULT: FwId = FwId {
-        bin_name: "keyvault",
-        ..RUNTIME_TEST_FWID_BASE
-    };
-
     pub const LOCKED_DV: FwId = FwId {
         bin_name: "locked_dv",
         ..RUNTIME_TEST_FWID_BASE
@@ -385,7 +380,6 @@ pub const REGISTERED_FW: &[&FwId] = &[
     &fmc_tests::MOCK_RT_WITH_UART,
     &fmc_tests::MOCK_RT_INTERACTIVE,
     &runtime_tests::BOOT,
-    &runtime_tests::KEYVAULT,
     &runtime_tests::LOCKED_DV,
     &runtime_tests::CERT,
     &runtime_tests::WDT,
diff --git a/runtime/src/disable.rs b/runtime/src/disable.rs
index 4dd11635..16e7ea0b 100644
--- a/runtime/src/disable.rs
+++ b/runtime/src/disable.rs
@@ -4,7 +4,8 @@ use crate::Drivers;
 use caliptra_common::keyids::{KEY_ID_RT_CDI, KEY_ID_RT_PRIV_KEY};
 use caliptra_common::mailbox_api::MailboxResp;
 use caliptra_drivers::{
-    hmac384_kdf, CaliptraError, CaliptraResult, KeyReadArgs, KeyUsage, KeyWriteArgs,
+    hmac384_kdf, Array4x12, CaliptraError, CaliptraResult, Hmac384Key, KeyReadArgs, KeyUsage,
+    KeyWriteArgs,
 };
 
 pub struct DisableAttestationCmd;
@@ -19,11 +20,32 @@ impl DisableAttestationCmd {
             .erase_key(KEY_ID_RT_PRIV_KEY)
             .map_err(|_| CaliptraError::RUNTIME_DISABLE_ATTESTATION_FAILED)?;
 
+        Self::zero_rt_cdi(drivers)?;
         Self::generate_dice_key(drivers)?;
         drivers.attestation_disabled = true;
         Ok(MailboxResp::default())
     }
 
+    // Set CDI key vault slot to an HMAC of a buffer of 0s.
+    fn zero_rt_cdi(drivers: &mut Drivers) -> CaliptraResult<()> {
+        hmac384_kdf(
+            &mut drivers.hmac384,
+            Hmac384Key::Array4x12(&Array4x12::default()),
+            b"zero_rt_cdi",
+            None,
+            &mut drivers.trng,
+            KeyWriteArgs::new(
+                KEY_ID_RT_CDI,
+                KeyUsage::default()
+                    .set_hmac_key_en()
+                    .set_ecc_key_gen_seed_en(),
+            )
+            .into(),
+        )?;
+
+        Ok(())
+    }
+
     // Dice key is derived from an empty CDI slot so it will not match the key that was certified in the rt_alias cert.
     fn generate_dice_key(drivers: &mut Drivers) -> CaliptraResult<()> {
         hmac384_kdf(


Test patch:
diff --git a/runtime/test-fw/Cargo.toml b/runtime/test-fw/Cargo.toml
index 1e341f71..dcf18960 100644
--- a/runtime/test-fw/Cargo.toml
+++ b/runtime/test-fw/Cargo.toml
@@ -33,11 +33,6 @@ name = "wdt"
 path = "src/wdt_timeout_tests.rs"
 required-features = ["riscv"]
 
-[[bin]]
-name = "keyvault"
-path = "src/keyvault_tests.rs"
-required-features = ["riscv"]
-
 [[bin]]
 name = "locked_dv"
 path = "src/locked_dv_test.rs"
diff --git a/runtime/test-fw/src/keyvault_tests.rs b/runtime/test-fw/src/keyvault_tests.rs
deleted file mode 100644
index 1cd7918f..00000000
--- a/runtime/test-fw/src/keyvault_tests.rs
+++ /dev/null
@@ -1,58 +0,0 @@
-/*++
-
-Licensed under the Apache-2.0 license.
-
-File Name:
-
-    keyvault_tests.rs
-
-Abstract:
-
-    File contains test cases for booting runtime firmware
-
---*/
-
-#![no_std]
-#![no_main]
-
-use caliptra_common::keyids::{KEY_ID_RT_CDI, KEY_ID_RT_PRIV_KEY};
-use caliptra_drivers::{hmac384_kdf, Hmac384, KeyReadArgs, KeyUsage, KeyVault, KeyWriteArgs, Trng};
-use caliptra_registers::{
-    csrng::CsrngReg, entropy_src::EntropySrcReg, hmac::HmacReg, kv::KvReg, soc_ifc::SocIfcReg,
-    soc_ifc_trng::SocIfcTrngReg,
-};
-use caliptra_test_harness::{runtime_handlers, test_suite};
-
-fn test_derive_ecc_key_from_cdi_in_erased_kv_slot() {
-    let mut vault = unsafe { KeyVault::new(KvReg::new()) };
-    let mut hmac384 = unsafe { Hmac384::new(HmacReg::new()) };
-    let mut trng = unsafe {
-        Trng::new(
-            CsrngReg::new(),
-            EntropySrcReg::new(),
-            SocIfcTrngReg::new(),
-            &SocIfcReg::new(),
-        )
-        .unwrap()
-    };
-    vault.erase_key(KEY_ID_RT_CDI).unwrap();
-    hmac384_kdf(
-        &mut hmac384,
-        KeyReadArgs::new(KEY_ID_RT_CDI).into(),
-        b"dice_keygen",
-        None,
-        &mut trng,
-        KeyWriteArgs::new(
-            KEY_ID_RT_PRIV_KEY,
-            KeyUsage::default()
-                .set_hmac_key_en()
-                .set_ecc_key_gen_seed_en(),
-        )
-        .into(),
-    )
-    .unwrap();
-}
-
-test_suite! {
-    test_derive_ecc_key_from_cdi_in_erased_kv_slot,
-}
diff --git a/runtime/tests/integration_tests.rs b/runtime/tests/integration_tests.rs
index aba4b5e0..586acadd 100644
--- a/runtime/tests/integration_tests.rs
+++ b/runtime/tests/integration_tests.rs
@@ -94,13 +94,6 @@ fn test_boot() {
     model.step_until_exit_success().unwrap();
 }
 
-#[test]
-fn test_keyvault() {
-    let mut model = run_rt_test(Some(&firmware::runtime_tests::KEYVAULT), None);
-
-    model.step_until_exit_success().unwrap();
-}
-
 #[test]
 fn test_locked_dv_slot() {
     let mut model = run_rt_test(Some(&firmware::runtime_tests::LOCKED_DV), None);