crypto: ahash - Add unaligned handling and default operations
[linux-2.6-block.git] / include / crypto / hash.h
CommitLineData
18e33e6d
HX
1/*
2 * Hash: Hash algorithms under the crypto API
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
13#ifndef _CRYPTO_HASH_H
14#define _CRYPTO_HASH_H
15
16#include <linux/crypto.h>
17
88056ec3
HX
18struct crypto_ahash;
19
20struct hash_alg_common {
21 unsigned int digestsize;
22 unsigned int statesize;
23
24 struct crypto_alg base;
25};
26
27struct ahash_request {
28 struct crypto_async_request base;
29
30 unsigned int nbytes;
31 struct scatterlist *src;
32 u8 *result;
33
66f6ce5e
HX
34 /* This field may only be used by the ahash API code. */
35 void *priv;
36
88056ec3
HX
37 void *__ctx[] CRYPTO_MINALIGN_ATTR;
38};
39
40struct ahash_alg {
41 int (*init)(struct ahash_request *req);
42 int (*update)(struct ahash_request *req);
43 int (*final)(struct ahash_request *req);
44 int (*finup)(struct ahash_request *req);
45 int (*digest)(struct ahash_request *req);
46 int (*export)(struct ahash_request *req, void *out);
47 int (*import)(struct ahash_request *req, const void *in);
48 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
49 unsigned int keylen);
50
51 struct hash_alg_common halg;
52};
53
7b5a080b
HX
54struct shash_desc {
55 struct crypto_shash *tfm;
56 u32 flags;
57
58 void *__ctx[] CRYPTO_MINALIGN_ATTR;
59};
60
61struct shash_alg {
62 int (*init)(struct shash_desc *desc);
63 int (*update)(struct shash_desc *desc, const u8 *data,
64 unsigned int len);
65 int (*final)(struct shash_desc *desc, u8 *out);
66 int (*finup)(struct shash_desc *desc, const u8 *data,
67 unsigned int len, u8 *out);
68 int (*digest)(struct shash_desc *desc, const u8 *data,
69 unsigned int len, u8 *out);
99d27e1c
HX
70 int (*export)(struct shash_desc *desc, void *out);
71 int (*import)(struct shash_desc *desc, const void *in);
7b5a080b
HX
72 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
73 unsigned int keylen);
74
75 unsigned int descsize;
88056ec3
HX
76
77 /* These fields must match hash_alg_common. */
7b5a080b 78 unsigned int digestsize;
99d27e1c 79 unsigned int statesize;
7b5a080b
HX
80
81 struct crypto_alg base;
82};
83
18e33e6d 84struct crypto_ahash {
88056ec3
HX
85 int (*init)(struct ahash_request *req);
86 int (*update)(struct ahash_request *req);
87 int (*final)(struct ahash_request *req);
88 int (*finup)(struct ahash_request *req);
89 int (*digest)(struct ahash_request *req);
90 int (*export)(struct ahash_request *req, void *out);
91 int (*import)(struct ahash_request *req, const void *in);
92 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
93 unsigned int keylen);
94
88056ec3 95 unsigned int reqsize;
18e33e6d
HX
96 struct crypto_tfm base;
97};
98
7b5a080b 99struct crypto_shash {
113adefc 100 unsigned int descsize;
7b5a080b
HX
101 struct crypto_tfm base;
102};
103
18e33e6d
HX
104static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
105{
88056ec3 106 return container_of(tfm, struct crypto_ahash, base);
18e33e6d
HX
107}
108
88056ec3
HX
109struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
110 u32 mask);
18e33e6d
HX
111
112static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
113{
114 return &tfm->base;
115}
116
117static inline void crypto_free_ahash(struct crypto_ahash *tfm)
118{
88056ec3 119 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
18e33e6d
HX
120}
121
122static inline unsigned int crypto_ahash_alignmask(
123 struct crypto_ahash *tfm)
124{
125 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
126}
127
88056ec3
HX
128static inline struct hash_alg_common *__crypto_hash_alg_common(
129 struct crypto_alg *alg)
130{
131 return container_of(alg, struct hash_alg_common, base);
132}
133
134static inline struct hash_alg_common *crypto_hash_alg_common(
135 struct crypto_ahash *tfm)
18e33e6d 136{
88056ec3 137 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
18e33e6d
HX
138}
139
140static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
141{
500b3e3c 142 return crypto_hash_alg_common(tfm)->digestsize;
88056ec3
HX
143}
144
145static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
146{
147 return crypto_hash_alg_common(tfm)->statesize;
18e33e6d
HX
148}
149
150static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
151{
152 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
153}
154
155static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
156{
157 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
158}
159
160static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
161{
162 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
163}
164
165static inline struct crypto_ahash *crypto_ahash_reqtfm(
166 struct ahash_request *req)
167{
168 return __crypto_ahash_cast(req->base.tfm);
169}
170
171static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
172{
88056ec3 173 return tfm->reqsize;
18e33e6d
HX
174}
175
dec8b786
HX
176static inline void *ahash_request_ctx(struct ahash_request *req)
177{
178 return req->__ctx;
179}
180
66f6ce5e
HX
181int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
182 unsigned int keylen);
183int crypto_ahash_finup(struct ahash_request *req);
184int crypto_ahash_final(struct ahash_request *req);
185int crypto_ahash_digest(struct ahash_request *req);
18e33e6d 186
88056ec3 187static inline int crypto_ahash_export(struct ahash_request *req, void *out)
dec8b786 188{
88056ec3 189 return crypto_ahash_reqtfm(req)->export(req, out);
dec8b786
HX
190}
191
88056ec3
HX
192static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
193{
194 return crypto_ahash_reqtfm(req)->import(req, in);
195}
dec8b786 196
318e5313
HX
197static inline int crypto_ahash_init(struct ahash_request *req)
198{
88056ec3 199 return crypto_ahash_reqtfm(req)->init(req);
318e5313
HX
200}
201
202static inline int crypto_ahash_update(struct ahash_request *req)
203{
88056ec3 204 return crypto_ahash_reqtfm(req)->update(req);
318e5313
HX
205}
206
18e33e6d
HX
207static inline void ahash_request_set_tfm(struct ahash_request *req,
208 struct crypto_ahash *tfm)
209{
210 req->base.tfm = crypto_ahash_tfm(tfm);
211}
212
213static inline struct ahash_request *ahash_request_alloc(
214 struct crypto_ahash *tfm, gfp_t gfp)
215{
216 struct ahash_request *req;
217
218 req = kmalloc(sizeof(struct ahash_request) +
219 crypto_ahash_reqsize(tfm), gfp);
220
221 if (likely(req))
222 ahash_request_set_tfm(req, tfm);
223
224 return req;
225}
226
227static inline void ahash_request_free(struct ahash_request *req)
228{
aef73cfc 229 kzfree(req);
18e33e6d
HX
230}
231
232static inline struct ahash_request *ahash_request_cast(
233 struct crypto_async_request *req)
234{
235 return container_of(req, struct ahash_request, base);
236}
237
238static inline void ahash_request_set_callback(struct ahash_request *req,
239 u32 flags,
240 crypto_completion_t complete,
241 void *data)
242{
243 req->base.complete = complete;
244 req->base.data = data;
245 req->base.flags = flags;
246}
247
248static inline void ahash_request_set_crypt(struct ahash_request *req,
249 struct scatterlist *src, u8 *result,
250 unsigned int nbytes)
251{
252 req->src = src;
253 req->nbytes = nbytes;
254 req->result = result;
255}
256
7b5a080b
HX
257struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
258 u32 mask);
259
260static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
261{
262 return &tfm->base;
263}
264
265static inline void crypto_free_shash(struct crypto_shash *tfm)
266{
412e87ae 267 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
7b5a080b
HX
268}
269
270static inline unsigned int crypto_shash_alignmask(
271 struct crypto_shash *tfm)
272{
273 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
274}
275
97495986
HX
276static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
277{
278 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
279}
280
7b5a080b
HX
281static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
282{
283 return container_of(alg, struct shash_alg, base);
284}
285
286static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
287{
288 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
289}
290
291static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
292{
293 return crypto_shash_alg(tfm)->digestsize;
294}
295
99d27e1c
HX
296static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
297{
298 return crypto_shash_alg(tfm)->statesize;
299}
300
7b5a080b
HX
301static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
302{
303 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
304}
305
306static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
307{
308 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
309}
310
311static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
312{
313 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
314}
315
316static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
317{
113adefc 318 return tfm->descsize;
7b5a080b
HX
319}
320
321static inline void *shash_desc_ctx(struct shash_desc *desc)
322{
323 return desc->__ctx;
324}
325
326int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
327 unsigned int keylen);
328int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
329 unsigned int len, u8 *out);
330
99d27e1c 331static inline int crypto_shash_export(struct shash_desc *desc, void *out)
dec8b786 332{
99d27e1c 333 return crypto_shash_alg(desc->tfm)->export(desc, out);
dec8b786
HX
334}
335
99d27e1c
HX
336static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
337{
338 return crypto_shash_alg(desc->tfm)->import(desc, in);
339}
dec8b786 340
7b5a080b
HX
341static inline int crypto_shash_init(struct shash_desc *desc)
342{
343 return crypto_shash_alg(desc->tfm)->init(desc);
344}
345
346int crypto_shash_update(struct shash_desc *desc, const u8 *data,
347 unsigned int len);
348int crypto_shash_final(struct shash_desc *desc, u8 *out);
349int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
350 unsigned int len, u8 *out);
351
18e33e6d 352#endif /* _CRYPTO_HASH_H */