]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - crypto/api.c
Merge with /shiny/git/linux-2.6/.git
[karo-tx-linux.git] / crypto / api.c
index f55856b219927e082b83627e4564c7ef3879bef2..b4728811ce3b1570e7ee2806644692d099c32cdf 100644 (file)
  * any later version.
  *
  */
+
+#include <linux/compiler.h>
 #include <linux/init.h>
 #include <linux/crypto.h>
 #include <linux/errno.h>
+#include <linux/kmod.h>
 #include <linux/rwsem.h>
 #include <linux/slab.h>
 #include "internal.h"
@@ -33,7 +36,7 @@ static inline void crypto_alg_put(struct crypto_alg *alg)
        module_put(alg->cra_module);
 }
 
-struct crypto_alg *crypto_alg_lookup(const char *name)
+static struct crypto_alg *crypto_alg_lookup(const char *name)
 {
        struct crypto_alg *q, *alg = NULL;
 
@@ -54,6 +57,13 @@ struct crypto_alg *crypto_alg_lookup(const char *name)
        return alg;
 }
 
+/* A far more intelligent version of this is planned.  For now, just
+ * try an exact match on the name of the algorithm. */
+static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
+{
+       return try_then_request_module(crypto_alg_lookup(name), name);
+}
+
 static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
 {
        tfm->crt_flags = 0;
@@ -117,20 +127,46 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
        }
 }
 
+static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
+{
+       unsigned int len;
+
+       switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
+       default:
+               BUG();
+
+       case CRYPTO_ALG_TYPE_CIPHER:
+               len = crypto_cipher_ctxsize(alg, flags);
+               break;
+               
+       case CRYPTO_ALG_TYPE_DIGEST:
+               len = crypto_digest_ctxsize(alg, flags);
+               break;
+               
+       case CRYPTO_ALG_TYPE_COMPRESS:
+               len = crypto_compress_ctxsize(alg, flags);
+               break;
+       }
+
+       return len + alg->cra_alignmask;
+}
+
 struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
 {
        struct crypto_tfm *tfm = NULL;
        struct crypto_alg *alg;
+       unsigned int tfm_size;
 
        alg = crypto_alg_mod_lookup(name);
        if (alg == NULL)
                goto out;
-       
-       tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
+
+       tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
+       tfm = kmalloc(tfm_size, GFP_KERNEL);
        if (tfm == NULL)
                goto out_put;
 
-       memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
+       memset(tfm, 0, tfm_size);
        
        tfm->__crt_alg = alg;
        
@@ -155,8 +191,14 @@ out:
 
 void crypto_free_tfm(struct crypto_tfm *tfm)
 {
-       struct crypto_alg *alg = tfm->__crt_alg;
-       int size = sizeof(*tfm) + alg->cra_ctxsize;
+       struct crypto_alg *alg;
+       int size;
+
+       if (unlikely(!tfm))
+               return;
+
+       alg = tfm->__crt_alg;
+       size = sizeof(*tfm) + alg->cra_ctxsize;
 
        crypto_exit_ops(tfm);
        crypto_alg_put(alg);