File: common.py

package info (click to toggle)
imx-code-signing-tool 3.4.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,912 kB
  • sloc: ansic: 10,258; sh: 2,558; python: 391; yacc: 245; makefile: 203; lex: 59
file content (107 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2019, 2021-2023 NXP
#

# from cryptography.hazmat.backends              import default_backend
# from cryptography.hazmat.primitives.asymmetric import ec, utils, rsa, padding
# from cryptography.hazmat.primitives            import hashes

# Definitions

LINE_BREAK = "-" * 80
TAB        = " " * 4

STR_SECP256R1 = "secp256r1"
STR_SECP384R1 = "secp384r1"
STR_SECP521R1 = "secp521r1"

# curves = {
#     STR_SECP256R1 : ec.SECP256R1(),
#     STR_SECP384R1 : ec.SECP384R1(),
#     STR_SECP521R1 : ec.SECP521R1()
# }

STR_SHA256 = "sha256"
STR_SHA384 = "sha384"
STR_SHA512 = "sha512"

# hash_algos = {
#     STR_SHA256 : hashes.SHA256(),
#     STR_SHA384 : hashes.SHA384(),
#     STR_SHA512 : hashes.SHA512(),
# }

# Globals

tab_lvl = 0

# Error handling

def error(msg, inbytes=None, persist=False):
    import sys
    print("\n{:}: {:}: {:}".format(sys.argv[0], "ERROR", msg))
    if inbytes != None:
        print("{:#x}".format(inbytes))
    exit(-1)

# Get data from file

def get_bytes(infile, offset, size):
    try:
        infile.seek(offset)
        data = infile.read(size)
        if len(data) != size: raise Error
        return data
    except:
        error("cannot read {:} ({:#x}) bytes from offset {:#x}".format(size,
                                                                size, offset))

# Convert bytes to int

def bytes2int(bytes, order="little"):
    return int.from_bytes(bytes, byteorder=order)

# Tabs and breaks handling

def line_break():
    global tab_lvl
    tab_lvl = 0
    print(LINE_BREAK)

def tab():
    global tab_lvl
    tab_lvl += 1

def untab():
    global tab_lvl
    if tab_lvl > 0: tab_lvl -= 1

# Logs handling

def log(msg, new_line=False):
    global tab_lvl
    if new_line: print()
    print("{:}{:}".format(TAB * tab_lvl, msg))

def log_v(msg, val):
    global tab_lvl
    print("{:}{:.<16} {:}".format(TAB * tab_lvl, msg, val))

def log_x(msg, val, pad = 0):
    global tab_lvl
    print("{:}{:.<16} 0x{:0{pad}x}".format(TAB * tab_lvl, msg, val, pad = pad))

def log__x(msg, val, pad = 0):
    global tab_lvl
    print("{:}{:.<16} {:0{pad}x}".format(TAB * tab_lvl, msg, val, pad = pad))

def log_len(msg, val):
    global tab_lvl
    print("{:}{:.<16} {:} ({:#x})".format(TAB * tab_lvl, msg, val, val))

# Main

if __name__ == "__main__":
    print("This script cannot be called directly")