blob: 88090f9a69b7cae086a35d2245b77ba7406133ef [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 )
41 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
42 assert binascii.hexlify(actual_ciphertext) == ciphertext
43
Donald Stufft3704a832013-08-09 06:01:41 -040044 @parameterize_kat_encrypt("CBCGFSbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040045 def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext):
46 cipher = BlockCipher(
47 ciphers.AES(binascii.unhexlify(key)),
48 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
49 )
50 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
51 assert binascii.hexlify(actual_ciphertext) == ciphertext
52
Donald Stufft3704a832013-08-09 06:01:41 -040053 @parameterize_kat_encrypt("CBCGFSbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040054 def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext):
55 cipher = BlockCipher(
56 ciphers.AES(binascii.unhexlify(key)),
57 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
58 )
59 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
60 assert binascii.hexlify(actual_ciphertext) == ciphertext
61
Donald Stufft3704a832013-08-09 06:01:41 -040062 @parameterize_kat_encrypt("CBCKeySbox128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040063 def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext):
64 cipher = BlockCipher(
65 ciphers.AES(binascii.unhexlify(key)),
66 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
67 )
68 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
69 assert binascii.hexlify(actual_ciphertext) == ciphertext
70
Donald Stufft3704a832013-08-09 06:01:41 -040071 @parameterize_kat_encrypt("CBCKeySbox192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040072 def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext):
73 cipher = BlockCipher(
74 ciphers.AES(binascii.unhexlify(key)),
75 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
76 )
77 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
78 assert binascii.hexlify(actual_ciphertext) == ciphertext
79
Donald Stufft3704a832013-08-09 06:01:41 -040080 @parameterize_kat_encrypt("CBCKeySbox256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040081 def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext):
82 cipher = BlockCipher(
83 ciphers.AES(binascii.unhexlify(key)),
84 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
85 )
86 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
87 assert binascii.hexlify(actual_ciphertext) == ciphertext
88
Donald Stufft3704a832013-08-09 06:01:41 -040089 @parameterize_kat_encrypt("CBCVarKey128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040090 def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext):
91 cipher = BlockCipher(
92 ciphers.AES(binascii.unhexlify(key)),
93 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
94 )
95 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
96 assert binascii.hexlify(actual_ciphertext) == ciphertext
97
Donald Stufft3704a832013-08-09 06:01:41 -040098 @parameterize_kat_encrypt("CBCVarKey192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -040099 def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext):
100 cipher = BlockCipher(
101 ciphers.AES(binascii.unhexlify(key)),
102 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
103 )
104 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
105 assert binascii.hexlify(actual_ciphertext) == ciphertext
106
Donald Stufft3704a832013-08-09 06:01:41 -0400107 @parameterize_kat_encrypt("CBCVarKey256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400108 def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext):
109 cipher = BlockCipher(
110 ciphers.AES(binascii.unhexlify(key)),
111 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
112 )
113 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
114 assert binascii.hexlify(actual_ciphertext) == ciphertext
115
Donald Stufft3704a832013-08-09 06:01:41 -0400116 @parameterize_kat_encrypt("CBCVarTxt128.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400117 def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext):
118 cipher = BlockCipher(
119 ciphers.AES(binascii.unhexlify(key)),
120 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
121 )
122 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
123 assert binascii.hexlify(actual_ciphertext) == ciphertext
124
Donald Stufft3704a832013-08-09 06:01:41 -0400125 @parameterize_kat_encrypt("CBCVarTxt192.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400126 def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext):
127 cipher = BlockCipher(
128 ciphers.AES(binascii.unhexlify(key)),
129 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
130 )
131 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
132 assert binascii.hexlify(actual_ciphertext) == ciphertext
133
Donald Stufft3704a832013-08-09 06:01:41 -0400134 @parameterize_kat_encrypt("CBCVarTxt256.rsp")
Donald Stufftec672e82013-08-09 01:20:03 -0400135 def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext):
136 cipher = BlockCipher(
137 ciphers.AES(binascii.unhexlify(key)),
138 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
139 )
140 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
141 assert binascii.hexlify(actual_ciphertext) == ciphertext
Donald Stufft3704a832013-08-09 06:01:41 -0400142
143 @paramterize_mmt_encrypt("CBCMMT128.rsp")
144 def test_MMT_128_encrypt(self, key, iv, plaintext, ciphertext):
145 cipher = BlockCipher(
146 ciphers.AES(binascii.unhexlify(key)),
147 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
148 )
149 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
150 assert binascii.hexlify(actual_ciphertext) == ciphertext
151
152 @paramterize_mmt_encrypt("CBCMMT192.rsp")
153 def test_MMT_192_encrypt(self, key, iv, plaintext, ciphertext):
154 cipher = BlockCipher(
155 ciphers.AES(binascii.unhexlify(key)),
156 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
157 )
158 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
159 assert binascii.hexlify(actual_ciphertext) == ciphertext
160
161 @paramterize_mmt_encrypt("CBCMMT256.rsp")
162 def test_MMT_256_encrypt(self, key, iv, plaintext, ciphertext):
163 cipher = BlockCipher(
164 ciphers.AES(binascii.unhexlify(key)),
165 modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
166 )
167 actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize()
168 assert binascii.hexlify(actual_ciphertext) == ciphertext