[CRYPTO] api: Add crypto_alg reference counting
[pandora-kernel.git] / crypto / api.c
index 40ae42e..5994a58 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
+ * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  *
  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  * and Nettle, by Niels Möller.
 #include <linux/init.h>
 #include <linux/crypto.h>
 #include <linux/errno.h>
+#include <linux/kernel.h>
 #include <linux/kmod.h>
 #include <linux/rwsem.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include "internal.h"
 
 LIST_HEAD(crypto_alg_list);
 DECLARE_RWSEM(crypto_alg_sem);
 
-static inline int crypto_alg_get(struct crypto_alg *alg)
+static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
 {
-       return try_module_get(alg->cra_module);
+       atomic_inc(&alg->cra_refcnt);
+       return alg;
 }
 
 static inline void crypto_alg_put(struct crypto_alg *alg)
 {
+       if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
+               alg->cra_destroy(alg);
+}
+
+static struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
+{
+       return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
+}
+
+static void crypto_mod_put(struct crypto_alg *alg)
+{
+       crypto_alg_put(alg);
        module_put(alg->cra_module);
 }
 
 static struct crypto_alg *crypto_alg_lookup(const char *name)
 {
        struct crypto_alg *q, *alg = NULL;
+       int best = -1;
 
        if (!name)
                return NULL;
@@ -46,11 +63,23 @@ static struct crypto_alg *crypto_alg_lookup(const char *name)
        down_read(&crypto_alg_sem);
        
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
-               if (!(strcmp(q->cra_name, name))) {
-                       if (crypto_alg_get(q))
-                               alg = q;
+               int exact, fuzzy;
+
+               exact = !strcmp(q->cra_driver_name, name);
+               fuzzy = !strcmp(q->cra_name, name);
+               if (!exact && !(fuzzy && q->cra_priority > best))
+                       continue;
+
+               if (unlikely(!crypto_mod_get(q)))
+                       continue;
+
+               best = q->cra_priority;
+               if (alg)
+                       crypto_mod_put(alg);
+               alg = q;
+
+               if (exact)
                        break;
-               }
        }
        
        up_read(&crypto_alg_sem);
@@ -149,7 +178,7 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
                break;
        }
 
-       return len + alg->cra_alignmask;
+       return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
 }
 
 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
@@ -163,29 +192,30 @@ struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
                goto out;
 
        tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
-       tfm = kmalloc(tfm_size, GFP_KERNEL);
+       tfm = kzalloc(tfm_size, GFP_KERNEL);
        if (tfm == NULL)
                goto out_put;
 
-       memset(tfm, 0, tfm_size);
-       
        tfm->__crt_alg = alg;
        
        if (crypto_init_flags(tfm, flags))
                goto out_free_tfm;
                
-       if (crypto_init_ops(tfm)) {
-               crypto_exit_ops(tfm);
+       if (crypto_init_ops(tfm))
                goto out_free_tfm;
-       }
+
+       if (alg->cra_init && alg->cra_init(tfm))
+               goto cra_init_failed;
 
        goto out;
 
+cra_init_failed:
+       crypto_exit_ops(tfm);
 out_free_tfm:
        kfree(tfm);
        tfm = NULL;
 out_put:
-       crypto_alg_put(alg);
+       crypto_mod_put(alg);
 out:
        return tfm;
 }
@@ -201,15 +231,34 @@ void crypto_free_tfm(struct crypto_tfm *tfm)
        alg = tfm->__crt_alg;
        size = sizeof(*tfm) + alg->cra_ctxsize;
 
+       if (alg->cra_exit)
+               alg->cra_exit(tfm);
        crypto_exit_ops(tfm);
-       crypto_alg_put(alg);
+       crypto_mod_put(alg);
        memset(tfm, 0, size);
        kfree(tfm);
 }
 
+static inline int crypto_set_driver_name(struct crypto_alg *alg)
+{
+       static const char suffix[] = "-generic";
+       char *driver_name = alg->cra_driver_name;
+       int len;
+
+       if (*driver_name)
+               return 0;
+
+       len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
+       if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
+               return -ENAMETOOLONG;
+
+       memcpy(driver_name + len, suffix, sizeof(suffix));
+       return 0;
+}
+
 int crypto_register_alg(struct crypto_alg *alg)
 {
-       int ret = 0;
+       int ret;
        struct crypto_alg *q;
 
        if (alg->cra_alignmask & (alg->cra_alignmask + 1))
@@ -218,19 +267,27 @@ int crypto_register_alg(struct crypto_alg *alg)
        if (alg->cra_alignmask & alg->cra_blocksize)
                return -EINVAL;
 
-       if (alg->cra_blocksize > PAGE_SIZE)
+       if (alg->cra_blocksize > PAGE_SIZE / 8)
+               return -EINVAL;
+
+       if (alg->cra_priority < 0)
                return -EINVAL;
        
+       ret = crypto_set_driver_name(alg);
+       if (unlikely(ret))
+               return ret;
+
        down_write(&crypto_alg_sem);
        
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
-               if (!(strcmp(q->cra_name, alg->cra_name))) {
+               if (q == alg) {
                        ret = -EEXIST;
                        goto out;
                }
        }
        
-       list_add_tail(&alg->cra_list, &crypto_alg_list);
+       list_add(&alg->cra_list, &crypto_alg_list);
+       atomic_set(&alg->cra_refcnt, 1);
 out:   
        up_write(&crypto_alg_sem);
        return ret;
@@ -241,8 +298,6 @@ int crypto_unregister_alg(struct crypto_alg *alg)
        int ret = -ENOENT;
        struct crypto_alg *q;
        
-       BUG_ON(!alg->cra_module);
-       
        down_write(&crypto_alg_sem);
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
                if (alg == q) {
@@ -253,7 +308,15 @@ int crypto_unregister_alg(struct crypto_alg *alg)
        }
 out:   
        up_write(&crypto_alg_sem);
-       return ret;
+
+       if (ret)
+               return ret;
+
+       BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
+       if (alg->cra_destroy)
+               alg->cra_destroy(alg);
+
+       return 0;
 }
 
 int crypto_alg_available(const char *name, u32 flags)
@@ -262,7 +325,7 @@ int crypto_alg_available(const char *name, u32 flags)
        struct crypto_alg *alg = crypto_alg_mod_lookup(name);
        
        if (alg) {
-               crypto_alg_put(alg);
+               crypto_mod_put(alg);
                ret = 1;
        }