Created
July 18, 2016 05:44
-
-
Save chris-wood/a65f4d816be8dec756fce6178f33b49d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xor(x, y): | |
''' XOR two lists of bytes. | |
''' | |
return map(lambda (xx, yy) : xx ^ yy, zip(x, y)) | |
def random_vector(n): | |
''' Generate a random byte list with n elements. | |
''' | |
return [random.randint(0, 255) for i in range(n)] | |
def encrypt_cbc(key, iv, vectors): | |
''' "Encrypt" a list of plaintext blocks using the given key and IV in CBC mode. | |
We don't really encrypt here... we just XOR the plaintext with the key. This has | |
no impact on the PO attack. | |
''' | |
# Pad, if necessary | |
if len(vectors[-1]) < 16: | |
vectors[-1] = pad_pkcs7(vectors[-1]) | |
else: | |
vectors.append(pad_pkcs7([])) | |
result = [] | |
state = iv | |
for i, v in enumerate(vectors): | |
input_block = xor(state, v) | |
state = xor(key, input_block) # replacement for AES | |
result.append(state) | |
return result | |
def decrypt_cbc(key, iv, vectors): | |
''' Invert our "encryption" in CBC mode. | |
''' | |
pt = [] | |
state = iv | |
for i, v in enumerate(vectors): | |
next_state = v | |
output_block = xor(key, v) # replacement for AESi | |
xor_result = xor(state, output_block) | |
pt.append(xor_result) | |
state = next_state | |
if not is_valid_pad_pkcs7(pt[-1]): | |
raise Exception("Invalid padding") | |
return pt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment