@@ -72,16 +72,19 @@ def padding_oracle(cipher: bytes,
72
72
oracle (function) a function: oracle(cipher: bytes) -> bool
73
73
num_threads (int) how many oracle functions will be run in parallel (default: 1)
74
74
log_level (int) log level (default: logging.INFO)
75
- null (bytes) the null byte if the (default: b' ')
75
+ null (bytes) the default byte when plaintext are not set (default: b' ')
76
76
77
77
Returns:
78
78
plaintext (bytes) the decrypted plaintext
79
79
'''
80
80
81
- # Check the oracle function
81
+ # Check args
82
82
assert callable (oracle ), 'the oracle function should be callable'
83
83
assert oracle .__code__ .co_argcount == 1 , 'expect oracle function with only 1 argument'
84
+ assert isinstance (cipher , bytes ), 'cipher should have type bytes'
85
+ assert isinstance (block_size , int ), 'block_size should have type int'
84
86
assert len (cipher ) % block_size == 0 , 'cipher length should be multiple of block size'
87
+ assert 1 <= num_threads <= 1000 , 'num_threads should be in [1, 1000]'
85
88
assert isinstance (null , bytes ), 'expect null with type bytes'
86
89
assert len (null ) == 1 , 'null byte should have length of 1'
87
90
@@ -97,7 +100,7 @@ def _oracle_wrapper(i: int, j: int, cipher: bytes):
97
100
logger .debug ('error details at block[{}][{}]: ' , i , j , traceback .format_exc ())
98
101
return False
99
102
100
- # The plaintext bytes list to save the decrypted data
103
+ # The plaintext bytes list to store the decrypted data
101
104
plaintext = [null ] * (len (cipher ) - block_size )
102
105
103
106
# Update the decrypted plaintext list
@@ -145,14 +148,12 @@ def _block_decrypt_task(i, prev: bytes, block: bytes):
145
148
_update_plaintext (i * block_size - j , bytes ([p ]))
146
149
147
150
for n in range (j ):
148
- guess_list [- n - 1 ] ^= j
149
- guess_list [- n - 1 ] ^= j + 1
151
+ guess_list [- n - 1 ] ^= j ^ (j + 1 )
150
152
151
153
blocks = []
152
154
153
155
for i in range (0 , len (cipher ), block_size ):
154
- j = i + block_size
155
- blocks .append (cipher [i :j ])
156
+ blocks .append (cipher [i :i + block_size ])
156
157
157
158
logger .debug ('blocks: {}' .format (blocks ))
158
159
0 commit comments