hbc/pywii/pywii-tools/ecchecksig.py

58 lines
1.1 KiB
Python
Raw Normal View History

2021-03-17 15:53:31 +01:00
#!/usr/bin/env python3
2016-11-23 06:35:12 +01:00
import sys
import pywii as wii
if len(sys.argv) != 3:
2021-03-17 15:53:31 +01:00
print("Usage: %s keyfile.[priv|pub] infile"%sys.argv[0])
2016-11-23 06:35:12 +01:00
sys.exit(1)
if sys.argv[1] == "-":
k = sys.stdin.read()
else:
k = open(sys.argv[1],"rb").read()
if len(k) not in (30,60):
2021-03-17 15:53:31 +01:00
print("Failed to read key")
2016-11-23 06:35:12 +01:00
sys.exit(2)
if len(k) == 30:
2021-03-17 15:53:31 +01:00
print("Key is a private key, generating public key...")
2016-11-23 06:35:12 +01:00
q = wii.ec.priv_to_pub(k)
else:
q = k
2021-03-17 15:53:31 +01:00
print("Public key:")
2016-11-23 06:35:12 +01:00
pq = q.encode('hex')
2021-03-17 15:53:31 +01:00
print("X =",pq[:30])
print(" ",pq[30:60])
print("Y =",pq[60:90])
print(" ",pq[90:])
print()
2016-11-23 06:35:12 +01:00
indata = open(sys.argv[2],"rb").read()
if len(indata) < 64 or indata[:4] != "SIG0":
2021-03-17 15:53:31 +01:00
print("Invalid header")
2016-11-23 06:35:12 +01:00
sys.exit(3)
r = indata[4:34]
s = indata[34:64]
sha = wii.SHA.new(indata[64:]).digest()
2021-03-17 15:53:31 +01:00
print("SHA1: %s"%sha.encode('hex'))
2016-11-23 06:35:12 +01:00
2021-03-17 15:53:31 +01:00
print()
print("Signature:")
print("R =",r[:15].encode('hex'))
print(" ",r[15:].encode('hex'))
print("S =",s[:15].encode('hex'))
print(" ",s[15:].encode('hex'))
print()
2016-11-23 06:35:12 +01:00
if wii.ec.check_ecdsa(q,r,s,sha):
2021-03-17 15:53:31 +01:00
print("Signature is VALID")
2016-11-23 06:35:12 +01:00
else:
2021-03-17 15:53:31 +01:00
print("Signature is INVALID")
2016-11-23 06:35:12 +01:00
sys.exit(4)