2016-02-22 11:13:36 +00:00
|
|
|
# Ed25519 digital signatures
|
|
|
|
# Based on http://ed25519.cr.yp.to/python/ed25519.py
|
|
|
|
# See also http://ed25519.cr.yp.to/software.html
|
|
|
|
# Adapted by Ron Garret
|
|
|
|
# Sped up considerably using coordinate transforms found on:
|
|
|
|
# http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
|
|
|
# Specifically add-2008-hwcd-4 and dbl-2008-hwcd
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
import hashlib
|
|
|
|
import random
|
|
|
|
|
|
|
|
try: # pragma nocover
|
2016-02-22 11:13:36 +00:00
|
|
|
unicode
|
|
|
|
PY3 = False
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def asbytes(b):
|
|
|
|
"""Convert array of integers to byte string"""
|
|
|
|
return ''.join(chr(x) for x in b)
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def joinbytes(b):
|
|
|
|
"""Convert array of bytes to byte string"""
|
|
|
|
return ''.join(b)
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def bit(h, i):
|
|
|
|
"""Return i'th bit of bytestring h"""
|
2017-10-13 12:49:49 +00:00
|
|
|
return (ord(h[i // 8]) >> (i % 8)) & 1
|
|
|
|
except NameError: # pragma nocover
|
2016-02-22 11:13:36 +00:00
|
|
|
PY3 = True
|
|
|
|
asbytes = bytes
|
|
|
|
joinbytes = bytes
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
def bit(h, i):
|
|
|
|
return (h[i // 8] >> (i % 8)) & 1
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
b = 256
|
2017-10-13 12:49:49 +00:00
|
|
|
q = 2 ** 255 - 19
|
|
|
|
l = 2 ** 252 + 27742317777372353535851937790883648493
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
def H(m):
|
|
|
|
return hashlib.sha512(m).digest()
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def expmod(b, e, m):
|
2017-10-13 12:49:49 +00:00
|
|
|
if e == 0:
|
|
|
|
return 1
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
t = expmod(b, e // 2, m) ** 2 % m
|
2017-10-13 12:49:49 +00:00
|
|
|
if e & 1:
|
|
|
|
t = (t * b) % m
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
return t
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
# Can probably get some extra speedup here by replacing this with
|
|
|
|
# an extended-euclidean, but performance seems OK without that
|
|
|
|
def inv(x):
|
2017-10-13 12:49:49 +00:00
|
|
|
return expmod(x, q - 2, q)
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
d = -121665 * inv(121666)
|
2017-10-13 12:49:49 +00:00
|
|
|
I = expmod(2, (q - 1) // 4, q)
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
def xrecover(y):
|
2017-10-13 12:49:49 +00:00
|
|
|
xx = (y * y - 1) * inv(d * y * y + 1)
|
|
|
|
x = expmod(xx, (q + 3) // 8, q)
|
|
|
|
if (x * x - xx) % q != 0:
|
|
|
|
x = (x * I) % q
|
|
|
|
|
|
|
|
if x % 2 != 0:
|
|
|
|
x = q - x
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
return x
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
By = 4 * inv(5)
|
|
|
|
Bx = xrecover(By)
|
2017-10-13 12:49:49 +00:00
|
|
|
B = [Bx % q, By % q]
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
# def edwards(P,Q):
|
2016-02-22 11:13:36 +00:00
|
|
|
# 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)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
# def scalarmult(P,e):
|
2016-02-22 11:13:36 +00:00
|
|
|
# if e == 0: return [0,1]
|
|
|
|
# Q = scalarmult(P,e/2)
|
|
|
|
# Q = edwards(Q,Q)
|
|
|
|
# if e & 1: Q = edwards(Q,P)
|
|
|
|
# return Q
|
|
|
|
|
|
|
|
# Faster (!) version based on:
|
|
|
|
# http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
|
|
|
|
|
|
|
def xpt_add(pt1, pt2):
|
|
|
|
(X1, Y1, Z1, T1) = pt1
|
|
|
|
(X2, Y2, Z2, T2) = pt2
|
2017-10-13 12:49:49 +00:00
|
|
|
A = ((Y1 - X1) * (Y2 + X2)) % q
|
|
|
|
B = ((Y1 + X1) * (Y2 - X2)) % q
|
|
|
|
C = (Z1 * 2 * T2) % q
|
|
|
|
D = (T1 * 2 * Z2) % q
|
|
|
|
E = (D + C) % q
|
|
|
|
F = (B - A) % q
|
|
|
|
G = (B + A) % q
|
|
|
|
H = (D - C) % q
|
|
|
|
X3 = (E * F) % q
|
|
|
|
Y3 = (G * H) % q
|
|
|
|
Z3 = (F * G) % q
|
|
|
|
T3 = (E * H) % q
|
2016-02-22 11:13:36 +00:00
|
|
|
return (X3, Y3, Z3, T3)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
|
|
|
def xpt_double(pt):
|
2016-02-22 11:13:36 +00:00
|
|
|
(X1, Y1, Z1, _) = pt
|
2017-10-13 12:49:49 +00:00
|
|
|
A = (X1 * X1)
|
|
|
|
B = (Y1 * Y1)
|
|
|
|
C = (2 * Z1 * Z1)
|
2016-02-22 11:13:36 +00:00
|
|
|
D = (-A) % q
|
2017-10-13 12:49:49 +00:00
|
|
|
J = (X1 + Y1) % q
|
|
|
|
E = (J * J - A - B) % q
|
|
|
|
G = (D + B) % q
|
|
|
|
F = (G - C) % q
|
|
|
|
H = (D - B) % q
|
|
|
|
X3 = (E * F) % q
|
|
|
|
Y3 = (G * H) % q
|
|
|
|
Z3 = (F * G) % q
|
|
|
|
T3 = (E * H) % q
|
|
|
|
return X3, Y3, Z3, T3
|
|
|
|
|
|
|
|
|
|
|
|
def pt_xform(pt):
|
2016-02-22 11:13:36 +00:00
|
|
|
(x, y) = pt
|
2017-10-13 12:49:49 +00:00
|
|
|
return x, y, 1, (x * y) % q
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
def pt_unxform(pt):
|
2016-02-22 11:13:36 +00:00
|
|
|
(x, y, z, _) = pt
|
2017-10-13 12:49:49 +00:00
|
|
|
return (x * inv(z)) % q, (y * inv(z)) % q
|
|
|
|
|
|
|
|
|
|
|
|
def xpt_mult(pt, n):
|
|
|
|
if n == 0:
|
|
|
|
return pt_xform((0, 1))
|
|
|
|
|
|
|
|
_ = xpt_double(xpt_mult(pt, n >> 1))
|
|
|
|
return xpt_add(_, pt) if n & 1 else _
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def scalarmult(pt, e):
|
|
|
|
return pt_unxform(xpt_mult(pt_xform(pt), e))
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def encodeint(y):
|
|
|
|
bits = [(y >> i) & 1 for i in range(b)]
|
|
|
|
e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
|
2017-10-13 12:49:49 +00:00
|
|
|
for i in range(b // 8)]
|
2016-02-22 11:13:36 +00:00
|
|
|
return asbytes(e)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def encodepoint(P):
|
|
|
|
x = P[0]
|
|
|
|
y = P[1]
|
|
|
|
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
|
|
|
|
e = [(sum([bits[i * 8 + j] << j for j in range(8)]))
|
2017-10-13 12:49:49 +00:00
|
|
|
for i in range(b // 8)]
|
2016-02-22 11:13:36 +00:00
|
|
|
return asbytes(e)
|
2017-10-13 12:49:49 +00:00
|
|
|
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def publickey(sk):
|
|
|
|
h = H(sk)
|
2017-10-13 12:49:49 +00:00
|
|
|
a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
|
|
|
|
A = scalarmult(B, a)
|
2016-02-22 11:13:36 +00:00
|
|
|
return encodepoint(A)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def Hint(m):
|
|
|
|
h = H(m)
|
2017-10-13 12:49:49 +00:00
|
|
|
return sum(2 ** i * bit(h, i) for i in range(2 * b))
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
def signature(m, sk, pk):
|
2016-02-22 11:13:36 +00:00
|
|
|
h = H(sk)
|
2017-10-13 12:49:49 +00:00
|
|
|
a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
|
|
|
|
inter = joinbytes([h[i] for i in range(b // 8, b // 4)])
|
2016-02-22 11:13:36 +00:00
|
|
|
r = Hint(inter + m)
|
2017-10-13 12:49:49 +00:00
|
|
|
R = scalarmult(B, r)
|
2016-02-22 11:13:36 +00:00
|
|
|
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
|
|
|
|
return encodepoint(R) + encodeint(S)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def isoncurve(P):
|
|
|
|
x = P[0]
|
|
|
|
y = P[1]
|
2017-10-13 12:49:49 +00:00
|
|
|
return (-x * x + y * y - 1 - d * x * x * y * y) % q == 0
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
def decodeint(s):
|
2017-10-13 12:49:49 +00:00
|
|
|
return sum(2 ** i * bit(s, i) for i in range(0, b))
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
def decodepoint(s):
|
2017-10-13 12:49:49 +00:00
|
|
|
y = sum(2 ** i * bit(s, i) for i in range(0, b - 1))
|
2016-02-22 11:13:36 +00:00
|
|
|
x = xrecover(y)
|
2017-10-13 12:49:49 +00:00
|
|
|
if x & 1 != bit(s, b - 1):
|
|
|
|
x = q - x
|
|
|
|
|
|
|
|
P = [x, y]
|
|
|
|
if not isoncurve(P):
|
|
|
|
raise Exception("decoding point that is not on curve")
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
return P
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def checkvalid(s, m, pk):
|
2017-10-13 12:49:49 +00:00
|
|
|
if len(s) != b // 4:
|
|
|
|
raise Exception("signature length is wrong")
|
|
|
|
if len(pk) != b // 8:
|
|
|
|
raise Exception("public-key length is wrong")
|
|
|
|
|
|
|
|
R = decodepoint(s[0:b // 8])
|
2016-02-22 11:13:36 +00:00
|
|
|
A = decodepoint(pk)
|
2017-10-13 12:49:49 +00:00
|
|
|
S = decodeint(s[b // 8:b // 4])
|
2016-02-22 11:13:36 +00:00
|
|
|
h = Hint(encodepoint(R) + pk + m)
|
2017-10-13 12:49:49 +00:00
|
|
|
v1 = scalarmult(B, S)
|
|
|
|
# v2 = edwards(R,scalarmult(A,h))
|
2016-02-22 11:13:36 +00:00
|
|
|
v2 = pt_unxform(xpt_add(pt_xform(R), pt_xform(scalarmult(A, h))))
|
2017-10-13 12:49:49 +00:00
|
|
|
return v1 == v2
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
|
|
|
|
##########################################################
|
|
|
|
#
|
|
|
|
# Curve25519 reference implementation by Matthew Dempsky, from:
|
|
|
|
# http://cr.yp.to/highspeed/naclcrypto-20090310.pdf
|
|
|
|
|
|
|
|
# P = 2 ** 255 - 19
|
|
|
|
P = q
|
|
|
|
A = 486662
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
|
|
|
# def expmod(b, e, m):
|
2016-02-22 11:13:36 +00:00
|
|
|
# if e == 0: return 1
|
|
|
|
# t = expmod(b, e / 2, m) ** 2 % m
|
|
|
|
# if e & 1: t = (t * b) % m
|
|
|
|
# return t
|
|
|
|
|
|
|
|
# def inv(x): return expmod(x, P - 2, P)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def add(n, m, d):
|
|
|
|
(xn, zn) = n
|
2017-10-13 12:49:49 +00:00
|
|
|
(xm, zm) = m
|
2016-02-22 11:13:36 +00:00
|
|
|
(xd, zd) = d
|
|
|
|
x = 4 * (xm * xn - zm * zn) ** 2 * zd
|
|
|
|
z = 4 * (xm * zn - zm * xn) ** 2 * xd
|
|
|
|
return (x % P, z % P)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def double(n):
|
|
|
|
(xn, zn) = n
|
|
|
|
x = (xn ** 2 - zn ** 2) ** 2
|
|
|
|
z = 4 * xn * zn * (xn ** 2 + A * xn * zn + zn ** 2)
|
|
|
|
return (x % P, z % P)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def curve25519(n, base=9):
|
2017-10-13 12:49:49 +00:00
|
|
|
one = (base, 1)
|
2016-02-22 11:13:36 +00:00
|
|
|
two = double(one)
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
# f(m) evaluates to a tuple
|
|
|
|
# containing the mth multiple and the
|
|
|
|
# (m+1)th multiple of base.
|
|
|
|
def f(m):
|
2017-10-13 12:49:49 +00:00
|
|
|
if m == 1:
|
|
|
|
return (one, two)
|
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
(pm, pm1) = f(m // 2)
|
2017-10-13 12:49:49 +00:00
|
|
|
if m & 1:
|
2016-02-22 11:13:36 +00:00
|
|
|
return (add(pm, pm1, one), double(pm1))
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
return (double(pm), add(pm, pm1, one))
|
2017-10-13 12:49:49 +00:00
|
|
|
|
|
|
|
((x, z), _) = f(n)
|
2016-02-22 11:13:36 +00:00
|
|
|
return (x * inv(z)) % P
|
|
|
|
|
|
|
|
|
|
|
|
def genkey(n=0):
|
2017-10-13 12:49:49 +00:00
|
|
|
n = n or random.randint(0, P)
|
2016-02-22 11:13:36 +00:00
|
|
|
n &= ~7
|
|
|
|
n &= ~(128 << 8 * 31)
|
|
|
|
n |= 64 << 8 * 31
|
|
|
|
return n
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
|
|
|
# def str2int(s):
|
2016-02-22 11:13:36 +00:00
|
|
|
# return int(hexlify(s), 16)
|
|
|
|
# # return sum(ord(s[i]) << (8 * i) for i in range(32))
|
|
|
|
#
|
2017-10-13 12:49:49 +00:00
|
|
|
# def int2str(n):
|
2016-02-22 11:13:36 +00:00
|
|
|
# return unhexlify("%x" % n)
|
|
|
|
# # return ''.join([chr((n >> (8 * i)) & 255) for i in range(32)])
|
|
|
|
|
|
|
|
#################################################
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def dsa_test():
|
|
|
|
import os
|
2017-10-13 12:49:49 +00:00
|
|
|
msg = str(random.randint(q, q + q)).encode('utf-8')
|
2016-02-22 11:13:36 +00:00
|
|
|
sk = os.urandom(32)
|
|
|
|
pk = publickey(sk)
|
|
|
|
sig = signature(msg, sk, pk)
|
|
|
|
return checkvalid(sig, msg, pk)
|
|
|
|
|
2017-10-13 12:49:49 +00:00
|
|
|
|
2016-02-22 11:13:36 +00:00
|
|
|
def dh_test():
|
|
|
|
sk1 = genkey()
|
|
|
|
sk2 = genkey()
|
|
|
|
return curve25519(sk1, curve25519(sk2)) == curve25519(sk2, curve25519(sk1))
|