blob: 4a1b6991576ec388a62c1bfd202555abfac00500 [file] [log] [blame]
David Brazdil9a83e612022-09-27 17:38:10 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Bare metal wrapper around libopen_dice.
18
19#![no_std]
20
Alice Wanga7773662023-02-03 09:37:17 +000021pub use diced_open_dice::{
22 check_result, Config, DiceError, Hash, InputValues, Result, HASH_SIZE, HIDDEN_SIZE,
23};
Alice Wang31226132023-01-31 12:44:39 +000024pub use open_dice_cbor_bindgen::DiceMode;
25
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000026use open_dice_cbor_bindgen::DiceHash;
David Brazdil9a83e612022-09-27 17:38:10 +000027
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000028pub mod bcc;
29
30const CDI_SIZE: usize = open_dice_cbor_bindgen::DICE_CDI_SIZE as usize;
David Brazdil9a83e612022-09-27 17:38:10 +000031
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000032/// Array type of CDIs.
33pub type Cdi = [u8; CDI_SIZE];
David Brazdil9a83e612022-09-27 17:38:10 +000034
David Brazdil9a83e612022-09-27 17:38:10 +000035fn ctx() -> *mut core::ffi::c_void {
36 core::ptr::null_mut()
37}
38
39/// Hash the provided input using DICE's default hash function.
Pierre-Clément Tosidb14ada2022-12-06 15:27:16 +000040pub fn hash(bytes: &[u8]) -> Result<Hash> {
David Brazdil9a83e612022-09-27 17:38:10 +000041 let mut output: Hash = [0; HASH_SIZE];
42 // SAFETY - DiceHash takes a sized input buffer and writes to a constant-sized output buffer.
Alice Wanga7e4ce02023-02-03 13:37:14 +000043 check_result(unsafe { DiceHash(ctx(), bytes.as_ptr(), bytes.len(), output.as_mut_ptr()) })?;
David Brazdil9a83e612022-09-27 17:38:10 +000044 Ok(output)
45}