Begin moving things over to module with default_params/default_curve

pull/51/head
tuxxy 2018-02-07 22:26:38 -07:00
parent af62006e9b
commit ef766d8bba
3 changed files with 31 additions and 24 deletions

View File

@ -1,5 +1,6 @@
from umbral.bignum import BigNum
from cryptography.hazmat.backends.openssl import backend
from cryptography.hazmat.primitives.asymmetric import ec
from umbral.config import default_curve
@ -15,7 +16,7 @@ class Point(object):
self.group = group
@classmethod
def gen_rand(cls, curve):
def gen_rand(cls, curve: ec.EllipticCurve=None):
"""
Returns a Point object with a cryptographically secure EC_POINT based
on the provided curve.

View File

@ -186,44 +186,46 @@ class PRE(object):
g = params.g
return priv * g
def split_rekey(self, priv_a, pub_b, threshold, N):
def split_rekey(self, priv_a, pub_b, threshold, N, params: UmbralParameters=None):
"""
Creates a re-encryption key and splits it using Shamir's Secret Sharing.
Requires a threshold number of fragments out of N to rebuild rekey.
Returns rekeys and the vKeys.
"""
params = params if params is not None else default_params()
if type(priv_a) == UmbralPrivateKey:
priv_a = priv_a.bn_key
if type(pub_b) == UmbralPublicKey:
pub_b = pub_b.point_key
g = self.params.g
g = params.g
pub_a = priv_a * g
x = BigNum.gen_rand(self.params.curve)
x = BigNum.gen_rand(params.curve)
xcomp = x * g
d = hash_to_bn([xcomp, pub_b, pub_b * x], self.params)
d = hash_to_bn([xcomp, pub_b, pub_b * x], params)
coeffs = [priv_a * (~d)]
coeffs += [BigNum.gen_rand(self.params.curve) for _ in range(threshold - 1)]
coeffs += [BigNum.gen_rand(params.curve) for _ in range(threshold - 1)]
h = self.params.h
u = self.params.u
h = params.h
u = params.u
vKeys = [coeff * h for coeff in coeffs]
rk_shares = []
for _ in range(N):
id_kfrag = BigNum.gen_rand(self.params.curve)
id_kfrag = BigNum.gen_rand(params.curve)
rk = poly_eval(coeffs, id_kfrag)
u1 = rk * u
y = BigNum.gen_rand(self.params.curve)
y = BigNum.gen_rand(params.curve)
z1 = hash_to_bn([y * g, id_kfrag, pub_a, pub_b, u1, xcomp], self.params)
z1 = hash_to_bn([y * g, id_kfrag, pub_a, pub_b, u1, xcomp], params)
z2 = y - priv_a * z1
kFrag = KFrag(id_=id_kfrag, key=rk, x=xcomp, u1=u1, z1=z1, z2=z2)

View File

@ -6,6 +6,7 @@ from cryptography.exceptions import InternalError
from umbral.bignum import BigNum
from umbral.point import Point
def lambda_coeff(id_i, selected_ids):
ids = [x for x in selected_ids if x != id_i]
@ -20,6 +21,7 @@ def lambda_coeff(id_i, selected_ids):
return result
def poly_eval(coeff, x):
result = coeff[-1]
for i in range(-2, -len(coeff) - 1, -1):
@ -27,28 +29,29 @@ def poly_eval(coeff, x):
return result
# minVal = (1 << 256) % self.order (i.e., 2^256 % order)
MINVAL_SECP256K1_HASH_256 = 432420386565659656852420866394968145599
def hash_to_bn(list, params):
def hash_to_bn(crypto_items, params):
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
for x in list:
if isinstance(x, Point):
bytes = x.to_bytes()
elif isinstance(x, BigNum):
bytes = int(x).to_bytes(32, byteorder='big')
for item in crypto_items:
if isinstance(item, Point):
data_bytes = item.to_bytes()
elif isinstance(item, BigNum):
data_bytes = int(item).to_bytes(32, byteorder='big')
else:
# print(type(x))
bytes = x
digest.update(bytes)
data_bytes = item
digest.update(data_bytes)
i = 0
h = 0
while h < MINVAL_SECP256K1_HASH_256:
digest_i = digest.copy()
digest_i.update(i.to_bytes(32, byteorder='big'))
hash = digest_i.finalize()
h = int.from_bytes(hash, byteorder='big', signed=False)
hash_digest = digest_i.finalize()
h = int.from_bytes(hash_digest, byteorder='big', signed=False)
i += 1
hash_bn = h % int(params.order)
@ -78,9 +81,9 @@ def unsafe_hash_to_point(curve, data, label=None):
ibytes = i.to_bytes(4, byteorder='big')
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(label + ibytes + data)
hash = digest.finalize()
hash_digest = digest.finalize()
compressed02 = b"\x02"+hash
compressed02 = b"\x02" + hash_digest
try:
h = Point.from_bytes(compressed02, curve)
@ -101,6 +104,7 @@ def unsafe_hash_to_point(curve, data, label=None):
# Only happens with probability 2^(-32)
raise ValueError('Could not hash input into the curve')
def kdf(ecpoint, key_length):
data = ecpoint.to_bytes(is_compressed=True)