crypto: testmgr - Fix error flow of test_comp
[linux-2.6.git] / crypto / testmgr.c
1 /*
2  * Algorithm testing framework and tests.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
6  * Copyright (c) 2007 Nokia Siemens Networks
7  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  */
15
16 #include <crypto/hash.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22
23 #include "internal.h"
24 #include "testmgr.h"
25
26 /*
27  * Need slab memory for testing (size in number of pages).
28  */
29 #define XBUFSIZE        8
30
31 /*
32  * Indexes into the xbuf to simulate cross-page access.
33  */
34 #define IDX1            32
35 #define IDX2            32400
36 #define IDX3            1
37 #define IDX4            8193
38 #define IDX5            22222
39 #define IDX6            17101
40 #define IDX7            27333
41 #define IDX8            3000
42
43 /*
44 * Used by test_cipher()
45 */
46 #define ENCRYPT 1
47 #define DECRYPT 0
48
49 struct tcrypt_result {
50         struct completion completion;
51         int err;
52 };
53
54 struct aead_test_suite {
55         struct {
56                 struct aead_testvec *vecs;
57                 unsigned int count;
58         } enc, dec;
59 };
60
61 struct cipher_test_suite {
62         struct {
63                 struct cipher_testvec *vecs;
64                 unsigned int count;
65         } enc, dec;
66 };
67
68 struct comp_test_suite {
69         struct {
70                 struct comp_testvec *vecs;
71                 unsigned int count;
72         } comp, decomp;
73 };
74
75 struct hash_test_suite {
76         struct hash_testvec *vecs;
77         unsigned int count;
78 };
79
80 struct alg_test_desc {
81         const char *alg;
82         int (*test)(const struct alg_test_desc *desc, const char *driver,
83                     u32 type, u32 mask);
84
85         union {
86                 struct aead_test_suite aead;
87                 struct cipher_test_suite cipher;
88                 struct comp_test_suite comp;
89                 struct hash_test_suite hash;
90         } suite;
91 };
92
93 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
94
95 static char *xbuf[XBUFSIZE];
96 static char *axbuf[XBUFSIZE];
97
98 static void hexdump(unsigned char *buf, unsigned int len)
99 {
100         print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
101                         16, 1,
102                         buf, len, false);
103 }
104
105 static void tcrypt_complete(struct crypto_async_request *req, int err)
106 {
107         struct tcrypt_result *res = req->data;
108
109         if (err == -EINPROGRESS)
110                 return;
111
112         res->err = err;
113         complete(&res->completion);
114 }
115
116 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
117                      unsigned int tcount)
118 {
119         const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
120         unsigned int i, j, k, temp;
121         struct scatterlist sg[8];
122         char result[64];
123         struct ahash_request *req;
124         struct tcrypt_result tresult;
125         int ret;
126         void *hash_buff;
127
128         init_completion(&tresult.completion);
129
130         req = ahash_request_alloc(tfm, GFP_KERNEL);
131         if (!req) {
132                 printk(KERN_ERR "alg: hash: Failed to allocate request for "
133                        "%s\n", algo);
134                 ret = -ENOMEM;
135                 goto out_noreq;
136         }
137         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
138                                    tcrypt_complete, &tresult);
139
140         for (i = 0; i < tcount; i++) {
141                 memset(result, 0, 64);
142
143                 hash_buff = xbuf[0];
144
145                 memcpy(hash_buff, template[i].plaintext, template[i].psize);
146                 sg_init_one(&sg[0], hash_buff, template[i].psize);
147
148                 if (template[i].ksize) {
149                         crypto_ahash_clear_flags(tfm, ~0);
150                         ret = crypto_ahash_setkey(tfm, template[i].key,
151                                                   template[i].ksize);
152                         if (ret) {
153                                 printk(KERN_ERR "alg: hash: setkey failed on "
154                                        "test %d for %s: ret=%d\n", i + 1, algo,
155                                        -ret);
156                                 goto out;
157                         }
158                 }
159
160                 ahash_request_set_crypt(req, sg, result, template[i].psize);
161                 ret = crypto_ahash_digest(req);
162                 switch (ret) {
163                 case 0:
164                         break;
165                 case -EINPROGRESS:
166                 case -EBUSY:
167                         ret = wait_for_completion_interruptible(
168                                 &tresult.completion);
169                         if (!ret && !(ret = tresult.err)) {
170                                 INIT_COMPLETION(tresult.completion);
171                                 break;
172                         }
173                         /* fall through */
174                 default:
175                         printk(KERN_ERR "alg: hash: digest failed on test %d "
176                                "for %s: ret=%d\n", i + 1, algo, -ret);
177                         goto out;
178                 }
179
180                 if (memcmp(result, template[i].digest,
181                            crypto_ahash_digestsize(tfm))) {
182                         printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
183                                i + 1, algo);
184                         hexdump(result, crypto_ahash_digestsize(tfm));
185                         ret = -EINVAL;
186                         goto out;
187                 }
188         }
189
190         j = 0;
191         for (i = 0; i < tcount; i++) {
192                 if (template[i].np) {
193                         j++;
194                         memset(result, 0, 64);
195
196                         temp = 0;
197                         sg_init_table(sg, template[i].np);
198                         for (k = 0; k < template[i].np; k++) {
199                                 sg_set_buf(&sg[k],
200                                            memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
201                                                   offset_in_page(IDX[k]),
202                                                   template[i].plaintext + temp,
203                                                   template[i].tap[k]),
204                                            template[i].tap[k]);
205                                 temp += template[i].tap[k];
206                         }
207
208                         if (template[i].ksize) {
209                                 crypto_ahash_clear_flags(tfm, ~0);
210                                 ret = crypto_ahash_setkey(tfm, template[i].key,
211                                                           template[i].ksize);
212
213                                 if (ret) {
214                                         printk(KERN_ERR "alg: hash: setkey "
215                                                "failed on chunking test %d "
216                                                "for %s: ret=%d\n", j, algo,
217                                                -ret);
218                                         goto out;
219                                 }
220                         }
221
222                         ahash_request_set_crypt(req, sg, result,
223                                                 template[i].psize);
224                         ret = crypto_ahash_digest(req);
225                         switch (ret) {
226                         case 0:
227                                 break;
228                         case -EINPROGRESS:
229                         case -EBUSY:
230                                 ret = wait_for_completion_interruptible(
231                                         &tresult.completion);
232                                 if (!ret && !(ret = tresult.err)) {
233                                         INIT_COMPLETION(tresult.completion);
234                                         break;
235                                 }
236                                 /* fall through */
237                         default:
238                                 printk(KERN_ERR "alg: hash: digest failed "
239                                        "on chunking test %d for %s: "
240                                        "ret=%d\n", j, algo, -ret);
241                                 goto out;
242                         }
243
244                         if (memcmp(result, template[i].digest,
245                                    crypto_ahash_digestsize(tfm))) {
246                                 printk(KERN_ERR "alg: hash: Chunking test %d "
247                                        "failed for %s\n", j, algo);
248                                 hexdump(result, crypto_ahash_digestsize(tfm));
249                                 ret = -EINVAL;
250                                 goto out;
251                         }
252                 }
253         }
254
255         ret = 0;
256
257 out:
258         ahash_request_free(req);
259 out_noreq:
260         return ret;
261 }
262
263 static int test_aead(struct crypto_aead *tfm, int enc,
264                      struct aead_testvec *template, unsigned int tcount)
265 {
266         const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
267         unsigned int i, j, k, n, temp;
268         int ret = 0;
269         char *q;
270         char *key;
271         struct aead_request *req;
272         struct scatterlist sg[8];
273         struct scatterlist asg[8];
274         const char *e;
275         struct tcrypt_result result;
276         unsigned int authsize;
277         void *input;
278         void *assoc;
279         char iv[MAX_IVLEN];
280
281         if (enc == ENCRYPT)
282                 e = "encryption";
283         else
284                 e = "decryption";
285
286         init_completion(&result.completion);
287
288         req = aead_request_alloc(tfm, GFP_KERNEL);
289         if (!req) {
290                 printk(KERN_ERR "alg: aead: Failed to allocate request for "
291                        "%s\n", algo);
292                 ret = -ENOMEM;
293                 goto out;
294         }
295
296         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
297                                   tcrypt_complete, &result);
298
299         for (i = 0, j = 0; i < tcount; i++) {
300                 if (!template[i].np) {
301                         j++;
302
303                         /* some tepmplates have no input data but they will
304                          * touch input
305                          */
306                         input = xbuf[0];
307                         assoc = axbuf[0];
308
309                         memcpy(input, template[i].input, template[i].ilen);
310                         memcpy(assoc, template[i].assoc, template[i].alen);
311                         if (template[i].iv)
312                                 memcpy(iv, template[i].iv, MAX_IVLEN);
313                         else
314                                 memset(iv, 0, MAX_IVLEN);
315
316                         crypto_aead_clear_flags(tfm, ~0);
317                         if (template[i].wk)
318                                 crypto_aead_set_flags(
319                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
320
321                         key = template[i].key;
322
323                         ret = crypto_aead_setkey(tfm, key,
324                                                  template[i].klen);
325                         if (!ret == template[i].fail) {
326                                 printk(KERN_ERR "alg: aead: setkey failed on "
327                                        "test %d for %s: flags=%x\n", j, algo,
328                                        crypto_aead_get_flags(tfm));
329                                 goto out;
330                         } else if (ret)
331                                 continue;
332
333                         authsize = abs(template[i].rlen - template[i].ilen);
334                         ret = crypto_aead_setauthsize(tfm, authsize);
335                         if (ret) {
336                                 printk(KERN_ERR "alg: aead: Failed to set "
337                                        "authsize to %u on test %d for %s\n",
338                                        authsize, j, algo);
339                                 goto out;
340                         }
341
342                         sg_init_one(&sg[0], input,
343                                     template[i].ilen + (enc ? authsize : 0));
344
345                         sg_init_one(&asg[0], assoc, template[i].alen);
346
347                         aead_request_set_crypt(req, sg, sg,
348                                                template[i].ilen, iv);
349
350                         aead_request_set_assoc(req, asg, template[i].alen);
351
352                         ret = enc ?
353                                 crypto_aead_encrypt(req) :
354                                 crypto_aead_decrypt(req);
355
356                         switch (ret) {
357                         case 0:
358                                 break;
359                         case -EINPROGRESS:
360                         case -EBUSY:
361                                 ret = wait_for_completion_interruptible(
362                                         &result.completion);
363                                 if (!ret && !(ret = result.err)) {
364                                         INIT_COMPLETION(result.completion);
365                                         break;
366                                 }
367                                 /* fall through */
368                         default:
369                                 printk(KERN_ERR "alg: aead: %s failed on test "
370                                        "%d for %s: ret=%d\n", e, j, algo, -ret);
371                                 goto out;
372                         }
373
374                         q = input;
375                         if (memcmp(q, template[i].result, template[i].rlen)) {
376                                 printk(KERN_ERR "alg: aead: Test %d failed on "
377                                        "%s for %s\n", j, e, algo);
378                                 hexdump(q, template[i].rlen);
379                                 ret = -EINVAL;
380                                 goto out;
381                         }
382                 }
383         }
384
385         for (i = 0, j = 0; i < tcount; i++) {
386                 if (template[i].np) {
387                         j++;
388
389                         if (template[i].iv)
390                                 memcpy(iv, template[i].iv, MAX_IVLEN);
391                         else
392                                 memset(iv, 0, MAX_IVLEN);
393
394                         crypto_aead_clear_flags(tfm, ~0);
395                         if (template[i].wk)
396                                 crypto_aead_set_flags(
397                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
398                         key = template[i].key;
399
400                         ret = crypto_aead_setkey(tfm, key, template[i].klen);
401                         if (!ret == template[i].fail) {
402                                 printk(KERN_ERR "alg: aead: setkey failed on "
403                                        "chunk test %d for %s: flags=%x\n", j,
404                                        algo, crypto_aead_get_flags(tfm));
405                                 goto out;
406                         } else if (ret)
407                                 continue;
408
409                         authsize = abs(template[i].rlen - template[i].ilen);
410
411                         ret = -EINVAL;
412                         sg_init_table(sg, template[i].np);
413                         for (k = 0, temp = 0; k < template[i].np; k++) {
414                                 if (WARN_ON(offset_in_page(IDX[k]) +
415                                             template[i].tap[k] > PAGE_SIZE))
416                                         goto out;
417
418                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
419                                     offset_in_page(IDX[k]);
420
421                                 memcpy(q, template[i].input + temp,
422                                        template[i].tap[k]);
423
424                                 n = template[i].tap[k];
425                                 if (k == template[i].np - 1 && enc)
426                                         n += authsize;
427                                 if (offset_in_page(q) + n < PAGE_SIZE)
428                                         q[n] = 0;
429
430                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
431                                 temp += template[i].tap[k];
432                         }
433
434                         ret = crypto_aead_setauthsize(tfm, authsize);
435                         if (ret) {
436                                 printk(KERN_ERR "alg: aead: Failed to set "
437                                        "authsize to %u on chunk test %d for "
438                                        "%s\n", authsize, j, algo);
439                                 goto out;
440                         }
441
442                         if (enc) {
443                                 if (WARN_ON(sg[k - 1].offset +
444                                             sg[k - 1].length + authsize >
445                                             PAGE_SIZE)) {
446                                         ret = -EINVAL;
447                                         goto out;
448                                 }
449
450                                 sg[k - 1].length += authsize;
451                         }
452
453                         sg_init_table(asg, template[i].anp);
454                         for (k = 0, temp = 0; k < template[i].anp; k++) {
455                                 sg_set_buf(&asg[k],
456                                            memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
457                                                   offset_in_page(IDX[k]),
458                                                   template[i].assoc + temp,
459                                                   template[i].atap[k]),
460                                            template[i].atap[k]);
461                                 temp += template[i].atap[k];
462                         }
463
464                         aead_request_set_crypt(req, sg, sg,
465                                                template[i].ilen,
466                                                iv);
467
468                         aead_request_set_assoc(req, asg, template[i].alen);
469
470                         ret = enc ?
471                                 crypto_aead_encrypt(req) :
472                                 crypto_aead_decrypt(req);
473
474                         switch (ret) {
475                         case 0:
476                                 break;
477                         case -EINPROGRESS:
478                         case -EBUSY:
479                                 ret = wait_for_completion_interruptible(
480                                         &result.completion);
481                                 if (!ret && !(ret = result.err)) {
482                                         INIT_COMPLETION(result.completion);
483                                         break;
484                                 }
485                                 /* fall through */
486                         default:
487                                 printk(KERN_ERR "alg: aead: %s failed on "
488                                        "chunk test %d for %s: ret=%d\n", e, j,
489                                        algo, -ret);
490                                 goto out;
491                         }
492
493                         ret = -EINVAL;
494                         for (k = 0, temp = 0; k < template[i].np; k++) {
495                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
496                                     offset_in_page(IDX[k]);
497
498                                 n = template[i].tap[k];
499                                 if (k == template[i].np - 1)
500                                         n += enc ? authsize : -authsize;
501
502                                 if (memcmp(q, template[i].result + temp, n)) {
503                                         printk(KERN_ERR "alg: aead: Chunk "
504                                                "test %d failed on %s at page "
505                                                "%u for %s\n", j, e, k, algo);
506                                         hexdump(q, n);
507                                         goto out;
508                                 }
509
510                                 q += n;
511                                 if (k == template[i].np - 1 && !enc) {
512                                         if (memcmp(q, template[i].input +
513                                                       temp + n, authsize))
514                                                 n = authsize;
515                                         else
516                                                 n = 0;
517                                 } else {
518                                         for (n = 0; offset_in_page(q + n) &&
519                                                     q[n]; n++)
520                                                 ;
521                                 }
522                                 if (n) {
523                                         printk(KERN_ERR "alg: aead: Result "
524                                                "buffer corruption in chunk "
525                                                "test %d on %s at page %u for "
526                                                "%s: %u bytes:\n", j, e, k,
527                                                algo, n);
528                                         hexdump(q, n);
529                                         goto out;
530                                 }
531
532                                 temp += template[i].tap[k];
533                         }
534                 }
535         }
536
537         ret = 0;
538
539 out:
540         aead_request_free(req);
541         return ret;
542 }
543
544 static int test_cipher(struct crypto_cipher *tfm, int enc,
545                        struct cipher_testvec *template, unsigned int tcount)
546 {
547         const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
548         unsigned int i, j, k;
549         int ret;
550         char *q;
551         const char *e;
552         void *data;
553
554         if (enc == ENCRYPT)
555                 e = "encryption";
556         else
557                 e = "decryption";
558
559         j = 0;
560         for (i = 0; i < tcount; i++) {
561                 if (template[i].np)
562                         continue;
563
564                 j++;
565
566                 data = xbuf[0];
567                 memcpy(data, template[i].input, template[i].ilen);
568
569                 crypto_cipher_clear_flags(tfm, ~0);
570                 if (template[i].wk)
571                         crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
572
573                 ret = crypto_cipher_setkey(tfm, template[i].key,
574                                            template[i].klen);
575                 if (!ret == template[i].fail) {
576                         printk(KERN_ERR "alg: cipher: setkey failed "
577                                "on test %d for %s: flags=%x\n", j,
578                                algo, crypto_cipher_get_flags(tfm));
579                         goto out;
580                 } else if (ret)
581                         continue;
582
583                 for (k = 0; k < template[i].ilen;
584                      k += crypto_cipher_blocksize(tfm)) {
585                         if (enc)
586                                 crypto_cipher_encrypt_one(tfm, data + k,
587                                                           data + k);
588                         else
589                                 crypto_cipher_decrypt_one(tfm, data + k,
590                                                           data + k);
591                 }
592
593                 q = data;
594                 if (memcmp(q, template[i].result, template[i].rlen)) {
595                         printk(KERN_ERR "alg: cipher: Test %d failed "
596                                "on %s for %s\n", j, e, algo);
597                         hexdump(q, template[i].rlen);
598                         ret = -EINVAL;
599                         goto out;
600                 }
601         }
602
603         ret = 0;
604
605 out:
606         return ret;
607 }
608
609 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
610                          struct cipher_testvec *template, unsigned int tcount)
611 {
612         const char *algo =
613                 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
614         unsigned int i, j, k, n, temp;
615         int ret;
616         char *q;
617         struct ablkcipher_request *req;
618         struct scatterlist sg[8];
619         const char *e;
620         struct tcrypt_result result;
621         void *data;
622         char iv[MAX_IVLEN];
623
624         if (enc == ENCRYPT)
625                 e = "encryption";
626         else
627                 e = "decryption";
628
629         init_completion(&result.completion);
630
631         req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
632         if (!req) {
633                 printk(KERN_ERR "alg: skcipher: Failed to allocate request "
634                        "for %s\n", algo);
635                 ret = -ENOMEM;
636                 goto out;
637         }
638
639         ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
640                                         tcrypt_complete, &result);
641
642         j = 0;
643         for (i = 0; i < tcount; i++) {
644                 if (template[i].iv)
645                         memcpy(iv, template[i].iv, MAX_IVLEN);
646                 else
647                         memset(iv, 0, MAX_IVLEN);
648
649                 if (!(template[i].np)) {
650                         j++;
651
652                         data = xbuf[0];
653                         memcpy(data, template[i].input, template[i].ilen);
654
655                         crypto_ablkcipher_clear_flags(tfm, ~0);
656                         if (template[i].wk)
657                                 crypto_ablkcipher_set_flags(
658                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
659
660                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
661                                                        template[i].klen);
662                         if (!ret == template[i].fail) {
663                                 printk(KERN_ERR "alg: skcipher: setkey failed "
664                                        "on test %d for %s: flags=%x\n", j,
665                                        algo, crypto_ablkcipher_get_flags(tfm));
666                                 goto out;
667                         } else if (ret)
668                                 continue;
669
670                         sg_init_one(&sg[0], data, template[i].ilen);
671
672                         ablkcipher_request_set_crypt(req, sg, sg,
673                                                      template[i].ilen, iv);
674                         ret = enc ?
675                                 crypto_ablkcipher_encrypt(req) :
676                                 crypto_ablkcipher_decrypt(req);
677
678                         switch (ret) {
679                         case 0:
680                                 break;
681                         case -EINPROGRESS:
682                         case -EBUSY:
683                                 ret = wait_for_completion_interruptible(
684                                         &result.completion);
685                                 if (!ret && !((ret = result.err))) {
686                                         INIT_COMPLETION(result.completion);
687                                         break;
688                                 }
689                                 /* fall through */
690                         default:
691                                 printk(KERN_ERR "alg: skcipher: %s failed on "
692                                        "test %d for %s: ret=%d\n", e, j, algo,
693                                        -ret);
694                                 goto out;
695                         }
696
697                         q = data;
698                         if (memcmp(q, template[i].result, template[i].rlen)) {
699                                 printk(KERN_ERR "alg: skcipher: Test %d "
700                                        "failed on %s for %s\n", j, e, algo);
701                                 hexdump(q, template[i].rlen);
702                                 ret = -EINVAL;
703                                 goto out;
704                         }
705                 }
706         }
707
708         j = 0;
709         for (i = 0; i < tcount; i++) {
710
711                 if (template[i].iv)
712                         memcpy(iv, template[i].iv, MAX_IVLEN);
713                 else
714                         memset(iv, 0, MAX_IVLEN);
715
716                 if (template[i].np) {
717                         j++;
718
719                         crypto_ablkcipher_clear_flags(tfm, ~0);
720                         if (template[i].wk)
721                                 crypto_ablkcipher_set_flags(
722                                         tfm, CRYPTO_TFM_REQ_WEAK_KEY);
723
724                         ret = crypto_ablkcipher_setkey(tfm, template[i].key,
725                                                        template[i].klen);
726                         if (!ret == template[i].fail) {
727                                 printk(KERN_ERR "alg: skcipher: setkey failed "
728                                        "on chunk test %d for %s: flags=%x\n",
729                                        j, algo,
730                                        crypto_ablkcipher_get_flags(tfm));
731                                 goto out;
732                         } else if (ret)
733                                 continue;
734
735                         temp = 0;
736                         ret = -EINVAL;
737                         sg_init_table(sg, template[i].np);
738                         for (k = 0; k < template[i].np; k++) {
739                                 if (WARN_ON(offset_in_page(IDX[k]) +
740                                             template[i].tap[k] > PAGE_SIZE))
741                                         goto out;
742
743                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
744                                     offset_in_page(IDX[k]);
745
746                                 memcpy(q, template[i].input + temp,
747                                        template[i].tap[k]);
748
749                                 if (offset_in_page(q) + template[i].tap[k] <
750                                     PAGE_SIZE)
751                                         q[template[i].tap[k]] = 0;
752
753                                 sg_set_buf(&sg[k], q, template[i].tap[k]);
754
755                                 temp += template[i].tap[k];
756                         }
757
758                         ablkcipher_request_set_crypt(req, sg, sg,
759                                         template[i].ilen, iv);
760
761                         ret = enc ?
762                                 crypto_ablkcipher_encrypt(req) :
763                                 crypto_ablkcipher_decrypt(req);
764
765                         switch (ret) {
766                         case 0:
767                                 break;
768                         case -EINPROGRESS:
769                         case -EBUSY:
770                                 ret = wait_for_completion_interruptible(
771                                         &result.completion);
772                                 if (!ret && !((ret = result.err))) {
773                                         INIT_COMPLETION(result.completion);
774                                         break;
775                                 }
776                                 /* fall through */
777                         default:
778                                 printk(KERN_ERR "alg: skcipher: %s failed on "
779                                        "chunk test %d for %s: ret=%d\n", e, j,
780                                        algo, -ret);
781                                 goto out;
782                         }
783
784                         temp = 0;
785                         ret = -EINVAL;
786                         for (k = 0; k < template[i].np; k++) {
787                                 q = xbuf[IDX[k] >> PAGE_SHIFT] +
788                                     offset_in_page(IDX[k]);
789
790                                 if (memcmp(q, template[i].result + temp,
791                                            template[i].tap[k])) {
792                                         printk(KERN_ERR "alg: skcipher: Chunk "
793                                                "test %d failed on %s at page "
794                                                "%u for %s\n", j, e, k, algo);
795                                         hexdump(q, template[i].tap[k]);
796                                         goto out;
797                                 }
798
799                                 q += template[i].tap[k];
800                                 for (n = 0; offset_in_page(q + n) && q[n]; n++)
801                                         ;
802                                 if (n) {
803                                         printk(KERN_ERR "alg: skcipher: "
804                                                "Result buffer corruption in "
805                                                "chunk test %d on %s at page "
806                                                "%u for %s: %u bytes:\n", j, e,
807                                                k, algo, n);
808                                         hexdump(q, n);
809                                         goto out;
810                                 }
811                                 temp += template[i].tap[k];
812                         }
813                 }
814         }
815
816         ret = 0;
817
818 out:
819         ablkcipher_request_free(req);
820         return ret;
821 }
822
823 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
824                      struct comp_testvec *dtemplate, int ctcount, int dtcount)
825 {
826         const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
827         unsigned int i;
828         char result[COMP_BUF_SIZE];
829         int ret;
830
831         for (i = 0; i < ctcount; i++) {
832                 int ilen, dlen = COMP_BUF_SIZE;
833
834                 memset(result, 0, sizeof (result));
835
836                 ilen = ctemplate[i].inlen;
837                 ret = crypto_comp_compress(tfm, ctemplate[i].input,
838                                            ilen, result, &dlen);
839                 if (ret) {
840                         printk(KERN_ERR "alg: comp: compression failed "
841                                "on test %d for %s: ret=%d\n", i + 1, algo,
842                                -ret);
843                         goto out;
844                 }
845
846                 if (memcmp(result, ctemplate[i].output, dlen)) {
847                         printk(KERN_ERR "alg: comp: Compression test %d "
848                                "failed for %s\n", i + 1, algo);
849                         hexdump(result, dlen);
850                         ret = -EINVAL;
851                         goto out;
852                 }
853         }
854
855         for (i = 0; i < dtcount; i++) {
856                 int ilen, dlen = COMP_BUF_SIZE;
857
858                 memset(result, 0, sizeof (result));
859
860                 ilen = dtemplate[i].inlen;
861                 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
862                                              ilen, result, &dlen);
863                 if (ret) {
864                         printk(KERN_ERR "alg: comp: decompression failed "
865                                "on test %d for %s: ret=%d\n", i + 1, algo,
866                                -ret);
867                         goto out;
868                 }
869
870                 if (memcmp(result, dtemplate[i].output, dlen)) {
871                         printk(KERN_ERR "alg: comp: Decompression test %d "
872                                "failed for %s\n", i + 1, algo);
873                         hexdump(result, dlen);
874                         ret = -EINVAL;
875                         goto out;
876                 }
877         }
878
879         ret = 0;
880
881 out:
882         return ret;
883 }
884
885 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
886                          u32 type, u32 mask)
887 {
888         struct crypto_aead *tfm;
889         int err = 0;
890
891         tfm = crypto_alloc_aead(driver, type, mask);
892         if (IS_ERR(tfm)) {
893                 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
894                        "%ld\n", driver, PTR_ERR(tfm));
895                 return PTR_ERR(tfm);
896         }
897
898         if (desc->suite.aead.enc.vecs) {
899                 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
900                                 desc->suite.aead.enc.count);
901                 if (err)
902                         goto out;
903         }
904
905         if (!err && desc->suite.aead.dec.vecs)
906                 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
907                                 desc->suite.aead.dec.count);
908
909 out:
910         crypto_free_aead(tfm);
911         return err;
912 }
913
914 static int alg_test_cipher(const struct alg_test_desc *desc,
915                            const char *driver, u32 type, u32 mask)
916 {
917         struct crypto_cipher *tfm;
918         int err = 0;
919
920         tfm = crypto_alloc_cipher(driver, type, mask);
921         if (IS_ERR(tfm)) {
922                 printk(KERN_ERR "alg: cipher: Failed to load transform for "
923                        "%s: %ld\n", driver, PTR_ERR(tfm));
924                 return PTR_ERR(tfm);
925         }
926
927         if (desc->suite.cipher.enc.vecs) {
928                 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
929                                   desc->suite.cipher.enc.count);
930                 if (err)
931                         goto out;
932         }
933
934         if (desc->suite.cipher.dec.vecs)
935                 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
936                                   desc->suite.cipher.dec.count);
937
938 out:
939         crypto_free_cipher(tfm);
940         return err;
941 }
942
943 static int alg_test_skcipher(const struct alg_test_desc *desc,
944                              const char *driver, u32 type, u32 mask)
945 {
946         struct crypto_ablkcipher *tfm;
947         int err = 0;
948
949         tfm = crypto_alloc_ablkcipher(driver, type, mask);
950         if (IS_ERR(tfm)) {
951                 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
952                        "%s: %ld\n", driver, PTR_ERR(tfm));
953                 return PTR_ERR(tfm);
954         }
955
956         if (desc->suite.cipher.enc.vecs) {
957                 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
958                                     desc->suite.cipher.enc.count);
959                 if (err)
960                         goto out;
961         }
962
963         if (desc->suite.cipher.dec.vecs)
964                 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
965                                     desc->suite.cipher.dec.count);
966
967 out:
968         crypto_free_ablkcipher(tfm);
969         return err;
970 }
971
972 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
973                          u32 type, u32 mask)
974 {
975         struct crypto_comp *tfm;
976         int err;
977
978         tfm = crypto_alloc_comp(driver, type, mask);
979         if (IS_ERR(tfm)) {
980                 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
981                        "%ld\n", driver, PTR_ERR(tfm));
982                 return PTR_ERR(tfm);
983         }
984
985         err = test_comp(tfm, desc->suite.comp.comp.vecs,
986                         desc->suite.comp.decomp.vecs,
987                         desc->suite.comp.comp.count,
988                         desc->suite.comp.decomp.count);
989
990         crypto_free_comp(tfm);
991         return err;
992 }
993
994 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
995                          u32 type, u32 mask)
996 {
997         struct crypto_ahash *tfm;
998         int err;
999
1000         tfm = crypto_alloc_ahash(driver, type, mask);
1001         if (IS_ERR(tfm)) {
1002                 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1003                        "%ld\n", driver, PTR_ERR(tfm));
1004                 return PTR_ERR(tfm);
1005         }
1006
1007         err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
1008
1009         crypto_free_ahash(tfm);
1010         return err;
1011 }
1012
1013 static int alg_test_crc32c(const struct alg_test_desc *desc,
1014                            const char *driver, u32 type, u32 mask)
1015 {
1016         struct crypto_shash *tfm;
1017         u32 val;
1018         int err;
1019
1020         err = alg_test_hash(desc, driver, type, mask);
1021         if (err)
1022                 goto out;
1023
1024         tfm = crypto_alloc_shash(driver, type, mask);
1025         if (IS_ERR(tfm)) {
1026                 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1027                        "%ld\n", driver, PTR_ERR(tfm));
1028                 err = PTR_ERR(tfm);
1029                 goto out;
1030         }
1031
1032         do {
1033                 struct {
1034                         struct shash_desc shash;
1035                         char ctx[crypto_shash_descsize(tfm)];
1036                 } sdesc;
1037
1038                 sdesc.shash.tfm = tfm;
1039                 sdesc.shash.flags = 0;
1040
1041                 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1042                 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1043                 if (err) {
1044                         printk(KERN_ERR "alg: crc32c: Operation failed for "
1045                                "%s: %d\n", driver, err);
1046                         break;
1047                 }
1048
1049                 if (val != ~420553207) {
1050                         printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1051                                "%d\n", driver, val);
1052                         err = -EINVAL;
1053                 }
1054         } while (0);
1055
1056         crypto_free_shash(tfm);
1057
1058 out:
1059         return err;
1060 }
1061
1062 /* Please keep this list sorted by algorithm name. */
1063 static const struct alg_test_desc alg_test_descs[] = {
1064         {
1065                 .alg = "cbc(aes)",
1066                 .test = alg_test_skcipher,
1067                 .suite = {
1068                         .cipher = {
1069                                 .enc = {
1070                                         .vecs = aes_cbc_enc_tv_template,
1071                                         .count = AES_CBC_ENC_TEST_VECTORS
1072                                 },
1073                                 .dec = {
1074                                         .vecs = aes_cbc_dec_tv_template,
1075                                         .count = AES_CBC_DEC_TEST_VECTORS
1076                                 }
1077                         }
1078                 }
1079         }, {
1080                 .alg = "cbc(anubis)",
1081                 .test = alg_test_skcipher,
1082                 .suite = {
1083                         .cipher = {
1084                                 .enc = {
1085                                         .vecs = anubis_cbc_enc_tv_template,
1086                                         .count = ANUBIS_CBC_ENC_TEST_VECTORS
1087                                 },
1088                                 .dec = {
1089                                         .vecs = anubis_cbc_dec_tv_template,
1090                                         .count = ANUBIS_CBC_DEC_TEST_VECTORS
1091                                 }
1092                         }
1093                 }
1094         }, {
1095                 .alg = "cbc(blowfish)",
1096                 .test = alg_test_skcipher,
1097                 .suite = {
1098                         .cipher = {
1099                                 .enc = {
1100                                         .vecs = bf_cbc_enc_tv_template,
1101                                         .count = BF_CBC_ENC_TEST_VECTORS
1102                                 },
1103                                 .dec = {
1104                                         .vecs = bf_cbc_dec_tv_template,
1105                                         .count = BF_CBC_DEC_TEST_VECTORS
1106                                 }
1107                         }
1108                 }
1109         }, {
1110                 .alg = "cbc(camellia)",
1111                 .test = alg_test_skcipher,
1112                 .suite = {
1113                         .cipher = {
1114                                 .enc = {
1115                                         .vecs = camellia_cbc_enc_tv_template,
1116                                         .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1117                                 },
1118                                 .dec = {
1119                                         .vecs = camellia_cbc_dec_tv_template,
1120                                         .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1121                                 }
1122                         }
1123                 }
1124         }, {
1125                 .alg = "cbc(des)",
1126                 .test = alg_test_skcipher,
1127                 .suite = {
1128                         .cipher = {
1129                                 .enc = {
1130                                         .vecs = des_cbc_enc_tv_template,
1131                                         .count = DES_CBC_ENC_TEST_VECTORS
1132                                 },
1133                                 .dec = {
1134                                         .vecs = des_cbc_dec_tv_template,
1135                                         .count = DES_CBC_DEC_TEST_VECTORS
1136                                 }
1137                         }
1138                 }
1139         }, {
1140                 .alg = "cbc(des3_ede)",
1141                 .test = alg_test_skcipher,
1142                 .suite = {
1143                         .cipher = {
1144                                 .enc = {
1145                                         .vecs = des3_ede_cbc_enc_tv_template,
1146                                         .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1147                                 },
1148                                 .dec = {
1149                                         .vecs = des3_ede_cbc_dec_tv_template,
1150                                         .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1151                                 }
1152                         }
1153                 }
1154         }, {
1155                 .alg = "cbc(twofish)",
1156                 .test = alg_test_skcipher,
1157                 .suite = {
1158                         .cipher = {
1159                                 .enc = {
1160                                         .vecs = tf_cbc_enc_tv_template,
1161                                         .count = TF_CBC_ENC_TEST_VECTORS
1162                                 },
1163                                 .dec = {
1164                                         .vecs = tf_cbc_dec_tv_template,
1165                                         .count = TF_CBC_DEC_TEST_VECTORS
1166                                 }
1167                         }
1168                 }
1169         }, {
1170                 .alg = "ccm(aes)",
1171                 .test = alg_test_aead,
1172                 .suite = {
1173                         .aead = {
1174                                 .enc = {
1175                                         .vecs = aes_ccm_enc_tv_template,
1176                                         .count = AES_CCM_ENC_TEST_VECTORS
1177                                 },
1178                                 .dec = {
1179                                         .vecs = aes_ccm_dec_tv_template,
1180                                         .count = AES_CCM_DEC_TEST_VECTORS
1181                                 }
1182                         }
1183                 }
1184         }, {
1185                 .alg = "crc32c",
1186                 .test = alg_test_crc32c,
1187                 .suite = {
1188                         .hash = {
1189                                 .vecs = crc32c_tv_template,
1190                                 .count = CRC32C_TEST_VECTORS
1191                         }
1192                 }
1193         }, {
1194                 .alg = "cts(cbc(aes))",
1195                 .test = alg_test_skcipher,
1196                 .suite = {
1197                         .cipher = {
1198                                 .enc = {
1199                                         .vecs = cts_mode_enc_tv_template,
1200                                         .count = CTS_MODE_ENC_TEST_VECTORS
1201                                 },
1202                                 .dec = {
1203                                         .vecs = cts_mode_dec_tv_template,
1204                                         .count = CTS_MODE_DEC_TEST_VECTORS
1205                                 }
1206                         }
1207                 }
1208         }, {
1209                 .alg = "deflate",
1210                 .test = alg_test_comp,
1211                 .suite = {
1212                         .comp = {
1213                                 .comp = {
1214                                         .vecs = deflate_comp_tv_template,
1215                                         .count = DEFLATE_COMP_TEST_VECTORS
1216                                 },
1217                                 .decomp = {
1218                                         .vecs = deflate_decomp_tv_template,
1219                                         .count = DEFLATE_DECOMP_TEST_VECTORS
1220                                 }
1221                         }
1222                 }
1223         }, {
1224                 .alg = "ecb(aes)",
1225                 .test = alg_test_skcipher,
1226                 .suite = {
1227                         .cipher = {
1228                                 .enc = {
1229                                         .vecs = aes_enc_tv_template,
1230                                         .count = AES_ENC_TEST_VECTORS
1231                                 },
1232                                 .dec = {
1233                                         .vecs = aes_dec_tv_template,
1234                                         .count = AES_DEC_TEST_VECTORS
1235                                 }
1236                         }
1237                 }
1238         }, {
1239                 .alg = "ecb(anubis)",
1240                 .test = alg_test_skcipher,
1241                 .suite = {
1242                         .cipher = {
1243                                 .enc = {
1244                                         .vecs = anubis_enc_tv_template,
1245                                         .count = ANUBIS_ENC_TEST_VECTORS
1246                                 },
1247                                 .dec = {
1248                                         .vecs = anubis_dec_tv_template,
1249                                         .count = ANUBIS_DEC_TEST_VECTORS
1250                                 }
1251                         }
1252                 }
1253         }, {
1254                 .alg = "ecb(arc4)",
1255                 .test = alg_test_skcipher,
1256                 .suite = {
1257                         .cipher = {
1258                                 .enc = {
1259                                         .vecs = arc4_enc_tv_template,
1260                                         .count = ARC4_ENC_TEST_VECTORS
1261                                 },
1262                                 .dec = {
1263                                         .vecs = arc4_dec_tv_template,
1264                                         .count = ARC4_DEC_TEST_VECTORS
1265                                 }
1266                         }
1267                 }
1268         }, {
1269                 .alg = "ecb(blowfish)",
1270                 .test = alg_test_skcipher,
1271                 .suite = {
1272                         .cipher = {
1273                                 .enc = {
1274                                         .vecs = bf_enc_tv_template,
1275                                         .count = BF_ENC_TEST_VECTORS
1276                                 },
1277                                 .dec = {
1278                                         .vecs = bf_dec_tv_template,
1279                                         .count = BF_DEC_TEST_VECTORS
1280                                 }
1281                         }
1282                 }
1283         }, {
1284                 .alg = "ecb(camellia)",
1285                 .test = alg_test_skcipher,
1286                 .suite = {
1287                         .cipher = {
1288                                 .enc = {
1289                                         .vecs = camellia_enc_tv_template,
1290                                         .count = CAMELLIA_ENC_TEST_VECTORS
1291                                 },
1292                                 .dec = {
1293                                         .vecs = camellia_dec_tv_template,
1294                                         .count = CAMELLIA_DEC_TEST_VECTORS
1295                                 }
1296                         }
1297                 }
1298         }, {
1299                 .alg = "ecb(cast5)",
1300                 .test = alg_test_skcipher,
1301                 .suite = {
1302                         .cipher = {
1303                                 .enc = {
1304                                         .vecs = cast5_enc_tv_template,
1305                                         .count = CAST5_ENC_TEST_VECTORS
1306                                 },
1307                                 .dec = {
1308                                         .vecs = cast5_dec_tv_template,
1309                                         .count = CAST5_DEC_TEST_VECTORS
1310                                 }
1311                         }
1312                 }
1313         }, {
1314                 .alg = "ecb(cast6)",
1315                 .test = alg_test_skcipher,
1316                 .suite = {
1317                         .cipher = {
1318                                 .enc = {
1319                                         .vecs = cast6_enc_tv_template,
1320                                         .count = CAST6_ENC_TEST_VECTORS
1321                                 },
1322                                 .dec = {
1323                                         .vecs = cast6_dec_tv_template,
1324                                         .count = CAST6_DEC_TEST_VECTORS
1325                                 }
1326                         }
1327                 }
1328         }, {
1329                 .alg = "ecb(des)",
1330                 .test = alg_test_skcipher,
1331                 .suite = {
1332                         .cipher = {
1333                                 .enc = {
1334                                         .vecs = des_enc_tv_template,
1335                                         .count = DES_ENC_TEST_VECTORS
1336                                 },
1337                                 .dec = {
1338                                         .vecs = des_dec_tv_template,
1339                                         .count = DES_DEC_TEST_VECTORS
1340                                 }
1341                         }
1342                 }
1343         }, {
1344                 .alg = "ecb(des3_ede)",
1345                 .test = alg_test_skcipher,
1346                 .suite = {
1347                         .cipher = {
1348                                 .enc = {
1349                                         .vecs = des3_ede_enc_tv_template,
1350                                         .count = DES3_EDE_ENC_TEST_VECTORS
1351                                 },
1352                                 .dec = {
1353                                         .vecs = des3_ede_dec_tv_template,
1354                                         .count = DES3_EDE_DEC_TEST_VECTORS
1355                                 }
1356                         }
1357                 }
1358         }, {
1359                 .alg = "ecb(khazad)",
1360                 .test = alg_test_skcipher,
1361                 .suite = {
1362                         .cipher = {
1363                                 .enc = {
1364                                         .vecs = khazad_enc_tv_template,
1365                                         .count = KHAZAD_ENC_TEST_VECTORS
1366                                 },
1367                                 .dec = {
1368                                         .vecs = khazad_dec_tv_template,
1369                                         .count = KHAZAD_DEC_TEST_VECTORS
1370                                 }
1371                         }
1372                 }
1373         }, {
1374                 .alg = "ecb(seed)",
1375                 .test = alg_test_skcipher,
1376                 .suite = {
1377                         .cipher = {
1378                                 .enc = {
1379                                         .vecs = seed_enc_tv_template,
1380                                         .count = SEED_ENC_TEST_VECTORS
1381                                 },
1382                                 .dec = {
1383                                         .vecs = seed_dec_tv_template,
1384                                         .count = SEED_DEC_TEST_VECTORS
1385                                 }
1386                         }
1387                 }
1388         }, {
1389                 .alg = "ecb(serpent)",
1390                 .test = alg_test_skcipher,
1391                 .suite = {
1392                         .cipher = {
1393                                 .enc = {
1394                                         .vecs = serpent_enc_tv_template,
1395                                         .count = SERPENT_ENC_TEST_VECTORS
1396                                 },
1397                                 .dec = {
1398                                         .vecs = serpent_dec_tv_template,
1399                                         .count = SERPENT_DEC_TEST_VECTORS
1400                                 }
1401                         }
1402                 }
1403         }, {
1404                 .alg = "ecb(tea)",
1405                 .test = alg_test_skcipher,
1406                 .suite = {
1407                         .cipher = {
1408                                 .enc = {
1409                                         .vecs = tea_enc_tv_template,
1410                                         .count = TEA_ENC_TEST_VECTORS
1411                                 },
1412                                 .dec = {
1413                                         .vecs = tea_dec_tv_template,
1414                                         .count = TEA_DEC_TEST_VECTORS
1415                                 }
1416                         }
1417                 }
1418         }, {
1419                 .alg = "ecb(tnepres)",
1420                 .test = alg_test_skcipher,
1421                 .suite = {
1422                         .cipher = {
1423                                 .enc = {
1424                                         .vecs = tnepres_enc_tv_template,
1425                                         .count = TNEPRES_ENC_TEST_VECTORS
1426                                 },
1427                                 .dec = {
1428                                         .vecs = tnepres_dec_tv_template,
1429                                         .count = TNEPRES_DEC_TEST_VECTORS
1430                                 }
1431                         }
1432                 }
1433         }, {
1434                 .alg = "ecb(twofish)",
1435                 .test = alg_test_skcipher,
1436                 .suite = {
1437                         .cipher = {
1438                                 .enc = {
1439                                         .vecs = tf_enc_tv_template,
1440                                         .count = TF_ENC_TEST_VECTORS
1441                                 },
1442                                 .dec = {
1443                                         .vecs = tf_dec_tv_template,
1444                                         .count = TF_DEC_TEST_VECTORS
1445                                 }
1446                         }
1447                 }
1448         }, {
1449                 .alg = "ecb(xeta)",
1450                 .test = alg_test_skcipher,
1451                 .suite = {
1452                         .cipher = {
1453                                 .enc = {
1454                                         .vecs = xeta_enc_tv_template,
1455                                         .count = XETA_ENC_TEST_VECTORS
1456                                 },
1457                                 .dec = {
1458                                         .vecs = xeta_dec_tv_template,
1459                                         .count = XETA_DEC_TEST_VECTORS
1460                                 }
1461                         }
1462                 }
1463         }, {
1464                 .alg = "ecb(xtea)",
1465                 .test = alg_test_skcipher,
1466                 .suite = {
1467                         .cipher = {
1468                                 .enc = {
1469                                         .vecs = xtea_enc_tv_template,
1470                                         .count = XTEA_ENC_TEST_VECTORS
1471                                 },
1472                                 .dec = {
1473                                         .vecs = xtea_dec_tv_template,
1474                                         .count = XTEA_DEC_TEST_VECTORS
1475                                 }
1476                         }
1477                 }
1478         }, {
1479                 .alg = "gcm(aes)",
1480                 .test = alg_test_aead,
1481                 .suite = {
1482                         .aead = {
1483                                 .enc = {
1484                                         .vecs = aes_gcm_enc_tv_template,
1485                                         .count = AES_GCM_ENC_TEST_VECTORS
1486                                 },
1487                                 .dec = {
1488                                         .vecs = aes_gcm_dec_tv_template,
1489                                         .count = AES_GCM_DEC_TEST_VECTORS
1490                                 }
1491                         }
1492                 }
1493         }, {
1494                 .alg = "hmac(md5)",
1495                 .test = alg_test_hash,
1496                 .suite = {
1497                         .hash = {
1498                                 .vecs = hmac_md5_tv_template,
1499                                 .count = HMAC_MD5_TEST_VECTORS
1500                         }
1501                 }
1502         }, {
1503                 .alg = "hmac(rmd128)",
1504                 .test = alg_test_hash,
1505                 .suite = {
1506                         .hash = {
1507                                 .vecs = hmac_rmd128_tv_template,
1508                                 .count = HMAC_RMD128_TEST_VECTORS
1509                         }
1510                 }
1511         }, {
1512                 .alg = "hmac(rmd160)",
1513                 .test = alg_test_hash,
1514                 .suite = {
1515                         .hash = {
1516                                 .vecs = hmac_rmd160_tv_template,
1517                                 .count = HMAC_RMD160_TEST_VECTORS
1518                         }
1519                 }
1520         }, {
1521                 .alg = "hmac(sha1)",
1522                 .test = alg_test_hash,
1523                 .suite = {
1524                         .hash = {
1525                                 .vecs = hmac_sha1_tv_template,
1526                                 .count = HMAC_SHA1_TEST_VECTORS
1527                         }
1528                 }
1529         }, {
1530                 .alg = "hmac(sha224)",
1531                 .test = alg_test_hash,
1532                 .suite = {
1533                         .hash = {
1534                                 .vecs = hmac_sha224_tv_template,
1535                                 .count = HMAC_SHA224_TEST_VECTORS
1536                         }
1537                 }
1538         }, {
1539                 .alg = "hmac(sha256)",
1540                 .test = alg_test_hash,
1541                 .suite = {
1542                         .hash = {
1543                                 .vecs = hmac_sha256_tv_template,
1544                                 .count = HMAC_SHA256_TEST_VECTORS
1545                         }
1546                 }
1547         }, {
1548                 .alg = "hmac(sha384)",
1549                 .test = alg_test_hash,
1550                 .suite = {
1551                         .hash = {
1552                                 .vecs = hmac_sha384_tv_template,
1553                                 .count = HMAC_SHA384_TEST_VECTORS
1554                         }
1555                 }
1556         }, {
1557                 .alg = "hmac(sha512)",
1558                 .test = alg_test_hash,
1559                 .suite = {
1560                         .hash = {
1561                                 .vecs = hmac_sha512_tv_template,
1562                                 .count = HMAC_SHA512_TEST_VECTORS
1563                         }
1564                 }
1565         }, {
1566                 .alg = "lrw(aes)",
1567                 .test = alg_test_skcipher,
1568                 .suite = {
1569                         .cipher = {
1570                                 .enc = {
1571                                         .vecs = aes_lrw_enc_tv_template,
1572                                         .count = AES_LRW_ENC_TEST_VECTORS
1573                                 },
1574                                 .dec = {
1575                                         .vecs = aes_lrw_dec_tv_template,
1576                                         .count = AES_LRW_DEC_TEST_VECTORS
1577                                 }
1578                         }
1579                 }
1580         }, {
1581                 .alg = "lzo",
1582                 .test = alg_test_comp,
1583                 .suite = {
1584                         .comp = {
1585                                 .comp = {
1586                                         .vecs = lzo_comp_tv_template,
1587                                         .count = LZO_COMP_TEST_VECTORS
1588                                 },
1589                                 .decomp = {
1590                                         .vecs = lzo_decomp_tv_template,
1591                                         .count = LZO_DECOMP_TEST_VECTORS
1592                                 }
1593                         }
1594                 }
1595         }, {
1596                 .alg = "md4",
1597                 .test = alg_test_hash,
1598                 .suite = {
1599                         .hash = {
1600                                 .vecs = md4_tv_template,
1601                                 .count = MD4_TEST_VECTORS
1602                         }
1603                 }
1604         }, {
1605                 .alg = "md5",
1606                 .test = alg_test_hash,
1607                 .suite = {
1608                         .hash = {
1609                                 .vecs = md5_tv_template,
1610                                 .count = MD5_TEST_VECTORS
1611                         }
1612                 }
1613         }, {
1614                 .alg = "michael_mic",
1615                 .test = alg_test_hash,
1616                 .suite = {
1617                         .hash = {
1618                                 .vecs = michael_mic_tv_template,
1619                                 .count = MICHAEL_MIC_TEST_VECTORS
1620                         }
1621                 }
1622         }, {
1623                 .alg = "pcbc(fcrypt)",
1624                 .test = alg_test_skcipher,
1625                 .suite = {
1626                         .cipher = {
1627                                 .enc = {
1628                                         .vecs = fcrypt_pcbc_enc_tv_template,
1629                                         .count = FCRYPT_ENC_TEST_VECTORS
1630                                 },
1631                                 .dec = {
1632                                         .vecs = fcrypt_pcbc_dec_tv_template,
1633                                         .count = FCRYPT_DEC_TEST_VECTORS
1634                                 }
1635                         }
1636                 }
1637         }, {
1638                 .alg = "rfc3686(ctr(aes))",
1639                 .test = alg_test_skcipher,
1640                 .suite = {
1641                         .cipher = {
1642                                 .enc = {
1643                                         .vecs = aes_ctr_enc_tv_template,
1644                                         .count = AES_CTR_ENC_TEST_VECTORS
1645                                 },
1646                                 .dec = {
1647                                         .vecs = aes_ctr_dec_tv_template,
1648                                         .count = AES_CTR_DEC_TEST_VECTORS
1649                                 }
1650                         }
1651                 }
1652         }, {
1653                 .alg = "rmd128",
1654                 .test = alg_test_hash,
1655                 .suite = {
1656                         .hash = {
1657                                 .vecs = rmd128_tv_template,
1658                                 .count = RMD128_TEST_VECTORS
1659                         }
1660                 }
1661         }, {
1662                 .alg = "rmd160",
1663                 .test = alg_test_hash,
1664                 .suite = {
1665                         .hash = {
1666                                 .vecs = rmd160_tv_template,
1667                                 .count = RMD160_TEST_VECTORS
1668                         }
1669                 }
1670         }, {
1671                 .alg = "rmd256",
1672                 .test = alg_test_hash,
1673                 .suite = {
1674                         .hash = {
1675                                 .vecs = rmd256_tv_template,
1676                                 .count = RMD256_TEST_VECTORS
1677                         }
1678                 }
1679         }, {
1680                 .alg = "rmd320",
1681                 .test = alg_test_hash,
1682                 .suite = {
1683                         .hash = {
1684                                 .vecs = rmd320_tv_template,
1685                                 .count = RMD320_TEST_VECTORS
1686                         }
1687                 }
1688         }, {
1689                 .alg = "salsa20",
1690                 .test = alg_test_skcipher,
1691                 .suite = {
1692                         .cipher = {
1693                                 .enc = {
1694                                         .vecs = salsa20_stream_enc_tv_template,
1695                                         .count = SALSA20_STREAM_ENC_TEST_VECTORS
1696                                 }
1697                         }
1698                 }
1699         }, {
1700                 .alg = "sha1",
1701                 .test = alg_test_hash,
1702                 .suite = {
1703                         .hash = {
1704                                 .vecs = sha1_tv_template,
1705                                 .count = SHA1_TEST_VECTORS
1706                         }
1707                 }
1708         }, {
1709                 .alg = "sha224",
1710                 .test = alg_test_hash,
1711                 .suite = {
1712                         .hash = {
1713                                 .vecs = sha224_tv_template,
1714                                 .count = SHA224_TEST_VECTORS
1715                         }
1716                 }
1717         }, {
1718                 .alg = "sha256",
1719                 .test = alg_test_hash,
1720                 .suite = {
1721                         .hash = {
1722                                 .vecs = sha256_tv_template,
1723                                 .count = SHA256_TEST_VECTORS
1724                         }
1725                 }
1726         }, {
1727                 .alg = "sha384",
1728                 .test = alg_test_hash,
1729                 .suite = {
1730                         .hash = {
1731                                 .vecs = sha384_tv_template,
1732                                 .count = SHA384_TEST_VECTORS
1733                         }
1734                 }
1735         }, {
1736                 .alg = "sha512",
1737                 .test = alg_test_hash,
1738                 .suite = {
1739                         .hash = {
1740                                 .vecs = sha512_tv_template,
1741                                 .count = SHA512_TEST_VECTORS
1742                         }
1743                 }
1744         }, {
1745                 .alg = "tgr128",
1746                 .test = alg_test_hash,
1747                 .suite = {
1748                         .hash = {
1749                                 .vecs = tgr128_tv_template,
1750                                 .count = TGR128_TEST_VECTORS
1751                         }
1752                 }
1753         }, {
1754                 .alg = "tgr160",
1755                 .test = alg_test_hash,
1756                 .suite = {
1757                         .hash = {
1758                                 .vecs = tgr160_tv_template,
1759                                 .count = TGR160_TEST_VECTORS
1760                         }
1761                 }
1762         }, {
1763                 .alg = "tgr192",
1764                 .test = alg_test_hash,
1765                 .suite = {
1766                         .hash = {
1767                                 .vecs = tgr192_tv_template,
1768                                 .count = TGR192_TEST_VECTORS
1769                         }
1770                 }
1771         }, {
1772                 .alg = "wp256",
1773                 .test = alg_test_hash,
1774                 .suite = {
1775                         .hash = {
1776                                 .vecs = wp256_tv_template,
1777                                 .count = WP256_TEST_VECTORS
1778                         }
1779                 }
1780         }, {
1781                 .alg = "wp384",
1782                 .test = alg_test_hash,
1783                 .suite = {
1784                         .hash = {
1785                                 .vecs = wp384_tv_template,
1786                                 .count = WP384_TEST_VECTORS
1787                         }
1788                 }
1789         }, {
1790                 .alg = "wp512",
1791                 .test = alg_test_hash,
1792                 .suite = {
1793                         .hash = {
1794                                 .vecs = wp512_tv_template,
1795                                 .count = WP512_TEST_VECTORS
1796                         }
1797                 }
1798         }, {
1799                 .alg = "xcbc(aes)",
1800                 .test = alg_test_hash,
1801                 .suite = {
1802                         .hash = {
1803                                 .vecs = aes_xcbc128_tv_template,
1804                                 .count = XCBC_AES_TEST_VECTORS
1805                         }
1806                 }
1807         }, {
1808                 .alg = "xts(aes)",
1809                 .test = alg_test_skcipher,
1810                 .suite = {
1811                         .cipher = {
1812                                 .enc = {
1813                                         .vecs = aes_xts_enc_tv_template,
1814                                         .count = AES_XTS_ENC_TEST_VECTORS
1815                                 },
1816                                 .dec = {
1817                                         .vecs = aes_xts_dec_tv_template,
1818                                         .count = AES_XTS_DEC_TEST_VECTORS
1819                                 }
1820                         }
1821                 }
1822         }
1823 };
1824
1825 static int alg_find_test(const char *alg)
1826 {
1827         int start = 0;
1828         int end = ARRAY_SIZE(alg_test_descs);
1829
1830         while (start < end) {
1831                 int i = (start + end) / 2;
1832                 int diff = strcmp(alg_test_descs[i].alg, alg);
1833
1834                 if (diff > 0) {
1835                         end = i;
1836                         continue;
1837                 }
1838
1839                 if (diff < 0) {
1840                         start = i + 1;
1841                         continue;
1842                 }
1843
1844                 return i;
1845         }
1846
1847         return -1;
1848 }
1849
1850 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
1851 {
1852         int i;
1853         int rc;
1854
1855         if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
1856                 char nalg[CRYPTO_MAX_ALG_NAME];
1857
1858                 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
1859                     sizeof(nalg))
1860                         return -ENAMETOOLONG;
1861
1862                 i = alg_find_test(nalg);
1863                 if (i < 0)
1864                         goto notest;
1865
1866                 return alg_test_cipher(alg_test_descs + i, driver, type, mask);
1867         }
1868
1869         i = alg_find_test(alg);
1870         if (i < 0)
1871                 goto notest;
1872
1873         rc = alg_test_descs[i].test(alg_test_descs + i, driver,
1874                                       type, mask);
1875         if (fips_enabled && rc)
1876                 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
1877
1878         return rc;
1879
1880 notest:
1881         printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
1882         return 0;
1883 }
1884 EXPORT_SYMBOL_GPL(alg_test);
1885
1886 int __init testmgr_init(void)
1887 {
1888         int i;
1889
1890         for (i = 0; i < XBUFSIZE; i++) {
1891                 xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
1892                 if (!xbuf[i])
1893                         goto err_free_xbuf;
1894         }
1895
1896         for (i = 0; i < XBUFSIZE; i++) {
1897                 axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
1898                 if (!axbuf[i])
1899                         goto err_free_axbuf;
1900         }
1901
1902         return 0;
1903
1904 err_free_axbuf:
1905         for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
1906                 free_page((unsigned long)axbuf[i]);
1907 err_free_xbuf:
1908         for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
1909                 free_page((unsigned long)xbuf[i]);
1910
1911         return -ENOMEM;
1912 }
1913
1914 void testmgr_exit(void)
1915 {
1916         int i;
1917
1918         for (i = 0; i < XBUFSIZE; i++)
1919                 free_page((unsigned long)axbuf[i]);
1920         for (i = 0; i < XBUFSIZE; i++)
1921                 free_page((unsigned long)xbuf[i]);
1922 }