nfsd: nfsd should drop CAP_MKNOD for non-root
[safe/jmp/linux-2.6] / crypto / shash.c
index 82ec4bd..d5a2b61 100644 (file)
@@ -10,6 +10,7 @@
  *
  */
 
+#include <crypto/scatterwalk.h>
 #include <crypto/internal/hash.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/seq_file.h>
 
+static const struct crypto_type crypto_shash_type;
+
 static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
 {
        return container_of(tfm, struct crypto_shash, base);
 }
 
+#include "internal.h"
+
 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
                                  unsigned int keylen)
 {
@@ -50,6 +55,9 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
        struct shash_alg *shash = crypto_shash_alg(tfm);
        unsigned long alignmask = crypto_shash_alignmask(tfm);
 
+       if (!shash->setkey)
+               return -ENOSYS;
+
        if ((unsigned long)key & alignmask)
                return shash_setkey_unaligned(tfm, key, keylen);
 
@@ -167,6 +175,270 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
 }
 EXPORT_SYMBOL_GPL(crypto_shash_digest);
 
+int crypto_shash_import(struct shash_desc *desc, const u8 *in)
+{
+       struct crypto_shash *tfm = desc->tfm;
+       struct shash_alg *alg = crypto_shash_alg(tfm);
+
+       memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
+
+       if (alg->reinit)
+               alg->reinit(desc);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(crypto_shash_import);
+
+static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
+                             unsigned int keylen)
+{
+       struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
+
+       return crypto_shash_setkey(*ctx, key, keylen);
+}
+
+static int shash_async_init(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 crypto_shash_init(desc);
+}
+
+static int shash_async_update(struct ahash_request *req)
+{
+       struct shash_desc *desc = ahash_request_ctx(req);
+       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_shash_update(desc, walk.data, nbytes);
+
+       return nbytes;
+}
+
+static int shash_async_final(struct ahash_request *req)
+{
+       return crypto_shash_final(ahash_request_ctx(req), req->result);
+}
+
+static int shash_async_digest(struct ahash_request *req)
+{
+       struct scatterlist *sg = req->src;
+       unsigned int offset = sg->offset;
+       unsigned int nbytes = req->nbytes;
+       int err;
+
+       if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
+               struct crypto_shash **ctx =
+                       crypto_ahash_ctx(crypto_ahash_reqtfm(req));
+               struct shash_desc *desc = ahash_request_ctx(req);
+               void *data;
+
+               desc->tfm = *ctx;
+               desc->flags = req->base.flags;
+
+               data = crypto_kmap(sg_page(sg), 0);
+               err = crypto_shash_digest(desc, data + offset, nbytes,
+                                         req->result);
+               crypto_kunmap(data, 0);
+               crypto_yield(desc->flags);
+               goto out;
+       }
+
+       err = shash_async_init(req);
+       if (err)
+               goto out;
+
+       err = shash_async_update(req);
+       if (err)
+               goto out;
+
+       err = shash_async_final(req);
+
+out:
+       return err;
+}
+
+static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
+{
+       struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
+
+       crypto_free_shash(*ctx);
+}
+
+static 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_shash **ctx = crypto_tfm_ctx(tfm);
+       struct crypto_shash *shash;
+
+       if (!crypto_mod_get(calg))
+               return -EAGAIN;
+
+       shash = __crypto_shash_cast(crypto_create_tfm(
+               calg, &crypto_shash_type));
+       if (IS_ERR(shash)) {
+               crypto_mod_put(calg);
+               return PTR_ERR(shash);
+       }
+
+       *ctx = shash;
+       tfm->exit = crypto_exit_shash_ops_async;
+
+       crt->init = shash_async_init;
+       crt->update = shash_async_update;
+       crt->final  = shash_async_final;
+       crt->digest = shash_async_digest;
+       crt->setkey = shash_async_setkey;
+
+       crt->digestsize = alg->digestsize;
+       crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
+
+       return 0;
+}
+
+static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
+                              unsigned int keylen)
+{
+       struct shash_desc *desc = crypto_hash_ctx(tfm);
+
+       return crypto_shash_setkey(desc->tfm, key, keylen);
+}
+
+static int shash_compat_init(struct hash_desc *hdesc)
+{
+       struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
+
+       desc->flags = hdesc->flags;
+
+       return crypto_shash_init(desc);
+}
+
+static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
+                              unsigned int len)
+{
+       struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
+       struct crypto_hash_walk walk;
+       int nbytes;
+
+       for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
+            nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
+               nbytes = crypto_shash_update(desc, walk.data, nbytes);
+
+       return nbytes;
+}
+
+static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
+{
+       return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out);
+}
+
+static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
+                              unsigned int nbytes, u8 *out)
+{
+       unsigned int offset = sg->offset;
+       int err;
+
+       if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
+               struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
+               void *data;
+
+               desc->flags = hdesc->flags;
+
+               data = crypto_kmap(sg_page(sg), 0);
+               err = crypto_shash_digest(desc, data + offset, nbytes, out);
+               crypto_kunmap(data, 0);
+               crypto_yield(desc->flags);
+               goto out;
+       }
+
+       err = shash_compat_init(hdesc);
+       if (err)
+               goto out;
+
+       err = shash_compat_update(hdesc, sg, nbytes);
+       if (err)
+               goto out;
+
+       err = shash_compat_final(hdesc, out);
+
+out:
+       return err;
+}
+
+static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
+{
+       struct shash_desc *desc= crypto_tfm_ctx(tfm);
+
+       crypto_free_shash(desc->tfm);
+}
+
+static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
+{
+       struct hash_tfm *crt = &tfm->crt_hash;
+       struct crypto_alg *calg = tfm->__crt_alg;
+       struct shash_alg *alg = __crypto_shash_alg(calg);
+       struct shash_desc *desc = crypto_tfm_ctx(tfm);
+       struct crypto_shash *shash;
+
+       if (!crypto_mod_get(calg))
+               return -EAGAIN;
+
+       shash = __crypto_shash_cast(crypto_create_tfm(
+               calg, &crypto_shash_type));
+       if (IS_ERR(shash)) {
+               crypto_mod_put(calg);
+               return PTR_ERR(shash);
+       }
+
+       desc->tfm = shash;
+       tfm->exit = crypto_exit_shash_ops_compat;
+
+       crt->init = shash_compat_init;
+       crt->update = shash_compat_update;
+       crt->final  = shash_compat_final;
+       crt->digest = shash_compat_digest;
+       crt->setkey = shash_compat_setkey;
+
+       crt->digestsize = alg->digestsize;
+
+       return 0;
+}
+
+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;
+}
+
+static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
+                                        u32 mask)
+{
+       struct shash_alg *salg = __crypto_shash_alg(alg);
+
+       switch (mask & CRYPTO_ALG_TYPE_MASK) {
+       case CRYPTO_ALG_TYPE_HASH_MASK:
+               return sizeof(struct shash_desc) + salg->descsize;
+       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)
 {
@@ -194,7 +466,9 @@ static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
 }
 
 static const struct crypto_type crypto_shash_type = {
+       .ctxsize = crypto_shash_ctxsize,
        .extsize = crypto_shash_extsize,
+       .init = crypto_init_shash_ops,
        .init_tfm = crypto_shash_init_tfm,
 #ifdef CONFIG_PROC_FS
        .show = crypto_shash_show,