Debian Codegraph @

mbn/hashseg.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0-only AND BSD-3-Clause
2 # Copyright (C) 2021-2023 Stephan Gerhold (GPL-2.0-only)
3 # MBN header format adapted from:
4 # - signlk: https://git.linaro.org/landing-teams/working/qualcomm/signlk.git
5 # - coreboot (util/qualcomm/mbn_tools.py, util/cbfstool/platform_fixups.c)
6 # Copyright (c) 2016, 2018, The Linux Foundation. All rights reserved. (BSD-3-Clause)
7 # See also:
8 # - https://www.qualcomm.com/media/documents/files/secure-boot-and-image-authentication-technical-overview-v1-0.pdf
9 # - https://www.qualcomm.com/media/documents/files/secure-boot-and-image-authentication-technical-overview-v2-0.pdf
10 from __future__ import annotations
11
12 import dataclasses
13 import hashlib
14 from dataclasses import dataclass
15 from io import BytesIO
16 from struct import Struct
17
18 from . import cert
19 from . import elf
20
21 # A typical Qualcomm firmware might have the following program headers:
22 # LOAD off 0x00000800 vaddr 0x86400000 paddr 0x86400000 align 2**11
23 # filesz 0x00001000 memsz 0x00001000 flags rwx
24 #
25 # The signed version will then look like:
26 # NULL off 0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**0
27 # filesz 0x000000e8 memsz 0x00000000 flags --- 7000000
28 # NULL off 0x00001000 vaddr 0x86401000 paddr 0x86401000 align 2**12
29 # filesz 0x00000988 memsz 0x00001000 flags --- 2200000
30 # LOAD off 0x00002000 vaddr 0x86400000 paddr 0x86400000 align 2**11
31 # filesz 0x00001000 memsz 0x00001000 flags rwx
32 #
33 # The second NULL program header with off 0x1000 and filesz 0x988 is the actual
34 # "hash table segment" or shortly "hash segment" (see Figure 2 on page 6 in the PDF).
35 # It contains the MBN header specified below, then a couple of hashes (e.g. SHA256):
36 # 1. Hash of ELF header and program headers
37 # 2. Empty hash for hash segment
38 # 3. Hashes for data of each memory segment (described by program header)
39 # Finally, it contains an RSA signature and the concatenated certificate chain.
40 #
41 # The first NULL program header is never loaded anywhere, because
42 # vaddr = paddr = memsz = 0. However, the "off" and "filesz" cover exactly
43 # the ELF header (including all program headers). It is a placeholder so that
44 # each hash covers the data of exactly one program header.
45
46 # For definitions of the ELF PHDR flags used by Qualcomm, see:
47 # https://github.com/coreboot/coreboot/blob/812d0e2f626dfea7e7deb960a8dc08ff0e026bc1/util/qualcomm/mbn_tools.py#L108-L189
48 PHDR_FLAGS_SEGMENT_TYPE_MASK = 0x07000000
49 PHDR_FLAGS_SEGMENT_TYPE_SHIFT = 0x18
50 PHDR_FLAGS_SEGMENT_TYPE_HASH = (0x2 << PHDR_FLAGS_SEGMENT_TYPE_SHIFT)
51 PHDR_FLAGS_SEGMENT_TYPE_HDR = (0x7 << PHDR_FLAGS_SEGMENT_TYPE_SHIFT)
52
53 PDHR_FLAGS_ACCESS_TYPE_MASK = 0x00E00000
54 PHDR_FLAGS_ACCESS_TYPE_SHIFT = 0x15
55 PHDR_FLAGS_ACCESS_TYPE_RO = (0x1 << PHDR_FLAGS_ACCESS_TYPE_SHIFT)
56
57 # Flags we use for placeholder for hash over ELF header and hash segment
58 PHDR_FLAGS_HDR_PLACEHOLDER = PHDR_FLAGS_SEGMENT_TYPE_HDR
59 PHDR_FLAGS_HASH_SEGMENT = (PHDR_FLAGS_SEGMENT_TYPE_HASH | PHDR_FLAGS_ACCESS_TYPE_RO)
60
61 EXTRA_PHDRS = 2 # header placeholder + hash segment
62
63 # Note: None of the alignments seem to be truly required,
64 # this could probably be reduced to get smaller file sizes.
65 HASH_SEG_ALIGN = 0x1000
66 CERT_CHAIN_ALIGN = 16
67
68 # According to the v2.0 PDF the metadata is 128 bytes long, but this does not
69 # seem to work. All official firmware seems to use 120 bytes instead.
70 MBN_V6_METADATA_SIZE = 120
71
72 # See OEM Metadata 2.0 definition in coreboot source code:
73 # https://github.com/coreboot/coreboot/blob/812d0e2f626dfea7e7deb960a8dc08ff0e026bc1/util/qualcomm/mbn_tools.py#L506-L691
74 MBN_V7_OEM_2_0_METADATA_SIZE = 224
75
76
77 @dataclass
78 class _HashSegment:
79 image_id: int = 0 # Type of image (unused?)
80 version: int = 0 # Header version number
81
82 hash_size = 0
83 signature_size = 0
84 cert_chain_size = 0
85 total_size = 0
86
87 hashes = []
88 signature = b''
89 cert_chain = b''
90
91 FORMAT = Struct('<10L')
92 Hash = hashlib.sha256
93
94 @property
95 def size_with_header(self):
96 return self.FORMAT.size + self.total_size
97
98 def update(self, dest_addr: int):
99 self.hash_size = len(self.hashes) * self.Hash().digest_size
100 self.signature_size = len(self.signature)
101 self.cert_chain_size = len(self.cert_chain)
102 self.total_size = self.hash_size + self.signature_size + self.cert_chain_size
103
104 def check(self):
105 assert len(self.hashes) * self.Hash().digest_size == self.hash_size
106 assert len(self.signature) == self.signature_size
107 assert len(self.cert_chain) == self.cert_chain_size
108
109 def pack_header(self):
110 self.check()
111 return self.FORMAT.pack(*dataclasses.astuple(self))
112
113 def pack(self):
114 return self.pack_header() \
115 + b''.join(self.hashes) \
116 + self.signature + self.cert_chain
117
118
119 @dataclass
120 class HashSegmentV3(_HashSegment):
121 version: int = 3 # Header version number
122
123 flash_addr: int = 0 # Location of image in flash (historical)
124 dest_addr: int = 0 # Physical address of loaded hash segment data
125 total_size: int = 0 # = hash_size + signature_size + cert_chain_size
126 hash_size: int = 0 # Size of hashes for all program segments
127 signature_addr: int = 0 # Physical address of loaded attestation signature
128 signature_size: int = 0 # Size of attestation signature
129 cert_chain_addr: int = 0 # Physical address of loaded certificate chain
130 cert_chain_size: int = 0 # Size of certificate chain
131
132 def update(self, dest_addr: int):
133 super().update(dest_addr)
134 self.dest_addr = dest_addr + self.FORMAT.size
135 self.signature_addr = self.dest_addr + self.hash_size
136 self.cert_chain_addr = self.signature_addr + self.signature_size
137
138
139 @dataclass
140 class HashSegmentV5(_HashSegment):
141 version: int = 5 # Header version number
142
143 signature_size_qcom: int = 0 # Size of signature from Qualcomm
144 cert_chain_size_qcom: int = 0 # Size of certificate chain from Qualcomm
145 total_size: int = 0 # = hash_size + signature_size + cert_chain_size
146 hash_size: int = 0 # Size of hashes for all program segments
147 signature_addr: int = 0xffffffff # unused?
148 signature_size: int = 0 # Size of attestation signature
149 cert_chain_addr: int = 0xffffffff # unused?
150 cert_chain_size: int = 0 # Size of certificate chain
151
152 signature_qcom = b''
153 cert_chain_qcom = b''
154
155 def update(self, dest_addr: int):
156 super().update(dest_addr)
157 self.signature_size_qcom = len(self.signature_qcom)
158 self.cert_chain_size_qcom = len(self.cert_chain_qcom)
159 self.total_size += self.signature_size_qcom + self.cert_chain_size_qcom
160
161 def check(self):
162 super().check()
163 assert len(self.signature_qcom) == self.signature_size_qcom
164 assert len(self.cert_chain_qcom) == self.cert_chain_size_qcom
165
166 def pack(self):
167 return self.pack_header() \
168 + b''.join(self.hashes) \
169 + self.signature_qcom + self.cert_chain_qcom \
170 + self.signature + self.cert_chain
171
172
173 @dataclass
174 class HashSegmentV6(HashSegmentV5):
175 version: int = 6 # Header version number
176
177 metadata_size_qcom: int = 0 # Size of metadata from Qualcomm
178 metadata_size: int = 0 # Size of metadata
179
180 metadata_qcom = b''
181 metadata = b''
182
183 FORMAT = Struct('<12L')
184 Hash = hashlib.sha384
185
186 def update(self, dest_addr: int):
187 super().update(dest_addr)
188 self.metadata_size_qcom = len(self.metadata_qcom)
189 self.metadata_size = len(self.metadata)
190 self.total_size += self.metadata_size_qcom + self.metadata_size
191
192 def check(self):
193 super().check()
194 assert len(self.metadata_qcom) == self.metadata_size_qcom
195 assert len(self.metadata) == self.metadata_size
196
197 def pack(self):
198 return self.pack_header() \
199 + self.metadata_qcom + self.metadata \
200 + b''.join(self.hashes) \
201 + self.signature_qcom + self.cert_chain_qcom \
202 + self.signature + self.cert_chain
203
204
205 @dataclass
206 # Information from MBNv7 definition in Coreboot source code:
207 # https://github.com/coreboot/coreboot/blob/812d0e2f626dfea7e7deb960a8dc08ff0e026bc1/util/qualcomm/mbn_tools.py#L506-L691
208 class HashSegmentV7(_HashSegment):
209 version: int = 7 # Header version number
210
211 common_metadata_size: int = 24 # Size of "common metadata" below
212 metadata_size_qcom: int = 0 # Size of metadata from Qualcomm
213 metadata_size: int = 0 # Size of metadata from OEM
214 hash_size: int = 0 # Size of hashes for all program segments
215 signature_size_qcom: int = 0 # Size of signature from Qualcomm
216 cert_chain_size_qcom: int = 0 # Size of certificate chain from Qualcomm
217 signature_size: int = 0 # Size of attestation signature
218 cert_chain_size: int = 0 # Size of certificate chain
219
220 # Common metadata, placed directly after MBNv7 header
221 common_metadata_major_version: int = 0
222 common_metadata_minor_version: int = 0
223 software_id: int = 0 # Type of software image, mandatory
224 secondary_software_id: int = 0
225 hash_table_algorithm: int = 3 # SHA384
226 measurement_register_target: int = 0
227
228 metadata_qcom = b''
229 metadata = b''
230 signature_qcom = b''
231 cert_chain_qcom = b''
232
233 FORMAT = Struct('<16L')
234 Hash = hashlib.sha384
235
236 def update(self, dest_addr: int):
237 super().update(dest_addr)
238 self.metadata_size_qcom = len(self.metadata_qcom)
239 self.metadata_size = len(self.metadata)
240 self.signature_size_qcom = len(self.signature_qcom)
241 self.cert_chain_size_qcom = len(self.cert_chain_qcom)
242 # self.common_metadata_size is already included as part of the header
243 self.total_size += self.metadata_size_qcom + self.metadata_size
244 self.total_size += self.signature_size_qcom + self.cert_chain_size_qcom
245
246 def check(self):
247 super().check()
248 assert len(self.metadata_qcom) == self.metadata_size_qcom
249 assert len(self.metadata) == self.metadata_size
250 assert len(self.signature_qcom) == self.signature_size_qcom
251 assert len(self.cert_chain_qcom) == self.cert_chain_size_qcom
252
253 def pack(self):
254 return self.pack_header() \
255 + self.metadata_qcom + self.metadata \
256 + b''.join(self.hashes) \
257 + self.signature_qcom + self.cert_chain_qcom \
258 + self.signature + self.cert_chain
259
260 HashSegment = {
261 3: HashSegmentV3,
262 5: HashSegmentV5,
263 6: HashSegmentV6,
264 7: HashSegmentV7,
265 }
266
267
268 def drop(elff: elf.Elf):
269 # Drop existing hash segments
270 elff.phdrs = [phdr for phdr in elff.phdrs if phdr.p_type != elf.Phdr.PT_NULL
271 or (phdr.p_flags & PHDR_FLAGS_SEGMENT_TYPE_MASK) not in
272 [PHDR_FLAGS_SEGMENT_TYPE_HASH, PHDR_FLAGS_SEGMENT_TYPE_HDR]]
273
274
275 def generate(elff: elf.Elf, version: int, sw_id: int):
276 drop(elff)
277 assert elff.phdrs, "Need at least one program header"
278
279 hash_seg = HashSegment[version]()
280
281 if version == 6:
282 # TODO: Figure out metadata format and fill this with useful data
283 hash_seg.metadata = b'\0' * MBN_V6_METADATA_SIZE
284
285 # Software ID is mandatory for MBN v7
286 if version == 7:
287 hash_seg.software_id = sw_id
288 # The format is documented in Coreboot util/qualcomm/mbn_tools.py
289 # (see class Boot_Hdr), but for simplicity we just keep this empty.
290 hash_seg.metadata = b'\0' * MBN_V7_OEM_2_0_METADATA_SIZE
291
292 # Generate hash for all existing segments with data
293 digest_size = hash_seg.Hash().digest_size
294 hash_seg.hashes = [b'\0' * digest_size] * (len(elff.phdrs) + EXTRA_PHDRS)
295 for i, phdr in enumerate(elff.phdrs, start=EXTRA_PHDRS):
296 if phdr.data:
297 hash_seg.hashes[i] = hash_seg.Hash(phdr.data).digest()
298 total_hashes_size = len(hash_seg.hashes) * digest_size
299
300 # Generate certificate chain with specified OU fields (for < v6)
301 # on >= v6 this is part of the metadata instead
302 ou_fields = []
303 if version < 6:
304 ou_fields = [
305 # Note: The SW_ID is checked by the firmware on some platforms (even if secure boot
306 # is disabled), so it must match the firmware type being signed. Everything else seems
307 # to be mostly ignored when secure boot is off and is just added here to match the
308 # documentation and better mimic the official firmware.
309 "01 %016X SW_ID" % sw_id,
310 "02 %016X HW_ID" % 0,
311 "03 %016X DEBUG" % 2, # DISABLED
312 "04 %04X OEM_ID" % 0,
313 "05 %08X SW_SIZE" % (hash_seg.FORMAT.size + total_hashes_size),
314 "06 %04X MODEL_ID" % 0,
315 "07 %04X SHA256" % 1,
316 ]
317 hash_seg.cert_chain = cert.generate_chain(ou_fields)
318 hash_seg.cert_chain = hash_seg.cert_chain.ljust(elf.align(len(hash_seg.cert_chain), CERT_CHAIN_ALIGN), b'\xff')
319 # hash_seg.cert_chain = b'' # uncomment this to omit the certificate chain in the signed image
320
321 # TODO: Generate actual signature with our generated attestation certificate!
322 # There are different signature schemes that could be implemented (RSASSA-PKCS#1 v1.5
323 # RSASSA-PSS, ECDSA over P-384) but it's not entirely clear yet which chipsets supports/
324 # uses which. The signature does not seem to be checked on devices without secure boot,
325 # so just use a dummy value for now.
326 hash_seg.signature = b'\xff' * (cert.ATT_KEY.key_size // 8)
327 # hash_seg.signature = b'' # uncomment this to omit the signature in the signed image
328
329 # Align maximum end address to get address for hash table header, then update header
330 hash_addr = elf.align(max(phdr.p_paddr + phdr.p_memsz for phdr in elff.phdrs), HASH_SEG_ALIGN)
331 hash_seg.update(hash_addr)
332 print(hash_seg)
333
334 # Insert new hash NULL segment
335 hash_phdr = elf.Phdr(elf.Phdr.PT_NULL, HASH_SEG_ALIGN, hash_addr, hash_addr, hash_seg.size_with_header,
336 elf.align(hash_seg.size_with_header, HASH_SEG_ALIGN),
337 PHDR_FLAGS_HASH_SEGMENT, HASH_SEG_ALIGN)
338 elff.phdrs.insert(0, hash_phdr)
339
340 # Insert new ELF header placeholder program header
341 hdr_hash_phdr = elf.Phdr(elf.Phdr.PT_NULL, 0, 0, 0, 0, 0, PHDR_FLAGS_HDR_PLACEHOLDER, 0)
342 elff.phdrs.insert(0, hdr_hash_phdr)
343
344 # Now determine size of ELF header (including program headers)
345 hdr_hash_phdr.p_filesz = elff.total_header_size()
346
347 # Recompute attributes to match final output (e.g. adjust e_phnum)
348 elff.update()
349
350 # Compute the hash for the ELF header
351 with BytesIO() as hdr_io:
352 elff.save_header(hdr_io)
353 hash_seg.hashes[0] = hash_seg.Hash(hdr_io.getbuffer()).digest()
354
355 # And finally, assemble the hash segment
356 hash_phdr.data = hash_seg.pack()
357 assert len(hash_phdr.data) == hash_phdr.p_filesz