-
Notifications
You must be signed in to change notification settings - Fork 4
/
p35.py
50 lines (37 loc) · 1.63 KB
/
p35.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
from hashlib import sha1
from os import urandom
from main import Solution
from p10 import aes_cbc_decrypt
from p11 import aes_cbc_encrypt
from p13 import validate_pkcs7
from p33 import DiffieHellman
def p35() -> str:
p = DiffieHellman.default_p
for (g, sk) in [(1, 1), (p, 0), (p - 1, p - 1)]:
alice = DiffieHellman(g=g)
bob = DiffieHellman(g=g)
alice.derive_shared_secret(bob.public)
bob.derive_shared_secret(alice.public)
a_msg = b'When does this ever happen?'
a_iv = urandom(16)
a_key = sha1(str(alice.shared).encode()).digest()[:16]
a_sends = aes_cbc_encrypt(a_msg, a_key, a_iv), a_iv
e_key = sha1(str(sk).encode()).digest()[:16]
try:
e_msg = validate_pkcs7(aes_cbc_decrypt(a_sends[0], e_key, a_iv))
except ValueError:
sk = pow(p-1, 2, p)
e_key = sha1(str(sk).encode()).digest()[:16]
e_msg = validate_pkcs7(aes_cbc_decrypt(a_sends[0], e_key, a_iv))
if e_msg != a_msg:
return 'Intercepted Traffic Incorrectly Decrypted'
b_iv = urandom(16)
b_key = sha1(str(bob.shared).encode()).digest()[:16]
b_msg = validate_pkcs7(aes_cbc_decrypt(a_sends[0], b_key, a_iv))
b_sends = aes_cbc_encrypt(b_msg, b_key, b_iv), b_iv
e_msg = validate_pkcs7(aes_cbc_decrypt(b_sends[0], e_key, b_iv))
if e_msg != b_msg:
return 'Intercepted Traffic Incorrectly Decrypted'
return 'All Traffic Intercepted And Decrypted!'
def main() -> Solution:
return Solution('35: Implement DH with negotiated groups, and break with malicious "g" parameters', p35)