xref: /linux/crypto/lrw.c (revision 9ebed9d182e03d12d39915b72e4b960046bc4039)
164470f1bSRik Snel /* LRW: as defined by Cyril Guyot in
264470f1bSRik Snel  *	http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
364470f1bSRik Snel  *
464470f1bSRik Snel  * Copyright (c) 2006 Rik Snel <rsnel@cube.dyndns.org>
564470f1bSRik Snel  *
664470f1bSRik Snel  * Based om ecb.c
764470f1bSRik Snel  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
864470f1bSRik Snel  *
964470f1bSRik Snel  * This program is free software; you can redistribute it and/or modify it
1064470f1bSRik Snel  * under the terms of the GNU General Public License as published by the Free
1164470f1bSRik Snel  * Software Foundation; either version 2 of the License, or (at your option)
1264470f1bSRik Snel  * any later version.
1364470f1bSRik Snel  */
1464470f1bSRik Snel /* This implementation is checked against the test vectors in the above
1564470f1bSRik Snel  * document and by a test vector provided by Ken Buchanan at
1664470f1bSRik Snel  * http://www.mail-archive.com/stds-p1619@listserv.ieee.org/msg00173.html
1764470f1bSRik Snel  *
1864470f1bSRik Snel  * The test vectors are included in the testing module tcrypt.[ch] */
1964470f1bSRik Snel #include <crypto/algapi.h>
2064470f1bSRik Snel #include <linux/err.h>
2164470f1bSRik Snel #include <linux/init.h>
2264470f1bSRik Snel #include <linux/kernel.h>
2364470f1bSRik Snel #include <linux/module.h>
2464470f1bSRik Snel #include <linux/scatterlist.h>
2564470f1bSRik Snel #include <linux/slab.h>
2664470f1bSRik Snel 
2764470f1bSRik Snel #include <crypto/b128ops.h>
2864470f1bSRik Snel #include <crypto/gf128mul.h>
2964470f1bSRik Snel 
3064470f1bSRik Snel struct priv {
3164470f1bSRik Snel 	struct crypto_cipher *child;
3264470f1bSRik Snel 	/* optimizes multiplying a random (non incrementing, as at the
3364470f1bSRik Snel 	 * start of a new sector) value with key2, we could also have
3464470f1bSRik Snel 	 * used 4k optimization tables or no optimization at all. In the
3564470f1bSRik Snel 	 * latter case we would have to store key2 here */
3664470f1bSRik Snel 	struct gf128mul_64k *table;
3764470f1bSRik Snel 	/* stores:
3864470f1bSRik Snel 	 *  key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
3964470f1bSRik Snel 	 *  key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
4064470f1bSRik Snel 	 *  key2*{ 0,0,...1,1,1,1,1 }, etc
4164470f1bSRik Snel 	 * needed for optimized multiplication of incrementing values
4264470f1bSRik Snel 	 * with key2 */
4364470f1bSRik Snel 	be128 mulinc[128];
4464470f1bSRik Snel };
4564470f1bSRik Snel 
4664470f1bSRik Snel static inline void setbit128_bbe(void *b, int bit)
4764470f1bSRik Snel {
4864470f1bSRik Snel 	__set_bit(bit ^ 0x78, b);
4964470f1bSRik Snel }
5064470f1bSRik Snel 
5164470f1bSRik Snel static int setkey(struct crypto_tfm *parent, const u8 *key,
5264470f1bSRik Snel 		  unsigned int keylen)
5364470f1bSRik Snel {
5464470f1bSRik Snel 	struct priv *ctx = crypto_tfm_ctx(parent);
5564470f1bSRik Snel 	struct crypto_cipher *child = ctx->child;
5664470f1bSRik Snel 	int err, i;
5764470f1bSRik Snel 	be128 tmp = { 0 };
5864470f1bSRik Snel 	int bsize = crypto_cipher_blocksize(child);
5964470f1bSRik Snel 
6064470f1bSRik Snel 	crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
6164470f1bSRik Snel 	crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
6264470f1bSRik Snel 				       CRYPTO_TFM_REQ_MASK);
6364470f1bSRik Snel 	if ((err = crypto_cipher_setkey(child, key, keylen - bsize)))
6464470f1bSRik Snel 		return err;
6564470f1bSRik Snel 	crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
6664470f1bSRik Snel 				     CRYPTO_TFM_RES_MASK);
6764470f1bSRik Snel 
6864470f1bSRik Snel 	if (ctx->table)
6964470f1bSRik Snel 		gf128mul_free_64k(ctx->table);
7064470f1bSRik Snel 
7164470f1bSRik Snel 	/* initialize multiplication table for Key2 */
7264470f1bSRik Snel 	ctx->table = gf128mul_init_64k_bbe((be128 *)(key + keylen - bsize));
7364470f1bSRik Snel 	if (!ctx->table)
7464470f1bSRik Snel 		return -ENOMEM;
7564470f1bSRik Snel 
7664470f1bSRik Snel 	/* initialize optimization table */
7764470f1bSRik Snel 	for (i = 0; i < 128; i++) {
7864470f1bSRik Snel 		setbit128_bbe(&tmp, i);
7964470f1bSRik Snel 		ctx->mulinc[i] = tmp;
8064470f1bSRik Snel 		gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
8164470f1bSRik Snel 	}
8264470f1bSRik Snel 
8364470f1bSRik Snel 	return 0;
8464470f1bSRik Snel }
8564470f1bSRik Snel 
8664470f1bSRik Snel struct sinfo {
8764470f1bSRik Snel 	be128 t;
8864470f1bSRik Snel 	struct crypto_tfm *tfm;
8964470f1bSRik Snel 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
9064470f1bSRik Snel };
9164470f1bSRik Snel 
9264470f1bSRik Snel static inline void inc(be128 *iv)
9364470f1bSRik Snel {
9464470f1bSRik Snel 	if (!(iv->b = cpu_to_be64(be64_to_cpu(iv->b) + 1)))
9564470f1bSRik Snel 		iv->a = cpu_to_be64(be64_to_cpu(iv->a) + 1);
9664470f1bSRik Snel }
9764470f1bSRik Snel 
98*9ebed9d1SDavid S. Miller static inline void lrw_round(struct sinfo *s, void *dst, const void *src)
9964470f1bSRik Snel {
10064470f1bSRik Snel 	be128_xor(dst, &s->t, src);		/* PP <- T xor P */
10164470f1bSRik Snel 	s->fn(s->tfm, dst, dst);		/* CC <- E(Key2,PP) */
10264470f1bSRik Snel 	be128_xor(dst, dst, &s->t);		/* C <- T xor CC */
10364470f1bSRik Snel }
10464470f1bSRik Snel 
10564470f1bSRik Snel /* this returns the number of consequative 1 bits starting
10664470f1bSRik Snel  * from the right, get_index128(00 00 00 00 00 00 ... 00 00 10 FB) = 2 */
10764470f1bSRik Snel static inline int get_index128(be128 *block)
10864470f1bSRik Snel {
10964470f1bSRik Snel 	int x;
11064470f1bSRik Snel 	__be32 *p = (__be32 *) block;
11164470f1bSRik Snel 
11264470f1bSRik Snel 	for (p += 3, x = 0; x < 128; p--, x += 32) {
11364470f1bSRik Snel 		u32 val = be32_to_cpup(p);
11464470f1bSRik Snel 
11564470f1bSRik Snel 		if (!~val)
11664470f1bSRik Snel 			continue;
11764470f1bSRik Snel 
11864470f1bSRik Snel 		return x + ffz(val);
11964470f1bSRik Snel 	}
12064470f1bSRik Snel 
12164470f1bSRik Snel 	return x;
12264470f1bSRik Snel }
12364470f1bSRik Snel 
12464470f1bSRik Snel static int crypt(struct blkcipher_desc *d,
12564470f1bSRik Snel 		 struct blkcipher_walk *w, struct priv *ctx,
12664470f1bSRik Snel 		 void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
12764470f1bSRik Snel {
12864470f1bSRik Snel 	int err;
12964470f1bSRik Snel 	unsigned int avail;
13064470f1bSRik Snel 	const int bs = crypto_cipher_blocksize(ctx->child);
13164470f1bSRik Snel 	struct sinfo s = {
13264470f1bSRik Snel 		.tfm = crypto_cipher_tfm(ctx->child),
13364470f1bSRik Snel 		.fn = fn
13464470f1bSRik Snel 	};
13564470f1bSRik Snel 	be128 *iv;
13664470f1bSRik Snel 	u8 *wsrc;
13764470f1bSRik Snel 	u8 *wdst;
13864470f1bSRik Snel 
13964470f1bSRik Snel 	err = blkcipher_walk_virt(d, w);
14064470f1bSRik Snel 	if (!(avail = w->nbytes))
14164470f1bSRik Snel 		return err;
14264470f1bSRik Snel 
14364470f1bSRik Snel 	wsrc = w->src.virt.addr;
14464470f1bSRik Snel 	wdst = w->dst.virt.addr;
14564470f1bSRik Snel 
14664470f1bSRik Snel 	/* calculate first value of T */
14764470f1bSRik Snel 	iv = (be128 *)w->iv;
14864470f1bSRik Snel 	s.t = *iv;
14964470f1bSRik Snel 
15064470f1bSRik Snel 	/* T <- I*Key2 */
15164470f1bSRik Snel 	gf128mul_64k_bbe(&s.t, ctx->table);
15264470f1bSRik Snel 
15364470f1bSRik Snel 	goto first;
15464470f1bSRik Snel 
15564470f1bSRik Snel 	for (;;) {
15664470f1bSRik Snel 		do {
15764470f1bSRik Snel 			/* T <- I*Key2, using the optimization
15864470f1bSRik Snel 			 * discussed in the specification */
15964470f1bSRik Snel 			be128_xor(&s.t, &s.t, &ctx->mulinc[get_index128(iv)]);
16064470f1bSRik Snel 			inc(iv);
16164470f1bSRik Snel 
16264470f1bSRik Snel first:
163*9ebed9d1SDavid S. Miller 			lrw_round(&s, wdst, wsrc);
16464470f1bSRik Snel 
16564470f1bSRik Snel 			wsrc += bs;
16664470f1bSRik Snel 			wdst += bs;
16764470f1bSRik Snel 		} while ((avail -= bs) >= bs);
16864470f1bSRik Snel 
16964470f1bSRik Snel 		err = blkcipher_walk_done(d, w, avail);
17064470f1bSRik Snel 		if (!(avail = w->nbytes))
17164470f1bSRik Snel 			break;
17264470f1bSRik Snel 
17364470f1bSRik Snel 		wsrc = w->src.virt.addr;
17464470f1bSRik Snel 		wdst = w->dst.virt.addr;
17564470f1bSRik Snel 	}
17664470f1bSRik Snel 
17764470f1bSRik Snel 	return err;
17864470f1bSRik Snel }
17964470f1bSRik Snel 
18064470f1bSRik Snel static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
18164470f1bSRik Snel 		   struct scatterlist *src, unsigned int nbytes)
18264470f1bSRik Snel {
18364470f1bSRik Snel 	struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
18464470f1bSRik Snel 	struct blkcipher_walk w;
18564470f1bSRik Snel 
18664470f1bSRik Snel 	blkcipher_walk_init(&w, dst, src, nbytes);
18764470f1bSRik Snel 	return crypt(desc, &w, ctx,
18864470f1bSRik Snel 		     crypto_cipher_alg(ctx->child)->cia_encrypt);
18964470f1bSRik Snel }
19064470f1bSRik Snel 
19164470f1bSRik Snel static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
19264470f1bSRik Snel 		   struct scatterlist *src, unsigned int nbytes)
19364470f1bSRik Snel {
19464470f1bSRik Snel 	struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
19564470f1bSRik Snel 	struct blkcipher_walk w;
19664470f1bSRik Snel 
19764470f1bSRik Snel 	blkcipher_walk_init(&w, dst, src, nbytes);
19864470f1bSRik Snel 	return crypt(desc, &w, ctx,
19964470f1bSRik Snel 		     crypto_cipher_alg(ctx->child)->cia_decrypt);
20064470f1bSRik Snel }
20164470f1bSRik Snel 
20264470f1bSRik Snel static int init_tfm(struct crypto_tfm *tfm)
20364470f1bSRik Snel {
20464470f1bSRik Snel 	struct crypto_instance *inst = (void *)tfm->__crt_alg;
20564470f1bSRik Snel 	struct crypto_spawn *spawn = crypto_instance_ctx(inst);
20664470f1bSRik Snel 	struct priv *ctx = crypto_tfm_ctx(tfm);
20764470f1bSRik Snel 	u32 *flags = &tfm->crt_flags;
20864470f1bSRik Snel 
20964470f1bSRik Snel 	tfm = crypto_spawn_tfm(spawn);
21064470f1bSRik Snel 	if (IS_ERR(tfm))
21164470f1bSRik Snel 		return PTR_ERR(tfm);
21264470f1bSRik Snel 
21364470f1bSRik Snel 	if (crypto_tfm_alg_blocksize(tfm) != 16) {
21464470f1bSRik Snel 		*flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
21564470f1bSRik Snel 		return -EINVAL;
21664470f1bSRik Snel 	}
21764470f1bSRik Snel 
21864470f1bSRik Snel 	ctx->child = crypto_cipher_cast(tfm);
21964470f1bSRik Snel 	return 0;
22064470f1bSRik Snel }
22164470f1bSRik Snel 
22264470f1bSRik Snel static void exit_tfm(struct crypto_tfm *tfm)
22364470f1bSRik Snel {
22464470f1bSRik Snel 	struct priv *ctx = crypto_tfm_ctx(tfm);
22564470f1bSRik Snel 	if (ctx->table)
22664470f1bSRik Snel 		gf128mul_free_64k(ctx->table);
22764470f1bSRik Snel 	crypto_free_cipher(ctx->child);
22864470f1bSRik Snel }
22964470f1bSRik Snel 
23064470f1bSRik Snel static struct crypto_instance *alloc(void *param, unsigned int len)
23164470f1bSRik Snel {
23264470f1bSRik Snel 	struct crypto_instance *inst;
23364470f1bSRik Snel 	struct crypto_alg *alg;
23464470f1bSRik Snel 
23564470f1bSRik Snel 	alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
23664470f1bSRik Snel 				  CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
23764470f1bSRik Snel 	if (IS_ERR(alg))
23864470f1bSRik Snel 		return ERR_PTR(PTR_ERR(alg));
23964470f1bSRik Snel 
24064470f1bSRik Snel 	inst = crypto_alloc_instance("lrw", alg);
24164470f1bSRik Snel 	if (IS_ERR(inst))
24264470f1bSRik Snel 		goto out_put_alg;
24364470f1bSRik Snel 
24464470f1bSRik Snel 	inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
24564470f1bSRik Snel 	inst->alg.cra_priority = alg->cra_priority;
24664470f1bSRik Snel 	inst->alg.cra_blocksize = alg->cra_blocksize;
24764470f1bSRik Snel 
24864470f1bSRik Snel 	if (alg->cra_alignmask < 7) inst->alg.cra_alignmask = 7;
24964470f1bSRik Snel 	else inst->alg.cra_alignmask = alg->cra_alignmask;
25064470f1bSRik Snel 	inst->alg.cra_type = &crypto_blkcipher_type;
25164470f1bSRik Snel 
25264470f1bSRik Snel 	if (!(alg->cra_blocksize % 4))
25364470f1bSRik Snel 		inst->alg.cra_alignmask |= 3;
25464470f1bSRik Snel 	inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
25564470f1bSRik Snel 	inst->alg.cra_blkcipher.min_keysize =
25664470f1bSRik Snel 		alg->cra_cipher.cia_min_keysize + alg->cra_blocksize;
25764470f1bSRik Snel 	inst->alg.cra_blkcipher.max_keysize =
25864470f1bSRik Snel 		alg->cra_cipher.cia_max_keysize + alg->cra_blocksize;
25964470f1bSRik Snel 
26064470f1bSRik Snel 	inst->alg.cra_ctxsize = sizeof(struct priv);
26164470f1bSRik Snel 
26264470f1bSRik Snel 	inst->alg.cra_init = init_tfm;
26364470f1bSRik Snel 	inst->alg.cra_exit = exit_tfm;
26464470f1bSRik Snel 
26564470f1bSRik Snel 	inst->alg.cra_blkcipher.setkey = setkey;
26664470f1bSRik Snel 	inst->alg.cra_blkcipher.encrypt = encrypt;
26764470f1bSRik Snel 	inst->alg.cra_blkcipher.decrypt = decrypt;
26864470f1bSRik Snel 
26964470f1bSRik Snel out_put_alg:
27064470f1bSRik Snel 	crypto_mod_put(alg);
27164470f1bSRik Snel 	return inst;
27264470f1bSRik Snel }
27364470f1bSRik Snel 
27464470f1bSRik Snel static void free(struct crypto_instance *inst)
27564470f1bSRik Snel {
27664470f1bSRik Snel 	crypto_drop_spawn(crypto_instance_ctx(inst));
27764470f1bSRik Snel 	kfree(inst);
27864470f1bSRik Snel }
27964470f1bSRik Snel 
28064470f1bSRik Snel static struct crypto_template crypto_tmpl = {
28164470f1bSRik Snel 	.name = "lrw",
28264470f1bSRik Snel 	.alloc = alloc,
28364470f1bSRik Snel 	.free = free,
28464470f1bSRik Snel 	.module = THIS_MODULE,
28564470f1bSRik Snel };
28664470f1bSRik Snel 
28764470f1bSRik Snel static int __init crypto_module_init(void)
28864470f1bSRik Snel {
28964470f1bSRik Snel 	return crypto_register_template(&crypto_tmpl);
29064470f1bSRik Snel }
29164470f1bSRik Snel 
29264470f1bSRik Snel static void __exit crypto_module_exit(void)
29364470f1bSRik Snel {
29464470f1bSRik Snel 	crypto_unregister_template(&crypto_tmpl);
29564470f1bSRik Snel }
29664470f1bSRik Snel 
29764470f1bSRik Snel module_init(crypto_module_init);
29864470f1bSRik Snel module_exit(crypto_module_exit);
29964470f1bSRik Snel 
30064470f1bSRik Snel MODULE_LICENSE("GPL");
30164470f1bSRik Snel MODULE_DESCRIPTION("LRW block cipher mode");
302