David Brown | c8d6201 | 2021-10-27 15:03:48 -0600 | [diff] [blame] | 1 | // Copyright (c) 2017-2021 Linaro LTD |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 2 | // Copyright (c) 2017-2019 JUUL Labs |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 3 | // Copyright (c) 2019-2023 Arm Limited |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 4 | // |
| 5 | // SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | //! Interface wrappers to C API entering to the bootloader |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 8 | |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 9 | use crate::area::AreaDesc; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 10 | use simflash::SimMultiFlash; |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 11 | use crate::api; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 12 | |
Antonio de Angelis | 65eb35c | 2022-11-22 14:11:47 +0000 | [diff] [blame] | 13 | #[allow(unused)] |
| 14 | use std::sync::Once; |
| 15 | |
David Brown | 5d15513 | 2024-06-10 14:07:32 -0600 | [diff] [blame] | 16 | use std::borrow::Borrow; |
| 17 | |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 18 | /// The result of an invocation of `boot_go`. This is intentionally opaque so that we can provide |
| 19 | /// accessors for everything we need from this. |
| 20 | #[derive(Debug)] |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 21 | pub enum BootGoResult { |
| 22 | /// This run was stopped by the flash simulation mechanism. |
| 23 | Stopped, |
| 24 | /// The bootloader ran to completion with the following data. |
| 25 | Normal { |
| 26 | result: i32, |
| 27 | asserts: u8, |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 28 | |
| 29 | resp: api::BootRsp, |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 30 | }, |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | impl BootGoResult { |
| 34 | /// Was this run interrupted. |
| 35 | pub fn interrupted(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 36 | matches!(self, BootGoResult::Stopped) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /// Was this boot run successful (returned 0) |
| 40 | pub fn success(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 41 | matches!(self, BootGoResult::Normal { result: 0, .. }) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /// Success, but also no asserts. |
| 45 | pub fn success_no_asserts(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 46 | matches!(self, BootGoResult::Normal { |
| 47 | result: 0, |
| 48 | asserts: 0, |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 49 | .. |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 50 | }) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 51 | } |
| 52 | |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 53 | /// Get the asserts count. An interrupted run will be considered to have no asserts. |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 54 | pub fn asserts(&self) -> u8 { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 55 | match self { |
| 56 | BootGoResult::Normal { asserts, .. } => *asserts, |
| 57 | _ => 0, |
| 58 | } |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 59 | } |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 60 | |
| 61 | /// Retrieve the 'resp' field that is filled in. |
| 62 | pub fn resp(&self) -> Option<&api::BootRsp> { |
| 63 | match self { |
| 64 | BootGoResult::Normal { resp, .. } => Some(resp), |
| 65 | _ => None, |
| 66 | } |
| 67 | } |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 68 | } |
| 69 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 70 | /// Invoke the bootloader on this flash device. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 71 | pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 72 | counter: Option<&mut i32>, image_index: Option<i32>, |
| 73 | catch_asserts: bool) -> BootGoResult { |
Antonio de Angelis | 65eb35c | 2022-11-22 14:11:47 +0000 | [diff] [blame] | 74 | init_crypto(); |
| 75 | |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 76 | for (&dev_id, flash) in multiflash.iter_mut() { |
| 77 | api::set_flash(dev_id, flash); |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 78 | } |
| 79 | let mut sim_ctx = api::CSimContext { |
| 80 | flash_counter: match counter { |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 81 | None => 0, |
| 82 | Some(ref c) => **c as libc::c_int |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 83 | }, |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 84 | c_catch_asserts: if catch_asserts { 1 } else { 0 }, |
David Brown | a706317 | 2024-06-25 10:00:31 -0600 | [diff] [blame] | 85 | .. Default::default() |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 86 | }; |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 87 | let mut rsp = api::BootRsp { |
| 88 | br_hdr: std::ptr::null(), |
| 89 | flash_dev_id: 0, |
| 90 | image_off: 0, |
| 91 | }; |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 92 | let result: i32 = unsafe { |
David Brown | 5d15513 | 2024-06-10 14:07:32 -0600 | [diff] [blame] | 93 | let adesc = areadesc.get_c(); |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 94 | match image_index { |
| 95 | None => raw::invoke_boot_go(&mut sim_ctx as *mut _, |
David Brown | 5d15513 | 2024-06-10 14:07:32 -0600 | [diff] [blame] | 96 | adesc.borrow() as *const _, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 97 | &mut rsp as *mut _, -1) as i32, |
| 98 | Some(i) => raw::invoke_boot_go(&mut sim_ctx as *mut _, |
David Brown | 5d15513 | 2024-06-10 14:07:32 -0600 | [diff] [blame] | 99 | adesc.borrow() as *const _, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 100 | &mut rsp as *mut _, |
| 101 | i as i32) as i32 |
| 102 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 103 | }; |
| 104 | let asserts = sim_ctx.c_asserts; |
David Brown | 8608c53 | 2021-03-10 05:18:11 -0700 | [diff] [blame] | 105 | if let Some(c) = counter { |
| 106 | *c = sim_ctx.flash_counter; |
| 107 | } |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 108 | for &dev_id in multiflash.keys() { |
| 109 | api::clear_flash(dev_id); |
| 110 | } |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 111 | if result == -0x13579 { |
| 112 | BootGoResult::Stopped |
| 113 | } else { |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 114 | BootGoResult::Normal { result, asserts, resp: rsp } |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 115 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 118 | pub fn boot_trailer_sz(align: u32) -> u32 { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 119 | unsafe { raw::boot_trailer_sz(align) } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 122 | pub fn boot_status_sz(align: u32) -> u32 { |
| 123 | unsafe { raw::boot_status_sz(align) } |
| 124 | } |
| 125 | |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 126 | pub fn boot_magic_sz() -> usize { |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 127 | unsafe { raw::boot_magic_sz() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | pub fn boot_max_align() -> usize { |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 131 | unsafe { raw::boot_max_align() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 132 | } |
| 133 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 134 | pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> { |
| 135 | unsafe { |
| 136 | let mut encbuf: [u8; 256] = [0; 256]; |
| 137 | if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32, |
| 138 | seckey.as_ptr(), seckey.len() as u32, |
| 139 | encbuf.as_mut_ptr()) == 0 { |
| 140 | return Ok(encbuf); |
| 141 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 142 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 146 | pub fn kw_encrypt(kek: &[u8], seckey: &[u8], keylen: u32) -> Result<Vec<u8>, &'static str> { |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 147 | unsafe { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 148 | let mut encbuf = vec![0u8; 24]; |
| 149 | if keylen == 32 { |
| 150 | encbuf = vec![0u8; 40]; |
| 151 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 152 | if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 { |
| 153 | return Ok(encbuf); |
| 154 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 155 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 159 | pub fn set_security_counter(image_index: u32, security_counter_value: u32) { |
| 160 | api::sim_set_nv_counter_for_image(image_index, security_counter_value); |
| 161 | } |
| 162 | |
| 163 | pub fn get_security_counter(image_index: u32) -> u32 { |
| 164 | let mut counter_val: u32 = 0; |
| 165 | api::sim_get_nv_counter_for_image(image_index, &mut counter_val as *mut u32); |
| 166 | return counter_val; |
| 167 | } |
| 168 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 169 | mod raw { |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 170 | use crate::area::CAreaDesc; |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 171 | use crate::api::{BootRsp, CSimContext}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 172 | |
| 173 | extern "C" { |
| 174 | // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to |
| 175 | // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798 |
| 176 | // for information and tracking. |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 177 | pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame] | 178 | rsp: *mut BootRsp, image_index: libc::c_int) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 179 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 180 | pub fn boot_trailer_sz(min_write_sz: u32) -> u32; |
| 181 | pub fn boot_status_sz(min_write_sz: u32) -> u32; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 182 | |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 183 | pub fn boot_magic_sz() -> u32; |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 184 | pub fn boot_max_align() -> u32; |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 185 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 186 | pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint, |
| 187 | seckey: *const u8, seckey_len: libc::c_uint, |
| 188 | encbuf: *mut u8) -> libc::c_int; |
| 189 | |
| 190 | pub fn kw_encrypt_(kek: *const u8, seckey: *const u8, |
| 191 | encbuf: *mut u8) -> libc::c_int; |
Antonio de Angelis | 65eb35c | 2022-11-22 14:11:47 +0000 | [diff] [blame] | 192 | |
| 193 | #[allow(unused)] |
| 194 | pub fn psa_crypto_init() -> u32; |
| 195 | |
| 196 | #[allow(unused)] |
| 197 | pub fn mbedtls_test_enable_insecure_external_rng(); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
Antonio de Angelis | 65eb35c | 2022-11-22 14:11:47 +0000 | [diff] [blame] | 200 | |
| 201 | #[allow(unused)] |
| 202 | static PSA_INIT_SYNC: Once = Once::new(); |
| 203 | |
| 204 | #[allow(unused)] |
| 205 | static MBEDTLS_EXTERNAL_RNG_ENABLE_SYNC: Once = Once::new(); |
| 206 | |
| 207 | #[cfg(feature = "psa-crypto-api")] |
| 208 | fn init_crypto() { |
| 209 | PSA_INIT_SYNC.call_once(|| { |
| 210 | assert_eq!(unsafe { raw::psa_crypto_init() }, 0); |
| 211 | }); |
| 212 | |
| 213 | /* The PSA APIs require properly initialisation of the entropy subsystem |
| 214 | * The configuration adds the option MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG when the |
| 215 | * psa-crypto-api feature is enabled. As a result the tests use the implementation |
| 216 | * of the test external rng that needs to be initialised before being able to use it |
| 217 | */ |
| 218 | MBEDTLS_EXTERNAL_RNG_ENABLE_SYNC.call_once(|| { |
| 219 | unsafe { raw::mbedtls_test_enable_insecure_external_rng() } |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | #[cfg(not(feature = "psa-crypto-api"))] |
| 224 | fn init_crypto() { |
| 225 | // When the feature is not enabled, the init is just empty |
| 226 | } |