12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
218e33e6dSHerbert Xu /*
318e33e6dSHerbert Xu * Hash: Hash algorithms under the crypto API
418e33e6dSHerbert Xu *
518e33e6dSHerbert Xu * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
618e33e6dSHerbert Xu */
718e33e6dSHerbert Xu
818e33e6dSHerbert Xu #ifndef _CRYPTO_HASH_H
918e33e6dSHerbert Xu #define _CRYPTO_HASH_H
1018e33e6dSHerbert Xu
1142808e5dSHerbert Xu #include <linux/atomic.h>
1218e33e6dSHerbert Xu #include <linux/crypto.h>
13e67ffe0aSHerbert Xu #include <linux/string.h>
1418e33e6dSHerbert Xu
1588056ec3SHerbert Xu struct crypto_ahash;
1688056ec3SHerbert Xu
175d8c723fSStephan Mueller /**
185d8c723fSStephan Mueller * DOC: Message Digest Algorithm Definitions
195d8c723fSStephan Mueller *
205d8c723fSStephan Mueller * These data structures define modular message digest algorithm
215d8c723fSStephan Mueller * implementations, managed via crypto_register_ahash(),
225d8c723fSStephan Mueller * crypto_register_shash(), crypto_unregister_ahash() and
235d8c723fSStephan Mueller * crypto_unregister_shash().
245d8c723fSStephan Mueller */
255d8c723fSStephan Mueller
2642808e5dSHerbert Xu /*
2742808e5dSHerbert Xu * struct crypto_istat_hash - statistics for has algorithm
2842808e5dSHerbert Xu * @hash_cnt: number of hash requests
2942808e5dSHerbert Xu * @hash_tlen: total data size hashed
3042808e5dSHerbert Xu * @err_cnt: number of error for hash requests
3142808e5dSHerbert Xu */
3242808e5dSHerbert Xu struct crypto_istat_hash {
3342808e5dSHerbert Xu atomic64_t hash_cnt;
3442808e5dSHerbert Xu atomic64_t hash_tlen;
3542808e5dSHerbert Xu atomic64_t err_cnt;
3642808e5dSHerbert Xu };
3742808e5dSHerbert Xu
3842808e5dSHerbert Xu #ifdef CONFIG_CRYPTO_STATS
3942808e5dSHerbert Xu #define HASH_ALG_COMMON_STAT struct crypto_istat_hash stat;
4042808e5dSHerbert Xu #else
4142808e5dSHerbert Xu #define HASH_ALG_COMMON_STAT
4242808e5dSHerbert Xu #endif
4342808e5dSHerbert Xu
44083a7e87SHerbert Xu /*
455d8c723fSStephan Mueller * struct hash_alg_common - define properties of message digest
4642808e5dSHerbert Xu * @stat: Statistics for hash algorithm.
475d8c723fSStephan Mueller * @digestsize: Size of the result of the transformation. A buffer of this size
485d8c723fSStephan Mueller * must be available to the @final and @finup calls, so they can
495d8c723fSStephan Mueller * store the resulting hash into it. For various predefined sizes,
505d8c723fSStephan Mueller * search include/crypto/ using
515d8c723fSStephan Mueller * git grep _DIGEST_SIZE include/crypto.
525d8c723fSStephan Mueller * @statesize: Size of the block for partial state of the transformation. A
535d8c723fSStephan Mueller * buffer of this size must be passed to the @export function as it
545d8c723fSStephan Mueller * will save the partial state of the transformation into it. On the
555d8c723fSStephan Mueller * other side, the @import function will load the state from a
565d8c723fSStephan Mueller * buffer of this size as well.
5752744af3SStephan Mueller * @base: Start of data structure of cipher algorithm. The common data
5852744af3SStephan Mueller * structure of crypto_alg contains information common to all ciphers.
5952744af3SStephan Mueller * The hash_alg_common data structure now adds the hash-specific
6052744af3SStephan Mueller * information.
615d8c723fSStephan Mueller */
6242808e5dSHerbert Xu #define HASH_ALG_COMMON { \
6342808e5dSHerbert Xu HASH_ALG_COMMON_STAT \
6442808e5dSHerbert Xu \
6542808e5dSHerbert Xu unsigned int digestsize; \
6642808e5dSHerbert Xu unsigned int statesize; \
6742808e5dSHerbert Xu \
6842808e5dSHerbert Xu struct crypto_alg base; \
6942808e5dSHerbert Xu }
7042808e5dSHerbert Xu struct hash_alg_common HASH_ALG_COMMON;
7188056ec3SHerbert Xu
7288056ec3SHerbert Xu struct ahash_request {
7388056ec3SHerbert Xu struct crypto_async_request base;
7488056ec3SHerbert Xu
7588056ec3SHerbert Xu unsigned int nbytes;
7688056ec3SHerbert Xu struct scatterlist *src;
7788056ec3SHerbert Xu u8 *result;
7888056ec3SHerbert Xu
7966f6ce5eSHerbert Xu /* This field may only be used by the ahash API code. */
8066f6ce5eSHerbert Xu void *priv;
8166f6ce5eSHerbert Xu
8288056ec3SHerbert Xu void *__ctx[] CRYPTO_MINALIGN_ATTR;
8388056ec3SHerbert Xu };
8488056ec3SHerbert Xu
855d8c723fSStephan Mueller /**
865d8c723fSStephan Mueller * struct ahash_alg - asynchronous message digest definition
87b40fa82cStcharding * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the
8812f7c14aSMasanari Iida * state of the HASH transformation at the beginning. This shall fill in
895d8c723fSStephan Mueller * the internal structures used during the entire duration of the whole
903d053d53SKamil Konieczny * transformation. No data processing happens at this point. Driver code
913d053d53SKamil Konieczny * implementation must not use req->result.
92b40fa82cStcharding * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This
935d8c723fSStephan Mueller * function actually pushes blocks of data from upper layers into the
945d8c723fSStephan Mueller * driver, which then passes those to the hardware as seen fit. This
955d8c723fSStephan Mueller * function must not finalize the HASH transformation by calculating the
965d8c723fSStephan Mueller * final message digest as this only adds more data into the
975d8c723fSStephan Mueller * transformation. This function shall not modify the transformation
985d8c723fSStephan Mueller * context, as this function may be called in parallel with the same
995d8c723fSStephan Mueller * transformation object. Data processing can happen synchronously
1003d053d53SKamil Konieczny * [SHASH] or asynchronously [AHASH] at this point. Driver must not use
1013d053d53SKamil Konieczny * req->result.
102b40fa82cStcharding * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the
1035d8c723fSStephan Mueller * transformation and retrieves the resulting hash from the driver and
1045d8c723fSStephan Mueller * pushes it back to upper layers. No data processing happens at this
105560b1a82SKamil Konieczny * point unless hardware requires it to finish the transformation
106560b1a82SKamil Konieczny * (then the data buffered by the device driver is processed).
107b40fa82cStcharding * @finup: **[optional]** Combination of @update and @final. This function is effectively a
1085d8c723fSStephan Mueller * combination of @update and @final calls issued in sequence. As some
1095d8c723fSStephan Mueller * hardware cannot do @update and @final separately, this callback was
1105d8c723fSStephan Mueller * added to allow such hardware to be used at least by IPsec. Data
1115d8c723fSStephan Mueller * processing can happen synchronously [SHASH] or asynchronously [AHASH]
1125d8c723fSStephan Mueller * at this point.
1135d8c723fSStephan Mueller * @digest: Combination of @init and @update and @final. This function
1145d8c723fSStephan Mueller * effectively behaves as the entire chain of operations, @init,
1155d8c723fSStephan Mueller * @update and @final issued in sequence. Just like @finup, this was
1165d8c723fSStephan Mueller * added for hardware which cannot do even the @finup, but can only do
1175d8c723fSStephan Mueller * the whole transformation in one run. Data processing can happen
1185d8c723fSStephan Mueller * synchronously [SHASH] or asynchronously [AHASH] at this point.
1195d8c723fSStephan Mueller * @setkey: Set optional key used by the hashing algorithm. Intended to push
1205d8c723fSStephan Mueller * optional key used by the hashing algorithm from upper layers into
1215d8c723fSStephan Mueller * the driver. This function can store the key in the transformation
1225d8c723fSStephan Mueller * context or can outright program it into the hardware. In the former
1235d8c723fSStephan Mueller * case, one must be careful to program the key into the hardware at
1245d8c723fSStephan Mueller * appropriate time and one must be careful that .setkey() can be
1255d8c723fSStephan Mueller * called multiple times during the existence of the transformation
1265d8c723fSStephan Mueller * object. Not all hashing algorithms do implement this function as it
1275d8c723fSStephan Mueller * is only needed for keyed message digests. SHAx/MDx/CRCx do NOT
1285d8c723fSStephan Mueller * implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement
1295d8c723fSStephan Mueller * this function. This function must be called before any other of the
1305d8c723fSStephan Mueller * @init, @update, @final, @finup, @digest is called. No data
1315d8c723fSStephan Mueller * processing happens at this point.
1325d8c723fSStephan Mueller * @export: Export partial state of the transformation. This function dumps the
1335d8c723fSStephan Mueller * entire state of the ongoing transformation into a provided block of
1345d8c723fSStephan Mueller * data so it can be @import 'ed back later on. This is useful in case
1355d8c723fSStephan Mueller * you want to save partial result of the transformation after
1365d8c723fSStephan Mueller * processing certain amount of data and reload this partial result
1375d8c723fSStephan Mueller * multiple times later on for multiple re-use. No data processing
1383d053d53SKamil Konieczny * happens at this point. Driver must not use req->result.
1395d8c723fSStephan Mueller * @import: Import partial state of the transformation. This function loads the
1405d8c723fSStephan Mueller * entire state of the ongoing transformation from a provided block of
1415d8c723fSStephan Mueller * data so the transformation can continue from this point onward. No
1423d053d53SKamil Konieczny * data processing happens at this point. Driver must not use
1433d053d53SKamil Konieczny * req->result.
144e73d340dSHerbert Xu * @init_tfm: Initialize the cryptographic transformation object.
145e73d340dSHerbert Xu * This function is called only once at the instantiation
146e73d340dSHerbert Xu * time, right after the transformation context was
147e73d340dSHerbert Xu * allocated. In case the cryptographic hardware has
148e73d340dSHerbert Xu * some special requirements which need to be handled
149e73d340dSHerbert Xu * by software, this function shall check for the precise
150e73d340dSHerbert Xu * requirement of the transformation and put any software
151e73d340dSHerbert Xu * fallbacks in place.
152e73d340dSHerbert Xu * @exit_tfm: Deinitialize the cryptographic transformation object.
153e73d340dSHerbert Xu * This is a counterpart to @init_tfm, used to remove
154e73d340dSHerbert Xu * various changes set in @init_tfm.
155ed3630b8SHerbert Xu * @clone_tfm: Copy transform into new object, may allocate memory.
15652744af3SStephan Mueller * @halg: see struct hash_alg_common
1575d8c723fSStephan Mueller */
15888056ec3SHerbert Xu struct ahash_alg {
15988056ec3SHerbert Xu int (*init)(struct ahash_request *req);
16088056ec3SHerbert Xu int (*update)(struct ahash_request *req);
16188056ec3SHerbert Xu int (*final)(struct ahash_request *req);
16288056ec3SHerbert Xu int (*finup)(struct ahash_request *req);
16388056ec3SHerbert Xu int (*digest)(struct ahash_request *req);
16488056ec3SHerbert Xu int (*export)(struct ahash_request *req, void *out);
16588056ec3SHerbert Xu int (*import)(struct ahash_request *req, const void *in);
16688056ec3SHerbert Xu int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
16788056ec3SHerbert Xu unsigned int keylen);
168e73d340dSHerbert Xu int (*init_tfm)(struct crypto_ahash *tfm);
169e73d340dSHerbert Xu void (*exit_tfm)(struct crypto_ahash *tfm);
170ed3630b8SHerbert Xu int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src);
17188056ec3SHerbert Xu
17288056ec3SHerbert Xu struct hash_alg_common halg;
17388056ec3SHerbert Xu };
17488056ec3SHerbert Xu
1757b5a080bSHerbert Xu struct shash_desc {
1767b5a080bSHerbert Xu struct crypto_shash *tfm;
177660d2062SArd Biesheuvel void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
1787b5a080bSHerbert Xu };
1797b5a080bSHerbert Xu
180b68a7ec1SKees Cook #define HASH_MAX_DIGESTSIZE 64
181e1354400SEric Biggers
182e1354400SEric Biggers /*
183e1354400SEric Biggers * Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc'
184e1354400SEric Biggers * containing a 'struct sha3_state'.
185e1354400SEric Biggers */
186e1354400SEric Biggers #define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360)
187e1354400SEric Biggers
188a0a77af1SBehan Webster #define SHASH_DESC_ON_STACK(shash, ctx) \
189660d2062SArd Biesheuvel char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
190660d2062SArd Biesheuvel __aligned(__alignof__(struct shash_desc)); \
191a0a77af1SBehan Webster struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
192a0a77af1SBehan Webster
1935d8c723fSStephan Mueller /**
1945d8c723fSStephan Mueller * struct shash_alg - synchronous message digest definition
1955d8c723fSStephan Mueller * @init: see struct ahash_alg
1965d8c723fSStephan Mueller * @update: see struct ahash_alg
1975d8c723fSStephan Mueller * @final: see struct ahash_alg
1985d8c723fSStephan Mueller * @finup: see struct ahash_alg
1995d8c723fSStephan Mueller * @digest: see struct ahash_alg
2005d8c723fSStephan Mueller * @export: see struct ahash_alg
2015d8c723fSStephan Mueller * @import: see struct ahash_alg
2025d8c723fSStephan Mueller * @setkey: see struct ahash_alg
203fbce6be5SHerbert Xu * @init_tfm: Initialize the cryptographic transformation object.
204fbce6be5SHerbert Xu * This function is called only once at the instantiation
205fbce6be5SHerbert Xu * time, right after the transformation context was
206fbce6be5SHerbert Xu * allocated. In case the cryptographic hardware has
207fbce6be5SHerbert Xu * some special requirements which need to be handled
208fbce6be5SHerbert Xu * by software, this function shall check for the precise
209fbce6be5SHerbert Xu * requirement of the transformation and put any software
210fbce6be5SHerbert Xu * fallbacks in place.
211fbce6be5SHerbert Xu * @exit_tfm: Deinitialize the cryptographic transformation object.
212fbce6be5SHerbert Xu * This is a counterpart to @init_tfm, used to remove
213fbce6be5SHerbert Xu * various changes set in @init_tfm.
214ed3630b8SHerbert Xu * @clone_tfm: Copy transform into new object, may allocate memory.
2155d8c723fSStephan Mueller * @digestsize: see struct ahash_alg
2165d8c723fSStephan Mueller * @statesize: see struct ahash_alg
21752744af3SStephan Mueller * @descsize: Size of the operational state for the message digest. This state
2185d8c723fSStephan Mueller * size is the memory size that needs to be allocated for
2195d8c723fSStephan Mueller * shash_desc.__ctx
22042808e5dSHerbert Xu * @stat: Statistics for hash algorithm.
2215d8c723fSStephan Mueller * @base: internally used
22242808e5dSHerbert Xu * @halg: see struct hash_alg_common
223083a7e87SHerbert Xu * @HASH_ALG_COMMON: see struct hash_alg_common
2245d8c723fSStephan Mueller */
2257b5a080bSHerbert Xu struct shash_alg {
2267b5a080bSHerbert Xu int (*init)(struct shash_desc *desc);
2277b5a080bSHerbert Xu int (*update)(struct shash_desc *desc, const u8 *data,
2287b5a080bSHerbert Xu unsigned int len);
2297b5a080bSHerbert Xu int (*final)(struct shash_desc *desc, u8 *out);
2307b5a080bSHerbert Xu int (*finup)(struct shash_desc *desc, const u8 *data,
2317b5a080bSHerbert Xu unsigned int len, u8 *out);
2327b5a080bSHerbert Xu int (*digest)(struct shash_desc *desc, const u8 *data,
2337b5a080bSHerbert Xu unsigned int len, u8 *out);
23499d27e1cSHerbert Xu int (*export)(struct shash_desc *desc, void *out);
23599d27e1cSHerbert Xu int (*import)(struct shash_desc *desc, const void *in);
2367b5a080bSHerbert Xu int (*setkey)(struct crypto_shash *tfm, const u8 *key,
2377b5a080bSHerbert Xu unsigned int keylen);
238fbce6be5SHerbert Xu int (*init_tfm)(struct crypto_shash *tfm);
239fbce6be5SHerbert Xu void (*exit_tfm)(struct crypto_shash *tfm);
240ed3630b8SHerbert Xu int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);
2417b5a080bSHerbert Xu
2427b5a080bSHerbert Xu unsigned int descsize;
24388056ec3SHerbert Xu
24442808e5dSHerbert Xu union {
24542808e5dSHerbert Xu struct HASH_ALG_COMMON;
24642808e5dSHerbert Xu struct hash_alg_common halg;
2477b5a080bSHerbert Xu };
24842808e5dSHerbert Xu };
24942808e5dSHerbert Xu #undef HASH_ALG_COMMON
25042808e5dSHerbert Xu #undef HASH_ALG_COMMON_STAT
2517b5a080bSHerbert Xu
25218e33e6dSHerbert Xu struct crypto_ahash {
25388056ec3SHerbert Xu int (*init)(struct ahash_request *req);
25488056ec3SHerbert Xu int (*update)(struct ahash_request *req);
25588056ec3SHerbert Xu int (*final)(struct ahash_request *req);
25688056ec3SHerbert Xu int (*finup)(struct ahash_request *req);
25788056ec3SHerbert Xu int (*digest)(struct ahash_request *req);
25888056ec3SHerbert Xu int (*export)(struct ahash_request *req, void *out);
25988056ec3SHerbert Xu int (*import)(struct ahash_request *req, const void *in);
26088056ec3SHerbert Xu int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
26188056ec3SHerbert Xu unsigned int keylen);
26288056ec3SHerbert Xu
263*c7535fb2SHerbert Xu unsigned int statesize;
26488056ec3SHerbert Xu unsigned int reqsize;
26518e33e6dSHerbert Xu struct crypto_tfm base;
26618e33e6dSHerbert Xu };
26718e33e6dSHerbert Xu
2687b5a080bSHerbert Xu struct crypto_shash {
269113adefcSHerbert Xu unsigned int descsize;
2707b5a080bSHerbert Xu struct crypto_tfm base;
2717b5a080bSHerbert Xu };
2727b5a080bSHerbert Xu
27390240ffbSStephan Mueller /**
27490240ffbSStephan Mueller * DOC: Asynchronous Message Digest API
27590240ffbSStephan Mueller *
27690240ffbSStephan Mueller * The asynchronous message digest API is used with the ciphers of type
27790240ffbSStephan Mueller * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
27890240ffbSStephan Mueller *
27990240ffbSStephan Mueller * The asynchronous cipher operation discussion provided for the
280d63007ebSArd Biesheuvel * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
28190240ffbSStephan Mueller */
28290240ffbSStephan Mueller
__crypto_ahash_cast(struct crypto_tfm * tfm)28318e33e6dSHerbert Xu static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
28418e33e6dSHerbert Xu {
28588056ec3SHerbert Xu return container_of(tfm, struct crypto_ahash, base);
28618e33e6dSHerbert Xu }
28718e33e6dSHerbert Xu
28890240ffbSStephan Mueller /**
28990240ffbSStephan Mueller * crypto_alloc_ahash() - allocate ahash cipher handle
29090240ffbSStephan Mueller * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
29190240ffbSStephan Mueller * ahash cipher
29290240ffbSStephan Mueller * @type: specifies the type of the cipher
29390240ffbSStephan Mueller * @mask: specifies the mask for the cipher
29490240ffbSStephan Mueller *
29590240ffbSStephan Mueller * Allocate a cipher handle for an ahash. The returned struct
29690240ffbSStephan Mueller * crypto_ahash is the cipher handle that is required for any subsequent
29790240ffbSStephan Mueller * API invocation for that ahash.
29890240ffbSStephan Mueller *
29990240ffbSStephan Mueller * Return: allocated cipher handle in case of success; IS_ERR() is true in case
30090240ffbSStephan Mueller * of an error, PTR_ERR() returns the error code.
30190240ffbSStephan Mueller */
30288056ec3SHerbert Xu struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
30388056ec3SHerbert Xu u32 mask);
30418e33e6dSHerbert Xu
305ed3630b8SHerbert Xu struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);
306ed3630b8SHerbert Xu
crypto_ahash_tfm(struct crypto_ahash * tfm)30718e33e6dSHerbert Xu static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
30818e33e6dSHerbert Xu {
30918e33e6dSHerbert Xu return &tfm->base;
31018e33e6dSHerbert Xu }
31118e33e6dSHerbert Xu
31290240ffbSStephan Mueller /**
31390240ffbSStephan Mueller * crypto_free_ahash() - zeroize and free the ahash handle
31490240ffbSStephan Mueller * @tfm: cipher handle to be freed
31583681f2bSArd Biesheuvel *
31683681f2bSArd Biesheuvel * If @tfm is a NULL or error pointer, this function does nothing.
31790240ffbSStephan Mueller */
crypto_free_ahash(struct crypto_ahash * tfm)31818e33e6dSHerbert Xu static inline void crypto_free_ahash(struct crypto_ahash *tfm)
31918e33e6dSHerbert Xu {
32088056ec3SHerbert Xu crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
32118e33e6dSHerbert Xu }
32218e33e6dSHerbert Xu
3238d18e34cSHerbert Xu /**
3248d18e34cSHerbert Xu * crypto_has_ahash() - Search for the availability of an ahash.
3258d18e34cSHerbert Xu * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
3268d18e34cSHerbert Xu * ahash
3278d18e34cSHerbert Xu * @type: specifies the type of the ahash
3288d18e34cSHerbert Xu * @mask: specifies the mask for the ahash
3298d18e34cSHerbert Xu *
3308d18e34cSHerbert Xu * Return: true when the ahash is known to the kernel crypto API; false
3318d18e34cSHerbert Xu * otherwise
3328d18e34cSHerbert Xu */
3338d18e34cSHerbert Xu int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
3348d18e34cSHerbert Xu
crypto_ahash_alg_name(struct crypto_ahash * tfm)335d12481bcSHerbert Xu static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
336d12481bcSHerbert Xu {
337d12481bcSHerbert Xu return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
338d12481bcSHerbert Xu }
339d12481bcSHerbert Xu
crypto_ahash_driver_name(struct crypto_ahash * tfm)340d12481bcSHerbert Xu static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
341d12481bcSHerbert Xu {
342d12481bcSHerbert Xu return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
343d12481bcSHerbert Xu }
344d12481bcSHerbert Xu
crypto_ahash_alignmask(struct crypto_ahash * tfm)34518e33e6dSHerbert Xu static inline unsigned int crypto_ahash_alignmask(
34618e33e6dSHerbert Xu struct crypto_ahash *tfm)
34718e33e6dSHerbert Xu {
34818e33e6dSHerbert Xu return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
34918e33e6dSHerbert Xu }
35018e33e6dSHerbert Xu
351524e56c3SHerbert Xu /**
352524e56c3SHerbert Xu * crypto_ahash_blocksize() - obtain block size for cipher
353524e56c3SHerbert Xu * @tfm: cipher handle
354524e56c3SHerbert Xu *
355524e56c3SHerbert Xu * The block size for the message digest cipher referenced with the cipher
356524e56c3SHerbert Xu * handle is returned.
357524e56c3SHerbert Xu *
358524e56c3SHerbert Xu * Return: block size of cipher
359524e56c3SHerbert Xu */
crypto_ahash_blocksize(struct crypto_ahash * tfm)360524e56c3SHerbert Xu static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
361524e56c3SHerbert Xu {
362524e56c3SHerbert Xu return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
363524e56c3SHerbert Xu }
364524e56c3SHerbert Xu
__crypto_hash_alg_common(struct crypto_alg * alg)36588056ec3SHerbert Xu static inline struct hash_alg_common *__crypto_hash_alg_common(
36688056ec3SHerbert Xu struct crypto_alg *alg)
36718e33e6dSHerbert Xu {
36888056ec3SHerbert Xu return container_of(alg, struct hash_alg_common, base);
36988056ec3SHerbert Xu }
37088056ec3SHerbert Xu
crypto_hash_alg_common(struct crypto_ahash * tfm)37188056ec3SHerbert Xu static inline struct hash_alg_common *crypto_hash_alg_common(
37288056ec3SHerbert Xu struct crypto_ahash *tfm)
37388056ec3SHerbert Xu {
37488056ec3SHerbert Xu return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
37518e33e6dSHerbert Xu }
37618e33e6dSHerbert Xu
37790240ffbSStephan Mueller /**
37890240ffbSStephan Mueller * crypto_ahash_digestsize() - obtain message digest size
37990240ffbSStephan Mueller * @tfm: cipher handle
38090240ffbSStephan Mueller *
38190240ffbSStephan Mueller * The size for the message digest created by the message digest cipher
38290240ffbSStephan Mueller * referenced with the cipher handle is returned.
38390240ffbSStephan Mueller *
38490240ffbSStephan Mueller *
38590240ffbSStephan Mueller * Return: message digest size of cipher
38690240ffbSStephan Mueller */
crypto_ahash_digestsize(struct crypto_ahash * tfm)38718e33e6dSHerbert Xu static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
38818e33e6dSHerbert Xu {
389500b3e3cSHerbert Xu return crypto_hash_alg_common(tfm)->digestsize;
39088056ec3SHerbert Xu }
39188056ec3SHerbert Xu
392379d972bSRabin Vincent /**
393379d972bSRabin Vincent * crypto_ahash_statesize() - obtain size of the ahash state
394379d972bSRabin Vincent * @tfm: cipher handle
395379d972bSRabin Vincent *
396379d972bSRabin Vincent * Return the size of the ahash state. With the crypto_ahash_export()
397379d972bSRabin Vincent * function, the caller can export the state into a buffer whose size is
398379d972bSRabin Vincent * defined with this function.
399379d972bSRabin Vincent *
400379d972bSRabin Vincent * Return: size of the ahash state
401379d972bSRabin Vincent */
crypto_ahash_statesize(struct crypto_ahash * tfm)40288056ec3SHerbert Xu static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
40388056ec3SHerbert Xu {
404*c7535fb2SHerbert Xu return tfm->statesize;
40518e33e6dSHerbert Xu }
40618e33e6dSHerbert Xu
crypto_ahash_get_flags(struct crypto_ahash * tfm)40718e33e6dSHerbert Xu static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
40818e33e6dSHerbert Xu {
40918e33e6dSHerbert Xu return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
41018e33e6dSHerbert Xu }
41118e33e6dSHerbert Xu
crypto_ahash_set_flags(struct crypto_ahash * tfm,u32 flags)41218e33e6dSHerbert Xu static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
41318e33e6dSHerbert Xu {
41418e33e6dSHerbert Xu crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
41518e33e6dSHerbert Xu }
41618e33e6dSHerbert Xu
crypto_ahash_clear_flags(struct crypto_ahash * tfm,u32 flags)41718e33e6dSHerbert Xu static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
41818e33e6dSHerbert Xu {
41918e33e6dSHerbert Xu crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
42018e33e6dSHerbert Xu }
42118e33e6dSHerbert Xu
42290240ffbSStephan Mueller /**
42390240ffbSStephan Mueller * crypto_ahash_reqtfm() - obtain cipher handle from request
42490240ffbSStephan Mueller * @req: asynchronous request handle that contains the reference to the ahash
42590240ffbSStephan Mueller * cipher handle
42690240ffbSStephan Mueller *
42790240ffbSStephan Mueller * Return the ahash cipher handle that is registered with the asynchronous
42890240ffbSStephan Mueller * request handle ahash_request.
42990240ffbSStephan Mueller *
43090240ffbSStephan Mueller * Return: ahash cipher handle
43190240ffbSStephan Mueller */
crypto_ahash_reqtfm(struct ahash_request * req)43218e33e6dSHerbert Xu static inline struct crypto_ahash *crypto_ahash_reqtfm(
43318e33e6dSHerbert Xu struct ahash_request *req)
43418e33e6dSHerbert Xu {
43518e33e6dSHerbert Xu return __crypto_ahash_cast(req->base.tfm);
43618e33e6dSHerbert Xu }
43718e33e6dSHerbert Xu
43890240ffbSStephan Mueller /**
43990240ffbSStephan Mueller * crypto_ahash_reqsize() - obtain size of the request data structure
44090240ffbSStephan Mueller * @tfm: cipher handle
44190240ffbSStephan Mueller *
442379d972bSRabin Vincent * Return: size of the request data
44390240ffbSStephan Mueller */
crypto_ahash_reqsize(struct crypto_ahash * tfm)44418e33e6dSHerbert Xu static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
44518e33e6dSHerbert Xu {
44688056ec3SHerbert Xu return tfm->reqsize;
44718e33e6dSHerbert Xu }
44818e33e6dSHerbert Xu
ahash_request_ctx(struct ahash_request * req)449dec8b786SHerbert Xu static inline void *ahash_request_ctx(struct ahash_request *req)
450dec8b786SHerbert Xu {
451dec8b786SHerbert Xu return req->__ctx;
452dec8b786SHerbert Xu }
453dec8b786SHerbert Xu
45490240ffbSStephan Mueller /**
45590240ffbSStephan Mueller * crypto_ahash_setkey - set key for cipher handle
45690240ffbSStephan Mueller * @tfm: cipher handle
45790240ffbSStephan Mueller * @key: buffer holding the key
45890240ffbSStephan Mueller * @keylen: length of the key in bytes
45990240ffbSStephan Mueller *
46090240ffbSStephan Mueller * The caller provided key is set for the ahash cipher. The cipher
46190240ffbSStephan Mueller * handle must point to a keyed hash in order for this function to succeed.
46290240ffbSStephan Mueller *
46390240ffbSStephan Mueller * Return: 0 if the setting of the key was successful; < 0 if an error occurred
46490240ffbSStephan Mueller */
46566f6ce5eSHerbert Xu int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
46666f6ce5eSHerbert Xu unsigned int keylen);
46790240ffbSStephan Mueller
46890240ffbSStephan Mueller /**
46990240ffbSStephan Mueller * crypto_ahash_finup() - update and finalize message digest
47090240ffbSStephan Mueller * @req: reference to the ahash_request handle that holds all information
47190240ffbSStephan Mueller * needed to perform the cipher operation
47290240ffbSStephan Mueller *
47390240ffbSStephan Mueller * This function is a "short-hand" for the function calls of
474560b1a82SKamil Konieczny * crypto_ahash_update and crypto_ahash_final. The parameters have the same
47590240ffbSStephan Mueller * meaning as discussed for those separate functions.
47690240ffbSStephan Mueller *
477560b1a82SKamil Konieczny * Return: see crypto_ahash_final()
47890240ffbSStephan Mueller */
47966f6ce5eSHerbert Xu int crypto_ahash_finup(struct ahash_request *req);
48090240ffbSStephan Mueller
48190240ffbSStephan Mueller /**
48290240ffbSStephan Mueller * crypto_ahash_final() - calculate message digest
48390240ffbSStephan Mueller * @req: reference to the ahash_request handle that holds all information
48490240ffbSStephan Mueller * needed to perform the cipher operation
48590240ffbSStephan Mueller *
48690240ffbSStephan Mueller * Finalize the message digest operation and create the message digest
48790240ffbSStephan Mueller * based on all data added to the cipher handle. The message digest is placed
48890240ffbSStephan Mueller * into the output buffer registered with the ahash_request handle.
48990240ffbSStephan Mueller *
490560b1a82SKamil Konieczny * Return:
491560b1a82SKamil Konieczny * 0 if the message digest was successfully calculated;
492155f7d32SZhen Lei * -EINPROGRESS if data is fed into hardware (DMA) or queued for later;
493560b1a82SKamil Konieczny * -EBUSY if queue is full and request should be resubmitted later;
494560b1a82SKamil Konieczny * other < 0 if an error occurred
49590240ffbSStephan Mueller */
49666f6ce5eSHerbert Xu int crypto_ahash_final(struct ahash_request *req);
49790240ffbSStephan Mueller
49890240ffbSStephan Mueller /**
49990240ffbSStephan Mueller * crypto_ahash_digest() - calculate message digest for a buffer
50090240ffbSStephan Mueller * @req: reference to the ahash_request handle that holds all information
50190240ffbSStephan Mueller * needed to perform the cipher operation
50290240ffbSStephan Mueller *
50390240ffbSStephan Mueller * This function is a "short-hand" for the function calls of crypto_ahash_init,
50490240ffbSStephan Mueller * crypto_ahash_update and crypto_ahash_final. The parameters have the same
50590240ffbSStephan Mueller * meaning as discussed for those separate three functions.
50690240ffbSStephan Mueller *
507560b1a82SKamil Konieczny * Return: see crypto_ahash_final()
50890240ffbSStephan Mueller */
50966f6ce5eSHerbert Xu int crypto_ahash_digest(struct ahash_request *req);
51018e33e6dSHerbert Xu
51190240ffbSStephan Mueller /**
51290240ffbSStephan Mueller * crypto_ahash_export() - extract current message digest state
51390240ffbSStephan Mueller * @req: reference to the ahash_request handle whose state is exported
51490240ffbSStephan Mueller * @out: output buffer of sufficient size that can hold the hash state
51590240ffbSStephan Mueller *
51690240ffbSStephan Mueller * This function exports the hash state of the ahash_request handle into the
51790240ffbSStephan Mueller * caller-allocated output buffer out which must have sufficient size (e.g. by
518379d972bSRabin Vincent * calling crypto_ahash_statesize()).
51990240ffbSStephan Mueller *
52090240ffbSStephan Mueller * Return: 0 if the export was successful; < 0 if an error occurred
52190240ffbSStephan Mueller */
crypto_ahash_export(struct ahash_request * req,void * out)52288056ec3SHerbert Xu static inline int crypto_ahash_export(struct ahash_request *req, void *out)
523dec8b786SHerbert Xu {
52488056ec3SHerbert Xu return crypto_ahash_reqtfm(req)->export(req, out);
525dec8b786SHerbert Xu }
526dec8b786SHerbert Xu
52790240ffbSStephan Mueller /**
52890240ffbSStephan Mueller * crypto_ahash_import() - import message digest state
52990240ffbSStephan Mueller * @req: reference to ahash_request handle the state is imported into
53090240ffbSStephan Mueller * @in: buffer holding the state
53190240ffbSStephan Mueller *
53290240ffbSStephan Mueller * This function imports the hash state into the ahash_request handle from the
53390240ffbSStephan Mueller * input buffer. That buffer should have been generated with the
53490240ffbSStephan Mueller * crypto_ahash_export function.
53590240ffbSStephan Mueller *
53690240ffbSStephan Mueller * Return: 0 if the import was successful; < 0 if an error occurred
53790240ffbSStephan Mueller */
crypto_ahash_import(struct ahash_request * req,const void * in)53888056ec3SHerbert Xu static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
53988056ec3SHerbert Xu {
5409fa68f62SEric Biggers struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
5419fa68f62SEric Biggers
5429fa68f62SEric Biggers if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
5439fa68f62SEric Biggers return -ENOKEY;
5449fa68f62SEric Biggers
5459fa68f62SEric Biggers return tfm->import(req, in);
54688056ec3SHerbert Xu }
547dec8b786SHerbert Xu
54890240ffbSStephan Mueller /**
54990240ffbSStephan Mueller * crypto_ahash_init() - (re)initialize message digest handle
55090240ffbSStephan Mueller * @req: ahash_request handle that already is initialized with all necessary
55190240ffbSStephan Mueller * data using the ahash_request_* API functions
55290240ffbSStephan Mueller *
55390240ffbSStephan Mueller * The call (re-)initializes the message digest referenced by the ahash_request
55490240ffbSStephan Mueller * handle. Any potentially existing state created by previous operations is
55590240ffbSStephan Mueller * discarded.
55690240ffbSStephan Mueller *
557560b1a82SKamil Konieczny * Return: see crypto_ahash_final()
55890240ffbSStephan Mueller */
crypto_ahash_init(struct ahash_request * req)559318e5313SHerbert Xu static inline int crypto_ahash_init(struct ahash_request *req)
560318e5313SHerbert Xu {
5619fa68f62SEric Biggers struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
5629fa68f62SEric Biggers
5639fa68f62SEric Biggers if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
5649fa68f62SEric Biggers return -ENOKEY;
5659fa68f62SEric Biggers
5669fa68f62SEric Biggers return tfm->init(req);
567318e5313SHerbert Xu }
568318e5313SHerbert Xu
hash_get_stat(struct hash_alg_common * alg)56942808e5dSHerbert Xu static inline struct crypto_istat_hash *hash_get_stat(
57042808e5dSHerbert Xu struct hash_alg_common *alg)
57142808e5dSHerbert Xu {
57242808e5dSHerbert Xu #ifdef CONFIG_CRYPTO_STATS
57342808e5dSHerbert Xu return &alg->stat;
57442808e5dSHerbert Xu #else
57542808e5dSHerbert Xu return NULL;
57642808e5dSHerbert Xu #endif
57742808e5dSHerbert Xu }
57842808e5dSHerbert Xu
crypto_hash_errstat(struct hash_alg_common * alg,int err)57942808e5dSHerbert Xu static inline int crypto_hash_errstat(struct hash_alg_common *alg, int err)
58042808e5dSHerbert Xu {
58142808e5dSHerbert Xu if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
58242808e5dSHerbert Xu return err;
58342808e5dSHerbert Xu
58442808e5dSHerbert Xu if (err && err != -EINPROGRESS && err != -EBUSY)
58542808e5dSHerbert Xu atomic64_inc(&hash_get_stat(alg)->err_cnt);
58642808e5dSHerbert Xu
58742808e5dSHerbert Xu return err;
58842808e5dSHerbert Xu }
58942808e5dSHerbert Xu
59090240ffbSStephan Mueller /**
59190240ffbSStephan Mueller * crypto_ahash_update() - add data to message digest for processing
59290240ffbSStephan Mueller * @req: ahash_request handle that was previously initialized with the
59390240ffbSStephan Mueller * crypto_ahash_init call.
59490240ffbSStephan Mueller *
59590240ffbSStephan Mueller * Updates the message digest state of the &ahash_request handle. The input data
59690240ffbSStephan Mueller * is pointed to by the scatter/gather list registered in the &ahash_request
59790240ffbSStephan Mueller * handle
59890240ffbSStephan Mueller *
599560b1a82SKamil Konieczny * Return: see crypto_ahash_final()
60090240ffbSStephan Mueller */
crypto_ahash_update(struct ahash_request * req)601318e5313SHerbert Xu static inline int crypto_ahash_update(struct ahash_request *req)
602318e5313SHerbert Xu {
603f7d76e05SCorentin Labbe struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
60442808e5dSHerbert Xu struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
605cac5818cSCorentin Labbe
60642808e5dSHerbert Xu if (IS_ENABLED(CONFIG_CRYPTO_STATS))
60742808e5dSHerbert Xu atomic64_add(req->nbytes, &hash_get_stat(alg)->hash_tlen);
60842808e5dSHerbert Xu
60942808e5dSHerbert Xu return crypto_hash_errstat(alg, tfm->update(req));
610318e5313SHerbert Xu }
611318e5313SHerbert Xu
61290240ffbSStephan Mueller /**
61390240ffbSStephan Mueller * DOC: Asynchronous Hash Request Handle
61490240ffbSStephan Mueller *
61590240ffbSStephan Mueller * The &ahash_request data structure contains all pointers to data
61690240ffbSStephan Mueller * required for the asynchronous cipher operation. This includes the cipher
61790240ffbSStephan Mueller * handle (which can be used by multiple &ahash_request instances), pointer
61890240ffbSStephan Mueller * to plaintext and the message digest output buffer, asynchronous callback
61990240ffbSStephan Mueller * function, etc. It acts as a handle to the ahash_request_* API calls in a
62090240ffbSStephan Mueller * similar way as ahash handle to the crypto_ahash_* API calls.
62190240ffbSStephan Mueller */
62290240ffbSStephan Mueller
62390240ffbSStephan Mueller /**
62490240ffbSStephan Mueller * ahash_request_set_tfm() - update cipher handle reference in request
62590240ffbSStephan Mueller * @req: request handle to be modified
62690240ffbSStephan Mueller * @tfm: cipher handle that shall be added to the request handle
62790240ffbSStephan Mueller *
62890240ffbSStephan Mueller * Allow the caller to replace the existing ahash handle in the request
62990240ffbSStephan Mueller * data structure with a different one.
63090240ffbSStephan Mueller */
ahash_request_set_tfm(struct ahash_request * req,struct crypto_ahash * tfm)63118e33e6dSHerbert Xu static inline void ahash_request_set_tfm(struct ahash_request *req,
63218e33e6dSHerbert Xu struct crypto_ahash *tfm)
63318e33e6dSHerbert Xu {
63418e33e6dSHerbert Xu req->base.tfm = crypto_ahash_tfm(tfm);
63518e33e6dSHerbert Xu }
63618e33e6dSHerbert Xu
63790240ffbSStephan Mueller /**
63890240ffbSStephan Mueller * ahash_request_alloc() - allocate request data structure
63990240ffbSStephan Mueller * @tfm: cipher handle to be registered with the request
64090240ffbSStephan Mueller * @gfp: memory allocation flag that is handed to kmalloc by the API call.
64190240ffbSStephan Mueller *
64290240ffbSStephan Mueller * Allocate the request data structure that must be used with the ahash
64390240ffbSStephan Mueller * message digest API calls. During
64490240ffbSStephan Mueller * the allocation, the provided ahash handle
64590240ffbSStephan Mueller * is registered in the request data structure.
64690240ffbSStephan Mueller *
6476eae29e7SEric Biggers * Return: allocated request handle in case of success, or NULL if out of memory
64890240ffbSStephan Mueller */
ahash_request_alloc(struct crypto_ahash * tfm,gfp_t gfp)64918e33e6dSHerbert Xu static inline struct ahash_request *ahash_request_alloc(
65018e33e6dSHerbert Xu struct crypto_ahash *tfm, gfp_t gfp)
65118e33e6dSHerbert Xu {
65218e33e6dSHerbert Xu struct ahash_request *req;
65318e33e6dSHerbert Xu
65418e33e6dSHerbert Xu req = kmalloc(sizeof(struct ahash_request) +
65518e33e6dSHerbert Xu crypto_ahash_reqsize(tfm), gfp);
65618e33e6dSHerbert Xu
65718e33e6dSHerbert Xu if (likely(req))
65818e33e6dSHerbert Xu ahash_request_set_tfm(req, tfm);
65918e33e6dSHerbert Xu
66018e33e6dSHerbert Xu return req;
66118e33e6dSHerbert Xu }
66218e33e6dSHerbert Xu
66390240ffbSStephan Mueller /**
66490240ffbSStephan Mueller * ahash_request_free() - zeroize and free the request data structure
66590240ffbSStephan Mueller * @req: request data structure cipher handle to be freed
66690240ffbSStephan Mueller */
ahash_request_free(struct ahash_request * req)66718e33e6dSHerbert Xu static inline void ahash_request_free(struct ahash_request *req)
66818e33e6dSHerbert Xu {
669453431a5SWaiman Long kfree_sensitive(req);
67018e33e6dSHerbert Xu }
67118e33e6dSHerbert Xu
ahash_request_zero(struct ahash_request * req)672e67ffe0aSHerbert Xu static inline void ahash_request_zero(struct ahash_request *req)
673e67ffe0aSHerbert Xu {
674e67ffe0aSHerbert Xu memzero_explicit(req, sizeof(*req) +
675e67ffe0aSHerbert Xu crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
676e67ffe0aSHerbert Xu }
677e67ffe0aSHerbert Xu
ahash_request_cast(struct crypto_async_request * req)67818e33e6dSHerbert Xu static inline struct ahash_request *ahash_request_cast(
67918e33e6dSHerbert Xu struct crypto_async_request *req)
68018e33e6dSHerbert Xu {
68118e33e6dSHerbert Xu return container_of(req, struct ahash_request, base);
68218e33e6dSHerbert Xu }
68318e33e6dSHerbert Xu
68490240ffbSStephan Mueller /**
68590240ffbSStephan Mueller * ahash_request_set_callback() - set asynchronous callback function
68690240ffbSStephan Mueller * @req: request handle
68790240ffbSStephan Mueller * @flags: specify zero or an ORing of the flags
68890240ffbSStephan Mueller * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
68990240ffbSStephan Mueller * increase the wait queue beyond the initial maximum size;
69090240ffbSStephan Mueller * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
69190240ffbSStephan Mueller * @compl: callback function pointer to be registered with the request handle
69290240ffbSStephan Mueller * @data: The data pointer refers to memory that is not used by the kernel
69390240ffbSStephan Mueller * crypto API, but provided to the callback function for it to use. Here,
69490240ffbSStephan Mueller * the caller can provide a reference to memory the callback function can
69590240ffbSStephan Mueller * operate on. As the callback function is invoked asynchronously to the
69690240ffbSStephan Mueller * related functionality, it may need to access data structures of the
69790240ffbSStephan Mueller * related functionality which can be referenced using this pointer. The
69890240ffbSStephan Mueller * callback function can access the memory via the "data" field in the
69990240ffbSStephan Mueller * &crypto_async_request data structure provided to the callback function.
70090240ffbSStephan Mueller *
70190240ffbSStephan Mueller * This function allows setting the callback function that is triggered once
70290240ffbSStephan Mueller * the cipher operation completes.
70390240ffbSStephan Mueller *
70490240ffbSStephan Mueller * The callback function is registered with the &ahash_request handle and
7050184cfe7SStephan Mueller * must comply with the following template::
70690240ffbSStephan Mueller *
70790240ffbSStephan Mueller * void callback_function(struct crypto_async_request *req, int error)
70890240ffbSStephan Mueller */
ahash_request_set_callback(struct ahash_request * req,u32 flags,crypto_completion_t compl,void * data)70918e33e6dSHerbert Xu static inline void ahash_request_set_callback(struct ahash_request *req,
71018e33e6dSHerbert Xu u32 flags,
7113e3dc25fSMark Rustad crypto_completion_t compl,
71218e33e6dSHerbert Xu void *data)
71318e33e6dSHerbert Xu {
7143e3dc25fSMark Rustad req->base.complete = compl;
71518e33e6dSHerbert Xu req->base.data = data;
71618e33e6dSHerbert Xu req->base.flags = flags;
71718e33e6dSHerbert Xu }
71818e33e6dSHerbert Xu
71990240ffbSStephan Mueller /**
72090240ffbSStephan Mueller * ahash_request_set_crypt() - set data buffers
72190240ffbSStephan Mueller * @req: ahash_request handle to be updated
72290240ffbSStephan Mueller * @src: source scatter/gather list
72390240ffbSStephan Mueller * @result: buffer that is filled with the message digest -- the caller must
72490240ffbSStephan Mueller * ensure that the buffer has sufficient space by, for example, calling
72590240ffbSStephan Mueller * crypto_ahash_digestsize()
72690240ffbSStephan Mueller * @nbytes: number of bytes to process from the source scatter/gather list
72790240ffbSStephan Mueller *
72890240ffbSStephan Mueller * By using this call, the caller references the source scatter/gather list.
72990240ffbSStephan Mueller * The source scatter/gather list points to the data the message digest is to
73090240ffbSStephan Mueller * be calculated for.
73190240ffbSStephan Mueller */
ahash_request_set_crypt(struct ahash_request * req,struct scatterlist * src,u8 * result,unsigned int nbytes)73218e33e6dSHerbert Xu static inline void ahash_request_set_crypt(struct ahash_request *req,
73318e33e6dSHerbert Xu struct scatterlist *src, u8 *result,
73418e33e6dSHerbert Xu unsigned int nbytes)
73518e33e6dSHerbert Xu {
73618e33e6dSHerbert Xu req->src = src;
73718e33e6dSHerbert Xu req->nbytes = nbytes;
73818e33e6dSHerbert Xu req->result = result;
73918e33e6dSHerbert Xu }
74018e33e6dSHerbert Xu
741968ab291SStephan Mueller /**
742968ab291SStephan Mueller * DOC: Synchronous Message Digest API
743968ab291SStephan Mueller *
744968ab291SStephan Mueller * The synchronous message digest API is used with the ciphers of type
745968ab291SStephan Mueller * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
746968ab291SStephan Mueller *
747968ab291SStephan Mueller * The message digest API is able to maintain state information for the
748968ab291SStephan Mueller * caller.
749968ab291SStephan Mueller *
750da087a4cSRandy Dunlap * The synchronous message digest API can store user-related context in its
751968ab291SStephan Mueller * shash_desc request data structure.
752968ab291SStephan Mueller */
753968ab291SStephan Mueller
754968ab291SStephan Mueller /**
755968ab291SStephan Mueller * crypto_alloc_shash() - allocate message digest handle
756968ab291SStephan Mueller * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
757968ab291SStephan Mueller * message digest cipher
758968ab291SStephan Mueller * @type: specifies the type of the cipher
759968ab291SStephan Mueller * @mask: specifies the mask for the cipher
760968ab291SStephan Mueller *
761968ab291SStephan Mueller * Allocate a cipher handle for a message digest. The returned &struct
762968ab291SStephan Mueller * crypto_shash is the cipher handle that is required for any subsequent
763968ab291SStephan Mueller * API invocation for that message digest.
764968ab291SStephan Mueller *
765968ab291SStephan Mueller * Return: allocated cipher handle in case of success; IS_ERR() is true in case
766968ab291SStephan Mueller * of an error, PTR_ERR() returns the error code.
767968ab291SStephan Mueller */
7687b5a080bSHerbert Xu struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
7697b5a080bSHerbert Xu u32 mask);
7707b5a080bSHerbert Xu
771ed3630b8SHerbert Xu struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);
772ed3630b8SHerbert Xu
77385cc4243SHannes Reinecke int crypto_has_shash(const char *alg_name, u32 type, u32 mask);
77485cc4243SHannes Reinecke
crypto_shash_tfm(struct crypto_shash * tfm)7757b5a080bSHerbert Xu static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
7767b5a080bSHerbert Xu {
7777b5a080bSHerbert Xu return &tfm->base;
7787b5a080bSHerbert Xu }
7797b5a080bSHerbert Xu
780968ab291SStephan Mueller /**
781968ab291SStephan Mueller * crypto_free_shash() - zeroize and free the message digest handle
782968ab291SStephan Mueller * @tfm: cipher handle to be freed
78383681f2bSArd Biesheuvel *
78483681f2bSArd Biesheuvel * If @tfm is a NULL or error pointer, this function does nothing.
785968ab291SStephan Mueller */
crypto_free_shash(struct crypto_shash * tfm)7867b5a080bSHerbert Xu static inline void crypto_free_shash(struct crypto_shash *tfm)
7877b5a080bSHerbert Xu {
788412e87aeSHerbert Xu crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
7897b5a080bSHerbert Xu }
7907b5a080bSHerbert Xu
crypto_shash_alg_name(struct crypto_shash * tfm)791d12481bcSHerbert Xu static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
792d12481bcSHerbert Xu {
793d12481bcSHerbert Xu return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
794d12481bcSHerbert Xu }
795d12481bcSHerbert Xu
crypto_shash_driver_name(struct crypto_shash * tfm)796d12481bcSHerbert Xu static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
797d12481bcSHerbert Xu {
798d12481bcSHerbert Xu return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
799d12481bcSHerbert Xu }
800d12481bcSHerbert Xu
crypto_shash_alignmask(struct crypto_shash * tfm)8017b5a080bSHerbert Xu static inline unsigned int crypto_shash_alignmask(
8027b5a080bSHerbert Xu struct crypto_shash *tfm)
8037b5a080bSHerbert Xu {
8047b5a080bSHerbert Xu return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
8057b5a080bSHerbert Xu }
8067b5a080bSHerbert Xu
807968ab291SStephan Mueller /**
808968ab291SStephan Mueller * crypto_shash_blocksize() - obtain block size for cipher
809968ab291SStephan Mueller * @tfm: cipher handle
810968ab291SStephan Mueller *
811968ab291SStephan Mueller * The block size for the message digest cipher referenced with the cipher
812968ab291SStephan Mueller * handle is returned.
813968ab291SStephan Mueller *
814968ab291SStephan Mueller * Return: block size of cipher
815968ab291SStephan Mueller */
crypto_shash_blocksize(struct crypto_shash * tfm)81697495986SHerbert Xu static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
81797495986SHerbert Xu {
81897495986SHerbert Xu return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
81997495986SHerbert Xu }
82097495986SHerbert Xu
__crypto_shash_alg(struct crypto_alg * alg)8217b5a080bSHerbert Xu static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
8227b5a080bSHerbert Xu {
8237b5a080bSHerbert Xu return container_of(alg, struct shash_alg, base);
8247b5a080bSHerbert Xu }
8257b5a080bSHerbert Xu
crypto_shash_alg(struct crypto_shash * tfm)8267b5a080bSHerbert Xu static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
8277b5a080bSHerbert Xu {
8287b5a080bSHerbert Xu return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
8297b5a080bSHerbert Xu }
8307b5a080bSHerbert Xu
831968ab291SStephan Mueller /**
832968ab291SStephan Mueller * crypto_shash_digestsize() - obtain message digest size
833968ab291SStephan Mueller * @tfm: cipher handle
834968ab291SStephan Mueller *
835968ab291SStephan Mueller * The size for the message digest created by the message digest cipher
836968ab291SStephan Mueller * referenced with the cipher handle is returned.
837968ab291SStephan Mueller *
838968ab291SStephan Mueller * Return: digest size of cipher
839968ab291SStephan Mueller */
crypto_shash_digestsize(struct crypto_shash * tfm)8407b5a080bSHerbert Xu static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
8417b5a080bSHerbert Xu {
8427b5a080bSHerbert Xu return crypto_shash_alg(tfm)->digestsize;
8437b5a080bSHerbert Xu }
8447b5a080bSHerbert Xu
crypto_shash_statesize(struct crypto_shash * tfm)84599d27e1cSHerbert Xu static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
84699d27e1cSHerbert Xu {
84799d27e1cSHerbert Xu return crypto_shash_alg(tfm)->statesize;
84899d27e1cSHerbert Xu }
84999d27e1cSHerbert Xu
crypto_shash_get_flags(struct crypto_shash * tfm)8507b5a080bSHerbert Xu static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
8517b5a080bSHerbert Xu {
8527b5a080bSHerbert Xu return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
8537b5a080bSHerbert Xu }
8547b5a080bSHerbert Xu
crypto_shash_set_flags(struct crypto_shash * tfm,u32 flags)8557b5a080bSHerbert Xu static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
8567b5a080bSHerbert Xu {
8577b5a080bSHerbert Xu crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
8587b5a080bSHerbert Xu }
8597b5a080bSHerbert Xu
crypto_shash_clear_flags(struct crypto_shash * tfm,u32 flags)8607b5a080bSHerbert Xu static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
8617b5a080bSHerbert Xu {
8627b5a080bSHerbert Xu crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
8637b5a080bSHerbert Xu }
8647b5a080bSHerbert Xu
865968ab291SStephan Mueller /**
866968ab291SStephan Mueller * crypto_shash_descsize() - obtain the operational state size
867968ab291SStephan Mueller * @tfm: cipher handle
868968ab291SStephan Mueller *
869968ab291SStephan Mueller * The size of the operational state the cipher needs during operation is
870968ab291SStephan Mueller * returned for the hash referenced with the cipher handle. This size is
871968ab291SStephan Mueller * required to calculate the memory requirements to allow the caller allocating
872968ab291SStephan Mueller * sufficient memory for operational state.
873968ab291SStephan Mueller *
874968ab291SStephan Mueller * The operational state is defined with struct shash_desc where the size of
875968ab291SStephan Mueller * that data structure is to be calculated as
876968ab291SStephan Mueller * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
877968ab291SStephan Mueller *
878968ab291SStephan Mueller * Return: size of the operational state
879968ab291SStephan Mueller */
crypto_shash_descsize(struct crypto_shash * tfm)8807b5a080bSHerbert Xu static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
8817b5a080bSHerbert Xu {
882113adefcSHerbert Xu return tfm->descsize;
8837b5a080bSHerbert Xu }
8847b5a080bSHerbert Xu
shash_desc_ctx(struct shash_desc * desc)8857b5a080bSHerbert Xu static inline void *shash_desc_ctx(struct shash_desc *desc)
8867b5a080bSHerbert Xu {
8877b5a080bSHerbert Xu return desc->__ctx;
8887b5a080bSHerbert Xu }
8897b5a080bSHerbert Xu
890968ab291SStephan Mueller /**
891968ab291SStephan Mueller * crypto_shash_setkey() - set key for message digest
892968ab291SStephan Mueller * @tfm: cipher handle
893968ab291SStephan Mueller * @key: buffer holding the key
894968ab291SStephan Mueller * @keylen: length of the key in bytes
895968ab291SStephan Mueller *
896968ab291SStephan Mueller * The caller provided key is set for the keyed message digest cipher. The
897968ab291SStephan Mueller * cipher handle must point to a keyed message digest cipher in order for this
898968ab291SStephan Mueller * function to succeed.
899968ab291SStephan Mueller *
900877b5691SEric Biggers * Context: Any context.
901968ab291SStephan Mueller * Return: 0 if the setting of the key was successful; < 0 if an error occurred
902968ab291SStephan Mueller */
9037b5a080bSHerbert Xu int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
9047b5a080bSHerbert Xu unsigned int keylen);
905968ab291SStephan Mueller
906968ab291SStephan Mueller /**
907968ab291SStephan Mueller * crypto_shash_digest() - calculate message digest for buffer
908968ab291SStephan Mueller * @desc: see crypto_shash_final()
909968ab291SStephan Mueller * @data: see crypto_shash_update()
910968ab291SStephan Mueller * @len: see crypto_shash_update()
911968ab291SStephan Mueller * @out: see crypto_shash_final()
912968ab291SStephan Mueller *
913968ab291SStephan Mueller * This function is a "short-hand" for the function calls of crypto_shash_init,
914968ab291SStephan Mueller * crypto_shash_update and crypto_shash_final. The parameters have the same
915968ab291SStephan Mueller * meaning as discussed for those separate three functions.
916968ab291SStephan Mueller *
917877b5691SEric Biggers * Context: Any context.
918968ab291SStephan Mueller * Return: 0 if the message digest creation was successful; < 0 if an error
919968ab291SStephan Mueller * occurred
920968ab291SStephan Mueller */
9217b5a080bSHerbert Xu int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
9227b5a080bSHerbert Xu unsigned int len, u8 *out);
9237b5a080bSHerbert Xu
924968ab291SStephan Mueller /**
925822a98b8SEric Biggers * crypto_shash_tfm_digest() - calculate message digest for buffer
926822a98b8SEric Biggers * @tfm: hash transformation object
927822a98b8SEric Biggers * @data: see crypto_shash_update()
928822a98b8SEric Biggers * @len: see crypto_shash_update()
929822a98b8SEric Biggers * @out: see crypto_shash_final()
930822a98b8SEric Biggers *
931822a98b8SEric Biggers * This is a simplified version of crypto_shash_digest() for users who don't
932822a98b8SEric Biggers * want to allocate their own hash descriptor (shash_desc). Instead,
933822a98b8SEric Biggers * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)
934822a98b8SEric Biggers * directly, and it allocates a hash descriptor on the stack internally.
935822a98b8SEric Biggers * Note that this stack allocation may be fairly large.
936822a98b8SEric Biggers *
937822a98b8SEric Biggers * Context: Any context.
938822a98b8SEric Biggers * Return: 0 on success; < 0 if an error occurred.
939822a98b8SEric Biggers */
940822a98b8SEric Biggers int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
941822a98b8SEric Biggers unsigned int len, u8 *out);
942822a98b8SEric Biggers
943822a98b8SEric Biggers /**
944968ab291SStephan Mueller * crypto_shash_export() - extract operational state for message digest
945968ab291SStephan Mueller * @desc: reference to the operational state handle whose state is exported
946968ab291SStephan Mueller * @out: output buffer of sufficient size that can hold the hash state
947968ab291SStephan Mueller *
948968ab291SStephan Mueller * This function exports the hash state of the operational state handle into the
949968ab291SStephan Mueller * caller-allocated output buffer out which must have sufficient size (e.g. by
950968ab291SStephan Mueller * calling crypto_shash_descsize).
951968ab291SStephan Mueller *
952877b5691SEric Biggers * Context: Any context.
953968ab291SStephan Mueller * Return: 0 if the export creation was successful; < 0 if an error occurred
954968ab291SStephan Mueller */
crypto_shash_export(struct shash_desc * desc,void * out)95599d27e1cSHerbert Xu static inline int crypto_shash_export(struct shash_desc *desc, void *out)
956dec8b786SHerbert Xu {
95799d27e1cSHerbert Xu return crypto_shash_alg(desc->tfm)->export(desc, out);
958dec8b786SHerbert Xu }
959dec8b786SHerbert Xu
960968ab291SStephan Mueller /**
961968ab291SStephan Mueller * crypto_shash_import() - import operational state
962968ab291SStephan Mueller * @desc: reference to the operational state handle the state imported into
963968ab291SStephan Mueller * @in: buffer holding the state
964968ab291SStephan Mueller *
965968ab291SStephan Mueller * This function imports the hash state into the operational state handle from
966968ab291SStephan Mueller * the input buffer. That buffer should have been generated with the
967968ab291SStephan Mueller * crypto_ahash_export function.
968968ab291SStephan Mueller *
969877b5691SEric Biggers * Context: Any context.
970968ab291SStephan Mueller * Return: 0 if the import was successful; < 0 if an error occurred
971968ab291SStephan Mueller */
crypto_shash_import(struct shash_desc * desc,const void * in)97299d27e1cSHerbert Xu static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
97399d27e1cSHerbert Xu {
9749fa68f62SEric Biggers struct crypto_shash *tfm = desc->tfm;
9759fa68f62SEric Biggers
9769fa68f62SEric Biggers if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
9779fa68f62SEric Biggers return -ENOKEY;
9789fa68f62SEric Biggers
9799fa68f62SEric Biggers return crypto_shash_alg(tfm)->import(desc, in);
98099d27e1cSHerbert Xu }
981dec8b786SHerbert Xu
982968ab291SStephan Mueller /**
983968ab291SStephan Mueller * crypto_shash_init() - (re)initialize message digest
984968ab291SStephan Mueller * @desc: operational state handle that is already filled
985968ab291SStephan Mueller *
986968ab291SStephan Mueller * The call (re-)initializes the message digest referenced by the
987968ab291SStephan Mueller * operational state handle. Any potentially existing state created by
988968ab291SStephan Mueller * previous operations is discarded.
989968ab291SStephan Mueller *
990877b5691SEric Biggers * Context: Any context.
991968ab291SStephan Mueller * Return: 0 if the message digest initialization was successful; < 0 if an
992968ab291SStephan Mueller * error occurred
993968ab291SStephan Mueller */
crypto_shash_init(struct shash_desc * desc)9947b5a080bSHerbert Xu static inline int crypto_shash_init(struct shash_desc *desc)
9957b5a080bSHerbert Xu {
9969fa68f62SEric Biggers struct crypto_shash *tfm = desc->tfm;
9979fa68f62SEric Biggers
9989fa68f62SEric Biggers if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
9999fa68f62SEric Biggers return -ENOKEY;
10009fa68f62SEric Biggers
10019fa68f62SEric Biggers return crypto_shash_alg(tfm)->init(desc);
10027b5a080bSHerbert Xu }
10037b5a080bSHerbert Xu
1004968ab291SStephan Mueller /**
1005968ab291SStephan Mueller * crypto_shash_update() - add data to message digest for processing
1006968ab291SStephan Mueller * @desc: operational state handle that is already initialized
1007968ab291SStephan Mueller * @data: input data to be added to the message digest
1008968ab291SStephan Mueller * @len: length of the input data
1009968ab291SStephan Mueller *
1010968ab291SStephan Mueller * Updates the message digest state of the operational state handle.
1011968ab291SStephan Mueller *
1012877b5691SEric Biggers * Context: Any context.
1013968ab291SStephan Mueller * Return: 0 if the message digest update was successful; < 0 if an error
1014968ab291SStephan Mueller * occurred
1015968ab291SStephan Mueller */
10167b5a080bSHerbert Xu int crypto_shash_update(struct shash_desc *desc, const u8 *data,
10177b5a080bSHerbert Xu unsigned int len);
1018968ab291SStephan Mueller
1019968ab291SStephan Mueller /**
1020968ab291SStephan Mueller * crypto_shash_final() - calculate message digest
1021968ab291SStephan Mueller * @desc: operational state handle that is already filled with data
1022968ab291SStephan Mueller * @out: output buffer filled with the message digest
1023968ab291SStephan Mueller *
1024968ab291SStephan Mueller * Finalize the message digest operation and create the message digest
1025968ab291SStephan Mueller * based on all data added to the cipher handle. The message digest is placed
1026968ab291SStephan Mueller * into the output buffer. The caller must ensure that the output buffer is
1027968ab291SStephan Mueller * large enough by using crypto_shash_digestsize.
1028968ab291SStephan Mueller *
1029877b5691SEric Biggers * Context: Any context.
1030968ab291SStephan Mueller * Return: 0 if the message digest creation was successful; < 0 if an error
1031968ab291SStephan Mueller * occurred
1032968ab291SStephan Mueller */
10337b5a080bSHerbert Xu int crypto_shash_final(struct shash_desc *desc, u8 *out);
1034968ab291SStephan Mueller
1035968ab291SStephan Mueller /**
1036968ab291SStephan Mueller * crypto_shash_finup() - calculate message digest of buffer
1037968ab291SStephan Mueller * @desc: see crypto_shash_final()
1038968ab291SStephan Mueller * @data: see crypto_shash_update()
1039968ab291SStephan Mueller * @len: see crypto_shash_update()
1040968ab291SStephan Mueller * @out: see crypto_shash_final()
1041968ab291SStephan Mueller *
1042968ab291SStephan Mueller * This function is a "short-hand" for the function calls of
1043968ab291SStephan Mueller * crypto_shash_update and crypto_shash_final. The parameters have the same
1044968ab291SStephan Mueller * meaning as discussed for those separate functions.
1045968ab291SStephan Mueller *
1046877b5691SEric Biggers * Context: Any context.
1047968ab291SStephan Mueller * Return: 0 if the message digest creation was successful; < 0 if an error
1048968ab291SStephan Mueller * occurred
1049968ab291SStephan Mueller */
10507b5a080bSHerbert Xu int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
10517b5a080bSHerbert Xu unsigned int len, u8 *out);
10527b5a080bSHerbert Xu
shash_desc_zero(struct shash_desc * desc)1053e67ffe0aSHerbert Xu static inline void shash_desc_zero(struct shash_desc *desc)
1054e67ffe0aSHerbert Xu {
1055e67ffe0aSHerbert Xu memzero_explicit(desc,
1056e67ffe0aSHerbert Xu sizeof(*desc) + crypto_shash_descsize(desc->tfm));
1057e67ffe0aSHerbert Xu }
1058e67ffe0aSHerbert Xu
105918e33e6dSHerbert Xu #endif /* _CRYPTO_HASH_H */
1060