blob: c9e4a6df1700a39e9fd1bde19ba1e52eb371bb15 [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 Brown187dd882017-07-11 11:15:23 -060016#[repr(u8)]
17#[derive(Copy, Clone, PartialEq, Eq)]
18#[allow(dead_code)] // TODO: For now
19pub enum TlvKinds {
David Brown43cda332017-09-01 09:53:23 -060020 KEYHASH = 0x01,
David Brown27648b82017-08-31 10:40:29 -060021 SHA256 = 0x10,
22 RSA2048 = 0x20,
23 ECDSA224 = 0x21,
24 ECDSA256 = 0x22,
David Brown187dd882017-07-11 11:15:23 -060025}
26
27pub struct TlvGen {
David Brown43cda332017-09-01 09:53:23 -060028 flags: u32,
David Brown187dd882017-07-11 11:15:23 -060029 kinds: Vec<TlvKinds>,
30 size: u16,
David Brown4243ab02017-07-11 12:24:23 -060031 payload: Vec<u8>,
David Brown187dd882017-07-11 11:15:23 -060032}
33
34impl TlvGen {
35 /// Construct a new tlv generator that will only contain a hash of the data.
David Brown7e701d82017-07-11 13:24:25 -060036 #[allow(dead_code)]
David Brown187dd882017-07-11 11:15:23 -060037 pub fn new_hash_only() -> TlvGen {
38 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060039 flags: 0,
David Brown187dd882017-07-11 11:15:23 -060040 kinds: vec![TlvKinds::SHA256],
41 size: 4 + 32,
David Brown4243ab02017-07-11 12:24:23 -060042 payload: vec![],
David Brown187dd882017-07-11 11:15:23 -060043 }
44 }
45
David Brown7e701d82017-07-11 13:24:25 -060046 #[allow(dead_code)]
47 pub fn new_rsa_pss() -> TlvGen {
48 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060049 flags: 0,
50 kinds: vec![TlvKinds::SHA256, TlvKinds::KEYHASH, TlvKinds::RSA2048],
David Brown7e701d82017-07-11 13:24:25 -060051 size: 4 + 32 + 4 + 256,
52 payload: vec![],
53 }
54 }
55
David Brown187dd882017-07-11 11:15:23 -060056 /// Retrieve the header flags for this configuration. This can be called at any time.
57 pub fn get_flags(&self) -> u32 {
David Brown43cda332017-09-01 09:53:23 -060058 self.flags
David Brown187dd882017-07-11 11:15:23 -060059 }
60
61 /// Retrieve the size that the TLV will occupy. This can be called at any time.
62 pub fn get_size(&self) -> u16 {
63 self.size
64 }
65
66 /// Add bytes to the covered hash.
67 pub fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -060068 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -060069 }
70
71 /// Compute the TLV given the specified block of data.
David Brown8054ce22017-07-11 12:12:09 -060072 pub fn make_tlv(self) -> Vec<u8> {
David Brown187dd882017-07-11 11:15:23 -060073 let mut result: Vec<u8> = vec![];
74
75 if self.kinds.contains(&TlvKinds::SHA256) {
David Brown4243ab02017-07-11 12:24:23 -060076 let hash = digest::digest(&digest::SHA256, &self.payload);
David Brown8054ce22017-07-11 12:12:09 -060077 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -060078
David Brown8054ce22017-07-11 12:12:09 -060079 assert!(hash.len() == 32);
David Brown187dd882017-07-11 11:15:23 -060080 result.push(TlvKinds::SHA256 as u8);
81 result.push(0);
82 result.push(32);
83 result.push(0);
David Brown8054ce22017-07-11 12:12:09 -060084 result.extend_from_slice(hash);
David Brown187dd882017-07-11 11:15:23 -060085 }
86
David Brown7e701d82017-07-11 13:24:25 -060087 if self.kinds.contains(&TlvKinds::RSA2048) {
David Brown43cda332017-09-01 09:53:23 -060088 // Output the hash of the public key.
89 let hash = digest::digest(&digest::SHA256, RSA_PUB_KEY);
90 let hash = hash.as_ref();
91
92 assert!(hash.len() == 32);
93 result.push(TlvKinds::KEYHASH as u8);
94 result.push(0);
95 result.push(32);
96 result.push(0);
97 result.extend_from_slice(hash);
98
David Brown7e701d82017-07-11 13:24:25 -060099 // 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}
David Brown43cda332017-09-01 09:53:23 -0600120
121include!("rsa_pub_key-rs.txt");