Verified Commit 101679be authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Refactor the script more python-compliant way

parent c18bf2b8
#!/usr/bin/python
#!/usr/bin/env python3
from sys import argv
import sys
from hashlib import sha512
from binascii import hexlify
from struct import pack, unpack
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"
def change_endian(s):
res = b''
for i in range(0, len(s), 4):
res += pack(">I", unpack("<I", s[i:i+4])[0])
return res
res = b''
for i in range(0, len(s), 4):
res += pack(">I", unpack("<I", s[i:i+4])[0])
return res
def main():
pubkey = open(PUBKEY_PATH).read()
path = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-regs@d0000000:crypto@0/"
pubkey_path = path + "mox_pubkey"
sign_path = path + "mox_do_sign"
if pubkey == "none\n":
print("no public key burned")
exit(1)
pubkey = open(pubkey_path).read()
print("MOX burned public key: {}".format(pubkey))
if pubkey == "none\n":
print("no public key burned")
exit(1)
if len(sys.argv) < 2:
print("message not given")
exit(1)
print("MOX burned public key: %s" % (pubkey,))
message = sys.argv[1]
print("message: {}".format(message))
if len(argv) < 2:
print("message not given")
exit(1)
h = sha512()
h.update(bytes(message, encoding='utf-8'))
dig = h.digest()
print("message: %s" % (argv[1],))
print("message hash: {}".format(h.hexdigest()))
h = sha512()
h.update(argv[1].encode("utf-8"))
dig = h.digest()
s = open(SIGN_PATH, "wb")
s.write(change_endian(dig))
s.close()
print("message hash: %s" % (h.hexdigest(),))
s = open(SIGN_PATH, "rb")
sig = s.read()
s.close()
s = open(sign_path, "wb")
s.write(change_endian(dig))
s.close()
sig = change_endian(sig)
s = open(sign_path, "rb")
sig = s.read()
s.close()
print("signature: {}".format((sig[2:68] + sig[70:]).hex()))
sig = change_endian(sig)
print("signature: %s" % (hexlify(sig[2:68] + sig[70:]).decode("ascii"),))
if __name__ == "__main__":
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