Beispiel #1
0
def test_simple_api(N, M, curve=default_curve()):
    """Manually injects umbralparameters for multi-curve testing."""

    params = UmbralParameters(curve=curve)

    priv_key_alice = keys.UmbralPrivateKey.gen_key(params=params)
    pub_key_alice = priv_key_alice.get_pubkey()

    priv_key_bob = keys.UmbralPrivateKey.gen_key(params=params)
    pub_key_bob = priv_key_bob.get_pubkey()

    plain_data = b'peace at dawn'
    ciphertext, capsule = pre.encrypt(pub_key_alice, plain_data, params=params)

    cleartext = pre.decrypt(ciphertext, capsule, priv_key_alice, params=params)
    assert cleartext == plain_data

    kfrags = pre.split_rekey(priv_key_alice, pub_key_bob, M, N, params=params)
    for kfrag in kfrags:
        cfrag = pre.reencrypt(kfrag, capsule, params=params)
        capsule.attach_cfrag(cfrag)

    reenc_cleartext = pre.decrypt(ciphertext,
                                  capsule,
                                  priv_key_bob,
                                  pub_key_alice,
                                  params=params)
    assert reenc_cleartext == plain_data
Beispiel #2
0
    def get_offer_from_blockchain(self, offer_custon_id):
        # TODO
        # NOW IT IS BUMP
        f = open('./blockchain', 'rb')
        data = pickle.load(f)
        ciphertext = data[0]
        capsule = pre.Capsule.from_bytes(data[1], UmbralParameters(Curve(714)))
        #f.close()

        return ciphertext, capsule
Beispiel #3
0
def test_simple_api_on_multiple_curves(N, M, curve):
    params = UmbralParameters(curve=curve)

    delegating_privkey = keys.UmbralPrivateKey.gen_key(params=params)
    signing_privkey = keys.UmbralPrivateKey.gen_key(params=params)
    alices_keys = delegating_privkey, signing_privkey

    receiving_privkey = keys.UmbralPrivateKey.gen_key(params=params)
    receiving_pubkey = receiving_privkey.get_pubkey()
    bobs_keys = receiving_privkey, receiving_pubkey

    test_simple_api(alices_keys, bobs_keys, N, M, curve)
Beispiel #4
0
def test_simple_api(N, M, curve=default_curve()):
    """Manually injects umbralparameters for multi-curve testing."""
    params = UmbralParameters(curve=curve)

    priv_key_alice = keys.UmbralPrivateKey.gen_key(params=params)
    pub_key_alice = priv_key_alice.get_pubkey()

    priv_key_bob = keys.UmbralPrivateKey.gen_key(params=params)
    pub_key_bob = priv_key_bob.get_pubkey()

    plain_data = b'attack at dawn'
    ciphertext, capsule = umbral.encrypt(pub_key_alice, plain_data)

    cleartext = umbral.decrypt(capsule, priv_key_alice, ciphertext)
    assert cleartext == plain_data

    rekeys, _unused_vkeys = umbral.split_rekey(priv_key_alice, pub_key_bob, M, N, params=params)
    for rekey in rekeys:
        c_frag = umbral.reencrypt(rekey, capsule, params=params)
        capsule.attach_cfrag(c_frag)

    reenc_cleartext = umbral.decrypt(capsule, priv_key_bob, ciphertext, pub_key_alice)
    assert reenc_cleartext == plain_data
Beispiel #5
0
    def test_recepient_add_records_set(self):
        #Generate keys
        config.set_default_curve(SECP256K1)

        patient_private_key = keys.UmbralPrivateKey.gen_key()
        patient_public_key = patient_private_key.get_pubkey()
        recepient_private_key = keys.UmbralPrivateKey.gen_key()
        recepient_public_key = recepient_private_key.get_pubkey()

        recepient_signing_key = keys.UmbralPrivateKey.gen_key()
        recepient_verifying_key = recepient_signing_key.get_pubkey()
        recepient_signer = signing.Signer(private_key=recepient_signing_key)

        #Convert keys to hex
        hexed_patient_public_key = patient_public_key.to_bytes().hex()
        hexed_recepient_public_key = recepient_public_key.to_bytes().hex()
        hexed_recepient_verifying_key = recepient_verifying_key.to_bytes().hex()

        #Signup patient
        data = {
            'first_name': 'Gordo',
            'last_name': 'Freema',
            'email': '*****@*****.**',
            'password': '******',
            'eth_address': '0x73015966604928A312F79F7E69291a656Cb88603',
            'pub_key': hexed_patient_public_key
        }

        response = self.client.post('/patients/signup/', data)

        #Signup recepient
        data = {
            'organisation_id': 'someseriousorganisationidentifier',
            'first_name': 'Gordon',
            'last_name': 'Freeman',
            'email': '*****@*****.**',
            'password': '******',
            'eth_address': '0x73015966604928A312F79F7E69291a656Cb88603',
            'pub_key': hexed_recepient_public_key
        }

        response = self.client.post('/recepients/signup/', data)

        #Log in as patient
        self.client.login(username='******', password='******')

        data = {
            'patient_id': 2,
            'recepient_id': 2
        }

        #Delegate add records permission to recepient
        response = self.client.post('/patients/delegations/make/', data)

        #Log in as recepient
        self.client.login(username='******', password='******')

        #Encrypt message
        plaintext = b'Proxy Re-encryption is cool!'
        ciphertext, capsule = pre.encrypt(recepient_public_key, plaintext)
        kfrags = pre.generate_kfrags(delegating_privkey=recepient_private_key,
                                     signer=recepient_signer,
                                     receiving_pubkey=patient_public_key,
                                     threshold=10,
                                     N=20)
        #Add encrypted record
        data = {
            'patient_id': 2,
            'type': 'type',
            'data': ciphertext.hex(),
            'capsule': capsule.to_bytes().hex()
        }

        response = self.client.post('/records/add/', data)

        self.assertEqual(response.status_code, 201)

        #Add kfrags 
        for kfrag in kfrags:
            data = {
                'delegation_id': 1,
                'bytes': kfrag.to_bytes().hex()
            }
            self.client.post('/patients/kfrags/', data)
        
        #Add verifying_key
        data = {
            'records_set_id': 1,
            'recepient_id': 3,
            'verifying_key': hexed_recepient_verifying_key
        }

        response = self.client.post('/re_encryptions/', data)
        
        #Login as patient
        self.client.login(username='******', password='******')

        response = self.client.get('/me/records/')

        from_proxy_capsule = bytes.fromhex(response.data[0]['capsule'])
        from_proxy_data = bytes.fromhex(response.data[0]['data'])

        umbral_parameteres = UmbralParameters(SECP256K1)

        from_proxy_capsule = pre.Capsule.from_bytes(
            from_proxy_capsule,
            umbral_parameteres
        )

        response = self.client.get('/patients/kfrags/')
        """
        Patient can receive kfrags for each re_encryption
        """

        from_proxy_kfrags = [KFrag.from_bytes(bytes.fromhex(kfrag['bytes'])) for kfrag in response.data]

        response = self.client.get('/re_encryptions/1/')
        from_proxy_verifying_key = bytes.fromhex(response.data['verifying_key'])

        """
        Patient gets verifying_key
        """

        from_proxy_verifying_key = keys.UmbralPublicKey.from_bytes(
            from_proxy_verifying_key,
            params=umbral_parameteres
        )

        from_proxy_capsule.set_correctness_keys(
            delegating=recepient_public_key,
            receiving=patient_public_key,
            verifying=from_proxy_verifying_key
        )


        for kfrag in from_proxy_kfrags:
            cfrag = pre.reencrypt(kfrag, from_proxy_capsule)
            from_proxy_capsule.attach_cfrag(cfrag)

        cleartext = pre.decrypt(
            ciphertext=from_proxy_data,
            capsule=from_proxy_capsule,
            decrypting_key=patient_private_key
        )

        """
        Patient opens the capsule to decrypt records
        organisation added. Then patient can reencrypt whole
        records to delegate editing and reading permissions
        to other organisations
        """

        self.assertEqual(cleartext, plaintext)
Beispiel #6
0
import base64
import sys
from umbral import pre, keys
from umbral.kfrags import KFrag
from umbral.config import default_curve
from umbral.params import UmbralParameters

encoded_kfrags = sys.argv[1].split(',')
for idx, row in enumerate(encoded_kfrags):
    encoded_kfrags[idx] = KFrag.from_bytes(base64.b64decode(row))
kfrags = tuple(encoded_kfrags)

params = UmbralParameters(default_curve())
capsule = pre.Capsule.from_bytes(base64.b64decode(sys.argv[2]), params)
ciphertext = base64.b64decode(sys.argv[3])
bobs_private_key = keys.UmbralPrivateKey.from_bytes(
    base64.b64decode(sys.argv[4]))
bobs_public_key = keys.UmbralPublicKey.from_bytes(base64.b64decode(
    sys.argv[5]))
alice_public_key = keys.UmbralPublicKey.from_bytes(
    base64.b64decode(sys.argv[6]))

capsule.set_correctness_keys(alice_public_key, bobs_public_key,
                             alice_public_key)

for kfrag in kfrags:
    cfrag = pre.reencrypt(kfrag, capsule)
    capsule.attach_cfrag(cfrag)

cleartext = pre.decrypt(ciphertext, capsule, bobs_private_key,
                        alice_public_key)