Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Block crypto operations until tests complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2021 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This file defines the fips140_crypto_register_*() functions, to which all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * calls to crypto_register_*() in the module are redirected.  These functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * override the tfm initialization function of each algorithm to insert a wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * for the module having completed its self-tests and integrity check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * The exact field that we override depends on the algorithm type.  For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * algorithm types that have a strongly-typed initialization function pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * (e.g. skcipher), we must override that, since cra_init isn't guaranteed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * called for those despite the field being present in the base struct.  For the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * other algorithm types (e.g. "cipher") we must override cra_init.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * All of this applies to both normal algorithms and template instances.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * The purpose of all of this is to meet a FIPS requirement where the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * must not produce any output from cryptographic algorithms until it completes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * its tests.  Technically this is impossible, but this solution meets the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * intent of the requirement, assuming the user makes a supported sequence of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * API calls.  Note that we can't simply run the tests before registering the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * algorithms, as the algorithms must be registered in order to run the tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * It would be much easier to handle this in the kernel's crypto API framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Unfortunately, that was deemed insufficient because the module itself is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * required to do the enforcement.  What is *actually* required is still very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * vague, but the approach implemented here should meet the requirement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * This file is the one place in fips140.ko that needs to call the kernel's real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * algorithm registration functions, so #undefine all the macros from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * fips140-defs.h so that the "fips140_" prefix doesn't automatically get added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #undef aead_register_instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #undef ahash_register_instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #undef crypto_register_aead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #undef crypto_register_aeads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #undef crypto_register_ahash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #undef crypto_register_ahashes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #undef crypto_register_alg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #undef crypto_register_algs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #undef crypto_register_rng
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #undef crypto_register_rngs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #undef crypto_register_shash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #undef crypto_register_shashes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #undef crypto_register_skcipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #undef crypto_register_skciphers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #undef shash_register_instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #undef skcipher_register_instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <crypto/internal/rng.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #include <linux/xarray.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #include "fips140-module.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /* Indicates whether the self-tests and integrity check have completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) DECLARE_COMPLETION(fips140_tests_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /* The thread running the self-tests and integrity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) struct task_struct *fips140_init_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * Map from crypto_alg to original initialization function (possibly NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * Note: unregistering an algorithm will leak its map entry, as we don't bother
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * to remove it.  This should be fine since fips140.ko can't be unloaded.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * proper solution would be to store the original function pointer in a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * field in 'struct crypto_alg', but that would require kernel support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static DEFINE_XARRAY(fips140_init_func_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static bool fips140_ready(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return completion_done(&fips140_tests_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Wait until crypto operations are allowed to proceed.  Return true if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * tests are done, or false if the caller is the thread running the tests so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * is allowed to proceed anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static bool fips140_wait_until_ready(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (fips140_ready())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * The thread running the tests must not wait.  Since tfms can only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * allocated in task context, we can reliably determine whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * invocation is from that thread or not by checking 'current'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (current == fips140_init_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	pr_info("blocking user of %s until tests complete\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		alg->cra_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	wait_for_completion(&fips140_tests_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	pr_info("tests done, allowing %s to proceed\n", alg->cra_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int fips140_store_init_function(struct crypto_alg *alg, void *func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * The XArray API requires 4-byte aligned values.  Although function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * pointers in general aren't guaranteed to be 4-byte aligned, it should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 * be the case for the platforms this module is used on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (WARN_ON((unsigned long)func & 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ret = xa_store(&fips140_init_func_map, (unsigned long)alg, func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return xa_err(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Get the algorithm's original initialization function (possibly NULL) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void *fips140_load_init_function(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return xa_load(&fips140_init_func_map, (unsigned long)alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* tfm initialization function overrides */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int fips140_alg_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct crypto_alg *alg = tfm->__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int (*cra_init)(struct crypto_tfm *tfm) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		fips140_load_init_function(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (fips140_wait_until_ready(alg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		WRITE_ONCE(alg->cra_init, cra_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return cra_init ? cra_init(tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int fips140_aead_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct aead_alg *alg = crypto_aead_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int (*init)(struct crypto_aead *tfm) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		fips140_load_init_function(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (fips140_wait_until_ready(&alg->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		WRITE_ONCE(alg->init, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return init ? init(tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int fips140_ahash_init_tfm(struct crypto_ahash *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct ahash_alg *alg = container_of(halg, struct ahash_alg, halg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int (*init_tfm)(struct crypto_ahash *tfm) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		fips140_load_init_function(&halg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (fips140_wait_until_ready(&halg->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		WRITE_ONCE(alg->init_tfm, init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return init_tfm ? init_tfm(tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int fips140_shash_init_tfm(struct crypto_shash *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct shash_alg *alg = crypto_shash_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int (*init_tfm)(struct crypto_shash *tfm) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		fips140_load_init_function(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (fips140_wait_until_ready(&alg->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		WRITE_ONCE(alg->init_tfm, init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return init_tfm ? init_tfm(tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int fips140_skcipher_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int (*init)(struct crypto_skcipher *tfm) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		fips140_load_init_function(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (fips140_wait_until_ready(&alg->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		WRITE_ONCE(alg->init, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return init ? init(tfm) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Single algorithm registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) #define prepare_alg(alg, base_alg, field, wrapper_func)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ({									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	int err = 0;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!fips140_ready() && alg->field != wrapper_func) {		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		err = fips140_store_init_function(base_alg, alg->field);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (err == 0)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			alg->field = wrapper_func;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	err;								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int fips140_prepare_alg(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * Override cra_init.  This is only for algorithm types like cipher and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * rng that don't have a strongly-typed initialization function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return prepare_alg(alg, alg, cra_init, fips140_alg_init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int fips140_prepare_aead_alg(struct aead_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return prepare_alg(alg, &alg->base, init, fips140_aead_init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int fips140_prepare_ahash_alg(struct ahash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return prepare_alg(alg, &alg->halg.base, init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			   fips140_ahash_init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int fips140_prepare_rng_alg(struct rng_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * rng doesn't have a strongly-typed initialization function, so we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * treat rng algorithms as "generic" algorithms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return fips140_prepare_alg(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int fips140_prepare_shash_alg(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return prepare_alg(alg, &alg->base, init_tfm, fips140_shash_init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int fips140_prepare_skcipher_alg(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return prepare_alg(alg, &alg->base, init, fips140_skcipher_init_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int fips140_crypto_register_alg(struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return fips140_prepare_alg(alg) ?: crypto_register_alg(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int fips140_crypto_register_aead(struct aead_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return fips140_prepare_aead_alg(alg) ?: crypto_register_aead(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int fips140_crypto_register_ahash(struct ahash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return fips140_prepare_ahash_alg(alg) ?: crypto_register_ahash(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int fips140_crypto_register_rng(struct rng_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return fips140_prepare_rng_alg(alg) ?: crypto_register_rng(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int fips140_crypto_register_shash(struct shash_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return fips140_prepare_shash_alg(alg) ?: crypto_register_shash(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int fips140_crypto_register_skcipher(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return fips140_prepare_skcipher_alg(alg) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Instance registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int fips140_aead_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				   struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return fips140_prepare_aead_alg(&inst->alg) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int fips140_ahash_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				    struct ahash_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return fips140_prepare_ahash_alg(&inst->alg) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		ahash_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int fips140_shash_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				    struct shash_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	return fips140_prepare_shash_alg(&inst->alg) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		shash_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int fips140_skcipher_register_instance(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				       struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	return fips140_prepare_skcipher_alg(&inst->alg) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* Bulk algorithm registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int fips140_crypto_register_algs(struct crypto_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		err = fips140_prepare_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return crypto_register_algs(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int fips140_crypto_register_aeads(struct aead_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		err = fips140_prepare_aead_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return crypto_register_aeads(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int fips140_crypto_register_ahashes(struct ahash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		err = fips140_prepare_ahash_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return crypto_register_ahashes(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int fips140_crypto_register_rngs(struct rng_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		err = fips140_prepare_rng_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return crypto_register_rngs(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int fips140_crypto_register_shashes(struct shash_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		err = fips140_prepare_shash_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return crypto_register_shashes(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int fips140_crypto_register_skciphers(struct skcipher_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		err = fips140_prepare_skcipher_alg(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return crypto_register_skciphers(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }