aboutsummaryrefslogtreecommitdiffstats
path: root/docs/development/custom-vectors/cast5/generate_cast5.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-12 16:17:04 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-12 16:17:04 -0600
commitcf6ffb5ef3b50fb6485f3e669c28156c03a5420c (patch)
tree17e5a8e9bcd70db1e692912113febe5d9ae09ff2 /docs/development/custom-vectors/cast5/generate_cast5.py
parent493efbb64095dca073926a07fdc654418490d8be (diff)
downloadcryptography-cf6ffb5ef3b50fb6485f3e669c28156c03a5420c.tar.gz
cryptography-cf6ffb5ef3b50fb6485f3e669c28156c03a5420c.tar.bz2
cryptography-cf6ffb5ef3b50fb6485f3e669c28156c03a5420c.zip
add cast5 (cbc, cfb, ofb) vector source info to docs
Diffstat (limited to 'docs/development/custom-vectors/cast5/generate_cast5.py')
-rw-r--r--docs/development/custom-vectors/cast5/generate_cast5.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/docs/development/custom-vectors/cast5/generate_cast5.py b/docs/development/custom-vectors/cast5/generate_cast5.py
new file mode 100644
index 00000000..f038825a
--- /dev/null
+++ b/docs/development/custom-vectors/cast5/generate_cast5.py
@@ -0,0 +1,58 @@
+import binascii
+
+from cryptography.hazmat.backends.openssl.backend import backend
+from cryptography.hazmat.primitives.ciphers import base, algorithms, modes
+
+
+def encrypt(mode, key, iv, plaintext):
+ cipher = base.Cipher(
+ algorithms.CAST5(binascii.unhexlify(key)),
+ mode(binascii.unhexlify(iv)),
+ backend
+ )
+ encryptor = cipher.encryptor()
+ ct = encryptor.update(binascii.unhexlify(plaintext))
+ ct += encryptor.finalize()
+ return binascii.hexlify(ct)
+
+
+def build_vectors(mode, filename):
+ vector_file = open(filename, "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(mode, 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(mode, key, iv, plaintext)))
+ return "\n".join(output)
+
+
+def write_file(data, filename):
+ with open(filename, "w") as f:
+ f.write(data)
+
+cbc_path = "tests/hazmat/primitives/vectors/ciphers/AES/CBC/CBCMMT128.rsp"
+write_file(build_vectors(modes.CBC, cbc_path), "cast5-cbc.txt")
+ofb_path = "tests/hazmat/primitives/vectors/ciphers/AES/OFB/OFBMMT128.rsp"
+write_file(build_vectors(modes.OFB, ofb_path), "cast5-ofb.txt")
+cfb_path = "tests/hazmat/primitives/vectors/ciphers/AES/CFB/CFB128MMT128.rsp"
+write_file(build_vectors(modes.CFB, cfb_path), "cast5-cfb.txt")