blob: f637443b19420c9137a1bcce0a147ebd1a838888 [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 Brown8054ce22017-07-11 12:12:09 -060011use ring::digest;
David Brown187dd882017-07-11 11:15:23 -060012
13#[derive(EnumFlags, Copy, Clone, Debug)]
14#[repr(u32)]
15#[allow(non_camel_case_types)]
16pub enum Flags {
17 PIC = 0x000001,
18 SHA256 = 0x000002,
19 PKCS15_RSA2048_SHA256 = 0x000004,
20 ECDSA224_SHA256 = 0x000008,
21 NON_BOOTABLE = 0x000010,
22 ECDSA256_SHA256 = 0x000020,
23 PKCS1_PSS_RSA2048_SHA256 = 0x000040,
24}
25
26#[repr(u8)]
27#[derive(Copy, Clone, PartialEq, Eq)]
28#[allow(dead_code)] // TODO: For now
29pub enum TlvKinds {
30 SHA256 = 1,
31 RSA2048 = 2,
32 ECDSA224 = 3,
33 ECDSA256 = 4,
34}
35
36pub struct TlvGen {
37 flags: Flags,
38 kinds: Vec<TlvKinds>,
39 size: u16,
David Brown4243ab02017-07-11 12:24:23 -060040 payload: Vec<u8>,
David Brown187dd882017-07-11 11:15:23 -060041}
42
43impl TlvGen {
44 /// Construct a new tlv generator that will only contain a hash of the data.
45 pub fn new_hash_only() -> TlvGen {
46 TlvGen {
47 flags: Flags::SHA256,
48 kinds: vec![TlvKinds::SHA256],
49 size: 4 + 32,
David Brown4243ab02017-07-11 12:24:23 -060050 payload: vec![],
David Brown187dd882017-07-11 11:15:23 -060051 }
52 }
53
54 /// Retrieve the header flags for this configuration. This can be called at any time.
55 pub fn get_flags(&self) -> u32 {
56 self.flags as u32
57 }
58
59 /// Retrieve the size that the TLV will occupy. This can be called at any time.
60 pub fn get_size(&self) -> u16 {
61 self.size
62 }
63
64 /// Add bytes to the covered hash.
65 pub fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -060066 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -060067 }
68
69 /// Compute the TLV given the specified block of data.
David Brown8054ce22017-07-11 12:12:09 -060070 pub fn make_tlv(self) -> Vec<u8> {
David Brown187dd882017-07-11 11:15:23 -060071 let mut result: Vec<u8> = vec![];
72
73 if self.kinds.contains(&TlvKinds::SHA256) {
David Brown4243ab02017-07-11 12:24:23 -060074 let hash = digest::digest(&digest::SHA256, &self.payload);
David Brown8054ce22017-07-11 12:12:09 -060075 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -060076
David Brown8054ce22017-07-11 12:12:09 -060077 assert!(hash.len() == 32);
David Brown187dd882017-07-11 11:15:23 -060078 result.push(TlvKinds::SHA256 as u8);
79 result.push(0);
80 result.push(32);
81 result.push(0);
David Brown8054ce22017-07-11 12:12:09 -060082 result.extend_from_slice(hash);
David Brown187dd882017-07-11 11:15:23 -060083 }
84
85 result
86 }
87}