[dice] Add DiceKdf wrapper to libdiced_open_dice
This function will be used to derived the sealing key for
encrypted storage in Microdroid later.
Bug: 267575445
Test: atest libdiced_open_dice_nostd.integration_test \
libdiced_open_dice.integration_test \
diced_open_dice_cbor_test
Change-Id: I7c2e2eb49c3942a3d0c67f076d311a939da754d4
diff --git a/diced/open_dice/src/lib.rs b/diced/open_dice/src/lib.rs
index 9184a44..0efea61 100644
--- a/diced/open_dice/src/lib.rs
+++ b/diced/open_dice/src/lib.rs
@@ -34,6 +34,6 @@
PRIVATE_KEY_SEED_SIZE,
};
pub use error::{check_result, DiceError, Result};
-pub use ops::hash;
+pub use ops::{hash, kdf};
#[cfg(feature = "std")]
pub use retry::{retry_bcc_format_config_descriptor, retry_bcc_main_flow, OwnedDiceArtifacts};
diff --git a/diced/open_dice/src/ops.rs b/diced/open_dice/src/ops.rs
index ca0e341..0eab116 100644
--- a/diced/open_dice/src/ops.rs
+++ b/diced/open_dice/src/ops.rs
@@ -18,7 +18,7 @@
use crate::dice::{Hash, HASH_SIZE};
use crate::error::{check_result, Result};
-use open_dice_cbor_bindgen::DiceHash;
+use open_dice_cbor_bindgen::{DiceHash, DiceKdf};
use std::ptr;
/// Hashes the provided input using DICE's hash function `DiceHash`.
@@ -36,3 +36,23 @@
})?;
Ok(output)
}
+
+/// An implementation of HKDF-SHA512. Derives a key of `derived_key.len()` bytes from `ikm`, `salt`,
+/// and `info`. The derived key is written to the `derived_key`.
+pub fn kdf(ikm: &[u8], salt: &[u8], info: &[u8], derived_key: &mut [u8]) -> Result<()> {
+ // SAFETY: The function writes to the `derived_key`, within the given bounds, and only reads the
+ // input values. The first argument context is not used in this function.
+ check_result(unsafe {
+ DiceKdf(
+ ptr::null_mut(), // context
+ derived_key.len(),
+ ikm.as_ptr(),
+ ikm.len(),
+ salt.as_ptr(),
+ salt.len(),
+ info.as_ptr(),
+ info.len(),
+ derived_key.as_mut_ptr(),
+ )
+ })
+}