blob: 42cc802f290dc9238ffb2725b560a16e87842729 [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
17use core::ffi::CStr;
Alice Wang1f0add02023-01-23 16:22:53 +000018use core::mem::size_of;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000019use dice::bcc::format_config_descriptor;
20use dice::bcc::Handover;
21use dice::hash;
Alice Wang5aeed332023-02-02 09:42:21 +000022use dice::Config;
Alice Wang31226132023-01-31 12:44:39 +000023use dice::DiceMode;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000024use dice::InputValues;
Alice Wanga7773662023-02-03 09:37:17 +000025use dice::HIDDEN_SIZE;
Alice Wang1f0add02023-01-23 16:22:53 +000026use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData};
27
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
35fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> dice::Result<dice::Hash> {
36 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
44/// Derive the VM-specific secrets and certificate through DICE.
45pub fn derive_next_bcc(
46 bcc: &Handover,
47 next_bcc: &mut [u8],
Alice Wang1f0add02023-01-23 16:22:53 +000048 verified_boot_data: &VerifiedBootData,
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000049 authority: &[u8],
50) -> dice::Result<usize> {
Alice Wang1f0add02023-01-23 16:22:53 +000051 let code_hash = to_dice_hash(verified_boot_data)?;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000052 let auth_hash = hash(authority)?;
Alice Wang1f0add02023-01-23 16:22:53 +000053 let mode = to_dice_mode(verified_boot_data.debug_level);
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000054 let component_name = CStr::from_bytes_with_nul(b"vm_entry\0").unwrap();
55 let mut config_descriptor_buffer = [0; 128];
56 let config_descriptor_size = format_config_descriptor(
57 &mut config_descriptor_buffer,
58 Some(component_name),
59 None, // component_version
60 false, // resettable
61 )?;
62 let config = &config_descriptor_buffer[..config_descriptor_size];
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000063
64 let input_values = InputValues::new(
Alice Wanga7773662023-02-03 09:37:17 +000065 code_hash,
Alice Wang5aeed332023-02-02 09:42:21 +000066 Config::Descriptor(config),
Alice Wanga7773662023-02-03 09:37:17 +000067 auth_hash,
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000068 mode,
Alice Wanga7773662023-02-03 09:37:17 +000069 [0u8; HIDDEN_SIZE], // TODO(b/249723852): Get salt from instance.img (virtio-blk) and/or TRNG.
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000070 );
71
72 bcc.main_flow(&input_values, next_bcc)
73}