1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (C) 2021 Huawei Technologies Co., Ltd
5
6
7def dump(msg, buf):
8    import codecs
9
10    print(msg, end='')
11    print(codecs.encode(buf, 'hex').decode('utf-8'))
12
13
14def main():
15    import struct
16    import sys
17
18    img_type_name = {1: 'SHDR_BOOTSTRAP_TA', 2: 'SHDR_ENCRYPTED_TA'}
19    algo_name = {0x70414930: 'RSASSA_PKCS1_PSS_MGF1_SHA256',
20                 0x70004830: 'RSASSA_PKCS1_V1_5_SHA256'}
21
22    with open(sys.argv[1], 'rb') as f:
23        shdr = f.read(20)
24        (magic, img_type, img_size, algo, digest_len,
25            sig_len) = struct.unpack('<IIIIHH', shdr)
26        print(f'Magic: 0x{magic:x} ', end='')
27        if magic == 0x4f545348:  # SHDR_MAGIC
28            print('(correct)')
29        else:
30            print('(**INCORRECT**)')
31            return
32        print(f'Image type: {img_type} ({img_type_name[img_type]})')
33        print(f'Image size: {img_size} bytes')
34        print(f'Signing algorithm: 0x{algo:x} ({algo_name[algo]})')
35        print(f'Digest length: {digest_len} bytes')
36        print(f'Signature length: {sig_len} bytes')
37        digest = f.read(digest_len)
38        dump('Digest: ', digest)
39
40
41if __name__ == '__main__':
42    main()
43