blob: b2c193acc1ab23c99c0d6e6f2bde5cf994dcb581 [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 Xu88056ec2009-07-14 12:28:26 +080018struct crypto_ahash;
19
Stephan Mueller5d8c7232014-11-12 05:26:03 +010020/**
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 */
Herbert Xu88056ec2009-07-14 12:28:26 +080042struct hash_alg_common {
43 unsigned int digestsize;
44 unsigned int statesize;
45
46 struct crypto_alg base;
47};
48
49struct ahash_request {
50 struct crypto_async_request base;
51
52 unsigned int nbytes;
53 struct scatterlist *src;
54 u8 *result;
55
Herbert Xu66f6ce52009-07-15 12:40:40 +080056 /* This field may only be used by the ahash API code. */
57 void *priv;
58
Herbert Xu88056ec2009-07-14 12:28:26 +080059 void *__ctx[] CRYPTO_MINALIGN_ATTR;
60};
61
Stephan Mueller5d8c7232014-11-12 05:26:03 +010062/**
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 */
Herbert Xu88056ec2009-07-14 12:28:26 +0800118struct 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
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000132struct shash_desc {
133 struct crypto_shash *tfm;
134 u32 flags;
135
136 void *__ctx[] CRYPTO_MINALIGN_ATTR;
137};
138
Behan Webstera0a77af2014-09-08 00:05:09 -0500139#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
Stephan Mueller5d8c7232014-11-12 05:26:03 +0100144/**
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 */
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000161struct 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);
Herbert Xu99d27e12009-07-09 20:30:57 +0800170 int (*export)(struct shash_desc *desc, void *out);
171 int (*import)(struct shash_desc *desc, const void *in);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000172 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
173 unsigned int keylen);
174
175 unsigned int descsize;
Herbert Xu88056ec2009-07-14 12:28:26 +0800176
177 /* These fields must match hash_alg_common. */
Herbert Xufa649662009-07-15 21:16:05 +0800178 unsigned int digestsize
179 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
Herbert Xu99d27e12009-07-09 20:30:57 +0800180 unsigned int statesize;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000181
182 struct crypto_alg base;
183};
184
Herbert Xu18e33e62008-07-10 16:01:22 +0800185struct crypto_ahash {
Herbert Xu88056ec2009-07-14 12:28:26 +0800186 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
Herbert Xu88056ec2009-07-14 12:28:26 +0800196 unsigned int reqsize;
Herbert Xu18e33e62008-07-10 16:01:22 +0800197 struct crypto_tfm base;
198};
199
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000200struct crypto_shash {
Herbert Xu113adef2009-07-14 12:50:12 +0800201 unsigned int descsize;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000202 struct crypto_tfm base;
203};
204
Stephan Mueller90240ff2014-11-12 05:26:41 +0100205/**
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
Herbert Xu18e33e62008-07-10 16:01:22 +0800215static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
216{
Herbert Xu88056ec2009-07-14 12:28:26 +0800217 return container_of(tfm, struct crypto_ahash, base);
Herbert Xu18e33e62008-07-10 16:01:22 +0800218}
219
Stephan Mueller90240ff2014-11-12 05:26:41 +0100220/**
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 */
Herbert Xu88056ec2009-07-14 12:28:26 +0800234struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
235 u32 mask);
Herbert Xu18e33e62008-07-10 16:01:22 +0800236
237static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
238{
239 return &tfm->base;
240}
241
Stephan Mueller90240ff2014-11-12 05:26:41 +0100242/**
243 * crypto_free_ahash() - zeroize and free the ahash handle
244 * @tfm: cipher handle to be freed
245 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800246static inline void crypto_free_ahash(struct crypto_ahash *tfm)
247{
Herbert Xu88056ec2009-07-14 12:28:26 +0800248 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
Herbert Xu18e33e62008-07-10 16:01:22 +0800249}
250
251static inline unsigned int crypto_ahash_alignmask(
252 struct crypto_ahash *tfm)
253{
254 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
255}
256
Herbert Xu88056ec2009-07-14 12:28:26 +0800257static inline struct hash_alg_common *__crypto_hash_alg_common(
258 struct crypto_alg *alg)
Herbert Xu18e33e62008-07-10 16:01:22 +0800259{
Herbert Xu88056ec2009-07-14 12:28:26 +0800260 return container_of(alg, struct hash_alg_common, base);
261}
262
263static inline struct hash_alg_common *crypto_hash_alg_common(
264 struct crypto_ahash *tfm)
265{
266 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
Herbert Xu18e33e62008-07-10 16:01:22 +0800267}
268
Stephan Mueller90240ff2014-11-12 05:26:41 +0100269/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800279static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
280{
Herbert Xu500b3e32009-07-14 20:29:57 +0800281 return crypto_hash_alg_common(tfm)->digestsize;
Herbert Xu88056ec2009-07-14 12:28:26 +0800282}
283
284static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
285{
286 return crypto_hash_alg_common(tfm)->statesize;
Herbert Xu18e33e62008-07-10 16:01:22 +0800287}
288
289static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
290{
291 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
292}
293
294static 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
299static 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
Stephan Mueller90240ff2014-11-12 05:26:41 +0100304/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800314static inline struct crypto_ahash *crypto_ahash_reqtfm(
315 struct ahash_request *req)
316{
317 return __crypto_ahash_cast(req->base.tfm);
318}
319
Stephan Mueller90240ff2014-11-12 05:26:41 +0100320/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800330static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
331{
Herbert Xu88056ec2009-07-14 12:28:26 +0800332 return tfm->reqsize;
Herbert Xu18e33e62008-07-10 16:01:22 +0800333}
334
Herbert Xudec8b782008-11-02 21:38:11 +0800335static inline void *ahash_request_ctx(struct ahash_request *req)
336{
337 return req->__ctx;
338}
339
Stephan Mueller90240ff2014-11-12 05:26:41 +0100340/**
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 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800351int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
352 unsigned int keylen);
Stephan Mueller90240ff2014-11-12 05:26:41 +0100353
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 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800366int crypto_ahash_finup(struct ahash_request *req);
Stephan Mueller90240ff2014-11-12 05:26:41 +0100367
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 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800380int crypto_ahash_final(struct ahash_request *req);
Stephan Mueller90240ff2014-11-12 05:26:41 +0100381
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 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800394int crypto_ahash_digest(struct ahash_request *req);
Herbert Xu18e33e62008-07-10 16:01:22 +0800395
Stephan Mueller90240ff2014-11-12 05:26:41 +0100396/**
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 */
Herbert Xu88056ec2009-07-14 12:28:26 +0800407static inline int crypto_ahash_export(struct ahash_request *req, void *out)
Herbert Xudec8b782008-11-02 21:38:11 +0800408{
Herbert Xu88056ec2009-07-14 12:28:26 +0800409 return crypto_ahash_reqtfm(req)->export(req, out);
Herbert Xudec8b782008-11-02 21:38:11 +0800410}
411
Stephan Mueller90240ff2014-11-12 05:26:41 +0100412/**
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 */
Herbert Xu88056ec2009-07-14 12:28:26 +0800423static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
424{
425 return crypto_ahash_reqtfm(req)->import(req, in);
426}
Herbert Xudec8b782008-11-02 21:38:11 +0800427
Stephan Mueller90240ff2014-11-12 05:26:41 +0100428/**
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 */
Herbert Xu318e5312008-08-05 13:34:30 +0800440static inline int crypto_ahash_init(struct ahash_request *req)
441{
Herbert Xu88056ec2009-07-14 12:28:26 +0800442 return crypto_ahash_reqtfm(req)->init(req);
Herbert Xu318e5312008-08-05 13:34:30 +0800443}
444
Stephan Mueller90240ff2014-11-12 05:26:41 +0100445/**
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 */
Herbert Xu318e5312008-08-05 13:34:30 +0800457static inline int crypto_ahash_update(struct ahash_request *req)
458{
Herbert Xu88056ec2009-07-14 12:28:26 +0800459 return crypto_ahash_reqtfm(req)->update(req);
Herbert Xu318e5312008-08-05 13:34:30 +0800460}
461
Stephan Mueller90240ff2014-11-12 05:26:41 +0100462/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800481static 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
Stephan Mueller90240ff2014-11-12 05:26:41 +0100487/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800500static 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
Stephan Mueller90240ff2014-11-12 05:26:41 +0100514/**
515 * ahash_request_free() - zeroize and free the request data structure
516 * @req: request data structure cipher handle to be freed
517 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800518static inline void ahash_request_free(struct ahash_request *req)
519{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800520 kzfree(req);
Herbert Xu18e33e62008-07-10 16:01:22 +0800521}
522
523static 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
Stephan Mueller90240ff2014-11-12 05:26:41 +0100529/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800554static inline void ahash_request_set_callback(struct ahash_request *req,
555 u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700556 crypto_completion_t compl,
Herbert Xu18e33e62008-07-10 16:01:22 +0800557 void *data)
558{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700559 req->base.complete = compl;
Herbert Xu18e33e62008-07-10 16:01:22 +0800560 req->base.data = data;
561 req->base.flags = flags;
562}
563
Stephan Mueller90240ff2014-11-12 05:26:41 +0100564/**
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 */
Herbert Xu18e33e62008-07-10 16:01:22 +0800577static 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
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000586struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
587 u32 mask);
588
589static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
590{
591 return &tfm->base;
592}
593
594static inline void crypto_free_shash(struct crypto_shash *tfm)
595{
Herbert Xu412e87a2009-02-05 16:51:25 +1100596 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000597}
598
599static inline unsigned int crypto_shash_alignmask(
600 struct crypto_shash *tfm)
601{
602 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
603}
604
Herbert Xu97495982009-02-03 12:47:44 +1100605static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
606{
607 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
608}
609
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000610static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
611{
612 return container_of(alg, struct shash_alg, base);
613}
614
615static 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
620static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
621{
622 return crypto_shash_alg(tfm)->digestsize;
623}
624
Herbert Xu99d27e12009-07-09 20:30:57 +0800625static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
626{
627 return crypto_shash_alg(tfm)->statesize;
628}
629
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000630static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
631{
632 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
633}
634
635static 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
640static 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
645static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
646{
Herbert Xu113adef2009-07-14 12:50:12 +0800647 return tfm->descsize;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000648}
649
650static inline void *shash_desc_ctx(struct shash_desc *desc)
651{
652 return desc->__ctx;
653}
654
655int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
656 unsigned int keylen);
657int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
658 unsigned int len, u8 *out);
659
Herbert Xu99d27e12009-07-09 20:30:57 +0800660static inline int crypto_shash_export(struct shash_desc *desc, void *out)
Herbert Xudec8b782008-11-02 21:38:11 +0800661{
Herbert Xu99d27e12009-07-09 20:30:57 +0800662 return crypto_shash_alg(desc->tfm)->export(desc, out);
Herbert Xudec8b782008-11-02 21:38:11 +0800663}
664
Herbert Xu99d27e12009-07-09 20:30:57 +0800665static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
666{
667 return crypto_shash_alg(desc->tfm)->import(desc, in);
668}
Herbert Xudec8b782008-11-02 21:38:11 +0800669
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000670static inline int crypto_shash_init(struct shash_desc *desc)
671{
672 return crypto_shash_alg(desc->tfm)->init(desc);
673}
674
675int crypto_shash_update(struct shash_desc *desc, const u8 *data,
676 unsigned int len);
677int crypto_shash_final(struct shash_desc *desc, u8 *out);
678int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
679 unsigned int len, u8 *out);
680
Herbert Xu18e33e62008-07-10 16:01:22 +0800681#endif /* _CRYPTO_HASH_H */