blob: f4c88b09389b2b8b88097f6f3e8e31f202f9189e [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 std::sync::Arc;
12use pem;
13use ring::{digest, rand, signature};
14use untrusted;
David Brown187dd882017-07-11 11:15:23 -060015
David Brown1e158592017-07-11 12:29:25 -060016bitflags! {
17 struct Flags: u32 {
18 const FLAG_PIC = 0x000001;
19 const FLAG_SHA256 = 0x000002;
20 const FLAG_PKCS15_RSA2048_SHA256 = 0x000004;
21 const FLAG_ECDSA224_SHA256 = 0x000008;
22 const FLAG_NON_BOOTABLE = 0x000010;
23 const FLAG_ECDSA256_SHA256 = 0x000020;
24 const FLAG_PKCS1_PSS_RSA2048_SHA256 = 0x000040;
25 }
David Brown187dd882017-07-11 11:15:23 -060026}
27
28#[repr(u8)]
29#[derive(Copy, Clone, PartialEq, Eq)]
30#[allow(dead_code)] // TODO: For now
31pub enum TlvKinds {
David Brown27648b82017-08-31 10:40:29 -060032 SHA256 = 0x10,
33 RSA2048 = 0x20,
34 ECDSA224 = 0x21,
35 ECDSA256 = 0x22,
David Brown187dd882017-07-11 11:15:23 -060036}
37
38pub struct TlvGen {
39 flags: Flags,
40 kinds: Vec<TlvKinds>,
41 size: u16,
David Brown4243ab02017-07-11 12:24:23 -060042 payload: Vec<u8>,
David Brown187dd882017-07-11 11:15:23 -060043}
44
45impl TlvGen {
46 /// Construct a new tlv generator that will only contain a hash of the data.
David Brown7e701d82017-07-11 13:24:25 -060047 #[allow(dead_code)]
David Brown187dd882017-07-11 11:15:23 -060048 pub fn new_hash_only() -> TlvGen {
49 TlvGen {
David Brown1e158592017-07-11 12:29:25 -060050 flags: FLAG_SHA256,
David Brown187dd882017-07-11 11:15:23 -060051 kinds: vec![TlvKinds::SHA256],
52 size: 4 + 32,
David Brown4243ab02017-07-11 12:24:23 -060053 payload: vec![],
David Brown187dd882017-07-11 11:15:23 -060054 }
55 }
56
David Brown7e701d82017-07-11 13:24:25 -060057 #[allow(dead_code)]
58 pub fn new_rsa_pss() -> TlvGen {
59 TlvGen {
60 flags: FLAG_SHA256 | FLAG_PKCS1_PSS_RSA2048_SHA256,
61 kinds: vec![TlvKinds::SHA256, TlvKinds::RSA2048],
62 size: 4 + 32 + 4 + 256,
63 payload: vec![],
64 }
65 }
66
David Brown187dd882017-07-11 11:15:23 -060067 /// Retrieve the header flags for this configuration. This can be called at any time.
68 pub fn get_flags(&self) -> u32 {
David Brown1e158592017-07-11 12:29:25 -060069 self.flags.bits()
David Brown187dd882017-07-11 11:15:23 -060070 }
71
72 /// Retrieve the size that the TLV will occupy. This can be called at any time.
73 pub fn get_size(&self) -> u16 {
74 self.size
75 }
76
77 /// Add bytes to the covered hash.
78 pub fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -060079 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -060080 }
81
82 /// Compute the TLV given the specified block of data.
David Brown8054ce22017-07-11 12:12:09 -060083 pub fn make_tlv(self) -> Vec<u8> {
David Brown187dd882017-07-11 11:15:23 -060084 let mut result: Vec<u8> = vec![];
85
86 if self.kinds.contains(&TlvKinds::SHA256) {
David Brown4243ab02017-07-11 12:24:23 -060087 let hash = digest::digest(&digest::SHA256, &self.payload);
David Brown8054ce22017-07-11 12:12:09 -060088 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -060089
David Brown8054ce22017-07-11 12:12:09 -060090 assert!(hash.len() == 32);
David Brown187dd882017-07-11 11:15:23 -060091 result.push(TlvKinds::SHA256 as u8);
92 result.push(0);
93 result.push(32);
94 result.push(0);
David Brown8054ce22017-07-11 12:12:09 -060095 result.extend_from_slice(hash);
David Brown187dd882017-07-11 11:15:23 -060096 }
97
David Brown7e701d82017-07-11 13:24:25 -060098 if self.kinds.contains(&TlvKinds::RSA2048) {
99 // For now assume PSS.
100 let key_bytes = pem::parse(include_bytes!("../../root-rsa-2048.pem").as_ref()).unwrap();
101 assert_eq!(key_bytes.tag, "RSA PRIVATE KEY");
102 let key_bytes = untrusted::Input::from(&key_bytes.contents);
103 let key = signature::RSAKeyPair::from_der(key_bytes).unwrap();
104 let mut signer = signature::RSASigningState::new(Arc::new(key)).unwrap();
105 let rng = rand::SystemRandom::new();
106 let mut signature = vec![0; signer.key_pair().public_modulus_len()];
107 assert_eq!(signature.len(), 256);
108 signer.sign(&signature::RSA_PSS_SHA256, &rng, &self.payload, &mut signature).unwrap();
109
110 result.push(TlvKinds::RSA2048 as u8);
111 result.push(0);
112 result.push((signature.len() & 0xFF) as u8);
113 result.push(((signature.len() >> 8) & 0xFF) as u8);
114 result.extend_from_slice(&signature);
115 }
116
David Brown187dd882017-07-11 11:15:23 -0600117 result
118 }
119}