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)  * CCM: Counter with CBC-MAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
^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/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct ccm_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct crypto_skcipher_spawn ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct crypto_ahash_spawn mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct crypto_ccm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct crypto_ahash *mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct crypto_skcipher *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct crypto_rfc4309_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct crypto_aead *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u8 nonce[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct crypto_rfc4309_req_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct scatterlist src[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct scatterlist dst[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct aead_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct crypto_ccm_req_priv_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u8 odata[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u8 idata[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u8 auth_tag[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct scatterlist src[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct scatterlist dst[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		struct ahash_request ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		struct skcipher_request skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	};
^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 cbcmac_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct crypto_cipher *child;
^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 cbcmac_desc_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int set_msg_len(u8 *block, unsigned int msglen, int csize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	__be32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	memset(block, 0, csize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	block += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (csize >= 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		csize = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	else if (msglen > (1 << (8 * csize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	data = cpu_to_be32(msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			     unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct crypto_skcipher *ctr = ctx->ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct crypto_ahash *mac = ctx->mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				       CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	err = crypto_skcipher_setkey(ctr, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				    CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return crypto_ahash_setkey(mac, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				  unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	switch (authsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	case 12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	case 14:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int format_input(u8 *info, struct aead_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			unsigned int cryptlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	unsigned int lp = req->iv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned int l = lp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned int m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	m = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	memcpy(info, req->iv, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* format control info per RFC 3610 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * NIST Special Publication 800-38C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	*info |= (8 * ((m - 2) / 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (req->assoclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		*info |= 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return set_msg_len(info + 16 - l, cryptlen, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int format_adata(u8 *adata, unsigned int a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/* add control info for associated data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * RFC 3610 and NIST Special Publication 800-38C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (a < 65280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		*(__be16 *)adata = cpu_to_be16(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	} else  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		*(__be16 *)adata = cpu_to_be16(0xfffe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		*(__be32 *)&adata[2] = cpu_to_be32(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		len = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			   unsigned int cryptlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct ahash_request *ahreq = &pctx->ahreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	unsigned int assoclen = req->assoclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct scatterlist sg[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	u8 *odata = pctx->odata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	u8 *idata = pctx->idata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int ilen, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/* format control data for input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	err = format_input(odata, req, cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	sg_init_table(sg, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	sg_set_buf(&sg[0], odata, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	/* format associated data and compute into mac */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (assoclen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ilen = format_adata(idata, assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		sg_set_buf(&sg[1], idata, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		sg_chain(sg, 3, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		ilen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		sg_chain(sg, 2, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	ahash_request_set_tfm(ahreq, ctx->mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	err = crypto_ahash_init(ahreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	err = crypto_ahash_update(ahreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* we need to pad the MAC input to a round multiple of the block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ilen = 16 - (assoclen + ilen) % 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (ilen < 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		memset(idata, 0, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		sg_init_table(sg, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		sg_set_buf(&sg[0], idata, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (plain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			sg_chain(sg, 2, plain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		plain = sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		cryptlen += ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	err = crypto_ahash_finup(ahreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	u8 *odata = pctx->odata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		scatterwalk_map_and_copy(odata, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 					 req->assoclen + req->cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 					 crypto_aead_authsize(aead), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	aead_request_complete(req, err);
^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 inline int crypto_ccm_check_iv(const u8 *iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* 2 <= L <= 8, so 1 <= L' <= 7. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (1 > iv[0] || iv[0] > 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	u8 *iv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	err = crypto_ccm_check_iv(iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (err)
^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) 	pctx->flags = aead_request_flags(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 /* Note: rfc 3610 and NIST 800-38C require counter of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * zero to encrypt auth tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	memset(iv + 15 - iv[0], 0, iv[0] + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	sg_init_table(pctx->src, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	sg_set_buf(pctx->src, tag, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (sg != pctx->src + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		sg_chain(pctx->src, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		sg_init_table(pctx->dst, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		sg_set_buf(pctx->dst, tag, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (sg != pctx->dst + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			sg_chain(pctx->dst, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int crypto_ccm_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct skcipher_request *skreq = &pctx->skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	u8 *odata = pctx->odata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	u8 *iv = req->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	err = crypto_ccm_init_crypt(req, odata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	dst = pctx->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (req->src != req->dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		dst = pctx->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	skcipher_request_set_tfm(skreq, ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	skcipher_request_set_callback(skreq, pctx->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				      crypto_ccm_encrypt_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	err = crypto_skcipher_encrypt(skreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	/* copy authtag to end of dst */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				 crypto_aead_authsize(aead), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				   int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct aead_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	unsigned int cryptlen = req->cryptlen - authsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	pctx->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		err = crypto_ccm_auth(req, dst, cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			err = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^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 crypto_ccm_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct skcipher_request *skreq = &pctx->skreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct scatterlist *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	unsigned int authsize = crypto_aead_authsize(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	unsigned int cryptlen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	u8 *authtag = pctx->auth_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	u8 *odata = pctx->odata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	u8 *iv = pctx->idata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	cryptlen -= authsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	err = crypto_ccm_init_crypt(req, authtag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				 authsize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	dst = pctx->src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (req->src != req->dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		dst = pctx->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	memcpy(iv, req->iv, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	skcipher_request_set_tfm(skreq, ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	skcipher_request_set_callback(skreq, pctx->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 				      crypto_ccm_decrypt_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	err = crypto_skcipher_decrypt(skreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/* verify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (crypto_memneq(authtag, odata, authsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct crypto_ahash *mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	struct crypto_skcipher *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	mac = crypto_spawn_ahash(&ictx->mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (IS_ERR(mac))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		return PTR_ERR(mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	ctr = crypto_spawn_skcipher(&ictx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	err = PTR_ERR(ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (IS_ERR(ctr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		goto err_free_mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	ctx->mac = mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	ctx->ctr = ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	align = crypto_aead_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	crypto_aead_set_reqsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		align + sizeof(struct crypto_ccm_req_priv_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		max(crypto_ahash_reqsize(mac), crypto_skcipher_reqsize(ctr)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) err_free_mac:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	crypto_free_ahash(mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	crypto_free_ahash(ctx->mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	crypto_free_skcipher(ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static void crypto_ccm_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	crypto_drop_ahash(&ctx->mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	crypto_drop_skcipher(&ctx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int crypto_ccm_create_common(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 				    struct rtattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 				    const char *ctr_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				    const char *mac_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	struct ccm_instance_ctx *ictx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	struct skcipher_alg *ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct hash_alg_common *mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	ictx = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	err = crypto_grab_ahash(&ictx->mac, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 				mac_name, 0, mask | CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	mac = crypto_spawn_ahash_alg(&ictx->mac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (strncmp(mac->base.cra_name, "cbcmac(", 7) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	    mac->digestsize != 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	err = crypto_grab_skcipher(&ictx->ctr, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 				   ctr_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	    crypto_skcipher_alg_ivsize(ctr) != 16 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	    ctr->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	/* ctr and cbcmac must use the same underlying block cipher. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (strcmp(ctr->base.cra_name + 4, mac->base.cra_name + 7) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		     "ccm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		     "ccm_base(%s,%s)", ctr->base.cra_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		     mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	inst->alg.base.cra_priority = (mac->base.cra_priority +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 				       ctr->base.cra_priority) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	inst->alg.base.cra_alignmask = mac->base.cra_alignmask |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 				       ctr->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	inst->alg.ivsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	inst->alg.maxauthsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	inst->alg.init = crypto_ccm_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	inst->alg.exit = crypto_ccm_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	inst->alg.setkey = crypto_ccm_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	inst->alg.setauthsize = crypto_ccm_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	inst->alg.encrypt = crypto_ccm_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	inst->alg.decrypt = crypto_ccm_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	inst->free = crypto_ccm_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		crypto_ccm_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	const char *cipher_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	char ctr_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	char mac_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	cipher_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (IS_ERR(cipher_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return PTR_ERR(cipher_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		     cipher_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		     cipher_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static int crypto_ccm_base_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 				  struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	const char *ctr_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	const char *mac_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	ctr_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (IS_ERR(ctr_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		return PTR_ERR(ctr_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	mac_name = crypto_attr_alg_name(tb[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (IS_ERR(mac_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		return PTR_ERR(mac_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	return crypto_ccm_create_common(tmpl, tb, ctr_name, mac_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				 unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	struct crypto_aead *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (keylen < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	keylen -= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	memcpy(ctx->nonce, key + keylen, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 				     CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return crypto_aead_setkey(child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 				      unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	switch (authsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	case 12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	return crypto_aead_setauthsize(ctx->child, authsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	struct aead_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	struct crypto_aead *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 			   crypto_aead_alignmask(child) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	/* L' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	iv[0] = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	memcpy(iv + 1, ctx->nonce, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	memcpy(iv + 4, req->iv, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	sg_init_table(rctx->src, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (sg != rctx->src + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		sg_chain(rctx->src, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (req->src != req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		sg_init_table(rctx->dst, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		if (sg != rctx->dst + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			sg_chain(rctx->dst, 2, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	aead_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	aead_request_set_callback(subreq, req->base.flags, req->base.complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 				  req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	aead_request_set_crypt(subreq, rctx->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			       req->src == req->dst ? rctx->src : rctx->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			       req->cryptlen, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	aead_request_set_ad(subreq, req->assoclen - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	return subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static int crypto_rfc4309_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	if (req->assoclen != 16 && req->assoclen != 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	req = crypto_rfc4309_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	return crypto_aead_encrypt(req);
^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_rfc4309_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	if (req->assoclen != 16 && req->assoclen != 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	req = crypto_rfc4309_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	return crypto_aead_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	struct aead_instance *inst = aead_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	struct crypto_aead *aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	aead = crypto_spawn_aead(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	if (IS_ERR(aead))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		return PTR_ERR(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	ctx->child = aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	align = crypto_aead_alignmask(aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	crypto_aead_set_reqsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		sizeof(struct crypto_rfc4309_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		align + 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	crypto_free_aead(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static void crypto_rfc4309_free(struct aead_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	crypto_drop_aead(aead_instance_ctx(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static int crypto_rfc4309_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 				 struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	struct aead_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	struct crypto_aead_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	struct aead_alg *alg;
^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_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
^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) 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	spawn = aead_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			       crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	alg = crypto_spawn_aead_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	/* We only support 16-byte blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	if (crypto_aead_alg_ivsize(alg) != 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	/* Not a stream cipher? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	if (alg->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		     "rfc4309(%s)", alg->base.cra_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	    CRYPTO_MAX_ALG_NAME ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	    snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		     "rfc4309(%s)", alg->base.cra_driver_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	inst->alg.ivsize = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	inst->alg.maxauthsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	inst->alg.init = crypto_rfc4309_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	inst->alg.exit = crypto_rfc4309_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	inst->alg.setkey = crypto_rfc4309_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	inst->alg.setauthsize = crypto_rfc4309_setauthsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	inst->alg.encrypt = crypto_rfc4309_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	inst->alg.decrypt = crypto_rfc4309_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	inst->free = crypto_rfc4309_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	err = aead_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		crypto_rfc4309_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 				     const u8 *inkey, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	return crypto_cipher_setkey(ctx->child, inkey, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	int bs = crypto_shash_digestsize(pdesc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(pdesc->tfm) - bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	ctx->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	memset(dg, 0, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 				       unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	struct crypto_shash *parent = pdesc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	struct crypto_cipher *tfm = tctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	int bs = crypto_shash_digestsize(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 		unsigned int l = min(len, bs - ctx->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		crypto_xor(dg + ctx->len, p, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 		ctx->len +=l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		len -= l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		p += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		if (ctx->len == bs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 			crypto_cipher_encrypt_one(tfm, dg, dg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 			ctx->len = 0;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	struct crypto_shash *parent = pdesc->tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	struct crypto_cipher *tfm = tctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	int bs = crypto_shash_digestsize(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	u8 *dg = (u8 *)ctx + crypto_shash_descsize(parent) - bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	if (ctx->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		crypto_cipher_encrypt_one(tfm, dg, dg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	memcpy(out, dg, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static int cbcmac_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	struct crypto_cipher *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	struct crypto_instance *inst = (void *)tfm->__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	cipher = crypto_spawn_cipher(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	if (IS_ERR(cipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		return PTR_ERR(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	ctx->child = cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	crypto_free_cipher(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	struct shash_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	struct crypto_cipher_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	struct crypto_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	spawn = shash_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 				 crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	alg = crypto_spawn_cipher_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	inst->alg.base.cra_priority = alg->cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	inst->alg.digestsize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	inst->alg.descsize = ALIGN(sizeof(struct cbcmac_desc_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 				   alg->cra_alignmask + 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 			     alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	inst->alg.base.cra_init = cbcmac_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	inst->alg.base.cra_exit = cbcmac_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	inst->alg.init = crypto_cbcmac_digest_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	inst->alg.update = crypto_cbcmac_digest_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	inst->alg.final = crypto_cbcmac_digest_final;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	inst->alg.setkey = crypto_cbcmac_digest_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	inst->free = shash_free_singlespawn_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	err = shash_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		shash_free_singlespawn_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	return err;
^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 struct crypto_template crypto_ccm_tmpls[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 		.name = "cbcmac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 		.create = cbcmac_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 		.name = "ccm_base",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 		.create = crypto_ccm_base_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 		.name = "ccm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 		.create = crypto_ccm_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 		.name = "rfc4309",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 		.create = crypto_rfc4309_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) static int __init crypto_ccm_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	return crypto_register_templates(crypto_ccm_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 					 ARRAY_SIZE(crypto_ccm_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) static void __exit crypto_ccm_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	crypto_unregister_templates(crypto_ccm_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 				    ARRAY_SIZE(crypto_ccm_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) subsys_initcall(crypto_ccm_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) module_exit(crypto_ccm_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) MODULE_DESCRIPTION("Counter with CBC MAC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) MODULE_ALIAS_CRYPTO("ccm_base");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) MODULE_ALIAS_CRYPTO("rfc4309");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) MODULE_ALIAS_CRYPTO("ccm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) MODULE_ALIAS_CRYPTO("cbcmac");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) MODULE_IMPORT_NS(CRYPTO_INTERNAL);