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);
369 ret = wait_for_completion_interruptible(
371 if (!ret && !(ret = result.err)) {
372 INIT_COMPLETION(result.completion);
377 printk(KERN_ERR "alg: aead: %s failed on test "
378 "%d for %s: ret=%d\n", e, j, algo, -ret);
383 if (memcmp(q, template[i].result, template[i].rlen)) {
384 printk(KERN_ERR "alg: aead: Test %d failed on "
385 "%s for %s\n", j, e, algo);
386 hexdump(q, template[i].rlen);
393 for (i = 0, j = 0; i < tcount; i++) {
394 if (template[i].np) {
398 memcpy(iv, template[i].iv, MAX_IVLEN);
400 memset(iv, 0, MAX_IVLEN);
402 crypto_aead_clear_flags(tfm, ~0);
404 crypto_aead_set_flags(
405 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
406 key = template[i].key;
408 ret = crypto_aead_setkey(tfm, key, template[i].klen);
409 if (!ret == template[i].fail) {
410 printk(KERN_ERR "alg: aead: setkey failed on "
411 "chunk test %d for %s: flags=%x\n", j,
412 algo, crypto_aead_get_flags(tfm));
417 authsize = abs(template[i].rlen - template[i].ilen);
420 sg_init_table(sg, template[i].np);
421 for (k = 0, temp = 0; k < template[i].np; k++) {
422 if (WARN_ON(offset_in_page(IDX[k]) +
423 template[i].tap[k] > PAGE_SIZE))
426 q = xbuf[IDX[k] >> PAGE_SHIFT] +
427 offset_in_page(IDX[k]);
429 memcpy(q, template[i].input + temp,
432 n = template[i].tap[k];
433 if (k == template[i].np - 1 && enc)
435 if (offset_in_page(q) + n < PAGE_SIZE)
438 sg_set_buf(&sg[k], q, template[i].tap[k]);
439 temp += template[i].tap[k];
442 ret = crypto_aead_setauthsize(tfm, authsize);
444 printk(KERN_ERR "alg: aead: Failed to set "
445 "authsize to %u on chunk test %d for "
446 "%s\n", authsize, j, algo);
451 if (WARN_ON(sg[k - 1].offset +
452 sg[k - 1].length + authsize >
458 sg[k - 1].length += authsize;
461 sg_init_table(asg, template[i].anp);
462 for (k = 0, temp = 0; k < template[i].anp; k++) {
464 memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
465 offset_in_page(IDX[k]),
466 template[i].assoc + temp,
467 template[i].atap[k]),
468 template[i].atap[k]);
469 temp += template[i].atap[k];
472 aead_request_set_crypt(req, sg, sg,
476 aead_request_set_assoc(req, asg, template[i].alen);
479 crypto_aead_encrypt(req) :
480 crypto_aead_decrypt(req);
487 ret = wait_for_completion_interruptible(
489 if (!ret && !(ret = result.err)) {
490 INIT_COMPLETION(result.completion);
495 printk(KERN_ERR "alg: aead: %s failed on "
496 "chunk test %d for %s: ret=%d\n", e, j,
502 for (k = 0, temp = 0; k < template[i].np; k++) {
503 q = xbuf[IDX[k] >> PAGE_SHIFT] +
504 offset_in_page(IDX[k]);
506 n = template[i].tap[k];
507 if (k == template[i].np - 1)
508 n += enc ? authsize : -authsize;
510 if (memcmp(q, template[i].result + temp, n)) {
511 printk(KERN_ERR "alg: aead: Chunk "
512 "test %d failed on %s at page "
513 "%u for %s\n", j, e, k, algo);
519 if (k == template[i].np - 1 && !enc) {
520 if (memcmp(q, template[i].input +
526 for (n = 0; offset_in_page(q + n) &&
531 printk(KERN_ERR "alg: aead: Result "
532 "buffer corruption in chunk "
533 "test %d on %s at page %u for "
534 "%s: %u bytes:\n", j, e, k,
540 temp += template[i].tap[k];
548 aead_request_free(req);
552 static int test_cipher(struct crypto_cipher *tfm, int enc,
553 struct cipher_testvec *template, unsigned int tcount)
555 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
556 unsigned int i, j, k;
568 for (i = 0; i < tcount; i++) {
575 memcpy(data, template[i].input, template[i].ilen);
577 crypto_cipher_clear_flags(tfm, ~0);
579 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
581 ret = crypto_cipher_setkey(tfm, template[i].key,
583 if (!ret == template[i].fail) {
584 printk(KERN_ERR "alg: cipher: setkey failed "
585 "on test %d for %s: flags=%x\n", j,
586 algo, crypto_cipher_get_flags(tfm));
591 for (k = 0; k < template[i].ilen;
592 k += crypto_cipher_blocksize(tfm)) {
594 crypto_cipher_encrypt_one(tfm, data + k,
597 crypto_cipher_decrypt_one(tfm, data + k,
602 if (memcmp(q, template[i].result, template[i].rlen)) {
603 printk(KERN_ERR "alg: cipher: Test %d failed "
604 "on %s for %s\n", j, e, algo);
605 hexdump(q, template[i].rlen);
617 static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
618 struct cipher_testvec *template, unsigned int tcount)
621 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
622 unsigned int i, j, k, n, temp;
625 struct ablkcipher_request *req;
626 struct scatterlist sg[8];
628 struct tcrypt_result result;
637 init_completion(&result.completion);
639 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
641 printk(KERN_ERR "alg: skcipher: Failed to allocate request "
647 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
648 tcrypt_complete, &result);
651 for (i = 0; i < tcount; i++) {
653 memcpy(iv, template[i].iv, MAX_IVLEN);
655 memset(iv, 0, MAX_IVLEN);
657 if (!(template[i].np)) {
661 memcpy(data, template[i].input, template[i].ilen);
663 crypto_ablkcipher_clear_flags(tfm, ~0);
665 crypto_ablkcipher_set_flags(
666 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
668 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
670 if (!ret == template[i].fail) {
671 printk(KERN_ERR "alg: skcipher: setkey failed "
672 "on test %d for %s: flags=%x\n", j,
673 algo, crypto_ablkcipher_get_flags(tfm));
678 sg_init_one(&sg[0], data, template[i].ilen);
680 ablkcipher_request_set_crypt(req, sg, sg,
681 template[i].ilen, iv);
683 crypto_ablkcipher_encrypt(req) :
684 crypto_ablkcipher_decrypt(req);
691 ret = wait_for_completion_interruptible(
693 if (!ret && !((ret = result.err))) {
694 INIT_COMPLETION(result.completion);
699 printk(KERN_ERR "alg: skcipher: %s failed on "
700 "test %d for %s: ret=%d\n", e, j, algo,
706 if (memcmp(q, template[i].result, template[i].rlen)) {
707 printk(KERN_ERR "alg: skcipher: Test %d "
708 "failed on %s for %s\n", j, e, algo);
709 hexdump(q, template[i].rlen);
717 for (i = 0; i < tcount; i++) {
720 memcpy(iv, template[i].iv, MAX_IVLEN);
722 memset(iv, 0, MAX_IVLEN);
724 if (template[i].np) {
727 crypto_ablkcipher_clear_flags(tfm, ~0);
729 crypto_ablkcipher_set_flags(
730 tfm, CRYPTO_TFM_REQ_WEAK_KEY);
732 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
734 if (!ret == template[i].fail) {
735 printk(KERN_ERR "alg: skcipher: setkey failed "
736 "on chunk test %d for %s: flags=%x\n",
738 crypto_ablkcipher_get_flags(tfm));
745 sg_init_table(sg, template[i].np);
746 for (k = 0; k < template[i].np; k++) {
747 if (WARN_ON(offset_in_page(IDX[k]) +
748 template[i].tap[k] > PAGE_SIZE))
751 q = xbuf[IDX[k] >> PAGE_SHIFT] +
752 offset_in_page(IDX[k]);
754 memcpy(q, template[i].input + temp,
757 if (offset_in_page(q) + template[i].tap[k] <
759 q[template[i].tap[k]] = 0;
761 sg_set_buf(&sg[k], q, template[i].tap[k]);
763 temp += template[i].tap[k];
766 ablkcipher_request_set_crypt(req, sg, sg,
767 template[i].ilen, iv);
770 crypto_ablkcipher_encrypt(req) :
771 crypto_ablkcipher_decrypt(req);
778 ret = wait_for_completion_interruptible(
780 if (!ret && !((ret = result.err))) {
781 INIT_COMPLETION(result.completion);
786 printk(KERN_ERR "alg: skcipher: %s failed on "
787 "chunk test %d for %s: ret=%d\n", e, j,
794 for (k = 0; k < template[i].np; k++) {
795 q = xbuf[IDX[k] >> PAGE_SHIFT] +
796 offset_in_page(IDX[k]);
798 if (memcmp(q, template[i].result + temp,
799 template[i].tap[k])) {
800 printk(KERN_ERR "alg: skcipher: Chunk "
801 "test %d failed on %s at page "
802 "%u for %s\n", j, e, k, algo);
803 hexdump(q, template[i].tap[k]);
807 q += template[i].tap[k];
808 for (n = 0; offset_in_page(q + n) && q[n]; n++)
811 printk(KERN_ERR "alg: skcipher: "
812 "Result buffer corruption in "
813 "chunk test %d on %s at page "
814 "%u for %s: %u bytes:\n", j, e,
819 temp += template[i].tap[k];
827 ablkcipher_request_free(req);
831 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
832 struct comp_testvec *dtemplate, int ctcount, int dtcount)
834 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
836 char result[COMP_BUF_SIZE];
839 for (i = 0; i < ctcount; i++) {
841 unsigned int dlen = COMP_BUF_SIZE;
843 memset(result, 0, sizeof (result));
845 ilen = ctemplate[i].inlen;
846 ret = crypto_comp_compress(tfm, ctemplate[i].input,
847 ilen, result, &dlen);
849 printk(KERN_ERR "alg: comp: compression failed "
850 "on test %d for %s: ret=%d\n", i + 1, algo,
855 if (dlen != ctemplate[i].outlen) {
856 printk(KERN_ERR "alg: comp: Compression test %d "
857 "failed for %s: output len = %d\n", i + 1, algo,
863 if (memcmp(result, ctemplate[i].output, dlen)) {
864 printk(KERN_ERR "alg: comp: Compression test %d "
865 "failed for %s\n", i + 1, algo);
866 hexdump(result, dlen);
872 for (i = 0; i < dtcount; i++) {
874 unsigned int dlen = COMP_BUF_SIZE;
876 memset(result, 0, sizeof (result));
878 ilen = dtemplate[i].inlen;
879 ret = crypto_comp_decompress(tfm, dtemplate[i].input,
880 ilen, result, &dlen);
882 printk(KERN_ERR "alg: comp: decompression failed "
883 "on test %d for %s: ret=%d\n", i + 1, algo,
888 if (dlen != dtemplate[i].outlen) {
889 printk(KERN_ERR "alg: comp: Decompression test %d "
890 "failed for %s: output len = %d\n", i + 1, algo,
896 if (memcmp(result, dtemplate[i].output, dlen)) {
897 printk(KERN_ERR "alg: comp: Decompression test %d "
898 "failed for %s\n", i + 1, algo);
899 hexdump(result, dlen);
911 static int test_pcomp(struct crypto_pcomp *tfm,
912 struct pcomp_testvec *ctemplate,
913 struct pcomp_testvec *dtemplate, int ctcount,
916 const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
918 char result[COMP_BUF_SIZE];
921 for (i = 0; i < ctcount; i++) {
922 struct comp_request req;
924 error = crypto_compress_setup(tfm, ctemplate[i].params,
925 ctemplate[i].paramsize);
927 pr_err("alg: pcomp: compression setup failed on test "
928 "%d for %s: error=%d\n", i + 1, algo, error);
932 error = crypto_compress_init(tfm);
934 pr_err("alg: pcomp: compression init failed on test "
935 "%d for %s: error=%d\n", i + 1, algo, error);
939 memset(result, 0, sizeof(result));
941 req.next_in = ctemplate[i].input;
942 req.avail_in = ctemplate[i].inlen / 2;
943 req.next_out = result;
944 req.avail_out = ctemplate[i].outlen / 2;
946 error = crypto_compress_update(tfm, &req);
947 if (error && (error != -EAGAIN || req.avail_in)) {
948 pr_err("alg: pcomp: compression update failed on test "
949 "%d for %s: error=%d\n", i + 1, algo, error);
953 /* Add remaining input data */
954 req.avail_in += (ctemplate[i].inlen + 1) / 2;
956 error = crypto_compress_update(tfm, &req);
957 if (error && (error != -EAGAIN || req.avail_in)) {
958 pr_err("alg: pcomp: compression update failed on test "
959 "%d for %s: error=%d\n", i + 1, algo, error);
963 /* Provide remaining output space */
964 req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
966 error = crypto_compress_final(tfm, &req);
968 pr_err("alg: pcomp: compression final failed on test "
969 "%d for %s: error=%d\n", i + 1, algo, error);
973 if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
974 pr_err("alg: comp: Compression test %d failed for %s: "
975 "output len = %d (expected %d)\n", i + 1, algo,
976 COMP_BUF_SIZE - req.avail_out,
977 ctemplate[i].outlen);
981 if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
982 pr_err("alg: pcomp: Compression test %d failed for "
983 "%s\n", i + 1, algo);
984 hexdump(result, ctemplate[i].outlen);
989 for (i = 0; i < dtcount; i++) {
990 struct comp_request req;
992 error = crypto_decompress_setup(tfm, dtemplate[i].params,
993 dtemplate[i].paramsize);
995 pr_err("alg: pcomp: decompression setup failed on "
996 "test %d for %s: error=%d\n", i + 1, algo,
1001 error = crypto_decompress_init(tfm);
1003 pr_err("alg: pcomp: decompression init failed on test "
1004 "%d for %s: error=%d\n", i + 1, algo, error);
1008 memset(result, 0, sizeof(result));
1010 req.next_in = dtemplate[i].input;
1011 req.avail_in = dtemplate[i].inlen / 2;
1012 req.next_out = result;
1013 req.avail_out = dtemplate[i].outlen / 2;
1015 error = crypto_decompress_update(tfm, &req);
1016 if (error && (error != -EAGAIN || req.avail_in)) {
1017 pr_err("alg: pcomp: decompression update failed on "
1018 "test %d for %s: error=%d\n", i + 1, algo,
1023 /* Add remaining input data */
1024 req.avail_in += (dtemplate[i].inlen + 1) / 2;
1026 error = crypto_decompress_update(tfm, &req);
1027 if (error && (error != -EAGAIN || req.avail_in)) {
1028 pr_err("alg: pcomp: decompression update failed on "
1029 "test %d for %s: error=%d\n", i + 1, algo,
1034 /* Provide remaining output space */
1035 req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
1037 error = crypto_decompress_final(tfm, &req);
1038 if (error && (error != -EAGAIN || req.avail_in)) {
1039 pr_err("alg: pcomp: decompression final failed on "
1040 "test %d for %s: error=%d\n", i + 1, algo,
1045 if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
1046 pr_err("alg: comp: Decompression test %d failed for "
1047 "%s: output len = %d (expected %d)\n", i + 1,
1048 algo, COMP_BUF_SIZE - req.avail_out,
1049 dtemplate[i].outlen);
1053 if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
1054 pr_err("alg: pcomp: Decompression test %d failed for "
1055 "%s\n", i + 1, algo);
1056 hexdump(result, dtemplate[i].outlen);
1064 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
1067 struct crypto_aead *tfm;
1070 tfm = crypto_alloc_aead(driver, type, mask);
1072 printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
1073 "%ld\n", driver, PTR_ERR(tfm));
1074 return PTR_ERR(tfm);
1077 if (desc->suite.aead.enc.vecs) {
1078 err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
1079 desc->suite.aead.enc.count);
1084 if (!err && desc->suite.aead.dec.vecs)
1085 err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
1086 desc->suite.aead.dec.count);
1089 crypto_free_aead(tfm);
1093 static int alg_test_cipher(const struct alg_test_desc *desc,
1094 const char *driver, u32 type, u32 mask)
1096 struct crypto_cipher *tfm;
1099 tfm = crypto_alloc_cipher(driver, type, mask);
1101 printk(KERN_ERR "alg: cipher: Failed to load transform for "
1102 "%s: %ld\n", driver, PTR_ERR(tfm));
1103 return PTR_ERR(tfm);
1106 if (desc->suite.cipher.enc.vecs) {
1107 err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1108 desc->suite.cipher.enc.count);
1113 if (desc->suite.cipher.dec.vecs)
1114 err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1115 desc->suite.cipher.dec.count);
1118 crypto_free_cipher(tfm);
1122 static int alg_test_skcipher(const struct alg_test_desc *desc,
1123 const char *driver, u32 type, u32 mask)
1125 struct crypto_ablkcipher *tfm;
1128 tfm = crypto_alloc_ablkcipher(driver, type, mask);
1130 printk(KERN_ERR "alg: skcipher: Failed to load transform for "
1131 "%s: %ld\n", driver, PTR_ERR(tfm));
1132 return PTR_ERR(tfm);
1135 if (desc->suite.cipher.enc.vecs) {
1136 err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
1137 desc->suite.cipher.enc.count);
1142 if (desc->suite.cipher.dec.vecs)
1143 err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
1144 desc->suite.cipher.dec.count);
1147 crypto_free_ablkcipher(tfm);
1151 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
1154 struct crypto_comp *tfm;
1157 tfm = crypto_alloc_comp(driver, type, mask);
1159 printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
1160 "%ld\n", driver, PTR_ERR(tfm));
1161 return PTR_ERR(tfm);
1164 err = test_comp(tfm, desc->suite.comp.comp.vecs,
1165 desc->suite.comp.decomp.vecs,
1166 desc->suite.comp.comp.count,
1167 desc->suite.comp.decomp.count);
1169 crypto_free_comp(tfm);
1173 static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
1176 struct crypto_pcomp *tfm;
1179 tfm = crypto_alloc_pcomp(driver, type, mask);
1181 pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
1182 driver, PTR_ERR(tfm));
1183 return PTR_ERR(tfm);
1186 err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
1187 desc->suite.pcomp.decomp.vecs,
1188 desc->suite.pcomp.comp.count,
1189 desc->suite.pcomp.decomp.count);
1191 crypto_free_pcomp(tfm);
1195 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
1198 struct crypto_ahash *tfm;
1201 tfm = crypto_alloc_ahash(driver, type, mask);
1203 printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
1204 "%ld\n", driver, PTR_ERR(tfm));
1205 return PTR_ERR(tfm);
1208 err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
1210 crypto_free_ahash(tfm);
1214 static int alg_test_crc32c(const struct alg_test_desc *desc,
1215 const char *driver, u32 type, u32 mask)
1217 struct crypto_shash *tfm;
1221 err = alg_test_hash(desc, driver, type, mask);
1225 tfm = crypto_alloc_shash(driver, type, mask);
1227 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
1228 "%ld\n", driver, PTR_ERR(tfm));
1235 struct shash_desc shash;
1236 char ctx[crypto_shash_descsize(tfm)];
1239 sdesc.shash.tfm = tfm;
1240 sdesc.shash.flags = 0;
1242 *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
1243 err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
1245 printk(KERN_ERR "alg: crc32c: Operation failed for "
1246 "%s: %d\n", driver, err);
1250 if (val != ~420553207) {
1251 printk(KERN_ERR "alg: crc32c: Test failed for %s: "
1252 "%d\n", driver, val);
1257 crypto_free_shash(tfm);
1263 /* Please keep this list sorted by algorithm name. */
1264 static const struct alg_test_desc alg_test_descs[] = {
1267 .test = alg_test_skcipher,
1271 .vecs = aes_cbc_enc_tv_template,
1272 .count = AES_CBC_ENC_TEST_VECTORS
1275 .vecs = aes_cbc_dec_tv_template,
1276 .count = AES_CBC_DEC_TEST_VECTORS
1281 .alg = "cbc(anubis)",
1282 .test = alg_test_skcipher,
1286 .vecs = anubis_cbc_enc_tv_template,
1287 .count = ANUBIS_CBC_ENC_TEST_VECTORS
1290 .vecs = anubis_cbc_dec_tv_template,
1291 .count = ANUBIS_CBC_DEC_TEST_VECTORS
1296 .alg = "cbc(blowfish)",
1297 .test = alg_test_skcipher,
1301 .vecs = bf_cbc_enc_tv_template,
1302 .count = BF_CBC_ENC_TEST_VECTORS
1305 .vecs = bf_cbc_dec_tv_template,
1306 .count = BF_CBC_DEC_TEST_VECTORS
1311 .alg = "cbc(camellia)",
1312 .test = alg_test_skcipher,
1316 .vecs = camellia_cbc_enc_tv_template,
1317 .count = CAMELLIA_CBC_ENC_TEST_VECTORS
1320 .vecs = camellia_cbc_dec_tv_template,
1321 .count = CAMELLIA_CBC_DEC_TEST_VECTORS
1327 .test = alg_test_skcipher,
1331 .vecs = des_cbc_enc_tv_template,
1332 .count = DES_CBC_ENC_TEST_VECTORS
1335 .vecs = des_cbc_dec_tv_template,
1336 .count = DES_CBC_DEC_TEST_VECTORS
1341 .alg = "cbc(des3_ede)",
1342 .test = alg_test_skcipher,
1346 .vecs = des3_ede_cbc_enc_tv_template,
1347 .count = DES3_EDE_CBC_ENC_TEST_VECTORS
1350 .vecs = des3_ede_cbc_dec_tv_template,
1351 .count = DES3_EDE_CBC_DEC_TEST_VECTORS
1356 .alg = "cbc(twofish)",
1357 .test = alg_test_skcipher,
1361 .vecs = tf_cbc_enc_tv_template,
1362 .count = TF_CBC_ENC_TEST_VECTORS
1365 .vecs = tf_cbc_dec_tv_template,
1366 .count = TF_CBC_DEC_TEST_VECTORS
1372 .test = alg_test_aead,
1376 .vecs = aes_ccm_enc_tv_template,
1377 .count = AES_CCM_ENC_TEST_VECTORS
1380 .vecs = aes_ccm_dec_tv_template,
1381 .count = AES_CCM_DEC_TEST_VECTORS
1387 .test = alg_test_crc32c,
1390 .vecs = crc32c_tv_template,
1391 .count = CRC32C_TEST_VECTORS
1395 .alg = "cts(cbc(aes))",
1396 .test = alg_test_skcipher,
1400 .vecs = cts_mode_enc_tv_template,
1401 .count = CTS_MODE_ENC_TEST_VECTORS
1404 .vecs = cts_mode_dec_tv_template,
1405 .count = CTS_MODE_DEC_TEST_VECTORS
1411 .test = alg_test_comp,
1415 .vecs = deflate_comp_tv_template,
1416 .count = DEFLATE_COMP_TEST_VECTORS
1419 .vecs = deflate_decomp_tv_template,
1420 .count = DEFLATE_DECOMP_TEST_VECTORS
1426 .test = alg_test_skcipher,
1430 .vecs = aes_enc_tv_template,
1431 .count = AES_ENC_TEST_VECTORS
1434 .vecs = aes_dec_tv_template,
1435 .count = AES_DEC_TEST_VECTORS
1440 .alg = "ecb(anubis)",
1441 .test = alg_test_skcipher,
1445 .vecs = anubis_enc_tv_template,
1446 .count = ANUBIS_ENC_TEST_VECTORS
1449 .vecs = anubis_dec_tv_template,
1450 .count = ANUBIS_DEC_TEST_VECTORS
1456 .test = alg_test_skcipher,
1460 .vecs = arc4_enc_tv_template,
1461 .count = ARC4_ENC_TEST_VECTORS
1464 .vecs = arc4_dec_tv_template,
1465 .count = ARC4_DEC_TEST_VECTORS
1470 .alg = "ecb(blowfish)",
1471 .test = alg_test_skcipher,
1475 .vecs = bf_enc_tv_template,
1476 .count = BF_ENC_TEST_VECTORS
1479 .vecs = bf_dec_tv_template,
1480 .count = BF_DEC_TEST_VECTORS
1485 .alg = "ecb(camellia)",
1486 .test = alg_test_skcipher,
1490 .vecs = camellia_enc_tv_template,
1491 .count = CAMELLIA_ENC_TEST_VECTORS
1494 .vecs = camellia_dec_tv_template,
1495 .count = CAMELLIA_DEC_TEST_VECTORS
1500 .alg = "ecb(cast5)",
1501 .test = alg_test_skcipher,
1505 .vecs = cast5_enc_tv_template,
1506 .count = CAST5_ENC_TEST_VECTORS
1509 .vecs = cast5_dec_tv_template,
1510 .count = CAST5_DEC_TEST_VECTORS
1515 .alg = "ecb(cast6)",
1516 .test = alg_test_skcipher,
1520 .vecs = cast6_enc_tv_template,
1521 .count = CAST6_ENC_TEST_VECTORS
1524 .vecs = cast6_dec_tv_template,
1525 .count = CAST6_DEC_TEST_VECTORS
1531 .test = alg_test_skcipher,
1535 .vecs = des_enc_tv_template,
1536 .count = DES_ENC_TEST_VECTORS
1539 .vecs = des_dec_tv_template,
1540 .count = DES_DEC_TEST_VECTORS
1545 .alg = "ecb(des3_ede)",
1546 .test = alg_test_skcipher,
1550 .vecs = des3_ede_enc_tv_template,
1551 .count = DES3_EDE_ENC_TEST_VECTORS
1554 .vecs = des3_ede_dec_tv_template,
1555 .count = DES3_EDE_DEC_TEST_VECTORS
1560 .alg = "ecb(khazad)",
1561 .test = alg_test_skcipher,
1565 .vecs = khazad_enc_tv_template,
1566 .count = KHAZAD_ENC_TEST_VECTORS
1569 .vecs = khazad_dec_tv_template,
1570 .count = KHAZAD_DEC_TEST_VECTORS
1576 .test = alg_test_skcipher,
1580 .vecs = seed_enc_tv_template,
1581 .count = SEED_ENC_TEST_VECTORS
1584 .vecs = seed_dec_tv_template,
1585 .count = SEED_DEC_TEST_VECTORS
1590 .alg = "ecb(serpent)",
1591 .test = alg_test_skcipher,
1595 .vecs = serpent_enc_tv_template,
1596 .count = SERPENT_ENC_TEST_VECTORS
1599 .vecs = serpent_dec_tv_template,
1600 .count = SERPENT_DEC_TEST_VECTORS
1606 .test = alg_test_skcipher,
1610 .vecs = tea_enc_tv_template,
1611 .count = TEA_ENC_TEST_VECTORS
1614 .vecs = tea_dec_tv_template,
1615 .count = TEA_DEC_TEST_VECTORS
1620 .alg = "ecb(tnepres)",
1621 .test = alg_test_skcipher,
1625 .vecs = tnepres_enc_tv_template,
1626 .count = TNEPRES_ENC_TEST_VECTORS
1629 .vecs = tnepres_dec_tv_template,
1630 .count = TNEPRES_DEC_TEST_VECTORS
1635 .alg = "ecb(twofish)",
1636 .test = alg_test_skcipher,
1640 .vecs = tf_enc_tv_template,
1641 .count = TF_ENC_TEST_VECTORS
1644 .vecs = tf_dec_tv_template,
1645 .count = TF_DEC_TEST_VECTORS
1651 .test = alg_test_skcipher,
1655 .vecs = xeta_enc_tv_template,
1656 .count = XETA_ENC_TEST_VECTORS
1659 .vecs = xeta_dec_tv_template,
1660 .count = XETA_DEC_TEST_VECTORS
1666 .test = alg_test_skcipher,
1670 .vecs = xtea_enc_tv_template,
1671 .count = XTEA_ENC_TEST_VECTORS
1674 .vecs = xtea_dec_tv_template,
1675 .count = XTEA_DEC_TEST_VECTORS
1681 .test = alg_test_aead,
1685 .vecs = aes_gcm_enc_tv_template,
1686 .count = AES_GCM_ENC_TEST_VECTORS
1689 .vecs = aes_gcm_dec_tv_template,
1690 .count = AES_GCM_DEC_TEST_VECTORS
1696 .test = alg_test_hash,
1699 .vecs = hmac_md5_tv_template,
1700 .count = HMAC_MD5_TEST_VECTORS
1704 .alg = "hmac(rmd128)",
1705 .test = alg_test_hash,
1708 .vecs = hmac_rmd128_tv_template,
1709 .count = HMAC_RMD128_TEST_VECTORS
1713 .alg = "hmac(rmd160)",
1714 .test = alg_test_hash,
1717 .vecs = hmac_rmd160_tv_template,
1718 .count = HMAC_RMD160_TEST_VECTORS
1722 .alg = "hmac(sha1)",
1723 .test = alg_test_hash,
1726 .vecs = hmac_sha1_tv_template,
1727 .count = HMAC_SHA1_TEST_VECTORS
1731 .alg = "hmac(sha224)",
1732 .test = alg_test_hash,
1735 .vecs = hmac_sha224_tv_template,
1736 .count = HMAC_SHA224_TEST_VECTORS
1740 .alg = "hmac(sha256)",
1741 .test = alg_test_hash,
1744 .vecs = hmac_sha256_tv_template,
1745 .count = HMAC_SHA256_TEST_VECTORS
1749 .alg = "hmac(sha384)",
1750 .test = alg_test_hash,
1753 .vecs = hmac_sha384_tv_template,
1754 .count = HMAC_SHA384_TEST_VECTORS
1758 .alg = "hmac(sha512)",
1759 .test = alg_test_hash,
1762 .vecs = hmac_sha512_tv_template,
1763 .count = HMAC_SHA512_TEST_VECTORS
1768 .test = alg_test_skcipher,
1772 .vecs = aes_lrw_enc_tv_template,
1773 .count = AES_LRW_ENC_TEST_VECTORS
1776 .vecs = aes_lrw_dec_tv_template,
1777 .count = AES_LRW_DEC_TEST_VECTORS
1783 .test = alg_test_comp,
1787 .vecs = lzo_comp_tv_template,
1788 .count = LZO_COMP_TEST_VECTORS
1791 .vecs = lzo_decomp_tv_template,
1792 .count = LZO_DECOMP_TEST_VECTORS
1798 .test = alg_test_hash,
1801 .vecs = md4_tv_template,
1802 .count = MD4_TEST_VECTORS
1807 .test = alg_test_hash,
1810 .vecs = md5_tv_template,
1811 .count = MD5_TEST_VECTORS
1815 .alg = "michael_mic",
1816 .test = alg_test_hash,
1819 .vecs = michael_mic_tv_template,
1820 .count = MICHAEL_MIC_TEST_VECTORS
1824 .alg = "pcbc(fcrypt)",
1825 .test = alg_test_skcipher,
1829 .vecs = fcrypt_pcbc_enc_tv_template,
1830 .count = FCRYPT_ENC_TEST_VECTORS
1833 .vecs = fcrypt_pcbc_dec_tv_template,
1834 .count = FCRYPT_DEC_TEST_VECTORS
1839 .alg = "rfc3686(ctr(aes))",
1840 .test = alg_test_skcipher,
1844 .vecs = aes_ctr_enc_tv_template,
1845 .count = AES_CTR_ENC_TEST_VECTORS
1848 .vecs = aes_ctr_dec_tv_template,
1849 .count = AES_CTR_DEC_TEST_VECTORS
1855 .test = alg_test_hash,
1858 .vecs = rmd128_tv_template,
1859 .count = RMD128_TEST_VECTORS
1864 .test = alg_test_hash,
1867 .vecs = rmd160_tv_template,
1868 .count = RMD160_TEST_VECTORS
1873 .test = alg_test_hash,
1876 .vecs = rmd256_tv_template,
1877 .count = RMD256_TEST_VECTORS
1882 .test = alg_test_hash,
1885 .vecs = rmd320_tv_template,
1886 .count = RMD320_TEST_VECTORS
1891 .test = alg_test_skcipher,
1895 .vecs = salsa20_stream_enc_tv_template,
1896 .count = SALSA20_STREAM_ENC_TEST_VECTORS
1902 .test = alg_test_hash,
1905 .vecs = sha1_tv_template,
1906 .count = SHA1_TEST_VECTORS
1911 .test = alg_test_hash,
1914 .vecs = sha224_tv_template,
1915 .count = SHA224_TEST_VECTORS
1920 .test = alg_test_hash,
1923 .vecs = sha256_tv_template,
1924 .count = SHA256_TEST_VECTORS
1929 .test = alg_test_hash,
1932 .vecs = sha384_tv_template,
1933 .count = SHA384_TEST_VECTORS
1938 .test = alg_test_hash,
1941 .vecs = sha512_tv_template,
1942 .count = SHA512_TEST_VECTORS
1947 .test = alg_test_hash,
1950 .vecs = tgr128_tv_template,
1951 .count = TGR128_TEST_VECTORS
1956 .test = alg_test_hash,
1959 .vecs = tgr160_tv_template,
1960 .count = TGR160_TEST_VECTORS
1965 .test = alg_test_hash,
1968 .vecs = tgr192_tv_template,
1969 .count = TGR192_TEST_VECTORS
1974 .test = alg_test_hash,
1977 .vecs = wp256_tv_template,
1978 .count = WP256_TEST_VECTORS
1983 .test = alg_test_hash,
1986 .vecs = wp384_tv_template,
1987 .count = WP384_TEST_VECTORS
1992 .test = alg_test_hash,
1995 .vecs = wp512_tv_template,
1996 .count = WP512_TEST_VECTORS
2001 .test = alg_test_hash,
2004 .vecs = aes_xcbc128_tv_template,
2005 .count = XCBC_AES_TEST_VECTORS
2010 .test = alg_test_skcipher,
2014 .vecs = aes_xts_enc_tv_template,
2015 .count = AES_XTS_ENC_TEST_VECTORS
2018 .vecs = aes_xts_dec_tv_template,
2019 .count = AES_XTS_DEC_TEST_VECTORS
2025 .test = alg_test_pcomp,
2029 .vecs = zlib_comp_tv_template,
2030 .count = ZLIB_COMP_TEST_VECTORS
2033 .vecs = zlib_decomp_tv_template,
2034 .count = ZLIB_DECOMP_TEST_VECTORS
2041 static int alg_find_test(const char *alg)
2044 int end = ARRAY_SIZE(alg_test_descs);
2046 while (start < end) {
2047 int i = (start + end) / 2;
2048 int diff = strcmp(alg_test_descs[i].alg, alg);
2066 int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
2071 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
2072 char nalg[CRYPTO_MAX_ALG_NAME];
2074 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
2076 return -ENAMETOOLONG;
2078 i = alg_find_test(nalg);
2082 return alg_test_cipher(alg_test_descs + i, driver, type, mask);
2085 i = alg_find_test(alg);
2089 rc = alg_test_descs[i].test(alg_test_descs + i, driver,
2091 if (fips_enabled && rc)
2092 panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
2097 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
2100 EXPORT_SYMBOL_GPL(alg_test);
2102 int __init testmgr_init(void)
2106 for (i = 0; i < XBUFSIZE; i++) {
2107 xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
2112 for (i = 0; i < XBUFSIZE; i++) {
2113 axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
2115 goto err_free_axbuf;
2121 for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
2122 free_page((unsigned long)axbuf[i]);
2124 for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
2125 free_page((unsigned long)xbuf[i]);
2130 void testmgr_exit(void)
2134 for (i = 0; i < XBUFSIZE; i++)
2135 free_page((unsigned long)axbuf[i]);
2136 for (i = 0; i < XBUFSIZE; i++)
2137 free_page((unsigned long)xbuf[i]);