Verified Commit a868c697 authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Implement check functions as decorators

parent d8859e3b
......@@ -3,16 +3,13 @@ Main entry point of MOX OTP package
"""
import sys
import os
from .argparser import parse_args, hash_type
from .checks import check_serial, check_pubkey, first_line_of_file
from .exceptions import MoxOtpApiError, MoxOtpSetupError, MoxOtpUsageError
from .checks import PUBKEY_PATH, SERIAL_PATH, SIGN_PATH
SYSFS_ROOT = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-regs@d0000000:crypto@0/"
PUBKEY_PATH = SYSFS_ROOT + "mox_pubkey"
SIGN_PATH = SYSFS_ROOT + "mox_do_sign"
SERIAL_PATH = SYSFS_ROOT + "mox_serial_number"
# number of bytes to read at once
CHUNK_SIZE = 1024
......@@ -24,34 +21,6 @@ def errprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def check_sysfs():
if not os.path.isdir(SYSFS_ROOT):
raise MoxOtpSetupError("sysfs root directory does not exists (probably not running on MOX device)")
def first_line_of_file(filename):
with open(filename, "r") as f:
line = f.readline()
return line.rstrip("\n")
def check_serial():
try:
first_line_of_file(SERIAL_PATH)
except (FileNotFoundError, PermissionError):
raise MoxOtpApiError("Could not find MOX serial file")
def check_pubkey():
try:
pubkey = first_line_of_file(PUBKEY_PATH)
except (FileNotFoundError, PermissionError):
raise MoxOtpApiError("Could not find MOX pubkey file")
if pubkey in ["", "\n", "none\n"]:
raise MoxOtpSetupError("This device does not have its OTP key generated or accessible")
def count_hash_from_file(f):
"""f is opened file for reading in binary mode
"""
......@@ -77,39 +46,33 @@ def sign_hash(h):
return sig[2:68] + sig[70:]
@check_serial
def do_serial():
"""print serial number from OTP
"""
check_sysfs()
check_serial()
print(first_line_of_file(SERIAL_PATH))
@check_pubkey
def do_pubkey():
"""print public key from OTP
"""
check_sysfs()
check_pubkey()
print(first_line_of_file(PUBKEY_PATH))
@check_pubkey
def do_sign(inputfile):
"""print signature of given (opened) binary input stream
"""
check_sysfs()
check_pubkey()
dig = count_hash_from_file(inputfile)
sig = sign_hash(dig)
print(sig.hex())
@check_pubkey
def do_sign_hash(hexstr):
"""print signature of given hash
"""
check_sysfs()
check_pubkey()
dig = bytes.fromhex(hexstr)
sig = sign_hash(dig)
print(sig.hex())
......
"""
Internal checks for MOX OTP package
"""
import os
from functools import wraps
from .exceptions import MoxOtpApiError, MoxOtpSetupError
SYSFS_ROOT = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-regs@d0000000:crypto@0/"
PUBKEY_PATH = SYSFS_ROOT + "mox_pubkey"
SIGN_PATH = SYSFS_ROOT + "mox_do_sign"
SERIAL_PATH = SYSFS_ROOT + "mox_serial_number"
def first_line_of_file(filename):
with open(filename, "r") as f:
line = f.readline()
return line.rstrip("\n")
def check_sysfs():
if not os.path.isdir(SYSFS_ROOT):
raise MoxOtpSetupError("sysfs root directory does not exists (probably not running on MOX device)")
def check_serial(f):
@wraps(f)
def _checked(*args, **kwargs):
check_sysfs()
try:
first_line_of_file(SERIAL_PATH)
except (FileNotFoundError, PermissionError):
raise MoxOtpApiError("Could not find MOX serial file")
return f(*args, **kwargs)
return _checked
def check_pubkey(f):
@wraps(f)
def _checked(*args, **kwargs):
check_sysfs()
try:
pubkey = first_line_of_file(PUBKEY_PATH)
except (FileNotFoundError, PermissionError):
raise MoxOtpApiError("Could not find MOX pubkey file")
if pubkey in ["", "\n", "none\n"]:
raise MoxOtpSetupError("This device does not have its OTP key generated or accessible")
return f(*args, **kwargs)
return _checked
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment