Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-flexiantxendom0-3.2.10.git] / crypto / shash.c
index 834d9d2..9100912 100644 (file)
@@ -17,6 +17,8 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/seq_file.h>
+#include <linux/cryptouser.h>
+#include <net/netlink.h>
 
 #include "internal.h"
 
@@ -37,7 +39,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
        u8 *buffer, *alignbuffer;
        int err;
 
-       absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
+       absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
        buffer = kmalloc(absize, GFP_KERNEL);
        if (!buffer)
                return -ENOMEM;
@@ -183,14 +185,16 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
 }
 EXPORT_SYMBOL_GPL(crypto_shash_digest);
 
-static int shash_no_export(struct shash_desc *desc, void *out)
+static int shash_default_export(struct shash_desc *desc, void *out)
 {
-       return -ENOSYS;
+       memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
+       return 0;
 }
 
-static int shash_no_import(struct shash_desc *desc, const void *in)
+static int shash_default_import(struct shash_desc *desc, const void *in)
 {
-       return -ENOSYS;
+       memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
+       return 0;
 }
 
 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
@@ -240,12 +244,17 @@ int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
        struct crypto_hash_walk walk;
        int nbytes;
 
-       for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
-            nbytes = crypto_hash_walk_done(&walk, nbytes))
+       nbytes = crypto_hash_walk_first(req, &walk);
+       if (!nbytes)
+               return crypto_shash_final(desc, req->result);
+
+       do {
                nbytes = crypto_hash_walk_last(&walk) ?
                         crypto_shash_finup(desc, walk.data, nbytes,
                                            req->result) :
                         crypto_shash_update(desc, walk.data, nbytes);
+               nbytes = crypto_hash_walk_done(&walk, nbytes);
+       } while (nbytes > 0);
 
        return nbytes;
 }
@@ -303,7 +312,13 @@ static int shash_async_export(struct ahash_request *req, void *out)
 
 static int shash_async_import(struct ahash_request *req, const void *in)
 {
-       return crypto_shash_import(ahash_request_ctx(req), in);
+       struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
+       struct shash_desc *desc = ahash_request_ctx(req);
+
+       desc->tfm = *ctx;
+       desc->flags = req->base.flags;
+
+       return crypto_shash_import(desc, in);
 }
 
 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
@@ -343,7 +358,7 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
                crt->setkey = shash_async_setkey;
        if (alg->export)
                crt->export = shash_async_export;
-       if (alg->setkey)
+       if (alg->import)
                crt->import = shash_async_import;
 
        crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
@@ -509,6 +524,31 @@ static unsigned int crypto_shash_extsize(struct crypto_alg *alg)
        return alg->cra_ctxsize;
 }
 
+#ifdef CONFIG_NET
+static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
+{
+       struct crypto_report_hash rhash;
+       struct shash_alg *salg = __crypto_shash_alg(alg);
+
+       snprintf(rhash.type, CRYPTO_MAX_ALG_NAME, "%s", "shash");
+       rhash.blocksize = alg->cra_blocksize;
+       rhash.digestsize = salg->digestsize;
+
+       NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH,
+               sizeof(struct crypto_report_hash), &rhash);
+
+       return 0;
+
+nla_put_failure:
+       return -EMSGSIZE;
+}
+#else
+static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
+{
+       return -ENOSYS;
+}
+#endif
+
 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
        __attribute__ ((unused));
 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
@@ -528,6 +568,7 @@ static const struct crypto_type crypto_shash_type = {
 #ifdef CONFIG_PROC_FS
        .show = crypto_shash_show,
 #endif
+       .report = crypto_shash_report,
        .maskclear = ~CRYPTO_ALG_TYPE_MASK,
        .maskset = CRYPTO_ALG_TYPE_MASK,
        .type = CRYPTO_ALG_TYPE_SHASH,
@@ -558,10 +599,11 @@ static int shash_prepare_alg(struct shash_alg *alg)
                alg->finup = shash_finup_unaligned;
        if (!alg->digest)
                alg->digest = shash_digest_unaligned;
-       if (!alg->import)
-               alg->import = shash_no_import;
-       if (!alg->export)
-               alg->export = shash_no_export;
+       if (!alg->export) {
+               alg->export = shash_default_export;
+               alg->import = shash_default_import;
+               alg->statesize = alg->descsize;
+       }
        if (!alg->setkey)
                alg->setkey = shash_no_setkey;