| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
HASH_SEG_ALIGN = 0x1000 |
| 66 |
CERT_CHAIN_ALIGN = 16 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
MBN_V6_METADATA_SIZE = 120 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
MBN_V7_OEM_2_0_METADATA_SIZE = 224 |
| 75 |
|
| 76 |
|
| 77 |
@dataclass |
| 78 |
class _HashSegment: |
| 79 |
image_id: int = 0 |
| 80 |
version: int = 0 |
| 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 |
| 122 |
|
| 123 |
flash_addr: int = 0 |
| 124 |
dest_addr: int = 0 |
| 125 |
total_size: int = 0 |
| 126 |
hash_size: int = 0 |
| 127 |
signature_addr: int = 0 |
| 128 |
signature_size: int = 0 |
| 129 |
cert_chain_addr: int = 0 |
| 130 |
cert_chain_size: int = 0 |
| 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 |
| 142 |
|
| 143 |
signature_size_qcom: int = 0 |
| 144 |
cert_chain_size_qcom: int = 0 |
| 145 |
total_size: int = 0 |
| 146 |
hash_size: int = 0 |
| 147 |
signature_addr: int = 0xffffffff |
| 148 |
signature_size: int = 0 |
| 149 |
cert_chain_addr: int = 0xffffffff |
| 150 |
cert_chain_size: int = 0 |
| 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 |
| 176 |
|
| 177 |
metadata_size_qcom: int = 0 |
| 178 |
metadata_size: int = 0 |
| 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 |
|
| 207 |
|
| 208 |
class HashSegmentV7(_HashSegment): |
| 209 |
version: int = 7 |
| 210 |
|
| 211 |
common_metadata_size: int = 24 |
| 212 |
metadata_size_qcom: int = 0 |
| 213 |
metadata_size: int = 0 |
| 214 |
hash_size: int = 0 |
| 215 |
signature_size_qcom: int = 0 |
| 216 |
cert_chain_size_qcom: int = 0 |
| 217 |
signature_size: int = 0 |
| 218 |
cert_chain_size: int = 0 |
| 219 |
|
| 220 |
|
| 221 |
common_metadata_major_version: int = 0 |
| 222 |
common_metadata_minor_version: int = 0 |
| 223 |
software_id: int = 0 |
| 224 |
secondary_software_id: int = 0 |
| 225 |
hash_table_algorithm: int = 3 |
| 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 |
|
| 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 |
|
| 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 |
|
| 283 |
hash_seg.metadata = b'\0' * MBN_V6_METADATA_SIZE |
| 284 |
|
| 285 |
|
| 286 |
if version == 7: |
| 287 |
hash_seg.software_id = sw_id |
| 288 |
|
| 289 |
|
| 290 |
hash_seg.metadata = b'\0' * MBN_V7_OEM_2_0_METADATA_SIZE |
| 291 |
|
| 292 |
|
| 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 |
|
| 301 |
|
| 302 |
ou_fields = [] |
| 303 |
if version < 6: |
| 304 |
ou_fields = [ |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
"01 %016X SW_ID" % sw_id, |
| 310 |
"02 %016X HW_ID" % 0, |
| 311 |
"03 %016X DEBUG" % 2, |
| 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 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
hash_seg.signature = b'\xff' * (cert.ATT_KEY.key_size // 8) |
| 327 |
|
| 328 |
|
| 329 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 345 |
hdr_hash_phdr.p_filesz = elff.total_header_size() |
| 346 |
|
| 347 |
|
| 348 |
elff.update() |
| 349 |
|
| 350 |
|
| 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 |
|
| 356 |
hash_phdr.data = hash_seg.pack() |
| 357 |
assert len(hash_phdr.data) == hash_phdr.p_filesz |