aboutsummaryrefslogtreecommitdiffstats
path: root/docs/development/custom-vectors/seed/generate_seed.py
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-04-08 09:17:02 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-04-08 11:38:10 -0500
commit6d8f9b0273e2edfbbf5fedc5dbc5c2c427a3e3e9 (patch)
treed360ef25b99c7c88a399a1017420a058c83c7cb3 /docs/development/custom-vectors/seed/generate_seed.py
parentfb201e9f21c19d2e31020012fafdeb85083d36c4 (diff)
downloadcryptography-6d8f9b0273e2edfbbf5fedc5dbc5c2c427a3e3e9.tar.gz
cryptography-6d8f9b0273e2edfbbf5fedc5dbc5c2c427a3e3e9.tar.bz2
cryptography-6d8f9b0273e2edfbbf5fedc5dbc5c2c427a3e3e9.zip
add SEED CFB/OFB bespoke vectors and documentation
Diffstat (limited to 'docs/development/custom-vectors/seed/generate_seed.py')
-rw-r--r--docs/development/custom-vectors/seed/generate_seed.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/docs/development/custom-vectors/seed/generate_seed.py b/docs/development/custom-vectors/seed/generate_seed.py
new file mode 100644
index 00000000..d59597fd
--- /dev/null
+++ b/docs/development/custom-vectors/seed/generate_seed.py
@@ -0,0 +1,57 @@
+import binascii
+
+from cryptography.hazmat.backends.openssl.backend import backend
+from cryptography.hazmat.primitives.ciphers import algorithms, base, modes
+
+
+def encrypt(mode, key, iv, plaintext):
+ cipher = base.Cipher(
+ algorithms.SEED(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):
+ with open(filename, "r") as f:
+ vector_file = f.read().splitlines()
+
+ count = 0
+ output = []
+ key = None
+ iv = None
+ plaintext = None
+ for line in vector_file:
+ line = line.strip()
+ if line.startswith("KEY"):
+ if count != 0:
+ output.append("CIPHERTEXT = {0}".format(
+ encrypt(mode, key, iv, plaintext))
+ )
+ output.append("\nCOUNT = {0}".format(count))
+ count += 1
+ name, key = line.split(" = ")
+ output.append("KEY = {0}".format(key))
+ elif line.startswith("IV"):
+ name, iv = line.split(" = ")
+ output.append("IV = {0}".format(iv))
+ elif line.startswith("PLAINTEXT"):
+ name, plaintext = line.split(" = ")
+ output.append("PLAINTEXT = {0}".format(plaintext))
+
+ output.append("CIPHERTEXT = {0}".format(encrypt(mode, key, iv, plaintext)))
+ return "\n".join(output)
+
+
+def write_file(data, filename):
+ with open(filename, "w") as f:
+ f.write(data)
+
+OFB_PATH = "vectors/cryptography_vectors/ciphers/AES/OFB/OFBMMT128.rsp"
+write_file(build_vectors(modes.OFB, OFB_PATH), "seed-ofb.txt")
+CFB_PATH = "vectors/cryptography_vectors/ciphers/AES/CFB/CFB128MMT128.rsp"
+write_file(build_vectors(modes.CFB, CFB_PATH), "seed-cfb.txt")