blob: 93d9617b70123e5b7d42156c4c8861c40383489a [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::{
Alice Wang3213d492023-02-03 15:52:18 +000019 DiceConfigType, DiceInputValues, DiceMode, DICE_CDI_SIZE, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
Alice Wang0b9e1102023-02-02 09:57:06 +000020 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;
Alice Wang3213d492023-02-03 15:52:18 +000030/// The size of a CDI.
31pub const CDI_SIZE: usize = DICE_CDI_SIZE as usize;
Alice Wang0b9e1102023-02-02 09:57:06 +000032
33/// Array type of hashes used by DICE.
34pub type Hash = [u8; HASH_SIZE];
35/// Array type of additional input.
36pub type Hidden = [u8; HIDDEN_SIZE];
37/// Array type of inline configuration values.
38pub type InlineConfig = [u8; INLINE_CONFIG_SIZE];
Alice Wang3213d492023-02-03 15:52:18 +000039/// Array type of CDIs.
40pub type Cdi = [u8; CDI_SIZE];
Alice Wang0b9e1102023-02-02 09:57:06 +000041
42/// Configuration descriptor for DICE input values.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum Config<'a> {
45 /// Reference to an inline descriptor.
46 Inline(&'a InlineConfig),
47 /// Reference to a free form descriptor that will be hashed by the implementation.
48 Descriptor(&'a [u8]),
49}
50
51impl Config<'_> {
52 fn dice_config_type(&self) -> DiceConfigType {
53 match self {
54 Self::Inline(_) => DiceConfigType::kDiceConfigTypeInline,
55 Self::Descriptor(_) => DiceConfigType::kDiceConfigTypeDescriptor,
56 }
57 }
58
59 fn inline_config(&self) -> InlineConfig {
60 match self {
61 Self::Inline(inline) => **inline,
62 Self::Descriptor(_) => [0u8; INLINE_CONFIG_SIZE],
63 }
64 }
65
66 fn descriptor_ptr(&self) -> *const u8 {
67 match self {
68 Self::Descriptor(descriptor) => descriptor.as_ptr(),
69 _ => ptr::null(),
70 }
71 }
72
73 fn descriptor_size(&self) -> usize {
74 match self {
75 Self::Descriptor(descriptor) => descriptor.len(),
76 _ => 0,
77 }
78 }
79}
80
81/// Wrap of `DiceInputValues`.
82#[derive(Clone, Debug)]
83pub struct InputValues(DiceInputValues);
84
85impl InputValues {
86 /// Creates a new `InputValues`.
87 pub fn new(
Alice Wangb68814b2023-02-02 13:15:32 +000088 code_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000089 config: Config,
Alice Wangb68814b2023-02-02 13:15:32 +000090 authority_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000091 mode: DiceMode,
Alice Wangb68814b2023-02-02 13:15:32 +000092 hidden: Hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +000093 ) -> Self {
94 Self(DiceInputValues {
Alice Wangb68814b2023-02-02 13:15:32 +000095 code_hash,
96 code_descriptor: ptr::null(),
97 code_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +000098 config_type: config.dice_config_type(),
99 config_value: config.inline_config(),
100 config_descriptor: config.descriptor_ptr(),
101 config_descriptor_size: config.descriptor_size(),
Alice Wangb68814b2023-02-02 13:15:32 +0000102 authority_hash,
103 authority_descriptor: ptr::null(),
104 authority_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +0000105 mode,
Alice Wangb68814b2023-02-02 13:15:32 +0000106 hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +0000107 })
108 }
109
110 /// Returns a raw pointer to the wrapped `DiceInputValues`.
111 pub fn as_ptr(&self) -> *const DiceInputValues {
112 &self.0 as *const DiceInputValues
113 }
114}