def setup_test(window=1, packet_size=1000, packets=10000): """ Create two nodes who will be sending packets to each other. :param window: the window size for packets (1 is synchronous) :param packet_size: the size of each packet :param packets: the number of packets to send in the experiment :return: the deferred that fires once the experiment is complete """ configuration = get_default_configuration() configuration['overlays'] = [] configuration['keys'] = [] master_peer = Peer(ECCrypto().generate_key(u"low")) peer_ipv8 = IPv8(configuration) peer_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))} peer_ipv8.overlays = [ LoadOverlay(master_peer, peer_ipv8.keys['my_peer'], peer_ipv8.endpoint, peer_ipv8.network, window, packet_size, packets) ] counterparty_ipv8 = IPv8(configuration) counterparty_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))} counterparty_ipv8.overlays = [ LoadOverlay(master_peer, counterparty_ipv8.keys['my_peer'], counterparty_ipv8.endpoint, counterparty_ipv8.network, 0, packet_size, packets) ] peer_ipv8.overlays[0].send(counterparty_ipv8.endpoint.get_address()) return DeferredList( [peer_ipv8.overlays[0].done, counterparty_ipv8.overlays[0].done])
def connect_peer(self, mid): matched_node = MockObject() matched_node.mid = mid matched_node.public_key = ECCrypto().generate_key("low").pub() nearby_node = MockObject() nearby_node.mid = unhexlify('b' * 20) nearby_node.public_key = ECCrypto().generate_key("low").pub() return succeed([matched_node, nearby_node])
class EndpointServer(DiscoveryCommunity): """ Make some small modifications to the Community to allow it a dynamic prefix. We will also only answer introduction requests. """ def __init__(self, endpoint): my_peer = Peer(ECCrypto().generate_key(u"very-low")) super(EndpointServer, self).__init__(my_peer, endpoint, Network()) self.churn_lc = self.register_task("churn", LoopingCall(self.do_churn)).start(5.0, now=False) self.churn_strategy = RandomChurn(self) self.crypto = ECCrypto() def on_packet(self, packet, warn_unknown=False): source_address, data = packet try: probable_peer = self.network.get_verified_by_address(source_address) if probable_peer: probable_peer.last_response = time.time() if data[22] == chr(246): self.on_generic_introduction_request(source_address, data, data[:22]) elif warn_unknown: self.logger.warning("Tracker received unknown message %s", str(data[22])) except: import traceback traceback.print_exc() def on_generic_introduction_request(self, source_address, data, prefix): auth, dist, payload = self._ez_unpack_auth(IntroductionRequestPayload, data) peer = Peer(auth.public_key_bin, source_address) self.network.add_verified_peer(peer) self.network.discover_services(peer, [prefix[2:], ]) intro_peers = [p for p in self.network.get_peers_for_service(prefix[2:]) if not(p == peer)] if intro_peers: intro_peer = random.choice(intro_peers) packet = self.create_introduction_response(payload.destination_address, peer.address, payload.identifier, introduction=intro_peer) signature_length = self.crypto.get_signature_length(self.my_peer.public_key) packet = prefix + packet[22:-signature_length] signature = self.crypto.create_signature(self.my_peer.key, packet) self.endpoint.send(peer.address, packet + signature) def do_churn(self): """ Dynamically scale churn to check one fifth of our network every step. """ self.churn_strategy.sample_size = min(len(self.network.verified_peers)//5, 1) self.churn_strategy.take_step()
def bootstrap_new_identity(self, amount): """ One-way payment channel. Create a new temporary identity, and transfer funds to the new identity. A different party can then take the result and do a transfer from the temporary identity to itself """ # Create new identity for the temporary identity crypto = ECCrypto() tmp_peer = Peer(crypto.generate_key(u"curve25519")) # Create the transaction specification transaction = {'up': 0, 'down': amount, 'type': b'tribler_bandwidth'} # Create the two half blocks that form the transaction local_half_block = TriblerBandwidthBlock.create( b'tribler_bandwidth', transaction, self.trustchain.persistence, self.trustchain.my_peer.public_key.key_to_bin(), link_pk=tmp_peer.public_key.key_to_bin()) local_half_block.sign(self.trustchain.my_peer.key) tmp_half_block = TriblerBandwidthBlock.create( b'tribler_bandwidth', transaction, self.trustchain.persistence, tmp_peer.public_key.key_to_bin(), link=local_half_block, link_pk=self.trustchain.my_peer.public_key.key_to_bin()) tmp_half_block.sign(tmp_peer.key) self.trustchain.persistence.add_block(local_half_block) self.trustchain.persistence.add_block(tmp_half_block) # Create the bootstrapped identity format block = { 'block_hash': b64encode(tmp_half_block.hash), 'sequence_number': tmp_half_block.sequence_number } result = { 'private_key': b64encode(tmp_peer.key.key_to_bin()), 'transaction': { 'up': amount, 'down': 0 }, 'block': block } return result
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def started(self): async def check_peers(): global INSTANCES, LOW_EDGE, LOW_EDGE_PEER, START_TIME if self.get_peers(): if LOW_EDGE and self.my_peer != LOW_EDGE_PEER: print("%.4f,%.4f" % (LOW_EDGE, time.time() - START_TIME)) async def shutdown(): for instance in INSTANCES: await instance.stop(False) get_event_loop().stop() ensure_future(shutdown()) else: LOW_EDGE = time.time() - START_TIME LOW_EDGE_PEER = self.my_peer self.register_task("check_peers", check_peers, interval=0.1, delay=0) def create_introduction_response(self, lan_socket_address, socket_address, identifier, introduction=None): return super(MyCommunity, self).create_introduction_response( lan_socket_address, socket_address, identifier, introduction, b'1') def create_introduction_request(self, socket_address): return super(MyCommunity, self).create_introduction_request(socket_address, b'2')
def test_peer_inequality_key(self): """ Check if peers with a different key and same address are not equal. """ other = Peer(ECCrypto().generate_key(u"very-low"), self.peer.address) self.assertNotEqual(self.peer, other)
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def started(self): def check_peers(): global INSTANCES, LOW_EDGE, LOW_EDGE_PEER, START_TIME if self.get_peers(): if LOW_EDGE and self.my_peer != LOW_EDGE_PEER: for instance in INSTANCES: instance.stop(False) reactor.callFromThread(reactor.stop) print("%.4f,%.4f" % (LOW_EDGE, time.time() - START_TIME)) else: LOW_EDGE = time.time() - START_TIME LOW_EDGE_PEER = self.my_peer self.register_task("check_peers", LoopingCall(check_peers)).start(0.1, True) def create_introduction_response(self, lan_socket_address, socket_address, identifier, introduction=None): return super(MyCommunity, self).create_introduction_response( lan_socket_address, socket_address, identifier, introduction, b'1') def create_introduction_request(self, socket_address): return super(MyCommunity, self).create_introduction_request(socket_address, b'2')
def __init__(self, crypto_curve, overlay_class, *args, **kwargs): self.endpoint = AutoMockEndpoint() self.endpoint.open() self.network = Network() self.my_peer = Peer(ECCrypto().generate_key(crypto_curve), self.endpoint.wan_address) self.overlay = overlay_class(self.my_peer, self.endpoint, self.network, *args, **kwargs) self.discovery = MockWalk(self.overlay) self.overlay.my_estimated_wan = self.endpoint.wan_address self.overlay.my_estimated_lan = self.endpoint.lan_address
async def setUp(self): """ Setup various variables and load the tunnel community in the main downloader session. """ await TestAsServer.setUp(self) self.seed_tdef = None self.sessions = [] self.session2 = None self.bypass_dht = False self.seed_config = None self.tunnel_community_seeder = None self.eccrypto = ECCrypto() ec = self.eccrypto.generate_key(u"curve25519") self.test_class = TriblerTunnelCommunity self.test_class.master_peer = Peer(ec) self.tunnel_community = await self.load_tunnel_community_in_session( self.session, exitnode=True, start_lt=True) self.session.tunnel_community = self.tunnel_community # Magic! self.tunnel_communities = []
async def load_tunnel_community_in_session(self, session, exitnode=False, start_lt=False): """ Load the tunnel community in a given session. We are using our own tunnel community here instead of the one used in Tribler. """ await self.sanitize_network(session) keypair = ECCrypto().generate_key(u"curve25519") tunnel_peer = Peer(keypair) session.config.set_tunnel_community_exitnode_enabled(exitnode) overlay = self.test_class(tunnel_peer, session.ipv8.endpoint, session.ipv8.network, tribler_session=session, settings={"max_circuits": 1}) if exitnode: overlay.settings.peer_flags.add(PEER_FLAG_EXIT_ANY) overlay._use_main_thread = False overlay.dht_provider = MockDHTProvider( Peer(overlay.my_peer.key, overlay.my_estimated_wan)) overlay.settings.remove_tunnel_delay = 0 session.ipv8.overlays.append(overlay) await overlay.wait_for_socks_servers() if start_lt: # If libtorrent tries to connect to the socks5 servers before they are loaded, # it will never recover (on Mac/Linux with Libtorrent >=1.2.0). Therefore, we start # libtorrent afterwards. tunnel_community_ports = session.config.get_tunnel_community_socks5_listen_ports( ) session.config.set_anon_proxy_settings( 2, ("127.0.0.1", tunnel_community_ports)) session.dlmgr = DownloadManager(session) session.dlmgr.initialize() session.dlmgr.is_shutdown_ready = lambda: True return overlay
async def start_communities(): # Override the Community master peers so we don't interfere with the live network # Also hook in our custom logic for introduction responses for community_cls in _COMMUNITIES.values(): community_cls.master_peer = Peer(ECCrypto().generate_key(u"medium")) community_cls.introduction_response_callback = custom_intro_response_cb # Create two peers with separate working directories previous_workdir = getcwd() for i in [1, 2]: configuration = get_default_configuration() configuration['port'] = 12000 + randint(0, 10000) configuration['logger']['level'] = "CRITICAL" for overlay in configuration['overlays']: overlay['walkers'] = [walker for walker in overlay['walkers'] if walker['strategy'] in _WALKERS] workdir = path.abspath(path.join(path.dirname(__file__), str(i))) if not path.exists(workdir): mkdir(workdir) chdir(workdir) await IPv8(configuration).start() chdir(previous_workdir)
def setUp(self): self.ecc = ECCrypto()
class TestSerialization(unittest.TestCase): """ Test whether keys can be serialized and unserialized correctly. """ def setUp(self): self.ec = ECCrypto() self.key = self.ec.generate_key(u"very-low") self.key_nacl = self.ec.generate_key(u"curve25519") def test_private_to_bin(self): """ Check if M2Crypto derived key bins are valid. """ private_bin = self.key.key_to_bin() self.assertTrue(self.ec.is_valid_private_bin(private_bin)) def test_private_nacl_to_bin(self): """ Check if libnacl derived key bins are valid. """ private_bin = self.key_nacl.key_to_bin() self.assertTrue(self.ec.is_valid_private_bin(private_bin)) def test_private_to_pem(self): """ Check if keys can be serialized and loaded correctly in PEM format. """ private_pem = self.key.key_to_pem() # Convert the PEM to a DER keystring prefix = "-----BEGIN EC PRIVATE KEY-----\n" postfix = "-----END EC PRIVATE KEY-----\n" keystring = private_pem[len(prefix):-len(postfix)].decode("BASE64") # Reconstruct a key with this keystring key = M2CryptoSK(keystring=keystring) self.assertEqual(private_pem, key.key_to_pem()) def test_public_to_bin(self): """ Check if M2Crypto derived public key bins are valid. """ public_bin = self.key.pub().key_to_bin() self.assertTrue(self.ec.is_valid_public_bin(public_bin)) def test_public_nacl_to_bin(self): """ Check if libnacl derived public key bins are valid. """ public_bin = self.key_nacl.pub().key_to_bin() self.assertTrue(self.ec.is_valid_public_bin(public_bin)) def test_public_to_pem(self): """ Check if public keys can be serialized and loaded correctly in PEM format. """ public_pem = self.key.pub().key_to_pem() # Convert the PEM to a DER keystring prefix = "-----BEGIN PUBLIC KEY-----\n" postfix = "-----END PUBLIC KEY-----\n" keystring = public_pem[len(prefix):-len(postfix)].decode("BASE64") # Reconstruct a key with this keystring key = M2CryptoPK(keystring=keystring) self.assertEqual(public_pem, key.key_to_pem())
def setUp(self): self.ec = ECCrypto() self.key = self.ec.generate_key(u"very-low") self.key_nacl = self.ec.generate_key(u"curve25519")
def __init__(self): endpoint = AutoMockEndpoint() endpoint.open() network = Network() peer = Peer(ECCrypto().generate_key(u"very-low"), endpoint.wan_address) super(MockCommunity, self).__init__(peer, endpoint, network)
# If it takes longer than 30 seconds to find anything, abort the experiment and set the intro time to -1.0 def on_timeout(): for definition in get_default_configuration()['overlays']: if definition['class'] not in RESULTS: RESULTS[definition['class']] = -1.0 print(definition['class'], "found no peers at all!", file=sys.stderr) reactor.callFromThread(reactor.stop) # Override the Community master peers so we don't interfere with the live network # Also hook in our custom logic for introduction responses for community_cls in _COMMUNITIES.values(): community_cls.master_peer = Peer(ECCrypto().generate_key(u"medium")) community_cls.introduction_response_callback = custom_intro_response_cb # Create two peers with separate working directories previous_workdir = getcwd() for i in [1, 2]: configuration = get_default_configuration() configuration['port'] = 12000 + randint(0, 10000) configuration['logger']['level'] = "CRITICAL" for overlay in configuration['overlays']: overlay['walkers'] = [ walker for walker in overlay['walkers'] if walker['strategy'] in _WALKERS ] workdir = path.abspath(path.join(path.dirname(__file__), "%d" % i)) if not path.exists(workdir):
class TestTunnelBase(TestAsServer): async def setUp(self): """ Setup various variables and load the tunnel community in the main downloader session. """ await TestAsServer.setUp(self) self.seed_tdef = None self.sessions = [] self.session2 = None self.bypass_dht = False self.seed_config = None self.tunnel_community_seeder = None self.eccrypto = ECCrypto() ec = self.eccrypto.generate_key(u"curve25519") self.test_class = TriblerTunnelCommunity self.test_class.master_peer = Peer(ec) self.tunnel_community = await self.load_tunnel_community_in_session( self.session, exitnode=True, start_lt=True) self.session.tunnel_community = self.tunnel_community # Magic! self.tunnel_communities = [] def setUpPreSession(self): TestAsServer.setUpPreSession(self) self.config.set_ipv8_enabled(True) self.config.set_ipv8_port(-1) self.config.set_libtorrent_enabled(False) self.config.set_trustchain_enabled(False) self.config.set_tunnel_community_socks5_listen_ports(self.get_ports(5)) async def tearDown(self): if self.session2: await self.session2.shutdown() await gather(*[s.shutdown() for s in self.sessions]) await gather(*[tc.unload() for tc in self.tunnel_communities]) if self.tunnel_community_seeder: await self.tunnel_community_seeder.unload() await TestAsServer.tearDown(self) async def setup_nodes(self, num_relays=1, num_exitnodes=1, seed_hops=0): """ Setup all required nodes, including the relays, exit nodes and seeder. """ baseindex = 3 for i in range(baseindex, baseindex + num_relays): # Normal relays proxy = await self.create_proxy(i) self.tunnel_communities.append(proxy) baseindex += num_relays + 1 for i in range(baseindex, baseindex + num_exitnodes): # Exit nodes proxy = await self.create_proxy(i, exitnode=True) self.tunnel_communities.append(proxy) # Setup the seeder session await self.setup_tunnel_seeder(seed_hops) # Add the tunnel community of the downloader session self.tunnel_communities.append(self.tunnel_community) self._logger.info("Introducing all nodes to each other in tests") other_tunnel_communities = [self.tunnel_community_seeder ] if self.tunnel_community_seeder else [] for community_introduce in self.tunnel_communities + other_tunnel_communities: for community in self.tunnel_communities + other_tunnel_communities: if community != community_introduce: community.walk_to( ('127.0.0.1', community_introduce.endpoint.get_address()[1])) await self.deliver_messages() async def sanitize_network(self, session): # We disable the discovery communities in this session since we don't want to walk to the live network for overlay in session.ipv8.overlays: if isinstance(overlay, DiscoveryCommunity): await overlay.unload() session.ipv8.overlays = [] session.ipv8.strategies = [] # Also reset the IPv8 network session.ipv8.network = Network() async def load_tunnel_community_in_session(self, session, exitnode=False, start_lt=False): """ Load the tunnel community in a given session. We are using our own tunnel community here instead of the one used in Tribler. """ await self.sanitize_network(session) keypair = ECCrypto().generate_key(u"curve25519") tunnel_peer = Peer(keypair) session.config.set_tunnel_community_exitnode_enabled(exitnode) overlay = self.test_class(tunnel_peer, session.ipv8.endpoint, session.ipv8.network, tribler_session=session, settings={"max_circuits": 1}) if exitnode: overlay.settings.peer_flags.add(PEER_FLAG_EXIT_ANY) overlay._use_main_thread = False overlay.dht_provider = MockDHTProvider( Peer(overlay.my_peer.key, overlay.my_estimated_wan)) overlay.settings.remove_tunnel_delay = 0 session.ipv8.overlays.append(overlay) await overlay.wait_for_socks_servers() if start_lt: # If libtorrent tries to connect to the socks5 servers before they are loaded, # it will never recover (on Mac/Linux with Libtorrent >=1.2.0). Therefore, we start # libtorrent afterwards. tunnel_community_ports = session.config.get_tunnel_community_socks5_listen_ports( ) session.config.set_anon_proxy_settings( 2, ("127.0.0.1", tunnel_community_ports)) session.dlmgr = DownloadManager(session) session.dlmgr.initialize() session.dlmgr.is_shutdown_ready = lambda: True return overlay async def create_proxy(self, index, exitnode=False): """ Create a single proxy and load the tunnel community in the session of that proxy. """ from tribler_core.session import Session self.setUpPreSession() config = self.config.copy() config.set_libtorrent_enabled(False) config.set_tunnel_community_socks5_listen_ports(self.get_ports(5)) session = Session(config) session.upgrader_enabled = False await session.start() self.sessions.append(session) return await self.load_tunnel_community_in_session(session, exitnode=exitnode) async def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from tribler_core.session import Session self.seed_config = self.config.copy() self.seed_config._state_dir = self.getRootStateDir(2) self.seed_config.set_libtorrent_enabled(hops == 0) self.seed_config.set_tunnel_community_socks5_listen_ports( self.get_ports(5)) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.upgrader_enabled = False await self.session2.start() tdef = TorrentDef() tdef.add_content(TESTS_DATA_DIR / "video.avi") tdef.set_tracker("http://localhost/announce") torrentfn = self.session2.config.get_state_dir() / "gen.torrent" tdef.save(torrent_filepath=torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = await self.load_tunnel_community_in_session( self.session2, start_lt=True) self.tunnel_community_seeder.build_tunnels(hops) else: await self.sanitize_network(self.session2) dscfg = DownloadConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.dlmgr.start_download(tdef=tdef, config=dscfg) d.set_state_callback(self.seeder_state_callback) def seeder_state_callback(self, ds): """ The callback of the seeder download. For now, this only logs the state of the download that's seeder and is useful for debugging purposes. """ if self.tunnel_community_seeder: self.tunnel_community_seeder.monitor_downloads([ds]) d = ds.get_download() self._logger.debug("seeder: %s %s %s", repr(d.get_def().get_name()), dlstatus_strings[ds.get_status()], ds.get_progress()) return 5.0 def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ self.session.config.set_libtorrent_dht_readiness_timeout(0) dscfg = DownloadConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.dlmgr.start_download(tdef=self.seed_tdef, config=dscfg) self.tunnel_community.bittorrent_peers[download] = [ ("127.0.0.1", self.session2.config.get_libtorrent_port()) ] return download async def deliver_messages(self, timeout=.1): """ Allow peers to communicate. The strategy is as follows: 1. Measure the amount of tasks 2. After 10 milliseconds, check if we are below 2 twice in a row 3. If not, go back to handling calls (step 2) or return, if the timeout has been reached :param timeout: the maximum time to wait for messages to be delivered """ rtime = 0 probable_exit = False while rtime < timeout: await sleep(.01) rtime += .01 if len(all_tasks()) < 2: if probable_exit: break probable_exit = True else: probable_exit = False
class TestECCrypto(unittest.TestCase): m2crypto_key = ECCrypto().generate_key(u"very-low") libnacl_key = ECCrypto().generate_key(u"curve25519") def setUp(self): self.ecc = ECCrypto() def test_available(self): """ Check if the required curves are available. """ available = self.ecc.security_levels self.assertIn(u"very-low", available) self.assertIn(u"low", available) self.assertIn(u"medium", available) self.assertIn(u"high", available) self.assertIn(u"curve25519", available) def test_generate_m2crypto(self): """ Check if M2Crypto backend keys can be generated correctly. """ self.assertIsInstance(TestECCrypto.m2crypto_key, Key) self.assertIsInstance(TestECCrypto.m2crypto_key, PrivateKey) self.assertIsInstance(TestECCrypto.m2crypto_key, PublicKey) self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoSK) self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoPK) def test_generate_nacl(self): """ Check if libnacl backend keys can be generated correctly. """ self.assertIsInstance(TestECCrypto.libnacl_key, Key) self.assertIsInstance(TestECCrypto.libnacl_key, PrivateKey) self.assertIsInstance(TestECCrypto.libnacl_key, PublicKey) self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLSK) self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLPK) def test_generate_bogus(self): """ Check if a bogus curve produces a RuntimeError """ self.assertRaises(RuntimeError, self.ecc.generate_key, u"idontexist") def test_key_to_bin_m2crypto(self): """ Check if ECCrypto correctly detects an M2Crypto key for bin. """ key_bin = self.ecc.key_to_bin(TestECCrypto.m2crypto_key) self.assertEqual(key_bin, TestECCrypto.m2crypto_key.key_to_bin()) def test_key_to_bin_nacl(self): """ Check if ECCrypto correctly detects an libnacl key for bin. """ key_bin = self.ecc.key_to_bin(TestECCrypto.libnacl_key) self.assertEqual(key_bin, TestECCrypto.libnacl_key.key_to_bin()) def test_key_to_hash_m2crypto(self): """ Check if ECCrypto correctly detects an M2Crypto key for hash. """ key_hash = self.ecc.key_to_hash(TestECCrypto.m2crypto_key) self.assertEqual(key_hash, TestECCrypto.m2crypto_key.key_to_hash()) def test_key_to_hash_nacl(self): """ Check if ECCrypto correctly detects an libnacl key for hash. """ key_hash = self.ecc.key_to_hash(TestECCrypto.libnacl_key) self.assertEqual(key_hash, TestECCrypto.libnacl_key.key_to_hash()) def test_is_valid_private_bin_m2crypto(self): """ Check if ECCrypto can detect a valid M2Crypto private key. """ self.assertTrue( self.ecc.is_valid_private_bin( TestECCrypto.m2crypto_key.key_to_bin())) def test_is_valid_private_bin_m2crypto_public(self): """ Check if ECCrypto doesn't detect a valid public M2Crypto key as a private key. """ self.assertFalse( self.ecc.is_valid_private_bin( TestECCrypto.m2crypto_key.pub().key_to_bin())) def test_is_valid_private_bin_nacl(self): """ Check if ECCrypto can detect a valid libnacl private key. """ self.assertTrue( self.ecc.is_valid_private_bin( TestECCrypto.libnacl_key.key_to_bin())) def test_is_valid_private_bin_nacl_public(self): """ Check if ECCrypto doesn't detect a valid public libnacl key as a private key. """ self.assertFalse( self.ecc.is_valid_private_bin( TestECCrypto.libnacl_key.pub().key_to_bin())) def test_is_valid_public_bin_m2crypto(self): """ Check if ECCrypto doesn't detect a valid M2Crypto private key as a public key. """ self.assertFalse( self.ecc.is_valid_public_bin( TestECCrypto.m2crypto_key.key_to_bin())) def test_is_valid_public_bin_m2crypto_public(self): """ Check if ECCrypto detects a valid public M2Crypto key as a public key. """ self.assertTrue( self.ecc.is_valid_public_bin( TestECCrypto.m2crypto_key.pub().key_to_bin())) def test_is_valid_public_bin_nacl(self): """ Check if ECCrypto doesn't detect a valid libnacl private key as a public key. """ self.assertFalse( self.ecc.is_valid_public_bin( TestECCrypto.libnacl_key.key_to_bin())) def test_is_valid_public_bin_nacl_public(self): """ Check if ECCrypto detects a valid public libnacl key as a public key. """ self.assertTrue( self.ecc.is_valid_public_bin( TestECCrypto.libnacl_key.pub().key_to_bin()))
def setUp(self): self.ec = ECCrypto() self.data = "".join([chr(i) for i in range(256)])
def __init__(self, configuration, endpoint_override=None, enable_statistics=False): if endpoint_override: self.endpoint = endpoint_override else: self.endpoint = UDPEndpoint(port=configuration['port'], ip=configuration['address']) self.endpoint.open() if enable_statistics: self.endpoint = StatisticsEndpoint(self, self.endpoint) self.network = Network() # Load/generate keys self.keys = {} for key_block in configuration['keys']: if key_block['file'] and isfile(key_block['file']): with open(key_block['file'], 'r') as f: content = f.read() try: # IPv8 Standardized bin format self.keys[key_block['alias']] = Peer( ECCrypto().key_from_private_bin(content)) except ValueError: try: # Try old Tribler M2Crypto PEM format content = content[31:-30].replace( '\n', '').decode("BASE64") peer = Peer(M2CryptoSK(keystring=content)) peer.mid # This will error out if the keystring is not M2Crypto self.keys[key_block['alias']] = peer except: # Try old LibNacl format content = "LibNaCLSK:" + content self.keys[key_block['alias']] = Peer( ECCrypto().key_from_private_bin(content)) else: self.keys[key_block['alias']] = Peer( ECCrypto().generate_key(key_block['generation'])) if key_block['file']: with open(key_block['file'], 'w') as f: f.write( self.keys[key_block['alias']].key.key_to_bin()) # Setup logging logging.basicConfig(**configuration['logger']) self.overlay_lock = RLock() self.strategies = [] self.overlays = [] for overlay in configuration['overlays']: overlay_class = _COMMUNITIES[overlay['class']] my_peer = self.keys[overlay['key']] overlay_instance = overlay_class(my_peer, self.endpoint, self.network, **overlay['initialize']) self.overlays.append(overlay_instance) for walker in overlay['walkers']: strategy_class = _WALKERS[walker['strategy']] args = walker['init'] target_peers = walker['peers'] self.strategies.append( (strategy_class(overlay_instance, **args), target_peers)) for config in overlay['on_start']: reactor.callWhenRunning( getattr(overlay_instance, config[0]), *config[1:]) self.state_machine_lc = LoopingCall(self.on_tick) self.state_machine_lc.start(configuration['walker_interval'], False)
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def __init__(self, *args, **kwargs): super(MyCommunity, self).__init__(*args, **kwargs) self.request_cache = RequestCache() def unload(self): self.request_cache.shutdown() super(MyCommunity, self).unload() def finish_ping(self, cache, include=True): global RESULTS print(cache.hostname, cache.address, time.time() - cache.starttime) if include: if (cache.hostname, cache.address) in RESULTS: RESULTS[(cache.hostname, cache.address)].append(time.time() - cache.starttime) else: RESULTS[(cache.hostname, cache.address)] = [time.time() - cache.starttime] elif (cache.hostname, cache.address) not in RESULTS: RESULTS[(cache.hostname, cache.address)] = [] self.next_ping() def next_ping(self): global CHECK_QUEUE if CHECK_QUEUE: hostname, address = CHECK_QUEUE.pop() packet = self.create_introduction_request(address) self.request_cache.add( PingCache(self, hostname, address, time.time())) self.endpoint.send(address, packet) else: reactor.callFromThread(reactor.stop) def introduction_response_callback(self, peer, dist, payload): if self.request_cache.has(u"introping", payload.identifier): cache = self.request_cache.pop(u"introping", payload.identifier) self.finish_ping(cache) def started(self): global CHECK_QUEUE dnsmap = {} for (address, port) in _DNS_ADDRESSES: try: ip = gethostbyname(address) dnsmap[(ip, port)] = address except: pass UNKNOWN_NAME = '*' for (ip, port) in _DEFAULT_ADDRESSES: hostname = dnsmap.get((ip, port), None) if not hostname: hostname = UNKNOWN_NAME UNKNOWN_NAME = UNKNOWN_NAME + '*' CHECK_QUEUE.append((hostname, (ip, port))) CHECK_QUEUE = CHECK_QUEUE * CONST_REQUESTS self.next_ping()
from twisted.web import server from ipv8.REST.rest_manager import RESTRequest from ipv8.REST.root_endpoint import RootEndpoint from ipv8.attestation.identity.community import IdentityCommunity from ipv8.attestation.wallet.community import AttestationCommunity from ipv8.configuration import get_default_configuration from ipv8.keyvault.crypto import ECCrypto from ipv8.peer import Peer from ipv8.taskmanager import TaskManager from ipv8.test.REST.rtest.peer_communication import GetStyleRequests, PostStyleRequests from ipv8.test.REST.rtest.rest_peer_communication import HTTPGetRequester, HTTPPostRequester from ipv8_service import IPv8 COMMUNITY_TO_MASTER_PEER_KEY = { 'AttestationCommunity': ECCrypto().generate_key(u'high'), 'DiscoveryCommunity': ECCrypto().generate_key(u'high'), 'HiddenTunnelCommunity': ECCrypto().generate_key(u'high'), 'IdentityCommunity': ECCrypto().generate_key(u'high'), 'TrustChainCommunity': ECCrypto().generate_key(u'high'), 'TunnelCommunity': ECCrypto().generate_key(u'high') } class TestPeer(object): """ Class for the purpose of testing the REST API """ def __init__(self, path,
def __init__(self, endpoint): my_peer = Peer(ECCrypto().generate_key(u"very-low")) super(EndpointServer, self).__init__(my_peer, endpoint, Network()) self.churn_lc = self.register_task("churn", LoopingCall(self.do_churn)).start(5.0, now=False) self.churn_strategy = RandomChurn(self) self.crypto = ECCrypto()
def _generate_peer(): key = ECCrypto().generate_key(u'very-low') address = (".".join([str(random.randint(0, 255)) for _ in range(4)]), random.randint(0, 65535)) return Peer(key, address)
class TestPeer(unittest.TestCase): test_key = ECCrypto().generate_key(u"very-low") def setUp(self): super(TestPeer, self).setUp() self.peer = Peer(TestPeer.test_key, ("1.2.3.4", 5)) def test_default_timestamp(self): """ Check if the default Lamport timestamp of a Peer is 0. """ self.assertEqual(self.peer.get_lamport_timestamp(), 0) def test_increment_timestamp(self): """ Check if the Lamport timestamp of a Peer can be incremented. """ self.peer.update_clock(1) self.assertEqual(self.peer.get_lamport_timestamp(), 1) def test_increase_timestamp(self): """ Check if the Lamport timestamp of a Peer can be increased arbitrarily. """ self.peer.update_clock(42) self.assertEqual(self.peer.get_lamport_timestamp(), 42) def test_decrease_timestamp(self): """ Check if the Lamport timestamp of a Peer cannot be decreased. """ self.peer.update_clock(-1) self.assertEqual(self.peer.get_lamport_timestamp(), 0) def test_peer_equality(self): """ Check if peers with the same key and address are equal. """ other = Peer(self.peer.key, self.peer.address) self.assertEqual(self.peer, other) def test_peer_inequality_key(self): """ Check if peers with a different key and same address are not equal. """ other = Peer(ECCrypto().generate_key(u"very-low"), self.peer.address) self.assertNotEqual(self.peer, other) def test_peer_inequality_address(self): """ Check if peers with the same key and a different address are not equal. """ other = Peer(self.peer.key) self.assertNotEqual(self.peer, other) def test_to_string(self): """ Check if the __str__ method functions properly. """ self.assertEqual( str(self.peer), "Peer<1.2.3.4:5, %s>" % self.peer.mid.encode('base64')[:-1])
from __future__ import absolute_import from __future__ import print_function from binascii import hexlify import sys from ipv8.keyvault.crypto import ECCrypto # This script generates a curve25519 key and prints it in hex format if '--public' in sys.argv: print(hexlify(ECCrypto().generate_key(u"curve25519").pub().key_to_bin())) else: print(hexlify(ECCrypto().generate_key(u"curve25519").key_to_bin()))
class TestSignatures(unittest.TestCase): """ Test whether signatures can be created and then decoded correctly. """ def setUp(self): self.ec = ECCrypto() self.data = "".join([chr(i) for i in range(256)]) def test_vlow(self): """ Check if very-low security keys generate a valid signature. """ key = self.ec.generate_key(u"very-low") signature = key.signature(self.data) self.assertTrue( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_low(self): """ Check if low security keys generate a valid signature. """ key = self.ec.generate_key(u"low") signature = key.signature(self.data) self.assertTrue( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_medium(self): """ Check if medium security keys generate a valid signature. """ key = self.ec.generate_key(u"medium") signature = key.signature(self.data) self.assertTrue( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_high(self): """ Check if high security keys generate a valid signature. """ key = self.ec.generate_key(u"high") signature = key.signature(self.data) self.assertTrue( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_curve25519(self): """ Check if curve25519 keys generate a valid signature. """ key = self.ec.generate_key(u"curve25519") signature = key.signature(self.data) self.assertTrue( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_invalid_m2crypto(self): """ Check if an M2Crypto key detects an invalid signature. """ key = self.ec.generate_key(u"very-low") signature = "" self.assertFalse( self.ec.is_valid_signature(key.pub(), self.data, signature)) def test_invalid_nacl(self): """ Check if an libnacl key detects an invalid signature. """ key = self.ec.generate_key(u"curve25519") signature = "" self.assertFalse( self.ec.is_valid_signature(key.pub(), self.data, signature))