blob: d5e087f858d21f33204215b553dfb54193142f6a [file] [log] [blame]
Alice Wang0b9e1102023-02-02 09:57:06 +00001// Copyright 2023, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Structs and functions about the types used in DICE.
16//! This module mirrors the content in open-dice/include/dice/dice.h
17
Alice Wang0b9e1102023-02-02 09:57:06 +000018use open_dice_cbor_bindgen::{
19 DiceConfigType, DiceInputValues, DiceMode, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
20 DICE_INLINE_CONFIG_SIZE,
21};
Alice Wang856d6562023-02-03 13:51:08 +000022use std::ptr;
Alice Wang0b9e1102023-02-02 09:57:06 +000023
24/// The size of a DICE hash.
25pub const HASH_SIZE: usize = DICE_HASH_SIZE as usize;
26/// The size of the DICE hidden value.
27pub const HIDDEN_SIZE: usize = DICE_HIDDEN_SIZE as usize;
28/// The size of a DICE inline config.
29const INLINE_CONFIG_SIZE: usize = DICE_INLINE_CONFIG_SIZE as usize;
30
31/// Array type of hashes used by DICE.
32pub type Hash = [u8; HASH_SIZE];
33/// Array type of additional input.
34pub type Hidden = [u8; HIDDEN_SIZE];
35/// Array type of inline configuration values.
36pub type InlineConfig = [u8; INLINE_CONFIG_SIZE];
37
38/// Configuration descriptor for DICE input values.
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum Config<'a> {
41 /// Reference to an inline descriptor.
42 Inline(&'a InlineConfig),
43 /// Reference to a free form descriptor that will be hashed by the implementation.
44 Descriptor(&'a [u8]),
45}
46
47impl Config<'_> {
48 fn dice_config_type(&self) -> DiceConfigType {
49 match self {
50 Self::Inline(_) => DiceConfigType::kDiceConfigTypeInline,
51 Self::Descriptor(_) => DiceConfigType::kDiceConfigTypeDescriptor,
52 }
53 }
54
55 fn inline_config(&self) -> InlineConfig {
56 match self {
57 Self::Inline(inline) => **inline,
58 Self::Descriptor(_) => [0u8; INLINE_CONFIG_SIZE],
59 }
60 }
61
62 fn descriptor_ptr(&self) -> *const u8 {
63 match self {
64 Self::Descriptor(descriptor) => descriptor.as_ptr(),
65 _ => ptr::null(),
66 }
67 }
68
69 fn descriptor_size(&self) -> usize {
70 match self {
71 Self::Descriptor(descriptor) => descriptor.len(),
72 _ => 0,
73 }
74 }
75}
76
77/// Wrap of `DiceInputValues`.
78#[derive(Clone, Debug)]
79pub struct InputValues(DiceInputValues);
80
81impl InputValues {
82 /// Creates a new `InputValues`.
83 pub fn new(
84 code_hash: &Hash,
85 code_descriptor: Option<&[u8]>,
86 config: Config,
87 authority_hash: &Hash,
88 authority_descriptor: Option<&[u8]>,
89 mode: DiceMode,
90 hidden: Option<&Hidden>,
91 ) -> Self {
92 Self(DiceInputValues {
93 code_hash: *code_hash,
94 code_descriptor: code_descriptor.map_or(ptr::null(), |d| d.as_ptr()),
95 code_descriptor_size: code_descriptor.map_or(0, |d| d.len()),
96 config_type: config.dice_config_type(),
97 config_value: config.inline_config(),
98 config_descriptor: config.descriptor_ptr(),
99 config_descriptor_size: config.descriptor_size(),
100 authority_hash: *authority_hash,
101 authority_descriptor: authority_descriptor.map_or(ptr::null(), |d| d.as_ptr()),
102 authority_descriptor_size: authority_descriptor.map_or(0, |d| d.len()),
103 mode,
104 hidden: hidden.map_or([0; HIDDEN_SIZE], |h| *h),
105 })
106 }
107
108 /// Returns a raw pointer to the wrapped `DiceInputValues`.
109 pub fn as_ptr(&self) -> *const DiceInputValues {
110 &self.0 as *const DiceInputValues
111 }
112}