usb: cleanup usb_register_bus() and hook up sysfs group
[linux-2.6/kmemtrace.git] / crypto / hash.c
blob7dcff671c19b6d91a6127467b57c4db91a23fff4
1 /*
2 * Cryptographic Hash operations.
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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.
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
18 #include "internal.h"
20 static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type,
21 u32 mask)
23 return alg->cra_ctxsize;
26 static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key,
27 unsigned int keylen)
29 struct crypto_tfm *tfm = crypto_hash_tfm(crt);
30 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
31 unsigned long alignmask = crypto_hash_alignmask(crt);
32 int ret;
33 u8 *buffer, *alignbuffer;
34 unsigned long absize;
36 absize = keylen + alignmask;
37 buffer = kmalloc(absize, GFP_ATOMIC);
38 if (!buffer)
39 return -ENOMEM;
41 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
42 memcpy(alignbuffer, key, keylen);
43 ret = alg->setkey(crt, alignbuffer, keylen);
44 memset(alignbuffer, 0, keylen);
45 kfree(buffer);
46 return ret;
49 static int hash_setkey(struct crypto_hash *crt, const u8 *key,
50 unsigned int keylen)
52 struct crypto_tfm *tfm = crypto_hash_tfm(crt);
53 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
54 unsigned long alignmask = crypto_hash_alignmask(crt);
56 if ((unsigned long)key & alignmask)
57 return hash_setkey_unaligned(crt, key, keylen);
59 return alg->setkey(crt, key, keylen);
62 static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
64 struct hash_tfm *crt = &tfm->crt_hash;
65 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
67 if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
68 return -EINVAL;
70 crt->init = alg->init;
71 crt->update = alg->update;
72 crt->final = alg->final;
73 crt->digest = alg->digest;
74 crt->setkey = hash_setkey;
75 crt->digestsize = alg->digestsize;
77 return 0;
80 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
81 __attribute__ ((unused));
82 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
84 seq_printf(m, "type : hash\n");
85 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
86 seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
89 const struct crypto_type crypto_hash_type = {
90 .ctxsize = crypto_hash_ctxsize,
91 .init = crypto_init_hash_ops,
92 #ifdef CONFIG_PROC_FS
93 .show = crypto_hash_show,
94 #endif
96 EXPORT_SYMBOL_GPL(crypto_hash_type);
98 MODULE_LICENSE("GPL");
99 MODULE_DESCRIPTION("Generic cryptographic hash type");