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