]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /***************************************************************************** |
2 | Copyright (c) 2017-2018, Intel Corporation | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
5 | modification, are permitted provided that the following conditions are met: | |
6 | ||
7 | * Redistributions of source code must retain the above copyright notice, | |
8 | this list of conditions and the following disclaimer. | |
9 | * Redistributions in binary form must reproduce the above copyright | |
10 | notice, this list of conditions and the following disclaimer in the | |
11 | documentation and/or other materials provided with the distribution. | |
12 | * Neither the name of Intel Corporation nor the names of its contributors | |
13 | may be used to endorse or promote products derived from this software | |
14 | without specific prior written permission. | |
15 | ||
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | *****************************************************************************/ | |
27 | ||
28 | #include <stdint.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdio.h> | |
31 | #include <string.h> | |
32 | #include <assert.h> | |
33 | ||
34 | #include <intel-ipsec-mb.h> | |
35 | ||
36 | #include "gcm_ctr_vectors_test.h" | |
37 | ||
38 | #ifndef DIM | |
39 | #define DIM(x) (sizeof(x) / sizeof(x[0])) | |
40 | #endif | |
41 | ||
42 | int des_test(const enum arch_type arch, struct MB_MGR *mb_mgr); | |
43 | ||
44 | struct des_vector { | |
45 | const uint8_t *K; /* key */ | |
46 | const uint8_t *IV; /* initialization vector */ | |
47 | const uint8_t *P; /* plain text */ | |
48 | uint64_t Plen; /* plain text length */ | |
49 | const uint8_t *C; /* cipher text - same length as plain text */ | |
50 | }; | |
51 | ||
9f95a23c TL |
52 | struct des3_vector { |
53 | const uint8_t *K1; /* key */ | |
54 | const uint8_t *K2; /* key */ | |
55 | const uint8_t *K3; /* key */ | |
56 | const uint8_t *IV; /* initialization vector */ | |
57 | const uint8_t *P; /* plain text */ | |
58 | uint64_t Plen; /* plain text length */ | |
59 | const uint8_t *C; /* cipher text - same length as plain text */ | |
60 | }; | |
61 | ||
11fdf7f2 TL |
62 | /* CM-SP-SECv3.1-I07-170111 I.7 */ |
63 | static const uint8_t K1[] = { | |
64 | 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab | |
65 | }; | |
66 | static const uint8_t IV1[] = { | |
67 | 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a | |
68 | }; | |
69 | static const uint8_t P1[] = { | |
70 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
71 | 0x08, 0x09, 0x0a, 0x0b, 0x88, 0x41, 0x65, 0x06 | |
72 | }; | |
73 | static const uint8_t C1[] = { | |
74 | 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67, | |
75 | 0x9f, 0x04, 0xd1, 0xb6, 0x41, 0x3d, 0x4e, 0xed | |
76 | }; | |
77 | ||
78 | static const uint8_t K2[] = { | |
79 | 0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e | |
80 | }; | |
81 | static const uint8_t IV2[] = { | |
82 | 0x02, 0xa8, 0x11, 0x77, 0x4d, 0xcd, 0xe1, 0x3b | |
83 | }; | |
84 | static const uint8_t P2[] = { | |
85 | 0x05, 0xef, 0xf7, 0x00, 0xe9, 0xa1, 0x3a, 0xe5, | |
86 | 0xca, 0x0b, 0xcb, 0xd0, 0x48, 0x47, 0x64, 0xbd, | |
87 | 0x1f, 0x23, 0x1e, 0xa8, 0x1c, 0x7b, 0x64, 0xc5, | |
88 | 0x14, 0x73, 0x5a, 0xc5, 0x5e, 0x4b, 0x79, 0x63, | |
89 | 0x3b, 0x70, 0x64, 0x24, 0x11, 0x9e, 0x09, 0xdc, | |
90 | 0xaa, 0xd4, 0xac, 0xf2, 0x1b, 0x10, 0xaf, 0x3b, | |
91 | 0x33, 0xcd, 0xe3, 0x50, 0x48, 0x47, 0x15, 0x5c, | |
92 | 0xbb, 0x6f, 0x22, 0x19, 0xba, 0x9b, 0x7d, 0xf5 | |
93 | ||
94 | }; | |
95 | static const uint8_t C2[] = { | |
96 | 0xf3, 0x31, 0x8d, 0x01, 0x19, 0x4d, 0xa8, 0x00, | |
97 | 0xa4, 0x2c, 0x10, 0xb5, 0x33, 0xd6, 0xbc, 0x11, | |
98 | 0x97, 0x59, 0x2d, 0xcc, 0x9b, 0x5d, 0x35, 0x9a, | |
99 | 0xc3, 0x04, 0x5d, 0x07, 0x4c, 0x86, 0xbf, 0x72, | |
100 | 0xe5, 0x1a, 0x72, 0x25, 0x82, 0x22, 0x54, 0x03, | |
101 | 0xde, 0x8b, 0x7a, 0x58, 0x5c, 0x6c, 0x28, 0xdf, | |
102 | 0x41, 0x0e, 0x38, 0xd6, 0x2a, 0x86, 0xe3, 0x4f, | |
103 | 0xa2, 0x7c, 0x22, 0x39, 0x60, 0x06, 0x03, 0x6f | |
104 | }; | |
105 | ||
106 | static struct des_vector vectors[] = { | |
107 | {K1, IV1, P1, sizeof(P1), C1}, | |
108 | {K2, IV2, P2, sizeof(P2), C2}, | |
109 | }; | |
110 | ||
111 | /* CM-SP-SECv3.1-I07-170111 I.7 */ | |
112 | static const uint8_t DK1[] = { | |
113 | 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab | |
114 | }; | |
115 | static const uint8_t DIV1[] = { | |
116 | 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a | |
117 | }; | |
118 | static const uint8_t DP1[] = { | |
119 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
120 | 0x08, 0x09, 0x0a, 0x0b, 0x88, 0x41, 0x65, 0x06 | |
121 | }; | |
122 | static const uint8_t DC1[] = { | |
123 | 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67, | |
124 | 0x9f, 0x04, 0xd1, 0xb6, 0x41, 0x3d, 0x4e, 0xed | |
125 | }; | |
126 | ||
127 | static const uint8_t DK2[] = { | |
128 | 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab | |
129 | }; | |
130 | static const uint8_t DIV2[] = { | |
131 | 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a | |
132 | }; | |
133 | static const uint8_t DP2[] = { | |
134 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
135 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x91, | |
136 | 0xd2, 0xd1, 0x9f | |
137 | }; | |
138 | static const uint8_t DC2[] = { | |
139 | 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67, | |
140 | 0x51, 0x47, 0x46, 0x86, 0x8a, 0x71, 0xe5, 0x77, | |
141 | 0xef, 0xac, 0x88 | |
142 | }; | |
143 | ||
144 | static const uint8_t DK3[] = { | |
145 | 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab | |
146 | }; | |
147 | static const uint8_t DIV3[] = { | |
148 | 0x51, 0x47, 0x46, 0x86, 0x8a, 0x71, 0xe5, 0x77 | |
149 | }; | |
150 | static const uint8_t DP3[] = { | |
151 | 0xd2, 0xd1, 0x9f | |
152 | }; | |
153 | static const uint8_t DC3[] = { | |
154 | 0xef, 0xac, 0x88 | |
155 | }; | |
156 | ||
9f95a23c | 157 | |
11fdf7f2 TL |
158 | static struct des_vector docsis_vectors[] = { |
159 | {DK1, DIV1, DP1, sizeof(DP1), DC1}, | |
160 | {DK2, DIV2, DP2, sizeof(DP2), DC2}, | |
161 | {DK3, DIV3, DP3, sizeof(DP3), DC3}, | |
162 | }; | |
163 | ||
9f95a23c TL |
164 | /* 3DES vectors - 2x and 3x keys */ |
165 | ||
166 | static const uint8_t D3K1_1[] = { | |
167 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
168 | }; | |
169 | ||
170 | static const uint8_t D3K2_1[] = { | |
171 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
172 | }; | |
173 | ||
174 | static const uint8_t D3K3_1[] = { | |
175 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
176 | }; | |
177 | ||
178 | static const uint8_t D3IV_1[] = { | |
179 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
180 | }; | |
181 | ||
182 | ||
183 | static const uint8_t D3PT_1[] = { | |
184 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
185 | }; | |
186 | ||
187 | static const uint8_t D3CT_1[] = { | |
188 | 0xdf, 0x0b, 0x6c, 0x9c, 0x31, 0xcd, 0x0c, 0xe4 | |
189 | }; | |
190 | ||
191 | #define D3PT_LEN_1 8 | |
192 | ||
193 | static const uint8_t D3K1_2[] = { | |
194 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
195 | }; | |
196 | ||
197 | static const uint8_t D3K2_2[] = { | |
198 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
199 | }; | |
200 | ||
201 | static const uint8_t D3K3_2[] = { | |
202 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
203 | }; | |
204 | ||
205 | static const uint8_t D3IV_2[] = { | |
206 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
207 | }; | |
208 | ||
209 | static const uint8_t D3PT_2[] = { | |
210 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
211 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
212 | }; | |
213 | ||
214 | static const uint8_t D3CT_2[] = { | |
215 | 0xdd, 0xad, 0xa1, 0x61, 0xe8, 0xd7, 0x96, 0x73, | |
216 | 0xed, 0x75, 0x32, 0xe5, 0x92, 0x23, 0xcd, 0x0d | |
217 | }; | |
218 | ||
219 | #define D3PT_LEN_2 16 | |
220 | ||
221 | static const uint8_t D3K1_3[] = { | |
222 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
223 | }; | |
224 | ||
225 | static const uint8_t D3K2_3[] = { | |
226 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
227 | }; | |
228 | ||
229 | static const uint8_t D3K3_3[] = { | |
230 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 | |
231 | }; | |
232 | ||
233 | static const uint8_t D3IV_3[] = { | |
234 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
235 | }; | |
236 | ||
237 | static const uint8_t D3PT_3[] = { | |
238 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
239 | }; | |
240 | ||
241 | static const uint8_t D3CT_3[] = { | |
242 | 0x58, 0xed, 0x24, 0x8f, 0x77, 0xf6, 0xb1, 0x9e | |
243 | }; | |
244 | ||
245 | #define D3PT_LEN_3 8 | |
246 | ||
247 | static const uint8_t D3K1_4[] = { | |
248 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
249 | }; | |
250 | ||
251 | static const uint8_t D3K2_4[] = { | |
252 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
253 | }; | |
254 | ||
255 | static const uint8_t D3K3_4[] = { | |
256 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 | |
257 | }; | |
258 | ||
259 | static const uint8_t D3IV_4[] = { | |
260 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 | |
261 | }; | |
262 | ||
263 | static const uint8_t D3PT_4[] = { | |
264 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
265 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
266 | }; | |
267 | ||
268 | static const uint8_t D3CT_4[] = { | |
269 | 0x89, 0x4b, 0xc3, 0x08, 0x54, 0x26, 0xa4, 0x41, | |
270 | 0xf2, 0x7f, 0x73, 0xae, 0x26, 0xab, 0xbf, 0x74 | |
271 | }; | |
272 | ||
273 | #define D3PT_LEN_4 16 | |
274 | ||
275 | static struct des3_vector des3_vectors[] = { | |
276 | { D3K1_1, D3K2_1, D3K3_1, D3IV_1, D3PT_1, D3PT_LEN_1, D3CT_1 }, | |
277 | { D3K1_2, D3K2_2, D3K3_2, D3IV_2, D3PT_2, D3PT_LEN_2, D3CT_2 }, | |
278 | { D3K1_3, D3K2_3, D3K3_3, D3IV_3, D3PT_3, D3PT_LEN_3, D3CT_3 }, | |
279 | { D3K1_4, D3K2_4, D3K3_4, D3IV_4, D3PT_4, D3PT_LEN_4, D3CT_4 }, | |
280 | }; | |
281 | ||
11fdf7f2 TL |
282 | static int |
283 | test_des_many(struct MB_MGR *mb_mgr, | |
284 | const uint64_t *ks, | |
9f95a23c TL |
285 | const uint64_t *ks2, |
286 | const uint64_t *ks3, | |
11fdf7f2 TL |
287 | const void *iv, |
288 | const uint8_t *in_text, | |
289 | const uint8_t *out_text, | |
290 | unsigned text_len, | |
291 | int dir, | |
292 | int order, | |
293 | JOB_CIPHER_MODE cipher, | |
294 | const int in_place, | |
295 | const int num_jobs) | |
296 | { | |
297 | const void *ks_ptr[3]; /* 3DES */ | |
298 | struct JOB_AES_HMAC *job; | |
299 | uint8_t padding[16]; | |
300 | uint8_t **targets = malloc(num_jobs * sizeof(void *)); | |
301 | int i, jobs_rx = 0, ret = -1; | |
302 | ||
303 | assert(targets != NULL); | |
304 | ||
305 | memset(padding, -1, sizeof(padding)); | |
306 | ||
307 | for (i = 0; i < num_jobs; i++) { | |
308 | targets[i] = malloc(text_len + (sizeof(padding) * 2)); | |
309 | memset(targets[i], -1, text_len + (sizeof(padding) * 2)); | |
310 | if (in_place) { | |
311 | /* copy input text to the allocated buffer */ | |
312 | memcpy(targets[i] + sizeof(padding), in_text, text_len); | |
313 | } | |
314 | } | |
315 | ||
9f95a23c TL |
316 | /* Used in 3DES only */ |
317 | ks_ptr[0] = ks; | |
318 | ks_ptr[1] = ks2; | |
319 | ks_ptr[2] = ks3; | |
11fdf7f2 TL |
320 | |
321 | /* flush the scheduler */ | |
322 | while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) | |
323 | ; | |
324 | ||
325 | for (i = 0; i < num_jobs; i++) { | |
326 | job = IMB_GET_NEXT_JOB(mb_mgr); | |
327 | job->cipher_direction = dir; | |
328 | job->chain_order = order; | |
329 | if (!in_place) { | |
330 | job->dst = targets[i] + sizeof(padding); | |
331 | job->src = in_text; | |
332 | } else { | |
333 | job->dst = targets[i] + sizeof(padding); | |
334 | job->src = targets[i] + sizeof(padding); | |
335 | } | |
336 | job->cipher_mode = cipher; | |
337 | if (cipher == DES3) { | |
338 | job->aes_enc_key_expanded = (const void *) ks_ptr; | |
339 | job->aes_dec_key_expanded = (const void *) ks_ptr; | |
9f95a23c | 340 | job->aes_key_len_in_bytes = 24; /* 3x keys only */ |
11fdf7f2 TL |
341 | } else { |
342 | job->aes_enc_key_expanded = ks; | |
343 | job->aes_dec_key_expanded = ks; | |
9f95a23c | 344 | job->aes_key_len_in_bytes = 8; |
11fdf7f2 | 345 | } |
11fdf7f2 TL |
346 | job->iv = iv; |
347 | job->iv_len_in_bytes = 8; | |
348 | job->cipher_start_src_offset_in_bytes = 0; | |
349 | job->msg_len_to_cipher_in_bytes = text_len; | |
350 | job->user_data = (void *)((uint64_t)i); | |
351 | ||
352 | job->hash_alg = NULL_HASH; | |
353 | ||
354 | job = IMB_SUBMIT_JOB(mb_mgr); | |
355 | if (job != NULL) { | |
356 | const int num = (const int)((uint64_t)job->user_data); | |
357 | ||
358 | jobs_rx++; | |
359 | if (job->status != STS_COMPLETED) { | |
360 | printf("%d error status:%d, job %d", | |
361 | __LINE__, job->status, num); | |
362 | goto end; | |
363 | } | |
364 | if (memcmp(out_text, targets[num] + sizeof(padding), | |
365 | text_len)) { | |
366 | printf("%d mismatched\n", num); | |
367 | goto end; | |
368 | } | |
369 | if (memcmp(padding, targets[num], sizeof(padding))) { | |
370 | printf("%d overwrite head\n", num); | |
371 | goto end; | |
372 | } | |
373 | if (memcmp(padding, | |
374 | targets[num] + sizeof(padding) + text_len, | |
375 | sizeof(padding))) { | |
376 | printf("%d overwrite tail\n", num); | |
377 | goto end; | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) { | |
383 | const int num = (const int)((uint64_t)job->user_data); | |
384 | ||
385 | jobs_rx++; | |
386 | if (job->status != STS_COMPLETED) { | |
387 | printf("%d Error status:%d, job %d", | |
388 | __LINE__, job->status, num); | |
389 | goto end; | |
390 | } | |
391 | if (memcmp(out_text, targets[num] + sizeof(padding), | |
392 | text_len)) { | |
393 | printf("%d mismatched\n", num); | |
394 | goto end; | |
395 | } | |
396 | if (memcmp(padding, targets[num], sizeof(padding))) { | |
397 | printf("%d overwrite head\n", num); | |
398 | goto end; | |
399 | } | |
400 | if (memcmp(padding, targets[num] + sizeof(padding) + text_len, | |
401 | sizeof(padding))) { | |
402 | printf("%d overwrite tail\n", num); | |
403 | goto end; | |
404 | } | |
405 | } | |
406 | ||
407 | if (jobs_rx != num_jobs) { | |
408 | printf("Expected %d jobs, received %d\n", num_jobs, jobs_rx); | |
409 | goto end; | |
410 | } | |
411 | ret = 0; | |
412 | ||
413 | end: | |
414 | while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) | |
415 | ; | |
416 | ||
417 | for (i = 0; i < num_jobs; i++) | |
418 | free(targets[i]); | |
419 | free(targets); | |
420 | return ret; | |
421 | } | |
422 | ||
423 | static int | |
424 | test_des_one(struct MB_MGR *mb_mgr, | |
425 | const uint64_t *ks, | |
9f95a23c TL |
426 | const uint64_t *ks2, |
427 | const uint64_t *ks3, | |
11fdf7f2 TL |
428 | const void *iv, |
429 | const uint8_t *in_text, | |
430 | const uint8_t *out_text, | |
431 | unsigned text_len, | |
432 | int dir, | |
433 | int order, | |
434 | JOB_CIPHER_MODE cipher, | |
435 | const int in_place) | |
436 | { | |
437 | const void *ks_ptr[3]; /* 3DES */ | |
438 | struct JOB_AES_HMAC *job; | |
439 | uint8_t padding[16]; | |
440 | uint8_t *target = malloc(text_len + (sizeof(padding) * 2)); | |
441 | int ret = -1; | |
442 | ||
443 | assert(target != NULL); | |
444 | ||
445 | memset(target, -1, text_len + (sizeof(padding) * 2)); | |
446 | memset(padding, -1, sizeof(padding)); | |
447 | ||
448 | if (in_place) { | |
449 | /* copy input text to the allocated buffer */ | |
450 | memcpy(target + sizeof(padding), in_text, text_len); | |
451 | } | |
452 | ||
9f95a23c TL |
453 | /* Used in 3DES only */ |
454 | ks_ptr[0] = ks; | |
455 | ks_ptr[1] = ks2; | |
456 | ks_ptr[2] = ks3; | |
11fdf7f2 TL |
457 | |
458 | while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) | |
459 | ; | |
460 | ||
461 | job = IMB_GET_NEXT_JOB(mb_mgr); | |
462 | job->cipher_direction = dir; | |
463 | job->chain_order = order; | |
464 | if (!in_place) { | |
465 | job->dst = target + sizeof(padding); | |
466 | job->src = in_text; | |
467 | } else { | |
468 | job->dst = target + sizeof(padding); | |
469 | job->src = target + sizeof(padding); | |
470 | } | |
471 | job->cipher_mode = cipher; | |
472 | if (cipher == DES3) { | |
473 | job->aes_enc_key_expanded = (const void *) ks_ptr; | |
474 | job->aes_dec_key_expanded = (const void *) ks_ptr; | |
9f95a23c | 475 | job->aes_key_len_in_bytes = 24; |
11fdf7f2 TL |
476 | } else { |
477 | job->aes_enc_key_expanded = ks; | |
478 | job->aes_dec_key_expanded = ks; | |
9f95a23c | 479 | job->aes_key_len_in_bytes = 8; |
11fdf7f2 | 480 | } |
11fdf7f2 TL |
481 | job->iv = iv; |
482 | job->iv_len_in_bytes = 8; | |
483 | job->cipher_start_src_offset_in_bytes = 0; | |
484 | job->msg_len_to_cipher_in_bytes = text_len; | |
485 | ||
486 | job->hash_alg = NULL_HASH; | |
487 | ||
488 | job = IMB_SUBMIT_JOB(mb_mgr); | |
489 | if (job) { | |
490 | printf("%d Unexpected return from submit_job\n", __LINE__); | |
491 | goto end; | |
492 | } | |
493 | job = IMB_FLUSH_JOB(mb_mgr); | |
494 | if (!job) { | |
495 | printf("%d Unexpected null return from flush_job\n", __LINE__); | |
496 | goto end; | |
497 | } | |
498 | if (job->status != STS_COMPLETED) { | |
499 | printf("%d Error status:%d", __LINE__, job->status); | |
500 | goto end; | |
501 | } | |
502 | if (memcmp(out_text, target + sizeof(padding), text_len)) { | |
503 | printf("mismatched\n"); | |
504 | goto end; | |
505 | } | |
506 | if (memcmp(padding, target, sizeof(padding))) { | |
507 | printf("overwrite head\n"); | |
508 | goto end; | |
509 | } | |
510 | if (memcmp(padding, target + sizeof(padding) + text_len, | |
511 | sizeof(padding))) { | |
512 | printf("overwrite tail\n"); | |
513 | goto end; | |
514 | } | |
515 | ret = 0; | |
516 | end: | |
517 | while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) | |
518 | ; | |
519 | ||
520 | free(target); | |
521 | return ret; | |
522 | } | |
523 | ||
524 | static int | |
525 | test_des(struct MB_MGR *mb_mgr, | |
526 | const uint64_t *ks, | |
9f95a23c TL |
527 | const uint64_t *ks2, |
528 | const uint64_t *ks3, | |
11fdf7f2 TL |
529 | const void *iv, |
530 | const uint8_t *in_text, | |
531 | const uint8_t *out_text, | |
532 | unsigned text_len, | |
533 | int dir, | |
534 | int order, | |
535 | JOB_CIPHER_MODE cipher, | |
536 | const int in_place) | |
537 | { | |
538 | int ret = 0; | |
539 | ||
9f95a23c TL |
540 | if (cipher == DES3) { |
541 | if (ks2 == NULL && ks3 == NULL) { | |
542 | ret |= test_des_one(mb_mgr, ks, ks, ks, iv, in_text, | |
543 | out_text, text_len, dir, order, | |
544 | cipher, in_place); | |
545 | ret |= test_des_many(mb_mgr, ks, ks, ks, iv, in_text, | |
546 | out_text, text_len, dir, order, | |
547 | cipher, in_place, 32); | |
548 | } else { | |
549 | ret |= test_des_one(mb_mgr, ks, ks2, ks3, iv, in_text, | |
550 | out_text, text_len, dir, order, | |
551 | cipher, in_place); | |
552 | ret |= test_des_many(mb_mgr, ks, ks2, ks3, iv, in_text, | |
553 | out_text, text_len, dir, order, | |
554 | cipher, in_place, 32); | |
555 | } | |
556 | } else { | |
557 | ret |= test_des_one(mb_mgr, ks, NULL, NULL, iv, in_text, | |
558 | out_text, text_len, dir, order, cipher, | |
559 | in_place); | |
560 | ret |= test_des_many(mb_mgr, ks, NULL, NULL, iv, in_text, | |
561 | out_text, text_len, dir, order, cipher, | |
562 | in_place, 32); | |
563 | } | |
11fdf7f2 TL |
564 | return ret; |
565 | } | |
566 | ||
567 | static int | |
568 | test_des_vectors(struct MB_MGR *mb_mgr, const int vec_cnt, | |
569 | const struct des_vector *vec_tab, const char *banner, | |
570 | const JOB_CIPHER_MODE cipher) | |
571 | { | |
572 | int vect, errors = 0; | |
573 | uint64_t ks[16]; | |
574 | ||
575 | printf("%s:\n", banner); | |
576 | for (vect = 0; vect < vec_cnt; vect++) { | |
577 | #ifdef DEBUG | |
578 | printf("Standard vector %d/%d PTLen:%d\n", | |
579 | vect + 1, vec_cnt, | |
580 | (int) vec_tab[vect].Plen); | |
581 | #else | |
582 | printf("."); | |
583 | #endif | |
584 | des_key_schedule(ks, vec_tab[vect].K); | |
585 | ||
9f95a23c | 586 | if (test_des(mb_mgr, ks, NULL, NULL, |
11fdf7f2 TL |
587 | vec_tab[vect].IV, |
588 | vec_tab[vect].P, vec_tab[vect].C, | |
589 | (unsigned) vec_tab[vect].Plen, | |
590 | ENCRYPT, CIPHER_HASH, cipher, 0)) { | |
591 | printf("error #%d encrypt\n", vect + 1); | |
592 | errors++; | |
593 | } | |
594 | ||
9f95a23c | 595 | if (test_des(mb_mgr, ks, NULL, NULL, |
11fdf7f2 TL |
596 | vec_tab[vect].IV, |
597 | vec_tab[vect].C, vec_tab[vect].P, | |
598 | (unsigned) vec_tab[vect].Plen, | |
599 | DECRYPT, HASH_CIPHER, cipher, 0)) { | |
600 | printf("error #%d decrypt\n", vect + 1); | |
601 | errors++; | |
602 | } | |
603 | ||
9f95a23c | 604 | if (test_des(mb_mgr, ks, NULL, NULL, |
11fdf7f2 TL |
605 | vec_tab[vect].IV, |
606 | vec_tab[vect].P, vec_tab[vect].C, | |
607 | (unsigned) vec_tab[vect].Plen, | |
608 | ENCRYPT, CIPHER_HASH, cipher, 1)) { | |
609 | printf("error #%d encrypt in-place\n", vect + 1); | |
610 | errors++; | |
611 | } | |
612 | ||
9f95a23c | 613 | if (test_des(mb_mgr, ks, NULL, NULL, |
11fdf7f2 TL |
614 | vec_tab[vect].IV, |
615 | vec_tab[vect].C, vec_tab[vect].P, | |
616 | (unsigned) vec_tab[vect].Plen, | |
617 | DECRYPT, HASH_CIPHER, cipher, 1)) { | |
618 | printf("error #%d decrypt in-place\n", vect + 1); | |
619 | errors++; | |
620 | } | |
621 | } | |
622 | printf("\n"); | |
623 | return errors; | |
624 | } | |
625 | ||
9f95a23c TL |
626 | static int |
627 | test_des3_vectors(struct MB_MGR *mb_mgr, const int vec_cnt, | |
628 | const struct des3_vector *vec_tab, const char *banner) | |
629 | { | |
630 | int vect, errors = 0; | |
631 | uint64_t ks1[16]; | |
632 | uint64_t ks2[16]; | |
633 | uint64_t ks3[16]; | |
634 | ||
635 | printf("%s:\n", banner); | |
636 | for (vect = 0; vect < vec_cnt; vect++) { | |
637 | #ifdef DEBUG | |
638 | printf("Standard vector %d/%d PTLen:%d\n", | |
639 | vect + 1, vec_cnt, | |
640 | (int) vec_tab[vect].Plen); | |
641 | #else | |
642 | printf("."); | |
643 | #endif | |
644 | des_key_schedule(ks1, vec_tab[vect].K1); | |
645 | des_key_schedule(ks2, vec_tab[vect].K2); | |
646 | des_key_schedule(ks3, vec_tab[vect].K3); | |
647 | ||
648 | if (test_des(mb_mgr, ks1, ks2, ks3, | |
649 | vec_tab[vect].IV, | |
650 | vec_tab[vect].P, vec_tab[vect].C, | |
651 | (unsigned) vec_tab[vect].Plen, | |
652 | ENCRYPT, CIPHER_HASH, DES3, 0)) { | |
653 | printf("error #%d encrypt\n", vect + 1); | |
654 | errors++; | |
655 | } | |
656 | ||
657 | if (test_des(mb_mgr, ks1, ks2, ks3, | |
658 | vec_tab[vect].IV, | |
659 | vec_tab[vect].C, vec_tab[vect].P, | |
660 | (unsigned) vec_tab[vect].Plen, | |
661 | DECRYPT, HASH_CIPHER, DES3, 0)) { | |
662 | printf("error #%d decrypt\n", vect + 1); | |
663 | errors++; | |
664 | } | |
665 | ||
666 | if (test_des(mb_mgr, ks1, ks2, ks3, | |
667 | vec_tab[vect].IV, | |
668 | vec_tab[vect].P, vec_tab[vect].C, | |
669 | (unsigned) vec_tab[vect].Plen, | |
670 | ENCRYPT, CIPHER_HASH, DES3, 1)) { | |
671 | printf("error #%d encrypt in-place\n", vect + 1); | |
672 | errors++; | |
673 | } | |
674 | ||
675 | if (test_des(mb_mgr, ks1, ks2, ks3, | |
676 | vec_tab[vect].IV, | |
677 | vec_tab[vect].C, vec_tab[vect].P, | |
678 | (unsigned) vec_tab[vect].Plen, | |
679 | DECRYPT, HASH_CIPHER, DES3, 1)) { | |
680 | printf("error #%d decrypt in-place\n", vect + 1); | |
681 | errors++; | |
682 | } | |
683 | } | |
684 | printf("\n"); | |
685 | return errors; | |
686 | } | |
687 | ||
11fdf7f2 TL |
688 | int |
689 | des_test(const enum arch_type arch, | |
690 | struct MB_MGR *mb_mgr) | |
691 | { | |
692 | int errors; | |
693 | ||
694 | (void) arch; /* unused */ | |
695 | ||
696 | errors = test_des_vectors(mb_mgr, DIM(vectors), vectors, | |
697 | "DES standard test vectors", DES); | |
698 | ||
699 | errors += test_des_vectors(mb_mgr, DIM(docsis_vectors), docsis_vectors, | |
700 | "DOCSIS DES standard test vectors", | |
701 | DOCSIS_DES); | |
702 | ||
9f95a23c TL |
703 | errors += test_des_vectors(mb_mgr, DIM(vectors), vectors, |
704 | "3DES (single key) standard test vectors", | |
705 | DES3); | |
706 | ||
707 | errors += test_des3_vectors(mb_mgr, DIM(des3_vectors), des3_vectors, | |
708 | "3DES (multiple keys) test vectors"); | |
11fdf7f2 TL |
709 | |
710 | if (0 == errors) | |
711 | printf("...Pass\n"); | |
712 | else | |
713 | printf("...Fail\n"); | |
714 | ||
715 | return errors; | |
716 | } |