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