blob: aa40bb867800545a8df72684db82e13616201f63 [file] [log] [blame]
Alice Wang2925b0a2023-01-19 10:44:24 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Utility methods used by API tests.
18
19use anyhow::Result;
20use avb_bindgen::{
21 avb_footer_validate_and_byteswap, avb_vbmeta_image_header_to_host_byte_order, AvbFooter,
22 AvbVBMetaImageHeader,
23};
24use pvmfw_avb::{verify_payload, AvbSlotVerifyError};
25use std::{
26 fs,
27 mem::{size_of, transmute, MaybeUninit},
28};
29
30const MICRODROID_KERNEL_IMG_PATH: &str = "microdroid_kernel";
31const INITRD_NORMAL_IMG_PATH: &str = "microdroid_initrd_normal.img";
32const INITRD_DEBUG_IMG_PATH: &str = "microdroid_initrd_debuggable.img";
33const PUBLIC_KEY_RSA4096_PATH: &str = "data/testkey_rsa4096_pub.bin";
34
35pub const PUBLIC_KEY_RSA2048_PATH: &str = "data/testkey_rsa2048_pub.bin";
36
37pub fn assert_payload_verification_with_initrd_eq(
38 kernel: &[u8],
39 initrd: &[u8],
40 trusted_public_key: &[u8],
41 expected_result: Result<(), AvbSlotVerifyError>,
42) -> Result<()> {
43 assert_payload_verification_eq(kernel, Some(initrd), trusted_public_key, expected_result)
44}
45
46pub fn assert_payload_verification_eq(
47 kernel: &[u8],
48 initrd: Option<&[u8]>,
49 trusted_public_key: &[u8],
50 expected_result: Result<(), AvbSlotVerifyError>,
51) -> Result<()> {
52 assert_eq!(expected_result, verify_payload(kernel, initrd, trusted_public_key));
53 Ok(())
54}
55
56pub fn load_latest_signed_kernel() -> Result<Vec<u8>> {
57 Ok(fs::read(MICRODROID_KERNEL_IMG_PATH)?)
58}
59
60pub fn load_latest_initrd_normal() -> Result<Vec<u8>> {
61 Ok(fs::read(INITRD_NORMAL_IMG_PATH)?)
62}
63
64pub fn load_latest_initrd_debug() -> Result<Vec<u8>> {
65 Ok(fs::read(INITRD_DEBUG_IMG_PATH)?)
66}
67
68pub fn load_trusted_public_key() -> Result<Vec<u8>> {
69 Ok(fs::read(PUBLIC_KEY_RSA4096_PATH)?)
70}
71
72pub fn extract_avb_footer(kernel: &[u8]) -> Result<AvbFooter> {
73 let footer_start = kernel.len() - size_of::<AvbFooter>();
74 // SAFETY: The slice is the same size as the struct which only contains simple data types.
75 let mut footer = unsafe {
76 transmute::<[u8; size_of::<AvbFooter>()], AvbFooter>(kernel[footer_start..].try_into()?)
77 };
78 // SAFETY: The function updates the struct in-place.
79 unsafe {
80 avb_footer_validate_and_byteswap(&footer, &mut footer);
81 }
82 Ok(footer)
83}
84
85pub fn extract_vbmeta_header(kernel: &[u8], footer: &AvbFooter) -> Result<AvbVBMetaImageHeader> {
86 let vbmeta_offset: usize = footer.vbmeta_offset.try_into()?;
87 let vbmeta_size: usize = footer.vbmeta_size.try_into()?;
88 let vbmeta_src = &kernel[vbmeta_offset..(vbmeta_offset + vbmeta_size)];
89 // SAFETY: The latest kernel has a valid VBMeta header at the position specified in footer.
90 let vbmeta_header = unsafe {
91 let mut header = MaybeUninit::uninit();
92 let src = vbmeta_src.as_ptr() as *const _ as *const AvbVBMetaImageHeader;
93 avb_vbmeta_image_header_to_host_byte_order(src, header.as_mut_ptr());
94 header.assume_init()
95 };
96 Ok(vbmeta_header)
97}