-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
297 lines (227 loc) · 8.11 KB
/
Copy pathclient.py
File metadata and controls
297 lines (227 loc) · 8.11 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import hashlib
import os
import sys
import requests
from base64 import b64encode, b64decode
from flask import Flask, abort, request
from cryptography.hazmat.primitives.asymmetric.x25519 import (
X25519PrivateKey,
X25519PublicKey,
)
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from xeddsa import XEd25519
ENCODING = serialization.Encoding.Raw
PRIV_FORMAT = serialization.PrivateFormat.Raw
PUB_FORMAT = serialization.PublicFormat.Raw
ENCRYPTION = serialization.NoEncryption
F = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF .to_bytes(
32, "big"
)
SERVER = "http://localhost:5000"
app = Flask(__name__)
username = "unset"
port = "5001"
bundle = {}
shared_keys = {}
@app.route("/")
def index():
return f"Hello, {username}!"
@app.route("/perform_handshake", methods=["POST"])
def perform_handshake():
data = request.json
person = data["send_to"]
print(f"Performing handshake with {person}.")
prekey_bundle = requests.get(f"{SERVER}/get_prekey_bundle/{person}").json()
identity = X25519PublicKey.from_public_bytes(strb(prekey_bundle["identity"]))
spk = X25519PublicKey.from_public_bytes(strb(prekey_bundle["spk"]))
signature = strb(prekey_bundle["signature"])
if not verify(identity, spk, signature):
print("SIGNED PREKEY VERIFICATION FAILED. ABORTING.")
abort(403)
self_id = bundle["identity"]
opk_index = prekey_bundle["opk_index"]
opk = X25519PublicKey.from_public_bytes(strb(prekey_bundle["opk"]))
ephemeral = X25519PrivateKey.generate()
dh1 = self_id.exchange(spk)
dh2 = ephemeral.exchange(identity)
dh3 = ephemeral.exchange(spk)
dh4 = ephemeral.exchange(opk)
dh = dh1 + dh2 + dh3 + dh4
shared_key = HKDF(
algorithm=hashes.SHA512(), length=32, salt=None, info=b"handshake"
).derive(F + dh)
self_id = public_bytes(self_id.public_key())
ephemeral = public_bytes(ephemeral.public_key())
del dh1, dh2, dh3, dh4, dh
ik_a = hash(self_id)
ik_b = hash(public_bytes(identity))
associated_data = ik_a + ik_b
nonce = os.urandom(12)
message = b"shaking hands"
aead = ChaCha20Poly1305(shared_key).encrypt(nonce, message, associated_data)
print("Sending handshake data.")
success = requests.post(
f"{SERVER}/perform_handshake",
json=dict(
sender_uid=hashlib.sha1(username.encode("utf-8")).hexdigest(),
send_to=person,
identity=bstr(self_id),
ephemeral=bstr(ephemeral),
index=opk_index,
nonce=bstr(nonce),
aead=bstr(aead),
),
).json()["success"]
if success:
print("Handshake successful! Storing shared key.")
shared_keys[hashlib.sha1(person.encode("utf-8")).hexdigest()] = dict(shared_key=shared_key, associated_data=associated_data)
else:
print("Handshake failed!")
return dict(success=success)
@app.route("/receive_handshake", methods=["POST"])
def receive_handshake():
data = request.json
uid = data["sender_uid"]
print(f"Received a handshake request from UID {uid}!")
identity = X25519PublicKey.from_public_bytes(strb(data["identity"]))
ephemeral = X25519PublicKey.from_public_bytes(strb(data["ephemeral"]))
index = data["index"]
opk = bundle["opks"].pop(index)
nonce = strb(data["nonce"])
aead = strb(data["aead"])
dh1 = bundle["spk"].exchange(identity)
dh2 = bundle["identity"].exchange(ephemeral)
dh3 = bundle["spk"].exchange(ephemeral)
dh4 = opk.exchange(ephemeral)
dh = dh1 + dh2 + dh3 + dh4
shared_key = HKDF(
algorithm=hashes.SHA512(), length=32, salt=None, info=b"handshake"
).derive(F + dh)
del dh, dh1, dh2, dh3, dh4, opk
ik_a = hash(public_bytes(identity))
ik_b = hash(public_bytes(bundle["identity"].public_key()))
associated_data = ik_a + ik_b
message = ChaCha20Poly1305(shared_key).decrypt(nonce, aead, associated_data)
if message != b"shaking hands":
del shared_key
print("AEAD CHECK FAILED. ABORTING HANDSHAKE.")
abort(403)
print("Validation successful! Storing shared_key.")
shared_keys[uid] = dict(shared_key=shared_key, associated_data=associated_data)
return dict(success=True)
@app.route("/all_data")
def all_data():
return dict(
username=username,
port=request.host.split(":")[-1],
identity=dict(
public=bstr(public_bytes(bundle["identity"].public_key())),
private=bstr(private_bytes(bundle["identity"])),
),
spk=dict(
public=bstr(public_bytes(bundle["spk"].public_key())),
private=bstr(private_bytes(bundle["spk"])),
),
signature=bstr(bundle["signature"]),
opks={
k: dict(
public=bstr(public_bytes(v.public_key())),
private=bstr(private_bytes(v)),
)
for k, v in bundle["opks"].items()
},
connections={
k: dict(
sk = bstr(v.get("shared_key")),
ad = bstr(v.get("associated_data"))
)
for k, v in shared_keys.items()
}
)
def sign(identity: X25519PrivateKey, spk: X25519PublicKey):
"""Uses the `identity` private key to sign a hashed
version of the `spk` public key.
:param identity: the user's identity
:type identity: X25519PrivateKey
:param spk: the user's signed prekey
:type spk: X25519PublicKey
:return: the byte sequence representing an EdDSA signature
on the hashed `spk` public key
:rtype: bytes
"""
data = hash(public_bytes(spk))
id_bytes = private_bytes(identity)
xed = XEd25519(id_bytes, None)
return xed.sign(data=data)
def verify(identity: X25519PublicKey, spk: X25519PublicKey, signature: bytes):
"""Verifies that the received `data` is an EdDSA signature
on the hashed `spk` public key using the `identity` key.
:param identity: the user's identity
:type identity: X25519PublicKey
:param spk: the user's signed prekey
:type spk: X25519PublicKey
:param data: the received byte sequence
:type data: bytes
:return: whether the verificaiton was successful or not
:rtype: bool
"""
data = hash(public_bytes(spk))
id_bytes = public_bytes(identity)
xed = XEd25519(None, id_bytes)
try:
xed.verify(data, signature)
return True
except:
return False
def initialize_bundle():
identity = X25519PrivateKey.generate()
spk = X25519PrivateKey.generate()
signature = sign(identity, spk.public_key())
opks = {str(i): X25519PrivateKey.generate() for i in range(5)}
bundle["identity"] = identity
bundle["spk"] = spk
bundle["signature"] = signature
bundle["opks"] = opks
identity = bstr(public_bytes(identity.public_key()))
spk = bstr(public_bytes(spk.public_key()))
signature = bstr(signature)
opks = {k: bstr(public_bytes(v.public_key())) for k, v in opks.items()}
requests.post(
f"{SERVER}/register_user",
json=dict(
username=username,
prekey_bundle=dict(
identity=identity,
spk=spk,
signature=signature,
opks=opks,
),
port=port,
),
)
def public_bytes(key: X25519PublicKey):
return key.public_bytes(encoding=ENCODING, format=PUB_FORMAT)
def private_bytes(key: X25519PrivateKey):
return key.private_bytes(
encoding=ENCODING, format=PRIV_FORMAT, encryption_algorithm=ENCRYPTION()
)
def bstr(data: bytes):
return b64encode(data).decode("utf-8")
def strb(data: str):
return b64decode(data.encode("utf-8"))
def hash(data: bytes):
hasher = hashes.Hash(hashes.SHA512())
hasher.update(data)
return hasher.finalize()
if __name__ == "__main__":
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
s.listen(1)
port = s.getsockname()[1]
s.close()
username = sys.argv[-1]
initialize_bundle()
app.run(port=port)