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