blob: 9c5f59a4560368a33cd7717a002935539ac7979f [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 crate::helpers::flushed_zeroize;
18use core::ffi::c_void;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000019use core::ffi::CStr;
Alice Wang1f0add02023-01-23 16:22:53 +000020use core::mem::size_of;
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000021use core::slice;
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000022
Alice Wang5aeed332023-02-02 09:42:21 +000023use dice::Config;
Alice Wang31226132023-01-31 12:44:39 +000024use dice::DiceMode;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000025use dice::InputValues;
Alice Wangcb9d2f92023-02-06 10:29:00 +000026use diced_open_dice::{bcc_format_config_descriptor, hash, HIDDEN_SIZE};
Alice Wang1f0add02023-01-23 16:22:53 +000027use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData};
28
Alice Wang31226132023-01-31 12:44:39 +000029fn to_dice_mode(debug_level: DebugLevel) -> DiceMode {
Alice Wang1f0add02023-01-23 16:22:53 +000030 match debug_level {
Alice Wang31226132023-01-31 12:44:39 +000031 DebugLevel::None => DiceMode::kDiceModeNormal,
32 DebugLevel::Full => DiceMode::kDiceModeDebug,
Alice Wang1f0add02023-01-23 16:22:53 +000033 }
34}
35
36fn 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 Tosi4f4f5eb2022-12-08 14:31:42 +000044
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000045pub struct PartialInputs {
46 code_hash: dice::Hash,
47 auth_hash: dice::Hash,
48 mode: DiceMode,
49}
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000050
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000051impl PartialInputs {
52 pub fn new(data: &VerifiedBootData) -> dice::Result<Self> {
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 Tosi4f4f5eb2022-12-08 14:31:42 +000056
Pierre-Clément Tosif58f3a32023-02-02 16:24:23 +000057 Ok(Self { code_hash, auth_hash, mode })
58 }
59
60 pub fn into_input_values(self, salt: &[u8; HIDDEN_SIZE]) -> dice::Result<InputValues> {
61 let component_name = CStr::from_bytes_with_nul(b"vm_entry\0").unwrap();
62 let mut config_descriptor_buffer = [0; 128];
63 let config_descriptor_size = bcc_format_config_descriptor(
64 Some(component_name),
65 None, // component_version
66 false, // resettable
67 &mut config_descriptor_buffer,
68 )?;
69 let config = &config_descriptor_buffer[..config_descriptor_size];
70
71 Ok(InputValues::new(
72 self.code_hash,
73 Config::Descriptor(config),
74 self.auth_hash,
75 self.mode,
76 *salt,
77 ))
78 }
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000079}
Pierre-Clément Tosibec84662023-01-04 14:25:33 +000080
81/// Flushes data caches over the provided address range.
82///
83/// # Safety
84///
85/// The provided address and size must be to a valid address range (typically on the stack, .bss,
86/// .data, or provided BCC).
87#[no_mangle]
88unsafe extern "C" fn DiceClearMemory(_ctx: *mut c_void, size: usize, addr: *mut c_void) {
89 // SAFETY - We must trust that the slice will be valid arrays/variables on the C code stack.
90 let region = unsafe { slice::from_raw_parts_mut(addr as *mut u8, size) };
91 flushed_zeroize(region)
92}