Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[linux-2.6.git] / crypto / shash.c
index 3b1b06b..21fc12e 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;
@@ -45,8 +47,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
        alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
        memcpy(alignbuffer, key, keylen);
        err = shash->setkey(tfm, alignbuffer, keylen);
-       memset(alignbuffer, 0, keylen);
-       kfree(buffer);
+       kzfree(buffer);
        return err;
 }
 
@@ -77,15 +78,19 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
        unsigned long alignmask = crypto_shash_alignmask(tfm);
        unsigned int unaligned_len = alignmask + 1 -
                                     ((unsigned long)data & alignmask);
-       u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
+       u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
                __attribute__ ((aligned));
+       u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
+       int err;
 
        if (unaligned_len > len)
                unaligned_len = len;
 
        memcpy(buf, data, unaligned_len);
+       err = shash->update(desc, buf, unaligned_len);
+       memset(buf, 0, unaligned_len);
 
-       return shash->update(desc, buf, unaligned_len) ?:
+       return err ?:
               shash->update(desc, data + unaligned_len, len - unaligned_len);
 }
 
@@ -109,12 +114,19 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
        unsigned long alignmask = crypto_shash_alignmask(tfm);
        struct shash_alg *shash = crypto_shash_alg(tfm);
        unsigned int ds = crypto_shash_digestsize(tfm);
-       u8 buf[shash_align_buffer_size(ds, alignmask)]
+       u8 ubuf[shash_align_buffer_size(ds, alignmask)]
                __attribute__ ((aligned));
+       u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
        int err;
 
        err = shash->final(desc, buf);
+       if (err)
+               goto out;
+
        memcpy(out, buf, ds);
+
+out:
+       memset(buf, 0, ds);
        return err;
 }
 
@@ -173,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,
@@ -225,6 +239,38 @@ static int shash_async_final(struct ahash_request *req)
        return crypto_shash_final(ahash_request_ctx(req), req->result);
 }
 
+int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
+{
+       struct crypto_hash_walk walk;
+       int 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;
+}
+EXPORT_SYMBOL_GPL(shash_ahash_finup);
+
+static int shash_async_finup(struct ahash_request *req)
+{
+       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 shash_ahash_finup(req, desc);
+}
+
 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
 {
        struct scatterlist *sg = req->src;
@@ -235,15 +281,14 @@ int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
        if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
                void *data;
 
-               data = crypto_kmap(sg_page(sg), 0);
+               data = kmap_atomic(sg_page(sg));
                err = crypto_shash_digest(desc, data + offset, nbytes,
                                          req->result);
-               crypto_kunmap(data, 0);
+               kunmap_atomic(data);
                crypto_yield(desc->flags);
        } else
                err = crypto_shash_init(desc) ?:
-                     shash_ahash_update(req, desc) ?:
-                     crypto_shash_final(desc, req->result);
+                     shash_ahash_finup(req, desc);
 
        return err;
 }
@@ -260,6 +305,22 @@ static int shash_async_digest(struct ahash_request *req)
        return shash_ahash_digest(req, desc);
 }
 
+static int shash_async_export(struct ahash_request *req, void *out)
+{
+       return crypto_shash_export(ahash_request_ctx(req), out);
+}
+
+static int shash_async_import(struct ahash_request *req, const void *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)
 {
        struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
@@ -267,11 +328,11 @@ static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
        crypto_free_shash(*ctx);
 }
 
-static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
+int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
 {
        struct crypto_alg *calg = tfm->__crt_alg;
        struct shash_alg *alg = __crypto_shash_alg(calg);
-       struct ahash_tfm *crt = &tfm->crt_ahash;
+       struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
        struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
        struct crypto_shash *shash;
 
@@ -289,11 +350,17 @@ static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
 
        crt->init = shash_async_init;
        crt->update = shash_async_update;
-       crt->final  = shash_async_final;
+       crt->final = shash_async_final;
+       crt->finup = shash_async_finup;
        crt->digest = shash_async_digest;
-       crt->setkey = shash_async_setkey;
 
-       crt->digestsize = alg->digestsize;
+       if (alg->setkey)
+               crt->setkey = shash_async_setkey;
+       if (alg->export)
+               crt->export = shash_async_export;
+       if (alg->import)
+               crt->import = shash_async_import;
+
        crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
 
        return 0;
@@ -353,9 +420,9 @@ static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
 
                desc->flags = hdesc->flags;
 
-               data = crypto_kmap(sg_page(sg), 0);
+               data = kmap_atomic(sg_page(sg));
                err = crypto_shash_digest(desc, data + offset, nbytes, out);
-               crypto_kunmap(data, 0);
+               kunmap_atomic(data);
                crypto_yield(desc->flags);
                goto out;
        }
@@ -428,8 +495,6 @@ static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
        switch (mask & CRYPTO_ALG_TYPE_MASK) {
        case CRYPTO_ALG_TYPE_HASH_MASK:
                return crypto_init_shash_ops_compat(tfm);
-       case CRYPTO_ALG_TYPE_AHASH_MASK:
-               return crypto_init_shash_ops_async(tfm);
        }
 
        return -EINVAL;
@@ -441,15 +506,12 @@ static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
        switch (mask & CRYPTO_ALG_TYPE_MASK) {
        case CRYPTO_ALG_TYPE_HASH_MASK:
                return sizeof(struct shash_desc *);
-       case CRYPTO_ALG_TYPE_AHASH_MASK:
-               return sizeof(struct crypto_shash *);
        }
 
        return 0;
 }
 
-static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
-                                const struct crypto_type *frontend)
+static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
 {
        struct crypto_shash *hash = __crypto_shash_cast(tfm);
 
@@ -457,12 +519,36 @@ static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
        return 0;
 }
 
-static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
-                                        const struct crypto_type *frontend)
+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)
@@ -482,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,
@@ -512,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;