2018-07-25 17:55:12 +00:00
|
|
|
|
"""
|
|
|
|
|
Copyright (C) 2018 NuCypher
|
|
|
|
|
|
|
|
|
|
This file is part of pyUmbral.
|
|
|
|
|
|
|
|
|
|
pyUmbral is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
pyUmbral is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with pyUmbral. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
"""
|
|
|
|
|
|
2018-02-23 06:24:09 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
2018-06-01 16:43:07 +00:00
|
|
|
|
from umbral import pre
|
2018-02-23 06:24:09 +00:00
|
|
|
|
from umbral.config import default_curve
|
|
|
|
|
from umbral.params import UmbralParameters
|
2018-05-21 22:35:06 +00:00
|
|
|
|
from umbral.signing import Signer
|
2018-08-17 23:42:27 +00:00
|
|
|
|
from umbral.keys import UmbralPrivateKey
|
|
|
|
|
from ..conftest import parameters, other_supported_curves
|
2018-02-23 06:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("N, M", parameters)
|
2018-05-31 12:33:11 +00:00
|
|
|
|
def test_simple_api(N, M, curve=default_curve()):
|
2018-08-09 12:09:21 +00:00
|
|
|
|
"""
|
|
|
|
|
This test models the main interactions between NuCypher 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 with NuCypher:
|
|
|
|
|
key generation, delegation, encryption, decryption by
|
|
|
|
|
Alice, re-encryption by Ursula, and decryption by Bob.
|
|
|
|
|
|
|
|
|
|
Manually injects umbralparameters for multi-curve testing."""
|
|
|
|
|
|
|
|
|
|
# Generation of global parameters
|
2018-02-23 06:24:09 +00:00
|
|
|
|
params = UmbralParameters(curve=curve)
|
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Key Generation (Alice)
|
2018-06-01 16:43:07 +00:00
|
|
|
|
delegating_privkey = UmbralPrivateKey.gen_key(params=params)
|
2018-05-31 10:46:43 +00:00
|
|
|
|
delegating_pubkey = delegating_privkey.get_pubkey()
|
|
|
|
|
|
2018-06-01 16:43:07 +00:00
|
|
|
|
signing_privkey = UmbralPrivateKey.gen_key(params=params)
|
2018-05-31 12:33:11 +00:00
|
|
|
|
signing_pubkey = signing_privkey.get_pubkey()
|
2018-05-25 22:43:19 +00:00
|
|
|
|
signer = Signer(signing_privkey)
|
2018-02-23 06:24:09 +00:00
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Key Generation (Bob)
|
2018-06-01 16:43:07 +00:00
|
|
|
|
receiving_privkey = UmbralPrivateKey.gen_key(params=params)
|
2018-05-31 12:33:11 +00:00
|
|
|
|
receiving_pubkey = receiving_privkey.get_pubkey()
|
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Encryption by an unnamed data source
|
2018-03-13 16:53:12 +00:00
|
|
|
|
plain_data = b'peace at dawn'
|
2018-06-01 10:30:19 +00:00
|
|
|
|
ciphertext, capsule = pre.encrypt(delegating_pubkey, plain_data)
|
2018-05-30 05:28:14 +00:00
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Decryption by Alice
|
2018-06-01 10:30:19 +00:00
|
|
|
|
cleartext = pre.decrypt(ciphertext, capsule, delegating_privkey)
|
2018-02-23 06:24:09 +00:00
|
|
|
|
assert cleartext == plain_data
|
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Split Re-Encryption Key Generation (aka Delegation)
|
2018-10-04 09:30:47 +00:00
|
|
|
|
kfrags = pre.generate_kfrags(delegating_privkey, receiving_pubkey, M, N, signer)
|
2018-08-09 12:09:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Capsule preparation (necessary before re-encryotion and activation)
|
2018-05-31 10:46:43 +00:00
|
|
|
|
capsule.set_correctness_keys(delegating=delegating_pubkey,
|
|
|
|
|
receiving=receiving_pubkey,
|
|
|
|
|
verifying=signing_pubkey)
|
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Bob requests re-encryption to some set of M ursulas
|
|
|
|
|
cfrags = list()
|
|
|
|
|
for kfrag in kfrags[:M]:
|
2018-08-16 04:15:58 +00:00
|
|
|
|
# Ursula checks that the received kfrag is valid
|
2018-09-16 19:59:22 +00:00
|
|
|
|
assert kfrag.verify(signing_pubkey, delegating_pubkey, receiving_pubkey, params)
|
2018-08-16 04:15:58 +00:00
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Re-encryption by an Ursula
|
2018-06-01 10:30:19 +00:00
|
|
|
|
cfrag = pre.reencrypt(kfrag, capsule)
|
2018-08-09 12:09:21 +00:00
|
|
|
|
|
|
|
|
|
# Bob collects the result
|
|
|
|
|
cfrags.append(cfrag)
|
|
|
|
|
|
|
|
|
|
# Capsule activation (by Bob)
|
|
|
|
|
for cfrag in cfrags:
|
2018-05-31 01:28:54 +00:00
|
|
|
|
capsule.attach_cfrag(cfrag)
|
2018-02-23 06:24:09 +00:00
|
|
|
|
|
2018-08-09 12:09:21 +00:00
|
|
|
|
# Decryption by Bob
|
2018-06-04 12:37:32 +00:00
|
|
|
|
reenc_cleartext = pre.decrypt(ciphertext, capsule, receiving_privkey)
|
2018-02-23 06:24:09 +00:00
|
|
|
|
assert reenc_cleartext == plain_data
|
|
|
|
|
|
|
|
|
|
|
2018-07-30 08:26:19 +00:00
|
|
|
|
@pytest.mark.parametrize("curve", other_supported_curves)
|
2018-02-23 06:24:09 +00:00
|
|
|
|
@pytest.mark.parametrize("N, M", parameters)
|
|
|
|
|
def test_simple_api_on_multiple_curves(N, M, curve):
|
2018-05-31 12:33:11 +00:00
|
|
|
|
test_simple_api(N, M, curve)
|
2018-10-02 15:33:35 +00:00
|
|
|
|
|