xref: /linux/include/crypto/hash.h (revision cbc9f5bcfa72d7e58cbace18c97e4df7e3a73058)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Hash: Hash algorithms under the crypto API
4  *
5  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
6  */
7 
8 #ifndef _CRYPTO_HASH_H
9 #define _CRYPTO_HASH_H
10 
11 #include <linux/atomic.h>
12 #include <linux/crypto.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 
16 /* Set this bit for virtual address instead of SG list. */
17 #define CRYPTO_AHASH_REQ_VIRT	0x00000001
18 
19 struct crypto_ahash;
20 
21 /**
22  * DOC: Message Digest Algorithm Definitions
23  *
24  * These data structures define modular message digest algorithm
25  * implementations, managed via crypto_register_ahash(),
26  * crypto_register_shash(), crypto_unregister_ahash() and
27  * crypto_unregister_shash().
28  */
29 
30 /*
31  * struct hash_alg_common - define properties of message digest
32  * @digestsize: Size of the result of the transformation. A buffer of this size
33  *	        must be available to the @final and @finup calls, so they can
34  *	        store the resulting hash into it. For various predefined sizes,
35  *	        search include/crypto/ using
36  *	        git grep _DIGEST_SIZE include/crypto.
37  * @statesize: Size of the block for partial state of the transformation. A
38  *	       buffer of this size must be passed to the @export function as it
39  *	       will save the partial state of the transformation into it. On the
40  *	       other side, the @import function will load the state from a
41  *	       buffer of this size as well.
42  * @base: Start of data structure of cipher algorithm. The common data
43  *	  structure of crypto_alg contains information common to all ciphers.
44  *	  The hash_alg_common data structure now adds the hash-specific
45  *	  information.
46  */
47 #define HASH_ALG_COMMON {		\
48 	unsigned int digestsize;	\
49 	unsigned int statesize;		\
50 					\
51 	struct crypto_alg base;		\
52 }
53 struct hash_alg_common HASH_ALG_COMMON;
54 
55 struct ahash_request {
56 	struct crypto_async_request base;
57 
58 	unsigned int nbytes;
59 	union {
60 		struct scatterlist *src;
61 		const u8 *svirt;
62 	};
63 	u8 *result;
64 
65 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
66 };
67 
68 /**
69  * struct ahash_alg - asynchronous message digest definition
70  * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the
71  *	  state of the HASH transformation at the beginning. This shall fill in
72  *	  the internal structures used during the entire duration of the whole
73  *	  transformation. No data processing happens at this point. Driver code
74  *	  implementation must not use req->result.
75  * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This
76  *	   function actually pushes blocks of data from upper layers into the
77  *	   driver, which then passes those to the hardware as seen fit. This
78  *	   function must not finalize the HASH transformation by calculating the
79  *	   final message digest as this only adds more data into the
80  *	   transformation. This function shall not modify the transformation
81  *	   context, as this function may be called in parallel with the same
82  *	   transformation object. Data processing can happen synchronously
83  *	   [SHASH] or asynchronously [AHASH] at this point. Driver must not use
84  *	   req->result.
85  * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the
86  *	   transformation and retrieves the resulting hash from the driver and
87  *	   pushes it back to upper layers. No data processing happens at this
88  *	   point unless hardware requires it to finish the transformation
89  *	   (then the data buffered by the device driver is processed).
90  * @finup: **[optional]** Combination of @update and @final. This function is effectively a
91  *	   combination of @update and @final calls issued in sequence. As some
92  *	   hardware cannot do @update and @final separately, this callback was
93  *	   added to allow such hardware to be used at least by IPsec. Data
94  *	   processing can happen synchronously [SHASH] or asynchronously [AHASH]
95  *	   at this point.
96  * @digest: Combination of @init and @update and @final. This function
97  *	    effectively behaves as the entire chain of operations, @init,
98  *	    @update and @final issued in sequence. Just like @finup, this was
99  *	    added for hardware which cannot do even the @finup, but can only do
100  *	    the whole transformation in one run. Data processing can happen
101  *	    synchronously [SHASH] or asynchronously [AHASH] at this point.
102  * @setkey: Set optional key used by the hashing algorithm. Intended to push
103  *	    optional key used by the hashing algorithm from upper layers into
104  *	    the driver. This function can store the key in the transformation
105  *	    context or can outright program it into the hardware. In the former
106  *	    case, one must be careful to program the key into the hardware at
107  *	    appropriate time and one must be careful that .setkey() can be
108  *	    called multiple times during the existence of the transformation
109  *	    object. Not  all hashing algorithms do implement this function as it
110  *	    is only needed for keyed message digests. SHAx/MDx/CRCx do NOT
111  *	    implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement
112  *	    this function. This function must be called before any other of the
113  *	    @init, @update, @final, @finup, @digest is called. No data
114  *	    processing happens at this point.
115  * @export: Export partial state of the transformation. This function dumps the
116  *	    entire state of the ongoing transformation into a provided block of
117  *	    data so it can be @import 'ed back later on. This is useful in case
118  *	    you want to save partial result of the transformation after
119  *	    processing certain amount of data and reload this partial result
120  *	    multiple times later on for multiple re-use. No data processing
121  *	    happens at this point. Driver must not use req->result.
122  * @import: Import partial state of the transformation. This function loads the
123  *	    entire state of the ongoing transformation from a provided block of
124  *	    data so the transformation can continue from this point onward. No
125  *	    data processing happens at this point. Driver must not use
126  *	    req->result.
127  * @init_tfm: Initialize the cryptographic transformation object.
128  *	      This function is called only once at the instantiation
129  *	      time, right after the transformation context was
130  *	      allocated. In case the cryptographic hardware has
131  *	      some special requirements which need to be handled
132  *	      by software, this function shall check for the precise
133  *	      requirement of the transformation and put any software
134  *	      fallbacks in place.
135  * @exit_tfm: Deinitialize the cryptographic transformation object.
136  *	      This is a counterpart to @init_tfm, used to remove
137  *	      various changes set in @init_tfm.
138  * @clone_tfm: Copy transform into new object, may allocate memory.
139  * @halg: see struct hash_alg_common
140  */
141 struct ahash_alg {
142 	int (*init)(struct ahash_request *req);
143 	int (*update)(struct ahash_request *req);
144 	int (*final)(struct ahash_request *req);
145 	int (*finup)(struct ahash_request *req);
146 	int (*digest)(struct ahash_request *req);
147 	int (*export)(struct ahash_request *req, void *out);
148 	int (*import)(struct ahash_request *req, const void *in);
149 	int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
150 		      unsigned int keylen);
151 	int (*init_tfm)(struct crypto_ahash *tfm);
152 	void (*exit_tfm)(struct crypto_ahash *tfm);
153 	int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src);
154 
155 	struct hash_alg_common halg;
156 };
157 
158 struct shash_desc {
159 	struct crypto_shash *tfm;
160 	void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
161 };
162 
163 #define HASH_MAX_DIGESTSIZE	 64
164 
165 /*
166  * Worst case is hmac(sha3-224-generic).  Its context is a nested 'shash_desc'
167  * containing a 'struct sha3_state'.
168  */
169 #define HASH_MAX_DESCSIZE	(sizeof(struct shash_desc) + 360)
170 
171 #define SHASH_DESC_ON_STACK(shash, ctx)					     \
172 	char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
173 		__aligned(__alignof__(struct shash_desc));		     \
174 	struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
175 
176 /**
177  * struct shash_alg - synchronous message digest definition
178  * @init: see struct ahash_alg
179  * @update: see struct ahash_alg
180  * @final: see struct ahash_alg
181  * @finup: see struct ahash_alg
182  * @digest: see struct ahash_alg
183  * @export: see struct ahash_alg
184  * @import: see struct ahash_alg
185  * @setkey: see struct ahash_alg
186  * @init_tfm: Initialize the cryptographic transformation object.
187  *	      This function is called only once at the instantiation
188  *	      time, right after the transformation context was
189  *	      allocated. In case the cryptographic hardware has
190  *	      some special requirements which need to be handled
191  *	      by software, this function shall check for the precise
192  *	      requirement of the transformation and put any software
193  *	      fallbacks in place.
194  * @exit_tfm: Deinitialize the cryptographic transformation object.
195  *	      This is a counterpart to @init_tfm, used to remove
196  *	      various changes set in @init_tfm.
197  * @clone_tfm: Copy transform into new object, may allocate memory.
198  * @descsize: Size of the operational state for the message digest. This state
199  * 	      size is the memory size that needs to be allocated for
200  *	      shash_desc.__ctx
201  * @halg: see struct hash_alg_common
202  * @HASH_ALG_COMMON: see struct hash_alg_common
203  */
204 struct shash_alg {
205 	int (*init)(struct shash_desc *desc);
206 	int (*update)(struct shash_desc *desc, const u8 *data,
207 		      unsigned int len);
208 	int (*final)(struct shash_desc *desc, u8 *out);
209 	int (*finup)(struct shash_desc *desc, const u8 *data,
210 		     unsigned int len, u8 *out);
211 	int (*digest)(struct shash_desc *desc, const u8 *data,
212 		      unsigned int len, u8 *out);
213 	int (*export)(struct shash_desc *desc, void *out);
214 	int (*import)(struct shash_desc *desc, const void *in);
215 	int (*setkey)(struct crypto_shash *tfm, const u8 *key,
216 		      unsigned int keylen);
217 	int (*init_tfm)(struct crypto_shash *tfm);
218 	void (*exit_tfm)(struct crypto_shash *tfm);
219 	int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);
220 
221 	unsigned int descsize;
222 
223 	union {
224 		struct HASH_ALG_COMMON;
225 		struct hash_alg_common halg;
226 	};
227 };
228 #undef HASH_ALG_COMMON
229 
230 struct crypto_ahash {
231 	bool using_shash; /* Underlying algorithm is shash, not ahash */
232 	unsigned int statesize;
233 	unsigned int reqsize;
234 	struct crypto_tfm base;
235 };
236 
237 struct crypto_shash {
238 	unsigned int descsize;
239 	struct crypto_tfm base;
240 };
241 
242 /**
243  * DOC: Asynchronous Message Digest API
244  *
245  * The asynchronous message digest API is used with the ciphers of type
246  * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
247  *
248  * The asynchronous cipher operation discussion provided for the
249  * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
250  */
251 
252 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
253 {
254 	return container_of(tfm, struct crypto_ahash, base);
255 }
256 
257 /**
258  * crypto_alloc_ahash() - allocate ahash cipher handle
259  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
260  *	      ahash cipher
261  * @type: specifies the type of the cipher
262  * @mask: specifies the mask for the cipher
263  *
264  * Allocate a cipher handle for an ahash. The returned struct
265  * crypto_ahash is the cipher handle that is required for any subsequent
266  * API invocation for that ahash.
267  *
268  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
269  *	   of an error, PTR_ERR() returns the error code.
270  */
271 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
272 					u32 mask);
273 
274 struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);
275 
276 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
277 {
278 	return &tfm->base;
279 }
280 
281 /**
282  * crypto_free_ahash() - zeroize and free the ahash handle
283  * @tfm: cipher handle to be freed
284  *
285  * If @tfm is a NULL or error pointer, this function does nothing.
286  */
287 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
288 {
289 	crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
290 }
291 
292 /**
293  * crypto_has_ahash() - Search for the availability of an ahash.
294  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
295  *	      ahash
296  * @type: specifies the type of the ahash
297  * @mask: specifies the mask for the ahash
298  *
299  * Return: true when the ahash is known to the kernel crypto API; false
300  *	   otherwise
301  */
302 int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
303 
304 static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
305 {
306 	return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
307 }
308 
309 static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
310 {
311 	return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
312 }
313 
314 /**
315  * crypto_ahash_blocksize() - obtain block size for cipher
316  * @tfm: cipher handle
317  *
318  * The block size for the message digest cipher referenced with the cipher
319  * handle is returned.
320  *
321  * Return: block size of cipher
322  */
323 static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
324 {
325 	return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
326 }
327 
328 static inline struct hash_alg_common *__crypto_hash_alg_common(
329 	struct crypto_alg *alg)
330 {
331 	return container_of(alg, struct hash_alg_common, base);
332 }
333 
334 static inline struct hash_alg_common *crypto_hash_alg_common(
335 	struct crypto_ahash *tfm)
336 {
337 	return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
338 }
339 
340 /**
341  * crypto_ahash_digestsize() - obtain message digest size
342  * @tfm: cipher handle
343  *
344  * The size for the message digest created by the message digest cipher
345  * referenced with the cipher handle is returned.
346  *
347  *
348  * Return: message digest size of cipher
349  */
350 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
351 {
352 	return crypto_hash_alg_common(tfm)->digestsize;
353 }
354 
355 /**
356  * crypto_ahash_statesize() - obtain size of the ahash state
357  * @tfm: cipher handle
358  *
359  * Return the size of the ahash state. With the crypto_ahash_export()
360  * function, the caller can export the state into a buffer whose size is
361  * defined with this function.
362  *
363  * Return: size of the ahash state
364  */
365 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
366 {
367 	return tfm->statesize;
368 }
369 
370 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
371 {
372 	return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
373 }
374 
375 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
376 {
377 	crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
378 }
379 
380 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
381 {
382 	crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
383 }
384 
385 /**
386  * crypto_ahash_reqtfm() - obtain cipher handle from request
387  * @req: asynchronous request handle that contains the reference to the ahash
388  *	 cipher handle
389  *
390  * Return the ahash cipher handle that is registered with the asynchronous
391  * request handle ahash_request.
392  *
393  * Return: ahash cipher handle
394  */
395 static inline struct crypto_ahash *crypto_ahash_reqtfm(
396 	struct ahash_request *req)
397 {
398 	return __crypto_ahash_cast(req->base.tfm);
399 }
400 
401 /**
402  * crypto_ahash_reqsize() - obtain size of the request data structure
403  * @tfm: cipher handle
404  *
405  * Return: size of the request data
406  */
407 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
408 {
409 	return tfm->reqsize;
410 }
411 
412 static inline void *ahash_request_ctx(struct ahash_request *req)
413 {
414 	return req->__ctx;
415 }
416 
417 /**
418  * crypto_ahash_setkey - set key for cipher handle
419  * @tfm: cipher handle
420  * @key: buffer holding the key
421  * @keylen: length of the key in bytes
422  *
423  * The caller provided key is set for the ahash cipher. The cipher
424  * handle must point to a keyed hash in order for this function to succeed.
425  *
426  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
427  */
428 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
429 			unsigned int keylen);
430 
431 /**
432  * crypto_ahash_finup() - update and finalize message digest
433  * @req: reference to the ahash_request handle that holds all information
434  *	 needed to perform the cipher operation
435  *
436  * This function is a "short-hand" for the function calls of
437  * crypto_ahash_update and crypto_ahash_final. The parameters have the same
438  * meaning as discussed for those separate functions.
439  *
440  * Return: see crypto_ahash_final()
441  */
442 int crypto_ahash_finup(struct ahash_request *req);
443 
444 /**
445  * crypto_ahash_final() - calculate message digest
446  * @req: reference to the ahash_request handle that holds all information
447  *	 needed to perform the cipher operation
448  *
449  * Finalize the message digest operation and create the message digest
450  * based on all data added to the cipher handle. The message digest is placed
451  * into the output buffer registered with the ahash_request handle.
452  *
453  * Return:
454  * 0		if the message digest was successfully calculated;
455  * -EINPROGRESS	if data is fed into hardware (DMA) or queued for later;
456  * -EBUSY	if queue is full and request should be resubmitted later;
457  * other < 0	if an error occurred
458  */
459 int crypto_ahash_final(struct ahash_request *req);
460 
461 /**
462  * crypto_ahash_digest() - calculate message digest for a buffer
463  * @req: reference to the ahash_request handle that holds all information
464  *	 needed to perform the cipher operation
465  *
466  * This function is a "short-hand" for the function calls of crypto_ahash_init,
467  * crypto_ahash_update and crypto_ahash_final. The parameters have the same
468  * meaning as discussed for those separate three functions.
469  *
470  * Return: see crypto_ahash_final()
471  */
472 int crypto_ahash_digest(struct ahash_request *req);
473 
474 /**
475  * crypto_ahash_export() - extract current message digest state
476  * @req: reference to the ahash_request handle whose state is exported
477  * @out: output buffer of sufficient size that can hold the hash state
478  *
479  * This function exports the hash state of the ahash_request handle into the
480  * caller-allocated output buffer out which must have sufficient size (e.g. by
481  * calling crypto_ahash_statesize()).
482  *
483  * Return: 0 if the export was successful; < 0 if an error occurred
484  */
485 int crypto_ahash_export(struct ahash_request *req, void *out);
486 
487 /**
488  * crypto_ahash_import() - import message digest state
489  * @req: reference to ahash_request handle the state is imported into
490  * @in: buffer holding the state
491  *
492  * This function imports the hash state into the ahash_request handle from the
493  * input buffer. That buffer should have been generated with the
494  * crypto_ahash_export function.
495  *
496  * Return: 0 if the import was successful; < 0 if an error occurred
497  */
498 int crypto_ahash_import(struct ahash_request *req, const void *in);
499 
500 /**
501  * crypto_ahash_init() - (re)initialize message digest handle
502  * @req: ahash_request handle that already is initialized with all necessary
503  *	 data using the ahash_request_* API functions
504  *
505  * The call (re-)initializes the message digest referenced by the ahash_request
506  * handle. Any potentially existing state created by previous operations is
507  * discarded.
508  *
509  * Return: see crypto_ahash_final()
510  */
511 int crypto_ahash_init(struct ahash_request *req);
512 
513 /**
514  * crypto_ahash_update() - add data to message digest for processing
515  * @req: ahash_request handle that was previously initialized with the
516  *	 crypto_ahash_init call.
517  *
518  * Updates the message digest state of the &ahash_request handle. The input data
519  * is pointed to by the scatter/gather list registered in the &ahash_request
520  * handle
521  *
522  * Return: see crypto_ahash_final()
523  */
524 int crypto_ahash_update(struct ahash_request *req);
525 
526 /**
527  * DOC: Asynchronous Hash Request Handle
528  *
529  * The &ahash_request data structure contains all pointers to data
530  * required for the asynchronous cipher operation. This includes the cipher
531  * handle (which can be used by multiple &ahash_request instances), pointer
532  * to plaintext and the message digest output buffer, asynchronous callback
533  * function, etc. It acts as a handle to the ahash_request_* API calls in a
534  * similar way as ahash handle to the crypto_ahash_* API calls.
535  */
536 
537 /**
538  * ahash_request_set_tfm() - update cipher handle reference in request
539  * @req: request handle to be modified
540  * @tfm: cipher handle that shall be added to the request handle
541  *
542  * Allow the caller to replace the existing ahash handle in the request
543  * data structure with a different one.
544  */
545 static inline void ahash_request_set_tfm(struct ahash_request *req,
546 					 struct crypto_ahash *tfm)
547 {
548 	req->base.tfm = crypto_ahash_tfm(tfm);
549 }
550 
551 /**
552  * ahash_request_alloc() - allocate request data structure
553  * @tfm: cipher handle to be registered with the request
554  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
555  *
556  * Allocate the request data structure that must be used with the ahash
557  * message digest API calls. During
558  * the allocation, the provided ahash handle
559  * is registered in the request data structure.
560  *
561  * Return: allocated request handle in case of success, or NULL if out of memory
562  */
563 static inline struct ahash_request *ahash_request_alloc_noprof(
564 	struct crypto_ahash *tfm, gfp_t gfp)
565 {
566 	struct ahash_request *req;
567 
568 	req = kmalloc_noprof(sizeof(struct ahash_request) +
569 			     crypto_ahash_reqsize(tfm), gfp);
570 
571 	if (likely(req))
572 		ahash_request_set_tfm(req, tfm);
573 
574 	return req;
575 }
576 #define ahash_request_alloc(...)	alloc_hooks(ahash_request_alloc_noprof(__VA_ARGS__))
577 
578 /**
579  * ahash_request_free() - zeroize and free the request data structure
580  * @req: request data structure cipher handle to be freed
581  */
582 static inline void ahash_request_free(struct ahash_request *req)
583 {
584 	kfree_sensitive(req);
585 }
586 
587 static inline struct ahash_request *ahash_request_cast(
588 	struct crypto_async_request *req)
589 {
590 	return container_of(req, struct ahash_request, base);
591 }
592 
593 /**
594  * ahash_request_set_callback() - set asynchronous callback function
595  * @req: request handle
596  * @flags: specify zero or an ORing of the flags
597  *	   CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
598  *	   increase the wait queue beyond the initial maximum size;
599  *	   CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
600  * @compl: callback function pointer to be registered with the request handle
601  * @data: The data pointer refers to memory that is not used by the kernel
602  *	  crypto API, but provided to the callback function for it to use. Here,
603  *	  the caller can provide a reference to memory the callback function can
604  *	  operate on. As the callback function is invoked asynchronously to the
605  *	  related functionality, it may need to access data structures of the
606  *	  related functionality which can be referenced using this pointer. The
607  *	  callback function can access the memory via the "data" field in the
608  *	  &crypto_async_request data structure provided to the callback function.
609  *
610  * This function allows setting the callback function that is triggered once
611  * the cipher operation completes.
612  *
613  * The callback function is registered with the &ahash_request handle and
614  * must comply with the following template::
615  *
616  *	void callback_function(struct crypto_async_request *req, int error)
617  */
618 static inline void ahash_request_set_callback(struct ahash_request *req,
619 					      u32 flags,
620 					      crypto_completion_t compl,
621 					      void *data)
622 {
623 	u32 keep = CRYPTO_AHASH_REQ_VIRT;
624 
625 	req->base.complete = compl;
626 	req->base.data = data;
627 	flags &= ~keep;
628 	req->base.flags &= keep;
629 	req->base.flags |= flags;
630 }
631 
632 /**
633  * ahash_request_set_crypt() - set data buffers
634  * @req: ahash_request handle to be updated
635  * @src: source scatter/gather list
636  * @result: buffer that is filled with the message digest -- the caller must
637  *	    ensure that the buffer has sufficient space by, for example, calling
638  *	    crypto_ahash_digestsize()
639  * @nbytes: number of bytes to process from the source scatter/gather list
640  *
641  * By using this call, the caller references the source scatter/gather list.
642  * The source scatter/gather list points to the data the message digest is to
643  * be calculated for.
644  */
645 static inline void ahash_request_set_crypt(struct ahash_request *req,
646 					   struct scatterlist *src, u8 *result,
647 					   unsigned int nbytes)
648 {
649 	req->src = src;
650 	req->nbytes = nbytes;
651 	req->result = result;
652 	req->base.flags &= ~CRYPTO_AHASH_REQ_VIRT;
653 }
654 
655 /**
656  * ahash_request_set_virt() - set virtual address data buffers
657  * @req: ahash_request handle to be updated
658  * @src: source virtual address
659  * @result: buffer that is filled with the message digest -- the caller must
660  *	    ensure that the buffer has sufficient space by, for example, calling
661  *	    crypto_ahash_digestsize()
662  * @nbytes: number of bytes to process from the source virtual address
663  *
664  * By using this call, the caller references the source virtual address.
665  * The source virtual address points to the data the message digest is to
666  * be calculated for.
667  */
668 static inline void ahash_request_set_virt(struct ahash_request *req,
669 					  const u8 *src, u8 *result,
670 					  unsigned int nbytes)
671 {
672 	req->svirt = src;
673 	req->nbytes = nbytes;
674 	req->result = result;
675 	req->base.flags |= CRYPTO_AHASH_REQ_VIRT;
676 }
677 
678 /**
679  * DOC: Synchronous Message Digest API
680  *
681  * The synchronous message digest API is used with the ciphers of type
682  * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
683  *
684  * The message digest API is able to maintain state information for the
685  * caller.
686  *
687  * The synchronous message digest API can store user-related context in its
688  * shash_desc request data structure.
689  */
690 
691 /**
692  * crypto_alloc_shash() - allocate message digest handle
693  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
694  *	      message digest cipher
695  * @type: specifies the type of the cipher
696  * @mask: specifies the mask for the cipher
697  *
698  * Allocate a cipher handle for a message digest. The returned &struct
699  * crypto_shash is the cipher handle that is required for any subsequent
700  * API invocation for that message digest.
701  *
702  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
703  *	   of an error, PTR_ERR() returns the error code.
704  */
705 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
706 					u32 mask);
707 
708 struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);
709 
710 int crypto_has_shash(const char *alg_name, u32 type, u32 mask);
711 
712 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
713 {
714 	return &tfm->base;
715 }
716 
717 /**
718  * crypto_free_shash() - zeroize and free the message digest handle
719  * @tfm: cipher handle to be freed
720  *
721  * If @tfm is a NULL or error pointer, this function does nothing.
722  */
723 static inline void crypto_free_shash(struct crypto_shash *tfm)
724 {
725 	crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
726 }
727 
728 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
729 {
730 	return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
731 }
732 
733 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
734 {
735 	return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
736 }
737 
738 /**
739  * crypto_shash_blocksize() - obtain block size for cipher
740  * @tfm: cipher handle
741  *
742  * The block size for the message digest cipher referenced with the cipher
743  * handle is returned.
744  *
745  * Return: block size of cipher
746  */
747 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
748 {
749 	return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
750 }
751 
752 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
753 {
754 	return container_of(alg, struct shash_alg, base);
755 }
756 
757 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
758 {
759 	return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
760 }
761 
762 /**
763  * crypto_shash_digestsize() - obtain message digest size
764  * @tfm: cipher handle
765  *
766  * The size for the message digest created by the message digest cipher
767  * referenced with the cipher handle is returned.
768  *
769  * Return: digest size of cipher
770  */
771 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
772 {
773 	return crypto_shash_alg(tfm)->digestsize;
774 }
775 
776 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
777 {
778 	return crypto_shash_alg(tfm)->statesize;
779 }
780 
781 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
782 {
783 	return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
784 }
785 
786 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
787 {
788 	crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
789 }
790 
791 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
792 {
793 	crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
794 }
795 
796 /**
797  * crypto_shash_descsize() - obtain the operational state size
798  * @tfm: cipher handle
799  *
800  * The size of the operational state the cipher needs during operation is
801  * returned for the hash referenced with the cipher handle. This size is
802  * required to calculate the memory requirements to allow the caller allocating
803  * sufficient memory for operational state.
804  *
805  * The operational state is defined with struct shash_desc where the size of
806  * that data structure is to be calculated as
807  * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
808  *
809  * Return: size of the operational state
810  */
811 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
812 {
813 	return tfm->descsize;
814 }
815 
816 static inline void *shash_desc_ctx(struct shash_desc *desc)
817 {
818 	return desc->__ctx;
819 }
820 
821 /**
822  * crypto_shash_setkey() - set key for message digest
823  * @tfm: cipher handle
824  * @key: buffer holding the key
825  * @keylen: length of the key in bytes
826  *
827  * The caller provided key is set for the keyed message digest cipher. The
828  * cipher handle must point to a keyed message digest cipher in order for this
829  * function to succeed.
830  *
831  * Context: Softirq or process context.
832  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
833  */
834 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
835 			unsigned int keylen);
836 
837 /**
838  * crypto_shash_digest() - calculate message digest for buffer
839  * @desc: see crypto_shash_final()
840  * @data: see crypto_shash_update()
841  * @len: see crypto_shash_update()
842  * @out: see crypto_shash_final()
843  *
844  * This function is a "short-hand" for the function calls of crypto_shash_init,
845  * crypto_shash_update and crypto_shash_final. The parameters have the same
846  * meaning as discussed for those separate three functions.
847  *
848  * Context: Softirq or process context.
849  * Return: 0 if the message digest creation was successful; < 0 if an error
850  *	   occurred
851  */
852 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
853 			unsigned int len, u8 *out);
854 
855 /**
856  * crypto_shash_tfm_digest() - calculate message digest for buffer
857  * @tfm: hash transformation object
858  * @data: see crypto_shash_update()
859  * @len: see crypto_shash_update()
860  * @out: see crypto_shash_final()
861  *
862  * This is a simplified version of crypto_shash_digest() for users who don't
863  * want to allocate their own hash descriptor (shash_desc).  Instead,
864  * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)
865  * directly, and it allocates a hash descriptor on the stack internally.
866  * Note that this stack allocation may be fairly large.
867  *
868  * Context: Softirq or process context.
869  * Return: 0 on success; < 0 if an error occurred.
870  */
871 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
872 			    unsigned int len, u8 *out);
873 
874 /**
875  * crypto_shash_export() - extract operational state for message digest
876  * @desc: reference to the operational state handle whose state is exported
877  * @out: output buffer of sufficient size that can hold the hash state
878  *
879  * This function exports the hash state of the operational state handle into the
880  * caller-allocated output buffer out which must have sufficient size (e.g. by
881  * calling crypto_shash_descsize).
882  *
883  * Context: Softirq or process context.
884  * Return: 0 if the export creation was successful; < 0 if an error occurred
885  */
886 int crypto_shash_export(struct shash_desc *desc, void *out);
887 
888 /**
889  * crypto_shash_import() - import operational state
890  * @desc: reference to the operational state handle the state imported into
891  * @in: buffer holding the state
892  *
893  * This function imports the hash state into the operational state handle from
894  * the input buffer. That buffer should have been generated with the
895  * crypto_ahash_export function.
896  *
897  * Context: Softirq or process context.
898  * Return: 0 if the import was successful; < 0 if an error occurred
899  */
900 int crypto_shash_import(struct shash_desc *desc, const void *in);
901 
902 /**
903  * crypto_shash_init() - (re)initialize message digest
904  * @desc: operational state handle that is already filled
905  *
906  * The call (re-)initializes the message digest referenced by the
907  * operational state handle. Any potentially existing state created by
908  * previous operations is discarded.
909  *
910  * Context: Softirq or process context.
911  * Return: 0 if the message digest initialization was successful; < 0 if an
912  *	   error occurred
913  */
914 static inline int crypto_shash_init(struct shash_desc *desc)
915 {
916 	struct crypto_shash *tfm = desc->tfm;
917 
918 	if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
919 		return -ENOKEY;
920 
921 	return crypto_shash_alg(tfm)->init(desc);
922 }
923 
924 /**
925  * crypto_shash_update() - add data to message digest for processing
926  * @desc: operational state handle that is already initialized
927  * @data: input data to be added to the message digest
928  * @len: length of the input data
929  *
930  * Updates the message digest state of the operational state handle.
931  *
932  * Context: Softirq or process context.
933  * Return: 0 if the message digest update was successful; < 0 if an error
934  *	   occurred
935  */
936 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
937 			unsigned int len);
938 
939 /**
940  * crypto_shash_final() - calculate message digest
941  * @desc: operational state handle that is already filled with data
942  * @out: output buffer filled with the message digest
943  *
944  * Finalize the message digest operation and create the message digest
945  * based on all data added to the cipher handle. The message digest is placed
946  * into the output buffer. The caller must ensure that the output buffer is
947  * large enough by using crypto_shash_digestsize.
948  *
949  * Context: Softirq or process context.
950  * Return: 0 if the message digest creation was successful; < 0 if an error
951  *	   occurred
952  */
953 int crypto_shash_final(struct shash_desc *desc, u8 *out);
954 
955 /**
956  * crypto_shash_finup() - calculate message digest of buffer
957  * @desc: see crypto_shash_final()
958  * @data: see crypto_shash_update()
959  * @len: see crypto_shash_update()
960  * @out: see crypto_shash_final()
961  *
962  * This function is a "short-hand" for the function calls of
963  * crypto_shash_update and crypto_shash_final. The parameters have the same
964  * meaning as discussed for those separate functions.
965  *
966  * Context: Softirq or process context.
967  * Return: 0 if the message digest creation was successful; < 0 if an error
968  *	   occurred
969  */
970 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
971 		       unsigned int len, u8 *out);
972 
973 static inline void shash_desc_zero(struct shash_desc *desc)
974 {
975 	memzero_explicit(desc,
976 			 sizeof(*desc) + crypto_shash_descsize(desc->tfm));
977 }
978 
979 static inline bool ahash_is_async(struct crypto_ahash *tfm)
980 {
981 	return crypto_tfm_is_async(&tfm->base);
982 }
983 
984 #endif	/* _CRYPTO_HASH_H */
985