blob: 1e5d239617ec268b049a4fca17ca4d4c36d2a3fa [file] [log] [blame]
Alex Gaynora2e1f542013-08-10 08:59:11 -04001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
Donald Stufftec672e82013-08-09 01:20:03 -040014"""
15Test using the NIST Test Vectors
16"""
Hynek Schlawack425f5842013-08-11 09:54:59 +020017
18from __future__ import absolute_import, division, print_function
19
Donald Stufftec672e82013-08-09 01:20:03 -040020import binascii
Alex Gaynor735df002013-09-09 16:46:03 -070021import itertools
Alex Gaynoraef7ee82013-08-08 22:31:11 -070022import os
Donald Stufftec672e82013-08-09 01:20:03 -040023
24import pytest
25
Donald Stufftea8e9fc2013-08-10 15:22:28 -040026from cryptography.primitives.block import BlockCipher, ciphers, modes
Donald Stufftec672e82013-08-09 01:20:03 -040027
28from ..utils import load_nist_vectors_from_file
29
30
Paul Kehrer2354fcd2013-09-10 19:15:36 -050031def parameterize_encrypt_test(cipher, vector_type, params, fnames):
32 return pytest.mark.parametrize(params,
Alex Gaynor735df002013-09-09 16:46:03 -070033 list(itertools.chain.from_iterable(
34 load_nist_vectors_from_file(
35 os.path.join(cipher, vector_type, fname),
36 "ENCRYPT",
Paul Kehrer2354fcd2013-09-10 19:15:36 -050037 params
Alex Gaynor735df002013-09-09 16:46:03 -070038 )
39 for fname in fnames
40 ))
Donald Stufft3704a832013-08-09 06:01:41 -040041 )
42
43
Alex Gaynoraef7ee82013-08-08 22:31:11 -070044class TestAES_CBC(object):
Paul Kehrer2354fcd2013-09-10 19:15:36 -050045 @parameterize_encrypt_test(
46 "AES", "KAT",
47 ("key", "iv", "plaintext", "ciphertext"),
48 [
49 "CBCGFSbox128.rsp",
50 "CBCGFSbox192.rsp",
51 "CBCGFSbox256.rsp",
52 "CBCKeySbox128.rsp",
53 "CBCKeySbox192.rsp",
54 "CBCKeySbox256.rsp",
55 "CBCVarKey128.rsp",
56 "CBCVarKey192.rsp",
57 "CBCVarKey256.rsp",
58 "CBCVarTxt128.rsp",
59 "CBCVarTxt192.rsp",
60 "CBCVarTxt256.rsp",
61 ]
62 )
Alex Gaynor735df002013-09-09 16:46:03 -070063 def test_KAT(self, key, iv, plaintext, ciphertext):
Donald Stufftec672e82013-08-09 01:20:03 -040064 cipher = BlockCipher(
65 ciphers.AES(binascii.unhexlify(key)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040066 modes.CBC(binascii.unhexlify(iv)),
Donald Stufftec672e82013-08-09 01:20:03 -040067 )
Donald Stufft5de03922013-08-09 07:28:31 -040068 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
69 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040070 assert binascii.hexlify(actual_ciphertext) == ciphertext
71
Paul Kehrer2354fcd2013-09-10 19:15:36 -050072 @parameterize_encrypt_test(
73 "AES", "MMT",
74 ("key", "iv", "plaintext", "ciphertext"),
75 [
76 "CBCMMT128.rsp",
77 "CBCMMT192.rsp",
78 "CBCMMT256.rsp",
79 ]
80 )
Alex Gaynor735df002013-09-09 16:46:03 -070081 def test_MMT(self, key, iv, plaintext, ciphertext):
Donald Stufft3704a832013-08-09 06:01:41 -040082 cipher = BlockCipher(
83 ciphers.AES(binascii.unhexlify(key)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040084 modes.CBC(binascii.unhexlify(iv)),
Donald Stufft3704a832013-08-09 06:01:41 -040085 )
Donald Stufft5de03922013-08-09 07:28:31 -040086 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
87 actual_ciphertext += cipher.finalize()
Donald Stufft3704a832013-08-09 06:01:41 -040088 assert binascii.hexlify(actual_ciphertext) == ciphertext
Paul Kehrer13f108f2013-09-09 21:41:03 -050089
90
91class TestAES_ECB(object):
92 @parameterize_encrypt_test(
93 "AES", "KAT",
94 ("key", "plaintext", "ciphertext"),
95 [
96 "ECBGFSbox128.rsp",
97 "ECBGFSbox192.rsp",
98 "ECBGFSbox256.rsp",
99 "ECBKeySbox128.rsp",
100 "ECBKeySbox192.rsp",
101 "ECBKeySbox256.rsp",
102 "ECBVarKey128.rsp",
103 "ECBVarKey192.rsp",
104 "ECBVarKey256.rsp",
105 "ECBVarTxt128.rsp",
106 "ECBVarTxt192.rsp",
107 "ECBVarTxt256.rsp",
108 ]
109 )
110 def test_KAT(self, key, plaintext, ciphertext):
111 cipher = BlockCipher(
112 ciphers.AES(binascii.unhexlify(key)),
113 modes.ECB()
114 )
115 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
116 actual_ciphertext += cipher.finalize()
117 assert binascii.hexlify(actual_ciphertext) == ciphertext
118
119 @parameterize_encrypt_test(
120 "AES", "MMT",
121 ("key", "plaintext", "ciphertext"),
122 [
123 "ECBMMT128.rsp",
124 "ECBMMT192.rsp",
125 "ECBMMT256.rsp",
126 ]
127 )
128 def test_MMT(self, key, plaintext, ciphertext):
129 cipher = BlockCipher(
130 ciphers.AES(binascii.unhexlify(key)),
131 modes.ECB()
132 )
133 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
134 actual_ciphertext += cipher.finalize()
135 assert binascii.hexlify(actual_ciphertext) == ciphertext
Paul Kehrer6f412a02013-09-10 21:30:50 -0500136
137
138class TestAES_OFB(object):
139 @parameterize_encrypt_test(
140 "AES", "KAT",
141 ("key", "iv", "plaintext", "ciphertext"),
142 [
143 "OFBGFSbox128.rsp",
144 "OFBGFSbox192.rsp",
145 "OFBGFSbox256.rsp",
146 "OFBKeySbox128.rsp",
147 "OFBKeySbox192.rsp",
148 "OFBKeySbox256.rsp",
149 "OFBVarKey128.rsp",
150 "OFBVarKey192.rsp",
151 "OFBVarKey256.rsp",
152 "OFBVarTxt128.rsp",
153 "OFBVarTxt192.rsp",
154 "OFBVarTxt256.rsp",
155 ]
156 )
157 def test_KAT(self, key, iv, plaintext, ciphertext):
158 cipher = BlockCipher(
159 ciphers.AES(binascii.unhexlify(key)),
160 modes.OFB(binascii.unhexlify(iv))
161 )
162 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
163 actual_ciphertext += cipher.finalize()
164 assert binascii.hexlify(actual_ciphertext) == ciphertext
165
166 @parameterize_encrypt_test(
167 "AES", "MMT",
168 ("key", "iv", "plaintext", "ciphertext"),
169 [
170 "OFBMMT128.rsp",
171 "OFBMMT192.rsp",
172 "OFBMMT256.rsp",
173 ]
174 )
175 def test_MMT(self, key, iv, plaintext, ciphertext):
176 cipher = BlockCipher(
177 ciphers.AES(binascii.unhexlify(key)),
178 modes.OFB(binascii.unhexlify(iv))
179 )
180 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
181 actual_ciphertext += cipher.finalize()
182 assert binascii.hexlify(actual_ciphertext) == ciphertext
Paul Kehrera1ec2622013-09-11 09:38:45 -0500183
184
185class TestAES_CFB(object):
186 @parameterize_encrypt_test(
187 "AES", "KAT",
188 ("key", "iv", "plaintext", "ciphertext"),
189 [
190 "CFB128GFSbox128.rsp",
191 "CFB128GFSbox192.rsp",
192 "CFB128GFSbox256.rsp",
193 "CFB128KeySbox128.rsp",
194 "CFB128KeySbox192.rsp",
195 "CFB128KeySbox256.rsp",
196 "CFB128VarKey128.rsp",
197 "CFB128VarKey192.rsp",
198 "CFB128VarKey256.rsp",
199 "CFB128VarTxt128.rsp",
200 "CFB128VarTxt192.rsp",
201 "CFB128VarTxt256.rsp",
202 ]
203 )
204 def test_KAT(self, key, iv, plaintext, ciphertext):
205 cipher = BlockCipher(
206 ciphers.AES(binascii.unhexlify(key)),
207 modes.CFB(binascii.unhexlify(iv))
208 )
209 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
210 actual_ciphertext += cipher.finalize()
211 assert binascii.hexlify(actual_ciphertext) == ciphertext
212
213 @parameterize_encrypt_test(
214 "AES", "MMT",
215 ("key", "iv", "plaintext", "ciphertext"),
216 [
217 "CFB128MMT128.rsp",
218 "CFB128MMT192.rsp",
219 "CFB128MMT256.rsp",
220 ]
221 )
222 def test_MMT(self, key, iv, plaintext, ciphertext):
223 cipher = BlockCipher(
224 ciphers.AES(binascii.unhexlify(key)),
225 modes.CFB(binascii.unhexlify(iv))
226 )
227 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
228 actual_ciphertext += cipher.finalize()
229 assert binascii.hexlify(actual_ciphertext) == ciphertext