-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_secure.py
executable file
·140 lines (107 loc) · 4.31 KB
/
client_secure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /bin/env python3
import random
from binascii import hexlify
import tinyec.ec
from Crypto.Util.Padding import pad, unpad
from tinyec import registry
import secrets
import socket
from threading import Thread
from Crypto.Cipher import AES
import argparse
import os
from ecdsa import SigningKey, VerifyingKey
from ecdsa.der import encode_sequence, encode_integer
import hashlib
import random
IP = "178.128.200.134"
PORT = 6073
KEY = None
PUB_KEY = None
class SendThread(Thread):
def __init__(self, conn, secret, iv):
Thread.__init__(self)
self.conn = conn
self.secret = secret
self.cipher = AES.new(secret, AES.MODE_CBC, iv)
def send(self, data):
self.conn.send(self.cipher.encrypt(pad(data, AES.block_size)))
def run(self):
while True:
self.send(input().encode('utf-8'))
class ReceiveThread(Thread):
def __init__(self, conn, secret, iv, send_thread):
Thread.__init__(self)
self.conn = conn
self.secret = secret
self.send = send_thread
self.cipher = AES.new(secret, AES.MODE_CBC, iv)
def run(self):
while True:
from_server = self.conn.recv(AES.block_size)
if len(from_server) > 0:
data = unpad(self.cipher.decrypt(from_server), AES.block_size)
if data != b"ACK <--":
print("RECV>", data.decode())
self.send.send(b"ACK <--")
else:
print(data.decode())
def compress(pub_key):
return hex(pub_key.x) + hex(pub_key.y % 2)[2:]
def gen_key(mypriv, curve):
return (mypriv * curve.g)
parser = argparse.ArgumentParser(description='etedex')
parser.add_argument("-i", "--ip", dest='ip',
help="specify the ip", type=str)
parser.add_argument("-k", dest='key',
help="user private ecc key", type=str)
if __name__ == '__main__':
args = parser.parse_args()
ip = args.ip
if ip: IP = ip
with open(args.key, 'r') as key_f:
private_key = "".join(key_f.readlines())
key = SigningKey.from_pem(private_key)
# print(key.privkey.secret_multiplier)
h = hashlib.sha256()
h.update(private_key.encode())
h.update(random.Random().randint(0, 2 ** 31).to_bytes(4, byteorder='big'))
id = h.digest().hex()
print("YOUR_ID", id)
del private_key
oth_id = input("other's id?>")
curve = registry.get_curve('secp256r1')
session_key = secrets.randbelow(curve.field.n)
session_pub = (session_key * curve.g)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((IP, PORT))
client.send(id.encode()) # me
client.send(oth_id.encode()) # hashwanted
# my identity
my_der = key.verifying_key.to_der()
print(hexlify(my_der))
client.send(my_der)
sig = key.sign(session_pub.x.to_bytes(32, byteorder='big') + session_pub.y.to_bytes(32, byteorder='big'),
hashfunc=hashlib.sha256)
client.send(len(sig).to_bytes(1, byteorder='big'))
client.send(sig)
client.send(session_pub.x.to_bytes(32, byteorder='big') + session_pub.y.to_bytes(32, byteorder='big'))
if client.recv(1) != b'\x30':
exit(-255)
der_len = int.from_bytes(client.recv(1), byteorder='big')
oth_der = client.recv(der_len)
data_len = int.from_bytes(client.recv(1), byteorder='big')
oth_sig = client.recv(data_len)
othpubx = client.recv(32)
othpuby = client.recv(32)
# get other identity
oth_pub = VerifyingKey.from_der(b'\x30' + der_len.to_bytes(1, byteorder='big') + oth_der)
oth_pub.verify(oth_sig, othpubx + othpuby, hashfunc=hashlib.sha256)
ecdh = tinyec.ec.Point(curve, int.from_bytes(othpubx, byteorder='big'), int.from_bytes(othpuby, byteorder='big'))
secret = session_key * ecdh
print("session info: ")
print("session key (mine/theirs): (", session_pub.x, session_pub.y, ") / (", othpubx, othpuby, ") ")
s = SendThread(client, secret.x.to_bytes(32, byteorder='big'), secret.y.to_bytes(32, byteorder='big')[:16])
s.start()
ReceiveThread(client, secret.x.to_bytes(32, byteorder='big'), secret.y.to_bytes(32, byteorder='big')[:16],
s).start()