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> 24004a403cSLoc Ho 25004a403cSLoc Ho #include "internal.h" 26004a403cSLoc Ho 27*88056ec3SHerbert Xu static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) 28*88056ec3SHerbert Xu { 29*88056ec3SHerbert Xu return container_of(crypto_hash_alg_common(hash), struct ahash_alg, 30*88056ec3SHerbert Xu halg); 31*88056ec3SHerbert Xu } 32*88056ec3SHerbert Xu 3320036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk) 3420036252SHerbert Xu { 3520036252SHerbert Xu unsigned int alignmask = walk->alignmask; 3620036252SHerbert Xu unsigned int offset = walk->offset; 3720036252SHerbert Xu unsigned int nbytes = min(walk->entrylen, 3820036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - offset); 3920036252SHerbert Xu 4020036252SHerbert Xu walk->data = crypto_kmap(walk->pg, 0); 4120036252SHerbert Xu walk->data += offset; 4220036252SHerbert Xu 4320036252SHerbert Xu if (offset & alignmask) 4420036252SHerbert Xu nbytes = alignmask + 1 - (offset & alignmask); 4520036252SHerbert Xu 4620036252SHerbert Xu walk->entrylen -= nbytes; 4720036252SHerbert Xu return nbytes; 4820036252SHerbert Xu } 4920036252SHerbert Xu 5020036252SHerbert Xu static int hash_walk_new_entry(struct crypto_hash_walk *walk) 5120036252SHerbert Xu { 5220036252SHerbert Xu struct scatterlist *sg; 5320036252SHerbert Xu 5420036252SHerbert Xu sg = walk->sg; 5520036252SHerbert Xu walk->pg = sg_page(sg); 5620036252SHerbert Xu walk->offset = sg->offset; 5720036252SHerbert Xu walk->entrylen = sg->length; 5820036252SHerbert Xu 5920036252SHerbert Xu if (walk->entrylen > walk->total) 6020036252SHerbert Xu walk->entrylen = walk->total; 6120036252SHerbert Xu walk->total -= walk->entrylen; 6220036252SHerbert Xu 6320036252SHerbert Xu return hash_walk_next(walk); 6420036252SHerbert Xu } 6520036252SHerbert Xu 6620036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) 6720036252SHerbert Xu { 6820036252SHerbert Xu unsigned int alignmask = walk->alignmask; 6920036252SHerbert Xu unsigned int nbytes = walk->entrylen; 7020036252SHerbert Xu 7120036252SHerbert Xu walk->data -= walk->offset; 7220036252SHerbert Xu 7320036252SHerbert Xu if (nbytes && walk->offset & alignmask && !err) { 7420036252SHerbert Xu walk->offset += alignmask - 1; 7520036252SHerbert Xu walk->offset = ALIGN(walk->offset, alignmask + 1); 7620036252SHerbert Xu walk->data += walk->offset; 7720036252SHerbert Xu 7820036252SHerbert Xu nbytes = min(nbytes, 7920036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - walk->offset); 8020036252SHerbert Xu walk->entrylen -= nbytes; 8120036252SHerbert Xu 8220036252SHerbert Xu return nbytes; 8320036252SHerbert Xu } 8420036252SHerbert Xu 8520036252SHerbert Xu crypto_kunmap(walk->data, 0); 8620036252SHerbert Xu crypto_yield(walk->flags); 8720036252SHerbert Xu 8820036252SHerbert Xu if (err) 8920036252SHerbert Xu return err; 9020036252SHerbert Xu 91d315a0e0SHerbert Xu if (nbytes) { 9220036252SHerbert Xu walk->offset = 0; 93d315a0e0SHerbert Xu walk->pg++; 9420036252SHerbert Xu return hash_walk_next(walk); 95d315a0e0SHerbert Xu } 9620036252SHerbert Xu 9720036252SHerbert Xu if (!walk->total) 9820036252SHerbert Xu return 0; 9920036252SHerbert Xu 10020036252SHerbert Xu walk->sg = scatterwalk_sg_next(walk->sg); 10120036252SHerbert Xu 10220036252SHerbert Xu return hash_walk_new_entry(walk); 10320036252SHerbert Xu } 10420036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done); 10520036252SHerbert Xu 10620036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req, 10720036252SHerbert Xu struct crypto_hash_walk *walk) 10820036252SHerbert Xu { 10920036252SHerbert Xu walk->total = req->nbytes; 11020036252SHerbert Xu 11120036252SHerbert Xu if (!walk->total) 11220036252SHerbert Xu return 0; 11320036252SHerbert Xu 11420036252SHerbert Xu walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); 11520036252SHerbert Xu walk->sg = req->src; 11620036252SHerbert Xu walk->flags = req->base.flags; 11720036252SHerbert Xu 11820036252SHerbert Xu return hash_walk_new_entry(walk); 11920036252SHerbert Xu } 12020036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first); 12120036252SHerbert Xu 1225f7082edSHerbert Xu int crypto_hash_walk_first_compat(struct hash_desc *hdesc, 1235f7082edSHerbert Xu struct crypto_hash_walk *walk, 1245f7082edSHerbert Xu struct scatterlist *sg, unsigned int len) 1255f7082edSHerbert Xu { 1265f7082edSHerbert Xu walk->total = len; 1275f7082edSHerbert Xu 1285f7082edSHerbert Xu if (!walk->total) 1295f7082edSHerbert Xu return 0; 1305f7082edSHerbert Xu 1315f7082edSHerbert Xu walk->alignmask = crypto_hash_alignmask(hdesc->tfm); 1325f7082edSHerbert Xu walk->sg = sg; 1335f7082edSHerbert Xu walk->flags = hdesc->flags; 1345f7082edSHerbert Xu 1355f7082edSHerbert Xu return hash_walk_new_entry(walk); 1365f7082edSHerbert Xu } 1375f7082edSHerbert Xu 138004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, 139004a403cSLoc Ho unsigned int keylen) 140004a403cSLoc Ho { 141004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 142004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 143004a403cSLoc Ho int ret; 144004a403cSLoc Ho u8 *buffer, *alignbuffer; 145004a403cSLoc Ho unsigned long absize; 146004a403cSLoc Ho 147004a403cSLoc Ho absize = keylen + alignmask; 148004a403cSLoc Ho buffer = kmalloc(absize, GFP_ATOMIC); 149004a403cSLoc Ho if (!buffer) 150004a403cSLoc Ho return -ENOMEM; 151004a403cSLoc Ho 152004a403cSLoc Ho alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 153004a403cSLoc Ho memcpy(alignbuffer, key, keylen); 154004a403cSLoc Ho ret = ahash->setkey(tfm, alignbuffer, keylen); 155004a403cSLoc Ho memset(alignbuffer, 0, keylen); 156004a403cSLoc Ho kfree(buffer); 157004a403cSLoc Ho return ret; 158004a403cSLoc Ho } 159004a403cSLoc Ho 160004a403cSLoc Ho static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 161004a403cSLoc Ho unsigned int keylen) 162004a403cSLoc Ho { 163004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 164004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 165004a403cSLoc Ho 166004a403cSLoc Ho if ((unsigned long)key & alignmask) 167004a403cSLoc Ho return ahash_setkey_unaligned(tfm, key, keylen); 168004a403cSLoc Ho 169004a403cSLoc Ho return ahash->setkey(tfm, key, keylen); 170004a403cSLoc Ho } 171004a403cSLoc Ho 1723751f402SHerbert Xu static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, 1733751f402SHerbert Xu unsigned int keylen) 1743751f402SHerbert Xu { 1753751f402SHerbert Xu return -ENOSYS; 1763751f402SHerbert Xu } 1773751f402SHerbert Xu 178004a403cSLoc Ho static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) 179004a403cSLoc Ho { 180*88056ec3SHerbert Xu struct old_ahash_alg *alg = &tfm->__crt_alg->cra_ahash; 181*88056ec3SHerbert Xu struct crypto_ahash *crt = __crypto_ahash_cast(tfm); 182*88056ec3SHerbert Xu struct ahash_alg *nalg = crypto_ahash_alg(crt); 183004a403cSLoc Ho 184ca786dc7SHerbert Xu if (alg->digestsize > PAGE_SIZE / 8) 185004a403cSLoc Ho return -EINVAL; 186004a403cSLoc Ho 187004a403cSLoc Ho crt->init = alg->init; 188004a403cSLoc Ho crt->update = alg->update; 189004a403cSLoc Ho crt->final = alg->final; 190004a403cSLoc Ho crt->digest = alg->digest; 1913751f402SHerbert Xu crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey; 192004a403cSLoc Ho crt->digestsize = alg->digestsize; 193004a403cSLoc Ho 194*88056ec3SHerbert Xu nalg->setkey = alg->setkey; 195*88056ec3SHerbert Xu nalg->halg.digestsize = alg->digestsize; 196*88056ec3SHerbert Xu 197004a403cSLoc Ho return 0; 198004a403cSLoc Ho } 199004a403cSLoc Ho 200*88056ec3SHerbert Xu static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) 201*88056ec3SHerbert Xu { 202*88056ec3SHerbert Xu struct crypto_ahash *hash = __crypto_ahash_cast(tfm); 203*88056ec3SHerbert Xu struct ahash_alg *alg = crypto_ahash_alg(hash); 204*88056ec3SHerbert Xu struct old_ahash_alg *oalg = crypto_old_ahash_alg(hash); 205*88056ec3SHerbert Xu 206*88056ec3SHerbert Xu if (tfm->__crt_alg->cra_type != &crypto_ahash_type) 207*88056ec3SHerbert Xu return crypto_init_shash_ops_async(tfm); 208*88056ec3SHerbert Xu 209*88056ec3SHerbert Xu if (oalg->init) 210*88056ec3SHerbert Xu return crypto_init_ahash_ops(tfm, 0, 0); 211*88056ec3SHerbert Xu 212*88056ec3SHerbert Xu hash->init = alg->init; 213*88056ec3SHerbert Xu hash->update = alg->update; 214*88056ec3SHerbert Xu hash->final = alg->final; 215*88056ec3SHerbert Xu hash->digest = alg->digest; 216*88056ec3SHerbert Xu hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey; 217*88056ec3SHerbert Xu hash->digestsize = alg->halg.digestsize; 218*88056ec3SHerbert Xu 219*88056ec3SHerbert Xu return 0; 220*88056ec3SHerbert Xu } 221*88056ec3SHerbert Xu 222*88056ec3SHerbert Xu static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) 223*88056ec3SHerbert Xu { 224*88056ec3SHerbert Xu if (alg->cra_type == &crypto_ahash_type) 225*88056ec3SHerbert Xu return alg->cra_ctxsize; 226*88056ec3SHerbert Xu 227*88056ec3SHerbert Xu return sizeof(struct crypto_shash *); 228*88056ec3SHerbert Xu } 229*88056ec3SHerbert Xu 230004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 231004a403cSLoc Ho __attribute__ ((unused)); 232004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 233004a403cSLoc Ho { 234004a403cSLoc Ho seq_printf(m, "type : ahash\n"); 235004a403cSLoc Ho seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? 236004a403cSLoc Ho "yes" : "no"); 237004a403cSLoc Ho seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 238*88056ec3SHerbert Xu seq_printf(m, "digestsize : %u\n", 239*88056ec3SHerbert Xu __crypto_hash_alg_common(alg)->digestsize); 240004a403cSLoc Ho } 241004a403cSLoc Ho 242004a403cSLoc Ho const struct crypto_type crypto_ahash_type = { 243*88056ec3SHerbert Xu .extsize = crypto_ahash_extsize, 244*88056ec3SHerbert Xu .init_tfm = crypto_ahash_init_tfm, 245004a403cSLoc Ho #ifdef CONFIG_PROC_FS 246004a403cSLoc Ho .show = crypto_ahash_show, 247004a403cSLoc Ho #endif 248*88056ec3SHerbert Xu .maskclear = ~CRYPTO_ALG_TYPE_MASK, 249*88056ec3SHerbert Xu .maskset = CRYPTO_ALG_TYPE_AHASH_MASK, 250*88056ec3SHerbert Xu .type = CRYPTO_ALG_TYPE_AHASH, 251*88056ec3SHerbert Xu .tfmsize = offsetof(struct crypto_ahash, base), 252004a403cSLoc Ho }; 253004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type); 254004a403cSLoc Ho 255*88056ec3SHerbert Xu struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, 256*88056ec3SHerbert Xu u32 mask) 257*88056ec3SHerbert Xu { 258*88056ec3SHerbert Xu return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask); 259*88056ec3SHerbert Xu } 260*88056ec3SHerbert Xu EXPORT_SYMBOL_GPL(crypto_alloc_ahash); 261*88056ec3SHerbert Xu 262004a403cSLoc Ho MODULE_LICENSE("GPL"); 263004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); 264