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