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; |
| 18 | |
| 19 | use dice::bcc::format_config_descriptor; |
| 20 | use dice::bcc::Handover; |
| 21 | use dice::hash; |
| 22 | use dice::ConfigType; |
| 23 | use dice::InputValues; |
| 24 | |
| 25 | /// Derive the VM-specific secrets and certificate through DICE. |
| 26 | pub fn derive_next_bcc( |
| 27 | bcc: &Handover, |
| 28 | next_bcc: &mut [u8], |
| 29 | code: &[u8], |
| 30 | debug_mode: bool, |
| 31 | authority: &[u8], |
| 32 | ) -> dice::Result<usize> { |
| 33 | let code_hash = hash(code)?; |
| 34 | let auth_hash = hash(authority)?; |
| 35 | let mode = if debug_mode { dice::Mode::Debug } else { dice::Mode::Normal }; |
| 36 | let component_name = CStr::from_bytes_with_nul(b"vm_entry\0").unwrap(); |
| 37 | let mut config_descriptor_buffer = [0; 128]; |
| 38 | let config_descriptor_size = format_config_descriptor( |
| 39 | &mut config_descriptor_buffer, |
| 40 | Some(component_name), |
| 41 | None, // component_version |
| 42 | false, // resettable |
| 43 | )?; |
| 44 | let config = &config_descriptor_buffer[..config_descriptor_size]; |
| 45 | let config = ConfigType::Descriptor(config); |
| 46 | |
| 47 | let input_values = InputValues::new( |
| 48 | &code_hash, |
| 49 | None, // code_descriptor |
| 50 | &config, |
| 51 | Some(&auth_hash), |
| 52 | None, // auth_descriptor |
| 53 | mode, |
| 54 | None, // TODO(b/249723852): Get salt from instance.img (virtio-blk) and/or TRNG. |
| 55 | ); |
| 56 | |
| 57 | bcc.main_flow(&input_values, next_bcc) |
| 58 | } |