-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_mixnets.py
More file actions
384 lines (286 loc) · 13.7 KB
/
Copy pathlab_mixnets.py
File metadata and controls
384 lines (286 loc) · 13.7 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#####################################################
# COMP0061 Privacy Enhancing Technologies -- Lab on Mix Systems
#
# Basics of Mix networks and Traffic Analysis
#
# Run the tests through:
# $ pytest -v
#####################################################
# TASK 1 -- Ensure petlib is installed on the System
# and also pytest. Ensure the Lab Code can
# be imported.
from struct import pack, unpack
from typing import NamedTuple
from Cryptodome.Cipher import AES
from Cryptodome.Hash import HMAC, SHA512
from Cryptodome.Math.Numbers import Integer
from Cryptodome.PublicKey import ECC, _curve
Curve = _curve._Curve
PrivKey = Integer
PubKey = ECC.EccPoint
def aes_ctr_enc_dec(key, iv, message):
""" A helper function that implements AES Counter (CTR) Mode encryption and decryption.
Expects a key (16 byte), and IV (16 bytes) and an input plaintext / ciphertext.
If it is not obvious convince yourself that CTR encryption and decryption are in fact the same operations.
"""
cipher = AES.new(key, AES.MODE_CTR, nonce=iv)
output = cipher.encrypt(message)
return output
def _point_to_bytes(p: ECC.EccPoint) -> bytes:
x, y = p.xy
return x.to_bytes() + y.to_bytes()
#####################################################
# TASK 2 -- Build a simple 1-hop mix client.
#
#
# This is the type of messages destined for the one-hop mix
OneHopMixMessage = NamedTuple('OneHopMixMessage', [('ec_public_key', PubKey),
('hmac', bytes),
('address', bytes),
('message', bytes)])
def mix_server_one_hop(private_key: PrivKey, message_list: list[OneHopMixMessage]) -> list[tuple[bytes, bytes]]:
""" Implements the decoding for a simple one-hop mix.
Each message is decoded in turn:
- A shared key is derived from the message public key and the mix private_key.
- the hmac is checked against all encrypted parts of the message
- the address and message are decrypted, decoded and returned
"""
out_queue = []
# Process all messages
for msg in message_list:
# Check elements and lengths
if not len(msg.hmac) == 20 or \
not len(msg.address) == 258 or \
not len(msg.message) == 1002:
raise Exception("Malformed input message")
# First get a shared key
shared_element = msg.ec_public_key * private_key
key_material = SHA512.new(_point_to_bytes(shared_element)).digest()
# Use different parts of the shared key for different operations
hmac_key = key_material[:16]
address_key = key_material[16:32]
message_key = key_material[32:48]
# Check the HMAC
h = HMAC.new(key=hmac_key, digestmod=SHA512)
h.update(msg.address)
h.update(msg.message)
expected_mac = h.digest()
if not msg.hmac == expected_mac[:20]:
raise Exception("HMAC check failure")
# Decrypt the address and the message
iv = b"\x00" * 8
address_plaintext = aes_ctr_enc_dec(address_key, iv, msg.address)
message_plaintext = aes_ctr_enc_dec(message_key, iv, msg.message)
# Decode the address and message
address_len, address_full = unpack("!H256s", address_plaintext)
message_len, message_full = unpack("!H1000s", message_plaintext)
output = (address_full[:address_len], message_full[:message_len])
out_queue += [output]
return sorted(out_queue)
def mix_client_one_hop(group: Curve, public_key: PubKey, address: bytes, message: bytes) -> OneHopMixMessage:
"""
Encode a message to travel through a single mix with a set public key.
The maximum size of the final address and the message are 256 bytes and 1000 bytes respectively.
Returns an 'OneHopMixMessage' with four parts: a public key, an HMAC (20 bytes), an address ciphertext (256 + 2 bytes) and a message ciphertext (1002 bytes).
"""
assert isinstance(address, bytes) and len(address) <= 256
assert isinstance(message, bytes) and len(message) <= 1000
# Encode the address and message
# Use those as the payload for encryption
address_plaintext = pack("!H256s", len(address), address)
message_plaintext = pack("!H1000s", len(message), message)
# Generate a fresh public key
private_key = Integer.random_range(min_inclusive=1, max_exclusive=group.order)
client_public_key = group.G * private_key
iv = b"\x00" * 8
shared_element = public_key * private_key
key_material = SHA512.new(_point_to_bytes(shared_element)).digest()
# Use different parts of the shared key for different operations
hmac_key = key_material[:16]
address_key = key_material[16:32]
message_key = key_material[32:48]
# Encrypt address and message
address_cipher = aes_ctr_enc_dec(address_key, iv, address_plaintext)
message_cipher = aes_ctr_enc_dec(message_key, iv, message_plaintext)
# Check the HMAC
h = HMAC.new(key=hmac_key, digestmod=SHA512)
h.update(address_cipher)
h.update(message_cipher)
expected_mac = h.digest()[:20]
return OneHopMixMessage(client_public_key, expected_mac, address_cipher, message_cipher)
#####################################################
# TASK 3 -- Build a n-hop mix client.
# Mixes are in a fixed cascade.
#
# This is the type of messages destined for the n-hop mix
NHopMixMessage = NamedTuple('NHopMixMessage', [('ec_public_key', PubKey),
('hmacs', list[bytes]),
('address', bytes),
('message', bytes)])
def mix_server_n_hop(private_key: PrivKey, message_list: list[NHopMixMessage], final=False):
""" Decodes a NHopMixMessage message and outputs either messages destined
to the next mix or a list of tuples (address, message) (if final=True) to be
sent to their final recipients.
Broadly speaking the mix will process each message in turn:
- it derives a shared key (using its private_key),
- checks the first hmac,
- decrypts all other parts,
- either forwards or decodes the message.
"""
out_queue = []
# Process all messages
for msg in message_list:
# Check elements and lengths
if not isinstance(msg.hmacs, list) or \
not len(msg.hmacs[0]) == 20 or \
not len(msg.address) == 258 or \
not len(msg.message) == 1002:
raise Exception("Malformed input message")
# First get a shared key
shared_element = msg.ec_public_key * private_key
key_material = SHA512.new(_point_to_bytes(shared_element)).digest()
# Use different parts of the shared key for different operations
hmac_key = key_material[:16]
address_key = key_material[16:32]
message_key = key_material[32:48]
# Extract a blinding factor for the public_key
blinding_factor = Integer.from_bytes(key_material[48:])
new_ec_public_key = msg.ec_public_key * blinding_factor
# Check the HMAC
h = HMAC.new(key=hmac_key, digestmod=SHA512)
for other_mac in msg.hmacs[1:]:
h.update(other_mac)
h.update(msg.address)
h.update(msg.message)
expected_mac = h.digest()
# print(msg.hmacs, expected_mac[:20])
if not msg.hmacs[0] == expected_mac[:20]:
raise Exception("HMAC check failure")
# Decrypt hmacs
new_hmacs = []
for i, other_mac in enumerate(msg.hmacs[1:]):
# Ensure the IV is different for each hmac
iv = pack("H6s", i, b"\x00" * 6)
hmac_plaintext = aes_ctr_enc_dec(hmac_key, iv, other_mac)
new_hmacs += [hmac_plaintext]
# Decrypt address & message
iv = b"\x00" * 8
address_plaintext = aes_ctr_enc_dec(address_key, iv, msg.address)
message_plaintext = aes_ctr_enc_dec(message_key, iv, msg.message)
if final:
# Decode the address and message
address_len, address_full = unpack("!H256s", address_plaintext)
message_len, message_full = unpack("!H1000s", message_plaintext)
out_msg = (address_full[:address_len], message_full[:message_len])
out_queue += [out_msg]
else:
# Pass the new mix message to the next mix
out_msg = NHopMixMessage(new_ec_public_key, new_hmacs, address_plaintext, message_plaintext)
out_queue += [out_msg]
return out_queue
def mix_client_n_hop(group: Curve, public_keys: list[PubKey], address: bytes, message: bytes) -> NHopMixMessage:
"""
Encode a message to travel through a sequence of mixes with a sequence public keys.
The maximum size of the final address and the message are 256 bytes and 1000 bytes respectively.
Returns an 'NHopMixMessage' with four parts: a public key, a list of hmacs (20 bytes each),
an address ciphertext (256 + 2 bytes) and a message ciphertext (1002 bytes).
"""
assert isinstance(address, bytes) and len(address) <= 256
assert isinstance(message, bytes) and len(message) <= 1000
# Encode the address and message
# use those encoded values as the payload you encrypt!
address_plaintext = pack("!H256s", len(address), address)
message_plaintext = pack("!H1000s", len(message), message)
# Generate a fresh public key
private_key = Integer.random_range(min_inclusive=1, max_exclusive=group.order)
client_public_key = group.G * private_key
address_cipher = address_plaintext
message_cipher = message_plaintext
i = 0
while i + 1 < len(public_keys):
shared_element = public_keys[i] * private_key
key_material = SHA512.new(_point_to_bytes(shared_element)).digest()
blinding_factor = Integer.from_bytes(key_material[48:])
for j in range(i+1, len(public_keys)):
public_keys[j] *= blinding_factor
i += 1
hmacs = []
for pubkey in reversed(public_keys):
shared_element = pubkey * private_key
key_material = SHA512.new(_point_to_bytes(shared_element)).digest()
# Use different parts of the shared key for different operations
hmac_key = key_material[:16]
address_key = key_material[16:32]
message_key = key_material[32:48]
# Encrypt address and message
iv = b"\x00" * 8
address_cipher = aes_ctr_enc_dec(address_key, iv, address_cipher)
message_cipher = aes_ctr_enc_dec(message_key, iv, message_cipher)
# Encrypt hmacs
new_hmacs = []
for i, mac in enumerate(hmacs):
# Ensure the IV is different for each hmac
iv = pack("H6s", i, b"\x00" * 6)
hmac_encrypt = aes_ctr_enc_dec(hmac_key, iv, mac)
new_hmacs.append(hmac_encrypt)
# Check the HMAC
h = HMAC.new(key=hmac_key, digestmod=SHA512)
for hmac in new_hmacs:
h.update(hmac)
h.update(address_cipher)
h.update(message_cipher)
new_hmac = h.digest()[:20]
new_hmacs.insert(0, new_hmac)
hmacs = new_hmacs
return NHopMixMessage(client_public_key, hmacs, address_cipher, message_cipher)
#####################################################
# TASK 4 -- Statistical Disclosure Attack
# Given a set of anonymized traces the objective is to output an ordered list of likely `friends` of a target user.
import random
Trace = list[tuple[list[int], list[int]]]
def generate_trace(number_of_users: int, threshold_size: int, number_of_rounds: int, targets_friends: list[int]) -> Trace:
""" Generate a simulated trace of traffic. """
others = range(1, number_of_users)
all_users = range(number_of_users)
trace = []
# Generate traces in which Alice (user 0) is not sending
for _ in range(9 * number_of_rounds // 10):
senders = sorted(random.sample(others, threshold_size))
receivers = sorted(random.sample(all_users, threshold_size))
trace += [(senders, receivers)]
# Generate traces in which Alice (user 0) is sending
for _ in range(number_of_rounds // 10):
senders = sorted([0] + random.sample(others, threshold_size - 1))
# Alice sends to a friend
friend = random.choice(targets_friends)
receivers = sorted([friend] + random.sample(all_users, threshold_size - 1))
trace += [(senders, receivers)]
random.shuffle(trace)
return trace
from collections import Counter
def analyze_trace(trace: Trace, target_number_of_friends: int, target: int = 0) -> list[int]:
"""
Given a trace of traffic, and a given number of friends,
return the list of receiver identifiers that are the most likely
friends of the target.
"""
receiver_counter = Counter()
for senders, receivers in trace:
if target in senders:
for receiver in receivers:
receiver_counter[receiver] += 1
friends = [user for user, _ in receiver_counter.most_common(target_number_of_friends)]
return friends
#####################################################
# TASK Q1 - Answer the following question:
#
# The mix packet format you worked on uses AES-CTR with an IV set to all zeros.
# Explain whether this is a security concern and justify your answer.
""" TODO: Your answer HERE """
#####################################################
# TASK Q2 - Answer the following question:
#
# What assumptions does your implementation of the Statistical Disclosure Attack makes about the distribution of traffic
# from non-target senders to receivers?
# Is the correctness of the result returned dependent on this background distribution?
""" TODO: Your answer HERE """