Repository: chipsalliance/caliptra-sw
Pull request #597 (2023-08-04 21:54:32)

Remove Csrng::drop().
Csrng::assume_initialized() isn't very useful when the Csrng is uninstantiated when it is dropped. Replace drop with an explicit method.


Patch:
diff --git a/drivers/src/csrng.rs b/drivers/src/csrng.rs
index 6ef1a1aa..66eb56a3 100644
--- a/drivers/src/csrng.rs
+++ b/drivers/src/csrng.rs
@@ -160,10 +160,8 @@ impl Csrng {
             specific: e.alert_fail_counts().read(),
         }
     }
-}
 
-impl Drop for Csrng {
-    fn drop(&mut self) {
+    pub fn uninstantiate(mut self) {
         let _ = send_command(&mut self.csrng, Command::Uninstantiate);
     }
 }


Test patch:
diff --git a/drivers/test-fw/Cargo.toml b/drivers/test-fw/Cargo.toml
index 75c7fc1d..9dc6d9af 100644
--- a/drivers/test-fw/Cargo.toml
+++ b/drivers/test-fw/Cargo.toml
@@ -126,6 +126,11 @@ name = "csrng"
 path = "src/bin/csrng_tests.rs"
 required-features = ["riscv"]
 
+[[bin]]
+name = "csrng2"
+path = "src/bin/csrng_tests2.rs"
+required-features = ["riscv"]
+
 [[bin]]
 name = "trng_driver_responder"
 path = "src/bin/trng_driver_responder.rs"
diff --git a/drivers/test-fw/src/bin/csrng_tests2.rs b/drivers/test-fw/src/bin/csrng_tests2.rs
new file mode 100644
index 00000000..6ccf926b
--- /dev/null
+++ b/drivers/test-fw/src/bin/csrng_tests2.rs
@@ -0,0 +1,34 @@
+// Licensed under the Apache-2.0 license
+
+#![no_std]
+#![no_main]
+
+use core::num::NonZeroUsize;
+
+use caliptra_drivers::Csrng;
+use caliptra_registers::{csrng::CsrngReg, entropy_src::EntropySrcReg};
+use caliptra_test_harness::test_suite;
+
+fn test_assume_initialized() {
+    let csrng_reg = unsafe { CsrngReg::new() };
+    let entropy_src_reg = unsafe { EntropySrcReg::new() };
+
+    let mut csrng0 = Csrng::new(csrng_reg, entropy_src_reg).expect("construct CSRNG");
+
+    let one = NonZeroUsize::new(1).unwrap();
+
+    assert_eq!(csrng0.generate(one).unwrap().next().unwrap(), 0x15eb2a44);
+
+    {
+        let mut csrng1 =
+            unsafe { Csrng::assume_initialized(CsrngReg::new(), EntropySrcReg::new()) };
+
+        assert_eq!(csrng1.generate(one).unwrap().next().unwrap(), 0xb5848d3a);
+    }
+
+    assert_eq!(csrng0.generate(one).unwrap().next().unwrap(), 0x22a79509);
+}
+
+test_suite! {
+    test_assume_initialized,
+}
diff --git a/drivers/tests/integration_tests.rs b/drivers/tests/integration_tests.rs
index 7e4d9be0..47592e4a 100644
--- a/drivers/tests/integration_tests.rs
+++ b/drivers/tests/integration_tests.rs
@@ -568,11 +568,18 @@ fn test_negative_lms() {
     run_driver_test("test_negative_lms");
 }
 
-#[test]
-fn test_csrng() {
+fn trng_nibbles() -> impl Iterator<Item = u8> {
+    // reversed form of
     // https://github.com/chipsalliance/caliptra-rtl/blob/fa91d66f30223899403f4e65a6f697a6f9100fd1/src/csrng/tb/csrng_tb.sv#L461
-    const TRNG_ENTROPY: &str = "33F63B65F57AD68765693560E743CC5010518E4BF4ECBEBA71DC56AAA08B394311731D9DF763FC5D27E4ED3E4B7DE947";
+    const TRNG_ENTROPY: &str = "749ED7B4E3DE4E72D5CF367FD9D137113493B80AAA65CD17ABEBCE4FB4E8150105CC347E06539656786DA75F56B36F33";
 
+    TRNG_ENTROPY
+        .chars()
+        .map(|b| b.to_digit(16).expect("bad nibble digit") as u8)
+}
+
+#[test]
+fn test_csrng() {
     let rom = caliptra_builder::build_firmware_rom(&FwId {
         crate_name: "caliptra-drivers-test-bin",
         bin_name: "csrng",
@@ -581,15 +588,33 @@ fn test_csrng() {
     })
     .unwrap();
 
-    let trng_nibbles = TRNG_ENTROPY
-        .chars()
-        .rev()
-        .map(|b| b.to_digit(16).expect("bad nibble digit") as u8);
+    let mut model = caliptra_hw_model::new(BootParams {
+        init_params: InitParams {
+            rom: &rom,
+            itrng_nibbles: Box::new(trng_nibbles()),
+            ..Default::default()
+        },
+        ..Default::default()
+    })
+    .unwrap();
+
+    model.step_until_exit_success().unwrap();
+}
+
+#[test]
+fn test_csrng2() {
+    let rom = caliptra_builder::build_firmware_rom(&FwId {
+        crate_name: "caliptra-drivers-test-bin",
+        bin_name: "csrng2",
+        features: &["emu"],
+        ..Default::default()
+    })
+    .unwrap();
 
     let mut model = caliptra_hw_model::new(BootParams {
         init_params: InitParams {
             rom: &rom,
-            itrng_nibbles: Box::new(trng_nibbles),
+            itrng_nibbles: Box::new(trng_nibbles()),
             ..Default::default()
         },
         ..Default::default()