blob: a4615d36f039ba2d186b940544f158f8edbe9b1a [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 Wang9c40eca2023-02-03 13:10:24 +000018pub use open_dice_cbor_bindgen::DiceMode;
Alice Wang0b9e1102023-02-02 09:57:06 +000019use open_dice_cbor_bindgen::{
Alice Wang9c40eca2023-02-03 13:10:24 +000020 DiceConfigType, DiceInputValues, DICE_CDI_SIZE, DICE_HASH_SIZE, DICE_HIDDEN_SIZE,
Alice Wang0b9e1102023-02-02 09:57:06 +000021 DICE_INLINE_CONFIG_SIZE,
22};
Alice Wang856d6562023-02-03 13:51:08 +000023use std::ptr;
Alice Wang0b9e1102023-02-02 09:57:06 +000024
25/// The size of a DICE hash.
26pub const HASH_SIZE: usize = DICE_HASH_SIZE as usize;
27/// The size of the DICE hidden value.
28pub const HIDDEN_SIZE: usize = DICE_HIDDEN_SIZE as usize;
29/// The size of a DICE inline config.
30const INLINE_CONFIG_SIZE: usize = DICE_INLINE_CONFIG_SIZE as usize;
Alice Wang3213d492023-02-03 15:52:18 +000031/// The size of a CDI.
32pub const CDI_SIZE: usize = DICE_CDI_SIZE as usize;
Alice Wang0b9e1102023-02-02 09:57:06 +000033
34/// Array type of hashes used by DICE.
35pub type Hash = [u8; HASH_SIZE];
36/// Array type of additional input.
37pub type Hidden = [u8; HIDDEN_SIZE];
38/// Array type of inline configuration values.
39pub type InlineConfig = [u8; INLINE_CONFIG_SIZE];
Alice Wang3213d492023-02-03 15:52:18 +000040/// Array type of CDIs.
41pub type Cdi = [u8; CDI_SIZE];
Alice Wang0b9e1102023-02-02 09:57:06 +000042
43/// Configuration descriptor for DICE input values.
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum Config<'a> {
46 /// Reference to an inline descriptor.
47 Inline(&'a InlineConfig),
48 /// Reference to a free form descriptor that will be hashed by the implementation.
49 Descriptor(&'a [u8]),
50}
51
52impl Config<'_> {
53 fn dice_config_type(&self) -> DiceConfigType {
54 match self {
55 Self::Inline(_) => DiceConfigType::kDiceConfigTypeInline,
56 Self::Descriptor(_) => DiceConfigType::kDiceConfigTypeDescriptor,
57 }
58 }
59
60 fn inline_config(&self) -> InlineConfig {
61 match self {
62 Self::Inline(inline) => **inline,
63 Self::Descriptor(_) => [0u8; INLINE_CONFIG_SIZE],
64 }
65 }
66
67 fn descriptor_ptr(&self) -> *const u8 {
68 match self {
69 Self::Descriptor(descriptor) => descriptor.as_ptr(),
70 _ => ptr::null(),
71 }
72 }
73
74 fn descriptor_size(&self) -> usize {
75 match self {
76 Self::Descriptor(descriptor) => descriptor.len(),
77 _ => 0,
78 }
79 }
80}
81
82/// Wrap of `DiceInputValues`.
83#[derive(Clone, Debug)]
84pub struct InputValues(DiceInputValues);
85
86impl InputValues {
87 /// Creates a new `InputValues`.
88 pub fn new(
Alice Wangb68814b2023-02-02 13:15:32 +000089 code_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000090 config: Config,
Alice Wangb68814b2023-02-02 13:15:32 +000091 authority_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000092 mode: DiceMode,
Alice Wangb68814b2023-02-02 13:15:32 +000093 hidden: Hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +000094 ) -> Self {
95 Self(DiceInputValues {
Alice Wangb68814b2023-02-02 13:15:32 +000096 code_hash,
97 code_descriptor: ptr::null(),
98 code_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +000099 config_type: config.dice_config_type(),
100 config_value: config.inline_config(),
101 config_descriptor: config.descriptor_ptr(),
102 config_descriptor_size: config.descriptor_size(),
Alice Wangb68814b2023-02-02 13:15:32 +0000103 authority_hash,
104 authority_descriptor: ptr::null(),
105 authority_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +0000106 mode,
Alice Wangb68814b2023-02-02 13:15:32 +0000107 hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +0000108 })
109 }
110
111 /// Returns a raw pointer to the wrapped `DiceInputValues`.
112 pub fn as_ptr(&self) -> *const DiceInputValues {
113 &self.0 as *const DiceInputValues
114 }
115}