1004a403cSLoc Ho /* 2004a403cSLoc Ho * Asynchronous Cryptographic Hash operations. 3004a403cSLoc Ho * 4004a403cSLoc Ho * This is the asynchronous version of hash.c with notification of 5004a403cSLoc Ho * completion via a callback. 6004a403cSLoc Ho * 7004a403cSLoc Ho * Copyright (c) 2008 Loc Ho <lho@amcc.com> 8004a403cSLoc Ho * 9004a403cSLoc Ho * This program is free software; you can redistribute it and/or modify it 10004a403cSLoc Ho * under the terms of the GNU General Public License as published by the Free 11004a403cSLoc Ho * Software Foundation; either version 2 of the License, or (at your option) 12004a403cSLoc Ho * any later version. 13004a403cSLoc Ho * 14004a403cSLoc Ho */ 15004a403cSLoc Ho 1620036252SHerbert Xu #include <crypto/internal/hash.h> 1720036252SHerbert Xu #include <crypto/scatterwalk.h> 18004a403cSLoc Ho #include <linux/err.h> 19004a403cSLoc Ho #include <linux/kernel.h> 20004a403cSLoc Ho #include <linux/module.h> 21004a403cSLoc Ho #include <linux/sched.h> 22004a403cSLoc Ho #include <linux/slab.h> 23004a403cSLoc Ho #include <linux/seq_file.h> 246238cbaeSSteffen Klassert #include <linux/cryptouser.h> 256238cbaeSSteffen Klassert #include <net/netlink.h> 26004a403cSLoc Ho 27004a403cSLoc Ho #include "internal.h" 28004a403cSLoc Ho 2966f6ce5eSHerbert Xu struct ahash_request_priv { 3066f6ce5eSHerbert Xu crypto_completion_t complete; 3166f6ce5eSHerbert Xu void *data; 3266f6ce5eSHerbert Xu u8 *result; 3366f6ce5eSHerbert Xu void *ubuf[] CRYPTO_MINALIGN_ATTR; 3466f6ce5eSHerbert Xu }; 3566f6ce5eSHerbert Xu 3688056ec3SHerbert Xu static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) 3788056ec3SHerbert Xu { 3888056ec3SHerbert Xu return container_of(crypto_hash_alg_common(hash), struct ahash_alg, 3988056ec3SHerbert Xu halg); 4088056ec3SHerbert Xu } 4188056ec3SHerbert Xu 4220036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk) 4320036252SHerbert Xu { 4420036252SHerbert Xu unsigned int alignmask = walk->alignmask; 4520036252SHerbert Xu unsigned int offset = walk->offset; 4620036252SHerbert Xu unsigned int nbytes = min(walk->entrylen, 4720036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - offset); 4820036252SHerbert Xu 4920036252SHerbert Xu walk->data = crypto_kmap(walk->pg, 0); 5020036252SHerbert Xu walk->data += offset; 5120036252SHerbert Xu 5223a75eeeSSzilveszter Ördög if (offset & alignmask) { 5323a75eeeSSzilveszter Ördög unsigned int unaligned = alignmask + 1 - (offset & alignmask); 5423a75eeeSSzilveszter Ördög if (nbytes > unaligned) 5523a75eeeSSzilveszter Ördög nbytes = unaligned; 5623a75eeeSSzilveszter Ördög } 5720036252SHerbert Xu 5820036252SHerbert Xu walk->entrylen -= nbytes; 5920036252SHerbert Xu return nbytes; 6020036252SHerbert Xu } 6120036252SHerbert Xu 6220036252SHerbert Xu static int hash_walk_new_entry(struct crypto_hash_walk *walk) 6320036252SHerbert Xu { 6420036252SHerbert Xu struct scatterlist *sg; 6520036252SHerbert Xu 6620036252SHerbert Xu sg = walk->sg; 6720036252SHerbert Xu walk->pg = sg_page(sg); 6820036252SHerbert Xu walk->offset = sg->offset; 6920036252SHerbert Xu walk->entrylen = sg->length; 7020036252SHerbert Xu 7120036252SHerbert Xu if (walk->entrylen > walk->total) 7220036252SHerbert Xu walk->entrylen = walk->total; 7320036252SHerbert Xu walk->total -= walk->entrylen; 7420036252SHerbert Xu 7520036252SHerbert Xu return hash_walk_next(walk); 7620036252SHerbert Xu } 7720036252SHerbert Xu 7820036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) 7920036252SHerbert Xu { 8020036252SHerbert Xu unsigned int alignmask = walk->alignmask; 8120036252SHerbert Xu unsigned int nbytes = walk->entrylen; 8220036252SHerbert Xu 8320036252SHerbert Xu walk->data -= walk->offset; 8420036252SHerbert Xu 8520036252SHerbert Xu if (nbytes && walk->offset & alignmask && !err) { 8620036252SHerbert Xu walk->offset = ALIGN(walk->offset, alignmask + 1); 8720036252SHerbert Xu walk->data += walk->offset; 8820036252SHerbert Xu 8920036252SHerbert Xu nbytes = min(nbytes, 9020036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - walk->offset); 9120036252SHerbert Xu walk->entrylen -= nbytes; 9220036252SHerbert Xu 9320036252SHerbert Xu return nbytes; 9420036252SHerbert Xu } 9520036252SHerbert Xu 9620036252SHerbert Xu crypto_kunmap(walk->data, 0); 9720036252SHerbert Xu crypto_yield(walk->flags); 9820036252SHerbert Xu 9920036252SHerbert Xu if (err) 10020036252SHerbert Xu return err; 10120036252SHerbert Xu 102d315a0e0SHerbert Xu if (nbytes) { 10320036252SHerbert Xu walk->offset = 0; 104d315a0e0SHerbert Xu walk->pg++; 10520036252SHerbert Xu return hash_walk_next(walk); 106d315a0e0SHerbert Xu } 10720036252SHerbert Xu 10820036252SHerbert Xu if (!walk->total) 10920036252SHerbert Xu return 0; 11020036252SHerbert Xu 11120036252SHerbert Xu walk->sg = scatterwalk_sg_next(walk->sg); 11220036252SHerbert Xu 11320036252SHerbert Xu return hash_walk_new_entry(walk); 11420036252SHerbert Xu } 11520036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done); 11620036252SHerbert Xu 11720036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req, 11820036252SHerbert Xu struct crypto_hash_walk *walk) 11920036252SHerbert Xu { 12020036252SHerbert Xu walk->total = req->nbytes; 12120036252SHerbert Xu 12220036252SHerbert Xu if (!walk->total) 12320036252SHerbert Xu return 0; 12420036252SHerbert Xu 12520036252SHerbert Xu walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); 12620036252SHerbert Xu walk->sg = req->src; 12720036252SHerbert Xu walk->flags = req->base.flags; 12820036252SHerbert Xu 12920036252SHerbert Xu return hash_walk_new_entry(walk); 13020036252SHerbert Xu } 13120036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first); 13220036252SHerbert Xu 1335f7082edSHerbert Xu int crypto_hash_walk_first_compat(struct hash_desc *hdesc, 1345f7082edSHerbert Xu struct crypto_hash_walk *walk, 1355f7082edSHerbert Xu struct scatterlist *sg, unsigned int len) 1365f7082edSHerbert Xu { 1375f7082edSHerbert Xu walk->total = len; 1385f7082edSHerbert Xu 1395f7082edSHerbert Xu if (!walk->total) 1405f7082edSHerbert Xu return 0; 1415f7082edSHerbert Xu 1425f7082edSHerbert Xu walk->alignmask = crypto_hash_alignmask(hdesc->tfm); 1435f7082edSHerbert Xu walk->sg = sg; 1445f7082edSHerbert Xu walk->flags = hdesc->flags; 1455f7082edSHerbert Xu 1465f7082edSHerbert Xu return hash_walk_new_entry(walk); 1475f7082edSHerbert Xu } 1485f7082edSHerbert Xu 149004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, 150004a403cSLoc Ho unsigned int keylen) 151004a403cSLoc Ho { 152004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 153004a403cSLoc Ho int ret; 154004a403cSLoc Ho u8 *buffer, *alignbuffer; 155004a403cSLoc Ho unsigned long absize; 156004a403cSLoc Ho 157004a403cSLoc Ho absize = keylen + alignmask; 158093900c2SHerbert Xu buffer = kmalloc(absize, GFP_KERNEL); 159004a403cSLoc Ho if (!buffer) 160004a403cSLoc Ho return -ENOMEM; 161004a403cSLoc Ho 162004a403cSLoc Ho alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 163004a403cSLoc Ho memcpy(alignbuffer, key, keylen); 164a70c5225SHerbert Xu ret = tfm->setkey(tfm, alignbuffer, keylen); 1658c32c516SHerbert Xu kzfree(buffer); 166004a403cSLoc Ho return ret; 167004a403cSLoc Ho } 168004a403cSLoc Ho 16966f6ce5eSHerbert Xu int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 170004a403cSLoc Ho unsigned int keylen) 171004a403cSLoc Ho { 172004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 173004a403cSLoc Ho 174004a403cSLoc Ho if ((unsigned long)key & alignmask) 175004a403cSLoc Ho return ahash_setkey_unaligned(tfm, key, keylen); 176004a403cSLoc Ho 177a70c5225SHerbert Xu return tfm->setkey(tfm, key, keylen); 178004a403cSLoc Ho } 17966f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_setkey); 180004a403cSLoc Ho 1813751f402SHerbert Xu static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, 1823751f402SHerbert Xu unsigned int keylen) 1833751f402SHerbert Xu { 1843751f402SHerbert Xu return -ENOSYS; 1853751f402SHerbert Xu } 1863751f402SHerbert Xu 18766f6ce5eSHerbert Xu static inline unsigned int ahash_align_buffer_size(unsigned len, 18866f6ce5eSHerbert Xu unsigned long mask) 18966f6ce5eSHerbert Xu { 19066f6ce5eSHerbert Xu return len + (mask & ~(crypto_tfm_ctx_alignment() - 1)); 19166f6ce5eSHerbert Xu } 19266f6ce5eSHerbert Xu 19366f6ce5eSHerbert Xu static void ahash_op_unaligned_finish(struct ahash_request *req, int err) 19466f6ce5eSHerbert Xu { 19566f6ce5eSHerbert Xu struct ahash_request_priv *priv = req->priv; 19666f6ce5eSHerbert Xu 19766f6ce5eSHerbert Xu if (err == -EINPROGRESS) 19866f6ce5eSHerbert Xu return; 19966f6ce5eSHerbert Xu 20066f6ce5eSHerbert Xu if (!err) 20166f6ce5eSHerbert Xu memcpy(priv->result, req->result, 20266f6ce5eSHerbert Xu crypto_ahash_digestsize(crypto_ahash_reqtfm(req))); 20366f6ce5eSHerbert Xu 20466f6ce5eSHerbert Xu kzfree(priv); 20566f6ce5eSHerbert Xu } 20666f6ce5eSHerbert Xu 20766f6ce5eSHerbert Xu static void ahash_op_unaligned_done(struct crypto_async_request *req, int err) 20866f6ce5eSHerbert Xu { 20966f6ce5eSHerbert Xu struct ahash_request *areq = req->data; 21066f6ce5eSHerbert Xu struct ahash_request_priv *priv = areq->priv; 21166f6ce5eSHerbert Xu crypto_completion_t complete = priv->complete; 21266f6ce5eSHerbert Xu void *data = priv->data; 21366f6ce5eSHerbert Xu 21466f6ce5eSHerbert Xu ahash_op_unaligned_finish(areq, err); 21566f6ce5eSHerbert Xu 21666f6ce5eSHerbert Xu complete(data, err); 21766f6ce5eSHerbert Xu } 21866f6ce5eSHerbert Xu 21966f6ce5eSHerbert Xu static int ahash_op_unaligned(struct ahash_request *req, 22066f6ce5eSHerbert Xu int (*op)(struct ahash_request *)) 22166f6ce5eSHerbert Xu { 22266f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 22366f6ce5eSHerbert Xu unsigned long alignmask = crypto_ahash_alignmask(tfm); 22466f6ce5eSHerbert Xu unsigned int ds = crypto_ahash_digestsize(tfm); 22566f6ce5eSHerbert Xu struct ahash_request_priv *priv; 22666f6ce5eSHerbert Xu int err; 22766f6ce5eSHerbert Xu 22866f6ce5eSHerbert Xu priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask), 22966f6ce5eSHerbert Xu (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? 2305befbd5aSSteffen Klassert GFP_KERNEL : GFP_ATOMIC); 23166f6ce5eSHerbert Xu if (!priv) 23266f6ce5eSHerbert Xu return -ENOMEM; 23366f6ce5eSHerbert Xu 23466f6ce5eSHerbert Xu priv->result = req->result; 23566f6ce5eSHerbert Xu priv->complete = req->base.complete; 23666f6ce5eSHerbert Xu priv->data = req->base.data; 23766f6ce5eSHerbert Xu 23866f6ce5eSHerbert Xu req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1); 23966f6ce5eSHerbert Xu req->base.complete = ahash_op_unaligned_done; 24066f6ce5eSHerbert Xu req->base.data = req; 24166f6ce5eSHerbert Xu req->priv = priv; 24266f6ce5eSHerbert Xu 24366f6ce5eSHerbert Xu err = op(req); 24466f6ce5eSHerbert Xu ahash_op_unaligned_finish(req, err); 24566f6ce5eSHerbert Xu 24666f6ce5eSHerbert Xu return err; 24766f6ce5eSHerbert Xu } 24866f6ce5eSHerbert Xu 24966f6ce5eSHerbert Xu static int crypto_ahash_op(struct ahash_request *req, 25066f6ce5eSHerbert Xu int (*op)(struct ahash_request *)) 25166f6ce5eSHerbert Xu { 25266f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 25366f6ce5eSHerbert Xu unsigned long alignmask = crypto_ahash_alignmask(tfm); 25466f6ce5eSHerbert Xu 25566f6ce5eSHerbert Xu if ((unsigned long)req->result & alignmask) 25666f6ce5eSHerbert Xu return ahash_op_unaligned(req, op); 25766f6ce5eSHerbert Xu 25866f6ce5eSHerbert Xu return op(req); 25966f6ce5eSHerbert Xu } 26066f6ce5eSHerbert Xu 26166f6ce5eSHerbert Xu int crypto_ahash_final(struct ahash_request *req) 26266f6ce5eSHerbert Xu { 26366f6ce5eSHerbert Xu return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final); 26466f6ce5eSHerbert Xu } 26566f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_final); 26666f6ce5eSHerbert Xu 26766f6ce5eSHerbert Xu int crypto_ahash_finup(struct ahash_request *req) 26866f6ce5eSHerbert Xu { 26966f6ce5eSHerbert Xu return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup); 27066f6ce5eSHerbert Xu } 27166f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_finup); 27266f6ce5eSHerbert Xu 27366f6ce5eSHerbert Xu int crypto_ahash_digest(struct ahash_request *req) 27466f6ce5eSHerbert Xu { 27566f6ce5eSHerbert Xu return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest); 27666f6ce5eSHerbert Xu } 27766f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_digest); 27866f6ce5eSHerbert Xu 27966f6ce5eSHerbert Xu static void ahash_def_finup_finish2(struct ahash_request *req, int err) 28066f6ce5eSHerbert Xu { 28166f6ce5eSHerbert Xu struct ahash_request_priv *priv = req->priv; 28266f6ce5eSHerbert Xu 28366f6ce5eSHerbert Xu if (err == -EINPROGRESS) 28466f6ce5eSHerbert Xu return; 28566f6ce5eSHerbert Xu 28666f6ce5eSHerbert Xu if (!err) 28766f6ce5eSHerbert Xu memcpy(priv->result, req->result, 28866f6ce5eSHerbert Xu crypto_ahash_digestsize(crypto_ahash_reqtfm(req))); 28966f6ce5eSHerbert Xu 29066f6ce5eSHerbert Xu kzfree(priv); 29166f6ce5eSHerbert Xu } 29266f6ce5eSHerbert Xu 29366f6ce5eSHerbert Xu static void ahash_def_finup_done2(struct crypto_async_request *req, int err) 29466f6ce5eSHerbert Xu { 29566f6ce5eSHerbert Xu struct ahash_request *areq = req->data; 29666f6ce5eSHerbert Xu struct ahash_request_priv *priv = areq->priv; 29766f6ce5eSHerbert Xu crypto_completion_t complete = priv->complete; 29866f6ce5eSHerbert Xu void *data = priv->data; 29966f6ce5eSHerbert Xu 30066f6ce5eSHerbert Xu ahash_def_finup_finish2(areq, err); 30166f6ce5eSHerbert Xu 30266f6ce5eSHerbert Xu complete(data, err); 30366f6ce5eSHerbert Xu } 30466f6ce5eSHerbert Xu 30566f6ce5eSHerbert Xu static int ahash_def_finup_finish1(struct ahash_request *req, int err) 30666f6ce5eSHerbert Xu { 30766f6ce5eSHerbert Xu if (err) 30866f6ce5eSHerbert Xu goto out; 30966f6ce5eSHerbert Xu 31066f6ce5eSHerbert Xu req->base.complete = ahash_def_finup_done2; 31166f6ce5eSHerbert Xu req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 31266f6ce5eSHerbert Xu err = crypto_ahash_reqtfm(req)->final(req); 31366f6ce5eSHerbert Xu 31466f6ce5eSHerbert Xu out: 31566f6ce5eSHerbert Xu ahash_def_finup_finish2(req, err); 31666f6ce5eSHerbert Xu return err; 31766f6ce5eSHerbert Xu } 31866f6ce5eSHerbert Xu 31966f6ce5eSHerbert Xu static void ahash_def_finup_done1(struct crypto_async_request *req, int err) 32066f6ce5eSHerbert Xu { 32166f6ce5eSHerbert Xu struct ahash_request *areq = req->data; 32266f6ce5eSHerbert Xu struct ahash_request_priv *priv = areq->priv; 32366f6ce5eSHerbert Xu crypto_completion_t complete = priv->complete; 32466f6ce5eSHerbert Xu void *data = priv->data; 32566f6ce5eSHerbert Xu 32666f6ce5eSHerbert Xu err = ahash_def_finup_finish1(areq, err); 32766f6ce5eSHerbert Xu 32866f6ce5eSHerbert Xu complete(data, err); 32966f6ce5eSHerbert Xu } 33066f6ce5eSHerbert Xu 33166f6ce5eSHerbert Xu static int ahash_def_finup(struct ahash_request *req) 33266f6ce5eSHerbert Xu { 33366f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 33466f6ce5eSHerbert Xu unsigned long alignmask = crypto_ahash_alignmask(tfm); 33566f6ce5eSHerbert Xu unsigned int ds = crypto_ahash_digestsize(tfm); 33666f6ce5eSHerbert Xu struct ahash_request_priv *priv; 33766f6ce5eSHerbert Xu 33866f6ce5eSHerbert Xu priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask), 33966f6ce5eSHerbert Xu (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? 3405befbd5aSSteffen Klassert GFP_KERNEL : GFP_ATOMIC); 34166f6ce5eSHerbert Xu if (!priv) 34266f6ce5eSHerbert Xu return -ENOMEM; 34366f6ce5eSHerbert Xu 34466f6ce5eSHerbert Xu priv->result = req->result; 34566f6ce5eSHerbert Xu priv->complete = req->base.complete; 34666f6ce5eSHerbert Xu priv->data = req->base.data; 34766f6ce5eSHerbert Xu 34866f6ce5eSHerbert Xu req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1); 34966f6ce5eSHerbert Xu req->base.complete = ahash_def_finup_done1; 35066f6ce5eSHerbert Xu req->base.data = req; 35166f6ce5eSHerbert Xu req->priv = priv; 35266f6ce5eSHerbert Xu 35366f6ce5eSHerbert Xu return ahash_def_finup_finish1(req, tfm->update(req)); 35466f6ce5eSHerbert Xu } 35566f6ce5eSHerbert Xu 35666f6ce5eSHerbert Xu static int ahash_no_export(struct ahash_request *req, void *out) 35766f6ce5eSHerbert Xu { 35866f6ce5eSHerbert Xu return -ENOSYS; 35966f6ce5eSHerbert Xu } 36066f6ce5eSHerbert Xu 36166f6ce5eSHerbert Xu static int ahash_no_import(struct ahash_request *req, const void *in) 36266f6ce5eSHerbert Xu { 36366f6ce5eSHerbert Xu return -ENOSYS; 36466f6ce5eSHerbert Xu } 36566f6ce5eSHerbert Xu 36688056ec3SHerbert Xu static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) 36788056ec3SHerbert Xu { 36888056ec3SHerbert Xu struct crypto_ahash *hash = __crypto_ahash_cast(tfm); 36988056ec3SHerbert Xu struct ahash_alg *alg = crypto_ahash_alg(hash); 37088056ec3SHerbert Xu 37166f6ce5eSHerbert Xu hash->setkey = ahash_nosetkey; 37266f6ce5eSHerbert Xu hash->export = ahash_no_export; 37366f6ce5eSHerbert Xu hash->import = ahash_no_import; 37466f6ce5eSHerbert Xu 37588056ec3SHerbert Xu if (tfm->__crt_alg->cra_type != &crypto_ahash_type) 37688056ec3SHerbert Xu return crypto_init_shash_ops_async(tfm); 37788056ec3SHerbert Xu 37888056ec3SHerbert Xu hash->init = alg->init; 37988056ec3SHerbert Xu hash->update = alg->update; 38088056ec3SHerbert Xu hash->final = alg->final; 38166f6ce5eSHerbert Xu hash->finup = alg->finup ?: ahash_def_finup; 38288056ec3SHerbert Xu hash->digest = alg->digest; 38366f6ce5eSHerbert Xu 38466f6ce5eSHerbert Xu if (alg->setkey) 38566f6ce5eSHerbert Xu hash->setkey = alg->setkey; 38666f6ce5eSHerbert Xu if (alg->export) 38766f6ce5eSHerbert Xu hash->export = alg->export; 38866f6ce5eSHerbert Xu if (alg->import) 38966f6ce5eSHerbert Xu hash->import = alg->import; 39088056ec3SHerbert Xu 39188056ec3SHerbert Xu return 0; 39288056ec3SHerbert Xu } 39388056ec3SHerbert Xu 39488056ec3SHerbert Xu static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) 39588056ec3SHerbert Xu { 39688056ec3SHerbert Xu if (alg->cra_type == &crypto_ahash_type) 39788056ec3SHerbert Xu return alg->cra_ctxsize; 39888056ec3SHerbert Xu 39988056ec3SHerbert Xu return sizeof(struct crypto_shash *); 40088056ec3SHerbert Xu } 40188056ec3SHerbert Xu 402*3acc8473SHerbert Xu #ifdef CONFIG_NET 4036238cbaeSSteffen Klassert static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) 4046238cbaeSSteffen Klassert { 4056238cbaeSSteffen Klassert struct crypto_report_hash rhash; 4066238cbaeSSteffen Klassert 4076238cbaeSSteffen Klassert snprintf(rhash.type, CRYPTO_MAX_ALG_NAME, "%s", "ahash"); 4086238cbaeSSteffen Klassert 4096238cbaeSSteffen Klassert rhash.blocksize = alg->cra_blocksize; 4106238cbaeSSteffen Klassert rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize; 4116238cbaeSSteffen Klassert 4126238cbaeSSteffen Klassert NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH, 4136238cbaeSSteffen Klassert sizeof(struct crypto_report_hash), &rhash); 4146238cbaeSSteffen Klassert 4156238cbaeSSteffen Klassert return 0; 4166238cbaeSSteffen Klassert 4176238cbaeSSteffen Klassert nla_put_failure: 4186238cbaeSSteffen Klassert return -EMSGSIZE; 4196238cbaeSSteffen Klassert } 420*3acc8473SHerbert Xu #else 421*3acc8473SHerbert Xu static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) 422*3acc8473SHerbert Xu { 423*3acc8473SHerbert Xu return -ENOSYS; 424*3acc8473SHerbert Xu } 425*3acc8473SHerbert Xu #endif 4266238cbaeSSteffen Klassert 427004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 428004a403cSLoc Ho __attribute__ ((unused)); 429004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 430004a403cSLoc Ho { 431004a403cSLoc Ho seq_printf(m, "type : ahash\n"); 432004a403cSLoc Ho seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? 433004a403cSLoc Ho "yes" : "no"); 434004a403cSLoc Ho seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 43588056ec3SHerbert Xu seq_printf(m, "digestsize : %u\n", 43688056ec3SHerbert Xu __crypto_hash_alg_common(alg)->digestsize); 437004a403cSLoc Ho } 438004a403cSLoc Ho 439004a403cSLoc Ho const struct crypto_type crypto_ahash_type = { 44088056ec3SHerbert Xu .extsize = crypto_ahash_extsize, 44188056ec3SHerbert Xu .init_tfm = crypto_ahash_init_tfm, 442004a403cSLoc Ho #ifdef CONFIG_PROC_FS 443004a403cSLoc Ho .show = crypto_ahash_show, 444004a403cSLoc Ho #endif 4456238cbaeSSteffen Klassert .report = crypto_ahash_report, 44688056ec3SHerbert Xu .maskclear = ~CRYPTO_ALG_TYPE_MASK, 44788056ec3SHerbert Xu .maskset = CRYPTO_ALG_TYPE_AHASH_MASK, 44888056ec3SHerbert Xu .type = CRYPTO_ALG_TYPE_AHASH, 44988056ec3SHerbert Xu .tfmsize = offsetof(struct crypto_ahash, base), 450004a403cSLoc Ho }; 451004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type); 452004a403cSLoc Ho 45388056ec3SHerbert Xu struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, 45488056ec3SHerbert Xu u32 mask) 45588056ec3SHerbert Xu { 45688056ec3SHerbert Xu return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask); 45788056ec3SHerbert Xu } 45888056ec3SHerbert Xu EXPORT_SYMBOL_GPL(crypto_alloc_ahash); 45988056ec3SHerbert Xu 46001c2deceSHerbert Xu static int ahash_prepare_alg(struct ahash_alg *alg) 46101c2deceSHerbert Xu { 46201c2deceSHerbert Xu struct crypto_alg *base = &alg->halg.base; 46301c2deceSHerbert Xu 46401c2deceSHerbert Xu if (alg->halg.digestsize > PAGE_SIZE / 8 || 46501c2deceSHerbert Xu alg->halg.statesize > PAGE_SIZE / 8) 46601c2deceSHerbert Xu return -EINVAL; 46701c2deceSHerbert Xu 46801c2deceSHerbert Xu base->cra_type = &crypto_ahash_type; 46901c2deceSHerbert Xu base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; 47001c2deceSHerbert Xu base->cra_flags |= CRYPTO_ALG_TYPE_AHASH; 47101c2deceSHerbert Xu 47201c2deceSHerbert Xu return 0; 47301c2deceSHerbert Xu } 47401c2deceSHerbert Xu 47501c2deceSHerbert Xu int crypto_register_ahash(struct ahash_alg *alg) 47601c2deceSHerbert Xu { 47701c2deceSHerbert Xu struct crypto_alg *base = &alg->halg.base; 47801c2deceSHerbert Xu int err; 47901c2deceSHerbert Xu 48001c2deceSHerbert Xu err = ahash_prepare_alg(alg); 48101c2deceSHerbert Xu if (err) 48201c2deceSHerbert Xu return err; 48301c2deceSHerbert Xu 48401c2deceSHerbert Xu return crypto_register_alg(base); 48501c2deceSHerbert Xu } 48601c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_register_ahash); 48701c2deceSHerbert Xu 48801c2deceSHerbert Xu int crypto_unregister_ahash(struct ahash_alg *alg) 48901c2deceSHerbert Xu { 49001c2deceSHerbert Xu return crypto_unregister_alg(&alg->halg.base); 49101c2deceSHerbert Xu } 49201c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_unregister_ahash); 49301c2deceSHerbert Xu 49401c2deceSHerbert Xu int ahash_register_instance(struct crypto_template *tmpl, 49501c2deceSHerbert Xu struct ahash_instance *inst) 49601c2deceSHerbert Xu { 49701c2deceSHerbert Xu int err; 49801c2deceSHerbert Xu 49901c2deceSHerbert Xu err = ahash_prepare_alg(&inst->alg); 50001c2deceSHerbert Xu if (err) 50101c2deceSHerbert Xu return err; 50201c2deceSHerbert Xu 50301c2deceSHerbert Xu return crypto_register_instance(tmpl, ahash_crypto_instance(inst)); 50401c2deceSHerbert Xu } 50501c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_register_instance); 50601c2deceSHerbert Xu 50701c2deceSHerbert Xu void ahash_free_instance(struct crypto_instance *inst) 50801c2deceSHerbert Xu { 50901c2deceSHerbert Xu crypto_drop_spawn(crypto_instance_ctx(inst)); 51001c2deceSHerbert Xu kfree(ahash_instance(inst)); 51101c2deceSHerbert Xu } 51201c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_free_instance); 51301c2deceSHerbert Xu 51401c2deceSHerbert Xu int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn, 51501c2deceSHerbert Xu struct hash_alg_common *alg, 51601c2deceSHerbert Xu struct crypto_instance *inst) 51701c2deceSHerbert Xu { 51801c2deceSHerbert Xu return crypto_init_spawn2(&spawn->base, &alg->base, inst, 51901c2deceSHerbert Xu &crypto_ahash_type); 52001c2deceSHerbert Xu } 52101c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn); 52201c2deceSHerbert Xu 52301c2deceSHerbert Xu struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask) 52401c2deceSHerbert Xu { 52501c2deceSHerbert Xu struct crypto_alg *alg; 52601c2deceSHerbert Xu 52701c2deceSHerbert Xu alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask); 52801c2deceSHerbert Xu return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg); 52901c2deceSHerbert Xu } 53001c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_attr_alg); 53101c2deceSHerbert Xu 532004a403cSLoc Ho MODULE_LICENSE("GPL"); 533004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); 534