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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Shared crypto simd helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2016 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Based on aesni-intel_glue.c by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Copyright (C) 2008, Intel Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *    Author: Huang Ying <ying.huang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Shared crypto SIMD helpers.  These functions dynamically create and register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * an skcipher or AEAD algorithm that wraps another, internal algorithm.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * wrapper ensures that the internal algorithm is only executed in a context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * where SIMD instructions are usable, i.e. where may_use_simd() returns true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * If SIMD is already usable, the wrapper directly calls the internal algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Otherwise it defers execution to a workqueue via cryptd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * This is an alternative to the internal algorithm implementing a fallback for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * the !may_use_simd() case itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Note that the wrapper algorithm is asynchronous, i.e. it has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * CRYPTO_ALG_ASYNC flag set.  Therefore it won't be found by users who
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * explicitly allocate a synchronous algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <crypto/cryptd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* skcipher support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct simd_skcipher_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	const char *ialg_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct skcipher_alg alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct simd_skcipher_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct cryptd_skcipher *cryptd_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int simd_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct crypto_skcipher *child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					 CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return crypto_skcipher_setkey(child, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int simd_skcipher_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct skcipher_request *subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	subreq = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	*subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!crypto_simd_usable() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	    (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		child = cryptd_skcipher_child(ctx->cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	skcipher_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return crypto_skcipher_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int simd_skcipher_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct skcipher_request *subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	subreq = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	*subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (!crypto_simd_usable() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	    (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		child = cryptd_skcipher_child(ctx->cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	skcipher_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return crypto_skcipher_decrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void simd_skcipher_exit(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	cryptd_free_skcipher(ctx->cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int simd_skcipher_init(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct cryptd_skcipher *cryptd_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct simd_skcipher_alg *salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	alg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	salg = container_of(alg, struct simd_skcipher_alg, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	cryptd_tfm = cryptd_alloc_skcipher(salg->ialg_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					   CRYPTO_ALG_INTERNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					   CRYPTO_ALG_INTERNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (IS_ERR(cryptd_tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return PTR_ERR(cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ctx->cryptd_tfm = cryptd_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	reqsize = crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	reqsize = max(reqsize, crypto_skcipher_reqsize(&cryptd_tfm->base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	reqsize += sizeof(struct skcipher_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	crypto_skcipher_set_reqsize(tfm, reqsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 						      const char *drvname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 						      const char *basename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct simd_skcipher_alg *salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct skcipher_alg *ialg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	tfm = crypto_alloc_skcipher(basename, CRYPTO_ALG_INTERNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				    CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return ERR_CAST(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	ialg = crypto_skcipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	salg = kzalloc(sizeof(*salg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (!salg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		salg = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		goto out_put_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	salg->ialg_name = basename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	alg = &salg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", algname) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		     drvname) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	alg->base.cra_flags = CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		(ialg->base.cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	alg->base.cra_priority = ialg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	alg->base.cra_blocksize = ialg->base.cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	alg->base.cra_alignmask = ialg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	alg->base.cra_module = ialg->base.cra_module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	alg->base.cra_ctxsize = sizeof(struct simd_skcipher_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	alg->ivsize = ialg->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	alg->chunksize = ialg->chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	alg->min_keysize = ialg->min_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	alg->max_keysize = ialg->max_keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	alg->init = simd_skcipher_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	alg->exit = simd_skcipher_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	alg->setkey = simd_skcipher_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	alg->encrypt = simd_skcipher_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	alg->decrypt = simd_skcipher_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	err = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) out_put_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	crypto_free_skcipher(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) out_free_salg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	kfree(salg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	salg = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	goto out_put_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) EXPORT_SYMBOL_GPL(simd_skcipher_create_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct simd_skcipher_alg *simd_skcipher_create(const char *algname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 					       const char *basename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	char drvname[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (snprintf(drvname, CRYPTO_MAX_ALG_NAME, "simd-%s", basename) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return simd_skcipher_create_compat(algname, drvname, basename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) EXPORT_SYMBOL_GPL(simd_skcipher_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) void simd_skcipher_free(struct simd_skcipher_alg *salg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	crypto_unregister_skcipher(&salg->alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	kfree(salg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL_GPL(simd_skcipher_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int simd_register_skciphers_compat(struct skcipher_alg *algs, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				   struct simd_skcipher_alg **simd_algs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	const char *algname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	const char *drvname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	const char *basename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct simd_skcipher_alg *simd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	err = crypto_register_skciphers(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		WARN_ON(strncmp(algs[i].base.cra_name, "__", 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		WARN_ON(strncmp(algs[i].base.cra_driver_name, "__", 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		algname = algs[i].base.cra_name + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		drvname = algs[i].base.cra_driver_name + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		basename = algs[i].base.cra_driver_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		simd = simd_skcipher_create_compat(algname, drvname, basename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		err = PTR_ERR(simd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (IS_ERR(simd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		simd_algs[i] = simd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) err_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	simd_unregister_skciphers(algs, count, simd_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) EXPORT_SYMBOL_GPL(simd_register_skciphers_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) void simd_unregister_skciphers(struct skcipher_alg *algs, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			       struct simd_skcipher_alg **simd_algs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	crypto_unregister_skciphers(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (simd_algs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			simd_skcipher_free(simd_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			simd_algs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) EXPORT_SYMBOL_GPL(simd_unregister_skciphers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* AEAD support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct simd_aead_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	const char *ialg_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct aead_alg alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct simd_aead_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct cryptd_aead *cryptd_tfm;
^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) static int simd_aead_setkey(struct crypto_aead *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct crypto_aead *child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	crypto_aead_set_flags(child, crypto_aead_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				     CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return crypto_aead_setkey(child, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int simd_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct crypto_aead *child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return crypto_aead_setauthsize(child, authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int simd_aead_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct aead_request *subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	subreq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	*subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (!crypto_simd_usable() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	    (in_atomic() && cryptd_aead_queued(ctx->cryptd_tfm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		child = cryptd_aead_child(ctx->cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	aead_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return crypto_aead_encrypt(subreq);
^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) static int simd_aead_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct aead_request *subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	subreq = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	*subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (!crypto_simd_usable() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	    (in_atomic() && cryptd_aead_queued(ctx->cryptd_tfm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		child = &ctx->cryptd_tfm->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		child = cryptd_aead_child(ctx->cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	aead_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return crypto_aead_decrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void simd_aead_exit(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	cryptd_free_aead(ctx->cryptd_tfm);
^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) static int simd_aead_init(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct cryptd_aead *cryptd_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct simd_aead_alg *salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	struct aead_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	unsigned reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	alg = crypto_aead_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	salg = container_of(alg, struct simd_aead_alg, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	cryptd_tfm = cryptd_alloc_aead(salg->ialg_name, CRYPTO_ALG_INTERNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				       CRYPTO_ALG_INTERNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (IS_ERR(cryptd_tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return PTR_ERR(cryptd_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	ctx->cryptd_tfm = cryptd_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	reqsize = crypto_aead_reqsize(cryptd_aead_child(cryptd_tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	reqsize = max(reqsize, crypto_aead_reqsize(&cryptd_tfm->base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	reqsize += sizeof(struct aead_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	crypto_aead_set_reqsize(tfm, reqsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct simd_aead_alg *simd_aead_create_compat(const char *algname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 					      const char *drvname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 					      const char *basename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct simd_aead_alg *salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct crypto_aead *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct aead_alg *ialg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	struct aead_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	tfm = crypto_alloc_aead(basename, CRYPTO_ALG_INTERNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		return ERR_CAST(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	ialg = crypto_aead_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	salg = kzalloc(sizeof(*salg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!salg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		salg = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		goto out_put_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	salg->ialg_name = basename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	alg = &salg->alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", algname) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		     drvname) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	alg->base.cra_flags = CRYPTO_ALG_ASYNC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		(ialg->base.cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	alg->base.cra_priority = ialg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	alg->base.cra_blocksize = ialg->base.cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	alg->base.cra_alignmask = ialg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	alg->base.cra_module = ialg->base.cra_module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	alg->base.cra_ctxsize = sizeof(struct simd_aead_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	alg->ivsize = ialg->ivsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	alg->maxauthsize = ialg->maxauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	alg->chunksize = ialg->chunksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	alg->init = simd_aead_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	alg->exit = simd_aead_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	alg->setkey = simd_aead_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	alg->setauthsize = simd_aead_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	alg->encrypt = simd_aead_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	alg->decrypt = simd_aead_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	err = crypto_register_aead(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		goto out_free_salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) out_put_tfm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	crypto_free_aead(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	return salg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) out_free_salg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	kfree(salg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	salg = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	goto out_put_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) EXPORT_SYMBOL_GPL(simd_aead_create_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct simd_aead_alg *simd_aead_create(const char *algname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 				       const char *basename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	char drvname[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (snprintf(drvname, CRYPTO_MAX_ALG_NAME, "simd-%s", basename) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	return simd_aead_create_compat(algname, drvname, basename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) EXPORT_SYMBOL_GPL(simd_aead_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) void simd_aead_free(struct simd_aead_alg *salg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	crypto_unregister_aead(&salg->alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	kfree(salg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) EXPORT_SYMBOL_GPL(simd_aead_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) int simd_register_aeads_compat(struct aead_alg *algs, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			       struct simd_aead_alg **simd_algs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	const char *algname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	const char *drvname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	const char *basename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct simd_aead_alg *simd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	err = crypto_register_aeads(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		WARN_ON(strncmp(algs[i].base.cra_name, "__", 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		WARN_ON(strncmp(algs[i].base.cra_driver_name, "__", 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		algname = algs[i].base.cra_name + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		drvname = algs[i].base.cra_driver_name + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		basename = algs[i].base.cra_driver_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		simd = simd_aead_create_compat(algname, drvname, basename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		err = PTR_ERR(simd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (IS_ERR(simd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			goto err_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		simd_algs[i] = simd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) err_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	simd_unregister_aeads(algs, count, simd_algs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) EXPORT_SYMBOL_GPL(simd_register_aeads_compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) void simd_unregister_aeads(struct aead_alg *algs, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			   struct simd_aead_alg **simd_algs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	crypto_unregister_aeads(algs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		if (simd_algs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			simd_aead_free(simd_algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			simd_algs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) EXPORT_SYMBOL_GPL(simd_unregister_aeads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) MODULE_LICENSE("GPL");