David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Wang | a7e4ce0 | 2023-02-03 13:37:14 +0000 | [diff] [blame^] | 21 | pub use diced_open_dice::{check_result, Config, DiceError, Hash, InputValues, Result, HASH_SIZE}; |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 22 | pub use open_dice_cbor_bindgen::DiceMode; |
| 23 | |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 24 | use open_dice_cbor_bindgen::DiceHash; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 25 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame] | 26 | pub mod bcc; |
| 27 | |
| 28 | const CDI_SIZE: usize = open_dice_cbor_bindgen::DICE_CDI_SIZE as usize; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 29 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame] | 30 | /// Array type of CDIs. |
| 31 | pub type Cdi = [u8; CDI_SIZE]; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 32 | |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 33 | fn ctx() -> *mut core::ffi::c_void { |
| 34 | core::ptr::null_mut() |
| 35 | } |
| 36 | |
| 37 | /// Hash the provided input using DICE's default hash function. |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 38 | pub fn hash(bytes: &[u8]) -> Result<Hash> { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 39 | let mut output: Hash = [0; HASH_SIZE]; |
| 40 | // SAFETY - DiceHash takes a sized input buffer and writes to a constant-sized output buffer. |
Alice Wang | a7e4ce0 | 2023-02-03 13:37:14 +0000 | [diff] [blame^] | 41 | check_result(unsafe { DiceHash(ctx(), bytes.as_ptr(), bytes.len(), output.as_mut_ptr()) })?; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 42 | Ok(output) |
| 43 | } |