def test_blockchain_ursulas_reencrypt(blockchain_ursulas, blockchain_alice, blockchain_bob, policy_value): label = b'bbo' # TODO: Make sample selection buffer configurable - #1061 # Currently, it only supports N<=6, since for N=7, it tries to sample 11 ursulas due to wiggle room, # and blockchain_ursulas only contains 10. # For N >= 7 : NotEnoughBlockchainUrsulas: Cannot create policy with 7 arrangements: There are 10 active stakers, need at least 11. m = n = 6 expiration = maya.now() + datetime.timedelta(days=5) _policy = blockchain_alice.grant(bob=blockchain_bob, label=label, m=m, n=n, expiration=expiration, value=policy_value) enrico = Enrico.from_alice(blockchain_alice, label) message = b"Oh, this isn't even BO. This is beyond BO. It's BBO." message_kit, signature = enrico.encrypt_message(message) blockchain_bob.join_policy(label, bytes(blockchain_alice.stamp)) plaintext = blockchain_bob.retrieve(message_kit, enrico, blockchain_alice.stamp, label) assert plaintext[0] == message
def enrico_control_from_alice(federated_alice, random_policy_label): enrico = Enrico.from_alice(federated_alice, random_policy_label) enrico_control = enrico.make_wsgi_app() enrico_control.config['DEBUG'] = True enrico_control.config['TESTING'] = True yield enrico_control.test_client()
def test_federated_alice_can_decrypt(federated_alice, federated_bob): """ Test that alice can decrypt data encrypted by an enrico for her own derived policy pubkey. """ # Setup the policy details threshold, shares = 2, 3 policy_end_datetime = maya.now() + datetime.timedelta(days=5) label = b"this_is_the_path_to_which_access_is_being_granted" policy = federated_alice.create_policy( bob=federated_bob, label=label, threshold=threshold, shares=shares, expiration=policy_end_datetime, ) enrico = Enrico.from_alice( federated_alice, policy.label, ) plaintext = b"this is the first thing i'm encrypting ever." # use the enrico to encrypt the message message_kit = enrico.encrypt_message(plaintext) # decrypt the data decrypted_data = federated_alice.decrypt_message_kit( label=policy.label, message_kit=message_kit, ) assert [plaintext] == decrypted_data
def test_blockchain_ursulas_reencrypt(blockchain_ursulas, blockchain_alice, blockchain_bob, policy_value): label = b'bbo' # TODO: Investigate issues with wiggle room and additional ursulas during sampling. See also #1061 and #1090 # 1 <= N <= 4 : OK, although for N=4 it can fail with very small probability (<1%) # M = N = 5: Fails with prob. ~66% --> Cannot create policy with 5 arrangements: Selection failed after 5 attempts # N == 6 : NotEnoughBlockchainUrsulas: Cannot create policy with 6 arrangements: Selection failed after 5 attempts # N >= 7 : NotEnoughBlockchainUrsulas: Cannot create policy with 7 arrangements: Cannot create policy with 7 arrangements: 10 stakers are available, need 11 (for wiggle room) m = n = 3 expiration = maya.now() + datetime.timedelta(days=5) _policy = blockchain_alice.grant(bob=blockchain_bob, label=label, m=m, n=n, expiration=expiration, value=policy_value) enrico = Enrico.from_alice(blockchain_alice, label) message = b"Oh, this isn't even BO. This is beyond BO. It's BBO." message_kit, signature = enrico.encrypt_message(message) blockchain_bob.join_policy(label, bytes(blockchain_alice.stamp)) plaintext = blockchain_bob.retrieve(message_kit, enrico, blockchain_alice.stamp, label) assert plaintext[0] == message
def test_blockchain_ursulas_reencrypt(blockchain_ursulas, blockchain_alice, blockchain_bob, policy_value): label = b'bbo' # TODO: Make sample selection buffer configurable - #1061 m = n = 10 expiration = maya.now() + datetime.timedelta(days=5) _policy = blockchain_alice.grant(bob=blockchain_bob, label=label, m=m, n=n, expiration=expiration, value=policy_value) enrico = Enrico.from_alice(blockchain_alice, label) message = b"Oh, this isn't even BO. This is beyond BO. It's BBO." message_kit, signature = enrico.encrypt_message(message) blockchain_bob.start_learning_loop(now=True) blockchain_bob.join_policy(label, bytes(blockchain_alice.stamp)) plaintext = blockchain_bob.retrieve( message_kit, alice_verifying_key=blockchain_alice.stamp, label=label, enrico=enrico) assert plaintext[0] == message # Let's consider also that a node may be down when granting blockchain_alice.network_middleware = NodeIsDownMiddleware() blockchain_alice.network_middleware.node_is_down(blockchain_ursulas[0]) with pytest.raises(BlockchainPolicy.NotEnoughBlockchainUrsulas): _policy = blockchain_alice.grant(bob=blockchain_bob, label=b'another-label', m=m, n=n, expiration=expiration, value=policy_value)
def test_message_kit(enacted_federated_policy, federated_alice): # Setup enrico = Enrico.from_alice(federated_alice, label=enacted_federated_policy.label) message = 'this is a message' plaintext_bytes = bytes(message, encoding='utf-8') message_kit = enrico.encrypt_message(plaintext=plaintext_bytes) message_kit_bytes = bytes(message_kit) message_kit = MessageKitClass.from_bytes(message_kit_bytes) # Test field = MessageKit() serialized = field._serialize(value=message_kit, attr=None, obj=None) assert serialized == b64encode(bytes(message_kit)).decode() deserialized = field._deserialize(value=serialized, attr=None, data=None) deserialized_plaintext = federated_alice.decrypt_message_kit(enacted_federated_policy.label, deserialized)[0] assert deserialized_plaintext == plaintext_bytes with pytest.raises(InvalidInputData): field._deserialize(value=b"MessageKit", attr=None, data=None)
def test_blockchain_ursulas_reencrypt(blockchain_ursulas, blockchain_alice, blockchain_bob, policy_value): label = b'bbo' # TODO: Make sample selection buffer configurable - #1061 threshold = shares = 10 expiration = maya.now() + datetime.timedelta(days=35) _policy = blockchain_alice.grant(bob=blockchain_bob, label=label, threshold=threshold, shares=shares, expiration=expiration, value=policy_value) enrico = Enrico.from_alice(blockchain_alice, label) message = b"Oh, this isn't even BO. This is beyond BO. It's BBO." message_kit = enrico.encrypt_message(message) blockchain_bob.start_learning_loop(now=True) plaintexts = blockchain_bob.retrieve_and_decrypt( [message_kit], encrypted_treasure_map=_policy.treasure_map, alice_verifying_key=blockchain_alice.stamp.as_umbral_pubkey()) assert plaintexts == [message] # Let's consider also that a node may be down when granting blockchain_alice.network_middleware = NodeIsDownMiddleware() blockchain_alice.network_middleware.node_is_down(blockchain_ursulas[0]) with pytest.raises(BlockchainPolicy.NotEnoughUrsulas): _policy = blockchain_alice.grant(bob=blockchain_bob, label=b'another-label', threshold=threshold, shares=shares, expiration=expiration, value=policy_value)
def test_message_kit_serialization_via_enrico(enacted_federated_policy, federated_alice): # Enrico enrico = Enrico.from_alice(federated_alice, label=enacted_federated_policy.label) # Plaintext message = 'this is a message' plaintext_bytes = bytes(message, encoding='utf-8') # Create message_kit, signature = enrico.encrypt_message(plaintext=plaintext_bytes) # Serialize message_kit_bytes = message_kit.to_bytes() # Deserialize the_same_message_kit = UmbralMessageKit.from_bytes(message_kit_bytes) # Confirm assert message_kit_bytes == the_same_message_kit.to_bytes()
def test_message_kit_serialization_via_enrico(federated_alice): mock_label = b'this is a label' # Enrico enrico = Enrico.from_alice(federated_alice, label=mock_label) # Plaintext message = 'this is a message' plaintext_bytes = bytes(message, encoding='utf-8') # Create message_kit = enrico.encrypt_message(plaintext=plaintext_bytes) # Serialize message_kit_bytes = bytes(message_kit) # Deserialize the_same_message_kit = MessageKit.from_bytes(message_kit_bytes) # Confirm assert message_kit_bytes == bytes(the_same_message_kit)
def test_federated_alice_can_decrypt(federated_alice, federated_bob): """ Test that alice can decrypt data encrypted by an enrico for her own derived policy pubkey. """ # Setup the policy details m, n = 2, 3 policy_end_datetime = maya.now() + datetime.timedelta(days=5) label = b"this_is_the_path_to_which_access_is_being_granted" policy = federated_alice.create_policy( bob=federated_bob, label=label, m=m, n=n, expiration=policy_end_datetime, ) enrico = Enrico.from_alice( federated_alice, policy.label, ) plaintext = b"this is the first thing i'm encrypting ever." # use the enrico to encrypt the message message_kit, signature = enrico.encrypt_message(plaintext) # decrypt the data decrypted_data = federated_alice.verify_from( enrico, message_kit, signature=signature, decrypt=True, label=policy.label ) assert plaintext == decrypted_data
def enrico_control_from_alice(federated_alice, random_policy_label): enrico = Enrico.from_alice(federated_alice, random_policy_label) web_controller = enrico.make_web_controller(crash_on_error=True) yield web_controller._web_app.test_client()
def enrico_rpc_controller_from_alice(federated_alice, random_policy_label): enrico = Enrico.from_alice(federated_alice, random_policy_label) rpc_controller = enrico.make_rpc_controller(crash_on_error=True) yield rpc_controller.test_client()
def enrico_web_controller_from_alice(blockchain_alice, random_policy_label): enrico = Enrico.from_alice(blockchain_alice, random_policy_label) web_controller = enrico.make_web_controller(crash_on_error=True) yield web_controller.test_client()