xref: /openbmc/linux/crypto/pcbc.c (revision 45fe93dff2fb58b22de04c729f8447ba0f773d93)
191652be5SDavid Howells /*
291652be5SDavid Howells  * PCBC: Propagating Cipher Block Chaining mode
391652be5SDavid Howells  *
491652be5SDavid Howells  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
591652be5SDavid Howells  * Written by David Howells (dhowells@redhat.com)
691652be5SDavid Howells  *
791652be5SDavid Howells  * Derived from cbc.c
891652be5SDavid Howells  * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
991652be5SDavid Howells  *
1091652be5SDavid Howells  * This program is free software; you can redistribute it and/or modify it
1191652be5SDavid Howells  * under the terms of the GNU General Public License as published by the Free
1291652be5SDavid Howells  * Software Foundation; either version 2 of the License, or (at your option)
1391652be5SDavid Howells  * any later version.
1491652be5SDavid Howells  *
1591652be5SDavid Howells  */
1691652be5SDavid Howells 
17043a4400SHerbert Xu #include <crypto/internal/skcipher.h>
1891652be5SDavid Howells #include <linux/err.h>
1991652be5SDavid Howells #include <linux/init.h>
2091652be5SDavid Howells #include <linux/kernel.h>
2191652be5SDavid Howells #include <linux/module.h>
2291652be5SDavid Howells #include <linux/slab.h>
23d8c34b94SGideon Israel Dsouza #include <linux/compiler.h>
2491652be5SDavid Howells 
2591652be5SDavid Howells struct crypto_pcbc_ctx {
2691652be5SDavid Howells 	struct crypto_cipher *child;
2791652be5SDavid Howells };
2891652be5SDavid Howells 
29043a4400SHerbert Xu static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
3091652be5SDavid Howells 			      unsigned int keylen)
3191652be5SDavid Howells {
32043a4400SHerbert Xu 	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
3391652be5SDavid Howells 	struct crypto_cipher *child = ctx->child;
3491652be5SDavid Howells 	int err;
3591652be5SDavid Howells 
3691652be5SDavid Howells 	crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
37043a4400SHerbert Xu 	crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
3891652be5SDavid Howells 				       CRYPTO_TFM_REQ_MASK);
3991652be5SDavid Howells 	err = crypto_cipher_setkey(child, key, keylen);
40043a4400SHerbert Xu 	crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
4191652be5SDavid Howells 					  CRYPTO_TFM_RES_MASK);
4291652be5SDavid Howells 	return err;
4391652be5SDavid Howells }
4491652be5SDavid Howells 
45043a4400SHerbert Xu static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
46043a4400SHerbert Xu 				       struct skcipher_walk *walk,
47d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
4891652be5SDavid Howells {
4991652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
5091652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
5191652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
5291652be5SDavid Howells 	u8 *dst = walk->dst.virt.addr;
5391652be5SDavid Howells 	u8 *iv = walk->iv;
5491652be5SDavid Howells 
5591652be5SDavid Howells 	do {
56d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
57043a4400SHerbert Xu 		crypto_cipher_encrypt_one(tfm, dst, iv);
58*45fe93dfSArd Biesheuvel 		crypto_xor_cpy(iv, dst, src, bsize);
5991652be5SDavid Howells 
6091652be5SDavid Howells 		src += bsize;
6191652be5SDavid Howells 		dst += bsize;
6291652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
6391652be5SDavid Howells 
6491652be5SDavid Howells 	return nbytes;
6591652be5SDavid Howells }
6691652be5SDavid Howells 
67043a4400SHerbert Xu static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
68043a4400SHerbert Xu 				       struct skcipher_walk *walk,
69d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
7091652be5SDavid Howells {
7191652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
7291652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
7391652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
7491652be5SDavid Howells 	u8 *iv = walk->iv;
7591652be5SDavid Howells 	u8 tmpbuf[bsize];
7691652be5SDavid Howells 
7791652be5SDavid Howells 	do {
7891652be5SDavid Howells 		memcpy(tmpbuf, src, bsize);
79d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
80043a4400SHerbert Xu 		crypto_cipher_encrypt_one(tfm, src, iv);
81*45fe93dfSArd Biesheuvel 		crypto_xor_cpy(iv, tmpbuf, src, bsize);
8291652be5SDavid Howells 
8391652be5SDavid Howells 		src += bsize;
8491652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
8591652be5SDavid Howells 
8691652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
8791652be5SDavid Howells 
8891652be5SDavid Howells 	return nbytes;
8991652be5SDavid Howells }
9091652be5SDavid Howells 
91043a4400SHerbert Xu static int crypto_pcbc_encrypt(struct skcipher_request *req)
9291652be5SDavid Howells {
93043a4400SHerbert Xu 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
94043a4400SHerbert Xu 	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
9591652be5SDavid Howells 	struct crypto_cipher *child = ctx->child;
96043a4400SHerbert Xu 	struct skcipher_walk walk;
97043a4400SHerbert Xu 	unsigned int nbytes;
9891652be5SDavid Howells 	int err;
9991652be5SDavid Howells 
100043a4400SHerbert Xu 	err = skcipher_walk_virt(&walk, req, false);
10191652be5SDavid Howells 
10291652be5SDavid Howells 	while ((nbytes = walk.nbytes)) {
10391652be5SDavid Howells 		if (walk.src.virt.addr == walk.dst.virt.addr)
104043a4400SHerbert Xu 			nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
105d0b9007aSHerbert Xu 							     child);
10691652be5SDavid Howells 		else
107043a4400SHerbert Xu 			nbytes = crypto_pcbc_encrypt_segment(req, &walk,
108d0b9007aSHerbert Xu 							     child);
109043a4400SHerbert Xu 		err = skcipher_walk_done(&walk, nbytes);
11091652be5SDavid Howells 	}
11191652be5SDavid Howells 
11291652be5SDavid Howells 	return err;
11391652be5SDavid Howells }
11491652be5SDavid Howells 
115043a4400SHerbert Xu static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
116043a4400SHerbert Xu 				       struct skcipher_walk *walk,
117d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
11891652be5SDavid Howells {
11991652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
12091652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
12191652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
12291652be5SDavid Howells 	u8 *dst = walk->dst.virt.addr;
12391652be5SDavid Howells 	u8 *iv = walk->iv;
12491652be5SDavid Howells 
12591652be5SDavid Howells 	do {
126043a4400SHerbert Xu 		crypto_cipher_decrypt_one(tfm, dst, src);
127d0b9007aSHerbert Xu 		crypto_xor(dst, iv, bsize);
128*45fe93dfSArd Biesheuvel 		crypto_xor_cpy(iv, dst, src, bsize);
12991652be5SDavid Howells 
13091652be5SDavid Howells 		src += bsize;
13191652be5SDavid Howells 		dst += bsize;
13291652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
13391652be5SDavid Howells 
13491652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
13591652be5SDavid Howells 
13691652be5SDavid Howells 	return nbytes;
13791652be5SDavid Howells }
13891652be5SDavid Howells 
139043a4400SHerbert Xu static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
140043a4400SHerbert Xu 				       struct skcipher_walk *walk,
141d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
14291652be5SDavid Howells {
14391652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
14491652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
14591652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
14691652be5SDavid Howells 	u8 *iv = walk->iv;
147d8c34b94SGideon Israel Dsouza 	u8 tmpbuf[bsize] __aligned(__alignof__(u32));
14891652be5SDavid Howells 
14991652be5SDavid Howells 	do {
15091652be5SDavid Howells 		memcpy(tmpbuf, src, bsize);
151043a4400SHerbert Xu 		crypto_cipher_decrypt_one(tfm, src, src);
152d0b9007aSHerbert Xu 		crypto_xor(src, iv, bsize);
153*45fe93dfSArd Biesheuvel 		crypto_xor_cpy(iv, src, tmpbuf, bsize);
15491652be5SDavid Howells 
15591652be5SDavid Howells 		src += bsize;
15691652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
15791652be5SDavid Howells 
15891652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
15991652be5SDavid Howells 
16091652be5SDavid Howells 	return nbytes;
16191652be5SDavid Howells }
16291652be5SDavid Howells 
163043a4400SHerbert Xu static int crypto_pcbc_decrypt(struct skcipher_request *req)
16491652be5SDavid Howells {
165043a4400SHerbert Xu 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
166043a4400SHerbert Xu 	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
16791652be5SDavid Howells 	struct crypto_cipher *child = ctx->child;
168043a4400SHerbert Xu 	struct skcipher_walk walk;
169043a4400SHerbert Xu 	unsigned int nbytes;
17091652be5SDavid Howells 	int err;
17191652be5SDavid Howells 
172043a4400SHerbert Xu 	err = skcipher_walk_virt(&walk, req, false);
17391652be5SDavid Howells 
17491652be5SDavid Howells 	while ((nbytes = walk.nbytes)) {
17591652be5SDavid Howells 		if (walk.src.virt.addr == walk.dst.virt.addr)
176043a4400SHerbert Xu 			nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
177d0b9007aSHerbert Xu 							     child);
17891652be5SDavid Howells 		else
179043a4400SHerbert Xu 			nbytes = crypto_pcbc_decrypt_segment(req, &walk,
180d0b9007aSHerbert Xu 							     child);
181043a4400SHerbert Xu 		err = skcipher_walk_done(&walk, nbytes);
18291652be5SDavid Howells 	}
18391652be5SDavid Howells 
18491652be5SDavid Howells 	return err;
18591652be5SDavid Howells }
18691652be5SDavid Howells 
187043a4400SHerbert Xu static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
18891652be5SDavid Howells {
189043a4400SHerbert Xu 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
190043a4400SHerbert Xu 	struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
191043a4400SHerbert Xu 	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
1922e306ee0SHerbert Xu 	struct crypto_cipher *cipher;
19391652be5SDavid Howells 
1942e306ee0SHerbert Xu 	cipher = crypto_spawn_cipher(spawn);
1952e306ee0SHerbert Xu 	if (IS_ERR(cipher))
1962e306ee0SHerbert Xu 		return PTR_ERR(cipher);
19791652be5SDavid Howells 
1982e306ee0SHerbert Xu 	ctx->child = cipher;
19991652be5SDavid Howells 	return 0;
20091652be5SDavid Howells }
20191652be5SDavid Howells 
202043a4400SHerbert Xu static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
20391652be5SDavid Howells {
204043a4400SHerbert Xu 	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
205043a4400SHerbert Xu 
20691652be5SDavid Howells 	crypto_free_cipher(ctx->child);
20791652be5SDavid Howells }
20891652be5SDavid Howells 
209043a4400SHerbert Xu static void crypto_pcbc_free(struct skcipher_instance *inst)
21091652be5SDavid Howells {
211043a4400SHerbert Xu 	crypto_drop_skcipher(skcipher_instance_ctx(inst));
212043a4400SHerbert Xu 	kfree(inst);
213043a4400SHerbert Xu }
214043a4400SHerbert Xu 
215043a4400SHerbert Xu static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
216043a4400SHerbert Xu {
217043a4400SHerbert Xu 	struct skcipher_instance *inst;
218043a4400SHerbert Xu 	struct crypto_attr_type *algt;
219043a4400SHerbert Xu 	struct crypto_spawn *spawn;
22091652be5SDavid Howells 	struct crypto_alg *alg;
221ebc610e5SHerbert Xu 	int err;
22291652be5SDavid Howells 
223043a4400SHerbert Xu 	algt = crypto_get_attr_type(tb);
224043a4400SHerbert Xu 	if (IS_ERR(algt))
225043a4400SHerbert Xu 		return PTR_ERR(algt);
226ebc610e5SHerbert Xu 
227043a4400SHerbert Xu 	if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
228043a4400SHerbert Xu 	    ~CRYPTO_ALG_INTERNAL)
229043a4400SHerbert Xu 		return -EINVAL;
230043a4400SHerbert Xu 
231043a4400SHerbert Xu 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
232043a4400SHerbert Xu 	if (!inst)
233043a4400SHerbert Xu 		return -ENOMEM;
234043a4400SHerbert Xu 
235043a4400SHerbert Xu 	alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
236043a4400SHerbert Xu 				      (algt->type & CRYPTO_ALG_INTERNAL),
237043a4400SHerbert Xu 				  CRYPTO_ALG_TYPE_MASK |
238043a4400SHerbert Xu 				  (algt->mask & CRYPTO_ALG_INTERNAL));
239043a4400SHerbert Xu 	err = PTR_ERR(alg);
24091652be5SDavid Howells 	if (IS_ERR(alg))
241043a4400SHerbert Xu 		goto err_free_inst;
24291652be5SDavid Howells 
243043a4400SHerbert Xu 	spawn = skcipher_instance_ctx(inst);
244043a4400SHerbert Xu 	err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
245043a4400SHerbert Xu 				CRYPTO_ALG_TYPE_MASK);
246043a4400SHerbert Xu 	crypto_mod_put(alg);
247043a4400SHerbert Xu 	if (err)
248043a4400SHerbert Xu 		goto err_free_inst;
24991652be5SDavid Howells 
250043a4400SHerbert Xu 	err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
251043a4400SHerbert Xu 	if (err)
252043a4400SHerbert Xu 		goto err_drop_spawn;
253043a4400SHerbert Xu 
254043a4400SHerbert Xu 	inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
255043a4400SHerbert Xu 	inst->alg.base.cra_priority = alg->cra_priority;
256043a4400SHerbert Xu 	inst->alg.base.cra_blocksize = alg->cra_blocksize;
257043a4400SHerbert Xu 	inst->alg.base.cra_alignmask = alg->cra_alignmask;
25891652be5SDavid Howells 
259043a4400SHerbert Xu 	inst->alg.ivsize = alg->cra_blocksize;
260043a4400SHerbert Xu 	inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
261043a4400SHerbert Xu 	inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
26291652be5SDavid Howells 
263043a4400SHerbert Xu 	inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
26491652be5SDavid Howells 
265043a4400SHerbert Xu 	inst->alg.init = crypto_pcbc_init_tfm;
266043a4400SHerbert Xu 	inst->alg.exit = crypto_pcbc_exit_tfm;
26791652be5SDavid Howells 
268043a4400SHerbert Xu 	inst->alg.setkey = crypto_pcbc_setkey;
269043a4400SHerbert Xu 	inst->alg.encrypt = crypto_pcbc_encrypt;
270043a4400SHerbert Xu 	inst->alg.decrypt = crypto_pcbc_decrypt;
27191652be5SDavid Howells 
272043a4400SHerbert Xu 	inst->free = crypto_pcbc_free;
27391652be5SDavid Howells 
274043a4400SHerbert Xu 	err = skcipher_register_instance(tmpl, inst);
275043a4400SHerbert Xu 	if (err)
276043a4400SHerbert Xu 		goto err_drop_spawn;
277043a4400SHerbert Xu 
278043a4400SHerbert Xu out:
279043a4400SHerbert Xu 	return err;
280043a4400SHerbert Xu 
281043a4400SHerbert Xu err_drop_spawn:
282043a4400SHerbert Xu 	crypto_drop_spawn(spawn);
283043a4400SHerbert Xu err_free_inst:
28491652be5SDavid Howells 	kfree(inst);
285043a4400SHerbert Xu 	goto out;
28691652be5SDavid Howells }
28791652be5SDavid Howells 
28891652be5SDavid Howells static struct crypto_template crypto_pcbc_tmpl = {
28991652be5SDavid Howells 	.name = "pcbc",
290043a4400SHerbert Xu 	.create = crypto_pcbc_create,
29191652be5SDavid Howells 	.module = THIS_MODULE,
29291652be5SDavid Howells };
29391652be5SDavid Howells 
29491652be5SDavid Howells static int __init crypto_pcbc_module_init(void)
29591652be5SDavid Howells {
29691652be5SDavid Howells 	return crypto_register_template(&crypto_pcbc_tmpl);
29791652be5SDavid Howells }
29891652be5SDavid Howells 
29991652be5SDavid Howells static void __exit crypto_pcbc_module_exit(void)
30091652be5SDavid Howells {
30191652be5SDavid Howells 	crypto_unregister_template(&crypto_pcbc_tmpl);
30291652be5SDavid Howells }
30391652be5SDavid Howells 
30491652be5SDavid Howells module_init(crypto_pcbc_module_init);
30591652be5SDavid Howells module_exit(crypto_pcbc_module_exit);
30691652be5SDavid Howells 
30791652be5SDavid Howells MODULE_LICENSE("GPL");
30891652be5SDavid Howells MODULE_DESCRIPTION("PCBC block cipher algorithm");
3094943ba16SKees Cook MODULE_ALIAS_CRYPTO("pcbc");
310