def kfrags(alices_keys, bobs_keys): delegating_sk, signing_sk = alices_keys receiving_sk, receiving_pk = bobs_keys yield generate_kfrags(delegating_sk=delegating_sk, signer=Signer(signing_sk), receiving_pk=receiving_pk, threshold=6, num_kfrags=10)
def test_wrong_threshold(alices_keys, bobs_keys): delegating_sk, signing_sk = alices_keys _receiving_sk, receiving_pk = bobs_keys with pytest.raises(ValueError): KeyFragBase(delegating_sk=delegating_sk, receiving_pk=receiving_pk, signer=Signer(signing_sk), threshold=0)
def test_wrong_num_kfrags(alices_keys, bobs_keys): delegating_sk, signing_sk = alices_keys _receiving_sk, receiving_pk = bobs_keys # Trying to create less kfrags than the threshold with pytest.raises(ValueError): generate_kfrags(delegating_sk=delegating_sk, signer=Signer(signing_sk), receiving_pk=receiving_pk, threshold=3, num_kfrags=2)
def test_simple_api(num_kfrags, threshold): """ This test models the main interactions between actors (i.e., Alice, Bob, Data Source, and Ursulas) and artifacts (i.e., public and private keys, ciphertexts, capsules, KFrags, CFrags, etc). The test covers all the main stages of data sharing: key generation, delegation, encryption, decryption by Alice, re-encryption by Ursula, and decryption by Bob. """ # Key Generation (Alice) delegating_sk = SecretKey.random() delegating_pk = delegating_sk.public_key() signing_sk = SecretKey.random() signer = Signer(signing_sk) verifying_pk = signing_sk.public_key() # Key Generation (Bob) receiving_sk = SecretKey.random() receiving_pk = receiving_sk.public_key() # Encryption by an unnamed data source plaintext = b'peace at dawn' capsule, ciphertext = encrypt(delegating_pk, plaintext) # Decryption by Alice plaintext_decrypted = decrypt_original(delegating_sk, capsule, ciphertext) assert plaintext_decrypted == plaintext # Split Re-Encryption Key Generation (aka Delegation) kfrags = generate_kfrags(delegating_sk=delegating_sk, receiving_pk=receiving_pk, signer=signer, threshold=threshold, num_kfrags=num_kfrags) # Bob requests re-encryption to some set of M ursulas cfrags = [reencrypt(capsule, kfrag) for kfrag in kfrags] # Decryption by Bob plaintext_reenc = decrypt_reencrypted( receiving_sk=receiving_sk, delegating_pk=delegating_pk, capsule=capsule, verified_cfrags=cfrags[:threshold], ciphertext=ciphertext, ) assert plaintext_reenc == plaintext
def test_open_reencrypted(alices_keys, bobs_keys): threshold = 6 shares = 10 delegating_sk, signing_sk = alices_keys receiving_sk, receiving_pk = bobs_keys signer = Signer(signing_sk) delegating_pk = delegating_sk.public_key() capsule, key = Capsule.from_public_key(delegating_pk) kfrags = generate_kfrags(delegating_sk=delegating_sk, signer=signer, receiving_pk=receiving_pk, threshold=threshold, shares=shares) cfrags = [reencrypt(capsule, kfrag).cfrag for kfrag in kfrags] key_back = capsule.open_reencrypted(receiving_sk, delegating_pk, cfrags[:threshold]) assert key_back == key # No cfrags at all with pytest.raises(ValueError, match="Empty CapsuleFrag sequence"): capsule.open_reencrypted(receiving_sk, delegating_pk, []) # Not enough cfrags with pytest.raises(ValueError, match="Internal validation failed"): capsule.open_reencrypted(receiving_sk, delegating_pk, cfrags[:threshold - 1]) # Repeating cfrags with pytest.raises(ValueError, match="Some of the CapsuleFrags are repeated"): capsule.open_reencrypted(receiving_sk, delegating_pk, [cfrags[0]] + cfrags[:threshold - 1]) # Mismatched cfrags kfrags2 = generate_kfrags(delegating_sk=delegating_sk, signer=signer, receiving_pk=receiving_pk, threshold=threshold, shares=shares) cfrags2 = [reencrypt(capsule, kfrag).cfrag for kfrag in kfrags2] with pytest.raises(ValueError, match="CapsuleFrags are not pairwise consistent"): capsule.open_reencrypted(receiving_sk, delegating_pk, [cfrags2[0]] + cfrags[:threshold - 1])
def test_kfrag_signing(alices_keys, bobs_keys, sign_delegating_key, sign_receiving_key): delegating_sk, signing_sk = alices_keys _receiving_sk, receiving_pk = bobs_keys verifying_pk = signing_sk.public_key() delegating_pk = delegating_sk.public_key() base = KeyFragBase(delegating_sk=delegating_sk, receiving_pk=receiving_pk, signer=Signer(signing_sk), threshold=6) kfrag = KeyFrag.from_base(base=base, sign_delegating_key=sign_delegating_key, sign_receiving_key=sign_receiving_key) # serialize/deserialize to make sure sign_* fields are serialized correctly kfrag = KeyFrag.from_bytes(bytes(kfrag)) for pass_delegating_key, pass_receiving_key in zip([False, True], [False, True]): delegating_key_ok = (not sign_delegating_key) or pass_delegating_key receiving_key_ok = (not sign_receiving_key) or pass_receiving_key should_verify = delegating_key_ok and receiving_key_ok verification_passed = True try: kfrag.verify( verifying_pk=verifying_pk, delegating_pk=delegating_pk if pass_delegating_key else None, receiving_pk=receiving_pk if pass_receiving_key else None) except VerificationError: verification_passed = False assert verification_passed == should_verify
import random from umbral import ( SecretKey, Signer, CapsuleFrag, encrypt, generate_kfrags, reencrypt, decrypt_original, decrypt_reencrypted) # Generate an Umbral key pair # --------------------------- # First, Let's generate two asymmetric key pairs for Alice: # A delegating key pair and a Signing key pair. alices_secret_key = SecretKey.random() alices_public_key = alices_secret_key.public_key() alices_signing_key = SecretKey.random() alices_verifying_key = alices_signing_key.public_key() alices_signer = Signer(alices_signing_key) # Encrypt some data for Alice # --------------------------- # Now let's encrypt data with Alice's public key. # Invocation of `pre.encrypt` returns both the `ciphertext`, # and a `capsule`. Anyone with Alice's public key can perform # this operation. plaintext = b'Proxy Re-encryption is cool!' capsule, ciphertext = encrypt(alices_public_key, plaintext) print(ciphertext) # Decrypt data for Alice # ---------------------- # Since data was encrypted with Alice's public key,
######### # SETUP # ######### # We create also some Umbral objects for later delegating_sk = SecretKey.random() receiving_sk = SecretKey.random() signing_sk = SecretKey.random() verifying_pk = signing_sk.public_key() delegating_pk = delegating_sk.public_key() receiving_pk = receiving_sk.public_key() kfrags = generate_kfrags(delegating_sk=delegating_sk, receiving_pk=receiving_pk, signer=Signer(signing_sk), threshold=6, shares=10, ) plain_data = b'peace at dawn' capsule, ciphertext = encrypt(delegating_pk, plain_data) cfrag = CapsuleFrag.from_bytes(bytes(reencrypt(capsule, kfrags[0]))) points = [capsule.point_e, cfrag.point_e1, cfrag.proof.point_e2, capsule.point_v, cfrag.point_v1, cfrag.proof.point_v2, cfrag.proof.kfrag_commitment, cfrag.proof.kfrag_pok] z = cfrag.proof.signature