Remove metadata support from reencrypt()

pull/270/head
Bogdan Opanchuk 2021-06-01 23:53:29 -07:00
parent c7292fe92c
commit 6a1bc0ce14
8 changed files with 27 additions and 104 deletions

View File

@ -1,6 +1,6 @@
import pytest
from umbral import reencrypt, CapsuleFrag, PublicKey, Capsule, VerificationError
from umbral import encrypt, reencrypt, CapsuleFrag, PublicKey, Capsule, VerificationError
from umbral.curve_point import CurvePoint
@ -8,9 +8,8 @@ def test_cfrag_serialization(verification_keys, capsule, kfrags):
verifying_pk, delegating_pk, receiving_pk = verification_keys
metadata = b'This is an example of metadata for re-encryption request'
for kfrag in kfrags:
cfrag = reencrypt(capsule, kfrag, metadata=metadata)
cfrag = reencrypt(capsule, kfrag)
cfrag_bytes = bytes(cfrag)
new_cfrag = CapsuleFrag.from_bytes(cfrag_bytes)
@ -19,34 +18,15 @@ def test_cfrag_serialization(verification_keys, capsule, kfrags):
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata,
)
assert verified_cfrag == cfrag
# No metadata
with pytest.raises(VerificationError):
new_cfrag.verify(capsule,
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
)
# Wrong metadata
with pytest.raises(VerificationError):
new_cfrag.verify(capsule,
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=b'Not the same metadata',
)
# Wrong delegating key
with pytest.raises(VerificationError):
new_cfrag.verify(capsule,
verifying_pk=verifying_pk,
delegating_pk=receiving_pk,
receiving_pk=receiving_pk,
metadata=metadata,
)
# Wrong receiving key
@ -55,7 +35,6 @@ def test_cfrag_serialization(verification_keys, capsule, kfrags):
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=delegating_pk,
metadata=metadata,
)
# Wrong signing key
@ -64,34 +43,6 @@ def test_cfrag_serialization(verification_keys, capsule, kfrags):
verifying_pk=receiving_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata,
)
def test_cfrag_serialization_no_metadata(verification_keys, capsule, kfrags):
verifying_pk, delegating_pk, receiving_pk = verification_keys
for kfrag in kfrags:
# Create with no metadata
cfrag = reencrypt(capsule, kfrag)
cfrag_bytes = bytes(cfrag)
new_cfrag = CapsuleFrag.from_bytes(cfrag_bytes)
verified_cfrag = new_cfrag.verify(capsule,
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
)
assert verified_cfrag == cfrag
with pytest.raises(VerificationError):
new_cfrag.verify(capsule,
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=b'some metadata',
)
@ -103,8 +54,7 @@ def test_cfrag_with_wrong_capsule(verification_keys, kfrags, capsule_and_ciphert
capsule_alice1 = capsule
capsule_alice2, _unused_key2 = Capsule.from_public_key(delegating_pk)
metadata = b"some metadata"
cfrag = reencrypt(capsule_alice2, kfrags[0], metadata=metadata)
cfrag = reencrypt(capsule_alice2, kfrags[0])
cfrag = CapsuleFrag.from_bytes(bytes(cfrag)) # de-verify
with pytest.raises(VerificationError):
@ -112,7 +62,6 @@ def test_cfrag_with_wrong_capsule(verification_keys, kfrags, capsule_and_ciphert
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata,
)
@ -121,8 +70,7 @@ def test_cfrag_with_wrong_data(verification_keys, kfrags, capsule_and_ciphertext
capsule, ciphertext = capsule_and_ciphertext
verifying_pk, delegating_pk, receiving_pk = verification_keys
metadata = b"some metadata"
cfrag = reencrypt(capsule, kfrags[0], metadata=metadata)
cfrag = reencrypt(capsule, kfrags[0])
# Let's put random garbage in one of the cfrags
cfrag = CapsuleFrag.from_bytes(bytes(cfrag)) # de-verify
@ -134,7 +82,6 @@ def test_cfrag_with_wrong_data(verification_keys, kfrags, capsule_and_ciphertext
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata,
)
@ -142,8 +89,8 @@ def test_cfrag_is_hashable(verification_keys, capsule, kfrags):
verifying_pk, delegating_pk, receiving_pk = verification_keys
cfrag0 = reencrypt(capsule, kfrags[0], metadata=b'abcdef')
cfrag1 = reencrypt(capsule, kfrags[1], metadata=b'abcdef')
cfrag0 = reencrypt(capsule, kfrags[0])
cfrag1 = reencrypt(capsule, kfrags[1])
assert hash(cfrag0) != hash(cfrag1)
@ -154,13 +101,13 @@ def test_cfrag_is_hashable(verification_keys, capsule, kfrags):
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=b'abcdef')
)
assert hash(verified_cfrag) == hash(cfrag0)
def test_cfrag_str(capsule, kfrags):
cfrag0 = reencrypt(capsule, kfrags[0], metadata=b'abcdef')
cfrag0 = reencrypt(capsule, kfrags[0])
s = str(cfrag0)
assert 'VerifiedCapsuleFrag' in s

View File

@ -143,16 +143,16 @@ def test_kfrags(implementations):
def _reencrypt(umbral, verifying_pk_bytes, delegating_pk_bytes, receiving_pk_bytes,
capsule_bytes, kfrags_bytes, threshold, metadata):
capsule_bytes, kfrags_bytes, threshold):
capsule = umbral.Capsule.from_bytes(bytes(capsule_bytes))
verified_kfrags = _verify_kfrags(umbral, kfrags_bytes,
verifying_pk_bytes, delegating_pk_bytes, receiving_pk_bytes)
cfrags = [umbral.reencrypt(capsule, kfrag, metadata=metadata) for kfrag in verified_kfrags[:threshold]]
cfrags = [umbral.reencrypt(capsule, kfrag) for kfrag in verified_kfrags[:threshold]]
return [bytes(cfrag) for cfrag in cfrags]
def _decrypt_reencrypted(umbral, receiving_sk_bytes, delegating_pk_bytes, verifying_pk_bytes,
capsule_bytes, cfrags_bytes, ciphertext, metadata):
capsule_bytes, cfrags_bytes, ciphertext):
receiving_sk = umbral.SecretKey.from_bytes(receiving_sk_bytes)
receiving_pk = umbral.PublicKey.from_secret_key(receiving_sk)
@ -166,7 +166,7 @@ def _decrypt_reencrypted(umbral, receiving_sk_bytes, delegating_pk_bytes, verify
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata)
)
for cfrag in cfrags]
# Decryption by Bob
@ -184,7 +184,6 @@ def test_reencrypt(implementations):
umbral1, umbral2 = implementations
metadata = b'metadata'
threshold = 2
num_kfrags = 3
plaintext = b'peace at dawn'
@ -203,13 +202,13 @@ def test_reencrypt(implementations):
# On client 2
cfrags_bytes = _reencrypt(umbral2, verifying_pk_bytes, delegating_pk_bytes, receiving_pk_bytes,
capsule_bytes, kfrags_bytes, threshold, metadata)
capsule_bytes, kfrags_bytes, threshold)
# On client 1
plaintext_reencrypted = _decrypt_reencrypted(umbral1,
receiving_sk_bytes, delegating_pk_bytes, verifying_pk_bytes,
capsule_bytes, cfrags_bytes, ciphertext, metadata)
capsule_bytes, cfrags_bytes, ciphertext)
assert plaintext_reencrypted == plaintext

View File

@ -147,14 +147,12 @@ def test_cfrags():
CapsuleFrag.from_bytes(bytes.fromhex(json_kfrag['cfrag'])))
for json_kfrag in vector_suite['vectors']]
metadata = bytes.fromhex(vector_suite['metadata'])
for kfrag, cfrag in kfrags_n_cfrags:
verified_kfrag = kfrag.verify(verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk)
new_cfrag = reencrypt(capsule, verified_kfrag, metadata=metadata).cfrag
new_cfrag = reencrypt(capsule, verified_kfrag).cfrag
assert new_cfrag.point_e1 == cfrag.point_e1
assert new_cfrag.point_v1 == cfrag.point_v1
assert new_cfrag.kfrag_id == cfrag.kfrag_id
@ -163,4 +161,4 @@ def test_cfrags():
verifying_pk=verifying_pk,
delegating_pk=delegating_pk,
receiving_pk=receiving_pk,
metadata=metadata)
)

View File

@ -58,7 +58,6 @@ class CapsuleFragProof(Serializable):
kfrag: KeyFrag,
cfrag_e1: CurvePoint,
cfrag_v1: CurvePoint,
metadata: Optional[bytes],
) -> 'CapsuleFragProof':
params = PARAMETERS
@ -81,7 +80,7 @@ class CapsuleFragProof(Serializable):
v2 = v * t
u2 = u * t
h = hash_to_cfrag_verification([e, e1, e2, v, v1, v2, u, u1, u2], metadata)
h = hash_to_cfrag_verification([e, e1, e2, v, v1, v2, u, u1, u2])
###
@ -141,15 +140,11 @@ class CapsuleFrag(Serializable):
bytes(self.proof))
@classmethod
def reencrypted(cls,
capsule: Capsule,
kfrag: KeyFrag,
metadata: Optional[bytes] = None,
) -> 'CapsuleFrag':
def reencrypted(cls, capsule: Capsule, kfrag: KeyFrag) -> 'CapsuleFrag':
rk = kfrag.key
e1 = capsule.point_e * rk
v1 = capsule.point_v * rk
proof = CapsuleFragProof.from_kfrag_and_cfrag(capsule, kfrag, e1, v1, metadata)
proof = CapsuleFragProof.from_kfrag_and_cfrag(capsule, kfrag, e1, v1)
return cls(point_e1=e1,
point_v1=v1,
@ -163,12 +158,9 @@ class CapsuleFrag(Serializable):
verifying_pk: PublicKey,
delegating_pk: PublicKey,
receiving_pk: PublicKey,
metadata: Optional[bytes] = None,
) -> 'VerifiedCapsuleFrag':
"""
Verifies the validity of this fragment.
``metadata`` should coincide with the one given to :py:func:`reencrypt`.
"""
params = PARAMETERS
@ -189,7 +181,7 @@ class CapsuleFrag(Serializable):
v2 = self.proof.point_v2
u2 = self.proof.kfrag_pok
h = hash_to_cfrag_verification([e, e1, e2, v, v1, v2, u, u1, u2], metadata)
h = hash_to_cfrag_verification([e, e1, e2, v, v1, v2, u, u1, u2])
###

View File

@ -63,14 +63,10 @@ def hash_to_shared_secret(precursor: CurvePoint,
return CurveScalar.from_digest(digest)
def hash_to_cfrag_verification(points: Iterable[CurvePoint],
metadata: Optional[bytes] = None
) -> CurveScalar:
def hash_to_cfrag_verification(points: Iterable[CurvePoint]) -> CurveScalar:
digest = Hash(b"CFRAG_VERIFICATION")
for point in points:
digest.update(point)
if metadata is not None:
digest.update(metadata)
return CurveScalar.from_digest(digest)

View File

@ -60,23 +60,17 @@ def generate_kfrags(delegating_sk: SecretKey,
return [VerifiedKeyFrag(kfrag) for kfrag in kfrags]
def reencrypt(capsule: Capsule,
kfrag: VerifiedKeyFrag,
metadata: Optional[bytes] = None
) -> VerifiedCapsuleFrag:
def reencrypt(capsule: Capsule, kfrag: VerifiedKeyFrag) -> VerifiedCapsuleFrag:
"""
Creates a capsule fragment using the given key fragment.
Capsule fragments can later be used to decrypt the ciphertext.
If `metadata` is provided, it will have to be used for verification in
:py:meth:`CapsuleFrag.verify`.
"""
# We could let duck typing do its work,
# but it's better to make a common error more understandable.
if isinstance(kfrag, KeyFrag) and not isinstance(kfrag, VerifiedKeyFrag):
raise TypeError("KeyFrag must be verified before reencryption")
return VerifiedCapsuleFrag(CapsuleFrag.reencrypted(capsule, kfrag.kfrag, metadata))
return VerifiedCapsuleFrag(CapsuleFrag.reencrypted(capsule, kfrag.kfrag))
def decrypt_reencrypted(decrypting_sk: SecretKey,

View File

@ -237,9 +237,8 @@ create_test_vector_file(vector_suite, 'vectors_kfrags.json', generate_again=gene
vectors = list()
metadata = b'kfrag_metadata'
for kfrag in kfrags:
cfrag = reencrypt(capsule, kfrag, metadata)
cfrag = reencrypt(capsule, kfrag)
json_input = {'kfrag': hexlify(kfrag), 'cfrag': hexlify(cfrag)}
vectors.append(json_input)
@ -249,10 +248,9 @@ vector_suite = {
'enclosed Capsule, under the enclosed delegating, '
'verifying and receiving keys. Each CFrag must deserialize '
'correctly and can be replicated with a call to '
'`reencrypt(kfrag, capsule, , b\'kfrag_metadata\')`'),
'`reencrypt(kfrag, capsule)`'),
'params': 'default',
'capsule': hexlify(capsule),
'metadata': hexlify(metadata),
'verifying_pk': hexlify(verifying_pk),
'delegating_pk': hexlify(delegating_pk),
'receiving_pk': hexlify(receiving_pk),

View File

@ -1,9 +1,8 @@
{
"name": "Test vectors for CFrags",
"description": "This is a collection of CFrags, originated from the enclosed Capsule, under the enclosed delegating, verifying and receiving keys. Each CFrag must deserialize correctly and can be replicated with a call to `reencrypt(kfrag, capsule, , b'kfrag_metadata')`",
"description": "This is a collection of CFrags, originated from the enclosed Capsule, under the enclosed delegating, verifying and receiving keys. Each CFrag must deserialize correctly and can be replicated with a call to `reencrypt(kfrag, capsule)`",
"params": "default",
"capsule": "02558f1de19a58e73a94e8fbbc6d3b1de2d312d90746ea74cb29f046943bf5787102906780e9484aec2102a01a157f10ced5aec952cd00631d94d5ea2edfa9b6808361b109353b0827b7e4013ab92a70eb3337a37f6fe34b3ccb058592caa246c974",
"metadata": "6b667261675f6d65746164617461",
"verifying_pk": "030b95b3f249297824b32d3391392d62a9aff32e8698fa78c7e8ce4a9d17071f56",
"delegating_pk": "02d67029bb92522059225d190038230c23466e28d132d48f714f9098168a562b8a",
"receiving_pk": "03b0d0243e8954b408047eee3b09b5ed132ccc25ec70e99fc74b6e9f54e5ecf9c7",
@ -49,4 +48,4 @@
"cfrag": "03a3e0b6b2fa9f634acac1d4ed682dbf0c08ba27889087854059d80d021797d7b403a64ced5bc32ad0cf5746eb5ab1ebdc12a1d1bbbd0f45d0fc90c3e4b56ba70546df55683a69e4136205a61c895959d86eb64840bb2175d5d15e4491b27e6a38bc036523005d1234abcd44d36704620cfcff444edac7a64dfc256281966351a7803b02ef471b06988f07ce34c7bfa5825603eedbde4138c05296055cff54f6204d245a02119417e7aa9c207a71540abefb8de8e04c977db7d11a82e2f98a1ea3fff5519e03ab6ffe8768ecd246956362065ca48488183f96c3ee5957be7a7ce02f3ef9cb1202e8febd066a999b83b4441662636c6c978cb8789553914b5ed94c02d4784432d74e9fffcb5ce10b65d95624b23940415f0dbe29e174a6b73c78cb3d5d380d8882ae3cc293a58554a55dbc50c0e111c6e9bb0208631833da5e66b1adb2aa1ecdd615c09098fba7af4b03763b846e1f5358a6bac27c36eadd6cd2c98fd58c277e0b"
}
]
}
}