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 2720036252SHerbert Xu static int hash_walk_next(struct crypto_hash_walk *walk) 2820036252SHerbert Xu { 2920036252SHerbert Xu unsigned int alignmask = walk->alignmask; 3020036252SHerbert Xu unsigned int offset = walk->offset; 3120036252SHerbert Xu unsigned int nbytes = min(walk->entrylen, 3220036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - offset); 3320036252SHerbert Xu 3420036252SHerbert Xu walk->data = crypto_kmap(walk->pg, 0); 3520036252SHerbert Xu walk->data += offset; 3620036252SHerbert Xu 3720036252SHerbert Xu if (offset & alignmask) 3820036252SHerbert Xu nbytes = alignmask + 1 - (offset & alignmask); 3920036252SHerbert Xu 4020036252SHerbert Xu walk->entrylen -= nbytes; 4120036252SHerbert Xu return nbytes; 4220036252SHerbert Xu } 4320036252SHerbert Xu 4420036252SHerbert Xu static int hash_walk_new_entry(struct crypto_hash_walk *walk) 4520036252SHerbert Xu { 4620036252SHerbert Xu struct scatterlist *sg; 4720036252SHerbert Xu 4820036252SHerbert Xu sg = walk->sg; 4920036252SHerbert Xu walk->pg = sg_page(sg); 5020036252SHerbert Xu walk->offset = sg->offset; 5120036252SHerbert Xu walk->entrylen = sg->length; 5220036252SHerbert Xu 5320036252SHerbert Xu if (walk->entrylen > walk->total) 5420036252SHerbert Xu walk->entrylen = walk->total; 5520036252SHerbert Xu walk->total -= walk->entrylen; 5620036252SHerbert Xu 5720036252SHerbert Xu return hash_walk_next(walk); 5820036252SHerbert Xu } 5920036252SHerbert Xu 6020036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) 6120036252SHerbert Xu { 6220036252SHerbert Xu unsigned int alignmask = walk->alignmask; 6320036252SHerbert Xu unsigned int nbytes = walk->entrylen; 6420036252SHerbert Xu 6520036252SHerbert Xu walk->data -= walk->offset; 6620036252SHerbert Xu 6720036252SHerbert Xu if (nbytes && walk->offset & alignmask && !err) { 6820036252SHerbert Xu walk->offset += alignmask - 1; 6920036252SHerbert Xu walk->offset = ALIGN(walk->offset, alignmask + 1); 7020036252SHerbert Xu walk->data += walk->offset; 7120036252SHerbert Xu 7220036252SHerbert Xu nbytes = min(nbytes, 7320036252SHerbert Xu ((unsigned int)(PAGE_SIZE)) - walk->offset); 7420036252SHerbert Xu walk->entrylen -= nbytes; 7520036252SHerbert Xu 7620036252SHerbert Xu return nbytes; 7720036252SHerbert Xu } 7820036252SHerbert Xu 7920036252SHerbert Xu crypto_kunmap(walk->data, 0); 8020036252SHerbert Xu crypto_yield(walk->flags); 8120036252SHerbert Xu 8220036252SHerbert Xu if (err) 8320036252SHerbert Xu return err; 8420036252SHerbert Xu 85*d315a0e0SHerbert Xu if (nbytes) { 8620036252SHerbert Xu walk->offset = 0; 87*d315a0e0SHerbert Xu walk->pg++; 8820036252SHerbert Xu return hash_walk_next(walk); 89*d315a0e0SHerbert Xu } 9020036252SHerbert Xu 9120036252SHerbert Xu if (!walk->total) 9220036252SHerbert Xu return 0; 9320036252SHerbert Xu 9420036252SHerbert Xu walk->sg = scatterwalk_sg_next(walk->sg); 9520036252SHerbert Xu 9620036252SHerbert Xu return hash_walk_new_entry(walk); 9720036252SHerbert Xu } 9820036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done); 9920036252SHerbert Xu 10020036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req, 10120036252SHerbert Xu struct crypto_hash_walk *walk) 10220036252SHerbert Xu { 10320036252SHerbert Xu walk->total = req->nbytes; 10420036252SHerbert Xu 10520036252SHerbert Xu if (!walk->total) 10620036252SHerbert Xu return 0; 10720036252SHerbert Xu 10820036252SHerbert Xu walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); 10920036252SHerbert Xu walk->sg = req->src; 11020036252SHerbert Xu walk->flags = req->base.flags; 11120036252SHerbert Xu 11220036252SHerbert Xu return hash_walk_new_entry(walk); 11320036252SHerbert Xu } 11420036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first); 11520036252SHerbert Xu 1165f7082edSHerbert Xu int crypto_hash_walk_first_compat(struct hash_desc *hdesc, 1175f7082edSHerbert Xu struct crypto_hash_walk *walk, 1185f7082edSHerbert Xu struct scatterlist *sg, unsigned int len) 1195f7082edSHerbert Xu { 1205f7082edSHerbert Xu walk->total = len; 1215f7082edSHerbert Xu 1225f7082edSHerbert Xu if (!walk->total) 1235f7082edSHerbert Xu return 0; 1245f7082edSHerbert Xu 1255f7082edSHerbert Xu walk->alignmask = crypto_hash_alignmask(hdesc->tfm); 1265f7082edSHerbert Xu walk->sg = sg; 1275f7082edSHerbert Xu walk->flags = hdesc->flags; 1285f7082edSHerbert Xu 1295f7082edSHerbert Xu return hash_walk_new_entry(walk); 1305f7082edSHerbert Xu } 1315f7082edSHerbert Xu 132004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, 133004a403cSLoc Ho unsigned int keylen) 134004a403cSLoc Ho { 135004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 136004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 137004a403cSLoc Ho int ret; 138004a403cSLoc Ho u8 *buffer, *alignbuffer; 139004a403cSLoc Ho unsigned long absize; 140004a403cSLoc Ho 141004a403cSLoc Ho absize = keylen + alignmask; 142004a403cSLoc Ho buffer = kmalloc(absize, GFP_ATOMIC); 143004a403cSLoc Ho if (!buffer) 144004a403cSLoc Ho return -ENOMEM; 145004a403cSLoc Ho 146004a403cSLoc Ho alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 147004a403cSLoc Ho memcpy(alignbuffer, key, keylen); 148004a403cSLoc Ho ret = ahash->setkey(tfm, alignbuffer, keylen); 149004a403cSLoc Ho memset(alignbuffer, 0, keylen); 150004a403cSLoc Ho kfree(buffer); 151004a403cSLoc Ho return ret; 152004a403cSLoc Ho } 153004a403cSLoc Ho 154004a403cSLoc Ho static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 155004a403cSLoc Ho unsigned int keylen) 156004a403cSLoc Ho { 157004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 158004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 159004a403cSLoc Ho 160004a403cSLoc Ho if ((unsigned long)key & alignmask) 161004a403cSLoc Ho return ahash_setkey_unaligned(tfm, key, keylen); 162004a403cSLoc Ho 163004a403cSLoc Ho return ahash->setkey(tfm, key, keylen); 164004a403cSLoc Ho } 165004a403cSLoc Ho 1663751f402SHerbert Xu static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, 1673751f402SHerbert Xu unsigned int keylen) 1683751f402SHerbert Xu { 1693751f402SHerbert Xu return -ENOSYS; 1703751f402SHerbert Xu } 1713751f402SHerbert Xu 172dec8b786SHerbert Xu int crypto_ahash_import(struct ahash_request *req, const u8 *in) 173dec8b786SHerbert Xu { 174dec8b786SHerbert Xu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 175dec8b786SHerbert Xu struct ahash_alg *alg = crypto_ahash_alg(tfm); 176dec8b786SHerbert Xu 177dec8b786SHerbert Xu memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm)); 178dec8b786SHerbert Xu 179dec8b786SHerbert Xu if (alg->reinit) 180dec8b786SHerbert Xu alg->reinit(req); 181dec8b786SHerbert Xu 182dec8b786SHerbert Xu return 0; 183dec8b786SHerbert Xu } 184dec8b786SHerbert Xu EXPORT_SYMBOL_GPL(crypto_ahash_import); 185dec8b786SHerbert Xu 186004a403cSLoc Ho static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type, 187004a403cSLoc Ho u32 mask) 188004a403cSLoc Ho { 189004a403cSLoc Ho return alg->cra_ctxsize; 190004a403cSLoc Ho } 191004a403cSLoc Ho 192004a403cSLoc Ho static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) 193004a403cSLoc Ho { 194004a403cSLoc Ho struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash; 195004a403cSLoc Ho struct ahash_tfm *crt = &tfm->crt_ahash; 196004a403cSLoc Ho 197ca786dc7SHerbert Xu if (alg->digestsize > PAGE_SIZE / 8) 198004a403cSLoc Ho return -EINVAL; 199004a403cSLoc Ho 200004a403cSLoc Ho crt->init = alg->init; 201004a403cSLoc Ho crt->update = alg->update; 202004a403cSLoc Ho crt->final = alg->final; 203004a403cSLoc Ho crt->digest = alg->digest; 2043751f402SHerbert Xu crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey; 205004a403cSLoc Ho crt->digestsize = alg->digestsize; 206004a403cSLoc Ho 207004a403cSLoc Ho return 0; 208004a403cSLoc Ho } 209004a403cSLoc Ho 210004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 211004a403cSLoc Ho __attribute__ ((unused)); 212004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 213004a403cSLoc Ho { 214004a403cSLoc Ho seq_printf(m, "type : ahash\n"); 215004a403cSLoc Ho seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? 216004a403cSLoc Ho "yes" : "no"); 217004a403cSLoc Ho seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 218bb402f16SLee Nipper seq_printf(m, "digestsize : %u\n", alg->cra_ahash.digestsize); 219004a403cSLoc Ho } 220004a403cSLoc Ho 221004a403cSLoc Ho const struct crypto_type crypto_ahash_type = { 222004a403cSLoc Ho .ctxsize = crypto_ahash_ctxsize, 223004a403cSLoc Ho .init = crypto_init_ahash_ops, 224004a403cSLoc Ho #ifdef CONFIG_PROC_FS 225004a403cSLoc Ho .show = crypto_ahash_show, 226004a403cSLoc Ho #endif 227004a403cSLoc Ho }; 228004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type); 229004a403cSLoc Ho 230004a403cSLoc Ho MODULE_LICENSE("GPL"); 231004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); 232