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 | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 16 | #[repr(u8)] |
| 17 | #[derive(Copy, Clone, PartialEq, Eq)] |
| 18 | #[allow(dead_code)] // TODO: For now |
| 19 | pub enum TlvKinds { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 20 | KEYHASH = 0x01, |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 21 | SHA256 = 0x10, |
| 22 | RSA2048 = 0x20, |
| 23 | ECDSA224 = 0x21, |
| 24 | ECDSA256 = 0x22, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | pub struct TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 28 | flags: u32, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 29 | kinds: Vec<TlvKinds>, |
| 30 | size: u16, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 31 | payload: Vec<u8>, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | impl TlvGen { |
| 35 | /// 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] | 36 | #[allow(dead_code)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 37 | pub fn new_hash_only() -> TlvGen { |
| 38 | TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 39 | flags: 0, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 40 | kinds: vec![TlvKinds::SHA256], |
| 41 | size: 4 + 32, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 42 | payload: vec![], |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 46 | #[allow(dead_code)] |
| 47 | pub fn new_rsa_pss() -> TlvGen { |
| 48 | TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 49 | flags: 0, |
| 50 | kinds: vec![TlvKinds::SHA256, TlvKinds::KEYHASH, TlvKinds::RSA2048], |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 51 | size: 4 + 32 + 4 + 256, |
| 52 | payload: vec![], |
| 53 | } |
| 54 | } |
| 55 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 56 | /// Retrieve the header flags for this configuration. This can be called at any time. |
| 57 | pub fn get_flags(&self) -> u32 { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 58 | self.flags |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 59 | } |
| 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 Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 68 | self.payload.extend_from_slice(bytes); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// Compute the TLV given the specified block of data. |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 72 | pub fn make_tlv(self) -> Vec<u8> { |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 73 | let mut result: Vec<u8> = vec![]; |
| 74 | |
| 75 | if self.kinds.contains(&TlvKinds::SHA256) { |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 76 | let hash = digest::digest(&digest::SHA256, &self.payload); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 77 | let hash = hash.as_ref(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 78 | |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 79 | assert!(hash.len() == 32); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 80 | result.push(TlvKinds::SHA256 as u8); |
| 81 | result.push(0); |
| 82 | result.push(32); |
| 83 | result.push(0); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 84 | result.extend_from_slice(hash); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 85 | } |
| 86 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 87 | if self.kinds.contains(&TlvKinds::RSA2048) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 88 | // 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 Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 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 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame^] | 120 | |
| 121 | include!("rsa_pub_key-rs.txt"); |