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