1 /* 2 * Algorithm testing framework and tests. 3 * 4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 5 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org> 6 * Copyright (c) 2007 Nokia Siemens Networks 7 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> 8 * Copyright (c) 2019 Google LLC 9 * 10 * Updated RFC4106 AES-GCM testing. 11 * Authors: Aidan O'Mahony (aidan.o.mahony@intel.com) 12 * Adrian Hoban <adrian.hoban@intel.com> 13 * Gabriele Paoloni <gabriele.paoloni@intel.com> 14 * Tadeusz Struk (tadeusz.struk@intel.com) 15 * Copyright (c) 2010, Intel Corporation. 16 * 17 * This program is free software; you can redistribute it and/or modify it 18 * under the terms of the GNU General Public License as published by the Free 19 * Software Foundation; either version 2 of the License, or (at your option) 20 * any later version. 21 * 22 */ 23 24 #include <crypto/aead.h> 25 #include <crypto/hash.h> 26 #include <crypto/skcipher.h> 27 #include <linux/err.h> 28 #include <linux/fips.h> 29 #include <linux/module.h> 30 #include <linux/once.h> 31 #include <linux/random.h> 32 #include <linux/scatterlist.h> 33 #include <linux/slab.h> 34 #include <linux/string.h> 35 #include <crypto/rng.h> 36 #include <crypto/drbg.h> 37 #include <crypto/akcipher.h> 38 #include <crypto/kpp.h> 39 #include <crypto/acompress.h> 40 #include <crypto/internal/simd.h> 41 42 #include "internal.h" 43 44 static bool notests; 45 module_param(notests, bool, 0644); 46 MODULE_PARM_DESC(notests, "disable crypto self-tests"); 47 48 static bool panic_on_fail; 49 module_param(panic_on_fail, bool, 0444); 50 51 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 52 static bool noextratests; 53 module_param(noextratests, bool, 0644); 54 MODULE_PARM_DESC(noextratests, "disable expensive crypto self-tests"); 55 56 static unsigned int fuzz_iterations = 100; 57 module_param(fuzz_iterations, uint, 0644); 58 MODULE_PARM_DESC(fuzz_iterations, "number of fuzz test iterations"); 59 60 DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test); 61 EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test); 62 #endif 63 64 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS 65 66 /* a perfect nop */ 67 int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 68 { 69 return 0; 70 } 71 72 #else 73 74 #include "testmgr.h" 75 76 /* 77 * Need slab memory for testing (size in number of pages). 78 */ 79 #define XBUFSIZE 8 80 81 /* 82 * Used by test_cipher() 83 */ 84 #define ENCRYPT 1 85 #define DECRYPT 0 86 87 struct aead_test_suite { 88 const struct aead_testvec *vecs; 89 unsigned int count; 90 }; 91 92 struct cipher_test_suite { 93 const struct cipher_testvec *vecs; 94 unsigned int count; 95 }; 96 97 struct comp_test_suite { 98 struct { 99 const struct comp_testvec *vecs; 100 unsigned int count; 101 } comp, decomp; 102 }; 103 104 struct hash_test_suite { 105 const struct hash_testvec *vecs; 106 unsigned int count; 107 }; 108 109 struct cprng_test_suite { 110 const struct cprng_testvec *vecs; 111 unsigned int count; 112 }; 113 114 struct drbg_test_suite { 115 const struct drbg_testvec *vecs; 116 unsigned int count; 117 }; 118 119 struct akcipher_test_suite { 120 const struct akcipher_testvec *vecs; 121 unsigned int count; 122 }; 123 124 struct kpp_test_suite { 125 const struct kpp_testvec *vecs; 126 unsigned int count; 127 }; 128 129 struct alg_test_desc { 130 const char *alg; 131 int (*test)(const struct alg_test_desc *desc, const char *driver, 132 u32 type, u32 mask); 133 int fips_allowed; /* set if alg is allowed in fips mode */ 134 135 union { 136 struct aead_test_suite aead; 137 struct cipher_test_suite cipher; 138 struct comp_test_suite comp; 139 struct hash_test_suite hash; 140 struct cprng_test_suite cprng; 141 struct drbg_test_suite drbg; 142 struct akcipher_test_suite akcipher; 143 struct kpp_test_suite kpp; 144 } suite; 145 }; 146 147 static void hexdump(unsigned char *buf, unsigned int len) 148 { 149 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET, 150 16, 1, 151 buf, len, false); 152 } 153 154 static int __testmgr_alloc_buf(char *buf[XBUFSIZE], int order) 155 { 156 int i; 157 158 for (i = 0; i < XBUFSIZE; i++) { 159 buf[i] = (char *)__get_free_pages(GFP_KERNEL, order); 160 if (!buf[i]) 161 goto err_free_buf; 162 } 163 164 return 0; 165 166 err_free_buf: 167 while (i-- > 0) 168 free_pages((unsigned long)buf[i], order); 169 170 return -ENOMEM; 171 } 172 173 static int testmgr_alloc_buf(char *buf[XBUFSIZE]) 174 { 175 return __testmgr_alloc_buf(buf, 0); 176 } 177 178 static void __testmgr_free_buf(char *buf[XBUFSIZE], int order) 179 { 180 int i; 181 182 for (i = 0; i < XBUFSIZE; i++) 183 free_pages((unsigned long)buf[i], order); 184 } 185 186 static void testmgr_free_buf(char *buf[XBUFSIZE]) 187 { 188 __testmgr_free_buf(buf, 0); 189 } 190 191 #define TESTMGR_POISON_BYTE 0xfe 192 #define TESTMGR_POISON_LEN 16 193 194 static inline void testmgr_poison(void *addr, size_t len) 195 { 196 memset(addr, TESTMGR_POISON_BYTE, len); 197 } 198 199 /* Is the memory region still fully poisoned? */ 200 static inline bool testmgr_is_poison(const void *addr, size_t len) 201 { 202 return memchr_inv(addr, TESTMGR_POISON_BYTE, len) == NULL; 203 } 204 205 /* flush type for hash algorithms */ 206 enum flush_type { 207 /* merge with update of previous buffer(s) */ 208 FLUSH_TYPE_NONE = 0, 209 210 /* update with previous buffer(s) before doing this one */ 211 FLUSH_TYPE_FLUSH, 212 213 /* likewise, but also export and re-import the intermediate state */ 214 FLUSH_TYPE_REIMPORT, 215 }; 216 217 /* finalization function for hash algorithms */ 218 enum finalization_type { 219 FINALIZATION_TYPE_FINAL, /* use final() */ 220 FINALIZATION_TYPE_FINUP, /* use finup() */ 221 FINALIZATION_TYPE_DIGEST, /* use digest() */ 222 }; 223 224 #define TEST_SG_TOTAL 10000 225 226 /** 227 * struct test_sg_division - description of a scatterlist entry 228 * 229 * This struct describes one entry of a scatterlist being constructed to check a 230 * crypto test vector. 231 * 232 * @proportion_of_total: length of this chunk relative to the total length, 233 * given as a proportion out of TEST_SG_TOTAL so that it 234 * scales to fit any test vector 235 * @offset: byte offset into a 2-page buffer at which this chunk will start 236 * @offset_relative_to_alignmask: if true, add the algorithm's alignmask to the 237 * @offset 238 * @flush_type: for hashes, whether an update() should be done now vs. 239 * continuing to accumulate data 240 * @nosimd: if doing the pending update(), do it with SIMD disabled? 241 */ 242 struct test_sg_division { 243 unsigned int proportion_of_total; 244 unsigned int offset; 245 bool offset_relative_to_alignmask; 246 enum flush_type flush_type; 247 bool nosimd; 248 }; 249 250 /** 251 * struct testvec_config - configuration for testing a crypto test vector 252 * 253 * This struct describes the data layout and other parameters with which each 254 * crypto test vector can be tested. 255 * 256 * @name: name of this config, logged for debugging purposes if a test fails 257 * @inplace: operate on the data in-place, if applicable for the algorithm type? 258 * @req_flags: extra request_flags, e.g. CRYPTO_TFM_REQ_MAY_SLEEP 259 * @src_divs: description of how to arrange the source scatterlist 260 * @dst_divs: description of how to arrange the dst scatterlist, if applicable 261 * for the algorithm type. Defaults to @src_divs if unset. 262 * @iv_offset: misalignment of the IV in the range [0..MAX_ALGAPI_ALIGNMASK+1], 263 * where 0 is aligned to a 2*(MAX_ALGAPI_ALIGNMASK+1) byte boundary 264 * @iv_offset_relative_to_alignmask: if true, add the algorithm's alignmask to 265 * the @iv_offset 266 * @finalization_type: what finalization function to use for hashes 267 * @nosimd: execute with SIMD disabled? Requires !CRYPTO_TFM_REQ_MAY_SLEEP. 268 */ 269 struct testvec_config { 270 const char *name; 271 bool inplace; 272 u32 req_flags; 273 struct test_sg_division src_divs[XBUFSIZE]; 274 struct test_sg_division dst_divs[XBUFSIZE]; 275 unsigned int iv_offset; 276 bool iv_offset_relative_to_alignmask; 277 enum finalization_type finalization_type; 278 bool nosimd; 279 }; 280 281 #define TESTVEC_CONFIG_NAMELEN 192 282 283 /* 284 * The following are the lists of testvec_configs to test for each algorithm 285 * type when the basic crypto self-tests are enabled, i.e. when 286 * CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is unset. They aim to provide good test 287 * coverage, while keeping the test time much shorter than the full fuzz tests 288 * so that the basic tests can be enabled in a wider range of circumstances. 289 */ 290 291 /* Configs for skciphers and aeads */ 292 static const struct testvec_config default_cipher_testvec_configs[] = { 293 { 294 .name = "in-place", 295 .inplace = true, 296 .src_divs = { { .proportion_of_total = 10000 } }, 297 }, { 298 .name = "out-of-place", 299 .src_divs = { { .proportion_of_total = 10000 } }, 300 }, { 301 .name = "unaligned buffer, offset=1", 302 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } }, 303 .iv_offset = 1, 304 }, { 305 .name = "buffer aligned only to alignmask", 306 .src_divs = { 307 { 308 .proportion_of_total = 10000, 309 .offset = 1, 310 .offset_relative_to_alignmask = true, 311 }, 312 }, 313 .iv_offset = 1, 314 .iv_offset_relative_to_alignmask = true, 315 }, { 316 .name = "two even aligned splits", 317 .src_divs = { 318 { .proportion_of_total = 5000 }, 319 { .proportion_of_total = 5000 }, 320 }, 321 }, { 322 .name = "uneven misaligned splits, may sleep", 323 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP, 324 .src_divs = { 325 { .proportion_of_total = 1900, .offset = 33 }, 326 { .proportion_of_total = 3300, .offset = 7 }, 327 { .proportion_of_total = 4800, .offset = 18 }, 328 }, 329 .iv_offset = 3, 330 }, { 331 .name = "misaligned splits crossing pages, inplace", 332 .inplace = true, 333 .src_divs = { 334 { 335 .proportion_of_total = 7500, 336 .offset = PAGE_SIZE - 32 337 }, { 338 .proportion_of_total = 2500, 339 .offset = PAGE_SIZE - 7 340 }, 341 }, 342 } 343 }; 344 345 static const struct testvec_config default_hash_testvec_configs[] = { 346 { 347 .name = "init+update+final aligned buffer", 348 .src_divs = { { .proportion_of_total = 10000 } }, 349 .finalization_type = FINALIZATION_TYPE_FINAL, 350 }, { 351 .name = "init+finup aligned buffer", 352 .src_divs = { { .proportion_of_total = 10000 } }, 353 .finalization_type = FINALIZATION_TYPE_FINUP, 354 }, { 355 .name = "digest aligned buffer", 356 .src_divs = { { .proportion_of_total = 10000 } }, 357 .finalization_type = FINALIZATION_TYPE_DIGEST, 358 }, { 359 .name = "init+update+final misaligned buffer", 360 .src_divs = { { .proportion_of_total = 10000, .offset = 1 } }, 361 .finalization_type = FINALIZATION_TYPE_FINAL, 362 }, { 363 .name = "digest buffer aligned only to alignmask", 364 .src_divs = { 365 { 366 .proportion_of_total = 10000, 367 .offset = 1, 368 .offset_relative_to_alignmask = true, 369 }, 370 }, 371 .finalization_type = FINALIZATION_TYPE_DIGEST, 372 }, { 373 .name = "init+update+update+final two even splits", 374 .src_divs = { 375 { .proportion_of_total = 5000 }, 376 { 377 .proportion_of_total = 5000, 378 .flush_type = FLUSH_TYPE_FLUSH, 379 }, 380 }, 381 .finalization_type = FINALIZATION_TYPE_FINAL, 382 }, { 383 .name = "digest uneven misaligned splits, may sleep", 384 .req_flags = CRYPTO_TFM_REQ_MAY_SLEEP, 385 .src_divs = { 386 { .proportion_of_total = 1900, .offset = 33 }, 387 { .proportion_of_total = 3300, .offset = 7 }, 388 { .proportion_of_total = 4800, .offset = 18 }, 389 }, 390 .finalization_type = FINALIZATION_TYPE_DIGEST, 391 }, { 392 .name = "digest misaligned splits crossing pages", 393 .src_divs = { 394 { 395 .proportion_of_total = 7500, 396 .offset = PAGE_SIZE - 32, 397 }, { 398 .proportion_of_total = 2500, 399 .offset = PAGE_SIZE - 7, 400 }, 401 }, 402 .finalization_type = FINALIZATION_TYPE_DIGEST, 403 }, { 404 .name = "import/export", 405 .src_divs = { 406 { 407 .proportion_of_total = 6500, 408 .flush_type = FLUSH_TYPE_REIMPORT, 409 }, { 410 .proportion_of_total = 3500, 411 .flush_type = FLUSH_TYPE_REIMPORT, 412 }, 413 }, 414 .finalization_type = FINALIZATION_TYPE_FINAL, 415 } 416 }; 417 418 static unsigned int count_test_sg_divisions(const struct test_sg_division *divs) 419 { 420 unsigned int remaining = TEST_SG_TOTAL; 421 unsigned int ndivs = 0; 422 423 do { 424 remaining -= divs[ndivs++].proportion_of_total; 425 } while (remaining); 426 427 return ndivs; 428 } 429 430 #define SGDIVS_HAVE_FLUSHES BIT(0) 431 #define SGDIVS_HAVE_NOSIMD BIT(1) 432 433 static bool valid_sg_divisions(const struct test_sg_division *divs, 434 unsigned int count, int *flags_ret) 435 { 436 unsigned int total = 0; 437 unsigned int i; 438 439 for (i = 0; i < count && total != TEST_SG_TOTAL; i++) { 440 if (divs[i].proportion_of_total <= 0 || 441 divs[i].proportion_of_total > TEST_SG_TOTAL - total) 442 return false; 443 total += divs[i].proportion_of_total; 444 if (divs[i].flush_type != FLUSH_TYPE_NONE) 445 *flags_ret |= SGDIVS_HAVE_FLUSHES; 446 if (divs[i].nosimd) 447 *flags_ret |= SGDIVS_HAVE_NOSIMD; 448 } 449 return total == TEST_SG_TOTAL && 450 memchr_inv(&divs[i], 0, (count - i) * sizeof(divs[0])) == NULL; 451 } 452 453 /* 454 * Check whether the given testvec_config is valid. This isn't strictly needed 455 * since every testvec_config should be valid, but check anyway so that people 456 * don't unknowingly add broken configs that don't do what they wanted. 457 */ 458 static bool valid_testvec_config(const struct testvec_config *cfg) 459 { 460 int flags = 0; 461 462 if (cfg->name == NULL) 463 return false; 464 465 if (!valid_sg_divisions(cfg->src_divs, ARRAY_SIZE(cfg->src_divs), 466 &flags)) 467 return false; 468 469 if (cfg->dst_divs[0].proportion_of_total) { 470 if (!valid_sg_divisions(cfg->dst_divs, 471 ARRAY_SIZE(cfg->dst_divs), &flags)) 472 return false; 473 } else { 474 if (memchr_inv(cfg->dst_divs, 0, sizeof(cfg->dst_divs))) 475 return false; 476 /* defaults to dst_divs=src_divs */ 477 } 478 479 if (cfg->iv_offset + 480 (cfg->iv_offset_relative_to_alignmask ? MAX_ALGAPI_ALIGNMASK : 0) > 481 MAX_ALGAPI_ALIGNMASK + 1) 482 return false; 483 484 if ((flags & (SGDIVS_HAVE_FLUSHES | SGDIVS_HAVE_NOSIMD)) && 485 cfg->finalization_type == FINALIZATION_TYPE_DIGEST) 486 return false; 487 488 if ((cfg->nosimd || (flags & SGDIVS_HAVE_NOSIMD)) && 489 (cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP)) 490 return false; 491 492 return true; 493 } 494 495 struct test_sglist { 496 char *bufs[XBUFSIZE]; 497 struct scatterlist sgl[XBUFSIZE]; 498 struct scatterlist sgl_saved[XBUFSIZE]; 499 struct scatterlist *sgl_ptr; 500 unsigned int nents; 501 }; 502 503 static int init_test_sglist(struct test_sglist *tsgl) 504 { 505 return __testmgr_alloc_buf(tsgl->bufs, 1 /* two pages per buffer */); 506 } 507 508 static void destroy_test_sglist(struct test_sglist *tsgl) 509 { 510 return __testmgr_free_buf(tsgl->bufs, 1 /* two pages per buffer */); 511 } 512 513 /** 514 * build_test_sglist() - build a scatterlist for a crypto test 515 * 516 * @tsgl: the scatterlist to build. @tsgl->bufs[] contains an array of 2-page 517 * buffers which the scatterlist @tsgl->sgl[] will be made to point into. 518 * @divs: the layout specification on which the scatterlist will be based 519 * @alignmask: the algorithm's alignmask 520 * @total_len: the total length of the scatterlist to build in bytes 521 * @data: if non-NULL, the buffers will be filled with this data until it ends. 522 * Otherwise the buffers will be poisoned. In both cases, some bytes 523 * past the end of each buffer will be poisoned to help detect overruns. 524 * @out_divs: if non-NULL, the test_sg_division to which each scatterlist entry 525 * corresponds will be returned here. This will match @divs except 526 * that divisions resolving to a length of 0 are omitted as they are 527 * not included in the scatterlist. 528 * 529 * Return: 0 or a -errno value 530 */ 531 static int build_test_sglist(struct test_sglist *tsgl, 532 const struct test_sg_division *divs, 533 const unsigned int alignmask, 534 const unsigned int total_len, 535 struct iov_iter *data, 536 const struct test_sg_division *out_divs[XBUFSIZE]) 537 { 538 struct { 539 const struct test_sg_division *div; 540 size_t length; 541 } partitions[XBUFSIZE]; 542 const unsigned int ndivs = count_test_sg_divisions(divs); 543 unsigned int len_remaining = total_len; 544 unsigned int i; 545 546 BUILD_BUG_ON(ARRAY_SIZE(partitions) != ARRAY_SIZE(tsgl->sgl)); 547 if (WARN_ON(ndivs > ARRAY_SIZE(partitions))) 548 return -EINVAL; 549 550 /* Calculate the (div, length) pairs */ 551 tsgl->nents = 0; 552 for (i = 0; i < ndivs; i++) { 553 unsigned int len_this_sg = 554 min(len_remaining, 555 (total_len * divs[i].proportion_of_total + 556 TEST_SG_TOTAL / 2) / TEST_SG_TOTAL); 557 558 if (len_this_sg != 0) { 559 partitions[tsgl->nents].div = &divs[i]; 560 partitions[tsgl->nents].length = len_this_sg; 561 tsgl->nents++; 562 len_remaining -= len_this_sg; 563 } 564 } 565 if (tsgl->nents == 0) { 566 partitions[tsgl->nents].div = &divs[0]; 567 partitions[tsgl->nents].length = 0; 568 tsgl->nents++; 569 } 570 partitions[tsgl->nents - 1].length += len_remaining; 571 572 /* Set up the sgl entries and fill the data or poison */ 573 sg_init_table(tsgl->sgl, tsgl->nents); 574 for (i = 0; i < tsgl->nents; i++) { 575 unsigned int offset = partitions[i].div->offset; 576 void *addr; 577 578 if (partitions[i].div->offset_relative_to_alignmask) 579 offset += alignmask; 580 581 while (offset + partitions[i].length + TESTMGR_POISON_LEN > 582 2 * PAGE_SIZE) { 583 if (WARN_ON(offset <= 0)) 584 return -EINVAL; 585 offset /= 2; 586 } 587 588 addr = &tsgl->bufs[i][offset]; 589 sg_set_buf(&tsgl->sgl[i], addr, partitions[i].length); 590 591 if (out_divs) 592 out_divs[i] = partitions[i].div; 593 594 if (data) { 595 size_t copy_len, copied; 596 597 copy_len = min(partitions[i].length, data->count); 598 copied = copy_from_iter(addr, copy_len, data); 599 if (WARN_ON(copied != copy_len)) 600 return -EINVAL; 601 testmgr_poison(addr + copy_len, partitions[i].length + 602 TESTMGR_POISON_LEN - copy_len); 603 } else { 604 testmgr_poison(addr, partitions[i].length + 605 TESTMGR_POISON_LEN); 606 } 607 } 608 609 sg_mark_end(&tsgl->sgl[tsgl->nents - 1]); 610 tsgl->sgl_ptr = tsgl->sgl; 611 memcpy(tsgl->sgl_saved, tsgl->sgl, tsgl->nents * sizeof(tsgl->sgl[0])); 612 return 0; 613 } 614 615 /* 616 * Verify that a scatterlist crypto operation produced the correct output. 617 * 618 * @tsgl: scatterlist containing the actual output 619 * @expected_output: buffer containing the expected output 620 * @len_to_check: length of @expected_output in bytes 621 * @unchecked_prefix_len: number of ignored bytes in @tsgl prior to real result 622 * @check_poison: verify that the poison bytes after each chunk are intact? 623 * 624 * Return: 0 if correct, -EINVAL if incorrect, -EOVERFLOW if buffer overrun. 625 */ 626 static int verify_correct_output(const struct test_sglist *tsgl, 627 const char *expected_output, 628 unsigned int len_to_check, 629 unsigned int unchecked_prefix_len, 630 bool check_poison) 631 { 632 unsigned int i; 633 634 for (i = 0; i < tsgl->nents; i++) { 635 struct scatterlist *sg = &tsgl->sgl_ptr[i]; 636 unsigned int len = sg->length; 637 unsigned int offset = sg->offset; 638 const char *actual_output; 639 640 if (unchecked_prefix_len) { 641 if (unchecked_prefix_len >= len) { 642 unchecked_prefix_len -= len; 643 continue; 644 } 645 offset += unchecked_prefix_len; 646 len -= unchecked_prefix_len; 647 unchecked_prefix_len = 0; 648 } 649 len = min(len, len_to_check); 650 actual_output = page_address(sg_page(sg)) + offset; 651 if (memcmp(expected_output, actual_output, len) != 0) 652 return -EINVAL; 653 if (check_poison && 654 !testmgr_is_poison(actual_output + len, TESTMGR_POISON_LEN)) 655 return -EOVERFLOW; 656 len_to_check -= len; 657 expected_output += len; 658 } 659 if (WARN_ON(len_to_check != 0)) 660 return -EINVAL; 661 return 0; 662 } 663 664 static bool is_test_sglist_corrupted(const struct test_sglist *tsgl) 665 { 666 unsigned int i; 667 668 for (i = 0; i < tsgl->nents; i++) { 669 if (tsgl->sgl[i].page_link != tsgl->sgl_saved[i].page_link) 670 return true; 671 if (tsgl->sgl[i].offset != tsgl->sgl_saved[i].offset) 672 return true; 673 if (tsgl->sgl[i].length != tsgl->sgl_saved[i].length) 674 return true; 675 } 676 return false; 677 } 678 679 struct cipher_test_sglists { 680 struct test_sglist src; 681 struct test_sglist dst; 682 }; 683 684 static struct cipher_test_sglists *alloc_cipher_test_sglists(void) 685 { 686 struct cipher_test_sglists *tsgls; 687 688 tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL); 689 if (!tsgls) 690 return NULL; 691 692 if (init_test_sglist(&tsgls->src) != 0) 693 goto fail_kfree; 694 if (init_test_sglist(&tsgls->dst) != 0) 695 goto fail_destroy_src; 696 697 return tsgls; 698 699 fail_destroy_src: 700 destroy_test_sglist(&tsgls->src); 701 fail_kfree: 702 kfree(tsgls); 703 return NULL; 704 } 705 706 static void free_cipher_test_sglists(struct cipher_test_sglists *tsgls) 707 { 708 if (tsgls) { 709 destroy_test_sglist(&tsgls->src); 710 destroy_test_sglist(&tsgls->dst); 711 kfree(tsgls); 712 } 713 } 714 715 /* Build the src and dst scatterlists for an skcipher or AEAD test */ 716 static int build_cipher_test_sglists(struct cipher_test_sglists *tsgls, 717 const struct testvec_config *cfg, 718 unsigned int alignmask, 719 unsigned int src_total_len, 720 unsigned int dst_total_len, 721 const struct kvec *inputs, 722 unsigned int nr_inputs) 723 { 724 struct iov_iter input; 725 int err; 726 727 iov_iter_kvec(&input, WRITE, inputs, nr_inputs, src_total_len); 728 err = build_test_sglist(&tsgls->src, cfg->src_divs, alignmask, 729 cfg->inplace ? 730 max(dst_total_len, src_total_len) : 731 src_total_len, 732 &input, NULL); 733 if (err) 734 return err; 735 736 if (cfg->inplace) { 737 tsgls->dst.sgl_ptr = tsgls->src.sgl; 738 tsgls->dst.nents = tsgls->src.nents; 739 return 0; 740 } 741 return build_test_sglist(&tsgls->dst, 742 cfg->dst_divs[0].proportion_of_total ? 743 cfg->dst_divs : cfg->src_divs, 744 alignmask, dst_total_len, NULL, NULL); 745 } 746 747 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 748 static char *generate_random_sgl_divisions(struct test_sg_division *divs, 749 size_t max_divs, char *p, char *end, 750 bool gen_flushes, u32 req_flags) 751 { 752 struct test_sg_division *div = divs; 753 unsigned int remaining = TEST_SG_TOTAL; 754 755 do { 756 unsigned int this_len; 757 const char *flushtype_str; 758 759 if (div == &divs[max_divs - 1] || prandom_u32() % 2 == 0) 760 this_len = remaining; 761 else 762 this_len = 1 + (prandom_u32() % remaining); 763 div->proportion_of_total = this_len; 764 765 if (prandom_u32() % 4 == 0) 766 div->offset = (PAGE_SIZE - 128) + (prandom_u32() % 128); 767 else if (prandom_u32() % 2 == 0) 768 div->offset = prandom_u32() % 32; 769 else 770 div->offset = prandom_u32() % PAGE_SIZE; 771 if (prandom_u32() % 8 == 0) 772 div->offset_relative_to_alignmask = true; 773 774 div->flush_type = FLUSH_TYPE_NONE; 775 if (gen_flushes) { 776 switch (prandom_u32() % 4) { 777 case 0: 778 div->flush_type = FLUSH_TYPE_REIMPORT; 779 break; 780 case 1: 781 div->flush_type = FLUSH_TYPE_FLUSH; 782 break; 783 } 784 } 785 786 if (div->flush_type != FLUSH_TYPE_NONE && 787 !(req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && 788 prandom_u32() % 2 == 0) 789 div->nosimd = true; 790 791 switch (div->flush_type) { 792 case FLUSH_TYPE_FLUSH: 793 if (div->nosimd) 794 flushtype_str = "<flush,nosimd>"; 795 else 796 flushtype_str = "<flush>"; 797 break; 798 case FLUSH_TYPE_REIMPORT: 799 if (div->nosimd) 800 flushtype_str = "<reimport,nosimd>"; 801 else 802 flushtype_str = "<reimport>"; 803 break; 804 default: 805 flushtype_str = ""; 806 break; 807 } 808 809 BUILD_BUG_ON(TEST_SG_TOTAL != 10000); /* for "%u.%u%%" */ 810 p += scnprintf(p, end - p, "%s%u.%u%%@%s+%u%s", flushtype_str, 811 this_len / 100, this_len % 100, 812 div->offset_relative_to_alignmask ? 813 "alignmask" : "", 814 div->offset, this_len == remaining ? "" : ", "); 815 remaining -= this_len; 816 div++; 817 } while (remaining); 818 819 return p; 820 } 821 822 /* Generate a random testvec_config for fuzz testing */ 823 static void generate_random_testvec_config(struct testvec_config *cfg, 824 char *name, size_t max_namelen) 825 { 826 char *p = name; 827 char * const end = name + max_namelen; 828 829 memset(cfg, 0, sizeof(*cfg)); 830 831 cfg->name = name; 832 833 p += scnprintf(p, end - p, "random:"); 834 835 if (prandom_u32() % 2 == 0) { 836 cfg->inplace = true; 837 p += scnprintf(p, end - p, " inplace"); 838 } 839 840 if (prandom_u32() % 2 == 0) { 841 cfg->req_flags |= CRYPTO_TFM_REQ_MAY_SLEEP; 842 p += scnprintf(p, end - p, " may_sleep"); 843 } 844 845 switch (prandom_u32() % 4) { 846 case 0: 847 cfg->finalization_type = FINALIZATION_TYPE_FINAL; 848 p += scnprintf(p, end - p, " use_final"); 849 break; 850 case 1: 851 cfg->finalization_type = FINALIZATION_TYPE_FINUP; 852 p += scnprintf(p, end - p, " use_finup"); 853 break; 854 default: 855 cfg->finalization_type = FINALIZATION_TYPE_DIGEST; 856 p += scnprintf(p, end - p, " use_digest"); 857 break; 858 } 859 860 if (!(cfg->req_flags & CRYPTO_TFM_REQ_MAY_SLEEP) && 861 prandom_u32() % 2 == 0) { 862 cfg->nosimd = true; 863 p += scnprintf(p, end - p, " nosimd"); 864 } 865 866 p += scnprintf(p, end - p, " src_divs=["); 867 p = generate_random_sgl_divisions(cfg->src_divs, 868 ARRAY_SIZE(cfg->src_divs), p, end, 869 (cfg->finalization_type != 870 FINALIZATION_TYPE_DIGEST), 871 cfg->req_flags); 872 p += scnprintf(p, end - p, "]"); 873 874 if (!cfg->inplace && prandom_u32() % 2 == 0) { 875 p += scnprintf(p, end - p, " dst_divs=["); 876 p = generate_random_sgl_divisions(cfg->dst_divs, 877 ARRAY_SIZE(cfg->dst_divs), 878 p, end, false, 879 cfg->req_flags); 880 p += scnprintf(p, end - p, "]"); 881 } 882 883 if (prandom_u32() % 2 == 0) { 884 cfg->iv_offset = 1 + (prandom_u32() % MAX_ALGAPI_ALIGNMASK); 885 p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset); 886 } 887 888 WARN_ON_ONCE(!valid_testvec_config(cfg)); 889 } 890 891 static void crypto_disable_simd_for_test(void) 892 { 893 preempt_disable(); 894 __this_cpu_write(crypto_simd_disabled_for_test, true); 895 } 896 897 static void crypto_reenable_simd_for_test(void) 898 { 899 __this_cpu_write(crypto_simd_disabled_for_test, false); 900 preempt_enable(); 901 } 902 #else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */ 903 static void crypto_disable_simd_for_test(void) 904 { 905 } 906 907 static void crypto_reenable_simd_for_test(void) 908 { 909 } 910 #endif /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */ 911 912 static int do_ahash_op(int (*op)(struct ahash_request *req), 913 struct ahash_request *req, 914 struct crypto_wait *wait, bool nosimd) 915 { 916 int err; 917 918 if (nosimd) 919 crypto_disable_simd_for_test(); 920 921 err = op(req); 922 923 if (nosimd) 924 crypto_reenable_simd_for_test(); 925 926 return crypto_wait_req(err, wait); 927 } 928 929 static int check_nonfinal_hash_op(const char *op, int err, 930 u8 *result, unsigned int digestsize, 931 const char *driver, unsigned int vec_num, 932 const struct testvec_config *cfg) 933 { 934 if (err) { 935 pr_err("alg: hash: %s %s() failed with err %d on test vector %u, cfg=\"%s\"\n", 936 driver, op, err, vec_num, cfg->name); 937 return err; 938 } 939 if (!testmgr_is_poison(result, digestsize)) { 940 pr_err("alg: hash: %s %s() used result buffer on test vector %u, cfg=\"%s\"\n", 941 driver, op, vec_num, cfg->name); 942 return -EINVAL; 943 } 944 return 0; 945 } 946 947 static int test_hash_vec_cfg(const char *driver, 948 const struct hash_testvec *vec, 949 unsigned int vec_num, 950 const struct testvec_config *cfg, 951 struct ahash_request *req, 952 struct test_sglist *tsgl, 953 u8 *hashstate) 954 { 955 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 956 const unsigned int alignmask = crypto_ahash_alignmask(tfm); 957 const unsigned int digestsize = crypto_ahash_digestsize(tfm); 958 const unsigned int statesize = crypto_ahash_statesize(tfm); 959 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 960 const struct test_sg_division *divs[XBUFSIZE]; 961 DECLARE_CRYPTO_WAIT(wait); 962 struct kvec _input; 963 struct iov_iter input; 964 unsigned int i; 965 struct scatterlist *pending_sgl; 966 unsigned int pending_len; 967 u8 result[HASH_MAX_DIGESTSIZE + TESTMGR_POISON_LEN]; 968 int err; 969 970 /* Set the key, if specified */ 971 if (vec->ksize) { 972 err = crypto_ahash_setkey(tfm, vec->key, vec->ksize); 973 if (err) { 974 if (err == vec->setkey_error) 975 return 0; 976 pr_err("alg: hash: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n", 977 driver, vec_num, vec->setkey_error, err, 978 crypto_ahash_get_flags(tfm)); 979 return err; 980 } 981 if (vec->setkey_error) { 982 pr_err("alg: hash: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n", 983 driver, vec_num, vec->setkey_error); 984 return -EINVAL; 985 } 986 } 987 988 /* Build the scatterlist for the source data */ 989 _input.iov_base = (void *)vec->plaintext; 990 _input.iov_len = vec->psize; 991 iov_iter_kvec(&input, WRITE, &_input, 1, vec->psize); 992 err = build_test_sglist(tsgl, cfg->src_divs, alignmask, vec->psize, 993 &input, divs); 994 if (err) { 995 pr_err("alg: hash: %s: error preparing scatterlist for test vector %u, cfg=\"%s\"\n", 996 driver, vec_num, cfg->name); 997 return err; 998 } 999 1000 /* Do the actual hashing */ 1001 1002 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm)); 1003 testmgr_poison(result, digestsize + TESTMGR_POISON_LEN); 1004 1005 if (cfg->finalization_type == FINALIZATION_TYPE_DIGEST || 1006 vec->digest_error) { 1007 /* Just using digest() */ 1008 ahash_request_set_callback(req, req_flags, crypto_req_done, 1009 &wait); 1010 ahash_request_set_crypt(req, tsgl->sgl, result, vec->psize); 1011 err = do_ahash_op(crypto_ahash_digest, req, &wait, cfg->nosimd); 1012 if (err) { 1013 if (err == vec->digest_error) 1014 return 0; 1015 pr_err("alg: hash: %s digest() failed on test vector %u; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 1016 driver, vec_num, vec->digest_error, err, 1017 cfg->name); 1018 return err; 1019 } 1020 if (vec->digest_error) { 1021 pr_err("alg: hash: %s digest() unexpectedly succeeded on test vector %u; expected_error=%d, cfg=\"%s\"\n", 1022 driver, vec_num, vec->digest_error, cfg->name); 1023 return -EINVAL; 1024 } 1025 goto result_ready; 1026 } 1027 1028 /* Using init(), zero or more update(), then final() or finup() */ 1029 1030 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait); 1031 ahash_request_set_crypt(req, NULL, result, 0); 1032 err = do_ahash_op(crypto_ahash_init, req, &wait, cfg->nosimd); 1033 err = check_nonfinal_hash_op("init", err, result, digestsize, 1034 driver, vec_num, cfg); 1035 if (err) 1036 return err; 1037 1038 pending_sgl = NULL; 1039 pending_len = 0; 1040 for (i = 0; i < tsgl->nents; i++) { 1041 if (divs[i]->flush_type != FLUSH_TYPE_NONE && 1042 pending_sgl != NULL) { 1043 /* update() with the pending data */ 1044 ahash_request_set_callback(req, req_flags, 1045 crypto_req_done, &wait); 1046 ahash_request_set_crypt(req, pending_sgl, result, 1047 pending_len); 1048 err = do_ahash_op(crypto_ahash_update, req, &wait, 1049 divs[i]->nosimd); 1050 err = check_nonfinal_hash_op("update", err, 1051 result, digestsize, 1052 driver, vec_num, cfg); 1053 if (err) 1054 return err; 1055 pending_sgl = NULL; 1056 pending_len = 0; 1057 } 1058 if (divs[i]->flush_type == FLUSH_TYPE_REIMPORT) { 1059 /* Test ->export() and ->import() */ 1060 testmgr_poison(hashstate + statesize, 1061 TESTMGR_POISON_LEN); 1062 err = crypto_ahash_export(req, hashstate); 1063 err = check_nonfinal_hash_op("export", err, 1064 result, digestsize, 1065 driver, vec_num, cfg); 1066 if (err) 1067 return err; 1068 if (!testmgr_is_poison(hashstate + statesize, 1069 TESTMGR_POISON_LEN)) { 1070 pr_err("alg: hash: %s export() overran state buffer on test vector %u, cfg=\"%s\"\n", 1071 driver, vec_num, cfg->name); 1072 return -EOVERFLOW; 1073 } 1074 1075 testmgr_poison(req->__ctx, crypto_ahash_reqsize(tfm)); 1076 err = crypto_ahash_import(req, hashstate); 1077 err = check_nonfinal_hash_op("import", err, 1078 result, digestsize, 1079 driver, vec_num, cfg); 1080 if (err) 1081 return err; 1082 } 1083 if (pending_sgl == NULL) 1084 pending_sgl = &tsgl->sgl[i]; 1085 pending_len += tsgl->sgl[i].length; 1086 } 1087 1088 ahash_request_set_callback(req, req_flags, crypto_req_done, &wait); 1089 ahash_request_set_crypt(req, pending_sgl, result, pending_len); 1090 if (cfg->finalization_type == FINALIZATION_TYPE_FINAL) { 1091 /* finish with update() and final() */ 1092 err = do_ahash_op(crypto_ahash_update, req, &wait, cfg->nosimd); 1093 err = check_nonfinal_hash_op("update", err, result, digestsize, 1094 driver, vec_num, cfg); 1095 if (err) 1096 return err; 1097 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd); 1098 if (err) { 1099 pr_err("alg: hash: %s final() failed with err %d on test vector %u, cfg=\"%s\"\n", 1100 driver, err, vec_num, cfg->name); 1101 return err; 1102 } 1103 } else { 1104 /* finish with finup() */ 1105 err = do_ahash_op(crypto_ahash_finup, req, &wait, cfg->nosimd); 1106 if (err) { 1107 pr_err("alg: hash: %s finup() failed with err %d on test vector %u, cfg=\"%s\"\n", 1108 driver, err, vec_num, cfg->name); 1109 return err; 1110 } 1111 } 1112 1113 result_ready: 1114 /* Check that the algorithm produced the correct digest */ 1115 if (memcmp(result, vec->digest, digestsize) != 0) { 1116 pr_err("alg: hash: %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n", 1117 driver, vec_num, cfg->name); 1118 return -EINVAL; 1119 } 1120 if (!testmgr_is_poison(&result[digestsize], TESTMGR_POISON_LEN)) { 1121 pr_err("alg: hash: %s overran result buffer on test vector %u, cfg=\"%s\"\n", 1122 driver, vec_num, cfg->name); 1123 return -EOVERFLOW; 1124 } 1125 1126 return 0; 1127 } 1128 1129 static int test_hash_vec(const char *driver, const struct hash_testvec *vec, 1130 unsigned int vec_num, struct ahash_request *req, 1131 struct test_sglist *tsgl, u8 *hashstate) 1132 { 1133 unsigned int i; 1134 int err; 1135 1136 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) { 1137 err = test_hash_vec_cfg(driver, vec, vec_num, 1138 &default_hash_testvec_configs[i], 1139 req, tsgl, hashstate); 1140 if (err) 1141 return err; 1142 } 1143 1144 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 1145 if (!noextratests) { 1146 struct testvec_config cfg; 1147 char cfgname[TESTVEC_CONFIG_NAMELEN]; 1148 1149 for (i = 0; i < fuzz_iterations; i++) { 1150 generate_random_testvec_config(&cfg, cfgname, 1151 sizeof(cfgname)); 1152 err = test_hash_vec_cfg(driver, vec, vec_num, &cfg, 1153 req, tsgl, hashstate); 1154 if (err) 1155 return err; 1156 } 1157 } 1158 #endif 1159 return 0; 1160 } 1161 1162 static int __alg_test_hash(const struct hash_testvec *vecs, 1163 unsigned int num_vecs, const char *driver, 1164 u32 type, u32 mask) 1165 { 1166 struct crypto_ahash *tfm; 1167 struct ahash_request *req = NULL; 1168 struct test_sglist *tsgl = NULL; 1169 u8 *hashstate = NULL; 1170 unsigned int i; 1171 int err; 1172 1173 tfm = crypto_alloc_ahash(driver, type, mask); 1174 if (IS_ERR(tfm)) { 1175 pr_err("alg: hash: failed to allocate transform for %s: %ld\n", 1176 driver, PTR_ERR(tfm)); 1177 return PTR_ERR(tfm); 1178 } 1179 1180 req = ahash_request_alloc(tfm, GFP_KERNEL); 1181 if (!req) { 1182 pr_err("alg: hash: failed to allocate request for %s\n", 1183 driver); 1184 err = -ENOMEM; 1185 goto out; 1186 } 1187 1188 tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL); 1189 if (!tsgl || init_test_sglist(tsgl) != 0) { 1190 pr_err("alg: hash: failed to allocate test buffers for %s\n", 1191 driver); 1192 kfree(tsgl); 1193 tsgl = NULL; 1194 err = -ENOMEM; 1195 goto out; 1196 } 1197 1198 hashstate = kmalloc(crypto_ahash_statesize(tfm) + TESTMGR_POISON_LEN, 1199 GFP_KERNEL); 1200 if (!hashstate) { 1201 pr_err("alg: hash: failed to allocate hash state buffer for %s\n", 1202 driver); 1203 err = -ENOMEM; 1204 goto out; 1205 } 1206 1207 for (i = 0; i < num_vecs; i++) { 1208 err = test_hash_vec(driver, &vecs[i], i, req, tsgl, hashstate); 1209 if (err) 1210 goto out; 1211 } 1212 err = 0; 1213 out: 1214 kfree(hashstate); 1215 if (tsgl) { 1216 destroy_test_sglist(tsgl); 1217 kfree(tsgl); 1218 } 1219 ahash_request_free(req); 1220 crypto_free_ahash(tfm); 1221 return err; 1222 } 1223 1224 static int alg_test_hash(const struct alg_test_desc *desc, const char *driver, 1225 u32 type, u32 mask) 1226 { 1227 const struct hash_testvec *template = desc->suite.hash.vecs; 1228 unsigned int tcount = desc->suite.hash.count; 1229 unsigned int nr_unkeyed, nr_keyed; 1230 int err; 1231 1232 /* 1233 * For OPTIONAL_KEY algorithms, we have to do all the unkeyed tests 1234 * first, before setting a key on the tfm. To make this easier, we 1235 * require that the unkeyed test vectors (if any) are listed first. 1236 */ 1237 1238 for (nr_unkeyed = 0; nr_unkeyed < tcount; nr_unkeyed++) { 1239 if (template[nr_unkeyed].ksize) 1240 break; 1241 } 1242 for (nr_keyed = 0; nr_unkeyed + nr_keyed < tcount; nr_keyed++) { 1243 if (!template[nr_unkeyed + nr_keyed].ksize) { 1244 pr_err("alg: hash: test vectors for %s out of order, " 1245 "unkeyed ones must come first\n", desc->alg); 1246 return -EINVAL; 1247 } 1248 } 1249 1250 err = 0; 1251 if (nr_unkeyed) { 1252 err = __alg_test_hash(template, nr_unkeyed, driver, type, mask); 1253 template += nr_unkeyed; 1254 } 1255 1256 if (!err && nr_keyed) 1257 err = __alg_test_hash(template, nr_keyed, driver, type, mask); 1258 1259 return err; 1260 } 1261 1262 static int test_aead_vec_cfg(const char *driver, int enc, 1263 const struct aead_testvec *vec, 1264 unsigned int vec_num, 1265 const struct testvec_config *cfg, 1266 struct aead_request *req, 1267 struct cipher_test_sglists *tsgls) 1268 { 1269 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 1270 const unsigned int alignmask = crypto_aead_alignmask(tfm); 1271 const unsigned int ivsize = crypto_aead_ivsize(tfm); 1272 const unsigned int authsize = vec->clen - vec->plen; 1273 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 1274 const char *op = enc ? "encryption" : "decryption"; 1275 DECLARE_CRYPTO_WAIT(wait); 1276 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN]; 1277 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) + 1278 cfg->iv_offset + 1279 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0); 1280 struct kvec input[2]; 1281 int expected_error; 1282 int err; 1283 1284 /* Set the key */ 1285 if (vec->wk) 1286 crypto_aead_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 1287 else 1288 crypto_aead_clear_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 1289 err = crypto_aead_setkey(tfm, vec->key, vec->klen); 1290 if (err && err != vec->setkey_error) { 1291 pr_err("alg: aead: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n", 1292 driver, vec_num, vec->setkey_error, err, 1293 crypto_aead_get_flags(tfm)); 1294 return err; 1295 } 1296 if (!err && vec->setkey_error) { 1297 pr_err("alg: aead: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n", 1298 driver, vec_num, vec->setkey_error); 1299 return -EINVAL; 1300 } 1301 1302 /* Set the authentication tag size */ 1303 err = crypto_aead_setauthsize(tfm, authsize); 1304 if (err && err != vec->setauthsize_error) { 1305 pr_err("alg: aead: %s setauthsize failed on test vector %u; expected_error=%d, actual_error=%d\n", 1306 driver, vec_num, vec->setauthsize_error, err); 1307 return err; 1308 } 1309 if (!err && vec->setauthsize_error) { 1310 pr_err("alg: aead: %s setauthsize unexpectedly succeeded on test vector %u; expected_error=%d\n", 1311 driver, vec_num, vec->setauthsize_error); 1312 return -EINVAL; 1313 } 1314 1315 if (vec->setkey_error || vec->setauthsize_error) 1316 return 0; 1317 1318 /* The IV must be copied to a buffer, as the algorithm may modify it */ 1319 if (WARN_ON(ivsize > MAX_IVLEN)) 1320 return -EINVAL; 1321 if (vec->iv) 1322 memcpy(iv, vec->iv, ivsize); 1323 else 1324 memset(iv, 0, ivsize); 1325 1326 /* Build the src/dst scatterlists */ 1327 input[0].iov_base = (void *)vec->assoc; 1328 input[0].iov_len = vec->alen; 1329 input[1].iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; 1330 input[1].iov_len = enc ? vec->plen : vec->clen; 1331 err = build_cipher_test_sglists(tsgls, cfg, alignmask, 1332 vec->alen + (enc ? vec->plen : 1333 vec->clen), 1334 vec->alen + (enc ? vec->clen : 1335 vec->plen), 1336 input, 2); 1337 if (err) { 1338 pr_err("alg: aead: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n", 1339 driver, op, vec_num, cfg->name); 1340 return err; 1341 } 1342 1343 /* Do the actual encryption or decryption */ 1344 testmgr_poison(req->__ctx, crypto_aead_reqsize(tfm)); 1345 aead_request_set_callback(req, req_flags, crypto_req_done, &wait); 1346 aead_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, 1347 enc ? vec->plen : vec->clen, iv); 1348 aead_request_set_ad(req, vec->alen); 1349 if (cfg->nosimd) 1350 crypto_disable_simd_for_test(); 1351 err = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req); 1352 if (cfg->nosimd) 1353 crypto_reenable_simd_for_test(); 1354 err = crypto_wait_req(err, &wait); 1355 1356 /* Check that the algorithm didn't overwrite things it shouldn't have */ 1357 if (req->cryptlen != (enc ? vec->plen : vec->clen) || 1358 req->assoclen != vec->alen || 1359 req->iv != iv || 1360 req->src != tsgls->src.sgl_ptr || 1361 req->dst != tsgls->dst.sgl_ptr || 1362 crypto_aead_reqtfm(req) != tfm || 1363 req->base.complete != crypto_req_done || 1364 req->base.flags != req_flags || 1365 req->base.data != &wait) { 1366 pr_err("alg: aead: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n", 1367 driver, op, vec_num, cfg->name); 1368 if (req->cryptlen != (enc ? vec->plen : vec->clen)) 1369 pr_err("alg: aead: changed 'req->cryptlen'\n"); 1370 if (req->assoclen != vec->alen) 1371 pr_err("alg: aead: changed 'req->assoclen'\n"); 1372 if (req->iv != iv) 1373 pr_err("alg: aead: changed 'req->iv'\n"); 1374 if (req->src != tsgls->src.sgl_ptr) 1375 pr_err("alg: aead: changed 'req->src'\n"); 1376 if (req->dst != tsgls->dst.sgl_ptr) 1377 pr_err("alg: aead: changed 'req->dst'\n"); 1378 if (crypto_aead_reqtfm(req) != tfm) 1379 pr_err("alg: aead: changed 'req->base.tfm'\n"); 1380 if (req->base.complete != crypto_req_done) 1381 pr_err("alg: aead: changed 'req->base.complete'\n"); 1382 if (req->base.flags != req_flags) 1383 pr_err("alg: aead: changed 'req->base.flags'\n"); 1384 if (req->base.data != &wait) 1385 pr_err("alg: aead: changed 'req->base.data'\n"); 1386 return -EINVAL; 1387 } 1388 if (is_test_sglist_corrupted(&tsgls->src)) { 1389 pr_err("alg: aead: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n", 1390 driver, op, vec_num, cfg->name); 1391 return -EINVAL; 1392 } 1393 if (tsgls->dst.sgl_ptr != tsgls->src.sgl && 1394 is_test_sglist_corrupted(&tsgls->dst)) { 1395 pr_err("alg: aead: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n", 1396 driver, op, vec_num, cfg->name); 1397 return -EINVAL; 1398 } 1399 1400 /* Check for success or failure */ 1401 expected_error = vec->novrfy ? -EBADMSG : vec->crypt_error; 1402 if (err) { 1403 if (err == expected_error) 1404 return 0; 1405 pr_err("alg: aead: %s %s failed on test vector %u; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 1406 driver, op, vec_num, expected_error, err, cfg->name); 1407 return err; 1408 } 1409 if (expected_error) { 1410 pr_err("alg: aead: %s %s unexpectedly succeeded on test vector %u; expected_error=%d, cfg=\"%s\"\n", 1411 driver, op, vec_num, expected_error, cfg->name); 1412 return -EINVAL; 1413 } 1414 1415 /* Check for the correct output (ciphertext or plaintext) */ 1416 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext, 1417 enc ? vec->clen : vec->plen, 1418 vec->alen, enc || !cfg->inplace); 1419 if (err == -EOVERFLOW) { 1420 pr_err("alg: aead: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n", 1421 driver, op, vec_num, cfg->name); 1422 return err; 1423 } 1424 if (err) { 1425 pr_err("alg: aead: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n", 1426 driver, op, vec_num, cfg->name); 1427 return err; 1428 } 1429 1430 return 0; 1431 } 1432 1433 static int test_aead_vec(const char *driver, int enc, 1434 const struct aead_testvec *vec, unsigned int vec_num, 1435 struct aead_request *req, 1436 struct cipher_test_sglists *tsgls) 1437 { 1438 unsigned int i; 1439 int err; 1440 1441 if (enc && vec->novrfy) 1442 return 0; 1443 1444 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) { 1445 err = test_aead_vec_cfg(driver, enc, vec, vec_num, 1446 &default_cipher_testvec_configs[i], 1447 req, tsgls); 1448 if (err) 1449 return err; 1450 } 1451 1452 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 1453 if (!noextratests) { 1454 struct testvec_config cfg; 1455 char cfgname[TESTVEC_CONFIG_NAMELEN]; 1456 1457 for (i = 0; i < fuzz_iterations; i++) { 1458 generate_random_testvec_config(&cfg, cfgname, 1459 sizeof(cfgname)); 1460 err = test_aead_vec_cfg(driver, enc, vec, vec_num, 1461 &cfg, req, tsgls); 1462 if (err) 1463 return err; 1464 } 1465 } 1466 #endif 1467 return 0; 1468 } 1469 1470 static int test_aead(const char *driver, int enc, 1471 const struct aead_test_suite *suite, 1472 struct aead_request *req, 1473 struct cipher_test_sglists *tsgls) 1474 { 1475 unsigned int i; 1476 int err; 1477 1478 for (i = 0; i < suite->count; i++) { 1479 err = test_aead_vec(driver, enc, &suite->vecs[i], i, req, 1480 tsgls); 1481 if (err) 1482 return err; 1483 } 1484 return 0; 1485 } 1486 1487 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver, 1488 u32 type, u32 mask) 1489 { 1490 const struct aead_test_suite *suite = &desc->suite.aead; 1491 struct crypto_aead *tfm; 1492 struct aead_request *req = NULL; 1493 struct cipher_test_sglists *tsgls = NULL; 1494 int err; 1495 1496 if (suite->count <= 0) { 1497 pr_err("alg: aead: empty test suite for %s\n", driver); 1498 return -EINVAL; 1499 } 1500 1501 tfm = crypto_alloc_aead(driver, type, mask); 1502 if (IS_ERR(tfm)) { 1503 pr_err("alg: aead: failed to allocate transform for %s: %ld\n", 1504 driver, PTR_ERR(tfm)); 1505 return PTR_ERR(tfm); 1506 } 1507 1508 req = aead_request_alloc(tfm, GFP_KERNEL); 1509 if (!req) { 1510 pr_err("alg: aead: failed to allocate request for %s\n", 1511 driver); 1512 err = -ENOMEM; 1513 goto out; 1514 } 1515 1516 tsgls = alloc_cipher_test_sglists(); 1517 if (!tsgls) { 1518 pr_err("alg: aead: failed to allocate test buffers for %s\n", 1519 driver); 1520 err = -ENOMEM; 1521 goto out; 1522 } 1523 1524 err = test_aead(driver, ENCRYPT, suite, req, tsgls); 1525 if (err) 1526 goto out; 1527 1528 err = test_aead(driver, DECRYPT, suite, req, tsgls); 1529 out: 1530 free_cipher_test_sglists(tsgls); 1531 aead_request_free(req); 1532 crypto_free_aead(tfm); 1533 return err; 1534 } 1535 1536 static int test_cipher(struct crypto_cipher *tfm, int enc, 1537 const struct cipher_testvec *template, 1538 unsigned int tcount) 1539 { 1540 const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm)); 1541 unsigned int i, j, k; 1542 char *q; 1543 const char *e; 1544 const char *input, *result; 1545 void *data; 1546 char *xbuf[XBUFSIZE]; 1547 int ret = -ENOMEM; 1548 1549 if (testmgr_alloc_buf(xbuf)) 1550 goto out_nobuf; 1551 1552 if (enc == ENCRYPT) 1553 e = "encryption"; 1554 else 1555 e = "decryption"; 1556 1557 j = 0; 1558 for (i = 0; i < tcount; i++) { 1559 1560 if (fips_enabled && template[i].fips_skip) 1561 continue; 1562 1563 input = enc ? template[i].ptext : template[i].ctext; 1564 result = enc ? template[i].ctext : template[i].ptext; 1565 j++; 1566 1567 ret = -EINVAL; 1568 if (WARN_ON(template[i].len > PAGE_SIZE)) 1569 goto out; 1570 1571 data = xbuf[0]; 1572 memcpy(data, input, template[i].len); 1573 1574 crypto_cipher_clear_flags(tfm, ~0); 1575 if (template[i].wk) 1576 crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 1577 1578 ret = crypto_cipher_setkey(tfm, template[i].key, 1579 template[i].klen); 1580 if (ret) { 1581 if (ret == template[i].setkey_error) 1582 continue; 1583 pr_err("alg: cipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n", 1584 algo, j, template[i].setkey_error, ret, 1585 crypto_cipher_get_flags(tfm)); 1586 goto out; 1587 } 1588 if (template[i].setkey_error) { 1589 pr_err("alg: cipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n", 1590 algo, j, template[i].setkey_error); 1591 ret = -EINVAL; 1592 goto out; 1593 } 1594 1595 for (k = 0; k < template[i].len; 1596 k += crypto_cipher_blocksize(tfm)) { 1597 if (enc) 1598 crypto_cipher_encrypt_one(tfm, data + k, 1599 data + k); 1600 else 1601 crypto_cipher_decrypt_one(tfm, data + k, 1602 data + k); 1603 } 1604 1605 q = data; 1606 if (memcmp(q, result, template[i].len)) { 1607 printk(KERN_ERR "alg: cipher: Test %d failed " 1608 "on %s for %s\n", j, e, algo); 1609 hexdump(q, template[i].len); 1610 ret = -EINVAL; 1611 goto out; 1612 } 1613 } 1614 1615 ret = 0; 1616 1617 out: 1618 testmgr_free_buf(xbuf); 1619 out_nobuf: 1620 return ret; 1621 } 1622 1623 static int test_skcipher_vec_cfg(const char *driver, int enc, 1624 const struct cipher_testvec *vec, 1625 unsigned int vec_num, 1626 const struct testvec_config *cfg, 1627 struct skcipher_request *req, 1628 struct cipher_test_sglists *tsgls) 1629 { 1630 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1631 const unsigned int alignmask = crypto_skcipher_alignmask(tfm); 1632 const unsigned int ivsize = crypto_skcipher_ivsize(tfm); 1633 const u32 req_flags = CRYPTO_TFM_REQ_MAY_BACKLOG | cfg->req_flags; 1634 const char *op = enc ? "encryption" : "decryption"; 1635 DECLARE_CRYPTO_WAIT(wait); 1636 u8 _iv[3 * (MAX_ALGAPI_ALIGNMASK + 1) + MAX_IVLEN]; 1637 u8 *iv = PTR_ALIGN(&_iv[0], 2 * (MAX_ALGAPI_ALIGNMASK + 1)) + 1638 cfg->iv_offset + 1639 (cfg->iv_offset_relative_to_alignmask ? alignmask : 0); 1640 struct kvec input; 1641 int err; 1642 1643 /* Set the key */ 1644 if (vec->wk) 1645 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 1646 else 1647 crypto_skcipher_clear_flags(tfm, 1648 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); 1649 err = crypto_skcipher_setkey(tfm, vec->key, vec->klen); 1650 if (err) { 1651 if (err == vec->setkey_error) 1652 return 0; 1653 pr_err("alg: skcipher: %s setkey failed on test vector %u; expected_error=%d, actual_error=%d, flags=%#x\n", 1654 driver, vec_num, vec->setkey_error, err, 1655 crypto_skcipher_get_flags(tfm)); 1656 return err; 1657 } 1658 if (vec->setkey_error) { 1659 pr_err("alg: skcipher: %s setkey unexpectedly succeeded on test vector %u; expected_error=%d\n", 1660 driver, vec_num, vec->setkey_error); 1661 return -EINVAL; 1662 } 1663 1664 /* The IV must be copied to a buffer, as the algorithm may modify it */ 1665 if (ivsize) { 1666 if (WARN_ON(ivsize > MAX_IVLEN)) 1667 return -EINVAL; 1668 if (vec->generates_iv && !enc) 1669 memcpy(iv, vec->iv_out, ivsize); 1670 else if (vec->iv) 1671 memcpy(iv, vec->iv, ivsize); 1672 else 1673 memset(iv, 0, ivsize); 1674 } else { 1675 if (vec->generates_iv) { 1676 pr_err("alg: skcipher: %s has ivsize=0 but test vector %u generates IV!\n", 1677 driver, vec_num); 1678 return -EINVAL; 1679 } 1680 iv = NULL; 1681 } 1682 1683 /* Build the src/dst scatterlists */ 1684 input.iov_base = enc ? (void *)vec->ptext : (void *)vec->ctext; 1685 input.iov_len = vec->len; 1686 err = build_cipher_test_sglists(tsgls, cfg, alignmask, 1687 vec->len, vec->len, &input, 1); 1688 if (err) { 1689 pr_err("alg: skcipher: %s %s: error preparing scatterlists for test vector %u, cfg=\"%s\"\n", 1690 driver, op, vec_num, cfg->name); 1691 return err; 1692 } 1693 1694 /* Do the actual encryption or decryption */ 1695 testmgr_poison(req->__ctx, crypto_skcipher_reqsize(tfm)); 1696 skcipher_request_set_callback(req, req_flags, crypto_req_done, &wait); 1697 skcipher_request_set_crypt(req, tsgls->src.sgl_ptr, tsgls->dst.sgl_ptr, 1698 vec->len, iv); 1699 if (cfg->nosimd) 1700 crypto_disable_simd_for_test(); 1701 err = enc ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req); 1702 if (cfg->nosimd) 1703 crypto_reenable_simd_for_test(); 1704 err = crypto_wait_req(err, &wait); 1705 1706 /* Check that the algorithm didn't overwrite things it shouldn't have */ 1707 if (req->cryptlen != vec->len || 1708 req->iv != iv || 1709 req->src != tsgls->src.sgl_ptr || 1710 req->dst != tsgls->dst.sgl_ptr || 1711 crypto_skcipher_reqtfm(req) != tfm || 1712 req->base.complete != crypto_req_done || 1713 req->base.flags != req_flags || 1714 req->base.data != &wait) { 1715 pr_err("alg: skcipher: %s %s corrupted request struct on test vector %u, cfg=\"%s\"\n", 1716 driver, op, vec_num, cfg->name); 1717 if (req->cryptlen != vec->len) 1718 pr_err("alg: skcipher: changed 'req->cryptlen'\n"); 1719 if (req->iv != iv) 1720 pr_err("alg: skcipher: changed 'req->iv'\n"); 1721 if (req->src != tsgls->src.sgl_ptr) 1722 pr_err("alg: skcipher: changed 'req->src'\n"); 1723 if (req->dst != tsgls->dst.sgl_ptr) 1724 pr_err("alg: skcipher: changed 'req->dst'\n"); 1725 if (crypto_skcipher_reqtfm(req) != tfm) 1726 pr_err("alg: skcipher: changed 'req->base.tfm'\n"); 1727 if (req->base.complete != crypto_req_done) 1728 pr_err("alg: skcipher: changed 'req->base.complete'\n"); 1729 if (req->base.flags != req_flags) 1730 pr_err("alg: skcipher: changed 'req->base.flags'\n"); 1731 if (req->base.data != &wait) 1732 pr_err("alg: skcipher: changed 'req->base.data'\n"); 1733 return -EINVAL; 1734 } 1735 if (is_test_sglist_corrupted(&tsgls->src)) { 1736 pr_err("alg: skcipher: %s %s corrupted src sgl on test vector %u, cfg=\"%s\"\n", 1737 driver, op, vec_num, cfg->name); 1738 return -EINVAL; 1739 } 1740 if (tsgls->dst.sgl_ptr != tsgls->src.sgl && 1741 is_test_sglist_corrupted(&tsgls->dst)) { 1742 pr_err("alg: skcipher: %s %s corrupted dst sgl on test vector %u, cfg=\"%s\"\n", 1743 driver, op, vec_num, cfg->name); 1744 return -EINVAL; 1745 } 1746 1747 /* Check for success or failure */ 1748 if (err) { 1749 if (err == vec->crypt_error) 1750 return 0; 1751 pr_err("alg: skcipher: %s %s failed on test vector %u; expected_error=%d, actual_error=%d, cfg=\"%s\"\n", 1752 driver, op, vec_num, vec->crypt_error, err, cfg->name); 1753 return err; 1754 } 1755 if (vec->crypt_error) { 1756 pr_err("alg: skcipher: %s %s unexpectedly succeeded on test vector %u; expected_error=%d, cfg=\"%s\"\n", 1757 driver, op, vec_num, vec->crypt_error, cfg->name); 1758 return -EINVAL; 1759 } 1760 1761 /* Check for the correct output (ciphertext or plaintext) */ 1762 err = verify_correct_output(&tsgls->dst, enc ? vec->ctext : vec->ptext, 1763 vec->len, 0, true); 1764 if (err == -EOVERFLOW) { 1765 pr_err("alg: skcipher: %s %s overran dst buffer on test vector %u, cfg=\"%s\"\n", 1766 driver, op, vec_num, cfg->name); 1767 return err; 1768 } 1769 if (err) { 1770 pr_err("alg: skcipher: %s %s test failed (wrong result) on test vector %u, cfg=\"%s\"\n", 1771 driver, op, vec_num, cfg->name); 1772 return err; 1773 } 1774 1775 /* If applicable, check that the algorithm generated the correct IV */ 1776 if (vec->iv_out && memcmp(iv, vec->iv_out, ivsize) != 0) { 1777 pr_err("alg: skcipher: %s %s test failed (wrong output IV) on test vector %u, cfg=\"%s\"\n", 1778 driver, op, vec_num, cfg->name); 1779 hexdump(iv, ivsize); 1780 return -EINVAL; 1781 } 1782 1783 return 0; 1784 } 1785 1786 static int test_skcipher_vec(const char *driver, int enc, 1787 const struct cipher_testvec *vec, 1788 unsigned int vec_num, 1789 struct skcipher_request *req, 1790 struct cipher_test_sglists *tsgls) 1791 { 1792 unsigned int i; 1793 int err; 1794 1795 if (fips_enabled && vec->fips_skip) 1796 return 0; 1797 1798 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) { 1799 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num, 1800 &default_cipher_testvec_configs[i], 1801 req, tsgls); 1802 if (err) 1803 return err; 1804 } 1805 1806 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 1807 if (!noextratests) { 1808 struct testvec_config cfg; 1809 char cfgname[TESTVEC_CONFIG_NAMELEN]; 1810 1811 for (i = 0; i < fuzz_iterations; i++) { 1812 generate_random_testvec_config(&cfg, cfgname, 1813 sizeof(cfgname)); 1814 err = test_skcipher_vec_cfg(driver, enc, vec, vec_num, 1815 &cfg, req, tsgls); 1816 if (err) 1817 return err; 1818 } 1819 } 1820 #endif 1821 return 0; 1822 } 1823 1824 static int test_skcipher(const char *driver, int enc, 1825 const struct cipher_test_suite *suite, 1826 struct skcipher_request *req, 1827 struct cipher_test_sglists *tsgls) 1828 { 1829 unsigned int i; 1830 int err; 1831 1832 for (i = 0; i < suite->count; i++) { 1833 err = test_skcipher_vec(driver, enc, &suite->vecs[i], i, req, 1834 tsgls); 1835 if (err) 1836 return err; 1837 } 1838 return 0; 1839 } 1840 1841 static int alg_test_skcipher(const struct alg_test_desc *desc, 1842 const char *driver, u32 type, u32 mask) 1843 { 1844 const struct cipher_test_suite *suite = &desc->suite.cipher; 1845 struct crypto_skcipher *tfm; 1846 struct skcipher_request *req = NULL; 1847 struct cipher_test_sglists *tsgls = NULL; 1848 int err; 1849 1850 if (suite->count <= 0) { 1851 pr_err("alg: skcipher: empty test suite for %s\n", driver); 1852 return -EINVAL; 1853 } 1854 1855 tfm = crypto_alloc_skcipher(driver, type, mask); 1856 if (IS_ERR(tfm)) { 1857 pr_err("alg: skcipher: failed to allocate transform for %s: %ld\n", 1858 driver, PTR_ERR(tfm)); 1859 return PTR_ERR(tfm); 1860 } 1861 1862 req = skcipher_request_alloc(tfm, GFP_KERNEL); 1863 if (!req) { 1864 pr_err("alg: skcipher: failed to allocate request for %s\n", 1865 driver); 1866 err = -ENOMEM; 1867 goto out; 1868 } 1869 1870 tsgls = alloc_cipher_test_sglists(); 1871 if (!tsgls) { 1872 pr_err("alg: skcipher: failed to allocate test buffers for %s\n", 1873 driver); 1874 err = -ENOMEM; 1875 goto out; 1876 } 1877 1878 err = test_skcipher(driver, ENCRYPT, suite, req, tsgls); 1879 if (err) 1880 goto out; 1881 1882 err = test_skcipher(driver, DECRYPT, suite, req, tsgls); 1883 out: 1884 free_cipher_test_sglists(tsgls); 1885 skcipher_request_free(req); 1886 crypto_free_skcipher(tfm); 1887 return err; 1888 } 1889 1890 static int test_comp(struct crypto_comp *tfm, 1891 const struct comp_testvec *ctemplate, 1892 const struct comp_testvec *dtemplate, 1893 int ctcount, int dtcount) 1894 { 1895 const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm)); 1896 char *output, *decomp_output; 1897 unsigned int i; 1898 int ret; 1899 1900 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 1901 if (!output) 1902 return -ENOMEM; 1903 1904 decomp_output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 1905 if (!decomp_output) { 1906 kfree(output); 1907 return -ENOMEM; 1908 } 1909 1910 for (i = 0; i < ctcount; i++) { 1911 int ilen; 1912 unsigned int dlen = COMP_BUF_SIZE; 1913 1914 memset(output, 0, COMP_BUF_SIZE); 1915 memset(decomp_output, 0, COMP_BUF_SIZE); 1916 1917 ilen = ctemplate[i].inlen; 1918 ret = crypto_comp_compress(tfm, ctemplate[i].input, 1919 ilen, output, &dlen); 1920 if (ret) { 1921 printk(KERN_ERR "alg: comp: compression failed " 1922 "on test %d for %s: ret=%d\n", i + 1, algo, 1923 -ret); 1924 goto out; 1925 } 1926 1927 ilen = dlen; 1928 dlen = COMP_BUF_SIZE; 1929 ret = crypto_comp_decompress(tfm, output, 1930 ilen, decomp_output, &dlen); 1931 if (ret) { 1932 pr_err("alg: comp: compression failed: decompress: on test %d for %s failed: ret=%d\n", 1933 i + 1, algo, -ret); 1934 goto out; 1935 } 1936 1937 if (dlen != ctemplate[i].inlen) { 1938 printk(KERN_ERR "alg: comp: Compression test %d " 1939 "failed for %s: output len = %d\n", i + 1, algo, 1940 dlen); 1941 ret = -EINVAL; 1942 goto out; 1943 } 1944 1945 if (memcmp(decomp_output, ctemplate[i].input, 1946 ctemplate[i].inlen)) { 1947 pr_err("alg: comp: compression failed: output differs: on test %d for %s\n", 1948 i + 1, algo); 1949 hexdump(decomp_output, dlen); 1950 ret = -EINVAL; 1951 goto out; 1952 } 1953 } 1954 1955 for (i = 0; i < dtcount; i++) { 1956 int ilen; 1957 unsigned int dlen = COMP_BUF_SIZE; 1958 1959 memset(decomp_output, 0, COMP_BUF_SIZE); 1960 1961 ilen = dtemplate[i].inlen; 1962 ret = crypto_comp_decompress(tfm, dtemplate[i].input, 1963 ilen, decomp_output, &dlen); 1964 if (ret) { 1965 printk(KERN_ERR "alg: comp: decompression failed " 1966 "on test %d for %s: ret=%d\n", i + 1, algo, 1967 -ret); 1968 goto out; 1969 } 1970 1971 if (dlen != dtemplate[i].outlen) { 1972 printk(KERN_ERR "alg: comp: Decompression test %d " 1973 "failed for %s: output len = %d\n", i + 1, algo, 1974 dlen); 1975 ret = -EINVAL; 1976 goto out; 1977 } 1978 1979 if (memcmp(decomp_output, dtemplate[i].output, dlen)) { 1980 printk(KERN_ERR "alg: comp: Decompression test %d " 1981 "failed for %s\n", i + 1, algo); 1982 hexdump(decomp_output, dlen); 1983 ret = -EINVAL; 1984 goto out; 1985 } 1986 } 1987 1988 ret = 0; 1989 1990 out: 1991 kfree(decomp_output); 1992 kfree(output); 1993 return ret; 1994 } 1995 1996 static int test_acomp(struct crypto_acomp *tfm, 1997 const struct comp_testvec *ctemplate, 1998 const struct comp_testvec *dtemplate, 1999 int ctcount, int dtcount) 2000 { 2001 const char *algo = crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm)); 2002 unsigned int i; 2003 char *output, *decomp_out; 2004 int ret; 2005 struct scatterlist src, dst; 2006 struct acomp_req *req; 2007 struct crypto_wait wait; 2008 2009 output = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 2010 if (!output) 2011 return -ENOMEM; 2012 2013 decomp_out = kmalloc(COMP_BUF_SIZE, GFP_KERNEL); 2014 if (!decomp_out) { 2015 kfree(output); 2016 return -ENOMEM; 2017 } 2018 2019 for (i = 0; i < ctcount; i++) { 2020 unsigned int dlen = COMP_BUF_SIZE; 2021 int ilen = ctemplate[i].inlen; 2022 void *input_vec; 2023 2024 input_vec = kmemdup(ctemplate[i].input, ilen, GFP_KERNEL); 2025 if (!input_vec) { 2026 ret = -ENOMEM; 2027 goto out; 2028 } 2029 2030 memset(output, 0, dlen); 2031 crypto_init_wait(&wait); 2032 sg_init_one(&src, input_vec, ilen); 2033 sg_init_one(&dst, output, dlen); 2034 2035 req = acomp_request_alloc(tfm); 2036 if (!req) { 2037 pr_err("alg: acomp: request alloc failed for %s\n", 2038 algo); 2039 kfree(input_vec); 2040 ret = -ENOMEM; 2041 goto out; 2042 } 2043 2044 acomp_request_set_params(req, &src, &dst, ilen, dlen); 2045 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2046 crypto_req_done, &wait); 2047 2048 ret = crypto_wait_req(crypto_acomp_compress(req), &wait); 2049 if (ret) { 2050 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n", 2051 i + 1, algo, -ret); 2052 kfree(input_vec); 2053 acomp_request_free(req); 2054 goto out; 2055 } 2056 2057 ilen = req->dlen; 2058 dlen = COMP_BUF_SIZE; 2059 sg_init_one(&src, output, ilen); 2060 sg_init_one(&dst, decomp_out, dlen); 2061 crypto_init_wait(&wait); 2062 acomp_request_set_params(req, &src, &dst, ilen, dlen); 2063 2064 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait); 2065 if (ret) { 2066 pr_err("alg: acomp: compression failed on test %d for %s: ret=%d\n", 2067 i + 1, algo, -ret); 2068 kfree(input_vec); 2069 acomp_request_free(req); 2070 goto out; 2071 } 2072 2073 if (req->dlen != ctemplate[i].inlen) { 2074 pr_err("alg: acomp: Compression test %d failed for %s: output len = %d\n", 2075 i + 1, algo, req->dlen); 2076 ret = -EINVAL; 2077 kfree(input_vec); 2078 acomp_request_free(req); 2079 goto out; 2080 } 2081 2082 if (memcmp(input_vec, decomp_out, req->dlen)) { 2083 pr_err("alg: acomp: Compression test %d failed for %s\n", 2084 i + 1, algo); 2085 hexdump(output, req->dlen); 2086 ret = -EINVAL; 2087 kfree(input_vec); 2088 acomp_request_free(req); 2089 goto out; 2090 } 2091 2092 kfree(input_vec); 2093 acomp_request_free(req); 2094 } 2095 2096 for (i = 0; i < dtcount; i++) { 2097 unsigned int dlen = COMP_BUF_SIZE; 2098 int ilen = dtemplate[i].inlen; 2099 void *input_vec; 2100 2101 input_vec = kmemdup(dtemplate[i].input, ilen, GFP_KERNEL); 2102 if (!input_vec) { 2103 ret = -ENOMEM; 2104 goto out; 2105 } 2106 2107 memset(output, 0, dlen); 2108 crypto_init_wait(&wait); 2109 sg_init_one(&src, input_vec, ilen); 2110 sg_init_one(&dst, output, dlen); 2111 2112 req = acomp_request_alloc(tfm); 2113 if (!req) { 2114 pr_err("alg: acomp: request alloc failed for %s\n", 2115 algo); 2116 kfree(input_vec); 2117 ret = -ENOMEM; 2118 goto out; 2119 } 2120 2121 acomp_request_set_params(req, &src, &dst, ilen, dlen); 2122 acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2123 crypto_req_done, &wait); 2124 2125 ret = crypto_wait_req(crypto_acomp_decompress(req), &wait); 2126 if (ret) { 2127 pr_err("alg: acomp: decompression failed on test %d for %s: ret=%d\n", 2128 i + 1, algo, -ret); 2129 kfree(input_vec); 2130 acomp_request_free(req); 2131 goto out; 2132 } 2133 2134 if (req->dlen != dtemplate[i].outlen) { 2135 pr_err("alg: acomp: Decompression test %d failed for %s: output len = %d\n", 2136 i + 1, algo, req->dlen); 2137 ret = -EINVAL; 2138 kfree(input_vec); 2139 acomp_request_free(req); 2140 goto out; 2141 } 2142 2143 if (memcmp(output, dtemplate[i].output, req->dlen)) { 2144 pr_err("alg: acomp: Decompression test %d failed for %s\n", 2145 i + 1, algo); 2146 hexdump(output, req->dlen); 2147 ret = -EINVAL; 2148 kfree(input_vec); 2149 acomp_request_free(req); 2150 goto out; 2151 } 2152 2153 kfree(input_vec); 2154 acomp_request_free(req); 2155 } 2156 2157 ret = 0; 2158 2159 out: 2160 kfree(decomp_out); 2161 kfree(output); 2162 return ret; 2163 } 2164 2165 static int test_cprng(struct crypto_rng *tfm, 2166 const struct cprng_testvec *template, 2167 unsigned int tcount) 2168 { 2169 const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm)); 2170 int err = 0, i, j, seedsize; 2171 u8 *seed; 2172 char result[32]; 2173 2174 seedsize = crypto_rng_seedsize(tfm); 2175 2176 seed = kmalloc(seedsize, GFP_KERNEL); 2177 if (!seed) { 2178 printk(KERN_ERR "alg: cprng: Failed to allocate seed space " 2179 "for %s\n", algo); 2180 return -ENOMEM; 2181 } 2182 2183 for (i = 0; i < tcount; i++) { 2184 memset(result, 0, 32); 2185 2186 memcpy(seed, template[i].v, template[i].vlen); 2187 memcpy(seed + template[i].vlen, template[i].key, 2188 template[i].klen); 2189 memcpy(seed + template[i].vlen + template[i].klen, 2190 template[i].dt, template[i].dtlen); 2191 2192 err = crypto_rng_reset(tfm, seed, seedsize); 2193 if (err) { 2194 printk(KERN_ERR "alg: cprng: Failed to reset rng " 2195 "for %s\n", algo); 2196 goto out; 2197 } 2198 2199 for (j = 0; j < template[i].loops; j++) { 2200 err = crypto_rng_get_bytes(tfm, result, 2201 template[i].rlen); 2202 if (err < 0) { 2203 printk(KERN_ERR "alg: cprng: Failed to obtain " 2204 "the correct amount of random data for " 2205 "%s (requested %d)\n", algo, 2206 template[i].rlen); 2207 goto out; 2208 } 2209 } 2210 2211 err = memcmp(result, template[i].result, 2212 template[i].rlen); 2213 if (err) { 2214 printk(KERN_ERR "alg: cprng: Test %d failed for %s\n", 2215 i, algo); 2216 hexdump(result, template[i].rlen); 2217 err = -EINVAL; 2218 goto out; 2219 } 2220 } 2221 2222 out: 2223 kfree(seed); 2224 return err; 2225 } 2226 2227 static int alg_test_cipher(const struct alg_test_desc *desc, 2228 const char *driver, u32 type, u32 mask) 2229 { 2230 const struct cipher_test_suite *suite = &desc->suite.cipher; 2231 struct crypto_cipher *tfm; 2232 int err; 2233 2234 tfm = crypto_alloc_cipher(driver, type, mask); 2235 if (IS_ERR(tfm)) { 2236 printk(KERN_ERR "alg: cipher: Failed to load transform for " 2237 "%s: %ld\n", driver, PTR_ERR(tfm)); 2238 return PTR_ERR(tfm); 2239 } 2240 2241 err = test_cipher(tfm, ENCRYPT, suite->vecs, suite->count); 2242 if (!err) 2243 err = test_cipher(tfm, DECRYPT, suite->vecs, suite->count); 2244 2245 crypto_free_cipher(tfm); 2246 return err; 2247 } 2248 2249 static int alg_test_comp(const struct alg_test_desc *desc, const char *driver, 2250 u32 type, u32 mask) 2251 { 2252 struct crypto_comp *comp; 2253 struct crypto_acomp *acomp; 2254 int err; 2255 u32 algo_type = type & CRYPTO_ALG_TYPE_ACOMPRESS_MASK; 2256 2257 if (algo_type == CRYPTO_ALG_TYPE_ACOMPRESS) { 2258 acomp = crypto_alloc_acomp(driver, type, mask); 2259 if (IS_ERR(acomp)) { 2260 pr_err("alg: acomp: Failed to load transform for %s: %ld\n", 2261 driver, PTR_ERR(acomp)); 2262 return PTR_ERR(acomp); 2263 } 2264 err = test_acomp(acomp, desc->suite.comp.comp.vecs, 2265 desc->suite.comp.decomp.vecs, 2266 desc->suite.comp.comp.count, 2267 desc->suite.comp.decomp.count); 2268 crypto_free_acomp(acomp); 2269 } else { 2270 comp = crypto_alloc_comp(driver, type, mask); 2271 if (IS_ERR(comp)) { 2272 pr_err("alg: comp: Failed to load transform for %s: %ld\n", 2273 driver, PTR_ERR(comp)); 2274 return PTR_ERR(comp); 2275 } 2276 2277 err = test_comp(comp, desc->suite.comp.comp.vecs, 2278 desc->suite.comp.decomp.vecs, 2279 desc->suite.comp.comp.count, 2280 desc->suite.comp.decomp.count); 2281 2282 crypto_free_comp(comp); 2283 } 2284 return err; 2285 } 2286 2287 static int alg_test_crc32c(const struct alg_test_desc *desc, 2288 const char *driver, u32 type, u32 mask) 2289 { 2290 struct crypto_shash *tfm; 2291 __le32 val; 2292 int err; 2293 2294 err = alg_test_hash(desc, driver, type, mask); 2295 if (err) 2296 return err; 2297 2298 tfm = crypto_alloc_shash(driver, type, mask); 2299 if (IS_ERR(tfm)) { 2300 if (PTR_ERR(tfm) == -ENOENT) { 2301 /* 2302 * This crc32c implementation is only available through 2303 * ahash API, not the shash API, so the remaining part 2304 * of the test is not applicable to it. 2305 */ 2306 return 0; 2307 } 2308 printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: " 2309 "%ld\n", driver, PTR_ERR(tfm)); 2310 return PTR_ERR(tfm); 2311 } 2312 2313 do { 2314 SHASH_DESC_ON_STACK(shash, tfm); 2315 u32 *ctx = (u32 *)shash_desc_ctx(shash); 2316 2317 shash->tfm = tfm; 2318 shash->flags = 0; 2319 2320 *ctx = 420553207; 2321 err = crypto_shash_final(shash, (u8 *)&val); 2322 if (err) { 2323 printk(KERN_ERR "alg: crc32c: Operation failed for " 2324 "%s: %d\n", driver, err); 2325 break; 2326 } 2327 2328 if (val != cpu_to_le32(~420553207)) { 2329 pr_err("alg: crc32c: Test failed for %s: %u\n", 2330 driver, le32_to_cpu(val)); 2331 err = -EINVAL; 2332 } 2333 } while (0); 2334 2335 crypto_free_shash(tfm); 2336 2337 return err; 2338 } 2339 2340 static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver, 2341 u32 type, u32 mask) 2342 { 2343 struct crypto_rng *rng; 2344 int err; 2345 2346 rng = crypto_alloc_rng(driver, type, mask); 2347 if (IS_ERR(rng)) { 2348 printk(KERN_ERR "alg: cprng: Failed to load transform for %s: " 2349 "%ld\n", driver, PTR_ERR(rng)); 2350 return PTR_ERR(rng); 2351 } 2352 2353 err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count); 2354 2355 crypto_free_rng(rng); 2356 2357 return err; 2358 } 2359 2360 2361 static int drbg_cavs_test(const struct drbg_testvec *test, int pr, 2362 const char *driver, u32 type, u32 mask) 2363 { 2364 int ret = -EAGAIN; 2365 struct crypto_rng *drng; 2366 struct drbg_test_data test_data; 2367 struct drbg_string addtl, pers, testentropy; 2368 unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL); 2369 2370 if (!buf) 2371 return -ENOMEM; 2372 2373 drng = crypto_alloc_rng(driver, type, mask); 2374 if (IS_ERR(drng)) { 2375 printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for " 2376 "%s\n", driver); 2377 kzfree(buf); 2378 return -ENOMEM; 2379 } 2380 2381 test_data.testentropy = &testentropy; 2382 drbg_string_fill(&testentropy, test->entropy, test->entropylen); 2383 drbg_string_fill(&pers, test->pers, test->perslen); 2384 ret = crypto_drbg_reset_test(drng, &pers, &test_data); 2385 if (ret) { 2386 printk(KERN_ERR "alg: drbg: Failed to reset rng\n"); 2387 goto outbuf; 2388 } 2389 2390 drbg_string_fill(&addtl, test->addtla, test->addtllen); 2391 if (pr) { 2392 drbg_string_fill(&testentropy, test->entpra, test->entprlen); 2393 ret = crypto_drbg_get_bytes_addtl_test(drng, 2394 buf, test->expectedlen, &addtl, &test_data); 2395 } else { 2396 ret = crypto_drbg_get_bytes_addtl(drng, 2397 buf, test->expectedlen, &addtl); 2398 } 2399 if (ret < 0) { 2400 printk(KERN_ERR "alg: drbg: could not obtain random data for " 2401 "driver %s\n", driver); 2402 goto outbuf; 2403 } 2404 2405 drbg_string_fill(&addtl, test->addtlb, test->addtllen); 2406 if (pr) { 2407 drbg_string_fill(&testentropy, test->entprb, test->entprlen); 2408 ret = crypto_drbg_get_bytes_addtl_test(drng, 2409 buf, test->expectedlen, &addtl, &test_data); 2410 } else { 2411 ret = crypto_drbg_get_bytes_addtl(drng, 2412 buf, test->expectedlen, &addtl); 2413 } 2414 if (ret < 0) { 2415 printk(KERN_ERR "alg: drbg: could not obtain random data for " 2416 "driver %s\n", driver); 2417 goto outbuf; 2418 } 2419 2420 ret = memcmp(test->expected, buf, test->expectedlen); 2421 2422 outbuf: 2423 crypto_free_rng(drng); 2424 kzfree(buf); 2425 return ret; 2426 } 2427 2428 2429 static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver, 2430 u32 type, u32 mask) 2431 { 2432 int err = 0; 2433 int pr = 0; 2434 int i = 0; 2435 const struct drbg_testvec *template = desc->suite.drbg.vecs; 2436 unsigned int tcount = desc->suite.drbg.count; 2437 2438 if (0 == memcmp(driver, "drbg_pr_", 8)) 2439 pr = 1; 2440 2441 for (i = 0; i < tcount; i++) { 2442 err = drbg_cavs_test(&template[i], pr, driver, type, mask); 2443 if (err) { 2444 printk(KERN_ERR "alg: drbg: Test %d failed for %s\n", 2445 i, driver); 2446 err = -EINVAL; 2447 break; 2448 } 2449 } 2450 return err; 2451 2452 } 2453 2454 static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec, 2455 const char *alg) 2456 { 2457 struct kpp_request *req; 2458 void *input_buf = NULL; 2459 void *output_buf = NULL; 2460 void *a_public = NULL; 2461 void *a_ss = NULL; 2462 void *shared_secret = NULL; 2463 struct crypto_wait wait; 2464 unsigned int out_len_max; 2465 int err = -ENOMEM; 2466 struct scatterlist src, dst; 2467 2468 req = kpp_request_alloc(tfm, GFP_KERNEL); 2469 if (!req) 2470 return err; 2471 2472 crypto_init_wait(&wait); 2473 2474 err = crypto_kpp_set_secret(tfm, vec->secret, vec->secret_size); 2475 if (err < 0) 2476 goto free_req; 2477 2478 out_len_max = crypto_kpp_maxsize(tfm); 2479 output_buf = kzalloc(out_len_max, GFP_KERNEL); 2480 if (!output_buf) { 2481 err = -ENOMEM; 2482 goto free_req; 2483 } 2484 2485 /* Use appropriate parameter as base */ 2486 kpp_request_set_input(req, NULL, 0); 2487 sg_init_one(&dst, output_buf, out_len_max); 2488 kpp_request_set_output(req, &dst, out_len_max); 2489 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2490 crypto_req_done, &wait); 2491 2492 /* Compute party A's public key */ 2493 err = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait); 2494 if (err) { 2495 pr_err("alg: %s: Party A: generate public key test failed. err %d\n", 2496 alg, err); 2497 goto free_output; 2498 } 2499 2500 if (vec->genkey) { 2501 /* Save party A's public key */ 2502 a_public = kmemdup(sg_virt(req->dst), out_len_max, GFP_KERNEL); 2503 if (!a_public) { 2504 err = -ENOMEM; 2505 goto free_output; 2506 } 2507 } else { 2508 /* Verify calculated public key */ 2509 if (memcmp(vec->expected_a_public, sg_virt(req->dst), 2510 vec->expected_a_public_size)) { 2511 pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n", 2512 alg); 2513 err = -EINVAL; 2514 goto free_output; 2515 } 2516 } 2517 2518 /* Calculate shared secret key by using counter part (b) public key. */ 2519 input_buf = kmemdup(vec->b_public, vec->b_public_size, GFP_KERNEL); 2520 if (!input_buf) { 2521 err = -ENOMEM; 2522 goto free_output; 2523 } 2524 2525 sg_init_one(&src, input_buf, vec->b_public_size); 2526 sg_init_one(&dst, output_buf, out_len_max); 2527 kpp_request_set_input(req, &src, vec->b_public_size); 2528 kpp_request_set_output(req, &dst, out_len_max); 2529 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2530 crypto_req_done, &wait); 2531 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait); 2532 if (err) { 2533 pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n", 2534 alg, err); 2535 goto free_all; 2536 } 2537 2538 if (vec->genkey) { 2539 /* Save the shared secret obtained by party A */ 2540 a_ss = kmemdup(sg_virt(req->dst), vec->expected_ss_size, GFP_KERNEL); 2541 if (!a_ss) { 2542 err = -ENOMEM; 2543 goto free_all; 2544 } 2545 2546 /* 2547 * Calculate party B's shared secret by using party A's 2548 * public key. 2549 */ 2550 err = crypto_kpp_set_secret(tfm, vec->b_secret, 2551 vec->b_secret_size); 2552 if (err < 0) 2553 goto free_all; 2554 2555 sg_init_one(&src, a_public, vec->expected_a_public_size); 2556 sg_init_one(&dst, output_buf, out_len_max); 2557 kpp_request_set_input(req, &src, vec->expected_a_public_size); 2558 kpp_request_set_output(req, &dst, out_len_max); 2559 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2560 crypto_req_done, &wait); 2561 err = crypto_wait_req(crypto_kpp_compute_shared_secret(req), 2562 &wait); 2563 if (err) { 2564 pr_err("alg: %s: Party B: compute shared secret failed. err %d\n", 2565 alg, err); 2566 goto free_all; 2567 } 2568 2569 shared_secret = a_ss; 2570 } else { 2571 shared_secret = (void *)vec->expected_ss; 2572 } 2573 2574 /* 2575 * verify shared secret from which the user will derive 2576 * secret key by executing whatever hash it has chosen 2577 */ 2578 if (memcmp(shared_secret, sg_virt(req->dst), 2579 vec->expected_ss_size)) { 2580 pr_err("alg: %s: compute shared secret test failed. Invalid output\n", 2581 alg); 2582 err = -EINVAL; 2583 } 2584 2585 free_all: 2586 kfree(a_ss); 2587 kfree(input_buf); 2588 free_output: 2589 kfree(a_public); 2590 kfree(output_buf); 2591 free_req: 2592 kpp_request_free(req); 2593 return err; 2594 } 2595 2596 static int test_kpp(struct crypto_kpp *tfm, const char *alg, 2597 const struct kpp_testvec *vecs, unsigned int tcount) 2598 { 2599 int ret, i; 2600 2601 for (i = 0; i < tcount; i++) { 2602 ret = do_test_kpp(tfm, vecs++, alg); 2603 if (ret) { 2604 pr_err("alg: %s: test failed on vector %d, err=%d\n", 2605 alg, i + 1, ret); 2606 return ret; 2607 } 2608 } 2609 return 0; 2610 } 2611 2612 static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver, 2613 u32 type, u32 mask) 2614 { 2615 struct crypto_kpp *tfm; 2616 int err = 0; 2617 2618 tfm = crypto_alloc_kpp(driver, type, mask); 2619 if (IS_ERR(tfm)) { 2620 pr_err("alg: kpp: Failed to load tfm for %s: %ld\n", 2621 driver, PTR_ERR(tfm)); 2622 return PTR_ERR(tfm); 2623 } 2624 if (desc->suite.kpp.vecs) 2625 err = test_kpp(tfm, desc->alg, desc->suite.kpp.vecs, 2626 desc->suite.kpp.count); 2627 2628 crypto_free_kpp(tfm); 2629 return err; 2630 } 2631 2632 static u8 *test_pack_u32(u8 *dst, u32 val) 2633 { 2634 memcpy(dst, &val, sizeof(val)); 2635 return dst + sizeof(val); 2636 } 2637 2638 static int test_akcipher_one(struct crypto_akcipher *tfm, 2639 const struct akcipher_testvec *vecs) 2640 { 2641 char *xbuf[XBUFSIZE]; 2642 struct akcipher_request *req; 2643 void *outbuf_enc = NULL; 2644 void *outbuf_dec = NULL; 2645 struct crypto_wait wait; 2646 unsigned int out_len_max, out_len = 0; 2647 int err = -ENOMEM; 2648 struct scatterlist src, dst, src_tab[3]; 2649 const char *m, *c; 2650 unsigned int m_size, c_size; 2651 const char *op; 2652 u8 *key, *ptr; 2653 2654 if (testmgr_alloc_buf(xbuf)) 2655 return err; 2656 2657 req = akcipher_request_alloc(tfm, GFP_KERNEL); 2658 if (!req) 2659 goto free_xbuf; 2660 2661 crypto_init_wait(&wait); 2662 2663 key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len, 2664 GFP_KERNEL); 2665 if (!key) 2666 goto free_xbuf; 2667 memcpy(key, vecs->key, vecs->key_len); 2668 ptr = key + vecs->key_len; 2669 ptr = test_pack_u32(ptr, vecs->algo); 2670 ptr = test_pack_u32(ptr, vecs->param_len); 2671 memcpy(ptr, vecs->params, vecs->param_len); 2672 2673 if (vecs->public_key_vec) 2674 err = crypto_akcipher_set_pub_key(tfm, key, vecs->key_len); 2675 else 2676 err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len); 2677 if (err) 2678 goto free_req; 2679 2680 /* 2681 * First run test which do not require a private key, such as 2682 * encrypt or verify. 2683 */ 2684 err = -ENOMEM; 2685 out_len_max = crypto_akcipher_maxsize(tfm); 2686 outbuf_enc = kzalloc(out_len_max, GFP_KERNEL); 2687 if (!outbuf_enc) 2688 goto free_req; 2689 2690 if (!vecs->siggen_sigver_test) { 2691 m = vecs->m; 2692 m_size = vecs->m_size; 2693 c = vecs->c; 2694 c_size = vecs->c_size; 2695 op = "encrypt"; 2696 } else { 2697 /* Swap args so we could keep plaintext (digest) 2698 * in vecs->m, and cooked signature in vecs->c. 2699 */ 2700 m = vecs->c; /* signature */ 2701 m_size = vecs->c_size; 2702 c = vecs->m; /* digest */ 2703 c_size = vecs->m_size; 2704 op = "verify"; 2705 } 2706 2707 if (WARN_ON(m_size > PAGE_SIZE)) 2708 goto free_all; 2709 memcpy(xbuf[0], m, m_size); 2710 2711 sg_init_table(src_tab, 3); 2712 sg_set_buf(&src_tab[0], xbuf[0], 8); 2713 sg_set_buf(&src_tab[1], xbuf[0] + 8, m_size - 8); 2714 if (vecs->siggen_sigver_test) { 2715 if (WARN_ON(c_size > PAGE_SIZE)) 2716 goto free_all; 2717 memcpy(xbuf[1], c, c_size); 2718 sg_set_buf(&src_tab[2], xbuf[1], c_size); 2719 akcipher_request_set_crypt(req, src_tab, NULL, m_size, c_size); 2720 } else { 2721 sg_init_one(&dst, outbuf_enc, out_len_max); 2722 akcipher_request_set_crypt(req, src_tab, &dst, m_size, 2723 out_len_max); 2724 } 2725 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 2726 crypto_req_done, &wait); 2727 2728 err = crypto_wait_req(vecs->siggen_sigver_test ? 2729 /* Run asymmetric signature verification */ 2730 crypto_akcipher_verify(req) : 2731 /* Run asymmetric encrypt */ 2732 crypto_akcipher_encrypt(req), &wait); 2733 if (err) { 2734 pr_err("alg: akcipher: %s test failed. err %d\n", op, err); 2735 goto free_all; 2736 } 2737 if (!vecs->siggen_sigver_test) { 2738 if (req->dst_len != c_size) { 2739 pr_err("alg: akcipher: %s test failed. Invalid output len\n", 2740 op); 2741 err = -EINVAL; 2742 goto free_all; 2743 } 2744 /* verify that encrypted message is equal to expected */ 2745 if (memcmp(c, outbuf_enc, c_size) != 0) { 2746 pr_err("alg: akcipher: %s test failed. Invalid output\n", 2747 op); 2748 hexdump(outbuf_enc, c_size); 2749 err = -EINVAL; 2750 goto free_all; 2751 } 2752 } 2753 2754 /* 2755 * Don't invoke (decrypt or sign) test which require a private key 2756 * for vectors with only a public key. 2757 */ 2758 if (vecs->public_key_vec) { 2759 err = 0; 2760 goto free_all; 2761 } 2762 outbuf_dec = kzalloc(out_len_max, GFP_KERNEL); 2763 if (!outbuf_dec) { 2764 err = -ENOMEM; 2765 goto free_all; 2766 } 2767 2768 op = vecs->siggen_sigver_test ? "sign" : "decrypt"; 2769 if (WARN_ON(c_size > PAGE_SIZE)) 2770 goto free_all; 2771 memcpy(xbuf[0], c, c_size); 2772 2773 sg_init_one(&src, xbuf[0], c_size); 2774 sg_init_one(&dst, outbuf_dec, out_len_max); 2775 crypto_init_wait(&wait); 2776 akcipher_request_set_crypt(req, &src, &dst, c_size, out_len_max); 2777 2778 err = crypto_wait_req(vecs->siggen_sigver_test ? 2779 /* Run asymmetric signature generation */ 2780 crypto_akcipher_sign(req) : 2781 /* Run asymmetric decrypt */ 2782 crypto_akcipher_decrypt(req), &wait); 2783 if (err) { 2784 pr_err("alg: akcipher: %s test failed. err %d\n", op, err); 2785 goto free_all; 2786 } 2787 out_len = req->dst_len; 2788 if (out_len < m_size) { 2789 pr_err("alg: akcipher: %s test failed. Invalid output len %u\n", 2790 op, out_len); 2791 err = -EINVAL; 2792 goto free_all; 2793 } 2794 /* verify that decrypted message is equal to the original msg */ 2795 if (memchr_inv(outbuf_dec, 0, out_len - m_size) || 2796 memcmp(m, outbuf_dec + out_len - m_size, m_size)) { 2797 pr_err("alg: akcipher: %s test failed. Invalid output\n", op); 2798 hexdump(outbuf_dec, out_len); 2799 err = -EINVAL; 2800 } 2801 free_all: 2802 kfree(outbuf_dec); 2803 kfree(outbuf_enc); 2804 free_req: 2805 akcipher_request_free(req); 2806 kfree(key); 2807 free_xbuf: 2808 testmgr_free_buf(xbuf); 2809 return err; 2810 } 2811 2812 static int test_akcipher(struct crypto_akcipher *tfm, const char *alg, 2813 const struct akcipher_testvec *vecs, 2814 unsigned int tcount) 2815 { 2816 const char *algo = 2817 crypto_tfm_alg_driver_name(crypto_akcipher_tfm(tfm)); 2818 int ret, i; 2819 2820 for (i = 0; i < tcount; i++) { 2821 ret = test_akcipher_one(tfm, vecs++); 2822 if (!ret) 2823 continue; 2824 2825 pr_err("alg: akcipher: test %d failed for %s, err=%d\n", 2826 i + 1, algo, ret); 2827 return ret; 2828 } 2829 return 0; 2830 } 2831 2832 static int alg_test_akcipher(const struct alg_test_desc *desc, 2833 const char *driver, u32 type, u32 mask) 2834 { 2835 struct crypto_akcipher *tfm; 2836 int err = 0; 2837 2838 tfm = crypto_alloc_akcipher(driver, type, mask); 2839 if (IS_ERR(tfm)) { 2840 pr_err("alg: akcipher: Failed to load tfm for %s: %ld\n", 2841 driver, PTR_ERR(tfm)); 2842 return PTR_ERR(tfm); 2843 } 2844 if (desc->suite.akcipher.vecs) 2845 err = test_akcipher(tfm, desc->alg, desc->suite.akcipher.vecs, 2846 desc->suite.akcipher.count); 2847 2848 crypto_free_akcipher(tfm); 2849 return err; 2850 } 2851 2852 static int alg_test_null(const struct alg_test_desc *desc, 2853 const char *driver, u32 type, u32 mask) 2854 { 2855 return 0; 2856 } 2857 2858 #define __VECS(tv) { .vecs = tv, .count = ARRAY_SIZE(tv) } 2859 2860 /* Please keep this list sorted by algorithm name. */ 2861 static const struct alg_test_desc alg_test_descs[] = { 2862 { 2863 .alg = "adiantum(xchacha12,aes)", 2864 .test = alg_test_skcipher, 2865 .suite = { 2866 .cipher = __VECS(adiantum_xchacha12_aes_tv_template) 2867 }, 2868 }, { 2869 .alg = "adiantum(xchacha20,aes)", 2870 .test = alg_test_skcipher, 2871 .suite = { 2872 .cipher = __VECS(adiantum_xchacha20_aes_tv_template) 2873 }, 2874 }, { 2875 .alg = "aegis128", 2876 .test = alg_test_aead, 2877 .suite = { 2878 .aead = __VECS(aegis128_tv_template) 2879 } 2880 }, { 2881 .alg = "aegis128l", 2882 .test = alg_test_aead, 2883 .suite = { 2884 .aead = __VECS(aegis128l_tv_template) 2885 } 2886 }, { 2887 .alg = "aegis256", 2888 .test = alg_test_aead, 2889 .suite = { 2890 .aead = __VECS(aegis256_tv_template) 2891 } 2892 }, { 2893 .alg = "ansi_cprng", 2894 .test = alg_test_cprng, 2895 .suite = { 2896 .cprng = __VECS(ansi_cprng_aes_tv_template) 2897 } 2898 }, { 2899 .alg = "authenc(hmac(md5),ecb(cipher_null))", 2900 .test = alg_test_aead, 2901 .suite = { 2902 .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template) 2903 } 2904 }, { 2905 .alg = "authenc(hmac(sha1),cbc(aes))", 2906 .test = alg_test_aead, 2907 .fips_allowed = 1, 2908 .suite = { 2909 .aead = __VECS(hmac_sha1_aes_cbc_tv_temp) 2910 } 2911 }, { 2912 .alg = "authenc(hmac(sha1),cbc(des))", 2913 .test = alg_test_aead, 2914 .suite = { 2915 .aead = __VECS(hmac_sha1_des_cbc_tv_temp) 2916 } 2917 }, { 2918 .alg = "authenc(hmac(sha1),cbc(des3_ede))", 2919 .test = alg_test_aead, 2920 .fips_allowed = 1, 2921 .suite = { 2922 .aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp) 2923 } 2924 }, { 2925 .alg = "authenc(hmac(sha1),ctr(aes))", 2926 .test = alg_test_null, 2927 .fips_allowed = 1, 2928 }, { 2929 .alg = "authenc(hmac(sha1),ecb(cipher_null))", 2930 .test = alg_test_aead, 2931 .suite = { 2932 .aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp) 2933 } 2934 }, { 2935 .alg = "authenc(hmac(sha1),rfc3686(ctr(aes)))", 2936 .test = alg_test_null, 2937 .fips_allowed = 1, 2938 }, { 2939 .alg = "authenc(hmac(sha224),cbc(des))", 2940 .test = alg_test_aead, 2941 .suite = { 2942 .aead = __VECS(hmac_sha224_des_cbc_tv_temp) 2943 } 2944 }, { 2945 .alg = "authenc(hmac(sha224),cbc(des3_ede))", 2946 .test = alg_test_aead, 2947 .fips_allowed = 1, 2948 .suite = { 2949 .aead = __VECS(hmac_sha224_des3_ede_cbc_tv_temp) 2950 } 2951 }, { 2952 .alg = "authenc(hmac(sha256),cbc(aes))", 2953 .test = alg_test_aead, 2954 .fips_allowed = 1, 2955 .suite = { 2956 .aead = __VECS(hmac_sha256_aes_cbc_tv_temp) 2957 } 2958 }, { 2959 .alg = "authenc(hmac(sha256),cbc(des))", 2960 .test = alg_test_aead, 2961 .suite = { 2962 .aead = __VECS(hmac_sha256_des_cbc_tv_temp) 2963 } 2964 }, { 2965 .alg = "authenc(hmac(sha256),cbc(des3_ede))", 2966 .test = alg_test_aead, 2967 .fips_allowed = 1, 2968 .suite = { 2969 .aead = __VECS(hmac_sha256_des3_ede_cbc_tv_temp) 2970 } 2971 }, { 2972 .alg = "authenc(hmac(sha256),ctr(aes))", 2973 .test = alg_test_null, 2974 .fips_allowed = 1, 2975 }, { 2976 .alg = "authenc(hmac(sha256),rfc3686(ctr(aes)))", 2977 .test = alg_test_null, 2978 .fips_allowed = 1, 2979 }, { 2980 .alg = "authenc(hmac(sha384),cbc(des))", 2981 .test = alg_test_aead, 2982 .suite = { 2983 .aead = __VECS(hmac_sha384_des_cbc_tv_temp) 2984 } 2985 }, { 2986 .alg = "authenc(hmac(sha384),cbc(des3_ede))", 2987 .test = alg_test_aead, 2988 .fips_allowed = 1, 2989 .suite = { 2990 .aead = __VECS(hmac_sha384_des3_ede_cbc_tv_temp) 2991 } 2992 }, { 2993 .alg = "authenc(hmac(sha384),ctr(aes))", 2994 .test = alg_test_null, 2995 .fips_allowed = 1, 2996 }, { 2997 .alg = "authenc(hmac(sha384),rfc3686(ctr(aes)))", 2998 .test = alg_test_null, 2999 .fips_allowed = 1, 3000 }, { 3001 .alg = "authenc(hmac(sha512),cbc(aes))", 3002 .fips_allowed = 1, 3003 .test = alg_test_aead, 3004 .suite = { 3005 .aead = __VECS(hmac_sha512_aes_cbc_tv_temp) 3006 } 3007 }, { 3008 .alg = "authenc(hmac(sha512),cbc(des))", 3009 .test = alg_test_aead, 3010 .suite = { 3011 .aead = __VECS(hmac_sha512_des_cbc_tv_temp) 3012 } 3013 }, { 3014 .alg = "authenc(hmac(sha512),cbc(des3_ede))", 3015 .test = alg_test_aead, 3016 .fips_allowed = 1, 3017 .suite = { 3018 .aead = __VECS(hmac_sha512_des3_ede_cbc_tv_temp) 3019 } 3020 }, { 3021 .alg = "authenc(hmac(sha512),ctr(aes))", 3022 .test = alg_test_null, 3023 .fips_allowed = 1, 3024 }, { 3025 .alg = "authenc(hmac(sha512),rfc3686(ctr(aes)))", 3026 .test = alg_test_null, 3027 .fips_allowed = 1, 3028 }, { 3029 .alg = "cbc(aes)", 3030 .test = alg_test_skcipher, 3031 .fips_allowed = 1, 3032 .suite = { 3033 .cipher = __VECS(aes_cbc_tv_template) 3034 }, 3035 }, { 3036 .alg = "cbc(anubis)", 3037 .test = alg_test_skcipher, 3038 .suite = { 3039 .cipher = __VECS(anubis_cbc_tv_template) 3040 }, 3041 }, { 3042 .alg = "cbc(blowfish)", 3043 .test = alg_test_skcipher, 3044 .suite = { 3045 .cipher = __VECS(bf_cbc_tv_template) 3046 }, 3047 }, { 3048 .alg = "cbc(camellia)", 3049 .test = alg_test_skcipher, 3050 .suite = { 3051 .cipher = __VECS(camellia_cbc_tv_template) 3052 }, 3053 }, { 3054 .alg = "cbc(cast5)", 3055 .test = alg_test_skcipher, 3056 .suite = { 3057 .cipher = __VECS(cast5_cbc_tv_template) 3058 }, 3059 }, { 3060 .alg = "cbc(cast6)", 3061 .test = alg_test_skcipher, 3062 .suite = { 3063 .cipher = __VECS(cast6_cbc_tv_template) 3064 }, 3065 }, { 3066 .alg = "cbc(des)", 3067 .test = alg_test_skcipher, 3068 .suite = { 3069 .cipher = __VECS(des_cbc_tv_template) 3070 }, 3071 }, { 3072 .alg = "cbc(des3_ede)", 3073 .test = alg_test_skcipher, 3074 .fips_allowed = 1, 3075 .suite = { 3076 .cipher = __VECS(des3_ede_cbc_tv_template) 3077 }, 3078 }, { 3079 /* Same as cbc(aes) except the key is stored in 3080 * hardware secure memory which we reference by index 3081 */ 3082 .alg = "cbc(paes)", 3083 .test = alg_test_null, 3084 .fips_allowed = 1, 3085 }, { 3086 .alg = "cbc(serpent)", 3087 .test = alg_test_skcipher, 3088 .suite = { 3089 .cipher = __VECS(serpent_cbc_tv_template) 3090 }, 3091 }, { 3092 .alg = "cbc(sm4)", 3093 .test = alg_test_skcipher, 3094 .suite = { 3095 .cipher = __VECS(sm4_cbc_tv_template) 3096 } 3097 }, { 3098 .alg = "cbc(twofish)", 3099 .test = alg_test_skcipher, 3100 .suite = { 3101 .cipher = __VECS(tf_cbc_tv_template) 3102 }, 3103 }, { 3104 .alg = "cbcmac(aes)", 3105 .fips_allowed = 1, 3106 .test = alg_test_hash, 3107 .suite = { 3108 .hash = __VECS(aes_cbcmac_tv_template) 3109 } 3110 }, { 3111 .alg = "ccm(aes)", 3112 .test = alg_test_aead, 3113 .fips_allowed = 1, 3114 .suite = { 3115 .aead = __VECS(aes_ccm_tv_template) 3116 } 3117 }, { 3118 .alg = "cfb(aes)", 3119 .test = alg_test_skcipher, 3120 .fips_allowed = 1, 3121 .suite = { 3122 .cipher = __VECS(aes_cfb_tv_template) 3123 }, 3124 }, { 3125 .alg = "chacha20", 3126 .test = alg_test_skcipher, 3127 .suite = { 3128 .cipher = __VECS(chacha20_tv_template) 3129 }, 3130 }, { 3131 .alg = "cmac(aes)", 3132 .fips_allowed = 1, 3133 .test = alg_test_hash, 3134 .suite = { 3135 .hash = __VECS(aes_cmac128_tv_template) 3136 } 3137 }, { 3138 .alg = "cmac(des3_ede)", 3139 .fips_allowed = 1, 3140 .test = alg_test_hash, 3141 .suite = { 3142 .hash = __VECS(des3_ede_cmac64_tv_template) 3143 } 3144 }, { 3145 .alg = "compress_null", 3146 .test = alg_test_null, 3147 }, { 3148 .alg = "crc32", 3149 .test = alg_test_hash, 3150 .fips_allowed = 1, 3151 .suite = { 3152 .hash = __VECS(crc32_tv_template) 3153 } 3154 }, { 3155 .alg = "crc32c", 3156 .test = alg_test_crc32c, 3157 .fips_allowed = 1, 3158 .suite = { 3159 .hash = __VECS(crc32c_tv_template) 3160 } 3161 }, { 3162 .alg = "crct10dif", 3163 .test = alg_test_hash, 3164 .fips_allowed = 1, 3165 .suite = { 3166 .hash = __VECS(crct10dif_tv_template) 3167 } 3168 }, { 3169 .alg = "ctr(aes)", 3170 .test = alg_test_skcipher, 3171 .fips_allowed = 1, 3172 .suite = { 3173 .cipher = __VECS(aes_ctr_tv_template) 3174 } 3175 }, { 3176 .alg = "ctr(blowfish)", 3177 .test = alg_test_skcipher, 3178 .suite = { 3179 .cipher = __VECS(bf_ctr_tv_template) 3180 } 3181 }, { 3182 .alg = "ctr(camellia)", 3183 .test = alg_test_skcipher, 3184 .suite = { 3185 .cipher = __VECS(camellia_ctr_tv_template) 3186 } 3187 }, { 3188 .alg = "ctr(cast5)", 3189 .test = alg_test_skcipher, 3190 .suite = { 3191 .cipher = __VECS(cast5_ctr_tv_template) 3192 } 3193 }, { 3194 .alg = "ctr(cast6)", 3195 .test = alg_test_skcipher, 3196 .suite = { 3197 .cipher = __VECS(cast6_ctr_tv_template) 3198 } 3199 }, { 3200 .alg = "ctr(des)", 3201 .test = alg_test_skcipher, 3202 .suite = { 3203 .cipher = __VECS(des_ctr_tv_template) 3204 } 3205 }, { 3206 .alg = "ctr(des3_ede)", 3207 .test = alg_test_skcipher, 3208 .fips_allowed = 1, 3209 .suite = { 3210 .cipher = __VECS(des3_ede_ctr_tv_template) 3211 } 3212 }, { 3213 /* Same as ctr(aes) except the key is stored in 3214 * hardware secure memory which we reference by index 3215 */ 3216 .alg = "ctr(paes)", 3217 .test = alg_test_null, 3218 .fips_allowed = 1, 3219 }, { 3220 .alg = "ctr(serpent)", 3221 .test = alg_test_skcipher, 3222 .suite = { 3223 .cipher = __VECS(serpent_ctr_tv_template) 3224 } 3225 }, { 3226 .alg = "ctr(sm4)", 3227 .test = alg_test_skcipher, 3228 .suite = { 3229 .cipher = __VECS(sm4_ctr_tv_template) 3230 } 3231 }, { 3232 .alg = "ctr(twofish)", 3233 .test = alg_test_skcipher, 3234 .suite = { 3235 .cipher = __VECS(tf_ctr_tv_template) 3236 } 3237 }, { 3238 .alg = "cts(cbc(aes))", 3239 .test = alg_test_skcipher, 3240 .fips_allowed = 1, 3241 .suite = { 3242 .cipher = __VECS(cts_mode_tv_template) 3243 } 3244 }, { 3245 .alg = "deflate", 3246 .test = alg_test_comp, 3247 .fips_allowed = 1, 3248 .suite = { 3249 .comp = { 3250 .comp = __VECS(deflate_comp_tv_template), 3251 .decomp = __VECS(deflate_decomp_tv_template) 3252 } 3253 } 3254 }, { 3255 .alg = "dh", 3256 .test = alg_test_kpp, 3257 .fips_allowed = 1, 3258 .suite = { 3259 .kpp = __VECS(dh_tv_template) 3260 } 3261 }, { 3262 .alg = "digest_null", 3263 .test = alg_test_null, 3264 }, { 3265 .alg = "drbg_nopr_ctr_aes128", 3266 .test = alg_test_drbg, 3267 .fips_allowed = 1, 3268 .suite = { 3269 .drbg = __VECS(drbg_nopr_ctr_aes128_tv_template) 3270 } 3271 }, { 3272 .alg = "drbg_nopr_ctr_aes192", 3273 .test = alg_test_drbg, 3274 .fips_allowed = 1, 3275 .suite = { 3276 .drbg = __VECS(drbg_nopr_ctr_aes192_tv_template) 3277 } 3278 }, { 3279 .alg = "drbg_nopr_ctr_aes256", 3280 .test = alg_test_drbg, 3281 .fips_allowed = 1, 3282 .suite = { 3283 .drbg = __VECS(drbg_nopr_ctr_aes256_tv_template) 3284 } 3285 }, { 3286 /* 3287 * There is no need to specifically test the DRBG with every 3288 * backend cipher -- covered by drbg_nopr_hmac_sha256 test 3289 */ 3290 .alg = "drbg_nopr_hmac_sha1", 3291 .fips_allowed = 1, 3292 .test = alg_test_null, 3293 }, { 3294 .alg = "drbg_nopr_hmac_sha256", 3295 .test = alg_test_drbg, 3296 .fips_allowed = 1, 3297 .suite = { 3298 .drbg = __VECS(drbg_nopr_hmac_sha256_tv_template) 3299 } 3300 }, { 3301 /* covered by drbg_nopr_hmac_sha256 test */ 3302 .alg = "drbg_nopr_hmac_sha384", 3303 .fips_allowed = 1, 3304 .test = alg_test_null, 3305 }, { 3306 .alg = "drbg_nopr_hmac_sha512", 3307 .test = alg_test_null, 3308 .fips_allowed = 1, 3309 }, { 3310 .alg = "drbg_nopr_sha1", 3311 .fips_allowed = 1, 3312 .test = alg_test_null, 3313 }, { 3314 .alg = "drbg_nopr_sha256", 3315 .test = alg_test_drbg, 3316 .fips_allowed = 1, 3317 .suite = { 3318 .drbg = __VECS(drbg_nopr_sha256_tv_template) 3319 } 3320 }, { 3321 /* covered by drbg_nopr_sha256 test */ 3322 .alg = "drbg_nopr_sha384", 3323 .fips_allowed = 1, 3324 .test = alg_test_null, 3325 }, { 3326 .alg = "drbg_nopr_sha512", 3327 .fips_allowed = 1, 3328 .test = alg_test_null, 3329 }, { 3330 .alg = "drbg_pr_ctr_aes128", 3331 .test = alg_test_drbg, 3332 .fips_allowed = 1, 3333 .suite = { 3334 .drbg = __VECS(drbg_pr_ctr_aes128_tv_template) 3335 } 3336 }, { 3337 /* covered by drbg_pr_ctr_aes128 test */ 3338 .alg = "drbg_pr_ctr_aes192", 3339 .fips_allowed = 1, 3340 .test = alg_test_null, 3341 }, { 3342 .alg = "drbg_pr_ctr_aes256", 3343 .fips_allowed = 1, 3344 .test = alg_test_null, 3345 }, { 3346 .alg = "drbg_pr_hmac_sha1", 3347 .fips_allowed = 1, 3348 .test = alg_test_null, 3349 }, { 3350 .alg = "drbg_pr_hmac_sha256", 3351 .test = alg_test_drbg, 3352 .fips_allowed = 1, 3353 .suite = { 3354 .drbg = __VECS(drbg_pr_hmac_sha256_tv_template) 3355 } 3356 }, { 3357 /* covered by drbg_pr_hmac_sha256 test */ 3358 .alg = "drbg_pr_hmac_sha384", 3359 .fips_allowed = 1, 3360 .test = alg_test_null, 3361 }, { 3362 .alg = "drbg_pr_hmac_sha512", 3363 .test = alg_test_null, 3364 .fips_allowed = 1, 3365 }, { 3366 .alg = "drbg_pr_sha1", 3367 .fips_allowed = 1, 3368 .test = alg_test_null, 3369 }, { 3370 .alg = "drbg_pr_sha256", 3371 .test = alg_test_drbg, 3372 .fips_allowed = 1, 3373 .suite = { 3374 .drbg = __VECS(drbg_pr_sha256_tv_template) 3375 } 3376 }, { 3377 /* covered by drbg_pr_sha256 test */ 3378 .alg = "drbg_pr_sha384", 3379 .fips_allowed = 1, 3380 .test = alg_test_null, 3381 }, { 3382 .alg = "drbg_pr_sha512", 3383 .fips_allowed = 1, 3384 .test = alg_test_null, 3385 }, { 3386 .alg = "ecb(aes)", 3387 .test = alg_test_skcipher, 3388 .fips_allowed = 1, 3389 .suite = { 3390 .cipher = __VECS(aes_tv_template) 3391 } 3392 }, { 3393 .alg = "ecb(anubis)", 3394 .test = alg_test_skcipher, 3395 .suite = { 3396 .cipher = __VECS(anubis_tv_template) 3397 } 3398 }, { 3399 .alg = "ecb(arc4)", 3400 .test = alg_test_skcipher, 3401 .suite = { 3402 .cipher = __VECS(arc4_tv_template) 3403 } 3404 }, { 3405 .alg = "ecb(blowfish)", 3406 .test = alg_test_skcipher, 3407 .suite = { 3408 .cipher = __VECS(bf_tv_template) 3409 } 3410 }, { 3411 .alg = "ecb(camellia)", 3412 .test = alg_test_skcipher, 3413 .suite = { 3414 .cipher = __VECS(camellia_tv_template) 3415 } 3416 }, { 3417 .alg = "ecb(cast5)", 3418 .test = alg_test_skcipher, 3419 .suite = { 3420 .cipher = __VECS(cast5_tv_template) 3421 } 3422 }, { 3423 .alg = "ecb(cast6)", 3424 .test = alg_test_skcipher, 3425 .suite = { 3426 .cipher = __VECS(cast6_tv_template) 3427 } 3428 }, { 3429 .alg = "ecb(cipher_null)", 3430 .test = alg_test_null, 3431 .fips_allowed = 1, 3432 }, { 3433 .alg = "ecb(des)", 3434 .test = alg_test_skcipher, 3435 .suite = { 3436 .cipher = __VECS(des_tv_template) 3437 } 3438 }, { 3439 .alg = "ecb(des3_ede)", 3440 .test = alg_test_skcipher, 3441 .fips_allowed = 1, 3442 .suite = { 3443 .cipher = __VECS(des3_ede_tv_template) 3444 } 3445 }, { 3446 .alg = "ecb(fcrypt)", 3447 .test = alg_test_skcipher, 3448 .suite = { 3449 .cipher = { 3450 .vecs = fcrypt_pcbc_tv_template, 3451 .count = 1 3452 } 3453 } 3454 }, { 3455 .alg = "ecb(khazad)", 3456 .test = alg_test_skcipher, 3457 .suite = { 3458 .cipher = __VECS(khazad_tv_template) 3459 } 3460 }, { 3461 /* Same as ecb(aes) except the key is stored in 3462 * hardware secure memory which we reference by index 3463 */ 3464 .alg = "ecb(paes)", 3465 .test = alg_test_null, 3466 .fips_allowed = 1, 3467 }, { 3468 .alg = "ecb(seed)", 3469 .test = alg_test_skcipher, 3470 .suite = { 3471 .cipher = __VECS(seed_tv_template) 3472 } 3473 }, { 3474 .alg = "ecb(serpent)", 3475 .test = alg_test_skcipher, 3476 .suite = { 3477 .cipher = __VECS(serpent_tv_template) 3478 } 3479 }, { 3480 .alg = "ecb(sm4)", 3481 .test = alg_test_skcipher, 3482 .suite = { 3483 .cipher = __VECS(sm4_tv_template) 3484 } 3485 }, { 3486 .alg = "ecb(tea)", 3487 .test = alg_test_skcipher, 3488 .suite = { 3489 .cipher = __VECS(tea_tv_template) 3490 } 3491 }, { 3492 .alg = "ecb(tnepres)", 3493 .test = alg_test_skcipher, 3494 .suite = { 3495 .cipher = __VECS(tnepres_tv_template) 3496 } 3497 }, { 3498 .alg = "ecb(twofish)", 3499 .test = alg_test_skcipher, 3500 .suite = { 3501 .cipher = __VECS(tf_tv_template) 3502 } 3503 }, { 3504 .alg = "ecb(xeta)", 3505 .test = alg_test_skcipher, 3506 .suite = { 3507 .cipher = __VECS(xeta_tv_template) 3508 } 3509 }, { 3510 .alg = "ecb(xtea)", 3511 .test = alg_test_skcipher, 3512 .suite = { 3513 .cipher = __VECS(xtea_tv_template) 3514 } 3515 }, { 3516 .alg = "ecdh", 3517 .test = alg_test_kpp, 3518 .fips_allowed = 1, 3519 .suite = { 3520 .kpp = __VECS(ecdh_tv_template) 3521 } 3522 }, { 3523 .alg = "ecrdsa", 3524 .test = alg_test_akcipher, 3525 .suite = { 3526 .akcipher = __VECS(ecrdsa_tv_template) 3527 } 3528 }, { 3529 .alg = "gcm(aes)", 3530 .test = alg_test_aead, 3531 .fips_allowed = 1, 3532 .suite = { 3533 .aead = __VECS(aes_gcm_tv_template) 3534 } 3535 }, { 3536 .alg = "ghash", 3537 .test = alg_test_hash, 3538 .fips_allowed = 1, 3539 .suite = { 3540 .hash = __VECS(ghash_tv_template) 3541 } 3542 }, { 3543 .alg = "hmac(md5)", 3544 .test = alg_test_hash, 3545 .suite = { 3546 .hash = __VECS(hmac_md5_tv_template) 3547 } 3548 }, { 3549 .alg = "hmac(rmd128)", 3550 .test = alg_test_hash, 3551 .suite = { 3552 .hash = __VECS(hmac_rmd128_tv_template) 3553 } 3554 }, { 3555 .alg = "hmac(rmd160)", 3556 .test = alg_test_hash, 3557 .suite = { 3558 .hash = __VECS(hmac_rmd160_tv_template) 3559 } 3560 }, { 3561 .alg = "hmac(sha1)", 3562 .test = alg_test_hash, 3563 .fips_allowed = 1, 3564 .suite = { 3565 .hash = __VECS(hmac_sha1_tv_template) 3566 } 3567 }, { 3568 .alg = "hmac(sha224)", 3569 .test = alg_test_hash, 3570 .fips_allowed = 1, 3571 .suite = { 3572 .hash = __VECS(hmac_sha224_tv_template) 3573 } 3574 }, { 3575 .alg = "hmac(sha256)", 3576 .test = alg_test_hash, 3577 .fips_allowed = 1, 3578 .suite = { 3579 .hash = __VECS(hmac_sha256_tv_template) 3580 } 3581 }, { 3582 .alg = "hmac(sha3-224)", 3583 .test = alg_test_hash, 3584 .fips_allowed = 1, 3585 .suite = { 3586 .hash = __VECS(hmac_sha3_224_tv_template) 3587 } 3588 }, { 3589 .alg = "hmac(sha3-256)", 3590 .test = alg_test_hash, 3591 .fips_allowed = 1, 3592 .suite = { 3593 .hash = __VECS(hmac_sha3_256_tv_template) 3594 } 3595 }, { 3596 .alg = "hmac(sha3-384)", 3597 .test = alg_test_hash, 3598 .fips_allowed = 1, 3599 .suite = { 3600 .hash = __VECS(hmac_sha3_384_tv_template) 3601 } 3602 }, { 3603 .alg = "hmac(sha3-512)", 3604 .test = alg_test_hash, 3605 .fips_allowed = 1, 3606 .suite = { 3607 .hash = __VECS(hmac_sha3_512_tv_template) 3608 } 3609 }, { 3610 .alg = "hmac(sha384)", 3611 .test = alg_test_hash, 3612 .fips_allowed = 1, 3613 .suite = { 3614 .hash = __VECS(hmac_sha384_tv_template) 3615 } 3616 }, { 3617 .alg = "hmac(sha512)", 3618 .test = alg_test_hash, 3619 .fips_allowed = 1, 3620 .suite = { 3621 .hash = __VECS(hmac_sha512_tv_template) 3622 } 3623 }, { 3624 .alg = "hmac(streebog256)", 3625 .test = alg_test_hash, 3626 .suite = { 3627 .hash = __VECS(hmac_streebog256_tv_template) 3628 } 3629 }, { 3630 .alg = "hmac(streebog512)", 3631 .test = alg_test_hash, 3632 .suite = { 3633 .hash = __VECS(hmac_streebog512_tv_template) 3634 } 3635 }, { 3636 .alg = "jitterentropy_rng", 3637 .fips_allowed = 1, 3638 .test = alg_test_null, 3639 }, { 3640 .alg = "kw(aes)", 3641 .test = alg_test_skcipher, 3642 .fips_allowed = 1, 3643 .suite = { 3644 .cipher = __VECS(aes_kw_tv_template) 3645 } 3646 }, { 3647 .alg = "lrw(aes)", 3648 .test = alg_test_skcipher, 3649 .suite = { 3650 .cipher = __VECS(aes_lrw_tv_template) 3651 } 3652 }, { 3653 .alg = "lrw(camellia)", 3654 .test = alg_test_skcipher, 3655 .suite = { 3656 .cipher = __VECS(camellia_lrw_tv_template) 3657 } 3658 }, { 3659 .alg = "lrw(cast6)", 3660 .test = alg_test_skcipher, 3661 .suite = { 3662 .cipher = __VECS(cast6_lrw_tv_template) 3663 } 3664 }, { 3665 .alg = "lrw(serpent)", 3666 .test = alg_test_skcipher, 3667 .suite = { 3668 .cipher = __VECS(serpent_lrw_tv_template) 3669 } 3670 }, { 3671 .alg = "lrw(twofish)", 3672 .test = alg_test_skcipher, 3673 .suite = { 3674 .cipher = __VECS(tf_lrw_tv_template) 3675 } 3676 }, { 3677 .alg = "lz4", 3678 .test = alg_test_comp, 3679 .fips_allowed = 1, 3680 .suite = { 3681 .comp = { 3682 .comp = __VECS(lz4_comp_tv_template), 3683 .decomp = __VECS(lz4_decomp_tv_template) 3684 } 3685 } 3686 }, { 3687 .alg = "lz4hc", 3688 .test = alg_test_comp, 3689 .fips_allowed = 1, 3690 .suite = { 3691 .comp = { 3692 .comp = __VECS(lz4hc_comp_tv_template), 3693 .decomp = __VECS(lz4hc_decomp_tv_template) 3694 } 3695 } 3696 }, { 3697 .alg = "lzo", 3698 .test = alg_test_comp, 3699 .fips_allowed = 1, 3700 .suite = { 3701 .comp = { 3702 .comp = __VECS(lzo_comp_tv_template), 3703 .decomp = __VECS(lzo_decomp_tv_template) 3704 } 3705 } 3706 }, { 3707 .alg = "md4", 3708 .test = alg_test_hash, 3709 .suite = { 3710 .hash = __VECS(md4_tv_template) 3711 } 3712 }, { 3713 .alg = "md5", 3714 .test = alg_test_hash, 3715 .suite = { 3716 .hash = __VECS(md5_tv_template) 3717 } 3718 }, { 3719 .alg = "michael_mic", 3720 .test = alg_test_hash, 3721 .suite = { 3722 .hash = __VECS(michael_mic_tv_template) 3723 } 3724 }, { 3725 .alg = "morus1280", 3726 .test = alg_test_aead, 3727 .suite = { 3728 .aead = __VECS(morus1280_tv_template) 3729 } 3730 }, { 3731 .alg = "morus640", 3732 .test = alg_test_aead, 3733 .suite = { 3734 .aead = __VECS(morus640_tv_template) 3735 } 3736 }, { 3737 .alg = "nhpoly1305", 3738 .test = alg_test_hash, 3739 .suite = { 3740 .hash = __VECS(nhpoly1305_tv_template) 3741 } 3742 }, { 3743 .alg = "ofb(aes)", 3744 .test = alg_test_skcipher, 3745 .fips_allowed = 1, 3746 .suite = { 3747 .cipher = __VECS(aes_ofb_tv_template) 3748 } 3749 }, { 3750 /* Same as ofb(aes) except the key is stored in 3751 * hardware secure memory which we reference by index 3752 */ 3753 .alg = "ofb(paes)", 3754 .test = alg_test_null, 3755 .fips_allowed = 1, 3756 }, { 3757 .alg = "pcbc(fcrypt)", 3758 .test = alg_test_skcipher, 3759 .suite = { 3760 .cipher = __VECS(fcrypt_pcbc_tv_template) 3761 } 3762 }, { 3763 .alg = "pkcs1pad(rsa,sha224)", 3764 .test = alg_test_null, 3765 .fips_allowed = 1, 3766 }, { 3767 .alg = "pkcs1pad(rsa,sha256)", 3768 .test = alg_test_akcipher, 3769 .fips_allowed = 1, 3770 .suite = { 3771 .akcipher = __VECS(pkcs1pad_rsa_tv_template) 3772 } 3773 }, { 3774 .alg = "pkcs1pad(rsa,sha384)", 3775 .test = alg_test_null, 3776 .fips_allowed = 1, 3777 }, { 3778 .alg = "pkcs1pad(rsa,sha512)", 3779 .test = alg_test_null, 3780 .fips_allowed = 1, 3781 }, { 3782 .alg = "poly1305", 3783 .test = alg_test_hash, 3784 .suite = { 3785 .hash = __VECS(poly1305_tv_template) 3786 } 3787 }, { 3788 .alg = "rfc3686(ctr(aes))", 3789 .test = alg_test_skcipher, 3790 .fips_allowed = 1, 3791 .suite = { 3792 .cipher = __VECS(aes_ctr_rfc3686_tv_template) 3793 } 3794 }, { 3795 .alg = "rfc4106(gcm(aes))", 3796 .test = alg_test_aead, 3797 .fips_allowed = 1, 3798 .suite = { 3799 .aead = __VECS(aes_gcm_rfc4106_tv_template) 3800 } 3801 }, { 3802 .alg = "rfc4309(ccm(aes))", 3803 .test = alg_test_aead, 3804 .fips_allowed = 1, 3805 .suite = { 3806 .aead = __VECS(aes_ccm_rfc4309_tv_template) 3807 } 3808 }, { 3809 .alg = "rfc4543(gcm(aes))", 3810 .test = alg_test_aead, 3811 .suite = { 3812 .aead = __VECS(aes_gcm_rfc4543_tv_template) 3813 } 3814 }, { 3815 .alg = "rfc7539(chacha20,poly1305)", 3816 .test = alg_test_aead, 3817 .suite = { 3818 .aead = __VECS(rfc7539_tv_template) 3819 } 3820 }, { 3821 .alg = "rfc7539esp(chacha20,poly1305)", 3822 .test = alg_test_aead, 3823 .suite = { 3824 .aead = __VECS(rfc7539esp_tv_template) 3825 } 3826 }, { 3827 .alg = "rmd128", 3828 .test = alg_test_hash, 3829 .suite = { 3830 .hash = __VECS(rmd128_tv_template) 3831 } 3832 }, { 3833 .alg = "rmd160", 3834 .test = alg_test_hash, 3835 .suite = { 3836 .hash = __VECS(rmd160_tv_template) 3837 } 3838 }, { 3839 .alg = "rmd256", 3840 .test = alg_test_hash, 3841 .suite = { 3842 .hash = __VECS(rmd256_tv_template) 3843 } 3844 }, { 3845 .alg = "rmd320", 3846 .test = alg_test_hash, 3847 .suite = { 3848 .hash = __VECS(rmd320_tv_template) 3849 } 3850 }, { 3851 .alg = "rsa", 3852 .test = alg_test_akcipher, 3853 .fips_allowed = 1, 3854 .suite = { 3855 .akcipher = __VECS(rsa_tv_template) 3856 } 3857 }, { 3858 .alg = "salsa20", 3859 .test = alg_test_skcipher, 3860 .suite = { 3861 .cipher = __VECS(salsa20_stream_tv_template) 3862 } 3863 }, { 3864 .alg = "sha1", 3865 .test = alg_test_hash, 3866 .fips_allowed = 1, 3867 .suite = { 3868 .hash = __VECS(sha1_tv_template) 3869 } 3870 }, { 3871 .alg = "sha224", 3872 .test = alg_test_hash, 3873 .fips_allowed = 1, 3874 .suite = { 3875 .hash = __VECS(sha224_tv_template) 3876 } 3877 }, { 3878 .alg = "sha256", 3879 .test = alg_test_hash, 3880 .fips_allowed = 1, 3881 .suite = { 3882 .hash = __VECS(sha256_tv_template) 3883 } 3884 }, { 3885 .alg = "sha3-224", 3886 .test = alg_test_hash, 3887 .fips_allowed = 1, 3888 .suite = { 3889 .hash = __VECS(sha3_224_tv_template) 3890 } 3891 }, { 3892 .alg = "sha3-256", 3893 .test = alg_test_hash, 3894 .fips_allowed = 1, 3895 .suite = { 3896 .hash = __VECS(sha3_256_tv_template) 3897 } 3898 }, { 3899 .alg = "sha3-384", 3900 .test = alg_test_hash, 3901 .fips_allowed = 1, 3902 .suite = { 3903 .hash = __VECS(sha3_384_tv_template) 3904 } 3905 }, { 3906 .alg = "sha3-512", 3907 .test = alg_test_hash, 3908 .fips_allowed = 1, 3909 .suite = { 3910 .hash = __VECS(sha3_512_tv_template) 3911 } 3912 }, { 3913 .alg = "sha384", 3914 .test = alg_test_hash, 3915 .fips_allowed = 1, 3916 .suite = { 3917 .hash = __VECS(sha384_tv_template) 3918 } 3919 }, { 3920 .alg = "sha512", 3921 .test = alg_test_hash, 3922 .fips_allowed = 1, 3923 .suite = { 3924 .hash = __VECS(sha512_tv_template) 3925 } 3926 }, { 3927 .alg = "sm3", 3928 .test = alg_test_hash, 3929 .suite = { 3930 .hash = __VECS(sm3_tv_template) 3931 } 3932 }, { 3933 .alg = "streebog256", 3934 .test = alg_test_hash, 3935 .suite = { 3936 .hash = __VECS(streebog256_tv_template) 3937 } 3938 }, { 3939 .alg = "streebog512", 3940 .test = alg_test_hash, 3941 .suite = { 3942 .hash = __VECS(streebog512_tv_template) 3943 } 3944 }, { 3945 .alg = "tgr128", 3946 .test = alg_test_hash, 3947 .suite = { 3948 .hash = __VECS(tgr128_tv_template) 3949 } 3950 }, { 3951 .alg = "tgr160", 3952 .test = alg_test_hash, 3953 .suite = { 3954 .hash = __VECS(tgr160_tv_template) 3955 } 3956 }, { 3957 .alg = "tgr192", 3958 .test = alg_test_hash, 3959 .suite = { 3960 .hash = __VECS(tgr192_tv_template) 3961 } 3962 }, { 3963 .alg = "vmac64(aes)", 3964 .test = alg_test_hash, 3965 .suite = { 3966 .hash = __VECS(vmac64_aes_tv_template) 3967 } 3968 }, { 3969 .alg = "wp256", 3970 .test = alg_test_hash, 3971 .suite = { 3972 .hash = __VECS(wp256_tv_template) 3973 } 3974 }, { 3975 .alg = "wp384", 3976 .test = alg_test_hash, 3977 .suite = { 3978 .hash = __VECS(wp384_tv_template) 3979 } 3980 }, { 3981 .alg = "wp512", 3982 .test = alg_test_hash, 3983 .suite = { 3984 .hash = __VECS(wp512_tv_template) 3985 } 3986 }, { 3987 .alg = "xcbc(aes)", 3988 .test = alg_test_hash, 3989 .suite = { 3990 .hash = __VECS(aes_xcbc128_tv_template) 3991 } 3992 }, { 3993 .alg = "xchacha12", 3994 .test = alg_test_skcipher, 3995 .suite = { 3996 .cipher = __VECS(xchacha12_tv_template) 3997 }, 3998 }, { 3999 .alg = "xchacha20", 4000 .test = alg_test_skcipher, 4001 .suite = { 4002 .cipher = __VECS(xchacha20_tv_template) 4003 }, 4004 }, { 4005 .alg = "xts(aes)", 4006 .test = alg_test_skcipher, 4007 .fips_allowed = 1, 4008 .suite = { 4009 .cipher = __VECS(aes_xts_tv_template) 4010 } 4011 }, { 4012 .alg = "xts(camellia)", 4013 .test = alg_test_skcipher, 4014 .suite = { 4015 .cipher = __VECS(camellia_xts_tv_template) 4016 } 4017 }, { 4018 .alg = "xts(cast6)", 4019 .test = alg_test_skcipher, 4020 .suite = { 4021 .cipher = __VECS(cast6_xts_tv_template) 4022 } 4023 }, { 4024 /* Same as xts(aes) except the key is stored in 4025 * hardware secure memory which we reference by index 4026 */ 4027 .alg = "xts(paes)", 4028 .test = alg_test_null, 4029 .fips_allowed = 1, 4030 }, { 4031 .alg = "xts(serpent)", 4032 .test = alg_test_skcipher, 4033 .suite = { 4034 .cipher = __VECS(serpent_xts_tv_template) 4035 } 4036 }, { 4037 .alg = "xts(twofish)", 4038 .test = alg_test_skcipher, 4039 .suite = { 4040 .cipher = __VECS(tf_xts_tv_template) 4041 } 4042 }, { 4043 .alg = "xts4096(paes)", 4044 .test = alg_test_null, 4045 .fips_allowed = 1, 4046 }, { 4047 .alg = "xts512(paes)", 4048 .test = alg_test_null, 4049 .fips_allowed = 1, 4050 }, { 4051 .alg = "zlib-deflate", 4052 .test = alg_test_comp, 4053 .fips_allowed = 1, 4054 .suite = { 4055 .comp = { 4056 .comp = __VECS(zlib_deflate_comp_tv_template), 4057 .decomp = __VECS(zlib_deflate_decomp_tv_template) 4058 } 4059 } 4060 }, { 4061 .alg = "zstd", 4062 .test = alg_test_comp, 4063 .fips_allowed = 1, 4064 .suite = { 4065 .comp = { 4066 .comp = __VECS(zstd_comp_tv_template), 4067 .decomp = __VECS(zstd_decomp_tv_template) 4068 } 4069 } 4070 } 4071 }; 4072 4073 static void alg_check_test_descs_order(void) 4074 { 4075 int i; 4076 4077 for (i = 1; i < ARRAY_SIZE(alg_test_descs); i++) { 4078 int diff = strcmp(alg_test_descs[i - 1].alg, 4079 alg_test_descs[i].alg); 4080 4081 if (WARN_ON(diff > 0)) { 4082 pr_warn("testmgr: alg_test_descs entries in wrong order: '%s' before '%s'\n", 4083 alg_test_descs[i - 1].alg, 4084 alg_test_descs[i].alg); 4085 } 4086 4087 if (WARN_ON(diff == 0)) { 4088 pr_warn("testmgr: duplicate alg_test_descs entry: '%s'\n", 4089 alg_test_descs[i].alg); 4090 } 4091 } 4092 } 4093 4094 static void alg_check_testvec_configs(void) 4095 { 4096 int i; 4097 4098 for (i = 0; i < ARRAY_SIZE(default_cipher_testvec_configs); i++) 4099 WARN_ON(!valid_testvec_config( 4100 &default_cipher_testvec_configs[i])); 4101 4102 for (i = 0; i < ARRAY_SIZE(default_hash_testvec_configs); i++) 4103 WARN_ON(!valid_testvec_config( 4104 &default_hash_testvec_configs[i])); 4105 } 4106 4107 static void testmgr_onetime_init(void) 4108 { 4109 alg_check_test_descs_order(); 4110 alg_check_testvec_configs(); 4111 4112 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS 4113 pr_warn("alg: extra crypto tests enabled. This is intended for developer use only.\n"); 4114 #endif 4115 } 4116 4117 static int alg_find_test(const char *alg) 4118 { 4119 int start = 0; 4120 int end = ARRAY_SIZE(alg_test_descs); 4121 4122 while (start < end) { 4123 int i = (start + end) / 2; 4124 int diff = strcmp(alg_test_descs[i].alg, alg); 4125 4126 if (diff > 0) { 4127 end = i; 4128 continue; 4129 } 4130 4131 if (diff < 0) { 4132 start = i + 1; 4133 continue; 4134 } 4135 4136 return i; 4137 } 4138 4139 return -1; 4140 } 4141 4142 int alg_test(const char *driver, const char *alg, u32 type, u32 mask) 4143 { 4144 int i; 4145 int j; 4146 int rc; 4147 4148 if (!fips_enabled && notests) { 4149 printk_once(KERN_INFO "alg: self-tests disabled\n"); 4150 return 0; 4151 } 4152 4153 DO_ONCE(testmgr_onetime_init); 4154 4155 if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) { 4156 char nalg[CRYPTO_MAX_ALG_NAME]; 4157 4158 if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >= 4159 sizeof(nalg)) 4160 return -ENAMETOOLONG; 4161 4162 i = alg_find_test(nalg); 4163 if (i < 0) 4164 goto notest; 4165 4166 if (fips_enabled && !alg_test_descs[i].fips_allowed) 4167 goto non_fips_alg; 4168 4169 rc = alg_test_cipher(alg_test_descs + i, driver, type, mask); 4170 goto test_done; 4171 } 4172 4173 i = alg_find_test(alg); 4174 j = alg_find_test(driver); 4175 if (i < 0 && j < 0) 4176 goto notest; 4177 4178 if (fips_enabled && ((i >= 0 && !alg_test_descs[i].fips_allowed) || 4179 (j >= 0 && !alg_test_descs[j].fips_allowed))) 4180 goto non_fips_alg; 4181 4182 rc = 0; 4183 if (i >= 0) 4184 rc |= alg_test_descs[i].test(alg_test_descs + i, driver, 4185 type, mask); 4186 if (j >= 0 && j != i) 4187 rc |= alg_test_descs[j].test(alg_test_descs + j, driver, 4188 type, mask); 4189 4190 test_done: 4191 if (rc && (fips_enabled || panic_on_fail)) 4192 panic("alg: self-tests for %s (%s) failed in %s mode!\n", 4193 driver, alg, fips_enabled ? "fips" : "panic_on_fail"); 4194 4195 if (fips_enabled && !rc) 4196 pr_info("alg: self-tests for %s (%s) passed\n", driver, alg); 4197 4198 return rc; 4199 4200 notest: 4201 printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver); 4202 return 0; 4203 non_fips_alg: 4204 return -EINVAL; 4205 } 4206 4207 #endif /* CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */ 4208 4209 EXPORT_SYMBOL_GPL(alg_test); 4210