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