def test_sphinx_assoc_data(self): route = self.newTestRoute(5) destination = b"client" message = b"this is a test" rand_reader = RandReader() params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, route, self.pki, destination, message, rand_reader) packet = SphinxPacket(packet.header, SphinxBody(b"something else")) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(self.private_key_map[route[0]]) py.test.raises(SphinxBodySizeMismatchError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def test_end_to_end(self): rand_reader = ChachaNoiseReader( "47ade5905376604cde0b57e732936b4298281c8a67b6a62c6107482eb69e2941") # XXX should we make client_ids be strings or what? self.setUpMixVectors() message = b"the quick brown fox" params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, self.route, self.pki, self.route[-1], message, rand_reader) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(self.private_key_map[self.route[0]]) result = sphinx_packet_unwrap(params, replay_cache, key_state, packet) self.alpha = binascii.unhexlify( "b00eb0894f1b49530150c33cc4055cf3b97f3cac22f03f25050394bf5d80c954") self.beta = binascii.unhexlify( "13554b4891e71b85632e83baa0a230bd710b818a9d94863c7732deb742c167b3570b6799f99ff76c94bc58b613ff073e6dda60a914e03d6e537fe3e712cbb98e414e0cfec3fd14f0e79b66fc0338820aabb680cc6cb9274b836852bd737ecc121e828697675fc839ebf820ba9d53e17f94b5bad2631f915ae059d4e37fa04b776158f306d92ce2bce232f60412feff11c754450970bba6a318e45f9ce9210202d669c7bf7c38eb1c14b9cda4e6311eba" ) self.gamma = binascii.unhexlify("0b747d558acd9d59ceedc1f876634644") self.delta = binascii.unhexlify( "0269f1c546605e56162c97ed66054a14565e868193f159b1f25f962bb29a94581826586f955fff0841a9266bc6cb75aa8f217c6d2998bdbdf3782e0e0e8eaa2d3e159ccecccb12c7476d015de13daa6e4d757cf979abadba7e8a92153e5f28f56c94f084d3a9da487ff4a1f478b470f89c74e18179e7aff47e82710f973952a66043d341e27d54370506d63344a6fc738d39d1af3cc1d8394aeaf46a286688c9882fc95077a6b0b438cda481400e56debd0468aa9d5656a7e920ce0882bd07bee35801389ceb9a377a399e639a1d257d7ecd047c161f273faf1026ce5c3f7e5855865be24f53bb48e34dea1bad0c688c4c07564d8d771a8ad8ce980520d81da565a7da0e9e70eb1e975621729f146f090d8ad5e475ed42b4d68993c8ff7a75aaab4ef29d620b4caf5761a41887f3952950bc974468ef4381ebd8dc36dee74f9e1603c195527d84f45bcb18f9161ce5ba989abbc8fe887bbf90c6e2aa453f728bfdef11b776fff9796d8e3affe7b945a38f50285eb9e3dd3697082f0bbd554ea9f5c31e57c1e7f252fe76b69d7af55c9668688d1114de093c6c837dcd8a2836d3ed5199171860288806111893a468666e9ac83562d02d660f183451dbfdd094d26a988ae4bf67a86ae56fbef6a1a8cf53fa304ec41ac93a80c5b68a29e2fa195fa4b165659bf4dc6e2cff12becb34e5c7c6fa567868483f1ced888a441412408f51cff75c3e31d2535d95f9029017d02d993f6bd4b14b9f9d819a207afa7b38a4f70af0c93a3234c96a612f2633e456f2d09bd334fa8015a39f762c301e9fdcf4c525f2549e228dd10ea8549620606ac893a2a299644678ebb8872a217374289a4f75c638268929064f1f5ff51b4aa142fac7fe63d6b155fcc8539c34405635b9da0b7602dba8b6df82335ef03cc9afcc818761f1f4c87ac9e6a39caa249a99131492a8e48de7af9caf3aea7448936d6d2ce9b24f8a53385377196d16e69de43cb84ce6435a68d4e10fbcefeefeca20023ae76d34c7405f16d33d726073052985189cd4ec92d7a4d8cedf29e10a56c27fd5aa2be904d823a4b345bb2f4ae2e7c8ccc95a2e144fa012ad44bf7ee811f51965d90b60c590ab6794868e1d76b7678202a37473e6bd945ced2bd7802b7a5117cb87af00a43d5edae7830bdeb72440d071ce24fe59c4610fc7119044bd3f5d60aeabcc394f020e8e300ad0fe9b58023ca6470345514dab5a7212ce17b612094fadfc7f6e3d5542bff77f80e785064307d5ec8c26b80f06fb3b7d4d6f4c42b647564f4ba05371ef8c02f1fd32a2ae7522425136ab6eb8206f2e0094d78b644b7057aad1d2afa5f9e6abf082da932076cf63b173a1eef549ba18522200748705bac31e950849826a153185f9180aa71553fdb25152ac2a1674c8b007ba78274af411363b6dab068c3d0ceaec2873d96ba7" ) self.match_hop = "ff81855a360000000000000000000000" result = self.mixnet_test_state_machine(params, result) assert result == message
def test_sphinx_alpha_too_big(self): route = self.newTestRoute(5) destination = b"dest" message = b"this is a test" rand_reader = RandReader() params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, route, self.pki, destination, message, rand_reader) packet = SphinxPacket( SphinxHeader(packet.header.alpha + b"A", packet.header.beta, packet.header.gamma), packet.body) replay_cache = PacketReplayCacheDict() public_key, private_key = generate_node_keypair(rand_reader) key_state = SphinxNodeKeyState(private_key) py.test.raises(HeaderAlphaGroupMismatchError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def test_hypothesis_toobig_body_size(alpha, beta, gamma, delta, private_key): sphinx_header = SphinxHeader(alpha, beta, gamma) sphinx_body = SphinxBody(delta) packet = SphinxPacket(sphinx_header, sphinx_body) params = SphinxParams(max_hops=5, payload_size=1024) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(private_key=private_key) py.test.raises(SphinxBodySizeMismatchError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def send(self, route, message): """ send a wrapped inside a forward sphinx packet """ first_hop_addr = self.pki.get_mix_addr(self.transport.name, route[0]) sphinx_packet = SphinxPacket.forward_message(self.params, route, self.pki, route[-1], message, self.rand_reader) raw_sphinx_packet = sphinx_packet.get_raw_bytes() return self.transport.send(first_hop_addr, raw_sphinx_packet)
def no_test_hypothesis_incorrect_mac(alpha, beta, gamma, delta, private_key): group = GroupCurve25519() sphinx_header = SphinxHeader(alpha, beta, gamma) sphinx_body = SphinxBody(delta) packet = SphinxPacket(sphinx_header, sphinx_body) params = SphinxParams(max_hops=5, payload_size=1024) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(private_key=private_key) py.test.raises(IncorrectMACError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def received(self, raw_sphinx_packet): """ receive a raw_packet, decode it and unwrap/decrypt it and return the results """ action = start_action( action_type=u"mix packet unwrap", ) with action.context(): sphinx_packet = SphinxPacket.from_raw_bytes(self.params, raw_sphinx_packet) unwrapped_packet = sphinx_packet_unwrap(self.params, self.replay_cache, self.key_state, sphinx_packet) self.packet_received_handler(unwrapped_packet)
def test_sphinx_corrupt_mac(self): route = self.newTestRoute(5) destination = b"client" message = b"this is a test" rand_reader = RandReader() params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, route, self.pki, destination, message, rand_reader) replay_cache = PacketReplayCacheDict() public_key, private_key = generate_node_keypair(rand_reader) key_state = SphinxNodeKeyState(private_key) py.test.raises(IncorrectMACError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def test_sphinx_replay(self): rand_reader = ChachaNoiseReader( "47ade5905376604cde0b57e732936b4298281c8a67b6a62c6107482eb69e2941") self.setUpMixVectors() message = b"the quick brown fox" params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, self.route, self.pki, self.route[-1], message, rand_reader) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(self.private_key_map[self.route[0]]) sphinx_packet_unwrap(params, replay_cache, key_state, packet) py.test.raises(ReplayError, sphinx_packet_unwrap, params, replay_cache, key_state, packet)
def test_sphinx_replay(self): route = self.newTestRoute(5) destination = b"client" message = b"this is a test" rand_reader = RandReader() params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, route, self.pki, destination, message, rand_reader) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(self.private_key_map[route[0]]) sphinx_packet_unwrap(params, replay_cache, key_state, packet) py.test.raises(ReplayError, sphinx_packet_unwrap, params, replay_cache, key_state, packet) replay_cache.flush() sphinx_packet_unwrap(params, replay_cache, key_state, packet)
def test_hypothesis_scalarmult_error(alpha, beta, gamma, delta, private_key): sphinx_header = SphinxHeader(alpha, beta, gamma) sphinx_body = SphinxBody(delta) packet = SphinxPacket(sphinx_header, sphinx_body) params = SphinxParams(max_hops=5, payload_size=1024) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(private_key=private_key) def assumptions_wrap(): try: _ = sphinx_packet_unwrap(params, replay_cache, key_state, packet) raise Exception("wtf") except IncorrectMACError, _: assume(False) except AssertionError, _: return
def test_sphinx_single_hop(self): route = self.newTestRoute(1) destination = b"client" message = b"this is a test" rand_reader = RandReader() params = SphinxParams(5, 1024) packet = SphinxPacket.forward_message(params, route, self.pki, destination, message, rand_reader) replay_cache = PacketReplayCacheDict() key_state = SphinxNodeKeyState(self.private_key_map[route[0]]) result = sphinx_packet_unwrap(params, replay_cache, key_state, packet) received_dest, received_message = result.exit_hop assert result.client_hop is None assert result.next_hop is None assert received_dest == destination assert received_message == message
def create_invalid_message(params, route, node_map, dest, msg, rand_reader): route_len = len(route) assert len(dest) < 128 and len(dest) > 0 assert SECURITY_PARAMETER + 1 + len(dest) + len(msg) < params.payload_size # Compute the header and the secrets header, secrets = create_header(params, route, node_map, b"\xFE" * SECURITY_PARAMETER, b"\xFE" * SECURITY_PARAMETER, rand_reader) encoded_dest = destination_encode(dest) body = (b"\x00" * SECURITY_PARAMETER) + bytes(encoded_dest) + bytes(msg) padded_body = add_padding(body, params.payload_size) block_cipher = SphinxLioness() # Compute the delta values key = block_cipher.create_block_cipher_key(secrets[route_len - 1]) delta = block_cipher.encrypt(key, padded_body) for i in range(route_len - 2, -1, -1): delta = block_cipher.encrypt( block_cipher.create_block_cipher_key(secrets[i]), delta) return SphinxPacket(header=header, body=SphinxBody(delta))