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