-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm46.py
executable file
·51 lines (38 loc) · 1.33 KB
/
m46.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
#!/usr/bin/env python3
"""RSA parity oracle"""
import base64
import m39
class RSAParityOracle:
def __init__(self, size: int = 1024, e: int = 3) -> None:
keypair = m39.keygen(size, e)
self.pubkey = keypair.public
self._private_key = keypair.private
def is_even(self, c: int) -> bool:
"""Parity of plaintext"""
return m39.decrypt_int(c, self._private_key) % 2 == 0
def parity_oracle_attack(c: int, oracle: RSAParityOracle) -> bytes:
"""Binary search to decrypt via parity oracle"""
n = oracle.pubkey.modulus
coefficient = m39.encrypt_int(2, oracle.pubkey)
a, b = 0, 1
# We operate in the interval [0, 1] instead of [0, n] to avoid
# dealing with large floating point division.
for i in range(1, n.bit_length() + 1):
c = (coefficient * c) % n
interval_width = b - a
a *= 2
b *= 2
if not oracle.is_even(c):
a += interval_width
else:
b -= interval_width
print(m39.to_bytes(n * b // 2 ** i))
return m39.to_bytes(n * b // 2 ** n.bit_length())
def main() -> None:
with open("data/46.txt") as fd:
m = base64.b64decode(fd.read())
oracle = RSAParityOracle()
c = m39.encrypt(m, oracle.pubkey)
assert m == parity_oracle_attack(c, oracle)
if __name__ == "__main__":
main()