Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-drm-fsl-dcu.git] / crypto / api.c
index 7e5522cf856e047ca277a9b1547531ef073e6134..55af8bb0f0502eb7f80f3ed2c71fb21518319252 100644 (file)
@@ -212,31 +212,13 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
 
-static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
+static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
 {
-       tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
-       flags &= ~CRYPTO_TFM_REQ_MASK;
-       
-       switch (crypto_tfm_alg_type(tfm)) {
-       case CRYPTO_ALG_TYPE_CIPHER:
-               return crypto_init_cipher_flags(tfm, flags);
-               
-       case CRYPTO_ALG_TYPE_DIGEST:
-               return crypto_init_digest_flags(tfm, flags);
-               
-       case CRYPTO_ALG_TYPE_COMPRESS:
-               return crypto_init_compress_flags(tfm, flags);
-       
-       default:
-               break;
-       }
-       
-       BUG();
-       return -EINVAL;
-}
+       const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
+
+       if (type_obj)
+               return type_obj->init(tfm, type, mask);
 
-static int crypto_init_ops(struct crypto_tfm *tfm)
-{
        switch (crypto_tfm_alg_type(tfm)) {
        case CRYPTO_ALG_TYPE_CIPHER:
                return crypto_init_cipher_ops(tfm);
@@ -257,6 +239,14 @@ static int crypto_init_ops(struct crypto_tfm *tfm)
 
 static void crypto_exit_ops(struct crypto_tfm *tfm)
 {
+       const struct crypto_type *type = tfm->__crt_alg->cra_type;
+
+       if (type) {
+               if (type->exit)
+                       type->exit(tfm);
+               return;
+       }
+
        switch (crypto_tfm_alg_type(tfm)) {
        case CRYPTO_ALG_TYPE_CIPHER:
                crypto_exit_cipher_ops(tfm);
@@ -276,28 +266,33 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
        }
 }
 
-static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
+static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
 {
+       const struct crypto_type *type_obj = alg->cra_type;
        unsigned int len;
 
+       len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
+       if (type_obj)
+               return len + type_obj->ctxsize(alg, type, mask);
+
        switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
        default:
                BUG();
 
        case CRYPTO_ALG_TYPE_CIPHER:
-               len = crypto_cipher_ctxsize(alg, flags);
+               len += crypto_cipher_ctxsize(alg);
                break;
                
        case CRYPTO_ALG_TYPE_DIGEST:
-               len = crypto_digest_ctxsize(alg, flags);
+               len += crypto_digest_ctxsize(alg);
                break;
                
        case CRYPTO_ALG_TYPE_COMPRESS:
-               len = crypto_compress_ctxsize(alg, flags);
+               len += crypto_compress_ctxsize(alg);
                break;
        }
 
-       return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
+       return len;
 }
 
 void crypto_shoot_alg(struct crypto_alg *alg)
@@ -308,24 +303,21 @@ void crypto_shoot_alg(struct crypto_alg *alg)
 }
 EXPORT_SYMBOL_GPL(crypto_shoot_alg);
 
-struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 flags)
+struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
+                                     u32 mask)
 {
        struct crypto_tfm *tfm = NULL;
        unsigned int tfm_size;
        int err = -ENOMEM;
 
-       tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
+       tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
        tfm = kzalloc(tfm_size, GFP_KERNEL);
        if (tfm == NULL)
-               goto out;
+               goto out_err;
 
        tfm->__crt_alg = alg;
 
-       err = crypto_init_flags(tfm, flags);
-       if (err)
-               goto out_free_tfm;
-               
-       err = crypto_init_ops(tfm);
+       err = crypto_init_ops(tfm, type, mask);
        if (err)
                goto out_free_tfm;
 
@@ -341,37 +333,73 @@ cra_init_failed:
        crypto_exit_ops(tfm);
 out_free_tfm:
        kfree(tfm);
+out_err:
        tfm = ERR_PTR(err);
 out:
        return tfm;
 }
 EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
 
-struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
+/*
+ *     crypto_alloc_base - Locate algorithm and allocate transform
+ *     @alg_name: Name of algorithm
+ *     @type: Type of algorithm
+ *     @mask: Mask for type comparison
+ *
+ *     crypto_alloc_base() will first attempt to locate an already loaded
+ *     algorithm.  If that fails and the kernel supports dynamically loadable
+ *     modules, it will then attempt to load a module of the same name or
+ *     alias.  If that fails it will send a query to any loaded crypto manager
+ *     to construct an algorithm on the fly.  A refcount is grabbed on the
+ *     algorithm which is then associated with the new transform.
+ *
+ *     The returned transform is of a non-determinate type.  Most people
+ *     should use one of the more specific allocation functions such as
+ *     crypto_alloc_blkcipher.
+ *
+ *     In case of error the return value is an error pointer.
+ */
+struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
 {
-       struct crypto_tfm *tfm = NULL;
+       struct crypto_tfm *tfm;
        int err;
 
-       do {
+       for (;;) {
                struct crypto_alg *alg;
 
-               alg = crypto_alg_mod_lookup(name, 0, 0);
-               err = PTR_ERR(alg);
-               if (IS_ERR(alg))
-                       continue;
+               alg = crypto_alg_mod_lookup(alg_name, type, mask);
+               if (IS_ERR(alg)) {
+                       err = PTR_ERR(alg);
+                       goto err;
+               }
 
-               tfm = __crypto_alloc_tfm(alg, flags);
-               err = 0;
-               if (IS_ERR(tfm)) {
-                       crypto_mod_put(alg);
-                       err = PTR_ERR(tfm);
-                       tfm = NULL;
+               tfm = __crypto_alloc_tfm(alg, type, mask);
+               if (!IS_ERR(tfm))
+                       return tfm;
+
+               crypto_mod_put(alg);
+               err = PTR_ERR(tfm);
+
+err:
+               if (err != -EAGAIN)
+                       break;
+               if (signal_pending(current)) {
+                       err = -EINTR;
+                       break;
                }
-       } while (err == -EAGAIN && !signal_pending(current));
+       }
 
-       return tfm;
+       return ERR_PTR(err);
 }
-
+EXPORT_SYMBOL_GPL(crypto_alloc_base);
+/*
+ *     crypto_free_tfm - Free crypto transform
+ *     @tfm: Transform to free
+ *
+ *     crypto_free_tfm() frees up the transform and any associated resources,
+ *     then drops the refcount on the associated algorithm.
+ */
 void crypto_free_tfm(struct crypto_tfm *tfm)
 {
        struct crypto_alg *alg;
@@ -391,10 +419,12 @@ void crypto_free_tfm(struct crypto_tfm *tfm)
        kfree(tfm);
 }
 
-int crypto_alg_available(const char *name, u32 flags)
+EXPORT_SYMBOL_GPL(crypto_free_tfm);
+
+int crypto_has_alg(const char *name, u32 type, u32 mask)
 {
        int ret = 0;
-       struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
+       struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
        
        if (!IS_ERR(alg)) {
                crypto_mod_put(alg);
@@ -403,7 +433,4 @@ int crypto_alg_available(const char *name, u32 flags)
        
        return ret;
 }
-
-EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
-EXPORT_SYMBOL_GPL(crypto_free_tfm);
-EXPORT_SYMBOL_GPL(crypto_alg_available);
+EXPORT_SYMBOL_GPL(crypto_has_alg);