Verified Commit 529ce200 authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Move helper functions to separate module

parent a868c697
......@@ -4,9 +4,11 @@ Main entry point of MOX OTP package
import sys
from .argparser import parse_args, hash_type
from .checks import check_serial, check_pubkey, first_line_of_file
from .argparser import parse_args
from .checks import check_serial, check_pubkey
from .exceptions import MoxOtpApiError, MoxOtpSetupError, MoxOtpUsageError
from .helpers import errprint, first_line_of_file, hash_type
from .checks import PUBKEY_PATH, SERIAL_PATH, SIGN_PATH
......@@ -17,10 +19,6 @@ CHUNK_SIZE = 1024
MAX_SIGNATURE_LENGTH = 512
def errprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def count_hash_from_file(f):
"""f is opened file for reading in binary mode
"""
......
......@@ -3,33 +3,14 @@ An argument parser for MOX OTP
"""
import argparse
import hashlib
import sys
from .exceptions import MoxOtpSetupError
from .helpers import hash_type_length
from .helpers import HASH_TYPE
# hash algorithm used for message signature
HASH_TYPE = "sha512"
VERSION="0.1-alpha"
def hash_type():
"""Returns constructed hash of HASH_TYPE
"""
try:
h = hashlib.new(HASH_TYPE)
except ValueError:
raise MoxOtpSetupError("Hash type {} is not available".format(HASH_TYPE))
return h
def hash_type_length():
"""Returns number of bytes for HASH_TYPE
"""
h = hash_type()
return h.digest_size
VERSION = '0.1-alpha'
def type_hexstr(hexstr):
......
......@@ -6,6 +6,7 @@ import os
from functools import wraps
from .exceptions import MoxOtpApiError, MoxOtpSetupError
from .helpers import first_line_of_file
SYSFS_ROOT = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-regs@d0000000:crypto@0/"
......@@ -14,12 +15,6 @@ 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)")
......
"""
Helper functions for MOX OTP package
"""
import hashlib
import sys
from .exceptions import MoxOtpSetupError
# hash algorithm used for message signature
HASH_TYPE = "sha512"
def errprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def first_line_of_file(filename):
with open(filename, "r") as f:
line = f.readline()
return line.rstrip("\n")
def hash_type():
"""Returns constructed hash of HASH_TYPE
"""
try:
h = hashlib.new(HASH_TYPE)
except ValueError:
raise MoxOtpSetupError("Hash type {} is not available".format(HASH_TYPE))
return h
def hash_type_length():
"""Returns number of bytes for HASH_TYPE
"""
h = hash_type()
return h.digest_size
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