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