pyca/cryptography — pull request #589 by reaperhulk (2014-02-09 17:50:26+00:00)
Title: CAST5 CBC, OFB, CFB vectors made from the AES MMT test data

These vectors were built with the script below (using OpenSSL) and tested against the `commoncrypto` backend as well. The IV was truncated to 16 hex chars (8 bytes) as CAST5 is a 64-bit block cipher.

Landing these vectors is dependent on them being validated against at least one additional implementation (Botan, Go, etc) and figuring out how we want to document their generation. We may also want to add additional key lengths (the RFC vectors for ECB have 128-bit, 80-bit, and 40-bit).

```python
import binascii

from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.primitives.ciphers import base, algorithms, modes

def encrypt(key, iv, plaintext):
cipher = base.Cipher(
algorithms.CAST5(binascii.unhexlify(key)),
modes.OFB(binascii.unhexlify(iv)),
backend
)
encryptor = cipher.encryptor()
ct = encryptor.update(binascii.unhexlify(plaintext))
ct += encryptor.finalize()
return binascii.hexlify(ct)

vector_file = open(
"tests/hazmat/primitives/vectors/ciphers/AES/OFB/OFBMMT128.rsp",
"r"
)

count = 0
output = []
key = None
iv = None
plaintext = None
ct = None
for line in vector_file:
line = line.strip()
if line.startswith("KEY"):
if count != 0:
output.append("CIPHERTEXT = {}".format(encrypt(key, iv, plaintext)))
output.append("\nCOUNT = {}".format(count))
count += 1
name, key = line.split(" = ")
output.append("KEY = {}".format(key))
elif line.startswith("IV"):
name, iv = line.split(" = ")
iv = iv[0:16]
output.append("IV = {}".format(iv))
elif line.startswith("PLAINTEXT"):
name, plaintext = line.split(" = ")
output.append("PLAINTEXT = {}".format(plaintext))

output.append("CIPHERTEXT = {}".format(encrypt(key, iv, plaintext)))
print("\n".join(output))
```

refs #393, #417