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