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)  * GCM: Galois/Counter Mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <crypto/gf128mul.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <crypto/null.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <crypto/gcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) struct gcm_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 	struct crypto_skcipher_spawn ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	struct crypto_ahash_spawn ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) struct crypto_gcm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	struct crypto_skcipher *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	struct crypto_ahash *ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) struct crypto_rfc4106_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	u8 nonce[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) struct crypto_rfc4106_req_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	struct scatterlist src[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	struct scatterlist dst[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct aead_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) struct crypto_rfc4543_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct crypto_aead_spawn aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) struct crypto_rfc4543_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	struct crypto_sync_skcipher *null;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	u8 nonce[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) struct crypto_rfc4543_req_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	struct aead_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) struct crypto_gcm_ghash_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	unsigned int cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	struct scatterlist *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	int (*complete)(struct aead_request *req, u32 flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) struct crypto_gcm_req_priv_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	u8 iv[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	u8 auth_tag[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	u8 iauth_tag[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct scatterlist src[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct scatterlist dst[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	struct crypto_gcm_ghash_ctx ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		struct ahash_request ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		struct skcipher_request skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	} u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	u8 buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) } *gcm_zeroes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 			     unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct crypto_ahash *ghash = ctx->ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct crypto_skcipher *ctr = ctx->ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		be128 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		u8 iv[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		struct crypto_wait wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		struct scatterlist sg[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		struct skcipher_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	} *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 				       CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	err = crypto_skcipher_setkey(ctr, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	crypto_init_wait(&data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	sg_init_one(data->sg, &data->hash, sizeof(data->hash));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	skcipher_request_set_tfm(&data->req, ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 						  CRYPTO_TFM_REQ_MAY_BACKLOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 				      crypto_req_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 				      &data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	skcipher_request_set_crypt(&data->req, data->sg, data->sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				   sizeof(data->hash), data->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 							&data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 			       CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	kfree_sensitive(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 				  unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return crypto_gcm_check_authsize(authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static void crypto_gcm_init_common(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	__be32 counter = cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	sg_init_table(pctx->src, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	if (sg != pctx->src + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		sg_chain(pctx->src, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		sg_init_table(pctx->dst, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		if (sg != pctx->dst + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			sg_chain(pctx->dst, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) static void crypto_gcm_init_crypt(struct aead_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 				  unsigned int cryptlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	struct skcipher_request *skreq = &pctx->u.skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	dst = req->src == req->dst ? pctx->src : pctx->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	skcipher_request_set_tfm(skreq, ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	skcipher_request_set_crypt(skreq, pctx->src, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 				     cryptlen + sizeof(pctx->auth_tag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 				     pctx->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) static inline unsigned int gcm_remain(unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	len &= 0xfU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	return len ? 16 - len : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) static int gcm_hash_update(struct aead_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			   crypto_completion_t compl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			   struct scatterlist *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			   unsigned int len, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct ahash_request *ahreq = &pctx->u.ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	ahash_request_set_callback(ahreq, flags, compl, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	ahash_request_set_crypt(ahreq, src, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return crypto_ahash_update(ahreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) static int gcm_hash_remain(struct aead_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			   unsigned int remain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			   crypto_completion_t compl, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) static int gcm_hash_len(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct ahash_request *ahreq = &pctx->u.ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	be128 lengths;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	lengths.a = cpu_to_be64(req->assoclen * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	lengths.b = cpu_to_be64(gctx->cryptlen * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	memcpy(pctx->iauth_tag, &lengths, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	ahash_request_set_crypt(ahreq, &pctx->sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 				pctx->iauth_tag, sizeof(lengths));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	return crypto_ahash_finup(ahreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	return gctx->complete(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	err = gcm_hash_len_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	aead_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	return gcm_hash_len(req, flags) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	       gcm_hash_len_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 				       int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	err = gcm_hash_crypt_remain_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	aead_request_complete(req, err);
^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) static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	unsigned int remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	remain = gcm_remain(gctx->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (remain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		return gcm_hash_remain(req, remain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 				       gcm_hash_crypt_remain_done, flags) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		       gcm_hash_crypt_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	return gcm_hash_crypt_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	err = gcm_hash_crypt_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	aead_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	if (gctx->cryptlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		return gcm_hash_update(req, gcm_hash_crypt_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				       gctx->src, gctx->cryptlen, flags) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		       gcm_hash_crypt_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	return gcm_hash_crypt_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 				       int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	err = gcm_hash_assoc_remain_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	aead_request_complete(req, 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) static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	unsigned int remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	remain = gcm_remain(req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (remain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		return gcm_hash_remain(req, remain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				       gcm_hash_assoc_remain_done, flags) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		       gcm_hash_assoc_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	return gcm_hash_assoc_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	err = gcm_hash_assoc_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	aead_request_complete(req, 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) static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (req->assoclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		return gcm_hash_update(req, gcm_hash_assoc_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 				       req->src, req->assoclen, flags) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		       gcm_hash_assoc_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	return gcm_hash_assoc_remain_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	err = gcm_hash_init_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	aead_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static int gcm_hash(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	struct ahash_request *ahreq = &pctx->u.ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	ahash_request_set_tfm(ahreq, ctx->ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	return crypto_ahash_init(ahreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	       gcm_hash_init_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	u8 *auth_tag = pctx->auth_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	crypto_xor(auth_tag, pctx->iauth_tag, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	scatterwalk_map_and_copy(auth_tag, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 				 req->assoclen + req->cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 				 crypto_aead_authsize(aead), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	gctx->cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	gctx->complete = gcm_enc_copy_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	return gcm_hash(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	err = gcm_encrypt_continue(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	aead_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) static int crypto_gcm_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct skcipher_request *skreq = &pctx->u.skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	u32 flags = aead_request_flags(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	crypto_gcm_init_common(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	crypto_gcm_init_crypt(req, req->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	return crypto_skcipher_encrypt(skreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	       gcm_encrypt_continue(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static int crypto_gcm_verify(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	u8 *auth_tag = pctx->auth_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	u8 *iauth_tag = pctx->iauth_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	unsigned int cryptlen = req->cryptlen - authsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	crypto_xor(auth_tag, iauth_tag, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	scatterwalk_map_and_copy(iauth_tag, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 				 req->assoclen + cryptlen, authsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		err = crypto_gcm_verify(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	aead_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	struct skcipher_request *skreq = &pctx->u.skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	crypto_gcm_init_crypt(req, gctx->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) static int crypto_gcm_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	u32 flags = aead_request_flags(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	cryptlen -= authsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	crypto_gcm_init_common(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	gctx->src = sg_next(pctx->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	gctx->cryptlen = cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	gctx->complete = gcm_dec_hash_continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	return gcm_hash(req, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	struct crypto_skcipher *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct crypto_ahash *ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	ghash = crypto_spawn_ahash(&ictx->ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (IS_ERR(ghash))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		return PTR_ERR(ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	ctr = crypto_spawn_skcipher(&ictx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	err = PTR_ERR(ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (IS_ERR(ctr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		goto err_free_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	ctx->ctr = ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	ctx->ghash = ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	align = crypto_aead_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	crypto_aead_set_reqsize(tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		max(sizeof(struct skcipher_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		    crypto_skcipher_reqsize(ctr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		    sizeof(struct ahash_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		    crypto_ahash_reqsize(ghash)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) err_free_hash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	crypto_free_ahash(ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	crypto_free_ahash(ctx->ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	crypto_free_skcipher(ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) static void crypto_gcm_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	crypto_drop_skcipher(&ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	crypto_drop_ahash(&ctx->ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) static int crypto_gcm_create_common(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 				    struct rtattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 				    const char *ctr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				    const char *ghash_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	struct gcm_instance_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct skcipher_alg *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct hash_alg_common *ghash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 				ghash_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	ghash = crypto_spawn_ahash_alg(&ctx->ghash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	    ghash->digestsize != 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 				   ctr_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	/* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	    crypto_skcipher_alg_ivsize(ctr) != 16 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	    ctr->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		     "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		     "gcm_base(%s,%s)", ctr->base.cra_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		     ghash->base.cra_driver_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	inst->alg.base.cra_priority = (ghash->base.cra_priority +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				       ctr->base.cra_priority) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 				       ctr->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	inst->alg.ivsize = GCM_AES_IV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	inst->alg.maxauthsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	inst->alg.init = crypto_gcm_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	inst->alg.exit = crypto_gcm_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	inst->alg.setkey = crypto_gcm_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	inst->alg.setauthsize = crypto_gcm_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	inst->alg.encrypt = crypto_gcm_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	inst->alg.decrypt = crypto_gcm_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	inst->free = crypto_gcm_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		crypto_gcm_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	const char *cipher_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	char ctr_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	cipher_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (IS_ERR(cipher_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		return PTR_ERR(cipher_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) static int crypto_gcm_base_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 				  struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	const char *ctr_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	const char *ghash_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	ctr_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (IS_ERR(ctr_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		return PTR_ERR(ctr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	ghash_name = crypto_attr_alg_name(tb[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (IS_ERR(ghash_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return PTR_ERR(ghash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 				 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	struct crypto_aead *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (keylen < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	keylen -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	memcpy(ctx->nonce, key + keylen, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				     CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	return crypto_aead_setkey(child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				      unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	err = crypto_rfc4106_check_authsize(authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	return crypto_aead_setauthsize(ctx->child, authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	struct aead_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	struct crypto_aead *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			   crypto_aead_alignmask(child) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	memcpy(iv, ctx->nonce, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	memcpy(iv + 4, req->iv, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	sg_init_table(rctx->src, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (sg != rctx->src + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		sg_chain(rctx->src, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		sg_init_table(rctx->dst, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (sg != rctx->dst + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			sg_chain(rctx->dst, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	aead_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	aead_request_set_callback(subreq, req->base.flags, req->base.complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 				  req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	aead_request_set_crypt(subreq, rctx->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			       req->src == req->dst ? rctx->src : rctx->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			       req->cryptlen, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	aead_request_set_ad(subreq, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	return subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) static int crypto_rfc4106_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	err = crypto_ipsec_check_assoclen(req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	req = crypto_rfc4106_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	return crypto_aead_encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static int crypto_rfc4106_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	err = crypto_ipsec_check_assoclen(req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	req = crypto_rfc4106_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	return crypto_aead_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct crypto_aead *aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	aead = crypto_spawn_aead(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	if (IS_ERR(aead))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		return PTR_ERR(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	ctx->child = aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	align = crypto_aead_alignmask(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	crypto_aead_set_reqsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		sizeof(struct crypto_rfc4106_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		align + 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	crypto_free_aead(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static void crypto_rfc4106_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	crypto_drop_aead(aead_instance_ctx(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) static int crypto_rfc4106_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 				 struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	struct crypto_aead_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	struct aead_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	spawn = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			       crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	alg = crypto_spawn_aead_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	/* Underlying IV size must be 12. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	/* Not a stream cipher? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (alg->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		     "rfc4106(%s)", alg->base.cra_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	    CRYPTO_MAX_ALG_NAME ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	    snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		     "rfc4106(%s)", alg->base.cra_driver_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	inst->alg.init = crypto_rfc4106_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	inst->alg.exit = crypto_rfc4106_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	inst->alg.setkey = crypto_rfc4106_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	inst->alg.setauthsize = crypto_rfc4106_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	inst->alg.encrypt = crypto_rfc4106_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	inst->alg.decrypt = crypto_rfc4106_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	inst->free = crypto_rfc4106_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		crypto_rfc4106_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 				 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct crypto_aead *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	if (keylen < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	keylen -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	memcpy(ctx->nonce, key + keylen, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 				     CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	return crypto_aead_setkey(child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				      unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (authsize != 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return crypto_aead_setauthsize(ctx->child, authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct aead_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			   crypto_aead_alignmask(ctx->child) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		err = crypto_rfc4543_copy_src_to_dst(req, enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	memcpy(iv, ctx->nonce, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	memcpy(iv + 4, req->iv, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	aead_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	aead_request_set_callback(subreq, req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				  req->base.complete, req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	aead_request_set_crypt(subreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			       enc ? 0 : authsize, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				    subreq->cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	unsigned int nbytes = req->assoclen + req->cryptlen -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			      (enc ? 0 : authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	skcipher_request_set_sync_tfm(nreq, ctx->null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	return crypto_skcipher_encrypt(nreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) static int crypto_rfc4543_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	return crypto_ipsec_check_assoclen(req->assoclen) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	       crypto_rfc4543_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) static int crypto_rfc4543_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	return crypto_ipsec_check_assoclen(req->assoclen) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	       crypto_rfc4543_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	struct crypto_aead_spawn *spawn = &ictx->aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct crypto_aead *aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct crypto_sync_skcipher *null;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	aead = crypto_spawn_aead(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	if (IS_ERR(aead))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		return PTR_ERR(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	null = crypto_get_default_null_skcipher();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	err = PTR_ERR(null);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	if (IS_ERR(null))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		goto err_free_aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	ctx->child = aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	ctx->null = null;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	align = crypto_aead_alignmask(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	crypto_aead_set_reqsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		sizeof(struct crypto_rfc4543_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		align + GCM_AES_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) err_free_aead:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	crypto_free_aead(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	crypto_free_aead(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	crypto_put_default_null_skcipher();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static void crypto_rfc4543_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	crypto_drop_aead(&ctx->aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) static int crypto_rfc4543_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	struct aead_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	struct crypto_rfc4543_instance_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			       crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	alg = crypto_spawn_aead_alg(&ctx->aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	/* Underlying IV size must be 12. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	/* Not a stream cipher? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (alg->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		     "rfc4543(%s)", alg->base.cra_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	    CRYPTO_MAX_ALG_NAME ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	    snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		     "rfc4543(%s)", alg->base.cra_driver_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	inst->alg.init = crypto_rfc4543_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	inst->alg.exit = crypto_rfc4543_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	inst->alg.setkey = crypto_rfc4543_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	inst->alg.setauthsize = crypto_rfc4543_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	inst->alg.encrypt = crypto_rfc4543_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	inst->alg.decrypt = crypto_rfc4543_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	inst->free = crypto_rfc4543_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		crypto_rfc4543_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) static struct crypto_template crypto_gcm_tmpls[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		.name = "gcm_base",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		.create = crypto_gcm_base_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		.name = "gcm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		.create = crypto_gcm_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		.name = "rfc4106",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		.create = crypto_rfc4106_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		.name = "rfc4543",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		.create = crypto_rfc4543_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static int __init crypto_gcm_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (!gcm_zeroes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	err = crypto_register_templates(crypto_gcm_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 					ARRAY_SIZE(crypto_gcm_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		kfree(gcm_zeroes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static void __exit crypto_gcm_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	kfree(gcm_zeroes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	crypto_unregister_templates(crypto_gcm_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 				    ARRAY_SIZE(crypto_gcm_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) subsys_initcall(crypto_gcm_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) module_exit(crypto_gcm_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) MODULE_DESCRIPTION("Galois/Counter Mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) MODULE_ALIAS_CRYPTO("gcm_base");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) MODULE_ALIAS_CRYPTO("rfc4106");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) MODULE_ALIAS_CRYPTO("rfc4543");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) MODULE_ALIAS_CRYPTO("gcm");