Finnegan's Wake demo updated:

* No longer uses the DHT for Alice and Bob
* Uses new higher-level methods (and is much shorter).
pull/242/head
jMyles 2018-04-17 01:46:23 -07:00
parent 3f55470870
commit 4e6a0409ce
3 changed files with 58 additions and 31 deletions

View File

@ -8,38 +8,39 @@ import sys
from examples.sandbox_resources import SandboxNetworkyStuff from examples.sandbox_resources import SandboxNetworkyStuff
from nkms.characters import Alice, Bob, Ursula from nkms.characters import Alice, Bob, Ursula
from nkms.crypto.kits import MessageKit from nkms.crypto.api import keccak_digest
from nkms.crypto.powers import SigningPower, EncryptingPower from nkms.data_sources import DataSource
from nkms.network.node import NetworkyStuff from nkms.network.node import NetworkyStuff
from umbral import pre import maya
# Some basic setup.
ALICE = Alice() ALICE = Alice()
BOB = Bob() BOB = Bob()
URSULA = Ursula.from_rest_url(address="https://localhost", port="3550") URSULA = Ursula.from_rest_url(NetworkyStuff(), address="localhost", port=3601)
network_middleware = SandboxNetworkyStuff([URSULA])
networky_stuff = SandboxNetworkyStuff() # Here are our Policy details.
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
policy_end_datetime = datetime.datetime.now() + datetime.timedelta(days=5) m = 1
n = 1 n = 1
uri = b"secret/files/and/stuff" label = b"secret/files/and/stuff"
# Alice gets on the network and discovers Ursula, presumably from the blockchain.
ALICE.learn_about_nodes(address="https://localhost", port="3550") # Alice gets on the network and, knowing about at least one Ursula,
# Is able to discover all Ursulas.
ALICE.network_bootstrap([("localhost", 3601)])
# Alice grants to Bob. # Alice grants to Bob.
policy = ALICE.grant(BOB, uri, networky_stuff, m=1, n=n, policy = ALICE.grant(BOB, label, network_middleware, m=m, n=n,
expiration=policy_end_datetime) expiration=policy_end_datetime)
policy.publish_treasure_map(networky_stuff, use_dht=False)
hrac, treasure_map = policy.hrac(), policy.treasure_map hrac, treasure_map = policy.hrac(), policy.treasure_map
# Bob learns about Ursula, gets the TreasureMap, and follows it. # Bob can re-assemble the hrac himself with knowledge he already has.
BOB.learn_about_nodes(address="https://localhost", port="3550") hrac = keccak_digest(bytes(ALICE.stamp) + bytes(BOB.stamp) + label)
networky_stuff = NetworkyStuff() BOB.join_policy(ALICE, hrac, node_list=[("localhost", 3601)])
BOB.get_treasure_map(policy, networky_stuff)
BOB.follow_treasure_map(hrac)
# Now, Alice and Bob are ready for some throughput. # Now, Alice and Bob are ready for some throughput.
finnegans_wake = open(sys.argv[1], 'rb') finnegans_wake = open(sys.argv[1], 'rb')
start_time = datetime.datetime.now() start_time = datetime.datetime.now()
@ -55,16 +56,10 @@ for counter, plaintext in enumerate(finnegans_wake):
print("PREs per second: {}".format(counter / seconds)) print("PREs per second: {}".format(counter / seconds))
print("********************************") print("********************************")
ciphertext, capsule = pre.encrypt(ALICE.public_key(EncryptingPower), plaintext) data_source = DataSource(policy_pubkey_enc=policy.public_key())
message_kit, _signature = data_source.encapsulate_single_message(plaintext)
message_kit = MessageKit(ciphertext=ciphertext, capsule=capsule, delivered_cleartext = BOB.retrieve(hrac=hrac, message_kit=message_kit, data_source=data_source)
alice_pubkey=ALICE.public_key(EncryptingPower))
work_orders = BOB.generate_work_orders(hrac, capsule)
print(plaintext)
cfrags = BOB.get_reencrypted_c_frags(networky_stuff, work_orders[bytes(URSULA.stamp)])
capsule.attach_cfrag(cfrags[0])
delivered_cleartext = pre.decrypt(capsule, BOB._crypto_power._power_ups[EncryptingPower].keypair._privkey, ciphertext, ALICE.public_key(EncryptingPower))
assert plaintext == delivered_cleartext assert plaintext == delivered_cleartext
print("Retrieved: {}".format(delivered_cleartext)) print("Retrieved: {}".format(delivered_cleartext))

View File

@ -9,7 +9,7 @@ import os
from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives.asymmetric import ec
from hendrix.deploy.ssl import HendrixDeployTLS from hendrix.deploy.tls import HendrixDeployTLS
from hendrix.facilities.services import ExistingKeyTLSContextFactory from hendrix.facilities.services import ExistingKeyTLSContextFactory
from nkms.characters import Ursula from nkms.characters import Ursula
from OpenSSL.crypto import X509 from OpenSSL.crypto import X509
@ -19,14 +19,14 @@ from nkms.crypto.api import generate_self_signed_certificate
DB_NAME = "non-mining-proxy-node" DB_NAME = "non-mining-proxy-node"
_URSULA = Ursula(dht_port=3501, dht_interface="localhost", db_name=DB_NAME) _URSULA = Ursula(dht_port=3501, rest_port=3601, ip_address="localhost", db_name=DB_NAME)
_URSULA.listen() _URSULA.dht_listen()
CURVE = ec.SECP256R1 CURVE = ec.SECP256R1
cert, private_key = generate_self_signed_certificate(_URSULA.stamp.fingerprint().decode(), CURVE) cert, private_key = generate_self_signed_certificate(_URSULA.stamp.fingerprint().decode(), CURVE)
deployer = HendrixDeployTLS("start", deployer = HendrixDeployTLS("start",
{"wsgi":_URSULA.rest_app, "https_port": 3550}, {"wsgi":_URSULA.rest_app, "https_port": _URSULA.rest_port},
key=private_key, key=private_key,
cert=X509.from_cryptography(cert), cert=X509.from_cryptography(cert),
context_factory=ExistingKeyTLSContextFactory, context_factory=ExistingKeyTLSContextFactory,

View File

@ -0,0 +1,32 @@
import requests
from nkms.characters import Ursula
from nkms.network.node import NetworkyStuff
from nkms.crypto.powers import SigningPower, EncryptingPower
class SandboxNetworkyStuff(NetworkyStuff):
def __init__(self, ursulas):
self.ursulas = ursulas
def find_ursula(self, contract=None):
ursula = Ursula.as_discovered_on_network(dht_port=None,
ip_address="localhost",
rest_port=3601,
powers_and_keys={
SigningPower: self.ursulas[0].stamp.as_umbral_pubkey(),
EncryptingPower: self.ursulas[0].public_key(EncryptingPower)
}
)
response = requests.post("https://localhost:3601/consider_arrangement", bytes(contract), verify=False)
if response.status_code == 200:
response.was_accepted = True
else:
raise RuntimeError("Something went terribly wrong. What'd you do?!")
return ursula, response
def enact_policy(self, ursula, hrac, payload):
endpoint = 'https://{}:{}/kFrag/{}'.format(ursula.ip_address, ursula.rest_port, hrac.hex())
response = requests.post(endpoint, payload, verify=False)
# TODO: Something useful here and it's probably ready to go down into NetworkyStuff.
return response.status_code == 200