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