blob: af575b376a4deb2801534a425d98c25f21965e77 [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
Alex Gaynorab53bc52013-11-12 09:37:59 -080014import os
Donald Stufft9e1a48b2013-08-09 00:32:30 -040015import textwrap
16
Paul Kehrer79c16e92013-10-18 17:44:36 -050017import pytest
18
Alex Gaynorafdddca2013-10-21 21:00:20 -070019from .utils import (
Paul Kehrerf7f6a9f2013-11-11 20:43:52 -060020 load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors,
21 load_openssl_vectors, load_hash_vectors,
Alex Gaynorafdddca2013-10-21 21:00:20 -070022)
Donald Stufft9e1a48b2013-08-09 00:32:30 -040023
24
Alex Gaynorcf5fb332013-11-11 15:39:52 -080025def test_load_nist_vectors():
Donald Stufft9e1a48b2013-08-09 00:32:30 -040026 vector_data = textwrap.dedent("""
27 # CAVS 11.1
28 # Config info for aes_values
29 # AESVS GFSbox test data for CBC
30 # State : Encrypt and Decrypt
31 # Key Length : 128
32 # Generated on Fri Apr 22 15:11:33 2011
33
34 [ENCRYPT]
35
36 COUNT = 0
37 KEY = 00000000000000000000000000000000
38 IV = 00000000000000000000000000000000
39 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
40 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
41
42 COUNT = 1
43 KEY = 00000000000000000000000000000000
44 IV = 00000000000000000000000000000000
45 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
46 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
47
48 [DECRYPT]
49
50 COUNT = 0
51 KEY = 00000000000000000000000000000000
52 IV = 00000000000000000000000000000000
53 CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e
54 PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6
55
56 COUNT = 1
57 KEY = 00000000000000000000000000000000
58 IV = 00000000000000000000000000000000
59 CIPHERTEXT = a9a1631bf4996954ebc093957b234589
60 PLAINTEXT = 9798c4640bad75c7c3227db910174e72
61 """).splitlines()
62
Alex Gaynord3ce7032013-11-11 14:46:20 -080063 assert load_nist_vectors(vector_data) == [
64 {
65 "key": b"00000000000000000000000000000000",
66 "iv": b"00000000000000000000000000000000",
67 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
68 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
69 },
70 {
71 "key": b"00000000000000000000000000000000",
72 "iv": b"00000000000000000000000000000000",
73 "plaintext": b"9798c4640bad75c7c3227db910174e72",
74 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
75 },
Alex Gaynor1fe70b12013-10-16 11:59:17 -070076 {
77 "key": b"00000000000000000000000000000000",
78 "iv": b"00000000000000000000000000000000",
79 "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6",
80 "ciphertext": b"0336763e966d92595a567cc9ce537f5e",
81 },
82 {
83 "key": b"00000000000000000000000000000000",
84 "iv": b"00000000000000000000000000000000",
85 "plaintext": b"9798c4640bad75c7c3227db910174e72",
86 "ciphertext": b"a9a1631bf4996954ebc093957b234589",
87 },
Donald Stufft9e1a48b2013-08-09 00:32:30 -040088 ]
89
90
Paul Kehrer1951bf62013-09-15 12:05:43 -050091def test_load_cryptrec_vectors():
92 vector_data = textwrap.dedent("""
93 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
94 # Download is t_camelia.txt
95
96 # Camellia with 128-bit key
97
98 K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
99
100 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
101 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
102
103 P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
104 C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3
105
106 K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
107
108 P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
109 C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C
110 """).splitlines()
111
112 assert load_cryptrec_vectors(vector_data) == [
Alex Gaynor1fe70b12013-10-16 11:59:17 -0700113 {
114 "key": b"00000000000000000000000000000000",
115 "plaintext": b"80000000000000000000000000000000",
116 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
117 },
118 {
119 "key": b"00000000000000000000000000000000",
120 "plaintext": b"40000000000000000000000000000000",
121 "ciphertext": b"48CD6419809672D2349260D89A08D3D3",
122 },
123 {
124 "key": b"10000000000000000000000000000000",
125 "plaintext": b"80000000000000000000000000000000",
126 "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C",
127 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500128 ]
129
130
Donald Stufft3359d7e2013-10-19 19:33:06 -0400131def test_load_cryptrec_vectors_invalid():
132 vector_data = textwrap.dedent("""
133 # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/
134 # Download is t_camelia.txt
135
136 # Camellia with 128-bit key
137
138 E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
139 """).splitlines()
140
141 with pytest.raises(ValueError):
142 load_cryptrec_vectors(vector_data)
143
144
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500145def test_load_openssl_vectors():
Paul Kehrer05d72142013-09-15 14:03:15 -0500146 vector_data = textwrap.dedent(
147 """
148 # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt
149 # For all CFB128 encrypts and decrypts, the transformed sequence is
150 # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec
151 # CFB128-CAMELLIA128.Encrypt
152 """
153 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
154 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
155 "14F7646187817EB586599146B82BD719:1\n"
156 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
157 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
158 "A53D28BB82DF741103EA4F921A44880B:1\n\n"
159 "# CFB128-CAMELLIA128.Decrypt\n"
160 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
161 "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:"
162 "14F7646187817EB586599146B82BD719:0\n"
163 "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:"
164 "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:"
165 "A53D28BB82DF741103EA4F921A44880B:0"
166 ).splitlines()
Paul Kehrer1951bf62013-09-15 12:05:43 -0500167
Paul Kehrer6b99a1b2013-09-24 16:50:21 -0500168 assert load_openssl_vectors(vector_data) == [
Alex Gaynor016eed12013-10-16 14:16:04 -0700169 {
170 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
171 "iv": b"000102030405060708090A0B0C0D0E0F",
172 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
173 "ciphertext": b"14F7646187817EB586599146B82BD719",
174 },
175 {
176 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
177 "iv": b"14F7646187817EB586599146B82BD719",
178 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
179 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
180 },
181 {
182 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
183 "iv": b"000102030405060708090A0B0C0D0E0F",
184 "plaintext": b"6BC1BEE22E409F96E93D7E117393172A",
185 "ciphertext": b"14F7646187817EB586599146B82BD719",
186 },
187 {
188 "key": b"2B7E151628AED2A6ABF7158809CF4F3C",
189 "iv": b"14F7646187817EB586599146B82BD719",
190 "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51",
191 "ciphertext": b"A53D28BB82DF741103EA4F921A44880B",
192 },
Paul Kehrer1951bf62013-09-15 12:05:43 -0500193 ]
194
195
Paul Kehrer69e06522013-10-18 17:28:39 -0500196def test_load_hash_vectors():
197 vector_data = textwrap.dedent("""
198
199 # http://tools.ietf.org/html/rfc1321
Paul Kehrer87cd0db2013-10-18 18:01:26 -0500200 [irrelevant]
Paul Kehrer69e06522013-10-18 17:28:39 -0500201
202 Len = 0
203 Msg = 00
204 MD = d41d8cd98f00b204e9800998ecf8427e
205
206 Len = 8
207 Msg = 61
208 MD = 0cc175b9c0f1b6a831c399e269772661
209
210 Len = 24
211 Msg = 616263
212 MD = 900150983cd24fb0d6963f7d28e17f72
213
214 Len = 112
215 Msg = 6d65737361676520646967657374
216 MD = f96b697d7cb7938d525a2f31aaf161d0
217 """).splitlines()
218 assert load_hash_vectors(vector_data) == [
Paul Kehrer79c16e92013-10-18 17:44:36 -0500219 (b"", "d41d8cd98f00b204e9800998ecf8427e"),
220 (b"61", "0cc175b9c0f1b6a831c399e269772661"),
221 (b"616263", "900150983cd24fb0d6963f7d28e17f72"),
222 (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"),
Paul Kehrer69e06522013-10-18 17:28:39 -0500223 ]
224
225
Paul Kehrer0317b042013-10-28 17:34:27 -0500226def test_load_hmac_vectors():
227 vector_data = textwrap.dedent("""
228Len = 224
229# "Jefe"
230Key = 4a656665
231# "what do ya want for nothing?"
232Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f
233MD = 750c783e6ab0b503eaa86e310a5db738
234 """).splitlines()
235 assert load_hash_vectors(vector_data) == [
236 (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
237 "750c783e6ab0b503eaa86e310a5db738",
238 b"4a656665"),
239 ]
240
241
Paul Kehrer69e06522013-10-18 17:28:39 -0500242def test_load_hash_vectors_bad_data():
243 vector_data = textwrap.dedent("""
244 # http://tools.ietf.org/html/rfc1321
245
246 Len = 0
247 Msg = 00
248 UNKNOWN=Hello World
249 """).splitlines()
250 with pytest.raises(ValueError):
251 load_hash_vectors(vector_data)
252
Alex Gaynor41172ab2013-11-12 10:00:42 -0800253
Alex Gaynorab53bc52013-11-12 09:37:59 -0800254def test_load_vectors_from_file():
255 vectors = load_vectors_from_file(
256 os.path.join("ciphers", "Blowfish", "bf-cfb.txt"),
257 load_nist_vectors,
Paul Kehrer2b758672013-10-30 09:01:38 -0500258 )
Alex Gaynorab53bc52013-11-12 09:37:59 -0800259 assert vectors == [
260 {
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800261 "key": b"0123456789ABCDEFF0E1D2C3B4A59687",
262 "iv": b"FEDCBA9876543210",
Alex Gaynorab53bc52013-11-12 09:37:59 -0800263 "plaintext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800264 b"37363534333231204E6F77206973207468652074696D6520666F722000"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800265 ),
266 "ciphertext": (
Alex Gaynorc2f45d52013-11-12 09:50:25 -0800267 b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"
Alex Gaynorab53bc52013-11-12 09:37:59 -0800268 ),
269 }
270 ]