[dice] Move format_condig_descriptor to diced_open_dice
Test: atest diced_utils_test diced_sample_inputs_test \
diced_test diced_vendor_test diced_open_dice_cbor_test \
diced_client_test
Test: m pvmfw_img microdroid_manager
Test: atest microdroid_manager_test vmbase_example.integration_test
Bug: 267575445
Change-Id: Iba7f775c1d1a3070c119da546edad38c2bbf67ac
diff --git a/diced/open_dice/Android.bp b/diced/open_dice/Android.bp
index f4c2155..e581085 100644
--- a/diced/open_dice/Android.bp
+++ b/diced/open_dice/Android.bp
@@ -20,6 +20,7 @@
name: "libdiced_open_dice_nostd",
defaults: ["libdiced_open_dice_defaults"],
rustlibs: [
+ "libopen_dice_bcc_bindgen_nostd",
"libopen_dice_cbor_bindgen_nostd",
],
}
@@ -28,6 +29,7 @@
name: "libdiced_open_dice",
defaults: ["libdiced_open_dice_defaults"],
rustlibs: [
+ "libopen_dice_bcc_bindgen",
"libopen_dice_cbor_bindgen",
// For ZVec
"libkeystore2_crypto_rust",
@@ -35,4 +37,7 @@
features: [
"std",
],
+ whole_static_libs: [
+ "libopen_dice_bcc",
+ ],
}
\ No newline at end of file
diff --git a/diced/open_dice/src/bcc.rs b/diced/open_dice/src/bcc.rs
new file mode 100644
index 0000000..8bda225
--- /dev/null
+++ b/diced/open_dice/src/bcc.rs
@@ -0,0 +1,56 @@
+// Copyright 2023, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! This module mirrors the content in open-dice/include/dice/android/bcc.h
+
+use crate::error::{check_result, Result};
+use open_dice_bcc_bindgen::{
+ BccConfigValues, BccFormatConfigDescriptor, BCC_INPUT_COMPONENT_NAME,
+ BCC_INPUT_COMPONENT_VERSION, BCC_INPUT_RESETTABLE,
+};
+use std::{ffi::CStr, ptr};
+
+/// Formats a configuration descriptor following the BCC's specification.
+/// See https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ProtectedData.aidl
+pub fn bcc_format_config_descriptor(
+ name: Option<&CStr>,
+ version: Option<u64>,
+ resettable: bool,
+ buffer: &mut [u8],
+) -> Result<usize> {
+ let mut inputs = 0;
+ if name.is_some() {
+ inputs |= BCC_INPUT_COMPONENT_NAME;
+ }
+ if version.is_some() {
+ inputs |= BCC_INPUT_COMPONENT_VERSION;
+ }
+ if resettable {
+ inputs |= BCC_INPUT_RESETTABLE;
+ }
+
+ let values = BccConfigValues {
+ inputs,
+ component_name: name.map_or(ptr::null(), |p| p.as_ptr()),
+ component_version: version.unwrap_or(0),
+ };
+
+ let mut buffer_size = 0;
+ // SAFETY: The function writes to the buffer, within the given bounds, and only reads the
+ // input values. It writes its result to buffer_size.
+ check_result(unsafe {
+ BccFormatConfigDescriptor(&values, buffer.len(), buffer.as_mut_ptr(), &mut buffer_size)
+ })?;
+ Ok(buffer_size)
+}
diff --git a/diced/open_dice/src/dice.rs b/diced/open_dice/src/dice.rs
index 93d9617..a4615d3 100644
--- a/diced/open_dice/src/dice.rs
+++ b/diced/open_dice/src/dice.rs
@@ -15,8 +15,9 @@
//! Structs and functions about the types used in DICE.
//! This module mirrors the content in open-dice/include/dice/dice.h
+pub use open_dice_cbor_bindgen::DiceMode;
use open_dice_cbor_bindgen::{
- DiceConfigType, DiceInputValues, DiceMode, DICE_CDI_SIZE, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
+ DiceConfigType, DiceInputValues, DICE_CDI_SIZE, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
DICE_INLINE_CONFIG_SIZE,
};
use std::ptr;
diff --git a/diced/open_dice/src/error.rs b/diced/open_dice/src/error.rs
index 9cf2ae8..7405c51 100644
--- a/diced/open_dice/src/error.rs
+++ b/diced/open_dice/src/error.rs
@@ -29,11 +29,6 @@
BufferTooSmall,
/// Platform error.
PlatformError,
- /// Input string has an interior nul byte.
- /// TODO(b/267575445): Remove this error once we change the param of
- /// `format_config_descriptor to take &CStr.
- #[cfg(feature = "std")]
- CStrNulError,
/// The allocation of a ZVec failed.
#[cfg(feature = "std")]
ZVecError(keystore2_crypto::zvec::Error),
@@ -57,8 +52,6 @@
Self::BufferTooSmall => write!(f, "buffer too small"),
Self::PlatformError => write!(f, "platform error"),
#[cfg(feature = "std")]
- Self::CStrNulError => write!(f, "input string has an interior nul byte"),
- #[cfg(feature = "std")]
Self::ZVecError(e) => write!(f, "ZVec allocation failed {e}"),
}
}
diff --git a/diced/open_dice/src/lib.rs b/diced/open_dice/src/lib.rs
index 4cdd5c9..0723b2d 100644
--- a/diced/open_dice/src/lib.rs
+++ b/diced/open_dice/src/lib.rs
@@ -20,10 +20,17 @@
#[cfg(not(feature = "std"))]
extern crate core as std;
+mod bcc;
mod dice;
mod error;
+#[cfg(feature = "std")]
+mod retry;
+pub use bcc::bcc_format_config_descriptor;
pub use dice::{
- Cdi, Config, Hash, Hidden, InlineConfig, InputValues, CDI_SIZE, HASH_SIZE, HIDDEN_SIZE,
+ Cdi, Config, DiceMode, Hash, Hidden, InlineConfig, InputValues, CDI_SIZE, HASH_SIZE,
+ HIDDEN_SIZE,
};
pub use error::{check_result, DiceError, Result};
+#[cfg(feature = "std")]
+pub use retry::retry_bcc_format_config_descriptor;
diff --git a/diced/open_dice/src/retry.rs b/diced/open_dice/src/retry.rs
new file mode 100644
index 0000000..648fd91
--- /dev/null
+++ b/diced/open_dice/src/retry.rs
@@ -0,0 +1,64 @@
+// Copyright 2023, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! This module implements a retry version for multiple DICE functions that
+//! require preallocated output buffer. As the retry functions require
+//! memory allocation on heap, currently we only expose these functions in
+//! std environment.
+
+use crate::bcc::bcc_format_config_descriptor;
+use crate::error::{DiceError, Result};
+use std::ffi::CStr;
+
+/// Retries the given function with bigger output buffer size.
+fn retry_with_bigger_buffer<F>(mut f: F) -> Result<Vec<u8>>
+where
+ F: FnMut(&mut Vec<u8>) -> Result<usize>,
+{
+ const INITIAL_BUFFER_SIZE: usize = 256;
+ const MAX_BUFFER_SIZE: usize = 64 * 1024 * 1024;
+
+ let mut buffer = vec![0u8; INITIAL_BUFFER_SIZE];
+ while buffer.len() <= MAX_BUFFER_SIZE {
+ match f(&mut buffer) {
+ Err(DiceError::BufferTooSmall) => {
+ let new_size = buffer.len() * 2;
+ buffer.resize(new_size, 0);
+ }
+ Err(e) => return Err(e),
+ Ok(actual_size) => {
+ if actual_size > buffer.len() {
+ panic!(
+ "actual_size larger than buffer size: open-dice function
+ may have written past the end of the buffer."
+ );
+ }
+ buffer.truncate(actual_size);
+ return Ok(buffer);
+ }
+ }
+ }
+ Err(DiceError::PlatformError)
+}
+
+/// Formats a configuration descriptor following the BCC's specification.
+pub fn retry_bcc_format_config_descriptor(
+ name: Option<&CStr>,
+ version: Option<u64>,
+ resettable: bool,
+) -> Result<Vec<u8>> {
+ retry_with_bigger_buffer(|buffer| {
+ bcc_format_config_descriptor(name, version, resettable, buffer)
+ })
+}