blob: d68cd27b97f4ff492ec4bc20d25a9345377ae3d2 [file] [log] [blame]
Donald Stufftec672e82013-08-09 01:20:03 -04001"""
2Test using the NIST Test Vectors
3"""
4import binascii
Alex Gaynoraef7ee82013-08-08 22:31:11 -07005import os
Donald Stufftec672e82013-08-09 01:20:03 -04006
7import pytest
8
9from cryptography.primitives.block import BlockCipher, ciphers, modes, padding
10
11from ..utils import load_nist_vectors_from_file
12
13
Alex Gaynoraef7ee82013-08-08 22:31:11 -070014def parameterize_encrypt(fname):
15 return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
Donald Stufftec672e82013-08-09 01:20:03 -040016 load_nist_vectors_from_file(
Alex Gaynoraef7ee82013-08-08 22:31:11 -070017 os.path.join("AES/KAT/", fname),
Donald Stufftec672e82013-08-09 01:20:03 -040018 "ENCRYPT",
19 ["key", "iv", "plaintext", "ciphertext"],
20 ),
21 )
Alex Gaynoraef7ee82013-08-08 22:31:11 -070022
23
24class TestAES_CBC(object):
25 @parameterize_encrypt("CBCGFSbox128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040026 def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext):
27 cipher = BlockCipher(
28 ciphers.AES(binascii.unhexlify(key)),
29 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
30 )
31 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
32 assert binascii.hexlify(actual_ciphertext) == ciphertext
33
Alex Gaynoraef7ee82013-08-08 22:31:11 -070034 @parameterize_encrypt("CBCGFSbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040035 def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext):
36 cipher = BlockCipher(
37 ciphers.AES(binascii.unhexlify(key)),
38 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
39 )
40 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
41 assert binascii.hexlify(actual_ciphertext) == ciphertext
42
Alex Gaynoraef7ee82013-08-08 22:31:11 -070043 @parameterize_encrypt("CBCGFSbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040044 def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext):
45 cipher = BlockCipher(
46 ciphers.AES(binascii.unhexlify(key)),
47 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
48 )
49 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
50 assert binascii.hexlify(actual_ciphertext) == ciphertext
51
Alex Gaynoraef7ee82013-08-08 22:31:11 -070052 @parameterize_encrypt("CBCKeySbox128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040053 def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext):
54 cipher = BlockCipher(
55 ciphers.AES(binascii.unhexlify(key)),
56 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
57 )
58 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
59 assert binascii.hexlify(actual_ciphertext) == ciphertext
60
Alex Gaynoraef7ee82013-08-08 22:31:11 -070061 @parameterize_encrypt("CBCKeySbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040062 def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext):
63 cipher = BlockCipher(
64 ciphers.AES(binascii.unhexlify(key)),
65 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
66 )
67 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
68 assert binascii.hexlify(actual_ciphertext) == ciphertext
69
Alex Gaynoraef7ee82013-08-08 22:31:11 -070070 @parameterize_encrypt("CBCKeySbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040071 def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext):
72 cipher = BlockCipher(
73 ciphers.AES(binascii.unhexlify(key)),
74 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
75 )
76 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
77 assert binascii.hexlify(actual_ciphertext) == ciphertext
78
Alex Gaynoraef7ee82013-08-08 22:31:11 -070079 @parameterize_encrypt("CBCVarKey128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040080 def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext):
81 cipher = BlockCipher(
82 ciphers.AES(binascii.unhexlify(key)),
83 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
84 )
85 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
86 assert binascii.hexlify(actual_ciphertext) == ciphertext
87
Alex Gaynoraef7ee82013-08-08 22:31:11 -070088 @parameterize_encrypt("CBCVarKey192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040089 def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext):
90 cipher = BlockCipher(
91 ciphers.AES(binascii.unhexlify(key)),
92 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
93 )
94 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
95 assert binascii.hexlify(actual_ciphertext) == ciphertext
96
Alex Gaynoraef7ee82013-08-08 22:31:11 -070097 @parameterize_encrypt("CBCVarKey256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040098 def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext):
99 cipher = BlockCipher(
100 ciphers.AES(binascii.unhexlify(key)),
101 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
102 )
103 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
104 assert binascii.hexlify(actual_ciphertext) == ciphertext
105
Alex Gaynoraef7ee82013-08-08 22:31:11 -0700106 @parameterize_encrypt("CBCVarTxt128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400107 def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext):
108 cipher = BlockCipher(
109 ciphers.AES(binascii.unhexlify(key)),
110 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
111 )
112 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
113 assert binascii.hexlify(actual_ciphertext) == ciphertext
114
Alex Gaynoraef7ee82013-08-08 22:31:11 -0700115 @parameterize_encrypt("CBCVarTxt192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400116 def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext):
117 cipher = BlockCipher(
118 ciphers.AES(binascii.unhexlify(key)),
119 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
120 )
121 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
122 assert binascii.hexlify(actual_ciphertext) == ciphertext
123
Alex Gaynoraef7ee82013-08-08 22:31:11 -0700124 @parameterize_encrypt("CBCVarTxt256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400125 def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext):
126 cipher = BlockCipher(
127 ciphers.AES(binascii.unhexlify(key)),
128 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
129 )
130 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
131 assert binascii.hexlify(actual_ciphertext) == ciphertext