Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Synchronous Cryptographic Hash operations. |
| 3 | * |
| 4 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 13 | #include <crypto/scatterwalk.h> |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 14 | #include <crypto/internal/hash.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/seq_file.h> |
| 20 | |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 21 | #include "internal.h" |
| 22 | |
Herbert Xu | 3f683d6 | 2009-02-18 16:56:59 +0800 | [diff] [blame] | 23 | static const struct crypto_type crypto_shash_type; |
| 24 | |
Herbert Xu | 57cfe44 | 2009-07-11 22:17:39 +0800 | [diff] [blame] | 25 | static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, |
| 26 | unsigned int keylen) |
| 27 | { |
| 28 | return -ENOSYS; |
| 29 | } |
| 30 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 31 | static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, |
| 32 | unsigned int keylen) |
| 33 | { |
| 34 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 35 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 36 | unsigned long absize; |
| 37 | u8 *buffer, *alignbuffer; |
| 38 | int err; |
| 39 | |
| 40 | absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1)); |
| 41 | buffer = kmalloc(absize, GFP_KERNEL); |
| 42 | if (!buffer) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 46 | memcpy(alignbuffer, key, keylen); |
| 47 | err = shash->setkey(tfm, alignbuffer, keylen); |
| 48 | memset(alignbuffer, 0, keylen); |
| 49 | kfree(buffer); |
| 50 | return err; |
| 51 | } |
| 52 | |
| 53 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
| 54 | unsigned int keylen) |
| 55 | { |
| 56 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 57 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 58 | |
| 59 | if ((unsigned long)key & alignmask) |
| 60 | return shash_setkey_unaligned(tfm, key, keylen); |
| 61 | |
| 62 | return shash->setkey(tfm, key, keylen); |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(crypto_shash_setkey); |
| 65 | |
| 66 | static inline unsigned int shash_align_buffer_size(unsigned len, |
| 67 | unsigned long mask) |
| 68 | { |
| 69 | return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1)); |
| 70 | } |
| 71 | |
| 72 | static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, |
| 73 | unsigned int len) |
| 74 | { |
| 75 | struct crypto_shash *tfm = desc->tfm; |
| 76 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 77 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 78 | unsigned int unaligned_len = alignmask + 1 - |
| 79 | ((unsigned long)data & alignmask); |
| 80 | u8 buf[shash_align_buffer_size(unaligned_len, alignmask)] |
| 81 | __attribute__ ((aligned)); |
| 82 | |
Yehuda Sadeh | f4f6899 | 2009-03-27 13:03:51 +0800 | [diff] [blame] | 83 | if (unaligned_len > len) |
| 84 | unaligned_len = len; |
| 85 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 86 | memcpy(buf, data, unaligned_len); |
| 87 | |
| 88 | return shash->update(desc, buf, unaligned_len) ?: |
| 89 | shash->update(desc, data + unaligned_len, len - unaligned_len); |
| 90 | } |
| 91 | |
| 92 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, |
| 93 | unsigned int len) |
| 94 | { |
| 95 | struct crypto_shash *tfm = desc->tfm; |
| 96 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 97 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 98 | |
| 99 | if ((unsigned long)data & alignmask) |
| 100 | return shash_update_unaligned(desc, data, len); |
| 101 | |
| 102 | return shash->update(desc, data, len); |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(crypto_shash_update); |
| 105 | |
| 106 | static int shash_final_unaligned(struct shash_desc *desc, u8 *out) |
| 107 | { |
| 108 | struct crypto_shash *tfm = desc->tfm; |
| 109 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 110 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 111 | unsigned int ds = crypto_shash_digestsize(tfm); |
| 112 | u8 buf[shash_align_buffer_size(ds, alignmask)] |
| 113 | __attribute__ ((aligned)); |
| 114 | int err; |
| 115 | |
| 116 | err = shash->final(desc, buf); |
| 117 | memcpy(out, buf, ds); |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | int crypto_shash_final(struct shash_desc *desc, u8 *out) |
| 122 | { |
| 123 | struct crypto_shash *tfm = desc->tfm; |
| 124 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 125 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 126 | |
| 127 | if ((unsigned long)out & alignmask) |
| 128 | return shash_final_unaligned(desc, out); |
| 129 | |
| 130 | return shash->final(desc, out); |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(crypto_shash_final); |
| 133 | |
| 134 | static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data, |
| 135 | unsigned int len, u8 *out) |
| 136 | { |
| 137 | return crypto_shash_update(desc, data, len) ?: |
| 138 | crypto_shash_final(desc, out); |
| 139 | } |
| 140 | |
| 141 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
| 142 | unsigned int len, u8 *out) |
| 143 | { |
| 144 | struct crypto_shash *tfm = desc->tfm; |
| 145 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 146 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 147 | |
Herbert Xu | 8267ada | 2009-07-09 20:36:44 +0800 | [diff] [blame] | 148 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 149 | return shash_finup_unaligned(desc, data, len, out); |
| 150 | |
| 151 | return shash->finup(desc, data, len, out); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(crypto_shash_finup); |
| 154 | |
| 155 | static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data, |
| 156 | unsigned int len, u8 *out) |
| 157 | { |
| 158 | return crypto_shash_init(desc) ?: |
Herbert Xu | f88ad8d | 2009-07-08 23:32:08 +0800 | [diff] [blame] | 159 | crypto_shash_finup(desc, data, len, out); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
| 163 | unsigned int len, u8 *out) |
| 164 | { |
| 165 | struct crypto_shash *tfm = desc->tfm; |
| 166 | struct shash_alg *shash = crypto_shash_alg(tfm); |
| 167 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
| 168 | |
Herbert Xu | 8267ada | 2009-07-09 20:36:44 +0800 | [diff] [blame] | 169 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 170 | return shash_digest_unaligned(desc, data, len, out); |
| 171 | |
| 172 | return shash->digest(desc, data, len, out); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(crypto_shash_digest); |
| 175 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 176 | static int shash_no_export(struct shash_desc *desc, void *out) |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 177 | { |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 178 | return -ENOSYS; |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 179 | } |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 180 | |
| 181 | static int shash_no_import(struct shash_desc *desc, const void *in) |
| 182 | { |
| 183 | return -ENOSYS; |
| 184 | } |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 185 | |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 186 | static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 187 | unsigned int keylen) |
| 188 | { |
| 189 | struct crypto_shash **ctx = crypto_ahash_ctx(tfm); |
| 190 | |
| 191 | return crypto_shash_setkey(*ctx, key, keylen); |
| 192 | } |
| 193 | |
| 194 | static int shash_async_init(struct ahash_request *req) |
| 195 | { |
| 196 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
| 197 | struct shash_desc *desc = ahash_request_ctx(req); |
| 198 | |
| 199 | desc->tfm = *ctx; |
| 200 | desc->flags = req->base.flags; |
| 201 | |
| 202 | return crypto_shash_init(desc); |
| 203 | } |
| 204 | |
| 205 | static int shash_async_update(struct ahash_request *req) |
| 206 | { |
| 207 | struct shash_desc *desc = ahash_request_ctx(req); |
| 208 | struct crypto_hash_walk walk; |
| 209 | int nbytes; |
| 210 | |
| 211 | for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0; |
| 212 | nbytes = crypto_hash_walk_done(&walk, nbytes)) |
| 213 | nbytes = crypto_shash_update(desc, walk.data, nbytes); |
| 214 | |
| 215 | return nbytes; |
| 216 | } |
| 217 | |
| 218 | static int shash_async_final(struct ahash_request *req) |
| 219 | { |
| 220 | return crypto_shash_final(ahash_request_ctx(req), req->result); |
| 221 | } |
| 222 | |
| 223 | static int shash_async_digest(struct ahash_request *req) |
| 224 | { |
| 225 | struct scatterlist *sg = req->src; |
| 226 | unsigned int offset = sg->offset; |
| 227 | unsigned int nbytes = req->nbytes; |
| 228 | int err; |
| 229 | |
| 230 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { |
| 231 | struct crypto_shash **ctx = |
| 232 | crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
| 233 | struct shash_desc *desc = ahash_request_ctx(req); |
| 234 | void *data; |
| 235 | |
| 236 | desc->tfm = *ctx; |
| 237 | desc->flags = req->base.flags; |
| 238 | |
| 239 | data = crypto_kmap(sg_page(sg), 0); |
| 240 | err = crypto_shash_digest(desc, data + offset, nbytes, |
| 241 | req->result); |
| 242 | crypto_kunmap(data, 0); |
| 243 | crypto_yield(desc->flags); |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | err = shash_async_init(req); |
| 248 | if (err) |
| 249 | goto out; |
| 250 | |
| 251 | err = shash_async_update(req); |
| 252 | if (err) |
| 253 | goto out; |
| 254 | |
| 255 | err = shash_async_final(req); |
| 256 | |
| 257 | out: |
| 258 | return err; |
| 259 | } |
| 260 | |
| 261 | static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) |
| 262 | { |
| 263 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); |
| 264 | |
| 265 | crypto_free_shash(*ctx); |
| 266 | } |
| 267 | |
| 268 | static int crypto_init_shash_ops_async(struct crypto_tfm *tfm) |
| 269 | { |
| 270 | struct crypto_alg *calg = tfm->__crt_alg; |
| 271 | struct shash_alg *alg = __crypto_shash_alg(calg); |
| 272 | struct ahash_tfm *crt = &tfm->crt_ahash; |
| 273 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); |
| 274 | struct crypto_shash *shash; |
| 275 | |
| 276 | if (!crypto_mod_get(calg)) |
| 277 | return -EAGAIN; |
| 278 | |
Herbert Xu | 3f683d6 | 2009-02-18 16:56:59 +0800 | [diff] [blame] | 279 | shash = crypto_create_tfm(calg, &crypto_shash_type); |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 280 | if (IS_ERR(shash)) { |
| 281 | crypto_mod_put(calg); |
| 282 | return PTR_ERR(shash); |
| 283 | } |
| 284 | |
| 285 | *ctx = shash; |
| 286 | tfm->exit = crypto_exit_shash_ops_async; |
| 287 | |
| 288 | crt->init = shash_async_init; |
| 289 | crt->update = shash_async_update; |
| 290 | crt->final = shash_async_final; |
| 291 | crt->digest = shash_async_digest; |
| 292 | crt->setkey = shash_async_setkey; |
| 293 | |
| 294 | crt->digestsize = alg->digestsize; |
| 295 | crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 300 | static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key, |
| 301 | unsigned int keylen) |
| 302 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 303 | struct shash_desc **descp = crypto_hash_ctx(tfm); |
| 304 | struct shash_desc *desc = *descp; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 305 | |
| 306 | return crypto_shash_setkey(desc->tfm, key, keylen); |
| 307 | } |
| 308 | |
| 309 | static int shash_compat_init(struct hash_desc *hdesc) |
| 310 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 311 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
| 312 | struct shash_desc *desc = *descp; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 313 | |
| 314 | desc->flags = hdesc->flags; |
| 315 | |
| 316 | return crypto_shash_init(desc); |
| 317 | } |
| 318 | |
| 319 | static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg, |
| 320 | unsigned int len) |
| 321 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 322 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
| 323 | struct shash_desc *desc = *descp; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 324 | struct crypto_hash_walk walk; |
| 325 | int nbytes; |
| 326 | |
| 327 | for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len); |
| 328 | nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes)) |
| 329 | nbytes = crypto_shash_update(desc, walk.data, nbytes); |
| 330 | |
| 331 | return nbytes; |
| 332 | } |
| 333 | |
| 334 | static int shash_compat_final(struct hash_desc *hdesc, u8 *out) |
| 335 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 336 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
| 337 | |
| 338 | return crypto_shash_final(*descp, out); |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg, |
| 342 | unsigned int nbytes, u8 *out) |
| 343 | { |
| 344 | unsigned int offset = sg->offset; |
| 345 | int err; |
| 346 | |
| 347 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 348 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
| 349 | struct shash_desc *desc = *descp; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 350 | void *data; |
| 351 | |
| 352 | desc->flags = hdesc->flags; |
| 353 | |
| 354 | data = crypto_kmap(sg_page(sg), 0); |
| 355 | err = crypto_shash_digest(desc, data + offset, nbytes, out); |
| 356 | crypto_kunmap(data, 0); |
| 357 | crypto_yield(desc->flags); |
| 358 | goto out; |
| 359 | } |
| 360 | |
| 361 | err = shash_compat_init(hdesc); |
| 362 | if (err) |
| 363 | goto out; |
| 364 | |
| 365 | err = shash_compat_update(hdesc, sg, nbytes); |
| 366 | if (err) |
| 367 | goto out; |
| 368 | |
| 369 | err = shash_compat_final(hdesc, out); |
| 370 | |
| 371 | out: |
| 372 | return err; |
| 373 | } |
| 374 | |
| 375 | static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm) |
| 376 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 377 | struct shash_desc **descp = crypto_tfm_ctx(tfm); |
| 378 | struct shash_desc *desc = *descp; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 379 | |
| 380 | crypto_free_shash(desc->tfm); |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 381 | kzfree(desc); |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm) |
| 385 | { |
| 386 | struct hash_tfm *crt = &tfm->crt_hash; |
| 387 | struct crypto_alg *calg = tfm->__crt_alg; |
| 388 | struct shash_alg *alg = __crypto_shash_alg(calg); |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 389 | struct shash_desc **descp = crypto_tfm_ctx(tfm); |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 390 | struct crypto_shash *shash; |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 391 | struct shash_desc *desc; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 392 | |
Adrian-Ken Rueegsegger | 4abfd73 | 2009-02-05 16:19:31 +1100 | [diff] [blame] | 393 | if (!crypto_mod_get(calg)) |
| 394 | return -EAGAIN; |
| 395 | |
Herbert Xu | 3f683d6 | 2009-02-18 16:56:59 +0800 | [diff] [blame] | 396 | shash = crypto_create_tfm(calg, &crypto_shash_type); |
Adrian-Ken Rueegsegger | 4abfd73 | 2009-02-05 16:19:31 +1100 | [diff] [blame] | 397 | if (IS_ERR(shash)) { |
| 398 | crypto_mod_put(calg); |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 399 | return PTR_ERR(shash); |
Adrian-Ken Rueegsegger | 4abfd73 | 2009-02-05 16:19:31 +1100 | [diff] [blame] | 400 | } |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 401 | |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 402 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash), |
| 403 | GFP_KERNEL); |
| 404 | if (!desc) { |
| 405 | crypto_free_shash(shash); |
| 406 | return -ENOMEM; |
| 407 | } |
| 408 | |
| 409 | *descp = desc; |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 410 | desc->tfm = shash; |
| 411 | tfm->exit = crypto_exit_shash_ops_compat; |
| 412 | |
| 413 | crt->init = shash_compat_init; |
| 414 | crt->update = shash_compat_update; |
| 415 | crt->final = shash_compat_final; |
| 416 | crt->digest = shash_compat_digest; |
| 417 | crt->setkey = shash_compat_setkey; |
| 418 | |
| 419 | crt->digestsize = alg->digestsize; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 424 | static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
| 425 | { |
| 426 | switch (mask & CRYPTO_ALG_TYPE_MASK) { |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 427 | case CRYPTO_ALG_TYPE_HASH_MASK: |
| 428 | return crypto_init_shash_ops_compat(tfm); |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 429 | case CRYPTO_ALG_TYPE_AHASH_MASK: |
| 430 | return crypto_init_shash_ops_async(tfm); |
| 431 | } |
| 432 | |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type, |
| 437 | u32 mask) |
| 438 | { |
| 439 | switch (mask & CRYPTO_ALG_TYPE_MASK) { |
Herbert Xu | 5f7082e | 2008-08-31 22:21:09 +1000 | [diff] [blame] | 440 | case CRYPTO_ALG_TYPE_HASH_MASK: |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 441 | return sizeof(struct shash_desc *); |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 442 | case CRYPTO_ALG_TYPE_AHASH_MASK: |
| 443 | return sizeof(struct crypto_shash *); |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 449 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm, |
| 450 | const struct crypto_type *frontend) |
| 451 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame^] | 452 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
| 453 | |
| 454 | hash->descsize = crypto_shash_alg(hash)->descsize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static unsigned int crypto_shash_extsize(struct crypto_alg *alg, |
| 459 | const struct crypto_type *frontend) |
| 460 | { |
| 461 | return alg->cra_ctxsize; |
| 462 | } |
| 463 | |
| 464 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
| 465 | __attribute__ ((unused)); |
| 466 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
| 467 | { |
| 468 | struct shash_alg *salg = __crypto_shash_alg(alg); |
| 469 | |
| 470 | seq_printf(m, "type : shash\n"); |
| 471 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 472 | seq_printf(m, "digestsize : %u\n", salg->digestsize); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static const struct crypto_type crypto_shash_type = { |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 476 | .ctxsize = crypto_shash_ctxsize, |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 477 | .extsize = crypto_shash_extsize, |
Herbert Xu | 3b2f6df | 2008-08-31 18:52:18 +1000 | [diff] [blame] | 478 | .init = crypto_init_shash_ops, |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 479 | .init_tfm = crypto_shash_init_tfm, |
| 480 | #ifdef CONFIG_PROC_FS |
| 481 | .show = crypto_shash_show, |
| 482 | #endif |
| 483 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 484 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 485 | .type = CRYPTO_ALG_TYPE_SHASH, |
| 486 | .tfmsize = offsetof(struct crypto_shash, base), |
| 487 | }; |
| 488 | |
| 489 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
| 490 | u32 mask) |
| 491 | { |
Herbert Xu | 3f683d6 | 2009-02-18 16:56:59 +0800 | [diff] [blame] | 492 | return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 493 | } |
| 494 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); |
| 495 | |
Herbert Xu | 619a6eb | 2009-07-08 18:46:23 +0800 | [diff] [blame] | 496 | static int shash_prepare_alg(struct shash_alg *alg) |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 497 | { |
| 498 | struct crypto_alg *base = &alg->base; |
| 499 | |
| 500 | if (alg->digestsize > PAGE_SIZE / 8 || |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 501 | alg->descsize > PAGE_SIZE / 8 || |
| 502 | alg->statesize > PAGE_SIZE / 8) |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 503 | return -EINVAL; |
| 504 | |
| 505 | base->cra_type = &crypto_shash_type; |
| 506 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 507 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 508 | |
Herbert Xu | 8267ada | 2009-07-09 20:36:44 +0800 | [diff] [blame] | 509 | if (!alg->finup) |
| 510 | alg->finup = shash_finup_unaligned; |
| 511 | if (!alg->digest) |
| 512 | alg->digest = shash_digest_unaligned; |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 513 | if (!alg->import) |
| 514 | alg->import = shash_no_import; |
| 515 | if (!alg->export) |
| 516 | alg->export = shash_no_export; |
Herbert Xu | 57cfe44 | 2009-07-11 22:17:39 +0800 | [diff] [blame] | 517 | if (!alg->setkey) |
| 518 | alg->setkey = shash_no_setkey; |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 519 | |
Herbert Xu | 619a6eb | 2009-07-08 18:46:23 +0800 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | int crypto_register_shash(struct shash_alg *alg) |
| 524 | { |
| 525 | struct crypto_alg *base = &alg->base; |
| 526 | int err; |
| 527 | |
| 528 | err = shash_prepare_alg(alg); |
| 529 | if (err) |
| 530 | return err; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 531 | |
| 532 | return crypto_register_alg(base); |
| 533 | } |
| 534 | EXPORT_SYMBOL_GPL(crypto_register_shash); |
| 535 | |
| 536 | int crypto_unregister_shash(struct shash_alg *alg) |
| 537 | { |
| 538 | return crypto_unregister_alg(&alg->base); |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); |
| 541 | |
Herbert Xu | 619a6eb | 2009-07-08 18:46:23 +0800 | [diff] [blame] | 542 | int shash_register_instance(struct crypto_template *tmpl, |
| 543 | struct shash_instance *inst) |
| 544 | { |
| 545 | int err; |
| 546 | |
| 547 | err = shash_prepare_alg(&inst->alg); |
| 548 | if (err) |
| 549 | return err; |
| 550 | |
| 551 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); |
| 552 | } |
| 553 | EXPORT_SYMBOL_GPL(shash_register_instance); |
| 554 | |
Herbert Xu | 2e4fddd | 2009-07-07 15:17:12 +0800 | [diff] [blame] | 555 | void shash_free_instance(struct crypto_instance *inst) |
| 556 | { |
| 557 | crypto_drop_spawn(crypto_instance_ctx(inst)); |
| 558 | kfree(shash_instance(inst)); |
| 559 | } |
| 560 | EXPORT_SYMBOL_GPL(shash_free_instance); |
| 561 | |
Herbert Xu | 9429699 | 2009-07-08 17:21:37 +0800 | [diff] [blame] | 562 | int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn, |
| 563 | struct shash_alg *alg, |
| 564 | struct crypto_instance *inst) |
| 565 | { |
| 566 | return crypto_init_spawn2(&spawn->base, &alg->base, inst, |
| 567 | &crypto_shash_type); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(crypto_init_shash_spawn); |
| 570 | |
Herbert Xu | 7d6f5640 | 2009-07-08 17:56:28 +0800 | [diff] [blame] | 571 | struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask) |
| 572 | { |
| 573 | struct crypto_alg *alg; |
| 574 | |
| 575 | alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask); |
| 576 | return IS_ERR(alg) ? ERR_CAST(alg) : |
| 577 | container_of(alg, struct shash_alg, base); |
| 578 | } |
| 579 | EXPORT_SYMBOL_GPL(shash_attr_alg); |
| 580 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 581 | MODULE_LICENSE("GPL"); |
| 582 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |