1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Algorithm testing framework and tests. 4 * 5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 6 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org> 7 * Copyright (c) 2007 Nokia Siemens Networks 8 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> 9 * Copyright (c) 2019 Google LLC 10 * 11 * Updated RFC4106 AES-GCM testing. 12 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com) 13 * Adrian Hoban <adrian.hoban@intel.com> 14 * Gabriele Paoloni <gabriele.paoloni@intel.com> 15 * Tadeusz Struk (tadeusz.struk@intel.com) 16 * Copyright (c) 2010, Intel Corporation. 17 */ 18 19 #include <crypto/aead.h> 20 #include <crypto/hash.h> 21 #include <crypto/skcipher.h> 22 #include <linux/err.h> 23 #include <linux/fips.h> 24 #include <linux/module.h> 25 #include <linux/once.h> 26 #include <linux/prandom.h> 27 #include <linux/scatterlist.h> 28 #include <linux/slab.h> 29 #include <linux/string.h> 30 #include <linux/uio.h> 31 #include <crypto/rng.h> 32 #include <crypto/drbg.h> 33 #include <crypto/akcipher.h> 34 #include <crypto/kpp.h> 35 #include <crypto/acompress.h> 36 #include <crypto/sig.h> 37 #include <crypto/internal/cipher.h> 38 #include <crypto/internal/simd.h> 39 40 #include "internal.h" 41 42 MODULE_IMPORT_NS("CRYPTO_INTERNAL"); 43 44 static bool notests; 45 module_param(notests, bool, 0644); 46 MODULE_PARM_DESC(notests, "disable all crypto self-tests"); 47 48 #ifdef CONFIG_CRYPTO_SELFTESTS_FULL 49 static bool noslowtests; 50 module_param(noslowtests, bool, 0644); 51 MODULE_PARM_DESC(noslowtests, "disable slow crypto self-tests"); 52 53 static unsigned int fuzz_iterations = 100; 54 module_param(fuzz_iterations, uint, 0644); 55 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations"); 56 #else 57 #define noslowtests 1 58 #define fuzz_iterations 0 59 #endif 60 61 #ifndef CONFIG_CRYPTO_SELFTESTS 62 63 /* a perfect nop */ 64 int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 65 { 66 return 0; 67 } 68 69 #else 70 71 #include "testmgr.h" 72 73 /* 74 * Need slab memory for testing (size in number of pages). 75 */ 76 #define XBUFSIZE 8 77 78 /* 79 * Used by test_cipher() 80 */ 81 #define ENCRYPT 1 82 #define DECRYPT 0 83 84 struct aead_test_suite { 85 const struct aead_testvec *vecs; 86 unsigned int count; 87 88 /* 89 * Set if trying to decrypt an inauthentic ciphertext with this 90 * algorithm might result in EINVAL rather than EBADMSG, due to other 91 * validation the algorithm does on the inputs such as length checks. 92 */ 93 unsigned int einval_allowed : 1; 94 95 /* 96 * Set if this algorithm requires that the IV be located at the end of 97 * the AAD buffer, in addition to being given in the normal way. The 98 * behavior when the two IV copies differ is implementation-defined. 99 */ 100 unsigned int aad_iv : 1; 101 }; 102 103 struct cipher_test_suite { 104 const struct cipher_testvec *vecs; 105 unsigned int count; 106 }; 107 108 struct comp_test_suite { 109 struct { 110 const struct comp_testvec *vecs; 111 unsigned int count; 112 } comp, decomp; 113 }; 114 115 struct hash_test_suite { 116 const struct hash_testvec *vecs; 117 unsigned int count; 118 }; 119 120 struct cprng_test_suite { 121 const struct cprng_testvec *vecs; 122 unsigned int count; 123 }; 124 125 struct drbg_test_suite { 126 const struct drbg_testvec *vecs; 127 unsigned int count; 128 }; 129 130 struct akcipher_test_suite { 131 const struct akcipher_testvec *vecs; 132 unsigned int count; 133 }; 134 135 struct sig_test_suite { 136 const struct sig_testvec *vecs; 137 unsigned int count; 138 }; 139 140 struct kpp_test_suite { 141 const struct kpp_testvec *vecs; 142 unsigned int count; 143 }; 144 145 struct alg_test_desc { 146 const char *alg; 147 const char *generic_driver; 148 int (*test)(const struct alg_test_desc *desc, const char *driver, 149 u32 type, u32 mask); 150 int fips_allowed; /* set if alg is allowed in fips mode */ 151 152 union { 153 struct aead_test_suite aead; 154 struct cipher_test_suite cipher; 155 struct comp_test_suite comp; 156 struct hash_test_suite hash; 157 struct cprng_test_suite cprng; 158 struct drbg_test_suite drbg; 159 struct akcipher_test_suite akcipher; 160 struct sig_test_suite sig; 161 struct kpp_test_suite kpp; 162 } suite; 163 }; 164 165 static void hexdump(unsigned char *buf, unsigned int len) 166 { 167 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET, 168 16, 1, 169 buf, len, false); 170 } 171 172 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order) 173 { 174 int i; 175 176 for (i = 0; i < XBUFSIZE; i++) { 177 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order); 178 if (!buf[i]) 179 goto err_free_buf; 180 } 181 182 return 0; 183 184 err_free_buf: 185 while (i-- > 0) 186 free_pages((unsigned long)buf[i], order); 187 188 return -ENOMEM; 189 } 190 191 static int testmgr_alloc_buf(char *buf[XBUFSIZE]) 192 { 193 return __testmgr_alloc_buf(buf, 0); 194 } 195 196 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order) 197 { 198 int i; 199 200 for (i = 0; i < XBUFSIZE; i++) 201 free_pages((unsigned long)buf[i], order); 202 } 203 204 static void testmgr_free_buf(char *buf[XBUFSIZE]) 205 { 206 __testmgr_free_buf(buf, 0); 207 } 208 209 #define TESTMGR_POISON_BYTE 0xfe 210 #define TESTMGR_POISON_LEN 16 211 212 static inline void testmgr_poison(void *addr, size_t len) 213 { 214 memset(addr, TESTMGR_POISON_BYTE, len); 215 } 216 217 /* Is the memory region still fully poisoned? */ 218 static inline bool testmgr_is_poison(const void *addr, size_t len) 219 { 220 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL; 221 } 222 223 /* flush type for hash algorithms */ 224 enum flush_type { 225 /* merge with update of previous buffer(s) */ 226 FLUSH_TYPE_NONE = 0, 227 228 /* update with previous buffer(s) before doing this one */ 229 FLUSH_TYPE_FLUSH, 230 231 /* likewise, but also export and re-import the intermediate state */ 232 FLUSH_TYPE_REIMPORT, 233 }; 234 235 /* finalization function for hash algorithms */ 236 enum finalization_type { 237 FINALIZATION_TYPE_FINAL, /* use final() */ 238 FINALIZATION_TYPE_FINUP, /* use finup() */ 239 FINALIZATION_TYPE_DIGEST, /* use digest() */ 240 }; 241 242 /* 243 * Whether the crypto operation will occur in-place, and if so whether the 244 * source and destination scatterlist pointers will coincide (req->src == 245 * req->dst), or whether they'll merely point to two separate scatterlists 246 * (req->src != req->dst) that reference the same underlying memory. 247 * 248 * This is only relevant for algorithm types that support in-place operation. 249 */ 250 enum inplace_mode { 251 OUT_OF_PLACE, 252 INPLACE_ONE_SGLIST, 253 INPLACE_TWO_SGLISTS, 254 }; 255 256 #define TEST_SG_TOTAL 10000 257 258 /** 259 * struct test_sg_division - description of a scatterlist entry 260 * 261 * This struct describes one entry of a scatterlist being constructed to check a 262 * crypto test vector. 263 * 264 * @proportion_of_total: length of this chunk relative to the total length, 265 * given as a proportion out of TEST_SG_TOTAL so that it 266 * scales to fit any test vector 267 * @offset: byte offset into a 2-page buffer at which this chunk will start 268 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the 269 * @offset 270 * @flush_type: for hashes, whether an update() should be done now vs. 271 * continuing to accumulate data 272 * @nosimd: if doing the pending update(), do it with SIMD disabled? 273 */ 274 struct test_sg_division { 275 unsigned int proportion_of_total; 276 unsigned int offset; 277 bool offset_relative_to_alignmask; 278 enum flush_type flush_type; 279 bool nosimd; 280 }; 281 282 /** 283 * struct testvec_config - configuration for testing a crypto test vector 284 * 285 * This struct describes the data layout and other parameters with which each 286 * crypto test vector can be tested. 287 * 288 * @name: name of this config, logged for debugging purposes if a test fails 289 * @inplace_mode: whether and how to operate on the data in-place, if applicable 290 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP 291 * @src_divs: description of how to arrange the source scatterlist 292 * @dst_divs: description of how to arrange the dst scatterlist, if applicable 293 * for the algorithm type. Defaults to @src_divs if unset. 294 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1], 295 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary 296 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to 297 * the @iv_offset 298 * @key_offset: misalignment of the key, where 0 is default alignment 299 * @key_offset_relative_to_alignmask: if true, add the algorithm's alignmask to 300 * the @key_offset 301 * @finalization_type: what finalization function to use for hashes 302 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP. 303 * This applies to the parts of the operation that aren't controlled 304 * individually by @nosimd_setkey or @src_divs[].nosimd. 305 * @nosimd_setkey: set the key (if applicable) with SIMD disabled? Requires 306 * !CRYPTO_TFM_REQ_MAY_SLEEP. 307 */ 308 struct testvec_config { 309 const char *name; 310 enum inplace_mode inplace_mode; 311 u32 req_flags; 312 struct test_sg_division src_divs[XBUFSIZE]; 313 struct test_sg_division dst_divs[XBUFSIZE]; 314 unsigned int iv_offset; 315 unsigned int key_offset; 316 bool iv_offset_relative_to_alignmask; 317 bool key_offset_relative_to_alignmask; 318 enum finalization_type finalization_type; 319 bool nosimd; 320 bool nosimd_setkey; 321 }; 322 323 #define TESTVEC_CONFIG_NAMELEN 192 324 325 /* 326 * The following are the lists of testvec_configs to test for each algorithm 327 * type when the "fast" crypto self-tests are enabled. They aim to provide good 328 * test coverage, while keeping the test time much shorter than the "full" tests 329 * so that the "fast" tests can be enabled in a wider range of circumstances. 330 */ 331 332 /* Configs for skciphers and aeads */ 333 static const struct testvec_config default_cipher_testvec_configs[] = { 334 { 335 .name = "in-place (one sglist)", 336 .inplace_mode = INPLACE_ONE_SGLIST, 337 .src_divs = { { .proportion_of_total = 10000 } }, 338 }, { 339 .name = "in-place (two sglists)", 340 .inplace_mode = INPLACE_TWO_SGLISTS, 341 .src_divs = { { .proportion_of_total = 10000 } }, 342 }, { 343 .name = "out-of-place", 344 .inplace_mode = OUT_OF_PLACE, 345 .src_divs = { { .proportion_of_total = 10000 } }, 346 }, { 347 .name = "unaligned buffer, offset=1", 348 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } }, 349 .iv_offset = 1, 350 .key_offset = 1, 351 }, { 352 .name = "buffer aligned only to alignmask", 353 .src_divs = { 354 { 355 .proportion_of_total = 10000, 356 .offset = 1, 357 .offset_relative_to_alignmask = true, 358 }, 359 }, 360 .iv_offset = 1, 361 .iv_offset_relative_to_alignmask = true, 362 .key_offset = 1, 363 .key_offset_relative_to_alignmask = true, 364 }, { 365 .name = "two even aligned splits", 366 .src_divs = { 367 { .proportion_of_total = 5000 }, 368 { .proportion_of_total = 5000 }, 369 }, 370 }, { 371 .name = "one src, two even splits dst", 372 .inplace_mode = OUT_OF_PLACE, 373 .src_divs = { { .proportion_of_total = 10000 } }, 374 .dst_divs = { 375 { .proportion_of_total = 5000 }, 376 { .proportion_of_total = 5000 }, 377 }, 378 }, { 379 .name = "uneven misaligned splits, may sleep", 380 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP, 381 .src_divs = { 382 { .proportion_of_total = 1900, .offset = 33 }, 383 { .proportion_of_total = 3300, .offset = 7 }, 384 { .proportion_of_total = 4800, .offset = 18 }, 385 }, 386 .iv_offset = 3, 387 .key_offset = 3, 388 }, { 389 .name = "misaligned splits crossing pages, inplace", 390 .inplace_mode = INPLACE_ONE_SGLIST, 391 .src_divs = { 392 { 393 .proportion_of_total = 7500, 394 .offset = PAGE_SIZE - 32 395 }, { 396 .proportion_of_total = 2500, 397 .offset = PAGE_SIZE - 7 398 }, 399 }, 400 } 401 }; 402 403 static const struct testvec_config default_hash_testvec_configs[] = { 404 { 405 .name = "init+update+final aligned buffer", 406 .src_divs = { { .proportion_of_total = 10000 } }, 407 .finalization_type = FINALIZATION_TYPE_FINAL, 408 }, { 409 .name = "init+finup aligned buffer", 410 .src_divs = { { .proportion_of_total = 10000 } }, 411 .finalization_type = FINALIZATION_TYPE_FINUP, 412 }, { 413 .name = "digest aligned buffer", 414 .src_divs = { { .proportion_of_total = 10000 } }, 415 .finalization_type = FINALIZATION_TYPE_DIGEST, 416 }, { 417 .name = "init+update+final misaligned buffer", 418 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } }, 419 .finalization_type = FINALIZATION_TYPE_FINAL, 420 .key_offset = 1, 421 }, { 422 .name = "digest misaligned buffer", 423 .src_divs = { 424 { 425 .proportion_of_total = 10000, 426 .offset = 1, 427 }, 428 }, 429 .finalization_type = FINALIZATION_TYPE_DIGEST, 430 .key_offset = 1, 431 }, { 432 .name = "init+update+update+final two even splits", 433 .src_divs = { 434 { .proportion_of_total = 5000 }, 435 { 436 .proportion_of_total = 5000, 437 .flush_type = FLUSH_TYPE_FLUSH, 438 }, 439 }, 440 .finalization_type = FINALIZATION_TYPE_FINAL, 441 }, { 442 .name = "digest uneven misaligned splits, may sleep", 443 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP, 444 .src_divs = { 445 { .proportion_of_total = 1900, .offset = 33 }, 446 { .proportion_of_total = 3300, .offset = 7 }, 447 { .proportion_of_total = 4800, .offset = 18 }, 448 }, 449 .finalization_type = FINALIZATION_TYPE_DIGEST, 450 }, { 451 .name = "digest misaligned splits crossing pages", 452 .src_divs = { 453 { 454 .proportion_of_total = 7500, 455 .offset = PAGE_SIZE - 32, 456 }, { 457 .proportion_of_total = 2500, 458 .offset = PAGE_SIZE - 7, 459 }, 460 }, 461 .finalization_type = FINALIZATION_TYPE_DIGEST, 462 }, { 463 .name = "import/export", 464 .src_divs = { 465 { 466 .proportion_of_total = 6500, 467 .flush_type = FLUSH_TYPE_REIMPORT, 468 }, { 469 .proportion_of_total = 3500, 470 .flush_type = FLUSH_TYPE_REIMPORT, 471 }, 472 }, 473 .finalization_type = FINALIZATION_TYPE_FINAL, 474 } 475 }; 476 477 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs) 478 { 479 unsigned int remaining = TEST_SG_TOTAL; 480 unsigned int ndivs = 0; 481 482 do { 483 remaining -= divs[ndivs++].proportion_of_total; 484 } while (remaining); 485 486 return ndivs; 487 } 488 489 #define SGDIVS_HAVE_FLUSHES BIT(0) 490 #define SGDIVS_HAVE_NOSIMD BIT(1) 491 492 static bool valid_sg_divisions(const struct test_sg_division *divs, 493 unsigned int count, int *flags_ret) 494 { 495 unsigned int total = 0; 496 unsigned int i; 497 498 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) { 499 if (divs[i].proportion_of_total <= 0 || 500 divs[i].proportion_of_total > TEST_SG_TOTAL - total) 501 return false; 502 total += divs[i].proportion_of_total; 503 if (divs[i].flush_type != FLUSH_TYPE_NONE) 504 *flags_ret |= SGDIVS_HAVE_FLUSHES; 505 if (divs[i].nosimd) 506 *flags_ret |= SGDIVS_HAVE_NOSIMD; 507 } 508 return total == TEST_SG_TOTAL && 509 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL; 510 } 511 512 /* 513 * Check whether the given testvec_config is valid. This isn't strictly needed 514 * since every testvec_config should be valid, but check anyway so that people 515 * don't unknowingly add broken configs that don't do what they wanted. 516 */ 517 static bool valid_testvec_config(const struct testvec_config *cfg) 518 { 519 int flags = 0; 520 521 if (cfg->name == NULL) 522 return false; 523 524 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs), 525 &flags)) 526 return false; 527 528 if (cfg->dst_divs[0].proportion_of_total) { 529 if (!valid_sg_divisions(cfg->dst_divs, 530 ARRAY_SIZE(cfg->dst_divs), &flags)) 531 return false; 532 } else { 533 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs))) 534 return false; 535 /* defaults to dst_divs=src_divs */ 536 } 537 538 if (cfg->iv_offset + 539 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) > 540 MAX_ALGAPI_ALIGNMASK + 1) 541 return false; 542 543 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) && 544 cfg->finalization_type == FINALIZATION_TYPE_DIGEST) 545 return false; 546 547 if ((cfg->nosimd || cfg->nosimd_setkey || 548 (flags & SGDIVS_HAVE_NOSIMD)) && 549 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP)) 550 return false; 551 552 return true; 553 } 554 555 struct test_sglist { 556 char *bufs[XBUFSIZE]; 557 struct scatterlist sgl[XBUFSIZE]; 558 struct scatterlist sgl_saved[XBUFSIZE]; 559 struct scatterlist *sgl_ptr; 560 unsigned int nents; 561 }; 562 563 static int init_test_sglist(struct test_sglist *tsgl) 564 { 565 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */); 566 } 567 568 static void destroy_test_sglist(struct test_sglist *tsgl) 569 { 570 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */); 571 } 572 573 /** 574 * build_test_sglist() - build a scatterlist for a crypto test 575 * 576 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page 577 * buffers which the scatterlist @tsgl->sgl[] will be made to point into. 578 * @divs: the layout specification on which the scatterlist will be based 579 * @alignmask: the algorithm's alignmask 580 * @total_len: the total length of the scatterlist to build in bytes 581 * @data: if non-NULL, the buffers will be filled with this data until it ends. 582 * Otherwise the buffers will be poisoned. In both cases, some bytes 583 * past the end of each buffer will be poisoned to help detect overruns. 584 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry 585 * corresponds will be returned here. This will match @divs except 586 * that divisions resolving to a length of 0 are omitted as they are 587 * not included in the scatterlist. 588 * 589 * Return: 0 or a -errno value 590 */ 591 static int build_test_sglist(struct test_sglist *tsgl, 592 const struct test_sg_division *divs, 593 const unsigned int alignmask, 594 const unsigned int total_len, 595 struct iov_iter *data, 596 const struct test_sg_division *out_divs[XBUFSIZE]) 597 { 598 struct { 599 const struct test_sg_division *div; 600 size_t length; 601 } partitions[XBUFSIZE]; 602 const unsigned int ndivs = count_test_sg_divisions(divs); 603 unsigned int len_remaining = total_len; 604 unsigned int i; 605 606 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl)); 607 if (WARN_ON(ndivs > ARRAY_SIZE(partitions))) 608 return -EINVAL; 609 610 /* Calculate the (div, length) pairs */ 611 tsgl->nents = 0; 612 for (i = 0; i < ndivs; i++) { 613 unsigned int len_this_sg = 614 min(len_remaining, 615 (total_len * divs[i].proportion_of_total + 616 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL); 617 618 if (len_this_sg != 0) { 619 partitions[tsgl->nents].div = &divs[i]; 620 partitions[tsgl->nents].length = len_this_sg; 621 tsgl->nents++; 622 len_remaining -= len_this_sg; 623 } 624 } 625 if (tsgl->nents == 0) { 626 partitions[tsgl->nents].div = &divs[0]; 627 partitions[tsgl->nents].length = 0; 628 tsgl->nents++; 629 } 630 partitions[tsgl->nents - 1].length += len_remaining; 631 632 /* Set up the sgl entries and fill the data or poison */ 633 sg_init_table(tsgl->sgl, tsgl->nents); 634 for (i = 0; i < tsgl->nents; i++) { 635 unsigned int offset = partitions[i].div->offset; 636 void *addr; 637 638 if (partitions[i].div->offset_relative_to_alignmask) 639 offset += alignmask; 640 641 while (offset + partitions[i].length + TESTMGR_POISON_LEN > 642 2 * PAGE_SIZE) { 643 if (WARN_ON(offset <= 0)) 644 return -EINVAL; 645 offset /= 2; 646 } 647 648 addr = &tsgl->bufs[i][offset]; 649 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length); 650 651 if (out_divs) 652 out_divs[i] = partitions[i].div; 653 654 if (data) { 655 size_t copy_len, copied; 656 657 copy_len = min(partitions[i].length, data->count); 658 copied = copy_from_iter(addr, copy_len, data); 659 if (WARN_ON(copied != copy_len)) 660 return -EINVAL; 661 testmgr_poison(addr + copy_len, partitions[i].length + 662 TESTMGR_POISON_LEN - copy_len); 663 } else { 664 testmgr_poison(addr, partitions[i].length + 665 TESTMGR_POISON_LEN); 666 } 667 } 668 669 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]); 670 tsgl->sgl_ptr = tsgl->sgl; 671 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0])); 672 return 0; 673 } 674 675 /* 676 * Verify that a scatterlist crypto operation produced the correct output. 677 * 678 * @tsgl: scatterlist containing the actual output 679 * @expected_output: buffer containing the expected output 680 * @len_to_check: length of @expected_output in bytes 681 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result 682 * @check_poison: verify that the poison bytes after each chunk are intact? 683 * 684 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun. 685 */ 686 static int verify_correct_output(const struct test_sglist *tsgl, 687 const char *expected_output, 688 unsigned int len_to_check, 689 unsigned int unchecked_prefix_len, 690 bool check_poison) 691 { 692 unsigned int i; 693 694 for (i = 0; i < tsgl->nents; i++) { 695 struct scatterlist *sg = &tsgl->sgl_ptr[i]; 696 unsigned int len = sg->length; 697 unsigned int offset = sg->offset; 698 const char *actual_output; 699 700 if (unchecked_prefix_len) { 701 if (unchecked_prefix_len >= len) { 702 unchecked_prefix_len -= len; 703 continue; 704 } 705 offset += unchecked_prefix_len; 706 len -= unchecked_prefix_len; 707 unchecked_prefix_len = 0; 708 } 709 len = min(len, len_to_check); 710 actual_output = page_address(sg_page(sg)) + offset; 711 if (memcmp(expected_output, actual_output, len) != 0) 712 return -EINVAL; 713 if (check_poison && 714 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN)) 715 return -EOVERFLOW; 716 len_to_check -= len; 717 expected_output += len; 718 } 719 if (WARN_ON(len_to_check != 0)) 720 return -EINVAL; 721 return 0; 722 } 723 724 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl) 725 { 726 unsigned int i; 727 728 for (i = 0; i < tsgl->nents; i++) { 729 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link) 730 return true; 731 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset) 732 return true; 733 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length) 734 return true; 735 } 736 return false; 737 } 738 739 struct cipher_test_sglists { 740 struct test_sglist src; 741 struct test_sglist dst; 742 }; 743 744 static struct cipher_test_sglists *alloc_cipher_test_sglists(void) 745 { 746 struct cipher_test_sglists *tsgls; 747 748 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL); 749 if (!tsgls) 750 return NULL; 751 752 if (init_test_sglist(&tsgls->src) != 0) 753 goto fail_kfree; 754 if (init_test_sglist(&tsgls->dst) != 0) 755 goto fail_destroy_src; 756 757 return tsgls; 758 759 fail_destroy_src: 760 destroy_test_sglist(&tsgls->src); 761 fail_kfree: 762 kfree(tsgls); 763 return NULL; 764 } 765 766 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls) 767 { 768 if (tsgls) { 769 destroy_test_sglist(&tsgls->src); 770 destroy_test_sglist(&tsgls->dst); 771 kfree(tsgls); 772 } 773 } 774 775 /* Build the src and dst scatterlists for an skcipher or AEAD test */ 776 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls, 777 const struct testvec_config *cfg, 778 unsigned int alignmask, 779 unsigned int src_total_len, 780 unsigned int dst_total_len, 781 const struct kvec *inputs, 782 unsigned int nr_inputs) 783 { 784 struct iov_iter input; 785 int err; 786 787 iov_iter_kvec(&input, ITER_SOURCE, inputs, nr_inputs, src_total_len); 788 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask, 789 cfg->inplace_mode != OUT_OF_PLACE ? 790 max(dst_total_len, src_total_len) : 791 src_total_len, 792 &input, NULL); 793 if (err) 794 return err; 795 796 /* 797 * In-place crypto operations can use the same scatterlist for both the 798 * source and destination (req->src == req->dst), or can use separate 799 * scatterlists (req->src != req->dst) which point to the same 800 * underlying memory. Make sure to test both cases. 801 */ 802 if (cfg->inplace_mode == INPLACE_ONE_SGLIST) { 803 tsgls->dst.sgl_ptr = tsgls->src.sgl; 804 tsgls->dst.nents = tsgls->src.nents; 805 return 0; 806 } 807 if (cfg->inplace_mode == INPLACE_TWO_SGLISTS) { 808 /* 809 * For now we keep it simple and only test the case where the 810 * two scatterlists have identical entries, rather than 811 * different entries that split up the same memory differently. 812 */ 813 memcpy(tsgls->dst.sgl, tsgls->src.sgl, 814 tsgls->src.nents * sizeof(tsgls->src.sgl[0])); 815 memcpy(tsgls->dst.sgl_saved, tsgls->src.sgl, 816 tsgls->src.nents * sizeof(tsgls->src.sgl[0])); 817 tsgls->dst.sgl_ptr = tsgls->dst.sgl; 818 tsgls->dst.nents = tsgls->src.nents; 819 return 0; 820 } 821 /* Out of place */ 822 return build_test_sglist(&tsgls->dst, 823 cfg->dst_divs[0].proportion_of_total ? 824 cfg->dst_divs : cfg->src_divs, 825 alignmask, dst_total_len, NULL, NULL); 826 } 827 828 /* 829 * Support for testing passing a misaligned key to setkey(): 830 * 831 * If cfg->key_offset is set, copy the key into a new buffer at that offset, 832 * optionally adding alignmask. Else, just use the key directly. 833 */ 834 static int prepare_keybuf(const u8 *key, unsigned int ksize, 835 const struct testvec_config *cfg, 836 unsigned int alignmask, 837 const u8 **keybuf_ret, const u8 **keyptr_ret) 838 { 839 unsigned int key_offset = cfg->key_offset; 840 u8 *keybuf = NULL, *keyptr = (u8 *)key; 841 842 if (key_offset != 0) { 843 if (cfg->key_offset_relative_to_alignmask) 844 key_offset += alignmask; 845 keybuf = kmalloc(key_offset + ksize, GFP_KERNEL); 846 if (!keybuf) 847 return -ENOMEM; 848 keyptr = keybuf + key_offset; 849 memcpy(keyptr, key, ksize); 850 } 851 *keybuf_ret = keybuf; 852 *keyptr_ret = keyptr; 853 return 0; 854 } 855 856 /* 857 * Like setkey_f(tfm, key, ksize), but sometimes misalign the key. 858 * In addition, run the setkey function in no-SIMD context if requested. 859 */ 860 #define do_setkey(setkey_f, tfm, key, ksize, cfg, alignmask) \ 861 ({ \ 862 const u8 *keybuf, *keyptr; \ 863 int err; \ 864 \ 865 err = prepare_keybuf((key), (ksize), (cfg), (alignmask), \ 866 &keybuf, &keyptr); \ 867 if (err == 0) { \ 868 if ((cfg)->nosimd_setkey) \ 869 crypto_disable_simd_for_test(); \ 870 err = setkey_f((tfm), keyptr, (ksize)); \ 871 if ((cfg)->nosimd_setkey) \ 872 crypto_reenable_simd_for_test(); \ 873 kfree(keybuf); \ 874 } \ 875 err; \ 876 }) 877 878 /* 879 * The fuzz tests use prandom instead of the normal Linux RNG since they don't 880 * need cryptographically secure random numbers. This greatly improves the 881 * performance of these tests, especially if they are run before the Linux RNG 882 * has been initialized or if they are run on a lockdep-enabled kernel. 883 */ 884 885 static inline void init_rnd_state(struct rnd_state *rng) 886 { 887 prandom_seed_state(rng, get_random_u64()); 888 } 889 890 static inline u8 prandom_u8(struct rnd_state *rng) 891 { 892 return prandom_u32_state(rng); 893 } 894 895 static inline u32 prandom_u32_below(struct rnd_state *rng, u32 ceil) 896 { 897 /* 898 * This is slightly biased for non-power-of-2 values of 'ceil', but this 899 * isn't important here. 900 */ 901 return prandom_u32_state(rng) % ceil; 902 } 903 904 static inline bool prandom_bool(struct rnd_state *rng) 905 { 906 return prandom_u32_below(rng, 2); 907 } 908 909 static inline u32 prandom_u32_inclusive(struct rnd_state *rng, 910 u32 floor, u32 ceil) 911 { 912 return floor + prandom_u32_below(rng, ceil - floor + 1); 913 } 914 915 /* Generate a random length in range [0, max_len], but prefer smaller values */ 916 static unsigned int generate_random_length(struct rnd_state *rng, 917 unsigned int max_len) 918 { 919 unsigned int len = prandom_u32_below(rng, max_len + 1); 920 921 switch (prandom_u32_below(rng, 4)) { 922 case 0: 923 len %= 64; 924 break; 925 case 1: 926 len %= 256; 927 break; 928 case 2: 929 len %= 1024; 930 break; 931 default: 932 break; 933 } 934 if (len && prandom_u32_below(rng, 4) == 0) 935 len = rounddown_pow_of_two(len); 936 return len; 937 } 938 939 /* Flip a random bit in the given nonempty data buffer */ 940 static void flip_random_bit(struct rnd_state *rng, u8 *buf, size_t size) 941 { 942 size_t bitpos; 943 944 bitpos = prandom_u32_below(rng, size * 8); 945 buf[bitpos / 8] ^= 1 << (bitpos % 8); 946 } 947 948 /* Flip a random byte in the given nonempty data buffer */ 949 static void flip_random_byte(struct rnd_state *rng, u8 *buf, size_t size) 950 { 951 buf[prandom_u32_below(rng, size)] ^= 0xff; 952 } 953 954 /* Sometimes make some random changes to the given nonempty data buffer */ 955 static void mutate_buffer(struct rnd_state *rng, u8 *buf, size_t size) 956 { 957 size_t num_flips; 958 size_t i; 959 960 /* Sometimes flip some bits */ 961 if (prandom_u32_below(rng, 4) == 0) { 962 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8), 963 size * 8); 964 for (i = 0; i < num_flips; i++) 965 flip_random_bit(rng, buf, size); 966 } 967 968 /* Sometimes flip some bytes */ 969 if (prandom_u32_below(rng, 4) == 0) { 970 num_flips = min_t(size_t, 1 << prandom_u32_below(rng, 8), size); 971 for (i = 0; i < num_flips; i++) 972 flip_random_byte(rng, buf, size); 973 } 974 } 975 976 /* Randomly generate 'count' bytes, but sometimes make them "interesting" */ 977 static void generate_random_bytes(struct rnd_state *rng, u8 *buf, size_t count) 978 { 979 u8 b; 980 u8 increment; 981 size_t i; 982 983 if (count == 0) 984 return; 985 986 switch (prandom_u32_below(rng, 8)) { /* Choose a generation strategy */ 987 case 0: 988 case 1: 989 /* All the same byte, plus optional mutations */ 990 switch (prandom_u32_below(rng, 4)) { 991 case 0: 992 b = 0x00; 993 break; 994 case 1: 995 b = 0xff; 996 break; 997 default: 998 b = prandom_u8(rng); 999 break; 1000 } 1001 memset(buf, b, count); 1002 mutate_buffer(rng, buf, count); 1003 break; 1004 case 2: 1005 /* Ascending or descending bytes, plus optional mutations */ 1006 increment = prandom_u8(rng); 1007 b = prandom_u8(rng); 1008 for (i = 0; i < count; i++, b += increment) 1009 buf[i] = b; 1010 mutate_buffer(rng, buf, count); 1011 break; 1012 default: 1013 /* Fully random bytes */ 1014 prandom_bytes_state(rng, buf, count); 1015 } 1016 } 1017 1018 static char *generate_random_sgl_divisions(struct rnd_state *rng, 1019 struct test_sg_division *divs, 1020 size_t max_divs, char *p, char *end, 1021 bool gen_flushes, u32 req_flags) 1022 { 1023 struct test_sg_division *div = divs; 1024 unsigned int remaining = TEST_SG_TOTAL; 1025 1026 do { 1027 unsigned int this_len; 1028 const char *flushtype_str; 1029 1030 if (div == &divs[max_divs - 1] || prandom_bool(rng)) 1031 this_len = remaining; 1032 else if (prandom_u32_below(rng, 4) == 0) 1033 this_len = (remaining + 1) / 2; 1034 else 1035 this_len = prandom_u32_inclusive(rng, 1, remaining); 1036 div->proportion_of_total = this_len; 1037 1038 if (prandom_u32_below(rng, 4) == 0) 1039 div->offset = prandom_u32_inclusive(rng, 1040 PAGE_SIZE - 128, 1041 PAGE_SIZE - 1); 1042 else if (prandom_bool(rng)) 1043 div->offset = prandom_u32_below(rng, 32); 1044 else 1045 div->offset = prandom_u32_below(rng, PAGE_SIZE); 1046 if (prandom_u32_below(rng, 8) == 0) 1047 div->offset_relative_to_alignmask = true; 1048 1049 div->flush_type = FLUSH_TYPE_NONE; 1050 if (gen_flushes) { 1051 switch (prandom_u32_below(rng, 4)) { 1052 case 0: 1053 div->flush_type = FLUSH_TYPE_REIMPORT; 1054 break; 1055 case 1: 1056 div->flush_type = FLUSH_TYPE_FLUSH; 1057 break; 1058 } 1059 } 1060 1061 if (div->flush_type != FLUSH_TYPE_NONE && 1062 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && 1063 prandom_bool(rng)) 1064 div->nosimd = true; 1065 1066 switch (div->flush_type) { 1067 case FLUSH_TYPE_FLUSH: 1068 if (div->nosimd) 1069 flushtype_str = "<flush,nosimd>"; 1070 else 1071 flushtype_str = "<flush>"; 1072 break; 1073 case FLUSH_TYPE_REIMPORT: 1074 if (div->nosimd) 1075 flushtype_str = "<reimport,nosimd>"; 1076 else 1077 flushtype_str = "<reimport>"; 1078 break; 1079 default: 1080 flushtype_str = ""; 1081 break; 1082 } 1083 1084 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */ 1085 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str, 1086 this_len / 100, this_len % 100, 1087 div->offset_relative_to_alignmask ? 1088 "alignmask" : "", 1089 div->offset, this_len == remaining ? "" : ", "); 1090 remaining -= this_len; 1091 div++; 1092 } while (remaining); 1093 1094 return p; 1095 } 1096 1097 /* Generate a random testvec_config for fuzz testing */ 1098 static void generate_random_testvec_config(struct rnd_state *rng, 1099 struct testvec_config *cfg, 1100 char *name, size_t max_namelen) 1101 { 1102 char *p = name; 1103 char * const end = name + max_namelen; 1104 1105 memset(cfg, 0, sizeof(*cfg)); 1106 1107 cfg->name = name; 1108 1109 p += scnprintf(p, end - p, "random:"); 1110 1111 switch (prandom_u32_below(rng, 4)) { 1112 case 0: 1113 case 1: 1114 cfg->inplace_mode = OUT_OF_PLACE; 1115 break; 1116 case 2: 1117 cfg->inplace_mode = INPLACE_ONE_SGLIST; 1118 p += scnprintf(p, end - p, " inplace_one_sglist"); 1119 break; 1120 default: 1121 cfg->inplace_mode = INPLACE_TWO_SGLISTS; 1122 p += scnprintf(p, end - p, " inplace_two_sglists"); 1123 break; 1124 } 1125 1126 if (prandom_bool(rng)) { 1127 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP; 1128 p += scnprintf(p, end - p, " may_sleep"); 1129 } 1130 1131 switch (prandom_u32_below(rng, 4)) { 1132 case 0: 1133 cfg->finalization_type = FINALIZATION_TYPE_FINAL; 1134 p += scnprintf(p, end - p, " use_final"); 1135 break; 1136 case 1: 1137 cfg->finalization_type = FINALIZATION_TYPE_FINUP; 1138 p += scnprintf(p, end - p, " use_finup"); 1139 break; 1140 default: 1141 cfg->finalization_type = FINALIZATION_TYPE_DIGEST; 1142 p += scnprintf(p, end - p, " use_digest"); 1143 break; 1144 } 1145 1146 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP)) { 1147 if (prandom_bool(rng)) { 1148 cfg->nosimd = true; 1149 p += scnprintf(p, end - p, " nosimd"); 1150 } 1151 if (prandom_bool(rng)) { 1152 cfg->nosimd_setkey = true; 1153 p += scnprintf(p, end - p, " nosimd_setkey"); 1154 } 1155 } 1156 1157 p += scnprintf(p, end - p, " src_divs=["); 1158 p = generate_random_sgl_divisions(rng, cfg->src_divs, 1159 ARRAY_SIZE(cfg->src_divs), p, end, 1160 (cfg->finalization_type != 1161 FINALIZATION_TYPE_DIGEST), 1162 cfg->req_flags); 1163 p += scnprintf(p, end - p, "]"); 1164 1165 if (cfg->inplace_mode == OUT_OF_PLACE && prandom_bool(rng)) { 1166 p += scnprintf(p, end - p, " dst_divs=["); 1167 p = generate_random_sgl_divisions(rng, cfg->dst_divs, 1168 ARRAY_SIZE(cfg->dst_divs), 1169 p, end, false, 1170 cfg->req_flags); 1171 p += scnprintf(p, end - p, "]"); 1172 } 1173 1174 if (prandom_bool(rng)) { 1175 cfg->iv_offset = prandom_u32_inclusive(rng, 1, 1176 MAX_ALGAPI_ALIGNMASK); 1177 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset); 1178 } 1179 1180 if (prandom_bool(rng)) { 1181 cfg->key_offset = prandom_u32_inclusive(rng, 1, 1182 MAX_ALGAPI_ALIGNMASK); 1183 p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset); 1184 } 1185 1186 WARN_ON_ONCE(!valid_testvec_config(cfg)); 1187 } 1188 1189 static void crypto_disable_simd_for_test(void) 1190 { 1191 #ifdef CONFIG_CRYPTO_SELFTESTS_FULL 1192 migrate_disable(); 1193 __this_cpu_write(crypto_simd_disabled_for_test, true); 1194 #endif 1195 } 1196 1197 static void crypto_reenable_simd_for_test(void) 1198 { 1199 #ifdef CONFIG_CRYPTO_SELFTESTS_FULL 1200 __this_cpu_write(crypto_simd_disabled_for_test, false); 1201 migrate_enable(); 1202 #endif 1203 } 1204 1205 /* 1206 * Given an algorithm name, build the name of the generic implementation of that 1207 * algorithm, assuming the usual naming convention. Specifically, this appends 1208 * "-generic" to every part of the name that is not a template name. Examples: 1209 * 1210 * aes => aes-generic 1211 * cbc(aes) => cbc(aes-generic) 1212 * cts(cbc(aes)) => cts(cbc(aes-generic)) 1213 * rfc7539(chacha20,poly1305) => rfc7539(chacha20-generic,poly1305-generic) 1214 * 1215 * Return: 0 on success, or -ENAMETOOLONG if the generic name would be too long 1216 */ 1217 static int build_generic_driver_name(const char *algname, 1218 char driver_name[CRYPTO_MAX_ALG_NAME]) 1219 { 1220 const char *in = algname; 1221 char *out = driver_name; 1222 size_t len = strlen(algname); 1223 1224 if (len >= CRYPTO_MAX_ALG_NAME) 1225 goto too_long; 1226 do { 1227 const char *in_saved = in; 1228 1229 while (*in && *in != '(' && *in != ')' && *in != ',') 1230 *out++ = *in++; 1231 if (*in != '(' && in > in_saved) { 1232 len += 8; 1233 if (len >= CRYPTO_MAX_ALG_NAME) 1234 goto too_long; 1235 memcpy(out, "-generic", 8); 1236 out += 8; 1237 } 1238 } while ((*out++ = *in++) != '\0'); 1239 return 0; 1240 1241 too_long: 1242 pr_err("alg: generic driver name for \"%s\" would be too long\n", 1243 algname); 1244 return -ENAMETOOLONG; 1245 } 1246 1247 static int build_hash_sglist(struct test_sglist *tsgl, 1248 const struct hash_testvec *vec, 1249 const struct testvec_config *cfg, 1250 unsigned int alignmask, 1251 const struct test_sg_division *divs[XBUFSIZE]) 1252 { 1253 struct kvec kv; 1254 struct iov_iter input; 1255 1256 kv.iov_base = (void *)vec->plaintext; 1257 kv.iov_len = vec->psize; 1258 iov_iter_kvec(&input, ITER_SOURCE, &kv, 1, vec->psize); 1259 return build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize, 1260 &input, divs); 1261 } 1262 1263 static int check_hash_result(const char *type, 1264 const u8 *result, unsigned int digestsize, 1265 const struct hash_testvec *vec, 1266 const char *vec_name, 1267 const char *driver, 1268 const struct testvec_config *cfg) 1269 { 1270 if (memcmp(result, vec->digest, digestsize) != 0) { 1271 pr_err("alg: %s: %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n", 1272 type, driver, vec_name, cfg->name); 1273 return -EINVAL; 1274 } 1275 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) { 1276 pr_err("alg: %s: %s overran result buffer on test vector %s, cfg=\"%s\"\n", 1277 type, driver, vec_name, cfg->name); 1278 return -EOVERFLOW; 1279 } 1280 return 0; 1281 } 1282 1283 static inline int check_shash_op(const char *op, int err, 1284 const char *driver, const char *vec_name, 1285 const struct testvec_config *cfg) 1286 { 1287 if (err) 1288 pr_err("alg: shash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n", 1289 driver, op, err, vec_name, cfg->name); 1290 return err; 1291 } 1292 1293 /* Test one hash test vector in one configuration, using the shash API */ 1294 static int test_shash_vec_cfg(const struct hash_testvec *vec, 1295 const char *vec_name, 1296 const struct testvec_config *cfg, 1297 struct shash_desc *desc, 1298 struct test_sglist *tsgl, 1299 u8 *hashstate) 1300 { 1301 struct crypto_shash *tfm = desc->tfm; 1302 const unsigned int digestsize = crypto_shash_digestsize(tfm); 1303 const unsigned int statesize = crypto_shash_statesize(tfm); 1304 const char *driver = crypto_shash_driver_name(tfm); 1305 const struct test_sg_division *divs[XBUFSIZE]; 1306 unsigned int i; 1307 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN]; 1308 int err; 1309 1310 /* Set the key, if specified */ 1311 if (vec->ksize) { 1312 err = do_setkey(crypto_shash_setkey, tfm, vec->key, vec->ksize, 1313 cfg, 0); 1314 if (err) { 1315 if (err == vec->setkey_error) 1316 return 0; 1317 pr_err("alg: shash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n", 1318 driver, vec_name, vec->setkey_error, err, 1319 crypto_shash_get_flags(tfm)); 1320 return err; 1321 } 1322 if (vec->setkey_error) { 1323 pr_err("alg: shash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n", 1324 driver, vec_name, vec->setkey_error); 1325 return -EINVAL; 1326 } 1327 } 1328 1329 /* Build the scatterlist for the source data */ 1330 err = build_hash_sglist(tsgl, vec, cfg, 0, divs); 1331 if (err) { 1332 pr_err("alg: shash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n", 1333 driver, vec_name, cfg->name); 1334 return err; 1335 } 1336 1337 /* Do the actual hashing */ 1338 1339 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm)); 1340 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN); 1341 1342 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST || 1343 vec->digest_error) { 1344 /* Just using digest() */ 1345 if (tsgl->nents != 1) 1346 return 0; 1347 if (cfg->nosimd) 1348 crypto_disable_simd_for_test(); 1349 err = crypto_shash_digest(desc, sg_virt(&tsgl->sgl[0]), 1350 tsgl->sgl[0].length, result); 1351 if (cfg->nosimd) 1352 crypto_reenable_simd_for_test(); 1353 if (err) { 1354 if (err == vec->digest_error) 1355 return 0; 1356 pr_err("alg: shash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 1357 driver, vec_name, vec->digest_error, err, 1358 cfg->name); 1359 return err; 1360 } 1361 if (vec->digest_error) { 1362 pr_err("alg: shash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n", 1363 driver, vec_name, vec->digest_error, cfg->name); 1364 return -EINVAL; 1365 } 1366 goto result_ready; 1367 } 1368 1369 /* Using init(), zero or more update(), then final() or finup() */ 1370 1371 if (cfg->nosimd) 1372 crypto_disable_simd_for_test(); 1373 err = crypto_shash_init(desc); 1374 if (cfg->nosimd) 1375 crypto_reenable_simd_for_test(); 1376 err = check_shash_op("init", err, driver, vec_name, cfg); 1377 if (err) 1378 return err; 1379 1380 for (i = 0; i < tsgl->nents; i++) { 1381 if (i + 1 == tsgl->nents && 1382 cfg->finalization_type == FINALIZATION_TYPE_FINUP) { 1383 if (divs[i]->nosimd) 1384 crypto_disable_simd_for_test(); 1385 err = crypto_shash_finup(desc, sg_virt(&tsgl->sgl[i]), 1386 tsgl->sgl[i].length, result); 1387 if (divs[i]->nosimd) 1388 crypto_reenable_simd_for_test(); 1389 err = check_shash_op("finup", err, driver, vec_name, 1390 cfg); 1391 if (err) 1392 return err; 1393 goto result_ready; 1394 } 1395 if (divs[i]->nosimd) 1396 crypto_disable_simd_for_test(); 1397 err = crypto_shash_update(desc, sg_virt(&tsgl->sgl[i]), 1398 tsgl->sgl[i].length); 1399 if (divs[i]->nosimd) 1400 crypto_reenable_simd_for_test(); 1401 err = check_shash_op("update", err, driver, vec_name, cfg); 1402 if (err) 1403 return err; 1404 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) { 1405 /* Test ->export() and ->import() */ 1406 testmgr_poison(hashstate + statesize, 1407 TESTMGR_POISON_LEN); 1408 err = crypto_shash_export(desc, hashstate); 1409 err = check_shash_op("export", err, driver, vec_name, 1410 cfg); 1411 if (err) 1412 return err; 1413 if (!testmgr_is_poison(hashstate + statesize, 1414 TESTMGR_POISON_LEN)) { 1415 pr_err("alg: shash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n", 1416 driver, vec_name, cfg->name); 1417 return -EOVERFLOW; 1418 } 1419 testmgr_poison(desc->__ctx, crypto_shash_descsize(tfm)); 1420 err = crypto_shash_import(desc, hashstate); 1421 err = check_shash_op("import", err, driver, vec_name, 1422 cfg); 1423 if (err) 1424 return err; 1425 } 1426 } 1427 1428 if (cfg->nosimd) 1429 crypto_disable_simd_for_test(); 1430 err = crypto_shash_final(desc, result); 1431 if (cfg->nosimd) 1432 crypto_reenable_simd_for_test(); 1433 err = check_shash_op("final", err, driver, vec_name, cfg); 1434 if (err) 1435 return err; 1436 result_ready: 1437 return check_hash_result("shash", result, digestsize, vec, vec_name, 1438 driver, cfg); 1439 } 1440 1441 static int do_ahash_op(int (*op)(struct ahash_request *req), 1442 struct ahash_request *req, 1443 struct crypto_wait *wait, bool nosimd) 1444 { 1445 int err; 1446 1447 if (nosimd) 1448 crypto_disable_simd_for_test(); 1449 1450 err = op(req); 1451 1452 if (nosimd) 1453 crypto_reenable_simd_for_test(); 1454 1455 return crypto_wait_req(err, wait); 1456 } 1457 1458 static int check_nonfinal_ahash_op(const char *op, int err, 1459 u8 *result, unsigned int digestsize, 1460 const char *driver, const char *vec_name, 1461 const struct testvec_config *cfg) 1462 { 1463 if (err) { 1464 pr_err("alg: ahash: %s %s() failed with err %d on test vector %s, cfg=\"%s\"\n", 1465 driver, op, err, vec_name, cfg->name); 1466 return err; 1467 } 1468 if (!testmgr_is_poison(result, digestsize)) { 1469 pr_err("alg: ahash: %s %s() used result buffer on test vector %s, cfg=\"%s\"\n", 1470 driver, op, vec_name, cfg->name); 1471 return -EINVAL; 1472 } 1473 return 0; 1474 } 1475 1476 /* Test one hash test vector in one configuration, using the ahash API */ 1477 static int test_ahash_vec_cfg(const struct hash_testvec *vec, 1478 const char *vec_name, 1479 const struct testvec_config *cfg, 1480 struct ahash_request *req, 1481 struct test_sglist *tsgl, 1482 u8 *hashstate) 1483 { 1484 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1485 const unsigned int digestsize = crypto_ahash_digestsize(tfm); 1486 const unsigned int statesize = crypto_ahash_statesize(tfm); 1487 const char *driver = crypto_ahash_driver_name(tfm); 1488 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 1489 const struct test_sg_division *divs[XBUFSIZE]; 1490 DECLARE_CRYPTO_WAIT(wait); 1491 unsigned int i; 1492 struct scatterlist *pending_sgl; 1493 unsigned int pending_len; 1494 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN]; 1495 int err; 1496 1497 /* Set the key, if specified */ 1498 if (vec->ksize) { 1499 err = do_setkey(crypto_ahash_setkey, tfm, vec->key, vec->ksize, 1500 cfg, 0); 1501 if (err) { 1502 if (err == vec->setkey_error) 1503 return 0; 1504 pr_err("alg: ahash: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n", 1505 driver, vec_name, vec->setkey_error, err, 1506 crypto_ahash_get_flags(tfm)); 1507 return err; 1508 } 1509 if (vec->setkey_error) { 1510 pr_err("alg: ahash: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n", 1511 driver, vec_name, vec->setkey_error); 1512 return -EINVAL; 1513 } 1514 } 1515 1516 /* Build the scatterlist for the source data */ 1517 err = build_hash_sglist(tsgl, vec, cfg, 0, divs); 1518 if (err) { 1519 pr_err("alg: ahash: %s: error preparing scatterlist for test vector %s, cfg=\"%s\"\n", 1520 driver, vec_name, cfg->name); 1521 return err; 1522 } 1523 1524 /* Do the actual hashing */ 1525 1526 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm)); 1527 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN); 1528 1529 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST || 1530 vec->digest_error) { 1531 /* Just using digest() */ 1532 ahash_request_set_callback(req, req_flags, crypto_req_done, 1533 &wait); 1534 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize); 1535 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd); 1536 if (err) { 1537 if (err == vec->digest_error) 1538 return 0; 1539 pr_err("alg: ahash: %s digest() failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 1540 driver, vec_name, vec->digest_error, err, 1541 cfg->name); 1542 return err; 1543 } 1544 if (vec->digest_error) { 1545 pr_err("alg: ahash: %s digest() unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n", 1546 driver, vec_name, vec->digest_error, cfg->name); 1547 return -EINVAL; 1548 } 1549 goto result_ready; 1550 } 1551 1552 /* Using init(), zero or more update(), then final() or finup() */ 1553 1554 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait); 1555 ahash_request_set_crypt(req, NULL, result, 0); 1556 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd); 1557 err = check_nonfinal_ahash_op("init", err, result, digestsize, 1558 driver, vec_name, cfg); 1559 if (err) 1560 return err; 1561 1562 pending_sgl = NULL; 1563 pending_len = 0; 1564 for (i = 0; i < tsgl->nents; i++) { 1565 if (divs[i]->flush_type != FLUSH_TYPE_NONE && 1566 pending_sgl != NULL) { 1567 /* update() with the pending data */ 1568 ahash_request_set_callback(req, req_flags, 1569 crypto_req_done, &wait); 1570 ahash_request_set_crypt(req, pending_sgl, result, 1571 pending_len); 1572 err = do_ahash_op(crypto_ahash_update, req, &wait, 1573 divs[i]->nosimd); 1574 err = check_nonfinal_ahash_op("update", err, 1575 result, digestsize, 1576 driver, vec_name, cfg); 1577 if (err) 1578 return err; 1579 pending_sgl = NULL; 1580 pending_len = 0; 1581 } 1582 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) { 1583 /* Test ->export() and ->import() */ 1584 testmgr_poison(hashstate + statesize, 1585 TESTMGR_POISON_LEN); 1586 err = crypto_ahash_export(req, hashstate); 1587 err = check_nonfinal_ahash_op("export", err, 1588 result, digestsize, 1589 driver, vec_name, cfg); 1590 if (err) 1591 return err; 1592 if (!testmgr_is_poison(hashstate + statesize, 1593 TESTMGR_POISON_LEN)) { 1594 pr_err("alg: ahash: %s export() overran state buffer on test vector %s, cfg=\"%s\"\n", 1595 driver, vec_name, cfg->name); 1596 return -EOVERFLOW; 1597 } 1598 1599 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm)); 1600 err = crypto_ahash_import(req, hashstate); 1601 err = check_nonfinal_ahash_op("import", err, 1602 result, digestsize, 1603 driver, vec_name, cfg); 1604 if (err) 1605 return err; 1606 } 1607 if (pending_sgl == NULL) 1608 pending_sgl = &tsgl->sgl[i]; 1609 pending_len += tsgl->sgl[i].length; 1610 } 1611 1612 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait); 1613 ahash_request_set_crypt(req, pending_sgl, result, pending_len); 1614 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) { 1615 /* finish with update() and final() */ 1616 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd); 1617 err = check_nonfinal_ahash_op("update", err, result, digestsize, 1618 driver, vec_name, cfg); 1619 if (err) 1620 return err; 1621 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd); 1622 if (err) { 1623 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n", 1624 driver, err, vec_name, cfg->name); 1625 return err; 1626 } 1627 } else { 1628 /* finish with finup() */ 1629 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd); 1630 if (err) { 1631 pr_err("alg: ahash: %s finup() failed with err %d on test vector %s, cfg=\"%s\"\n", 1632 driver, err, vec_name, cfg->name); 1633 return err; 1634 } 1635 } 1636 1637 result_ready: 1638 return check_hash_result("ahash", result, digestsize, vec, vec_name, 1639 driver, cfg); 1640 } 1641 1642 static int test_hash_vec_cfg(const struct hash_testvec *vec, 1643 const char *vec_name, 1644 const struct testvec_config *cfg, 1645 struct ahash_request *req, 1646 struct shash_desc *desc, 1647 struct test_sglist *tsgl, 1648 u8 *hashstate) 1649 { 1650 int err; 1651 1652 /* 1653 * For algorithms implemented as "shash", most bugs will be detected by 1654 * both the shash and ahash tests. Test the shash API first so that the 1655 * failures involve less indirection, so are easier to debug. 1656 */ 1657 1658 if (desc) { 1659 err = test_shash_vec_cfg(vec, vec_name, cfg, desc, tsgl, 1660 hashstate); 1661 if (err) 1662 return err; 1663 } 1664 1665 return test_ahash_vec_cfg(vec, vec_name, cfg, req, tsgl, hashstate); 1666 } 1667 1668 static int test_hash_vec(const struct hash_testvec *vec, unsigned int vec_num, 1669 struct ahash_request *req, struct shash_desc *desc, 1670 struct test_sglist *tsgl, u8 *hashstate) 1671 { 1672 char vec_name[16]; 1673 unsigned int i; 1674 int err; 1675 1676 sprintf(vec_name, "%u", vec_num); 1677 1678 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) { 1679 err = test_hash_vec_cfg(vec, vec_name, 1680 &default_hash_testvec_configs[i], 1681 req, desc, tsgl, hashstate); 1682 if (err) 1683 return err; 1684 } 1685 1686 if (!noslowtests) { 1687 struct rnd_state rng; 1688 struct testvec_config cfg; 1689 char cfgname[TESTVEC_CONFIG_NAMELEN]; 1690 1691 init_rnd_state(&rng); 1692 1693 for (i = 0; i < fuzz_iterations; i++) { 1694 generate_random_testvec_config(&rng, &cfg, cfgname, 1695 sizeof(cfgname)); 1696 err = test_hash_vec_cfg(vec, vec_name, &cfg, 1697 req, desc, tsgl, hashstate); 1698 if (err) 1699 return err; 1700 cond_resched(); 1701 } 1702 } 1703 return 0; 1704 } 1705 1706 /* 1707 * Generate a hash test vector from the given implementation. 1708 * Assumes the buffers in 'vec' were already allocated. 1709 */ 1710 static void generate_random_hash_testvec(struct rnd_state *rng, 1711 struct ahash_request *req, 1712 struct hash_testvec *vec, 1713 unsigned int maxkeysize, 1714 unsigned int maxdatasize, 1715 char *name, size_t max_namelen) 1716 { 1717 /* Data */ 1718 vec->psize = generate_random_length(rng, maxdatasize); 1719 generate_random_bytes(rng, (u8 *)vec->plaintext, vec->psize); 1720 1721 /* 1722 * Key: length in range [1, maxkeysize], but usually choose maxkeysize. 1723 * If algorithm is unkeyed, then maxkeysize == 0 and set ksize = 0. 1724 */ 1725 vec->setkey_error = 0; 1726 vec->ksize = 0; 1727 if (maxkeysize) { 1728 vec->ksize = maxkeysize; 1729 if (prandom_u32_below(rng, 4) == 0) 1730 vec->ksize = prandom_u32_inclusive(rng, 1, maxkeysize); 1731 generate_random_bytes(rng, (u8 *)vec->key, vec->ksize); 1732 1733 vec->setkey_error = crypto_ahash_setkey( 1734 crypto_ahash_reqtfm(req), vec->key, vec->ksize); 1735 /* If the key couldn't be set, no need to continue to digest. */ 1736 if (vec->setkey_error) 1737 goto done; 1738 } 1739 1740 /* Digest */ 1741 vec->digest_error = crypto_hash_digest( 1742 crypto_ahash_reqtfm(req), vec->plaintext, 1743 vec->psize, (u8 *)vec->digest); 1744 done: 1745 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"", 1746 vec->psize, vec->ksize); 1747 } 1748 1749 /* 1750 * Test the hash algorithm represented by @req against the corresponding generic 1751 * implementation, if one is available. 1752 */ 1753 static int test_hash_vs_generic_impl(const char *generic_driver, 1754 unsigned int maxkeysize, 1755 struct ahash_request *req, 1756 struct shash_desc *desc, 1757 struct test_sglist *tsgl, 1758 u8 *hashstate) 1759 { 1760 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1761 const unsigned int digestsize = crypto_ahash_digestsize(tfm); 1762 const unsigned int blocksize = crypto_ahash_blocksize(tfm); 1763 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN; 1764 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name; 1765 const char *driver = crypto_ahash_driver_name(tfm); 1766 struct rnd_state rng; 1767 char _generic_driver[CRYPTO_MAX_ALG_NAME]; 1768 struct ahash_request *generic_req = NULL; 1769 struct crypto_ahash *generic_tfm = NULL; 1770 unsigned int i; 1771 struct hash_testvec vec = { 0 }; 1772 char vec_name[64]; 1773 struct testvec_config *cfg; 1774 char cfgname[TESTVEC_CONFIG_NAMELEN]; 1775 int err; 1776 1777 if (noslowtests) 1778 return 0; 1779 1780 init_rnd_state(&rng); 1781 1782 if (!generic_driver) { /* Use default naming convention? */ 1783 err = build_generic_driver_name(algname, _generic_driver); 1784 if (err) 1785 return err; 1786 generic_driver = _generic_driver; 1787 } 1788 1789 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */ 1790 return 0; 1791 1792 generic_tfm = crypto_alloc_ahash(generic_driver, 0, 0); 1793 if (IS_ERR(generic_tfm)) { 1794 err = PTR_ERR(generic_tfm); 1795 if (err == -ENOENT) { 1796 pr_warn("alg: hash: skipping comparison tests for %s because %s is unavailable\n", 1797 driver, generic_driver); 1798 return 0; 1799 } 1800 pr_err("alg: hash: error allocating %s (generic impl of %s): %d\n", 1801 generic_driver, algname, err); 1802 return err; 1803 } 1804 1805 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 1806 if (!cfg) { 1807 err = -ENOMEM; 1808 goto out; 1809 } 1810 1811 generic_req = ahash_request_alloc(generic_tfm, GFP_KERNEL); 1812 if (!generic_req) { 1813 err = -ENOMEM; 1814 goto out; 1815 } 1816 1817 /* Check the algorithm properties for consistency. */ 1818 1819 if (digestsize != crypto_ahash_digestsize(generic_tfm)) { 1820 pr_err("alg: hash: digestsize for %s (%u) doesn't match generic impl (%u)\n", 1821 driver, digestsize, 1822 crypto_ahash_digestsize(generic_tfm)); 1823 err = -EINVAL; 1824 goto out; 1825 } 1826 1827 if (blocksize != crypto_ahash_blocksize(generic_tfm)) { 1828 pr_err("alg: hash: blocksize for %s (%u) doesn't match generic impl (%u)\n", 1829 driver, blocksize, crypto_ahash_blocksize(generic_tfm)); 1830 err = -EINVAL; 1831 goto out; 1832 } 1833 1834 /* 1835 * Now generate test vectors using the generic implementation, and test 1836 * the other implementation against them. 1837 */ 1838 1839 vec.key = kmalloc(maxkeysize, GFP_KERNEL); 1840 vec.plaintext = kmalloc(maxdatasize, GFP_KERNEL); 1841 vec.digest = kmalloc(digestsize, GFP_KERNEL); 1842 if (!vec.key || !vec.plaintext || !vec.digest) { 1843 err = -ENOMEM; 1844 goto out; 1845 } 1846 1847 for (i = 0; i < fuzz_iterations * 8; i++) { 1848 generate_random_hash_testvec(&rng, generic_req, &vec, 1849 maxkeysize, maxdatasize, 1850 vec_name, sizeof(vec_name)); 1851 generate_random_testvec_config(&rng, cfg, cfgname, 1852 sizeof(cfgname)); 1853 1854 err = test_hash_vec_cfg(&vec, vec_name, cfg, 1855 req, desc, tsgl, hashstate); 1856 if (err) 1857 goto out; 1858 cond_resched(); 1859 } 1860 err = 0; 1861 out: 1862 kfree(cfg); 1863 kfree(vec.key); 1864 kfree(vec.plaintext); 1865 kfree(vec.digest); 1866 ahash_request_free(generic_req); 1867 crypto_free_ahash(generic_tfm); 1868 return err; 1869 } 1870 1871 static int alloc_shash(const char *driver, u32 type, u32 mask, 1872 struct crypto_shash **tfm_ret, 1873 struct shash_desc **desc_ret) 1874 { 1875 struct crypto_shash *tfm; 1876 struct shash_desc *desc; 1877 1878 tfm = crypto_alloc_shash(driver, type, mask); 1879 if (IS_ERR(tfm)) { 1880 if (PTR_ERR(tfm) == -ENOENT || PTR_ERR(tfm) == -EEXIST) { 1881 /* 1882 * This algorithm is only available through the ahash 1883 * API, not the shash API, so skip the shash tests. 1884 */ 1885 return 0; 1886 } 1887 pr_err("alg: hash: failed to allocate shash transform for %s: %ld\n", 1888 driver, PTR_ERR(tfm)); 1889 return PTR_ERR(tfm); 1890 } 1891 1892 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL); 1893 if (!desc) { 1894 crypto_free_shash(tfm); 1895 return -ENOMEM; 1896 } 1897 desc->tfm = tfm; 1898 1899 *tfm_ret = tfm; 1900 *desc_ret = desc; 1901 return 0; 1902 } 1903 1904 static int __alg_test_hash(const struct hash_testvec *vecs, 1905 unsigned int num_vecs, const char *driver, 1906 u32 type, u32 mask, 1907 const char *generic_driver, unsigned int maxkeysize) 1908 { 1909 struct crypto_ahash *atfm = NULL; 1910 struct ahash_request *req = NULL; 1911 struct crypto_shash *stfm = NULL; 1912 struct shash_desc *desc = NULL; 1913 struct test_sglist *tsgl = NULL; 1914 u8 *hashstate = NULL; 1915 unsigned int statesize; 1916 unsigned int i; 1917 int err; 1918 1919 /* 1920 * Always test the ahash API. This works regardless of whether the 1921 * algorithm is implemented as ahash or shash. 1922 */ 1923 1924 atfm = crypto_alloc_ahash(driver, type, mask); 1925 if (IS_ERR(atfm)) { 1926 if (PTR_ERR(atfm) == -ENOENT) 1927 return 0; 1928 pr_err("alg: hash: failed to allocate transform for %s: %ld\n", 1929 driver, PTR_ERR(atfm)); 1930 return PTR_ERR(atfm); 1931 } 1932 driver = crypto_ahash_driver_name(atfm); 1933 1934 req = ahash_request_alloc(atfm, GFP_KERNEL); 1935 if (!req) { 1936 pr_err("alg: hash: failed to allocate request for %s\n", 1937 driver); 1938 err = -ENOMEM; 1939 goto out; 1940 } 1941 1942 /* 1943 * If available also test the shash API, to cover corner cases that may 1944 * be missed by testing the ahash API only. 1945 */ 1946 err = alloc_shash(driver, type, mask, &stfm, &desc); 1947 if (err) 1948 goto out; 1949 1950 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL); 1951 if (!tsgl || init_test_sglist(tsgl) != 0) { 1952 pr_err("alg: hash: failed to allocate test buffers for %s\n", 1953 driver); 1954 kfree(tsgl); 1955 tsgl = NULL; 1956 err = -ENOMEM; 1957 goto out; 1958 } 1959 1960 statesize = crypto_ahash_statesize(atfm); 1961 if (stfm) 1962 statesize = max(statesize, crypto_shash_statesize(stfm)); 1963 hashstate = kmalloc(statesize + TESTMGR_POISON_LEN, GFP_KERNEL); 1964 if (!hashstate) { 1965 pr_err("alg: hash: failed to allocate hash state buffer for %s\n", 1966 driver); 1967 err = -ENOMEM; 1968 goto out; 1969 } 1970 1971 for (i = 0; i < num_vecs; i++) { 1972 if (fips_enabled && vecs[i].fips_skip) 1973 continue; 1974 1975 err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate); 1976 if (err) 1977 goto out; 1978 cond_resched(); 1979 } 1980 err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req, 1981 desc, tsgl, hashstate); 1982 out: 1983 kfree(hashstate); 1984 if (tsgl) { 1985 destroy_test_sglist(tsgl); 1986 kfree(tsgl); 1987 } 1988 kfree(desc); 1989 crypto_free_shash(stfm); 1990 ahash_request_free(req); 1991 crypto_free_ahash(atfm); 1992 return err; 1993 } 1994 1995 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver, 1996 u32 type, u32 mask) 1997 { 1998 const struct hash_testvec *template = desc->suite.hash.vecs; 1999 unsigned int tcount = desc->suite.hash.count; 2000 unsigned int nr_unkeyed, nr_keyed; 2001 unsigned int maxkeysize = 0; 2002 int err; 2003 2004 /* 2005 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests 2006 * first, before setting a key on the tfm. To make this easier, we 2007 * require that the unkeyed test vectors (if any) are listed first. 2008 */ 2009 2010 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) { 2011 if (template[nr_unkeyed].ksize) 2012 break; 2013 } 2014 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) { 2015 if (!template[nr_unkeyed + nr_keyed].ksize) { 2016 pr_err("alg: hash: test vectors for %s out of order, " 2017 "unkeyed ones must come first\n", desc->alg); 2018 return -EINVAL; 2019 } 2020 maxkeysize = max_t(unsigned int, maxkeysize, 2021 template[nr_unkeyed + nr_keyed].ksize); 2022 } 2023 2024 err = 0; 2025 if (nr_unkeyed) { 2026 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask, 2027 desc->generic_driver, maxkeysize); 2028 template += nr_unkeyed; 2029 } 2030 2031 if (!err && nr_keyed) 2032 err = __alg_test_hash(template, nr_keyed, driver, type, mask, 2033 desc->generic_driver, maxkeysize); 2034 2035 return err; 2036 } 2037 2038 static int test_aead_vec_cfg(int enc, const struct aead_testvec *vec, 2039 const char *vec_name, 2040 const struct testvec_config *cfg, 2041 struct aead_request *req, 2042 struct cipher_test_sglists *tsgls) 2043 { 2044 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2045 const unsigned int alignmask = crypto_aead_alignmask(tfm); 2046 const unsigned int ivsize = crypto_aead_ivsize(tfm); 2047 const unsigned int authsize = vec->clen - vec->plen; 2048 const char *driver = crypto_aead_driver_name(tfm); 2049 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 2050 const char *op = enc ? "encryption" : "decryption"; 2051 DECLARE_CRYPTO_WAIT(wait); 2052 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN]; 2053 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) + 2054 cfg->iv_offset + 2055 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0); 2056 struct kvec input[2]; 2057 int err; 2058 2059 /* Set the key */ 2060 if (vec->wk) 2061 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 2062 else 2063 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 2064 2065 err = do_setkey(crypto_aead_setkey, tfm, vec->key, vec->klen, 2066 cfg, alignmask); 2067 if (err && err != vec->setkey_error) { 2068 pr_err("alg: aead: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n", 2069 driver, vec_name, vec->setkey_error, err, 2070 crypto_aead_get_flags(tfm)); 2071 return err; 2072 } 2073 if (!err && vec->setkey_error) { 2074 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n", 2075 driver, vec_name, vec->setkey_error); 2076 return -EINVAL; 2077 } 2078 2079 /* Set the authentication tag size */ 2080 err = crypto_aead_setauthsize(tfm, authsize); 2081 if (err && err != vec->setauthsize_error) { 2082 pr_err("alg: aead: %s setauthsize failed on test vector %s; expected_error=%d, actual_error=%d\n", 2083 driver, vec_name, vec->setauthsize_error, err); 2084 return err; 2085 } 2086 if (!err && vec->setauthsize_error) { 2087 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %s; expected_error=%d\n", 2088 driver, vec_name, vec->setauthsize_error); 2089 return -EINVAL; 2090 } 2091 2092 if (vec->setkey_error || vec->setauthsize_error) 2093 return 0; 2094 2095 /* The IV must be copied to a buffer, as the algorithm may modify it */ 2096 if (WARN_ON(ivsize > MAX_IVLEN)) 2097 return -EINVAL; 2098 if (vec->iv) 2099 memcpy(iv, vec->iv, ivsize); 2100 else 2101 memset(iv, 0, ivsize); 2102 2103 /* Build the src/dst scatterlists */ 2104 input[0].iov_base = (void *)vec->assoc; 2105 input[0].iov_len = vec->alen; 2106 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; 2107 input[1].iov_len = enc ? vec->plen : vec->clen; 2108 err = build_cipher_test_sglists(tsgls, cfg, alignmask, 2109 vec->alen + (enc ? vec->plen : 2110 vec->clen), 2111 vec->alen + (enc ? vec->clen : 2112 vec->plen), 2113 input, 2); 2114 if (err) { 2115 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n", 2116 driver, op, vec_name, cfg->name); 2117 return err; 2118 } 2119 2120 /* Do the actual encryption or decryption */ 2121 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm)); 2122 aead_request_set_callback(req, req_flags, crypto_req_done, &wait); 2123 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, 2124 enc ? vec->plen : vec->clen, iv); 2125 aead_request_set_ad(req, vec->alen); 2126 if (cfg->nosimd) 2127 crypto_disable_simd_for_test(); 2128 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req); 2129 if (cfg->nosimd) 2130 crypto_reenable_simd_for_test(); 2131 err = crypto_wait_req(err, &wait); 2132 2133 /* Check that the algorithm didn't overwrite things it shouldn't have */ 2134 if (req->cryptlen != (enc ? vec->plen : vec->clen) || 2135 req->assoclen != vec->alen || 2136 req->iv != iv || 2137 req->src != tsgls->src.sgl_ptr || 2138 req->dst != tsgls->dst.sgl_ptr || 2139 crypto_aead_reqtfm(req) != tfm || 2140 req->base.complete != crypto_req_done || 2141 req->base.flags != req_flags || 2142 req->base.data != &wait) { 2143 pr_err("alg: aead: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n", 2144 driver, op, vec_name, cfg->name); 2145 if (req->cryptlen != (enc ? vec->plen : vec->clen)) 2146 pr_err("alg: aead: changed 'req->cryptlen'\n"); 2147 if (req->assoclen != vec->alen) 2148 pr_err("alg: aead: changed 'req->assoclen'\n"); 2149 if (req->iv != iv) 2150 pr_err("alg: aead: changed 'req->iv'\n"); 2151 if (req->src != tsgls->src.sgl_ptr) 2152 pr_err("alg: aead: changed 'req->src'\n"); 2153 if (req->dst != tsgls->dst.sgl_ptr) 2154 pr_err("alg: aead: changed 'req->dst'\n"); 2155 if (crypto_aead_reqtfm(req) != tfm) 2156 pr_err("alg: aead: changed 'req->base.tfm'\n"); 2157 if (req->base.complete != crypto_req_done) 2158 pr_err("alg: aead: changed 'req->base.complete'\n"); 2159 if (req->base.flags != req_flags) 2160 pr_err("alg: aead: changed 'req->base.flags'\n"); 2161 if (req->base.data != &wait) 2162 pr_err("alg: aead: changed 'req->base.data'\n"); 2163 return -EINVAL; 2164 } 2165 if (is_test_sglist_corrupted(&tsgls->src)) { 2166 pr_err("alg: aead: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n", 2167 driver, op, vec_name, cfg->name); 2168 return -EINVAL; 2169 } 2170 if (tsgls->dst.sgl_ptr != tsgls->src.sgl && 2171 is_test_sglist_corrupted(&tsgls->dst)) { 2172 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n", 2173 driver, op, vec_name, cfg->name); 2174 return -EINVAL; 2175 } 2176 2177 /* Check for unexpected success or failure, or wrong error code */ 2178 if ((err == 0 && vec->novrfy) || 2179 (err != vec->crypt_error && !(err == -EBADMSG && vec->novrfy))) { 2180 char expected_error[32]; 2181 2182 if (vec->novrfy && 2183 vec->crypt_error != 0 && vec->crypt_error != -EBADMSG) 2184 sprintf(expected_error, "-EBADMSG or %d", 2185 vec->crypt_error); 2186 else if (vec->novrfy) 2187 sprintf(expected_error, "-EBADMSG"); 2188 else 2189 sprintf(expected_error, "%d", vec->crypt_error); 2190 if (err) { 2191 pr_err("alg: aead: %s %s failed on test vector %s; expected_error=%s, actual_error=%d, cfg=\"%s\"\n", 2192 driver, op, vec_name, expected_error, err, 2193 cfg->name); 2194 return err; 2195 } 2196 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %s; expected_error=%s, cfg=\"%s\"\n", 2197 driver, op, vec_name, expected_error, cfg->name); 2198 return -EINVAL; 2199 } 2200 if (err) /* Expectedly failed. */ 2201 return 0; 2202 2203 /* Check for the correct output (ciphertext or plaintext) */ 2204 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext, 2205 enc ? vec->clen : vec->plen, 2206 vec->alen, 2207 enc || cfg->inplace_mode == OUT_OF_PLACE); 2208 if (err == -EOVERFLOW) { 2209 pr_err("alg: aead: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n", 2210 driver, op, vec_name, cfg->name); 2211 return err; 2212 } 2213 if (err) { 2214 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n", 2215 driver, op, vec_name, cfg->name); 2216 return err; 2217 } 2218 2219 return 0; 2220 } 2221 2222 static int test_aead_vec(int enc, const struct aead_testvec *vec, 2223 unsigned int vec_num, struct aead_request *req, 2224 struct cipher_test_sglists *tsgls) 2225 { 2226 char vec_name[16]; 2227 unsigned int i; 2228 int err; 2229 2230 if (enc && vec->novrfy) 2231 return 0; 2232 2233 sprintf(vec_name, "%u", vec_num); 2234 2235 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) { 2236 err = test_aead_vec_cfg(enc, vec, vec_name, 2237 &default_cipher_testvec_configs[i], 2238 req, tsgls); 2239 if (err) 2240 return err; 2241 } 2242 2243 if (!noslowtests) { 2244 struct rnd_state rng; 2245 struct testvec_config cfg; 2246 char cfgname[TESTVEC_CONFIG_NAMELEN]; 2247 2248 init_rnd_state(&rng); 2249 2250 for (i = 0; i < fuzz_iterations; i++) { 2251 generate_random_testvec_config(&rng, &cfg, cfgname, 2252 sizeof(cfgname)); 2253 err = test_aead_vec_cfg(enc, vec, vec_name, 2254 &cfg, req, tsgls); 2255 if (err) 2256 return err; 2257 cond_resched(); 2258 } 2259 } 2260 return 0; 2261 } 2262 2263 struct aead_slow_tests_ctx { 2264 struct rnd_state rng; 2265 struct aead_request *req; 2266 struct crypto_aead *tfm; 2267 const struct alg_test_desc *test_desc; 2268 struct cipher_test_sglists *tsgls; 2269 unsigned int maxdatasize; 2270 unsigned int maxkeysize; 2271 2272 struct aead_testvec vec; 2273 char vec_name[64]; 2274 char cfgname[TESTVEC_CONFIG_NAMELEN]; 2275 struct testvec_config cfg; 2276 }; 2277 2278 /* 2279 * Make at least one random change to a (ciphertext, AAD) pair. "Ciphertext" 2280 * here means the full ciphertext including the authentication tag. The 2281 * authentication tag (and hence also the ciphertext) is assumed to be nonempty. 2282 */ 2283 static void mutate_aead_message(struct rnd_state *rng, 2284 struct aead_testvec *vec, bool aad_iv, 2285 unsigned int ivsize) 2286 { 2287 const unsigned int aad_tail_size = aad_iv ? ivsize : 0; 2288 const unsigned int authsize = vec->clen - vec->plen; 2289 2290 if (prandom_bool(rng) && vec->alen > aad_tail_size) { 2291 /* Mutate the AAD */ 2292 flip_random_bit(rng, (u8 *)vec->assoc, 2293 vec->alen - aad_tail_size); 2294 if (prandom_bool(rng)) 2295 return; 2296 } 2297 if (prandom_bool(rng)) { 2298 /* Mutate auth tag (assuming it's at the end of ciphertext) */ 2299 flip_random_bit(rng, (u8 *)vec->ctext + vec->plen, authsize); 2300 } else { 2301 /* Mutate any part of the ciphertext */ 2302 flip_random_bit(rng, (u8 *)vec->ctext, vec->clen); 2303 } 2304 } 2305 2306 /* 2307 * Minimum authentication tag size in bytes at which we assume that we can 2308 * reliably generate inauthentic messages, i.e. not generate an authentic 2309 * message by chance. 2310 */ 2311 #define MIN_COLLISION_FREE_AUTHSIZE 8 2312 2313 static void generate_aead_message(struct rnd_state *rng, 2314 struct aead_request *req, 2315 const struct aead_test_suite *suite, 2316 struct aead_testvec *vec, 2317 bool prefer_inauthentic) 2318 { 2319 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2320 const unsigned int ivsize = crypto_aead_ivsize(tfm); 2321 const unsigned int authsize = vec->clen - vec->plen; 2322 const bool inauthentic = (authsize >= MIN_COLLISION_FREE_AUTHSIZE) && 2323 (prefer_inauthentic || 2324 prandom_u32_below(rng, 4) == 0); 2325 2326 /* Generate the AAD. */ 2327 generate_random_bytes(rng, (u8 *)vec->assoc, vec->alen); 2328 if (suite->aad_iv && vec->alen >= ivsize) 2329 /* Avoid implementation-defined behavior. */ 2330 memcpy((u8 *)vec->assoc + vec->alen - ivsize, vec->iv, ivsize); 2331 2332 if (inauthentic && prandom_bool(rng)) { 2333 /* Generate a random ciphertext. */ 2334 generate_random_bytes(rng, (u8 *)vec->ctext, vec->clen); 2335 } else { 2336 int i = 0; 2337 struct scatterlist src[2], dst; 2338 u8 iv[MAX_IVLEN]; 2339 DECLARE_CRYPTO_WAIT(wait); 2340 2341 /* Generate a random plaintext and encrypt it. */ 2342 sg_init_table(src, 2); 2343 if (vec->alen) 2344 sg_set_buf(&src[i++], vec->assoc, vec->alen); 2345 if (vec->plen) { 2346 generate_random_bytes(rng, (u8 *)vec->ptext, vec->plen); 2347 sg_set_buf(&src[i++], vec->ptext, vec->plen); 2348 } 2349 sg_init_one(&dst, vec->ctext, vec->alen + vec->clen); 2350 memcpy(iv, vec->iv, ivsize); 2351 aead_request_set_callback(req, 0, crypto_req_done, &wait); 2352 aead_request_set_crypt(req, src, &dst, vec->plen, iv); 2353 aead_request_set_ad(req, vec->alen); 2354 vec->crypt_error = crypto_wait_req(crypto_aead_encrypt(req), 2355 &wait); 2356 /* If encryption failed, we're done. */ 2357 if (vec->crypt_error != 0) 2358 return; 2359 memmove((u8 *)vec->ctext, vec->ctext + vec->alen, vec->clen); 2360 if (!inauthentic) 2361 return; 2362 /* 2363 * Mutate the authentic (ciphertext, AAD) pair to get an 2364 * inauthentic one. 2365 */ 2366 mutate_aead_message(rng, vec, suite->aad_iv, ivsize); 2367 } 2368 vec->novrfy = 1; 2369 if (suite->einval_allowed) 2370 vec->crypt_error = -EINVAL; 2371 } 2372 2373 /* 2374 * Generate an AEAD test vector 'vec' using the implementation specified by 2375 * 'req'. The buffers in 'vec' must already be allocated. 2376 * 2377 * If 'prefer_inauthentic' is true, then this function will generate inauthentic 2378 * test vectors (i.e. vectors with 'vec->novrfy=1') more often. 2379 */ 2380 static void generate_random_aead_testvec(struct rnd_state *rng, 2381 struct aead_request *req, 2382 struct aead_testvec *vec, 2383 const struct aead_test_suite *suite, 2384 unsigned int maxkeysize, 2385 unsigned int maxdatasize, 2386 char *name, size_t max_namelen, 2387 bool prefer_inauthentic) 2388 { 2389 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2390 const unsigned int ivsize = crypto_aead_ivsize(tfm); 2391 const unsigned int maxauthsize = crypto_aead_maxauthsize(tfm); 2392 unsigned int authsize; 2393 unsigned int total_len; 2394 2395 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */ 2396 vec->klen = maxkeysize; 2397 if (prandom_u32_below(rng, 4) == 0) 2398 vec->klen = prandom_u32_below(rng, maxkeysize + 1); 2399 generate_random_bytes(rng, (u8 *)vec->key, vec->klen); 2400 vec->setkey_error = crypto_aead_setkey(tfm, vec->key, vec->klen); 2401 2402 /* IV */ 2403 generate_random_bytes(rng, (u8 *)vec->iv, ivsize); 2404 2405 /* Tag length: in [0, maxauthsize], but usually choose maxauthsize */ 2406 authsize = maxauthsize; 2407 if (prandom_u32_below(rng, 4) == 0) 2408 authsize = prandom_u32_below(rng, maxauthsize + 1); 2409 if (prefer_inauthentic && authsize < MIN_COLLISION_FREE_AUTHSIZE) 2410 authsize = MIN_COLLISION_FREE_AUTHSIZE; 2411 if (WARN_ON(authsize > maxdatasize)) 2412 authsize = maxdatasize; 2413 maxdatasize -= authsize; 2414 vec->setauthsize_error = crypto_aead_setauthsize(tfm, authsize); 2415 2416 /* AAD, plaintext, and ciphertext lengths */ 2417 total_len = generate_random_length(rng, maxdatasize); 2418 if (prandom_u32_below(rng, 4) == 0) 2419 vec->alen = 0; 2420 else 2421 vec->alen = generate_random_length(rng, total_len); 2422 vec->plen = total_len - vec->alen; 2423 vec->clen = vec->plen + authsize; 2424 2425 /* 2426 * Generate the AAD, plaintext, and ciphertext. Not applicable if the 2427 * key or the authentication tag size couldn't be set. 2428 */ 2429 vec->novrfy = 0; 2430 vec->crypt_error = 0; 2431 if (vec->setkey_error == 0 && vec->setauthsize_error == 0) 2432 generate_aead_message(rng, req, suite, vec, prefer_inauthentic); 2433 snprintf(name, max_namelen, 2434 "\"random: alen=%u plen=%u authsize=%u klen=%u novrfy=%d\"", 2435 vec->alen, vec->plen, authsize, vec->klen, vec->novrfy); 2436 } 2437 2438 static void try_to_generate_inauthentic_testvec(struct aead_slow_tests_ctx *ctx) 2439 { 2440 int i; 2441 2442 for (i = 0; i < 10; i++) { 2443 generate_random_aead_testvec(&ctx->rng, ctx->req, &ctx->vec, 2444 &ctx->test_desc->suite.aead, 2445 ctx->maxkeysize, ctx->maxdatasize, 2446 ctx->vec_name, 2447 sizeof(ctx->vec_name), true); 2448 if (ctx->vec.novrfy) 2449 return; 2450 } 2451 } 2452 2453 /* 2454 * Generate inauthentic test vectors (i.e. ciphertext, AAD pairs that aren't the 2455 * result of an encryption with the key) and verify that decryption fails. 2456 */ 2457 static int test_aead_inauthentic_inputs(struct aead_slow_tests_ctx *ctx) 2458 { 2459 unsigned int i; 2460 int err; 2461 2462 for (i = 0; i < fuzz_iterations * 8; i++) { 2463 /* 2464 * Since this part of the tests isn't comparing the 2465 * implementation to another, there's no point in testing any 2466 * test vectors other than inauthentic ones (vec.novrfy=1) here. 2467 * 2468 * If we're having trouble generating such a test vector, e.g. 2469 * if the algorithm keeps rejecting the generated keys, don't 2470 * retry forever; just continue on. 2471 */ 2472 try_to_generate_inauthentic_testvec(ctx); 2473 if (ctx->vec.novrfy) { 2474 generate_random_testvec_config(&ctx->rng, &ctx->cfg, 2475 ctx->cfgname, 2476 sizeof(ctx->cfgname)); 2477 err = test_aead_vec_cfg(DECRYPT, &ctx->vec, 2478 ctx->vec_name, &ctx->cfg, 2479 ctx->req, ctx->tsgls); 2480 if (err) 2481 return err; 2482 } 2483 cond_resched(); 2484 } 2485 return 0; 2486 } 2487 2488 /* 2489 * Test the AEAD algorithm against the corresponding generic implementation, if 2490 * one is available. 2491 */ 2492 static int test_aead_vs_generic_impl(struct aead_slow_tests_ctx *ctx) 2493 { 2494 struct crypto_aead *tfm = ctx->tfm; 2495 const char *algname = crypto_aead_alg(tfm)->base.cra_name; 2496 const char *driver = crypto_aead_driver_name(tfm); 2497 const char *generic_driver = ctx->test_desc->generic_driver; 2498 char _generic_driver[CRYPTO_MAX_ALG_NAME]; 2499 struct crypto_aead *generic_tfm = NULL; 2500 struct aead_request *generic_req = NULL; 2501 unsigned int i; 2502 int err; 2503 2504 if (!generic_driver) { /* Use default naming convention? */ 2505 err = build_generic_driver_name(algname, _generic_driver); 2506 if (err) 2507 return err; 2508 generic_driver = _generic_driver; 2509 } 2510 2511 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */ 2512 return 0; 2513 2514 generic_tfm = crypto_alloc_aead(generic_driver, 0, 0); 2515 if (IS_ERR(generic_tfm)) { 2516 err = PTR_ERR(generic_tfm); 2517 if (err == -ENOENT) { 2518 pr_warn("alg: aead: skipping comparison tests for %s because %s is unavailable\n", 2519 driver, generic_driver); 2520 return 0; 2521 } 2522 pr_err("alg: aead: error allocating %s (generic impl of %s): %d\n", 2523 generic_driver, algname, err); 2524 return err; 2525 } 2526 2527 generic_req = aead_request_alloc(generic_tfm, GFP_KERNEL); 2528 if (!generic_req) { 2529 err = -ENOMEM; 2530 goto out; 2531 } 2532 2533 /* Check the algorithm properties for consistency. */ 2534 2535 if (crypto_aead_maxauthsize(tfm) != 2536 crypto_aead_maxauthsize(generic_tfm)) { 2537 pr_err("alg: aead: maxauthsize for %s (%u) doesn't match generic impl (%u)\n", 2538 driver, crypto_aead_maxauthsize(tfm), 2539 crypto_aead_maxauthsize(generic_tfm)); 2540 err = -EINVAL; 2541 goto out; 2542 } 2543 2544 if (crypto_aead_ivsize(tfm) != crypto_aead_ivsize(generic_tfm)) { 2545 pr_err("alg: aead: ivsize for %s (%u) doesn't match generic impl (%u)\n", 2546 driver, crypto_aead_ivsize(tfm), 2547 crypto_aead_ivsize(generic_tfm)); 2548 err = -EINVAL; 2549 goto out; 2550 } 2551 2552 if (crypto_aead_blocksize(tfm) != crypto_aead_blocksize(generic_tfm)) { 2553 pr_err("alg: aead: blocksize for %s (%u) doesn't match generic impl (%u)\n", 2554 driver, crypto_aead_blocksize(tfm), 2555 crypto_aead_blocksize(generic_tfm)); 2556 err = -EINVAL; 2557 goto out; 2558 } 2559 2560 /* 2561 * Now generate test vectors using the generic implementation, and test 2562 * the other implementation against them. 2563 */ 2564 for (i = 0; i < fuzz_iterations * 8; i++) { 2565 generate_random_aead_testvec(&ctx->rng, generic_req, &ctx->vec, 2566 &ctx->test_desc->suite.aead, 2567 ctx->maxkeysize, ctx->maxdatasize, 2568 ctx->vec_name, 2569 sizeof(ctx->vec_name), false); 2570 generate_random_testvec_config(&ctx->rng, &ctx->cfg, 2571 ctx->cfgname, 2572 sizeof(ctx->cfgname)); 2573 if (!ctx->vec.novrfy) { 2574 err = test_aead_vec_cfg(ENCRYPT, &ctx->vec, 2575 ctx->vec_name, &ctx->cfg, 2576 ctx->req, ctx->tsgls); 2577 if (err) 2578 goto out; 2579 } 2580 if (ctx->vec.crypt_error == 0 || ctx->vec.novrfy) { 2581 err = test_aead_vec_cfg(DECRYPT, &ctx->vec, 2582 ctx->vec_name, &ctx->cfg, 2583 ctx->req, ctx->tsgls); 2584 if (err) 2585 goto out; 2586 } 2587 cond_resched(); 2588 } 2589 err = 0; 2590 out: 2591 crypto_free_aead(generic_tfm); 2592 aead_request_free(generic_req); 2593 return err; 2594 } 2595 2596 static int test_aead_slow(const struct alg_test_desc *test_desc, 2597 struct aead_request *req, 2598 struct cipher_test_sglists *tsgls) 2599 { 2600 struct aead_slow_tests_ctx *ctx; 2601 unsigned int i; 2602 int err; 2603 2604 if (noslowtests) 2605 return 0; 2606 2607 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 2608 if (!ctx) 2609 return -ENOMEM; 2610 init_rnd_state(&ctx->rng); 2611 ctx->req = req; 2612 ctx->tfm = crypto_aead_reqtfm(req); 2613 ctx->test_desc = test_desc; 2614 ctx->tsgls = tsgls; 2615 ctx->maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN; 2616 ctx->maxkeysize = 0; 2617 for (i = 0; i < test_desc->suite.aead.count; i++) 2618 ctx->maxkeysize = max_t(unsigned int, ctx->maxkeysize, 2619 test_desc->suite.aead.vecs[i].klen); 2620 2621 ctx->vec.key = kmalloc(ctx->maxkeysize, GFP_KERNEL); 2622 ctx->vec.iv = kmalloc(crypto_aead_ivsize(ctx->tfm), GFP_KERNEL); 2623 ctx->vec.assoc = kmalloc(ctx->maxdatasize, GFP_KERNEL); 2624 ctx->vec.ptext = kmalloc(ctx->maxdatasize, GFP_KERNEL); 2625 ctx->vec.ctext = kmalloc(ctx->maxdatasize, GFP_KERNEL); 2626 if (!ctx->vec.key || !ctx->vec.iv || !ctx->vec.assoc || 2627 !ctx->vec.ptext || !ctx->vec.ctext) { 2628 err = -ENOMEM; 2629 goto out; 2630 } 2631 2632 err = test_aead_vs_generic_impl(ctx); 2633 if (err) 2634 goto out; 2635 2636 err = test_aead_inauthentic_inputs(ctx); 2637 out: 2638 kfree(ctx->vec.key); 2639 kfree(ctx->vec.iv); 2640 kfree(ctx->vec.assoc); 2641 kfree(ctx->vec.ptext); 2642 kfree(ctx->vec.ctext); 2643 kfree(ctx); 2644 return err; 2645 } 2646 2647 static int test_aead(int enc, const struct aead_test_suite *suite, 2648 struct aead_request *req, 2649 struct cipher_test_sglists *tsgls) 2650 { 2651 unsigned int i; 2652 int err; 2653 2654 for (i = 0; i < suite->count; i++) { 2655 err = test_aead_vec(enc, &suite->vecs[i], i, req, tsgls); 2656 if (err) 2657 return err; 2658 cond_resched(); 2659 } 2660 return 0; 2661 } 2662 2663 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver, 2664 u32 type, u32 mask) 2665 { 2666 const struct aead_test_suite *suite = &desc->suite.aead; 2667 struct crypto_aead *tfm; 2668 struct aead_request *req = NULL; 2669 struct cipher_test_sglists *tsgls = NULL; 2670 int err; 2671 2672 if (suite->count <= 0) { 2673 pr_err("alg: aead: empty test suite for %s\n", driver); 2674 return -EINVAL; 2675 } 2676 2677 tfm = crypto_alloc_aead(driver, type, mask); 2678 if (IS_ERR(tfm)) { 2679 if (PTR_ERR(tfm) == -ENOENT) 2680 return 0; 2681 pr_err("alg: aead: failed to allocate transform for %s: %ld\n", 2682 driver, PTR_ERR(tfm)); 2683 return PTR_ERR(tfm); 2684 } 2685 driver = crypto_aead_driver_name(tfm); 2686 2687 req = aead_request_alloc(tfm, GFP_KERNEL); 2688 if (!req) { 2689 pr_err("alg: aead: failed to allocate request for %s\n", 2690 driver); 2691 err = -ENOMEM; 2692 goto out; 2693 } 2694 2695 tsgls = alloc_cipher_test_sglists(); 2696 if (!tsgls) { 2697 pr_err("alg: aead: failed to allocate test buffers for %s\n", 2698 driver); 2699 err = -ENOMEM; 2700 goto out; 2701 } 2702 2703 err = test_aead(ENCRYPT, suite, req, tsgls); 2704 if (err) 2705 goto out; 2706 2707 err = test_aead(DECRYPT, suite, req, tsgls); 2708 if (err) 2709 goto out; 2710 2711 err = test_aead_slow(desc, req, tsgls); 2712 out: 2713 free_cipher_test_sglists(tsgls); 2714 aead_request_free(req); 2715 crypto_free_aead(tfm); 2716 return err; 2717 } 2718 2719 static int test_cipher(struct crypto_cipher *tfm, int enc, 2720 const struct cipher_testvec *template, 2721 unsigned int tcount) 2722 { 2723 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm)); 2724 unsigned int i, j, k; 2725 char *q; 2726 const char *e; 2727 const char *input, *result; 2728 void *data; 2729 char *xbuf[XBUFSIZE]; 2730 int ret = -ENOMEM; 2731 2732 if (testmgr_alloc_buf(xbuf)) 2733 goto out_nobuf; 2734 2735 if (enc == ENCRYPT) 2736 e = "encryption"; 2737 else 2738 e = "decryption"; 2739 2740 j = 0; 2741 for (i = 0; i < tcount; i++) { 2742 2743 if (fips_enabled && template[i].fips_skip) 2744 continue; 2745 2746 input = enc ? template[i].ptext : template[i].ctext; 2747 result = enc ? template[i].ctext : template[i].ptext; 2748 j++; 2749 2750 ret = -EINVAL; 2751 if (WARN_ON(template[i].len > PAGE_SIZE)) 2752 goto out; 2753 2754 data = xbuf[0]; 2755 memcpy(data, input, template[i].len); 2756 2757 crypto_cipher_clear_flags(tfm, ~0); 2758 if (template[i].wk) 2759 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 2760 2761 ret = crypto_cipher_setkey(tfm, template[i].key, 2762 template[i].klen); 2763 if (ret) { 2764 if (ret == template[i].setkey_error) 2765 continue; 2766 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n", 2767 algo, j, template[i].setkey_error, ret, 2768 crypto_cipher_get_flags(tfm)); 2769 goto out; 2770 } 2771 if (template[i].setkey_error) { 2772 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n", 2773 algo, j, template[i].setkey_error); 2774 ret = -EINVAL; 2775 goto out; 2776 } 2777 2778 for (k = 0; k < template[i].len; 2779 k += crypto_cipher_blocksize(tfm)) { 2780 if (enc) 2781 crypto_cipher_encrypt_one(tfm, data + k, 2782 data + k); 2783 else 2784 crypto_cipher_decrypt_one(tfm, data + k, 2785 data + k); 2786 } 2787 2788 q = data; 2789 if (memcmp(q, result, template[i].len)) { 2790 printk(KERN_ERR "alg: cipher: Test %d failed " 2791 "on %s for %s\n", j, e, algo); 2792 hexdump(q, template[i].len); 2793 ret = -EINVAL; 2794 goto out; 2795 } 2796 } 2797 2798 ret = 0; 2799 2800 out: 2801 testmgr_free_buf(xbuf); 2802 out_nobuf: 2803 return ret; 2804 } 2805 2806 static int test_skcipher_vec_cfg(int enc, const struct cipher_testvec *vec, 2807 const char *vec_name, 2808 const struct testvec_config *cfg, 2809 struct skcipher_request *req, 2810 struct cipher_test_sglists *tsgls) 2811 { 2812 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 2813 const unsigned int alignmask = crypto_skcipher_alignmask(tfm); 2814 const unsigned int ivsize = crypto_skcipher_ivsize(tfm); 2815 const char *driver = crypto_skcipher_driver_name(tfm); 2816 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 2817 const char *op = enc ? "encryption" : "decryption"; 2818 DECLARE_CRYPTO_WAIT(wait); 2819 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN]; 2820 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) + 2821 cfg->iv_offset + 2822 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0); 2823 struct kvec input; 2824 int err; 2825 2826 /* Set the key */ 2827 if (vec->wk) 2828 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 2829 else 2830 crypto_skcipher_clear_flags(tfm, 2831 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 2832 err = do_setkey(crypto_skcipher_setkey, tfm, vec->key, vec->klen, 2833 cfg, alignmask); 2834 if (err) { 2835 if (err == vec->setkey_error) 2836 return 0; 2837 pr_err("alg: skcipher: %s setkey failed on test vector %s; expected_error=%d, actual_error=%d, flags=%#x\n", 2838 driver, vec_name, vec->setkey_error, err, 2839 crypto_skcipher_get_flags(tfm)); 2840 return err; 2841 } 2842 if (vec->setkey_error) { 2843 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %s; expected_error=%d\n", 2844 driver, vec_name, vec->setkey_error); 2845 return -EINVAL; 2846 } 2847 2848 /* The IV must be copied to a buffer, as the algorithm may modify it */ 2849 if (ivsize) { 2850 if (WARN_ON(ivsize > MAX_IVLEN)) 2851 return -EINVAL; 2852 if (vec->iv) 2853 memcpy(iv, vec->iv, ivsize); 2854 else 2855 memset(iv, 0, ivsize); 2856 } else { 2857 iv = NULL; 2858 } 2859 2860 /* Build the src/dst scatterlists */ 2861 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; 2862 input.iov_len = vec->len; 2863 err = build_cipher_test_sglists(tsgls, cfg, alignmask, 2864 vec->len, vec->len, &input, 1); 2865 if (err) { 2866 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %s, cfg=\"%s\"\n", 2867 driver, op, vec_name, cfg->name); 2868 return err; 2869 } 2870 2871 /* Do the actual encryption or decryption */ 2872 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm)); 2873 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait); 2874 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, 2875 vec->len, iv); 2876 if (cfg->nosimd) 2877 crypto_disable_simd_for_test(); 2878 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req); 2879 if (cfg->nosimd) 2880 crypto_reenable_simd_for_test(); 2881 err = crypto_wait_req(err, &wait); 2882 2883 /* Check that the algorithm didn't overwrite things it shouldn't have */ 2884 if (req->cryptlen != vec->len || 2885 req->iv != iv || 2886 req->src != tsgls->src.sgl_ptr || 2887 req->dst != tsgls->dst.sgl_ptr || 2888 crypto_skcipher_reqtfm(req) != tfm || 2889 req->base.complete != crypto_req_done || 2890 req->base.flags != req_flags || 2891 req->base.data != &wait) { 2892 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %s, cfg=\"%s\"\n", 2893 driver, op, vec_name, cfg->name); 2894 if (req->cryptlen != vec->len) 2895 pr_err("alg: skcipher: changed 'req->cryptlen'\n"); 2896 if (req->iv != iv) 2897 pr_err("alg: skcipher: changed 'req->iv'\n"); 2898 if (req->src != tsgls->src.sgl_ptr) 2899 pr_err("alg: skcipher: changed 'req->src'\n"); 2900 if (req->dst != tsgls->dst.sgl_ptr) 2901 pr_err("alg: skcipher: changed 'req->dst'\n"); 2902 if (crypto_skcipher_reqtfm(req) != tfm) 2903 pr_err("alg: skcipher: changed 'req->base.tfm'\n"); 2904 if (req->base.complete != crypto_req_done) 2905 pr_err("alg: skcipher: changed 'req->base.complete'\n"); 2906 if (req->base.flags != req_flags) 2907 pr_err("alg: skcipher: changed 'req->base.flags'\n"); 2908 if (req->base.data != &wait) 2909 pr_err("alg: skcipher: changed 'req->base.data'\n"); 2910 return -EINVAL; 2911 } 2912 if (is_test_sglist_corrupted(&tsgls->src)) { 2913 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %s, cfg=\"%s\"\n", 2914 driver, op, vec_name, cfg->name); 2915 return -EINVAL; 2916 } 2917 if (tsgls->dst.sgl_ptr != tsgls->src.sgl && 2918 is_test_sglist_corrupted(&tsgls->dst)) { 2919 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %s, cfg=\"%s\"\n", 2920 driver, op, vec_name, cfg->name); 2921 return -EINVAL; 2922 } 2923 2924 /* Check for success or failure */ 2925 if (err) { 2926 if (err == vec->crypt_error) 2927 return 0; 2928 pr_err("alg: skcipher: %s %s failed on test vector %s; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 2929 driver, op, vec_name, vec->crypt_error, err, cfg->name); 2930 return err; 2931 } 2932 if (vec->crypt_error) { 2933 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %s; expected_error=%d, cfg=\"%s\"\n", 2934 driver, op, vec_name, vec->crypt_error, cfg->name); 2935 return -EINVAL; 2936 } 2937 2938 /* Check for the correct output (ciphertext or plaintext) */ 2939 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext, 2940 vec->len, 0, true); 2941 if (err == -EOVERFLOW) { 2942 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %s, cfg=\"%s\"\n", 2943 driver, op, vec_name, cfg->name); 2944 return err; 2945 } 2946 if (err) { 2947 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %s, cfg=\"%s\"\n", 2948 driver, op, vec_name, cfg->name); 2949 return err; 2950 } 2951 2952 /* If applicable, check that the algorithm generated the correct IV */ 2953 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) { 2954 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %s, cfg=\"%s\"\n", 2955 driver, op, vec_name, cfg->name); 2956 hexdump(iv, ivsize); 2957 return -EINVAL; 2958 } 2959 2960 return 0; 2961 } 2962 2963 static int test_skcipher_vec(int enc, const struct cipher_testvec *vec, 2964 unsigned int vec_num, 2965 struct skcipher_request *req, 2966 struct cipher_test_sglists *tsgls) 2967 { 2968 char vec_name[16]; 2969 unsigned int i; 2970 int err; 2971 2972 if (fips_enabled && vec->fips_skip) 2973 return 0; 2974 2975 sprintf(vec_name, "%u", vec_num); 2976 2977 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) { 2978 err = test_skcipher_vec_cfg(enc, vec, vec_name, 2979 &default_cipher_testvec_configs[i], 2980 req, tsgls); 2981 if (err) 2982 return err; 2983 } 2984 2985 if (!noslowtests) { 2986 struct rnd_state rng; 2987 struct testvec_config cfg; 2988 char cfgname[TESTVEC_CONFIG_NAMELEN]; 2989 2990 init_rnd_state(&rng); 2991 2992 for (i = 0; i < fuzz_iterations; i++) { 2993 generate_random_testvec_config(&rng, &cfg, cfgname, 2994 sizeof(cfgname)); 2995 err = test_skcipher_vec_cfg(enc, vec, vec_name, 2996 &cfg, req, tsgls); 2997 if (err) 2998 return err; 2999 cond_resched(); 3000 } 3001 } 3002 return 0; 3003 } 3004 3005 /* 3006 * Generate a symmetric cipher test vector from the given implementation. 3007 * Assumes the buffers in 'vec' were already allocated. 3008 */ 3009 static void generate_random_cipher_testvec(struct rnd_state *rng, 3010 struct skcipher_request *req, 3011 struct cipher_testvec *vec, 3012 unsigned int maxdatasize, 3013 char *name, size_t max_namelen) 3014 { 3015 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 3016 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm); 3017 const unsigned int ivsize = crypto_skcipher_ivsize(tfm); 3018 struct scatterlist src, dst; 3019 u8 iv[MAX_IVLEN]; 3020 DECLARE_CRYPTO_WAIT(wait); 3021 3022 /* Key: length in [0, maxkeysize], but usually choose maxkeysize */ 3023 vec->klen = maxkeysize; 3024 if (prandom_u32_below(rng, 4) == 0) 3025 vec->klen = prandom_u32_below(rng, maxkeysize + 1); 3026 generate_random_bytes(rng, (u8 *)vec->key, vec->klen); 3027 vec->setkey_error = crypto_skcipher_setkey(tfm, vec->key, vec->klen); 3028 3029 /* IV */ 3030 generate_random_bytes(rng, (u8 *)vec->iv, ivsize); 3031 3032 /* Plaintext */ 3033 vec->len = generate_random_length(rng, maxdatasize); 3034 generate_random_bytes(rng, (u8 *)vec->ptext, vec->len); 3035 3036 /* If the key couldn't be set, no need to continue to encrypt. */ 3037 if (vec->setkey_error) 3038 goto done; 3039 3040 /* Ciphertext */ 3041 sg_init_one(&src, vec->ptext, vec->len); 3042 sg_init_one(&dst, vec->ctext, vec->len); 3043 memcpy(iv, vec->iv, ivsize); 3044 skcipher_request_set_callback(req, 0, crypto_req_done, &wait); 3045 skcipher_request_set_crypt(req, &src, &dst, vec->len, iv); 3046 vec->crypt_error = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 3047 if (vec->crypt_error != 0) { 3048 /* 3049 * The only acceptable error here is for an invalid length, so 3050 * skcipher decryption should fail with the same error too. 3051 * We'll test for this. But to keep the API usage well-defined, 3052 * explicitly initialize the ciphertext buffer too. 3053 */ 3054 memset((u8 *)vec->ctext, 0, vec->len); 3055 } 3056 done: 3057 snprintf(name, max_namelen, "\"random: len=%u klen=%u\"", 3058 vec->len, vec->klen); 3059 } 3060 3061 /* 3062 * Test the skcipher algorithm represented by @req against the corresponding 3063 * generic implementation, if one is available. 3064 */ 3065 static int test_skcipher_vs_generic_impl(const char *generic_driver, 3066 struct skcipher_request *req, 3067 struct cipher_test_sglists *tsgls) 3068 { 3069 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 3070 const unsigned int maxkeysize = crypto_skcipher_max_keysize(tfm); 3071 const unsigned int ivsize = crypto_skcipher_ivsize(tfm); 3072 const unsigned int blocksize = crypto_skcipher_blocksize(tfm); 3073 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN; 3074 const char *algname = crypto_skcipher_alg(tfm)->base.cra_name; 3075 const char *driver = crypto_skcipher_driver_name(tfm); 3076 struct rnd_state rng; 3077 char _generic_driver[CRYPTO_MAX_ALG_NAME]; 3078 struct crypto_skcipher *generic_tfm = NULL; 3079 struct skcipher_request *generic_req = NULL; 3080 unsigned int i; 3081 struct cipher_testvec vec = { 0 }; 3082 char vec_name[64]; 3083 struct testvec_config *cfg; 3084 char cfgname[TESTVEC_CONFIG_NAMELEN]; 3085 int err; 3086 3087 if (noslowtests) 3088 return 0; 3089 3090 init_rnd_state(&rng); 3091 3092 if (!generic_driver) { /* Use default naming convention? */ 3093 err = build_generic_driver_name(algname, _generic_driver); 3094 if (err) 3095 return err; 3096 generic_driver = _generic_driver; 3097 } 3098 3099 if (strcmp(generic_driver, driver) == 0) /* Already the generic impl? */ 3100 return 0; 3101 3102 generic_tfm = crypto_alloc_skcipher(generic_driver, 0, 0); 3103 if (IS_ERR(generic_tfm)) { 3104 err = PTR_ERR(generic_tfm); 3105 if (err == -ENOENT) { 3106 pr_warn("alg: skcipher: skipping comparison tests for %s because %s is unavailable\n", 3107 driver, generic_driver); 3108 return 0; 3109 } 3110 pr_err("alg: skcipher: error allocating %s (generic impl of %s): %d\n", 3111 generic_driver, algname, err); 3112 return err; 3113 } 3114 3115 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 3116 if (!cfg) { 3117 err = -ENOMEM; 3118 goto out; 3119 } 3120 3121 generic_req = skcipher_request_alloc(generic_tfm, GFP_KERNEL); 3122 if (!generic_req) { 3123 err = -ENOMEM; 3124 goto out; 3125 } 3126 3127 /* Check the algorithm properties for consistency. */ 3128 3129 if (crypto_skcipher_min_keysize(tfm) != 3130 crypto_skcipher_min_keysize(generic_tfm)) { 3131 pr_err("alg: skcipher: min keysize for %s (%u) doesn't match generic impl (%u)\n", 3132 driver, crypto_skcipher_min_keysize(tfm), 3133 crypto_skcipher_min_keysize(generic_tfm)); 3134 err = -EINVAL; 3135 goto out; 3136 } 3137 3138 if (maxkeysize != crypto_skcipher_max_keysize(generic_tfm)) { 3139 pr_err("alg: skcipher: max keysize for %s (%u) doesn't match generic impl (%u)\n", 3140 driver, maxkeysize, 3141 crypto_skcipher_max_keysize(generic_tfm)); 3142 err = -EINVAL; 3143 goto out; 3144 } 3145 3146 if (ivsize != crypto_skcipher_ivsize(generic_tfm)) { 3147 pr_err("alg: skcipher: ivsize for %s (%u) doesn't match generic impl (%u)\n", 3148 driver, ivsize, crypto_skcipher_ivsize(generic_tfm)); 3149 err = -EINVAL; 3150 goto out; 3151 } 3152 3153 if (blocksize != crypto_skcipher_blocksize(generic_tfm)) { 3154 pr_err("alg: skcipher: blocksize for %s (%u) doesn't match generic impl (%u)\n", 3155 driver, blocksize, 3156 crypto_skcipher_blocksize(generic_tfm)); 3157 err = -EINVAL; 3158 goto out; 3159 } 3160 3161 /* 3162 * Now generate test vectors using the generic implementation, and test 3163 * the other implementation against them. 3164 */ 3165 3166 vec.key = kmalloc(maxkeysize, GFP_KERNEL); 3167 vec.iv = kmalloc(ivsize, GFP_KERNEL); 3168 vec.ptext = kmalloc(maxdatasize, GFP_KERNEL); 3169 vec.ctext = kmalloc(maxdatasize, GFP_KERNEL); 3170 if (!vec.key || !vec.iv || !vec.ptext || !vec.ctext) { 3171 err = -ENOMEM; 3172 goto out; 3173 } 3174 3175 for (i = 0; i < fuzz_iterations * 8; i++) { 3176 generate_random_cipher_testvec(&rng, generic_req, &vec, 3177 maxdatasize, 3178 vec_name, sizeof(vec_name)); 3179 generate_random_testvec_config(&rng, cfg, cfgname, 3180 sizeof(cfgname)); 3181 3182 err = test_skcipher_vec_cfg(ENCRYPT, &vec, vec_name, 3183 cfg, req, tsgls); 3184 if (err) 3185 goto out; 3186 err = test_skcipher_vec_cfg(DECRYPT, &vec, vec_name, 3187 cfg, req, tsgls); 3188 if (err) 3189 goto out; 3190 cond_resched(); 3191 } 3192 err = 0; 3193 out: 3194 kfree(cfg); 3195 kfree(vec.key); 3196 kfree(vec.iv); 3197 kfree(vec.ptext); 3198 kfree(vec.ctext); 3199 crypto_free_skcipher(generic_tfm); 3200 skcipher_request_free(generic_req); 3201 return err; 3202 } 3203 3204 static int test_skcipher(int enc, const struct cipher_test_suite *suite, 3205 struct skcipher_request *req, 3206 struct cipher_test_sglists *tsgls) 3207 { 3208 unsigned int i; 3209 int err; 3210 3211 for (i = 0; i < suite->count; i++) { 3212 err = test_skcipher_vec(enc, &suite->vecs[i], i, req, tsgls); 3213 if (err) 3214 return err; 3215 cond_resched(); 3216 } 3217 return 0; 3218 } 3219 3220 static int alg_test_skcipher(const struct alg_test_desc *desc, 3221 const char *driver, u32 type, u32 mask) 3222 { 3223 const struct cipher_test_suite *suite = &desc->suite.cipher; 3224 struct crypto_skcipher *tfm; 3225 struct skcipher_request *req = NULL; 3226 struct cipher_test_sglists *tsgls = NULL; 3227 int err; 3228 3229 if (suite->count <= 0) { 3230 pr_err("alg: skcipher: empty test suite for %s\n", driver); 3231 return -EINVAL; 3232 } 3233 3234 tfm = crypto_alloc_skcipher(driver, type, mask); 3235 if (IS_ERR(tfm)) { 3236 if (PTR_ERR(tfm) == -ENOENT) 3237 return 0; 3238 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n", 3239 driver, PTR_ERR(tfm)); 3240 return PTR_ERR(tfm); 3241 } 3242 driver = crypto_skcipher_driver_name(tfm); 3243 3244 req = skcipher_request_alloc(tfm, GFP_KERNEL); 3245 if (!req) { 3246 pr_err("alg: skcipher: failed to allocate request for %s\n", 3247 driver); 3248 err = -ENOMEM; 3249 goto out; 3250 } 3251 3252 tsgls = alloc_cipher_test_sglists(); 3253 if (!tsgls) { 3254 pr_err("alg: skcipher: failed to allocate test buffers for %s\n", 3255 driver); 3256 err = -ENOMEM; 3257 goto out; 3258 } 3259 3260 err = test_skcipher(ENCRYPT, suite, req, tsgls); 3261 if (err) 3262 goto out; 3263 3264 err = test_skcipher(DECRYPT, suite, req, tsgls); 3265 if (err) 3266 goto out; 3267 3268 err = test_skcipher_vs_generic_impl(desc->generic_driver, req, tsgls); 3269 out: 3270 free_cipher_test_sglists(tsgls); 3271 skcipher_request_free(req); 3272 crypto_free_skcipher(tfm); 3273 return err; 3274 } 3275 3276 static int test_acomp(struct crypto_acomp *tfm, 3277 const struct comp_testvec *ctemplate, 3278 const struct comp_testvec *dtemplate, 3279 int ctcount, int dtcount) 3280 { 3281 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm)); 3282 unsigned int i; 3283 char *output, *decomp_out; 3284 int ret; 3285 struct scatterlist src, dst; 3286 struct acomp_req *req; 3287 struct crypto_wait wait; 3288 3289 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 3290 if (!output) 3291 return -ENOMEM; 3292 3293 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 3294 if (!decomp_out) { 3295 kfree(output); 3296 return -ENOMEM; 3297 } 3298 3299 for (i = 0; i < ctcount; i++) { 3300 unsigned int dlen = COMP_BUF_SIZE; 3301 int ilen = ctemplate[i].inlen; 3302 void *input_vec; 3303 3304 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL); 3305 if (!input_vec) { 3306 ret = -ENOMEM; 3307 goto out; 3308 } 3309 3310 memset(output, 0, dlen); 3311 crypto_init_wait(&wait); 3312 sg_init_one(&src, input_vec, ilen); 3313 sg_init_one(&dst, output, dlen); 3314 3315 req = acomp_request_alloc(tfm); 3316 if (!req) { 3317 pr_err("alg: acomp: request alloc failed for %s\n", 3318 algo); 3319 kfree(input_vec); 3320 ret = -ENOMEM; 3321 goto out; 3322 } 3323 3324 acomp_request_set_params(req, &src, &dst, ilen, dlen); 3325 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3326 crypto_req_done, &wait); 3327 3328 ret = crypto_wait_req(crypto_acomp_compress(req), &wait); 3329 if (ret) { 3330 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n", 3331 i + 1, algo, -ret); 3332 kfree(input_vec); 3333 acomp_request_free(req); 3334 goto out; 3335 } 3336 3337 ilen = req->dlen; 3338 dlen = COMP_BUF_SIZE; 3339 sg_init_one(&src, output, ilen); 3340 sg_init_one(&dst, decomp_out, dlen); 3341 crypto_init_wait(&wait); 3342 acomp_request_set_params(req, &src, &dst, ilen, dlen); 3343 3344 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait); 3345 if (ret) { 3346 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n", 3347 i + 1, algo, -ret); 3348 kfree(input_vec); 3349 acomp_request_free(req); 3350 goto out; 3351 } 3352 3353 if (req->dlen != ctemplate[i].inlen) { 3354 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n", 3355 i + 1, algo, req->dlen); 3356 ret = -EINVAL; 3357 kfree(input_vec); 3358 acomp_request_free(req); 3359 goto out; 3360 } 3361 3362 if (memcmp(input_vec, decomp_out, req->dlen)) { 3363 pr_err("alg: acomp: Compression test %d failed for %s\n", 3364 i + 1, algo); 3365 hexdump(output, req->dlen); 3366 ret = -EINVAL; 3367 kfree(input_vec); 3368 acomp_request_free(req); 3369 goto out; 3370 } 3371 3372 kfree(input_vec); 3373 acomp_request_free(req); 3374 } 3375 3376 for (i = 0; i < dtcount; i++) { 3377 unsigned int dlen = COMP_BUF_SIZE; 3378 int ilen = dtemplate[i].inlen; 3379 void *input_vec; 3380 3381 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL); 3382 if (!input_vec) { 3383 ret = -ENOMEM; 3384 goto out; 3385 } 3386 3387 memset(output, 0, dlen); 3388 crypto_init_wait(&wait); 3389 sg_init_one(&src, input_vec, ilen); 3390 sg_init_one(&dst, output, dlen); 3391 3392 req = acomp_request_alloc(tfm); 3393 if (!req) { 3394 pr_err("alg: acomp: request alloc failed for %s\n", 3395 algo); 3396 kfree(input_vec); 3397 ret = -ENOMEM; 3398 goto out; 3399 } 3400 3401 acomp_request_set_params(req, &src, &dst, ilen, dlen); 3402 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3403 crypto_req_done, &wait); 3404 3405 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait); 3406 if (ret) { 3407 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n", 3408 i + 1, algo, -ret); 3409 kfree(input_vec); 3410 acomp_request_free(req); 3411 goto out; 3412 } 3413 3414 if (req->dlen != dtemplate[i].outlen) { 3415 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n", 3416 i + 1, algo, req->dlen); 3417 ret = -EINVAL; 3418 kfree(input_vec); 3419 acomp_request_free(req); 3420 goto out; 3421 } 3422 3423 if (memcmp(output, dtemplate[i].output, req->dlen)) { 3424 pr_err("alg: acomp: Decompression test %d failed for %s\n", 3425 i + 1, algo); 3426 hexdump(output, req->dlen); 3427 ret = -EINVAL; 3428 kfree(input_vec); 3429 acomp_request_free(req); 3430 goto out; 3431 } 3432 3433 kfree(input_vec); 3434 acomp_request_free(req); 3435 } 3436 3437 ret = 0; 3438 3439 out: 3440 kfree(decomp_out); 3441 kfree(output); 3442 return ret; 3443 } 3444 3445 static int test_cprng(struct crypto_rng *tfm, 3446 const struct cprng_testvec *template, 3447 unsigned int tcount) 3448 { 3449 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm)); 3450 int err = 0, i, j, seedsize; 3451 u8 *seed; 3452 char result[32]; 3453 3454 seedsize = crypto_rng_seedsize(tfm); 3455 3456 seed = kmalloc(seedsize, GFP_KERNEL); 3457 if (!seed) { 3458 printk(KERN_ERR "alg: cprng: Failed to allocate seed space " 3459 "for %s\n", algo); 3460 return -ENOMEM; 3461 } 3462 3463 for (i = 0; i < tcount; i++) { 3464 memset(result, 0, 32); 3465 3466 memcpy(seed, template[i].v, template[i].vlen); 3467 memcpy(seed + template[i].vlen, template[i].key, 3468 template[i].klen); 3469 memcpy(seed + template[i].vlen + template[i].klen, 3470 template[i].dt, template[i].dtlen); 3471 3472 err = crypto_rng_reset(tfm, seed, seedsize); 3473 if (err) { 3474 printk(KERN_ERR "alg: cprng: Failed to reset rng " 3475 "for %s\n", algo); 3476 goto out; 3477 } 3478 3479 for (j = 0; j < template[i].loops; j++) { 3480 err = crypto_rng_get_bytes(tfm, result, 3481 template[i].rlen); 3482 if (err < 0) { 3483 printk(KERN_ERR "alg: cprng: Failed to obtain " 3484 "the correct amount of random data for " 3485 "%s (requested %d)\n", algo, 3486 template[i].rlen); 3487 goto out; 3488 } 3489 } 3490 3491 err = memcmp(result, template[i].result, 3492 template[i].rlen); 3493 if (err) { 3494 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n", 3495 i, algo); 3496 hexdump(result, template[i].rlen); 3497 err = -EINVAL; 3498 goto out; 3499 } 3500 } 3501 3502 out: 3503 kfree(seed); 3504 return err; 3505 } 3506 3507 static int alg_test_cipher(const struct alg_test_desc *desc, 3508 const char *driver, u32 type, u32 mask) 3509 { 3510 const struct cipher_test_suite *suite = &desc->suite.cipher; 3511 struct crypto_cipher *tfm; 3512 int err; 3513 3514 tfm = crypto_alloc_cipher(driver, type, mask); 3515 if (IS_ERR(tfm)) { 3516 if (PTR_ERR(tfm) == -ENOENT) 3517 return 0; 3518 printk(KERN_ERR "alg: cipher: Failed to load transform for " 3519 "%s: %ld\n", driver, PTR_ERR(tfm)); 3520 return PTR_ERR(tfm); 3521 } 3522 3523 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count); 3524 if (!err) 3525 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count); 3526 3527 crypto_free_cipher(tfm); 3528 return err; 3529 } 3530 3531 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver, 3532 u32 type, u32 mask) 3533 { 3534 struct crypto_acomp *acomp; 3535 int err; 3536 3537 acomp = crypto_alloc_acomp(driver, type, mask); 3538 if (IS_ERR(acomp)) { 3539 if (PTR_ERR(acomp) == -ENOENT) 3540 return 0; 3541 pr_err("alg: acomp: Failed to load transform for %s: %ld\n", 3542 driver, PTR_ERR(acomp)); 3543 return PTR_ERR(acomp); 3544 } 3545 err = test_acomp(acomp, desc->suite.comp.comp.vecs, 3546 desc->suite.comp.decomp.vecs, 3547 desc->suite.comp.comp.count, 3548 desc->suite.comp.decomp.count); 3549 crypto_free_acomp(acomp); 3550 return err; 3551 } 3552 3553 static int alg_test_crc32c(const struct alg_test_desc *desc, 3554 const char *driver, u32 type, u32 mask) 3555 { 3556 struct crypto_shash *tfm; 3557 __le32 val; 3558 int err; 3559 3560 err = alg_test_hash(desc, driver, type, mask); 3561 if (err) 3562 return err; 3563 3564 tfm = crypto_alloc_shash(driver, type, mask); 3565 if (IS_ERR(tfm)) { 3566 if (PTR_ERR(tfm) == -ENOENT) { 3567 /* 3568 * This crc32c implementation is only available through 3569 * ahash API, not the shash API, so the remaining part 3570 * of the test is not applicable to it. 3571 */ 3572 return 0; 3573 } 3574 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: " 3575 "%ld\n", driver, PTR_ERR(tfm)); 3576 return PTR_ERR(tfm); 3577 } 3578 driver = crypto_shash_driver_name(tfm); 3579 3580 do { 3581 SHASH_DESC_ON_STACK(shash, tfm); 3582 u32 *ctx = (u32 *)shash_desc_ctx(shash); 3583 3584 shash->tfm = tfm; 3585 3586 *ctx = 420553207; 3587 err = crypto_shash_final(shash, (u8 *)&val); 3588 if (err) { 3589 printk(KERN_ERR "alg: crc32c: Operation failed for " 3590 "%s: %d\n", driver, err); 3591 break; 3592 } 3593 3594 if (val != cpu_to_le32(~420553207)) { 3595 pr_err("alg: crc32c: Test failed for %s: %u\n", 3596 driver, le32_to_cpu(val)); 3597 err = -EINVAL; 3598 } 3599 } while (0); 3600 3601 crypto_free_shash(tfm); 3602 3603 return err; 3604 } 3605 3606 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver, 3607 u32 type, u32 mask) 3608 { 3609 struct crypto_rng *rng; 3610 int err; 3611 3612 rng = crypto_alloc_rng(driver, type, mask); 3613 if (IS_ERR(rng)) { 3614 if (PTR_ERR(rng) == -ENOENT) 3615 return 0; 3616 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: " 3617 "%ld\n", driver, PTR_ERR(rng)); 3618 return PTR_ERR(rng); 3619 } 3620 3621 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count); 3622 3623 crypto_free_rng(rng); 3624 3625 return err; 3626 } 3627 3628 3629 static int drbg_cavs_test(const struct drbg_testvec *test, int pr, 3630 const char *driver, u32 type, u32 mask) 3631 { 3632 int ret = -EAGAIN; 3633 struct crypto_rng *drng; 3634 struct drbg_test_data test_data; 3635 struct drbg_string addtl, pers, testentropy; 3636 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL); 3637 3638 if (!buf) 3639 return -ENOMEM; 3640 3641 drng = crypto_alloc_rng(driver, type, mask); 3642 if (IS_ERR(drng)) { 3643 kfree_sensitive(buf); 3644 if (PTR_ERR(drng) == -ENOENT) 3645 return 0; 3646 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for " 3647 "%s\n", driver); 3648 return PTR_ERR(drng); 3649 } 3650 3651 test_data.testentropy = &testentropy; 3652 drbg_string_fill(&testentropy, test->entropy, test->entropylen); 3653 drbg_string_fill(&pers, test->pers, test->perslen); 3654 ret = crypto_drbg_reset_test(drng, &pers, &test_data); 3655 if (ret) { 3656 printk(KERN_ERR "alg: drbg: Failed to reset rng\n"); 3657 goto outbuf; 3658 } 3659 3660 drbg_string_fill(&addtl, test->addtla, test->addtllen); 3661 if (pr) { 3662 drbg_string_fill(&testentropy, test->entpra, test->entprlen); 3663 ret = crypto_drbg_get_bytes_addtl_test(drng, 3664 buf, test->expectedlen, &addtl, &test_data); 3665 } else { 3666 ret = crypto_drbg_get_bytes_addtl(drng, 3667 buf, test->expectedlen, &addtl); 3668 } 3669 if (ret < 0) { 3670 printk(KERN_ERR "alg: drbg: could not obtain random data for " 3671 "driver %s\n", driver); 3672 goto outbuf; 3673 } 3674 3675 drbg_string_fill(&addtl, test->addtlb, test->addtllen); 3676 if (pr) { 3677 drbg_string_fill(&testentropy, test->entprb, test->entprlen); 3678 ret = crypto_drbg_get_bytes_addtl_test(drng, 3679 buf, test->expectedlen, &addtl, &test_data); 3680 } else { 3681 ret = crypto_drbg_get_bytes_addtl(drng, 3682 buf, test->expectedlen, &addtl); 3683 } 3684 if (ret < 0) { 3685 printk(KERN_ERR "alg: drbg: could not obtain random data for " 3686 "driver %s\n", driver); 3687 goto outbuf; 3688 } 3689 3690 ret = memcmp(test->expected, buf, test->expectedlen); 3691 3692 outbuf: 3693 crypto_free_rng(drng); 3694 kfree_sensitive(buf); 3695 return ret; 3696 } 3697 3698 3699 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver, 3700 u32 type, u32 mask) 3701 { 3702 int err = 0; 3703 int pr = 0; 3704 int i = 0; 3705 const struct drbg_testvec *template = desc->suite.drbg.vecs; 3706 unsigned int tcount = desc->suite.drbg.count; 3707 3708 if (0 == memcmp(driver, "drbg_pr_", 8)) 3709 pr = 1; 3710 3711 for (i = 0; i < tcount; i++) { 3712 err = drbg_cavs_test(&template[i], pr, driver, type, mask); 3713 if (err) { 3714 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n", 3715 i, driver); 3716 err = -EINVAL; 3717 break; 3718 } 3719 } 3720 return err; 3721 3722 } 3723 3724 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec, 3725 const char *alg) 3726 { 3727 struct kpp_request *req; 3728 void *input_buf = NULL; 3729 void *output_buf = NULL; 3730 void *a_public = NULL; 3731 void *a_ss = NULL; 3732 void *shared_secret = NULL; 3733 struct crypto_wait wait; 3734 unsigned int out_len_max; 3735 int err = -ENOMEM; 3736 struct scatterlist src, dst; 3737 3738 req = kpp_request_alloc(tfm, GFP_KERNEL); 3739 if (!req) 3740 return err; 3741 3742 crypto_init_wait(&wait); 3743 3744 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size); 3745 if (err < 0) 3746 goto free_req; 3747 3748 out_len_max = crypto_kpp_maxsize(tfm); 3749 output_buf = kzalloc(out_len_max, GFP_KERNEL); 3750 if (!output_buf) { 3751 err = -ENOMEM; 3752 goto free_req; 3753 } 3754 3755 /* Use appropriate parameter as base */ 3756 kpp_request_set_input(req, NULL, 0); 3757 sg_init_one(&dst, output_buf, out_len_max); 3758 kpp_request_set_output(req, &dst, out_len_max); 3759 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3760 crypto_req_done, &wait); 3761 3762 /* Compute party A's public key */ 3763 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait); 3764 if (err) { 3765 pr_err("alg: %s: Party A: generate public key test failed. err %d\n", 3766 alg, err); 3767 goto free_output; 3768 } 3769 3770 if (vec->genkey) { 3771 /* Save party A's public key */ 3772 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL); 3773 if (!a_public) { 3774 err = -ENOMEM; 3775 goto free_output; 3776 } 3777 } else { 3778 /* Verify calculated public key */ 3779 if (memcmp(vec->expected_a_public, sg_virt(req->dst), 3780 vec->expected_a_public_size)) { 3781 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n", 3782 alg); 3783 err = -EINVAL; 3784 goto free_output; 3785 } 3786 } 3787 3788 /* Calculate shared secret key by using counter part (b) public key. */ 3789 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL); 3790 if (!input_buf) { 3791 err = -ENOMEM; 3792 goto free_output; 3793 } 3794 3795 sg_init_one(&src, input_buf, vec->b_public_size); 3796 sg_init_one(&dst, output_buf, out_len_max); 3797 kpp_request_set_input(req, &src, vec->b_public_size); 3798 kpp_request_set_output(req, &dst, out_len_max); 3799 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3800 crypto_req_done, &wait); 3801 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait); 3802 if (err) { 3803 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n", 3804 alg, err); 3805 goto free_all; 3806 } 3807 3808 if (vec->genkey) { 3809 /* Save the shared secret obtained by party A */ 3810 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL); 3811 if (!a_ss) { 3812 err = -ENOMEM; 3813 goto free_all; 3814 } 3815 3816 /* 3817 * Calculate party B's shared secret by using party A's 3818 * public key. 3819 */ 3820 err = crypto_kpp_set_secret(tfm, vec->b_secret, 3821 vec->b_secret_size); 3822 if (err < 0) 3823 goto free_all; 3824 3825 sg_init_one(&src, a_public, vec->expected_a_public_size); 3826 sg_init_one(&dst, output_buf, out_len_max); 3827 kpp_request_set_input(req, &src, vec->expected_a_public_size); 3828 kpp_request_set_output(req, &dst, out_len_max); 3829 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3830 crypto_req_done, &wait); 3831 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), 3832 &wait); 3833 if (err) { 3834 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n", 3835 alg, err); 3836 goto free_all; 3837 } 3838 3839 shared_secret = a_ss; 3840 } else { 3841 shared_secret = (void *)vec->expected_ss; 3842 } 3843 3844 /* 3845 * verify shared secret from which the user will derive 3846 * secret key by executing whatever hash it has chosen 3847 */ 3848 if (memcmp(shared_secret, sg_virt(req->dst), 3849 vec->expected_ss_size)) { 3850 pr_err("alg: %s: compute shared secret test failed. Invalid output\n", 3851 alg); 3852 err = -EINVAL; 3853 } 3854 3855 free_all: 3856 kfree(a_ss); 3857 kfree(input_buf); 3858 free_output: 3859 kfree(a_public); 3860 kfree(output_buf); 3861 free_req: 3862 kpp_request_free(req); 3863 return err; 3864 } 3865 3866 static int test_kpp(struct crypto_kpp *tfm, const char *alg, 3867 const struct kpp_testvec *vecs, unsigned int tcount) 3868 { 3869 int ret, i; 3870 3871 for (i = 0; i < tcount; i++) { 3872 ret = do_test_kpp(tfm, vecs++, alg); 3873 if (ret) { 3874 pr_err("alg: %s: test failed on vector %d, err=%d\n", 3875 alg, i + 1, ret); 3876 return ret; 3877 } 3878 } 3879 return 0; 3880 } 3881 3882 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver, 3883 u32 type, u32 mask) 3884 { 3885 struct crypto_kpp *tfm; 3886 int err = 0; 3887 3888 tfm = crypto_alloc_kpp(driver, type, mask); 3889 if (IS_ERR(tfm)) { 3890 if (PTR_ERR(tfm) == -ENOENT) 3891 return 0; 3892 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n", 3893 driver, PTR_ERR(tfm)); 3894 return PTR_ERR(tfm); 3895 } 3896 if (desc->suite.kpp.vecs) 3897 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs, 3898 desc->suite.kpp.count); 3899 3900 crypto_free_kpp(tfm); 3901 return err; 3902 } 3903 3904 static u8 *test_pack_u32(u8 *dst, u32 val) 3905 { 3906 memcpy(dst, &val, sizeof(val)); 3907 return dst + sizeof(val); 3908 } 3909 3910 static int test_akcipher_one(struct crypto_akcipher *tfm, 3911 const struct akcipher_testvec *vecs) 3912 { 3913 char *xbuf[XBUFSIZE]; 3914 struct akcipher_request *req; 3915 void *outbuf_enc = NULL; 3916 void *outbuf_dec = NULL; 3917 struct crypto_wait wait; 3918 unsigned int out_len_max, out_len = 0; 3919 int err = -ENOMEM; 3920 struct scatterlist src, dst, src_tab[2]; 3921 const char *c; 3922 unsigned int c_size; 3923 3924 if (testmgr_alloc_buf(xbuf)) 3925 return err; 3926 3927 req = akcipher_request_alloc(tfm, GFP_KERNEL); 3928 if (!req) 3929 goto free_xbuf; 3930 3931 crypto_init_wait(&wait); 3932 3933 if (vecs->public_key_vec) 3934 err = crypto_akcipher_set_pub_key(tfm, vecs->key, 3935 vecs->key_len); 3936 else 3937 err = crypto_akcipher_set_priv_key(tfm, vecs->key, 3938 vecs->key_len); 3939 if (err) 3940 goto free_req; 3941 3942 /* First run encrypt test which does not require a private key */ 3943 err = -ENOMEM; 3944 out_len_max = crypto_akcipher_maxsize(tfm); 3945 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL); 3946 if (!outbuf_enc) 3947 goto free_req; 3948 3949 c = vecs->c; 3950 c_size = vecs->c_size; 3951 3952 err = -E2BIG; 3953 if (WARN_ON(vecs->m_size > PAGE_SIZE)) 3954 goto free_all; 3955 memcpy(xbuf[0], vecs->m, vecs->m_size); 3956 3957 sg_init_table(src_tab, 2); 3958 sg_set_buf(&src_tab[0], xbuf[0], 8); 3959 sg_set_buf(&src_tab[1], xbuf[0] + 8, vecs->m_size - 8); 3960 sg_init_one(&dst, outbuf_enc, out_len_max); 3961 akcipher_request_set_crypt(req, src_tab, &dst, vecs->m_size, 3962 out_len_max); 3963 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 3964 crypto_req_done, &wait); 3965 3966 err = crypto_wait_req(crypto_akcipher_encrypt(req), &wait); 3967 if (err) { 3968 pr_err("alg: akcipher: encrypt test failed. err %d\n", err); 3969 goto free_all; 3970 } 3971 if (c) { 3972 if (req->dst_len != c_size) { 3973 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n"); 3974 err = -EINVAL; 3975 goto free_all; 3976 } 3977 /* verify that encrypted message is equal to expected */ 3978 if (memcmp(c, outbuf_enc, c_size) != 0) { 3979 pr_err("alg: akcipher: encrypt test failed. Invalid output\n"); 3980 hexdump(outbuf_enc, c_size); 3981 err = -EINVAL; 3982 goto free_all; 3983 } 3984 } 3985 3986 /* 3987 * Don't invoke decrypt test which requires a private key 3988 * for vectors with only a public key. 3989 */ 3990 if (vecs->public_key_vec) { 3991 err = 0; 3992 goto free_all; 3993 } 3994 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL); 3995 if (!outbuf_dec) { 3996 err = -ENOMEM; 3997 goto free_all; 3998 } 3999 4000 if (!c) { 4001 c = outbuf_enc; 4002 c_size = req->dst_len; 4003 } 4004 4005 err = -E2BIG; 4006 if (WARN_ON(c_size > PAGE_SIZE)) 4007 goto free_all; 4008 memcpy(xbuf[0], c, c_size); 4009 4010 sg_init_one(&src, xbuf[0], c_size); 4011 sg_init_one(&dst, outbuf_dec, out_len_max); 4012 crypto_init_wait(&wait); 4013 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max); 4014 4015 err = crypto_wait_req(crypto_akcipher_decrypt(req), &wait); 4016 if (err) { 4017 pr_err("alg: akcipher: decrypt test failed. err %d\n", err); 4018 goto free_all; 4019 } 4020 out_len = req->dst_len; 4021 if (out_len < vecs->m_size) { 4022 pr_err("alg: akcipher: decrypt test failed. Invalid output len %u\n", 4023 out_len); 4024 err = -EINVAL; 4025 goto free_all; 4026 } 4027 /* verify that decrypted message is equal to the original msg */ 4028 if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) || 4029 memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size, 4030 vecs->m_size)) { 4031 pr_err("alg: akcipher: decrypt test failed. Invalid output\n"); 4032 hexdump(outbuf_dec, out_len); 4033 err = -EINVAL; 4034 } 4035 free_all: 4036 kfree(outbuf_dec); 4037 kfree(outbuf_enc); 4038 free_req: 4039 akcipher_request_free(req); 4040 free_xbuf: 4041 testmgr_free_buf(xbuf); 4042 return err; 4043 } 4044 4045 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg, 4046 const struct akcipher_testvec *vecs, 4047 unsigned int tcount) 4048 { 4049 const char *algo = 4050 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm)); 4051 int ret, i; 4052 4053 for (i = 0; i < tcount; i++) { 4054 ret = test_akcipher_one(tfm, vecs++); 4055 if (!ret) 4056 continue; 4057 4058 pr_err("alg: akcipher: test %d failed for %s, err=%d\n", 4059 i + 1, algo, ret); 4060 return ret; 4061 } 4062 return 0; 4063 } 4064 4065 static int alg_test_akcipher(const struct alg_test_desc *desc, 4066 const char *driver, u32 type, u32 mask) 4067 { 4068 struct crypto_akcipher *tfm; 4069 int err = 0; 4070 4071 tfm = crypto_alloc_akcipher(driver, type, mask); 4072 if (IS_ERR(tfm)) { 4073 if (PTR_ERR(tfm) == -ENOENT) 4074 return 0; 4075 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n", 4076 driver, PTR_ERR(tfm)); 4077 return PTR_ERR(tfm); 4078 } 4079 if (desc->suite.akcipher.vecs) 4080 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs, 4081 desc->suite.akcipher.count); 4082 4083 crypto_free_akcipher(tfm); 4084 return err; 4085 } 4086 4087 static int test_sig_one(struct crypto_sig *tfm, const struct sig_testvec *vecs) 4088 { 4089 u8 *ptr, *key __free(kfree); 4090 int err, sig_size; 4091 4092 key = kmalloc(vecs->key_len + 2 * sizeof(u32) + vecs->param_len, 4093 GFP_KERNEL); 4094 if (!key) 4095 return -ENOMEM; 4096 4097 /* ecrdsa expects additional parameters appended to the key */ 4098 memcpy(key, vecs->key, vecs->key_len); 4099 ptr = key + vecs->key_len; 4100 ptr = test_pack_u32(ptr, vecs->algo); 4101 ptr = test_pack_u32(ptr, vecs->param_len); 4102 memcpy(ptr, vecs->params, vecs->param_len); 4103 4104 if (vecs->public_key_vec) 4105 err = crypto_sig_set_pubkey(tfm, key, vecs->key_len); 4106 else 4107 err = crypto_sig_set_privkey(tfm, key, vecs->key_len); 4108 if (err) 4109 return err; 4110 4111 /* 4112 * Run asymmetric signature verification first 4113 * (which does not require a private key) 4114 */ 4115 err = crypto_sig_verify(tfm, vecs->c, vecs->c_size, 4116 vecs->m, vecs->m_size); 4117 if (err) { 4118 pr_err("alg: sig: verify test failed: err %d\n", err); 4119 return err; 4120 } 4121 4122 /* 4123 * Don't invoke sign test (which requires a private key) 4124 * for vectors with only a public key. 4125 */ 4126 if (vecs->public_key_vec) 4127 return 0; 4128 4129 sig_size = crypto_sig_maxsize(tfm); 4130 if (sig_size < vecs->c_size) { 4131 pr_err("alg: sig: invalid maxsize %u\n", sig_size); 4132 return -EINVAL; 4133 } 4134 4135 u8 *sig __free(kfree) = kzalloc(sig_size, GFP_KERNEL); 4136 if (!sig) 4137 return -ENOMEM; 4138 4139 /* Run asymmetric signature generation */ 4140 err = crypto_sig_sign(tfm, vecs->m, vecs->m_size, sig, sig_size); 4141 if (err < 0) { 4142 pr_err("alg: sig: sign test failed: err %d\n", err); 4143 return err; 4144 } 4145 4146 /* Verify that generated signature equals cooked signature */ 4147 if (err != vecs->c_size || 4148 memcmp(sig, vecs->c, vecs->c_size) || 4149 memchr_inv(sig + vecs->c_size, 0, sig_size - vecs->c_size)) { 4150 pr_err("alg: sig: sign test failed: invalid output\n"); 4151 hexdump(sig, sig_size); 4152 return -EINVAL; 4153 } 4154 4155 return 0; 4156 } 4157 4158 static int test_sig(struct crypto_sig *tfm, const char *alg, 4159 const struct sig_testvec *vecs, unsigned int tcount) 4160 { 4161 const char *algo = crypto_tfm_alg_driver_name(crypto_sig_tfm(tfm)); 4162 int ret, i; 4163 4164 for (i = 0; i < tcount; i++) { 4165 ret = test_sig_one(tfm, vecs++); 4166 if (ret) { 4167 pr_err("alg: sig: test %d failed for %s: err %d\n", 4168 i + 1, algo, ret); 4169 return ret; 4170 } 4171 } 4172 return 0; 4173 } 4174 4175 static int alg_test_sig(const struct alg_test_desc *desc, const char *driver, 4176 u32 type, u32 mask) 4177 { 4178 struct crypto_sig *tfm; 4179 int err = 0; 4180 4181 tfm = crypto_alloc_sig(driver, type, mask); 4182 if (IS_ERR(tfm)) { 4183 pr_err("alg: sig: Failed to load tfm for %s: %ld\n", 4184 driver, PTR_ERR(tfm)); 4185 return PTR_ERR(tfm); 4186 } 4187 if (desc->suite.sig.vecs) 4188 err = test_sig(tfm, desc->alg, desc->suite.sig.vecs, 4189 desc->suite.sig.count); 4190 4191 crypto_free_sig(tfm); 4192 return err; 4193 } 4194 4195 static int alg_test_null(const struct alg_test_desc *desc, 4196 const char *driver, u32 type, u32 mask) 4197 { 4198 return 0; 4199 } 4200 4201 #define ____VECS(tv) .vecs = tv, .count = ARRAY_SIZE(tv) 4202 #define __VECS(tv) { ____VECS(tv) } 4203 4204 /* Please keep this list sorted by algorithm name. */ 4205 static const struct alg_test_desc alg_test_descs[] = { 4206 { 4207 .alg = "adiantum(xchacha12,aes)", 4208 .generic_driver = "adiantum(xchacha12-generic,aes-generic,nhpoly1305-generic)", 4209 .test = alg_test_skcipher, 4210 .suite = { 4211 .cipher = __VECS(adiantum_xchacha12_aes_tv_template) 4212 }, 4213 }, { 4214 .alg = "adiantum(xchacha20,aes)", 4215 .generic_driver = "adiantum(xchacha20-generic,aes-generic,nhpoly1305-generic)", 4216 .test = alg_test_skcipher, 4217 .suite = { 4218 .cipher = __VECS(adiantum_xchacha20_aes_tv_template) 4219 }, 4220 }, { 4221 .alg = "aegis128", 4222 .test = alg_test_aead, 4223 .suite = { 4224 .aead = __VECS(aegis128_tv_template) 4225 } 4226 }, { 4227 .alg = "ansi_cprng", 4228 .test = alg_test_cprng, 4229 .suite = { 4230 .cprng = __VECS(ansi_cprng_aes_tv_template) 4231 } 4232 }, { 4233 .alg = "authenc(hmac(md5),ecb(cipher_null))", 4234 .test = alg_test_aead, 4235 .suite = { 4236 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template) 4237 } 4238 }, { 4239 .alg = "authenc(hmac(sha1),cbc(aes))", 4240 .test = alg_test_aead, 4241 .fips_allowed = 1, 4242 .suite = { 4243 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp) 4244 } 4245 }, { 4246 .alg = "authenc(hmac(sha1),cbc(des))", 4247 .test = alg_test_aead, 4248 .suite = { 4249 .aead = __VECS(hmac_sha1_des_cbc_tv_temp) 4250 } 4251 }, { 4252 .alg = "authenc(hmac(sha1),cbc(des3_ede))", 4253 .test = alg_test_aead, 4254 .suite = { 4255 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp) 4256 } 4257 }, { 4258 .alg = "authenc(hmac(sha1),ctr(aes))", 4259 .test = alg_test_null, 4260 .fips_allowed = 1, 4261 }, { 4262 .alg = "authenc(hmac(sha1),ecb(cipher_null))", 4263 .test = alg_test_aead, 4264 .suite = { 4265 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp) 4266 } 4267 }, { 4268 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))", 4269 .test = alg_test_null, 4270 .fips_allowed = 1, 4271 }, { 4272 .alg = "authenc(hmac(sha224),cbc(des))", 4273 .test = alg_test_aead, 4274 .suite = { 4275 .aead = __VECS(hmac_sha224_des_cbc_tv_temp) 4276 } 4277 }, { 4278 .alg = "authenc(hmac(sha224),cbc(des3_ede))", 4279 .test = alg_test_aead, 4280 .suite = { 4281 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp) 4282 } 4283 }, { 4284 .alg = "authenc(hmac(sha256),cbc(aes))", 4285 .test = alg_test_aead, 4286 .fips_allowed = 1, 4287 .suite = { 4288 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp) 4289 } 4290 }, { 4291 .alg = "authenc(hmac(sha256),cbc(des))", 4292 .test = alg_test_aead, 4293 .suite = { 4294 .aead = __VECS(hmac_sha256_des_cbc_tv_temp) 4295 } 4296 }, { 4297 .alg = "authenc(hmac(sha256),cbc(des3_ede))", 4298 .test = alg_test_aead, 4299 .suite = { 4300 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp) 4301 } 4302 }, { 4303 .alg = "authenc(hmac(sha256),ctr(aes))", 4304 .test = alg_test_null, 4305 .fips_allowed = 1, 4306 }, { 4307 .alg = "authenc(hmac(sha256),cts(cbc(aes)))", 4308 .test = alg_test_aead, 4309 .suite = { 4310 .aead = __VECS(krb5_test_aes128_cts_hmac_sha256_128) 4311 } 4312 }, { 4313 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))", 4314 .test = alg_test_null, 4315 .fips_allowed = 1, 4316 }, { 4317 .alg = "authenc(hmac(sha384),cbc(des))", 4318 .test = alg_test_aead, 4319 .suite = { 4320 .aead = __VECS(hmac_sha384_des_cbc_tv_temp) 4321 } 4322 }, { 4323 .alg = "authenc(hmac(sha384),cbc(des3_ede))", 4324 .test = alg_test_aead, 4325 .suite = { 4326 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp) 4327 } 4328 }, { 4329 .alg = "authenc(hmac(sha384),ctr(aes))", 4330 .test = alg_test_null, 4331 .fips_allowed = 1, 4332 }, { 4333 .alg = "authenc(hmac(sha384),cts(cbc(aes)))", 4334 .test = alg_test_aead, 4335 .suite = { 4336 .aead = __VECS(krb5_test_aes256_cts_hmac_sha384_192) 4337 } 4338 }, { 4339 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))", 4340 .test = alg_test_null, 4341 .fips_allowed = 1, 4342 }, { 4343 .alg = "authenc(hmac(sha512),cbc(aes))", 4344 .fips_allowed = 1, 4345 .test = alg_test_aead, 4346 .suite = { 4347 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp) 4348 } 4349 }, { 4350 .alg = "authenc(hmac(sha512),cbc(des))", 4351 .test = alg_test_aead, 4352 .suite = { 4353 .aead = __VECS(hmac_sha512_des_cbc_tv_temp) 4354 } 4355 }, { 4356 .alg = "authenc(hmac(sha512),cbc(des3_ede))", 4357 .test = alg_test_aead, 4358 .suite = { 4359 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp) 4360 } 4361 }, { 4362 .alg = "authenc(hmac(sha512),ctr(aes))", 4363 .test = alg_test_null, 4364 .fips_allowed = 1, 4365 }, { 4366 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))", 4367 .test = alg_test_null, 4368 .fips_allowed = 1, 4369 }, { 4370 .alg = "blake2b-160", 4371 .test = alg_test_hash, 4372 .fips_allowed = 0, 4373 .suite = { 4374 .hash = __VECS(blake2b_160_tv_template) 4375 } 4376 }, { 4377 .alg = "blake2b-256", 4378 .test = alg_test_hash, 4379 .fips_allowed = 0, 4380 .suite = { 4381 .hash = __VECS(blake2b_256_tv_template) 4382 } 4383 }, { 4384 .alg = "blake2b-384", 4385 .test = alg_test_hash, 4386 .fips_allowed = 0, 4387 .suite = { 4388 .hash = __VECS(blake2b_384_tv_template) 4389 } 4390 }, { 4391 .alg = "blake2b-512", 4392 .test = alg_test_hash, 4393 .fips_allowed = 0, 4394 .suite = { 4395 .hash = __VECS(blake2b_512_tv_template) 4396 } 4397 }, { 4398 .alg = "cbc(aes)", 4399 .test = alg_test_skcipher, 4400 .fips_allowed = 1, 4401 .suite = { 4402 .cipher = __VECS(aes_cbc_tv_template) 4403 }, 4404 }, { 4405 .alg = "cbc(anubis)", 4406 .test = alg_test_skcipher, 4407 .suite = { 4408 .cipher = __VECS(anubis_cbc_tv_template) 4409 }, 4410 }, { 4411 .alg = "cbc(aria)", 4412 .test = alg_test_skcipher, 4413 .suite = { 4414 .cipher = __VECS(aria_cbc_tv_template) 4415 }, 4416 }, { 4417 .alg = "cbc(blowfish)", 4418 .test = alg_test_skcipher, 4419 .suite = { 4420 .cipher = __VECS(bf_cbc_tv_template) 4421 }, 4422 }, { 4423 .alg = "cbc(camellia)", 4424 .test = alg_test_skcipher, 4425 .suite = { 4426 .cipher = __VECS(camellia_cbc_tv_template) 4427 }, 4428 }, { 4429 .alg = "cbc(cast5)", 4430 .test = alg_test_skcipher, 4431 .suite = { 4432 .cipher = __VECS(cast5_cbc_tv_template) 4433 }, 4434 }, { 4435 .alg = "cbc(cast6)", 4436 .test = alg_test_skcipher, 4437 .suite = { 4438 .cipher = __VECS(cast6_cbc_tv_template) 4439 }, 4440 }, { 4441 .alg = "cbc(des)", 4442 .test = alg_test_skcipher, 4443 .suite = { 4444 .cipher = __VECS(des_cbc_tv_template) 4445 }, 4446 }, { 4447 .alg = "cbc(des3_ede)", 4448 .test = alg_test_skcipher, 4449 .suite = { 4450 .cipher = __VECS(des3_ede_cbc_tv_template) 4451 }, 4452 }, { 4453 /* Same as cbc(aes) except the key is stored in 4454 * hardware secure memory which we reference by index 4455 */ 4456 .alg = "cbc(paes)", 4457 .test = alg_test_null, 4458 .fips_allowed = 1, 4459 }, { 4460 /* Same as cbc(sm4) except the key is stored in 4461 * hardware secure memory which we reference by index 4462 */ 4463 .alg = "cbc(psm4)", 4464 .test = alg_test_null, 4465 }, { 4466 .alg = "cbc(serpent)", 4467 .test = alg_test_skcipher, 4468 .suite = { 4469 .cipher = __VECS(serpent_cbc_tv_template) 4470 }, 4471 }, { 4472 .alg = "cbc(sm4)", 4473 .test = alg_test_skcipher, 4474 .suite = { 4475 .cipher = __VECS(sm4_cbc_tv_template) 4476 } 4477 }, { 4478 .alg = "cbc(twofish)", 4479 .test = alg_test_skcipher, 4480 .suite = { 4481 .cipher = __VECS(tf_cbc_tv_template) 4482 }, 4483 }, { 4484 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390) 4485 .alg = "cbc-paes-s390", 4486 .fips_allowed = 1, 4487 .test = alg_test_skcipher, 4488 .suite = { 4489 .cipher = __VECS(aes_cbc_tv_template) 4490 } 4491 }, { 4492 #endif 4493 .alg = "cbcmac(aes)", 4494 .test = alg_test_hash, 4495 .suite = { 4496 .hash = __VECS(aes_cbcmac_tv_template) 4497 } 4498 }, { 4499 .alg = "cbcmac(sm4)", 4500 .test = alg_test_hash, 4501 .suite = { 4502 .hash = __VECS(sm4_cbcmac_tv_template) 4503 } 4504 }, { 4505 .alg = "ccm(aes)", 4506 .generic_driver = "ccm_base(ctr(aes-generic),cbcmac(aes-generic))", 4507 .test = alg_test_aead, 4508 .fips_allowed = 1, 4509 .suite = { 4510 .aead = { 4511 ____VECS(aes_ccm_tv_template), 4512 .einval_allowed = 1, 4513 } 4514 } 4515 }, { 4516 .alg = "ccm(sm4)", 4517 .generic_driver = "ccm_base(ctr(sm4-generic),cbcmac(sm4-generic))", 4518 .test = alg_test_aead, 4519 .suite = { 4520 .aead = { 4521 ____VECS(sm4_ccm_tv_template), 4522 .einval_allowed = 1, 4523 } 4524 } 4525 }, { 4526 .alg = "chacha20", 4527 .test = alg_test_skcipher, 4528 .suite = { 4529 .cipher = __VECS(chacha20_tv_template) 4530 }, 4531 }, { 4532 .alg = "cmac(aes)", 4533 .fips_allowed = 1, 4534 .test = alg_test_hash, 4535 .suite = { 4536 .hash = __VECS(aes_cmac128_tv_template) 4537 } 4538 }, { 4539 .alg = "cmac(camellia)", 4540 .test = alg_test_hash, 4541 .suite = { 4542 .hash = __VECS(camellia_cmac128_tv_template) 4543 } 4544 }, { 4545 .alg = "cmac(des3_ede)", 4546 .test = alg_test_hash, 4547 .suite = { 4548 .hash = __VECS(des3_ede_cmac64_tv_template) 4549 } 4550 }, { 4551 .alg = "cmac(sm4)", 4552 .test = alg_test_hash, 4553 .suite = { 4554 .hash = __VECS(sm4_cmac128_tv_template) 4555 } 4556 }, { 4557 .alg = "crc32", 4558 .test = alg_test_hash, 4559 .fips_allowed = 1, 4560 .suite = { 4561 .hash = __VECS(crc32_tv_template) 4562 } 4563 }, { 4564 .alg = "crc32c", 4565 .test = alg_test_crc32c, 4566 .fips_allowed = 1, 4567 .suite = { 4568 .hash = __VECS(crc32c_tv_template) 4569 } 4570 }, { 4571 .alg = "ctr(aes)", 4572 .test = alg_test_skcipher, 4573 .fips_allowed = 1, 4574 .suite = { 4575 .cipher = __VECS(aes_ctr_tv_template) 4576 } 4577 }, { 4578 .alg = "ctr(aria)", 4579 .test = alg_test_skcipher, 4580 .suite = { 4581 .cipher = __VECS(aria_ctr_tv_template) 4582 } 4583 }, { 4584 .alg = "ctr(blowfish)", 4585 .test = alg_test_skcipher, 4586 .suite = { 4587 .cipher = __VECS(bf_ctr_tv_template) 4588 } 4589 }, { 4590 .alg = "ctr(camellia)", 4591 .test = alg_test_skcipher, 4592 .suite = { 4593 .cipher = __VECS(camellia_ctr_tv_template) 4594 } 4595 }, { 4596 .alg = "ctr(cast5)", 4597 .test = alg_test_skcipher, 4598 .suite = { 4599 .cipher = __VECS(cast5_ctr_tv_template) 4600 } 4601 }, { 4602 .alg = "ctr(cast6)", 4603 .test = alg_test_skcipher, 4604 .suite = { 4605 .cipher = __VECS(cast6_ctr_tv_template) 4606 } 4607 }, { 4608 .alg = "ctr(des)", 4609 .test = alg_test_skcipher, 4610 .suite = { 4611 .cipher = __VECS(des_ctr_tv_template) 4612 } 4613 }, { 4614 .alg = "ctr(des3_ede)", 4615 .test = alg_test_skcipher, 4616 .suite = { 4617 .cipher = __VECS(des3_ede_ctr_tv_template) 4618 } 4619 }, { 4620 /* Same as ctr(aes) except the key is stored in 4621 * hardware secure memory which we reference by index 4622 */ 4623 .alg = "ctr(paes)", 4624 .test = alg_test_null, 4625 .fips_allowed = 1, 4626 }, { 4627 4628 /* Same as ctr(sm4) except the key is stored in 4629 * hardware secure memory which we reference by index 4630 */ 4631 .alg = "ctr(psm4)", 4632 .test = alg_test_null, 4633 }, { 4634 .alg = "ctr(serpent)", 4635 .test = alg_test_skcipher, 4636 .suite = { 4637 .cipher = __VECS(serpent_ctr_tv_template) 4638 } 4639 }, { 4640 .alg = "ctr(sm4)", 4641 .test = alg_test_skcipher, 4642 .suite = { 4643 .cipher = __VECS(sm4_ctr_tv_template) 4644 } 4645 }, { 4646 .alg = "ctr(twofish)", 4647 .test = alg_test_skcipher, 4648 .suite = { 4649 .cipher = __VECS(tf_ctr_tv_template) 4650 } 4651 }, { 4652 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390) 4653 .alg = "ctr-paes-s390", 4654 .fips_allowed = 1, 4655 .test = alg_test_skcipher, 4656 .suite = { 4657 .cipher = __VECS(aes_ctr_tv_template) 4658 } 4659 }, { 4660 #endif 4661 .alg = "cts(cbc(aes))", 4662 .test = alg_test_skcipher, 4663 .fips_allowed = 1, 4664 .suite = { 4665 .cipher = __VECS(cts_mode_tv_template) 4666 } 4667 }, { 4668 /* Same as cts(cbc((aes)) except the key is stored in 4669 * hardware secure memory which we reference by index 4670 */ 4671 .alg = "cts(cbc(paes))", 4672 .test = alg_test_null, 4673 .fips_allowed = 1, 4674 }, { 4675 .alg = "cts(cbc(sm4))", 4676 .test = alg_test_skcipher, 4677 .suite = { 4678 .cipher = __VECS(sm4_cts_tv_template) 4679 } 4680 }, { 4681 .alg = "curve25519", 4682 .test = alg_test_kpp, 4683 .suite = { 4684 .kpp = __VECS(curve25519_tv_template) 4685 } 4686 }, { 4687 .alg = "deflate", 4688 .test = alg_test_comp, 4689 .fips_allowed = 1, 4690 .suite = { 4691 .comp = { 4692 .comp = __VECS(deflate_comp_tv_template), 4693 .decomp = __VECS(deflate_decomp_tv_template) 4694 } 4695 } 4696 }, { 4697 .alg = "deflate-iaa", 4698 .test = alg_test_comp, 4699 .fips_allowed = 1, 4700 .suite = { 4701 .comp = { 4702 .comp = __VECS(deflate_comp_tv_template), 4703 .decomp = __VECS(deflate_decomp_tv_template) 4704 } 4705 } 4706 }, { 4707 .alg = "dh", 4708 .test = alg_test_kpp, 4709 .suite = { 4710 .kpp = __VECS(dh_tv_template) 4711 } 4712 }, { 4713 .alg = "digest_null", 4714 .test = alg_test_null, 4715 }, { 4716 .alg = "drbg_nopr_ctr_aes128", 4717 .test = alg_test_drbg, 4718 .fips_allowed = 1, 4719 .suite = { 4720 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template) 4721 } 4722 }, { 4723 .alg = "drbg_nopr_ctr_aes192", 4724 .test = alg_test_drbg, 4725 .fips_allowed = 1, 4726 .suite = { 4727 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template) 4728 } 4729 }, { 4730 .alg = "drbg_nopr_ctr_aes256", 4731 .test = alg_test_drbg, 4732 .fips_allowed = 1, 4733 .suite = { 4734 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template) 4735 } 4736 }, { 4737 .alg = "drbg_nopr_hmac_sha256", 4738 .test = alg_test_drbg, 4739 .fips_allowed = 1, 4740 .suite = { 4741 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template) 4742 } 4743 }, { 4744 /* 4745 * There is no need to specifically test the DRBG with every 4746 * backend cipher -- covered by drbg_nopr_hmac_sha512 test 4747 */ 4748 .alg = "drbg_nopr_hmac_sha384", 4749 .test = alg_test_null, 4750 }, { 4751 .alg = "drbg_nopr_hmac_sha512", 4752 .test = alg_test_drbg, 4753 .fips_allowed = 1, 4754 .suite = { 4755 .drbg = __VECS(drbg_nopr_hmac_sha512_tv_template) 4756 } 4757 }, { 4758 .alg = "drbg_nopr_sha256", 4759 .test = alg_test_drbg, 4760 .fips_allowed = 1, 4761 .suite = { 4762 .drbg = __VECS(drbg_nopr_sha256_tv_template) 4763 } 4764 }, { 4765 /* covered by drbg_nopr_sha256 test */ 4766 .alg = "drbg_nopr_sha384", 4767 .test = alg_test_null, 4768 }, { 4769 .alg = "drbg_nopr_sha512", 4770 .fips_allowed = 1, 4771 .test = alg_test_null, 4772 }, { 4773 .alg = "drbg_pr_ctr_aes128", 4774 .test = alg_test_drbg, 4775 .fips_allowed = 1, 4776 .suite = { 4777 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template) 4778 } 4779 }, { 4780 /* covered by drbg_pr_ctr_aes128 test */ 4781 .alg = "drbg_pr_ctr_aes192", 4782 .fips_allowed = 1, 4783 .test = alg_test_null, 4784 }, { 4785 .alg = "drbg_pr_ctr_aes256", 4786 .fips_allowed = 1, 4787 .test = alg_test_null, 4788 }, { 4789 .alg = "drbg_pr_hmac_sha256", 4790 .test = alg_test_drbg, 4791 .fips_allowed = 1, 4792 .suite = { 4793 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template) 4794 } 4795 }, { 4796 /* covered by drbg_pr_hmac_sha256 test */ 4797 .alg = "drbg_pr_hmac_sha384", 4798 .test = alg_test_null, 4799 }, { 4800 .alg = "drbg_pr_hmac_sha512", 4801 .test = alg_test_null, 4802 .fips_allowed = 1, 4803 }, { 4804 .alg = "drbg_pr_sha256", 4805 .test = alg_test_drbg, 4806 .fips_allowed = 1, 4807 .suite = { 4808 .drbg = __VECS(drbg_pr_sha256_tv_template) 4809 } 4810 }, { 4811 /* covered by drbg_pr_sha256 test */ 4812 .alg = "drbg_pr_sha384", 4813 .test = alg_test_null, 4814 }, { 4815 .alg = "drbg_pr_sha512", 4816 .fips_allowed = 1, 4817 .test = alg_test_null, 4818 }, { 4819 .alg = "ecb(aes)", 4820 .test = alg_test_skcipher, 4821 .fips_allowed = 1, 4822 .suite = { 4823 .cipher = __VECS(aes_tv_template) 4824 } 4825 }, { 4826 .alg = "ecb(anubis)", 4827 .test = alg_test_skcipher, 4828 .suite = { 4829 .cipher = __VECS(anubis_tv_template) 4830 } 4831 }, { 4832 .alg = "ecb(arc4)", 4833 .generic_driver = "arc4-generic", 4834 .test = alg_test_skcipher, 4835 .suite = { 4836 .cipher = __VECS(arc4_tv_template) 4837 } 4838 }, { 4839 .alg = "ecb(aria)", 4840 .test = alg_test_skcipher, 4841 .suite = { 4842 .cipher = __VECS(aria_tv_template) 4843 } 4844 }, { 4845 .alg = "ecb(blowfish)", 4846 .test = alg_test_skcipher, 4847 .suite = { 4848 .cipher = __VECS(bf_tv_template) 4849 } 4850 }, { 4851 .alg = "ecb(camellia)", 4852 .test = alg_test_skcipher, 4853 .suite = { 4854 .cipher = __VECS(camellia_tv_template) 4855 } 4856 }, { 4857 .alg = "ecb(cast5)", 4858 .test = alg_test_skcipher, 4859 .suite = { 4860 .cipher = __VECS(cast5_tv_template) 4861 } 4862 }, { 4863 .alg = "ecb(cast6)", 4864 .test = alg_test_skcipher, 4865 .suite = { 4866 .cipher = __VECS(cast6_tv_template) 4867 } 4868 }, { 4869 .alg = "ecb(cipher_null)", 4870 .test = alg_test_null, 4871 .fips_allowed = 1, 4872 }, { 4873 .alg = "ecb(des)", 4874 .test = alg_test_skcipher, 4875 .suite = { 4876 .cipher = __VECS(des_tv_template) 4877 } 4878 }, { 4879 .alg = "ecb(des3_ede)", 4880 .test = alg_test_skcipher, 4881 .suite = { 4882 .cipher = __VECS(des3_ede_tv_template) 4883 } 4884 }, { 4885 .alg = "ecb(fcrypt)", 4886 .test = alg_test_skcipher, 4887 .suite = { 4888 .cipher = { 4889 .vecs = fcrypt_pcbc_tv_template, 4890 .count = 1 4891 } 4892 } 4893 }, { 4894 .alg = "ecb(khazad)", 4895 .test = alg_test_skcipher, 4896 .suite = { 4897 .cipher = __VECS(khazad_tv_template) 4898 } 4899 }, { 4900 /* Same as ecb(aes) except the key is stored in 4901 * hardware secure memory which we reference by index 4902 */ 4903 .alg = "ecb(paes)", 4904 .test = alg_test_null, 4905 .fips_allowed = 1, 4906 }, { 4907 .alg = "ecb(seed)", 4908 .test = alg_test_skcipher, 4909 .suite = { 4910 .cipher = __VECS(seed_tv_template) 4911 } 4912 }, { 4913 .alg = "ecb(serpent)", 4914 .test = alg_test_skcipher, 4915 .suite = { 4916 .cipher = __VECS(serpent_tv_template) 4917 } 4918 }, { 4919 .alg = "ecb(sm4)", 4920 .test = alg_test_skcipher, 4921 .suite = { 4922 .cipher = __VECS(sm4_tv_template) 4923 } 4924 }, { 4925 .alg = "ecb(tea)", 4926 .test = alg_test_skcipher, 4927 .suite = { 4928 .cipher = __VECS(tea_tv_template) 4929 } 4930 }, { 4931 .alg = "ecb(twofish)", 4932 .test = alg_test_skcipher, 4933 .suite = { 4934 .cipher = __VECS(tf_tv_template) 4935 } 4936 }, { 4937 .alg = "ecb(xeta)", 4938 .test = alg_test_skcipher, 4939 .suite = { 4940 .cipher = __VECS(xeta_tv_template) 4941 } 4942 }, { 4943 .alg = "ecb(xtea)", 4944 .test = alg_test_skcipher, 4945 .suite = { 4946 .cipher = __VECS(xtea_tv_template) 4947 } 4948 }, { 4949 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390) 4950 .alg = "ecb-paes-s390", 4951 .fips_allowed = 1, 4952 .test = alg_test_skcipher, 4953 .suite = { 4954 .cipher = __VECS(aes_tv_template) 4955 } 4956 }, { 4957 #endif 4958 .alg = "ecdh-nist-p192", 4959 .test = alg_test_kpp, 4960 .suite = { 4961 .kpp = __VECS(ecdh_p192_tv_template) 4962 } 4963 }, { 4964 .alg = "ecdh-nist-p256", 4965 .test = alg_test_kpp, 4966 .fips_allowed = 1, 4967 .suite = { 4968 .kpp = __VECS(ecdh_p256_tv_template) 4969 } 4970 }, { 4971 .alg = "ecdh-nist-p384", 4972 .test = alg_test_kpp, 4973 .fips_allowed = 1, 4974 .suite = { 4975 .kpp = __VECS(ecdh_p384_tv_template) 4976 } 4977 }, { 4978 .alg = "ecdsa-nist-p192", 4979 .test = alg_test_sig, 4980 .suite = { 4981 .sig = __VECS(ecdsa_nist_p192_tv_template) 4982 } 4983 }, { 4984 .alg = "ecdsa-nist-p256", 4985 .test = alg_test_sig, 4986 .fips_allowed = 1, 4987 .suite = { 4988 .sig = __VECS(ecdsa_nist_p256_tv_template) 4989 } 4990 }, { 4991 .alg = "ecdsa-nist-p384", 4992 .test = alg_test_sig, 4993 .fips_allowed = 1, 4994 .suite = { 4995 .sig = __VECS(ecdsa_nist_p384_tv_template) 4996 } 4997 }, { 4998 .alg = "ecdsa-nist-p521", 4999 .test = alg_test_sig, 5000 .fips_allowed = 1, 5001 .suite = { 5002 .sig = __VECS(ecdsa_nist_p521_tv_template) 5003 } 5004 }, { 5005 .alg = "ecrdsa", 5006 .test = alg_test_sig, 5007 .suite = { 5008 .sig = __VECS(ecrdsa_tv_template) 5009 } 5010 }, { 5011 .alg = "essiv(authenc(hmac(sha256),cbc(aes)),sha256)", 5012 .test = alg_test_aead, 5013 .fips_allowed = 1, 5014 .suite = { 5015 .aead = __VECS(essiv_hmac_sha256_aes_cbc_tv_temp) 5016 } 5017 }, { 5018 .alg = "essiv(cbc(aes),sha256)", 5019 .test = alg_test_skcipher, 5020 .fips_allowed = 1, 5021 .suite = { 5022 .cipher = __VECS(essiv_aes_cbc_tv_template) 5023 } 5024 }, { 5025 #if IS_ENABLED(CONFIG_CRYPTO_DH_RFC7919_GROUPS) 5026 .alg = "ffdhe2048(dh)", 5027 .test = alg_test_kpp, 5028 .fips_allowed = 1, 5029 .suite = { 5030 .kpp = __VECS(ffdhe2048_dh_tv_template) 5031 } 5032 }, { 5033 .alg = "ffdhe3072(dh)", 5034 .test = alg_test_kpp, 5035 .fips_allowed = 1, 5036 .suite = { 5037 .kpp = __VECS(ffdhe3072_dh_tv_template) 5038 } 5039 }, { 5040 .alg = "ffdhe4096(dh)", 5041 .test = alg_test_kpp, 5042 .fips_allowed = 1, 5043 .suite = { 5044 .kpp = __VECS(ffdhe4096_dh_tv_template) 5045 } 5046 }, { 5047 .alg = "ffdhe6144(dh)", 5048 .test = alg_test_kpp, 5049 .fips_allowed = 1, 5050 .suite = { 5051 .kpp = __VECS(ffdhe6144_dh_tv_template) 5052 } 5053 }, { 5054 .alg = "ffdhe8192(dh)", 5055 .test = alg_test_kpp, 5056 .fips_allowed = 1, 5057 .suite = { 5058 .kpp = __VECS(ffdhe8192_dh_tv_template) 5059 } 5060 }, { 5061 #endif /* CONFIG_CRYPTO_DH_RFC7919_GROUPS */ 5062 .alg = "gcm(aes)", 5063 .generic_driver = "gcm_base(ctr(aes-generic),ghash-generic)", 5064 .test = alg_test_aead, 5065 .fips_allowed = 1, 5066 .suite = { 5067 .aead = __VECS(aes_gcm_tv_template) 5068 } 5069 }, { 5070 .alg = "gcm(aria)", 5071 .generic_driver = "gcm_base(ctr(aria-generic),ghash-generic)", 5072 .test = alg_test_aead, 5073 .suite = { 5074 .aead = __VECS(aria_gcm_tv_template) 5075 } 5076 }, { 5077 .alg = "gcm(sm4)", 5078 .generic_driver = "gcm_base(ctr(sm4-generic),ghash-generic)", 5079 .test = alg_test_aead, 5080 .suite = { 5081 .aead = __VECS(sm4_gcm_tv_template) 5082 } 5083 }, { 5084 .alg = "ghash", 5085 .test = alg_test_hash, 5086 .suite = { 5087 .hash = __VECS(ghash_tv_template) 5088 } 5089 }, { 5090 .alg = "hctr2(aes)", 5091 .generic_driver = 5092 "hctr2_base(xctr(aes-generic),polyval-generic)", 5093 .test = alg_test_skcipher, 5094 .suite = { 5095 .cipher = __VECS(aes_hctr2_tv_template) 5096 } 5097 }, { 5098 .alg = "hmac(md5)", 5099 .test = alg_test_hash, 5100 .suite = { 5101 .hash = __VECS(hmac_md5_tv_template) 5102 } 5103 }, { 5104 .alg = "hmac(rmd160)", 5105 .test = alg_test_hash, 5106 .suite = { 5107 .hash = __VECS(hmac_rmd160_tv_template) 5108 } 5109 }, { 5110 .alg = "hmac(sha1)", 5111 .test = alg_test_hash, 5112 .fips_allowed = 1, 5113 .suite = { 5114 .hash = __VECS(hmac_sha1_tv_template) 5115 } 5116 }, { 5117 .alg = "hmac(sha224)", 5118 .test = alg_test_hash, 5119 .fips_allowed = 1, 5120 .suite = { 5121 .hash = __VECS(hmac_sha224_tv_template) 5122 } 5123 }, { 5124 .alg = "hmac(sha256)", 5125 .test = alg_test_hash, 5126 .fips_allowed = 1, 5127 .suite = { 5128 .hash = __VECS(hmac_sha256_tv_template) 5129 } 5130 }, { 5131 .alg = "hmac(sha3-224)", 5132 .test = alg_test_hash, 5133 .fips_allowed = 1, 5134 .suite = { 5135 .hash = __VECS(hmac_sha3_224_tv_template) 5136 } 5137 }, { 5138 .alg = "hmac(sha3-256)", 5139 .test = alg_test_hash, 5140 .fips_allowed = 1, 5141 .suite = { 5142 .hash = __VECS(hmac_sha3_256_tv_template) 5143 } 5144 }, { 5145 .alg = "hmac(sha3-384)", 5146 .test = alg_test_hash, 5147 .fips_allowed = 1, 5148 .suite = { 5149 .hash = __VECS(hmac_sha3_384_tv_template) 5150 } 5151 }, { 5152 .alg = "hmac(sha3-512)", 5153 .test = alg_test_hash, 5154 .fips_allowed = 1, 5155 .suite = { 5156 .hash = __VECS(hmac_sha3_512_tv_template) 5157 } 5158 }, { 5159 .alg = "hmac(sha384)", 5160 .test = alg_test_hash, 5161 .fips_allowed = 1, 5162 .suite = { 5163 .hash = __VECS(hmac_sha384_tv_template) 5164 } 5165 }, { 5166 .alg = "hmac(sha512)", 5167 .test = alg_test_hash, 5168 .fips_allowed = 1, 5169 .suite = { 5170 .hash = __VECS(hmac_sha512_tv_template) 5171 } 5172 }, { 5173 .alg = "hmac(sm3)", 5174 .test = alg_test_hash, 5175 .suite = { 5176 .hash = __VECS(hmac_sm3_tv_template) 5177 } 5178 }, { 5179 .alg = "hmac(streebog256)", 5180 .test = alg_test_hash, 5181 .suite = { 5182 .hash = __VECS(hmac_streebog256_tv_template) 5183 } 5184 }, { 5185 .alg = "hmac(streebog512)", 5186 .test = alg_test_hash, 5187 .suite = { 5188 .hash = __VECS(hmac_streebog512_tv_template) 5189 } 5190 }, { 5191 .alg = "jitterentropy_rng", 5192 .fips_allowed = 1, 5193 .test = alg_test_null, 5194 }, { 5195 .alg = "krb5enc(cmac(camellia),cts(cbc(camellia)))", 5196 .test = alg_test_aead, 5197 .suite.aead = __VECS(krb5_test_camellia_cts_cmac) 5198 }, { 5199 .alg = "lrw(aes)", 5200 .generic_driver = "lrw(ecb(aes-generic))", 5201 .test = alg_test_skcipher, 5202 .suite = { 5203 .cipher = __VECS(aes_lrw_tv_template) 5204 } 5205 }, { 5206 .alg = "lrw(camellia)", 5207 .generic_driver = "lrw(ecb(camellia-generic))", 5208 .test = alg_test_skcipher, 5209 .suite = { 5210 .cipher = __VECS(camellia_lrw_tv_template) 5211 } 5212 }, { 5213 .alg = "lrw(cast6)", 5214 .generic_driver = "lrw(ecb(cast6-generic))", 5215 .test = alg_test_skcipher, 5216 .suite = { 5217 .cipher = __VECS(cast6_lrw_tv_template) 5218 } 5219 }, { 5220 .alg = "lrw(serpent)", 5221 .generic_driver = "lrw(ecb(serpent-generic))", 5222 .test = alg_test_skcipher, 5223 .suite = { 5224 .cipher = __VECS(serpent_lrw_tv_template) 5225 } 5226 }, { 5227 .alg = "lrw(twofish)", 5228 .generic_driver = "lrw(ecb(twofish-generic))", 5229 .test = alg_test_skcipher, 5230 .suite = { 5231 .cipher = __VECS(tf_lrw_tv_template) 5232 } 5233 }, { 5234 .alg = "lz4", 5235 .test = alg_test_comp, 5236 .fips_allowed = 1, 5237 .suite = { 5238 .comp = { 5239 .comp = __VECS(lz4_comp_tv_template), 5240 .decomp = __VECS(lz4_decomp_tv_template) 5241 } 5242 } 5243 }, { 5244 .alg = "lz4hc", 5245 .test = alg_test_comp, 5246 .fips_allowed = 1, 5247 .suite = { 5248 .comp = { 5249 .comp = __VECS(lz4hc_comp_tv_template), 5250 .decomp = __VECS(lz4hc_decomp_tv_template) 5251 } 5252 } 5253 }, { 5254 .alg = "lzo", 5255 .test = alg_test_comp, 5256 .fips_allowed = 1, 5257 .suite = { 5258 .comp = { 5259 .comp = __VECS(lzo_comp_tv_template), 5260 .decomp = __VECS(lzo_decomp_tv_template) 5261 } 5262 } 5263 }, { 5264 .alg = "lzo-rle", 5265 .test = alg_test_comp, 5266 .fips_allowed = 1, 5267 .suite = { 5268 .comp = { 5269 .comp = __VECS(lzorle_comp_tv_template), 5270 .decomp = __VECS(lzorle_decomp_tv_template) 5271 } 5272 } 5273 }, { 5274 .alg = "md4", 5275 .test = alg_test_hash, 5276 .suite = { 5277 .hash = __VECS(md4_tv_template) 5278 } 5279 }, { 5280 .alg = "md5", 5281 .test = alg_test_hash, 5282 .suite = { 5283 .hash = __VECS(md5_tv_template) 5284 } 5285 }, { 5286 .alg = "michael_mic", 5287 .test = alg_test_hash, 5288 .suite = { 5289 .hash = __VECS(michael_mic_tv_template) 5290 } 5291 }, { 5292 .alg = "nhpoly1305", 5293 .test = alg_test_hash, 5294 .suite = { 5295 .hash = __VECS(nhpoly1305_tv_template) 5296 } 5297 }, { 5298 .alg = "p1363(ecdsa-nist-p192)", 5299 .test = alg_test_null, 5300 }, { 5301 .alg = "p1363(ecdsa-nist-p256)", 5302 .test = alg_test_sig, 5303 .fips_allowed = 1, 5304 .suite = { 5305 .sig = __VECS(p1363_ecdsa_nist_p256_tv_template) 5306 } 5307 }, { 5308 .alg = "p1363(ecdsa-nist-p384)", 5309 .test = alg_test_null, 5310 .fips_allowed = 1, 5311 }, { 5312 .alg = "p1363(ecdsa-nist-p521)", 5313 .test = alg_test_null, 5314 .fips_allowed = 1, 5315 }, { 5316 .alg = "pcbc(fcrypt)", 5317 .test = alg_test_skcipher, 5318 .suite = { 5319 .cipher = __VECS(fcrypt_pcbc_tv_template) 5320 } 5321 }, { 5322 .alg = "pkcs1(rsa,none)", 5323 .test = alg_test_sig, 5324 .suite = { 5325 .sig = __VECS(pkcs1_rsa_none_tv_template) 5326 } 5327 }, { 5328 .alg = "pkcs1(rsa,sha224)", 5329 .test = alg_test_null, 5330 .fips_allowed = 1, 5331 }, { 5332 .alg = "pkcs1(rsa,sha256)", 5333 .test = alg_test_sig, 5334 .fips_allowed = 1, 5335 .suite = { 5336 .sig = __VECS(pkcs1_rsa_tv_template) 5337 } 5338 }, { 5339 .alg = "pkcs1(rsa,sha3-256)", 5340 .test = alg_test_null, 5341 .fips_allowed = 1, 5342 }, { 5343 .alg = "pkcs1(rsa,sha3-384)", 5344 .test = alg_test_null, 5345 .fips_allowed = 1, 5346 }, { 5347 .alg = "pkcs1(rsa,sha3-512)", 5348 .test = alg_test_null, 5349 .fips_allowed = 1, 5350 }, { 5351 .alg = "pkcs1(rsa,sha384)", 5352 .test = alg_test_null, 5353 .fips_allowed = 1, 5354 }, { 5355 .alg = "pkcs1(rsa,sha512)", 5356 .test = alg_test_null, 5357 .fips_allowed = 1, 5358 }, { 5359 .alg = "pkcs1pad(rsa)", 5360 .test = alg_test_null, 5361 .fips_allowed = 1, 5362 }, { 5363 .alg = "polyval", 5364 .test = alg_test_hash, 5365 .suite = { 5366 .hash = __VECS(polyval_tv_template) 5367 } 5368 }, { 5369 .alg = "rfc3686(ctr(aes))", 5370 .test = alg_test_skcipher, 5371 .fips_allowed = 1, 5372 .suite = { 5373 .cipher = __VECS(aes_ctr_rfc3686_tv_template) 5374 } 5375 }, { 5376 .alg = "rfc3686(ctr(sm4))", 5377 .test = alg_test_skcipher, 5378 .suite = { 5379 .cipher = __VECS(sm4_ctr_rfc3686_tv_template) 5380 } 5381 }, { 5382 .alg = "rfc4106(gcm(aes))", 5383 .generic_driver = "rfc4106(gcm_base(ctr(aes-generic),ghash-generic))", 5384 .test = alg_test_aead, 5385 .fips_allowed = 1, 5386 .suite = { 5387 .aead = { 5388 ____VECS(aes_gcm_rfc4106_tv_template), 5389 .einval_allowed = 1, 5390 .aad_iv = 1, 5391 } 5392 } 5393 }, { 5394 .alg = "rfc4309(ccm(aes))", 5395 .generic_driver = "rfc4309(ccm_base(ctr(aes-generic),cbcmac(aes-generic)))", 5396 .test = alg_test_aead, 5397 .fips_allowed = 1, 5398 .suite = { 5399 .aead = { 5400 ____VECS(aes_ccm_rfc4309_tv_template), 5401 .einval_allowed = 1, 5402 .aad_iv = 1, 5403 } 5404 } 5405 }, { 5406 .alg = "rfc4543(gcm(aes))", 5407 .generic_driver = "rfc4543(gcm_base(ctr(aes-generic),ghash-generic))", 5408 .test = alg_test_aead, 5409 .suite = { 5410 .aead = { 5411 ____VECS(aes_gcm_rfc4543_tv_template), 5412 .einval_allowed = 1, 5413 .aad_iv = 1, 5414 } 5415 } 5416 }, { 5417 .alg = "rfc7539(chacha20,poly1305)", 5418 .test = alg_test_aead, 5419 .suite = { 5420 .aead = __VECS(rfc7539_tv_template) 5421 } 5422 }, { 5423 .alg = "rfc7539esp(chacha20,poly1305)", 5424 .test = alg_test_aead, 5425 .suite = { 5426 .aead = { 5427 ____VECS(rfc7539esp_tv_template), 5428 .einval_allowed = 1, 5429 .aad_iv = 1, 5430 } 5431 } 5432 }, { 5433 .alg = "rmd160", 5434 .test = alg_test_hash, 5435 .suite = { 5436 .hash = __VECS(rmd160_tv_template) 5437 } 5438 }, { 5439 .alg = "rsa", 5440 .test = alg_test_akcipher, 5441 .fips_allowed = 1, 5442 .suite = { 5443 .akcipher = __VECS(rsa_tv_template) 5444 } 5445 }, { 5446 .alg = "sha1", 5447 .test = alg_test_hash, 5448 .fips_allowed = 1, 5449 .suite = { 5450 .hash = __VECS(sha1_tv_template) 5451 } 5452 }, { 5453 .alg = "sha224", 5454 .test = alg_test_hash, 5455 .fips_allowed = 1, 5456 .suite = { 5457 .hash = __VECS(sha224_tv_template) 5458 } 5459 }, { 5460 .alg = "sha256", 5461 .test = alg_test_hash, 5462 .fips_allowed = 1, 5463 .suite = { 5464 .hash = __VECS(sha256_tv_template) 5465 } 5466 }, { 5467 .alg = "sha3-224", 5468 .test = alg_test_hash, 5469 .fips_allowed = 1, 5470 .suite = { 5471 .hash = __VECS(sha3_224_tv_template) 5472 } 5473 }, { 5474 .alg = "sha3-256", 5475 .test = alg_test_hash, 5476 .fips_allowed = 1, 5477 .suite = { 5478 .hash = __VECS(sha3_256_tv_template) 5479 } 5480 }, { 5481 .alg = "sha3-384", 5482 .test = alg_test_hash, 5483 .fips_allowed = 1, 5484 .suite = { 5485 .hash = __VECS(sha3_384_tv_template) 5486 } 5487 }, { 5488 .alg = "sha3-512", 5489 .test = alg_test_hash, 5490 .fips_allowed = 1, 5491 .suite = { 5492 .hash = __VECS(sha3_512_tv_template) 5493 } 5494 }, { 5495 .alg = "sha384", 5496 .test = alg_test_hash, 5497 .fips_allowed = 1, 5498 .suite = { 5499 .hash = __VECS(sha384_tv_template) 5500 } 5501 }, { 5502 .alg = "sha512", 5503 .test = alg_test_hash, 5504 .fips_allowed = 1, 5505 .suite = { 5506 .hash = __VECS(sha512_tv_template) 5507 } 5508 }, { 5509 .alg = "sm3", 5510 .test = alg_test_hash, 5511 .suite = { 5512 .hash = __VECS(sm3_tv_template) 5513 } 5514 }, { 5515 .alg = "streebog256", 5516 .test = alg_test_hash, 5517 .suite = { 5518 .hash = __VECS(streebog256_tv_template) 5519 } 5520 }, { 5521 .alg = "streebog512", 5522 .test = alg_test_hash, 5523 .suite = { 5524 .hash = __VECS(streebog512_tv_template) 5525 } 5526 }, { 5527 .alg = "wp256", 5528 .test = alg_test_hash, 5529 .suite = { 5530 .hash = __VECS(wp256_tv_template) 5531 } 5532 }, { 5533 .alg = "wp384", 5534 .test = alg_test_hash, 5535 .suite = { 5536 .hash = __VECS(wp384_tv_template) 5537 } 5538 }, { 5539 .alg = "wp512", 5540 .test = alg_test_hash, 5541 .suite = { 5542 .hash = __VECS(wp512_tv_template) 5543 } 5544 }, { 5545 .alg = "x962(ecdsa-nist-p192)", 5546 .test = alg_test_sig, 5547 .suite = { 5548 .sig = __VECS(x962_ecdsa_nist_p192_tv_template) 5549 } 5550 }, { 5551 .alg = "x962(ecdsa-nist-p256)", 5552 .test = alg_test_sig, 5553 .fips_allowed = 1, 5554 .suite = { 5555 .sig = __VECS(x962_ecdsa_nist_p256_tv_template) 5556 } 5557 }, { 5558 .alg = "x962(ecdsa-nist-p384)", 5559 .test = alg_test_sig, 5560 .fips_allowed = 1, 5561 .suite = { 5562 .sig = __VECS(x962_ecdsa_nist_p384_tv_template) 5563 } 5564 }, { 5565 .alg = "x962(ecdsa-nist-p521)", 5566 .test = alg_test_sig, 5567 .fips_allowed = 1, 5568 .suite = { 5569 .sig = __VECS(x962_ecdsa_nist_p521_tv_template) 5570 } 5571 }, { 5572 .alg = "xcbc(aes)", 5573 .test = alg_test_hash, 5574 .suite = { 5575 .hash = __VECS(aes_xcbc128_tv_template) 5576 } 5577 }, { 5578 .alg = "xcbc(sm4)", 5579 .test = alg_test_hash, 5580 .suite = { 5581 .hash = __VECS(sm4_xcbc128_tv_template) 5582 } 5583 }, { 5584 .alg = "xchacha12", 5585 .test = alg_test_skcipher, 5586 .suite = { 5587 .cipher = __VECS(xchacha12_tv_template) 5588 }, 5589 }, { 5590 .alg = "xchacha20", 5591 .test = alg_test_skcipher, 5592 .suite = { 5593 .cipher = __VECS(xchacha20_tv_template) 5594 }, 5595 }, { 5596 .alg = "xctr(aes)", 5597 .test = alg_test_skcipher, 5598 .suite = { 5599 .cipher = __VECS(aes_xctr_tv_template) 5600 } 5601 }, { 5602 .alg = "xts(aes)", 5603 .generic_driver = "xts(ecb(aes-generic))", 5604 .test = alg_test_skcipher, 5605 .fips_allowed = 1, 5606 .suite = { 5607 .cipher = __VECS(aes_xts_tv_template) 5608 } 5609 }, { 5610 .alg = "xts(camellia)", 5611 .generic_driver = "xts(ecb(camellia-generic))", 5612 .test = alg_test_skcipher, 5613 .suite = { 5614 .cipher = __VECS(camellia_xts_tv_template) 5615 } 5616 }, { 5617 .alg = "xts(cast6)", 5618 .generic_driver = "xts(ecb(cast6-generic))", 5619 .test = alg_test_skcipher, 5620 .suite = { 5621 .cipher = __VECS(cast6_xts_tv_template) 5622 } 5623 }, { 5624 /* Same as xts(aes) except the key is stored in 5625 * hardware secure memory which we reference by index 5626 */ 5627 .alg = "xts(paes)", 5628 .test = alg_test_null, 5629 .fips_allowed = 1, 5630 }, { 5631 .alg = "xts(serpent)", 5632 .generic_driver = "xts(ecb(serpent-generic))", 5633 .test = alg_test_skcipher, 5634 .suite = { 5635 .cipher = __VECS(serpent_xts_tv_template) 5636 } 5637 }, { 5638 .alg = "xts(sm4)", 5639 .generic_driver = "xts(ecb(sm4-generic))", 5640 .test = alg_test_skcipher, 5641 .suite = { 5642 .cipher = __VECS(sm4_xts_tv_template) 5643 } 5644 }, { 5645 .alg = "xts(twofish)", 5646 .generic_driver = "xts(ecb(twofish-generic))", 5647 .test = alg_test_skcipher, 5648 .suite = { 5649 .cipher = __VECS(tf_xts_tv_template) 5650 } 5651 }, { 5652 #if IS_ENABLED(CONFIG_CRYPTO_PAES_S390) 5653 .alg = "xts-paes-s390", 5654 .fips_allowed = 1, 5655 .test = alg_test_skcipher, 5656 .suite = { 5657 .cipher = __VECS(aes_xts_tv_template) 5658 } 5659 }, { 5660 #endif 5661 .alg = "xxhash64", 5662 .test = alg_test_hash, 5663 .fips_allowed = 1, 5664 .suite = { 5665 .hash = __VECS(xxhash64_tv_template) 5666 } 5667 }, { 5668 .alg = "zstd", 5669 .test = alg_test_comp, 5670 .fips_allowed = 1, 5671 .suite = { 5672 .comp = { 5673 .comp = __VECS(zstd_comp_tv_template), 5674 .decomp = __VECS(zstd_decomp_tv_template) 5675 } 5676 } 5677 } 5678 }; 5679 5680 static void alg_check_test_descs_order(void) 5681 { 5682 int i; 5683 5684 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) { 5685 int diff = strcmp(alg_test_descs[i - 1].alg, 5686 alg_test_descs[i].alg); 5687 5688 if (WARN_ON(diff > 0)) { 5689 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n", 5690 alg_test_descs[i - 1].alg, 5691 alg_test_descs[i].alg); 5692 } 5693 5694 if (WARN_ON(diff == 0)) { 5695 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n", 5696 alg_test_descs[i].alg); 5697 } 5698 } 5699 } 5700 5701 static void alg_check_testvec_configs(void) 5702 { 5703 int i; 5704 5705 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) 5706 WARN_ON(!valid_testvec_config( 5707 &default_cipher_testvec_configs[i])); 5708 5709 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) 5710 WARN_ON(!valid_testvec_config( 5711 &default_hash_testvec_configs[i])); 5712 } 5713 5714 static void testmgr_onetime_init(void) 5715 { 5716 alg_check_test_descs_order(); 5717 alg_check_testvec_configs(); 5718 5719 if (!noslowtests) 5720 pr_warn("alg: full crypto tests enabled. This is intended for developer use only.\n"); 5721 } 5722 5723 static int alg_find_test(const char *alg) 5724 { 5725 int start = 0; 5726 int end = ARRAY_SIZE(alg_test_descs); 5727 5728 while (start < end) { 5729 int i = (start + end) / 2; 5730 int diff = strcmp(alg_test_descs[i].alg, alg); 5731 5732 if (diff > 0) { 5733 end = i; 5734 continue; 5735 } 5736 5737 if (diff < 0) { 5738 start = i + 1; 5739 continue; 5740 } 5741 5742 return i; 5743 } 5744 5745 return -1; 5746 } 5747 5748 static int alg_fips_disabled(const char *driver, const char *alg) 5749 { 5750 pr_info("alg: %s (%s) is disabled due to FIPS\n", alg, driver); 5751 5752 return -ECANCELED; 5753 } 5754 5755 int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 5756 { 5757 int i; 5758 int j; 5759 int rc; 5760 5761 if (!fips_enabled && notests) { 5762 printk_once(KERN_INFO "alg: self-tests disabled\n"); 5763 return 0; 5764 } 5765 5766 DO_ONCE(testmgr_onetime_init); 5767 5768 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) { 5769 char nalg[CRYPTO_MAX_ALG_NAME]; 5770 5771 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >= 5772 sizeof(nalg)) 5773 return -ENAMETOOLONG; 5774 5775 i = alg_find_test(nalg); 5776 if (i < 0) 5777 goto notest; 5778 5779 if (fips_enabled && !alg_test_descs[i].fips_allowed) 5780 goto non_fips_alg; 5781 5782 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask); 5783 goto test_done; 5784 } 5785 5786 i = alg_find_test(alg); 5787 j = alg_find_test(driver); 5788 if (i < 0 && j < 0) 5789 goto notest; 5790 5791 if (fips_enabled) { 5792 if (j >= 0 && !alg_test_descs[j].fips_allowed) 5793 return -EINVAL; 5794 5795 if (i >= 0 && !alg_test_descs[i].fips_allowed) 5796 goto non_fips_alg; 5797 } 5798 5799 rc = 0; 5800 if (i >= 0) 5801 rc |= alg_test_descs[i].test(alg_test_descs + i, driver, 5802 type, mask); 5803 if (j >= 0 && j != i) 5804 rc |= alg_test_descs[j].test(alg_test_descs + j, driver, 5805 type, mask); 5806 5807 test_done: 5808 if (rc) { 5809 if (fips_enabled) { 5810 fips_fail_notify(); 5811 panic("alg: self-tests for %s (%s) failed in fips mode!\n", 5812 driver, alg); 5813 } 5814 pr_warn("alg: self-tests for %s using %s failed (rc=%d)", 5815 alg, driver, rc); 5816 WARN(rc != -ENOENT, 5817 "alg: self-tests for %s using %s failed (rc=%d)", 5818 alg, driver, rc); 5819 } else { 5820 if (fips_enabled) 5821 pr_info("alg: self-tests for %s (%s) passed\n", 5822 driver, alg); 5823 } 5824 5825 return rc; 5826 5827 notest: 5828 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_LSKCIPHER) { 5829 char nalg[CRYPTO_MAX_ALG_NAME]; 5830 5831 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >= 5832 sizeof(nalg)) 5833 goto notest2; 5834 5835 i = alg_find_test(nalg); 5836 if (i < 0) 5837 goto notest2; 5838 5839 if (fips_enabled && !alg_test_descs[i].fips_allowed) 5840 goto non_fips_alg; 5841 5842 rc = alg_test_skcipher(alg_test_descs + i, driver, type, mask); 5843 goto test_done; 5844 } 5845 5846 notest2: 5847 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver); 5848 5849 if (type & CRYPTO_ALG_FIPS_INTERNAL) 5850 return alg_fips_disabled(driver, alg); 5851 5852 return 0; 5853 non_fips_alg: 5854 return alg_fips_disabled(driver, alg); 5855 } 5856 5857 #endif /* CONFIG_CRYPTO_SELFTESTS */ 5858 5859 EXPORT_SYMBOL_GPL(alg_test); 5860