Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 1 | // Copyright 2022, 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 | //! Support for DICE derivation and BCC generation. |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 16 | extern crate alloc; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 17 | |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 18 | use alloc::format; |
| 19 | use alloc::vec::Vec; |
| 20 | use ciborium::cbor; |
| 21 | use ciborium::Value; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 22 | use core::mem::size_of; |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 23 | use diced_open_dice::{ |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 24 | bcc_handover_main_flow, hash, Config, DiceContext, DiceMode, Hash, InputValues, HIDDEN_SIZE, |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 25 | }; |
Alan Stokes | a639b58 | 2023-11-22 13:14:27 +0000 | [diff] [blame] | 26 | use pvmfw_avb::{Capability, DebugLevel, Digest, VerifiedBootData}; |
Alan Stokes | a17cfba | 2024-02-14 17:34:51 +0000 | [diff] [blame] | 27 | use zerocopy::AsBytes; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 28 | |
Shikha Panwar | 47e5b61 | 2024-05-20 19:50:02 +0000 | [diff] [blame] | 29 | // pVM firmware (like other VM components) is expected to populate some fields in DICE |
| 30 | // Configuration Descriptor. See dice_for_avf_guest.cddl |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 31 | const COMPONENT_NAME_KEY: i64 = -70002; |
| 32 | const SECURITY_VERSION_KEY: i64 = -70005; |
| 33 | const RKP_VM_MARKER_KEY: i64 = -70006; |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 34 | const INSTANCE_HASH_KEY: i64 = -71003; |
| 35 | |
| 36 | #[derive(Debug)] |
| 37 | pub enum Error { |
| 38 | /// Error in CBOR operations |
Stephen Hines | e1b4289 | 2024-08-07 11:03:44 -0700 | [diff] [blame] | 39 | #[allow(dead_code)] |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 40 | CborError(ciborium::value::Error), |
| 41 | /// Error in DICE operations |
Stephen Hines | e1b4289 | 2024-08-07 11:03:44 -0700 | [diff] [blame] | 42 | #[allow(dead_code)] |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 43 | DiceError(diced_open_dice::DiceError), |
| 44 | } |
| 45 | |
| 46 | impl From<ciborium::value::Error> for Error { |
| 47 | fn from(e: ciborium::value::Error) -> Self { |
| 48 | Self::CborError(e) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl From<diced_open_dice::DiceError> for Error { |
| 53 | fn from(e: diced_open_dice::DiceError) -> Self { |
| 54 | Self::DiceError(e) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // DICE in pvmfw result type. |
| 59 | type Result<T> = core::result::Result<T, Error>; |
| 60 | |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 61 | fn to_dice_mode(debug_level: DebugLevel) -> DiceMode { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 62 | match debug_level { |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 63 | DebugLevel::None => DiceMode::kDiceModeNormal, |
| 64 | DebugLevel::Full => DiceMode::kDiceModeDebug, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 68 | fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> Result<Hash> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 69 | let mut digests = [0u8; size_of::<Digest>() * 2]; |
| 70 | digests[..size_of::<Digest>()].copy_from_slice(&verified_boot_data.kernel_digest); |
| 71 | if let Some(initrd_digest) = verified_boot_data.initrd_digest { |
| 72 | digests[size_of::<Digest>()..].copy_from_slice(&initrd_digest); |
| 73 | } |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 74 | Ok(hash(&digests)?) |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 75 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 76 | |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 77 | #[derive(Clone)] |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 78 | pub struct PartialInputs { |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 79 | pub code_hash: Hash, |
| 80 | pub auth_hash: Hash, |
| 81 | pub mode: DiceMode, |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 82 | pub security_version: u64, |
Alan Stokes | a639b58 | 2023-11-22 13:14:27 +0000 | [diff] [blame] | 83 | pub rkp_vm_marker: bool, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 84 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 85 | |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 86 | impl PartialInputs { |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 87 | pub fn new(data: &VerifiedBootData) -> Result<Self> { |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 88 | let code_hash = to_dice_hash(data)?; |
| 89 | let auth_hash = hash(data.public_key)?; |
| 90 | let mode = to_dice_mode(data.debug_level); |
Shikha Panwar | a26f16a | 2023-09-27 09:39:00 +0000 | [diff] [blame] | 91 | // We use rollback_index from vbmeta as the security_version field in dice certificate. |
| 92 | let security_version = data.rollback_index; |
Alan Stokes | a639b58 | 2023-11-22 13:14:27 +0000 | [diff] [blame] | 93 | let rkp_vm_marker = data.has_capability(Capability::RemoteAttest); |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 94 | |
Alan Stokes | a639b58 | 2023-11-22 13:14:27 +0000 | [diff] [blame] | 95 | Ok(Self { code_hash, auth_hash, mode, security_version, rkp_vm_marker }) |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 98 | pub fn write_next_bcc( |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 99 | self, |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 100 | current_bcc_handover: &[u8], |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 101 | salt: &[u8; HIDDEN_SIZE], |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 102 | instance_hash: Option<Hash>, |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 103 | deferred_rollback_protection: bool, |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 104 | next_bcc: &mut [u8], |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 105 | context: DiceContext, |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 106 | ) -> Result<()> { |
| 107 | let config = self |
| 108 | .generate_config_descriptor(instance_hash) |
| 109 | .map_err(|_| diced_open_dice::DiceError::InvalidInput)?; |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 110 | |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 111 | let dice_inputs = InputValues::new( |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 112 | self.code_hash, |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 113 | Config::Descriptor(&config), |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 114 | self.auth_hash, |
| 115 | self.mode, |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 116 | self.make_hidden(salt, deferred_rollback_protection)?, |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 117 | ); |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 118 | let _ = bcc_handover_main_flow(current_bcc_handover, &dice_inputs, next_bcc, context)?; |
Alan Stokes | c4354b8 | 2023-05-04 16:06:52 +0100 | [diff] [blame] | 119 | Ok(()) |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 120 | } |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 121 | |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 122 | fn make_hidden( |
| 123 | &self, |
| 124 | salt: &[u8; HIDDEN_SIZE], |
| 125 | deferred_rollback_protection: bool, |
| 126 | ) -> diced_open_dice::Result<[u8; HIDDEN_SIZE]> { |
Alan Stokes | a17cfba | 2024-02-14 17:34:51 +0000 | [diff] [blame] | 127 | // We want to make sure we get a different sealing CDI for: |
| 128 | // - VMs with different salt values |
| 129 | // - An RKP VM and any other VM (regardless of salt) |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 130 | // - depending on whether rollback protection has been deferred to payload. This ensures the |
| 131 | // adversary cannot leak the secrets by using old images & setting |
| 132 | // `deferred_rollback_protection` to true. |
Alan Stokes | a17cfba | 2024-02-14 17:34:51 +0000 | [diff] [blame] | 133 | // The hidden input for DICE affects the sealing CDI (but the values in the config |
| 134 | // descriptor do not). |
| 135 | // Since the hidden input has to be a fixed size, create it as a hash of the values we |
| 136 | // want included. |
| 137 | #[derive(AsBytes)] |
| 138 | #[repr(C, packed)] |
| 139 | struct HiddenInput { |
| 140 | rkp_vm_marker: bool, |
| 141 | salt: [u8; HIDDEN_SIZE], |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 142 | deferred_rollback_protection: bool, |
Alan Stokes | a17cfba | 2024-02-14 17:34:51 +0000 | [diff] [blame] | 143 | } |
Shikha Panwar | ffadd4d | 2024-05-28 13:47:56 +0000 | [diff] [blame] | 144 | hash( |
| 145 | HiddenInput { |
| 146 | rkp_vm_marker: self.rkp_vm_marker, |
| 147 | salt: *salt, |
| 148 | deferred_rollback_protection, |
| 149 | } |
| 150 | .as_bytes(), |
| 151 | ) |
Alan Stokes | a17cfba | 2024-02-14 17:34:51 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 154 | fn generate_config_descriptor(&self, instance_hash: Option<Hash>) -> Result<Vec<u8>> { |
| 155 | let mut config = Vec::with_capacity(4); |
| 156 | config.push((cbor!(COMPONENT_NAME_KEY)?, cbor!("vm_entry")?)); |
| 157 | if cfg!(dice_changes) { |
| 158 | config.push((cbor!(SECURITY_VERSION_KEY)?, cbor!(self.security_version)?)); |
| 159 | } |
| 160 | if self.rkp_vm_marker { |
| 161 | config.push((cbor!(RKP_VM_MARKER_KEY)?, Value::Null)) |
| 162 | } |
| 163 | if let Some(instance_hash) = instance_hash { |
| 164 | config.push((cbor!(INSTANCE_HASH_KEY)?, Value::from(instance_hash.as_slice()))); |
| 165 | } |
| 166 | let config = Value::Map(config); |
| 167 | Ok(cbor_util::serialize(&config).map_err(|e| { |
| 168 | ciborium::value::Error::Custom(format!("Error in serialization: {e:?}")) |
| 169 | })?) |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 170 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 171 | } |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame] | 172 | |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 173 | #[cfg(test)] |
| 174 | mod tests { |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 175 | use crate::{ |
| 176 | Hash, PartialInputs, COMPONENT_NAME_KEY, INSTANCE_HASH_KEY, RKP_VM_MARKER_KEY, |
| 177 | SECURITY_VERSION_KEY, |
| 178 | }; |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 179 | use ciborium::Value; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 180 | use diced_open_dice::DiceArtifacts; |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 181 | use diced_open_dice::DiceContext; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 182 | use diced_open_dice::DiceMode; |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 183 | use diced_open_dice::KeyAlgorithm; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 184 | use diced_open_dice::HIDDEN_SIZE; |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 185 | use diced_sample_inputs::make_sample_bcc_and_cdis; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 186 | use pvmfw_avb::Capability; |
| 187 | use pvmfw_avb::DebugLevel; |
| 188 | use pvmfw_avb::Digest; |
| 189 | use pvmfw_avb::VerifiedBootData; |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 190 | use std::collections::HashMap; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 191 | use std::mem::size_of; |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 192 | use std::vec; |
| 193 | |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 194 | const COMPONENT_VERSION_KEY: i64 = -70003; |
| 195 | const RESETTABLE_KEY: i64 = -70004; |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 196 | const BASE_VB_DATA: VerifiedBootData = VerifiedBootData { |
| 197 | debug_level: DebugLevel::None, |
| 198 | kernel_digest: [1u8; size_of::<Digest>()], |
| 199 | initrd_digest: Some([2u8; size_of::<Digest>()]), |
| 200 | public_key: b"public key", |
| 201 | capabilities: vec![], |
| 202 | rollback_index: 42, |
| 203 | }; |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 204 | const HASH: Hash = *b"sixtyfourbyteslongsentencearerarebutletsgiveitatrycantbethathard"; |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 205 | |
| 206 | #[test] |
| 207 | fn base_data_conversion() { |
| 208 | let vb_data = BASE_VB_DATA; |
| 209 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 210 | |
| 211 | assert_eq!(inputs.mode, DiceMode::kDiceModeNormal); |
| 212 | assert_eq!(inputs.security_version, 42); |
| 213 | assert!(!inputs.rkp_vm_marker); |
| 214 | |
| 215 | // TODO(b/313608219): Consider checks for code_hash and possibly auth_hash. |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn debuggable_conversion() { |
| 220 | let vb_data = VerifiedBootData { debug_level: DebugLevel::Full, ..BASE_VB_DATA }; |
| 221 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 222 | |
| 223 | assert_eq!(inputs.mode, DiceMode::kDiceModeDebug); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn rkp_vm_conversion() { |
| 228 | let vb_data = |
| 229 | VerifiedBootData { capabilities: vec![Capability::RemoteAttest], ..BASE_VB_DATA }; |
| 230 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 231 | |
| 232 | assert!(inputs.rkp_vm_marker); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn base_config_descriptor() { |
| 237 | let vb_data = BASE_VB_DATA; |
| 238 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 239 | let config_map = decode_config_descriptor(&inputs, None); |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 240 | |
| 241 | assert_eq!(config_map.get(&COMPONENT_NAME_KEY).unwrap().as_text().unwrap(), "vm_entry"); |
| 242 | assert_eq!(config_map.get(&COMPONENT_VERSION_KEY), None); |
| 243 | assert_eq!(config_map.get(&RESETTABLE_KEY), None); |
| 244 | if cfg!(dice_changes) { |
| 245 | assert_eq!( |
| 246 | config_map.get(&SECURITY_VERSION_KEY).unwrap().as_integer().unwrap(), |
| 247 | 42.into() |
| 248 | ); |
| 249 | } else { |
| 250 | assert_eq!(config_map.get(&SECURITY_VERSION_KEY), None); |
| 251 | } |
| 252 | assert_eq!(config_map.get(&RKP_VM_MARKER_KEY), None); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn config_descriptor_with_rkp_vm() { |
| 257 | let vb_data = |
| 258 | VerifiedBootData { capabilities: vec![Capability::RemoteAttest], ..BASE_VB_DATA }; |
| 259 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 260 | let config_map = decode_config_descriptor(&inputs, Some(HASH)); |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 261 | |
| 262 | assert!(config_map.get(&RKP_VM_MARKER_KEY).unwrap().is_null()); |
| 263 | } |
| 264 | |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 265 | #[test] |
| 266 | fn config_descriptor_with_instance_hash() { |
| 267 | let vb_data = |
| 268 | VerifiedBootData { capabilities: vec![Capability::RemoteAttest], ..BASE_VB_DATA }; |
| 269 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 270 | let config_map = decode_config_descriptor(&inputs, Some(HASH)); |
| 271 | assert_eq!(*config_map.get(&INSTANCE_HASH_KEY).unwrap(), Value::from(HASH.as_slice())); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn config_descriptor_without_instance_hash() { |
| 276 | let vb_data = |
| 277 | VerifiedBootData { capabilities: vec![Capability::RemoteAttest], ..BASE_VB_DATA }; |
| 278 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 279 | let config_map = decode_config_descriptor(&inputs, None); |
Chris Wailes | e957964 | 2024-05-29 18:25:19 -0700 | [diff] [blame] | 280 | assert!(!config_map.contains_key(&INSTANCE_HASH_KEY)); |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | fn decode_config_descriptor( |
| 284 | inputs: &PartialInputs, |
| 285 | instance_hash: Option<Hash>, |
| 286 | ) -> HashMap<i64, Value> { |
| 287 | let config_descriptor = inputs.generate_config_descriptor(instance_hash).unwrap(); |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 288 | |
| 289 | let cbor_map = |
Shikha Panwar | 8f7fc1a | 2024-04-10 10:41:34 +0000 | [diff] [blame] | 290 | cbor_util::deserialize::<Value>(&config_descriptor).unwrap().into_map().unwrap(); |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 291 | |
| 292 | cbor_map |
| 293 | .into_iter() |
| 294 | .map(|(k, v)| ((k.into_integer().unwrap().try_into().unwrap()), v)) |
| 295 | .collect() |
| 296 | } |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 297 | |
| 298 | #[test] |
| 299 | fn changing_deferred_rpb_changes_secrets() { |
| 300 | let vb_data = VerifiedBootData { debug_level: DebugLevel::Full, ..BASE_VB_DATA }; |
| 301 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 302 | let mut buffer_without_defer = [0; 4096]; |
| 303 | let mut buffer_with_defer = [0; 4096]; |
| 304 | let mut buffer_without_defer_retry = [0; 4096]; |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 305 | let context = DiceContext { |
| 306 | authority_algorithm: KeyAlgorithm::Ed25519, |
| 307 | subject_algorithm: KeyAlgorithm::Ed25519, |
| 308 | }; |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 309 | |
| 310 | let sample_dice_input: &[u8] = &[ |
| 311 | 0xa3, // CDI attest |
| 312 | 0x01, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 313 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 314 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // CDI seal |
| 315 | 0x02, 0x58, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 316 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 317 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // DICE chain |
| 318 | 0x03, 0x82, 0xa6, 0x01, 0x02, 0x03, 0x27, 0x04, 0x02, 0x20, 0x01, 0x21, 0x40, 0x22, |
| 319 | 0x40, 0x84, 0x40, 0xa0, 0x40, 0x40, |
| 320 | // 8-bytes of trailing data that aren't part of the DICE chain. |
| 321 | 0x84, 0x41, 0x55, 0xa0, 0x42, 0x11, 0x22, 0x40, |
| 322 | ]; |
| 323 | |
| 324 | inputs |
| 325 | .clone() |
| 326 | .write_next_bcc( |
| 327 | sample_dice_input, |
| 328 | &[0u8; HIDDEN_SIZE], |
| 329 | Some([0u8; 64]), |
| 330 | false, |
| 331 | &mut buffer_without_defer, |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 332 | context.clone(), |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 333 | ) |
| 334 | .unwrap(); |
| 335 | let bcc_handover1 = diced_open_dice::bcc_handover_parse(&buffer_without_defer).unwrap(); |
| 336 | |
| 337 | inputs |
| 338 | .clone() |
| 339 | .write_next_bcc( |
| 340 | sample_dice_input, |
| 341 | &[0u8; HIDDEN_SIZE], |
| 342 | Some([0u8; 64]), |
| 343 | true, |
| 344 | &mut buffer_with_defer, |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 345 | context.clone(), |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 346 | ) |
| 347 | .unwrap(); |
| 348 | let bcc_handover2 = diced_open_dice::bcc_handover_parse(&buffer_with_defer).unwrap(); |
| 349 | |
| 350 | inputs |
| 351 | .clone() |
| 352 | .write_next_bcc( |
| 353 | sample_dice_input, |
| 354 | &[0u8; HIDDEN_SIZE], |
| 355 | Some([0u8; 64]), |
| 356 | false, |
| 357 | &mut buffer_without_defer_retry, |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 358 | context.clone(), |
Shikha Panwar | f3acfd1 | 2024-05-28 15:48:13 +0000 | [diff] [blame] | 359 | ) |
| 360 | .unwrap(); |
| 361 | let bcc_handover3 = |
| 362 | diced_open_dice::bcc_handover_parse(&buffer_without_defer_retry).unwrap(); |
| 363 | |
| 364 | assert_ne!(bcc_handover1.cdi_seal(), bcc_handover2.cdi_seal()); |
| 365 | assert_eq!(bcc_handover1.cdi_seal(), bcc_handover3.cdi_seal()); |
| 366 | } |
Alice Wang | 87fbc4b | 2024-11-05 13:09:58 +0000 | [diff] [blame] | 367 | |
| 368 | #[test] |
| 369 | fn dice_derivation_with_different_algorithms_is_valid() { |
| 370 | let dice_artifacts = make_sample_bcc_and_cdis().unwrap(); |
| 371 | let bcc_handover0_bytes = to_bcc_handover(&dice_artifacts); |
| 372 | let vb_data = VerifiedBootData { debug_level: DebugLevel::Full, ..BASE_VB_DATA }; |
| 373 | let inputs = PartialInputs::new(&vb_data).unwrap(); |
| 374 | let mut buffer = [0; 4096]; |
| 375 | |
| 376 | inputs |
| 377 | .clone() |
| 378 | .write_next_bcc( |
| 379 | &bcc_handover0_bytes, |
| 380 | &[0u8; HIDDEN_SIZE], |
| 381 | Some([0u8; 64]), |
| 382 | true, |
| 383 | &mut buffer, |
| 384 | DiceContext { |
| 385 | authority_algorithm: KeyAlgorithm::Ed25519, |
| 386 | subject_algorithm: KeyAlgorithm::EcdsaP256, |
| 387 | }, |
| 388 | ) |
| 389 | .expect("Failed to derive Ed25519 -> EcdsaP256 BCC"); |
| 390 | let bcc_handover1 = diced_open_dice::bcc_handover_parse(&buffer).unwrap(); |
| 391 | let bcc_handover1_bytes = to_bcc_handover(&bcc_handover1); |
| 392 | buffer.fill(0); |
| 393 | |
| 394 | inputs |
| 395 | .clone() |
| 396 | .write_next_bcc( |
| 397 | &bcc_handover1_bytes, |
| 398 | &[0u8; HIDDEN_SIZE], |
| 399 | Some([0u8; 64]), |
| 400 | true, |
| 401 | &mut buffer, |
| 402 | DiceContext { |
| 403 | authority_algorithm: KeyAlgorithm::EcdsaP256, |
| 404 | subject_algorithm: KeyAlgorithm::EcdsaP384, |
| 405 | }, |
| 406 | ) |
| 407 | .expect("Failed to derive EcdsaP256 -> EcdsaP384 BCC"); |
| 408 | let bcc_handover2 = diced_open_dice::bcc_handover_parse(&buffer).unwrap(); |
| 409 | let bcc_handover2_bytes = to_bcc_handover(&bcc_handover2); |
| 410 | buffer.fill(0); |
| 411 | |
| 412 | inputs |
| 413 | .clone() |
| 414 | .write_next_bcc( |
| 415 | &bcc_handover2_bytes, |
| 416 | &[0u8; HIDDEN_SIZE], |
| 417 | Some([0u8; 64]), |
| 418 | true, |
| 419 | &mut buffer, |
| 420 | DiceContext { |
| 421 | authority_algorithm: KeyAlgorithm::EcdsaP384, |
| 422 | subject_algorithm: KeyAlgorithm::Ed25519, |
| 423 | }, |
| 424 | ) |
| 425 | .expect("Failed to derive EcdsaP384 -> Ed25519 BCC"); |
| 426 | let _bcc_handover3 = diced_open_dice::bcc_handover_parse(&buffer).unwrap(); |
| 427 | |
| 428 | // TODO(b/378813154): Check the DICE chain with `hwtrust` once the profile version |
| 429 | // is updated. |
| 430 | // The check cannot be done now because parsing the chain causes the following error: |
| 431 | // Invalid payload at index 3. Caused by: |
| 432 | // 0: opendice.example.p256 |
| 433 | // 1: unknown profile version |
| 434 | } |
| 435 | |
| 436 | fn to_bcc_handover(dice_artifacts: &dyn DiceArtifacts) -> Vec<u8> { |
| 437 | let dice_chain = cbor_util::deserialize::<Value>(dice_artifacts.bcc().unwrap()).unwrap(); |
| 438 | let bcc_handover = Value::Map(vec![ |
| 439 | (Value::Integer(1.into()), Value::Bytes(dice_artifacts.cdi_attest().to_vec())), |
| 440 | (Value::Integer(2.into()), Value::Bytes(dice_artifacts.cdi_seal().to_vec())), |
| 441 | (Value::Integer(3.into()), dice_chain), |
| 442 | ]); |
| 443 | cbor_util::serialize(&bcc_handover).unwrap() |
| 444 | } |
Alan Stokes | ddb988c | 2023-11-27 11:13:06 +0000 | [diff] [blame] | 445 | } |