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 | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 22 | use core::mem; |
| 23 | use core::ptr; |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 24 | use core::result; |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 25 | |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame^] | 26 | pub use open_dice_cbor_bindgen::DiceMode; |
| 27 | |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 28 | use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeDescriptor as DICE_CONFIG_TYPE_DESCRIPTOR; |
| 29 | use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeInline as DICE_CONFIG_TYPE_INLINE; |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 30 | use open_dice_cbor_bindgen::DiceHash; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 31 | use open_dice_cbor_bindgen::DiceInputValues; |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 32 | use open_dice_cbor_bindgen::DiceResult; |
| 33 | use open_dice_cbor_bindgen::DiceResult_kDiceResultBufferTooSmall as DICE_RESULT_BUFFER_TOO_SMALL; |
| 34 | use open_dice_cbor_bindgen::DiceResult_kDiceResultInvalidInput as DICE_RESULT_INVALID_INPUT; |
| 35 | use open_dice_cbor_bindgen::DiceResult_kDiceResultOk as DICE_RESULT_OK; |
| 36 | use open_dice_cbor_bindgen::DiceResult_kDiceResultPlatformError as DICE_RESULT_PLATFORM_ERROR; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 37 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame] | 38 | pub mod bcc; |
| 39 | |
| 40 | 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] | 41 | const HASH_SIZE: usize = open_dice_cbor_bindgen::DICE_HASH_SIZE as usize; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 42 | const HIDDEN_SIZE: usize = open_dice_cbor_bindgen::DICE_HIDDEN_SIZE as usize; |
| 43 | const INLINE_CONFIG_SIZE: usize = open_dice_cbor_bindgen::DICE_INLINE_CONFIG_SIZE as usize; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 44 | |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame] | 45 | /// Array type of CDIs. |
| 46 | pub type Cdi = [u8; CDI_SIZE]; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 47 | /// Array type of hashes used by DICE. |
| 48 | pub type Hash = [u8; HASH_SIZE]; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 49 | /// Array type of additional input. |
| 50 | pub type Hidden = [u8; HIDDEN_SIZE]; |
| 51 | /// Array type of inline configuration values. |
| 52 | pub type InlineConfig = [u8; INLINE_CONFIG_SIZE]; |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 53 | |
| 54 | /// Error type used by DICE. |
| 55 | pub enum Error { |
| 56 | /// Provided input was invalid. |
| 57 | InvalidInput, |
| 58 | /// Provided buffer was too small. |
| 59 | BufferTooSmall, |
| 60 | /// Unexpected platform error. |
| 61 | PlatformError, |
| 62 | /// Unexpected return value. |
| 63 | Unknown(DiceResult), |
| 64 | } |
| 65 | |
Pierre-Clément Tosi | c3b6173 | 2022-12-06 16:30:51 +0000 | [diff] [blame] | 66 | impl fmt::Debug for Error { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 67 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 68 | match self { |
| 69 | Error::InvalidInput => write!(f, "invalid input"), |
| 70 | Error::BufferTooSmall => write!(f, "buffer too small"), |
| 71 | Error::PlatformError => write!(f, "platform error"), |
| 72 | Error::Unknown(n) => write!(f, "unknown error: {}", n), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 77 | /// Result of DICE functions. |
| 78 | pub type Result<T> = result::Result<T, Error>; |
| 79 | |
| 80 | fn check_call(ret: DiceResult) -> Result<()> { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 81 | match ret { |
| 82 | DICE_RESULT_OK => Ok(()), |
| 83 | DICE_RESULT_INVALID_INPUT => Err(Error::InvalidInput), |
| 84 | DICE_RESULT_BUFFER_TOO_SMALL => Err(Error::BufferTooSmall), |
| 85 | DICE_RESULT_PLATFORM_ERROR => Err(Error::PlatformError), |
| 86 | n => Err(Error::Unknown(n)), |
| 87 | } |
| 88 | } |
| 89 | |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 90 | /// DICE configuration input type. |
| 91 | #[derive(Debug)] |
| 92 | pub enum ConfigType<'a> { |
| 93 | /// Uses the formatted 64-byte configuration input value (See the Open Profile for DICE). |
| 94 | Inline(InlineConfig), |
| 95 | /// Uses the 64-byte hash of more configuration data. |
| 96 | Descriptor(&'a [u8]), |
| 97 | } |
| 98 | |
| 99 | /// Set of DICE inputs. |
| 100 | #[repr(transparent)] |
| 101 | #[derive(Clone, Debug)] |
| 102 | pub struct InputValues(DiceInputValues); |
| 103 | |
| 104 | impl InputValues { |
| 105 | /// Wrap the DICE inputs in a InputValues, expected by bcc::main_flow(). |
| 106 | pub fn new( |
| 107 | code_hash: &Hash, |
| 108 | code_descriptor: Option<&[u8]>, |
| 109 | config: &ConfigType, |
| 110 | auth_hash: Option<&Hash>, |
| 111 | auth_descriptor: Option<&[u8]>, |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame^] | 112 | mode: DiceMode, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 113 | hidden: Option<&Hidden>, |
| 114 | ) -> Self { |
| 115 | const ZEROED_INLINE_CONFIG: InlineConfig = [0; INLINE_CONFIG_SIZE]; |
| 116 | let (config_type, config_value, config_descriptor) = match config { |
| 117 | ConfigType::Inline(value) => (DICE_CONFIG_TYPE_INLINE, *value, None), |
| 118 | ConfigType::Descriptor(desc) => { |
| 119 | (DICE_CONFIG_TYPE_DESCRIPTOR, ZEROED_INLINE_CONFIG, Some(*desc)) |
| 120 | } |
| 121 | }; |
| 122 | let (code_descriptor, code_descriptor_size) = as_raw_parts(code_descriptor); |
| 123 | let (config_descriptor, config_descriptor_size) = as_raw_parts(config_descriptor); |
| 124 | let (authority_descriptor, authority_descriptor_size) = as_raw_parts(auth_descriptor); |
| 125 | |
| 126 | Self(DiceInputValues { |
| 127 | code_hash: *code_hash, |
| 128 | code_descriptor, |
| 129 | code_descriptor_size, |
| 130 | config_type, |
| 131 | config_value, |
| 132 | config_descriptor, |
| 133 | config_descriptor_size, |
| 134 | authority_hash: auth_hash.map_or([0; mem::size_of::<Hash>()], |h| *h), |
| 135 | authority_descriptor, |
| 136 | authority_descriptor_size, |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame^] | 137 | mode, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 138 | hidden: hidden.map_or([0; mem::size_of::<Hidden>()], |h| *h), |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 143 | fn ctx() -> *mut core::ffi::c_void { |
| 144 | core::ptr::null_mut() |
| 145 | } |
| 146 | |
| 147 | /// Hash the provided input using DICE's default hash function. |
Pierre-Clément Tosi | db14ada | 2022-12-06 15:27:16 +0000 | [diff] [blame] | 148 | pub fn hash(bytes: &[u8]) -> Result<Hash> { |
David Brazdil | 9a83e61 | 2022-09-27 17:38:10 +0000 | [diff] [blame] | 149 | let mut output: Hash = [0; HASH_SIZE]; |
| 150 | // SAFETY - DiceHash takes a sized input buffer and writes to a constant-sized output buffer. |
| 151 | check_call(unsafe { DiceHash(ctx(), bytes.as_ptr(), bytes.len(), output.as_mut_ptr()) })?; |
| 152 | Ok(output) |
| 153 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 154 | |
| 155 | fn as_raw_parts<T: Sized>(s: Option<&[T]>) -> (*const T, usize) { |
| 156 | match s { |
| 157 | Some(s) => (s.as_ptr(), s.len()), |
| 158 | None => (ptr::null(), 0), |
| 159 | } |
| 160 | } |