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 | |
| 17 | use core::ffi::CStr; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 18 | use core::mem::size_of; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 19 | use dice::bcc::format_config_descriptor; |
| 20 | use dice::bcc::Handover; |
| 21 | use dice::hash; |
| 22 | use dice::ConfigType; |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 23 | use dice::DiceMode; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 24 | use dice::InputValues; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 25 | use pvmfw_avb::{DebugLevel, Digest, VerifiedBootData}; |
| 26 | |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 27 | fn to_dice_mode(debug_level: DebugLevel) -> DiceMode { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 28 | match debug_level { |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 29 | DebugLevel::None => DiceMode::kDiceModeNormal, |
| 30 | DebugLevel::Full => DiceMode::kDiceModeDebug, |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
| 34 | fn to_dice_hash(verified_boot_data: &VerifiedBootData) -> dice::Result<dice::Hash> { |
| 35 | let mut digests = [0u8; size_of::<Digest>() * 2]; |
| 36 | digests[..size_of::<Digest>()].copy_from_slice(&verified_boot_data.kernel_digest); |
| 37 | if let Some(initrd_digest) = verified_boot_data.initrd_digest { |
| 38 | digests[size_of::<Digest>()..].copy_from_slice(&initrd_digest); |
| 39 | } |
| 40 | hash(&digests) |
| 41 | } |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 42 | |
| 43 | /// Derive the VM-specific secrets and certificate through DICE. |
| 44 | pub fn derive_next_bcc( |
| 45 | bcc: &Handover, |
| 46 | next_bcc: &mut [u8], |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 47 | verified_boot_data: &VerifiedBootData, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 48 | authority: &[u8], |
| 49 | ) -> dice::Result<usize> { |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 50 | let code_hash = to_dice_hash(verified_boot_data)?; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 51 | let auth_hash = hash(authority)?; |
Alice Wang | 1f0add0 | 2023-01-23 16:22:53 +0000 | [diff] [blame] | 52 | let mode = to_dice_mode(verified_boot_data.debug_level); |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 53 | let component_name = CStr::from_bytes_with_nul(b"vm_entry\0").unwrap(); |
| 54 | let mut config_descriptor_buffer = [0; 128]; |
| 55 | let config_descriptor_size = format_config_descriptor( |
| 56 | &mut config_descriptor_buffer, |
| 57 | Some(component_name), |
| 58 | None, // component_version |
| 59 | false, // resettable |
| 60 | )?; |
| 61 | let config = &config_descriptor_buffer[..config_descriptor_size]; |
| 62 | let config = ConfigType::Descriptor(config); |
| 63 | |
| 64 | let input_values = InputValues::new( |
| 65 | &code_hash, |
| 66 | None, // code_descriptor |
| 67 | &config, |
| 68 | Some(&auth_hash), |
| 69 | None, // auth_descriptor |
| 70 | mode, |
| 71 | None, // TODO(b/249723852): Get salt from instance.img (virtio-blk) and/or TRNG. |
| 72 | ); |
| 73 | |
| 74 | bcc.main_flow(&input_values, next_bcc) |
| 75 | } |