net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / crypto / ahash.c
index 26be87c..555c5e5 100644 (file)
@@ -30,6 +30,7 @@ struct ahash_request_priv {
        crypto_completion_t complete;
        void *data;
        u8 *result;
+       u32 flags;
        void *ubuf[] CRYPTO_MINALIGN_ATTR;
 };
 
@@ -171,11 +172,18 @@ int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
                        unsigned int keylen)
 {
        unsigned long alignmask = crypto_ahash_alignmask(tfm);
+       int err;
 
        if ((unsigned long)key & alignmask)
-               return ahash_setkey_unaligned(tfm, key, keylen);
+               err = ahash_setkey_unaligned(tfm, key, keylen);
+       else
+               err = tfm->setkey(tfm, key, keylen);
 
-       return tfm->setkey(tfm, key, keylen);
+       if (err)
+               return err;
+
+       crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
+       return 0;
 }
 EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
 
@@ -191,55 +199,12 @@ static inline unsigned int ahash_align_buffer_size(unsigned len,
        return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
 }
 
-static void ahash_op_unaligned_finish(struct ahash_request *req, int err)
-{
-       struct ahash_request_priv *priv = req->priv;
-
-       if (err == -EINPROGRESS)
-               return;
-
-       if (!err)
-               memcpy(priv->result, req->result,
-                      crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
-
-       /* Restore the original crypto request. */
-       req->result = priv->result;
-       req->base.complete = priv->complete;
-       req->base.data = priv->data;
-       req->priv = NULL;
-
-       /* Free the req->priv.priv from the ADJUSTED request. */
-       kzfree(priv);
-}
-
-static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
-{
-       struct ahash_request *areq = req->data;
-
-       /*
-        * Restore the original request, see ahash_op_unaligned() for what
-        * goes where.
-        *
-        * The "struct ahash_request *req" here is in fact the "req.base"
-        * from the ADJUSTED request from ahash_op_unaligned(), thus as it
-        * is a pointer to self, it is also the ADJUSTED "req" .
-        */
-
-       /* First copy areq->result into areq->priv.result */
-       ahash_op_unaligned_finish(areq, err);
-
-       /* Complete the ORIGINAL request. */
-       areq->base.complete(&areq->base, err);
-}
-
-static int ahash_op_unaligned(struct ahash_request *req,
-                             int (*op)(struct ahash_request *))
+static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
 {
        struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
        unsigned long alignmask = crypto_ahash_alignmask(tfm);
        unsigned int ds = crypto_ahash_digestsize(tfm);
        struct ahash_request_priv *priv;
-       int err;
 
        priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
                       (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
@@ -275,6 +240,8 @@ static int ahash_op_unaligned(struct ahash_request *req,
        priv->result = req->result;
        priv->complete = req->base.complete;
        priv->data = req->base.data;
+       priv->flags = req->base.flags;
+
        /*
         * WARNING: We do not backup req->priv here! The req->priv
         *          is for internal use of the Crypto API and the
@@ -282,12 +249,83 @@ static int ahash_op_unaligned(struct ahash_request *req,
         */
 
        req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
-       req->base.complete = ahash_op_unaligned_done;
+       req->base.complete = cplt;
        req->base.data = req;
        req->priv = priv;
 
+       return 0;
+}
+
+static void ahash_restore_req(struct ahash_request *req, int err)
+{
+       struct ahash_request_priv *priv = req->priv;
+
+       if (!err)
+               memcpy(priv->result, req->result,
+                      crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
+
+       /* Restore the original crypto request. */
+       req->result = priv->result;
+
+       ahash_request_set_callback(req, priv->flags,
+                                  priv->complete, priv->data);
+       req->priv = NULL;
+
+       /* Free the req->priv.priv from the ADJUSTED request. */
+       kzfree(priv);
+}
+
+static void ahash_notify_einprogress(struct ahash_request *req)
+{
+       struct ahash_request_priv *priv = req->priv;
+       struct crypto_async_request oreq;
+
+       oreq.data = priv->data;
+
+       priv->complete(&oreq, -EINPROGRESS);
+}
+
+static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
+{
+       struct ahash_request *areq = req->data;
+
+       if (err == -EINPROGRESS) {
+               ahash_notify_einprogress(areq);
+               return;
+       }
+
+       /*
+        * Restore the original request, see ahash_op_unaligned() for what
+        * goes where.
+        *
+        * The "struct ahash_request *req" here is in fact the "req.base"
+        * from the ADJUSTED request from ahash_op_unaligned(), thus as it
+        * is a pointer to self, it is also the ADJUSTED "req" .
+        */
+
+       /* First copy req->result into req->priv.result */
+       ahash_restore_req(areq, err);
+
+       /* Complete the ORIGINAL request. */
+       areq->base.complete(&areq->base, err);
+}
+
+static int ahash_op_unaligned(struct ahash_request *req,
+                             int (*op)(struct ahash_request *))
+{
+       int err;
+
+       err = ahash_save_req(req, ahash_op_unaligned_done);
+       if (err)
+               return err;
+
        err = op(req);
-       ahash_op_unaligned_finish(req, err);
+       if (err == -EINPROGRESS ||
+           (err == -EBUSY && (ahash_request_flags(req) &
+                              CRYPTO_TFM_REQ_MAY_BACKLOG)))
+               return err;
+
+       ahash_restore_req(req, err);
 
        return err;
 }
@@ -318,34 +356,25 @@ EXPORT_SYMBOL_GPL(crypto_ahash_finup);
 
 int crypto_ahash_digest(struct ahash_request *req)
 {
-       return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
-}
-EXPORT_SYMBOL_GPL(crypto_ahash_digest);
-
-static void ahash_def_finup_finish2(struct ahash_request *req, int err)
-{
-       struct ahash_request_priv *priv = req->priv;
+       struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 
-       if (err == -EINPROGRESS)
-               return;
+       if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
+               return -ENOKEY;
 
-       if (!err)
-               memcpy(priv->result, req->result,
-                      crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
-
-       kzfree(priv);
+       return crypto_ahash_op(req, tfm->digest);
 }
+EXPORT_SYMBOL_GPL(crypto_ahash_digest);
 
 static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
 {
        struct ahash_request *areq = req->data;
-       struct ahash_request_priv *priv = areq->priv;
-       crypto_completion_t complete = priv->complete;
-       void *data = priv->data;
 
-       ahash_def_finup_finish2(areq, err);
+       if (err == -EINPROGRESS)
+               return;
 
-       complete(data, err);
+       ahash_restore_req(areq, err);
+
+       areq->base.complete(&areq->base, err);
 }
 
 static int ahash_def_finup_finish1(struct ahash_request *req, int err)
@@ -354,49 +383,52 @@ static int ahash_def_finup_finish1(struct ahash_request *req, int err)
                goto out;
 
        req->base.complete = ahash_def_finup_done2;
-       req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
+
        err = crypto_ahash_reqtfm(req)->final(req);
+       if (err == -EINPROGRESS ||
+           (err == -EBUSY && (ahash_request_flags(req) &
+                              CRYPTO_TFM_REQ_MAY_BACKLOG)))
+               return err;
 
 out:
-       ahash_def_finup_finish2(req, err);
+       ahash_restore_req(req, err);
        return err;
 }
 
 static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
 {
        struct ahash_request *areq = req->data;
-       struct ahash_request_priv *priv = areq->priv;
-       crypto_completion_t complete = priv->complete;
-       void *data = priv->data;
+
+       if (err == -EINPROGRESS) {
+               ahash_notify_einprogress(areq);
+               return;
+       }
+
+       areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
 
        err = ahash_def_finup_finish1(areq, err);
+       if (areq->priv)
+               return;
 
-       complete(data, err);
+       areq->base.complete(&areq->base, err);
 }
 
 static int ahash_def_finup(struct ahash_request *req)
 {
        struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
-       unsigned long alignmask = crypto_ahash_alignmask(tfm);
-       unsigned int ds = crypto_ahash_digestsize(tfm);
-       struct ahash_request_priv *priv;
-
-       priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
-                      (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
-                      GFP_KERNEL : GFP_ATOMIC);
-       if (!priv)
-               return -ENOMEM;
+       int err;
 
-       priv->result = req->result;
-       priv->complete = req->base.complete;
-       priv->data = req->base.data;
+       err = ahash_save_req(req, ahash_def_finup_done1);
+       if (err)
+               return err;
 
-       req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
-       req->base.complete = ahash_def_finup_done1;
-       req->base.data = req;
-       req->priv = priv;
+       err = tfm->update(req);
+       if (err == -EINPROGRESS ||
+           (err == -EBUSY && (ahash_request_flags(req) &
+                              CRYPTO_TFM_REQ_MAY_BACKLOG)))
+               return err;
 
-       return ahash_def_finup_finish1(req, tfm->update(req));
+       return ahash_def_finup_finish1(req, err);
 }
 
 static int ahash_no_export(struct ahash_request *req, void *out)
@@ -415,7 +447,6 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
        struct ahash_alg *alg = crypto_ahash_alg(hash);
 
        hash->setkey = ahash_nosetkey;
-       hash->has_setkey = false;
        hash->export = ahash_no_export;
        hash->import = ahash_no_import;
 
@@ -430,7 +461,8 @@ static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
 
        if (alg->setkey) {
                hash->setkey = alg->setkey;
-               hash->has_setkey = true;
+               if (!(alg->halg.base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
+                       crypto_ahash_set_flags(hash, CRYPTO_TFM_NEED_KEY);
        }
        if (alg->export)
                hash->export = alg->export;
@@ -579,5 +611,16 @@ struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
 }
 EXPORT_SYMBOL_GPL(ahash_attr_alg);
 
+bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
+{
+       struct crypto_alg *alg = &halg->base;
+
+       if (alg->cra_type != &crypto_ahash_type)
+               return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
+
+       return __crypto_ahash_alg(alg)->setkey != NULL;
+}
+EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
+
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");