blob: fd56dd307f996080863c2480e16516dabe0d0142 [file] [log] [blame]
Alex Gaynorf312a5c2013-08-10 15:23:38 -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 Stufft9e1a48b2013-08-09 00:32:30 -040014import textwrap
15
16from .utils import load_nist_vectors, load_nist_vectors_from_file
17
18
19def test_load_nist_vectors_encrypt():
20 vector_data = textwrap.dedent("""
21 # CAVS 11.1
22 # Config info for aes_values
23 # AESVS GFSbox test data for CBC
24 # State : Encrypt and Decrypt
25 # Key Length : 128
26 # Generated on Fri Apr 22 15:11:33 2011
27
28 [ENCRYPT]
29
30 COUNT = 0
31 KEY = 00000000000000000000000000000000
32 IV = 00000000000000000000000000000000
33 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
34 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
35
36 COUNT = 1
37 KEY = 00000000000000000000000000000000
38 IV = 00000000000000000000000000000000
39 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
40 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
41
42 [DECRYPT]
43
44 COUNT = 0
45 KEY = 00000000000000000000000000000000
46 IV = 00000000000000000000000000000000
47 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
48 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
49
50 COUNT = 1
51 KEY = 00000000000000000000000000000000
52 IV = 00000000000000000000000000000000
53 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
54 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
55 """).splitlines()
56
57 assert load_nist_vectors(vector_data, "ENCRYPT",
58 ["key", "iv", "plaintext", "ciphertext"],
59 ) == [
60 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -040061 b"00000000000000000000000000000000",
62 b"00000000000000000000000000000000",
63 b"f34481ec3cc627bacd5dc3fb08f273e6",
64 b"0336763e966d92595a567cc9ce537f5e",
Donald Stufft9e1a48b2013-08-09 00:32:30 -040065 ),
66 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -040067 b"00000000000000000000000000000000",
68 b"00000000000000000000000000000000",
69 b"9798c4640bad75c7c3227db910174e72",
70 b"a9a1631bf4996954ebc093957b234589",
Donald Stufft9e1a48b2013-08-09 00:32:30 -040071 ),
72 ]
73
74
75def test_load_nist_vectors_decrypt():
76 vector_data = textwrap.dedent("""
77 # CAVS 11.1
78 # Config info for aes_values
79 # AESVS GFSbox test data for CBC
80 # State : Encrypt and Decrypt
81 # Key Length : 128
82 # Generated on Fri Apr 22 15:11:33 2011
83
84 [ENCRYPT]
85
86 COUNT = 0
87 KEY = 00000000000000000000000000000000
88 IV = 00000000000000000000000000000000
89 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
90 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
91
92 COUNT = 1
93 KEY = 00000000000000000000000000000000
94 IV = 00000000000000000000000000000000
95 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
96 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
97
98 [DECRYPT]
99
100 COUNT = 0
101 KEY = 00000000000000000000000000000000
102 IV = 00000000000000000000000000000000
103 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
104 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
105
106 COUNT = 1
107 KEY = 00000000000000000000000000000000
108 IV = 00000000000000000000000000000000
109 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
110 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
111 """).splitlines()
112
113 assert load_nist_vectors(vector_data, "DECRYPT",
114 ["key", "iv", "ciphertext", "plaintext"],
115 ) == [
116 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400117 b"00000000000000000000000000000000",
118 b"00000000000000000000000000000000",
119 b"0336763e966d92595a567cc9ce537f5e",
120 b"f34481ec3cc627bacd5dc3fb08f273e6",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400121 ),
122 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400123 b"00000000000000000000000000000000",
124 b"00000000000000000000000000000000",
125 b"a9a1631bf4996954ebc093957b234589",
126 b"9798c4640bad75c7c3227db910174e72",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400127 ),
128 ]
129
130
131def test_load_nist_vectors_from_file_encrypt():
132 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400133 "AES/KAT/CBCGFSbox128.rsp",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400134 "ENCRYPT",
135 ["key", "iv", "plaintext", "ciphertext"],
136 ) == [
137 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400138 b"00000000000000000000000000000000",
139 b"00000000000000000000000000000000",
140 b"f34481ec3cc627bacd5dc3fb08f273e6",
141 b"0336763e966d92595a567cc9ce537f5e",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400142 ),
143 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400144 b"00000000000000000000000000000000",
145 b"00000000000000000000000000000000",
146 b"9798c4640bad75c7c3227db910174e72",
147 b"a9a1631bf4996954ebc093957b234589",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400148 ),
149 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400150 b"00000000000000000000000000000000",
151 b"00000000000000000000000000000000",
152 b"96ab5c2ff612d9dfaae8c31f30c42168",
153 b"ff4f8391a6a40ca5b25d23bedd44a597",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400154 ),
155 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400156 b"00000000000000000000000000000000",
157 b"00000000000000000000000000000000",
158 b"6a118a874519e64e9963798a503f1d35",
159 b"dc43be40be0e53712f7e2bf5ca707209",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400160 ),
161 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400162 b"00000000000000000000000000000000",
163 b"00000000000000000000000000000000",
164 b"cb9fceec81286ca3e989bd979b0cb284",
165 b"92beedab1895a94faa69b632e5cc47ce",
166 ),
167 (
168 b"00000000000000000000000000000000",
169 b"00000000000000000000000000000000",
170 b"b26aeb1874e47ca8358ff22378f09144",
171 b"459264f4798f6a78bacb89c15ed3d601",
172 ),
173 (
174 b"00000000000000000000000000000000",
175 b"00000000000000000000000000000000",
176 b"58c8e00b2631686d54eab84b91f0aca1",
177 b"08a4e2efec8a8e3312ca7460b9040bbf",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400178 ),
179 ]
180
181
182def test_load_nist_vectors_from_file_decypt():
183 assert load_nist_vectors_from_file(
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400184 "AES/KAT/CBCGFSbox128.rsp",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400185 "DECRYPT",
186 ["key", "iv", "ciphertext", "plaintext"],
187 ) == [
188 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400189 b"00000000000000000000000000000000",
190 b"00000000000000000000000000000000",
191 b"0336763e966d92595a567cc9ce537f5e",
192 b"f34481ec3cc627bacd5dc3fb08f273e6",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400193 ),
194 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400195 b"00000000000000000000000000000000",
196 b"00000000000000000000000000000000",
197 b"a9a1631bf4996954ebc093957b234589",
198 b"9798c4640bad75c7c3227db910174e72",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400199 ),
200 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400201 b"00000000000000000000000000000000",
202 b"00000000000000000000000000000000",
203 b"ff4f8391a6a40ca5b25d23bedd44a597",
204 b"96ab5c2ff612d9dfaae8c31f30c42168",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400205 ),
206 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400207 b"00000000000000000000000000000000",
208 b"00000000000000000000000000000000",
209 b"dc43be40be0e53712f7e2bf5ca707209",
210 b"6a118a874519e64e9963798a503f1d35",
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400211 ),
212 (
Alex Gaynorf312a5c2013-08-10 15:23:38 -0400213 b"00000000000000000000000000000000",
214 b"00000000000000000000000000000000",
215 b"92beedab1895a94faa69b632e5cc47ce",
216 b"cb9fceec81286ca3e989bd979b0cb284",
217 ),
218 (
219 b"00000000000000000000000000000000",
220 b"00000000000000000000000000000000",
221 b"459264f4798f6a78bacb89c15ed3d601",
222 b"b26aeb1874e47ca8358ff22378f09144"
223 ),
224 (
225 b"00000000000000000000000000000000",
226 b"00000000000000000000000000000000",
227 b"08a4e2efec8a8e3312ca7460b9040bbf",
228 b"58c8e00b2631686d54eab84b91f0aca1"
Donald Stufft9e1a48b2013-08-09 00:32:30 -0400229 ),
230 ]