-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiffie_hellman_test.py
55 lines (40 loc) · 1.71 KB
/
diffie_hellman_test.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
import unittest
from ecc_impl import ECC
from diffie_hellman import DiffieHellman
class DiffieHellmanTestCase(unittest.TestCase):
def test_using_p_192(self):
"""
test diffie hellman key agreement using curve P-192
takes about 5 seconds to complete due to size of keys
"""
point_g = (
0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, # Gx
0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811) # Gy
curve = ECC(
-3, # a
0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1, # b
6277101735386680763835789423207666416083908700390324961279, # p
point_g)
dh = DiffieHellman(curve, point_g)
private_key1 = 7919
public_key1 = dh.generate_public_key(private_key1)
private_key2 = 8387
public_key2 = dh.generate_public_key(private_key2)
private_key3 = 10657
public_key3 = dh.generate_public_key(private_key3)
secret_pr1_pub2 = dh.secret_key(private_key1, public_key2)
secret_pr2_pub1 = dh.secret_key(private_key2, public_key1)
secret_pr1_pub3 = dh.secret_key(private_key1, public_key3)
secret_pr3_pub1 = dh.secret_key(private_key3, public_key1)
secret_pr2_pub3 = dh.secret_key(private_key2, public_key3)
secret_pr3_pub2 = dh.secret_key(private_key3, public_key2)
# Check if each key pair receives the same shared secret key
self.assertTrue(secret_pr1_pub2 == secret_pr2_pub1)
self.assertTrue(secret_pr1_pub3 == secret_pr3_pub1)
self.assertTrue(secret_pr2_pub3 == secret_pr3_pub2)
# Non-key pairs should not receive the same shared secret key
self.assertFalse(secret_pr1_pub3 == secret_pr1_pub2)
self.assertFalse(secret_pr2_pub1 == secret_pr2_pub3)
self.assertFalse(secret_pr3_pub2 == secret_pr3_pub1)
if __name__ == "__main__":
unittest.main()