def test_unapply_whole_block_padding(self): padded = b"YELLOW\x03\x03\x03" expected_bytes = b"YELLOW" actual_bytes = PKCS7Padding.unapply(padded) self.assertEqual(expected_bytes, actual_bytes)
def test_unapply_cryptopals_case(self): padded = b"YELLOW SUBMARINE\x04\x04\x04\x04" expected_bytes = b"YELLOW SUBMARINE" actual_bytes = PKCS7Padding.unapply(padded) self.assertEqual(expected_bytes, actual_bytes)
def test_unapply_all_padding_returns_empty(self): padded = b"\x04\x04\x04\x04" expected_bytes = b"" actual_bytes = PKCS7Padding.unapply(padded) self.assertEqual(expected_bytes, actual_bytes)
def main(): """ Already implemented `PKCS7Padding.unapply()` in set2.challenge09 Display that it solves the challenge """ b = b"ICE ICE BABY\x04\x04\x04\x04" print("PKCS7Padding.unapply(%s):" % b) print(PKCS7Padding.unapply(b)) b = b"ICE ICE BABY\x05\x05\x05\x05" print("PKCS7Padding.unapply(%s):" % b) try: PKCS7Padding.unapply(b) except (InvalidPaddingException): print("Exception raised") b = b"ICE ICE BABY\x01\x02\x03\x04" print("PKCS7Padding.unapply(%s):" % b) try: PKCS7Padding.unapply(b) except (InvalidPaddingException): print("Exception raised")
def decrypt_profile(encrypted: bytes) -> str: """ params: encrypted: profile encrypted using `encrypted_profile()` returns: encoded profile """ cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB) ecb = ECBMode(16, cipher.encrypt, cipher.decrypt) padded = ecb.decrypt(encrypted) return PKCS7Padding.unapply(padded).decode()
def valid_padding(encrypted: bytes, iv: bytes) -> bool: """ params: encrypted: encrypted message iv: IV used to encrypt `encrypted` returns: True if decryption of `encrypted` has valid padding False otherwise """ blksize = len(CONSISTENT_KEY) cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB) cbc = CBCMode(blksize=blksize, encrypt_blk=cipher.encrypt, decrypt_blk=cipher.decrypt, iv=iv) decrypted = cbc.decrypt(encrypted) try: PKCS7Padding.unapply(decrypted) return True except InvalidPaddingException: return False
def is_admin(encrypted: bytes, blksize: int = 16) -> bool: """ params: encrypted: message encrypted using `encryption_oracle()` blksize: blocksize used by `encryption_oracle()` returns: True if decrypted message contains ";admin=true;", False otherwise """ cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB) cbc = CBCMode(blksize=blksize, encrypt_blk=cipher.encrypt, decrypt_blk=cipher.decrypt, iv=CONSISTENT_IV) padded = cbc.decrypt(encrypted) decrypted = PKCS7Padding.unapply(padded) return b";admin=true;" in decrypted
def break_cbc(encrypted: bytes, iv: bytes, padding_oracle: Callable[[bytes, bytes], bool]) \ -> bytes: """ params: encrypted: encrypted message from `encryption_oracle()` iv: IV used for encryption from `encryption_oracle()` padding_oracle: `valid_padding()` returns: decrypted bytes for `encrypted` """ decrypted = b'' prev_blk = iv for curr_blk in blocks(encrypted, 16): decrypted += break_cbc_single_blk(prev_blk, curr_blk, padding_oracle) prev_blk = curr_blk return PKCS7Padding.unapply(decrypted)
def validate_ascii(encrypted: bytes) -> bytes: """ params: encrypted: message encrypted using `aes_cbc()` returns: decrypted bytes if they contain extended ascii characters, None otherwise """ cipher = AES.new(key, AES.MODE_ECB) cbc = CBCMode(blksize=blksize, encrypt_blk=cipher.encrypt, decrypt_blk=cipher.decrypt, iv=key) padded = cbc.decrypt(encrypted) decrypted = PKCS7Padding.unapply(padded) for c in decrypted: if c >= 128: return decrypted return None
def test_unapply_padding_value_does_not_match_number_of_pads(self): padded = b"ICE ICE BABY\x05\x05\x05\x05" with self.assertRaises(InvalidPaddingException): PKCS7Padding.unapply(padded)
def test_unapply_padding_value_incosistent(self): padded = b"ICE ICE BABY\x01\x02\x03\x04" with self.assertRaises(InvalidPaddingException): PKCS7Padding.unapply(padded)
def test_unapply_padding_value_larger_than_valid_raises(self): padded = b"YELLOW\x16\x16\x16" with self.assertRaises(InvalidPaddingException): PKCS7Padding.unapply(padded)
def test_unapply_padding_ending_with_zero_raises(self): padded = b"YELLOW\x00" with self.assertRaises(InvalidPaddingException): PKCS7Padding.unapply(padded)