Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 1 | // 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 Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame^] | 18 | pub use open_dice_cbor_bindgen::DiceMode; |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 19 | use open_dice_cbor_bindgen::{ |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame^] | 20 | DiceConfigType, DiceInputValues, DICE_CDI_SIZE, DICE_HASH_SIZE, DICE_HIDDEN_SIZE, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 21 | DICE_INLINE_CONFIG_SIZE, |
| 22 | }; |
Alice Wang | 856d656 | 2023-02-03 13:51:08 +0000 | [diff] [blame] | 23 | use std::ptr; |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 24 | |
| 25 | /// The size of a DICE hash. |
| 26 | pub const HASH_SIZE: usize = DICE_HASH_SIZE as usize; |
| 27 | /// The size of the DICE hidden value. |
| 28 | pub const HIDDEN_SIZE: usize = DICE_HIDDEN_SIZE as usize; |
| 29 | /// The size of a DICE inline config. |
| 30 | const INLINE_CONFIG_SIZE: usize = DICE_INLINE_CONFIG_SIZE as usize; |
Alice Wang | 3213d49 | 2023-02-03 15:52:18 +0000 | [diff] [blame] | 31 | /// The size of a CDI. |
| 32 | pub const CDI_SIZE: usize = DICE_CDI_SIZE as usize; |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 33 | |
| 34 | /// Array type of hashes used by DICE. |
| 35 | pub type Hash = [u8; HASH_SIZE]; |
| 36 | /// Array type of additional input. |
| 37 | pub type Hidden = [u8; HIDDEN_SIZE]; |
| 38 | /// Array type of inline configuration values. |
| 39 | pub type InlineConfig = [u8; INLINE_CONFIG_SIZE]; |
Alice Wang | 3213d49 | 2023-02-03 15:52:18 +0000 | [diff] [blame] | 40 | /// Array type of CDIs. |
| 41 | pub type Cdi = [u8; CDI_SIZE]; |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 42 | |
| 43 | /// Configuration descriptor for DICE input values. |
| 44 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 45 | pub 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 | |
| 52 | impl 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)] |
| 84 | pub struct InputValues(DiceInputValues); |
| 85 | |
| 86 | impl InputValues { |
| 87 | /// Creates a new `InputValues`. |
| 88 | pub fn new( |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 89 | code_hash: Hash, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 90 | config: Config, |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 91 | authority_hash: Hash, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 92 | mode: DiceMode, |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 93 | hidden: Hidden, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 94 | ) -> Self { |
| 95 | Self(DiceInputValues { |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 96 | code_hash, |
| 97 | code_descriptor: ptr::null(), |
| 98 | code_descriptor_size: 0, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 99 | 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 Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 103 | authority_hash, |
| 104 | authority_descriptor: ptr::null(), |
| 105 | authority_descriptor_size: 0, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 106 | mode, |
Alice Wang | b68814b | 2023-02-02 13:15:32 +0000 | [diff] [blame] | 107 | hidden, |
Alice Wang | 0b9e110 | 2023-02-02 09:57:06 +0000 | [diff] [blame] | 108 | }) |
| 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 | } |