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-2020 JUUL Labs |
Roland Mikhel | 75c7c31 | 2023-02-23 15:39:14 +0100 | [diff] [blame] | 3 | // Copyright (c) 2021-2023 Arm Limited |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 4 | // |
| 5 | // SPDX-License-Identifier: Apache-2.0 |
| 6 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 7 | //! TLV Support |
| 8 | //! |
| 9 | //! mcuboot images are followed immediately by a list of TLV items that contain integrity |
| 10 | //! information about the image. Their generation is made a little complicated because the size of |
| 11 | //! the TLV block is in the image header, which is included in the hash. Since some signatures can |
| 12 | //! vary in size, we just make them the largest size possible. |
| 13 | //! |
| 14 | //! Because of this header, we have to make two passes. The first pass will compute the size of |
| 15 | //! the TLV, and the second pass will build the data for the TLV. |
| 16 | |
David Brown | 91d6863 | 2019-07-29 14:32:13 -0600 | [diff] [blame] | 17 | use byteorder::{ |
| 18 | LittleEndian, WriteBytesExt, |
| 19 | }; |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 20 | use cipher::FromBlockCipher; |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 21 | use crate::caps::Caps; |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 22 | use crate::image::ImageVersion; |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 23 | use log::info; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 24 | use ring::{digest, rand, agreement, hkdf, hmac}; |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 25 | use ring::rand::SecureRandom; |
Fabio Utzig | 05ab014 | 2018-07-10 09:15:28 -0300 | [diff] [blame] | 26 | use ring::signature::{ |
| 27 | RsaKeyPair, |
| 28 | RSA_PSS_SHA256, |
| 29 | EcdsaKeyPair, |
| 30 | ECDSA_P256_SHA256_ASN1_SIGNING, |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 31 | Ed25519KeyPair, |
Fabio Utzig | 05ab014 | 2018-07-10 09:15:28 -0300 | [diff] [blame] | 32 | }; |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 33 | use aes::{ |
| 34 | Aes128, |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 35 | Aes128Ctr, |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 36 | Aes256, |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 37 | Aes256Ctr, |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 38 | NewBlockCipher |
| 39 | }; |
| 40 | use cipher::{ |
| 41 | generic_array::GenericArray, |
| 42 | StreamCipher, |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 43 | }; |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 44 | use mcuboot_sys::c; |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 45 | use typenum::{U16, U32}; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 46 | |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 47 | #[repr(u16)] |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 48 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 49 | #[allow(dead_code)] // TODO: For now |
| 50 | pub enum TlvKinds { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 51 | KEYHASH = 0x01, |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 52 | SHA256 = 0x10, |
| 53 | RSA2048 = 0x20, |
David Vincze | 4395b80 | 2023-04-27 16:11:49 +0200 | [diff] [blame] | 54 | ECDSASIG = 0x22, |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 55 | RSA3072 = 0x23, |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 56 | ED25519 = 0x24, |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 57 | ENCRSA2048 = 0x30, |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 58 | ENCKW = 0x31, |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 59 | ENCEC256 = 0x32, |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 60 | ENCX25519 = 0x33, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 61 | DEPENDENCY = 0x40, |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 62 | SECCNT = 0x50, |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | #[allow(dead_code, non_camel_case_types)] |
| 66 | pub enum TlvFlags { |
| 67 | PIC = 0x01, |
| 68 | NON_BOOTABLE = 0x02, |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 69 | ENCRYPTED_AES128 = 0x04, |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 70 | ENCRYPTED_AES256 = 0x08, |
David Brown | d8713a5 | 2021-10-22 16:27:23 -0600 | [diff] [blame] | 71 | RAM_LOAD = 0x20, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 72 | } |
| 73 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 74 | /// A generator for manifests. The format of the manifest can be either a |
| 75 | /// traditional "TLV" or a SUIT-style manifest. |
| 76 | pub trait ManifestGen { |
David Brown | ac46e26 | 2019-01-11 15:46:18 -0700 | [diff] [blame] | 77 | /// Retrieve the header magic value for this manifest type. |
| 78 | fn get_magic(&self) -> u32; |
| 79 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 80 | /// Retrieve the flags value for this particular manifest type. |
| 81 | fn get_flags(&self) -> u32; |
| 82 | |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 83 | /// Retrieve the number of bytes of this manifest that is "protected". |
| 84 | /// This field is stored in the outside image header instead of the |
| 85 | /// manifest header. |
| 86 | fn protect_size(&self) -> u16; |
| 87 | |
| 88 | /// Add a dependency on another image. |
| 89 | fn add_dependency(&mut self, id: u8, version: &ImageVersion); |
| 90 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 91 | /// Add a sequence of bytes to the payload that the manifest is |
| 92 | /// protecting. |
| 93 | fn add_bytes(&mut self, bytes: &[u8]); |
| 94 | |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 95 | /// Set an internal flag indicating that the next `make_tlv` should |
| 96 | /// corrupt the signature. |
| 97 | fn corrupt_sig(&mut self); |
| 98 | |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 99 | /// Estimate the size of the TLV. This can be called before the payload is added (but after |
| 100 | /// other information is added). Some of the signature algorithms can generate variable sized |
| 101 | /// data, and therefore, this can slightly overestimate the size. |
| 102 | fn estimate_size(&self) -> usize; |
| 103 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 104 | /// Construct the manifest for this payload. |
| 105 | fn make_tlv(self: Box<Self>) -> Vec<u8>; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 106 | |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 107 | /// Generate a new encryption random key |
| 108 | fn generate_enc_key(&mut self); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 109 | |
| 110 | /// Return the current encryption key |
| 111 | fn get_enc_key(&self) -> Vec<u8>; |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 112 | |
| 113 | /// Set the security counter to the specified value. |
| 114 | fn set_security_counter(&mut self, security_cnt: Option<u32>); |
Roland Mikhel | 6945bb6 | 2023-04-11 15:57:49 +0200 | [diff] [blame] | 115 | |
| 116 | /// Sets the ignore_ram_load_flag so that can be validated when it is missing, |
| 117 | /// it will not load successfully. |
| 118 | fn set_ignore_ram_load_flag(&mut self); |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 121 | #[derive(Debug, Default)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 122 | pub struct TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 123 | flags: u32, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 124 | kinds: Vec<TlvKinds>, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 125 | payload: Vec<u8>, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 126 | dependencies: Vec<Dependency>, |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 127 | enc_key: Vec<u8>, |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 128 | /// Should this signature be corrupted. |
| 129 | gen_corrupted: bool, |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 130 | security_cnt: Option<u32>, |
Roland Mikhel | 6945bb6 | 2023-04-11 15:57:49 +0200 | [diff] [blame] | 131 | /// Ignore RAM_LOAD flag |
| 132 | ignore_ram_load_flag: bool, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 133 | } |
| 134 | |
David Brown | c3898d6 | 2019-08-05 14:20:02 -0600 | [diff] [blame] | 135 | #[derive(Debug)] |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 136 | struct Dependency { |
| 137 | id: u8, |
| 138 | version: ImageVersion, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | impl TlvGen { |
| 142 | /// Construct a new tlv generator that will only contain a hash of the data. |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 143 | #[allow(dead_code)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 144 | pub fn new_hash_only() -> TlvGen { |
| 145 | TlvGen { |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 146 | kinds: vec![TlvKinds::SHA256], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 147 | ..Default::default() |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 151 | #[allow(dead_code)] |
| 152 | pub fn new_rsa_pss() -> TlvGen { |
| 153 | TlvGen { |
Fabio Utzig | 754438d | 2018-12-14 06:39:58 -0200 | [diff] [blame] | 154 | kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 155 | ..Default::default() |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 159 | #[allow(dead_code)] |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 160 | pub fn new_rsa3072_pss() -> TlvGen { |
| 161 | TlvGen { |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 162 | kinds: vec![TlvKinds::SHA256, TlvKinds::RSA3072], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 163 | ..Default::default() |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | #[allow(dead_code)] |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 168 | pub fn new_ecdsa() -> TlvGen { |
| 169 | TlvGen { |
Roland Mikhel | 3097851 | 2023-02-08 14:06:58 +0100 | [diff] [blame] | 170 | kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSASIG], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 171 | ..Default::default() |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 175 | #[allow(dead_code)] |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 176 | pub fn new_ed25519() -> TlvGen { |
| 177 | TlvGen { |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 178 | kinds: vec![TlvKinds::SHA256, TlvKinds::ED25519], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 179 | ..Default::default() |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 184 | pub fn new_enc_rsa(aes_key_size: u32) -> TlvGen { |
| 185 | let flag = if aes_key_size == 256 { |
| 186 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 187 | } else { |
| 188 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 189 | }; |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 190 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 191 | flags: flag, |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 192 | kinds: vec![TlvKinds::SHA256, TlvKinds::ENCRSA2048], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 193 | ..Default::default() |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 198 | pub fn new_sig_enc_rsa(aes_key_size: u32) -> TlvGen { |
| 199 | let flag = if aes_key_size == 256 { |
| 200 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 201 | } else { |
| 202 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 203 | }; |
Fabio Utzig | 754438d | 2018-12-14 06:39:58 -0200 | [diff] [blame] | 204 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 205 | flags: flag, |
Fabio Utzig | 754438d | 2018-12-14 06:39:58 -0200 | [diff] [blame] | 206 | kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCRSA2048], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 207 | ..Default::default() |
Fabio Utzig | 754438d | 2018-12-14 06:39:58 -0200 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 212 | pub fn new_enc_kw(aes_key_size: u32) -> TlvGen { |
| 213 | let flag = if aes_key_size == 256 { |
| 214 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 215 | } else { |
| 216 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 217 | }; |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 218 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 219 | flags: flag, |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 220 | kinds: vec![TlvKinds::SHA256, TlvKinds::ENCKW], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 221 | ..Default::default() |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 225 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 226 | pub fn new_rsa_kw(aes_key_size: u32) -> TlvGen { |
| 227 | let flag = if aes_key_size == 256 { |
| 228 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 229 | } else { |
| 230 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 231 | }; |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 232 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 233 | flags: flag, |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 234 | kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCKW], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 235 | ..Default::default() |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 239 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 240 | pub fn new_ecdsa_kw(aes_key_size: u32) -> TlvGen { |
| 241 | let flag = if aes_key_size == 256 { |
| 242 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 243 | } else { |
| 244 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 245 | }; |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 246 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 247 | flags: flag, |
Roland Mikhel | 3097851 | 2023-02-08 14:06:58 +0100 | [diff] [blame] | 248 | kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSASIG, TlvKinds::ENCKW], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 249 | ..Default::default() |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 254 | pub fn new_ecies_p256(aes_key_size: u32) -> TlvGen { |
| 255 | let flag = if aes_key_size == 256 { |
| 256 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 257 | } else { |
| 258 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 259 | }; |
Fabio Utzig | 66b4caa | 2020-01-04 20:19:28 -0300 | [diff] [blame] | 260 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 261 | flags: flag, |
Fabio Utzig | 66b4caa | 2020-01-04 20:19:28 -0300 | [diff] [blame] | 262 | kinds: vec![TlvKinds::SHA256, TlvKinds::ENCEC256], |
Fabio Utzig | 66b4caa | 2020-01-04 20:19:28 -0300 | [diff] [blame] | 263 | ..Default::default() |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 268 | pub fn new_ecdsa_ecies_p256(aes_key_size: u32) -> TlvGen { |
| 269 | let flag = if aes_key_size == 256 { |
| 270 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 271 | } else { |
| 272 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 273 | }; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 274 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 275 | flags: flag, |
Roland Mikhel | 3097851 | 2023-02-08 14:06:58 +0100 | [diff] [blame] | 276 | kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSASIG, TlvKinds::ENCEC256], |
Fabio Utzig | 9a2b5de | 2019-11-22 12:50:02 -0300 | [diff] [blame] | 277 | ..Default::default() |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 278 | } |
| 279 | } |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 280 | |
| 281 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 282 | pub fn new_ecies_x25519(aes_key_size: u32) -> TlvGen { |
| 283 | let flag = if aes_key_size == 256 { |
| 284 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 285 | } else { |
| 286 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 287 | }; |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 288 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 289 | flags: flag, |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 290 | kinds: vec![TlvKinds::SHA256, TlvKinds::ENCX25519], |
| 291 | ..Default::default() |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #[allow(dead_code)] |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 296 | pub fn new_ed25519_ecies_x25519(aes_key_size: u32) -> TlvGen { |
| 297 | let flag = if aes_key_size == 256 { |
| 298 | TlvFlags::ENCRYPTED_AES256 as u32 |
| 299 | } else { |
| 300 | TlvFlags::ENCRYPTED_AES128 as u32 |
| 301 | }; |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 302 | TlvGen { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 303 | flags: flag, |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 304 | kinds: vec![TlvKinds::SHA256, TlvKinds::ED25519, TlvKinds::ENCX25519], |
| 305 | ..Default::default() |
| 306 | } |
| 307 | } |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 308 | |
| 309 | #[allow(dead_code)] |
| 310 | pub fn new_sec_cnt() -> TlvGen { |
| 311 | TlvGen { |
| 312 | kinds: vec![TlvKinds::SHA256, TlvKinds::SECCNT], |
| 313 | ..Default::default() |
| 314 | } |
| 315 | } |
| 316 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | impl ManifestGen for TlvGen { |
David Brown | ac46e26 | 2019-01-11 15:46:18 -0700 | [diff] [blame] | 320 | fn get_magic(&self) -> u32 { |
| 321 | 0x96f3b83d |
| 322 | } |
| 323 | |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 324 | /// Retrieve the header flags for this configuration. This can be called at any time. |
| 325 | fn get_flags(&self) -> u32 { |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 326 | // For the RamLoad case, add in the flag for this feature. |
Roland Mikhel | 6945bb6 | 2023-04-11 15:57:49 +0200 | [diff] [blame] | 327 | if Caps::RamLoad.present() && !self.ignore_ram_load_flag { |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 328 | self.flags | (TlvFlags::RAM_LOAD as u32) |
| 329 | } else { |
| 330 | self.flags |
| 331 | } |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 332 | } |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 333 | |
| 334 | /// Add bytes to the covered hash. |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 335 | fn add_bytes(&mut self, bytes: &[u8]) { |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 336 | self.payload.extend_from_slice(bytes); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 337 | } |
| 338 | |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 339 | fn protect_size(&self) -> u16 { |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 340 | let mut size = 0; |
| 341 | if !self.dependencies.is_empty() || (Caps::HwRollbackProtection.present() && self.security_cnt.is_some()) { |
| 342 | // include the TLV area header. |
| 343 | size += 4; |
| 344 | // add space for each dependency. |
| 345 | size += (self.dependencies.len() as u16) * (4 + std::mem::size_of::<Dependency>() as u16); |
| 346 | if Caps::HwRollbackProtection.present() && self.security_cnt.is_some() { |
| 347 | size += 4 + 4; |
| 348 | } |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 349 | } |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 350 | size |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | fn add_dependency(&mut self, id: u8, version: &ImageVersion) { |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 354 | self.dependencies.push(Dependency { |
David Brown | 4dfb33c | 2021-03-10 05:15:45 -0700 | [diff] [blame] | 355 | id, |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 356 | version: version.clone(), |
| 357 | }); |
| 358 | } |
| 359 | |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 360 | fn corrupt_sig(&mut self) { |
| 361 | self.gen_corrupted = true; |
| 362 | } |
| 363 | |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 364 | fn estimate_size(&self) -> usize { |
| 365 | // Begin the estimate with the 4 byte header. |
| 366 | let mut estimate = 4; |
| 367 | // A very poor estimate. |
| 368 | |
| 369 | // Estimate the size of the image hash. |
| 370 | if self.kinds.contains(&TlvKinds::SHA256) { |
| 371 | estimate += 4 + 32; |
| 372 | } |
| 373 | |
| 374 | // Add an estimate in for each of the signature algorithms. |
| 375 | if self.kinds.contains(&TlvKinds::RSA2048) { |
| 376 | estimate += 4 + 32; // keyhash |
| 377 | estimate += 4 + 256; // RSA2048 |
| 378 | } |
| 379 | if self.kinds.contains(&TlvKinds::RSA3072) { |
| 380 | estimate += 4 + 32; // keyhash |
| 381 | estimate += 4 + 384; // RSA3072 |
| 382 | } |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 383 | if self.kinds.contains(&TlvKinds::ED25519) { |
| 384 | estimate += 4 + 32; // keyhash |
| 385 | estimate += 4 + 64; // ED25519 signature. |
| 386 | } |
Roland Mikhel | 6205c10 | 2023-02-06 13:32:02 +0100 | [diff] [blame] | 387 | if self.kinds.contains(&TlvKinds::ECDSASIG) { |
| 388 | estimate += 4 + 32; // keyhash |
Roland Mikhel | 3097851 | 2023-02-08 14:06:58 +0100 | [diff] [blame] | 389 | |
| 390 | // ECDSA signatures are encoded as ASN.1 with the x and y values stored as signed |
| 391 | // integers. As such, the size can vary by 2 bytes, if the 256-bit value has the high |
| 392 | // bit, it takes an extra 0 byte to avoid it being seen as a negative number. |
Roland Mikhel | 6205c10 | 2023-02-06 13:32:02 +0100 | [diff] [blame] | 393 | estimate += 4 + 72; // ECDSA256 (varies) |
| 394 | } |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 395 | |
| 396 | // Estimate encryption. |
| 397 | let flag = TlvFlags::ENCRYPTED_AES256 as u32; |
| 398 | let aes256 = (self.get_flags() & flag) == flag; |
| 399 | |
| 400 | if self.kinds.contains(&TlvKinds::ENCRSA2048) { |
| 401 | estimate += 4 + 256; |
| 402 | } |
| 403 | if self.kinds.contains(&TlvKinds::ENCKW) { |
| 404 | estimate += 4 + if aes256 { 40 } else { 24 }; |
| 405 | } |
| 406 | if self.kinds.contains(&TlvKinds::ENCEC256) { |
| 407 | estimate += 4 + if aes256 { 129 } else { 113 }; |
| 408 | } |
| 409 | if self.kinds.contains(&TlvKinds::ENCX25519) { |
| 410 | estimate += 4 + if aes256 { 96 } else { 80 }; |
| 411 | } |
| 412 | |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 413 | // Gather the size of the protected TLV area. |
| 414 | estimate += self.protect_size() as usize; |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 415 | |
| 416 | estimate |
| 417 | } |
| 418 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 419 | /// Compute the TLV given the specified block of data. |
David Brown | 43643dd | 2019-01-11 15:43:28 -0700 | [diff] [blame] | 420 | fn make_tlv(self: Box<Self>) -> Vec<u8> { |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 421 | let size_estimate = self.estimate_size(); |
| 422 | |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 423 | let mut protected_tlv: Vec<u8> = vec![]; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 424 | |
David Brown | 2b73ed9 | 2020-01-08 17:01:22 -0700 | [diff] [blame] | 425 | if self.protect_size() > 0 { |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 426 | protected_tlv.push(0x08); |
| 427 | protected_tlv.push(0x69); |
David Brown | 2b73ed9 | 2020-01-08 17:01:22 -0700 | [diff] [blame] | 428 | let size = self.protect_size(); |
| 429 | protected_tlv.write_u16::<LittleEndian>(size).unwrap(); |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 430 | for dep in &self.dependencies { |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 431 | protected_tlv.write_u16::<LittleEndian>(TlvKinds::DEPENDENCY as u16).unwrap(); |
David Brown | 4fae8b8 | 2019-12-05 11:26:59 -0700 | [diff] [blame] | 432 | protected_tlv.write_u16::<LittleEndian>(12).unwrap(); |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 433 | |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 434 | // The dependency. |
| 435 | protected_tlv.push(dep.id); |
David Brown | f66b205 | 2021-03-10 05:26:36 -0700 | [diff] [blame] | 436 | protected_tlv.push(0); |
| 437 | protected_tlv.write_u16::<LittleEndian>(0).unwrap(); |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 438 | protected_tlv.push(dep.version.major); |
| 439 | protected_tlv.push(dep.version.minor); |
| 440 | protected_tlv.write_u16::<LittleEndian>(dep.version.revision).unwrap(); |
| 441 | protected_tlv.write_u32::<LittleEndian>(dep.version.build_num).unwrap(); |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 442 | } |
David Brown | 2b73ed9 | 2020-01-08 17:01:22 -0700 | [diff] [blame] | 443 | |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 444 | // Security counter has to be at the protected TLV area also |
| 445 | if Caps::HwRollbackProtection.present() && self.security_cnt.is_some() { |
| 446 | protected_tlv.write_u16::<LittleEndian>(TlvKinds::SECCNT as u16).unwrap(); |
| 447 | protected_tlv.write_u16::<LittleEndian>(std::mem::size_of::<u32>() as u16).unwrap(); |
| 448 | protected_tlv.write_u32::<LittleEndian>(self.security_cnt.unwrap() as u32).unwrap(); |
| 449 | } |
| 450 | |
David Brown | 2b73ed9 | 2020-01-08 17:01:22 -0700 | [diff] [blame] | 451 | assert_eq!(size, protected_tlv.len() as u16, "protected TLV length incorrect"); |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Ring does the signature itself, which means that it must be |
| 455 | // given a full, contiguous payload. Although this does help from |
| 456 | // a correct usage perspective, it is fairly stupid from an |
| 457 | // efficiency view. If this is shown to be a performance issue |
| 458 | // with the tests, the protected data could be appended to the |
| 459 | // payload, and then removed after the signature is done. For now, |
| 460 | // just make a signed payload. |
| 461 | let mut sig_payload = self.payload.clone(); |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 462 | sig_payload.extend_from_slice(&protected_tlv); |
| 463 | |
| 464 | let mut result: Vec<u8> = vec![]; |
| 465 | |
| 466 | // add back signed payload |
| 467 | result.extend_from_slice(&protected_tlv); |
| 468 | |
| 469 | // add non-protected payload |
David Brown | 3dc86c9 | 2020-01-08 17:22:55 -0700 | [diff] [blame] | 470 | let npro_pos = result.len(); |
Fabio Utzig | ea3d3ab | 2019-09-11 19:35:33 -0300 | [diff] [blame] | 471 | result.push(0x07); |
| 472 | result.push(0x69); |
David Brown | 3dc86c9 | 2020-01-08 17:22:55 -0700 | [diff] [blame] | 473 | // Placeholder for the size. |
| 474 | result.write_u16::<LittleEndian>(0).unwrap(); |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 475 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 476 | if self.kinds.contains(&TlvKinds::SHA256) { |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 477 | // If a signature is not requested, corrupt the hash we are |
| 478 | // generating. But, if there is a signature, output the |
| 479 | // correct hash. We want the hash test to pass so that the |
| 480 | // signature verification can be validated. |
| 481 | let mut corrupt_hash = self.gen_corrupted; |
| 482 | for k in &[TlvKinds::RSA2048, TlvKinds::RSA3072, |
Roland Mikhel | 3097851 | 2023-02-08 14:06:58 +0100 | [diff] [blame] | 483 | TlvKinds::ED25519, TlvKinds::ECDSASIG] |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 484 | { |
| 485 | if self.kinds.contains(k) { |
| 486 | corrupt_hash = false; |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if corrupt_hash { |
| 492 | sig_payload[0] ^= 1; |
| 493 | } |
| 494 | |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 495 | let hash = digest::digest(&digest::SHA256, &sig_payload); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 496 | let hash = hash.as_ref(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 497 | |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 498 | assert!(hash.len() == 32); |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 499 | result.write_u16::<LittleEndian>(TlvKinds::SHA256 as u16).unwrap(); |
David Brown | 4fae8b8 | 2019-12-05 11:26:59 -0700 | [diff] [blame] | 500 | result.write_u16::<LittleEndian>(32).unwrap(); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 501 | result.extend_from_slice(hash); |
David Brown | e90b13f | 2019-12-06 15:04:00 -0700 | [diff] [blame] | 502 | |
| 503 | // Undo the corruption. |
| 504 | if corrupt_hash { |
| 505 | sig_payload[0] ^= 1; |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | |
| 510 | if self.gen_corrupted { |
| 511 | // Corrupt what is signed by modifying the input to the |
| 512 | // signature code. |
| 513 | sig_payload[0] ^= 1; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 514 | } |
| 515 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 516 | if self.kinds.contains(&TlvKinds::RSA2048) || |
| 517 | self.kinds.contains(&TlvKinds::RSA3072) { |
| 518 | |
| 519 | let is_rsa2048 = self.kinds.contains(&TlvKinds::RSA2048); |
| 520 | |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 521 | // Output the hash of the public key. |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 522 | let hash = if is_rsa2048 { |
| 523 | digest::digest(&digest::SHA256, RSA_PUB_KEY) |
| 524 | } else { |
| 525 | digest::digest(&digest::SHA256, RSA3072_PUB_KEY) |
| 526 | }; |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 527 | let hash = hash.as_ref(); |
| 528 | |
| 529 | assert!(hash.len() == 32); |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 530 | result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap(); |
David Brown | 4fae8b8 | 2019-12-05 11:26:59 -0700 | [diff] [blame] | 531 | result.write_u16::<LittleEndian>(32).unwrap(); |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 532 | result.extend_from_slice(hash); |
| 533 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 534 | // For now assume PSS. |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 535 | let key_bytes = if is_rsa2048 { |
| 536 | pem::parse(include_bytes!("../../root-rsa-2048.pem").as_ref()).unwrap() |
| 537 | } else { |
| 538 | pem::parse(include_bytes!("../../root-rsa-3072.pem").as_ref()).unwrap() |
| 539 | }; |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 540 | assert_eq!(key_bytes.tag, "RSA PRIVATE KEY"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 541 | let key_pair = RsaKeyPair::from_der(&key_bytes.contents).unwrap(); |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 542 | let rng = rand::SystemRandom::new(); |
Fabio Utzig | 05ab014 | 2018-07-10 09:15:28 -0300 | [diff] [blame] | 543 | let mut signature = vec![0; key_pair.public_modulus_len()]; |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 544 | if is_rsa2048 { |
| 545 | assert_eq!(signature.len(), 256); |
| 546 | } else { |
| 547 | assert_eq!(signature.len(), 384); |
| 548 | } |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 549 | key_pair.sign(&RSA_PSS_SHA256, &rng, &sig_payload, &mut signature).unwrap(); |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 550 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 551 | if is_rsa2048 { |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 552 | result.write_u16::<LittleEndian>(TlvKinds::RSA2048 as u16).unwrap(); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 553 | } else { |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 554 | result.write_u16::<LittleEndian>(TlvKinds::RSA3072 as u16).unwrap(); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 555 | } |
David Brown | 91d6863 | 2019-07-29 14:32:13 -0600 | [diff] [blame] | 556 | result.write_u16::<LittleEndian>(signature.len() as u16).unwrap(); |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 557 | result.extend_from_slice(&signature); |
| 558 | } |
| 559 | |
Roland Mikhel | 6205c10 | 2023-02-06 13:32:02 +0100 | [diff] [blame] | 560 | if self.kinds.contains(&TlvKinds::ECDSASIG) { |
| 561 | let rng = rand::SystemRandom::new(); |
| 562 | let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY); |
| 563 | let key_bytes = pem::parse(include_bytes!("../../root-ec-p256-pkcs8.pem").as_ref()).unwrap(); |
| 564 | let sign_algo = &ECDSA_P256_SHA256_ASN1_SIGNING; |
| 565 | let key_pair = EcdsaKeyPair::from_pkcs8(sign_algo, &key_bytes.contents).unwrap(); |
| 566 | let signature = key_pair.sign(&rng,&sig_payload).unwrap(); |
| 567 | |
| 568 | // Write public key |
| 569 | let keyhash_slice = keyhash.as_ref(); |
| 570 | assert!(keyhash_slice.len() == 32); |
| 571 | result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap(); |
| 572 | result.write_u16::<LittleEndian>(32).unwrap(); |
| 573 | result.extend_from_slice(keyhash_slice); |
| 574 | |
| 575 | // Write signature |
| 576 | result.write_u16::<LittleEndian>(TlvKinds::ECDSASIG as u16).unwrap(); |
| 577 | let signature = signature.as_ref().to_vec(); |
| 578 | result.write_u16::<LittleEndian>(signature.len() as u16).unwrap(); |
| 579 | result.extend_from_slice(&signature); |
| 580 | } |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 581 | if self.kinds.contains(&TlvKinds::ED25519) { |
| 582 | let keyhash = digest::digest(&digest::SHA256, ED25519_PUB_KEY); |
| 583 | let keyhash = keyhash.as_ref(); |
| 584 | |
| 585 | assert!(keyhash.len() == 32); |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 586 | result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap(); |
David Brown | 4fae8b8 | 2019-12-05 11:26:59 -0700 | [diff] [blame] | 587 | result.write_u16::<LittleEndian>(32).unwrap(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 588 | result.extend_from_slice(keyhash); |
| 589 | |
David Brown | 7a81c4b | 2019-07-29 15:20:21 -0600 | [diff] [blame] | 590 | let hash = digest::digest(&digest::SHA256, &sig_payload); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 591 | let hash = hash.as_ref(); |
| 592 | assert!(hash.len() == 32); |
| 593 | |
| 594 | let key_bytes = pem::parse(include_bytes!("../../root-ed25519.pem").as_ref()).unwrap(); |
| 595 | assert_eq!(key_bytes.tag, "PRIVATE KEY"); |
| 596 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 597 | let key_pair = Ed25519KeyPair::from_seed_and_public_key( |
| 598 | &key_bytes.contents[16..48], &ED25519_PUB_KEY[12..44]).unwrap(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 599 | let signature = key_pair.sign(&hash); |
| 600 | |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 601 | result.write_u16::<LittleEndian>(TlvKinds::ED25519 as u16).unwrap(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 602 | |
| 603 | let signature = signature.as_ref().to_vec(); |
David Brown | 91d6863 | 2019-07-29 14:32:13 -0600 | [diff] [blame] | 604 | result.write_u16::<LittleEndian>(signature.len() as u16).unwrap(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 605 | result.extend_from_slice(signature.as_ref()); |
| 606 | } |
| 607 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 608 | if self.kinds.contains(&TlvKinds::ENCRSA2048) { |
| 609 | let key_bytes = pem::parse(include_bytes!("../../enc-rsa2048-pub.pem") |
| 610 | .as_ref()).unwrap(); |
| 611 | assert_eq!(key_bytes.tag, "PUBLIC KEY"); |
| 612 | |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 613 | let cipherkey = self.get_enc_key(); |
| 614 | let cipherkey = cipherkey.as_slice(); |
| 615 | let encbuf = match c::rsa_oaep_encrypt(&key_bytes.contents, cipherkey) { |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 616 | Ok(v) => v, |
| 617 | Err(_) => panic!("Failed to encrypt secret key"), |
| 618 | }; |
| 619 | |
| 620 | assert!(encbuf.len() == 256); |
David Brown | 6972118 | 2019-12-04 14:50:52 -0700 | [diff] [blame] | 621 | result.write_u16::<LittleEndian>(TlvKinds::ENCRSA2048 as u16).unwrap(); |
David Brown | 4fae8b8 | 2019-12-05 11:26:59 -0700 | [diff] [blame] | 622 | result.write_u16::<LittleEndian>(256).unwrap(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 623 | result.extend_from_slice(&encbuf); |
| 624 | } |
| 625 | |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 626 | if self.kinds.contains(&TlvKinds::ENCKW) { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 627 | let flag = TlvFlags::ENCRYPTED_AES256 as u32; |
| 628 | let aes256 = (self.get_flags() & flag) == flag; |
| 629 | let key_bytes = if aes256 { |
| 630 | base64::decode( |
| 631 | include_str!("../../enc-aes256kw.b64").trim()).unwrap() |
| 632 | } else { |
| 633 | base64::decode( |
| 634 | include_str!("../../enc-aes128kw.b64").trim()).unwrap() |
| 635 | }; |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 636 | let cipherkey = self.get_enc_key(); |
| 637 | let cipherkey = cipherkey.as_slice(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 638 | let keylen = if aes256 { 32 } else { 16 }; |
| 639 | let encbuf = match c::kw_encrypt(&key_bytes, cipherkey, keylen) { |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 640 | Ok(v) => v, |
| 641 | Err(_) => panic!("Failed to encrypt secret key"), |
| 642 | }; |
| 643 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 644 | let size = if aes256 { 40 } else { 24 }; |
| 645 | assert!(encbuf.len() == size); |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 646 | result.write_u16::<LittleEndian>(TlvKinds::ENCKW as u16).unwrap(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 647 | result.write_u16::<LittleEndian>(size as u16).unwrap(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 648 | result.extend_from_slice(&encbuf); |
| 649 | } |
| 650 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 651 | if self.kinds.contains(&TlvKinds::ENCEC256) || self.kinds.contains(&TlvKinds::ENCX25519) { |
| 652 | let key_bytes = if self.kinds.contains(&TlvKinds::ENCEC256) { |
| 653 | pem::parse(include_bytes!("../../enc-ec256-pub.pem").as_ref()).unwrap() |
| 654 | } else { |
| 655 | pem::parse(include_bytes!("../../enc-x25519-pub.pem").as_ref()).unwrap() |
| 656 | }; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 657 | assert_eq!(key_bytes.tag, "PUBLIC KEY"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 658 | let rng = rand::SystemRandom::new(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 659 | let alg = if self.kinds.contains(&TlvKinds::ENCEC256) { |
| 660 | &agreement::ECDH_P256 |
| 661 | } else { |
| 662 | &agreement::X25519 |
| 663 | }; |
| 664 | let pk = match agreement::EphemeralPrivateKey::generate(alg, &rng) { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 665 | Ok(v) => v, |
| 666 | Err(_) => panic!("Failed to generate ephemeral keypair"), |
| 667 | }; |
| 668 | |
| 669 | let pubk = match pk.compute_public_key() { |
| 670 | Ok(pubk) => pubk, |
| 671 | Err(_) => panic!("Failed computing ephemeral public key"), |
| 672 | }; |
| 673 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 674 | let peer_pubk = if self.kinds.contains(&TlvKinds::ENCEC256) { |
| 675 | agreement::UnparsedPublicKey::new(&agreement::ECDH_P256, &key_bytes.contents[26..]) |
| 676 | } else { |
| 677 | agreement::UnparsedPublicKey::new(&agreement::X25519, &key_bytes.contents[12..]) |
| 678 | }; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 679 | |
| 680 | #[derive(Debug, PartialEq)] |
| 681 | struct OkmLen<T: core::fmt::Debug + PartialEq>(T); |
| 682 | |
| 683 | impl hkdf::KeyType for OkmLen<usize> { |
| 684 | fn len(&self) -> usize { |
| 685 | self.0 |
| 686 | } |
| 687 | } |
| 688 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 689 | let flag = TlvFlags::ENCRYPTED_AES256 as u32; |
| 690 | let aes256 = (self.get_flags() & flag) == flag; |
| 691 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 692 | let derived_key = match agreement::agree_ephemeral( |
| 693 | pk, &peer_pubk, ring::error::Unspecified, |shared| { |
| 694 | let salt = hkdf::Salt::new(hkdf::HKDF_SHA256, &[]); |
| 695 | let prk = salt.extract(&shared); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 696 | let okm_len = if aes256 { 64 } else { 48 }; |
| 697 | let okm = match prk.expand(&[b"MCUBoot_ECIES_v1"], OkmLen(okm_len)) { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 698 | Ok(okm) => okm, |
| 699 | Err(_) => panic!("Failed building HKDF OKM"), |
| 700 | }; |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 701 | let mut buf = if aes256 { vec![0u8; 64] } else { vec![0u8; 48] }; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 702 | match okm.fill(&mut buf) { |
| 703 | Ok(_) => Ok(buf), |
| 704 | Err(_) => panic!("Failed generating HKDF output"), |
| 705 | } |
| 706 | }, |
| 707 | ) { |
| 708 | Ok(v) => v, |
| 709 | Err(_) => panic!("Failed building HKDF"), |
| 710 | }; |
| 711 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 712 | let nonce = GenericArray::from_slice(&[0; 16]); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 713 | let mut cipherkey = self.get_enc_key(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 714 | if aes256 { |
| 715 | let key: &GenericArray<u8, U32> = GenericArray::from_slice(&derived_key[..32]); |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 716 | let block = Aes256::new(&key); |
| 717 | let mut cipher = Aes256Ctr::from_block_cipher(block, &nonce); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 718 | cipher.apply_keystream(&mut cipherkey); |
| 719 | } else { |
| 720 | let key: &GenericArray<u8, U16> = GenericArray::from_slice(&derived_key[..16]); |
David Brown | 9c6322f | 2021-08-19 13:03:39 -0600 | [diff] [blame] | 721 | let block = Aes128::new(&key); |
| 722 | let mut cipher = Aes128Ctr::from_block_cipher(block, &nonce); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 723 | cipher.apply_keystream(&mut cipherkey); |
| 724 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 725 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 726 | let size = if aes256 { 32 } else { 16 }; |
| 727 | let key = hmac::Key::new(hmac::HMAC_SHA256, &derived_key[size..]); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 728 | let tag = hmac::sign(&key, &cipherkey); |
| 729 | |
| 730 | let mut buf = vec![]; |
| 731 | buf.append(&mut pubk.as_ref().to_vec()); |
| 732 | buf.append(&mut tag.as_ref().to_vec()); |
| 733 | buf.append(&mut cipherkey); |
| 734 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 735 | if self.kinds.contains(&TlvKinds::ENCEC256) { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 736 | let size = if aes256 { 129 } else { 113 }; |
| 737 | assert!(buf.len() == size); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 738 | result.write_u16::<LittleEndian>(TlvKinds::ENCEC256 as u16).unwrap(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 739 | result.write_u16::<LittleEndian>(size as u16).unwrap(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 740 | } else { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 741 | let size = if aes256 { 96 } else { 80 }; |
| 742 | assert!(buf.len() == size); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 743 | result.write_u16::<LittleEndian>(TlvKinds::ENCX25519 as u16).unwrap(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 744 | result.write_u16::<LittleEndian>(size as u16).unwrap(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 745 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 746 | result.extend_from_slice(&buf); |
| 747 | } |
| 748 | |
David Brown | 3dc86c9 | 2020-01-08 17:22:55 -0700 | [diff] [blame] | 749 | // Patch the size back into the TLV header. |
| 750 | let size = (result.len() - npro_pos) as u16; |
| 751 | let mut size_buf = &mut result[npro_pos + 2 .. npro_pos + 4]; |
| 752 | size_buf.write_u16::<LittleEndian>(size).unwrap(); |
| 753 | |
David Brown | b408b43 | 2021-10-27 15:55:39 -0600 | [diff] [blame] | 754 | // ECDSA is stored as an ASN.1 integer. For a 128-bit value, this maximally results in 33 |
| 755 | // bytes of storage for each of the two values. If the high bit is zero, it will take 32 |
| 756 | // bytes, if the top 8 bits are zero, it will take 31 bits, and so on. The smaller size |
| 757 | // will occur with decreasing likelihood. We'll allow this to get a bit smaller, hopefully |
| 758 | // allowing the tests to pass with false failures rare. For this case, we'll handle up to |
| 759 | // the top 16 bits of both numbers being all zeros (1 in 2^32). |
| 760 | if !Caps::has_ecdsa() { |
| 761 | if size_estimate != result.len() { |
| 762 | panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len()); |
| 763 | } |
| 764 | } else { |
| 765 | if size_estimate < result.len() || size_estimate > result.len() + 6 { |
| 766 | panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len()); |
| 767 | } |
David Brown | ef4f074 | 2021-10-22 17:09:50 -0600 | [diff] [blame] | 768 | } |
| 769 | if size_estimate != result.len() { |
| 770 | log::warn!("Size off: {} actual {}", size_estimate, result.len()); |
| 771 | } |
| 772 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 773 | result |
| 774 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 775 | |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 776 | fn generate_enc_key(&mut self) { |
| 777 | let rng = rand::SystemRandom::new(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 778 | let flag = TlvFlags::ENCRYPTED_AES256 as u32; |
| 779 | let aes256 = (self.get_flags() & flag) == flag; |
| 780 | let mut buf = if aes256 { |
| 781 | vec![0u8; 32] |
| 782 | } else { |
| 783 | vec![0u8; 16] |
| 784 | }; |
David Brown | 26edaf3 | 2021-03-10 05:27:38 -0700 | [diff] [blame] | 785 | if rng.fill(&mut buf).is_err() { |
| 786 | panic!("Error generating encrypted key"); |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 787 | } |
| 788 | info!("New encryption key: {:02x?}", buf); |
| 789 | self.enc_key = buf; |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | fn get_enc_key(&self) -> Vec<u8> { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 793 | if self.enc_key.len() != 32 && self.enc_key.len() != 16 { |
Fabio Utzig | e84f0ef | 2019-11-22 12:29:32 -0300 | [diff] [blame] | 794 | panic!("No random key was generated"); |
| 795 | } |
| 796 | self.enc_key.clone() |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 797 | } |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 798 | |
| 799 | fn set_security_counter(&mut self, security_cnt: Option<u32>) { |
| 800 | self.security_cnt = security_cnt; |
| 801 | } |
Roland Mikhel | 6945bb6 | 2023-04-11 15:57:49 +0200 | [diff] [blame] | 802 | |
| 803 | fn set_ignore_ram_load_flag(&mut self) { |
| 804 | self.ignore_ram_load_flag = true; |
| 805 | } |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 806 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 807 | |
| 808 | include!("rsa_pub_key-rs.txt"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 809 | include!("rsa3072_pub_key-rs.txt"); |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 810 | include!("ecdsa_pub_key-rs.txt"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 811 | include!("ed25519_pub_key-rs.txt"); |