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