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