Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/ctr/crime.py
2589 views
1
def attack(encrypt_oracle, known_prefix, padding_byte):
2
"""
3
Recovers a secret using the CRIME attack (CTR version).
4
:param encrypt_oracle: the encryption oracle
5
:param known_prefix: a known prefix of the secret to recover
6
:param padding_byte: a byte which is never used in the plaintext
7
:return: the secret
8
"""
9
known_prefix = bytearray(known_prefix)
10
padding_bytes = bytes([padding_byte])
11
while True:
12
for i in range(256):
13
# Don't try the padding byte.
14
if i == padding_byte:
15
continue
16
17
l1 = len(encrypt_oracle(padding_bytes + known_prefix + bytes([i]) + padding_bytes + padding_bytes))
18
l2 = len(encrypt_oracle(padding_bytes + known_prefix + padding_bytes + bytes([i]) + padding_bytes))
19
if l1 < l2:
20
known_prefix.append(i)
21
break
22
else:
23
return known_prefix
24
25