blob: fcd6b5709a9a9d2c8f7e6d70fd9866a6968f8ec3 [file] [log] [blame]
David Brownc8d62012021-10-27 15:03:48 -06001// Copyright (c) 2017-2021 Linaro LTD
David Browne2acfae2020-01-21 16:45:01 -07002// Copyright (c) 2017-2020 JUUL Labs
Roland Mikhel75c7c312023-02-23 15:39:14 +01003// Copyright (c) 2021-2023 Arm Limited
David Browne2acfae2020-01-21 16:45:01 -07004//
5// SPDX-License-Identifier: Apache-2.0
6
David Brown187dd882017-07-11 11:15:23 -06007//! 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 Brown91d68632019-07-29 14:32:13 -060017use byteorder::{
18 LittleEndian, WriteBytesExt,
19};
David Brown9c6322f2021-08-19 13:03:39 -060020use cipher::FromBlockCipher;
David Brown8a4e23b2021-06-11 10:29:01 -060021use crate::caps::Caps;
David Brown7a81c4b2019-07-29 15:20:21 -060022use crate::image::ImageVersion;
Fabio Utzige84f0ef2019-11-22 12:29:32 -030023use log::info;
Fabio Utzig90f449e2019-10-24 07:43:53 -030024use ring::{digest, rand, agreement, hkdf, hmac};
Fabio Utzige84f0ef2019-11-22 12:29:32 -030025use ring::rand::SecureRandom;
Fabio Utzig05ab0142018-07-10 09:15:28 -030026use ring::signature::{
27 RsaKeyPair,
28 RSA_PSS_SHA256,
29 EcdsaKeyPair,
30 ECDSA_P256_SHA256_ASN1_SIGNING,
Fabio Utzig97710282019-05-24 17:44:49 -030031 Ed25519KeyPair,
Fabio Utzig05ab0142018-07-10 09:15:28 -030032};
David Brown9c6322f2021-08-19 13:03:39 -060033use aes::{
34 Aes128,
Fabio Utzig90f449e2019-10-24 07:43:53 -030035 Aes128Ctr,
David Brown9c6322f2021-08-19 13:03:39 -060036 Aes256,
Salome Thirot6fdbf552021-05-14 16:46:14 +010037 Aes256Ctr,
David Brown9c6322f2021-08-19 13:03:39 -060038 NewBlockCipher
39};
40use cipher::{
41 generic_array::GenericArray,
42 StreamCipher,
Fabio Utzig90f449e2019-10-24 07:43:53 -030043};
Fabio Utzig80fde2f2017-12-05 09:25:31 -020044use mcuboot_sys::c;
Salome Thirot6fdbf552021-05-14 16:46:14 +010045use typenum::{U16, U32};
David Brown187dd882017-07-11 11:15:23 -060046
David Brown69721182019-12-04 14:50:52 -070047#[repr(u16)]
David Brownc3898d62019-08-05 14:20:02 -060048#[derive(Copy, Clone, Debug, PartialEq, Eq)]
David Brown187dd882017-07-11 11:15:23 -060049#[allow(dead_code)] // TODO: For now
50pub enum TlvKinds {
David Brown43cda332017-09-01 09:53:23 -060051 KEYHASH = 0x01,
David Brown27648b82017-08-31 10:40:29 -060052 SHA256 = 0x10,
53 RSA2048 = 0x20,
David Brown27648b82017-08-31 10:40:29 -060054 ECDSA256 = 0x22,
Fabio Utzig39297432019-05-08 18:51:10 -030055 RSA3072 = 0x23,
Fabio Utzig97710282019-05-24 17:44:49 -030056 ED25519 = 0x24,
Fabio Utzig1e48b912018-09-18 09:04:18 -030057 ENCRSA2048 = 0x30,
Salome Thirot0f641972021-05-14 11:19:55 +010058 ENCKW = 0x31,
Fabio Utzig90f449e2019-10-24 07:43:53 -030059 ENCEC256 = 0x32,
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030060 ENCX25519 = 0x33,
David Brown7a81c4b2019-07-29 15:20:21 -060061 DEPENDENCY = 0x40,
Fabio Utzig1e48b912018-09-18 09:04:18 -030062}
63
64#[allow(dead_code, non_camel_case_types)]
65pub enum TlvFlags {
66 PIC = 0x01,
67 NON_BOOTABLE = 0x02,
Salome Thirot6fdbf552021-05-14 16:46:14 +010068 ENCRYPTED_AES128 = 0x04,
Salome Thirot6fdbf552021-05-14 16:46:14 +010069 ENCRYPTED_AES256 = 0x08,
David Brownd8713a52021-10-22 16:27:23 -060070 RAM_LOAD = 0x20,
David Brown187dd882017-07-11 11:15:23 -060071}
72
David Brown43643dd2019-01-11 15:43:28 -070073/// A generator for manifests. The format of the manifest can be either a
74/// traditional "TLV" or a SUIT-style manifest.
75pub trait ManifestGen {
David Brownac46e262019-01-11 15:46:18 -070076 /// Retrieve the header magic value for this manifest type.
77 fn get_magic(&self) -> u32;
78
David Brown43643dd2019-01-11 15:43:28 -070079 /// Retrieve the flags value for this particular manifest type.
80 fn get_flags(&self) -> u32;
81
David Brown7a81c4b2019-07-29 15:20:21 -060082 /// Retrieve the number of bytes of this manifest that is "protected".
83 /// This field is stored in the outside image header instead of the
84 /// manifest header.
85 fn protect_size(&self) -> u16;
86
87 /// Add a dependency on another image.
88 fn add_dependency(&mut self, id: u8, version: &ImageVersion);
89
David Brown43643dd2019-01-11 15:43:28 -070090 /// Add a sequence of bytes to the payload that the manifest is
91 /// protecting.
92 fn add_bytes(&mut self, bytes: &[u8]);
93
David Browne90b13f2019-12-06 15:04:00 -070094 /// Set an internal flag indicating that the next `make_tlv` should
95 /// corrupt the signature.
96 fn corrupt_sig(&mut self);
97
David Brownef4f0742021-10-22 17:09:50 -060098 /// Estimate the size of the TLV. This can be called before the payload is added (but after
99 /// other information is added). Some of the signature algorithms can generate variable sized
100 /// data, and therefore, this can slightly overestimate the size.
101 fn estimate_size(&self) -> usize;
102
David Brown43643dd2019-01-11 15:43:28 -0700103 /// Construct the manifest for this payload.
104 fn make_tlv(self: Box<Self>) -> Vec<u8>;
Fabio Utzig90f449e2019-10-24 07:43:53 -0300105
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300106 /// Generate a new encryption random key
107 fn generate_enc_key(&mut self);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300108
109 /// Return the current encryption key
110 fn get_enc_key(&self) -> Vec<u8>;
David Brown43643dd2019-01-11 15:43:28 -0700111}
112
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300113#[derive(Debug, Default)]
David Brown187dd882017-07-11 11:15:23 -0600114pub struct TlvGen {
David Brown43cda332017-09-01 09:53:23 -0600115 flags: u32,
David Brown187dd882017-07-11 11:15:23 -0600116 kinds: Vec<TlvKinds>,
David Brown4243ab02017-07-11 12:24:23 -0600117 payload: Vec<u8>,
David Brown7a81c4b2019-07-29 15:20:21 -0600118 dependencies: Vec<Dependency>,
Fabio Utzig90f449e2019-10-24 07:43:53 -0300119 enc_key: Vec<u8>,
David Browne90b13f2019-12-06 15:04:00 -0700120 /// Should this signature be corrupted.
121 gen_corrupted: bool,
David Brown7a81c4b2019-07-29 15:20:21 -0600122}
123
David Brownc3898d62019-08-05 14:20:02 -0600124#[derive(Debug)]
David Brown7a81c4b2019-07-29 15:20:21 -0600125struct Dependency {
126 id: u8,
127 version: ImageVersion,
David Brown187dd882017-07-11 11:15:23 -0600128}
129
130impl TlvGen {
131 /// Construct a new tlv generator that will only contain a hash of the data.
David Brown7e701d82017-07-11 13:24:25 -0600132 #[allow(dead_code)]
David Brown187dd882017-07-11 11:15:23 -0600133 pub fn new_hash_only() -> TlvGen {
134 TlvGen {
David Brown187dd882017-07-11 11:15:23 -0600135 kinds: vec![TlvKinds::SHA256],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300136 ..Default::default()
David Brown187dd882017-07-11 11:15:23 -0600137 }
138 }
139
David Brown7e701d82017-07-11 13:24:25 -0600140 #[allow(dead_code)]
141 pub fn new_rsa_pss() -> TlvGen {
142 TlvGen {
Fabio Utzig754438d2018-12-14 06:39:58 -0200143 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300144 ..Default::default()
David Brown7e701d82017-07-11 13:24:25 -0600145 }
146 }
147
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200148 #[allow(dead_code)]
Fabio Utzig39297432019-05-08 18:51:10 -0300149 pub fn new_rsa3072_pss() -> TlvGen {
150 TlvGen {
Fabio Utzig39297432019-05-08 18:51:10 -0300151 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA3072],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300152 ..Default::default()
Fabio Utzig39297432019-05-08 18:51:10 -0300153 }
154 }
155
156 #[allow(dead_code)]
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200157 pub fn new_ecdsa() -> TlvGen {
158 TlvGen {
Fabio Utzig754438d2018-12-14 06:39:58 -0200159 kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSA256],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300160 ..Default::default()
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200161 }
162 }
163
Fabio Utzig1e48b912018-09-18 09:04:18 -0300164 #[allow(dead_code)]
Fabio Utzig97710282019-05-24 17:44:49 -0300165 pub fn new_ed25519() -> TlvGen {
166 TlvGen {
Fabio Utzig97710282019-05-24 17:44:49 -0300167 kinds: vec![TlvKinds::SHA256, TlvKinds::ED25519],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300168 ..Default::default()
Fabio Utzig97710282019-05-24 17:44:49 -0300169 }
170 }
171
172 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100173 pub fn new_enc_rsa(aes_key_size: u32) -> TlvGen {
174 let flag = if aes_key_size == 256 {
175 TlvFlags::ENCRYPTED_AES256 as u32
176 } else {
177 TlvFlags::ENCRYPTED_AES128 as u32
178 };
Fabio Utzig1e48b912018-09-18 09:04:18 -0300179 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100180 flags: flag,
Fabio Utzig1e48b912018-09-18 09:04:18 -0300181 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCRSA2048],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300182 ..Default::default()
Fabio Utzig1e48b912018-09-18 09:04:18 -0300183 }
184 }
185
186 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100187 pub fn new_sig_enc_rsa(aes_key_size: u32) -> TlvGen {
188 let flag = if aes_key_size == 256 {
189 TlvFlags::ENCRYPTED_AES256 as u32
190 } else {
191 TlvFlags::ENCRYPTED_AES128 as u32
192 };
Fabio Utzig754438d2018-12-14 06:39:58 -0200193 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100194 flags: flag,
Fabio Utzig754438d2018-12-14 06:39:58 -0200195 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCRSA2048],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300196 ..Default::default()
Fabio Utzig754438d2018-12-14 06:39:58 -0200197 }
198 }
199
200 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100201 pub fn new_enc_kw(aes_key_size: u32) -> TlvGen {
202 let flag = if aes_key_size == 256 {
203 TlvFlags::ENCRYPTED_AES256 as u32
204 } else {
205 TlvFlags::ENCRYPTED_AES128 as u32
206 };
Fabio Utzig1e48b912018-09-18 09:04:18 -0300207 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100208 flags: flag,
Salome Thirot0f641972021-05-14 11:19:55 +0100209 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCKW],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300210 ..Default::default()
Fabio Utzig1e48b912018-09-18 09:04:18 -0300211 }
212 }
213
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200214 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100215 pub fn new_rsa_kw(aes_key_size: u32) -> TlvGen {
216 let flag = if aes_key_size == 256 {
217 TlvFlags::ENCRYPTED_AES256 as u32
218 } else {
219 TlvFlags::ENCRYPTED_AES128 as u32
220 };
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200221 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100222 flags: flag,
Salome Thirot0f641972021-05-14 11:19:55 +0100223 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCKW],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300224 ..Default::default()
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200225 }
226 }
227
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200228 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100229 pub fn new_ecdsa_kw(aes_key_size: u32) -> TlvGen {
230 let flag = if aes_key_size == 256 {
231 TlvFlags::ENCRYPTED_AES256 as u32
232 } else {
233 TlvFlags::ENCRYPTED_AES128 as u32
234 };
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200235 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100236 flags: flag,
Salome Thirot0f641972021-05-14 11:19:55 +0100237 kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSA256, TlvKinds::ENCKW],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300238 ..Default::default()
Fabio Utzig90f449e2019-10-24 07:43:53 -0300239 }
240 }
241
242 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100243 pub fn new_ecies_p256(aes_key_size: u32) -> TlvGen {
244 let flag = if aes_key_size == 256 {
245 TlvFlags::ENCRYPTED_AES256 as u32
246 } else {
247 TlvFlags::ENCRYPTED_AES128 as u32
248 };
Fabio Utzig66b4caa2020-01-04 20:19:28 -0300249 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100250 flags: flag,
Fabio Utzig66b4caa2020-01-04 20:19:28 -0300251 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCEC256],
Fabio Utzig66b4caa2020-01-04 20:19:28 -0300252 ..Default::default()
253 }
254 }
255
256 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100257 pub fn new_ecdsa_ecies_p256(aes_key_size: u32) -> TlvGen {
258 let flag = if aes_key_size == 256 {
259 TlvFlags::ENCRYPTED_AES256 as u32
260 } else {
261 TlvFlags::ENCRYPTED_AES128 as u32
262 };
Fabio Utzig90f449e2019-10-24 07:43:53 -0300263 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100264 flags: flag,
Fabio Utzig90f449e2019-10-24 07:43:53 -0300265 kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSA256, TlvKinds::ENCEC256],
Fabio Utzig9a2b5de2019-11-22 12:50:02 -0300266 ..Default::default()
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200267 }
268 }
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300269
270 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100271 pub fn new_ecies_x25519(aes_key_size: u32) -> TlvGen {
272 let flag = if aes_key_size == 256 {
273 TlvFlags::ENCRYPTED_AES256 as u32
274 } else {
275 TlvFlags::ENCRYPTED_AES128 as u32
276 };
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300277 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100278 flags: flag,
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300279 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCX25519],
280 ..Default::default()
281 }
282 }
283
284 #[allow(dead_code)]
Salome Thirot6fdbf552021-05-14 16:46:14 +0100285 pub fn new_ed25519_ecies_x25519(aes_key_size: u32) -> TlvGen {
286 let flag = if aes_key_size == 256 {
287 TlvFlags::ENCRYPTED_AES256 as u32
288 } else {
289 TlvFlags::ENCRYPTED_AES128 as u32
290 };
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300291 TlvGen {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100292 flags: flag,
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300293 kinds: vec![TlvKinds::SHA256, TlvKinds::ED25519, TlvKinds::ENCX25519],
294 ..Default::default()
295 }
296 }
David Brown43643dd2019-01-11 15:43:28 -0700297}
298
299impl ManifestGen for TlvGen {
David Brownac46e262019-01-11 15:46:18 -0700300 fn get_magic(&self) -> u32 {
301 0x96f3b83d
302 }
303
David Brown43643dd2019-01-11 15:43:28 -0700304 /// Retrieve the header flags for this configuration. This can be called at any time.
305 fn get_flags(&self) -> u32 {
David Brown8a4e23b2021-06-11 10:29:01 -0600306 // For the RamLoad case, add in the flag for this feature.
307 if Caps::RamLoad.present() {
308 self.flags | (TlvFlags::RAM_LOAD as u32)
309 } else {
310 self.flags
311 }
David Brown43643dd2019-01-11 15:43:28 -0700312 }
David Brown187dd882017-07-11 11:15:23 -0600313
314 /// Add bytes to the covered hash.
David Brown43643dd2019-01-11 15:43:28 -0700315 fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -0600316 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -0600317 }
318
David Brown7a81c4b2019-07-29 15:20:21 -0600319 fn protect_size(&self) -> u16 {
David Brown2b73ed92020-01-08 17:01:22 -0700320 if self.dependencies.is_empty() {
David Brown7a81c4b2019-07-29 15:20:21 -0600321 0
322 } else {
David Brown2b73ed92020-01-08 17:01:22 -0700323 // Include the header and space for each dependency.
324 4 + (self.dependencies.len() as u16) * (4 + 4 + 8)
David Brown7a81c4b2019-07-29 15:20:21 -0600325 }
326 }
327
328 fn add_dependency(&mut self, id: u8, version: &ImageVersion) {
David Brown7a81c4b2019-07-29 15:20:21 -0600329 self.dependencies.push(Dependency {
David Brown4dfb33c2021-03-10 05:15:45 -0700330 id,
David Brown7a81c4b2019-07-29 15:20:21 -0600331 version: version.clone(),
332 });
333 }
334
David Browne90b13f2019-12-06 15:04:00 -0700335 fn corrupt_sig(&mut self) {
336 self.gen_corrupted = true;
337 }
338
David Brownef4f0742021-10-22 17:09:50 -0600339 fn estimate_size(&self) -> usize {
340 // Begin the estimate with the 4 byte header.
341 let mut estimate = 4;
342 // A very poor estimate.
343
344 // Estimate the size of the image hash.
345 if self.kinds.contains(&TlvKinds::SHA256) {
346 estimate += 4 + 32;
347 }
348
349 // Add an estimate in for each of the signature algorithms.
350 if self.kinds.contains(&TlvKinds::RSA2048) {
351 estimate += 4 + 32; // keyhash
352 estimate += 4 + 256; // RSA2048
353 }
354 if self.kinds.contains(&TlvKinds::RSA3072) {
355 estimate += 4 + 32; // keyhash
356 estimate += 4 + 384; // RSA3072
357 }
358 if self.kinds.contains(&TlvKinds::ECDSA256) {
359 estimate += 4 + 32; // keyhash
360
361 // ECDSA signatures are encoded as ASN.1 with the x and y values stored as signed
362 // integers. As such, the size can vary by 2 bytes, if the 256-bit value has the high
363 // bit, it takes an extra 0 byte to avoid it being seen as a negative number.
364 estimate += 4 + 72; // ECDSA256 (varies)
365 }
366 if self.kinds.contains(&TlvKinds::ED25519) {
367 estimate += 4 + 32; // keyhash
368 estimate += 4 + 64; // ED25519 signature.
369 }
370
371 // Estimate encryption.
372 let flag = TlvFlags::ENCRYPTED_AES256 as u32;
373 let aes256 = (self.get_flags() & flag) == flag;
374
375 if self.kinds.contains(&TlvKinds::ENCRSA2048) {
376 estimate += 4 + 256;
377 }
378 if self.kinds.contains(&TlvKinds::ENCKW) {
379 estimate += 4 + if aes256 { 40 } else { 24 };
380 }
381 if self.kinds.contains(&TlvKinds::ENCEC256) {
382 estimate += 4 + if aes256 { 129 } else { 113 };
383 }
384 if self.kinds.contains(&TlvKinds::ENCX25519) {
385 estimate += 4 + if aes256 { 96 } else { 80 };
386 }
387
388 // Gather the size of the dependency information.
389 if self.protect_size() > 0 {
390 estimate += 4 + (16 * self.dependencies.len());
391 }
392
393 estimate
394 }
395
David Brown187dd882017-07-11 11:15:23 -0600396 /// Compute the TLV given the specified block of data.
David Brown43643dd2019-01-11 15:43:28 -0700397 fn make_tlv(self: Box<Self>) -> Vec<u8> {
David Brownef4f0742021-10-22 17:09:50 -0600398 let size_estimate = self.estimate_size();
399
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300400 let mut protected_tlv: Vec<u8> = vec![];
David Brown187dd882017-07-11 11:15:23 -0600401
David Brown2b73ed92020-01-08 17:01:22 -0700402 if self.protect_size() > 0 {
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300403 protected_tlv.push(0x08);
404 protected_tlv.push(0x69);
David Brown2b73ed92020-01-08 17:01:22 -0700405 let size = self.protect_size();
406 protected_tlv.write_u16::<LittleEndian>(size).unwrap();
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300407 for dep in &self.dependencies {
David Brown69721182019-12-04 14:50:52 -0700408 protected_tlv.write_u16::<LittleEndian>(TlvKinds::DEPENDENCY as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700409 protected_tlv.write_u16::<LittleEndian>(12).unwrap();
David Brownf5b33d82017-09-01 10:58:27 -0600410
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300411 // The dependency.
412 protected_tlv.push(dep.id);
David Brownf66b2052021-03-10 05:26:36 -0700413 protected_tlv.push(0);
414 protected_tlv.write_u16::<LittleEndian>(0).unwrap();
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300415 protected_tlv.push(dep.version.major);
416 protected_tlv.push(dep.version.minor);
417 protected_tlv.write_u16::<LittleEndian>(dep.version.revision).unwrap();
418 protected_tlv.write_u32::<LittleEndian>(dep.version.build_num).unwrap();
David Brown7a81c4b2019-07-29 15:20:21 -0600419 }
David Brown2b73ed92020-01-08 17:01:22 -0700420
421 assert_eq!(size, protected_tlv.len() as u16, "protected TLV length incorrect");
David Brown7a81c4b2019-07-29 15:20:21 -0600422 }
423
424 // Ring does the signature itself, which means that it must be
425 // given a full, contiguous payload. Although this does help from
426 // a correct usage perspective, it is fairly stupid from an
427 // efficiency view. If this is shown to be a performance issue
428 // with the tests, the protected data could be appended to the
429 // payload, and then removed after the signature is done. For now,
430 // just make a signed payload.
431 let mut sig_payload = self.payload.clone();
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300432 sig_payload.extend_from_slice(&protected_tlv);
433
434 let mut result: Vec<u8> = vec![];
435
436 // add back signed payload
437 result.extend_from_slice(&protected_tlv);
438
439 // add non-protected payload
David Brown3dc86c92020-01-08 17:22:55 -0700440 let npro_pos = result.len();
Fabio Utzigea3d3ab2019-09-11 19:35:33 -0300441 result.push(0x07);
442 result.push(0x69);
David Brown3dc86c92020-01-08 17:22:55 -0700443 // Placeholder for the size.
444 result.write_u16::<LittleEndian>(0).unwrap();
David Brown7a81c4b2019-07-29 15:20:21 -0600445
David Brown187dd882017-07-11 11:15:23 -0600446 if self.kinds.contains(&TlvKinds::SHA256) {
David Browne90b13f2019-12-06 15:04:00 -0700447 // If a signature is not requested, corrupt the hash we are
448 // generating. But, if there is a signature, output the
449 // correct hash. We want the hash test to pass so that the
450 // signature verification can be validated.
451 let mut corrupt_hash = self.gen_corrupted;
452 for k in &[TlvKinds::RSA2048, TlvKinds::RSA3072,
Roland Mikhel75c7c312023-02-23 15:39:14 +0100453 TlvKinds::ECDSA256, TlvKinds::ED25519]
David Browne90b13f2019-12-06 15:04:00 -0700454 {
455 if self.kinds.contains(k) {
456 corrupt_hash = false;
457 break;
458 }
459 }
460
461 if corrupt_hash {
462 sig_payload[0] ^= 1;
463 }
464
David Brown7a81c4b2019-07-29 15:20:21 -0600465 let hash = digest::digest(&digest::SHA256, &sig_payload);
David Brown8054ce22017-07-11 12:12:09 -0600466 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -0600467
David Brown8054ce22017-07-11 12:12:09 -0600468 assert!(hash.len() == 32);
David Brown69721182019-12-04 14:50:52 -0700469 result.write_u16::<LittleEndian>(TlvKinds::SHA256 as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700470 result.write_u16::<LittleEndian>(32).unwrap();
David Brown8054ce22017-07-11 12:12:09 -0600471 result.extend_from_slice(hash);
David Browne90b13f2019-12-06 15:04:00 -0700472
473 // Undo the corruption.
474 if corrupt_hash {
475 sig_payload[0] ^= 1;
476 }
477
478 }
479
480 if self.gen_corrupted {
481 // Corrupt what is signed by modifying the input to the
482 // signature code.
483 sig_payload[0] ^= 1;
David Brown187dd882017-07-11 11:15:23 -0600484 }
485
Fabio Utzig39297432019-05-08 18:51:10 -0300486 if self.kinds.contains(&TlvKinds::RSA2048) ||
487 self.kinds.contains(&TlvKinds::RSA3072) {
488
489 let is_rsa2048 = self.kinds.contains(&TlvKinds::RSA2048);
490
David Brown43cda332017-09-01 09:53:23 -0600491 // Output the hash of the public key.
Fabio Utzig39297432019-05-08 18:51:10 -0300492 let hash = if is_rsa2048 {
493 digest::digest(&digest::SHA256, RSA_PUB_KEY)
494 } else {
495 digest::digest(&digest::SHA256, RSA3072_PUB_KEY)
496 };
David Brown43cda332017-09-01 09:53:23 -0600497 let hash = hash.as_ref();
498
499 assert!(hash.len() == 32);
David Brown69721182019-12-04 14:50:52 -0700500 result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700501 result.write_u16::<LittleEndian>(32).unwrap();
David Brown43cda332017-09-01 09:53:23 -0600502 result.extend_from_slice(hash);
503
David Brown7e701d82017-07-11 13:24:25 -0600504 // For now assume PSS.
Fabio Utzig39297432019-05-08 18:51:10 -0300505 let key_bytes = if is_rsa2048 {
506 pem::parse(include_bytes!("../../root-rsa-2048.pem").as_ref()).unwrap()
507 } else {
508 pem::parse(include_bytes!("../../root-rsa-3072.pem").as_ref()).unwrap()
509 };
David Brown7e701d82017-07-11 13:24:25 -0600510 assert_eq!(key_bytes.tag, "RSA PRIVATE KEY");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300511 let key_pair = RsaKeyPair::from_der(&key_bytes.contents).unwrap();
David Brown7e701d82017-07-11 13:24:25 -0600512 let rng = rand::SystemRandom::new();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300513 let mut signature = vec![0; key_pair.public_modulus_len()];
Fabio Utzig39297432019-05-08 18:51:10 -0300514 if is_rsa2048 {
515 assert_eq!(signature.len(), 256);
516 } else {
517 assert_eq!(signature.len(), 384);
518 }
David Brown7a81c4b2019-07-29 15:20:21 -0600519 key_pair.sign(&RSA_PSS_SHA256, &rng, &sig_payload, &mut signature).unwrap();
David Brown7e701d82017-07-11 13:24:25 -0600520
Fabio Utzig39297432019-05-08 18:51:10 -0300521 if is_rsa2048 {
David Brown69721182019-12-04 14:50:52 -0700522 result.write_u16::<LittleEndian>(TlvKinds::RSA2048 as u16).unwrap();
Fabio Utzig39297432019-05-08 18:51:10 -0300523 } else {
David Brown69721182019-12-04 14:50:52 -0700524 result.write_u16::<LittleEndian>(TlvKinds::RSA3072 as u16).unwrap();
Fabio Utzig39297432019-05-08 18:51:10 -0300525 }
David Brown91d68632019-07-29 14:32:13 -0600526 result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
David Brown7e701d82017-07-11 13:24:25 -0600527 result.extend_from_slice(&signature);
528 }
529
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200530 if self.kinds.contains(&TlvKinds::ECDSA256) {
531 let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY);
532 let keyhash = keyhash.as_ref();
533
534 assert!(keyhash.len() == 32);
David Brown69721182019-12-04 14:50:52 -0700535 result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700536 result.write_u16::<LittleEndian>(32).unwrap();
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200537 result.extend_from_slice(keyhash);
538
Fabio Utzig05ab0142018-07-10 09:15:28 -0300539 let key_bytes = pem::parse(include_bytes!("../../root-ec-p256-pkcs8.pem").as_ref()).unwrap();
540 assert_eq!(key_bytes.tag, "PRIVATE KEY");
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200541
Fabio Utzig05ab0142018-07-10 09:15:28 -0300542 let key_pair = EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING,
Fabio Utzig90f449e2019-10-24 07:43:53 -0300543 &key_bytes.contents).unwrap();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300544 let rng = rand::SystemRandom::new();
Fabio Utzig90f449e2019-10-24 07:43:53 -0300545 let signature = key_pair.sign(&rng, &sig_payload).unwrap();
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200546
David Brown69721182019-12-04 14:50:52 -0700547 result.write_u16::<LittleEndian>(TlvKinds::ECDSA256 as u16).unwrap();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300548
David Browna4c58642019-12-12 17:28:33 -0700549 let signature = signature.as_ref().to_vec();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300550
David Brown91d68632019-07-29 14:32:13 -0600551 result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300552 result.extend_from_slice(signature.as_ref());
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200553 }
554
Fabio Utzig97710282019-05-24 17:44:49 -0300555 if self.kinds.contains(&TlvKinds::ED25519) {
556 let keyhash = digest::digest(&digest::SHA256, ED25519_PUB_KEY);
557 let keyhash = keyhash.as_ref();
558
559 assert!(keyhash.len() == 32);
David Brown69721182019-12-04 14:50:52 -0700560 result.write_u16::<LittleEndian>(TlvKinds::KEYHASH as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700561 result.write_u16::<LittleEndian>(32).unwrap();
Fabio Utzig97710282019-05-24 17:44:49 -0300562 result.extend_from_slice(keyhash);
563
David Brown7a81c4b2019-07-29 15:20:21 -0600564 let hash = digest::digest(&digest::SHA256, &sig_payload);
Fabio Utzig97710282019-05-24 17:44:49 -0300565 let hash = hash.as_ref();
566 assert!(hash.len() == 32);
567
568 let key_bytes = pem::parse(include_bytes!("../../root-ed25519.pem").as_ref()).unwrap();
569 assert_eq!(key_bytes.tag, "PRIVATE KEY");
570
Fabio Utzig90f449e2019-10-24 07:43:53 -0300571 let key_pair = Ed25519KeyPair::from_seed_and_public_key(
572 &key_bytes.contents[16..48], &ED25519_PUB_KEY[12..44]).unwrap();
Fabio Utzig97710282019-05-24 17:44:49 -0300573 let signature = key_pair.sign(&hash);
574
David Brown69721182019-12-04 14:50:52 -0700575 result.write_u16::<LittleEndian>(TlvKinds::ED25519 as u16).unwrap();
Fabio Utzig97710282019-05-24 17:44:49 -0300576
577 let signature = signature.as_ref().to_vec();
David Brown91d68632019-07-29 14:32:13 -0600578 result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
Fabio Utzig97710282019-05-24 17:44:49 -0300579 result.extend_from_slice(signature.as_ref());
580 }
581
Fabio Utzig1e48b912018-09-18 09:04:18 -0300582 if self.kinds.contains(&TlvKinds::ENCRSA2048) {
583 let key_bytes = pem::parse(include_bytes!("../../enc-rsa2048-pub.pem")
584 .as_ref()).unwrap();
585 assert_eq!(key_bytes.tag, "PUBLIC KEY");
586
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300587 let cipherkey = self.get_enc_key();
588 let cipherkey = cipherkey.as_slice();
589 let encbuf = match c::rsa_oaep_encrypt(&key_bytes.contents, cipherkey) {
Fabio Utzig1e48b912018-09-18 09:04:18 -0300590 Ok(v) => v,
591 Err(_) => panic!("Failed to encrypt secret key"),
592 };
593
594 assert!(encbuf.len() == 256);
David Brown69721182019-12-04 14:50:52 -0700595 result.write_u16::<LittleEndian>(TlvKinds::ENCRSA2048 as u16).unwrap();
David Brown4fae8b82019-12-05 11:26:59 -0700596 result.write_u16::<LittleEndian>(256).unwrap();
Fabio Utzig1e48b912018-09-18 09:04:18 -0300597 result.extend_from_slice(&encbuf);
598 }
599
Salome Thirot0f641972021-05-14 11:19:55 +0100600 if self.kinds.contains(&TlvKinds::ENCKW) {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100601 let flag = TlvFlags::ENCRYPTED_AES256 as u32;
602 let aes256 = (self.get_flags() & flag) == flag;
603 let key_bytes = if aes256 {
604 base64::decode(
605 include_str!("../../enc-aes256kw.b64").trim()).unwrap()
606 } else {
607 base64::decode(
608 include_str!("../../enc-aes128kw.b64").trim()).unwrap()
609 };
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300610 let cipherkey = self.get_enc_key();
611 let cipherkey = cipherkey.as_slice();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100612 let keylen = if aes256 { 32 } else { 16 };
613 let encbuf = match c::kw_encrypt(&key_bytes, cipherkey, keylen) {
Fabio Utzig1e48b912018-09-18 09:04:18 -0300614 Ok(v) => v,
615 Err(_) => panic!("Failed to encrypt secret key"),
616 };
617
Salome Thirot6fdbf552021-05-14 16:46:14 +0100618 let size = if aes256 { 40 } else { 24 };
619 assert!(encbuf.len() == size);
Salome Thirot0f641972021-05-14 11:19:55 +0100620 result.write_u16::<LittleEndian>(TlvKinds::ENCKW as u16).unwrap();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100621 result.write_u16::<LittleEndian>(size as u16).unwrap();
Fabio Utzig1e48b912018-09-18 09:04:18 -0300622 result.extend_from_slice(&encbuf);
623 }
624
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300625 if self.kinds.contains(&TlvKinds::ENCEC256) || self.kinds.contains(&TlvKinds::ENCX25519) {
626 let key_bytes = if self.kinds.contains(&TlvKinds::ENCEC256) {
627 pem::parse(include_bytes!("../../enc-ec256-pub.pem").as_ref()).unwrap()
628 } else {
629 pem::parse(include_bytes!("../../enc-x25519-pub.pem").as_ref()).unwrap()
630 };
Fabio Utzig90f449e2019-10-24 07:43:53 -0300631 assert_eq!(key_bytes.tag, "PUBLIC KEY");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300632 let rng = rand::SystemRandom::new();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300633 let alg = if self.kinds.contains(&TlvKinds::ENCEC256) {
634 &agreement::ECDH_P256
635 } else {
636 &agreement::X25519
637 };
638 let pk = match agreement::EphemeralPrivateKey::generate(alg, &rng) {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300639 Ok(v) => v,
640 Err(_) => panic!("Failed to generate ephemeral keypair"),
641 };
642
643 let pubk = match pk.compute_public_key() {
644 Ok(pubk) => pubk,
645 Err(_) => panic!("Failed computing ephemeral public key"),
646 };
647
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300648 let peer_pubk = if self.kinds.contains(&TlvKinds::ENCEC256) {
649 agreement::UnparsedPublicKey::new(&agreement::ECDH_P256, &key_bytes.contents[26..])
650 } else {
651 agreement::UnparsedPublicKey::new(&agreement::X25519, &key_bytes.contents[12..])
652 };
Fabio Utzig90f449e2019-10-24 07:43:53 -0300653
654 #[derive(Debug, PartialEq)]
655 struct OkmLen<T: core::fmt::Debug + PartialEq>(T);
656
657 impl hkdf::KeyType for OkmLen<usize> {
658 fn len(&self) -> usize {
659 self.0
660 }
661 }
662
Salome Thirot6fdbf552021-05-14 16:46:14 +0100663 let flag = TlvFlags::ENCRYPTED_AES256 as u32;
664 let aes256 = (self.get_flags() & flag) == flag;
665
Fabio Utzig90f449e2019-10-24 07:43:53 -0300666 let derived_key = match agreement::agree_ephemeral(
667 pk, &peer_pubk, ring::error::Unspecified, |shared| {
668 let salt = hkdf::Salt::new(hkdf::HKDF_SHA256, &[]);
669 let prk = salt.extract(&shared);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100670 let okm_len = if aes256 { 64 } else { 48 };
671 let okm = match prk.expand(&[b"MCUBoot_ECIES_v1"], OkmLen(okm_len)) {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300672 Ok(okm) => okm,
673 Err(_) => panic!("Failed building HKDF OKM"),
674 };
Salome Thirot6fdbf552021-05-14 16:46:14 +0100675 let mut buf = if aes256 { vec![0u8; 64] } else { vec![0u8; 48] };
Fabio Utzig90f449e2019-10-24 07:43:53 -0300676 match okm.fill(&mut buf) {
677 Ok(_) => Ok(buf),
678 Err(_) => panic!("Failed generating HKDF output"),
679 }
680 },
681 ) {
682 Ok(v) => v,
683 Err(_) => panic!("Failed building HKDF"),
684 };
685
Fabio Utzig90f449e2019-10-24 07:43:53 -0300686 let nonce = GenericArray::from_slice(&[0; 16]);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300687 let mut cipherkey = self.get_enc_key();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100688 if aes256 {
689 let key: &GenericArray<u8, U32> = GenericArray::from_slice(&derived_key[..32]);
David Brown9c6322f2021-08-19 13:03:39 -0600690 let block = Aes256::new(&key);
691 let mut cipher = Aes256Ctr::from_block_cipher(block, &nonce);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100692 cipher.apply_keystream(&mut cipherkey);
693 } else {
694 let key: &GenericArray<u8, U16> = GenericArray::from_slice(&derived_key[..16]);
David Brown9c6322f2021-08-19 13:03:39 -0600695 let block = Aes128::new(&key);
696 let mut cipher = Aes128Ctr::from_block_cipher(block, &nonce);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100697 cipher.apply_keystream(&mut cipherkey);
698 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300699
Salome Thirot6fdbf552021-05-14 16:46:14 +0100700 let size = if aes256 { 32 } else { 16 };
701 let key = hmac::Key::new(hmac::HMAC_SHA256, &derived_key[size..]);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300702 let tag = hmac::sign(&key, &cipherkey);
703
704 let mut buf = vec![];
705 buf.append(&mut pubk.as_ref().to_vec());
706 buf.append(&mut tag.as_ref().to_vec());
707 buf.append(&mut cipherkey);
708
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300709 if self.kinds.contains(&TlvKinds::ENCEC256) {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100710 let size = if aes256 { 129 } else { 113 };
711 assert!(buf.len() == size);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300712 result.write_u16::<LittleEndian>(TlvKinds::ENCEC256 as u16).unwrap();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100713 result.write_u16::<LittleEndian>(size as u16).unwrap();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300714 } else {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100715 let size = if aes256 { 96 } else { 80 };
716 assert!(buf.len() == size);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300717 result.write_u16::<LittleEndian>(TlvKinds::ENCX25519 as u16).unwrap();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100718 result.write_u16::<LittleEndian>(size as u16).unwrap();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300719 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300720 result.extend_from_slice(&buf);
721 }
722
David Brown3dc86c92020-01-08 17:22:55 -0700723 // Patch the size back into the TLV header.
724 let size = (result.len() - npro_pos) as u16;
725 let mut size_buf = &mut result[npro_pos + 2 .. npro_pos + 4];
726 size_buf.write_u16::<LittleEndian>(size).unwrap();
727
David Brownb408b432021-10-27 15:55:39 -0600728 // ECDSA is stored as an ASN.1 integer. For a 128-bit value, this maximally results in 33
729 // bytes of storage for each of the two values. If the high bit is zero, it will take 32
730 // bytes, if the top 8 bits are zero, it will take 31 bits, and so on. The smaller size
731 // will occur with decreasing likelihood. We'll allow this to get a bit smaller, hopefully
732 // allowing the tests to pass with false failures rare. For this case, we'll handle up to
733 // the top 16 bits of both numbers being all zeros (1 in 2^32).
734 if !Caps::has_ecdsa() {
735 if size_estimate != result.len() {
736 panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len());
737 }
738 } else {
739 if size_estimate < result.len() || size_estimate > result.len() + 6 {
740 panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len());
741 }
David Brownef4f0742021-10-22 17:09:50 -0600742 }
743 if size_estimate != result.len() {
744 log::warn!("Size off: {} actual {}", size_estimate, result.len());
745 }
746
David Brown187dd882017-07-11 11:15:23 -0600747 result
748 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300749
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300750 fn generate_enc_key(&mut self) {
751 let rng = rand::SystemRandom::new();
Salome Thirot6fdbf552021-05-14 16:46:14 +0100752 let flag = TlvFlags::ENCRYPTED_AES256 as u32;
753 let aes256 = (self.get_flags() & flag) == flag;
754 let mut buf = if aes256 {
755 vec![0u8; 32]
756 } else {
757 vec![0u8; 16]
758 };
David Brown26edaf32021-03-10 05:27:38 -0700759 if rng.fill(&mut buf).is_err() {
760 panic!("Error generating encrypted key");
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300761 }
762 info!("New encryption key: {:02x?}", buf);
763 self.enc_key = buf;
Fabio Utzig90f449e2019-10-24 07:43:53 -0300764 }
765
766 fn get_enc_key(&self) -> Vec<u8> {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100767 if self.enc_key.len() != 32 && self.enc_key.len() != 16 {
Fabio Utzige84f0ef2019-11-22 12:29:32 -0300768 panic!("No random key was generated");
769 }
770 self.enc_key.clone()
Fabio Utzig90f449e2019-10-24 07:43:53 -0300771 }
David Brown187dd882017-07-11 11:15:23 -0600772}
David Brown43cda332017-09-01 09:53:23 -0600773
774include!("rsa_pub_key-rs.txt");
Fabio Utzig39297432019-05-08 18:51:10 -0300775include!("rsa3072_pub_key-rs.txt");
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200776include!("ecdsa_pub_key-rs.txt");
Fabio Utzig97710282019-05-24 17:44:49 -0300777include!("ed25519_pub_key-rs.txt");