Beispiel #1
0
    def encryption_oracle(b: bytes) -> bytes:
        """
        params:
            b: bytes to encrypt
        returns:
            `b` encrypted using AES-128 with a random key using either
            ECB mode or CBC mode randomly (one in two chance)
            also prepends 5-10 random bytes and appends 5-10 random bytes to
            `b` before encryption
            if CBC mode is used random bytes are used for the IV
        """
        prefix_size = random.randint(5, 10)
        prefix = rand_bytes_gen(prefix_size)
        suffix_size = random.randint(5, 10)
        suffix = rand_bytes_gen(suffix_size)

        plain = PKCS7Padding.apply(prefix+b+suffix, blksize)
        key = rand_bytes_gen(blksize)
        cipher = AES.new(key, AES.MODE_ECB)

        if use_ecb:
            # print("ECB")  # for manual verification
            ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)
            return ecb.encrypt(plain)

        iv = rand_bytes_gen(blksize)
        cbc = CBCMode(
            blksize=blksize,
            encrypt_blk=cipher.encrypt,
            decrypt_blk=cipher.decrypt,
            iv=iv
        )

        # print("CBC")
        return cbc.encrypt(plain)
Beispiel #2
0
    def test_encrypt_bytes_of_proper_length(self):
        ecb = ECBMode(2, self.mock_fun, self.mock_fun)
        b = b"\x00\x01\x00\x01"
        expected_bytes = b"\x00\x00\x00\x00"

        actual_bytes = ecb.encrypt(b)

        self.assertEqual(expected_bytes, actual_bytes)
Beispiel #3
0
    def test_encrypt_decrypt_integration_case(self):
        key = b"YELLOW SUBMARINE"
        cipher = AES.new(key, AES.MODE_ECB)
        ecb = ECBMode(16, cipher.encrypt, cipher.decrypt)
        plaintext = (b"Lorem ipsum dolo"
                     b"r sit amet, cons"
                     b"ectetur adipisci"
                     b"ng elitAAAAAAAAA")

        encrypted = ecb.encrypt(plaintext)
        decrypted = ecb.decrypt(encrypted)

        self.assertEqual(plaintext, decrypted)
Beispiel #4
0
def encrypt_profile(profile: str) -> bytes:
    """
    params:
        profile: encoded profile
    returns:
        `profile` encrypted using AES-128 ECB mode with a consistent key
    """
    blksize = 16
    plain = PKCS7Padding.apply(profile.encode(), blksize)
    cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB)
    ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)

    return ecb.encrypt(plain)
Beispiel #5
0
    def encryption_oracle(b: bytes) -> bytes:
        """
        params:
            b: bytes to encrypt
        returns:
            `b` encrypted using AES-128-ECB
            appends `unknownstr` to `b` before encrypting
        """
        plain = PKCS7Padding.apply(b + unknownstr, blksize)
        cipher = AES.new(CONSISTENT_KEY, AES.MODE_ECB)
        ecb = ECBMode(blksize, cipher.encrypt, cipher.decrypt)

        return ecb.encrypt(plain)
Beispiel #6
0
    def test_encrypt_bytes_not_padded_raises(self):
        ecb = ECBMode(2, self.mock_fun, self.mock_fun)
        b = b"\x00\x01\x00\x01\x23"

        with self.assertRaises(ValueError):
            ecb.encrypt(b)
Beispiel #7
0
 def test_encrypt_none_bytes_raises_typeerror(self):
     ecb = ECBMode(1, self.mock_fun, self.mock_fun)
     with self.assertRaises(TypeError):
         ecb.encrypt(None)