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)  * PCBC: Propagating Cipher Block Chaining mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Derived from cbc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 				       struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 				       struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u8 *dst = walk->dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8 * const iv = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		crypto_xor(iv, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		crypto_cipher_encrypt_one(tfm, dst, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		crypto_xor_cpy(iv, dst, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		dst += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				       struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				       struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u8 * const iv = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		memcpy(tmpbuf, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		crypto_xor(iv, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		crypto_cipher_encrypt_one(tfm, src, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		crypto_xor_cpy(iv, tmpbuf, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int crypto_pcbc_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	while ((nbytes = walk.nbytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (walk.src.virt.addr == walk.dst.virt.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 							     cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			nbytes = crypto_pcbc_encrypt_segment(req, &walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 							     cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		err = skcipher_walk_done(&walk, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return err;
^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_pcbc_decrypt_segment(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				       struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				       struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u8 *dst = walk->dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u8 * const iv = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		crypto_cipher_decrypt_one(tfm, dst, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		crypto_xor(dst, iv, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		crypto_xor_cpy(iv, dst, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		dst += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				       struct skcipher_walk *walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 				       struct crypto_cipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int bsize = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned int nbytes = walk->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u8 *src = walk->src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	u8 * const iv = walk->iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		memcpy(tmpbuf, src, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		crypto_cipher_decrypt_one(tfm, src, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		crypto_xor(src, iv, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		crypto_xor_cpy(iv, src, tmpbuf, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		src += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	} while ((nbytes -= bsize) >= bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int crypto_pcbc_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	while ((nbytes = walk.nbytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (walk.src.virt.addr == walk.dst.virt.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 							     cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			nbytes = crypto_pcbc_decrypt_segment(req, &walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 							     cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		err = skcipher_walk_done(&walk, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	inst = skcipher_alloc_instance_simple(tmpl, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (IS_ERR(inst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return PTR_ERR(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	inst->alg.encrypt = crypto_pcbc_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	inst->alg.decrypt = crypto_pcbc_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		inst->free(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static struct crypto_template crypto_pcbc_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.name = "pcbc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.create = crypto_pcbc_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int __init crypto_pcbc_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return crypto_register_template(&crypto_pcbc_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void __exit crypto_pcbc_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	crypto_unregister_template(&crypto_pcbc_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) subsys_initcall(crypto_pcbc_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) module_exit(crypto_pcbc_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_DESCRIPTION("PCBC block cipher mode of operation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) MODULE_ALIAS_CRYPTO("pcbc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) MODULE_IMPORT_NS(CRYPTO_INTERNAL);