From b0d5a8d27cf6a4b341df93e6f8de880f51a9fb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Lizo=C5=84czyk?= Date: Wed, 10 Jan 2018 17:46:57 +0100 Subject: [PATCH] external-tests: add python implementation Signed-off-by: Jason A. Donenfeld --- contrib/external-tests/python/main.py | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 contrib/external-tests/python/main.py diff --git a/contrib/external-tests/python/main.py b/contrib/external-tests/python/main.py new file mode 100755 index 0000000..ac20688 --- /dev/null +++ b/contrib/external-tests/python/main.py @@ -0,0 +1,94 @@ +#!/usr/bin/python3 + +# SPDX-License-Identifier: MIT +# Author: Piotr Lizonczyk + +import base64 +import datetime +from hashlib import blake2s +import socket +import struct + +from scapy.layers.inet import IP, ICMP + +from noise.connection import NoiseConnection, Keypair + + +address = ('demo.wireguard.com', 12913) + +our_private = base64.b64decode('WAmgVYXkbT2bCtdcDwolI88/iVi/aV3/PHcUBTQSYmo=') +their_public = base64.b64decode('qRCwZSKInrMAq5sepfCdaCsRJaoLe5jhtzfiw7CjbwM=') +preshared = base64.b64decode('FpCyhws9cxwWoV4xELtfJvjJN+zQVRPISllRWgeopVE=') +prologue = b'WireGuard v1 zx2c4 Jason@zx2c4.com' + +noise = NoiseConnection.from_name(b'Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s') +noise.set_as_initiator() +noise.set_keypair_from_private_bytes(Keypair.STATIC, our_private) +noise.set_keypair_from_public_bytes(Keypair.REMOTE_STATIC, their_public) +noise.set_psks(psk=preshared) +noise.set_prologue(prologue) +noise.start_handshake() + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + + +# 1. Prepare and send handshake initiation packet +now = datetime.datetime.now() +tai = struct.pack('!qi', 4611686018427387914 + int(now.timestamp()), int(now.microsecond * 1e3)) +initiation_packet = b'\x01' # Type: initiation +initiation_packet += b'\x00' * 3 # Reserved +initiation_packet += struct.pack('