xref: /openbmc/linux/crypto/pcbc.c (revision 4943ba16bbc2db05115707b3ff7b4874e9e3c560)
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 
1791652be5SDavid Howells #include <crypto/algapi.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/scatterlist.h>
2391652be5SDavid Howells #include <linux/slab.h>
2491652be5SDavid Howells 
2591652be5SDavid Howells struct crypto_pcbc_ctx {
2691652be5SDavid Howells 	struct crypto_cipher *child;
2791652be5SDavid Howells };
2891652be5SDavid Howells 
2991652be5SDavid Howells static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key,
3091652be5SDavid Howells 			      unsigned int keylen)
3191652be5SDavid Howells {
3291652be5SDavid Howells 	struct crypto_pcbc_ctx *ctx = crypto_tfm_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);
3791652be5SDavid Howells 	crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
3891652be5SDavid Howells 				CRYPTO_TFM_REQ_MASK);
3991652be5SDavid Howells 	err = crypto_cipher_setkey(child, key, keylen);
4091652be5SDavid Howells 	crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
4191652be5SDavid Howells 			     CRYPTO_TFM_RES_MASK);
4291652be5SDavid Howells 	return err;
4391652be5SDavid Howells }
4491652be5SDavid Howells 
4591652be5SDavid Howells static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
4691652be5SDavid Howells 				       struct blkcipher_walk *walk,
47d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
4891652be5SDavid Howells {
4991652be5SDavid Howells 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
5091652be5SDavid Howells 		crypto_cipher_alg(tfm)->cia_encrypt;
5191652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
5291652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
5391652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
5491652be5SDavid Howells 	u8 *dst = walk->dst.virt.addr;
5591652be5SDavid Howells 	u8 *iv = walk->iv;
5691652be5SDavid Howells 
5791652be5SDavid Howells 	do {
58d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
5991652be5SDavid Howells 		fn(crypto_cipher_tfm(tfm), dst, iv);
6091652be5SDavid Howells 		memcpy(iv, dst, bsize);
61d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
6291652be5SDavid Howells 
6391652be5SDavid Howells 		src += bsize;
6491652be5SDavid Howells 		dst += bsize;
6591652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
6691652be5SDavid Howells 
6791652be5SDavid Howells 	return nbytes;
6891652be5SDavid Howells }
6991652be5SDavid Howells 
7091652be5SDavid Howells static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
7191652be5SDavid Howells 				       struct blkcipher_walk *walk,
72d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
7391652be5SDavid Howells {
7491652be5SDavid Howells 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
7591652be5SDavid Howells 		crypto_cipher_alg(tfm)->cia_encrypt;
7691652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
7791652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
7891652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
7991652be5SDavid Howells 	u8 *iv = walk->iv;
8091652be5SDavid Howells 	u8 tmpbuf[bsize];
8191652be5SDavid Howells 
8291652be5SDavid Howells 	do {
8391652be5SDavid Howells 		memcpy(tmpbuf, src, bsize);
84d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
8591652be5SDavid Howells 		fn(crypto_cipher_tfm(tfm), src, iv);
86d0b9007aSHerbert Xu 		memcpy(iv, tmpbuf, bsize);
87d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
8891652be5SDavid Howells 
8991652be5SDavid Howells 		src += bsize;
9091652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
9191652be5SDavid Howells 
9291652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
9391652be5SDavid Howells 
9491652be5SDavid Howells 	return nbytes;
9591652be5SDavid Howells }
9691652be5SDavid Howells 
9791652be5SDavid Howells static int crypto_pcbc_encrypt(struct blkcipher_desc *desc,
9891652be5SDavid Howells 			       struct scatterlist *dst, struct scatterlist *src,
9991652be5SDavid Howells 			       unsigned int nbytes)
10091652be5SDavid Howells {
10191652be5SDavid Howells 	struct blkcipher_walk walk;
10291652be5SDavid Howells 	struct crypto_blkcipher *tfm = desc->tfm;
10391652be5SDavid Howells 	struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
10491652be5SDavid Howells 	struct crypto_cipher *child = ctx->child;
10591652be5SDavid Howells 	int err;
10691652be5SDavid Howells 
10791652be5SDavid Howells 	blkcipher_walk_init(&walk, dst, src, nbytes);
10891652be5SDavid Howells 	err = blkcipher_walk_virt(desc, &walk);
10991652be5SDavid Howells 
11091652be5SDavid Howells 	while ((nbytes = walk.nbytes)) {
11191652be5SDavid Howells 		if (walk.src.virt.addr == walk.dst.virt.addr)
112d0b9007aSHerbert Xu 			nbytes = crypto_pcbc_encrypt_inplace(desc, &walk,
113d0b9007aSHerbert Xu 							     child);
11491652be5SDavid Howells 		else
115d0b9007aSHerbert Xu 			nbytes = crypto_pcbc_encrypt_segment(desc, &walk,
116d0b9007aSHerbert Xu 							     child);
11791652be5SDavid Howells 		err = blkcipher_walk_done(desc, &walk, nbytes);
11891652be5SDavid Howells 	}
11991652be5SDavid Howells 
12091652be5SDavid Howells 	return err;
12191652be5SDavid Howells }
12291652be5SDavid Howells 
12391652be5SDavid Howells static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
12491652be5SDavid Howells 				       struct blkcipher_walk *walk,
125d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
12691652be5SDavid Howells {
12791652be5SDavid Howells 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
12891652be5SDavid Howells 		crypto_cipher_alg(tfm)->cia_decrypt;
12991652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
13091652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
13191652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
13291652be5SDavid Howells 	u8 *dst = walk->dst.virt.addr;
13391652be5SDavid Howells 	u8 *iv = walk->iv;
13491652be5SDavid Howells 
13591652be5SDavid Howells 	do {
13691652be5SDavid Howells 		fn(crypto_cipher_tfm(tfm), dst, src);
137d0b9007aSHerbert Xu 		crypto_xor(dst, iv, bsize);
13891652be5SDavid Howells 		memcpy(iv, src, bsize);
139d0b9007aSHerbert Xu 		crypto_xor(iv, dst, bsize);
14091652be5SDavid Howells 
14191652be5SDavid Howells 		src += bsize;
14291652be5SDavid Howells 		dst += bsize;
14391652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
14491652be5SDavid Howells 
14591652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
14691652be5SDavid Howells 
14791652be5SDavid Howells 	return nbytes;
14891652be5SDavid Howells }
14991652be5SDavid Howells 
15091652be5SDavid Howells static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
15191652be5SDavid Howells 				       struct blkcipher_walk *walk,
152d0b9007aSHerbert Xu 				       struct crypto_cipher *tfm)
15391652be5SDavid Howells {
15491652be5SDavid Howells 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
15591652be5SDavid Howells 		crypto_cipher_alg(tfm)->cia_decrypt;
15691652be5SDavid Howells 	int bsize = crypto_cipher_blocksize(tfm);
15791652be5SDavid Howells 	unsigned int nbytes = walk->nbytes;
15891652be5SDavid Howells 	u8 *src = walk->src.virt.addr;
15991652be5SDavid Howells 	u8 *iv = walk->iv;
16091652be5SDavid Howells 	u8 tmpbuf[bsize];
16191652be5SDavid Howells 
16291652be5SDavid Howells 	do {
16391652be5SDavid Howells 		memcpy(tmpbuf, src, bsize);
16491652be5SDavid Howells 		fn(crypto_cipher_tfm(tfm), src, src);
165d0b9007aSHerbert Xu 		crypto_xor(src, iv, bsize);
16691652be5SDavid Howells 		memcpy(iv, tmpbuf, bsize);
167d0b9007aSHerbert Xu 		crypto_xor(iv, src, bsize);
16891652be5SDavid Howells 
16991652be5SDavid Howells 		src += bsize;
17091652be5SDavid Howells 	} while ((nbytes -= bsize) >= bsize);
17191652be5SDavid Howells 
17291652be5SDavid Howells 	memcpy(walk->iv, iv, bsize);
17391652be5SDavid Howells 
17491652be5SDavid Howells 	return nbytes;
17591652be5SDavid Howells }
17691652be5SDavid Howells 
17791652be5SDavid Howells static int crypto_pcbc_decrypt(struct blkcipher_desc *desc,
17891652be5SDavid Howells 			       struct scatterlist *dst, struct scatterlist *src,
17991652be5SDavid Howells 			       unsigned int nbytes)
18091652be5SDavid Howells {
18191652be5SDavid Howells 	struct blkcipher_walk walk;
18291652be5SDavid Howells 	struct crypto_blkcipher *tfm = desc->tfm;
18391652be5SDavid Howells 	struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
18491652be5SDavid Howells 	struct crypto_cipher *child = ctx->child;
18591652be5SDavid Howells 	int err;
18691652be5SDavid Howells 
18791652be5SDavid Howells 	blkcipher_walk_init(&walk, dst, src, nbytes);
18891652be5SDavid Howells 	err = blkcipher_walk_virt(desc, &walk);
18991652be5SDavid Howells 
19091652be5SDavid Howells 	while ((nbytes = walk.nbytes)) {
19191652be5SDavid Howells 		if (walk.src.virt.addr == walk.dst.virt.addr)
192d0b9007aSHerbert Xu 			nbytes = crypto_pcbc_decrypt_inplace(desc, &walk,
193d0b9007aSHerbert Xu 							     child);
19491652be5SDavid Howells 		else
195d0b9007aSHerbert Xu 			nbytes = crypto_pcbc_decrypt_segment(desc, &walk,
196d0b9007aSHerbert Xu 							     child);
19791652be5SDavid Howells 		err = blkcipher_walk_done(desc, &walk, nbytes);
19891652be5SDavid Howells 	}
19991652be5SDavid Howells 
20091652be5SDavid Howells 	return err;
20191652be5SDavid Howells }
20291652be5SDavid Howells 
20391652be5SDavid Howells static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
20491652be5SDavid Howells {
20591652be5SDavid Howells 	struct crypto_instance *inst = (void *)tfm->__crt_alg;
20691652be5SDavid Howells 	struct crypto_spawn *spawn = crypto_instance_ctx(inst);
20791652be5SDavid Howells 	struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
2082e306ee0SHerbert Xu 	struct crypto_cipher *cipher;
20991652be5SDavid Howells 
2102e306ee0SHerbert Xu 	cipher = crypto_spawn_cipher(spawn);
2112e306ee0SHerbert Xu 	if (IS_ERR(cipher))
2122e306ee0SHerbert Xu 		return PTR_ERR(cipher);
21391652be5SDavid Howells 
2142e306ee0SHerbert Xu 	ctx->child = cipher;
21591652be5SDavid Howells 	return 0;
21691652be5SDavid Howells }
21791652be5SDavid Howells 
21891652be5SDavid Howells static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
21991652be5SDavid Howells {
22091652be5SDavid Howells 	struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
22191652be5SDavid Howells 	crypto_free_cipher(ctx->child);
22291652be5SDavid Howells }
22391652be5SDavid Howells 
224ebc610e5SHerbert Xu static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb)
22591652be5SDavid Howells {
22691652be5SDavid Howells 	struct crypto_instance *inst;
22791652be5SDavid Howells 	struct crypto_alg *alg;
228ebc610e5SHerbert Xu 	int err;
22991652be5SDavid Howells 
230ebc610e5SHerbert Xu 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
231ebc610e5SHerbert Xu 	if (err)
232ebc610e5SHerbert Xu 		return ERR_PTR(err);
233ebc610e5SHerbert Xu 
234ebc610e5SHerbert Xu 	alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
235ebc610e5SHerbert Xu 				  CRYPTO_ALG_TYPE_MASK);
23691652be5SDavid Howells 	if (IS_ERR(alg))
237e231c2eeSDavid Howells 		return ERR_CAST(alg);
23891652be5SDavid Howells 
23991652be5SDavid Howells 	inst = crypto_alloc_instance("pcbc", alg);
24091652be5SDavid Howells 	if (IS_ERR(inst))
24191652be5SDavid Howells 		goto out_put_alg;
24291652be5SDavid Howells 
24391652be5SDavid Howells 	inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
24491652be5SDavid Howells 	inst->alg.cra_priority = alg->cra_priority;
24591652be5SDavid Howells 	inst->alg.cra_blocksize = alg->cra_blocksize;
24691652be5SDavid Howells 	inst->alg.cra_alignmask = alg->cra_alignmask;
24791652be5SDavid Howells 	inst->alg.cra_type = &crypto_blkcipher_type;
24891652be5SDavid Howells 
249d0b9007aSHerbert Xu 	/* We access the data as u32s when xoring. */
250d0b9007aSHerbert Xu 	inst->alg.cra_alignmask |= __alignof__(u32) - 1;
251d0b9007aSHerbert Xu 
25291652be5SDavid Howells 	inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
25391652be5SDavid Howells 	inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
25491652be5SDavid Howells 	inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
25591652be5SDavid Howells 
25691652be5SDavid Howells 	inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
25791652be5SDavid Howells 
25891652be5SDavid Howells 	inst->alg.cra_init = crypto_pcbc_init_tfm;
25991652be5SDavid Howells 	inst->alg.cra_exit = crypto_pcbc_exit_tfm;
26091652be5SDavid Howells 
26191652be5SDavid Howells 	inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey;
26291652be5SDavid Howells 	inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt;
26391652be5SDavid Howells 	inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt;
26491652be5SDavid Howells 
26591652be5SDavid Howells out_put_alg:
26691652be5SDavid Howells 	crypto_mod_put(alg);
26791652be5SDavid Howells 	return inst;
26891652be5SDavid Howells }
26991652be5SDavid Howells 
27091652be5SDavid Howells static void crypto_pcbc_free(struct crypto_instance *inst)
27191652be5SDavid Howells {
27291652be5SDavid Howells 	crypto_drop_spawn(crypto_instance_ctx(inst));
27391652be5SDavid Howells 	kfree(inst);
27491652be5SDavid Howells }
27591652be5SDavid Howells 
27691652be5SDavid Howells static struct crypto_template crypto_pcbc_tmpl = {
27791652be5SDavid Howells 	.name = "pcbc",
27891652be5SDavid Howells 	.alloc = crypto_pcbc_alloc,
27991652be5SDavid Howells 	.free = crypto_pcbc_free,
28091652be5SDavid Howells 	.module = THIS_MODULE,
28191652be5SDavid Howells };
28291652be5SDavid Howells 
28391652be5SDavid Howells static int __init crypto_pcbc_module_init(void)
28491652be5SDavid Howells {
28591652be5SDavid Howells 	return crypto_register_template(&crypto_pcbc_tmpl);
28691652be5SDavid Howells }
28791652be5SDavid Howells 
28891652be5SDavid Howells static void __exit crypto_pcbc_module_exit(void)
28991652be5SDavid Howells {
29091652be5SDavid Howells 	crypto_unregister_template(&crypto_pcbc_tmpl);
29191652be5SDavid Howells }
29291652be5SDavid Howells 
29391652be5SDavid Howells module_init(crypto_pcbc_module_init);
29491652be5SDavid Howells module_exit(crypto_pcbc_module_exit);
29591652be5SDavid Howells 
29691652be5SDavid Howells MODULE_LICENSE("GPL");
29791652be5SDavid Howells MODULE_DESCRIPTION("PCBC block cipher algorithm");
298*4943ba16SKees Cook MODULE_ALIAS_CRYPTO("pcbc");
299