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 8520036252SHerbert Xu walk->offset = 0; 8620036252SHerbert Xu 8720036252SHerbert Xu if (nbytes) 8820036252SHerbert Xu return hash_walk_next(walk); 8920036252SHerbert Xu 9020036252SHerbert Xu if (!walk->total) 9120036252SHerbert Xu return 0; 9220036252SHerbert Xu 9320036252SHerbert Xu walk->sg = scatterwalk_sg_next(walk->sg); 9420036252SHerbert Xu 9520036252SHerbert Xu return hash_walk_new_entry(walk); 9620036252SHerbert Xu } 9720036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_done); 9820036252SHerbert Xu 9920036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req, 10020036252SHerbert Xu struct crypto_hash_walk *walk) 10120036252SHerbert Xu { 10220036252SHerbert Xu walk->total = req->nbytes; 10320036252SHerbert Xu 10420036252SHerbert Xu if (!walk->total) 10520036252SHerbert Xu return 0; 10620036252SHerbert Xu 10720036252SHerbert Xu walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); 10820036252SHerbert Xu walk->sg = req->src; 10920036252SHerbert Xu walk->flags = req->base.flags; 11020036252SHerbert Xu 11120036252SHerbert Xu return hash_walk_new_entry(walk); 11220036252SHerbert Xu } 11320036252SHerbert Xu EXPORT_SYMBOL_GPL(crypto_hash_walk_first); 11420036252SHerbert Xu 115004a403cSLoc Ho static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, 116004a403cSLoc Ho unsigned int keylen) 117004a403cSLoc Ho { 118004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 119004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 120004a403cSLoc Ho int ret; 121004a403cSLoc Ho u8 *buffer, *alignbuffer; 122004a403cSLoc Ho unsigned long absize; 123004a403cSLoc Ho 124004a403cSLoc Ho absize = keylen + alignmask; 125004a403cSLoc Ho buffer = kmalloc(absize, GFP_ATOMIC); 126004a403cSLoc Ho if (!buffer) 127004a403cSLoc Ho return -ENOMEM; 128004a403cSLoc Ho 129004a403cSLoc Ho alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 130004a403cSLoc Ho memcpy(alignbuffer, key, keylen); 131004a403cSLoc Ho ret = ahash->setkey(tfm, alignbuffer, keylen); 132004a403cSLoc Ho memset(alignbuffer, 0, keylen); 133004a403cSLoc Ho kfree(buffer); 134004a403cSLoc Ho return ret; 135004a403cSLoc Ho } 136004a403cSLoc Ho 137004a403cSLoc Ho static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 138004a403cSLoc Ho unsigned int keylen) 139004a403cSLoc Ho { 140004a403cSLoc Ho struct ahash_alg *ahash = crypto_ahash_alg(tfm); 141004a403cSLoc Ho unsigned long alignmask = crypto_ahash_alignmask(tfm); 142004a403cSLoc Ho 143004a403cSLoc Ho if ((unsigned long)key & alignmask) 144004a403cSLoc Ho return ahash_setkey_unaligned(tfm, key, keylen); 145004a403cSLoc Ho 146004a403cSLoc Ho return ahash->setkey(tfm, key, keylen); 147004a403cSLoc Ho } 148004a403cSLoc Ho 149004a403cSLoc Ho static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type, 150004a403cSLoc Ho u32 mask) 151004a403cSLoc Ho { 152004a403cSLoc Ho return alg->cra_ctxsize; 153004a403cSLoc Ho } 154004a403cSLoc Ho 155004a403cSLoc Ho static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) 156004a403cSLoc Ho { 157004a403cSLoc Ho struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash; 158004a403cSLoc Ho struct ahash_tfm *crt = &tfm->crt_ahash; 159004a403cSLoc Ho 160ca786dc7SHerbert Xu if (alg->digestsize > PAGE_SIZE / 8) 161004a403cSLoc Ho return -EINVAL; 162004a403cSLoc Ho 163004a403cSLoc Ho crt->init = alg->init; 164004a403cSLoc Ho crt->update = alg->update; 165004a403cSLoc Ho crt->final = alg->final; 166004a403cSLoc Ho crt->digest = alg->digest; 167004a403cSLoc Ho crt->setkey = ahash_setkey; 168004a403cSLoc Ho crt->digestsize = alg->digestsize; 169004a403cSLoc Ho 170004a403cSLoc Ho return 0; 171004a403cSLoc Ho } 172004a403cSLoc Ho 173004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 174004a403cSLoc Ho __attribute__ ((unused)); 175004a403cSLoc Ho static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) 176004a403cSLoc Ho { 177004a403cSLoc Ho seq_printf(m, "type : ahash\n"); 178004a403cSLoc Ho seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? 179004a403cSLoc Ho "yes" : "no"); 180004a403cSLoc Ho seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 181004a403cSLoc Ho seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize); 182004a403cSLoc Ho } 183004a403cSLoc Ho 184004a403cSLoc Ho const struct crypto_type crypto_ahash_type = { 185004a403cSLoc Ho .ctxsize = crypto_ahash_ctxsize, 186004a403cSLoc Ho .init = crypto_init_ahash_ops, 187004a403cSLoc Ho #ifdef CONFIG_PROC_FS 188004a403cSLoc Ho .show = crypto_ahash_show, 189004a403cSLoc Ho #endif 190004a403cSLoc Ho }; 191004a403cSLoc Ho EXPORT_SYMBOL_GPL(crypto_ahash_type); 192004a403cSLoc Ho 193004a403cSLoc Ho MODULE_LICENSE("GPL"); 194004a403cSLoc Ho MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); 195