blob: 9bbacc64e93cff4765a4032a0923b483853342a6 [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
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000021use core::fmt;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000022use core::mem;
23use core::ptr;
Pierre-Clément Tosidb14ada2022-12-06 15:27:16 +000024use core::result;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000025
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000026use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeDescriptor as DICE_CONFIG_TYPE_DESCRIPTOR;
27use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeInline as DICE_CONFIG_TYPE_INLINE;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000028use open_dice_cbor_bindgen::DiceHash;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000029use open_dice_cbor_bindgen::DiceInputValues;
30use open_dice_cbor_bindgen::DiceMode;
31use open_dice_cbor_bindgen::DiceMode_kDiceModeDebug as DICE_MODE_DEBUG;
32use open_dice_cbor_bindgen::DiceMode_kDiceModeMaintenance as DICE_MODE_MAINTENANCE;
33use open_dice_cbor_bindgen::DiceMode_kDiceModeNormal as DICE_MODE_NORMAL;
34use open_dice_cbor_bindgen::DiceMode_kDiceModeNotInitialized as DICE_MODE_NOT_INITIALIZED;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000035use open_dice_cbor_bindgen::DiceResult;
36use open_dice_cbor_bindgen::DiceResult_kDiceResultBufferTooSmall as DICE_RESULT_BUFFER_TOO_SMALL;
37use open_dice_cbor_bindgen::DiceResult_kDiceResultInvalidInput as DICE_RESULT_INVALID_INPUT;
38use open_dice_cbor_bindgen::DiceResult_kDiceResultOk as DICE_RESULT_OK;
39use open_dice_cbor_bindgen::DiceResult_kDiceResultPlatformError as DICE_RESULT_PLATFORM_ERROR;
David Brazdil9a83e612022-09-27 17:38:10 +000040
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000041pub mod bcc;
42
43const CDI_SIZE: usize = open_dice_cbor_bindgen::DICE_CDI_SIZE as usize;
David Brazdil9a83e612022-09-27 17:38:10 +000044const HASH_SIZE: usize = open_dice_cbor_bindgen::DICE_HASH_SIZE as usize;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000045const HIDDEN_SIZE: usize = open_dice_cbor_bindgen::DICE_HIDDEN_SIZE as usize;
46const INLINE_CONFIG_SIZE: usize = open_dice_cbor_bindgen::DICE_INLINE_CONFIG_SIZE as usize;
David Brazdil9a83e612022-09-27 17:38:10 +000047
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000048/// Array type of CDIs.
49pub type Cdi = [u8; CDI_SIZE];
David Brazdil9a83e612022-09-27 17:38:10 +000050/// Array type of hashes used by DICE.
51pub type Hash = [u8; HASH_SIZE];
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000052/// Array type of additional input.
53pub type Hidden = [u8; HIDDEN_SIZE];
54/// Array type of inline configuration values.
55pub type InlineConfig = [u8; INLINE_CONFIG_SIZE];
David Brazdil9a83e612022-09-27 17:38:10 +000056
57/// Error type used by DICE.
58pub 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 Tosic3b61732022-12-06 16:30:51 +000069impl fmt::Debug for Error {
David Brazdil9a83e612022-09-27 17:38:10 +000070 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 Tosidb14ada2022-12-06 15:27:16 +000080/// Result of DICE functions.
81pub type Result<T> = result::Result<T, Error>;
82
83fn check_call(ret: DiceResult) -> Result<()> {
David Brazdil9a83e612022-09-27 17:38:10 +000084 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 Tosi4f4f5eb2022-12-08 14:31:42 +000093/// DICE mode values.
94#[derive(Clone, Copy, Debug)]
95pub 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
107impl 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)]
115pub 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)]
125pub struct InputValues(DiceInputValues);
126
127impl 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 Brazdil9a83e612022-09-27 17:38:10 +0000166fn 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 Tosidb14ada2022-12-06 15:27:16 +0000171pub fn hash(bytes: &[u8]) -> Result<Hash> {
David Brazdil9a83e612022-09-27 17:38:10 +0000172 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 Tosi4f4f5eb2022-12-08 14:31:42 +0000177
178fn 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}