-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
68 lines (50 loc) · 2.06 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
from rsa_signature import RSASignature
from user import User
from session_end_point import SessionEndPoint
from utils import convert_to_bytes
from certificate_authority import CertificateAuthority
alice = User()
bob = User()
CA = CertificateAuthority()
CA.set_private_key_and_public_key(*RSASignature.generate_rsa())
alice.set_private_key_and_public_key(*RSASignature.generate_rsa())
bob.set_private_key_and_public_key(*RSASignature.generate_rsa())
certificate_public_key_alice = CA.issue_certificate(alice.public_key)
certificate_public_key_bob = CA.issue_certificate(bob.public_key)
# Bob checks the certificate of Alice
if not RSASignature.verify(
CA.public_key,
convert_to_bytes(alice.public_key),
certificate_public_key_alice):
print('Alice\'s public key is not verified')
exit()
# Alice checks the certificate of Bob
if not RSASignature.verify(
CA.public_key,
convert_to_bytes(bob.public_key),
certificate_public_key_bob):
print('Bob\'s public key is not verified')
exit()
alice_end_point = SessionEndPoint()
bob_end_point = SessionEndPoint()
signed_dh_public_key_of_alice = alice.sign_dh_public_key(alice_end_point)
if not RSASignature.verify(
alice.public_key,
convert_to_bytes(alice_end_point.public_key),
signed_dh_public_key_of_alice):
print("Alice's DH public key does not match with the signature.")
exit()
signed_dh_public_key_of_bob = bob.sign_dh_public_key(bob_end_point)
if not RSASignature.verify(
bob.public_key,
convert_to_bytes(bob_end_point.public_key),
signed_dh_public_key_of_bob):
print("Bob's DH public key does not match with the signature.")
exit()
alice_end_point.register_peer_dh_public_key(bob_end_point.public_key)
alice_end_point.start_session()
bob_end_point.register_peer_dh_public_key(alice_end_point.public_key)
bob_end_point.start_session()
print(bob_end_point.shared_key == alice_end_point.shared_key)
iv, msg, mac = alice_end_point.encrypt(b'fsdf, world!')
print(bob_end_point.decrypt(iv, msg, mac))