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)  * CTR: Counter mode
^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/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <crypto/ctr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <crypto/internal/cipher.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 <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) struct crypto_rfc3686_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct crypto_rfc3686_req_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	u8 iv[CTR_RFC3686_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static void crypto_ctr_crypt_final(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 				   struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned long alignmask = crypto_cipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8 *ctrblk = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	u8 *dst = walk->dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	crypto_xor_cpy(dst, keystream, src, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	crypto_inc(ctrblk, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int crypto_ctr_crypt_segment(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 				    struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		   crypto_cipher_alg(tfm)->cia_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u8 *ctrblk = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u8 *dst = walk->dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		/* create keystream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		fn(crypto_cipher_tfm(tfm), dst, ctrblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		crypto_xor(dst, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		/* increment counter in counterblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		crypto_inc(ctrblk, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		dst += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int crypto_ctr_crypt_inplace(struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				    struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		   crypto_cipher_alg(tfm)->cia_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned long alignmask = crypto_cipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u8 *ctrblk = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		/* create keystream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		crypto_xor(src, keystream, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		/* increment counter in counterblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		crypto_inc(ctrblk, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int crypto_ctr_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	const unsigned int bsize = crypto_cipher_blocksize(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	while (walk.nbytes >= bsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (walk.src.virt.addr == walk.dst.virt.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			nbytes = crypto_ctr_crypt_inplace(&walk, cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			nbytes = crypto_ctr_crypt_segment(&walk, cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		err = skcipher_walk_done(&walk, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (walk.nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		crypto_ctr_crypt_final(&walk, cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		err = skcipher_walk_done(&walk, 0);
^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 err;
^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 crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct crypto_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	inst = skcipher_alloc_instance_simple(tmpl, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (IS_ERR(inst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return PTR_ERR(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	alg = skcipher_ialg_simple(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* Block size must be >= 4 bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (alg->cra_blocksize < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		goto out_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/* If this is false we'd fail the alignment of crypto_inc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (alg->cra_blocksize % 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		goto out_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* CTR mode is a stream cipher. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	inst->alg.base.cra_blocksize = 1;
^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) 	 * To simplify the implementation, configure the skcipher walk to only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * give a partial block at the very end, never earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	inst->alg.chunksize = alg->cra_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	inst->alg.encrypt = crypto_ctr_crypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	inst->alg.decrypt = crypto_ctr_crypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) out_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		inst->free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				 const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct crypto_skcipher *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* the nonce is stored in bytes at end of key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (keylen < CTR_RFC3686_NONCE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	       CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	keylen -= CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 					 CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return crypto_skcipher_setkey(child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int crypto_rfc3686_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct crypto_skcipher *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	unsigned long align = crypto_skcipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct crypto_rfc3686_req_ctx *rctx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		(void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	u8 *iv = rctx->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/* set up counter block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	/* initialize counter portion of counter block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	*(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		cpu_to_be32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	skcipher_request_set_tfm(subreq, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	skcipher_request_set_callback(subreq, req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				      req->base.complete, req->base.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	skcipher_request_set_crypt(subreq, req->src, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				   req->cryptlen, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return crypto_skcipher_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct crypto_skcipher *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	unsigned long align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	unsigned int reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	cipher = crypto_spawn_skcipher(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (IS_ERR(cipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return PTR_ERR(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ctx->child = cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	align = crypto_skcipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	align &= ~(crypto_tfm_ctx_alignment() - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		  crypto_skcipher_reqsize(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	crypto_skcipher_set_reqsize(tfm, reqsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	crypto_free_skcipher(ctx->child);
^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 crypto_rfc3686_free(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	crypto_drop_skcipher(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int crypto_rfc3686_create(struct crypto_template *tmpl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				 struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct crypto_skcipher_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				   crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	alg = crypto_spawn_skcipher_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/* We only support 16-byte blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	/* Not a stream cipher? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (alg->base.cra_blocksize != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		     "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		     "rfc3686(%s)", alg->base.cra_driver_name) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	    CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	inst->alg.base.cra_blocksize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				CTR_RFC3686_NONCE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	inst->alg.setkey = crypto_rfc3686_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	inst->alg.encrypt = crypto_rfc3686_crypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	inst->alg.decrypt = crypto_rfc3686_crypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	inst->alg.init = crypto_rfc3686_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	inst->alg.exit = crypto_rfc3686_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	inst->free = crypto_rfc3686_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		crypto_rfc3686_free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static struct crypto_template crypto_ctr_tmpls[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		.name = "ctr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		.create = crypto_ctr_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		.name = "rfc3686",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		.create = crypto_rfc3686_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int __init crypto_ctr_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return crypto_register_templates(crypto_ctr_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 					 ARRAY_SIZE(crypto_ctr_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static void __exit crypto_ctr_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	crypto_unregister_templates(crypto_ctr_tmpls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				    ARRAY_SIZE(crypto_ctr_tmpls));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) subsys_initcall(crypto_ctr_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) module_exit(crypto_ctr_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) MODULE_DESCRIPTION("CTR block cipher mode of operation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) MODULE_ALIAS_CRYPTO("rfc3686");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_ALIAS_CRYPTO("ctr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) MODULE_IMPORT_NS(CRYPTO_INTERNAL);