WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
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
108
109
110
#!/usr/bin/env python3
#
# Copyright (c) 2022-2024 Shanghai Panchip Microelectronics Co.,Ltd.
#
# SPDX-License-Identifier: Apache-2.0
#
import re
import argparse
 
key_factor = bytes([0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa])
 
def decrypt_key_func(key):
    step3_res = []
    step4_res = ""
 
    local_key = key[16:] + key[0:16]
    key_bytes = bytes.fromhex(local_key)
 
    for i in range(0, 16):
        step3_res.append(hex(key_factor[i] ^ key_bytes[i]))
 
    for i in range(0, 16):
        step4_res = step4_res + (step3_res[i][2:]).zfill(2)
 
    return step4_res
 
def encrypt_key_func(key):
    step1_res = []
    step2_res = ""
    key_bytes = bytes.fromhex(key)
 
    for i in range(0, 16):
        step1_res.append((hex(key_factor[i] ^ key_bytes[i])).zfill(2))
 
    for i in range(8, 16):
        step2_res = step2_res + (step1_res[i][2:]).zfill(2)
 
    for i in range(0, 8):
        step2_res = step2_res + (step1_res[i][2:]).zfill(2)
 
    return step2_res
 
 
def crc_cal(file_data):
    crc = sum(file_data)
    return hex(crc)
 
 
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter)
 
    parser.add_argument(
        "-i",
        "--input_yaml_path",
        required=True,
        help="Directory of encrypt_info.yaml")
    parser.add_argument(
        "-o",
        "--output_bin_path",
        required=True,
        help="Directory of output encoded encrypt_info_enc.bin")
 
    args = parser.parse_args()
    # print(args.input_yaml_path)
    # print(args.output_bin_path)
 
    # open file
    with open(args.input_yaml_path+"/encrypt_info.yaml", "r") as original_file:
        with open(args.output_bin_path+"/encrypt_info_enc.bin", "wb+") as encrypt_file:
            # encode data and write to new file (binary format)
            for line in original_file:
                res = re.search("encrypt_key", line)
                if res:
                    symbol_index = line.index("'")
                    # Check if not empty string
                    if line[symbol_index+1] != "'":
                        key = line[symbol_index+1:symbol_index+33]
                        line_list = line.split("'", 2)
                        enc_key_encode = encrypt_key_func(key)
                        line = line_list[0] + "'" + enc_key_encode + "'" + line_list[2]
 
                res = re.search("debug_key", line)
                if res:
                    symbol_index = line.index("'")
                    # Check if not empty string
                    if line[symbol_index+1] != "'":
                        key = line[symbol_index+1:symbol_index+17]
                        line_list = line.split("'", 2)
                        dbg_key_encode = encrypt_key_func(key + key)
                        line = line_list[0] + "'" + dbg_key_encode + "'" + line_list[2]
 
                write_data =line.encode('utf-8')
                encrypt_file.write(write_data)
 
            # write crc to enc file
            encrypt_file.flush()
            encrypt_file.seek(0, 0)
            file_data = encrypt_file.read()
            crc = crc_cal(file_data)
            write_data = "check_info:\r\n".encode('utf-8')
            encrypt_file.write(write_data)
            crc_str = "  crc: '" + crc + "'\r\n"
            write_data = crc_str.encode('utf-8')
            encrypt_file.write(write_data)
 
    print("Encoded encrypt info file (encrypt_info_enc.bin) generated done")
 
if __name__ == '__main__':
    main()