Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: testmgr - Add hash export format testing

Ensure that the hash state can be exported to and imported from
the generic algorithm.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+94 -9
+86 -9
crypto/testmgr.c
··· 17 17 */ 18 18 19 19 #include <crypto/aead.h> 20 - #include <crypto/hash.h> 20 + #include <crypto/acompress.h> 21 + #include <crypto/akcipher.h> 22 + #include <crypto/drbg.h> 23 + #include <crypto/internal/cipher.h> 24 + #include <crypto/internal/hash.h> 25 + #include <crypto/internal/simd.h> 26 + #include <crypto/kpp.h> 27 + #include <crypto/rng.h> 28 + #include <crypto/sig.h> 21 29 #include <crypto/skcipher.h> 22 30 #include <linux/err.h> 23 31 #include <linux/fips.h> 32 + #include <linux/kernel.h> 24 33 #include <linux/module.h> 25 34 #include <linux/once.h> 26 35 #include <linux/prandom.h> ··· 37 28 #include <linux/slab.h> 38 29 #include <linux/string.h> 39 30 #include <linux/uio.h> 40 - #include <crypto/rng.h> 41 - #include <crypto/drbg.h> 42 - #include <crypto/akcipher.h> 43 - #include <crypto/kpp.h> 44 - #include <crypto/acompress.h> 45 - #include <crypto/sig.h> 46 - #include <crypto/internal/cipher.h> 47 - #include <crypto/internal/simd.h> 48 31 49 32 #include "internal.h" 50 33 ··· 1465 1464 return 0; 1466 1465 } 1467 1466 1467 + static int check_ahash_export(struct ahash_request *req, 1468 + const struct hash_testvec *vec, 1469 + const char *vec_name, 1470 + const struct testvec_config *cfg, 1471 + const char *driver, u8 *hashstate) 1472 + { 1473 + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1474 + const unsigned int digestsize = crypto_ahash_digestsize(tfm); 1475 + HASH_FBREQ_ON_STACK(fbreq, req); 1476 + int err; 1477 + 1478 + if (!vec->state) 1479 + return 0; 1480 + 1481 + err = crypto_ahash_export(req, hashstate); 1482 + if (err) { 1483 + pr_err("alg: ahash: %s mixed export() failed with err %d on test vector %s, cfg=\"%s\"\n", 1484 + driver, err, vec_name, cfg->name); 1485 + return err; 1486 + } 1487 + err = crypto_ahash_import(req, vec->state); 1488 + if (err) { 1489 + pr_err("alg: ahash: %s mixed import() failed with err %d on test vector %s, cfg=\"%s\"\n", 1490 + driver, err, vec_name, cfg->name); 1491 + return err; 1492 + } 1493 + err = crypto_ahash_import(fbreq, hashstate); 1494 + if (err) { 1495 + pr_err("alg: ahash: %s fallback import() failed with err %d on test vector %s, cfg=\"%s\"\n", 1496 + crypto_ahash_driver_name(crypto_ahash_reqtfm(fbreq)), err, vec_name, cfg->name); 1497 + return err; 1498 + } 1499 + ahash_request_set_crypt(fbreq, NULL, hashstate, 0); 1500 + testmgr_poison(hashstate, digestsize + TESTMGR_POISON_LEN); 1501 + err = crypto_ahash_final(fbreq); 1502 + if (err) { 1503 + pr_err("alg: ahash: %s fallback final() failed with err %d on test vector %s, cfg=\"%s\"\n", 1504 + crypto_ahash_driver_name(crypto_ahash_reqtfm(fbreq)), err, vec_name, cfg->name); 1505 + return err; 1506 + } 1507 + return check_hash_result("ahash export", hashstate, digestsize, vec, vec_name, driver, cfg); 1508 + } 1509 + 1468 1510 /* Test one hash test vector in one configuration, using the ahash API */ 1469 1511 static int test_ahash_vec_cfg(const struct hash_testvec *vec, 1470 1512 const char *vec_name, ··· 1653 1609 driver, vec_name, cfg); 1654 1610 if (err) 1655 1611 return err; 1612 + err = check_ahash_export(req, vec, vec_name, cfg, 1613 + driver, hashstate); 1614 + if (err) 1615 + return err; 1656 1616 err = do_ahash_op(crypto_ahash_final, req, &wait, cfg->nosimd); 1657 1617 if (err) { 1658 1618 pr_err("alg: ahash: %s final() failed with err %d on test vector %s, cfg=\"%s\"\n", ··· 1780 1732 vec->digest_error = crypto_hash_digest( 1781 1733 crypto_ahash_reqtfm(req), vec->plaintext, 1782 1734 vec->psize, (u8 *)vec->digest); 1735 + 1736 + if (vec->digest_error || !vec->state) 1737 + goto done; 1738 + 1739 + ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 1740 + ahash_request_set_virt(req, vec->plaintext, (u8 *)vec->digest, 1741 + vec->psize); 1742 + crypto_ahash_init(req); 1743 + crypto_ahash_update(req); 1744 + crypto_ahash_export(req, (u8 *)vec->state); 1745 + 1783 1746 done: 1784 1747 snprintf(name, max_namelen, "\"random: psize=%u ksize=%u\"", 1785 1748 vec->psize, vec->ksize); ··· 1809 1750 { 1810 1751 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1811 1752 const unsigned int digestsize = crypto_ahash_digestsize(tfm); 1753 + const unsigned int statesize = crypto_ahash_statesize(tfm); 1812 1754 const unsigned int blocksize = crypto_ahash_blocksize(tfm); 1813 1755 const unsigned int maxdatasize = (2 * PAGE_SIZE) - TESTMGR_POISON_LEN; 1814 1756 const char *algname = crypto_hash_alg_common(tfm)->base.cra_name; ··· 1882 1822 goto out; 1883 1823 } 1884 1824 1825 + if (crypto_hash_no_export_core(tfm) || 1826 + crypto_hash_no_export_core(generic_tfm)) 1827 + ; 1828 + else if (statesize != crypto_ahash_statesize(generic_tfm)) { 1829 + pr_err("alg: hash: statesize for %s (%u) doesn't match generic impl (%u)\n", 1830 + driver, statesize, 1831 + crypto_ahash_statesize(generic_tfm)); 1832 + err = -EINVAL; 1833 + goto out; 1834 + } else { 1835 + vec.state = kmalloc(statesize, GFP_KERNEL); 1836 + err = -ENOMEM; 1837 + if (!vec.state) 1838 + goto out; 1839 + } 1840 + 1885 1841 /* 1886 1842 * Now generate test vectors using the generic implementation, and test 1887 1843 * the other implementation against them. ··· 1930 1854 kfree(vec.key); 1931 1855 kfree(vec.plaintext); 1932 1856 kfree(vec.digest); 1857 + kfree(vec.state); 1933 1858 ahash_request_free(generic_req); 1934 1859 crypto_free_ahash(generic_tfm); 1935 1860 return err;
+2
crypto/testmgr.h
··· 29 29 * hash_testvec: structure to describe a hash (message digest) test 30 30 * @key: Pointer to key (NULL if none) 31 31 * @plaintext: Pointer to source data 32 + * @state: Pointer to expected state 32 33 * @digest: Pointer to expected digest 33 34 * @psize: Length of source data in bytes 34 35 * @ksize: Length of @key in bytes (0 if no key) ··· 40 39 struct hash_testvec { 41 40 const char *key; 42 41 const char *plaintext; 42 + const char *state; 43 43 const char *digest; 44 44 unsigned int psize; 45 45 unsigned short ksize;
+6
include/crypto/internal/hash.h
··· 91 91 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY); 92 92 } 93 93 94 + static inline bool crypto_hash_no_export_core(struct crypto_ahash *tfm) 95 + { 96 + return crypto_hash_alg_common(tfm)->base.cra_flags & 97 + CRYPTO_AHASH_ALG_NO_EXPORT_CORE; 98 + } 99 + 94 100 int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, 95 101 struct crypto_instance *inst, 96 102 const char *name, u32 type, u32 mask);