-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
101 lines (83 loc) · 2.91 KB
/
utils.py
File metadata and controls
101 lines (83 loc) · 2.91 KB
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
from pathlib import Path
from typing import List
import re
from hashlib import sha256
from ecdsa.curves import NIST256p # type: ignore
from ecdsa.keys import VerifyingKey # type: ignore
from ecdsa.util import sigdecode_der # type: ignore
# Check if a signature of a given message is valid
def check_signature_validity(public_key: bytes, signature: bytes, message: bytes) -> bool:
pk: VerifyingKey = VerifyingKey.from_string(
public_key,
curve=NIST256p,
hashfunc=sha256
)
return pk.verify(signature=signature,
data=message,
hashfunc=sha256,
sigdecode=sigdecode_der)
def verify_name(name: str) -> None:
"""Verify the app name, based on defines in Makefile
Args:
name (str): Name to be checked
"""
name_str = ""
lines = _read_makefile()
name_re = re.compile(r"^APPNAME\s?=\s?\"?(?P<val>\w+)\"?", re.I)
for line in lines:
info = name_re.match(line)
if info:
dinfo = info.groupdict()
name_str = dinfo["val"]
assert name == name_str
def verify_version(version: str) -> None:
"""Verify the app version, based on defines in Makefile
Args:
Version (str): Version to be checked
"""
vers_dict = {}
vers_str = ""
lines = _read_makefile()
version_re = re.compile(r"^APPVERSION_(?P<part>\w)\s?=\s?(?P<val>\d*)", re.I)
for line in lines:
info = version_re.match(line)
if info:
dinfo = info.groupdict()
vers_dict[dinfo["part"]] = dinfo["val"]
try:
vers_str = f"{vers_dict['M']}.{vers_dict['N']}.{vers_dict['P']}"
except KeyError:
pass
assert version == vers_str
def _read_makefile() -> List[str]:
"""Read lines from the parent Makefile """
parent = Path(__file__).parent.parent.resolve()
makefile = f"{parent}/Makefile"
with open(makefile, "r", encoding="utf-8") as f_p:
lines = f_p.readlines()
return lines
def checkpersonal_signature_validity(public_key: bytes, signature: bytes, message: bytes) -> bool:
pk: VerifyingKey = VerifyingKey.from_string(
public_key,
curve=NIST256p,
hashfunc=sha256
)
return pk.verify(signature=signature,
data=message,
hashfunc=sha256,
sigdecode=sigdecode_der)
def hex_to_bytes(hex_str: str) -> bytes:
"""
Converts a hexadecimal string to a bytes object.
Args:
hex_str (str): The hexadecimal string to convert.
Returns:
bytes: The corresponding bytes object.
"""
hex_str = hex_str.replace(" ", "").strip()
if len(hex_str) % 2 != 0:
raise ValueError("Hex string must have an even number of characters")
return bytes.fromhex(hex_str)
def int_byte(value: int) -> bytes:
byte_length = (value.bit_length() + 7) // 8
return value.to_bytes(byte_length, byteorder='little')