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