1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Hash: Hash algorithms under the crypto API
5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
11 #include <linux/crypto.h>
12 #include <linux/string.h>
17 * DOC: Message Digest Algorithm Definitions
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().
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
42 struct hash_alg_common {
43 unsigned int digestsize;
44 unsigned int statesize;
46 struct crypto_alg base;
49 struct ahash_request {
50 struct crypto_async_request base;
53 struct scatterlist *src;
56 /* This field may only be used by the ahash API code. */
59 void *__ctx[] CRYPTO_MINALIGN_ATTR;
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
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
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]
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
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
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);
152 struct hash_alg_common halg;
156 struct crypto_shash *tfm;
157 void *__ctx[] CRYPTO_MINALIGN_ATTR;
160 #define HASH_MAX_DIGESTSIZE 64
163 * Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc'
164 * containing a 'struct sha3_state'.
166 #define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360)
168 #define HASH_MAX_STATESIZE 512
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
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
201 * @base: internally used
204 int (*init)(struct shash_desc *desc);
205 int (*update)(struct shash_desc *desc, const u8 *data,
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);
219 unsigned int descsize;
221 /* These fields must match hash_alg_common. */
222 unsigned int digestsize
223 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
224 unsigned int statesize;
226 struct crypto_alg base;
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);
240 unsigned int reqsize;
241 struct crypto_tfm base;
244 struct crypto_shash {
245 unsigned int descsize;
246 struct crypto_tfm base;
250 * DOC: Asynchronous Message Digest API
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)
255 * The asynchronous cipher operation discussion provided for the
256 * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
259 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
261 return container_of(tfm, struct crypto_ahash, base);
265 * crypto_alloc_ahash() - allocate ahash cipher handle
266 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
268 * @type: specifies the type of the cipher
269 * @mask: specifies the mask for the cipher
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.
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.
278 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
281 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
287 * crypto_free_ahash() - zeroize and free the ahash handle
288 * @tfm: cipher handle to be freed
290 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
292 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
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
299 * @type: specifies the type of the ahash
300 * @mask: specifies the mask for the ahash
302 * Return: true when the ahash is known to the kernel crypto API; false
305 int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
307 static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
309 return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
312 static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
314 return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
317 static inline unsigned int crypto_ahash_alignmask(
318 struct crypto_ahash *tfm)
320 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
324 * crypto_ahash_blocksize() - obtain block size for cipher
325 * @tfm: cipher handle
327 * The block size for the message digest cipher referenced with the cipher
328 * handle is returned.
330 * Return: block size of cipher
332 static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
334 return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
337 static inline struct hash_alg_common *__crypto_hash_alg_common(
338 struct crypto_alg *alg)
340 return container_of(alg, struct hash_alg_common, base);
343 static inline struct hash_alg_common *crypto_hash_alg_common(
344 struct crypto_ahash *tfm)
346 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
350 * crypto_ahash_digestsize() - obtain message digest size
351 * @tfm: cipher handle
353 * The size for the message digest created by the message digest cipher
354 * referenced with the cipher handle is returned.
357 * Return: message digest size of cipher
359 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
361 return crypto_hash_alg_common(tfm)->digestsize;
365 * crypto_ahash_statesize() - obtain size of the ahash state
366 * @tfm: cipher handle
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.
372 * Return: size of the ahash state
374 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
376 return crypto_hash_alg_common(tfm)->statesize;
379 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
381 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
384 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
386 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
389 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
391 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
395 * crypto_ahash_reqtfm() - obtain cipher handle from request
396 * @req: asynchronous request handle that contains the reference to the ahash
399 * Return the ahash cipher handle that is registered with the asynchronous
400 * request handle ahash_request.
402 * Return: ahash cipher handle
404 static inline struct crypto_ahash *crypto_ahash_reqtfm(
405 struct ahash_request *req)
407 return __crypto_ahash_cast(req->base.tfm);
411 * crypto_ahash_reqsize() - obtain size of the request data structure
412 * @tfm: cipher handle
414 * Return: size of the request data
416 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
421 static inline void *ahash_request_ctx(struct ahash_request *req)
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
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.
435 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
437 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
438 unsigned int keylen);
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
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.
449 * Return: see crypto_ahash_final()
451 int crypto_ahash_finup(struct ahash_request *req);
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
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.
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
468 int crypto_ahash_final(struct ahash_request *req);
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
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.
479 * Return: see crypto_ahash_final()
481 int crypto_ahash_digest(struct ahash_request *req);
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
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()).
492 * Return: 0 if the export was successful; < 0 if an error occurred
494 static inline int crypto_ahash_export(struct ahash_request *req, void *out)
496 return crypto_ahash_reqtfm(req)->export(req, out);
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
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.
508 * Return: 0 if the import was successful; < 0 if an error occurred
510 static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
512 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
514 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
517 return tfm->import(req, in);
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
525 * The call (re-)initializes the message digest referenced by the ahash_request
526 * handle. Any potentially existing state created by previous operations is
529 * Return: see crypto_ahash_final()
531 static inline int crypto_ahash_init(struct ahash_request *req)
533 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
535 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
538 return tfm->init(req);
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.
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
550 * Return: see crypto_ahash_final()
552 static inline int crypto_ahash_update(struct ahash_request *req)
554 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
555 struct crypto_alg *alg = tfm->base.__crt_alg;
556 unsigned int nbytes = req->nbytes;
559 crypto_stats_get(alg);
560 ret = crypto_ahash_reqtfm(req)->update(req);
561 crypto_stats_ahash_update(nbytes, ret, alg);
566 * DOC: Asynchronous Hash Request Handle
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.
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
581 * Allow the caller to replace the existing ahash handle in the request
582 * data structure with a different one.
584 static inline void ahash_request_set_tfm(struct ahash_request *req,
585 struct crypto_ahash *tfm)
587 req->base.tfm = crypto_ahash_tfm(tfm);
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.
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.
600 * Return: allocated request handle in case of success, or NULL if out of memory
602 static inline struct ahash_request *ahash_request_alloc(
603 struct crypto_ahash *tfm, gfp_t gfp)
605 struct ahash_request *req;
607 req = kmalloc(sizeof(struct ahash_request) +
608 crypto_ahash_reqsize(tfm), gfp);
611 ahash_request_set_tfm(req, tfm);
617 * ahash_request_free() - zeroize and free the request data structure
618 * @req: request data structure cipher handle to be freed
620 static inline void ahash_request_free(struct ahash_request *req)
622 kfree_sensitive(req);
625 static inline void ahash_request_zero(struct ahash_request *req)
627 memzero_explicit(req, sizeof(*req) +
628 crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
631 static inline struct ahash_request *ahash_request_cast(
632 struct crypto_async_request *req)
634 return container_of(req, struct ahash_request, base);
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.
654 * This function allows setting the callback function that is triggered once
655 * the cipher operation completes.
657 * The callback function is registered with the &ahash_request handle and
658 * must comply with the following template::
660 * void callback_function(struct crypto_async_request *req, int error)
662 static inline void ahash_request_set_callback(struct ahash_request *req,
664 crypto_completion_t compl,
667 req->base.complete = compl;
668 req->base.data = data;
669 req->base.flags = flags;
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
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
685 static inline void ahash_request_set_crypt(struct ahash_request *req,
686 struct scatterlist *src, u8 *result,
690 req->nbytes = nbytes;
691 req->result = result;
695 * DOC: Synchronous Message Digest API
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)
700 * The message digest API is able to maintain state information for the
703 * The synchronous message digest API can store user-related context in its
704 * shash_desc request data structure.
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
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.
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.
721 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
724 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
730 * crypto_free_shash() - zeroize and free the message digest handle
731 * @tfm: cipher handle to be freed
733 static inline void crypto_free_shash(struct crypto_shash *tfm)
735 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
738 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
740 return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
743 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
745 return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
748 static inline unsigned int crypto_shash_alignmask(
749 struct crypto_shash *tfm)
751 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
755 * crypto_shash_blocksize() - obtain block size for cipher
756 * @tfm: cipher handle
758 * The block size for the message digest cipher referenced with the cipher
759 * handle is returned.
761 * Return: block size of cipher
763 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
765 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
768 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
770 return container_of(alg, struct shash_alg, base);
773 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
775 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
779 * crypto_shash_digestsize() - obtain message digest size
780 * @tfm: cipher handle
782 * The size for the message digest created by the message digest cipher
783 * referenced with the cipher handle is returned.
785 * Return: digest size of cipher
787 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
789 return crypto_shash_alg(tfm)->digestsize;
792 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
794 return crypto_shash_alg(tfm)->statesize;
797 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
799 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
802 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
804 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
807 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
809 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
813 * crypto_shash_descsize() - obtain the operational state size
814 * @tfm: cipher handle
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.
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)
825 * Return: size of the operational state
827 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
829 return tfm->descsize;
832 static inline void *shash_desc_ctx(struct shash_desc *desc)
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
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.
847 * Context: Any context.
848 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
850 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
851 unsigned int keylen);
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()
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.
864 * Context: Any context.
865 * Return: 0 if the message digest creation was successful; < 0 if an error
868 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
869 unsigned int len, u8 *out);
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()
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.
884 * Context: Any context.
885 * Return: 0 on success; < 0 if an error occurred.
887 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
888 unsigned int len, u8 *out);
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
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).
899 * Context: Any context.
900 * Return: 0 if the export creation was successful; < 0 if an error occurred
902 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
904 return crypto_shash_alg(desc->tfm)->export(desc, out);
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
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.
916 * Context: Any context.
917 * Return: 0 if the import was successful; < 0 if an error occurred
919 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
921 struct crypto_shash *tfm = desc->tfm;
923 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
926 return crypto_shash_alg(tfm)->import(desc, in);
930 * crypto_shash_init() - (re)initialize message digest
931 * @desc: operational state handle that is already filled
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.
937 * Context: Any context.
938 * Return: 0 if the message digest initialization was successful; < 0 if an
941 static inline int crypto_shash_init(struct shash_desc *desc)
943 struct crypto_shash *tfm = desc->tfm;
945 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
948 return crypto_shash_alg(tfm)->init(desc);
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
957 * Updates the message digest state of the operational state handle.
959 * Context: Any context.
960 * Return: 0 if the message digest update was successful; < 0 if an error
963 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
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
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.
976 * Context: Any context.
977 * Return: 0 if the message digest creation was successful; < 0 if an error
980 int crypto_shash_final(struct shash_desc *desc, u8 *out);
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()
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.
993 * Context: Any context.
994 * Return: 0 if the message digest creation was successful; < 0 if an error
997 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
998 unsigned int len, u8 *out);
1000 static inline void shash_desc_zero(struct shash_desc *desc)
1002 memzero_explicit(desc,
1003 sizeof(*desc) + crypto_shash_descsize(desc->tfm));
1006 #endif /* _CRYPTO_HASH_H */