2018-11-04 19:23:11 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
2019-03-05 02:50:11 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2018-11-04 19:23:11 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher 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
|
2019-03-05 02:50:11 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2018-11-04 19:23:11 +00:00
|
|
|
|
2019-03-05 02:50:11 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2018-11-04 19:23:11 +00:00
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2018-08-11 22:56:27 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
|
|
|
|
import pytest
|
2019-02-13 20:14:25 +00:00
|
|
|
from constant_sorrow import constants
|
2019-05-31 15:25:30 +00:00
|
|
|
from cryptography.exceptions import InvalidSignature
|
2019-07-02 05:36:02 +00:00
|
|
|
from eth_account._utils.transactions import Transaction
|
|
|
|
from eth_utils import to_checksum_address
|
2019-05-31 15:25:30 +00:00
|
|
|
|
2018-09-12 11:58:13 +00:00
|
|
|
from nucypher.characters.lawful import Alice, Character, Bob
|
2019-02-13 20:14:25 +00:00
|
|
|
from nucypher.characters.lawful import Enrico
|
2018-05-08 19:35:34 +00:00
|
|
|
from nucypher.crypto import api
|
2019-05-30 17:36:00 +00:00
|
|
|
from nucypher.crypto.api import verify_eip_191
|
2019-05-16 09:12:57 +00:00
|
|
|
from nucypher.crypto.powers import (CryptoPower,
|
|
|
|
SigningPower,
|
|
|
|
NoSigningPower,
|
2019-07-02 05:36:02 +00:00
|
|
|
TransactingPower)
|
2019-06-20 22:18:08 +00:00
|
|
|
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD
|
2019-05-30 17:36:00 +00:00
|
|
|
|
2017-11-09 21:48:40 +00:00
|
|
|
"""
|
|
|
|
Chapter 1: SIGNING
|
|
|
|
"""
|
|
|
|
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
def test_actor_without_signing_power_cannot_sign():
|
|
|
|
"""
|
|
|
|
We can create a Character with no real CryptoPower to speak of.
|
|
|
|
This Character can't even sign a message.
|
|
|
|
"""
|
|
|
|
cannot_sign = CryptoPower(power_ups=[])
|
2018-06-30 03:25:36 +00:00
|
|
|
non_signer = Character(crypto_power=cannot_sign,
|
2018-09-22 22:43:35 +00:00
|
|
|
start_learning_now=False,
|
2018-06-30 03:25:36 +00:00
|
|
|
federated_only=True)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
2018-02-24 06:39:10 +00:00
|
|
|
# The non-signer's stamp doesn't work for signing...
|
2019-02-14 08:23:48 +00:00
|
|
|
with pytest.raises(NoSigningPower):
|
2018-02-24 06:39:10 +00:00
|
|
|
non_signer.stamp("something")
|
2017-10-07 02:29:07 +00:00
|
|
|
|
2018-04-12 08:37:02 +00:00
|
|
|
# ...or as a way to cast the (non-existent) public key to bytes.
|
2019-02-14 08:23:48 +00:00
|
|
|
with pytest.raises(NoSigningPower):
|
2018-02-24 06:39:10 +00:00
|
|
|
bytes(non_signer.stamp)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_actor_with_signing_power_can_sign():
|
|
|
|
"""
|
|
|
|
However, simply giving that character a PowerUp bestows the power to sign.
|
|
|
|
|
|
|
|
Instead of having a Character verify the signature, we'll use the lower level API.
|
|
|
|
"""
|
|
|
|
message = b"Llamas."
|
|
|
|
|
2018-06-30 03:25:36 +00:00
|
|
|
signer = Character(crypto_power_ups=[SigningPower], is_me=True,
|
2018-09-22 22:43:35 +00:00
|
|
|
start_learning_now=False, federated_only=True)
|
2018-02-24 06:39:10 +00:00
|
|
|
stamp_of_the_signer = signer.stamp
|
2017-10-07 02:29:07 +00:00
|
|
|
|
2018-02-24 06:39:10 +00:00
|
|
|
# We can use the signer's stamp to sign a message (since the signer is_me)...
|
|
|
|
signature = stamp_of_the_signer(message)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
# ...or to get the signer's public key for verification purposes.
|
2018-02-13 23:45:02 +00:00
|
|
|
# (note: we use the private _der_encoded_bytes here to test directly against the API, instead of Character)
|
2019-06-04 18:13:29 +00:00
|
|
|
verification = api.verify_ecdsa(message, signature._der_encoded_bytes(),
|
2018-02-24 06:39:10 +00:00
|
|
|
stamp_of_the_signer.as_umbral_pubkey())
|
2017-10-07 02:29:07 +00:00
|
|
|
|
2017-10-07 02:46:51 +00:00
|
|
|
assert verification is True
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
|
2018-06-30 03:25:36 +00:00
|
|
|
def test_anybody_can_verify():
|
2017-10-07 02:29:07 +00:00
|
|
|
"""
|
|
|
|
In the last example, we used the lower-level Crypto API to verify the signature.
|
|
|
|
|
|
|
|
Here, we show that anybody can do it without needing to directly access Crypto.
|
|
|
|
"""
|
|
|
|
# Alice can sign by default, by dint of her _default_crypto_powerups.
|
2018-09-22 22:43:35 +00:00
|
|
|
alice = Alice(federated_only=True, start_learning_now=False)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
# So, our story is fairly simple: an everyman meets Alice.
|
2018-09-22 22:43:35 +00:00
|
|
|
somebody = Character(start_learning_now=False, federated_only=True)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
# Alice signs a message.
|
|
|
|
message = b"A message for all my friends who can only verify and not sign."
|
2018-02-24 06:39:10 +00:00
|
|
|
signature = alice.stamp(message)
|
2017-10-07 02:29:07 +00:00
|
|
|
|
|
|
|
# Our everyman can verify it.
|
2018-07-16 18:58:24 +00:00
|
|
|
cleartext = somebody.verify_from(alice, message, signature, decrypt=False)
|
2018-04-02 03:17:47 +00:00
|
|
|
assert cleartext is constants.NO_DECRYPTION_PERFORMED
|
2017-10-07 02:29:07 +00:00
|
|
|
|
2019-05-16 09:12:57 +00:00
|
|
|
# Of course, verification fails with any fake message
|
|
|
|
with pytest.raises(InvalidSignature):
|
|
|
|
fake = b"McLovin 892 Momona St. Honolulu, HI 96820"
|
|
|
|
_ = somebody.verify_from(alice, fake, signature, decrypt=False)
|
|
|
|
|
|
|
|
# Signature verification also works when Alice is not living with our
|
|
|
|
# everyman in the same process, and he only knows her by her public key
|
|
|
|
alice_pubkey_bytes = bytes(alice.stamp)
|
|
|
|
hearsay_alice = Character.from_public_keys({SigningPower: alice_pubkey_bytes})
|
|
|
|
|
|
|
|
cleartext = somebody.verify_from(hearsay_alice, message, signature, decrypt=False)
|
|
|
|
assert cleartext is constants.NO_DECRYPTION_PERFORMED
|
|
|
|
|
|
|
|
hearsay_alice = Character.from_public_keys(verifying_key=alice_pubkey_bytes)
|
|
|
|
|
|
|
|
cleartext = somebody.verify_from(hearsay_alice, message, signature, decrypt=False)
|
|
|
|
assert cleartext is constants.NO_DECRYPTION_PERFORMED
|
|
|
|
|
2018-05-21 19:40:42 +00:00
|
|
|
|
2019-08-14 02:17:08 +00:00
|
|
|
def test_character_transacting_power_signing(testerchain, agency, test_registry):
|
2018-06-24 21:46:48 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
# Pretend to be a character.
|
|
|
|
eth_address = testerchain.etherbase_account
|
2019-08-14 02:17:08 +00:00
|
|
|
signer = Character(is_me=True, registry=test_registry, checksum_address=eth_address)
|
2019-06-18 18:41:57 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
# Manually consume the power up
|
2019-08-12 07:15:43 +00:00
|
|
|
transacting_power = TransactingPower(password=INSECURE_DEVELOPMENT_PASSWORD,
|
2019-07-02 05:36:02 +00:00
|
|
|
account=eth_address)
|
2019-06-20 22:18:08 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
signer._crypto_power.consume_power_up(transacting_power)
|
2018-06-24 21:46:48 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
# Retrieve the power up
|
|
|
|
power = signer._crypto_power.power_ups(TransactingPower)
|
2018-06-24 21:46:48 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
assert power == transacting_power
|
|
|
|
assert testerchain.transacting_power == power
|
2018-06-24 21:46:48 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
assert power.is_active is True
|
|
|
|
assert power.is_unlocked is True
|
|
|
|
assert testerchain.transacting_power.is_unlocked is True
|
2019-06-18 18:41:57 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
# Sign Message
|
|
|
|
data_to_sign = b'Premium Select Luxury Pencil Holder'
|
|
|
|
signature = power.sign_message(message=data_to_sign)
|
|
|
|
is_verified = verify_eip_191(address=eth_address, message=data_to_sign, signature=signature)
|
|
|
|
assert is_verified is True
|
2018-06-24 21:46:48 +00:00
|
|
|
|
2019-07-02 05:36:02 +00:00
|
|
|
# Sign Transaction
|
|
|
|
transaction_dict = {'nonce': testerchain.client.w3.eth.getTransactionCount(eth_address),
|
|
|
|
'gasPrice': testerchain.client.w3.eth.gasPrice,
|
|
|
|
'gas': 100000,
|
|
|
|
'from': eth_address,
|
|
|
|
'to': testerchain.unassigned_accounts[1],
|
|
|
|
'value': 1,
|
|
|
|
'data': b''}
|
|
|
|
|
|
|
|
signed_transaction = power.sign_transaction(unsigned_transaction=transaction_dict)
|
|
|
|
|
|
|
|
# Demonstrate that the transaction is valid RLP encoded.
|
|
|
|
restored_transaction = Transaction.from_bytes(serialized_bytes=signed_transaction)
|
|
|
|
restored_dict = restored_transaction.as_dict()
|
|
|
|
assert to_checksum_address(restored_dict['to']) == transaction_dict['to']
|
2018-06-24 21:46:48 +00:00
|
|
|
|
|
|
|
|
2017-10-11 05:39:25 +00:00
|
|
|
"""
|
2017-11-09 21:48:40 +00:00
|
|
|
Chapter 2: ENCRYPTION
|
2017-10-11 05:39:25 +00:00
|
|
|
"""
|
|
|
|
|
2018-02-14 08:18:55 +00:00
|
|
|
|
2018-02-14 01:32:15 +00:00
|
|
|
def test_anybody_can_encrypt():
|
2017-10-11 05:39:25 +00:00
|
|
|
"""
|
2018-02-14 01:32:15 +00:00
|
|
|
Similar to anybody_can_verify() above; we show that anybody can encrypt.
|
2017-10-11 05:39:25 +00:00
|
|
|
"""
|
2018-09-22 22:43:35 +00:00
|
|
|
someone = Character(start_learning_now=False, federated_only=True)
|
2018-06-30 03:25:36 +00:00
|
|
|
bob = Bob(is_me=False, federated_only=True)
|
2017-10-11 05:39:25 +00:00
|
|
|
|
|
|
|
cleartext = b"This is Officer Rod Farva. Come in, Ursula! Come in Ursula!"
|
|
|
|
|
2018-06-23 03:28:08 +00:00
|
|
|
ciphertext, signature = someone.encrypt_for(bob, cleartext, sign=False)
|
2018-04-02 01:52:08 +00:00
|
|
|
|
|
|
|
assert signature == constants.NOT_SIGNED
|
2017-11-22 04:22:16 +00:00
|
|
|
assert ciphertext is not None
|
2018-02-14 08:19:09 +00:00
|
|
|
|
2018-05-21 19:40:42 +00:00
|
|
|
|
2018-09-13 19:35:44 +00:00
|
|
|
def test_node_deployer(federated_ursulas):
|
|
|
|
for ursula in federated_ursulas:
|
2018-07-16 21:36:19 +00:00
|
|
|
deployer = ursula.get_deployer()
|
2019-06-18 18:41:57 +00:00
|
|
|
assert deployer.options['https_port'] == ursula.rest_information()[0].port
|
2018-07-16 21:36:19 +00:00
|
|
|
assert deployer.application == ursula.rest_app
|
|
|
|
|
|
|
|
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
2018-07-16 18:58:24 +00:00
|
|
|
What follows are various combinations of signing and encrypting, to match
|
|
|
|
real-world scenarios.
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
|
|
|
|
2018-05-21 19:40:42 +00:00
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
def test_sign_cleartext_and_encrypt(federated_alice, federated_bob):
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
2018-09-24 20:59:38 +00:00
|
|
|
Exhibit One: federated_alice signs the cleartext and encrypts her signature inside
|
2018-07-16 18:58:24 +00:00
|
|
|
the ciphertext.
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
|
|
|
message = b"Have you accepted my answer on StackOverflow yet?"
|
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
message_kit, _signature = federated_alice.encrypt_for(federated_bob, message,
|
|
|
|
sign_plaintext=True)
|
2018-02-14 08:19:09 +00:00
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
# Notice that our function still returns the signature here, in case federated_alice
|
2018-07-16 18:58:24 +00:00
|
|
|
# wants to do something else with it, such as post it publicly for later
|
|
|
|
# public verifiability.
|
2018-02-14 08:19:09 +00:00
|
|
|
|
2018-07-16 18:58:24 +00:00
|
|
|
# However, we can expressly refrain from passing the Signature, and the
|
|
|
|
# verification still works:
|
2018-09-24 20:59:38 +00:00
|
|
|
cleartext = federated_bob.verify_from(federated_alice, message_kit, signature=None,
|
|
|
|
decrypt=True)
|
2018-02-14 08:19:09 +00:00
|
|
|
assert cleartext == message
|
|
|
|
|
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
def test_encrypt_and_sign_the_ciphertext(federated_alice, federated_bob):
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
2018-09-24 20:59:38 +00:00
|
|
|
Now, federated_alice encrypts first and then signs the ciphertext, providing a
|
2018-07-16 18:58:24 +00:00
|
|
|
Signature that is completely separate from the message.
|
2018-09-24 20:59:38 +00:00
|
|
|
This is useful in a scenario in which federated_bob needs to prove authenticity
|
2018-07-16 18:58:24 +00:00
|
|
|
publicly without disclosing contents.
|
2018-02-14 08:19:09 +00:00
|
|
|
"""
|
|
|
|
message = b"We have a reaaall problem."
|
2018-09-24 20:59:38 +00:00
|
|
|
message_kit, signature = federated_alice.encrypt_for(federated_bob, message,
|
|
|
|
sign_plaintext=False)
|
|
|
|
cleartext = federated_bob.verify_from(federated_alice, message_kit, signature, decrypt=True)
|
2018-02-14 08:19:09 +00:00
|
|
|
assert cleartext == message
|
|
|
|
|
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
def test_encrypt_and_sign_including_signature_in_both_places(federated_alice, federated_bob):
|
2018-04-05 17:20:34 +00:00
|
|
|
"""
|
2018-07-16 18:58:24 +00:00
|
|
|
Same as above, but showing that we can include the signature in both
|
|
|
|
the plaintext (to be found upon decryption) and also passed into
|
|
|
|
verify_from() (eg, gleaned over a side-channel).
|
2018-04-05 17:20:34 +00:00
|
|
|
"""
|
|
|
|
message = b"We have a reaaall problem."
|
2018-09-24 20:59:38 +00:00
|
|
|
message_kit, signature = federated_alice.encrypt_for(federated_bob, message,
|
|
|
|
sign_plaintext=True)
|
|
|
|
cleartext = federated_bob.verify_from(federated_alice, message_kit, signature,
|
|
|
|
decrypt=True)
|
2018-04-05 17:20:34 +00:00
|
|
|
assert cleartext == message
|
|
|
|
|
|
|
|
|
2018-09-24 20:59:38 +00:00
|
|
|
def test_encrypt_but_do_not_sign(federated_alice, federated_bob):
|
2018-02-28 03:55:46 +00:00
|
|
|
"""
|
2018-09-24 20:59:38 +00:00
|
|
|
Finally, federated_alice encrypts but declines to sign.
|
|
|
|
This is useful in a scenario in which federated_alice wishes to plausibly disavow
|
2018-07-16 18:58:24 +00:00
|
|
|
having created this content.
|
2018-02-28 03:55:46 +00:00
|
|
|
"""
|
2018-07-16 18:58:24 +00:00
|
|
|
# TODO: How do we accurately demonstrate this test safely, if at all?
|
2018-02-14 08:19:09 +00:00
|
|
|
message = b"If Bonnie comes home and finds an unencrypted private key in her keystore, I'm gonna get divorced."
|
|
|
|
|
2018-07-16 18:58:24 +00:00
|
|
|
# Alice might also want to encrypt a message but *not* sign it, in order
|
|
|
|
# to refrain from creating evidence that can prove she was the
|
|
|
|
# original sender.
|
2018-09-24 20:59:38 +00:00
|
|
|
message_kit, not_signature = federated_alice.encrypt_for(federated_bob, message, sign=False)
|
2018-02-14 08:19:09 +00:00
|
|
|
|
|
|
|
# The message is not signed...
|
2018-04-02 01:52:08 +00:00
|
|
|
assert not_signature == constants.NOT_SIGNED
|
2018-02-14 08:19:09 +00:00
|
|
|
|
|
|
|
# ...and thus, the message is not verified.
|
2019-05-31 15:26:05 +00:00
|
|
|
with pytest.raises(InvalidSignature):
|
2018-09-24 20:59:38 +00:00
|
|
|
federated_bob.verify_from(federated_alice, message_kit, decrypt=True)
|
2018-11-29 11:45:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_alice_can_decrypt(federated_alice):
|
|
|
|
label = b"boring test label"
|
|
|
|
|
2019-06-01 10:32:46 +00:00
|
|
|
policy_pubkey = federated_alice.get_policy_encrypting_key_from_label(label)
|
2018-11-29 11:45:56 +00:00
|
|
|
|
2019-02-15 04:36:09 +00:00
|
|
|
enrico = Enrico(policy_encrypting_key=policy_pubkey)
|
2018-11-29 11:45:56 +00:00
|
|
|
|
|
|
|
message = b"boring test message"
|
2019-02-13 20:14:25 +00:00
|
|
|
message_kit, signature = enrico.encrypt_message(message=message)
|
2018-11-29 11:45:56 +00:00
|
|
|
|
2019-02-15 04:36:09 +00:00
|
|
|
# Interesting thing: if Alice wants to decrypt, she needs to provide the label directly.
|
2019-02-13 20:14:25 +00:00
|
|
|
cleartext = federated_alice.verify_from(stranger=enrico,
|
2018-11-29 11:45:56 +00:00
|
|
|
message_kit=message_kit,
|
|
|
|
signature=signature,
|
2019-02-15 04:36:09 +00:00
|
|
|
decrypt=True,
|
|
|
|
label=label)
|
2018-11-29 11:45:56 +00:00
|
|
|
assert cleartext == message
|