blob: 1ff7b75c743fdc030e6bb5614f8ba1ee9db4350e [file] [log] [blame]
David Brown187dd882017-07-11 11:15:23 -06001//! TLV Support
2//!
3//! mcuboot images are followed immediately by a list of TLV items that contain integrity
4//! information about the image. Their generation is made a little complicated because the size of
5//! the TLV block is in the image header, which is included in the hash. Since some signatures can
6//! vary in size, we just make them the largest size possible.
7//!
8//! Because of this header, we have to make two passes. The first pass will compute the size of
9//! the TLV, and the second pass will build the data for the TLV.
10
David Brown7e701d82017-07-11 13:24:25 -060011use pem;
Fabio Utzig1e48b912018-09-18 09:04:18 -030012use base64;
Fabio Utzig05ab0142018-07-10 09:15:28 -030013use ring::{digest, rand};
14use ring::signature::{
15 RsaKeyPair,
16 RSA_PSS_SHA256,
17 EcdsaKeyPair,
18 ECDSA_P256_SHA256_ASN1_SIGNING,
19};
David Brown7e701d82017-07-11 13:24:25 -060020use untrusted;
Fabio Utzig80fde2f2017-12-05 09:25:31 -020021use mcuboot_sys::c;
David Brown187dd882017-07-11 11:15:23 -060022
David Brown187dd882017-07-11 11:15:23 -060023#[repr(u8)]
24#[derive(Copy, Clone, PartialEq, Eq)]
25#[allow(dead_code)] // TODO: For now
26pub enum TlvKinds {
David Brown43cda332017-09-01 09:53:23 -060027 KEYHASH = 0x01,
David Brown27648b82017-08-31 10:40:29 -060028 SHA256 = 0x10,
29 RSA2048 = 0x20,
30 ECDSA224 = 0x21,
31 ECDSA256 = 0x22,
Fabio Utzig1e48b912018-09-18 09:04:18 -030032 ENCRSA2048 = 0x30,
33 ENCKW128 = 0x31,
34}
35
36#[allow(dead_code, non_camel_case_types)]
37pub enum TlvFlags {
38 PIC = 0x01,
39 NON_BOOTABLE = 0x02,
40 ENCRYPTED = 0x04,
41 RAM_LOAD = 0x20,
David Brown187dd882017-07-11 11:15:23 -060042}
43
David Brown43643dd2019-01-11 15:43:28 -070044/// A generator for manifests. The format of the manifest can be either a
45/// traditional "TLV" or a SUIT-style manifest.
46pub trait ManifestGen {
47 /// Retrieve the flags value for this particular manifest type.
48 fn get_flags(&self) -> u32;
49
50 /// Add a sequence of bytes to the payload that the manifest is
51 /// protecting.
52 fn add_bytes(&mut self, bytes: &[u8]);
53
54 /// Construct the manifest for this payload.
55 fn make_tlv(self: Box<Self>) -> Vec<u8>;
56}
57
David Brown187dd882017-07-11 11:15:23 -060058pub struct TlvGen {
David Brown43cda332017-09-01 09:53:23 -060059 flags: u32,
David Brown187dd882017-07-11 11:15:23 -060060 kinds: Vec<TlvKinds>,
61 size: u16,
David Brown4243ab02017-07-11 12:24:23 -060062 payload: Vec<u8>,
David Brown187dd882017-07-11 11:15:23 -060063}
64
Fabio Utzig1e48b912018-09-18 09:04:18 -030065pub const AES_SEC_KEY: &[u8; 16] = b"0123456789ABCDEF";
66
David Brown187dd882017-07-11 11:15:23 -060067impl TlvGen {
68 /// Construct a new tlv generator that will only contain a hash of the data.
David Brown7e701d82017-07-11 13:24:25 -060069 #[allow(dead_code)]
David Brown187dd882017-07-11 11:15:23 -060070 pub fn new_hash_only() -> TlvGen {
71 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060072 flags: 0,
David Brown187dd882017-07-11 11:15:23 -060073 kinds: vec![TlvKinds::SHA256],
74 size: 4 + 32,
David Brown4243ab02017-07-11 12:24:23 -060075 payload: vec![],
David Brown187dd882017-07-11 11:15:23 -060076 }
77 }
78
David Brown7e701d82017-07-11 13:24:25 -060079 #[allow(dead_code)]
80 pub fn new_rsa_pss() -> TlvGen {
81 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060082 flags: 0,
Fabio Utzig754438d2018-12-14 06:39:58 -020083 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048],
84 size: 4 + 32 + 4 + 32 + 4 + 256,
David Brown7e701d82017-07-11 13:24:25 -060085 payload: vec![],
86 }
87 }
88
Fabio Utzig80fde2f2017-12-05 09:25:31 -020089 #[allow(dead_code)]
90 pub fn new_ecdsa() -> TlvGen {
91 TlvGen {
92 flags: 0,
Fabio Utzig754438d2018-12-14 06:39:58 -020093 kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSA256],
94 size: 4 + 32 + 4 + 32 + 4 + 72,
Fabio Utzig80fde2f2017-12-05 09:25:31 -020095 payload: vec![],
96 }
97 }
98
Fabio Utzig1e48b912018-09-18 09:04:18 -030099 #[allow(dead_code)]
100 pub fn new_enc_rsa() -> TlvGen {
101 TlvGen {
102 flags: TlvFlags::ENCRYPTED as u32,
103 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCRSA2048],
104 size: 4 + 32 + 4 + 256,
105 payload: vec![],
106 }
107 }
108
109 #[allow(dead_code)]
Fabio Utzig754438d2018-12-14 06:39:58 -0200110 pub fn new_sig_enc_rsa() -> TlvGen {
111 TlvGen {
112 flags: TlvFlags::ENCRYPTED as u32,
113 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCRSA2048],
114 size: 4 + 32 + 4 + 32 + 4 + 256 + 4 + 256,
115 payload: vec![],
116 }
117 }
118
119 #[allow(dead_code)]
Fabio Utzig1e48b912018-09-18 09:04:18 -0300120 pub fn new_enc_kw() -> TlvGen {
121 TlvGen {
122 flags: TlvFlags::ENCRYPTED as u32,
123 kinds: vec![TlvKinds::SHA256, TlvKinds::ENCKW128],
124 size: 4 + 32 + 4 + 24,
125 payload: vec![],
126 }
127 }
128
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200129 #[allow(dead_code)]
130 pub fn new_rsa_kw() -> TlvGen {
131 TlvGen {
132 flags: TlvFlags::ENCRYPTED as u32,
133 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048, TlvKinds::ENCKW128],
134 size: 4 + 32 + 4 + 32 + 4 + 256 + 4 + 24,
135 payload: vec![],
136 }
137 }
138
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200139 #[allow(dead_code)]
140 pub fn new_ecdsa_kw() -> TlvGen {
141 TlvGen {
142 flags: TlvFlags::ENCRYPTED as u32,
143 kinds: vec![TlvKinds::SHA256, TlvKinds::ECDSA256, TlvKinds::ENCKW128],
144 size: 4 + 32 + 4 + 32 + 4 + 72 + 4 + 24,
145 payload: vec![],
146 }
147 }
148
David Brown187dd882017-07-11 11:15:23 -0600149 /// Retrieve the size that the TLV will occupy. This can be called at any time.
150 pub fn get_size(&self) -> u16 {
David Brownf5b33d82017-09-01 10:58:27 -0600151 4 + self.size
David Brown187dd882017-07-11 11:15:23 -0600152 }
David Brown43643dd2019-01-11 15:43:28 -0700153}
154
155impl ManifestGen for TlvGen {
156 /// Retrieve the header flags for this configuration. This can be called at any time.
157 fn get_flags(&self) -> u32 {
158 self.flags
159 }
David Brown187dd882017-07-11 11:15:23 -0600160
161 /// Add bytes to the covered hash.
David Brown43643dd2019-01-11 15:43:28 -0700162 fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -0600163 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -0600164 }
165
166 /// Compute the TLV given the specified block of data.
David Brown43643dd2019-01-11 15:43:28 -0700167 fn make_tlv(self: Box<Self>) -> Vec<u8> {
David Brown187dd882017-07-11 11:15:23 -0600168 let mut result: Vec<u8> = vec![];
169
David Brownf5b33d82017-09-01 10:58:27 -0600170 let size = self.get_size();
171 result.push(0x07);
172 result.push(0x69);
173 result.push((size & 0xFF) as u8);
174 result.push(((size >> 8) & 0xFF) as u8);
175
David Brown187dd882017-07-11 11:15:23 -0600176 if self.kinds.contains(&TlvKinds::SHA256) {
David Brown4243ab02017-07-11 12:24:23 -0600177 let hash = digest::digest(&digest::SHA256, &self.payload);
David Brown8054ce22017-07-11 12:12:09 -0600178 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -0600179
David Brown8054ce22017-07-11 12:12:09 -0600180 assert!(hash.len() == 32);
David Brown187dd882017-07-11 11:15:23 -0600181 result.push(TlvKinds::SHA256 as u8);
182 result.push(0);
183 result.push(32);
184 result.push(0);
David Brown8054ce22017-07-11 12:12:09 -0600185 result.extend_from_slice(hash);
David Brown187dd882017-07-11 11:15:23 -0600186 }
187
David Brown7e701d82017-07-11 13:24:25 -0600188 if self.kinds.contains(&TlvKinds::RSA2048) {
David Brown43cda332017-09-01 09:53:23 -0600189 // Output the hash of the public key.
190 let hash = digest::digest(&digest::SHA256, RSA_PUB_KEY);
191 let hash = hash.as_ref();
192
193 assert!(hash.len() == 32);
194 result.push(TlvKinds::KEYHASH as u8);
195 result.push(0);
196 result.push(32);
197 result.push(0);
198 result.extend_from_slice(hash);
199
David Brown7e701d82017-07-11 13:24:25 -0600200 // For now assume PSS.
201 let key_bytes = pem::parse(include_bytes!("../../root-rsa-2048.pem").as_ref()).unwrap();
202 assert_eq!(key_bytes.tag, "RSA PRIVATE KEY");
203 let key_bytes = untrusted::Input::from(&key_bytes.contents);
Fabio Utzig05ab0142018-07-10 09:15:28 -0300204 let key_pair = RsaKeyPair::from_der(key_bytes).unwrap();
David Brown7e701d82017-07-11 13:24:25 -0600205 let rng = rand::SystemRandom::new();
Fabio Utzig05ab0142018-07-10 09:15:28 -0300206 let mut signature = vec![0; key_pair.public_modulus_len()];
David Brown7e701d82017-07-11 13:24:25 -0600207 assert_eq!(signature.len(), 256);
Fabio Utzig05ab0142018-07-10 09:15:28 -0300208 key_pair.sign(&RSA_PSS_SHA256, &rng, &self.payload, &mut signature).unwrap();
David Brown7e701d82017-07-11 13:24:25 -0600209
210 result.push(TlvKinds::RSA2048 as u8);
211 result.push(0);
212 result.push((signature.len() & 0xFF) as u8);
213 result.push(((signature.len() >> 8) & 0xFF) as u8);
214 result.extend_from_slice(&signature);
215 }
216
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200217 if self.kinds.contains(&TlvKinds::ECDSA256) {
218 let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY);
219 let keyhash = keyhash.as_ref();
220
221 assert!(keyhash.len() == 32);
222 result.push(TlvKinds::KEYHASH as u8);
223 result.push(0);
224 result.push(32);
225 result.push(0);
226 result.extend_from_slice(keyhash);
227
Fabio Utzig05ab0142018-07-10 09:15:28 -0300228 let key_bytes = pem::parse(include_bytes!("../../root-ec-p256-pkcs8.pem").as_ref()).unwrap();
229 assert_eq!(key_bytes.tag, "PRIVATE KEY");
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200230
Fabio Utzig05ab0142018-07-10 09:15:28 -0300231 let key_bytes = untrusted::Input::from(&key_bytes.contents);
232 let key_pair = EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING,
233 key_bytes).unwrap();
234 let rng = rand::SystemRandom::new();
235 let payload = untrusted::Input::from(&self.payload);
236 let signature = key_pair.sign(&rng, payload).unwrap();
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200237
238 result.push(TlvKinds::ECDSA256 as u8);
239 result.push(0);
Fabio Utzig05ab0142018-07-10 09:15:28 -0300240
241 // signature must be padded...
242 let mut signature = signature.as_ref().to_vec();
243 while signature.len() < 72 {
244 signature.push(0);
245 signature[1] += 1;
246 }
247
248 result.push((signature.len() & 0xFF) as u8);
249 result.push(((signature.len() >> 8) & 0xFF) as u8);
250 result.extend_from_slice(signature.as_ref());
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200251 }
252
Fabio Utzig1e48b912018-09-18 09:04:18 -0300253 if self.kinds.contains(&TlvKinds::ENCRSA2048) {
254 let key_bytes = pem::parse(include_bytes!("../../enc-rsa2048-pub.pem")
255 .as_ref()).unwrap();
256 assert_eq!(key_bytes.tag, "PUBLIC KEY");
257
258 let encbuf = match c::rsa_oaep_encrypt(&key_bytes.contents, AES_SEC_KEY) {
259 Ok(v) => v,
260 Err(_) => panic!("Failed to encrypt secret key"),
261 };
262
263 assert!(encbuf.len() == 256);
264 result.push(TlvKinds::ENCRSA2048 as u8);
265 result.push(0);
266 result.push(0);
267 result.push(1);
268 result.extend_from_slice(&encbuf);
269 }
270
271 if self.kinds.contains(&TlvKinds::ENCKW128) {
272 let key_bytes = base64::decode(
273 include_str!("../../enc-aes128kw.b64").trim()).unwrap();
274
275 let encbuf = match c::kw_encrypt(&key_bytes, AES_SEC_KEY) {
276 Ok(v) => v,
277 Err(_) => panic!("Failed to encrypt secret key"),
278 };
279
280 assert!(encbuf.len() == 24);
281 result.push(TlvKinds::ENCKW128 as u8);
282 result.push(0);
283 result.push(24);
284 result.push(0);
285 result.extend_from_slice(&encbuf);
286 }
287
David Brown187dd882017-07-11 11:15:23 -0600288 result
289 }
290}
David Brown43cda332017-09-01 09:53:23 -0600291
292include!("rsa_pub_key-rs.txt");
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200293include!("ecdsa_pub_key-rs.txt");