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. |
| 16 | |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame^] | 17 | use crate::helpers::flushed_zeroize; |
| 18 | use core::ffi::c_void; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 19 | use core::ffi::CStr; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 20 | use core::mem::size_of; |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame^] | 21 | use core::slice; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 22 | use dice::bcc::Handover; |
Alice Wang | 5aeed33 | 2023-02-02 09:42:21 +0000 | [diff] [blame] | 23 | use dice::Config; |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 24 | use dice::DiceMode; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 25 | use dice::InputValues; |
Alice Wang | cb9d2f9 | 2023-02-06 10:29:00 +0000 | [diff] [blame] | 26 | use diced_open_dice::{bcc_format_config_descriptor, hash, HIDDEN_SIZE}; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 27 | use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData}; |
| 28 | |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 29 | fn to_dice_mode(debug_level: DebugLevel) -> DiceMode { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 30 | match debug_level { |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 31 | DebugLevel::None => DiceMode::kDiceModeNormal, |
| 32 | DebugLevel::Full => DiceMode::kDiceModeDebug, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> dice::Result<dice::Hash> { |
| 37 | let mut digests = [0u8; size_of::<Digest>() * 2]; |
| 38 | digests[..size_of::<Digest>()].copy_from_slice(&verified_boot_data.kernel_digest); |
| 39 | if let Some(initrd_digest) = verified_boot_data.initrd_digest { |
| 40 | digests[size_of::<Digest>()..].copy_from_slice(&initrd_digest); |
| 41 | } |
| 42 | hash(&digests) |
| 43 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 44 | |
| 45 | /// Derive the VM-specific secrets and certificate through DICE. |
| 46 | pub fn derive_next_bcc( |
| 47 | bcc: &Handover, |
| 48 | next_bcc: &mut [u8], |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 49 | verified_boot_data: &VerifiedBootData, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 50 | authority: &[u8], |
| 51 | ) -> dice::Result<usize> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 52 | let code_hash = to_dice_hash(verified_boot_data)?; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 53 | let auth_hash = hash(authority)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 54 | let mode = to_dice_mode(verified_boot_data.debug_level); |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 55 | let component_name = CStr::from_bytes_with_nul(b"vm_entry\0").unwrap(); |
| 56 | let mut config_descriptor_buffer = [0; 128]; |
Alice Wang | 54511fd | 2023-02-07 08:41:15 +0000 | [diff] [blame] | 57 | let config_descriptor_size = bcc_format_config_descriptor( |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 58 | Some(component_name), |
| 59 | None, // component_version |
| 60 | false, // resettable |
Alice Wang | 54511fd | 2023-02-07 08:41:15 +0000 | [diff] [blame] | 61 | &mut config_descriptor_buffer, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 62 | )?; |
| 63 | let config = &config_descriptor_buffer[..config_descriptor_size]; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 64 | |
| 65 | let input_values = InputValues::new( |
Alice Wang | a777366 | 2023-02-03 09:37:17 +0000 | [diff] [blame] | 66 | code_hash, |
Alice Wang | 5aeed33 | 2023-02-02 09:42:21 +0000 | [diff] [blame] | 67 | Config::Descriptor(config), |
Alice Wang | a777366 | 2023-02-03 09:37:17 +0000 | [diff] [blame] | 68 | auth_hash, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 69 | mode, |
Alice Wang | a777366 | 2023-02-03 09:37:17 +0000 | [diff] [blame] | 70 | [0u8; HIDDEN_SIZE], // TODO(b/249723852): Get salt from instance.img (virtio-blk) and/or TRNG. |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 71 | ); |
| 72 | |
| 73 | bcc.main_flow(&input_values, next_bcc) |
| 74 | } |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame^] | 75 | |
| 76 | /// Flushes data caches over the provided address range. |
| 77 | /// |
| 78 | /// # Safety |
| 79 | /// |
| 80 | /// The provided address and size must be to a valid address range (typically on the stack, .bss, |
| 81 | /// .data, or provided BCC). |
| 82 | #[no_mangle] |
| 83 | unsafe extern "C" fn DiceClearMemory(_ctx: *mut c_void, size: usize, addr: *mut c_void) { |
| 84 | // SAFETY - We must trust that the slice will be valid arrays/variables on the C code stack. |
| 85 | let region = unsafe { slice::from_raw_parts_mut(addr as *mut u8, size) }; |
| 86 | flushed_zeroize(region) |
| 87 | } |