diff options
-rw-r--r-- | tests/test_utils.py | 223 | ||||
-rw-r--r-- | tests/utils.py | 83 |
2 files changed, 304 insertions, 2 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index fd56dd30..3768c6fc 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,220 @@ 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_encrypt(): + 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 + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:A53D28BB82DF741103EA4F921A44880B:1 + + # CFB128-CAMELLIA128.Decrypt + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:14F7646187817EB586599146B82BD719:0 + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:A53D28BB82DF741103EA4F921A44880B:0 + """).splitlines() + + assert load_openssl_vectors(vector_data, "ENCRYPT") == [ + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"000102030405060708090A0B0C0D0E0F", + b"6BC1BEE22E409F96E93D7E117393172A", + b"14F7646187817EB586599146B82BD719", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"14F7646187817EB586599146B82BD719", + b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + b"A53D28BB82DF741103EA4F921A44880B", + ), + ] + + +def test_load_openssl_vectors_decrypt(): + 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 + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:A53D28BB82DF741103EA4F921A44880B:1 + + # CFB128-CAMELLIA128.Decrypt + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:A53D28BB82DF741103EA4F921A44880B:30C81C46A35CE411E5FBC1191A0A52EF:9C2157A664626D1DEF9EA420FDE69B96:0 + CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:9C2157A664626D1DEF9EA420FDE69B96:F69F2445DF4F9B17AD2B417BE66C3710:742A25F0542340C7BAEF24CA8482BB09:0 + + """).splitlines() + + assert load_openssl_vectors(vector_data, "DECRYPT") == [ + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"A53D28BB82DF741103EA4F921A44880B", + b"30C81C46A35CE411E5FBC1191A0A52EF", + b"9C2157A664626D1DEF9EA420FDE69B96", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"9C2157A664626D1DEF9EA420FDE69B96", + b"F69F2445DF4F9B17AD2B417BE66C3710", + b"742A25F0542340C7BAEF24CA8482BB09", + ), + ] + + +def test_load_openssl_vectors_from_file_encrypt(): + test_list = load_openssl_vectors_from_file( + "Camellia/camellia-ofb.txt", + "ENCRYPT" + ) + assert len(test_list) == 12 + 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", + ), + ] + + +def test_load_openssl_vectors_from_file_decrypt(): + test_list = load_openssl_vectors_from_file( + "Camellia/camellia-ofb.txt", + "DECRYPT" + ) + assert len(test_list) == 12 + 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", + ), + ] + + +def test_load_openssl_vectors_from_file_no_enc_dec_flag(): + test_list = load_openssl_vectors_from_file( + "Camellia/camellia-cbc.txt", + "ENCRYPT" + ) + assert len(test_list) == 12 + assert test_list[:4] == [ + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"000102030405060708090A0B0C0D0E0F", + b"6BC1BEE22E409F96E93D7E117393172A", + b"1607CF494B36BBF00DAEB0B503C831AB", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"1607CF494B36BBF00DAEB0B503C831AB", + b"AE2D8A571E03AC9C9EB76FAC45AF8E51", + b"A2F2CF671629EF7840C5A5DFB5074887", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"A2F2CF671629EF7840C5A5DFB5074887", + b"30C81C46A35CE411E5FBC1191A0A52EF", + b"0F06165008CF8B8B5A63586362543E54", + ), + ( + b"2B7E151628AED2A6ABF7158809CF4F3C", + b"36A84CDAFD5F9A85ADA0F0A993D6D577", + b"F69F2445DF4F9B17AD2B417BE66C3710", + b"74C64268CDB8B8FAF5B34E8AF3732980", + ), + ] diff --git a/tests/utils.py b/tests/utils.py index 9362464e..bbb4843f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -59,3 +59,86 @@ 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): + keys, data = [], {} + + 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"): + keys.append(line.split(" : ")[1].replace(" ", "")) + # create an array under the key to hold all the P+C pairs + data[keys[-1]] = [] + elif line.startswith("P"): + # create a new dict to hold the next P+C pair + data[keys[-1]].append({}) + data[keys[-1]][-1]["P"] = line.split(" : ")[1].replace(" ", "") + elif line.startswith("C"): + data[keys[-1]][-1]["C"] = line.split(" : ")[1].replace(" ", "") + + cryptrec_list = [] + for key, value in data.items(): + for pair in value: + cryptrec_list.append( + (key.encode("ascii"), + pair["P"].encode("ascii"), + pair["C"].encode("ascii")) + ) + + return cryptrec_list + + +def load_openssl_vectors_from_file(filename, op): + 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, op) + + +def load_openssl_vectors(vector_data, op): + encrypt, decrypt = [], [] + + for line in vector_data: + line = line.strip() + + # Blank lines and comments are ignored + if not line or line.startswith("#"): + continue + + vector = line.split(":") + args_list = (vector[1].encode("ascii"), + vector[2].encode("ascii"), + vector[3].encode("ascii"), + vector[4].encode("ascii")) + # some OpenSSL vectors have a final field + # 0 for decrypt, 1 for encrypt + if len(vector) == 6: + if int(vector[5]) == 0: + decrypt.append(args_list) + else: + encrypt.append(args_list) + else: + # if they don't have 1 or 0 they are meant for both enc & dec + # and should be added to both the encrypt and decrypt list + encrypt.append(args_list) + decrypt.append(args_list) + + if op == "ENCRYPT": + return encrypt + else: + return decrypt |