pcbc.c 7.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * PCBC: Propagating Cipher Block Chaining mode
 *
 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * Derived from cbc.c
 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

17
#include <crypto/internal/skcipher.h>
18 19 20 21 22
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
23
#include <linux/compiler.h>
24 25 26 27 28

struct crypto_pcbc_ctx {
	struct crypto_cipher *child;
};

29
static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
30 31
			      unsigned int keylen)
{
32
	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
33 34 35 36
	struct crypto_cipher *child = ctx->child;
	int err;

	crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
37 38
	crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
				       CRYPTO_TFM_REQ_MASK);
39
	err = crypto_cipher_setkey(child, key, keylen);
40 41
	crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
					  CRYPTO_TFM_RES_MASK);
42 43 44
	return err;
}

45 46
static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
				       struct skcipher_walk *walk,
Herbert Xu's avatar
Herbert Xu committed
47
				       struct crypto_cipher *tfm)
48 49 50 51 52 53 54 55
{
	int bsize = crypto_cipher_blocksize(tfm);
	unsigned int nbytes = walk->nbytes;
	u8 *src = walk->src.virt.addr;
	u8 *dst = walk->dst.virt.addr;
	u8 *iv = walk->iv;

	do {
Herbert Xu's avatar
Herbert Xu committed
56
		crypto_xor(iv, src, bsize);
57
		crypto_cipher_encrypt_one(tfm, dst, iv);
58
		crypto_xor_cpy(iv, dst, src, bsize);
59 60 61 62 63 64 65 66

		src += bsize;
		dst += bsize;
	} while ((nbytes -= bsize) >= bsize);

	return nbytes;
}

67 68
static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
				       struct skcipher_walk *walk,
Herbert Xu's avatar
Herbert Xu committed
69
				       struct crypto_cipher *tfm)
70 71 72 73 74 75 76 77 78
{
	int bsize = crypto_cipher_blocksize(tfm);
	unsigned int nbytes = walk->nbytes;
	u8 *src = walk->src.virt.addr;
	u8 *iv = walk->iv;
	u8 tmpbuf[bsize];

	do {
		memcpy(tmpbuf, src, bsize);
Herbert Xu's avatar
Herbert Xu committed
79
		crypto_xor(iv, src, bsize);
80
		crypto_cipher_encrypt_one(tfm, src, iv);
81
		crypto_xor_cpy(iv, tmpbuf, src, bsize);
82 83 84 85 86 87 88 89 90

		src += bsize;
	} while ((nbytes -= bsize) >= bsize);

	memcpy(walk->iv, iv, bsize);

	return nbytes;
}

91
static int crypto_pcbc_encrypt(struct skcipher_request *req)
92
{
93 94
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
95
	struct crypto_cipher *child = ctx->child;
96 97
	struct skcipher_walk walk;
	unsigned int nbytes;
98 99
	int err;

100
	err = skcipher_walk_virt(&walk, req, false);
101 102 103

	while ((nbytes = walk.nbytes)) {
		if (walk.src.virt.addr == walk.dst.virt.addr)
104
			nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
Herbert Xu's avatar
Herbert Xu committed
105
							     child);
106
		else
107
			nbytes = crypto_pcbc_encrypt_segment(req, &walk,
Herbert Xu's avatar
Herbert Xu committed
108
							     child);
109
		err = skcipher_walk_done(&walk, nbytes);
110 111 112 113 114
	}

	return err;
}

115 116
static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
				       struct skcipher_walk *walk,
Herbert Xu's avatar
Herbert Xu committed
117
				       struct crypto_cipher *tfm)
118 119 120 121 122 123 124 125
{
	int bsize = crypto_cipher_blocksize(tfm);
	unsigned int nbytes = walk->nbytes;
	u8 *src = walk->src.virt.addr;
	u8 *dst = walk->dst.virt.addr;
	u8 *iv = walk->iv;

	do {
126
		crypto_cipher_decrypt_one(tfm, dst, src);
Herbert Xu's avatar
Herbert Xu committed
127
		crypto_xor(dst, iv, bsize);
128
		crypto_xor_cpy(iv, dst, src, bsize);
129 130 131 132 133 134 135 136 137 138

		src += bsize;
		dst += bsize;
	} while ((nbytes -= bsize) >= bsize);

	memcpy(walk->iv, iv, bsize);

	return nbytes;
}

139 140
static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
				       struct skcipher_walk *walk,
Herbert Xu's avatar
Herbert Xu committed
141
				       struct crypto_cipher *tfm)
142 143 144 145 146
{
	int bsize = crypto_cipher_blocksize(tfm);
	unsigned int nbytes = walk->nbytes;
	u8 *src = walk->src.virt.addr;
	u8 *iv = walk->iv;
147
	u8 tmpbuf[bsize] __aligned(__alignof__(u32));
148 149 150

	do {
		memcpy(tmpbuf, src, bsize);
151
		crypto_cipher_decrypt_one(tfm, src, src);
Herbert Xu's avatar
Herbert Xu committed
152
		crypto_xor(src, iv, bsize);
153
		crypto_xor_cpy(iv, src, tmpbuf, bsize);
154 155 156 157 158 159 160 161 162

		src += bsize;
	} while ((nbytes -= bsize) >= bsize);

	memcpy(walk->iv, iv, bsize);

	return nbytes;
}

163
static int crypto_pcbc_decrypt(struct skcipher_request *req)
164
{
165 166
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
167
	struct crypto_cipher *child = ctx->child;
168 169
	struct skcipher_walk walk;
	unsigned int nbytes;
170 171
	int err;

172
	err = skcipher_walk_virt(&walk, req, false);
173 174 175

	while ((nbytes = walk.nbytes)) {
		if (walk.src.virt.addr == walk.dst.virt.addr)
176
			nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
Herbert Xu's avatar
Herbert Xu committed
177
							     child);
178
		else
179
			nbytes = crypto_pcbc_decrypt_segment(req, &walk,
Herbert Xu's avatar
Herbert Xu committed
180
							     child);
181
		err = skcipher_walk_done(&walk, nbytes);
182 183 184 185 186
	}

	return err;
}

187
static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
188
{
189 190 191
	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
	struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
192
	struct crypto_cipher *cipher;
193

194 195 196
	cipher = crypto_spawn_cipher(spawn);
	if (IS_ERR(cipher))
		return PTR_ERR(cipher);
197

198
	ctx->child = cipher;
199 200 201
	return 0;
}

202
static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
203
{
204 205
	struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);

206 207 208
	crypto_free_cipher(ctx->child);
}

209 210 211 212 213 214 215
static void crypto_pcbc_free(struct skcipher_instance *inst)
{
	crypto_drop_skcipher(skcipher_instance_ctx(inst));
	kfree(inst);
}

static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
216
{
217 218 219
	struct skcipher_instance *inst;
	struct crypto_attr_type *algt;
	struct crypto_spawn *spawn;
220
	struct crypto_alg *alg;
221 222
	int err;

223 224 225 226 227 228 229
	algt = crypto_get_attr_type(tb);
	if (IS_ERR(algt))
		return PTR_ERR(algt);

	if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
	    ~CRYPTO_ALG_INTERNAL)
		return -EINVAL;
230

231 232 233 234 235 236 237 238 239
	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
	if (!inst)
		return -ENOMEM;

	alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
				      (algt->type & CRYPTO_ALG_INTERNAL),
				  CRYPTO_ALG_TYPE_MASK |
				  (algt->mask & CRYPTO_ALG_INTERNAL));
	err = PTR_ERR(alg);
240
	if (IS_ERR(alg))
241 242 243 244 245 246 247 248
		goto err_free_inst;

	spawn = skcipher_instance_ctx(inst);
	err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
				CRYPTO_ALG_TYPE_MASK);
	crypto_mod_put(alg);
	if (err)
		goto err_free_inst;
249

250 251 252
	err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
	if (err)
		goto err_drop_spawn;
253

254 255 256 257
	inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
	inst->alg.base.cra_priority = alg->cra_priority;
	inst->alg.base.cra_blocksize = alg->cra_blocksize;
	inst->alg.base.cra_alignmask = alg->cra_alignmask;
258

259 260 261
	inst->alg.ivsize = alg->cra_blocksize;
	inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
	inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
262

263
	inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
264

265 266
	inst->alg.init = crypto_pcbc_init_tfm;
	inst->alg.exit = crypto_pcbc_exit_tfm;
267

268 269 270
	inst->alg.setkey = crypto_pcbc_setkey;
	inst->alg.encrypt = crypto_pcbc_encrypt;
	inst->alg.decrypt = crypto_pcbc_decrypt;
271

272
	inst->free = crypto_pcbc_free;
273

274 275 276 277 278 279 280 281 282 283
	err = skcipher_register_instance(tmpl, inst);
	if (err)
		goto err_drop_spawn;

out:
	return err;

err_drop_spawn:
	crypto_drop_spawn(spawn);
err_free_inst:
284
	kfree(inst);
285
	goto out;
286 287 288 289
}

static struct crypto_template crypto_pcbc_tmpl = {
	.name = "pcbc",
290
	.create = crypto_pcbc_create,
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	.module = THIS_MODULE,
};

static int __init crypto_pcbc_module_init(void)
{
	return crypto_register_template(&crypto_pcbc_tmpl);
}

static void __exit crypto_pcbc_module_exit(void)
{
	crypto_unregister_template(&crypto_pcbc_tmpl);
}

module_init(crypto_pcbc_module_init);
module_exit(crypto_pcbc_module_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("PCBC block cipher algorithm");
309
MODULE_ALIAS_CRYPTO("pcbc");