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