UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / crypto / eseqiv.c
index 6f2cd06..42ce9f5 100644 (file)
  */
 
 #include <crypto/internal/skcipher.h>
+#include <crypto/rng.h>
 #include <crypto/scatterwalk.h>
 #include <linux/err.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/module.h>
-#include <linux/random.h>
 #include <linux/scatterlist.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
@@ -62,20 +62,6 @@ out:
        skcipher_givcrypt_complete(req, err);
 }
 
-static void eseqiv_chain(struct scatterlist *head, struct scatterlist *sg,
-                        int chain)
-{
-       if (chain) {
-               head->length += sg->length;
-               sg = scatterwalk_sg_next(sg);
-       }
-
-       if (sg)
-               scatterwalk_sg_chain(head, 2, sg);
-       else
-               sg_mark_end(head);
-}
-
 static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
 {
        struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
@@ -124,19 +110,20 @@ static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
 
        sg_init_table(reqctx->src, 2);
        sg_set_buf(reqctx->src, giv, ivsize);
-       eseqiv_chain(reqctx->src, osrc, vsrc == giv + ivsize);
+       scatterwalk_crypto_chain(reqctx->src, osrc, vsrc == giv + ivsize, 2);
 
        dst = reqctx->src;
        if (osrc != odst) {
                sg_init_table(reqctx->dst, 2);
                sg_set_buf(reqctx->dst, giv, ivsize);
-               eseqiv_chain(reqctx->dst, odst, vdst == giv + ivsize);
+               scatterwalk_crypto_chain(reqctx->dst, odst, vdst == giv + ivsize, 2);
 
                dst = reqctx->dst;
        }
 
        ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
-                                    req->creq.nbytes, req->creq.info);
+                                    req->creq.nbytes + ivsize,
+                                    req->creq.info);
 
        memcpy(req->creq.info, ctx->salt, ivsize);
 
@@ -152,7 +139,8 @@ static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
        if (err)
                goto out;
 
-       eseqiv_complete2(req);
+       if (giv != req->giv)
+               eseqiv_complete2(req);
 
 out:
        return err;
@@ -162,17 +150,22 @@ static int eseqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
 {
        struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
        struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
+       int err = 0;
 
        spin_lock_bh(&ctx->lock);
        if (crypto_ablkcipher_crt(geniv)->givencrypt != eseqiv_givencrypt_first)
                goto unlock;
 
        crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
-       get_random_bytes(ctx->salt, crypto_ablkcipher_ivsize(geniv));
+       err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
+                                  crypto_ablkcipher_ivsize(geniv));
 
 unlock:
        spin_unlock_bh(&ctx->lock);
 
+       if (err)
+               return err;
+
        return eseqiv_givencrypt(req);
 }
 
@@ -215,9 +208,13 @@ static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
        struct crypto_instance *inst;
        int err;
 
+       err = crypto_get_default_rng();
+       if (err)
+               return ERR_PTR(err);
+
        inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
        if (IS_ERR(inst))
-               goto out;
+               goto put_rng;
 
        err = -EINVAL;
        if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
@@ -237,24 +234,36 @@ out:
 free_inst:
        skcipher_geniv_free(inst);
        inst = ERR_PTR(err);
+put_rng:
+       crypto_put_default_rng();
        goto out;
 }
 
+static void eseqiv_free(struct crypto_instance *inst)
+{
+       skcipher_geniv_free(inst);
+       crypto_put_default_rng();
+}
+
 static struct crypto_template eseqiv_tmpl = {
        .name = "eseqiv",
        .alloc = eseqiv_alloc,
-       .free = skcipher_geniv_free,
+       .free = eseqiv_free,
        .module = THIS_MODULE,
 };
 
-int __init eseqiv_module_init(void)
+static int __init eseqiv_module_init(void)
 {
        return crypto_register_template(&eseqiv_tmpl);
 }
-EXPORT_SYMBOL_GPL(eseqiv_module_init);
 
-void __exit eseqiv_module_exit(void)
+static void __exit eseqiv_module_exit(void)
 {
        crypto_unregister_template(&eseqiv_tmpl);
 }
-EXPORT_SYMBOL_GPL(eseqiv_module_exit);
+
+module_init(eseqiv_module_init);
+module_exit(eseqiv_module_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");