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 hash_test_suite
{
76 struct hash_testvec
*vecs
;
80 struct alg_test_desc
{
82 int (*test
)(const struct alg_test_desc
*desc
, const char *driver
,
86 struct aead_test_suite aead
;
87 struct cipher_test_suite cipher
;
88 struct comp_test_suite comp
;
89 struct hash_test_suite hash
;
93 static unsigned int IDX
[8] = { IDX1
, IDX2
, IDX3
, IDX4
, IDX5
, IDX6
, IDX7
, IDX8
};
95 static char *xbuf
[XBUFSIZE
];
96 static char *axbuf
[XBUFSIZE
];
98 static void hexdump(unsigned char *buf
, unsigned int len
)
100 print_hex_dump(KERN_CONT
, "", DUMP_PREFIX_OFFSET
,
105 static void tcrypt_complete(struct crypto_async_request
*req
, int err
)
107 struct tcrypt_result
*res
= req
->data
;
109 if (err
== -EINPROGRESS
)
113 complete(&res
->completion
);
116 static int test_hash(struct crypto_ahash
*tfm
, struct hash_testvec
*template,
119 const char *algo
= crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm
));
120 unsigned int i
, j
, k
, temp
;
121 struct scatterlist sg
[8];
123 struct ahash_request
*req
;
124 struct tcrypt_result tresult
;
128 init_completion(&tresult
.completion
);
130 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
132 printk(KERN_ERR
"alg: hash: Failed to allocate request for "
137 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
138 tcrypt_complete
, &tresult
);
140 for (i
= 0; i
< tcount
; i
++) {
141 memset(result
, 0, 64);
145 memcpy(hash_buff
, template[i
].plaintext
, template[i
].psize
);
146 sg_init_one(&sg
[0], hash_buff
, template[i
].psize
);
148 if (template[i
].ksize
) {
149 crypto_ahash_clear_flags(tfm
, ~0);
150 ret
= crypto_ahash_setkey(tfm
, template[i
].key
,
153 printk(KERN_ERR
"alg: hash: setkey failed on "
154 "test %d for %s: ret=%d\n", i
+ 1, algo
,
160 ahash_request_set_crypt(req
, sg
, result
, template[i
].psize
);
161 ret
= crypto_ahash_digest(req
);
167 ret
= wait_for_completion_interruptible(
168 &tresult
.completion
);
169 if (!ret
&& !(ret
= tresult
.err
)) {
170 INIT_COMPLETION(tresult
.completion
);
175 printk(KERN_ERR
"alg: hash: digest failed on test %d "
176 "for %s: ret=%d\n", i
+ 1, algo
, -ret
);
180 if (memcmp(result
, template[i
].digest
,
181 crypto_ahash_digestsize(tfm
))) {
182 printk(KERN_ERR
"alg: hash: Test %d failed for %s\n",
184 hexdump(result
, crypto_ahash_digestsize(tfm
));
191 for (i
= 0; i
< tcount
; i
++) {
192 if (template[i
].np
) {
194 memset(result
, 0, 64);
197 sg_init_table(sg
, template[i
].np
);
198 for (k
= 0; k
< template[i
].np
; k
++) {
200 memcpy(xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
201 offset_in_page(IDX
[k
]),
202 template[i
].plaintext
+ temp
,
205 temp
+= template[i
].tap
[k
];
208 if (template[i
].ksize
) {
209 crypto_ahash_clear_flags(tfm
, ~0);
210 ret
= crypto_ahash_setkey(tfm
, template[i
].key
,
214 printk(KERN_ERR
"alg: hash: setkey "
215 "failed on chunking test %d "
216 "for %s: ret=%d\n", j
, algo
,
222 ahash_request_set_crypt(req
, sg
, result
,
224 ret
= crypto_ahash_digest(req
);
230 ret
= wait_for_completion_interruptible(
231 &tresult
.completion
);
232 if (!ret
&& !(ret
= tresult
.err
)) {
233 INIT_COMPLETION(tresult
.completion
);
238 printk(KERN_ERR
"alg: hash: digest failed "
239 "on chunking test %d for %s: "
240 "ret=%d\n", j
, algo
, -ret
);
244 if (memcmp(result
, template[i
].digest
,
245 crypto_ahash_digestsize(tfm
))) {
246 printk(KERN_ERR
"alg: hash: Chunking test %d "
247 "failed for %s\n", j
, algo
);
248 hexdump(result
, crypto_ahash_digestsize(tfm
));
258 ahash_request_free(req
);
263 static int test_aead(struct crypto_aead
*tfm
, int enc
,
264 struct aead_testvec
*template, unsigned int tcount
)
266 const char *algo
= crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm
));
267 unsigned int i
, j
, k
, n
, temp
;
271 struct aead_request
*req
;
272 struct scatterlist sg
[8];
273 struct scatterlist asg
[8];
275 struct tcrypt_result result
;
276 unsigned int authsize
;
286 init_completion(&result
.completion
);
288 req
= aead_request_alloc(tfm
, GFP_KERNEL
);
290 printk(KERN_ERR
"alg: aead: Failed to allocate request for "
296 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
297 tcrypt_complete
, &result
);
299 for (i
= 0, j
= 0; i
< tcount
; i
++) {
300 if (!template[i
].np
) {
303 /* some tepmplates have no input data but they will
309 memcpy(input
, template[i
].input
, template[i
].ilen
);
310 memcpy(assoc
, template[i
].assoc
, template[i
].alen
);
312 memcpy(iv
, template[i
].iv
, MAX_IVLEN
);
314 memset(iv
, 0, MAX_IVLEN
);
316 crypto_aead_clear_flags(tfm
, ~0);
318 crypto_aead_set_flags(
319 tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
321 key
= template[i
].key
;
323 ret
= crypto_aead_setkey(tfm
, key
,
325 if (!ret
== template[i
].fail
) {
326 printk(KERN_ERR
"alg: aead: setkey failed on "
327 "test %d for %s: flags=%x\n", j
, algo
,
328 crypto_aead_get_flags(tfm
));
333 authsize
= abs(template[i
].rlen
- template[i
].ilen
);
334 ret
= crypto_aead_setauthsize(tfm
, authsize
);
336 printk(KERN_ERR
"alg: aead: Failed to set "
337 "authsize to %u on test %d for %s\n",
342 sg_init_one(&sg
[0], input
,
343 template[i
].ilen
+ (enc
? authsize
: 0));
345 sg_init_one(&asg
[0], assoc
, template[i
].alen
);
347 aead_request_set_crypt(req
, sg
, sg
,
348 template[i
].ilen
, iv
);
350 aead_request_set_assoc(req
, asg
, template[i
].alen
);
353 crypto_aead_encrypt(req
) :
354 crypto_aead_decrypt(req
);
361 ret
= wait_for_completion_interruptible(
363 if (!ret
&& !(ret
= result
.err
)) {
364 INIT_COMPLETION(result
.completion
);
369 printk(KERN_ERR
"alg: aead: %s failed on test "
370 "%d for %s: ret=%d\n", e
, j
, algo
, -ret
);
375 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
376 printk(KERN_ERR
"alg: aead: Test %d failed on "
377 "%s for %s\n", j
, e
, algo
);
378 hexdump(q
, template[i
].rlen
);
385 for (i
= 0, j
= 0; i
< tcount
; i
++) {
386 if (template[i
].np
) {
390 memcpy(iv
, template[i
].iv
, MAX_IVLEN
);
392 memset(iv
, 0, MAX_IVLEN
);
394 crypto_aead_clear_flags(tfm
, ~0);
396 crypto_aead_set_flags(
397 tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
398 key
= template[i
].key
;
400 ret
= crypto_aead_setkey(tfm
, key
, template[i
].klen
);
401 if (!ret
== template[i
].fail
) {
402 printk(KERN_ERR
"alg: aead: setkey failed on "
403 "chunk test %d for %s: flags=%x\n", j
,
404 algo
, crypto_aead_get_flags(tfm
));
409 authsize
= abs(template[i
].rlen
- template[i
].ilen
);
412 sg_init_table(sg
, template[i
].np
);
413 for (k
= 0, temp
= 0; k
< template[i
].np
; k
++) {
414 if (WARN_ON(offset_in_page(IDX
[k
]) +
415 template[i
].tap
[k
] > PAGE_SIZE
))
418 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
419 offset_in_page(IDX
[k
]);
421 memcpy(q
, template[i
].input
+ temp
,
424 n
= template[i
].tap
[k
];
425 if (k
== template[i
].np
- 1 && enc
)
427 if (offset_in_page(q
) + n
< PAGE_SIZE
)
430 sg_set_buf(&sg
[k
], q
, template[i
].tap
[k
]);
431 temp
+= template[i
].tap
[k
];
434 ret
= crypto_aead_setauthsize(tfm
, authsize
);
436 printk(KERN_ERR
"alg: aead: Failed to set "
437 "authsize to %u on chunk test %d for "
438 "%s\n", authsize
, j
, algo
);
443 if (WARN_ON(sg
[k
- 1].offset
+
444 sg
[k
- 1].length
+ authsize
>
450 sg
[k
- 1].length
+= authsize
;
453 sg_init_table(asg
, template[i
].anp
);
454 for (k
= 0, temp
= 0; k
< template[i
].anp
; k
++) {
456 memcpy(axbuf
[IDX
[k
] >> PAGE_SHIFT
] +
457 offset_in_page(IDX
[k
]),
458 template[i
].assoc
+ temp
,
459 template[i
].atap
[k
]),
460 template[i
].atap
[k
]);
461 temp
+= template[i
].atap
[k
];
464 aead_request_set_crypt(req
, sg
, sg
,
468 aead_request_set_assoc(req
, asg
, template[i
].alen
);
471 crypto_aead_encrypt(req
) :
472 crypto_aead_decrypt(req
);
479 ret
= wait_for_completion_interruptible(
481 if (!ret
&& !(ret
= result
.err
)) {
482 INIT_COMPLETION(result
.completion
);
487 printk(KERN_ERR
"alg: aead: %s failed on "
488 "chunk test %d for %s: ret=%d\n", e
, j
,
494 for (k
= 0, temp
= 0; k
< template[i
].np
; k
++) {
495 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
496 offset_in_page(IDX
[k
]);
498 n
= template[i
].tap
[k
];
499 if (k
== template[i
].np
- 1)
500 n
+= enc
? authsize
: -authsize
;
502 if (memcmp(q
, template[i
].result
+ temp
, n
)) {
503 printk(KERN_ERR
"alg: aead: Chunk "
504 "test %d failed on %s at page "
505 "%u for %s\n", j
, e
, k
, algo
);
511 if (k
== template[i
].np
- 1 && !enc
) {
512 if (memcmp(q
, template[i
].input
+
518 for (n
= 0; offset_in_page(q
+ n
) &&
523 printk(KERN_ERR
"alg: aead: Result "
524 "buffer corruption in chunk "
525 "test %d on %s at page %u for "
526 "%s: %u bytes:\n", j
, e
, k
,
532 temp
+= template[i
].tap
[k
];
540 aead_request_free(req
);
544 static int test_cipher(struct crypto_cipher
*tfm
, int enc
,
545 struct cipher_testvec
*template, unsigned int tcount
)
547 const char *algo
= crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm
));
548 unsigned int i
, j
, k
;
560 for (i
= 0; i
< tcount
; i
++) {
567 memcpy(data
, template[i
].input
, template[i
].ilen
);
569 crypto_cipher_clear_flags(tfm
, ~0);
571 crypto_cipher_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
573 ret
= crypto_cipher_setkey(tfm
, template[i
].key
,
575 if (!ret
== template[i
].fail
) {
576 printk(KERN_ERR
"alg: cipher: setkey failed "
577 "on test %d for %s: flags=%x\n", j
,
578 algo
, crypto_cipher_get_flags(tfm
));
583 for (k
= 0; k
< template[i
].ilen
;
584 k
+= crypto_cipher_blocksize(tfm
)) {
586 crypto_cipher_encrypt_one(tfm
, data
+ k
,
589 crypto_cipher_decrypt_one(tfm
, data
+ k
,
594 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
595 printk(KERN_ERR
"alg: cipher: Test %d failed "
596 "on %s for %s\n", j
, e
, algo
);
597 hexdump(q
, template[i
].rlen
);
609 static int test_skcipher(struct crypto_ablkcipher
*tfm
, int enc
,
610 struct cipher_testvec
*template, unsigned int tcount
)
613 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm
));
614 unsigned int i
, j
, k
, n
, temp
;
617 struct ablkcipher_request
*req
;
618 struct scatterlist sg
[8];
620 struct tcrypt_result result
;
629 init_completion(&result
.completion
);
631 req
= ablkcipher_request_alloc(tfm
, GFP_KERNEL
);
633 printk(KERN_ERR
"alg: skcipher: Failed to allocate request "
639 ablkcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
640 tcrypt_complete
, &result
);
643 for (i
= 0; i
< tcount
; i
++) {
645 memcpy(iv
, template[i
].iv
, MAX_IVLEN
);
647 memset(iv
, 0, MAX_IVLEN
);
649 if (!(template[i
].np
)) {
653 memcpy(data
, template[i
].input
, template[i
].ilen
);
655 crypto_ablkcipher_clear_flags(tfm
, ~0);
657 crypto_ablkcipher_set_flags(
658 tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
660 ret
= crypto_ablkcipher_setkey(tfm
, template[i
].key
,
662 if (!ret
== template[i
].fail
) {
663 printk(KERN_ERR
"alg: skcipher: setkey failed "
664 "on test %d for %s: flags=%x\n", j
,
665 algo
, crypto_ablkcipher_get_flags(tfm
));
670 sg_init_one(&sg
[0], data
, template[i
].ilen
);
672 ablkcipher_request_set_crypt(req
, sg
, sg
,
673 template[i
].ilen
, iv
);
675 crypto_ablkcipher_encrypt(req
) :
676 crypto_ablkcipher_decrypt(req
);
683 ret
= wait_for_completion_interruptible(
685 if (!ret
&& !((ret
= result
.err
))) {
686 INIT_COMPLETION(result
.completion
);
691 printk(KERN_ERR
"alg: skcipher: %s failed on "
692 "test %d for %s: ret=%d\n", e
, j
, algo
,
698 if (memcmp(q
, template[i
].result
, template[i
].rlen
)) {
699 printk(KERN_ERR
"alg: skcipher: Test %d "
700 "failed on %s for %s\n", j
, e
, algo
);
701 hexdump(q
, template[i
].rlen
);
709 for (i
= 0; i
< tcount
; i
++) {
712 memcpy(iv
, template[i
].iv
, MAX_IVLEN
);
714 memset(iv
, 0, MAX_IVLEN
);
716 if (template[i
].np
) {
719 crypto_ablkcipher_clear_flags(tfm
, ~0);
721 crypto_ablkcipher_set_flags(
722 tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
724 ret
= crypto_ablkcipher_setkey(tfm
, template[i
].key
,
726 if (!ret
== template[i
].fail
) {
727 printk(KERN_ERR
"alg: skcipher: setkey failed "
728 "on chunk test %d for %s: flags=%x\n",
730 crypto_ablkcipher_get_flags(tfm
));
737 sg_init_table(sg
, template[i
].np
);
738 for (k
= 0; k
< template[i
].np
; k
++) {
739 if (WARN_ON(offset_in_page(IDX
[k
]) +
740 template[i
].tap
[k
] > PAGE_SIZE
))
743 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
744 offset_in_page(IDX
[k
]);
746 memcpy(q
, template[i
].input
+ temp
,
749 if (offset_in_page(q
) + template[i
].tap
[k
] <
751 q
[template[i
].tap
[k
]] = 0;
753 sg_set_buf(&sg
[k
], q
, template[i
].tap
[k
]);
755 temp
+= template[i
].tap
[k
];
758 ablkcipher_request_set_crypt(req
, sg
, sg
,
759 template[i
].ilen
, iv
);
762 crypto_ablkcipher_encrypt(req
) :
763 crypto_ablkcipher_decrypt(req
);
770 ret
= wait_for_completion_interruptible(
772 if (!ret
&& !((ret
= result
.err
))) {
773 INIT_COMPLETION(result
.completion
);
778 printk(KERN_ERR
"alg: skcipher: %s failed on "
779 "chunk test %d for %s: ret=%d\n", e
, j
,
786 for (k
= 0; k
< template[i
].np
; k
++) {
787 q
= xbuf
[IDX
[k
] >> PAGE_SHIFT
] +
788 offset_in_page(IDX
[k
]);
790 if (memcmp(q
, template[i
].result
+ temp
,
791 template[i
].tap
[k
])) {
792 printk(KERN_ERR
"alg: skcipher: Chunk "
793 "test %d failed on %s at page "
794 "%u for %s\n", j
, e
, k
, algo
);
795 hexdump(q
, template[i
].tap
[k
]);
799 q
+= template[i
].tap
[k
];
800 for (n
= 0; offset_in_page(q
+ n
) && q
[n
]; n
++)
803 printk(KERN_ERR
"alg: skcipher: "
804 "Result buffer corruption in "
805 "chunk test %d on %s at page "
806 "%u for %s: %u bytes:\n", j
, e
,
811 temp
+= template[i
].tap
[k
];
819 ablkcipher_request_free(req
);
823 static int test_comp(struct crypto_comp
*tfm
, struct comp_testvec
*ctemplate
,
824 struct comp_testvec
*dtemplate
, int ctcount
, int dtcount
)
826 const char *algo
= crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm
));
828 char result
[COMP_BUF_SIZE
];
831 for (i
= 0; i
< ctcount
; i
++) {
832 int ilen
, dlen
= COMP_BUF_SIZE
;
834 memset(result
, 0, sizeof (result
));
836 ilen
= ctemplate
[i
].inlen
;
837 ret
= crypto_comp_compress(tfm
, ctemplate
[i
].input
,
838 ilen
, result
, &dlen
);
840 printk(KERN_ERR
"alg: comp: compression failed "
841 "on test %d for %s: ret=%d\n", i
+ 1, algo
,
846 if (memcmp(result
, ctemplate
[i
].output
, dlen
)) {
847 printk(KERN_ERR
"alg: comp: Compression test %d "
848 "failed for %s\n", i
+ 1, algo
);
849 hexdump(result
, dlen
);
855 for (i
= 0; i
< dtcount
; i
++) {
856 int ilen
, ret
, dlen
= COMP_BUF_SIZE
;
858 memset(result
, 0, sizeof (result
));
860 ilen
= dtemplate
[i
].inlen
;
861 ret
= crypto_comp_decompress(tfm
, dtemplate
[i
].input
,
862 ilen
, result
, &dlen
);
864 printk(KERN_ERR
"alg: comp: decompression failed "
865 "on test %d for %s: ret=%d\n", i
+ 1, algo
,
870 if (memcmp(result
, dtemplate
[i
].output
, dlen
)) {
871 printk(KERN_ERR
"alg: comp: Decompression test %d "
872 "failed for %s\n", i
+ 1, algo
);
873 hexdump(result
, dlen
);
885 static int alg_test_aead(const struct alg_test_desc
*desc
, const char *driver
,
888 struct crypto_aead
*tfm
;
891 tfm
= crypto_alloc_aead(driver
, type
, mask
);
893 printk(KERN_ERR
"alg: aead: Failed to load transform for %s: "
894 "%ld\n", driver
, PTR_ERR(tfm
));
898 if (desc
->suite
.aead
.enc
.vecs
) {
899 err
= test_aead(tfm
, ENCRYPT
, desc
->suite
.aead
.enc
.vecs
,
900 desc
->suite
.aead
.enc
.count
);
905 if (!err
&& desc
->suite
.aead
.dec
.vecs
)
906 err
= test_aead(tfm
, DECRYPT
, desc
->suite
.aead
.dec
.vecs
,
907 desc
->suite
.aead
.dec
.count
);
910 crypto_free_aead(tfm
);
914 static int alg_test_cipher(const struct alg_test_desc
*desc
,
915 const char *driver
, u32 type
, u32 mask
)
917 struct crypto_cipher
*tfm
;
920 tfm
= crypto_alloc_cipher(driver
, type
, mask
);
922 printk(KERN_ERR
"alg: cipher: Failed to load transform for "
923 "%s: %ld\n", driver
, PTR_ERR(tfm
));
927 if (desc
->suite
.cipher
.enc
.vecs
) {
928 err
= test_cipher(tfm
, ENCRYPT
, desc
->suite
.cipher
.enc
.vecs
,
929 desc
->suite
.cipher
.enc
.count
);
934 if (desc
->suite
.cipher
.dec
.vecs
)
935 err
= test_cipher(tfm
, DECRYPT
, desc
->suite
.cipher
.dec
.vecs
,
936 desc
->suite
.cipher
.dec
.count
);
939 crypto_free_cipher(tfm
);
943 static int alg_test_skcipher(const struct alg_test_desc
*desc
,
944 const char *driver
, u32 type
, u32 mask
)
946 struct crypto_ablkcipher
*tfm
;
949 tfm
= crypto_alloc_ablkcipher(driver
, type
, mask
);
951 printk(KERN_ERR
"alg: skcipher: Failed to load transform for "
952 "%s: %ld\n", driver
, PTR_ERR(tfm
));
956 if (desc
->suite
.cipher
.enc
.vecs
) {
957 err
= test_skcipher(tfm
, ENCRYPT
, desc
->suite
.cipher
.enc
.vecs
,
958 desc
->suite
.cipher
.enc
.count
);
963 if (desc
->suite
.cipher
.dec
.vecs
)
964 err
= test_skcipher(tfm
, DECRYPT
, desc
->suite
.cipher
.dec
.vecs
,
965 desc
->suite
.cipher
.dec
.count
);
968 crypto_free_ablkcipher(tfm
);
972 static int alg_test_comp(const struct alg_test_desc
*desc
, const char *driver
,
975 struct crypto_comp
*tfm
;
978 tfm
= crypto_alloc_comp(driver
, type
, mask
);
980 printk(KERN_ERR
"alg: comp: Failed to load transform for %s: "
981 "%ld\n", driver
, PTR_ERR(tfm
));
985 err
= test_comp(tfm
, desc
->suite
.comp
.comp
.vecs
,
986 desc
->suite
.comp
.decomp
.vecs
,
987 desc
->suite
.comp
.comp
.count
,
988 desc
->suite
.comp
.decomp
.count
);
990 crypto_free_comp(tfm
);
994 static int alg_test_hash(const struct alg_test_desc
*desc
, const char *driver
,
997 struct crypto_ahash
*tfm
;
1000 tfm
= crypto_alloc_ahash(driver
, type
, mask
);
1002 printk(KERN_ERR
"alg: hash: Failed to load transform for %s: "
1003 "%ld\n", driver
, PTR_ERR(tfm
));
1004 return PTR_ERR(tfm
);
1007 err
= test_hash(tfm
, desc
->suite
.hash
.vecs
, desc
->suite
.hash
.count
);
1009 crypto_free_ahash(tfm
);
1013 /* Please keep this list sorted by algorithm name. */
1014 static const struct alg_test_desc alg_test_descs
[] = {
1017 .test
= alg_test_skcipher
,
1021 .vecs
= aes_cbc_enc_tv_template
,
1022 .count
= AES_CBC_ENC_TEST_VECTORS
1025 .vecs
= aes_cbc_dec_tv_template
,
1026 .count
= AES_CBC_DEC_TEST_VECTORS
1031 .alg
= "cbc(anubis)",
1032 .test
= alg_test_skcipher
,
1036 .vecs
= anubis_cbc_enc_tv_template
,
1037 .count
= ANUBIS_CBC_ENC_TEST_VECTORS
1040 .vecs
= anubis_cbc_dec_tv_template
,
1041 .count
= ANUBIS_CBC_DEC_TEST_VECTORS
1046 .alg
= "cbc(blowfish)",
1047 .test
= alg_test_skcipher
,
1051 .vecs
= bf_cbc_enc_tv_template
,
1052 .count
= BF_CBC_ENC_TEST_VECTORS
1055 .vecs
= bf_cbc_dec_tv_template
,
1056 .count
= BF_CBC_DEC_TEST_VECTORS
1061 .alg
= "cbc(camellia)",
1062 .test
= alg_test_skcipher
,
1066 .vecs
= camellia_cbc_enc_tv_template
,
1067 .count
= CAMELLIA_CBC_ENC_TEST_VECTORS
1070 .vecs
= camellia_cbc_dec_tv_template
,
1071 .count
= CAMELLIA_CBC_DEC_TEST_VECTORS
1077 .test
= alg_test_skcipher
,
1081 .vecs
= des_cbc_enc_tv_template
,
1082 .count
= DES_CBC_ENC_TEST_VECTORS
1085 .vecs
= des_cbc_dec_tv_template
,
1086 .count
= DES_CBC_DEC_TEST_VECTORS
1091 .alg
= "cbc(des3_ede)",
1092 .test
= alg_test_skcipher
,
1096 .vecs
= des3_ede_cbc_enc_tv_template
,
1097 .count
= DES3_EDE_CBC_ENC_TEST_VECTORS
1100 .vecs
= des3_ede_cbc_dec_tv_template
,
1101 .count
= DES3_EDE_CBC_DEC_TEST_VECTORS
1106 .alg
= "cbc(twofish)",
1107 .test
= alg_test_skcipher
,
1111 .vecs
= tf_cbc_enc_tv_template
,
1112 .count
= TF_CBC_ENC_TEST_VECTORS
1115 .vecs
= tf_cbc_dec_tv_template
,
1116 .count
= TF_CBC_DEC_TEST_VECTORS
1122 .test
= alg_test_aead
,
1126 .vecs
= aes_ccm_enc_tv_template
,
1127 .count
= AES_CCM_ENC_TEST_VECTORS
1130 .vecs
= aes_ccm_dec_tv_template
,
1131 .count
= AES_CCM_DEC_TEST_VECTORS
1137 .test
= alg_test_hash
,
1140 .vecs
= crc32c_tv_template
,
1141 .count
= CRC32C_TEST_VECTORS
1145 .alg
= "cts(cbc(aes))",
1146 .test
= alg_test_skcipher
,
1150 .vecs
= cts_mode_enc_tv_template
,
1151 .count
= CTS_MODE_ENC_TEST_VECTORS
1154 .vecs
= cts_mode_dec_tv_template
,
1155 .count
= CTS_MODE_DEC_TEST_VECTORS
1161 .test
= alg_test_comp
,
1165 .vecs
= deflate_comp_tv_template
,
1166 .count
= DEFLATE_COMP_TEST_VECTORS
1169 .vecs
= deflate_decomp_tv_template
,
1170 .count
= DEFLATE_DECOMP_TEST_VECTORS
1176 .test
= alg_test_skcipher
,
1180 .vecs
= aes_enc_tv_template
,
1181 .count
= AES_ENC_TEST_VECTORS
1184 .vecs
= aes_dec_tv_template
,
1185 .count
= AES_DEC_TEST_VECTORS
1190 .alg
= "ecb(anubis)",
1191 .test
= alg_test_skcipher
,
1195 .vecs
= anubis_enc_tv_template
,
1196 .count
= ANUBIS_ENC_TEST_VECTORS
1199 .vecs
= anubis_dec_tv_template
,
1200 .count
= ANUBIS_DEC_TEST_VECTORS
1206 .test
= alg_test_skcipher
,
1210 .vecs
= arc4_enc_tv_template
,
1211 .count
= ARC4_ENC_TEST_VECTORS
1214 .vecs
= arc4_dec_tv_template
,
1215 .count
= ARC4_DEC_TEST_VECTORS
1220 .alg
= "ecb(blowfish)",
1221 .test
= alg_test_skcipher
,
1225 .vecs
= bf_enc_tv_template
,
1226 .count
= BF_ENC_TEST_VECTORS
1229 .vecs
= bf_dec_tv_template
,
1230 .count
= BF_DEC_TEST_VECTORS
1235 .alg
= "ecb(camellia)",
1236 .test
= alg_test_skcipher
,
1240 .vecs
= camellia_enc_tv_template
,
1241 .count
= CAMELLIA_ENC_TEST_VECTORS
1244 .vecs
= camellia_dec_tv_template
,
1245 .count
= CAMELLIA_DEC_TEST_VECTORS
1250 .alg
= "ecb(cast5)",
1251 .test
= alg_test_skcipher
,
1255 .vecs
= cast5_enc_tv_template
,
1256 .count
= CAST5_ENC_TEST_VECTORS
1259 .vecs
= cast5_dec_tv_template
,
1260 .count
= CAST5_DEC_TEST_VECTORS
1265 .alg
= "ecb(cast6)",
1266 .test
= alg_test_skcipher
,
1270 .vecs
= cast6_enc_tv_template
,
1271 .count
= CAST6_ENC_TEST_VECTORS
1274 .vecs
= cast6_dec_tv_template
,
1275 .count
= CAST6_DEC_TEST_VECTORS
1281 .test
= alg_test_skcipher
,
1285 .vecs
= des_enc_tv_template
,
1286 .count
= DES_ENC_TEST_VECTORS
1289 .vecs
= des_dec_tv_template
,
1290 .count
= DES_DEC_TEST_VECTORS
1295 .alg
= "ecb(des3_ede)",
1296 .test
= alg_test_skcipher
,
1300 .vecs
= des3_ede_enc_tv_template
,
1301 .count
= DES3_EDE_ENC_TEST_VECTORS
1304 .vecs
= des3_ede_dec_tv_template
,
1305 .count
= DES3_EDE_DEC_TEST_VECTORS
1310 .alg
= "ecb(khazad)",
1311 .test
= alg_test_skcipher
,
1315 .vecs
= khazad_enc_tv_template
,
1316 .count
= KHAZAD_ENC_TEST_VECTORS
1319 .vecs
= khazad_dec_tv_template
,
1320 .count
= KHAZAD_DEC_TEST_VECTORS
1326 .test
= alg_test_skcipher
,
1330 .vecs
= seed_enc_tv_template
,
1331 .count
= SEED_ENC_TEST_VECTORS
1334 .vecs
= seed_dec_tv_template
,
1335 .count
= SEED_DEC_TEST_VECTORS
1340 .alg
= "ecb(serpent)",
1341 .test
= alg_test_skcipher
,
1345 .vecs
= serpent_enc_tv_template
,
1346 .count
= SERPENT_ENC_TEST_VECTORS
1349 .vecs
= serpent_dec_tv_template
,
1350 .count
= SERPENT_DEC_TEST_VECTORS
1356 .test
= alg_test_skcipher
,
1360 .vecs
= tea_enc_tv_template
,
1361 .count
= TEA_ENC_TEST_VECTORS
1364 .vecs
= tea_dec_tv_template
,
1365 .count
= TEA_DEC_TEST_VECTORS
1370 .alg
= "ecb(tnepres)",
1371 .test
= alg_test_skcipher
,
1375 .vecs
= tnepres_enc_tv_template
,
1376 .count
= TNEPRES_ENC_TEST_VECTORS
1379 .vecs
= tnepres_dec_tv_template
,
1380 .count
= TNEPRES_DEC_TEST_VECTORS
1385 .alg
= "ecb(twofish)",
1386 .test
= alg_test_skcipher
,
1390 .vecs
= tf_enc_tv_template
,
1391 .count
= TF_ENC_TEST_VECTORS
1394 .vecs
= tf_dec_tv_template
,
1395 .count
= TF_DEC_TEST_VECTORS
1401 .test
= alg_test_skcipher
,
1405 .vecs
= xeta_enc_tv_template
,
1406 .count
= XETA_ENC_TEST_VECTORS
1409 .vecs
= xeta_dec_tv_template
,
1410 .count
= XETA_DEC_TEST_VECTORS
1416 .test
= alg_test_skcipher
,
1420 .vecs
= xtea_enc_tv_template
,
1421 .count
= XTEA_ENC_TEST_VECTORS
1424 .vecs
= xtea_dec_tv_template
,
1425 .count
= XTEA_DEC_TEST_VECTORS
1431 .test
= alg_test_aead
,
1435 .vecs
= aes_gcm_enc_tv_template
,
1436 .count
= AES_GCM_ENC_TEST_VECTORS
1439 .vecs
= aes_gcm_dec_tv_template
,
1440 .count
= AES_GCM_DEC_TEST_VECTORS
1446 .test
= alg_test_hash
,
1449 .vecs
= hmac_md5_tv_template
,
1450 .count
= HMAC_MD5_TEST_VECTORS
1454 .alg
= "hmac(rmd128)",
1455 .test
= alg_test_hash
,
1458 .vecs
= hmac_rmd128_tv_template
,
1459 .count
= HMAC_RMD128_TEST_VECTORS
1463 .alg
= "hmac(rmd160)",
1464 .test
= alg_test_hash
,
1467 .vecs
= hmac_rmd160_tv_template
,
1468 .count
= HMAC_RMD160_TEST_VECTORS
1472 .alg
= "hmac(sha1)",
1473 .test
= alg_test_hash
,
1476 .vecs
= hmac_sha1_tv_template
,
1477 .count
= HMAC_SHA1_TEST_VECTORS
1481 .alg
= "hmac(sha224)",
1482 .test
= alg_test_hash
,
1485 .vecs
= hmac_sha224_tv_template
,
1486 .count
= HMAC_SHA224_TEST_VECTORS
1490 .alg
= "hmac(sha256)",
1491 .test
= alg_test_hash
,
1494 .vecs
= hmac_sha256_tv_template
,
1495 .count
= HMAC_SHA256_TEST_VECTORS
1499 .alg
= "hmac(sha384)",
1500 .test
= alg_test_hash
,
1503 .vecs
= hmac_sha384_tv_template
,
1504 .count
= HMAC_SHA384_TEST_VECTORS
1508 .alg
= "hmac(sha512)",
1509 .test
= alg_test_hash
,
1512 .vecs
= hmac_sha512_tv_template
,
1513 .count
= HMAC_SHA512_TEST_VECTORS
1518 .test
= alg_test_skcipher
,
1522 .vecs
= aes_lrw_enc_tv_template
,
1523 .count
= AES_LRW_ENC_TEST_VECTORS
1526 .vecs
= aes_lrw_dec_tv_template
,
1527 .count
= AES_LRW_DEC_TEST_VECTORS
1533 .test
= alg_test_comp
,
1537 .vecs
= lzo_comp_tv_template
,
1538 .count
= LZO_COMP_TEST_VECTORS
1541 .vecs
= lzo_decomp_tv_template
,
1542 .count
= LZO_DECOMP_TEST_VECTORS
1548 .test
= alg_test_hash
,
1551 .vecs
= md4_tv_template
,
1552 .count
= MD4_TEST_VECTORS
1557 .test
= alg_test_hash
,
1560 .vecs
= md5_tv_template
,
1561 .count
= MD5_TEST_VECTORS
1565 .alg
= "michael_mic",
1566 .test
= alg_test_hash
,
1569 .vecs
= michael_mic_tv_template
,
1570 .count
= MICHAEL_MIC_TEST_VECTORS
1574 .alg
= "pcbc(fcrypt)",
1575 .test
= alg_test_skcipher
,
1579 .vecs
= fcrypt_pcbc_enc_tv_template
,
1580 .count
= FCRYPT_ENC_TEST_VECTORS
1583 .vecs
= fcrypt_pcbc_dec_tv_template
,
1584 .count
= FCRYPT_DEC_TEST_VECTORS
1589 .alg
= "rfc3686(ctr(aes))",
1590 .test
= alg_test_skcipher
,
1594 .vecs
= aes_ctr_enc_tv_template
,
1595 .count
= AES_CTR_ENC_TEST_VECTORS
1598 .vecs
= aes_ctr_dec_tv_template
,
1599 .count
= AES_CTR_DEC_TEST_VECTORS
1605 .test
= alg_test_hash
,
1608 .vecs
= rmd128_tv_template
,
1609 .count
= RMD128_TEST_VECTORS
1614 .test
= alg_test_hash
,
1617 .vecs
= rmd160_tv_template
,
1618 .count
= RMD160_TEST_VECTORS
1623 .test
= alg_test_hash
,
1626 .vecs
= rmd256_tv_template
,
1627 .count
= RMD256_TEST_VECTORS
1632 .test
= alg_test_hash
,
1635 .vecs
= rmd320_tv_template
,
1636 .count
= RMD320_TEST_VECTORS
1641 .test
= alg_test_skcipher
,
1645 .vecs
= salsa20_stream_enc_tv_template
,
1646 .count
= SALSA20_STREAM_ENC_TEST_VECTORS
1652 .test
= alg_test_hash
,
1655 .vecs
= sha1_tv_template
,
1656 .count
= SHA1_TEST_VECTORS
1661 .test
= alg_test_hash
,
1664 .vecs
= sha224_tv_template
,
1665 .count
= SHA224_TEST_VECTORS
1670 .test
= alg_test_hash
,
1673 .vecs
= sha256_tv_template
,
1674 .count
= SHA256_TEST_VECTORS
1679 .test
= alg_test_hash
,
1682 .vecs
= sha384_tv_template
,
1683 .count
= SHA384_TEST_VECTORS
1688 .test
= alg_test_hash
,
1691 .vecs
= sha512_tv_template
,
1692 .count
= SHA512_TEST_VECTORS
1697 .test
= alg_test_hash
,
1700 .vecs
= tgr128_tv_template
,
1701 .count
= TGR128_TEST_VECTORS
1706 .test
= alg_test_hash
,
1709 .vecs
= tgr160_tv_template
,
1710 .count
= TGR160_TEST_VECTORS
1715 .test
= alg_test_hash
,
1718 .vecs
= tgr192_tv_template
,
1719 .count
= TGR192_TEST_VECTORS
1724 .test
= alg_test_hash
,
1727 .vecs
= wp256_tv_template
,
1728 .count
= WP256_TEST_VECTORS
1733 .test
= alg_test_hash
,
1736 .vecs
= wp384_tv_template
,
1737 .count
= WP384_TEST_VECTORS
1742 .test
= alg_test_hash
,
1745 .vecs
= wp512_tv_template
,
1746 .count
= WP512_TEST_VECTORS
1751 .test
= alg_test_hash
,
1754 .vecs
= aes_xcbc128_tv_template
,
1755 .count
= XCBC_AES_TEST_VECTORS
1760 .test
= alg_test_skcipher
,
1764 .vecs
= aes_xts_enc_tv_template
,
1765 .count
= AES_XTS_ENC_TEST_VECTORS
1768 .vecs
= aes_xts_dec_tv_template
,
1769 .count
= AES_XTS_DEC_TEST_VECTORS
1776 static int alg_find_test(const char *alg
)
1779 int end
= ARRAY_SIZE(alg_test_descs
);
1781 while (start
< end
) {
1782 int i
= (start
+ end
) / 2;
1783 int diff
= strcmp(alg_test_descs
[i
].alg
, alg
);
1801 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
)
1805 if ((type
& CRYPTO_ALG_TYPE_MASK
) == CRYPTO_ALG_TYPE_CIPHER
) {
1806 char nalg
[CRYPTO_MAX_ALG_NAME
];
1808 if (snprintf(nalg
, sizeof(nalg
), "ecb(%s)", alg
) >=
1810 return -ENAMETOOLONG
;
1812 i
= alg_find_test(nalg
);
1816 return alg_test_cipher(alg_test_descs
+ i
, driver
, type
, mask
);
1819 i
= alg_find_test(alg
);
1823 return alg_test_descs
[i
].test(alg_test_descs
+ i
, driver
,
1827 printk(KERN_INFO
"alg: No test for %s (%s)\n", alg
, driver
);
1830 EXPORT_SYMBOL_GPL(alg_test
);
1832 int __init
testmgr_init(void)
1836 for (i
= 0; i
< XBUFSIZE
; i
++) {
1837 xbuf
[i
] = (void *)__get_free_page(GFP_KERNEL
);
1842 for (i
= 0; i
< XBUFSIZE
; i
++) {
1843 axbuf
[i
] = (void *)__get_free_page(GFP_KERNEL
);
1845 goto err_free_axbuf
;
1851 for (i
= 0; i
< XBUFSIZE
&& axbuf
[i
]; i
++)
1852 free_page((unsigned long)axbuf
[i
]);
1854 for (i
= 0; i
< XBUFSIZE
&& xbuf
[i
]; i
++)
1855 free_page((unsigned long)xbuf
[i
]);
1860 void testmgr_exit(void)
1864 for (i
= 0; i
< XBUFSIZE
; i
++)
1865 free_page((unsigned long)axbuf
[i
]);
1866 for (i
= 0; i
< XBUFSIZE
; i
++)
1867 free_page((unsigned long)xbuf
[i
]);