blob: cc31f34a2d6cdb0503818e7d14844e8724590e1c [file] [log] [blame]
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +00001// 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 Tosibec84662023-01-04 14:25:33 +000017use core::ffi::c_void;
Alice Wang1f0add02023-01-23 16:22:53 +000018use core::mem::size_of;
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000019use core::slice;
Alice Wang843d8312023-02-15 09:47:06 +000020use diced_open_dice::{
Alan Stokesc6e92462023-08-22 14:43:17 +010021 bcc_format_config_descriptor, bcc_handover_main_flow, hash, Config, DiceConfigValues, DiceMode,
22 Hash, InputValues, HIDDEN_SIZE,
Alice Wang843d8312023-02-15 09:47:06 +000023};
Alice Wang1f0add02023-01-23 16:22:53 +000024use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData};
Alice Wanga3971062023-06-13 11:48:53 +000025use vmbase::cstr;
26use vmbase::memory::flushed_zeroize;
Alice Wang1f0add02023-01-23 16:22:53 +000027
Alice Wang31226132023-01-31 12:44:39 +000028fn to_dice_mode(debug_level: DebugLevel) -> DiceMode {
Alice Wang1f0add02023-01-23 16:22:53 +000029 match debug_level {
Alice Wang31226132023-01-31 12:44:39 +000030 DebugLevel::None => DiceMode::kDiceModeNormal,
31 DebugLevel::Full => DiceMode::kDiceModeDebug,
Alice Wang1f0add02023-01-23 16:22:53 +000032 }
33}
34
Alice Wang843d8312023-02-15 09:47:06 +000035fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> diced_open_dice::Result<Hash> {
Alice Wang1f0add02023-01-23 16:22:53 +000036 let mut digests = [0u8; size_of::<Digest>() * 2];
37 digests[..size_of::<Digest>()].copy_from_slice(&verified_boot_data.kernel_digest);
38 if let Some(initrd_digest) = verified_boot_data.initrd_digest {
39 digests[size_of::<Digest>()..].copy_from_slice(&initrd_digest);
40 }
41 hash(&digests)
42}
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000043
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000044pub struct PartialInputs {
Pierre-Clément Tosi1cc5eb72023-02-02 11:09:18 +000045 pub code_hash: Hash,
46 pub auth_hash: Hash,
47 pub mode: DiceMode,
Shikha Panwara26f16a2023-09-27 09:39:00 +000048 pub security_version: u64,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000049}
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000050
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000051impl PartialInputs {
Alice Wang843d8312023-02-15 09:47:06 +000052 pub fn new(data: &VerifiedBootData) -> diced_open_dice::Result<Self> {
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000053 let code_hash = to_dice_hash(data)?;
54 let auth_hash = hash(data.public_key)?;
55 let mode = to_dice_mode(data.debug_level);
Shikha Panwara26f16a2023-09-27 09:39:00 +000056 // We use rollback_index from vbmeta as the security_version field in dice certificate.
57 let security_version = data.rollback_index;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000058
Shikha Panwara26f16a2023-09-27 09:39:00 +000059 Ok(Self { code_hash, auth_hash, mode, security_version })
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000060 }
61
Alan Stokesc4354b82023-05-04 16:06:52 +010062 pub fn write_next_bcc(
Alice Wang843d8312023-02-15 09:47:06 +000063 self,
Alan Stokesc4354b82023-05-04 16:06:52 +010064 current_bcc_handover: &[u8],
Alice Wang843d8312023-02-15 09:47:06 +000065 salt: &[u8; HIDDEN_SIZE],
Alan Stokesc4354b82023-05-04 16:06:52 +010066 next_bcc: &mut [u8],
67 ) -> diced_open_dice::Result<()> {
68 let mut config_descriptor_buffer = [0; 128];
Shikha Panwara26f16a2023-09-27 09:39:00 +000069 let config_values = DiceConfigValues {
70 component_name: Some(cstr!("vm_entry")),
71 security_version: if cfg!(llpvm_changes) { Some(self.security_version) } else { None },
72 ..Default::default()
73 };
74
Alan Stokesc6e92462023-08-22 14:43:17 +010075 let config_descriptor_size =
76 bcc_format_config_descriptor(&config_values, &mut config_descriptor_buffer)?;
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000077 let config = &config_descriptor_buffer[..config_descriptor_size];
78
Alan Stokesc4354b82023-05-04 16:06:52 +010079 let dice_inputs = InputValues::new(
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000080 self.code_hash,
81 Config::Descriptor(config),
82 self.auth_hash,
83 self.mode,
84 *salt,
Alan Stokesc4354b82023-05-04 16:06:52 +010085 );
86 let _ = bcc_handover_main_flow(current_bcc_handover, &dice_inputs, next_bcc)?;
87 Ok(())
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000088 }
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000089}
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000090
91/// Flushes data caches over the provided address range.
92///
93/// # Safety
94///
95/// The provided address and size must be to a valid address range (typically on the stack, .bss,
96/// .data, or provided BCC).
97#[no_mangle]
98unsafe extern "C" fn DiceClearMemory(_ctx: *mut c_void, size: usize, addr: *mut c_void) {
Andrew Walbran20bb4e42023-07-07 13:55:55 +010099 // SAFETY: We must trust that the slice will be valid arrays/variables on the C code stack.
Pierre-Clément Tosibec84662023-01-04 14:25:33 +0000100 let region = unsafe { slice::from_raw_parts_mut(addr as *mut u8, size) };
101 flushed_zeroize(region)
102}