Verified Commit 923ea665 authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Split script into functions

parent 101679be
......@@ -9,6 +9,13 @@ SYSFS_ROOT = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-
PUBKEY_PATH = SYSFS_ROOT + "mox_pubkey"
SIGN_PATH = SYSFS_ROOT + "mox_do_sign"
# max number of bytes to read from sysfs sig file
MAX_SIGNATURE_LENGTH = 512
def errprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def change_endian(s):
res = b''
......@@ -17,39 +24,45 @@ def change_endian(s):
return res
def main():
pubkey = open(PUBKEY_PATH).read()
def check_pubkey():
try:
with open(PUBKEY_PATH, "r") as f:
pubkey = f.readline()
except (FileNotFoundError, PermissionError):
errprint("Could not find MOX pubkey file (probably not running on MOX device)")
exit(2)
if pubkey == "none\n":
print("no public key burned")
exit(1)
if pubkey in ["", "\n", "none\n"]:
errprint("This device does not have its OTP key generated or accessible")
exit(2)
print("MOX burned public key: {}".format(pubkey))
if len(sys.argv) < 2:
print("message not given")
exit(1)
message = sys.argv[1]
print("message: {}".format(message))
def sign_message(message):
h = sha512()
h.update(bytes(message, encoding='utf-8'))
h.update(bytes(message, encoding="utf-8"))
dig = h.digest()
print("message hash: {}".format(h.hexdigest()))
try:
with open(SIGN_PATH, "wb") as s:
s.write(change_endian(dig))
with open(SIGN_PATH, "rb") as s:
sig = change_endian(s.read(MAX_SIGNATURE_LENGTH))
except (FileNotFoundError, PermissionError):
errprint("Could not find MOX sign file – the sysfs API is probably broken")
exit(3)
s = open(SIGN_PATH, "wb")
s.write(change_endian(dig))
s.close()
print((sig[2:68] + sig[70:]).hex())
s = open(SIGN_PATH, "rb")
sig = s.read()
s.close()
sig = change_endian(sig)
def main():
check_pubkey()
if len(sys.argv) < 2:
print("message not given")
exit(1)
print("signature: {}".format((sig[2:68] + sig[70:]).hex()))
message = sys.argv[1]
sign_message(message)
if __name__ == "__main__":
......
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