162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Hash: Hash algorithms under the crypto API
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _CRYPTO_HASH_H
962306a36Sopenharmony_ci#define _CRYPTO_HASH_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/atomic.h>
1262306a36Sopenharmony_ci#include <linux/crypto.h>
1362306a36Sopenharmony_ci#include <linux/string.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct crypto_ahash;
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/**
1862306a36Sopenharmony_ci * DOC: Message Digest Algorithm Definitions
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * These data structures define modular message digest algorithm
2162306a36Sopenharmony_ci * implementations, managed via crypto_register_ahash(),
2262306a36Sopenharmony_ci * crypto_register_shash(), crypto_unregister_ahash() and
2362306a36Sopenharmony_ci * crypto_unregister_shash().
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * struct crypto_istat_hash - statistics for has algorithm
2862306a36Sopenharmony_ci * @hash_cnt:		number of hash requests
2962306a36Sopenharmony_ci * @hash_tlen:		total data size hashed
3062306a36Sopenharmony_ci * @err_cnt:		number of error for hash requests
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_cistruct crypto_istat_hash {
3362306a36Sopenharmony_ci	atomic64_t hash_cnt;
3462306a36Sopenharmony_ci	atomic64_t hash_tlen;
3562306a36Sopenharmony_ci	atomic64_t err_cnt;
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_STATS
3962306a36Sopenharmony_ci#define HASH_ALG_COMMON_STAT struct crypto_istat_hash stat;
4062306a36Sopenharmony_ci#else
4162306a36Sopenharmony_ci#define HASH_ALG_COMMON_STAT
4262306a36Sopenharmony_ci#endif
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/*
4562306a36Sopenharmony_ci * struct hash_alg_common - define properties of message digest
4662306a36Sopenharmony_ci * @stat: Statistics for hash algorithm.
4762306a36Sopenharmony_ci * @digestsize: Size of the result of the transformation. A buffer of this size
4862306a36Sopenharmony_ci *	        must be available to the @final and @finup calls, so they can
4962306a36Sopenharmony_ci *	        store the resulting hash into it. For various predefined sizes,
5062306a36Sopenharmony_ci *	        search include/crypto/ using
5162306a36Sopenharmony_ci *	        git grep _DIGEST_SIZE include/crypto.
5262306a36Sopenharmony_ci * @statesize: Size of the block for partial state of the transformation. A
5362306a36Sopenharmony_ci *	       buffer of this size must be passed to the @export function as it
5462306a36Sopenharmony_ci *	       will save the partial state of the transformation into it. On the
5562306a36Sopenharmony_ci *	       other side, the @import function will load the state from a
5662306a36Sopenharmony_ci *	       buffer of this size as well.
5762306a36Sopenharmony_ci * @base: Start of data structure of cipher algorithm. The common data
5862306a36Sopenharmony_ci *	  structure of crypto_alg contains information common to all ciphers.
5962306a36Sopenharmony_ci *	  The hash_alg_common data structure now adds the hash-specific
6062306a36Sopenharmony_ci *	  information.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ci#define HASH_ALG_COMMON {		\
6362306a36Sopenharmony_ci	HASH_ALG_COMMON_STAT		\
6462306a36Sopenharmony_ci					\
6562306a36Sopenharmony_ci	unsigned int digestsize;	\
6662306a36Sopenharmony_ci	unsigned int statesize;		\
6762306a36Sopenharmony_ci					\
6862306a36Sopenharmony_ci	struct crypto_alg base;		\
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_cistruct hash_alg_common HASH_ALG_COMMON;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistruct ahash_request {
7362306a36Sopenharmony_ci	struct crypto_async_request base;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	unsigned int nbytes;
7662306a36Sopenharmony_ci	struct scatterlist *src;
7762306a36Sopenharmony_ci	u8 *result;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* This field may only be used by the ahash API code. */
8062306a36Sopenharmony_ci	void *priv;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	void *__ctx[] CRYPTO_MINALIGN_ATTR;
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/**
8662306a36Sopenharmony_ci * struct ahash_alg - asynchronous message digest definition
8762306a36Sopenharmony_ci * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the
8862306a36Sopenharmony_ci *	  state of the HASH transformation at the beginning. This shall fill in
8962306a36Sopenharmony_ci *	  the internal structures used during the entire duration of the whole
9062306a36Sopenharmony_ci *	  transformation. No data processing happens at this point. Driver code
9162306a36Sopenharmony_ci *	  implementation must not use req->result.
9262306a36Sopenharmony_ci * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This
9362306a36Sopenharmony_ci *	   function actually pushes blocks of data from upper layers into the
9462306a36Sopenharmony_ci *	   driver, which then passes those to the hardware as seen fit. This
9562306a36Sopenharmony_ci *	   function must not finalize the HASH transformation by calculating the
9662306a36Sopenharmony_ci *	   final message digest as this only adds more data into the
9762306a36Sopenharmony_ci *	   transformation. This function shall not modify the transformation
9862306a36Sopenharmony_ci *	   context, as this function may be called in parallel with the same
9962306a36Sopenharmony_ci *	   transformation object. Data processing can happen synchronously
10062306a36Sopenharmony_ci *	   [SHASH] or asynchronously [AHASH] at this point. Driver must not use
10162306a36Sopenharmony_ci *	   req->result.
10262306a36Sopenharmony_ci * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the
10362306a36Sopenharmony_ci *	   transformation and retrieves the resulting hash from the driver and
10462306a36Sopenharmony_ci *	   pushes it back to upper layers. No data processing happens at this
10562306a36Sopenharmony_ci *	   point unless hardware requires it to finish the transformation
10662306a36Sopenharmony_ci *	   (then the data buffered by the device driver is processed).
10762306a36Sopenharmony_ci * @finup: **[optional]** Combination of @update and @final. This function is effectively a
10862306a36Sopenharmony_ci *	   combination of @update and @final calls issued in sequence. As some
10962306a36Sopenharmony_ci *	   hardware cannot do @update and @final separately, this callback was
11062306a36Sopenharmony_ci *	   added to allow such hardware to be used at least by IPsec. Data
11162306a36Sopenharmony_ci *	   processing can happen synchronously [SHASH] or asynchronously [AHASH]
11262306a36Sopenharmony_ci *	   at this point.
11362306a36Sopenharmony_ci * @digest: Combination of @init and @update and @final. This function
11462306a36Sopenharmony_ci *	    effectively behaves as the entire chain of operations, @init,
11562306a36Sopenharmony_ci *	    @update and @final issued in sequence. Just like @finup, this was
11662306a36Sopenharmony_ci *	    added for hardware which cannot do even the @finup, but can only do
11762306a36Sopenharmony_ci *	    the whole transformation in one run. Data processing can happen
11862306a36Sopenharmony_ci *	    synchronously [SHASH] or asynchronously [AHASH] at this point.
11962306a36Sopenharmony_ci * @setkey: Set optional key used by the hashing algorithm. Intended to push
12062306a36Sopenharmony_ci *	    optional key used by the hashing algorithm from upper layers into
12162306a36Sopenharmony_ci *	    the driver. This function can store the key in the transformation
12262306a36Sopenharmony_ci *	    context or can outright program it into the hardware. In the former
12362306a36Sopenharmony_ci *	    case, one must be careful to program the key into the hardware at
12462306a36Sopenharmony_ci *	    appropriate time and one must be careful that .setkey() can be
12562306a36Sopenharmony_ci *	    called multiple times during the existence of the transformation
12662306a36Sopenharmony_ci *	    object. Not  all hashing algorithms do implement this function as it
12762306a36Sopenharmony_ci *	    is only needed for keyed message digests. SHAx/MDx/CRCx do NOT
12862306a36Sopenharmony_ci *	    implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement
12962306a36Sopenharmony_ci *	    this function. This function must be called before any other of the
13062306a36Sopenharmony_ci *	    @init, @update, @final, @finup, @digest is called. No data
13162306a36Sopenharmony_ci *	    processing happens at this point.
13262306a36Sopenharmony_ci * @export: Export partial state of the transformation. This function dumps the
13362306a36Sopenharmony_ci *	    entire state of the ongoing transformation into a provided block of
13462306a36Sopenharmony_ci *	    data so it can be @import 'ed back later on. This is useful in case
13562306a36Sopenharmony_ci *	    you want to save partial result of the transformation after
13662306a36Sopenharmony_ci *	    processing certain amount of data and reload this partial result
13762306a36Sopenharmony_ci *	    multiple times later on for multiple re-use. No data processing
13862306a36Sopenharmony_ci *	    happens at this point. Driver must not use req->result.
13962306a36Sopenharmony_ci * @import: Import partial state of the transformation. This function loads the
14062306a36Sopenharmony_ci *	    entire state of the ongoing transformation from a provided block of
14162306a36Sopenharmony_ci *	    data so the transformation can continue from this point onward. No
14262306a36Sopenharmony_ci *	    data processing happens at this point. Driver must not use
14362306a36Sopenharmony_ci *	    req->result.
14462306a36Sopenharmony_ci * @init_tfm: Initialize the cryptographic transformation object.
14562306a36Sopenharmony_ci *	      This function is called only once at the instantiation
14662306a36Sopenharmony_ci *	      time, right after the transformation context was
14762306a36Sopenharmony_ci *	      allocated. In case the cryptographic hardware has
14862306a36Sopenharmony_ci *	      some special requirements which need to be handled
14962306a36Sopenharmony_ci *	      by software, this function shall check for the precise
15062306a36Sopenharmony_ci *	      requirement of the transformation and put any software
15162306a36Sopenharmony_ci *	      fallbacks in place.
15262306a36Sopenharmony_ci * @exit_tfm: Deinitialize the cryptographic transformation object.
15362306a36Sopenharmony_ci *	      This is a counterpart to @init_tfm, used to remove
15462306a36Sopenharmony_ci *	      various changes set in @init_tfm.
15562306a36Sopenharmony_ci * @clone_tfm: Copy transform into new object, may allocate memory.
15662306a36Sopenharmony_ci * @halg: see struct hash_alg_common
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_cistruct ahash_alg {
15962306a36Sopenharmony_ci	int (*init)(struct ahash_request *req);
16062306a36Sopenharmony_ci	int (*update)(struct ahash_request *req);
16162306a36Sopenharmony_ci	int (*final)(struct ahash_request *req);
16262306a36Sopenharmony_ci	int (*finup)(struct ahash_request *req);
16362306a36Sopenharmony_ci	int (*digest)(struct ahash_request *req);
16462306a36Sopenharmony_ci	int (*export)(struct ahash_request *req, void *out);
16562306a36Sopenharmony_ci	int (*import)(struct ahash_request *req, const void *in);
16662306a36Sopenharmony_ci	int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
16762306a36Sopenharmony_ci		      unsigned int keylen);
16862306a36Sopenharmony_ci	int (*init_tfm)(struct crypto_ahash *tfm);
16962306a36Sopenharmony_ci	void (*exit_tfm)(struct crypto_ahash *tfm);
17062306a36Sopenharmony_ci	int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	struct hash_alg_common halg;
17362306a36Sopenharmony_ci};
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistruct shash_desc {
17662306a36Sopenharmony_ci	struct crypto_shash *tfm;
17762306a36Sopenharmony_ci	void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#define HASH_MAX_DIGESTSIZE	 64
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/*
18362306a36Sopenharmony_ci * Worst case is hmac(sha3-224-generic).  Its context is a nested 'shash_desc'
18462306a36Sopenharmony_ci * containing a 'struct sha3_state'.
18562306a36Sopenharmony_ci */
18662306a36Sopenharmony_ci#define HASH_MAX_DESCSIZE	(sizeof(struct shash_desc) + 360)
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci#define SHASH_DESC_ON_STACK(shash, ctx)					     \
18962306a36Sopenharmony_ci	char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
19062306a36Sopenharmony_ci		__aligned(__alignof__(struct shash_desc));		     \
19162306a36Sopenharmony_ci	struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci/**
19462306a36Sopenharmony_ci * struct shash_alg - synchronous message digest definition
19562306a36Sopenharmony_ci * @init: see struct ahash_alg
19662306a36Sopenharmony_ci * @update: see struct ahash_alg
19762306a36Sopenharmony_ci * @final: see struct ahash_alg
19862306a36Sopenharmony_ci * @finup: see struct ahash_alg
19962306a36Sopenharmony_ci * @digest: see struct ahash_alg
20062306a36Sopenharmony_ci * @export: see struct ahash_alg
20162306a36Sopenharmony_ci * @import: see struct ahash_alg
20262306a36Sopenharmony_ci * @setkey: see struct ahash_alg
20362306a36Sopenharmony_ci * @init_tfm: Initialize the cryptographic transformation object.
20462306a36Sopenharmony_ci *	      This function is called only once at the instantiation
20562306a36Sopenharmony_ci *	      time, right after the transformation context was
20662306a36Sopenharmony_ci *	      allocated. In case the cryptographic hardware has
20762306a36Sopenharmony_ci *	      some special requirements which need to be handled
20862306a36Sopenharmony_ci *	      by software, this function shall check for the precise
20962306a36Sopenharmony_ci *	      requirement of the transformation and put any software
21062306a36Sopenharmony_ci *	      fallbacks in place.
21162306a36Sopenharmony_ci * @exit_tfm: Deinitialize the cryptographic transformation object.
21262306a36Sopenharmony_ci *	      This is a counterpart to @init_tfm, used to remove
21362306a36Sopenharmony_ci *	      various changes set in @init_tfm.
21462306a36Sopenharmony_ci * @clone_tfm: Copy transform into new object, may allocate memory.
21562306a36Sopenharmony_ci * @digestsize: see struct ahash_alg
21662306a36Sopenharmony_ci * @statesize: see struct ahash_alg
21762306a36Sopenharmony_ci * @descsize: Size of the operational state for the message digest. This state
21862306a36Sopenharmony_ci * 	      size is the memory size that needs to be allocated for
21962306a36Sopenharmony_ci *	      shash_desc.__ctx
22062306a36Sopenharmony_ci * @stat: Statistics for hash algorithm.
22162306a36Sopenharmony_ci * @base: internally used
22262306a36Sopenharmony_ci * @halg: see struct hash_alg_common
22362306a36Sopenharmony_ci * @HASH_ALG_COMMON: see struct hash_alg_common
22462306a36Sopenharmony_ci */
22562306a36Sopenharmony_cistruct shash_alg {
22662306a36Sopenharmony_ci	int (*init)(struct shash_desc *desc);
22762306a36Sopenharmony_ci	int (*update)(struct shash_desc *desc, const u8 *data,
22862306a36Sopenharmony_ci		      unsigned int len);
22962306a36Sopenharmony_ci	int (*final)(struct shash_desc *desc, u8 *out);
23062306a36Sopenharmony_ci	int (*finup)(struct shash_desc *desc, const u8 *data,
23162306a36Sopenharmony_ci		     unsigned int len, u8 *out);
23262306a36Sopenharmony_ci	int (*digest)(struct shash_desc *desc, const u8 *data,
23362306a36Sopenharmony_ci		      unsigned int len, u8 *out);
23462306a36Sopenharmony_ci	int (*export)(struct shash_desc *desc, void *out);
23562306a36Sopenharmony_ci	int (*import)(struct shash_desc *desc, const void *in);
23662306a36Sopenharmony_ci	int (*setkey)(struct crypto_shash *tfm, const u8 *key,
23762306a36Sopenharmony_ci		      unsigned int keylen);
23862306a36Sopenharmony_ci	int (*init_tfm)(struct crypto_shash *tfm);
23962306a36Sopenharmony_ci	void (*exit_tfm)(struct crypto_shash *tfm);
24062306a36Sopenharmony_ci	int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	unsigned int descsize;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	union {
24562306a36Sopenharmony_ci		struct HASH_ALG_COMMON;
24662306a36Sopenharmony_ci		struct hash_alg_common halg;
24762306a36Sopenharmony_ci	};
24862306a36Sopenharmony_ci};
24962306a36Sopenharmony_ci#undef HASH_ALG_COMMON
25062306a36Sopenharmony_ci#undef HASH_ALG_COMMON_STAT
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistruct crypto_ahash {
25362306a36Sopenharmony_ci	int (*init)(struct ahash_request *req);
25462306a36Sopenharmony_ci	int (*update)(struct ahash_request *req);
25562306a36Sopenharmony_ci	int (*final)(struct ahash_request *req);
25662306a36Sopenharmony_ci	int (*finup)(struct ahash_request *req);
25762306a36Sopenharmony_ci	int (*digest)(struct ahash_request *req);
25862306a36Sopenharmony_ci	int (*export)(struct ahash_request *req, void *out);
25962306a36Sopenharmony_ci	int (*import)(struct ahash_request *req, const void *in);
26062306a36Sopenharmony_ci	int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
26162306a36Sopenharmony_ci		      unsigned int keylen);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	unsigned int statesize;
26462306a36Sopenharmony_ci	unsigned int reqsize;
26562306a36Sopenharmony_ci	struct crypto_tfm base;
26662306a36Sopenharmony_ci};
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_cistruct crypto_shash {
26962306a36Sopenharmony_ci	unsigned int descsize;
27062306a36Sopenharmony_ci	struct crypto_tfm base;
27162306a36Sopenharmony_ci};
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci/**
27462306a36Sopenharmony_ci * DOC: Asynchronous Message Digest API
27562306a36Sopenharmony_ci *
27662306a36Sopenharmony_ci * The asynchronous message digest API is used with the ciphers of type
27762306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
27862306a36Sopenharmony_ci *
27962306a36Sopenharmony_ci * The asynchronous cipher operation discussion provided for the
28062306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
28162306a36Sopenharmony_ci */
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_cistatic inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
28462306a36Sopenharmony_ci{
28562306a36Sopenharmony_ci	return container_of(tfm, struct crypto_ahash, base);
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci/**
28962306a36Sopenharmony_ci * crypto_alloc_ahash() - allocate ahash cipher handle
29062306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
29162306a36Sopenharmony_ci *	      ahash cipher
29262306a36Sopenharmony_ci * @type: specifies the type of the cipher
29362306a36Sopenharmony_ci * @mask: specifies the mask for the cipher
29462306a36Sopenharmony_ci *
29562306a36Sopenharmony_ci * Allocate a cipher handle for an ahash. The returned struct
29662306a36Sopenharmony_ci * crypto_ahash is the cipher handle that is required for any subsequent
29762306a36Sopenharmony_ci * API invocation for that ahash.
29862306a36Sopenharmony_ci *
29962306a36Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case
30062306a36Sopenharmony_ci *	   of an error, PTR_ERR() returns the error code.
30162306a36Sopenharmony_ci */
30262306a36Sopenharmony_cistruct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
30362306a36Sopenharmony_ci					u32 mask);
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_cistruct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	return &tfm->base;
31062306a36Sopenharmony_ci}
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci/**
31362306a36Sopenharmony_ci * crypto_free_ahash() - zeroize and free the ahash handle
31462306a36Sopenharmony_ci * @tfm: cipher handle to be freed
31562306a36Sopenharmony_ci *
31662306a36Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing.
31762306a36Sopenharmony_ci */
31862306a36Sopenharmony_cistatic inline void crypto_free_ahash(struct crypto_ahash *tfm)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci/**
32462306a36Sopenharmony_ci * crypto_has_ahash() - Search for the availability of an ahash.
32562306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
32662306a36Sopenharmony_ci *	      ahash
32762306a36Sopenharmony_ci * @type: specifies the type of the ahash
32862306a36Sopenharmony_ci * @mask: specifies the mask for the ahash
32962306a36Sopenharmony_ci *
33062306a36Sopenharmony_ci * Return: true when the ahash is known to the kernel crypto API; false
33162306a36Sopenharmony_ci *	   otherwise
33262306a36Sopenharmony_ci */
33362306a36Sopenharmony_ciint crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_cistatic inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
33662306a36Sopenharmony_ci{
33762306a36Sopenharmony_ci	return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_cistatic inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
34362306a36Sopenharmony_ci}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_cistatic inline unsigned int crypto_ahash_alignmask(
34662306a36Sopenharmony_ci	struct crypto_ahash *tfm)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci/**
35262306a36Sopenharmony_ci * crypto_ahash_blocksize() - obtain block size for cipher
35362306a36Sopenharmony_ci * @tfm: cipher handle
35462306a36Sopenharmony_ci *
35562306a36Sopenharmony_ci * The block size for the message digest cipher referenced with the cipher
35662306a36Sopenharmony_ci * handle is returned.
35762306a36Sopenharmony_ci *
35862306a36Sopenharmony_ci * Return: block size of cipher
35962306a36Sopenharmony_ci */
36062306a36Sopenharmony_cistatic inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
36162306a36Sopenharmony_ci{
36262306a36Sopenharmony_ci	return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_cistatic inline struct hash_alg_common *__crypto_hash_alg_common(
36662306a36Sopenharmony_ci	struct crypto_alg *alg)
36762306a36Sopenharmony_ci{
36862306a36Sopenharmony_ci	return container_of(alg, struct hash_alg_common, base);
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_cistatic inline struct hash_alg_common *crypto_hash_alg_common(
37262306a36Sopenharmony_ci	struct crypto_ahash *tfm)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
37562306a36Sopenharmony_ci}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci/**
37862306a36Sopenharmony_ci * crypto_ahash_digestsize() - obtain message digest size
37962306a36Sopenharmony_ci * @tfm: cipher handle
38062306a36Sopenharmony_ci *
38162306a36Sopenharmony_ci * The size for the message digest created by the message digest cipher
38262306a36Sopenharmony_ci * referenced with the cipher handle is returned.
38362306a36Sopenharmony_ci *
38462306a36Sopenharmony_ci *
38562306a36Sopenharmony_ci * Return: message digest size of cipher
38662306a36Sopenharmony_ci */
38762306a36Sopenharmony_cistatic inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
38862306a36Sopenharmony_ci{
38962306a36Sopenharmony_ci	return crypto_hash_alg_common(tfm)->digestsize;
39062306a36Sopenharmony_ci}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci/**
39362306a36Sopenharmony_ci * crypto_ahash_statesize() - obtain size of the ahash state
39462306a36Sopenharmony_ci * @tfm: cipher handle
39562306a36Sopenharmony_ci *
39662306a36Sopenharmony_ci * Return the size of the ahash state. With the crypto_ahash_export()
39762306a36Sopenharmony_ci * function, the caller can export the state into a buffer whose size is
39862306a36Sopenharmony_ci * defined with this function.
39962306a36Sopenharmony_ci *
40062306a36Sopenharmony_ci * Return: size of the ahash state
40162306a36Sopenharmony_ci */
40262306a36Sopenharmony_cistatic inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	return tfm->statesize;
40562306a36Sopenharmony_ci}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_cistatic inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
40862306a36Sopenharmony_ci{
40962306a36Sopenharmony_ci	return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
41062306a36Sopenharmony_ci}
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_cistatic inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
41362306a36Sopenharmony_ci{
41462306a36Sopenharmony_ci	crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_cistatic inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
41862306a36Sopenharmony_ci{
41962306a36Sopenharmony_ci	crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
42062306a36Sopenharmony_ci}
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci/**
42362306a36Sopenharmony_ci * crypto_ahash_reqtfm() - obtain cipher handle from request
42462306a36Sopenharmony_ci * @req: asynchronous request handle that contains the reference to the ahash
42562306a36Sopenharmony_ci *	 cipher handle
42662306a36Sopenharmony_ci *
42762306a36Sopenharmony_ci * Return the ahash cipher handle that is registered with the asynchronous
42862306a36Sopenharmony_ci * request handle ahash_request.
42962306a36Sopenharmony_ci *
43062306a36Sopenharmony_ci * Return: ahash cipher handle
43162306a36Sopenharmony_ci */
43262306a36Sopenharmony_cistatic inline struct crypto_ahash *crypto_ahash_reqtfm(
43362306a36Sopenharmony_ci	struct ahash_request *req)
43462306a36Sopenharmony_ci{
43562306a36Sopenharmony_ci	return __crypto_ahash_cast(req->base.tfm);
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci/**
43962306a36Sopenharmony_ci * crypto_ahash_reqsize() - obtain size of the request data structure
44062306a36Sopenharmony_ci * @tfm: cipher handle
44162306a36Sopenharmony_ci *
44262306a36Sopenharmony_ci * Return: size of the request data
44362306a36Sopenharmony_ci */
44462306a36Sopenharmony_cistatic inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
44562306a36Sopenharmony_ci{
44662306a36Sopenharmony_ci	return tfm->reqsize;
44762306a36Sopenharmony_ci}
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_cistatic inline void *ahash_request_ctx(struct ahash_request *req)
45062306a36Sopenharmony_ci{
45162306a36Sopenharmony_ci	return req->__ctx;
45262306a36Sopenharmony_ci}
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci/**
45562306a36Sopenharmony_ci * crypto_ahash_setkey - set key for cipher handle
45662306a36Sopenharmony_ci * @tfm: cipher handle
45762306a36Sopenharmony_ci * @key: buffer holding the key
45862306a36Sopenharmony_ci * @keylen: length of the key in bytes
45962306a36Sopenharmony_ci *
46062306a36Sopenharmony_ci * The caller provided key is set for the ahash cipher. The cipher
46162306a36Sopenharmony_ci * handle must point to a keyed hash in order for this function to succeed.
46262306a36Sopenharmony_ci *
46362306a36Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred
46462306a36Sopenharmony_ci */
46562306a36Sopenharmony_ciint crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
46662306a36Sopenharmony_ci			unsigned int keylen);
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci/**
46962306a36Sopenharmony_ci * crypto_ahash_finup() - update and finalize message digest
47062306a36Sopenharmony_ci * @req: reference to the ahash_request handle that holds all information
47162306a36Sopenharmony_ci *	 needed to perform the cipher operation
47262306a36Sopenharmony_ci *
47362306a36Sopenharmony_ci * This function is a "short-hand" for the function calls of
47462306a36Sopenharmony_ci * crypto_ahash_update and crypto_ahash_final. The parameters have the same
47562306a36Sopenharmony_ci * meaning as discussed for those separate functions.
47662306a36Sopenharmony_ci *
47762306a36Sopenharmony_ci * Return: see crypto_ahash_final()
47862306a36Sopenharmony_ci */
47962306a36Sopenharmony_ciint crypto_ahash_finup(struct ahash_request *req);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci/**
48262306a36Sopenharmony_ci * crypto_ahash_final() - calculate message digest
48362306a36Sopenharmony_ci * @req: reference to the ahash_request handle that holds all information
48462306a36Sopenharmony_ci *	 needed to perform the cipher operation
48562306a36Sopenharmony_ci *
48662306a36Sopenharmony_ci * Finalize the message digest operation and create the message digest
48762306a36Sopenharmony_ci * based on all data added to the cipher handle. The message digest is placed
48862306a36Sopenharmony_ci * into the output buffer registered with the ahash_request handle.
48962306a36Sopenharmony_ci *
49062306a36Sopenharmony_ci * Return:
49162306a36Sopenharmony_ci * 0		if the message digest was successfully calculated;
49262306a36Sopenharmony_ci * -EINPROGRESS	if data is fed into hardware (DMA) or queued for later;
49362306a36Sopenharmony_ci * -EBUSY	if queue is full and request should be resubmitted later;
49462306a36Sopenharmony_ci * other < 0	if an error occurred
49562306a36Sopenharmony_ci */
49662306a36Sopenharmony_ciint crypto_ahash_final(struct ahash_request *req);
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci/**
49962306a36Sopenharmony_ci * crypto_ahash_digest() - calculate message digest for a buffer
50062306a36Sopenharmony_ci * @req: reference to the ahash_request handle that holds all information
50162306a36Sopenharmony_ci *	 needed to perform the cipher operation
50262306a36Sopenharmony_ci *
50362306a36Sopenharmony_ci * This function is a "short-hand" for the function calls of crypto_ahash_init,
50462306a36Sopenharmony_ci * crypto_ahash_update and crypto_ahash_final. The parameters have the same
50562306a36Sopenharmony_ci * meaning as discussed for those separate three functions.
50662306a36Sopenharmony_ci *
50762306a36Sopenharmony_ci * Return: see crypto_ahash_final()
50862306a36Sopenharmony_ci */
50962306a36Sopenharmony_ciint crypto_ahash_digest(struct ahash_request *req);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci/**
51262306a36Sopenharmony_ci * crypto_ahash_export() - extract current message digest state
51362306a36Sopenharmony_ci * @req: reference to the ahash_request handle whose state is exported
51462306a36Sopenharmony_ci * @out: output buffer of sufficient size that can hold the hash state
51562306a36Sopenharmony_ci *
51662306a36Sopenharmony_ci * This function exports the hash state of the ahash_request handle into the
51762306a36Sopenharmony_ci * caller-allocated output buffer out which must have sufficient size (e.g. by
51862306a36Sopenharmony_ci * calling crypto_ahash_statesize()).
51962306a36Sopenharmony_ci *
52062306a36Sopenharmony_ci * Return: 0 if the export was successful; < 0 if an error occurred
52162306a36Sopenharmony_ci */
52262306a36Sopenharmony_cistatic inline int crypto_ahash_export(struct ahash_request *req, void *out)
52362306a36Sopenharmony_ci{
52462306a36Sopenharmony_ci	return crypto_ahash_reqtfm(req)->export(req, out);
52562306a36Sopenharmony_ci}
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci/**
52862306a36Sopenharmony_ci * crypto_ahash_import() - import message digest state
52962306a36Sopenharmony_ci * @req: reference to ahash_request handle the state is imported into
53062306a36Sopenharmony_ci * @in: buffer holding the state
53162306a36Sopenharmony_ci *
53262306a36Sopenharmony_ci * This function imports the hash state into the ahash_request handle from the
53362306a36Sopenharmony_ci * input buffer. That buffer should have been generated with the
53462306a36Sopenharmony_ci * crypto_ahash_export function.
53562306a36Sopenharmony_ci *
53662306a36Sopenharmony_ci * Return: 0 if the import was successful; < 0 if an error occurred
53762306a36Sopenharmony_ci */
53862306a36Sopenharmony_cistatic inline int crypto_ahash_import(struct ahash_request *req, const void *in)
53962306a36Sopenharmony_ci{
54062306a36Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
54362306a36Sopenharmony_ci		return -ENOKEY;
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	return tfm->import(req, in);
54662306a36Sopenharmony_ci}
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci/**
54962306a36Sopenharmony_ci * crypto_ahash_init() - (re)initialize message digest handle
55062306a36Sopenharmony_ci * @req: ahash_request handle that already is initialized with all necessary
55162306a36Sopenharmony_ci *	 data using the ahash_request_* API functions
55262306a36Sopenharmony_ci *
55362306a36Sopenharmony_ci * The call (re-)initializes the message digest referenced by the ahash_request
55462306a36Sopenharmony_ci * handle. Any potentially existing state created by previous operations is
55562306a36Sopenharmony_ci * discarded.
55662306a36Sopenharmony_ci *
55762306a36Sopenharmony_ci * Return: see crypto_ahash_final()
55862306a36Sopenharmony_ci */
55962306a36Sopenharmony_cistatic inline int crypto_ahash_init(struct ahash_request *req)
56062306a36Sopenharmony_ci{
56162306a36Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci	if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
56462306a36Sopenharmony_ci		return -ENOKEY;
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci	return tfm->init(req);
56762306a36Sopenharmony_ci}
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_cistatic inline struct crypto_istat_hash *hash_get_stat(
57062306a36Sopenharmony_ci	struct hash_alg_common *alg)
57162306a36Sopenharmony_ci{
57262306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_STATS
57362306a36Sopenharmony_ci	return &alg->stat;
57462306a36Sopenharmony_ci#else
57562306a36Sopenharmony_ci	return NULL;
57662306a36Sopenharmony_ci#endif
57762306a36Sopenharmony_ci}
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_cistatic inline int crypto_hash_errstat(struct hash_alg_common *alg, int err)
58062306a36Sopenharmony_ci{
58162306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
58262306a36Sopenharmony_ci		return err;
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	if (err && err != -EINPROGRESS && err != -EBUSY)
58562306a36Sopenharmony_ci		atomic64_inc(&hash_get_stat(alg)->err_cnt);
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	return err;
58862306a36Sopenharmony_ci}
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci/**
59162306a36Sopenharmony_ci * crypto_ahash_update() - add data to message digest for processing
59262306a36Sopenharmony_ci * @req: ahash_request handle that was previously initialized with the
59362306a36Sopenharmony_ci *	 crypto_ahash_init call.
59462306a36Sopenharmony_ci *
59562306a36Sopenharmony_ci * Updates the message digest state of the &ahash_request handle. The input data
59662306a36Sopenharmony_ci * is pointed to by the scatter/gather list registered in the &ahash_request
59762306a36Sopenharmony_ci * handle
59862306a36Sopenharmony_ci *
59962306a36Sopenharmony_ci * Return: see crypto_ahash_final()
60062306a36Sopenharmony_ci */
60162306a36Sopenharmony_cistatic inline int crypto_ahash_update(struct ahash_request *req)
60262306a36Sopenharmony_ci{
60362306a36Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
60462306a36Sopenharmony_ci	struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_CRYPTO_STATS))
60762306a36Sopenharmony_ci		atomic64_add(req->nbytes, &hash_get_stat(alg)->hash_tlen);
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	return crypto_hash_errstat(alg, tfm->update(req));
61062306a36Sopenharmony_ci}
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci/**
61362306a36Sopenharmony_ci * DOC: Asynchronous Hash Request Handle
61462306a36Sopenharmony_ci *
61562306a36Sopenharmony_ci * The &ahash_request data structure contains all pointers to data
61662306a36Sopenharmony_ci * required for the asynchronous cipher operation. This includes the cipher
61762306a36Sopenharmony_ci * handle (which can be used by multiple &ahash_request instances), pointer
61862306a36Sopenharmony_ci * to plaintext and the message digest output buffer, asynchronous callback
61962306a36Sopenharmony_ci * function, etc. It acts as a handle to the ahash_request_* API calls in a
62062306a36Sopenharmony_ci * similar way as ahash handle to the crypto_ahash_* API calls.
62162306a36Sopenharmony_ci */
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci/**
62462306a36Sopenharmony_ci * ahash_request_set_tfm() - update cipher handle reference in request
62562306a36Sopenharmony_ci * @req: request handle to be modified
62662306a36Sopenharmony_ci * @tfm: cipher handle that shall be added to the request handle
62762306a36Sopenharmony_ci *
62862306a36Sopenharmony_ci * Allow the caller to replace the existing ahash handle in the request
62962306a36Sopenharmony_ci * data structure with a different one.
63062306a36Sopenharmony_ci */
63162306a36Sopenharmony_cistatic inline void ahash_request_set_tfm(struct ahash_request *req,
63262306a36Sopenharmony_ci					 struct crypto_ahash *tfm)
63362306a36Sopenharmony_ci{
63462306a36Sopenharmony_ci	req->base.tfm = crypto_ahash_tfm(tfm);
63562306a36Sopenharmony_ci}
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci/**
63862306a36Sopenharmony_ci * ahash_request_alloc() - allocate request data structure
63962306a36Sopenharmony_ci * @tfm: cipher handle to be registered with the request
64062306a36Sopenharmony_ci * @gfp: memory allocation flag that is handed to kmalloc by the API call.
64162306a36Sopenharmony_ci *
64262306a36Sopenharmony_ci * Allocate the request data structure that must be used with the ahash
64362306a36Sopenharmony_ci * message digest API calls. During
64462306a36Sopenharmony_ci * the allocation, the provided ahash handle
64562306a36Sopenharmony_ci * is registered in the request data structure.
64662306a36Sopenharmony_ci *
64762306a36Sopenharmony_ci * Return: allocated request handle in case of success, or NULL if out of memory
64862306a36Sopenharmony_ci */
64962306a36Sopenharmony_cistatic inline struct ahash_request *ahash_request_alloc(
65062306a36Sopenharmony_ci	struct crypto_ahash *tfm, gfp_t gfp)
65162306a36Sopenharmony_ci{
65262306a36Sopenharmony_ci	struct ahash_request *req;
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	req = kmalloc(sizeof(struct ahash_request) +
65562306a36Sopenharmony_ci		      crypto_ahash_reqsize(tfm), gfp);
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	if (likely(req))
65862306a36Sopenharmony_ci		ahash_request_set_tfm(req, tfm);
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	return req;
66162306a36Sopenharmony_ci}
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci/**
66462306a36Sopenharmony_ci * ahash_request_free() - zeroize and free the request data structure
66562306a36Sopenharmony_ci * @req: request data structure cipher handle to be freed
66662306a36Sopenharmony_ci */
66762306a36Sopenharmony_cistatic inline void ahash_request_free(struct ahash_request *req)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	kfree_sensitive(req);
67062306a36Sopenharmony_ci}
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_cistatic inline void ahash_request_zero(struct ahash_request *req)
67362306a36Sopenharmony_ci{
67462306a36Sopenharmony_ci	memzero_explicit(req, sizeof(*req) +
67562306a36Sopenharmony_ci			      crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
67662306a36Sopenharmony_ci}
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_cistatic inline struct ahash_request *ahash_request_cast(
67962306a36Sopenharmony_ci	struct crypto_async_request *req)
68062306a36Sopenharmony_ci{
68162306a36Sopenharmony_ci	return container_of(req, struct ahash_request, base);
68262306a36Sopenharmony_ci}
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci/**
68562306a36Sopenharmony_ci * ahash_request_set_callback() - set asynchronous callback function
68662306a36Sopenharmony_ci * @req: request handle
68762306a36Sopenharmony_ci * @flags: specify zero or an ORing of the flags
68862306a36Sopenharmony_ci *	   CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
68962306a36Sopenharmony_ci *	   increase the wait queue beyond the initial maximum size;
69062306a36Sopenharmony_ci *	   CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
69162306a36Sopenharmony_ci * @compl: callback function pointer to be registered with the request handle
69262306a36Sopenharmony_ci * @data: The data pointer refers to memory that is not used by the kernel
69362306a36Sopenharmony_ci *	  crypto API, but provided to the callback function for it to use. Here,
69462306a36Sopenharmony_ci *	  the caller can provide a reference to memory the callback function can
69562306a36Sopenharmony_ci *	  operate on. As the callback function is invoked asynchronously to the
69662306a36Sopenharmony_ci *	  related functionality, it may need to access data structures of the
69762306a36Sopenharmony_ci *	  related functionality which can be referenced using this pointer. The
69862306a36Sopenharmony_ci *	  callback function can access the memory via the "data" field in the
69962306a36Sopenharmony_ci *	  &crypto_async_request data structure provided to the callback function.
70062306a36Sopenharmony_ci *
70162306a36Sopenharmony_ci * This function allows setting the callback function that is triggered once
70262306a36Sopenharmony_ci * the cipher operation completes.
70362306a36Sopenharmony_ci *
70462306a36Sopenharmony_ci * The callback function is registered with the &ahash_request handle and
70562306a36Sopenharmony_ci * must comply with the following template::
70662306a36Sopenharmony_ci *
70762306a36Sopenharmony_ci *	void callback_function(struct crypto_async_request *req, int error)
70862306a36Sopenharmony_ci */
70962306a36Sopenharmony_cistatic inline void ahash_request_set_callback(struct ahash_request *req,
71062306a36Sopenharmony_ci					      u32 flags,
71162306a36Sopenharmony_ci					      crypto_completion_t compl,
71262306a36Sopenharmony_ci					      void *data)
71362306a36Sopenharmony_ci{
71462306a36Sopenharmony_ci	req->base.complete = compl;
71562306a36Sopenharmony_ci	req->base.data = data;
71662306a36Sopenharmony_ci	req->base.flags = flags;
71762306a36Sopenharmony_ci}
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci/**
72062306a36Sopenharmony_ci * ahash_request_set_crypt() - set data buffers
72162306a36Sopenharmony_ci * @req: ahash_request handle to be updated
72262306a36Sopenharmony_ci * @src: source scatter/gather list
72362306a36Sopenharmony_ci * @result: buffer that is filled with the message digest -- the caller must
72462306a36Sopenharmony_ci *	    ensure that the buffer has sufficient space by, for example, calling
72562306a36Sopenharmony_ci *	    crypto_ahash_digestsize()
72662306a36Sopenharmony_ci * @nbytes: number of bytes to process from the source scatter/gather list
72762306a36Sopenharmony_ci *
72862306a36Sopenharmony_ci * By using this call, the caller references the source scatter/gather list.
72962306a36Sopenharmony_ci * The source scatter/gather list points to the data the message digest is to
73062306a36Sopenharmony_ci * be calculated for.
73162306a36Sopenharmony_ci */
73262306a36Sopenharmony_cistatic inline void ahash_request_set_crypt(struct ahash_request *req,
73362306a36Sopenharmony_ci					   struct scatterlist *src, u8 *result,
73462306a36Sopenharmony_ci					   unsigned int nbytes)
73562306a36Sopenharmony_ci{
73662306a36Sopenharmony_ci	req->src = src;
73762306a36Sopenharmony_ci	req->nbytes = nbytes;
73862306a36Sopenharmony_ci	req->result = result;
73962306a36Sopenharmony_ci}
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci/**
74262306a36Sopenharmony_ci * DOC: Synchronous Message Digest API
74362306a36Sopenharmony_ci *
74462306a36Sopenharmony_ci * The synchronous message digest API is used with the ciphers of type
74562306a36Sopenharmony_ci * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
74662306a36Sopenharmony_ci *
74762306a36Sopenharmony_ci * The message digest API is able to maintain state information for the
74862306a36Sopenharmony_ci * caller.
74962306a36Sopenharmony_ci *
75062306a36Sopenharmony_ci * The synchronous message digest API can store user-related context in its
75162306a36Sopenharmony_ci * shash_desc request data structure.
75262306a36Sopenharmony_ci */
75362306a36Sopenharmony_ci
75462306a36Sopenharmony_ci/**
75562306a36Sopenharmony_ci * crypto_alloc_shash() - allocate message digest handle
75662306a36Sopenharmony_ci * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
75762306a36Sopenharmony_ci *	      message digest cipher
75862306a36Sopenharmony_ci * @type: specifies the type of the cipher
75962306a36Sopenharmony_ci * @mask: specifies the mask for the cipher
76062306a36Sopenharmony_ci *
76162306a36Sopenharmony_ci * Allocate a cipher handle for a message digest. The returned &struct
76262306a36Sopenharmony_ci * crypto_shash is the cipher handle that is required for any subsequent
76362306a36Sopenharmony_ci * API invocation for that message digest.
76462306a36Sopenharmony_ci *
76562306a36Sopenharmony_ci * Return: allocated cipher handle in case of success; IS_ERR() is true in case
76662306a36Sopenharmony_ci *	   of an error, PTR_ERR() returns the error code.
76762306a36Sopenharmony_ci */
76862306a36Sopenharmony_cistruct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
76962306a36Sopenharmony_ci					u32 mask);
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_cistruct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ciint crypto_has_shash(const char *alg_name, u32 type, u32 mask);
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_cistatic inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
77662306a36Sopenharmony_ci{
77762306a36Sopenharmony_ci	return &tfm->base;
77862306a36Sopenharmony_ci}
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci/**
78162306a36Sopenharmony_ci * crypto_free_shash() - zeroize and free the message digest handle
78262306a36Sopenharmony_ci * @tfm: cipher handle to be freed
78362306a36Sopenharmony_ci *
78462306a36Sopenharmony_ci * If @tfm is a NULL or error pointer, this function does nothing.
78562306a36Sopenharmony_ci */
78662306a36Sopenharmony_cistatic inline void crypto_free_shash(struct crypto_shash *tfm)
78762306a36Sopenharmony_ci{
78862306a36Sopenharmony_ci	crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
78962306a36Sopenharmony_ci}
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_cistatic inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
79262306a36Sopenharmony_ci{
79362306a36Sopenharmony_ci	return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
79462306a36Sopenharmony_ci}
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_cistatic inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
79762306a36Sopenharmony_ci{
79862306a36Sopenharmony_ci	return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
79962306a36Sopenharmony_ci}
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_cistatic inline unsigned int crypto_shash_alignmask(
80262306a36Sopenharmony_ci	struct crypto_shash *tfm)
80362306a36Sopenharmony_ci{
80462306a36Sopenharmony_ci	return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
80562306a36Sopenharmony_ci}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci/**
80862306a36Sopenharmony_ci * crypto_shash_blocksize() - obtain block size for cipher
80962306a36Sopenharmony_ci * @tfm: cipher handle
81062306a36Sopenharmony_ci *
81162306a36Sopenharmony_ci * The block size for the message digest cipher referenced with the cipher
81262306a36Sopenharmony_ci * handle is returned.
81362306a36Sopenharmony_ci *
81462306a36Sopenharmony_ci * Return: block size of cipher
81562306a36Sopenharmony_ci */
81662306a36Sopenharmony_cistatic inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
81762306a36Sopenharmony_ci{
81862306a36Sopenharmony_ci	return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
81962306a36Sopenharmony_ci}
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_cistatic inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
82262306a36Sopenharmony_ci{
82362306a36Sopenharmony_ci	return container_of(alg, struct shash_alg, base);
82462306a36Sopenharmony_ci}
82562306a36Sopenharmony_ci
82662306a36Sopenharmony_cistatic inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
82762306a36Sopenharmony_ci{
82862306a36Sopenharmony_ci	return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
82962306a36Sopenharmony_ci}
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci/**
83262306a36Sopenharmony_ci * crypto_shash_digestsize() - obtain message digest size
83362306a36Sopenharmony_ci * @tfm: cipher handle
83462306a36Sopenharmony_ci *
83562306a36Sopenharmony_ci * The size for the message digest created by the message digest cipher
83662306a36Sopenharmony_ci * referenced with the cipher handle is returned.
83762306a36Sopenharmony_ci *
83862306a36Sopenharmony_ci * Return: digest size of cipher
83962306a36Sopenharmony_ci */
84062306a36Sopenharmony_cistatic inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
84162306a36Sopenharmony_ci{
84262306a36Sopenharmony_ci	return crypto_shash_alg(tfm)->digestsize;
84362306a36Sopenharmony_ci}
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_cistatic inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
84662306a36Sopenharmony_ci{
84762306a36Sopenharmony_ci	return crypto_shash_alg(tfm)->statesize;
84862306a36Sopenharmony_ci}
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_cistatic inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
85162306a36Sopenharmony_ci{
85262306a36Sopenharmony_ci	return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
85362306a36Sopenharmony_ci}
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_cistatic inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
85662306a36Sopenharmony_ci{
85762306a36Sopenharmony_ci	crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
85862306a36Sopenharmony_ci}
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_cistatic inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
86162306a36Sopenharmony_ci{
86262306a36Sopenharmony_ci	crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
86362306a36Sopenharmony_ci}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci/**
86662306a36Sopenharmony_ci * crypto_shash_descsize() - obtain the operational state size
86762306a36Sopenharmony_ci * @tfm: cipher handle
86862306a36Sopenharmony_ci *
86962306a36Sopenharmony_ci * The size of the operational state the cipher needs during operation is
87062306a36Sopenharmony_ci * returned for the hash referenced with the cipher handle. This size is
87162306a36Sopenharmony_ci * required to calculate the memory requirements to allow the caller allocating
87262306a36Sopenharmony_ci * sufficient memory for operational state.
87362306a36Sopenharmony_ci *
87462306a36Sopenharmony_ci * The operational state is defined with struct shash_desc where the size of
87562306a36Sopenharmony_ci * that data structure is to be calculated as
87662306a36Sopenharmony_ci * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
87762306a36Sopenharmony_ci *
87862306a36Sopenharmony_ci * Return: size of the operational state
87962306a36Sopenharmony_ci */
88062306a36Sopenharmony_cistatic inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
88162306a36Sopenharmony_ci{
88262306a36Sopenharmony_ci	return tfm->descsize;
88362306a36Sopenharmony_ci}
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_cistatic inline void *shash_desc_ctx(struct shash_desc *desc)
88662306a36Sopenharmony_ci{
88762306a36Sopenharmony_ci	return desc->__ctx;
88862306a36Sopenharmony_ci}
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci/**
89162306a36Sopenharmony_ci * crypto_shash_setkey() - set key for message digest
89262306a36Sopenharmony_ci * @tfm: cipher handle
89362306a36Sopenharmony_ci * @key: buffer holding the key
89462306a36Sopenharmony_ci * @keylen: length of the key in bytes
89562306a36Sopenharmony_ci *
89662306a36Sopenharmony_ci * The caller provided key is set for the keyed message digest cipher. The
89762306a36Sopenharmony_ci * cipher handle must point to a keyed message digest cipher in order for this
89862306a36Sopenharmony_ci * function to succeed.
89962306a36Sopenharmony_ci *
90062306a36Sopenharmony_ci * Context: Any context.
90162306a36Sopenharmony_ci * Return: 0 if the setting of the key was successful; < 0 if an error occurred
90262306a36Sopenharmony_ci */
90362306a36Sopenharmony_ciint crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
90462306a36Sopenharmony_ci			unsigned int keylen);
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci/**
90762306a36Sopenharmony_ci * crypto_shash_digest() - calculate message digest for buffer
90862306a36Sopenharmony_ci * @desc: see crypto_shash_final()
90962306a36Sopenharmony_ci * @data: see crypto_shash_update()
91062306a36Sopenharmony_ci * @len: see crypto_shash_update()
91162306a36Sopenharmony_ci * @out: see crypto_shash_final()
91262306a36Sopenharmony_ci *
91362306a36Sopenharmony_ci * This function is a "short-hand" for the function calls of crypto_shash_init,
91462306a36Sopenharmony_ci * crypto_shash_update and crypto_shash_final. The parameters have the same
91562306a36Sopenharmony_ci * meaning as discussed for those separate three functions.
91662306a36Sopenharmony_ci *
91762306a36Sopenharmony_ci * Context: Any context.
91862306a36Sopenharmony_ci * Return: 0 if the message digest creation was successful; < 0 if an error
91962306a36Sopenharmony_ci *	   occurred
92062306a36Sopenharmony_ci */
92162306a36Sopenharmony_ciint crypto_shash_digest(struct shash_desc *desc, const u8 *data,
92262306a36Sopenharmony_ci			unsigned int len, u8 *out);
92362306a36Sopenharmony_ci
92462306a36Sopenharmony_ci/**
92562306a36Sopenharmony_ci * crypto_shash_tfm_digest() - calculate message digest for buffer
92662306a36Sopenharmony_ci * @tfm: hash transformation object
92762306a36Sopenharmony_ci * @data: see crypto_shash_update()
92862306a36Sopenharmony_ci * @len: see crypto_shash_update()
92962306a36Sopenharmony_ci * @out: see crypto_shash_final()
93062306a36Sopenharmony_ci *
93162306a36Sopenharmony_ci * This is a simplified version of crypto_shash_digest() for users who don't
93262306a36Sopenharmony_ci * want to allocate their own hash descriptor (shash_desc).  Instead,
93362306a36Sopenharmony_ci * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)
93462306a36Sopenharmony_ci * directly, and it allocates a hash descriptor on the stack internally.
93562306a36Sopenharmony_ci * Note that this stack allocation may be fairly large.
93662306a36Sopenharmony_ci *
93762306a36Sopenharmony_ci * Context: Any context.
93862306a36Sopenharmony_ci * Return: 0 on success; < 0 if an error occurred.
93962306a36Sopenharmony_ci */
94062306a36Sopenharmony_ciint crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
94162306a36Sopenharmony_ci			    unsigned int len, u8 *out);
94262306a36Sopenharmony_ci
94362306a36Sopenharmony_ci/**
94462306a36Sopenharmony_ci * crypto_shash_export() - extract operational state for message digest
94562306a36Sopenharmony_ci * @desc: reference to the operational state handle whose state is exported
94662306a36Sopenharmony_ci * @out: output buffer of sufficient size that can hold the hash state
94762306a36Sopenharmony_ci *
94862306a36Sopenharmony_ci * This function exports the hash state of the operational state handle into the
94962306a36Sopenharmony_ci * caller-allocated output buffer out which must have sufficient size (e.g. by
95062306a36Sopenharmony_ci * calling crypto_shash_descsize).
95162306a36Sopenharmony_ci *
95262306a36Sopenharmony_ci * Context: Any context.
95362306a36Sopenharmony_ci * Return: 0 if the export creation was successful; < 0 if an error occurred
95462306a36Sopenharmony_ci */
95562306a36Sopenharmony_cistatic inline int crypto_shash_export(struct shash_desc *desc, void *out)
95662306a36Sopenharmony_ci{
95762306a36Sopenharmony_ci	return crypto_shash_alg(desc->tfm)->export(desc, out);
95862306a36Sopenharmony_ci}
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_ci/**
96162306a36Sopenharmony_ci * crypto_shash_import() - import operational state
96262306a36Sopenharmony_ci * @desc: reference to the operational state handle the state imported into
96362306a36Sopenharmony_ci * @in: buffer holding the state
96462306a36Sopenharmony_ci *
96562306a36Sopenharmony_ci * This function imports the hash state into the operational state handle from
96662306a36Sopenharmony_ci * the input buffer. That buffer should have been generated with the
96762306a36Sopenharmony_ci * crypto_ahash_export function.
96862306a36Sopenharmony_ci *
96962306a36Sopenharmony_ci * Context: Any context.
97062306a36Sopenharmony_ci * Return: 0 if the import was successful; < 0 if an error occurred
97162306a36Sopenharmony_ci */
97262306a36Sopenharmony_cistatic inline int crypto_shash_import(struct shash_desc *desc, const void *in)
97362306a36Sopenharmony_ci{
97462306a36Sopenharmony_ci	struct crypto_shash *tfm = desc->tfm;
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci	if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
97762306a36Sopenharmony_ci		return -ENOKEY;
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci	return crypto_shash_alg(tfm)->import(desc, in);
98062306a36Sopenharmony_ci}
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci/**
98362306a36Sopenharmony_ci * crypto_shash_init() - (re)initialize message digest
98462306a36Sopenharmony_ci * @desc: operational state handle that is already filled
98562306a36Sopenharmony_ci *
98662306a36Sopenharmony_ci * The call (re-)initializes the message digest referenced by the
98762306a36Sopenharmony_ci * operational state handle. Any potentially existing state created by
98862306a36Sopenharmony_ci * previous operations is discarded.
98962306a36Sopenharmony_ci *
99062306a36Sopenharmony_ci * Context: Any context.
99162306a36Sopenharmony_ci * Return: 0 if the message digest initialization was successful; < 0 if an
99262306a36Sopenharmony_ci *	   error occurred
99362306a36Sopenharmony_ci */
99462306a36Sopenharmony_cistatic inline int crypto_shash_init(struct shash_desc *desc)
99562306a36Sopenharmony_ci{
99662306a36Sopenharmony_ci	struct crypto_shash *tfm = desc->tfm;
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci	if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
99962306a36Sopenharmony_ci		return -ENOKEY;
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci	return crypto_shash_alg(tfm)->init(desc);
100262306a36Sopenharmony_ci}
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci/**
100562306a36Sopenharmony_ci * crypto_shash_update() - add data to message digest for processing
100662306a36Sopenharmony_ci * @desc: operational state handle that is already initialized
100762306a36Sopenharmony_ci * @data: input data to be added to the message digest
100862306a36Sopenharmony_ci * @len: length of the input data
100962306a36Sopenharmony_ci *
101062306a36Sopenharmony_ci * Updates the message digest state of the operational state handle.
101162306a36Sopenharmony_ci *
101262306a36Sopenharmony_ci * Context: Any context.
101362306a36Sopenharmony_ci * Return: 0 if the message digest update was successful; < 0 if an error
101462306a36Sopenharmony_ci *	   occurred
101562306a36Sopenharmony_ci */
101662306a36Sopenharmony_ciint crypto_shash_update(struct shash_desc *desc, const u8 *data,
101762306a36Sopenharmony_ci			unsigned int len);
101862306a36Sopenharmony_ci
101962306a36Sopenharmony_ci/**
102062306a36Sopenharmony_ci * crypto_shash_final() - calculate message digest
102162306a36Sopenharmony_ci * @desc: operational state handle that is already filled with data
102262306a36Sopenharmony_ci * @out: output buffer filled with the message digest
102362306a36Sopenharmony_ci *
102462306a36Sopenharmony_ci * Finalize the message digest operation and create the message digest
102562306a36Sopenharmony_ci * based on all data added to the cipher handle. The message digest is placed
102662306a36Sopenharmony_ci * into the output buffer. The caller must ensure that the output buffer is
102762306a36Sopenharmony_ci * large enough by using crypto_shash_digestsize.
102862306a36Sopenharmony_ci *
102962306a36Sopenharmony_ci * Context: Any context.
103062306a36Sopenharmony_ci * Return: 0 if the message digest creation was successful; < 0 if an error
103162306a36Sopenharmony_ci *	   occurred
103262306a36Sopenharmony_ci */
103362306a36Sopenharmony_ciint crypto_shash_final(struct shash_desc *desc, u8 *out);
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci/**
103662306a36Sopenharmony_ci * crypto_shash_finup() - calculate message digest of buffer
103762306a36Sopenharmony_ci * @desc: see crypto_shash_final()
103862306a36Sopenharmony_ci * @data: see crypto_shash_update()
103962306a36Sopenharmony_ci * @len: see crypto_shash_update()
104062306a36Sopenharmony_ci * @out: see crypto_shash_final()
104162306a36Sopenharmony_ci *
104262306a36Sopenharmony_ci * This function is a "short-hand" for the function calls of
104362306a36Sopenharmony_ci * crypto_shash_update and crypto_shash_final. The parameters have the same
104462306a36Sopenharmony_ci * meaning as discussed for those separate functions.
104562306a36Sopenharmony_ci *
104662306a36Sopenharmony_ci * Context: Any context.
104762306a36Sopenharmony_ci * Return: 0 if the message digest creation was successful; < 0 if an error
104862306a36Sopenharmony_ci *	   occurred
104962306a36Sopenharmony_ci */
105062306a36Sopenharmony_ciint crypto_shash_finup(struct shash_desc *desc, const u8 *data,
105162306a36Sopenharmony_ci		       unsigned int len, u8 *out);
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_cistatic inline void shash_desc_zero(struct shash_desc *desc)
105462306a36Sopenharmony_ci{
105562306a36Sopenharmony_ci	memzero_explicit(desc,
105662306a36Sopenharmony_ci			 sizeof(*desc) + crypto_shash_descsize(desc->tfm));
105762306a36Sopenharmony_ci}
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci#endif	/* _CRYPTO_HASH_H */
1060