blob: 8262068576cd2cdf6111df7e1b593991ff9f572e [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
Donald Stufft3704a832013-08-09 06:01:41 -040014def parameterize_kat_encrypt(fname):
Alex Gaynoraef7ee82013-08-08 22:31:11 -070015 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
Donald Stufft3704a832013-08-09 06:01:41 -040024def paramterize_mmt_encrypt(fname):
25 return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
26 load_nist_vectors_from_file(
27 os.path.join("AES/MMT", fname),
28 "ENCRYPT",
29 ["key", "iv", "plaintext", "ciphertext"],
30 )
31 )
32
33
Alex Gaynoraef7ee82013-08-08 22:31:11 -070034class TestAES_CBC(object):
Donald Stufft3704a832013-08-09 06:01:41 -040035 @parameterize_kat_encrypt("CBCGFSbox128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040036 def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext):
37 cipher = BlockCipher(
38 ciphers.AES(binascii.unhexlify(key)),
39 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
40 )
Donald Stufft5de03922013-08-09 07:28:31 -040041 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
42 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040043 assert binascii.hexlify(actual_ciphertext) == ciphertext
44
Donald Stufft3704a832013-08-09 06:01:41 -040045 @parameterize_kat_encrypt("CBCGFSbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040046 def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext):
47 cipher = BlockCipher(
48 ciphers.AES(binascii.unhexlify(key)),
49 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
50 )
Donald Stufft5de03922013-08-09 07:28:31 -040051 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
52 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040053 assert binascii.hexlify(actual_ciphertext) == ciphertext
54
Donald Stufft3704a832013-08-09 06:01:41 -040055 @parameterize_kat_encrypt("CBCGFSbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040056 def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext):
57 cipher = BlockCipher(
58 ciphers.AES(binascii.unhexlify(key)),
59 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
60 )
Donald Stufft5de03922013-08-09 07:28:31 -040061 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
62 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040063 assert binascii.hexlify(actual_ciphertext) == ciphertext
64
Donald Stufft3704a832013-08-09 06:01:41 -040065 @parameterize_kat_encrypt("CBCKeySbox128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040066 def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext):
67 cipher = BlockCipher(
68 ciphers.AES(binascii.unhexlify(key)),
69 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
70 )
Donald Stufft5de03922013-08-09 07:28:31 -040071 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
72 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040073 assert binascii.hexlify(actual_ciphertext) == ciphertext
74
Donald Stufft3704a832013-08-09 06:01:41 -040075 @parameterize_kat_encrypt("CBCKeySbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040076 def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext):
77 cipher = BlockCipher(
78 ciphers.AES(binascii.unhexlify(key)),
79 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
80 )
Donald Stufft5de03922013-08-09 07:28:31 -040081 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
82 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040083 assert binascii.hexlify(actual_ciphertext) == ciphertext
84
Donald Stufft3704a832013-08-09 06:01:41 -040085 @parameterize_kat_encrypt("CBCKeySbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040086 def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext):
87 cipher = BlockCipher(
88 ciphers.AES(binascii.unhexlify(key)),
89 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
90 )
Donald Stufft5de03922013-08-09 07:28:31 -040091 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
92 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040093 assert binascii.hexlify(actual_ciphertext) == ciphertext
94
Donald Stufft3704a832013-08-09 06:01:41 -040095 @parameterize_kat_encrypt("CBCVarKey128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040096 def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext):
97 cipher = BlockCipher(
98 ciphers.AES(binascii.unhexlify(key)),
99 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
100 )
Donald Stufft5de03922013-08-09 07:28:31 -0400101 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
102 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400103 assert binascii.hexlify(actual_ciphertext) == ciphertext
104
Donald Stufft3704a832013-08-09 06:01:41 -0400105 @parameterize_kat_encrypt("CBCVarKey192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400106 def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext):
107 cipher = BlockCipher(
108 ciphers.AES(binascii.unhexlify(key)),
109 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
110 )
Donald Stufft5de03922013-08-09 07:28:31 -0400111 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
112 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400113 assert binascii.hexlify(actual_ciphertext) == ciphertext
114
Donald Stufft3704a832013-08-09 06:01:41 -0400115 @parameterize_kat_encrypt("CBCVarKey256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400116 def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext):
117 cipher = BlockCipher(
118 ciphers.AES(binascii.unhexlify(key)),
119 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
120 )
Donald Stufft5de03922013-08-09 07:28:31 -0400121 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
122 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400123 assert binascii.hexlify(actual_ciphertext) == ciphertext
124
Donald Stufft3704a832013-08-09 06:01:41 -0400125 @parameterize_kat_encrypt("CBCVarTxt128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400126 def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext):
127 cipher = BlockCipher(
128 ciphers.AES(binascii.unhexlify(key)),
129 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
130 )
Donald Stufft5de03922013-08-09 07:28:31 -0400131 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
132 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400133 assert binascii.hexlify(actual_ciphertext) == ciphertext
134
Donald Stufft3704a832013-08-09 06:01:41 -0400135 @parameterize_kat_encrypt("CBCVarTxt192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400136 def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext):
137 cipher = BlockCipher(
138 ciphers.AES(binascii.unhexlify(key)),
139 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
140 )
Donald Stufft5de03922013-08-09 07:28:31 -0400141 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
142 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400143 assert binascii.hexlify(actual_ciphertext) == ciphertext
144
Donald Stufft3704a832013-08-09 06:01:41 -0400145 @parameterize_kat_encrypt("CBCVarTxt256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400146 def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext):
147 cipher = BlockCipher(
148 ciphers.AES(binascii.unhexlify(key)),
149 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
150 )
Donald Stufft5de03922013-08-09 07:28:31 -0400151 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
152 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -0400153 assert binascii.hexlify(actual_ciphertext) == ciphertext
Donald Stufft3704a832013-08-09 06:01:41 -0400154
155 @paramterize_mmt_encrypt("CBCMMT128.rsp")
156 def test_MMT_128_encrypt(self, key, iv, plaintext, ciphertext):
157 cipher = BlockCipher(
158 ciphers.AES(binascii.unhexlify(key)),
159 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
160 )
Donald Stufft5de03922013-08-09 07:28:31 -0400161 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
162 actual_ciphertext += cipher.finalize()
Donald Stufft3704a832013-08-09 06:01:41 -0400163 assert binascii.hexlify(actual_ciphertext) == ciphertext
164
165 @paramterize_mmt_encrypt("CBCMMT192.rsp")
166 def test_MMT_192_encrypt(self, key, iv, plaintext, ciphertext):
167 cipher = BlockCipher(
168 ciphers.AES(binascii.unhexlify(key)),
169 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
170 )
Donald Stufft5de03922013-08-09 07:28:31 -0400171 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
172 actual_ciphertext += cipher.finalize()
Donald Stufft3704a832013-08-09 06:01:41 -0400173 assert binascii.hexlify(actual_ciphertext) == ciphertext
174
175 @paramterize_mmt_encrypt("CBCMMT256.rsp")
176 def test_MMT_256_encrypt(self, key, iv, plaintext, ciphertext):
177 cipher = BlockCipher(
178 ciphers.AES(binascii.unhexlify(key)),
179 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
180 )
Donald Stufft5de03922013-08-09 07:28:31 -0400181 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
182 actual_ciphertext += cipher.finalize()
Donald Stufft3704a832013-08-09 06:01:41 -0400183 assert binascii.hexlify(actual_ciphertext) == ciphertext