|
| 1 | +import math |
| 2 | +import os |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +from cryptopals.block import cbc_padding_oracle |
| 6 | + |
| 7 | + |
| 8 | +def flip_nth_bit(text: bytes, num: int): |
| 9 | + modified = text[:num] + bytes([text[num] ^ 1]) + text[num + 1 :] |
| 10 | + return modified |
| 11 | + |
| 12 | + |
| 13 | +class Solver(object): |
| 14 | + def __init__(self, block_size: int, iv: bytes, key: bytes, ciphertext: bytes): |
| 15 | + self.block_size = block_size |
| 16 | + self.ciphertext = ciphertext |
| 17 | + |
| 18 | + # We won't look at these, these would be used by the server |
| 19 | + self.iv = iv |
| 20 | + self.key = key |
| 21 | + |
| 22 | + self.reconstructed_bytes = [] # type: List |
| 23 | + self.debug = False # type: bool |
| 24 | + |
| 25 | + def _decrypt_single_block(self, block_num): |
| 26 | + previous_block_ciphertext = self.ciphertext[ |
| 27 | + (block_num - 2) |
| 28 | + * self.block_size : (block_num - 1) |
| 29 | + * self.block_size |
| 30 | + ] |
| 31 | + this_block_ciphertext = self.ciphertext[ |
| 32 | + (block_num - 1) * self.block_size : block_num * self.block_size |
| 33 | + ] |
| 34 | + |
| 35 | + block_cipher_outputs_prior_to_xor = [] |
| 36 | + |
| 37 | + for byte_num in range(self.block_size, 0, -1): |
| 38 | + |
| 39 | + valid_padding_byte = self.block_size - byte_num + 1 |
| 40 | + found_a_byte = False |
| 41 | + |
| 42 | + for num in range(255): |
| 43 | + num_of_prefix_bytes = byte_num - 1 |
| 44 | + |
| 45 | + test_block = b'0' * num_of_prefix_bytes + bytes( |
| 46 | + [num] |
| 47 | + ) |
| 48 | + |
| 49 | + # Now add the byte that will be padding for the bytes we've already |
| 50 | + # reconstructed. |
| 51 | + for block_cipher_output_byte in block_cipher_outputs_prior_to_xor: |
| 52 | + padding_byte = block_cipher_output_byte ^ valid_padding_byte |
| 53 | + test_block = test_block + bytes([padding_byte]) |
| 54 | + |
| 55 | + full_test_ciphertext = test_block + this_block_ciphertext |
| 56 | + |
| 57 | + # Note that we're passing in the key and IV but we're only |
| 58 | + # returning whether or not the padding is valid, not the |
| 59 | + # decrypted content. |
| 60 | + valid_padding = cbc_padding_oracle( |
| 61 | + self.key, full_test_ciphertext, self.iv |
| 62 | + ) |
| 63 | + |
| 64 | + if not valid_padding: |
| 65 | + continue |
| 66 | + |
| 67 | + block_cipher_output_byte = num ^ valid_padding_byte |
| 68 | + plaintext_byte = ( |
| 69 | + previous_block_ciphertext[num_of_prefix_bytes] |
| 70 | + ^ block_cipher_output_byte |
| 71 | + ) |
| 72 | + |
| 73 | + # Handling the padding block by ensuring we get the choice of |
| 74 | + # num correct (e.g. if the second to last byte in the block |
| 75 | + # happens to be 2, we will get valid padding for _two_ possible |
| 76 | + # bytes in the last byte position). |
| 77 | + byte_num_to_edit = self.block_size + byte_num - 2 |
| 78 | + degeneracy_ciphertext = flip_nth_bit( |
| 79 | + full_test_ciphertext, byte_num_to_edit - self.block_size |
| 80 | + ) |
| 81 | + if cbc_padding_oracle(self.key, degeneracy_ciphertext, self.iv): |
| 82 | + pass |
| 83 | + else: |
| 84 | + continue |
| 85 | + |
| 86 | + if self.debug: # Fail when we get a byte wrong before going any further |
| 87 | + try: |
| 88 | + expected_byte = self.plaintext_bytes[ |
| 89 | + self.block_size * (block_num - 1) + byte_num - 1] |
| 90 | + assert plaintext_byte == expected_byte |
| 91 | + except AssertionError: |
| 92 | + breakpoint() |
| 93 | + |
| 94 | + found_a_byte = True |
| 95 | + |
| 96 | + # Save the reconstructed byte and the output of the block cipher |
| 97 | + # prior to XOR as we'll need it for the next byte to reconstruct. |
| 98 | + block_cipher_outputs_prior_to_xor = [ |
| 99 | + block_cipher_output_byte |
| 100 | + ] + block_cipher_outputs_prior_to_xor |
| 101 | + self.reconstructed_bytes = [ |
| 102 | + plaintext_byte |
| 103 | + ] + self.reconstructed_bytes |
| 104 | + |
| 105 | + break |
| 106 | + |
| 107 | + if self.debug and not found_a_byte: |
| 108 | + breakpoint() |
| 109 | + elif not found_a_byte: |
| 110 | + raise Exception("Did not reconstruct a byte this block!") |
| 111 | + |
| 112 | + def run(self, plaintext_bytes) -> List: |
| 113 | + self.plaintext_bytes = plaintext_bytes |
| 114 | + self.num_blocks = math.ceil(len(plaintext_bytes) / self.block_size) |
| 115 | + |
| 116 | + # We start at rightmost block and move right to left. |
| 117 | + for block_num in range(self.num_blocks, 0, -1): |
| 118 | + if block_num == 1: |
| 119 | + # We don't have the IV to decrypt further, so we stop here. |
| 120 | + break |
| 121 | + |
| 122 | + self._decrypt_single_block(block_num) |
| 123 | + |
| 124 | + return self.reconstructed_bytes |
0 commit comments