[CRYPTO] aead: Add givcrypt operations
[linux-2.6/openmoko-kernel/knife-kernel.git] / crypto / chainiv.c
blob38868d160b472163e1f8ea14979217c82c002c2b
1 /*
2 * chainiv: Chain IV Generator
4 * Generate IVs simply be using the last block of the previous encryption.
5 * This is mainly useful for CBC with a synchronous algorithm.
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
16 #include <crypto/internal/skcipher.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/random.h>
21 #include <linux/spinlock.h>
22 #include <linux/string.h>
24 struct chainiv_ctx {
25 spinlock_t lock;
26 char iv[];
29 static int chainiv_givencrypt(struct skcipher_givcrypt_request *req)
31 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
32 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
33 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
34 unsigned int ivsize;
35 int err;
37 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
38 ablkcipher_request_set_callback(subreq, req->creq.base.flags &
39 ~CRYPTO_TFM_REQ_MAY_SLEEP,
40 req->creq.base.complete,
41 req->creq.base.data);
42 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
43 req->creq.nbytes, req->creq.info);
45 spin_lock_bh(&ctx->lock);
47 ivsize = crypto_ablkcipher_ivsize(geniv);
49 memcpy(req->giv, ctx->iv, ivsize);
50 memcpy(subreq->info, ctx->iv, ivsize);
52 err = crypto_ablkcipher_encrypt(subreq);
53 if (err)
54 goto unlock;
56 memcpy(ctx->iv, subreq->info, ivsize);
58 unlock:
59 spin_unlock_bh(&ctx->lock);
61 return err;
64 static int chainiv_givencrypt_first(struct skcipher_givcrypt_request *req)
66 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
67 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
69 spin_lock_bh(&ctx->lock);
70 if (crypto_ablkcipher_crt(geniv)->givencrypt !=
71 chainiv_givencrypt_first)
72 goto unlock;
74 crypto_ablkcipher_crt(geniv)->givencrypt = chainiv_givencrypt;
75 get_random_bytes(ctx->iv, crypto_ablkcipher_ivsize(geniv));
77 unlock:
78 spin_unlock_bh(&ctx->lock);
80 return chainiv_givencrypt(req);
83 static int chainiv_init(struct crypto_tfm *tfm)
85 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
86 struct chainiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
88 spin_lock_init(&ctx->lock);
90 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
92 return skcipher_geniv_init(tfm);
95 static struct crypto_template chainiv_tmpl;
97 static struct crypto_instance *chainiv_alloc(struct rtattr **tb)
99 struct crypto_instance *inst;
101 inst = skcipher_geniv_alloc(&chainiv_tmpl, tb, 0,
102 CRYPTO_ALG_ASYNC);
103 if (IS_ERR(inst))
104 goto out;
106 inst->alg.cra_ablkcipher.givencrypt = chainiv_givencrypt_first;
108 inst->alg.cra_init = chainiv_init;
109 inst->alg.cra_exit = skcipher_geniv_exit;
111 inst->alg.cra_ctxsize = sizeof(struct chainiv_ctx) +
112 inst->alg.cra_ablkcipher.ivsize;
114 out:
115 return inst;
118 static struct crypto_template chainiv_tmpl = {
119 .name = "chainiv",
120 .alloc = chainiv_alloc,
121 .free = skcipher_geniv_free,
122 .module = THIS_MODULE,
125 static int __init chainiv_module_init(void)
127 return crypto_register_template(&chainiv_tmpl);
130 static void __exit chainiv_module_exit(void)
132 crypto_unregister_template(&chainiv_tmpl);
135 module_init(chainiv_module_init);
136 module_exit(chainiv_module_exit);
138 MODULE_LICENSE("GPL");
139 MODULE_DESCRIPTION("Chain IV Generator");