blob: 4e303ac65ece8e457b0d2921414c23190f718546 [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
Jiyong Parkb87f3302023-03-21 10:03:11 +090017use crate::cstr;
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000018use crate::helpers::flushed_zeroize;
19use core::ffi::c_void;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000020use core::ffi::CStr;
Alice Wang1f0add02023-01-23 16:22:53 +000021use core::mem::size_of;
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000022use core::slice;
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000023
Alice Wang843d8312023-02-15 09:47:06 +000024use diced_open_dice::{
Alan Stokesc4354b82023-05-04 16:06:52 +010025 bcc_format_config_descriptor, bcc_handover_main_flow, hash, Config, DiceMode, Hash,
26 InputValues, HIDDEN_SIZE,
Alice Wang843d8312023-02-15 09:47:06 +000027};
Alice Wang1f0add02023-01-23 16:22:53 +000028use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData};
29
Alice Wang31226132023-01-31 12:44:39 +000030fn to_dice_mode(debug_level: DebugLevel) -> DiceMode {
Alice Wang1f0add02023-01-23 16:22:53 +000031 match debug_level {
Alice Wang31226132023-01-31 12:44:39 +000032 DebugLevel::None => DiceMode::kDiceModeNormal,
33 DebugLevel::Full => DiceMode::kDiceModeDebug,
Alice Wang1f0add02023-01-23 16:22:53 +000034 }
35}
36
Alice Wang843d8312023-02-15 09:47:06 +000037fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> diced_open_dice::Result<Hash> {
Alice Wang1f0add02023-01-23 16:22:53 +000038 let mut digests = [0u8; size_of::<Digest>() * 2];
39 digests[..size_of::<Digest>()].copy_from_slice(&verified_boot_data.kernel_digest);
40 if let Some(initrd_digest) = verified_boot_data.initrd_digest {
41 digests[size_of::<Digest>()..].copy_from_slice(&initrd_digest);
42 }
43 hash(&digests)
44}
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000045
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000046pub struct PartialInputs {
Pierre-Clément Tosi1cc5eb72023-02-02 11:09:18 +000047 pub code_hash: Hash,
48 pub auth_hash: Hash,
49 pub mode: DiceMode,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000050}
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000051
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000052impl PartialInputs {
Alice Wang843d8312023-02-15 09:47:06 +000053 pub fn new(data: &VerifiedBootData) -> diced_open_dice::Result<Self> {
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000054 let code_hash = to_dice_hash(data)?;
55 let auth_hash = hash(data.public_key)?;
56 let mode = to_dice_mode(data.debug_level);
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000057
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000058 Ok(Self { code_hash, auth_hash, mode })
59 }
60
Alan Stokesc4354b82023-05-04 16:06:52 +010061 pub fn write_next_bcc(
Alice Wang843d8312023-02-15 09:47:06 +000062 self,
Alan Stokesc4354b82023-05-04 16:06:52 +010063 current_bcc_handover: &[u8],
Alice Wang843d8312023-02-15 09:47:06 +000064 salt: &[u8; HIDDEN_SIZE],
Alan Stokesc4354b82023-05-04 16:06:52 +010065 next_bcc: &mut [u8],
66 ) -> diced_open_dice::Result<()> {
67 let mut config_descriptor_buffer = [0; 128];
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000068 let config_descriptor_size = bcc_format_config_descriptor(
Jiyong Parkb87f3302023-03-21 10:03:11 +090069 Some(cstr!("vm_entry")),
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000070 None, // component_version
71 false, // resettable
Alan Stokesc4354b82023-05-04 16:06:52 +010072 &mut config_descriptor_buffer,
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000073 )?;
74 let config = &config_descriptor_buffer[..config_descriptor_size];
75
Alan Stokesc4354b82023-05-04 16:06:52 +010076 let dice_inputs = InputValues::new(
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000077 self.code_hash,
78 Config::Descriptor(config),
79 self.auth_hash,
80 self.mode,
81 *salt,
Alan Stokesc4354b82023-05-04 16:06:52 +010082 );
83 let _ = bcc_handover_main_flow(current_bcc_handover, &dice_inputs, next_bcc)?;
84 Ok(())
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000085 }
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000086}
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000087
88/// Flushes data caches over the provided address range.
89///
90/// # Safety
91///
92/// The provided address and size must be to a valid address range (typically on the stack, .bss,
93/// .data, or provided BCC).
94#[no_mangle]
95unsafe extern "C" fn DiceClearMemory(_ctx: *mut c_void, size: usize, addr: *mut c_void) {
96 // SAFETY - We must trust that the slice will be valid arrays/variables on the C code stack.
97 let region = unsafe { slice::from_raw_parts_mut(addr as *mut u8, size) };
98 flushed_zeroize(region)
99}