-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_mixnets_test.py
More file actions
228 lines (173 loc) · 6.09 KB
/
Copy pathlab_mixnets_test.py
File metadata and controls
228 lines (173 loc) · 6.09 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
#####################################################
# COMP0061 Privacy Enhancing Technologies -- Lab on Mix Systems
#
# Basics of Mix networks and Traffic Analysis
#
# Run the tests through:
# $ pytest -v
import sys
import pytest
from os import urandom
from pytest import raises
from Cryptodome.PublicKey import _point
curves = _point._curves
from lab_mixnets import *
#####################################################
# TASK 1 -- Ensure libraries are installed on the system.
# Ensure the lab code can be imported.
@pytest.mark.task1
def test_libs_present():
"""
Check Cryptodome and pytest are imported
"""
assert "Cryptodome" in sys.modules
assert "pytest" in sys.modules
@pytest.mark.task1
def test_code_present():
"""
Check lab_mixnets is imported
"""
assert "lab_mixnets" in sys.modules
#####################################################
# TASK 2 -- Build a 1-hop mix client.
# What is a test fixture?
# http://pytest.org/latest/fixture.html
@pytest.fixture
def encode_Alice_message():
"""
Encode a single message
"""
group = curves["secp224r1"]
o = group.order
g = group.G
private_key = Integer.random_range(min_inclusive=1, max_exclusive=o)
public_key = g * private_key
m1 = mix_client_one_hop(group, public_key, b"Alice", b"Dear Alice,\nHello!\nBob")
return private_key, m1
@pytest.mark.task2
def test_Alice_message_overlong():
"""
Test overlong address or message
"""
group = curves["secp224r1"]
o = group.order
g = group.G
private_key = Integer.random_range(min_inclusive=1, max_exclusive=o)
public_key = g * private_key
with raises(AssertionError):
mix_client_one_hop(group, public_key, urandom(1000), b"Dear Alice,\nHello!\nBob")
with raises(AssertionError):
mix_client_one_hop(group, public_key, b"Alice", urandom(10000))
@pytest.mark.task2
def test_simple_client_part_type(encode_Alice_message):
private_key, Alice_message = encode_Alice_message
# Ensure the client encodes a NamedTuple of type "OneHopMixMessage"
assert isinstance(Alice_message, tuple)
assert len(Alice_message) == 4
assert Alice_message.ec_public_key
assert Alice_message.hmac
assert Alice_message.address
assert Alice_message.message
@pytest.mark.task2
def test_simple_client_decode(encode_Alice_message):
private_key, Alice_message = encode_Alice_message
# Ensure the mix can decode the message correctly
res1 = mix_server_one_hop(private_key, [Alice_message])
assert len(res1) == 1
assert res1[0] == (b"Alice", b"Dear Alice,\nHello!\nBob")
@pytest.mark.task2
def test_simple_client_decode_many():
group = curves["secp224r1"]
o = group.order
g = group.G
private_key = Integer.random_range(min_inclusive=1, max_exclusive=o)
public_key = g * private_key
messages = []
expected = []
for _ in range(100):
m_input = (urandom(256), urandom(1000))
expected += [m_input]
m = mix_client_one_hop(group, public_key, *m_input)
messages += [m]
# Ensure the mix can decode the message correctly
result = mix_server_one_hop(private_key, messages)
assert len(result) == 100
assert result == sorted(expected)
###################################
# TASK 3 -- A multi-hop mix
@pytest.mark.task3
def test_Alice_encode_1_hop():
"""
Test sending a multi-hop message through 1-hop
"""
group = curves["secp224r1"]
o = group.order
g = group.G
private_key = Integer.random_range(min_inclusive=1, max_exclusive=o)
public_key = g * private_key
address = b"Alice"
message = b"Dear Alice,\nHello!\nBob"
m1 = mix_client_n_hop(group, [public_key], address, message)
out = mix_server_n_hop(private_key, [m1], final=True)
assert out == [(address, message)]
@pytest.mark.task3
def test_Alice_encode_3_hop():
"""
Test sending a multi-hop message through 1-hop
"""
group = curves["secp224r1"]
o = group.order
g = group.G
private_keys = [Integer.random_range(min_inclusive=1, max_exclusive=o) for _ in range(3)]
public_keys = [g * private_key for private_key in private_keys]
address = b"Alice"
message = b"Dear Alice,\nHello!\nBob"
m1 = mix_client_n_hop(group, public_keys, address, message)
out = mix_server_n_hop(private_keys[0], [m1])
out = mix_server_n_hop(private_keys[1], out)
out = mix_server_n_hop(private_keys[2], out, final=True)
assert out == [(address, message)]
###########################################
# TASK 4 -- Simple traffic analysis / SDA
import random
@pytest.mark.task4
def test_trace_static():
# A fixed set and number of friends
exceptions = []
for _ in range(100):
try:
trace = generate_trace(100, 10, 1000, [1, 2, 3])
friends = analyze_trace(trace, 3)
assert len(friends) == 3
assert sorted(friends) == [1, 2, 3]
except AssertionError as err:
exceptions.append(err)
if len(exceptions) > 33:
message = f"Failed to correctly identify friends in {len(exceptions)}% test runs"
try:
exception = ExceptionGroup
except NameError:
exception = Exception
raise exception(message, exceptions)
@pytest.mark.task4
def test_trace_variable():
# A random number of friends and random contacts
random.seed('PETS')
exceptions = []
for _ in range(1000):
try:
friend_number = random.choice(range(1, 10))
friends = random.sample(range(100), friend_number)
trace = generate_trace(100, 10, 1000, friends)
TA_friends = analyze_trace(trace, len(friends))
assert len(TA_friends) == len(friends)
assert sorted(TA_friends) == sorted(friends)
except AssertionError as err:
exceptions.append(err)
if len(exceptions) > 333:
message = f"Failed to correctly identify friends in {len(exceptions) / 10}% test runs"
try:
exception = ExceptionGroup
except NameError:
exception = Exception
raise exception(message, exceptions)