mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 07:28:10 +00:00
tools: autopep8
This commit is contained in:
parent
f93b8a08bc
commit
596a4227ad
@ -6,6 +6,7 @@ COMMENT_PREFIX = '/// '
|
|||||||
current_method = None
|
current_method = None
|
||||||
current_package = None
|
current_package = None
|
||||||
|
|
||||||
|
|
||||||
def split_to_parts(line, mod_desc=None):
|
def split_to_parts(line, mod_desc=None):
|
||||||
global current_method
|
global current_method
|
||||||
global current_package
|
global current_package
|
||||||
@ -23,6 +24,7 @@ def split_to_parts(line, mod_desc=None):
|
|||||||
|
|
||||||
yield (current_package, line)
|
yield (current_package, line)
|
||||||
|
|
||||||
|
|
||||||
def store_to_file(dest, parts):
|
def store_to_file(dest, parts):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -42,6 +44,7 @@ def store_to_file(dest, parts):
|
|||||||
f.write(line)
|
f.write(line)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
def build_module(mod_file, dest):
|
def build_module(mod_file, dest):
|
||||||
if not (mod_file.endswith('.h') or mod_file.endswith('.c')):
|
if not (mod_file.endswith('.h') or mod_file.endswith('.c')):
|
||||||
return
|
return
|
||||||
@ -51,15 +54,17 @@ def build_module(mod_file, dest):
|
|||||||
if not l.startswith(COMMENT_PREFIX):
|
if not l.startswith(COMMENT_PREFIX):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
l = l[len(COMMENT_PREFIX):]#.strip()
|
l = l[len(COMMENT_PREFIX):] # .strip()
|
||||||
store_to_file(dest, split_to_parts(l, mod_desc))
|
store_to_file(dest, split_to_parts(l, mod_desc))
|
||||||
|
|
||||||
|
|
||||||
def build_directory(dir, dest):
|
def build_directory(dir, dest):
|
||||||
print("Building mocks for", dir, "to", dest)
|
print("Building mocks for", dir, "to", dest)
|
||||||
for pkg in os.listdir(dir):
|
for pkg in os.listdir(dir):
|
||||||
for mod in os.listdir(os.path.join(dir, pkg)):
|
for mod in os.listdir(os.path.join(dir, pkg)):
|
||||||
build_module(os.path.join(dir, pkg, mod), dest)
|
build_module(os.path.join(dir, pkg, mod), dest)
|
||||||
|
|
||||||
|
|
||||||
def clear_directory(top_dir):
|
def clear_directory(top_dir):
|
||||||
print("Clearing up directory", top_dir)
|
print("Clearing up directory", top_dir)
|
||||||
for root, dirs, files in os.walk(top_dir, topdown=False):
|
for root, dirs, files in os.walk(top_dir, topdown=False):
|
||||||
|
@ -4,17 +4,21 @@ import struct
|
|||||||
import binascii
|
import binascii
|
||||||
|
|
||||||
# encode vendor name, add length byte and padding to multiple of 4
|
# encode vendor name, add length byte and padding to multiple of 4
|
||||||
|
|
||||||
|
|
||||||
def encode_vendor(vname):
|
def encode_vendor(vname):
|
||||||
vbin = vname.encode('utf-8')
|
vbin = vname.encode('utf-8')
|
||||||
vbin = struct.pack('<B', len(vbin)) + vbin
|
vbin = struct.pack('<B', len(vbin)) + vbin
|
||||||
vbin += b'\0' * (-len(vbin) & 3)
|
vbin += b'\0' * (-len(vbin) & 3)
|
||||||
return vbin
|
return vbin
|
||||||
|
|
||||||
|
|
||||||
def encode_pubkey(pubkey):
|
def encode_pubkey(pubkey):
|
||||||
if len(pubkey) != 64:
|
if len(pubkey) != 64:
|
||||||
raise Exception('Wrong public key length')
|
raise Exception('Wrong public key length')
|
||||||
return binascii.unhexlify(pubkey)
|
return binascii.unhexlify(pubkey)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 7:
|
if len(sys.argv) < 7:
|
||||||
print('Usage build_vendorheader "pubkey1hex:pubkey2hex:..." m version vendorname vendorimage.toif vendorheader.bin')
|
print('Usage build_vendorheader "pubkey1hex:pubkey2hex:..." m version vendorname vendorimage.toif vendorheader.bin')
|
||||||
@ -31,7 +35,8 @@ def main():
|
|||||||
return 2
|
return 2
|
||||||
|
|
||||||
expiry = 0
|
expiry = 0
|
||||||
vheader = b'TRZV' + struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
|
vheader = b'TRZV' + \
|
||||||
|
struct.pack('<IIBBBB', 0, expiry, vmajor, vminor, m, len(keys))
|
||||||
for k in keys:
|
for k in keys:
|
||||||
vheader += k
|
vheader += k
|
||||||
vheader += encode_vendor(vname) + open(ifn, 'rb').read()
|
vheader += encode_vendor(vname) + open(ifn, 'rb').read()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def pairwise(iterable):
|
def pairwise(iterable):
|
||||||
a = iter(iterable)
|
a = iter(iterable)
|
||||||
return zip(a, a)
|
return zip(a, a)
|
||||||
|
@ -7,103 +7,129 @@ b = 256
|
|||||||
q = 2**255 - 19
|
q = 2**255 - 19
|
||||||
l = 2**252 + 27742317777372353535851937790883648493
|
l = 2**252 + 27742317777372353535851937790883648493
|
||||||
|
|
||||||
def H(m):
|
|
||||||
return hashlib.sha512(m).digest()
|
|
||||||
|
|
||||||
def expmod(b,e,m):
|
def H(m):
|
||||||
if e < 0:
|
return hashlib.sha512(m).digest()
|
||||||
raise Exception("negative exponent")
|
|
||||||
if e == 0: return 1
|
|
||||||
t = expmod(b,e>>1,m)**2 % m
|
def expmod(b, e, m):
|
||||||
if e & 1: t = (t*b) % m
|
if e < 0:
|
||||||
return t
|
raise Exception("negative exponent")
|
||||||
|
if e == 0:
|
||||||
|
return 1
|
||||||
|
t = expmod(b, e >> 1, m)**2 % m
|
||||||
|
if e & 1:
|
||||||
|
t = (t * b) % m
|
||||||
|
return t
|
||||||
|
|
||||||
|
|
||||||
def inv(x):
|
def inv(x):
|
||||||
return expmod(x,q-2,q)
|
return expmod(x, q - 2, q)
|
||||||
|
|
||||||
d = -121665 * inv(121666)
|
d = -121665 * inv(121666)
|
||||||
I = expmod(2,(q-1)>>2,q)
|
I = expmod(2, (q - 1) >> 2, q)
|
||||||
|
|
||||||
|
|
||||||
def xrecover(y):
|
def xrecover(y):
|
||||||
xx = (y*y-1) * inv(d*y*y+1)
|
xx = (y * y - 1) * inv(d * y * y + 1)
|
||||||
x = expmod(xx,(q+3)>>3,q)
|
x = expmod(xx, (q + 3) >> 3, q)
|
||||||
if (x*x - xx) % q != 0: x = (x*I) % q
|
if (x * x - xx) % q != 0:
|
||||||
if x % 2 != 0: x = q-x
|
x = (x * I) % q
|
||||||
return x
|
if x % 2 != 0:
|
||||||
|
x = q - x
|
||||||
|
return x
|
||||||
|
|
||||||
By = 4 * inv(5)
|
By = 4 * inv(5)
|
||||||
Bx = xrecover(By)
|
Bx = xrecover(By)
|
||||||
B = [Bx % q,By % q]
|
B = [Bx % q, By % q]
|
||||||
|
|
||||||
def edwards(P,Q):
|
|
||||||
x1 = P[0]
|
|
||||||
y1 = P[1]
|
|
||||||
x2 = Q[0]
|
|
||||||
y2 = Q[1]
|
|
||||||
x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
|
|
||||||
y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
|
|
||||||
return [x3 % q,y3 % q]
|
|
||||||
|
|
||||||
def scalarmult(P,e):
|
def edwards(P, Q):
|
||||||
if e == 0: return [0,1]
|
x1 = P[0]
|
||||||
Q = scalarmult(P,e>>1)
|
y1 = P[1]
|
||||||
Q = edwards(Q,Q)
|
x2 = Q[0]
|
||||||
if e & 1: Q = edwards(Q,P)
|
y2 = Q[1]
|
||||||
return Q
|
x3 = (x1 * y2 + x2 * y1) * inv(1 + d * x1 * x2 * y1 * y2)
|
||||||
|
y3 = (y1 * y2 + x1 * x2) * inv(1 - d * x1 * x2 * y1 * y2)
|
||||||
|
return [x3 % q, y3 % q]
|
||||||
|
|
||||||
|
|
||||||
|
def scalarmult(P, e):
|
||||||
|
if e == 0:
|
||||||
|
return [0, 1]
|
||||||
|
Q = scalarmult(P, e >> 1)
|
||||||
|
Q = edwards(Q, Q)
|
||||||
|
if e & 1:
|
||||||
|
Q = edwards(Q, P)
|
||||||
|
return Q
|
||||||
|
|
||||||
|
|
||||||
def encodeint(y):
|
def encodeint(y):
|
||||||
bits = [(y >> i) & 1 for i in range(b)]
|
bits = [(y >> i) & 1 for i in range(b)]
|
||||||
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b>>3)])
|
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
|
||||||
|
|
||||||
|
|
||||||
def encodepoint(P):
|
def encodepoint(P):
|
||||||
x = P[0]
|
x = P[0]
|
||||||
y = P[1]
|
y = P[1]
|
||||||
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
|
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
|
||||||
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b>>3)])
|
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
|
||||||
|
|
||||||
|
|
||||||
|
def bit(h, i):
|
||||||
|
return (h[i >> 3] >> (i & 7)) & 1
|
||||||
|
|
||||||
def bit(h,i):
|
|
||||||
return (h[i>>3] >> (i&7)) & 1
|
|
||||||
|
|
||||||
def publickey(sk):
|
def publickey(sk):
|
||||||
h = H(sk)
|
h = H(sk)
|
||||||
a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
|
a = 2**(b - 2) + sum(2**i * bit(h, i) for i in range(3, b - 2))
|
||||||
A = scalarmult(B,a)
|
A = scalarmult(B, a)
|
||||||
return encodepoint(A)
|
return encodepoint(A)
|
||||||
|
|
||||||
|
|
||||||
def Hint(m):
|
def Hint(m):
|
||||||
h = H(m)
|
h = H(m)
|
||||||
return sum(2**i * bit(h,i) for i in range(2*b))
|
return sum(2**i * bit(h, i) for i in range(2 * b))
|
||||||
|
|
||||||
|
|
||||||
|
def signature(m, sk, pk):
|
||||||
|
h = H(sk)
|
||||||
|
a = 2**(b - 2) + sum(2**i * bit(h, i) for i in range(3, b - 2))
|
||||||
|
r = Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + m)
|
||||||
|
R = scalarmult(B, r)
|
||||||
|
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
|
||||||
|
return encodepoint(R) + encodeint(S)
|
||||||
|
|
||||||
def signature(m,sk,pk):
|
|
||||||
h = H(sk)
|
|
||||||
a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
|
|
||||||
r = Hint(bytes([h[i] for i in range(b>>3,b>>2)]) + m)
|
|
||||||
R = scalarmult(B,r)
|
|
||||||
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
|
|
||||||
return encodepoint(R) + encodeint(S)
|
|
||||||
|
|
||||||
def isoncurve(P):
|
def isoncurve(P):
|
||||||
x = P[0]
|
x = P[0]
|
||||||
y = P[1]
|
y = P[1]
|
||||||
return (-x*x + y*y - 1 - d*x*x*y*y) % q == 0
|
return (-x * x + y * y - 1 - d * x * x * y * y) % q == 0
|
||||||
|
|
||||||
|
|
||||||
def decodeint(s):
|
def decodeint(s):
|
||||||
return sum(2**i * bit(s,i) for i in range(0,b))
|
return sum(2**i * bit(s, i) for i in range(0, b))
|
||||||
|
|
||||||
|
|
||||||
def decodepoint(s):
|
def decodepoint(s):
|
||||||
y = sum(2**i * bit(s,i) for i in range(0,b-1))
|
y = sum(2**i * bit(s, i) for i in range(0, b - 1))
|
||||||
x = xrecover(y)
|
x = xrecover(y)
|
||||||
if x & 1 != bit(s,b-1): x = q-x
|
if x & 1 != bit(s, b - 1):
|
||||||
P = [x,y]
|
x = q - x
|
||||||
if not isoncurve(P): raise Exception("decoding point that is not on curve")
|
P = [x, y]
|
||||||
return P
|
if not isoncurve(P):
|
||||||
|
raise Exception("decoding point that is not on curve")
|
||||||
|
return P
|
||||||
|
|
||||||
def checkvalid(s,m,pk):
|
|
||||||
if len(s) != b>>2: raise Exception("signature length is wrong")
|
def checkvalid(s, m, pk):
|
||||||
if len(pk) != b>>3: raise Exception("public-key length is wrong")
|
if len(s) != b >> 2:
|
||||||
R = decodepoint(s[0:b>>3])
|
raise Exception("signature length is wrong")
|
||||||
A = decodepoint(pk)
|
if len(pk) != b >> 3:
|
||||||
S = decodeint(s[b>>3:b>>2])
|
raise Exception("public-key length is wrong")
|
||||||
h = Hint(encodepoint(R) + pk + m)
|
R = decodepoint(s[0:b >> 3])
|
||||||
if scalarmult(B,S) != edwards(R,scalarmult(A,h)):
|
A = decodepoint(pk)
|
||||||
raise Exception("signature does not pass verification")
|
S = decodeint(s[b >> 3:b >> 2])
|
||||||
|
h = Hint(encodepoint(R) + pk + m)
|
||||||
|
if scalarmult(B, S) != edwards(R, scalarmult(A, h)):
|
||||||
|
raise Exception("signature does not pass verification")
|
||||||
|
@ -4,9 +4,11 @@ import binascii
|
|||||||
import ed25519raw
|
import ed25519raw
|
||||||
import pyblake2
|
import pyblake2
|
||||||
|
|
||||||
|
|
||||||
def hex(by):
|
def hex(by):
|
||||||
return str(binascii.hexlify(by), 'ascii')
|
return str(binascii.hexlify(by), 'ascii')
|
||||||
|
|
||||||
|
|
||||||
def combine_keys(pks):
|
def combine_keys(pks):
|
||||||
combine = None
|
combine = None
|
||||||
for pk in pks:
|
for pk in pks:
|
||||||
@ -17,6 +19,7 @@ def combine_keys(pks):
|
|||||||
combine = ed25519raw.edwards(combine, P)
|
combine = ed25519raw.edwards(combine, P)
|
||||||
return ed25519raw.encodepoint(combine)
|
return ed25519raw.encodepoint(combine)
|
||||||
|
|
||||||
|
|
||||||
def combine_sig(R, sigs):
|
def combine_sig(R, sigs):
|
||||||
s = 0
|
s = 0
|
||||||
for si in sigs:
|
for si in sigs:
|
||||||
@ -25,12 +28,14 @@ def combine_sig(R, sigs):
|
|||||||
sig = R + ed25519raw.encodeint(s)
|
sig = R + ed25519raw.encodeint(s)
|
||||||
return sig
|
return sig
|
||||||
|
|
||||||
|
|
||||||
def binom(n, k):
|
def binom(n, k):
|
||||||
b = 1
|
b = 1
|
||||||
for i in range(1, k+1):
|
for i in range(1, k + 1):
|
||||||
b = b * (n - k + i) // i
|
b = b * (n - k + i) // i
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
|
||||||
def compute_mask(combination, m, n):
|
def compute_mask(combination, m, n):
|
||||||
result = 0
|
result = 0
|
||||||
signer = 0
|
signer = 0
|
||||||
@ -47,6 +52,7 @@ def compute_mask(combination, m, n):
|
|||||||
signer = signer + 1
|
signer = signer + 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def createPubkey():
|
def createPubkey():
|
||||||
print('Enter randomness: ', end='')
|
print('Enter randomness: ', end='')
|
||||||
seckey = ed25519raw.H(input().encode('utf-8'))[0:32]
|
seckey = ed25519raw.H(input().encode('utf-8'))[0:32]
|
||||||
@ -54,38 +60,41 @@ def createPubkey():
|
|||||||
print('Secret Key: %s' % hex(seckey))
|
print('Secret Key: %s' % hex(seckey))
|
||||||
print('Public Key: %s' % hex(pubkey))
|
print('Public Key: %s' % hex(pubkey))
|
||||||
|
|
||||||
def combinePubkeys(m,n):
|
|
||||||
if binom(n,m) > 100:
|
def combinePubkeys(m, n):
|
||||||
|
if binom(n, m) > 100:
|
||||||
raise Exception("Too many keys")
|
raise Exception("Too many keys")
|
||||||
pks = []
|
pks = []
|
||||||
for i in range(0, n):
|
for i in range(0, n):
|
||||||
print('Enter pubkey %d: ' %i, end='')
|
print('Enter pubkey %d: ' % i, end='')
|
||||||
pk = binascii.unhexlify(input())
|
pk = binascii.unhexlify(input())
|
||||||
# remove 00 prefix if present
|
# remove 00 prefix if present
|
||||||
if len(pk) == 33:
|
if len(pk) == 33:
|
||||||
pk = pk[1:]
|
pk = pk[1:]
|
||||||
pks.append(ed25519raw.decodepoint(pk))
|
pks.append(ed25519raw.decodepoint(pk))
|
||||||
for i in range(0, binom(n,m)):
|
for i in range(0, binom(n, m)):
|
||||||
mask = compute_mask(i, m, n)
|
mask = compute_mask(i, m, n)
|
||||||
sum = None
|
sum = None
|
||||||
for j in range(0, n):
|
for j in range(0, n):
|
||||||
if mask & (1<<j) != 0:
|
if mask & (1 << j) != 0:
|
||||||
if sum == None:
|
if sum == None:
|
||||||
sum = pks[j]
|
sum = pks[j]
|
||||||
else:
|
else:
|
||||||
sum = ed25519raw.edwards(sum, pks[j])
|
sum = ed25519raw.edwards(sum, pks[j])
|
||||||
pk = ed25519raw.encodepoint(sum)
|
pk = ed25519raw.encodepoint(sum)
|
||||||
print ('Key %02x: %s' % (mask, hex(pk)))
|
print('Key %02x: %s' % (mask, hex(pk)))
|
||||||
|
|
||||||
def get_nonce(sk,data,ctr):
|
|
||||||
|
def get_nonce(sk, data, ctr):
|
||||||
h = ed25519raw.H(sk)
|
h = ed25519raw.H(sk)
|
||||||
b = ed25519raw.b
|
b = ed25519raw.b
|
||||||
a = 2**(b-2) + sum(2**i * ed25519raw.bit(h,i) for i in range(3,b-2))
|
a = 2**(b - 2) + sum(2**i * ed25519raw.bit(h, i) for i in range(3, b - 2))
|
||||||
r = ed25519raw.Hint(bytes([h[i] for i in range(b>>3,b>>2)]) + data +
|
r = ed25519raw.Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + data +
|
||||||
binascii.unhexlify("%08x" % ctr))
|
binascii.unhexlify("%08x" % ctr))
|
||||||
R = ed25519raw.scalarmult(ed25519raw.B,r)
|
R = ed25519raw.scalarmult(ed25519raw.B, r)
|
||||||
return (r, ed25519raw.encodepoint(R))
|
return (r, ed25519raw.encodepoint(R))
|
||||||
|
|
||||||
|
|
||||||
def phase1(data):
|
def phase1(data):
|
||||||
digest = pyblake2.blake2s(data).digest()
|
digest = pyblake2.blake2s(data).digest()
|
||||||
print('Digest: %s' % hex(digest))
|
print('Digest: %s' % hex(digest))
|
||||||
@ -96,6 +105,7 @@ def phase1(data):
|
|||||||
(_, R) = get_nonce(seckey, digest, ctr)
|
(_, R) = get_nonce(seckey, digest, ctr)
|
||||||
print('Local commit: %s' % hex(R))
|
print('Local commit: %s' % hex(R))
|
||||||
|
|
||||||
|
|
||||||
def combinePhase1(m):
|
def combinePhase1(m):
|
||||||
commits = []
|
commits = []
|
||||||
for i in range(0, m):
|
for i in range(0, m):
|
||||||
@ -103,6 +113,7 @@ def combinePhase1(m):
|
|||||||
commits.append(binascii.unhexlify(input()))
|
commits.append(binascii.unhexlify(input()))
|
||||||
print('Global commit: %s' % hex(combine_keys(commits)))
|
print('Global commit: %s' % hex(combine_keys(commits)))
|
||||||
|
|
||||||
|
|
||||||
def phase2(data):
|
def phase2(data):
|
||||||
digest = pyblake2.blake2s(data).digest()
|
digest = pyblake2.blake2s(data).digest()
|
||||||
print('Digest: %s' % hex(digest))
|
print('Digest: %s' % hex(digest))
|
||||||
@ -118,11 +129,12 @@ def phase2(data):
|
|||||||
(r, Ri) = get_nonce(seckey, digest, ctr)
|
(r, Ri) = get_nonce(seckey, digest, ctr)
|
||||||
h = ed25519raw.H(seckey)
|
h = ed25519raw.H(seckey)
|
||||||
b = ed25519raw.b
|
b = ed25519raw.b
|
||||||
a = 2**(b-2) + sum(2**i * ed25519raw.bit(h,i) for i in range(3,b-2))
|
a = 2**(b - 2) + sum(2**i * ed25519raw.bit(h, i) for i in range(3, b - 2))
|
||||||
S = (r + ed25519raw.Hint(R + pk + digest) * a) % ed25519raw.l
|
S = (r + ed25519raw.Hint(R + pk + digest) * a) % ed25519raw.l
|
||||||
print('Local commit: %s' % hex(Ri))
|
print('Local commit: %s' % hex(Ri))
|
||||||
print('Local sig: %s' % hex(ed25519raw.encodeint(S)))
|
print('Local sig: %s' % hex(ed25519raw.encodeint(S)))
|
||||||
|
|
||||||
|
|
||||||
def combinePhase2(m):
|
def combinePhase2(m):
|
||||||
sigs = []
|
sigs = []
|
||||||
print('Enter global commit: ', end='')
|
print('Enter global commit: ', end='')
|
||||||
@ -133,6 +145,7 @@ def combinePhase2(m):
|
|||||||
sig = combine_sig(R, sigs)
|
sig = combine_sig(R, sigs)
|
||||||
print('Combined sig: %s' % hex(sig))
|
print('Combined sig: %s' % hex(sig))
|
||||||
|
|
||||||
|
|
||||||
def checkSignature(data):
|
def checkSignature(data):
|
||||||
digest = pyblake2.blake2s(data).digest()
|
digest = pyblake2.blake2s(data).digest()
|
||||||
print('Digest: %s' % hex(digest))
|
print('Digest: %s' % hex(digest))
|
||||||
@ -143,6 +156,7 @@ def checkSignature(data):
|
|||||||
ed25519raw.checkvalid(sig, digest, pubkey)
|
ed25519raw.checkvalid(sig, digest, pubkey)
|
||||||
print('Valid Signature!')
|
print('Valid Signature!')
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print('Usage: keyctl phase options')
|
print('Usage: keyctl phase options')
|
||||||
print('Phases:')
|
print('Phases:')
|
||||||
@ -154,6 +168,7 @@ def usage():
|
|||||||
print(' keyctl combine_ph2 m: combine signatures')
|
print(' keyctl combine_ph2 m: combine signatures')
|
||||||
print(' keyctl check_sig file.bin: check signature')
|
print(' keyctl check_sig file.bin: check signature')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
usage()
|
usage()
|
||||||
@ -164,7 +179,7 @@ def main():
|
|||||||
elif func == 'combine_pub':
|
elif func == 'combine_pub':
|
||||||
m = int(sys.argv[2])
|
m = int(sys.argv[2])
|
||||||
n = int(sys.argv[3])
|
n = int(sys.argv[3])
|
||||||
combinePubkeys(m,n)
|
combinePubkeys(m, n)
|
||||||
elif func == 'ph1':
|
elif func == 'ph1':
|
||||||
filename = sys.argv[2]
|
filename = sys.argv[2]
|
||||||
data = open(filename, 'rb').read()
|
data = open(filename, 'rb').read()
|
||||||
@ -186,10 +201,11 @@ def main():
|
|||||||
else:
|
else:
|
||||||
usage()
|
usage()
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
data = sys.argv[1].encode('utf-8')
|
data = sys.argv[1].encode('utf-8')
|
||||||
N = 5
|
N = 5
|
||||||
keyset = [1,3,4]
|
keyset = [1, 3, 4]
|
||||||
|
|
||||||
digest = pyblake2.blake2s(data).digest()
|
digest = pyblake2.blake2s(data).digest()
|
||||||
print('Digest: %s' % hex(digest))
|
print('Digest: %s' % hex(digest))
|
||||||
@ -199,8 +215,8 @@ def test():
|
|||||||
commits = []
|
commits = []
|
||||||
sigs = []
|
sigs = []
|
||||||
for i in range(0, N):
|
for i in range(0, N):
|
||||||
print('----- Key %d ------' % (i+1))
|
print('----- Key %d ------' % (i + 1))
|
||||||
seckey = ed25519raw.H(("key%d"%(i+1)).encode('utf-8'))[0:32]
|
seckey = ed25519raw.H(("key%d" % (i + 1)).encode('utf-8'))[0:32]
|
||||||
pubkey = ed25519raw.publickey(seckey)
|
pubkey = ed25519raw.publickey(seckey)
|
||||||
print('Secret Key: %s' % hex(seckey))
|
print('Secret Key: %s' % hex(seckey))
|
||||||
print('Public Key: %s' % hex(pubkey))
|
print('Public Key: %s' % hex(pubkey))
|
||||||
@ -216,26 +232,27 @@ def test():
|
|||||||
globalPk = combine_keys([pks[i] for i in keyset])
|
globalPk = combine_keys([pks[i] for i in keyset])
|
||||||
globalR = combine_keys([commits[i] for i in keyset])
|
globalR = combine_keys([commits[i] for i in keyset])
|
||||||
print('-----------------')
|
print('-----------------')
|
||||||
print('Global pubkey: %s' %hex(globalPk))
|
print('Global pubkey: %s' % hex(globalPk))
|
||||||
print('Global commit: %s' %hex(globalR))
|
print('Global commit: %s' % hex(globalR))
|
||||||
print('-----------------')
|
print('-----------------')
|
||||||
|
|
||||||
for i in range(0,5):
|
for i in range(0, 5):
|
||||||
seckey = sks[i]
|
seckey = sks[i]
|
||||||
pubkey = pks[i]
|
pubkey = pks[i]
|
||||||
r = nonces[i]
|
r = nonces[i]
|
||||||
R = commits[i]
|
R = commits[i]
|
||||||
h = ed25519raw.H(seckey)
|
h = ed25519raw.H(seckey)
|
||||||
b = ed25519raw.b
|
b = ed25519raw.b
|
||||||
a = 2**(b-2) + sum(2**i * ed25519raw.bit(h,i) for i in range(3,b-2))
|
a = 2**(b - 2) + sum(2**i * ed25519raw.bit(h, i)
|
||||||
|
for i in range(3, b - 2))
|
||||||
S = (r + ed25519raw.Hint(globalR + globalPk + digest) * a) % ed25519raw.l
|
S = (r + ed25519raw.Hint(globalR + globalPk + digest) * a) % ed25519raw.l
|
||||||
print('Local sig %d: %s' % (i+1, hex(ed25519raw.encodeint(S))))
|
print('Local sig %d: %s' % (i + 1, hex(ed25519raw.encodeint(S))))
|
||||||
commits.append(R)
|
commits.append(R)
|
||||||
sigs.append(ed25519raw.encodeint(S))
|
sigs.append(ed25519raw.encodeint(S))
|
||||||
|
|
||||||
print('-----------------')
|
print('-----------------')
|
||||||
sig = combine_sig(globalR, [sigs[i] for i in [1,3,4]])
|
sig = combine_sig(globalR, [sigs[i] for i in [1, 3, 4]])
|
||||||
print('Global sig: %s' %hex(sig))
|
print('Global sig: %s' % hex(sig))
|
||||||
ed25519raw.checkvalid(sig, digest, globalPk)
|
ed25519raw.checkvalid(sig, digest, globalPk)
|
||||||
print('Valid Signature!')
|
print('Valid Signature!')
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ def process_rgb(w, h, data):
|
|||||||
pix = bytearray(w * h * 3)
|
pix = bytearray(w * h * 3)
|
||||||
for i in range(w * h):
|
for i in range(w * h):
|
||||||
c = (data[i * 2] << 8) + data[i * 2 + 1]
|
c = (data[i * 2] << 8) + data[i * 2 + 1]
|
||||||
pix[i * 3 ] = (c & 0xF800) >> 8
|
pix[i * 3 + 0] = (c & 0xF800) >> 8
|
||||||
pix[i * 3 + 1] = (c & 0x07C0) >> 3
|
pix[i * 3 + 1] = (c & 0x07C0) >> 3
|
||||||
pix[i * 3 + 2] = (c & 0x001F) << 3
|
pix[i * 3 + 2] = (c & 0x001F) << 3
|
||||||
return bytes(pix)
|
return bytes(pix)
|
||||||
@ -18,7 +18,7 @@ def process_rgb(w, h, data):
|
|||||||
def process_grayscale(w, h, data):
|
def process_grayscale(w, h, data):
|
||||||
pix = bytearray(w * h)
|
pix = bytearray(w * h)
|
||||||
for i in range(w * h // 2):
|
for i in range(w * h // 2):
|
||||||
pix[i * 2 ] = data[i] & 0xF0
|
pix[i * 2 + 0] = data[i] & 0xF0
|
||||||
pix[i * 2 + 1] = (data[i] & 0x0F) << 4
|
pix[i * 2 + 1] = (data[i] & 0x0F) << 4
|
||||||
return bytes(pix)
|
return bytes(pix)
|
||||||
|
|
||||||
@ -51,10 +51,10 @@ def process_image(ifn):
|
|||||||
return 4
|
return 4
|
||||||
data = zlib.decompress(data, -10)
|
data = zlib.decompress(data, -10)
|
||||||
|
|
||||||
|
|
||||||
if ifn.endswith('.toif'):
|
if ifn.endswith('.toif'):
|
||||||
if len(data) != w * h * 2:
|
if len(data) != w * h * 2:
|
||||||
print('Uncompressed data length mismatch (%d vs %d)' % (len(data), w * h * 2))
|
print('Uncompressed data length mismatch (%d vs %d)' %
|
||||||
|
(len(data), w * h * 2))
|
||||||
return 5
|
return 5
|
||||||
pix = process_rgb(w, h, data)
|
pix = process_rgb(w, h, data)
|
||||||
img = Image.frombuffer('RGB', (w, h), pix, 'raw', 'RGB', 0, 1)
|
img = Image.frombuffer('RGB', (w, h), pix, 'raw', 'RGB', 0, 1)
|
||||||
@ -63,7 +63,8 @@ def process_image(ifn):
|
|||||||
|
|
||||||
if ifn.endswith('.toig'):
|
if ifn.endswith('.toig'):
|
||||||
if len(data) != w * h // 2:
|
if len(data) != w * h // 2:
|
||||||
print('Uncompressed data length mismatch (%d vs %d)' % (len(data), w * h // 2))
|
print('Uncompressed data length mismatch (%d vs %d)' %
|
||||||
|
(len(data), w * h // 2))
|
||||||
return 6
|
return 6
|
||||||
pix = process_grayscale(w, h, data)
|
pix = process_grayscale(w, h, data)
|
||||||
img = Image.frombuffer('L', (w, h), pix, 'raw', 'L', 0, 1)
|
img = Image.frombuffer('L', (w, h), pix, 'raw', 'L', 0, 1)
|
||||||
|
17
tools/ttf2c
17
tools/ttf2c
@ -6,6 +6,7 @@ MAX_GLYPH = ord('~')
|
|||||||
|
|
||||||
# metrics explanation: https://www.freetype.org/freetype2/docs/glyphs/metrics.png
|
# metrics explanation: https://www.freetype.org/freetype2/docs/glyphs/metrics.png
|
||||||
|
|
||||||
|
|
||||||
def process_face(name, style, size):
|
def process_face(name, style, size):
|
||||||
print('Processing ... %s %s %s' % (name, style, size))
|
print('Processing ... %s %s %s' % (name, style, size))
|
||||||
face = freetype.Face('/usr/share/fonts/truetype/%s-%s.ttf' % (name, style))
|
face = freetype.Face('/usr/share/fonts/truetype/%s-%s.ttf' % (name, style))
|
||||||
@ -24,20 +25,20 @@ def process_face(name, style, size):
|
|||||||
face.load_char(c, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_NORMAL)
|
face.load_char(c, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_NORMAL)
|
||||||
bitmap = face.glyph.bitmap
|
bitmap = face.glyph.bitmap
|
||||||
metrics = face.glyph.metrics
|
metrics = face.glyph.metrics
|
||||||
assert(metrics.width // 64 == bitmap.width)
|
assert metrics.width // 64 == bitmap.width
|
||||||
assert(metrics.height // 64 == bitmap.rows)
|
assert metrics.height // 64 == bitmap.rows
|
||||||
assert(metrics.horiAdvance % 64 == 0)
|
assert metrics.horiAdvance % 64 == 0
|
||||||
assert(metrics.horiBearingX % 64 == 0)
|
assert metrics.horiBearingX % 64 == 0
|
||||||
assert(metrics.horiBearingY % 64 == 0)
|
assert metrics.horiBearingY % 64 == 0
|
||||||
assert(bitmap.width == bitmap.pitch)
|
assert bitmap.width == bitmap.pitch
|
||||||
assert(len(bitmap.buffer) == bitmap.pitch * bitmap.rows)
|
assert len(bitmap.buffer) == bitmap.pitch * bitmap.rows
|
||||||
print('Loaded glyph "%c" ... %d x %d @ %d grays (%d bytes)' % (c, bitmap.width, bitmap.rows, bitmap.num_grays, len(bitmap.buffer)))
|
print('Loaded glyph "%c" ... %d x %d @ %d grays (%d bytes)' % (c, bitmap.width, bitmap.rows, bitmap.num_grays, len(bitmap.buffer)))
|
||||||
f.write('/* %c */ static const uint8_t Font_%s_%s_%d_glyph_%d[] = { %d, %d, %d, %d, %d' % (c, name, style, size, i, bitmap.width, bitmap.rows, metrics.horiAdvance // 64, metrics.horiBearingX // 64, metrics.horiBearingY // 64))
|
f.write('/* %c */ static const uint8_t Font_%s_%s_%d_glyph_%d[] = { %d, %d, %d, %d, %d' % (c, name, style, size, i, bitmap.width, bitmap.rows, metrics.horiAdvance // 64, metrics.horiBearingX // 64, metrics.horiBearingY // 64))
|
||||||
buf = list(bitmap.buffer)
|
buf = list(bitmap.buffer)
|
||||||
if len(buf) > 0:
|
if len(buf) > 0:
|
||||||
if len(buf) % 2 > 0:
|
if len(buf) % 2 > 0:
|
||||||
buf.append(0)
|
buf.append(0)
|
||||||
buf = [ ( (a & 0xF0) | (b >> 4) ) for a, b in [buf[i:i + 2] for i in range(0, len(buf), 2)] ]
|
buf = [((a & 0xF0) | (b >> 4)) for a, b in [buf[i:i + 2] for i in range(0, len(buf), 2)]]
|
||||||
f.write(', ' + ', '.join(['%d' % x for x in buf]))
|
f.write(', ' + ', '.join(['%d' % x for x in buf]))
|
||||||
f.write(' };\n')
|
f.write(' };\n')
|
||||||
f.write('\nconst uint8_t * const Font_%s_%s_%d[%d + 1 - %d] = {\n' % (name, style, size, MAX_GLYPH, MIN_GLYPH))
|
f.write('\nconst uint8_t * const Font_%s_%s_%d[%d + 1 - %d] = {\n' % (name, style, size, MAX_GLYPH, MIN_GLYPH))
|
||||||
|
Loading…
Reference in New Issue
Block a user