1047 lines · 36.2 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0-or-later *//* * Hash: Hash algorithms under the crypto API *  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> */ #ifndef _CRYPTO_HASH_H#define _CRYPTO_HASH_H #include <linux/crypto.h>#include <linux/scatterlist.h>#include <linux/slab.h>#include <linux/string.h> /* Set this bit for virtual address instead of SG list. */#define CRYPTO_AHASH_REQ_VIRT	0x00000001 #define CRYPTO_AHASH_REQ_PRIVATE \	CRYPTO_AHASH_REQ_VIRT struct crypto_ahash; /** * DOC: Message Digest Algorithm Definitions * * These data structures define modular message digest algorithm * implementations, managed via crypto_register_ahash(), * crypto_register_shash(), crypto_unregister_ahash() and * crypto_unregister_shash(). */ /* * struct hash_alg_common - define properties of message digest * @digestsize: Size of the result of the transformation. A buffer of this size *	        must be available to the @final and @finup calls, so they can *	        store the resulting hash into it. For various predefined sizes, *	        search include/crypto/ using *	        git grep _DIGEST_SIZE include/crypto. * @statesize: Size of the block for partial state of the transformation. A *	       buffer of this size must be passed to the @export function as it *	       will save the partial state of the transformation into it. On the *	       other side, the @import function will load the state from a *	       buffer of this size as well. * @base: Start of data structure of cipher algorithm. The common data *	  structure of crypto_alg contains information common to all ciphers. *	  The hash_alg_common data structure now adds the hash-specific *	  information. */#define HASH_ALG_COMMON {		\	unsigned int digestsize;	\	unsigned int statesize;		\					\	struct crypto_alg base;		\}struct hash_alg_common HASH_ALG_COMMON; struct ahash_request {	struct crypto_async_request base; 	unsigned int nbytes;	union {		struct scatterlist *src;		const u8 *svirt;	};	u8 *result; 	struct scatterlist sg_head[2];	crypto_completion_t saved_complete;	void *saved_data; 	void *__ctx[] CRYPTO_MINALIGN_ATTR;}; /** * struct ahash_alg - asynchronous message digest definition * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the *	  state of the HASH transformation at the beginning. This shall fill in *	  the internal structures used during the entire duration of the whole *	  transformation. No data processing happens at this point. Driver code *	  implementation must not use req->result. * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This *	   function actually pushes blocks of data from upper layers into the *	   driver, which then passes those to the hardware as seen fit. This *	   function must not finalize the HASH transformation by calculating the *	   final message digest as this only adds more data into the *	   transformation. This function shall not modify the transformation *	   context, as this function may be called in parallel with the same *	   transformation object. Data processing can happen synchronously *	   [SHASH] or asynchronously [AHASH] at this point. Driver must not use *	   req->result. *	   For block-only algorithms, @update must return the number *	   of bytes to store in the API partial block buffer. * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the *	   transformation and retrieves the resulting hash from the driver and *	   pushes it back to upper layers. No data processing happens at this *	   point unless hardware requires it to finish the transformation *	   (then the data buffered by the device driver is processed). * @finup: **[optional]** Combination of @update and @final. This function is effectively a *	   combination of @update and @final calls issued in sequence. As some *	   hardware cannot do @update and @final separately, this callback was *	   added to allow such hardware to be used at least by IPsec. Data *	   processing can happen synchronously [SHASH] or asynchronously [AHASH] *	   at this point. * @digest: Combination of @init and @update and @final. This function *	    effectively behaves as the entire chain of operations, @init, *	    @update and @final issued in sequence. Just like @finup, this was *	    added for hardware which cannot do even the @finup, but can only do *	    the whole transformation in one run. Data processing can happen *	    synchronously [SHASH] or asynchronously [AHASH] at this point. * @setkey: Set optional key used by the hashing algorithm. Intended to push *	    optional key used by the hashing algorithm from upper layers into *	    the driver. This function can store the key in the transformation *	    context or can outright program it into the hardware. In the former *	    case, one must be careful to program the key into the hardware at *	    appropriate time and one must be careful that .setkey() can be *	    called multiple times during the existence of the transformation *	    object. Not  all hashing algorithms do implement this function as it *	    is only needed for keyed message digests. SHAx/MDx/CRCx do NOT *	    implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement *	    this function. This function must be called before any other of the *	    @init, @update, @final, @finup, @digest is called. No data *	    processing happens at this point. * @export: Export partial state of the transformation. This function dumps the *	    entire state of the ongoing transformation into a provided block of *	    data so it can be @import 'ed back later on. This is useful in case *	    you want to save partial result of the transformation after *	    processing certain amount of data and reload this partial result *	    multiple times later on for multiple re-use. No data processing *	    happens at this point. Driver must not use req->result. * @import: Import partial state of the transformation. This function loads the *	    entire state of the ongoing transformation from a provided block of *	    data so the transformation can continue from this point onward. No *	    data processing happens at this point. Driver must not use *	    req->result. * @export_core: Export partial state without partial block.  Only defined *		 for algorithms that are not block-only. * @import_core: Import partial state without partial block.  Only defined *		 for algorithms that are not block-only. * @init_tfm: Initialize the cryptographic transformation object. *	      This function is called only once at the instantiation *	      time, right after the transformation context was *	      allocated. In case the cryptographic hardware has *	      some special requirements which need to be handled *	      by software, this function shall check for the precise *	      requirement of the transformation and put any software *	      fallbacks in place. * @exit_tfm: Deinitialize the cryptographic transformation object. *	      This is a counterpart to @init_tfm, used to remove *	      various changes set in @init_tfm. * @halg: see struct hash_alg_common */struct ahash_alg {	int (*init)(struct ahash_request *req);	int (*update)(struct ahash_request *req);	int (*final)(struct ahash_request *req);	int (*finup)(struct ahash_request *req);	int (*digest)(struct ahash_request *req);	int (*export)(struct ahash_request *req, void *out);	int (*import)(struct ahash_request *req, const void *in);	int (*export_core)(struct ahash_request *req, void *out);	int (*import_core)(struct ahash_request *req, const void *in);	int (*setkey)(struct crypto_ahash *tfm, const u8 *key,		      unsigned int keylen);	int (*init_tfm)(struct crypto_ahash *tfm);	void (*exit_tfm)(struct crypto_ahash *tfm); 	struct hash_alg_common halg;}; struct shash_desc {	struct crypto_shash *tfm;	void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);}; #define HASH_MAX_DIGESTSIZE	 64 /* * The size of a core hash state and a partial block.  The final byte * is the length of the partial block. */#define HASH_STATE_AND_BLOCK(state, block) ((state) + (block) + 1)  /* Worst case is sha3-224. */#define HASH_MAX_STATESIZE	 HASH_STATE_AND_BLOCK(200, 144) /* This needs to match arch/s390/crypto/sha.h. */#define S390_SHA_CTX_SIZE	216 /* * Worst case is hmac(sha3-224-s390).  Its context is a nested 'shash_desc' * containing a 'struct s390_sha_ctx'. */#define SHA3_224_S390_DESCSIZE	HASH_STATE_AND_BLOCK(S390_SHA_CTX_SIZE, 144)#define HASH_MAX_DESCSIZE	(sizeof(struct shash_desc) + \				 SHA3_224_S390_DESCSIZE)#define MAX_SYNC_HASH_REQSIZE	(sizeof(struct ahash_request) + \				 HASH_MAX_DESCSIZE) #define SHASH_DESC_ON_STACK(shash, ctx)					     \	char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \		__aligned(__alignof__(struct shash_desc));		     \	struct shash_desc *shash = (struct shash_desc *)__##shash##_desc #define HASH_REQUEST_ON_STACK(name, _tfm) \	char __##name##_req[sizeof(struct ahash_request) + \			    MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \	struct ahash_request *name = \		ahash_request_on_stack_init(__##name##_req, (_tfm)) #define HASH_REQUEST_CLONE(name, gfp) \	hash_request_clone(name, sizeof(__##name##_req), gfp) #define CRYPTO_HASH_STATESIZE(coresize, blocksize) (coresize + blocksize + 1) /** * struct shash_alg - synchronous message digest definition * @init: see struct ahash_alg * @update: see struct ahash_alg * @final: see struct ahash_alg * @finup: see struct ahash_alg * @digest: see struct ahash_alg * @export: see struct ahash_alg * @import: see struct ahash_alg * @export_core: see struct ahash_alg * @import_core: see struct ahash_alg * @setkey: see struct ahash_alg * @init_tfm: Initialize the cryptographic transformation object. *	      This function is called only once at the instantiation *	      time, right after the transformation context was *	      allocated. In case the cryptographic hardware has *	      some special requirements which need to be handled *	      by software, this function shall check for the precise *	      requirement of the transformation and put any software *	      fallbacks in place. * @exit_tfm: Deinitialize the cryptographic transformation object. *	      This is a counterpart to @init_tfm, used to remove *	      various changes set in @init_tfm. * @descsize: Size of the operational state for the message digest. This state * 	      size is the memory size that needs to be allocated for *	      shash_desc.__ctx * @halg: see struct hash_alg_common * @HASH_ALG_COMMON: see struct hash_alg_common */struct shash_alg {	int (*init)(struct shash_desc *desc);	int (*update)(struct shash_desc *desc, const u8 *data,		      unsigned int len);	int (*final)(struct shash_desc *desc, u8 *out);	int (*finup)(struct shash_desc *desc, const u8 *data,		     unsigned int len, u8 *out);	int (*digest)(struct shash_desc *desc, const u8 *data,		      unsigned int len, u8 *out);	int (*export)(struct shash_desc *desc, void *out);	int (*import)(struct shash_desc *desc, const void *in);	int (*export_core)(struct shash_desc *desc, void *out);	int (*import_core)(struct shash_desc *desc, const void *in);	int (*setkey)(struct crypto_shash *tfm, const u8 *key,		      unsigned int keylen);	int (*init_tfm)(struct crypto_shash *tfm);	void (*exit_tfm)(struct crypto_shash *tfm); 	unsigned int descsize; 	union {		struct HASH_ALG_COMMON;		struct hash_alg_common halg;	};};#undef HASH_ALG_COMMON struct crypto_ahash {	bool using_shash; /* Underlying algorithm is shash, not ahash */	unsigned int statesize;	unsigned int reqsize;	struct crypto_tfm base;}; struct crypto_shash {	struct crypto_tfm base;}; /** * DOC: Asynchronous Message Digest API * * The asynchronous message digest API is used with the ciphers of type * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto) * * The asynchronous cipher operation discussion provided for the * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well. */ static inline bool ahash_req_on_stack(struct ahash_request *req){	return crypto_req_on_stack(&req->base);} static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm){	return container_of(tfm, struct crypto_ahash, base);} /** * crypto_alloc_ahash() - allocate ahash cipher handle * @alg_name: is the cra_name / name or cra_driver_name / driver name of the *	      ahash cipher * @type: specifies the type of the cipher * @mask: specifies the mask for the cipher * * Allocate a cipher handle for an ahash. The returned struct * crypto_ahash is the cipher handle that is required for any subsequent * API invocation for that ahash. * * Return: allocated cipher handle in case of success; IS_ERR() is true in case *	   of an error, PTR_ERR() returns the error code. */struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,					u32 mask); static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm){	return &tfm->base;} /** * crypto_free_ahash() - zeroize and free the ahash handle * @tfm: cipher handle to be freed * * If @tfm is a NULL or error pointer, this function does nothing. */static inline void crypto_free_ahash(struct crypto_ahash *tfm){	crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));} /** * crypto_has_ahash() - Search for the availability of an ahash. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the *	      ahash * @type: specifies the type of the ahash * @mask: specifies the mask for the ahash * * Return: true when the ahash is known to the kernel crypto API; false *	   otherwise */int crypto_has_ahash(const char *alg_name, u32 type, u32 mask); static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm){	return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));} static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm){	return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));} /** * crypto_ahash_blocksize() - obtain block size for cipher * @tfm: cipher handle * * The block size for the message digest cipher referenced with the cipher * handle is returned. * * Return: block size of cipher */static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm){	return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));} static inline struct hash_alg_common *__crypto_hash_alg_common(	struct crypto_alg *alg){	return container_of(alg, struct hash_alg_common, base);} static inline struct hash_alg_common *crypto_hash_alg_common(	struct crypto_ahash *tfm){	return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);} /** * crypto_ahash_digestsize() - obtain message digest size * @tfm: cipher handle * * The size for the message digest created by the message digest cipher * referenced with the cipher handle is returned. * * * Return: message digest size of cipher */static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm){	return crypto_hash_alg_common(tfm)->digestsize;} /** * crypto_ahash_statesize() - obtain size of the ahash state * @tfm: cipher handle * * Return the size of the ahash state. With the crypto_ahash_export() * function, the caller can export the state into a buffer whose size is * defined with this function. * * Return: size of the ahash state */static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm){	return tfm->statesize;} static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm){	return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));} static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags){	crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);} static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags){	crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);} /** * crypto_ahash_reqtfm() - obtain cipher handle from request * @req: asynchronous request handle that contains the reference to the ahash *	 cipher handle * * Return the ahash cipher handle that is registered with the asynchronous * request handle ahash_request. * * Return: ahash cipher handle */static inline struct crypto_ahash *crypto_ahash_reqtfm(	struct ahash_request *req){	return __crypto_ahash_cast(req->base.tfm);} /** * crypto_ahash_reqsize() - obtain size of the request data structure * @tfm: cipher handle * * Return: size of the request data */static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm){	return tfm->reqsize;} static inline void *ahash_request_ctx(struct ahash_request *req){	return req->__ctx;} /** * crypto_ahash_setkey - set key for cipher handle * @tfm: cipher handle * @key: buffer holding the key * @keylen: length of the key in bytes * * The caller provided key is set for the ahash cipher. The cipher * handle must point to a keyed hash in order for this function to succeed. * * Return: 0 if the setting of the key was successful; < 0 if an error occurred */int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,			unsigned int keylen); /** * crypto_ahash_finup() - update and finalize message digest * @req: reference to the ahash_request handle that holds all information *	 needed to perform the cipher operation * * This function is a "short-hand" for the function calls of * crypto_ahash_update and crypto_ahash_final. The parameters have the same * meaning as discussed for those separate functions. * * Return: see crypto_ahash_final() */int crypto_ahash_finup(struct ahash_request *req); /** * crypto_ahash_final() - calculate message digest * @req: reference to the ahash_request handle that holds all information *	 needed to perform the cipher operation * * Finalize the message digest operation and create the message digest * based on all data added to the cipher handle. The message digest is placed * into the output buffer registered with the ahash_request handle. * * Return: * 0		if the message digest was successfully calculated; * -EINPROGRESS	if data is fed into hardware (DMA) or queued for later;

Lines 1–500 of 1047.