Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic Hash operations. |
| 3 | * |
| 4 | * Copyright (c) 2006 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 | #include <linux/errno.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | |
| 17 | #include "internal.h" |
| 18 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 19 | static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type, |
| 20 | u32 mask) |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 21 | { |
| 22 | return alg->cra_ctxsize; |
| 23 | } |
| 24 | |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 25 | static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key, |
| 26 | unsigned int keylen) |
| 27 | { |
| 28 | struct crypto_tfm *tfm = crypto_hash_tfm(crt); |
| 29 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; |
| 30 | unsigned long alignmask = crypto_hash_alignmask(crt); |
| 31 | int ret; |
| 32 | u8 *buffer, *alignbuffer; |
| 33 | unsigned long absize; |
| 34 | |
| 35 | absize = keylen + alignmask; |
| 36 | buffer = kmalloc(absize, GFP_ATOMIC); |
| 37 | if (!buffer) |
| 38 | return -ENOMEM; |
| 39 | |
| 40 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
| 41 | memcpy(alignbuffer, key, keylen); |
| 42 | ret = alg->setkey(crt, alignbuffer, keylen); |
| 43 | memset(alignbuffer, 0, absize); |
| 44 | kfree(buffer); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static int hash_setkey(struct crypto_hash *crt, const u8 *key, |
| 49 | unsigned int keylen) |
| 50 | { |
| 51 | struct crypto_tfm *tfm = crypto_hash_tfm(crt); |
| 52 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; |
| 53 | unsigned long alignmask = crypto_hash_alignmask(crt); |
| 54 | |
| 55 | if ((unsigned long)key & alignmask) |
| 56 | return hash_setkey_unaligned(crt, key, keylen); |
| 57 | |
| 58 | return alg->setkey(crt, key, keylen); |
| 59 | } |
| 60 | |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame] | 61 | static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 62 | { |
| 63 | struct hash_tfm *crt = &tfm->crt_hash; |
| 64 | struct hash_alg *alg = &tfm->__crt_alg->cra_hash; |
| 65 | |
| 66 | if (alg->digestsize > crypto_tfm_alg_blocksize(tfm)) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | crt->init = alg->init; |
| 70 | crt->update = alg->update; |
| 71 | crt->final = alg->final; |
| 72 | crt->digest = alg->digest; |
Sebastian Siewior | ca7c393 | 2007-05-19 19:51:21 +1000 | [diff] [blame] | 73 | crt->setkey = hash_setkey; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 74 | crt->digestsize = alg->digestsize; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg) |
Herbert Xu | 03f5d8c | 2006-12-31 10:42:06 +1100 | [diff] [blame] | 80 | __attribute__ ((unused)); |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 81 | static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg) |
| 82 | { |
| 83 | seq_printf(m, "type : hash\n"); |
| 84 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
| 85 | seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize); |
| 86 | } |
| 87 | |
| 88 | const struct crypto_type crypto_hash_type = { |
| 89 | .ctxsize = crypto_hash_ctxsize, |
| 90 | .init = crypto_init_hash_ops, |
| 91 | #ifdef CONFIG_PROC_FS |
| 92 | .show = crypto_hash_show, |
| 93 | #endif |
| 94 | }; |
| 95 | EXPORT_SYMBOL_GPL(crypto_hash_type); |
| 96 | |
| 97 | MODULE_LICENSE("GPL"); |
| 98 | MODULE_DESCRIPTION("Generic cryptographic hash type"); |