def run_exchange(transport, code, app_id, side): # Send the SPAKE2 message spake = SPAKE2_Symmetric(util.to_bytes(code), idSymmetric=util.to_bytes(app_id)) outbound = spake.start() transport.send_json({ 'phase': u'pake', 'body': util.bytes_to_hexstr( util.dict_to_bytes({ 'pake_v1': util.bytes_to_hexstr(outbound), })), 'side': side, 'type': 'message', }) # Receive SPAKE2 message pake_msg = transport.receive_json() inbound = util.hexstr_to_bytes( util.bytes_to_dict(util.hexstr_to_bytes(pake_msg['body']))['pake_v1']) spake_key = spake.finish(inbound) transport.send_line(util.bytes_to_hexstr(spake_key))
def main(): parser = argparse.ArgumentParser(prog='nacl') parser.add_argument('--key', dest='key', type=str, help='NaCl key for secret box message, hex-encoded') parser.add_argument('--nonce', dest='nonce', type=str, help='NaCl nonce for secret box message, hex-encoded') params = parser.parse_args(sys.argv[1:]) run_exchange(Transport(input_stream=sys.stdin, output_stream=sys.stdout), util.hexstr_to_bytes(params.key), util.hexstr_to_bytes(params.nonce))
def run_exchange(transport, code, app_id, side): # Send the SPAKE2 message spake = SPAKE2_Symmetric( util.to_bytes(code), idSymmetric=util.to_bytes(app_id)) outbound = spake.start() transport.send_json({ 'phase': 'pake', 'body': util.bytes_to_hexstr( util.dict_to_bytes({ 'pake_v1': util.bytes_to_hexstr(outbound), }) ), 'side': side, 'type': 'message', }) # Receive SPAKE2 message pake_msg = transport.receive_json() inbound = util.hexstr_to_bytes( util.bytes_to_dict( util.hexstr_to_bytes(pake_msg['body']) )['pake_v1'] ) spake_key = spake.finish(inbound) # Send the versions message version_phase = 'version' transport.send_json({ 'phase': version_phase, 'body': util.bytes_to_hexstr( encrypt_data( derive_phase_key(spake_key, side, version_phase), util.dict_to_bytes({'app_versions': {}}) ) ), 'side': side, 'type': 'message', }) # Receive the versions message versions = transport.receive_json() their_versions = util.bytes_to_dict( decrypt_data( derive_phase_key(spake_key, versions['side'], versions['phase']), util.hexstr_to_bytes( versions['body'] ), ), ) return their_versions
def run_exchange(transport, key, nonce): box = SecretBox(key) line = transport.receive_line() decrypted = box.decrypt(util.hexstr_to_bytes(line)) transport.send_line(decrypted.decode('utf-8')) encrypted = util.bytes_to_hexstr(box.encrypt(decrypted, nonce)) transport.send_line(encrypted)
def main(): parser = argparse.ArgumentParser(prog='version_exchange') parser.add_argument( '--spake-key', dest='spake_key', type=unicode, help='SPAKE2 key to generate the one-off key, hex-encoded') parser.add_argument('--side', dest='side', type=unicode, help='Identifier for this side of the exchange') parser.add_argument( '--phase', dest='phase', type=unicode, help='The phase of the message we need a one-off key for') params = parser.parse_args(sys.argv[1:]) key = derive_phase_key(util.hexstr_to_bytes(params.spake_key), params.side, params.phase) print util.bytes_to_hexstr(key)