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