blob: 187075a6f528dce4d524b88c33e80fbc8db503c6 [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(
Alice Wangb68814b2023-02-02 13:15:32 +000084 code_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000085 config: Config,
Alice Wangb68814b2023-02-02 13:15:32 +000086 authority_hash: Hash,
Alice Wang0b9e1102023-02-02 09:57:06 +000087 mode: DiceMode,
Alice Wangb68814b2023-02-02 13:15:32 +000088 hidden: Hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +000089 ) -> Self {
90 Self(DiceInputValues {
Alice Wangb68814b2023-02-02 13:15:32 +000091 code_hash,
92 code_descriptor: ptr::null(),
93 code_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +000094 config_type: config.dice_config_type(),
95 config_value: config.inline_config(),
96 config_descriptor: config.descriptor_ptr(),
97 config_descriptor_size: config.descriptor_size(),
Alice Wangb68814b2023-02-02 13:15:32 +000098 authority_hash,
99 authority_descriptor: ptr::null(),
100 authority_descriptor_size: 0,
Alice Wang0b9e1102023-02-02 09:57:06 +0000101 mode,
Alice Wangb68814b2023-02-02 13:15:32 +0000102 hidden,
Alice Wang0b9e1102023-02-02 09:57:06 +0000103 })
104 }
105
106 /// Returns a raw pointer to the wrapped `DiceInputValues`.
107 pub fn as_ptr(&self) -> *const DiceInputValues {
108 &self.0 as *const DiceInputValues
109 }
110}