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 | |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 21 | use core::fmt; |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 22 | use core::result; |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 23 | |
| 24 | use open_dice_cbor_bindgen::DiceHash; |
| 25 | use open_dice_cbor_bindgen::DiceResult; |
| 26 | use open_dice_cbor_bindgen::DiceResult_kDiceResultBufferTooSmall as DICE_RESULT_BUFFER_TOO_SMALL; |
| 27 | use open_dice_cbor_bindgen::DiceResult_kDiceResultInvalidInput as DICE_RESULT_INVALID_INPUT; |
| 28 | use open_dice_cbor_bindgen::DiceResult_kDiceResultOk as DICE_RESULT_OK; |
| 29 | use open_dice_cbor_bindgen::DiceResult_kDiceResultPlatformError as DICE_RESULT_PLATFORM_ERROR; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 30 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame^] | 31 | pub mod bcc; |
| 32 | |
| 33 | 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] | 34 | const HASH_SIZE: usize = open_dice_cbor_bindgen::DICE_HASH_SIZE as usize; |
| 35 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame^] | 36 | /// Array type of CDIs. |
| 37 | pub type Cdi = [u8; CDI_SIZE]; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 38 | /// Array type of hashes used by DICE. |
| 39 | pub type Hash = [u8; HASH_SIZE]; |
| 40 | |
| 41 | /// Error type used by DICE. |
| 42 | pub enum Error { |
| 43 | /// Provided input was invalid. |
| 44 | InvalidInput, |
| 45 | /// Provided buffer was too small. |
| 46 | BufferTooSmall, |
| 47 | /// Unexpected platform error. |
| 48 | PlatformError, |
| 49 | /// Unexpected return value. |
| 50 | Unknown(DiceResult), |
| 51 | } |
| 52 | |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 53 | impl fmt::Debug for Error { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 54 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 55 | match self { |
| 56 | Error::InvalidInput => write!(f, "invalid input"), |
| 57 | Error::BufferTooSmall => write!(f, "buffer too small"), |
| 58 | Error::PlatformError => write!(f, "platform error"), |
| 59 | Error::Unknown(n) => write!(f, "unknown error: {}", n), |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 64 | /// Result of DICE functions. |
| 65 | pub type Result<T> = result::Result<T, Error>; |
| 66 | |
| 67 | fn check_call(ret: DiceResult) -> Result<()> { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 68 | match ret { |
| 69 | DICE_RESULT_OK => Ok(()), |
| 70 | DICE_RESULT_INVALID_INPUT => Err(Error::InvalidInput), |
| 71 | DICE_RESULT_BUFFER_TOO_SMALL => Err(Error::BufferTooSmall), |
| 72 | DICE_RESULT_PLATFORM_ERROR => Err(Error::PlatformError), |
| 73 | n => Err(Error::Unknown(n)), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn ctx() -> *mut core::ffi::c_void { |
| 78 | core::ptr::null_mut() |
| 79 | } |
| 80 | |
| 81 | /// Hash the provided input using DICE's default hash function. |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 82 | pub fn hash(bytes: &[u8]) -> Result<Hash> { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 83 | let mut output: Hash = [0; HASH_SIZE]; |
| 84 | // SAFETY - DiceHash takes a sized input buffer and writes to a constant-sized output buffer. |
| 85 | check_call(unsafe { DiceHash(ctx(), bytes.as_ptr(), bytes.len(), output.as_mut_ptr()) })?; |
| 86 | Ok(output) |
| 87 | } |