1import binascii
2
3import botan
4
5from tests.utils import load_nist_vectors
6
7BLOCK_SIZE = 64
8
9
10def encrypt(mode, key, iv, plaintext):
11    encryptor = botan.Cipher("IDEA/{0}/NoPadding".format(mode), "encrypt",
12                             binascii.unhexlify(key))
13
14    cipher_text = encryptor.cipher(binascii.unhexlify(plaintext),
15                                   binascii.unhexlify(iv))
16    return binascii.hexlify(cipher_text)
17
18
19def verify_vectors(mode, filename):
20    with open(filename, "r") as f:
21        vector_file = f.read().splitlines()
22
23    vectors = load_nist_vectors(vector_file)
24    for vector in vectors:
25        ct = encrypt(
26            mode,
27            vector["key"],
28            vector["iv"],
29            vector["plaintext"]
30        )
31        assert ct == vector["ciphertext"]
32
33
34cbc_path = "tests/hazmat/primitives/vectors/ciphers/IDEA/idea-cbc.txt"
35verify_vectors("CBC", cbc_path)
36ofb_path = "tests/hazmat/primitives/vectors/ciphers/IDEA/idea-ofb.txt"
37verify_vectors("OFB", ofb_path)
38cfb_path = "tests/hazmat/primitives/vectors/ciphers/IDEA/idea-cfb.txt"
39verify_vectors("CFB", cfb_path)
40