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