crypto: aead - Add common IV generation code
[deliverable/linux.git] / crypto / pcrypt.c
index c305d4112735cd3b7b18b477299b534b8c0e1b77..ff174b61d8204729c1e78fca2bbdd868be3f481a 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <crypto/algapi.h>
 #include <crypto/internal/aead.h>
+#include <linux/atomic.h>
 #include <linux/err.h>
 #include <linux/init.h>
 #include <linux/module.h>
@@ -60,8 +61,8 @@ static struct padata_pcrypt pdecrypt;
 static struct kset           *pcrypt_kset;
 
 struct pcrypt_instance_ctx {
-       struct crypto_spawn spawn;
-       unsigned int tfm_count;
+       struct crypto_aead_spawn spawn;
+       atomic_t tfm_count;
 };
 
 struct pcrypt_aead_ctx {
@@ -278,9 +279,8 @@ static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm)
        struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
        struct crypto_aead *cipher;
 
-       ictx->tfm_count++;
-
-       cpu_index = ictx->tfm_count % cpumask_weight(cpu_online_mask);
+       cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
+                   cpumask_weight(cpu_online_mask);
 
        ctx->cb_cpu = cpumask_first(cpu_online_mask);
        for (cpu = 0; cpu < cpu_index; cpu++)
@@ -292,9 +292,10 @@ static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm)
                return PTR_ERR(cipher);
 
        ctx->child = cipher;
-       tfm->crt_aead.reqsize = sizeof(struct pcrypt_request)
-               + sizeof(struct aead_givcrypt_request)
-               + crypto_aead_reqsize(cipher);
+       crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
+               sizeof(struct pcrypt_request) +
+               sizeof(struct aead_givcrypt_request) +
+               crypto_aead_reqsize(cipher));
 
        return 0;
 }
@@ -306,57 +307,50 @@ static void pcrypt_aead_exit_tfm(struct crypto_tfm *tfm)
        crypto_free_aead(ctx->child);
 }
 
-static struct crypto_instance *pcrypt_alloc_instance(struct crypto_alg *alg)
+static int pcrypt_init_instance(struct crypto_instance *inst,
+                               struct crypto_alg *alg)
 {
-       struct crypto_instance *inst;
-       struct pcrypt_instance_ctx *ctx;
-       int err;
-
-       inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
-       if (!inst) {
-               inst = ERR_PTR(-ENOMEM);
-               goto out;
-       }
-
-       err = -ENAMETOOLONG;
        if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
                     "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
-               goto out_free_inst;
+               return -ENAMETOOLONG;
 
        memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
 
-       ctx = crypto_instance_ctx(inst);
-       err = crypto_init_spawn(&ctx->spawn, alg, inst,
-                               CRYPTO_ALG_TYPE_MASK);
-       if (err)
-               goto out_free_inst;
-
        inst->alg.cra_priority = alg->cra_priority + 100;
        inst->alg.cra_blocksize = alg->cra_blocksize;
        inst->alg.cra_alignmask = alg->cra_alignmask;
 
-out:
-       return inst;
-
-out_free_inst:
-       kfree(inst);
-       inst = ERR_PTR(err);
-       goto out;
+       return 0;
 }
 
 static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb,
                                                 u32 type, u32 mask)
 {
+       struct pcrypt_instance_ctx *ctx;
        struct crypto_instance *inst;
        struct crypto_alg *alg;
+       const char *name;
+       int err;
+
+       name = crypto_attr_alg_name(tb[1]);
+       if (IS_ERR(name))
+               return ERR_CAST(name);
+
+       inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
+       if (!inst)
+               return ERR_PTR(-ENOMEM);
+
+       ctx = crypto_instance_ctx(inst);
+       crypto_set_aead_spawn(&ctx->spawn, inst);
 
-       alg = crypto_get_attr_alg(tb, type, (mask & CRYPTO_ALG_TYPE_MASK));
-       if (IS_ERR(alg))
-               return ERR_CAST(alg);
+       err = crypto_grab_aead(&ctx->spawn, name, 0, 0);
+       if (err)
+               goto out_free_inst;
 
-       inst = pcrypt_alloc_instance(alg);
-       if (IS_ERR(inst))
-               goto out_put_alg;
+       alg = crypto_aead_spawn_alg(&ctx->spawn);
+       err = pcrypt_init_instance(inst, alg);
+       if (err)
+               goto out_drop_aead;
 
        inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
        inst->alg.cra_type = &crypto_aead_type;
@@ -376,9 +370,15 @@ static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb,
        inst->alg.cra_aead.decrypt = pcrypt_aead_decrypt;
        inst->alg.cra_aead.givencrypt = pcrypt_aead_givencrypt;
 
-out_put_alg:
-       crypto_mod_put(alg);
+out:
        return inst;
+
+out_drop_aead:
+       crypto_drop_aead(&ctx->spawn);
+out_free_inst:
+       kfree(inst);
+       inst = ERR_PTR(err);
+       goto out;
 }
 
 static struct crypto_instance *pcrypt_alloc(struct rtattr **tb)
@@ -401,7 +401,7 @@ static void pcrypt_free(struct crypto_instance *inst)
 {
        struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst);
 
-       crypto_drop_spawn(&ctx->spawn);
+       crypto_drop_aead(&ctx->spawn);
        kfree(inst);
 }
 
This page took 0.027025 seconds and 5 git commands to generate.