12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2004a403cSLoc Ho /* 3004a403cSLoc Ho * Asynchronous Cryptographic Hash operations. 4004a403cSLoc Ho * 5004a403cSLoc Ho * This is the asynchronous version of hash.c with notification of 6004a403cSLoc Ho * completion via a callback. 7004a403cSLoc Ho * 8004a403cSLoc Ho * Copyright (c) 2008 Loc Ho <lho@amcc.com> 9004a403cSLoc Ho */ 10004a403cSLoc Ho 1120036252SHerbert Xu #include <crypto/internal/hash.h> 1220036252SHerbert Xu #include <crypto/scatterwalk.h> 13004a403cSLoc Ho #include <linux/err.h> 14004a403cSLoc Ho #include <linux/kernel.h> 15004a403cSLoc Ho #include <linux/module.h> 16004a403cSLoc Ho #include <linux/sched.h> 17004a403cSLoc Ho #include <linux/slab.h> 18004a403cSLoc Ho #include <linux/seq_file.h> 196238cbaeSSteffen Klassert #include <linux/cryptouser.h> 20d8c34b94SGideon Israel Dsouza #include <linux/compiler.h> 216238cbaeSSteffen Klassert #include <net/netlink.h> 22004a403cSLoc Ho 23004a403cSLoc Ho #include "internal.h" 24004a403cSLoc Ho 256d1b41fcSEric Biggers static const struct crypto_type crypto_ahash_type; 266d1b41fcSEric Biggers 2766f6ce5eSHerbert Xu struct ahash_request_priv { 2866f6ce5eSHerbert Xu crypto_completion_t complete; 2966f6ce5eSHerbert Xu void *data; 3066f6ce5eSHerbert Xu u8 *result; 31ef0579b6SHerbert Xu u32 flags; 3266f6ce5eSHerbert Xu void *ubuf[] CRYPTO_MINALIGN_ATTR; 3366f6ce5eSHerbert Xu }; 3466f6ce5eSHerbert Xu 3588056ec3SHerbert Xu static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) 3688056ec3SHerbert Xu { 3788056ec3SHerbert Xu return container_of(crypto_hash_alg_common(hash), struct ahash_alg, 3888056ec3SHerbert Xu halg); 3988056ec3SHerbert Xu } 4088056ec3SHerbert Xu 4120036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk) 4220036252SHerbert Xu { 4320036252SHerbert Xu unsigned int alignmask = walk->alignmask; 4420036252SHerbert Xu unsigned int offset = walk->offset; 4520036252SHerbert Xu unsigned int nbytes = min(walk->entrylen, 4620036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - offset); 4720036252SHerbert Xu 48*aa969515SArd Biesheuvel walk->data = kmap_local_page(walk->pg); 4920036252SHerbert Xu walk->data += offset; 5020036252SHerbert Xu 5123a75eeeSSzilveszter Ördög if (offset & alignmask) { 5223a75eeeSSzilveszter Ördög unsigned int unaligned = alignmask + 1 - (offset & alignmask); 53b516d514SJoshua I. James 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->offset = sg->offset; 6813f4bb78SHerbert Xu walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT); 6913f4bb78SHerbert Xu walk->offset = offset_in_page(walk->offset); 7020036252SHerbert Xu walk->entrylen = sg->length; 7120036252SHerbert Xu 7220036252SHerbert Xu if (walk->entrylen > walk->total) 7320036252SHerbert Xu walk->entrylen = walk->total; 7420036252SHerbert Xu walk->total -= walk->entrylen; 7520036252SHerbert Xu 7620036252SHerbert Xu return hash_walk_next(walk); 7720036252SHerbert Xu } 7820036252SHerbert Xu 7920036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) 8020036252SHerbert Xu { 8120036252SHerbert Xu unsigned int alignmask = walk->alignmask; 8220036252SHerbert Xu 8320036252SHerbert Xu walk->data -= walk->offset; 8420036252SHerbert Xu 8577568e53SEric Biggers if (walk->entrylen && (walk->offset & alignmask) && !err) { 8677568e53SEric Biggers unsigned int nbytes; 8720036252SHerbert Xu 8877568e53SEric Biggers walk->offset = ALIGN(walk->offset, alignmask + 1); 8977568e53SEric Biggers nbytes = min(walk->entrylen, 9077568e53SEric Biggers (unsigned int)(PAGE_SIZE - walk->offset)); 91900a081fSHerbert Xu if (nbytes) { 9277568e53SEric Biggers walk->entrylen -= nbytes; 93900a081fSHerbert Xu walk->data += walk->offset; 9420036252SHerbert Xu return nbytes; 9520036252SHerbert Xu } 96900a081fSHerbert Xu } 9720036252SHerbert Xu 98*aa969515SArd Biesheuvel kunmap_local(walk->data); 9920036252SHerbert Xu crypto_yield(walk->flags); 10020036252SHerbert Xu 10120036252SHerbert Xu if (err) 10220036252SHerbert Xu return err; 10320036252SHerbert Xu 10477568e53SEric Biggers if (walk->entrylen) { 10520036252SHerbert Xu walk->offset = 0; 106d315a0e0SHerbert Xu walk->pg++; 10720036252SHerbert Xu return hash_walk_next(walk); 108d315a0e0SHerbert Xu } 10920036252SHerbert Xu 11020036252SHerbert Xu if (!walk->total) 11120036252SHerbert Xu return 0; 11220036252SHerbert Xu 1135be4d4c9SCristian Stoica walk->sg = sg_next(walk->sg); 11420036252SHerbert Xu 11520036252SHerbert Xu return hash_walk_new_entry(walk); 11620036252SHerbert Xu } 11720036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done); 11820036252SHerbert Xu 11920036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req, 12020036252SHerbert Xu struct crypto_hash_walk *walk) 12120036252SHerbert Xu { 12220036252SHerbert Xu walk->total = req->nbytes; 12320036252SHerbert Xu 1246d9529c5STim Chen if (!walk->total) { 1256d9529c5STim Chen walk->entrylen = 0; 12620036252SHerbert Xu return 0; 1276d9529c5STim Chen } 12820036252SHerbert Xu 12920036252SHerbert Xu walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); 13020036252SHerbert Xu walk->sg = req->src; 1318afa25aaSIra Weiny walk->flags = req->base.flags; 13220036252SHerbert Xu 13320036252SHerbert Xu return hash_walk_new_entry(walk); 13420036252SHerbert Xu } 13520036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first); 13620036252SHerbert Xu 137004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, 138004a403cSLoc Ho unsigned int keylen) 139004a403cSLoc Ho { 140004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 141004a403cSLoc Ho int ret; 142004a403cSLoc Ho u8 *buffer, *alignbuffer; 143004a403cSLoc Ho unsigned long absize; 144004a403cSLoc Ho 145004a403cSLoc Ho absize = keylen + alignmask; 146093900c2SHerbert Xu buffer = kmalloc(absize, GFP_KERNEL); 147004a403cSLoc Ho if (!buffer) 148004a403cSLoc Ho return -ENOMEM; 149004a403cSLoc Ho 150004a403cSLoc Ho alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 151004a403cSLoc Ho memcpy(alignbuffer, key, keylen); 152a70c5225SHerbert Xu ret = tfm->setkey(tfm, alignbuffer, keylen); 153453431a5SWaiman Long kfree_sensitive(buffer); 154004a403cSLoc Ho return ret; 155004a403cSLoc Ho } 156004a403cSLoc Ho 157ba7d7433SEric Biggers static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, 158ba7d7433SEric Biggers unsigned int keylen) 159ba7d7433SEric Biggers { 160ba7d7433SEric Biggers return -ENOSYS; 161ba7d7433SEric Biggers } 162ba7d7433SEric Biggers 163ba7d7433SEric Biggers static void ahash_set_needkey(struct crypto_ahash *tfm) 164ba7d7433SEric Biggers { 165ba7d7433SEric Biggers const struct hash_alg_common *alg = crypto_hash_alg_common(tfm); 166ba7d7433SEric Biggers 167ba7d7433SEric Biggers if (tfm->setkey != ahash_nosetkey && 168ba7d7433SEric Biggers !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY)) 169ba7d7433SEric Biggers crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY); 170ba7d7433SEric Biggers } 171ba7d7433SEric Biggers 17266f6ce5eSHerbert Xu int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 173004a403cSLoc Ho unsigned int keylen) 174004a403cSLoc Ho { 175004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 1769fa68f62SEric Biggers int err; 177004a403cSLoc Ho 178004a403cSLoc Ho if ((unsigned long)key & alignmask) 1799fa68f62SEric Biggers err = ahash_setkey_unaligned(tfm, key, keylen); 1809fa68f62SEric Biggers else 1819fa68f62SEric Biggers err = tfm->setkey(tfm, key, keylen); 182004a403cSLoc Ho 183ba7d7433SEric Biggers if (unlikely(err)) { 184ba7d7433SEric Biggers ahash_set_needkey(tfm); 1859fa68f62SEric Biggers return err; 186ba7d7433SEric Biggers } 1879fa68f62SEric Biggers 1889fa68f62SEric Biggers crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); 1899fa68f62SEric Biggers return 0; 190004a403cSLoc Ho } 19166f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_setkey); 192004a403cSLoc Ho 19366f6ce5eSHerbert Xu static inline unsigned int ahash_align_buffer_size(unsigned len, 19466f6ce5eSHerbert Xu unsigned long mask) 19566f6ce5eSHerbert Xu { 19666f6ce5eSHerbert Xu return len + (mask & ~(crypto_tfm_ctx_alignment() - 1)); 19766f6ce5eSHerbert Xu } 19866f6ce5eSHerbert Xu 1991ffc9fbdSMarek Vasut static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt) 20066f6ce5eSHerbert Xu { 20166f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 20266f6ce5eSHerbert Xu unsigned long alignmask = crypto_ahash_alignmask(tfm); 20366f6ce5eSHerbert Xu unsigned int ds = crypto_ahash_digestsize(tfm); 20466f6ce5eSHerbert Xu struct ahash_request_priv *priv; 20566f6ce5eSHerbert Xu 20666f6ce5eSHerbert Xu priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask), 20766f6ce5eSHerbert Xu (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? 2085befbd5aSSteffen Klassert GFP_KERNEL : GFP_ATOMIC); 20966f6ce5eSHerbert Xu if (!priv) 21066f6ce5eSHerbert Xu return -ENOMEM; 21166f6ce5eSHerbert Xu 212ab6bf4e5SMarek Vasut /* 213ab6bf4e5SMarek Vasut * WARNING: Voodoo programming below! 214ab6bf4e5SMarek Vasut * 215ab6bf4e5SMarek Vasut * The code below is obscure and hard to understand, thus explanation 216ab6bf4e5SMarek Vasut * is necessary. See include/crypto/hash.h and include/linux/crypto.h 217ab6bf4e5SMarek Vasut * to understand the layout of structures used here! 218ab6bf4e5SMarek Vasut * 219ab6bf4e5SMarek Vasut * The code here will replace portions of the ORIGINAL request with 220ab6bf4e5SMarek Vasut * pointers to new code and buffers so the hashing operation can store 221ab6bf4e5SMarek Vasut * the result in aligned buffer. We will call the modified request 222ab6bf4e5SMarek Vasut * an ADJUSTED request. 223ab6bf4e5SMarek Vasut * 224ab6bf4e5SMarek Vasut * The newly mangled request will look as such: 225ab6bf4e5SMarek Vasut * 226ab6bf4e5SMarek Vasut * req { 227ab6bf4e5SMarek Vasut * .result = ADJUSTED[new aligned buffer] 228ab6bf4e5SMarek Vasut * .base.complete = ADJUSTED[pointer to completion function] 229ab6bf4e5SMarek Vasut * .base.data = ADJUSTED[*req (pointer to self)] 230ab6bf4e5SMarek Vasut * .priv = ADJUSTED[new priv] { 231ab6bf4e5SMarek Vasut * .result = ORIGINAL(result) 232ab6bf4e5SMarek Vasut * .complete = ORIGINAL(base.complete) 233ab6bf4e5SMarek Vasut * .data = ORIGINAL(base.data) 234ab6bf4e5SMarek Vasut * } 235ab6bf4e5SMarek Vasut */ 236ab6bf4e5SMarek Vasut 23766f6ce5eSHerbert Xu priv->result = req->result; 23866f6ce5eSHerbert Xu priv->complete = req->base.complete; 23966f6ce5eSHerbert Xu priv->data = req->base.data; 240ef0579b6SHerbert Xu priv->flags = req->base.flags; 241ef0579b6SHerbert Xu 242ab6bf4e5SMarek Vasut /* 243ab6bf4e5SMarek Vasut * WARNING: We do not backup req->priv here! The req->priv 244ab6bf4e5SMarek Vasut * is for internal use of the Crypto API and the 245ab6bf4e5SMarek Vasut * user must _NOT_ _EVER_ depend on it's content! 246ab6bf4e5SMarek Vasut */ 24766f6ce5eSHerbert Xu 24866f6ce5eSHerbert Xu req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1); 2491ffc9fbdSMarek Vasut req->base.complete = cplt; 25066f6ce5eSHerbert Xu req->base.data = req; 25166f6ce5eSHerbert Xu req->priv = priv; 25266f6ce5eSHerbert Xu 2531ffc9fbdSMarek Vasut return 0; 2541ffc9fbdSMarek Vasut } 2551ffc9fbdSMarek Vasut 256ef0579b6SHerbert Xu static void ahash_restore_req(struct ahash_request *req, int err) 2571ffc9fbdSMarek Vasut { 2581ffc9fbdSMarek Vasut struct ahash_request_priv *priv = req->priv; 2591ffc9fbdSMarek Vasut 260ef0579b6SHerbert Xu if (!err) 261ef0579b6SHerbert Xu memcpy(priv->result, req->result, 262ef0579b6SHerbert Xu crypto_ahash_digestsize(crypto_ahash_reqtfm(req))); 263ef0579b6SHerbert Xu 2641ffc9fbdSMarek Vasut /* Restore the original crypto request. */ 2651ffc9fbdSMarek Vasut req->result = priv->result; 266ef0579b6SHerbert Xu 267ef0579b6SHerbert Xu ahash_request_set_callback(req, priv->flags, 268ef0579b6SHerbert Xu priv->complete, priv->data); 2691ffc9fbdSMarek Vasut req->priv = NULL; 2701ffc9fbdSMarek Vasut 2711ffc9fbdSMarek Vasut /* Free the req->priv.priv from the ADJUSTED request. */ 272453431a5SWaiman Long kfree_sensitive(priv); 2731ffc9fbdSMarek Vasut } 2741ffc9fbdSMarek Vasut 275ef0579b6SHerbert Xu static void ahash_notify_einprogress(struct ahash_request *req) 2761ffc9fbdSMarek Vasut { 2771ffc9fbdSMarek Vasut struct ahash_request_priv *priv = req->priv; 278ef0579b6SHerbert Xu struct crypto_async_request oreq; 2791ffc9fbdSMarek Vasut 280ef0579b6SHerbert Xu oreq.data = priv->data; 2811ffc9fbdSMarek Vasut 282ef0579b6SHerbert Xu priv->complete(&oreq, -EINPROGRESS); 2831ffc9fbdSMarek Vasut } 2841ffc9fbdSMarek Vasut 2851ffc9fbdSMarek Vasut static void ahash_op_unaligned_done(struct crypto_async_request *req, int err) 2861ffc9fbdSMarek Vasut { 2871ffc9fbdSMarek Vasut struct ahash_request *areq = req->data; 2881ffc9fbdSMarek Vasut 289ef0579b6SHerbert Xu if (err == -EINPROGRESS) { 290ef0579b6SHerbert Xu ahash_notify_einprogress(areq); 291ef0579b6SHerbert Xu return; 292ef0579b6SHerbert Xu } 293ef0579b6SHerbert Xu 2941ffc9fbdSMarek Vasut /* 2951ffc9fbdSMarek Vasut * Restore the original request, see ahash_op_unaligned() for what 2961ffc9fbdSMarek Vasut * goes where. 2971ffc9fbdSMarek Vasut * 2981ffc9fbdSMarek Vasut * The "struct ahash_request *req" here is in fact the "req.base" 2991ffc9fbdSMarek Vasut * from the ADJUSTED request from ahash_op_unaligned(), thus as it 3001ffc9fbdSMarek Vasut * is a pointer to self, it is also the ADJUSTED "req" . 3011ffc9fbdSMarek Vasut */ 3021ffc9fbdSMarek Vasut 3031ffc9fbdSMarek Vasut /* First copy req->result into req->priv.result */ 304ef0579b6SHerbert Xu ahash_restore_req(areq, err); 3051ffc9fbdSMarek Vasut 3061ffc9fbdSMarek Vasut /* Complete the ORIGINAL request. */ 3071ffc9fbdSMarek Vasut areq->base.complete(&areq->base, err); 3081ffc9fbdSMarek Vasut } 3091ffc9fbdSMarek Vasut 3101ffc9fbdSMarek Vasut static int ahash_op_unaligned(struct ahash_request *req, 3111ffc9fbdSMarek Vasut int (*op)(struct ahash_request *)) 3121ffc9fbdSMarek Vasut { 3131ffc9fbdSMarek Vasut int err; 3141ffc9fbdSMarek Vasut 3151ffc9fbdSMarek Vasut err = ahash_save_req(req, ahash_op_unaligned_done); 3161ffc9fbdSMarek Vasut if (err) 3171ffc9fbdSMarek Vasut return err; 3181ffc9fbdSMarek Vasut 31966f6ce5eSHerbert Xu err = op(req); 3204e5b0ad5SGilad Ben-Yossef if (err == -EINPROGRESS || err == -EBUSY) 321ef0579b6SHerbert Xu return err; 322ef0579b6SHerbert Xu 323ef0579b6SHerbert Xu ahash_restore_req(req, err); 32466f6ce5eSHerbert Xu 32566f6ce5eSHerbert Xu return err; 32666f6ce5eSHerbert Xu } 32766f6ce5eSHerbert Xu 32866f6ce5eSHerbert Xu static int crypto_ahash_op(struct ahash_request *req, 32966f6ce5eSHerbert Xu int (*op)(struct ahash_request *)) 33066f6ce5eSHerbert Xu { 33166f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 33266f6ce5eSHerbert Xu unsigned long alignmask = crypto_ahash_alignmask(tfm); 33366f6ce5eSHerbert Xu 33466f6ce5eSHerbert Xu if ((unsigned long)req->result & alignmask) 33566f6ce5eSHerbert Xu return ahash_op_unaligned(req, op); 33666f6ce5eSHerbert Xu 33766f6ce5eSHerbert Xu return op(req); 33866f6ce5eSHerbert Xu } 33966f6ce5eSHerbert Xu 34066f6ce5eSHerbert Xu int crypto_ahash_final(struct ahash_request *req) 34166f6ce5eSHerbert Xu { 342f7d76e05SCorentin Labbe struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 343f7d76e05SCorentin Labbe struct crypto_alg *alg = tfm->base.__crt_alg; 344f7d76e05SCorentin Labbe unsigned int nbytes = req->nbytes; 345cac5818cSCorentin Labbe int ret; 346cac5818cSCorentin Labbe 347f7d76e05SCorentin Labbe crypto_stats_get(alg); 348cac5818cSCorentin Labbe ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final); 349f7d76e05SCorentin Labbe crypto_stats_ahash_final(nbytes, ret, alg); 350cac5818cSCorentin Labbe return ret; 35166f6ce5eSHerbert Xu } 35266f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_final); 35366f6ce5eSHerbert Xu 35466f6ce5eSHerbert Xu int crypto_ahash_finup(struct ahash_request *req) 35566f6ce5eSHerbert Xu { 356f7d76e05SCorentin Labbe struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 357f7d76e05SCorentin Labbe struct crypto_alg *alg = tfm->base.__crt_alg; 358f7d76e05SCorentin Labbe unsigned int nbytes = req->nbytes; 359cac5818cSCorentin Labbe int ret; 360cac5818cSCorentin Labbe 361f7d76e05SCorentin Labbe crypto_stats_get(alg); 362cac5818cSCorentin Labbe ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup); 363f7d76e05SCorentin Labbe crypto_stats_ahash_final(nbytes, ret, alg); 364cac5818cSCorentin Labbe return ret; 36566f6ce5eSHerbert Xu } 36666f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_finup); 36766f6ce5eSHerbert Xu 36866f6ce5eSHerbert Xu int crypto_ahash_digest(struct ahash_request *req) 36966f6ce5eSHerbert Xu { 3709fa68f62SEric Biggers struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 371f7d76e05SCorentin Labbe struct crypto_alg *alg = tfm->base.__crt_alg; 372f7d76e05SCorentin Labbe unsigned int nbytes = req->nbytes; 373cac5818cSCorentin Labbe int ret; 3749fa68f62SEric Biggers 375f7d76e05SCorentin Labbe crypto_stats_get(alg); 3769fa68f62SEric Biggers if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 377cac5818cSCorentin Labbe ret = -ENOKEY; 378cac5818cSCorentin Labbe else 379cac5818cSCorentin Labbe ret = crypto_ahash_op(req, tfm->digest); 380f7d76e05SCorentin Labbe crypto_stats_ahash_final(nbytes, ret, alg); 381cac5818cSCorentin Labbe return ret; 38266f6ce5eSHerbert Xu } 38366f6ce5eSHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_digest); 38466f6ce5eSHerbert Xu 38566f6ce5eSHerbert Xu static void ahash_def_finup_done2(struct crypto_async_request *req, int err) 38666f6ce5eSHerbert Xu { 38766f6ce5eSHerbert Xu struct ahash_request *areq = req->data; 38866f6ce5eSHerbert Xu 389ef0579b6SHerbert Xu if (err == -EINPROGRESS) 390ef0579b6SHerbert Xu return; 391ef0579b6SHerbert Xu 392ef0579b6SHerbert Xu ahash_restore_req(areq, err); 39366f6ce5eSHerbert Xu 394d4a7a0fbSMarek Vasut areq->base.complete(&areq->base, err); 39566f6ce5eSHerbert Xu } 39666f6ce5eSHerbert Xu 39766f6ce5eSHerbert Xu static int ahash_def_finup_finish1(struct ahash_request *req, int err) 39866f6ce5eSHerbert Xu { 39966f6ce5eSHerbert Xu if (err) 40066f6ce5eSHerbert Xu goto out; 40166f6ce5eSHerbert Xu 40266f6ce5eSHerbert Xu req->base.complete = ahash_def_finup_done2; 403ef0579b6SHerbert Xu 40466f6ce5eSHerbert Xu err = crypto_ahash_reqtfm(req)->final(req); 4054e5b0ad5SGilad Ben-Yossef if (err == -EINPROGRESS || err == -EBUSY) 406ef0579b6SHerbert Xu return err; 40766f6ce5eSHerbert Xu 40866f6ce5eSHerbert Xu out: 409ef0579b6SHerbert Xu ahash_restore_req(req, err); 41066f6ce5eSHerbert Xu return err; 41166f6ce5eSHerbert Xu } 41266f6ce5eSHerbert Xu 41366f6ce5eSHerbert Xu static void ahash_def_finup_done1(struct crypto_async_request *req, int err) 41466f6ce5eSHerbert Xu { 41566f6ce5eSHerbert Xu struct ahash_request *areq = req->data; 41666f6ce5eSHerbert Xu 417ef0579b6SHerbert Xu if (err == -EINPROGRESS) { 418ef0579b6SHerbert Xu ahash_notify_einprogress(areq); 419ef0579b6SHerbert Xu return; 420ef0579b6SHerbert Xu } 421ef0579b6SHerbert Xu 422ef0579b6SHerbert Xu areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 423ef0579b6SHerbert Xu 42466f6ce5eSHerbert Xu err = ahash_def_finup_finish1(areq, err); 425ef0579b6SHerbert Xu if (areq->priv) 426ef0579b6SHerbert Xu return; 42766f6ce5eSHerbert Xu 428d4a7a0fbSMarek Vasut areq->base.complete(&areq->base, err); 42966f6ce5eSHerbert Xu } 43066f6ce5eSHerbert Xu 43166f6ce5eSHerbert Xu static int ahash_def_finup(struct ahash_request *req) 43266f6ce5eSHerbert Xu { 43366f6ce5eSHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 434d4a7a0fbSMarek Vasut int err; 43566f6ce5eSHerbert Xu 436d4a7a0fbSMarek Vasut err = ahash_save_req(req, ahash_def_finup_done1); 437d4a7a0fbSMarek Vasut if (err) 438d4a7a0fbSMarek Vasut return err; 43966f6ce5eSHerbert Xu 440d4a7a0fbSMarek Vasut err = tfm->update(req); 4414e5b0ad5SGilad Ben-Yossef if (err == -EINPROGRESS || err == -EBUSY) 442ef0579b6SHerbert Xu return err; 443ef0579b6SHerbert Xu 444d4a7a0fbSMarek Vasut return ahash_def_finup_finish1(req, err); 44566f6ce5eSHerbert Xu } 44666f6ce5eSHerbert Xu 447e73d340dSHerbert Xu static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm) 448e73d340dSHerbert Xu { 449e73d340dSHerbert Xu struct crypto_ahash *hash = __crypto_ahash_cast(tfm); 450e73d340dSHerbert Xu struct ahash_alg *alg = crypto_ahash_alg(hash); 451e73d340dSHerbert Xu 452e73d340dSHerbert Xu alg->exit_tfm(hash); 453e73d340dSHerbert Xu } 454e73d340dSHerbert Xu 45588056ec3SHerbert Xu static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) 45688056ec3SHerbert Xu { 45788056ec3SHerbert Xu struct crypto_ahash *hash = __crypto_ahash_cast(tfm); 45888056ec3SHerbert Xu struct ahash_alg *alg = crypto_ahash_alg(hash); 45988056ec3SHerbert Xu 46066f6ce5eSHerbert Xu hash->setkey = ahash_nosetkey; 46166f6ce5eSHerbert Xu 46288056ec3SHerbert Xu if (tfm->__crt_alg->cra_type != &crypto_ahash_type) 46388056ec3SHerbert Xu return crypto_init_shash_ops_async(tfm); 46488056ec3SHerbert Xu 46588056ec3SHerbert Xu hash->init = alg->init; 46688056ec3SHerbert Xu hash->update = alg->update; 46788056ec3SHerbert Xu hash->final = alg->final; 46866f6ce5eSHerbert Xu hash->finup = alg->finup ?: ahash_def_finup; 46988056ec3SHerbert Xu hash->digest = alg->digest; 4706f221f7eSKamil Konieczny hash->export = alg->export; 4716f221f7eSKamil Konieczny hash->import = alg->import; 47266f6ce5eSHerbert Xu 473a5596d63SHerbert Xu if (alg->setkey) { 47466f6ce5eSHerbert Xu hash->setkey = alg->setkey; 475ba7d7433SEric Biggers ahash_set_needkey(hash); 476a5596d63SHerbert Xu } 47788056ec3SHerbert Xu 478e73d340dSHerbert Xu if (alg->exit_tfm) 479e73d340dSHerbert Xu tfm->exit = crypto_ahash_exit_tfm; 480e73d340dSHerbert Xu 481e73d340dSHerbert Xu return alg->init_tfm ? alg->init_tfm(hash) : 0; 48288056ec3SHerbert Xu } 48388056ec3SHerbert Xu 48488056ec3SHerbert Xu static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) 48588056ec3SHerbert Xu { 4862495cf25SHerbert Xu if (alg->cra_type != &crypto_ahash_type) 48788056ec3SHerbert Xu return sizeof(struct crypto_shash *); 4882495cf25SHerbert Xu 4892495cf25SHerbert Xu return crypto_alg_extsize(alg); 49088056ec3SHerbert Xu } 49188056ec3SHerbert Xu 49248fb3e57SEric Biggers static void crypto_ahash_free_instance(struct crypto_instance *inst) 49348fb3e57SEric Biggers { 49448fb3e57SEric Biggers struct ahash_instance *ahash = ahash_instance(inst); 49548fb3e57SEric Biggers 49648fb3e57SEric Biggers ahash->free(ahash); 49748fb3e57SEric Biggers } 49848fb3e57SEric Biggers 4993acc8473SHerbert Xu #ifdef CONFIG_NET 5006238cbaeSSteffen Klassert static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) 5016238cbaeSSteffen Klassert { 5026238cbaeSSteffen Klassert struct crypto_report_hash rhash; 5036238cbaeSSteffen Klassert 50437db69e0SEric Biggers memset(&rhash, 0, sizeof(rhash)); 50537db69e0SEric Biggers 50637db69e0SEric Biggers strscpy(rhash.type, "ahash", sizeof(rhash.type)); 5076238cbaeSSteffen Klassert 5086238cbaeSSteffen Klassert rhash.blocksize = alg->cra_blocksize; 5096238cbaeSSteffen Klassert rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize; 5106238cbaeSSteffen Klassert 51137db69e0SEric Biggers return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); 5126238cbaeSSteffen Klassert } 5133acc8473SHerbert Xu #else 5143acc8473SHerbert Xu static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) 5153acc8473SHerbert Xu { 5163acc8473SHerbert Xu return -ENOSYS; 5173acc8473SHerbert Xu } 5183acc8473SHerbert Xu #endif 5196238cbaeSSteffen Klassert 520004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 521d8c34b94SGideon Israel Dsouza __maybe_unused; 522004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 523004a403cSLoc Ho { 524004a403cSLoc Ho seq_printf(m, "type : ahash\n"); 525004a403cSLoc Ho seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? 526004a403cSLoc Ho "yes" : "no"); 527004a403cSLoc Ho seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 52888056ec3SHerbert Xu seq_printf(m, "digestsize : %u\n", 52988056ec3SHerbert Xu __crypto_hash_alg_common(alg)->digestsize); 530004a403cSLoc Ho } 531004a403cSLoc Ho 5326d1b41fcSEric Biggers static const struct crypto_type crypto_ahash_type = { 53388056ec3SHerbert Xu .extsize = crypto_ahash_extsize, 53488056ec3SHerbert Xu .init_tfm = crypto_ahash_init_tfm, 53548fb3e57SEric Biggers .free = crypto_ahash_free_instance, 536004a403cSLoc Ho #ifdef CONFIG_PROC_FS 537004a403cSLoc Ho .show = crypto_ahash_show, 538004a403cSLoc Ho #endif 5396238cbaeSSteffen Klassert .report = crypto_ahash_report, 54088056ec3SHerbert Xu .maskclear = ~CRYPTO_ALG_TYPE_MASK, 54188056ec3SHerbert Xu .maskset = CRYPTO_ALG_TYPE_AHASH_MASK, 54288056ec3SHerbert Xu .type = CRYPTO_ALG_TYPE_AHASH, 54388056ec3SHerbert Xu .tfmsize = offsetof(struct crypto_ahash, base), 544004a403cSLoc Ho }; 545004a403cSLoc Ho 54684a9c938SEric Biggers int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, 54784a9c938SEric Biggers struct crypto_instance *inst, 54884a9c938SEric Biggers const char *name, u32 type, u32 mask) 54984a9c938SEric Biggers { 55084a9c938SEric Biggers spawn->base.frontend = &crypto_ahash_type; 55184a9c938SEric Biggers return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 55284a9c938SEric Biggers } 55384a9c938SEric Biggers EXPORT_SYMBOL_GPL(crypto_grab_ahash); 55484a9c938SEric Biggers 55588056ec3SHerbert Xu struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, 55688056ec3SHerbert Xu u32 mask) 55788056ec3SHerbert Xu { 55888056ec3SHerbert Xu return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask); 55988056ec3SHerbert Xu } 56088056ec3SHerbert Xu EXPORT_SYMBOL_GPL(crypto_alloc_ahash); 56188056ec3SHerbert Xu 5628d18e34cSHerbert Xu int crypto_has_ahash(const char *alg_name, u32 type, u32 mask) 5638d18e34cSHerbert Xu { 5648d18e34cSHerbert Xu return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask); 5658d18e34cSHerbert Xu } 5668d18e34cSHerbert Xu EXPORT_SYMBOL_GPL(crypto_has_ahash); 5678d18e34cSHerbert Xu 56801c2deceSHerbert Xu static int ahash_prepare_alg(struct ahash_alg *alg) 56901c2deceSHerbert Xu { 57001c2deceSHerbert Xu struct crypto_alg *base = &alg->halg.base; 57101c2deceSHerbert Xu 572b68a7ec1SKees Cook if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE || 573b68a7ec1SKees Cook alg->halg.statesize > HASH_MAX_STATESIZE || 5748996eafdSRussell King alg->halg.statesize == 0) 57501c2deceSHerbert Xu return -EINVAL; 57601c2deceSHerbert Xu 57701c2deceSHerbert Xu base->cra_type = &crypto_ahash_type; 57801c2deceSHerbert Xu base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; 57901c2deceSHerbert Xu base->cra_flags |= CRYPTO_ALG_TYPE_AHASH; 58001c2deceSHerbert Xu 58101c2deceSHerbert Xu return 0; 58201c2deceSHerbert Xu } 58301c2deceSHerbert Xu 58401c2deceSHerbert Xu int crypto_register_ahash(struct ahash_alg *alg) 58501c2deceSHerbert Xu { 58601c2deceSHerbert Xu struct crypto_alg *base = &alg->halg.base; 58701c2deceSHerbert Xu int err; 58801c2deceSHerbert Xu 58901c2deceSHerbert Xu err = ahash_prepare_alg(alg); 59001c2deceSHerbert Xu if (err) 59101c2deceSHerbert Xu return err; 59201c2deceSHerbert Xu 59301c2deceSHerbert Xu return crypto_register_alg(base); 59401c2deceSHerbert Xu } 59501c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_register_ahash); 59601c2deceSHerbert Xu 597c6d633a9SEric Biggers void crypto_unregister_ahash(struct ahash_alg *alg) 59801c2deceSHerbert Xu { 599c6d633a9SEric Biggers crypto_unregister_alg(&alg->halg.base); 60001c2deceSHerbert Xu } 60101c2deceSHerbert Xu EXPORT_SYMBOL_GPL(crypto_unregister_ahash); 60201c2deceSHerbert Xu 6036f7473c5SRabin Vincent int crypto_register_ahashes(struct ahash_alg *algs, int count) 6046f7473c5SRabin Vincent { 6056f7473c5SRabin Vincent int i, ret; 6066f7473c5SRabin Vincent 6076f7473c5SRabin Vincent for (i = 0; i < count; i++) { 6086f7473c5SRabin Vincent ret = crypto_register_ahash(&algs[i]); 6096f7473c5SRabin Vincent if (ret) 6106f7473c5SRabin Vincent goto err; 6116f7473c5SRabin Vincent } 6126f7473c5SRabin Vincent 6136f7473c5SRabin Vincent return 0; 6146f7473c5SRabin Vincent 6156f7473c5SRabin Vincent err: 6166f7473c5SRabin Vincent for (--i; i >= 0; --i) 6176f7473c5SRabin Vincent crypto_unregister_ahash(&algs[i]); 6186f7473c5SRabin Vincent 6196f7473c5SRabin Vincent return ret; 6206f7473c5SRabin Vincent } 6216f7473c5SRabin Vincent EXPORT_SYMBOL_GPL(crypto_register_ahashes); 6226f7473c5SRabin Vincent 6236f7473c5SRabin Vincent void crypto_unregister_ahashes(struct ahash_alg *algs, int count) 6246f7473c5SRabin Vincent { 6256f7473c5SRabin Vincent int i; 6266f7473c5SRabin Vincent 6276f7473c5SRabin Vincent for (i = count - 1; i >= 0; --i) 6286f7473c5SRabin Vincent crypto_unregister_ahash(&algs[i]); 6296f7473c5SRabin Vincent } 6306f7473c5SRabin Vincent EXPORT_SYMBOL_GPL(crypto_unregister_ahashes); 6316f7473c5SRabin Vincent 63201c2deceSHerbert Xu int ahash_register_instance(struct crypto_template *tmpl, 63301c2deceSHerbert Xu struct ahash_instance *inst) 63401c2deceSHerbert Xu { 63501c2deceSHerbert Xu int err; 63601c2deceSHerbert Xu 637d4fdc2dfSEric Biggers if (WARN_ON(!inst->free)) 638d4fdc2dfSEric Biggers return -EINVAL; 639d4fdc2dfSEric Biggers 64001c2deceSHerbert Xu err = ahash_prepare_alg(&inst->alg); 64101c2deceSHerbert Xu if (err) 64201c2deceSHerbert Xu return err; 64301c2deceSHerbert Xu 64401c2deceSHerbert Xu return crypto_register_instance(tmpl, ahash_crypto_instance(inst)); 64501c2deceSHerbert Xu } 64601c2deceSHerbert Xu EXPORT_SYMBOL_GPL(ahash_register_instance); 64701c2deceSHerbert Xu 648cd6ed77aSEric Biggers bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg) 649cd6ed77aSEric Biggers { 650cd6ed77aSEric Biggers struct crypto_alg *alg = &halg->base; 651cd6ed77aSEric Biggers 652cd6ed77aSEric Biggers if (alg->cra_type != &crypto_ahash_type) 653cd6ed77aSEric Biggers return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg)); 654cd6ed77aSEric Biggers 655cd6ed77aSEric Biggers return __crypto_ahash_alg(alg)->setkey != NULL; 656cd6ed77aSEric Biggers } 657cd6ed77aSEric Biggers EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey); 658cd6ed77aSEric Biggers 659004a403cSLoc Ho MODULE_LICENSE("GPL"); 660004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); 661