2 * Algorithm testing framework and tests.
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>
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)
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>
27 * Need slab memory for testing (size in number of pages).
32 * Indexes into the xbuf to simulate cross-page access.
44 * Used by test_cipher()
49 struct tcrypt_result {
50 struct completion completion;
54 struct aead_test_suite {
56 struct aead_testvec *vecs;
61 struct cipher_test_suite {
63 struct cipher_testvec *vecs;
68 struct comp_test_suite {
70 struct comp_testvec *vecs;
75 struct pcomp_test_suite {
77 struct pcomp_testvec *vecs;
82 struct hash_test_suite {
83 struct hash_testvec *vecs;
87 struct alg_test_desc {
89 int (*test)(const struct alg_test_desc *desc, const char *driver,
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;
101 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
103 static char *xbuf[XBUFSIZE];
104 static char *axbuf[XBUFSIZE];
106 static void hexdump(unsigned char *buf, unsigned int len)
108 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
113 static void tcrypt_complete(struct crypto_async_request *req, int err)
115 struct tcrypt_result *res = req->data;
117 if (err == -EINPROGRESS)
121 complete(&res->completion);
124 static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
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];
131 struct ahash_request *req;
132 struct tcrypt_result tresult;
136 init_completion(&tresult.completion);
138 req = ahash_request_alloc(tfm, GFP_KERNEL);
140 printk(KERN_ERR "alg: hash: Failed to allocate request for "
145 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
146 tcrypt_complete, &tresult);
148 for (i = 0; i < tcount; i++) {
149 memset(result, 0, 64);
153 memcpy(hash_buff, template[i].plaintext, template[i].psize);
154 sg_init_one(&sg[0], hash_buff, template[i].psize);
156 if (template[i].ksize) {
157 crypto_ahash_clear_flags(tfm, ~0);
158 ret = crypto_ahash_setkey(tfm, template[i].key,
161 printk(KERN_ERR "alg: hash: setkey failed on "
162 "test %d for %s: ret=%d\n", i + 1, algo,
168 ahash_request_set_crypt(req, sg, result, template[i].psize);
169 ret = crypto_ahash_digest(req);
175 ret = wait_for_completion_interruptible(
176 &tresult.completion);
177 if (!ret && !(ret = tresult.err)) {
178 INIT_COMPLETION(tresult.completion);
183 printk(KERN_ERR "alg: hash: digest failed on test %d "
184 "for %s: ret=%d\n", i + 1, algo, -ret);
188 if (memcmp(result, template[i].digest,
189 crypto_ahash_digestsize(tfm))) {
190 printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
192 hexdump(result, crypto_ahash_digestsize(tfm));
199 for (i = 0; i < tcount; i++) {
200 if (template[i].np) {
202 memset(result, 0, 64);
205 sg_init_table(sg, template[i].np);
206 for (k = 0; k < template[i].np; k++) {
208 memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
209 offset_in_page(IDX[k]),
210 template[i].plaintext + temp,
213 temp += template[i].tap[k];
216 if (template[i].ksize) {
217 crypto_ahash_clear_flags(tfm, ~0);
218 ret = crypto_ahash_setkey(tfm, template[i].key,
222 printk(KERN_ERR "alg: hash: setkey "
223 "failed on chunking test %d "
224 "for %s: ret=%d\n", j, algo,
230 ahash_request_set_crypt(req, sg, result,
232 ret = crypto_ahash_digest(req);
238 ret = wait_for_completion_interruptible(
239 &tresult.completion);
240 if (!ret && !(ret = tresult.err)) {
241 INIT_COMPLETION(tresult.completion);
246 printk(KERN_ERR "alg: hash: digest failed "
247 "on chunking test %d for %s: "
248 "ret=%d\n", j, algo, -ret);
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));
266 ahash_request_free(req);
271 static int test_aead(struct crypto_aead *tfm, int enc,
272 struct aead_testvec *template, unsigned int tcount)
274 const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
275 unsigned int i, j, k, n, temp;
279 struct aead_request *req;
280 struct scatterlist sg[8];
281 struct scatterlist asg[8];
283 struct tcrypt_result result;
284 unsigned int authsize;
294 init_completion(&result.completion);
296 req = aead_request_alloc(tfm, GFP_KERNEL);
298 printk(KERN_ERR "alg: aead: Failed to allocate request for "
304 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
305 tcrypt_complete, &result);
307 for (i = 0, j = 0; i < tcount; i++) {
308 if (!template[i].np) {
311 /* some tepmplates have no input data but they will
317 memcpy(input, template[i].input, template[i].ilen);
318 memcpy(assoc, template[i].assoc, template[i].alen);
320 memcpy(iv, template[i].iv, MAX_IVLEN);
322 memset(iv, 0, MAX_IVLEN);
324 crypto_aead_clear_flags(tfm, ~0);
326 crypto_aead_set_flags(
327 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
329 key = template[i].key;
331 ret = crypto_aead_setkey(tfm, key,
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));
341 authsize = abs(template[i].rlen - template[i].ilen);
342 ret = crypto_aead_setauthsize(tfm, authsize);
344 printk(KERN_ERR "alg: aead: Failed to set "
345 "authsize to %u on test %d for %s\n",
350 sg_init_one(&sg[0], input,
351 template[i].ilen + (enc ? authsize : 0));
353 sg_init_one(&asg[0], assoc, template[i].alen);
355 aead_request_set_crypt(req, sg, sg,
356 template[i].ilen, iv);
358 aead_request_set_assoc(req, asg, template[i].alen);
361 crypto_aead_encrypt(req) :
362 crypto_aead_decrypt(req);
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",
372 /* so really, we got a bad message */
379 ret = wait_for_completion_interruptible(
381 if (!ret && !(ret = result.err)) {
382 INIT_COMPLETION(result.completion);
386 if (template[i].novrfy)
387 /* verification failure was expected */
391 printk(KERN_ERR "alg: aead: %s failed on test "
392 "%d for %s: ret=%d\n", e, j, algo, -ret);
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);
407 for (i = 0, j = 0; i < tcount; i++) {
408 if (template[i].np) {
412 memcpy(iv, template[i].iv, MAX_IVLEN);
414 memset(iv, 0, MAX_IVLEN);
416 crypto_aead_clear_flags(tfm, ~0);
418 crypto_aead_set_flags(
419 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
420 key = template[i].key;
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));
431 authsize = abs(template[i].rlen - template[i].ilen);
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))
440 q = xbuf[IDX[k] >> PAGE_SHIFT] +
441 offset_in_page(IDX[k]);
443 memcpy(q, template[i].input + temp,
446 n = template[i].tap[k];
447 if (k == template[i].np - 1 && enc)
449 if (offset_in_page(q) + n < PAGE_SIZE)
452 sg_set_buf(&sg[k], q, template[i].tap[k]);
453 temp += template[i].tap[k];
456 ret = crypto_aead_setauthsize(tfm, authsize);
458 printk(KERN_ERR "alg: aead: Failed to set "
459 "authsize to %u on chunk test %d for "
460 "%s\n", authsize, j, algo);
465 if (WARN_ON(sg[k - 1].offset +
466 sg[k - 1].length + authsize >
472 sg[k - 1].length += authsize;
475 sg_init_table(asg, template[i].anp);
476 for (k = 0, temp = 0; k < template[i].anp; 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];
486 aead_request_set_crypt(req, sg, sg,
490 aead_request_set_assoc(req, asg, template[i].alen);
493 crypto_aead_encrypt(req) :
494 crypto_aead_decrypt(req);
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",
504 /* so really, we got a bad message */
511 ret = wait_for_completion_interruptible(
513 if (!ret && !(ret = result.err)) {
514 INIT_COMPLETION(result.completion);
518 if (template[i].novrfy)
519 /* verification failure was expected */
523 printk(KERN_ERR "alg: aead: %s failed on "
524 "chunk test %d for %s: ret=%d\n", e, j,
530 for (k = 0, temp = 0; k < template[i].np; k++) {
531 q = xbuf[IDX[k] >> PAGE_SHIFT] +
532 offset_in_page(IDX[k]);
534 n = template[i].tap[k];
535 if (k == template[i].np - 1)
536 n += enc ? authsize : -authsize;
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);
547 if (k == template[i].np - 1 && !enc) {
548 if (memcmp(q, template[i].input +
554 for (n = 0; offset_in_page(q + 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,
568 temp += template[i].tap[k];
576 aead_request_free(req);
580 static int test_cipher(struct crypto_cipher *tfm, int enc,
581 struct cipher_testvec *template, unsigned int tcount)
583 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
584 unsigned int i, j, k;
596 for (i = 0; i < tcount; i++) {
603 memcpy(data, template[i].input, template[i].ilen);
605 crypto_cipher_clear_flags(tfm, ~0);
607 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
609 ret = crypto_cipher_setkey(tfm, template[i].key,
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));
619 for (k = 0; k < template[i].ilen;
620 k += crypto_cipher_blocksize(tfm)) {
622 crypto_cipher_encrypt_one(tfm, data + k,
625 crypto_cipher_decrypt_one(tfm, data + k,
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);
645 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
646 struct cipher_testvec *template, unsigned int tcount)
649 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
650 unsigned int i, j, k, n, temp;
653 struct ablkcipher_request *req;
654 struct scatterlist sg[8];
656 struct tcrypt_result result;
665 init_completion(&result.completion);
667 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
669 printk(KERN_ERR "alg: skcipher: Failed to allocate request "
675 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
676 tcrypt_complete, &result);
679 for (i = 0; i < tcount; i++) {
681 memcpy(iv, template[i].iv, MAX_IVLEN);
683 memset(iv, 0, MAX_IVLEN);
685 if (!(template[i].np)) {
689 memcpy(data, template[i].input, template[i].ilen);
691 crypto_ablkcipher_clear_flags(tfm, ~0);
693 crypto_ablkcipher_set_flags(
694 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
696 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
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));
706 sg_init_one(&sg[0], data, template[i].ilen);
708 ablkcipher_request_set_crypt(req, sg, sg,
709 template[i].ilen, iv);
711 crypto_ablkcipher_encrypt(req) :
712 crypto_ablkcipher_decrypt(req);
719 ret = wait_for_completion_interruptible(
721 if (!ret && !((ret = result.err))) {
722 INIT_COMPLETION(result.completion);
727 printk(KERN_ERR "alg: skcipher: %s failed on "
728 "test %d for %s: ret=%d\n", e, j, algo,
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);
745 for (i = 0; i < tcount; i++) {
748 memcpy(iv, template[i].iv, MAX_IVLEN);
750 memset(iv, 0, MAX_IVLEN);
752 if (template[i].np) {
755 crypto_ablkcipher_clear_flags(tfm, ~0);
757 crypto_ablkcipher_set_flags(
758 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
760 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
762 if (!ret == template[i].fail) {
763 printk(KERN_ERR "alg: skcipher: setkey failed "
764 "on chunk test %d for %s: flags=%x\n",
766 crypto_ablkcipher_get_flags(tfm));
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))
779 q = xbuf[IDX[k] >> PAGE_SHIFT] +
780 offset_in_page(IDX[k]);
782 memcpy(q, template[i].input + temp,
785 if (offset_in_page(q) + template[i].tap[k] <
787 q[template[i].tap[k]] = 0;
789 sg_set_buf(&sg[k], q, template[i].tap[k]);
791 temp += template[i].tap[k];
794 ablkcipher_request_set_crypt(req, sg, sg,
795 template[i].ilen, iv);
798 crypto_ablkcipher_encrypt(req) :
799 crypto_ablkcipher_decrypt(req);
806 ret = wait_for_completion_interruptible(
808 if (!ret && !((ret = result.err))) {
809 INIT_COMPLETION(result.completion);
814 printk(KERN_ERR "alg: skcipher: %s failed on "
815 "chunk test %d for %s: ret=%d\n", e, j,
822 for (k = 0; k < template[i].np; k++) {
823 q = xbuf[IDX[k] >> PAGE_SHIFT] +
824 offset_in_page(IDX[k]);
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]);
835 q += template[i].tap[k];
836 for (n = 0; offset_in_page(q + n) && q[n]; 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,
847 temp += template[i].tap[k];
855 ablkcipher_request_free(req);
859 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
860 struct comp_testvec *dtemplate, int ctcount, int dtcount)
862 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
864 char result[COMP_BUF_SIZE];
867 for (i = 0; i < ctcount; i++) {
869 unsigned int dlen = COMP_BUF_SIZE;
871 memset(result, 0, sizeof (result));
873 ilen = ctemplate[i].inlen;
874 ret = crypto_comp_compress(tfm, ctemplate[i].input,
875 ilen, result, &dlen);
877 printk(KERN_ERR "alg: comp: compression failed "
878 "on test %d for %s: ret=%d\n", i + 1, algo,
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,
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);
900 for (i = 0; i < dtcount; i++) {
902 unsigned int dlen = COMP_BUF_SIZE;
904 memset(result, 0, sizeof (result));
906 ilen = dtemplate[i].inlen;
907 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
908 ilen, result, &dlen);
910 printk(KERN_ERR "alg: comp: decompression failed "
911 "on test %d for %s: ret=%d\n", i + 1, algo,
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,
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);
939 static int test_pcomp(struct crypto_pcomp *tfm,
940 struct pcomp_testvec *ctemplate,
941 struct pcomp_testvec *dtemplate, int ctcount,
944 const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
946 char result[COMP_BUF_SIZE];
949 for (i = 0; i < ctcount; i++) {
950 struct comp_request req;
952 error = crypto_compress_setup(tfm, ctemplate[i].params,
953 ctemplate[i].paramsize);
955 pr_err("alg: pcomp: compression setup failed on test "
956 "%d for %s: error=%d\n", i + 1, algo, error);
960 error = crypto_compress_init(tfm);
962 pr_err("alg: pcomp: compression init failed on test "
963 "%d for %s: error=%d\n", i + 1, algo, error);
967 memset(result, 0, sizeof(result));
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;
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);
981 /* Add remaining input data */
982 req.avail_in += (ctemplate[i].inlen + 1) / 2;
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);
991 /* Provide remaining output space */
992 req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
994 error = crypto_compress_final(tfm, &req);
996 pr_err("alg: pcomp: compression final failed on test "
997 "%d for %s: error=%d\n", i + 1, algo, error);
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);
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);
1017 for (i = 0; i < dtcount; i++) {
1018 struct comp_request req;
1020 error = crypto_decompress_setup(tfm, dtemplate[i].params,
1021 dtemplate[i].paramsize);
1023 pr_err("alg: pcomp: decompression setup failed on "
1024 "test %d for %s: error=%d\n", i + 1, algo,
1029 error = crypto_decompress_init(tfm);
1031 pr_err("alg: pcomp: decompression init failed on test "
1032 "%d for %s: error=%d\n", i + 1, algo, error);
1036 memset(result, 0, sizeof(result));
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;
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,
1051 /* Add remaining input data */
1052 req.avail_in += (dtemplate[i].inlen + 1) / 2;
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,
1062 /* Provide remaining output space */
1063 req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
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,
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);
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);
1092 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1095 struct crypto_aead *tfm;
1098 tfm = crypto_alloc_aead(driver, type, mask);
1100 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1101 "%ld\n", driver, PTR_ERR(tfm));
1102 return PTR_ERR(tfm);
1105 if (desc->suite.aead.enc.vecs) {
1106 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1107 desc->suite.aead.enc.count);
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);
1117 crypto_free_aead(tfm);
1121 static int alg_test_cipher(const struct alg_test_desc *desc,
1122 const char *driver, u32 type, u32 mask)
1124 struct crypto_cipher *tfm;
1127 tfm = crypto_alloc_cipher(driver, type, mask);
1129 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1130 "%s: %ld\n", driver, PTR_ERR(tfm));
1131 return PTR_ERR(tfm);
1134 if (desc->suite.cipher.enc.vecs) {
1135 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1136 desc->suite.cipher.enc.count);
1141 if (desc->suite.cipher.dec.vecs)
1142 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1143 desc->suite.cipher.dec.count);
1146 crypto_free_cipher(tfm);
1150 static int alg_test_skcipher(const struct alg_test_desc *desc,
1151 const char *driver, u32 type, u32 mask)
1153 struct crypto_ablkcipher *tfm;
1156 tfm = crypto_alloc_ablkcipher(driver, type, mask);
1158 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1159 "%s: %ld\n", driver, PTR_ERR(tfm));
1160 return PTR_ERR(tfm);
1163 if (desc->suite.cipher.enc.vecs) {
1164 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1165 desc->suite.cipher.enc.count);
1170 if (desc->suite.cipher.dec.vecs)
1171 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1172 desc->suite.cipher.dec.count);
1175 crypto_free_ablkcipher(tfm);
1179 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1182 struct crypto_comp *tfm;
1185 tfm = crypto_alloc_comp(driver, type, mask);
1187 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1188 "%ld\n", driver, PTR_ERR(tfm));
1189 return PTR_ERR(tfm);
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);
1197 crypto_free_comp(tfm);
1201 static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1204 struct crypto_pcomp *tfm;
1207 tfm = crypto_alloc_pcomp(driver, type, mask);
1209 pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1210 driver, PTR_ERR(tfm));
1211 return PTR_ERR(tfm);
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);
1219 crypto_free_pcomp(tfm);
1223 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1226 struct crypto_ahash *tfm;
1229 tfm = crypto_alloc_ahash(driver, type, mask);
1231 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1232 "%ld\n", driver, PTR_ERR(tfm));
1233 return PTR_ERR(tfm);
1236 err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
1238 crypto_free_ahash(tfm);
1242 static int alg_test_crc32c(const struct alg_test_desc *desc,
1243 const char *driver, u32 type, u32 mask)
1245 struct crypto_shash *tfm;
1249 err = alg_test_hash(desc, driver, type, mask);
1253 tfm = crypto_alloc_shash(driver, type, mask);
1255 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1256 "%ld\n", driver, PTR_ERR(tfm));
1263 struct shash_desc shash;
1264 char ctx[crypto_shash_descsize(tfm)];
1267 sdesc.shash.tfm = tfm;
1268 sdesc.shash.flags = 0;
1270 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1271 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1273 printk(KERN_ERR "alg: crc32c: Operation failed for "
1274 "%s: %d\n", driver, err);
1278 if (val != ~420553207) {
1279 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1280 "%d\n", driver, val);
1285 crypto_free_shash(tfm);
1291 /* Please keep this list sorted by algorithm name. */
1292 static const struct alg_test_desc alg_test_descs[] = {
1295 .test = alg_test_skcipher,
1299 .vecs = aes_cbc_enc_tv_template,
1300 .count = AES_CBC_ENC_TEST_VECTORS
1303 .vecs = aes_cbc_dec_tv_template,
1304 .count = AES_CBC_DEC_TEST_VECTORS
1309 .alg = "cbc(anubis)",
1310 .test = alg_test_skcipher,
1314 .vecs = anubis_cbc_enc_tv_template,
1315 .count = ANUBIS_CBC_ENC_TEST_VECTORS
1318 .vecs = anubis_cbc_dec_tv_template,
1319 .count = ANUBIS_CBC_DEC_TEST_VECTORS
1324 .alg = "cbc(blowfish)",
1325 .test = alg_test_skcipher,
1329 .vecs = bf_cbc_enc_tv_template,
1330 .count = BF_CBC_ENC_TEST_VECTORS
1333 .vecs = bf_cbc_dec_tv_template,
1334 .count = BF_CBC_DEC_TEST_VECTORS
1339 .alg = "cbc(camellia)",
1340 .test = alg_test_skcipher,
1344 .vecs = camellia_cbc_enc_tv_template,
1345 .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1348 .vecs = camellia_cbc_dec_tv_template,
1349 .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1355 .test = alg_test_skcipher,
1359 .vecs = des_cbc_enc_tv_template,
1360 .count = DES_CBC_ENC_TEST_VECTORS
1363 .vecs = des_cbc_dec_tv_template,
1364 .count = DES_CBC_DEC_TEST_VECTORS
1369 .alg = "cbc(des3_ede)",
1370 .test = alg_test_skcipher,
1374 .vecs = des3_ede_cbc_enc_tv_template,
1375 .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1378 .vecs = des3_ede_cbc_dec_tv_template,
1379 .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1384 .alg = "cbc(twofish)",
1385 .test = alg_test_skcipher,
1389 .vecs = tf_cbc_enc_tv_template,
1390 .count = TF_CBC_ENC_TEST_VECTORS
1393 .vecs = tf_cbc_dec_tv_template,
1394 .count = TF_CBC_DEC_TEST_VECTORS
1400 .test = alg_test_aead,
1404 .vecs = aes_ccm_enc_tv_template,
1405 .count = AES_CCM_ENC_TEST_VECTORS
1408 .vecs = aes_ccm_dec_tv_template,
1409 .count = AES_CCM_DEC_TEST_VECTORS
1415 .test = alg_test_crc32c,
1418 .vecs = crc32c_tv_template,
1419 .count = CRC32C_TEST_VECTORS
1423 .alg = "cts(cbc(aes))",
1424 .test = alg_test_skcipher,
1428 .vecs = cts_mode_enc_tv_template,
1429 .count = CTS_MODE_ENC_TEST_VECTORS
1432 .vecs = cts_mode_dec_tv_template,
1433 .count = CTS_MODE_DEC_TEST_VECTORS
1439 .test = alg_test_comp,
1443 .vecs = deflate_comp_tv_template,
1444 .count = DEFLATE_COMP_TEST_VECTORS
1447 .vecs = deflate_decomp_tv_template,
1448 .count = DEFLATE_DECOMP_TEST_VECTORS
1454 .test = alg_test_skcipher,
1458 .vecs = aes_enc_tv_template,
1459 .count = AES_ENC_TEST_VECTORS
1462 .vecs = aes_dec_tv_template,
1463 .count = AES_DEC_TEST_VECTORS
1468 .alg = "ecb(anubis)",
1469 .test = alg_test_skcipher,
1473 .vecs = anubis_enc_tv_template,
1474 .count = ANUBIS_ENC_TEST_VECTORS
1477 .vecs = anubis_dec_tv_template,
1478 .count = ANUBIS_DEC_TEST_VECTORS
1484 .test = alg_test_skcipher,
1488 .vecs = arc4_enc_tv_template,
1489 .count = ARC4_ENC_TEST_VECTORS
1492 .vecs = arc4_dec_tv_template,
1493 .count = ARC4_DEC_TEST_VECTORS
1498 .alg = "ecb(blowfish)",
1499 .test = alg_test_skcipher,
1503 .vecs = bf_enc_tv_template,
1504 .count = BF_ENC_TEST_VECTORS
1507 .vecs = bf_dec_tv_template,
1508 .count = BF_DEC_TEST_VECTORS
1513 .alg = "ecb(camellia)",
1514 .test = alg_test_skcipher,
1518 .vecs = camellia_enc_tv_template,
1519 .count = CAMELLIA_ENC_TEST_VECTORS
1522 .vecs = camellia_dec_tv_template,
1523 .count = CAMELLIA_DEC_TEST_VECTORS
1528 .alg = "ecb(cast5)",
1529 .test = alg_test_skcipher,
1533 .vecs = cast5_enc_tv_template,
1534 .count = CAST5_ENC_TEST_VECTORS
1537 .vecs = cast5_dec_tv_template,
1538 .count = CAST5_DEC_TEST_VECTORS
1543 .alg = "ecb(cast6)",
1544 .test = alg_test_skcipher,
1548 .vecs = cast6_enc_tv_template,
1549 .count = CAST6_ENC_TEST_VECTORS
1552 .vecs = cast6_dec_tv_template,
1553 .count = CAST6_DEC_TEST_VECTORS
1559 .test = alg_test_skcipher,
1563 .vecs = des_enc_tv_template,
1564 .count = DES_ENC_TEST_VECTORS
1567 .vecs = des_dec_tv_template,
1568 .count = DES_DEC_TEST_VECTORS
1573 .alg = "ecb(des3_ede)",
1574 .test = alg_test_skcipher,
1578 .vecs = des3_ede_enc_tv_template,
1579 .count = DES3_EDE_ENC_TEST_VECTORS
1582 .vecs = des3_ede_dec_tv_template,
1583 .count = DES3_EDE_DEC_TEST_VECTORS
1588 .alg = "ecb(khazad)",
1589 .test = alg_test_skcipher,
1593 .vecs = khazad_enc_tv_template,
1594 .count = KHAZAD_ENC_TEST_VECTORS
1597 .vecs = khazad_dec_tv_template,
1598 .count = KHAZAD_DEC_TEST_VECTORS
1604 .test = alg_test_skcipher,
1608 .vecs = seed_enc_tv_template,
1609 .count = SEED_ENC_TEST_VECTORS
1612 .vecs = seed_dec_tv_template,
1613 .count = SEED_DEC_TEST_VECTORS
1618 .alg = "ecb(serpent)",
1619 .test = alg_test_skcipher,
1623 .vecs = serpent_enc_tv_template,
1624 .count = SERPENT_ENC_TEST_VECTORS
1627 .vecs = serpent_dec_tv_template,
1628 .count = SERPENT_DEC_TEST_VECTORS
1634 .test = alg_test_skcipher,
1638 .vecs = tea_enc_tv_template,
1639 .count = TEA_ENC_TEST_VECTORS
1642 .vecs = tea_dec_tv_template,
1643 .count = TEA_DEC_TEST_VECTORS
1648 .alg = "ecb(tnepres)",
1649 .test = alg_test_skcipher,
1653 .vecs = tnepres_enc_tv_template,
1654 .count = TNEPRES_ENC_TEST_VECTORS
1657 .vecs = tnepres_dec_tv_template,
1658 .count = TNEPRES_DEC_TEST_VECTORS
1663 .alg = "ecb(twofish)",
1664 .test = alg_test_skcipher,
1668 .vecs = tf_enc_tv_template,
1669 .count = TF_ENC_TEST_VECTORS
1672 .vecs = tf_dec_tv_template,
1673 .count = TF_DEC_TEST_VECTORS
1679 .test = alg_test_skcipher,
1683 .vecs = xeta_enc_tv_template,
1684 .count = XETA_ENC_TEST_VECTORS
1687 .vecs = xeta_dec_tv_template,
1688 .count = XETA_DEC_TEST_VECTORS
1694 .test = alg_test_skcipher,
1698 .vecs = xtea_enc_tv_template,
1699 .count = XTEA_ENC_TEST_VECTORS
1702 .vecs = xtea_dec_tv_template,
1703 .count = XTEA_DEC_TEST_VECTORS
1709 .test = alg_test_aead,
1713 .vecs = aes_gcm_enc_tv_template,
1714 .count = AES_GCM_ENC_TEST_VECTORS
1717 .vecs = aes_gcm_dec_tv_template,
1718 .count = AES_GCM_DEC_TEST_VECTORS
1724 .test = alg_test_hash,
1727 .vecs = hmac_md5_tv_template,
1728 .count = HMAC_MD5_TEST_VECTORS
1732 .alg = "hmac(rmd128)",
1733 .test = alg_test_hash,
1736 .vecs = hmac_rmd128_tv_template,
1737 .count = HMAC_RMD128_TEST_VECTORS
1741 .alg = "hmac(rmd160)",
1742 .test = alg_test_hash,
1745 .vecs = hmac_rmd160_tv_template,
1746 .count = HMAC_RMD160_TEST_VECTORS
1750 .alg = "hmac(sha1)",
1751 .test = alg_test_hash,
1754 .vecs = hmac_sha1_tv_template,
1755 .count = HMAC_SHA1_TEST_VECTORS
1759 .alg = "hmac(sha224)",
1760 .test = alg_test_hash,
1763 .vecs = hmac_sha224_tv_template,
1764 .count = HMAC_SHA224_TEST_VECTORS
1768 .alg = "hmac(sha256)",
1769 .test = alg_test_hash,
1772 .vecs = hmac_sha256_tv_template,
1773 .count = HMAC_SHA256_TEST_VECTORS
1777 .alg = "hmac(sha384)",
1778 .test = alg_test_hash,
1781 .vecs = hmac_sha384_tv_template,
1782 .count = HMAC_SHA384_TEST_VECTORS
1786 .alg = "hmac(sha512)",
1787 .test = alg_test_hash,
1790 .vecs = hmac_sha512_tv_template,
1791 .count = HMAC_SHA512_TEST_VECTORS
1796 .test = alg_test_skcipher,
1800 .vecs = aes_lrw_enc_tv_template,
1801 .count = AES_LRW_ENC_TEST_VECTORS
1804 .vecs = aes_lrw_dec_tv_template,
1805 .count = AES_LRW_DEC_TEST_VECTORS
1811 .test = alg_test_comp,
1815 .vecs = lzo_comp_tv_template,
1816 .count = LZO_COMP_TEST_VECTORS
1819 .vecs = lzo_decomp_tv_template,
1820 .count = LZO_DECOMP_TEST_VECTORS
1826 .test = alg_test_hash,
1829 .vecs = md4_tv_template,
1830 .count = MD4_TEST_VECTORS
1835 .test = alg_test_hash,
1838 .vecs = md5_tv_template,
1839 .count = MD5_TEST_VECTORS
1843 .alg = "michael_mic",
1844 .test = alg_test_hash,
1847 .vecs = michael_mic_tv_template,
1848 .count = MICHAEL_MIC_TEST_VECTORS
1852 .alg = "pcbc(fcrypt)",
1853 .test = alg_test_skcipher,
1857 .vecs = fcrypt_pcbc_enc_tv_template,
1858 .count = FCRYPT_ENC_TEST_VECTORS
1861 .vecs = fcrypt_pcbc_dec_tv_template,
1862 .count = FCRYPT_DEC_TEST_VECTORS
1867 .alg = "rfc3686(ctr(aes))",
1868 .test = alg_test_skcipher,
1872 .vecs = aes_ctr_enc_tv_template,
1873 .count = AES_CTR_ENC_TEST_VECTORS
1876 .vecs = aes_ctr_dec_tv_template,
1877 .count = AES_CTR_DEC_TEST_VECTORS
1883 .test = alg_test_hash,
1886 .vecs = rmd128_tv_template,
1887 .count = RMD128_TEST_VECTORS
1892 .test = alg_test_hash,
1895 .vecs = rmd160_tv_template,
1896 .count = RMD160_TEST_VECTORS
1901 .test = alg_test_hash,
1904 .vecs = rmd256_tv_template,
1905 .count = RMD256_TEST_VECTORS
1910 .test = alg_test_hash,
1913 .vecs = rmd320_tv_template,
1914 .count = RMD320_TEST_VECTORS
1919 .test = alg_test_skcipher,
1923 .vecs = salsa20_stream_enc_tv_template,
1924 .count = SALSA20_STREAM_ENC_TEST_VECTORS
1930 .test = alg_test_hash,
1933 .vecs = sha1_tv_template,
1934 .count = SHA1_TEST_VECTORS
1939 .test = alg_test_hash,
1942 .vecs = sha224_tv_template,
1943 .count = SHA224_TEST_VECTORS
1948 .test = alg_test_hash,
1951 .vecs = sha256_tv_template,
1952 .count = SHA256_TEST_VECTORS
1957 .test = alg_test_hash,
1960 .vecs = sha384_tv_template,
1961 .count = SHA384_TEST_VECTORS
1966 .test = alg_test_hash,
1969 .vecs = sha512_tv_template,
1970 .count = SHA512_TEST_VECTORS
1975 .test = alg_test_hash,
1978 .vecs = tgr128_tv_template,
1979 .count = TGR128_TEST_VECTORS
1984 .test = alg_test_hash,
1987 .vecs = tgr160_tv_template,
1988 .count = TGR160_TEST_VECTORS
1993 .test = alg_test_hash,
1996 .vecs = tgr192_tv_template,
1997 .count = TGR192_TEST_VECTORS
2002 .test = alg_test_hash,
2005 .vecs = wp256_tv_template,
2006 .count = WP256_TEST_VECTORS
2011 .test = alg_test_hash,
2014 .vecs = wp384_tv_template,
2015 .count = WP384_TEST_VECTORS
2020 .test = alg_test_hash,
2023 .vecs = wp512_tv_template,
2024 .count = WP512_TEST_VECTORS
2029 .test = alg_test_hash,
2032 .vecs = aes_xcbc128_tv_template,
2033 .count = XCBC_AES_TEST_VECTORS
2038 .test = alg_test_skcipher,
2042 .vecs = aes_xts_enc_tv_template,
2043 .count = AES_XTS_ENC_TEST_VECTORS
2046 .vecs = aes_xts_dec_tv_template,
2047 .count = AES_XTS_DEC_TEST_VECTORS
2053 .test = alg_test_pcomp,
2057 .vecs = zlib_comp_tv_template,
2058 .count = ZLIB_COMP_TEST_VECTORS
2061 .vecs = zlib_decomp_tv_template,
2062 .count = ZLIB_DECOMP_TEST_VECTORS
2069 static int alg_find_test(const char *alg)
2072 int end = ARRAY_SIZE(alg_test_descs);
2074 while (start < end) {
2075 int i = (start + end) / 2;
2076 int diff = strcmp(alg_test_descs[i].alg, alg);
2094 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
2099 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
2100 char nalg[CRYPTO_MAX_ALG_NAME];
2102 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
2104 return -ENAMETOOLONG;
2106 i = alg_find_test(nalg);
2110 return alg_test_cipher(alg_test_descs + i, driver, type, mask);
2113 i = alg_find_test(alg);
2117 rc = alg_test_descs[i].test(alg_test_descs + i, driver,
2119 if (fips_enabled && rc)
2120 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
2125 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
2128 EXPORT_SYMBOL_GPL(alg_test);
2130 int __init testmgr_init(void)
2134 for (i = 0; i < XBUFSIZE; i++) {
2135 xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
2140 for (i = 0; i < XBUFSIZE; i++) {
2141 axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
2143 goto err_free_axbuf;
2149 for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
2150 free_page((unsigned long)axbuf[i]);
2152 for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
2153 free_page((unsigned long)xbuf[i]);
2158 void testmgr_exit(void)
2162 for (i = 0; i < XBUFSIZE; i++)
2163 free_page((unsigned long)axbuf[i]);
2164 for (i = 0; i < XBUFSIZE; i++)
2165 free_page((unsigned long)xbuf[i]);