blob: 5332092312c474d5ff3bb68dd31cebaf67d4e889 [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
Alice Wang31226132023-01-31 12:44:39 +000026pub use open_dice_cbor_bindgen::DiceMode;
27
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000028use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeDescriptor as DICE_CONFIG_TYPE_DESCRIPTOR;
29use open_dice_cbor_bindgen::DiceConfigType_kDiceConfigTypeInline as DICE_CONFIG_TYPE_INLINE;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000030use open_dice_cbor_bindgen::DiceHash;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000031use open_dice_cbor_bindgen::DiceInputValues;
Pierre-Clément Tosic3b61732022-12-06 16:30:51 +000032use open_dice_cbor_bindgen::DiceResult;
33use open_dice_cbor_bindgen::DiceResult_kDiceResultBufferTooSmall as DICE_RESULT_BUFFER_TOO_SMALL;
34use open_dice_cbor_bindgen::DiceResult_kDiceResultInvalidInput as DICE_RESULT_INVALID_INPUT;
35use open_dice_cbor_bindgen::DiceResult_kDiceResultOk as DICE_RESULT_OK;
36use open_dice_cbor_bindgen::DiceResult_kDiceResultPlatformError as DICE_RESULT_PLATFORM_ERROR;
David Brazdil9a83e612022-09-27 17:38:10 +000037
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000038pub mod bcc;
39
40const CDI_SIZE: usize = open_dice_cbor_bindgen::DICE_CDI_SIZE as usize;
David Brazdil9a83e612022-09-27 17:38:10 +000041const HASH_SIZE: usize = open_dice_cbor_bindgen::DICE_HASH_SIZE as usize;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000042const HIDDEN_SIZE: usize = open_dice_cbor_bindgen::DICE_HIDDEN_SIZE as usize;
43const INLINE_CONFIG_SIZE: usize = open_dice_cbor_bindgen::DICE_INLINE_CONFIG_SIZE as usize;
David Brazdil9a83e612022-09-27 17:38:10 +000044
Pierre-Clément Tosi8edf72e2022-12-06 16:02:57 +000045/// Array type of CDIs.
46pub type Cdi = [u8; CDI_SIZE];
David Brazdil9a83e612022-09-27 17:38:10 +000047/// Array type of hashes used by DICE.
48pub type Hash = [u8; HASH_SIZE];
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000049/// Array type of additional input.
50pub type Hidden = [u8; HIDDEN_SIZE];
51/// Array type of inline configuration values.
52pub type InlineConfig = [u8; INLINE_CONFIG_SIZE];
David Brazdil9a83e612022-09-27 17:38:10 +000053
54/// Error type used by DICE.
55pub 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 Tosic3b61732022-12-06 16:30:51 +000066impl fmt::Debug for Error {
David Brazdil9a83e612022-09-27 17:38:10 +000067 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 Tosidb14ada2022-12-06 15:27:16 +000077/// Result of DICE functions.
78pub type Result<T> = result::Result<T, Error>;
79
80fn check_call(ret: DiceResult) -> Result<()> {
David Brazdil9a83e612022-09-27 17:38:10 +000081 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 Tosi4f4f5eb2022-12-08 14:31:42 +000090/// DICE configuration input type.
91#[derive(Debug)]
92pub 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)]
102pub struct InputValues(DiceInputValues);
103
104impl 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 Wang31226132023-01-31 12:44:39 +0000112 mode: DiceMode,
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +0000113 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 Wang31226132023-01-31 12:44:39 +0000137 mode,
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +0000138 hidden: hidden.map_or([0; mem::size_of::<Hidden>()], |h| *h),
139 })
140 }
141}
142
David Brazdil9a83e612022-09-27 17:38:10 +0000143fn 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 Tosidb14ada2022-12-06 15:27:16 +0000148pub fn hash(bytes: &[u8]) -> Result<Hash> {
David Brazdil9a83e612022-09-27 17:38:10 +0000149 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 Tosi4f4f5eb2022-12-08 14:31:42 +0000154
155fn 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}