blob: 0d7caa9ab748d96883f4dbfeb2612aab5b5e314f [file] [log] [blame]
Herbert Xu055bcee2006-08-19 22:24:23 +10001/*
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>
Herbert Xu791b4d52007-08-23 16:23:01 +080015#include <linux/slab.h>
Herbert Xu055bcee2006-08-19 22:24:23 +100016#include <linux/seq_file.h>
17
18#include "internal.h"
19
Herbert Xu27d2a332007-01-24 20:50:26 +110020static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type,
21 u32 mask)
Herbert Xu055bcee2006-08-19 22:24:23 +100022{
23 return alg->cra_ctxsize;
24}
25
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100026static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key,
27 unsigned int keylen)
28{
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;
35
36 absize = keylen + alignmask;
37 buffer = kmalloc(absize, GFP_ATOMIC);
38 if (!buffer)
39 return -ENOMEM;
40
41 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
42 memcpy(alignbuffer, key, keylen);
43 ret = alg->setkey(crt, alignbuffer, keylen);
Sebastian Siewior06817172007-08-03 20:33:47 +080044 memset(alignbuffer, 0, keylen);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100045 kfree(buffer);
46 return ret;
47}
48
49static int hash_setkey(struct crypto_hash *crt, const u8 *key,
Herbert Xu791b4d52007-08-23 16:23:01 +080050 unsigned int keylen)
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100051{
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);
55
56 if ((unsigned long)key & alignmask)
57 return hash_setkey_unaligned(crt, key, keylen);
58
59 return alg->setkey(crt, key, keylen);
60}
61
Loc Ho004a4032008-05-14 20:41:47 +080062static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
63 unsigned int keylen)
64{
65 struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
66 struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
67 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
68
69 return alg->setkey(tfm_hash, key, keylen);
70}
71
72static int hash_async_init(struct ahash_request *req)
73{
74 struct crypto_tfm *tfm = req->base.tfm;
75 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
76 struct hash_desc desc = {
77 .tfm = __crypto_hash_cast(tfm),
78 .flags = req->base.flags,
79 };
80
81 return alg->init(&desc);
82}
83
84static int hash_async_update(struct ahash_request *req)
85{
86 struct crypto_tfm *tfm = req->base.tfm;
87 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
88 struct hash_desc desc = {
89 .tfm = __crypto_hash_cast(tfm),
90 .flags = req->base.flags,
91 };
92
93 return alg->update(&desc, req->src, req->nbytes);
94}
95
96static int hash_async_final(struct ahash_request *req)
97{
98 struct crypto_tfm *tfm = req->base.tfm;
99 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
100 struct hash_desc desc = {
101 .tfm = __crypto_hash_cast(tfm),
102 .flags = req->base.flags,
103 };
104
105 return alg->final(&desc, req->result);
106}
107
108static int hash_async_digest(struct ahash_request *req)
109{
110 struct crypto_tfm *tfm = req->base.tfm;
111 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
112 struct hash_desc desc = {
113 .tfm = __crypto_hash_cast(tfm),
114 .flags = req->base.flags,
115 };
116
117 return alg->digest(&desc, req->src, req->nbytes, req->result);
118}
119
120static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
121{
122 struct ahash_tfm *crt = &tfm->crt_ahash;
123 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
124
125 crt->init = hash_async_init;
126 crt->update = hash_async_update;
127 crt->final = hash_async_final;
128 crt->digest = hash_async_digest;
129 crt->setkey = hash_async_setkey;
130 crt->digestsize = alg->digestsize;
131 crt->base = __crypto_ahash_cast(tfm);
132
133 return 0;
134}
135
136static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
Herbert Xu055bcee2006-08-19 22:24:23 +1000137{
138 struct hash_tfm *crt = &tfm->crt_hash;
139 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
140
Loc Ho004a4032008-05-14 20:41:47 +0800141 crt->init = alg->init;
142 crt->update = alg->update;
143 crt->final = alg->final;
144 crt->digest = alg->digest;
145 crt->setkey = hash_setkey;
146 crt->digestsize = alg->digestsize;
147
148 return 0;
149}
150
151static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
152{
153 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
154
Herbert Xuca786dc2008-07-07 20:23:56 +0800155 if (alg->digestsize > PAGE_SIZE / 8)
Herbert Xu055bcee2006-08-19 22:24:23 +1000156 return -EINVAL;
157
Loc Ho004a4032008-05-14 20:41:47 +0800158 if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
159 return crypto_init_hash_ops_async(tfm);
160 else
161 return crypto_init_hash_ops_sync(tfm);
Herbert Xu055bcee2006-08-19 22:24:23 +1000162}
163
164static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
Herbert Xu03f5d8c2006-12-31 10:42:06 +1100165 __attribute__ ((unused));
Herbert Xu055bcee2006-08-19 22:24:23 +1000166static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
167{
168 seq_printf(m, "type : hash\n");
169 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
170 seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
171}
172
173const struct crypto_type crypto_hash_type = {
174 .ctxsize = crypto_hash_ctxsize,
175 .init = crypto_init_hash_ops,
176#ifdef CONFIG_PROC_FS
177 .show = crypto_hash_show,
178#endif
179};
180EXPORT_SYMBOL_GPL(crypto_hash_type);
181
182MODULE_LICENSE("GPL");
183MODULE_DESCRIPTION("Generic cryptographic hash type");