Skip to content

Commit

Permalink
crypto: ahash - Add unaligned handling and default operations
Browse files Browse the repository at this point in the history
This patch exports the finup operation where available and adds
a default finup operation for ahash.  The operations final, finup
and digest also will now deal with unaligned result pointers by
copying it.  Finally export/import operations are will now be
exported too.

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed Jul 15, 2009
1 parent 093900c commit 66f6ce5
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 22 deletions.
204 changes: 201 additions & 3 deletions crypto/ahash.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

#include "internal.h"

struct ahash_request_priv {
crypto_completion_t complete;
void *data;
u8 *result;
void *ubuf[] CRYPTO_MINALIGN_ATTR;
};

static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
{
return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
Expand Down Expand Up @@ -156,7 +163,7 @@ static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
return ret;
}

static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
unsigned int keylen)
{
struct ahash_alg *ahash = crypto_ahash_alg(tfm);
Expand All @@ -167,26 +174,217 @@ static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,

return ahash->setkey(tfm, key, keylen);
}
EXPORT_SYMBOL_GPL(crypto_ahash_setkey);

static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
unsigned int keylen)
{
return -ENOSYS;
}

static inline unsigned int ahash_align_buffer_size(unsigned len,
unsigned long mask)
{
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)));

kzfree(priv);
}

static void ahash_op_unaligned_done(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_op_unaligned_finish(areq, err);

complete(data, err);
}

static int ahash_op_unaligned(struct ahash_request *req,
int (*op)(struct ahash_request *))
{
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) ?
GFP_ATOMIC : GFP_ATOMIC);
if (!priv)
return -ENOMEM;

priv->result = req->result;
priv->complete = req->base.complete;
priv->data = req->base.data;

req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
req->base.complete = ahash_op_unaligned_done;
req->base.data = req;
req->priv = priv;

err = op(req);
ahash_op_unaligned_finish(req, err);

return err;
}

static int crypto_ahash_op(struct ahash_request *req,
int (*op)(struct ahash_request *))
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
unsigned long alignmask = crypto_ahash_alignmask(tfm);

if ((unsigned long)req->result & alignmask)
return ahash_op_unaligned(req, op);

return op(req);
}

int crypto_ahash_final(struct ahash_request *req)
{
return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
}
EXPORT_SYMBOL_GPL(crypto_ahash_final);

int crypto_ahash_finup(struct ahash_request *req)
{
return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
}
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;

if (err == -EINPROGRESS)
return;

if (!err)
memcpy(priv->result, req->result,
crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));

kzfree(priv);
}

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);

complete(data, err);
}

static int ahash_def_finup_finish1(struct ahash_request *req, int err)
{
if (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);

out:
ahash_def_finup_finish2(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;

err = ahash_def_finup_finish1(areq, err);

complete(data, 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_ATOMIC : GFP_ATOMIC);
if (!priv)
return -ENOMEM;

priv->result = req->result;
priv->complete = req->base.complete;
priv->data = req->base.data;

req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
req->base.complete = ahash_def_finup_done1;
req->base.data = req;
req->priv = priv;

return ahash_def_finup_finish1(req, tfm->update(req));
}

static int ahash_no_export(struct ahash_request *req, void *out)
{
return -ENOSYS;
}

static int ahash_no_import(struct ahash_request *req, const void *in)
{
return -ENOSYS;
}

static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
{
struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
struct ahash_alg *alg = crypto_ahash_alg(hash);

hash->setkey = ahash_nosetkey;
hash->export = ahash_no_export;
hash->import = ahash_no_import;

if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
return crypto_init_shash_ops_async(tfm);

hash->init = alg->init;
hash->update = alg->update;
hash->final = alg->final;
hash->final = alg->final;
hash->finup = alg->finup ?: ahash_def_finup;
hash->digest = alg->digest;
hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;

if (alg->setkey)
hash->setkey = alg->setkey;
if (alg->export)
hash->export = alg->export;
if (alg->import)
hash->import = alg->import;

return 0;
}
Expand Down
52 changes: 48 additions & 4 deletions crypto/shash.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ 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;

for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
nbytes = crypto_hash_walk_done(&walk, nbytes))
nbytes = crypto_hash_walk_last(&walk) ?
crypto_shash_finup(desc, walk.data, nbytes,
req->result) :
crypto_shash_update(desc, walk.data, nbytes);

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;
Expand All @@ -252,8 +279,7 @@ int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
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;
}
Expand All @@ -270,6 +296,16 @@ 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)
{
return crypto_shash_import(ahash_request_ctx(req), in);
}

static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
{
struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
Expand All @@ -280,6 +316,7 @@ static void crypto_exit_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 crypto_ahash *crt = __crypto_ahash_cast(tfm);
struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
struct crypto_shash *shash;
Expand All @@ -298,9 +335,16 @@ 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;

if (alg->setkey)
crt->setkey = shash_async_setkey;
if (alg->export)
crt->export = shash_async_export;
if (alg->setkey)
crt->import = shash_async_import;

crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);

Expand Down
23 changes: 8 additions & 15 deletions include/crypto/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct ahash_request {
struct scatterlist *src;
u8 *result;

/* This field may only be used by the ahash API code. */
void *priv;

void *__ctx[] CRYPTO_MINALIGN_ATTR;
};

Expand Down Expand Up @@ -175,16 +178,11 @@ static inline void *ahash_request_ctx(struct ahash_request *req)
return req->__ctx;
}

static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
const u8 *key, unsigned int keylen)
{
return tfm->setkey(tfm, key, keylen);
}

static inline int crypto_ahash_digest(struct ahash_request *req)
{
return crypto_ahash_reqtfm(req)->digest(req);
}
int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
unsigned int keylen);
int crypto_ahash_finup(struct ahash_request *req);
int crypto_ahash_final(struct ahash_request *req);
int crypto_ahash_digest(struct ahash_request *req);

static inline int crypto_ahash_export(struct ahash_request *req, void *out)
{
Expand All @@ -206,11 +204,6 @@ static inline int crypto_ahash_update(struct ahash_request *req)
return crypto_ahash_reqtfm(req)->update(req);
}

static inline int crypto_ahash_final(struct ahash_request *req)
{
return crypto_ahash_reqtfm(req)->final(req);
}

static inline void ahash_request_set_tfm(struct ahash_request *req,
struct crypto_ahash *tfm)
{
Expand Down
6 changes: 6 additions & 0 deletions include/crypto/internal/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
struct crypto_hash_walk *walk,
struct scatterlist *sg, unsigned int len);

static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
{
return !(walk->entrylen | walk->total);
}

int crypto_register_ahash(struct ahash_alg *alg);
int crypto_unregister_ahash(struct ahash_alg *alg);
int ahash_register_instance(struct crypto_template *tmpl,
Expand Down Expand Up @@ -94,6 +99,7 @@ static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask);

int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);

int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
Expand Down

0 comments on commit 66f6ce5

Please sign in to comment.