Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Asynchronous Cryptographic Hash operations. |
| 3 | * |
| 4 | * This is the asynchronous version of hash.c with notification of |
| 5 | * completion via a callback. |
| 6 | * |
| 7 | * Copyright (c) 2008 Loc Ho <lho@amcc.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the Free |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
Herbert Xu | 2003625 | 2008-07-07 22:19:53 +0800 | [diff] [blame] | 16 | #include <crypto/internal/hash.h> |
| 17 | #include <crypto/scatterwalk.h> |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | |
| 25 | #include "internal.h" |
| 26 | |
Herbert Xu | 2003625 | 2008-07-07 22:19:53 +0800 | [diff] [blame] | 27 | static int hash_walk_next(struct crypto_hash_walk *walk) |
| 28 | { |
| 29 | unsigned int alignmask = walk->alignmask; |
| 30 | unsigned int offset = walk->offset; |
| 31 | unsigned int nbytes = min(walk->entrylen, |
| 32 | ((unsigned int)(PAGE_SIZE)) - offset); |
| 33 | |
| 34 | walk->data = crypto_kmap(walk->pg, 0); |
| 35 | walk->data += offset; |
| 36 | |
| 37 | if (offset & alignmask) |
| 38 | nbytes = alignmask + 1 - (offset & alignmask); |
| 39 | |
| 40 | walk->entrylen -= nbytes; |
| 41 | return nbytes; |
| 42 | } |
| 43 | |
| 44 | static int hash_walk_new_entry(struct crypto_hash_walk *walk) |
| 45 | { |
| 46 | struct scatterlist *sg; |
| 47 | |
| 48 | sg = walk->sg; |
| 49 | walk->pg = sg_page(sg); |
| 50 | walk->offset = sg->offset; |
| 51 | walk->entrylen = sg->length; |
| 52 | |
| 53 | if (walk->entrylen > walk->total) |
| 54 | walk->entrylen = walk->total; |
| 55 | walk->total -= walk->entrylen; |
| 56 | |
| 57 | return hash_walk_next(walk); |
| 58 | } |
| 59 | |
| 60 | int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) |
| 61 | { |
| 62 | unsigned int alignmask = walk->alignmask; |
| 63 | unsigned int nbytes = walk->entrylen; |
| 64 | |
| 65 | walk->data -= walk->offset; |
| 66 | |
| 67 | if (nbytes && walk->offset & alignmask && !err) { |
| 68 | walk->offset += alignmask - 1; |
| 69 | walk->offset = ALIGN(walk->offset, alignmask + 1); |
| 70 | walk->data += walk->offset; |
| 71 | |
| 72 | nbytes = min(nbytes, |
| 73 | ((unsigned int)(PAGE_SIZE)) - walk->offset); |
| 74 | walk->entrylen -= nbytes; |
| 75 | |
| 76 | return nbytes; |
| 77 | } |
| 78 | |
| 79 | crypto_kunmap(walk->data, 0); |
| 80 | crypto_yield(walk->flags); |
| 81 | |
| 82 | if (err) |
| 83 | return err; |
| 84 | |
| 85 | walk->offset = 0; |
| 86 | |
| 87 | if (nbytes) |
| 88 | return hash_walk_next(walk); |
| 89 | |
| 90 | if (!walk->total) |
| 91 | return 0; |
| 92 | |
| 93 | walk->sg = scatterwalk_sg_next(walk->sg); |
| 94 | |
| 95 | return hash_walk_new_entry(walk); |
| 96 | } |
| 97 | EXPORT_SYMBOL_GPL(crypto_hash_walk_done); |
| 98 | |
| 99 | int crypto_hash_walk_first(struct ahash_request *req, |
| 100 | struct crypto_hash_walk *walk) |
| 101 | { |
| 102 | walk->total = req->nbytes; |
| 103 | |
| 104 | if (!walk->total) |
| 105 | return 0; |
| 106 | |
| 107 | walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); |
| 108 | walk->sg = req->src; |
| 109 | walk->flags = req->base.flags; |
| 110 | |
| 111 | return hash_walk_new_entry(walk); |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(crypto_hash_walk_first); |
| 114 | |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 115 | static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, |
| 116 | unsigned int keylen) |
| 117 | { |
| 118 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); |
| 119 | unsigned long alignmask = crypto_ahash_alignmask(tfm); |
| 120 | int ret; |
| 121 | u8 *buffer, *alignbuffer; |
| 122 | unsigned long absize; |
| 123 | |
| 124 | absize = keylen + alignmask; |
| 125 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 126 | if (!buffer) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 130 | memcpy(alignbuffer, key, keylen); |
| 131 | ret = ahash->setkey(tfm, alignbuffer, keylen); |
| 132 | memset(alignbuffer, 0, keylen); |
| 133 | kfree(buffer); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 138 | unsigned int keylen) |
| 139 | { |
| 140 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); |
| 141 | unsigned long alignmask = crypto_ahash_alignmask(tfm); |
| 142 | |
| 143 | if ((unsigned long)key & alignmask) |
| 144 | return ahash_setkey_unaligned(tfm, key, keylen); |
| 145 | |
| 146 | return ahash->setkey(tfm, key, keylen); |
| 147 | } |
| 148 | |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 149 | int crypto_ahash_import(struct ahash_request *req, const u8 *in) |
| 150 | { |
| 151 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 152 | struct ahash_alg *alg = crypto_ahash_alg(tfm); |
| 153 | |
| 154 | memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm)); |
| 155 | |
| 156 | if (alg->reinit) |
| 157 | alg->reinit(req); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(crypto_ahash_import); |
| 162 | |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 163 | static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type, |
| 164 | u32 mask) |
| 165 | { |
| 166 | return alg->cra_ctxsize; |
| 167 | } |
| 168 | |
| 169 | static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
| 170 | { |
| 171 | struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash; |
| 172 | struct ahash_tfm *crt = &tfm->crt_ahash; |
| 173 | |
Herbert Xu | ca786dc | 2008-07-07 20:23:56 +0800 | [diff] [blame] | 174 | if (alg->digestsize > PAGE_SIZE / 8) |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | |
| 177 | crt->init = alg->init; |
| 178 | crt->update = alg->update; |
| 179 | crt->final = alg->final; |
| 180 | crt->digest = alg->digest; |
| 181 | crt->setkey = ahash_setkey; |
Loc Ho | 004a403 | 2008-05-14 20:41:47 +0800 | [diff] [blame] | 182 | crt->digestsize = alg->digestsize; |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) |
| 188 | __attribute__ ((unused)); |
| 189 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) |
| 190 | { |
| 191 | seq_printf(m, "type : ahash\n"); |
| 192 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? |
| 193 | "yes" : "no"); |
| 194 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 195 | seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize); |
| 196 | } |
| 197 | |
| 198 | const struct crypto_type crypto_ahash_type = { |
| 199 | .ctxsize = crypto_ahash_ctxsize, |
| 200 | .init = crypto_init_ahash_ops, |
| 201 | #ifdef CONFIG_PROC_FS |
| 202 | .show = crypto_ahash_show, |
| 203 | #endif |
| 204 | }; |
| 205 | EXPORT_SYMBOL_GPL(crypto_ahash_type); |
| 206 | |
| 207 | MODULE_LICENSE("GPL"); |
| 208 | MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); |