blob: 651f0415ed60021764d0ef5a3de44b37446f6e61 [file] [log] [blame]
Donald Stufftec672e82013-08-09 01:20:03 -04001"""
2Test using the NIST Test Vectors
3"""
4import binascii
5
6import pytest
7
8from cryptography.primitives.block import BlockCipher, ciphers, modes, padding
9
10from ..utils import load_nist_vectors_from_file
11
12
13class TestAES_CBC:
14
15 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
16 load_nist_vectors_from_file(
17 "AES/KAT/CBCGFSbox128.rsp",
18 "ENCRYPT",
19 ["key", "iv", "plaintext", "ciphertext"],
20 ),
21 )
22 def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext):
23 cipher = BlockCipher(
24 ciphers.AES(binascii.unhexlify(key)),
25 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
26 )
27 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
28 assert binascii.hexlify(actual_ciphertext) == ciphertext
29
30 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
31 load_nist_vectors_from_file(
32 "AES/KAT/CBCGFSbox192.rsp",
33 "ENCRYPT",
34 ["key", "iv", "plaintext", "ciphertext"],
35 ),
36 )
37 def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext):
38 cipher = BlockCipher(
39 ciphers.AES(binascii.unhexlify(key)),
40 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
41 )
42 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
43 assert binascii.hexlify(actual_ciphertext) == ciphertext
44
45 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
46 load_nist_vectors_from_file(
47 "AES/KAT/CBCGFSbox256.rsp",
48 "ENCRYPT",
49 ["key", "iv", "plaintext", "ciphertext"],
50 ),
51 )
52 def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext):
53 cipher = BlockCipher(
54 ciphers.AES(binascii.unhexlify(key)),
55 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
56 )
57 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
58 assert binascii.hexlify(actual_ciphertext) == ciphertext
59
60 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
61 load_nist_vectors_from_file(
62 "AES/KAT/CBCKeySbox128.rsp",
63 "ENCRYPT",
64 ["key", "iv", "plaintext", "ciphertext"],
65 ),
66 )
67 def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext):
68 cipher = BlockCipher(
69 ciphers.AES(binascii.unhexlify(key)),
70 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
71 )
72 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
73 assert binascii.hexlify(actual_ciphertext) == ciphertext
74
75 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
76 load_nist_vectors_from_file(
77 "AES/KAT/CBCKeySbox192.rsp",
78 "ENCRYPT",
79 ["key", "iv", "plaintext", "ciphertext"],
80 ),
81 )
82 def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext):
83 cipher = BlockCipher(
84 ciphers.AES(binascii.unhexlify(key)),
85 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
86 )
87 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
88 assert binascii.hexlify(actual_ciphertext) == ciphertext
89
90 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
91 load_nist_vectors_from_file(
92 "AES/KAT/CBCKeySbox256.rsp",
93 "ENCRYPT",
94 ["key", "iv", "plaintext", "ciphertext"],
95 ),
96 )
97 def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext):
98 cipher = BlockCipher(
99 ciphers.AES(binascii.unhexlify(key)),
100 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
101 )
102 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
103 assert binascii.hexlify(actual_ciphertext) == ciphertext
104
105 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
106 load_nist_vectors_from_file(
107 "AES/KAT/CBCVarKey128.rsp",
108 "ENCRYPT",
109 ["key", "iv", "plaintext", "ciphertext"],
110 ),
111 )
112 def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext):
113 cipher = BlockCipher(
114 ciphers.AES(binascii.unhexlify(key)),
115 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
116 )
117 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
118 assert binascii.hexlify(actual_ciphertext) == ciphertext
119
120 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
121 load_nist_vectors_from_file(
122 "AES/KAT/CBCVarKey192.rsp",
123 "ENCRYPT",
124 ["key", "iv", "plaintext", "ciphertext"],
125 ),
126 )
127 def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext):
128 cipher = BlockCipher(
129 ciphers.AES(binascii.unhexlify(key)),
130 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
131 )
132 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
133 assert binascii.hexlify(actual_ciphertext) == ciphertext
134
135 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
136 load_nist_vectors_from_file(
137 "AES/KAT/CBCVarKey256.rsp",
138 "ENCRYPT",
139 ["key", "iv", "plaintext", "ciphertext"],
140 ),
141 )
142 def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext):
143 cipher = BlockCipher(
144 ciphers.AES(binascii.unhexlify(key)),
145 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
146 )
147 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
148 assert binascii.hexlify(actual_ciphertext) == ciphertext
149
150 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
151 load_nist_vectors_from_file(
152 "AES/KAT/CBCVarTxt128.rsp",
153 "ENCRYPT",
154 ["key", "iv", "plaintext", "ciphertext"],
155 ),
156 )
157 def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext):
158 cipher = BlockCipher(
159 ciphers.AES(binascii.unhexlify(key)),
160 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
161 )
162 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
163 assert binascii.hexlify(actual_ciphertext) == ciphertext
164
165 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
166 load_nist_vectors_from_file(
167 "AES/KAT/CBCVarTxt192.rsp",
168 "ENCRYPT",
169 ["key", "iv", "plaintext", "ciphertext"],
170 ),
171 )
172 def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext):
173 cipher = BlockCipher(
174 ciphers.AES(binascii.unhexlify(key)),
175 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
176 )
177 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
178 assert binascii.hexlify(actual_ciphertext) == ciphertext
179
180 @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
181 load_nist_vectors_from_file(
182 "AES/KAT/CBCVarTxt256.rsp",
183 "ENCRYPT",
184 ["key", "iv", "plaintext", "ciphertext"],
185 ),
186 )
187 def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext):
188 cipher = BlockCipher(
189 ciphers.AES(binascii.unhexlify(key)),
190 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
191 )
192 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
193 assert binascii.hexlify(actual_ciphertext) == ciphertext