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 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame^] | 17 | use crate::cstr; |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame] | 18 | use crate::helpers::flushed_zeroize; |
| 19 | use core::ffi::c_void; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 20 | use core::ffi::CStr; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 21 | use core::mem::size_of; |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame] | 22 | use core::slice; |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 23 | |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 24 | use diced_open_dice::{ |
| 25 | bcc_format_config_descriptor, hash, Config, DiceMode, Hash, InputValues, HIDDEN_SIZE, |
| 26 | }; |
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 | |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 36 | fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> diced_open_dice::Result<Hash> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 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 | |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 45 | pub struct PartialInputs { |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 46 | pub code_hash: Hash, |
| 47 | pub auth_hash: Hash, |
| 48 | pub mode: DiceMode, |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 49 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 50 | |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 51 | impl PartialInputs { |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 52 | pub fn new(data: &VerifiedBootData) -> diced_open_dice::Result<Self> { |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 53 | let code_hash = to_dice_hash(data)?; |
| 54 | let auth_hash = hash(data.public_key)?; |
| 55 | let mode = to_dice_mode(data.debug_level); |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 56 | |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 57 | Ok(Self { code_hash, auth_hash, mode }) |
| 58 | } |
| 59 | |
Alice Wang | 843d831 | 2023-02-15 09:47:06 +0000 | [diff] [blame] | 60 | pub fn into_input_values( |
| 61 | self, |
| 62 | salt: &[u8; HIDDEN_SIZE], |
| 63 | ) -> diced_open_dice::Result<InputValues> { |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 64 | let mut config_descriptor_buffer = [0; 128]; |
| 65 | let config_descriptor_size = bcc_format_config_descriptor( |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame^] | 66 | Some(cstr!("vm_entry")), |
Pierre-Clément Tosi | f58f3a3 | 2023-02-02 16:24:23 +0000 | [diff] [blame] | 67 | None, // component_version |
| 68 | false, // resettable |
| 69 | &mut config_descriptor_buffer, |
| 70 | )?; |
| 71 | let config = &config_descriptor_buffer[..config_descriptor_size]; |
| 72 | |
| 73 | Ok(InputValues::new( |
| 74 | self.code_hash, |
| 75 | Config::Descriptor(config), |
| 76 | self.auth_hash, |
| 77 | self.mode, |
| 78 | *salt, |
| 79 | )) |
| 80 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 81 | } |
Pierre-Clément Tosi | bec8466 | 2023-01-04 14:25:33 +0000 | [diff] [blame] | 82 | |
| 83 | /// Flushes data caches over the provided address range. |
| 84 | /// |
| 85 | /// # Safety |
| 86 | /// |
| 87 | /// The provided address and size must be to a valid address range (typically on the stack, .bss, |
| 88 | /// .data, or provided BCC). |
| 89 | #[no_mangle] |
| 90 | unsafe extern "C" fn DiceClearMemory(_ctx: *mut c_void, size: usize, addr: *mut c_void) { |
| 91 | // SAFETY - We must trust that the slice will be valid arrays/variables on the C code stack. |
| 92 | let region = unsafe { slice::from_raw_parts_mut(addr as *mut u8, size) }; |
| 93 | flushed_zeroize(region) |
| 94 | } |