Repository: chipsalliance/caliptra-sw
Pull request #380 (2023-06-13 20:08:42)

Provide a builder pattern for KeyUsage.
Allows for more ergonomic call-sites.


Patch:
diff --git a/drivers/src/key_vault.rs b/drivers/src/key_vault.rs
index 23376a7c..77649714 100644
--- a/drivers/src/key_vault.rs
+++ b/drivers/src/key_vault.rs
@@ -141,6 +141,33 @@ bitfield! {
     pub ecc_data, set_ecc_data:5;
 }
 
+impl KeyUsage {
+    pub fn set_hmac_key_en(&mut self) -> KeyUsage {
+        self.set_hmac_key(true);
+        *self
+    }
+    pub fn set_hmac_data_en(&mut self) -> KeyUsage {
+        self.set_hmac_data(true);
+        *self
+    }
+    pub fn set_sha_data_en(&mut self) -> KeyUsage {
+        self.set_sha_data(true);
+        *self
+    }
+    pub fn set_ecc_private_key_en(&mut self) -> KeyUsage {
+        self.set_ecc_private_key(true);
+        *self
+    }
+    pub fn set_ecc_key_gen_seed_en(&mut self) -> KeyUsage {
+        self.set_ecc_key_gen_seed(true);
+        *self
+    }
+    pub fn set_ecc_data_en(&mut self) -> KeyUsage {
+        self.set_ecc_data(true);
+        *self
+    }
+}
+
 /// Caliptra Key Vault
 pub struct KeyVault {
     kv: KvReg,
diff --git a/fmc/src/flow/crypto.rs b/fmc/src/flow/crypto.rs
index ca5d7daa..bf837403 100644
--- a/fmc/src/flow/crypto.rs
+++ b/fmc/src/flow/crypto.rs
@@ -78,10 +78,12 @@ impl Crypto {
         tag: KeyId,
     ) -> CaliptraResult<KeyId> {
         // Tag
-        let mut usage = KeyUsage::default();
-        usage.set_hmac_key(true);
-        usage.set_ecc_key_gen_seed(true);
-        let tag_args = Hmac384Tag::Key(KeyWriteArgs::new(tag, usage));
+        let tag_args = Hmac384Tag::Key(KeyWriteArgs::new(
+            tag,
+            KeyUsage::default()
+                .set_hmac_key_en()
+                .set_ecc_key_gen_seed_en(),
+        ));
 
         // Calculate the CDI
         env.hmac384.hmac(key, data, tag_args)?;
@@ -107,10 +109,10 @@ impl Crypto {
     ) -> CaliptraResult<Ecc384KeyPair> {
         let seed = Ecc384Seed::Key(KeyReadArgs::new(seed));
 
-        let mut usage = KeyUsage::default();
-        usage.set_ecc_private_key(true);
-
-        let key_out = Ecc384PrivKeyOut::Key(KeyWriteArgs::new(priv_key, usage));
+        let key_out = Ecc384PrivKeyOut::Key(KeyWriteArgs::new(
+            priv_key,
+            KeyUsage::default().set_ecc_private_key_en(),
+        ));
 
         Ok(Ecc384KeyPair {
             priv_key,
diff --git a/rom/dev/src/flow/cold_reset/crypto.rs b/rom/dev/src/flow/cold_reset/crypto.rs
index e61aeeee..d67f9cbe 100644
--- a/rom/dev/src/flow/cold_reset/crypto.rs
+++ b/rom/dev/src/flow/cold_reset/crypto.rs
@@ -109,10 +109,12 @@ impl Crypto {
         tag: KeyId,
     ) -> CaliptraResult<KeyId> {
         // Tag
-        let mut usage = KeyUsage::default();
-        usage.set_hmac_key(true);
-        usage.set_ecc_key_gen_seed(true);
-        let tag_args = Hmac384Tag::Key(KeyWriteArgs::new(tag, usage));
+        let tag_args = Hmac384Tag::Key(KeyWriteArgs::new(
+            tag,
+            KeyUsage::default()
+                .set_hmac_key_en()
+                .set_ecc_key_gen_seed_en(),
+        ));
 
         // Calculate the CDI
         env.hmac384.hmac(key, data, tag_args)?;
@@ -139,10 +141,10 @@ impl Crypto {
         // [TODO] Add Nonce to the ecc384_key_gen function
         let seed = Ecc384Seed::Key(KeyReadArgs::new(seed));
 
-        let mut usage = KeyUsage::default();
-        usage.set_ecc_private_key(true);
-
-        let key_out = Ecc384PrivKeyOut::Key(KeyWriteArgs::new(priv_key, usage));
+        let key_out = Ecc384PrivKeyOut::Key(KeyWriteArgs::new(
+            priv_key,
+            KeyUsage::default().set_ecc_private_key_en(),
+        ));
 
         Ok(Ecc384KeyPair {
             priv_key,


Test patch:
diff --git a/drivers/test-fw/src/bin/ecc384_tests.rs b/drivers/test-fw/src/bin/ecc384_tests.rs
index 1795315c..52a8ca31 100644
--- a/drivers/test-fw/src/bin/ecc384_tests.rs
+++ b/drivers/test-fw/src/bin/ecc384_tests.rs
@@ -125,11 +125,9 @@ fn test_kv_seed_from_input_msg_from_input() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_ecc_private_key(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId2,
-        usage: key_usage, // ecc_private_key
+        usage: KeyUsage::default().set_ecc_private_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -179,11 +177,9 @@ fn test_kv_seed_from_kv_msg_from_input() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_ecc_key_gen_seed(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId0,
-        usage: key_usage,
+        usage: KeyUsage::default().set_ecc_key_gen_seed_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -218,11 +214,9 @@ fn test_kv_seed_from_kv_msg_from_input() {
         0x67, 0x1d, 0x8f,
     ];
     let key_in_seed = KeyReadArgs::new(KeyId::KeyId0);
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_ecc_private_key(true);
     let key_out_priv_key = KeyWriteArgs {
         id: KeyId::KeyId1,
-        usage: key_usage,
+        usage: KeyUsage::default().set_ecc_private_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(key_in_seed),
diff --git a/drivers/test-fw/src/bin/hmac384_tests.rs b/drivers/test-fw/src/bin/hmac384_tests.rs
index 18119e92..a2c272d0 100755
--- a/drivers/test-fw/src/bin/hmac384_tests.rs
+++ b/drivers/test-fw/src/bin/hmac384_tests.rs
@@ -94,11 +94,9 @@ fn test_hmac3() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_hmac_key(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId0,
-        usage: key_usage, // hmac_key
+        usage: KeyUsage::default().set_hmac_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -138,11 +136,9 @@ fn test_hmac4() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_hmac_key(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId0,
-        usage: key_usage, // hmac_key
+        usage: KeyUsage::default().set_hmac_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -187,11 +183,9 @@ fn test_hmac5() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_hmac_key(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId0,
-        usage: key_usage, // hmac_key
+        usage: KeyUsage::default().set_hmac_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -254,11 +248,9 @@ fn test_hmac6() {
     //
     let seed = [0u8; 48];
     let nonce = Array4xN::default();
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_hmac_key(true);
     let key_out_1 = KeyWriteArgs {
         id: KeyId::KeyId0,
-        usage: key_usage, // hmac_key
+        usage: KeyUsage::default().set_hmac_key_en(),
     };
     let result = ecc.key_pair(
         Ecc384Seed::from(&Ecc384Scalar::from(seed)),
@@ -302,9 +294,7 @@ fn test_hmac6() {
     assert_eq!(hmac_step_1, Array4x12::from(step_1_result_expected));
 
     // Generate the Tag Of Original Data and put the tag In KV @5.  KV @5 will be used as data in the next step
-    let mut key_usage = KeyUsage::default();
-    key_usage.set_hmac_data(true);
-    let out_tag = KeyWriteArgs::new(KeyId::KeyId5, key_usage);
+    let out_tag = KeyWriteArgs::new(KeyId::KeyId5, KeyUsage::default().set_hmac_data_en());
     let actual = hmac384.hmac(key.into(), (&data).into(), out_tag.into());
     assert!(actual.is_ok());