David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 1 | //! 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 Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 11 | use std::sync::Arc; |
| 12 | use pem; |
| 13 | use ring::{digest, rand, signature}; |
| 14 | use untrusted; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 15 | |
David Brown | 1e15859 | 2017-07-11 12:29:25 -0600 | [diff] [blame] | 16 | bitflags! { |
| 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 Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | #[repr(u8)] |
| 29 | #[derive(Copy, Clone, PartialEq, Eq)] |
| 30 | #[allow(dead_code)] // TODO: For now |
| 31 | pub enum TlvKinds { |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 32 | SHA256 = 0x10, |
| 33 | RSA2048 = 0x20, |
| 34 | ECDSA224 = 0x21, |
| 35 | ECDSA256 = 0x22, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | pub struct TlvGen { |
| 39 | flags: Flags, |
| 40 | kinds: Vec<TlvKinds>, |
| 41 | size: u16, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 42 | payload: Vec<u8>, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | impl TlvGen { |
| 46 | /// 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] | 47 | #[allow(dead_code)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 48 | pub fn new_hash_only() -> TlvGen { |
| 49 | TlvGen { |
David Brown | 1e15859 | 2017-07-11 12:29:25 -0600 | [diff] [blame] | 50 | flags: FLAG_SHA256, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 51 | kinds: vec![TlvKinds::SHA256], |
| 52 | size: 4 + 32, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 53 | payload: vec![], |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 57 | #[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 Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 67 | /// Retrieve the header flags for this configuration. This can be called at any time. |
| 68 | pub fn get_flags(&self) -> u32 { |
David Brown | 1e15859 | 2017-07-11 12:29:25 -0600 | [diff] [blame] | 69 | self.flags.bits() |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 70 | } |
| 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 Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 79 | self.payload.extend_from_slice(bytes); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// Compute the TLV given the specified block of data. |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 83 | pub fn make_tlv(self) -> Vec<u8> { |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 84 | let mut result: Vec<u8> = vec![]; |
| 85 | |
| 86 | if self.kinds.contains(&TlvKinds::SHA256) { |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 87 | let hash = digest::digest(&digest::SHA256, &self.payload); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 88 | let hash = hash.as_ref(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 89 | |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 90 | assert!(hash.len() == 32); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 91 | result.push(TlvKinds::SHA256 as u8); |
| 92 | result.push(0); |
| 93 | result.push(32); |
| 94 | result.push(0); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 95 | result.extend_from_slice(hash); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 96 | } |
| 97 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 98 | 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 Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 117 | result |
| 118 | } |
| 119 | } |