diff options
-rw-r--r-- | tests/test_utils.py | 142 | ||||
-rw-r--r-- | tests/utils.py | 63 |
2 files changed, 203 insertions, 2 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index fd56dd30..73394a51 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -13,7 +13,9 @@ import textwrap -from .utils import load_nist_vectors, load_nist_vectors_from_file +from .utils import (load_nist_vectors, load_nist_vectors_from_file, + load_cryptrec_vectors, load_cryptrec_vectors_from_file, + load_openssl_vectors, load_openssl_vectors_from_file) def test_load_nist_vectors_encrypt(): @@ -179,7 +181,7 @@ def test_load_nist_vectors_from_file_encrypt(): ] -def test_load_nist_vectors_from_file_decypt(): +def test_load_nist_vectors_from_file_decrypt(): assert load_nist_vectors_from_file( "AES/KAT/CBCGFSbox128.rsp", "DECRYPT", @@ -228,3 +230,139 @@ def test_load_nist_vectors_from_file_decypt(): b"58c8e00b2631686d54eab84b91f0aca1" ), ] + + +def test_load_cryptrec_vectors(): + vector_data = textwrap.dedent(""" + # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ + # Download is t_camelia.txt + + # Camellia with 128-bit key + + K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + + P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C + + P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3 + + K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + + P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C + """).splitlines() + + assert load_cryptrec_vectors(vector_data) == [ + ( + b"00000000000000000000000000000000", + b"80000000000000000000000000000000", + b"07923A39EB0A817D1C4D87BDB82D1F1C", + ), + ( + b"00000000000000000000000000000000", + b"40000000000000000000000000000000", + b"48CD6419809672D2349260D89A08D3D3", + ), + ( + b"10000000000000000000000000000000", + b"80000000000000000000000000000000", + b"07923A39EB0A817D1C4D87BDB82D1F1C", + ), + ] + + +def test_load_cryptrec_vectors_from_file_encrypt(): + test_set = load_cryptrec_vectors_from_file( + "Camellia/NTT/camellia-128-ecb.txt" + ) + assert test_set[0] == ( + ( + b"00000000000000000000000000000000", + b"80000000000000000000000000000000", + b"07923A39EB0A817D1C4D87BDB82D1F1C", + ) + ) + assert len(test_set) == 1280 + + +def test_load_openssl_vectors(): + vector_data = textwrap.dedent( + """ + # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt + # For all CFB128 encrypts and decrypts, the transformed sequence is + # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec + # CFB128-CAMELLIA128.Encrypt + """ + "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" + "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" + "14F7646187817EB586599146B82BD719:1\n" + "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" + "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" + "A53D28BB82DF741103EA4F921A44880B:1\n\n" + "# CFB128-CAMELLIA128.Decrypt\n" + "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" + "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" + "14F7646187817EB586599146B82BD719:0\n" + "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" + "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" + "A53D28BB82DF741103EA4F921A44880B:0" + ).splitlines() + + assert load_openssl_vectors(vector_data) == [ + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"000102030405060708090A0B0C0D0E0F", + b"6BC1BEE22E409F96E93D7E117393172A", + b"14F7646187817EB586599146B82BD719", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"14F7646187817EB586599146B82BD719", + b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + b"A53D28BB82DF741103EA4F921A44880B", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"000102030405060708090A0B0C0D0E0F", + b"6BC1BEE22E409F96E93D7E117393172A", + b"14F7646187817EB586599146B82BD719", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"14F7646187817EB586599146B82BD719", + b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + b"A53D28BB82DF741103EA4F921A44880B", + ), + ] + + +def test_load_openssl_vectors_from_file(): + test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt") + assert len(test_list) == 24 + assert test_list[:4] == [ + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"000102030405060708090A0B0C0D0E0F", + b"6BC1BEE22E409F96E93D7E117393172A", + b"14F7646187817EB586599146B82BD719", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"50FE67CC996D32B6DA0937E99BAFEC60", + b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + b"25623DB569CA51E01482649977E28D84", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"D9A4DADA0892239F6B8B3D7680E15674", + b"30C81C46A35CE411E5FBC1191A0A52EF", + b"C776634A60729DC657D12B9FCA801E98", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"A78819583F0308E7A6BF36B1386ABF23", + b"F69F2445DF4F9B17AD2B417BE66C3710", + b"D776379BE0E50825E681DA1A4C980E8E", + ), + ] diff --git a/tests/utils.py b/tests/utils.py index 9362464e..d06c9e3b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -59,3 +59,66 @@ def load_nist_vectors_from_file(filename, op, fields): ) with open(os.path.join(base, filename), "r") as vector_file: return load_nist_vectors(vector_file, op, fields) + + +def load_cryptrec_vectors_from_file(filename): + base = os.path.join( + os.path.dirname(__file__), "primitives", "vectors", "CRYPTREC", + ) + with open(os.path.join(base, filename), "r") as vector_file: + return load_cryptrec_vectors(vector_file) + + +def load_cryptrec_vectors(vector_data): + cryptrec_list = [] + + for line in vector_data: + line = line.strip() + + # Blank lines and comments are ignored + if not line or line.startswith("#"): + continue + + if line.startswith("K"): + key = line.split(" : ")[1].replace(" ", "").encode("ascii") + elif line.startswith("P"): + pt = line.split(" : ")[1].replace(" ", "").encode("ascii") + elif line.startswith("C"): + ct = line.split(" : ")[1].replace(" ", "").encode("ascii") + # after a C is found the K+P+C tuple is complete + # there are many P+C pairs for each K + cryptrec_list.append((key, pt, ct)) + return cryptrec_list + + +def load_openssl_vectors_from_file(filename): + base = os.path.join( + os.path.dirname(__file__), "primitives", "vectors", "OpenSSL", + ) + with open(os.path.join(base, filename), "r") as vector_file: + return load_openssl_vectors(vector_file) + + +def load_openssl_vectors(vector_data): + vectors = [] + + for line in vector_data: + line = line.strip() + + # Blank lines and comments are ignored + if not line or line.startswith("#"): + continue + + vector = line.split(":") + params = ( + # key + vector[1].encode("ascii"), + # iv + vector[2].encode("ascii"), + # plaintext + vector[3].encode("ascii"), + # ciphertext + vector[4].encode("ascii") + ) + vectors.append(params) + return vectors |