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