-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_X25519_Ed25519.py
388 lines (317 loc) · 16.6 KB
/
test_X25519_Ed25519.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
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
385
386
387
# This file is part of aiootp:
# a high-level async cryptographic anonymity library to scale, simplify,
# & automate privacy best practices for secure data & identity processing,
# communication, & storage.
#
# Licensed under the AGPLv3: https://www.gnu.org/licenses/agpl-3.0.html
# Copyright © 2019-2021 Gonzo Investigative Journalism Agency, LLC
# <gonzo.development@protonmail.ch>
# © 2019-2024 Ricchi (Richard) Machado <rmlibre@riseup.net>
# All rights reserved.
#
from test_initialization import *
OTHER_TYPE_MAPPING = {Ed25519.__name__: X25519, X25519.__name__: Ed25519}
async def basic_async_tests(tested_class):
secret_key_a = await tested_class().agenerate()
problem = (
"An invalid type was allowed to be imported as a secret key."
)
class_not_being_tested = OTHER_TYPE_MAPPING[tested_class.__name__]
some_secret_key = class_not_being_tested().generate()
async with Ignore(TypeError, if_else=violation(problem)):
await tested_class().aimport_secret_key(some_secret_key.secret_key)
async with Ignore(TypeError, if_else=violation(problem)):
await tested_class().aimport_secret_key(some_secret_key.public_key)
problem = (
"An invalid type was allowed to be imported as a public key."
)
some_public_key = class_not_being_tested().import_public_key(some_secret_key.public_key)
async with Ignore(TypeError, if_else=violation(problem)):
await tested_class().aimport_secret_key(some_public_key.public_key)
# Testing equality of async constructors
key_a_from_secret_object = await tested_class().aimport_secret_key(secret_key_a.secret_key)
key_a_from_secret_bytes = await tested_class().aimport_secret_key(secret_key_a.secret_bytes)
key_a_from_public_object = await tested_class().aimport_public_key(secret_key_a.public_key)
key_a_from_public_bytes = await tested_class().aimport_public_key(secret_key_a.public_bytes)
problem = (
"A falsey value secret key import didn't fail."
)
async with Ignore(ValueError, if_else=violation(problem)):
await tested_class().aimport_secret_key(b"")
problem = (
"A non-key type secret key import didn't fail."
)
for non_key_type in (None, "", 0):
async with Ignore(TypeError, if_else=violation(problem)):
await tested_class().aimport_secret_key(non_key_type)
problem = (
"An invalid length secret key import didn't fail."
)
for invalid_length in (1, 16, 31, 33, 48, 64):
async with Ignore(ValueError, if_else=violation(problem)):
await tested_class().aimport_secret_key(token_bytes(invalid_length))
problem = (
"A falsey value public key import didn't fail."
)
async with Ignore(ValueError, if_else=violation(problem)):
await tested_class().aimport_public_key(b"")
problem = (
"A non-key type public key import didn't fail."
)
for falsey_value in (None, "", 0):
async with Ignore(TypeError, if_else=violation(problem)):
await tested_class().aimport_public_key(falsey_value)
problem = (
"An invalid length public key import didn't fail."
)
for invalid_length in (1, 16, 31, 33, 48, 64):
async with Ignore(ValueError, if_else=violation(problem)):
await tested_class().aimport_public_key(token_bytes(invalid_length))
assert len(secret_key_a.public_bytes) == 32
assert len(secret_key_a.secret_bytes) == 32
assert type(key_a_from_public_object.public_key) == type(key_a_from_secret_object.public_key)
assert type(key_a_from_public_object.public_bytes) is bytes
assert type(key_a_from_public_bytes.public_bytes) is bytes
assert key_a_from_public_object.public_bytes == key_a_from_secret_object.public_bytes
assert key_a_from_public_bytes.public_bytes == key_a_from_secret_bytes.public_bytes
assert secret_key_a.secret_bytes == key_a_from_secret_object.secret_bytes
assert secret_key_a.secret_bytes == key_a_from_secret_bytes.secret_bytes
assert secret_key_a.public_bytes == key_a_from_secret_object.public_bytes
assert secret_key_a.public_bytes == key_a_from_secret_bytes.public_bytes
assert secret_key_a.secret_bytes != key_a_from_secret_object.public_bytes
assert not key_a_from_public_object.has_secret_key()
assert not key_a_from_public_bytes.has_secret_key()
assert key_a_from_public_object.has_public_key()
assert key_a_from_public_bytes.has_public_key()
assert secret_key_a.has_secret_key()
assert secret_key_a.has_public_key()
problem = (
"Async public key import was allowed when an instance was "
"already initialized with a key."
)
async with Ignore(PermissionError, if_else=violation(problem)):
await secret_key_a.aimport_public_key(secret_key_a.public_bytes)
problem = (
"Async secret key import was allowed when an instance was "
"already initialized with a key."
)
async with Ignore(PermissionError, if_else=violation(problem)):
await secret_key_a.aimport_secret_key(secret_key_a.secret_bytes)
def basic_sync_tests(tested_class):
secret_key_b = tested_class().generate()
# Testing equality of sync constructors
key_b_from_secret_object = tested_class().import_secret_key(secret_key_b.secret_key)
key_b_from_secret_bytes = tested_class().import_secret_key(secret_key_b.secret_bytes)
key_b_from_public_object = tested_class().import_public_key(secret_key_b.public_key)
key_b_from_public_bytes = tested_class().import_public_key(secret_key_b.public_bytes)
problem = (
"A falsey value secret key import didn't fail."
)
with Ignore(ValueError, if_else=violation(problem)):
tested_class().import_secret_key(b"")
problem = (
"A non-key type secret key import didn't fail."
)
for non_key_type in (None, "", 0):
with Ignore(TypeError, if_else=violation(problem)):
tested_class().import_secret_key(non_key_type)
problem = (
"An invalid length secret key import didn't fail."
)
for invalid_length in (1, 16, 31, 33, 48, 64):
with Ignore(ValueError, if_else=violation(problem)):
tested_class().import_secret_key(token_bytes(invalid_length))
problem = (
"A falsey value public key import didn't fail."
)
with Ignore(ValueError, if_else=violation(problem)):
tested_class().import_public_key(b"")
problem = (
"A non-key type public key import didn't fail."
)
for falsey_value in (None, "", 0):
with Ignore(TypeError, if_else=violation(problem)):
tested_class().import_public_key(falsey_value)
problem = (
"An invalid length public key import didn't fail."
)
for invalid_length in (1, 16, 31, 33, 48, 64):
with Ignore(ValueError, if_else=violation(problem)):
tested_class().import_public_key(token_bytes(invalid_length))
assert len(secret_key_b.public_bytes) == 32
assert len(secret_key_b.secret_bytes) == 32
assert type(key_b_from_public_object.public_key) == type(key_b_from_secret_object.public_key)
assert type(key_b_from_public_object.public_bytes) is bytes
assert type(key_b_from_public_bytes.public_bytes) is bytes
assert key_b_from_public_object.public_bytes == key_b_from_secret_object.public_bytes
assert key_b_from_public_bytes.public_bytes == key_b_from_secret_bytes.public_bytes
assert secret_key_b.secret_bytes == key_b_from_secret_object.secret_bytes
assert secret_key_b.secret_bytes == key_b_from_secret_bytes.secret_bytes
assert secret_key_b.public_bytes == key_b_from_secret_object.public_bytes
assert secret_key_b.public_bytes == key_b_from_secret_bytes.public_bytes
assert secret_key_b.secret_bytes != key_b_from_secret_object.public_bytes
assert not key_b_from_public_object.has_secret_key()
assert not key_b_from_public_bytes.has_secret_key()
assert key_b_from_public_object.has_public_key()
assert key_b_from_public_bytes.has_public_key()
assert secret_key_b.has_secret_key()
assert secret_key_b.has_public_key()
problem = (
"Sync public key import was allowed when an instance was "
"already initialized with a key."
)
with Ignore(PermissionError, if_else=violation(problem)):
secret_key_b.import_public_key(secret_key_b.public_bytes)
problem = (
"Sync secret key import was allowed when an instance was "
"already initialized with a key."
)
with Ignore(PermissionError, if_else=violation(problem)):
secret_key_b.import_secret_key(secret_key_b.secret_bytes)
async def test_X25519(database, async_database):
# Test class constructors
secret_key_a = await X25519().agenerate()
secret_key_b = X25519().generate()
await basic_async_tests(X25519)
basic_sync_tests(X25519)
# exchange methods create shared keys from different instances'
# public bytes
shared_key_a = secret_key_a.exchange(secret_key_b.public_bytes)
shared_key_b = secret_key_b.exchange(secret_key_a.public_bytes)
assert bytes_are_equal(shared_key_a, shared_key_b)
shared_key_a = await secret_key_a.aexchange(secret_key_b.public_bytes)
shared_key_b = await secret_key_b.aexchange(secret_key_a.public_bytes)
assert await abytes_are_equal(shared_key_a, shared_key_b)
# exchange methods create shared keys from different instances'
# public key object
shared_key_a = secret_key_a.exchange(secret_key_b.public_key)
shared_key_b = secret_key_b.exchange(secret_key_a.public_key)
assert bytes_are_equal(shared_key_a, shared_key_b)
shared_key_a = await secret_key_a.aexchange(secret_key_b.public_key)
shared_key_b = await secret_key_b.aexchange(secret_key_a.public_key)
assert await abytes_are_equal(shared_key_a, shared_key_b)
# exchange methods create shared keys from different instances'
# secret key object
problem = (
"A secret key was used in an interface expecting a public key."
)
with Ignore(TypeError, if_else=violation(problem)):
secret_key_a.exchange(secret_key_b.secret_key)
with Ignore(TypeError, if_else=violation(problem)):
secret_key_b.exchange(secret_key_a.secret_key)
with Ignore(TypeError, if_else=violation(problem)):
await secret_key_a.aexchange(secret_key_b.secret_key)
with Ignore(TypeError, if_else=violation(problem)):
await secret_key_b.aexchange(secret_key_a.secret_key)
class TestDiffieHellmanProtocols:
async def test_async_double_diffie_hellman(self) -> None:
server_key = await X25519().agenerate()
client = X25519.dh2_client()
server = server_key.dh2_server()
client_ephemeral_key = await client.asend(server_key.public_bytes)
server_kdf = await server.areceive(client_ephemeral_key)
server_ephemeral_key = await server.asend()
client_kdf = await client.areceive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_sync_double_diffie_hellman(self) -> None:
server_key = X25519().generate()
client = X25519.dh2_client()
server = server_key.dh2_server()
client_ephemeral_key = client.send(server_key.public_bytes)
server_kdf = server.receive(client_ephemeral_key)
server_ephemeral_key = server.send()
client_kdf = client.receive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_async_triple_diffie_hellman(self) -> None:
client_key = await X25519().agenerate()
server_key = await X25519().agenerate()
client = client_key.dh3_client()
server = server_key.dh3_server()
client_identity_key, client_ephemeral_key = await client.asend(server_key.public_bytes)
server_kdf = await server.areceive(client_identity_key, client_ephemeral_key)
server_ephemeral_key = await server.asend()
client_kdf = await client.areceive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_sync_triple_diffie_hellman(self) -> None:
client_key = X25519().generate()
server_key = X25519().generate()
client = client_key.dh3_client()
server = server_key.dh3_server()
client_identity_key, client_ephemeral_key = client.send(server_key.public_bytes)
server_kdf = server.receive(client_identity_key, client_ephemeral_key)
server_ephemeral_key = server.send()
client_kdf = client.receive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_double_diffie_hellman_interop(self) -> None:
server_key = await X25519().agenerate()
client = X25519.dh2_client()
server = server_key.dh2_server()
client_ephemeral_key = client.send(server_key.public_bytes)
server_kdf = await server.areceive(client_ephemeral_key)
server_ephemeral_key = await server.asend()
client_kdf = client.receive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
client = X25519.dh2_client()
server = server_key.dh2_server()
client_ephemeral_key = await client.asend(server_key.public_bytes)
server_kdf = server.receive(client_ephemeral_key)
server_ephemeral_key = server.send()
client_kdf = await client.areceive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_triple_diffie_hellman_interop(self) -> None:
client_key = await X25519().agenerate()
server_key = await X25519().agenerate()
client = client_key.dh3_client()
server = server_key.dh3_server()
client_identity_key, client_ephemeral_key = client.send(server_key.public_bytes)
server_kdf = await server.areceive(client_identity_key, client_ephemeral_key)
server_ephemeral_key = await server.asend()
client_kdf = client.receive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
client = client_key.dh3_client()
server = server_key.dh3_server()
client_identity_key, client_ephemeral_key = await client.asend(server_key.public_bytes)
server_kdf = server.receive(client_identity_key, client_ephemeral_key)
server_ephemeral_key = server.send()
client_kdf = await client.areceive(server_ephemeral_key)
assert client_kdf.sha3_512() == server_kdf.sha3_512()
async def test_Ed25519(database, async_database):
# Test class constructors
secret_key_a = await Ed25519().agenerate()
secret_key_b = Ed25519().generate()
await basic_async_tests(Ed25519)
basic_sync_tests(Ed25519)
async_signature = await secret_key_a.asign(plaintext_bytes)
signature = secret_key_b.sign(plaintext_bytes)
arbitrary_verifier = Ed25519().generate()
key_a_verifier = await Ed25519().aimport_public_key(secret_key_a.public_bytes)
key_b_verifier = Ed25519().import_public_key(secret_key_b.public_bytes)
# async verification succeeds when supplied a correct signature & data
await key_a_verifier.averify(async_signature, plaintext_bytes)
await arbitrary_verifier.averify(async_signature, plaintext_bytes, public_key=secret_key_a.public_bytes)
# async verification succeeds when supplied an incorrect signature & data
problem = (
"Async verification succeeded for an invalid signature."
)
async with Ignore(Ed25519.InvalidSignature, if_else=violation(problem)):
await key_a_verifier.averify(token_bytes(len(async_signature)), plaintext_bytes)
problem = (
"Async verification succeeded for an invalid signature."
)
async with Ignore(Ed25519.InvalidSignature, if_else=violation(problem)):
await arbitrary_verifier.averify(token_bytes(len(async_signature)), plaintext_bytes, public_key=secret_key_a.public_bytes)
# sync verification succeeds when supplied a correct signature & data
key_b_verifier.verify(signature, plaintext_bytes)
arbitrary_verifier.verify(signature, plaintext_bytes, public_key=secret_key_b.public_bytes)
# sync verification succeeds when supplied an incorrect signature & data
problem = (
"Verification succeeded for an invalid signature."
)
with Ignore(Ed25519.InvalidSignature, if_else=violation(problem)):
key_a_verifier.verify(token_bytes(len(async_signature)), plaintext_bytes)
problem = (
"Verification succeeded for an invalid signature."
)
with Ignore(Ed25519.InvalidSignature, if_else=violation(problem)):
arbitrary_verifier.verify(token_bytes(len(async_signature)), plaintext_bytes, public_key=secret_key_a.public_bytes)
__all__ = sorted({n for n in globals() if n.lower().startswith("test")})