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