]>
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 | ||
5d8c723f SM |
20 | /** |
21 | * DOC: Message Digest Algorithm Definitions | |
22 | * | |
23 | * These data structures define modular message digest algorithm | |
24 | * implementations, managed via crypto_register_ahash(), | |
25 | * crypto_register_shash(), crypto_unregister_ahash() and | |
26 | * crypto_unregister_shash(). | |
27 | */ | |
28 | ||
29 | /** | |
30 | * struct hash_alg_common - define properties of message digest | |
31 | * @digestsize: Size of the result of the transformation. A buffer of this size | |
32 | * must be available to the @final and @finup calls, so they can | |
33 | * store the resulting hash into it. For various predefined sizes, | |
34 | * search include/crypto/ using | |
35 | * git grep _DIGEST_SIZE include/crypto. | |
36 | * @statesize: Size of the block for partial state of the transformation. A | |
37 | * buffer of this size must be passed to the @export function as it | |
38 | * will save the partial state of the transformation into it. On the | |
39 | * other side, the @import function will load the state from a | |
40 | * buffer of this size as well. | |
41 | */ | |
88056ec3 HX |
42 | struct hash_alg_common { |
43 | unsigned int digestsize; | |
44 | unsigned int statesize; | |
45 | ||
46 | struct crypto_alg base; | |
47 | }; | |
48 | ||
49 | struct ahash_request { | |
50 | struct crypto_async_request base; | |
51 | ||
52 | unsigned int nbytes; | |
53 | struct scatterlist *src; | |
54 | u8 *result; | |
55 | ||
66f6ce5e HX |
56 | /* This field may only be used by the ahash API code. */ |
57 | void *priv; | |
58 | ||
88056ec3 HX |
59 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
60 | }; | |
61 | ||
5d8c723f SM |
62 | /** |
63 | * struct ahash_alg - asynchronous message digest definition | |
64 | * @init: Initialize the transformation context. Intended only to initialize the | |
65 | * state of the HASH transformation at the begining. This shall fill in | |
66 | * the internal structures used during the entire duration of the whole | |
67 | * transformation. No data processing happens at this point. | |
68 | * @update: Push a chunk of data into the driver for transformation. This | |
69 | * function actually pushes blocks of data from upper layers into the | |
70 | * driver, which then passes those to the hardware as seen fit. This | |
71 | * function must not finalize the HASH transformation by calculating the | |
72 | * final message digest as this only adds more data into the | |
73 | * transformation. This function shall not modify the transformation | |
74 | * context, as this function may be called in parallel with the same | |
75 | * transformation object. Data processing can happen synchronously | |
76 | * [SHASH] or asynchronously [AHASH] at this point. | |
77 | * @final: Retrieve result from the driver. This function finalizes the | |
78 | * transformation and retrieves the resulting hash from the driver and | |
79 | * pushes it back to upper layers. No data processing happens at this | |
80 | * point. | |
81 | * @finup: Combination of @update and @final. This function is effectively a | |
82 | * combination of @update and @final calls issued in sequence. As some | |
83 | * hardware cannot do @update and @final separately, this callback was | |
84 | * added to allow such hardware to be used at least by IPsec. Data | |
85 | * processing can happen synchronously [SHASH] or asynchronously [AHASH] | |
86 | * at this point. | |
87 | * @digest: Combination of @init and @update and @final. This function | |
88 | * effectively behaves as the entire chain of operations, @init, | |
89 | * @update and @final issued in sequence. Just like @finup, this was | |
90 | * added for hardware which cannot do even the @finup, but can only do | |
91 | * the whole transformation in one run. Data processing can happen | |
92 | * synchronously [SHASH] or asynchronously [AHASH] at this point. | |
93 | * @setkey: Set optional key used by the hashing algorithm. Intended to push | |
94 | * optional key used by the hashing algorithm from upper layers into | |
95 | * the driver. This function can store the key in the transformation | |
96 | * context or can outright program it into the hardware. In the former | |
97 | * case, one must be careful to program the key into the hardware at | |
98 | * appropriate time and one must be careful that .setkey() can be | |
99 | * called multiple times during the existence of the transformation | |
100 | * object. Not all hashing algorithms do implement this function as it | |
101 | * is only needed for keyed message digests. SHAx/MDx/CRCx do NOT | |
102 | * implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement | |
103 | * this function. This function must be called before any other of the | |
104 | * @init, @update, @final, @finup, @digest is called. No data | |
105 | * processing happens at this point. | |
106 | * @export: Export partial state of the transformation. This function dumps the | |
107 | * entire state of the ongoing transformation into a provided block of | |
108 | * data so it can be @import 'ed back later on. This is useful in case | |
109 | * you want to save partial result of the transformation after | |
110 | * processing certain amount of data and reload this partial result | |
111 | * multiple times later on for multiple re-use. No data processing | |
112 | * happens at this point. | |
113 | * @import: Import partial state of the transformation. This function loads the | |
114 | * entire state of the ongoing transformation from a provided block of | |
115 | * data so the transformation can continue from this point onward. No | |
116 | * data processing happens at this point. | |
117 | */ | |
88056ec3 HX |
118 | struct ahash_alg { |
119 | int (*init)(struct ahash_request *req); | |
120 | int (*update)(struct ahash_request *req); | |
121 | int (*final)(struct ahash_request *req); | |
122 | int (*finup)(struct ahash_request *req); | |
123 | int (*digest)(struct ahash_request *req); | |
124 | int (*export)(struct ahash_request *req, void *out); | |
125 | int (*import)(struct ahash_request *req, const void *in); | |
126 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, | |
127 | unsigned int keylen); | |
128 | ||
129 | struct hash_alg_common halg; | |
130 | }; | |
131 | ||
7b5a080b HX |
132 | struct shash_desc { |
133 | struct crypto_shash *tfm; | |
134 | u32 flags; | |
135 | ||
136 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | |
137 | }; | |
138 | ||
a0a77af1 BW |
139 | #define SHASH_DESC_ON_STACK(shash, ctx) \ |
140 | char __##shash##_desc[sizeof(struct shash_desc) + \ | |
141 | crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \ | |
142 | struct shash_desc *shash = (struct shash_desc *)__##shash##_desc | |
143 | ||
5d8c723f SM |
144 | /** |
145 | * struct shash_alg - synchronous message digest definition | |
146 | * @init: see struct ahash_alg | |
147 | * @update: see struct ahash_alg | |
148 | * @final: see struct ahash_alg | |
149 | * @finup: see struct ahash_alg | |
150 | * @digest: see struct ahash_alg | |
151 | * @export: see struct ahash_alg | |
152 | * @import: see struct ahash_alg | |
153 | * @setkey: see struct ahash_alg | |
154 | * @digestsize: see struct ahash_alg | |
155 | * @statesize: see struct ahash_alg | |
156 | * @dedcsize: Size of the operational state for the message digest. This state | |
157 | * size is the memory size that needs to be allocated for | |
158 | * shash_desc.__ctx | |
159 | * @base: internally used | |
160 | */ | |
7b5a080b HX |
161 | struct shash_alg { |
162 | int (*init)(struct shash_desc *desc); | |
163 | int (*update)(struct shash_desc *desc, const u8 *data, | |
164 | unsigned int len); | |
165 | int (*final)(struct shash_desc *desc, u8 *out); | |
166 | int (*finup)(struct shash_desc *desc, const u8 *data, | |
167 | unsigned int len, u8 *out); | |
168 | int (*digest)(struct shash_desc *desc, const u8 *data, | |
169 | unsigned int len, u8 *out); | |
99d27e1c HX |
170 | int (*export)(struct shash_desc *desc, void *out); |
171 | int (*import)(struct shash_desc *desc, const void *in); | |
7b5a080b HX |
172 | int (*setkey)(struct crypto_shash *tfm, const u8 *key, |
173 | unsigned int keylen); | |
174 | ||
175 | unsigned int descsize; | |
88056ec3 HX |
176 | |
177 | /* These fields must match hash_alg_common. */ | |
fa649664 HX |
178 | unsigned int digestsize |
179 | __attribute__ ((aligned(__alignof__(struct hash_alg_common)))); | |
99d27e1c | 180 | unsigned int statesize; |
7b5a080b HX |
181 | |
182 | struct crypto_alg base; | |
183 | }; | |
184 | ||
18e33e6d | 185 | struct crypto_ahash { |
88056ec3 HX |
186 | int (*init)(struct ahash_request *req); |
187 | int (*update)(struct ahash_request *req); | |
188 | int (*final)(struct ahash_request *req); | |
189 | int (*finup)(struct ahash_request *req); | |
190 | int (*digest)(struct ahash_request *req); | |
191 | int (*export)(struct ahash_request *req, void *out); | |
192 | int (*import)(struct ahash_request *req, const void *in); | |
193 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, | |
194 | unsigned int keylen); | |
195 | ||
88056ec3 | 196 | unsigned int reqsize; |
18e33e6d HX |
197 | struct crypto_tfm base; |
198 | }; | |
199 | ||
7b5a080b | 200 | struct crypto_shash { |
113adefc | 201 | unsigned int descsize; |
7b5a080b HX |
202 | struct crypto_tfm base; |
203 | }; | |
204 | ||
90240ffb SM |
205 | /** |
206 | * DOC: Asynchronous Message Digest API | |
207 | * | |
208 | * The asynchronous message digest API is used with the ciphers of type | |
209 | * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto) | |
210 | * | |
211 | * The asynchronous cipher operation discussion provided for the | |
212 | * CRYPTO_ALG_TYPE_ABLKCIPHER API applies here as well. | |
213 | */ | |
214 | ||
18e33e6d HX |
215 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
216 | { | |
88056ec3 | 217 | return container_of(tfm, struct crypto_ahash, base); |
18e33e6d HX |
218 | } |
219 | ||
90240ffb SM |
220 | /** |
221 | * crypto_alloc_ahash() - allocate ahash cipher handle | |
222 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
223 | * ahash cipher | |
224 | * @type: specifies the type of the cipher | |
225 | * @mask: specifies the mask for the cipher | |
226 | * | |
227 | * Allocate a cipher handle for an ahash. The returned struct | |
228 | * crypto_ahash is the cipher handle that is required for any subsequent | |
229 | * API invocation for that ahash. | |
230 | * | |
231 | * Return: allocated cipher handle in case of success; IS_ERR() is true in case | |
232 | * of an error, PTR_ERR() returns the error code. | |
233 | */ | |
88056ec3 HX |
234 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
235 | u32 mask); | |
18e33e6d HX |
236 | |
237 | static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) | |
238 | { | |
239 | return &tfm->base; | |
240 | } | |
241 | ||
90240ffb SM |
242 | /** |
243 | * crypto_free_ahash() - zeroize and free the ahash handle | |
244 | * @tfm: cipher handle to be freed | |
245 | */ | |
18e33e6d HX |
246 | static inline void crypto_free_ahash(struct crypto_ahash *tfm) |
247 | { | |
88056ec3 | 248 | crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm)); |
18e33e6d HX |
249 | } |
250 | ||
251 | static inline unsigned int crypto_ahash_alignmask( | |
252 | struct crypto_ahash *tfm) | |
253 | { | |
254 | return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm)); | |
255 | } | |
256 | ||
88056ec3 HX |
257 | static inline struct hash_alg_common *__crypto_hash_alg_common( |
258 | struct crypto_alg *alg) | |
259 | { | |
260 | return container_of(alg, struct hash_alg_common, base); | |
261 | } | |
262 | ||
263 | static inline struct hash_alg_common *crypto_hash_alg_common( | |
264 | struct crypto_ahash *tfm) | |
18e33e6d | 265 | { |
88056ec3 | 266 | return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg); |
18e33e6d HX |
267 | } |
268 | ||
90240ffb SM |
269 | /** |
270 | * crypto_ahash_digestsize() - obtain message digest size | |
271 | * @tfm: cipher handle | |
272 | * | |
273 | * The size for the message digest created by the message digest cipher | |
274 | * referenced with the cipher handle is returned. | |
275 | * | |
276 | * | |
277 | * Return: message digest size of cipher | |
278 | */ | |
18e33e6d HX |
279 | static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) |
280 | { | |
500b3e3c | 281 | return crypto_hash_alg_common(tfm)->digestsize; |
88056ec3 HX |
282 | } |
283 | ||
284 | static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm) | |
285 | { | |
286 | return crypto_hash_alg_common(tfm)->statesize; | |
18e33e6d HX |
287 | } |
288 | ||
289 | static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) | |
290 | { | |
291 | return crypto_tfm_get_flags(crypto_ahash_tfm(tfm)); | |
292 | } | |
293 | ||
294 | static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags) | |
295 | { | |
296 | crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags); | |
297 | } | |
298 | ||
299 | static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags) | |
300 | { | |
301 | crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags); | |
302 | } | |
303 | ||
90240ffb SM |
304 | /** |
305 | * crypto_ahash_reqtfm() - obtain cipher handle from request | |
306 | * @req: asynchronous request handle that contains the reference to the ahash | |
307 | * cipher handle | |
308 | * | |
309 | * Return the ahash cipher handle that is registered with the asynchronous | |
310 | * request handle ahash_request. | |
311 | * | |
312 | * Return: ahash cipher handle | |
313 | */ | |
18e33e6d HX |
314 | static inline struct crypto_ahash *crypto_ahash_reqtfm( |
315 | struct ahash_request *req) | |
316 | { | |
317 | return __crypto_ahash_cast(req->base.tfm); | |
318 | } | |
319 | ||
90240ffb SM |
320 | /** |
321 | * crypto_ahash_reqsize() - obtain size of the request data structure | |
322 | * @tfm: cipher handle | |
323 | * | |
324 | * Return the size of the ahash state size. With the crypto_ahash_export | |
325 | * function, the caller can export the state into a buffer whose size is | |
326 | * defined with this function. | |
327 | * | |
328 | * Return: size of the ahash state | |
329 | */ | |
18e33e6d HX |
330 | static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) |
331 | { | |
88056ec3 | 332 | return tfm->reqsize; |
18e33e6d HX |
333 | } |
334 | ||
dec8b786 HX |
335 | static inline void *ahash_request_ctx(struct ahash_request *req) |
336 | { | |
337 | return req->__ctx; | |
338 | } | |
339 | ||
90240ffb SM |
340 | /** |
341 | * crypto_ahash_setkey - set key for cipher handle | |
342 | * @tfm: cipher handle | |
343 | * @key: buffer holding the key | |
344 | * @keylen: length of the key in bytes | |
345 | * | |
346 | * The caller provided key is set for the ahash cipher. The cipher | |
347 | * handle must point to a keyed hash in order for this function to succeed. | |
348 | * | |
349 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred | |
350 | */ | |
66f6ce5e HX |
351 | int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
352 | unsigned int keylen); | |
90240ffb SM |
353 | |
354 | /** | |
355 | * crypto_ahash_finup() - update and finalize message digest | |
356 | * @req: reference to the ahash_request handle that holds all information | |
357 | * needed to perform the cipher operation | |
358 | * | |
359 | * This function is a "short-hand" for the function calls of | |
360 | * crypto_ahash_update and crypto_shash_final. The parameters have the same | |
361 | * meaning as discussed for those separate functions. | |
362 | * | |
363 | * Return: 0 if the message digest creation was successful; < 0 if an error | |
364 | * occurred | |
365 | */ | |
66f6ce5e | 366 | int crypto_ahash_finup(struct ahash_request *req); |
90240ffb SM |
367 | |
368 | /** | |
369 | * crypto_ahash_final() - calculate message digest | |
370 | * @req: reference to the ahash_request handle that holds all information | |
371 | * needed to perform the cipher operation | |
372 | * | |
373 | * Finalize the message digest operation and create the message digest | |
374 | * based on all data added to the cipher handle. The message digest is placed | |
375 | * into the output buffer registered with the ahash_request handle. | |
376 | * | |
377 | * Return: 0 if the message digest creation was successful; < 0 if an error | |
378 | * occurred | |
379 | */ | |
66f6ce5e | 380 | int crypto_ahash_final(struct ahash_request *req); |
90240ffb SM |
381 | |
382 | /** | |
383 | * crypto_ahash_digest() - calculate message digest for a buffer | |
384 | * @req: reference to the ahash_request handle that holds all information | |
385 | * needed to perform the cipher operation | |
386 | * | |
387 | * This function is a "short-hand" for the function calls of crypto_ahash_init, | |
388 | * crypto_ahash_update and crypto_ahash_final. The parameters have the same | |
389 | * meaning as discussed for those separate three functions. | |
390 | * | |
391 | * Return: 0 if the message digest creation was successful; < 0 if an error | |
392 | * occurred | |
393 | */ | |
66f6ce5e | 394 | int crypto_ahash_digest(struct ahash_request *req); |
18e33e6d | 395 | |
90240ffb SM |
396 | /** |
397 | * crypto_ahash_export() - extract current message digest state | |
398 | * @req: reference to the ahash_request handle whose state is exported | |
399 | * @out: output buffer of sufficient size that can hold the hash state | |
400 | * | |
401 | * This function exports the hash state of the ahash_request handle into the | |
402 | * caller-allocated output buffer out which must have sufficient size (e.g. by | |
403 | * calling crypto_ahash_reqsize). | |
404 | * | |
405 | * Return: 0 if the export was successful; < 0 if an error occurred | |
406 | */ | |
88056ec3 | 407 | static inline int crypto_ahash_export(struct ahash_request *req, void *out) |
dec8b786 | 408 | { |
88056ec3 | 409 | return crypto_ahash_reqtfm(req)->export(req, out); |
dec8b786 HX |
410 | } |
411 | ||
90240ffb SM |
412 | /** |
413 | * crypto_ahash_import() - import message digest state | |
414 | * @req: reference to ahash_request handle the state is imported into | |
415 | * @in: buffer holding the state | |
416 | * | |
417 | * This function imports the hash state into the ahash_request handle from the | |
418 | * input buffer. That buffer should have been generated with the | |
419 | * crypto_ahash_export function. | |
420 | * | |
421 | * Return: 0 if the import was successful; < 0 if an error occurred | |
422 | */ | |
88056ec3 HX |
423 | static inline int crypto_ahash_import(struct ahash_request *req, const void *in) |
424 | { | |
425 | return crypto_ahash_reqtfm(req)->import(req, in); | |
426 | } | |
dec8b786 | 427 | |
90240ffb SM |
428 | /** |
429 | * crypto_ahash_init() - (re)initialize message digest handle | |
430 | * @req: ahash_request handle that already is initialized with all necessary | |
431 | * data using the ahash_request_* API functions | |
432 | * | |
433 | * The call (re-)initializes the message digest referenced by the ahash_request | |
434 | * handle. Any potentially existing state created by previous operations is | |
435 | * discarded. | |
436 | * | |
437 | * Return: 0 if the message digest initialization was successful; < 0 if an | |
438 | * error occurred | |
439 | */ | |
318e5313 HX |
440 | static inline int crypto_ahash_init(struct ahash_request *req) |
441 | { | |
88056ec3 | 442 | return crypto_ahash_reqtfm(req)->init(req); |
318e5313 HX |
443 | } |
444 | ||
90240ffb SM |
445 | /** |
446 | * crypto_ahash_update() - add data to message digest for processing | |
447 | * @req: ahash_request handle that was previously initialized with the | |
448 | * crypto_ahash_init call. | |
449 | * | |
450 | * Updates the message digest state of the &ahash_request handle. The input data | |
451 | * is pointed to by the scatter/gather list registered in the &ahash_request | |
452 | * handle | |
453 | * | |
454 | * Return: 0 if the message digest update was successful; < 0 if an error | |
455 | * occurred | |
456 | */ | |
318e5313 HX |
457 | static inline int crypto_ahash_update(struct ahash_request *req) |
458 | { | |
88056ec3 | 459 | return crypto_ahash_reqtfm(req)->update(req); |
318e5313 HX |
460 | } |
461 | ||
90240ffb SM |
462 | /** |
463 | * DOC: Asynchronous Hash Request Handle | |
464 | * | |
465 | * The &ahash_request data structure contains all pointers to data | |
466 | * required for the asynchronous cipher operation. This includes the cipher | |
467 | * handle (which can be used by multiple &ahash_request instances), pointer | |
468 | * to plaintext and the message digest output buffer, asynchronous callback | |
469 | * function, etc. It acts as a handle to the ahash_request_* API calls in a | |
470 | * similar way as ahash handle to the crypto_ahash_* API calls. | |
471 | */ | |
472 | ||
473 | /** | |
474 | * ahash_request_set_tfm() - update cipher handle reference in request | |
475 | * @req: request handle to be modified | |
476 | * @tfm: cipher handle that shall be added to the request handle | |
477 | * | |
478 | * Allow the caller to replace the existing ahash handle in the request | |
479 | * data structure with a different one. | |
480 | */ | |
18e33e6d HX |
481 | static inline void ahash_request_set_tfm(struct ahash_request *req, |
482 | struct crypto_ahash *tfm) | |
483 | { | |
484 | req->base.tfm = crypto_ahash_tfm(tfm); | |
485 | } | |
486 | ||
90240ffb SM |
487 | /** |
488 | * ahash_request_alloc() - allocate request data structure | |
489 | * @tfm: cipher handle to be registered with the request | |
490 | * @gfp: memory allocation flag that is handed to kmalloc by the API call. | |
491 | * | |
492 | * Allocate the request data structure that must be used with the ahash | |
493 | * message digest API calls. During | |
494 | * the allocation, the provided ahash handle | |
495 | * is registered in the request data structure. | |
496 | * | |
497 | * Return: allocated request handle in case of success; IS_ERR() is true in case | |
498 | * of an error, PTR_ERR() returns the error code. | |
499 | */ | |
18e33e6d HX |
500 | static inline struct ahash_request *ahash_request_alloc( |
501 | struct crypto_ahash *tfm, gfp_t gfp) | |
502 | { | |
503 | struct ahash_request *req; | |
504 | ||
505 | req = kmalloc(sizeof(struct ahash_request) + | |
506 | crypto_ahash_reqsize(tfm), gfp); | |
507 | ||
508 | if (likely(req)) | |
509 | ahash_request_set_tfm(req, tfm); | |
510 | ||
511 | return req; | |
512 | } | |
513 | ||
90240ffb SM |
514 | /** |
515 | * ahash_request_free() - zeroize and free the request data structure | |
516 | * @req: request data structure cipher handle to be freed | |
517 | */ | |
18e33e6d HX |
518 | static inline void ahash_request_free(struct ahash_request *req) |
519 | { | |
aef73cfc | 520 | kzfree(req); |
18e33e6d HX |
521 | } |
522 | ||
523 | static inline struct ahash_request *ahash_request_cast( | |
524 | struct crypto_async_request *req) | |
525 | { | |
526 | return container_of(req, struct ahash_request, base); | |
527 | } | |
528 | ||
90240ffb SM |
529 | /** |
530 | * ahash_request_set_callback() - set asynchronous callback function | |
531 | * @req: request handle | |
532 | * @flags: specify zero or an ORing of the flags | |
533 | * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and | |
534 | * increase the wait queue beyond the initial maximum size; | |
535 | * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep | |
536 | * @compl: callback function pointer to be registered with the request handle | |
537 | * @data: The data pointer refers to memory that is not used by the kernel | |
538 | * crypto API, but provided to the callback function for it to use. Here, | |
539 | * the caller can provide a reference to memory the callback function can | |
540 | * operate on. As the callback function is invoked asynchronously to the | |
541 | * related functionality, it may need to access data structures of the | |
542 | * related functionality which can be referenced using this pointer. The | |
543 | * callback function can access the memory via the "data" field in the | |
544 | * &crypto_async_request data structure provided to the callback function. | |
545 | * | |
546 | * This function allows setting the callback function that is triggered once | |
547 | * the cipher operation completes. | |
548 | * | |
549 | * The callback function is registered with the &ahash_request handle and | |
550 | * must comply with the following template | |
551 | * | |
552 | * void callback_function(struct crypto_async_request *req, int error) | |
553 | */ | |
18e33e6d HX |
554 | static inline void ahash_request_set_callback(struct ahash_request *req, |
555 | u32 flags, | |
3e3dc25f | 556 | crypto_completion_t compl, |
18e33e6d HX |
557 | void *data) |
558 | { | |
3e3dc25f | 559 | req->base.complete = compl; |
18e33e6d HX |
560 | req->base.data = data; |
561 | req->base.flags = flags; | |
562 | } | |
563 | ||
90240ffb SM |
564 | /** |
565 | * ahash_request_set_crypt() - set data buffers | |
566 | * @req: ahash_request handle to be updated | |
567 | * @src: source scatter/gather list | |
568 | * @result: buffer that is filled with the message digest -- the caller must | |
569 | * ensure that the buffer has sufficient space by, for example, calling | |
570 | * crypto_ahash_digestsize() | |
571 | * @nbytes: number of bytes to process from the source scatter/gather list | |
572 | * | |
573 | * By using this call, the caller references the source scatter/gather list. | |
574 | * The source scatter/gather list points to the data the message digest is to | |
575 | * be calculated for. | |
576 | */ | |
18e33e6d HX |
577 | static inline void ahash_request_set_crypt(struct ahash_request *req, |
578 | struct scatterlist *src, u8 *result, | |
579 | unsigned int nbytes) | |
580 | { | |
581 | req->src = src; | |
582 | req->nbytes = nbytes; | |
583 | req->result = result; | |
584 | } | |
585 | ||
7b5a080b HX |
586 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
587 | u32 mask); | |
588 | ||
589 | static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) | |
590 | { | |
591 | return &tfm->base; | |
592 | } | |
593 | ||
594 | static inline void crypto_free_shash(struct crypto_shash *tfm) | |
595 | { | |
412e87ae | 596 | crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm)); |
7b5a080b HX |
597 | } |
598 | ||
599 | static inline unsigned int crypto_shash_alignmask( | |
600 | struct crypto_shash *tfm) | |
601 | { | |
602 | return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm)); | |
603 | } | |
604 | ||
97495986 HX |
605 | static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm) |
606 | { | |
607 | return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm)); | |
608 | } | |
609 | ||
7b5a080b HX |
610 | static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) |
611 | { | |
612 | return container_of(alg, struct shash_alg, base); | |
613 | } | |
614 | ||
615 | static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) | |
616 | { | |
617 | return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); | |
618 | } | |
619 | ||
620 | static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) | |
621 | { | |
622 | return crypto_shash_alg(tfm)->digestsize; | |
623 | } | |
624 | ||
99d27e1c HX |
625 | static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm) |
626 | { | |
627 | return crypto_shash_alg(tfm)->statesize; | |
628 | } | |
629 | ||
7b5a080b HX |
630 | static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) |
631 | { | |
632 | return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); | |
633 | } | |
634 | ||
635 | static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) | |
636 | { | |
637 | crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); | |
638 | } | |
639 | ||
640 | static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) | |
641 | { | |
642 | crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); | |
643 | } | |
644 | ||
645 | static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) | |
646 | { | |
113adefc | 647 | return tfm->descsize; |
7b5a080b HX |
648 | } |
649 | ||
650 | static inline void *shash_desc_ctx(struct shash_desc *desc) | |
651 | { | |
652 | return desc->__ctx; | |
653 | } | |
654 | ||
655 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, | |
656 | unsigned int keylen); | |
657 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | |
658 | unsigned int len, u8 *out); | |
659 | ||
99d27e1c | 660 | static inline int crypto_shash_export(struct shash_desc *desc, void *out) |
dec8b786 | 661 | { |
99d27e1c | 662 | return crypto_shash_alg(desc->tfm)->export(desc, out); |
dec8b786 HX |
663 | } |
664 | ||
99d27e1c HX |
665 | static inline int crypto_shash_import(struct shash_desc *desc, const void *in) |
666 | { | |
667 | return crypto_shash_alg(desc->tfm)->import(desc, in); | |
668 | } | |
dec8b786 | 669 | |
7b5a080b HX |
670 | static inline int crypto_shash_init(struct shash_desc *desc) |
671 | { | |
672 | return crypto_shash_alg(desc->tfm)->init(desc); | |
673 | } | |
674 | ||
675 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, | |
676 | unsigned int len); | |
677 | int crypto_shash_final(struct shash_desc *desc, u8 *out); | |
678 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, | |
679 | unsigned int len, u8 *out); | |
680 | ||
18e33e6d | 681 | #endif /* _CRYPTO_HASH_H */ |