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