/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Hash: Hash algorithms under the crypto API3*4* Copyright (c) 2008 Herbert Xu <[email protected]>5*/67#ifndef _CRYPTO_HASH_H8#define _CRYPTO_HASH_H910#include <linux/crypto.h>11#include <linux/scatterlist.h>12#include <linux/slab.h>13#include <linux/string.h>1415/* Set this bit for virtual address instead of SG list. */16#define CRYPTO_AHASH_REQ_VIRT 0x000000011718#define CRYPTO_AHASH_REQ_PRIVATE \19CRYPTO_AHASH_REQ_VIRT2021struct crypto_ahash;2223/**24* DOC: Message Digest Algorithm Definitions25*26* These data structures define modular message digest algorithm27* implementations, managed via crypto_register_ahash(),28* crypto_register_shash(), crypto_unregister_ahash() and29* crypto_unregister_shash().30*/3132/*33* struct hash_alg_common - define properties of message digest34* @digestsize: Size of the result of the transformation. A buffer of this size35* must be available to the @final and @finup calls, so they can36* store the resulting hash into it. For various predefined sizes,37* search include/crypto/ using38* git grep _DIGEST_SIZE include/crypto.39* @statesize: Size of the block for partial state of the transformation. A40* buffer of this size must be passed to the @export function as it41* will save the partial state of the transformation into it. On the42* other side, the @import function will load the state from a43* buffer of this size as well.44* @base: Start of data structure of cipher algorithm. The common data45* structure of crypto_alg contains information common to all ciphers.46* The hash_alg_common data structure now adds the hash-specific47* information.48*/49#define HASH_ALG_COMMON { \50unsigned int digestsize; \51unsigned int statesize; \52\53struct crypto_alg base; \54}55struct hash_alg_common HASH_ALG_COMMON;5657struct ahash_request {58struct crypto_async_request base;5960unsigned int nbytes;61union {62struct scatterlist *src;63const u8 *svirt;64};65u8 *result;6667struct scatterlist sg_head[2];68crypto_completion_t saved_complete;69void *saved_data;7071void *__ctx[] CRYPTO_MINALIGN_ATTR;72};7374/**75* struct ahash_alg - asynchronous message digest definition76* @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the77* state of the HASH transformation at the beginning. This shall fill in78* the internal structures used during the entire duration of the whole79* transformation. No data processing happens at this point. Driver code80* implementation must not use req->result.81* @update: **[mandatory]** Push a chunk of data into the driver for transformation. This82* function actually pushes blocks of data from upper layers into the83* driver, which then passes those to the hardware as seen fit. This84* function must not finalize the HASH transformation by calculating the85* final message digest as this only adds more data into the86* transformation. This function shall not modify the transformation87* context, as this function may be called in parallel with the same88* transformation object. Data processing can happen synchronously89* [SHASH] or asynchronously [AHASH] at this point. Driver must not use90* req->result.91* For block-only algorithms, @update must return the number92* of bytes to store in the API partial block buffer.93* @final: **[mandatory]** Retrieve result from the driver. This function finalizes the94* transformation and retrieves the resulting hash from the driver and95* pushes it back to upper layers. No data processing happens at this96* point unless hardware requires it to finish the transformation97* (then the data buffered by the device driver is processed).98* @finup: **[optional]** Combination of @update and @final. This function is effectively a99* combination of @update and @final calls issued in sequence. As some100* hardware cannot do @update and @final separately, this callback was101* added to allow such hardware to be used at least by IPsec. Data102* processing can happen synchronously [SHASH] or asynchronously [AHASH]103* at this point.104* @digest: Combination of @init and @update and @final. This function105* effectively behaves as the entire chain of operations, @init,106* @update and @final issued in sequence. Just like @finup, this was107* added for hardware which cannot do even the @finup, but can only do108* the whole transformation in one run. Data processing can happen109* synchronously [SHASH] or asynchronously [AHASH] at this point.110* @setkey: Set optional key used by the hashing algorithm. Intended to push111* optional key used by the hashing algorithm from upper layers into112* the driver. This function can store the key in the transformation113* context or can outright program it into the hardware. In the former114* case, one must be careful to program the key into the hardware at115* appropriate time and one must be careful that .setkey() can be116* called multiple times during the existence of the transformation117* object. Not all hashing algorithms do implement this function as it118* is only needed for keyed message digests. SHAx/MDx/CRCx do NOT119* implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement120* this function. This function must be called before any other of the121* @init, @update, @final, @finup, @digest is called. No data122* processing happens at this point.123* @export: Export partial state of the transformation. This function dumps the124* entire state of the ongoing transformation into a provided block of125* data so it can be @import 'ed back later on. This is useful in case126* you want to save partial result of the transformation after127* processing certain amount of data and reload this partial result128* multiple times later on for multiple re-use. No data processing129* happens at this point. Driver must not use req->result.130* @import: Import partial state of the transformation. This function loads the131* entire state of the ongoing transformation from a provided block of132* data so the transformation can continue from this point onward. No133* data processing happens at this point. Driver must not use134* req->result.135* @export_core: Export partial state without partial block. Only defined136* for algorithms that are not block-only.137* @import_core: Import partial state without partial block. Only defined138* for algorithms that are not block-only.139* @init_tfm: Initialize the cryptographic transformation object.140* This function is called only once at the instantiation141* time, right after the transformation context was142* allocated. In case the cryptographic hardware has143* some special requirements which need to be handled144* by software, this function shall check for the precise145* requirement of the transformation and put any software146* fallbacks in place.147* @exit_tfm: Deinitialize the cryptographic transformation object.148* This is a counterpart to @init_tfm, used to remove149* various changes set in @init_tfm.150* @clone_tfm: Copy transform into new object, may allocate memory.151* @halg: see struct hash_alg_common152*/153struct ahash_alg {154int (*init)(struct ahash_request *req);155int (*update)(struct ahash_request *req);156int (*final)(struct ahash_request *req);157int (*finup)(struct ahash_request *req);158int (*digest)(struct ahash_request *req);159int (*export)(struct ahash_request *req, void *out);160int (*import)(struct ahash_request *req, const void *in);161int (*export_core)(struct ahash_request *req, void *out);162int (*import_core)(struct ahash_request *req, const void *in);163int (*setkey)(struct crypto_ahash *tfm, const u8 *key,164unsigned int keylen);165int (*init_tfm)(struct crypto_ahash *tfm);166void (*exit_tfm)(struct crypto_ahash *tfm);167int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src);168169struct hash_alg_common halg;170};171172struct shash_desc {173struct crypto_shash *tfm;174void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);175};176177#define HASH_MAX_DIGESTSIZE 64178179/* Worst case is sha3-224. */180#define HASH_MAX_STATESIZE 200 + 144 + 1181182/*183* Worst case is hmac(sha3-224-s390). Its context is a nested 'shash_desc'184* containing a 'struct s390_sha_ctx'.185*/186#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 361)187#define MAX_SYNC_HASH_REQSIZE (sizeof(struct ahash_request) + \188HASH_MAX_DESCSIZE)189190#define SHASH_DESC_ON_STACK(shash, ctx) \191char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \192__aligned(__alignof__(struct shash_desc)); \193struct shash_desc *shash = (struct shash_desc *)__##shash##_desc194195#define HASH_REQUEST_ON_STACK(name, _tfm) \196char __##name##_req[sizeof(struct ahash_request) + \197MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \198struct ahash_request *name = \199ahash_request_on_stack_init(__##name##_req, (_tfm))200201#define HASH_REQUEST_CLONE(name, gfp) \202hash_request_clone(name, sizeof(__##name##_req), gfp)203204#define CRYPTO_HASH_STATESIZE(coresize, blocksize) (coresize + blocksize + 1)205206/**207* struct shash_alg - synchronous message digest definition208* @init: see struct ahash_alg209* @update: see struct ahash_alg210* @final: see struct ahash_alg211* @finup: see struct ahash_alg212* @digest: see struct ahash_alg213* @export: see struct ahash_alg214* @import: see struct ahash_alg215* @export_core: see struct ahash_alg216* @import_core: see struct ahash_alg217* @setkey: see struct ahash_alg218* @init_tfm: Initialize the cryptographic transformation object.219* This function is called only once at the instantiation220* time, right after the transformation context was221* allocated. In case the cryptographic hardware has222* some special requirements which need to be handled223* by software, this function shall check for the precise224* requirement of the transformation and put any software225* fallbacks in place.226* @exit_tfm: Deinitialize the cryptographic transformation object.227* This is a counterpart to @init_tfm, used to remove228* various changes set in @init_tfm.229* @clone_tfm: Copy transform into new object, may allocate memory.230* @descsize: Size of the operational state for the message digest. This state231* size is the memory size that needs to be allocated for232* shash_desc.__ctx233* @halg: see struct hash_alg_common234* @HASH_ALG_COMMON: see struct hash_alg_common235*/236struct shash_alg {237int (*init)(struct shash_desc *desc);238int (*update)(struct shash_desc *desc, const u8 *data,239unsigned int len);240int (*final)(struct shash_desc *desc, u8 *out);241int (*finup)(struct shash_desc *desc, const u8 *data,242unsigned int len, u8 *out);243int (*digest)(struct shash_desc *desc, const u8 *data,244unsigned int len, u8 *out);245int (*export)(struct shash_desc *desc, void *out);246int (*import)(struct shash_desc *desc, const void *in);247int (*export_core)(struct shash_desc *desc, void *out);248int (*import_core)(struct shash_desc *desc, const void *in);249int (*setkey)(struct crypto_shash *tfm, const u8 *key,250unsigned int keylen);251int (*init_tfm)(struct crypto_shash *tfm);252void (*exit_tfm)(struct crypto_shash *tfm);253int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);254255unsigned int descsize;256257union {258struct HASH_ALG_COMMON;259struct hash_alg_common halg;260};261};262#undef HASH_ALG_COMMON263264struct crypto_ahash {265bool using_shash; /* Underlying algorithm is shash, not ahash */266unsigned int statesize;267unsigned int reqsize;268struct crypto_tfm base;269};270271struct crypto_shash {272struct crypto_tfm base;273};274275/**276* DOC: Asynchronous Message Digest API277*278* The asynchronous message digest API is used with the ciphers of type279* CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)280*281* The asynchronous cipher operation discussion provided for the282* CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.283*/284285static inline bool ahash_req_on_stack(struct ahash_request *req)286{287return crypto_req_on_stack(&req->base);288}289290static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)291{292return container_of(tfm, struct crypto_ahash, base);293}294295/**296* crypto_alloc_ahash() - allocate ahash cipher handle297* @alg_name: is the cra_name / name or cra_driver_name / driver name of the298* ahash cipher299* @type: specifies the type of the cipher300* @mask: specifies the mask for the cipher301*302* Allocate a cipher handle for an ahash. The returned struct303* crypto_ahash is the cipher handle that is required for any subsequent304* API invocation for that ahash.305*306* Return: allocated cipher handle in case of success; IS_ERR() is true in case307* of an error, PTR_ERR() returns the error code.308*/309struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,310u32 mask);311312struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);313314static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)315{316return &tfm->base;317}318319/**320* crypto_free_ahash() - zeroize and free the ahash handle321* @tfm: cipher handle to be freed322*323* If @tfm is a NULL or error pointer, this function does nothing.324*/325static inline void crypto_free_ahash(struct crypto_ahash *tfm)326{327crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));328}329330/**331* crypto_has_ahash() - Search for the availability of an ahash.332* @alg_name: is the cra_name / name or cra_driver_name / driver name of the333* ahash334* @type: specifies the type of the ahash335* @mask: specifies the mask for the ahash336*337* Return: true when the ahash is known to the kernel crypto API; false338* otherwise339*/340int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);341342static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)343{344return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));345}346347static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)348{349return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));350}351352/**353* crypto_ahash_blocksize() - obtain block size for cipher354* @tfm: cipher handle355*356* The block size for the message digest cipher referenced with the cipher357* handle is returned.358*359* Return: block size of cipher360*/361static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)362{363return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));364}365366static inline struct hash_alg_common *__crypto_hash_alg_common(367struct crypto_alg *alg)368{369return container_of(alg, struct hash_alg_common, base);370}371372static inline struct hash_alg_common *crypto_hash_alg_common(373struct crypto_ahash *tfm)374{375return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);376}377378/**379* crypto_ahash_digestsize() - obtain message digest size380* @tfm: cipher handle381*382* The size for the message digest created by the message digest cipher383* referenced with the cipher handle is returned.384*385*386* Return: message digest size of cipher387*/388static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)389{390return crypto_hash_alg_common(tfm)->digestsize;391}392393/**394* crypto_ahash_statesize() - obtain size of the ahash state395* @tfm: cipher handle396*397* Return the size of the ahash state. With the crypto_ahash_export()398* function, the caller can export the state into a buffer whose size is399* defined with this function.400*401* Return: size of the ahash state402*/403static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)404{405return tfm->statesize;406}407408static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)409{410return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));411}412413static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)414{415crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);416}417418static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)419{420crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);421}422423/**424* crypto_ahash_reqtfm() - obtain cipher handle from request425* @req: asynchronous request handle that contains the reference to the ahash426* cipher handle427*428* Return the ahash cipher handle that is registered with the asynchronous429* request handle ahash_request.430*431* Return: ahash cipher handle432*/433static inline struct crypto_ahash *crypto_ahash_reqtfm(434struct ahash_request *req)435{436return __crypto_ahash_cast(req->base.tfm);437}438439/**440* crypto_ahash_reqsize() - obtain size of the request data structure441* @tfm: cipher handle442*443* Return: size of the request data444*/445static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)446{447return tfm->reqsize;448}449450static inline void *ahash_request_ctx(struct ahash_request *req)451{452return req->__ctx;453}454455/**456* crypto_ahash_setkey - set key for cipher handle457* @tfm: cipher handle458* @key: buffer holding the key459* @keylen: length of the key in bytes460*461* The caller provided key is set for the ahash cipher. The cipher462* handle must point to a keyed hash in order for this function to succeed.463*464* Return: 0 if the setting of the key was successful; < 0 if an error occurred465*/466int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,467unsigned int keylen);468469/**470* crypto_ahash_finup() - update and finalize message digest471* @req: reference to the ahash_request handle that holds all information472* needed to perform the cipher operation473*474* This function is a "short-hand" for the function calls of475* crypto_ahash_update and crypto_ahash_final. The parameters have the same476* meaning as discussed for those separate functions.477*478* Return: see crypto_ahash_final()479*/480int crypto_ahash_finup(struct ahash_request *req);481482/**483* crypto_ahash_final() - calculate message digest484* @req: reference to the ahash_request handle that holds all information485* needed to perform the cipher operation486*487* Finalize the message digest operation and create the message digest488* based on all data added to the cipher handle. The message digest is placed489* into the output buffer registered with the ahash_request handle.490*491* Return:492* 0 if the message digest was successfully calculated;493* -EINPROGRESS if data is fed into hardware (DMA) or queued for later;494* -EBUSY if queue is full and request should be resubmitted later;495* other < 0 if an error occurred496*/497static inline int crypto_ahash_final(struct ahash_request *req)498{499req->nbytes = 0;500return crypto_ahash_finup(req);501}502503/**504* crypto_ahash_digest() - calculate message digest for a buffer505* @req: reference to the ahash_request handle that holds all information506* needed to perform the cipher operation507*508* This function is a "short-hand" for the function calls of crypto_ahash_init,509* crypto_ahash_update and crypto_ahash_final. The parameters have the same510* meaning as discussed for those separate three functions.511*512* Return: see crypto_ahash_final()513*/514int crypto_ahash_digest(struct ahash_request *req);515516/**517* crypto_ahash_export() - extract current message digest state518* @req: reference to the ahash_request handle whose state is exported519* @out: output buffer of sufficient size that can hold the hash state520*521* This function exports the hash state of the ahash_request handle into the522* caller-allocated output buffer out which must have sufficient size (e.g. by523* calling crypto_ahash_statesize()).524*525* Return: 0 if the export was successful; < 0 if an error occurred526*/527int crypto_ahash_export(struct ahash_request *req, void *out);528529/**530* crypto_ahash_import() - import message digest state531* @req: reference to ahash_request handle the state is imported into532* @in: buffer holding the state533*534* This function imports the hash state into the ahash_request handle from the535* input buffer. That buffer should have been generated with the536* crypto_ahash_export function.537*538* Return: 0 if the import was successful; < 0 if an error occurred539*/540int crypto_ahash_import(struct ahash_request *req, const void *in);541542/**543* crypto_ahash_init() - (re)initialize message digest handle544* @req: ahash_request handle that already is initialized with all necessary545* data using the ahash_request_* API functions546*547* The call (re-)initializes the message digest referenced by the ahash_request548* handle. Any potentially existing state created by previous operations is549* discarded.550*551* Return: see crypto_ahash_final()552*/553int crypto_ahash_init(struct ahash_request *req);554555/**556* crypto_ahash_update() - add data to message digest for processing557* @req: ahash_request handle that was previously initialized with the558* crypto_ahash_init call.559*560* Updates the message digest state of the &ahash_request handle. The input data561* is pointed to by the scatter/gather list registered in the &ahash_request562* handle563*564* Return: see crypto_ahash_final()565*/566int crypto_ahash_update(struct ahash_request *req);567568/**569* DOC: Asynchronous Hash Request Handle570*571* The &ahash_request data structure contains all pointers to data572* required for the asynchronous cipher operation. This includes the cipher573* handle (which can be used by multiple &ahash_request instances), pointer574* to plaintext and the message digest output buffer, asynchronous callback575* function, etc. It acts as a handle to the ahash_request_* API calls in a576* similar way as ahash handle to the crypto_ahash_* API calls.577*/578579/**580* ahash_request_set_tfm() - update cipher handle reference in request581* @req: request handle to be modified582* @tfm: cipher handle that shall be added to the request handle583*584* Allow the caller to replace the existing ahash handle in the request585* data structure with a different one.586*/587static inline void ahash_request_set_tfm(struct ahash_request *req,588struct crypto_ahash *tfm)589{590crypto_request_set_tfm(&req->base, crypto_ahash_tfm(tfm));591}592593/**594* ahash_request_alloc() - allocate request data structure595* @tfm: cipher handle to be registered with the request596* @gfp: memory allocation flag that is handed to kmalloc by the API call.597*598* Allocate the request data structure that must be used with the ahash599* message digest API calls. During600* the allocation, the provided ahash handle601* is registered in the request data structure.602*603* Return: allocated request handle in case of success, or NULL if out of memory604*/605static inline struct ahash_request *ahash_request_alloc_noprof(606struct crypto_ahash *tfm, gfp_t gfp)607{608struct ahash_request *req;609610req = kmalloc_noprof(sizeof(struct ahash_request) +611crypto_ahash_reqsize(tfm), gfp);612613if (likely(req))614ahash_request_set_tfm(req, tfm);615616return req;617}618#define ahash_request_alloc(...) alloc_hooks(ahash_request_alloc_noprof(__VA_ARGS__))619620/**621* ahash_request_free() - zeroize and free the request data structure622* @req: request data structure cipher handle to be freed623*/624void ahash_request_free(struct ahash_request *req);625626static inline void ahash_request_zero(struct ahash_request *req)627{628memzero_explicit(req, sizeof(*req) +629crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));630}631632static inline struct ahash_request *ahash_request_cast(633struct crypto_async_request *req)634{635return container_of(req, struct ahash_request, base);636}637638/**639* ahash_request_set_callback() - set asynchronous callback function640* @req: request handle641* @flags: specify zero or an ORing of the flags642* CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and643* increase the wait queue beyond the initial maximum size;644* CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep645* @compl: callback function pointer to be registered with the request handle646* @data: The data pointer refers to memory that is not used by the kernel647* crypto API, but provided to the callback function for it to use. Here,648* the caller can provide a reference to memory the callback function can649* operate on. As the callback function is invoked asynchronously to the650* related functionality, it may need to access data structures of the651* related functionality which can be referenced using this pointer. The652* callback function can access the memory via the "data" field in the653* &crypto_async_request data structure provided to the callback function.654*655* This function allows setting the callback function that is triggered once656* the cipher operation completes.657*658* The callback function is registered with the &ahash_request handle and659* must comply with the following template::660*661* void callback_function(struct crypto_async_request *req, int error)662*/663static inline void ahash_request_set_callback(struct ahash_request *req,664u32 flags,665crypto_completion_t compl,666void *data)667{668flags &= ~CRYPTO_AHASH_REQ_PRIVATE;669flags |= req->base.flags & CRYPTO_AHASH_REQ_PRIVATE;670crypto_request_set_callback(&req->base, flags, compl, data);671}672673/**674* ahash_request_set_crypt() - set data buffers675* @req: ahash_request handle to be updated676* @src: source scatter/gather list677* @result: buffer that is filled with the message digest -- the caller must678* ensure that the buffer has sufficient space by, for example, calling679* crypto_ahash_digestsize()680* @nbytes: number of bytes to process from the source scatter/gather list681*682* By using this call, the caller references the source scatter/gather list.683* The source scatter/gather list points to the data the message digest is to684* be calculated for.685*/686static inline void ahash_request_set_crypt(struct ahash_request *req,687struct scatterlist *src, u8 *result,688unsigned int nbytes)689{690req->src = src;691req->nbytes = nbytes;692req->result = result;693req->base.flags &= ~CRYPTO_AHASH_REQ_VIRT;694}695696/**697* ahash_request_set_virt() - set virtual address data buffers698* @req: ahash_request handle to be updated699* @src: source virtual address700* @result: buffer that is filled with the message digest -- the caller must701* ensure that the buffer has sufficient space by, for example, calling702* crypto_ahash_digestsize()703* @nbytes: number of bytes to process from the source virtual address704*705* By using this call, the caller references the source virtual address.706* The source virtual address points to the data the message digest is to707* be calculated for.708*/709static inline void ahash_request_set_virt(struct ahash_request *req,710const u8 *src, u8 *result,711unsigned int nbytes)712{713req->svirt = src;714req->nbytes = nbytes;715req->result = result;716req->base.flags |= CRYPTO_AHASH_REQ_VIRT;717}718719/**720* DOC: Synchronous Message Digest API721*722* The synchronous message digest API is used with the ciphers of type723* CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)724*725* The message digest API is able to maintain state information for the726* caller.727*728* The synchronous message digest API can store user-related context in its729* shash_desc request data structure.730*/731732/**733* crypto_alloc_shash() - allocate message digest handle734* @alg_name: is the cra_name / name or cra_driver_name / driver name of the735* message digest cipher736* @type: specifies the type of the cipher737* @mask: specifies the mask for the cipher738*739* Allocate a cipher handle for a message digest. The returned &struct740* crypto_shash is the cipher handle that is required for any subsequent741* API invocation for that message digest.742*743* Return: allocated cipher handle in case of success; IS_ERR() is true in case744* of an error, PTR_ERR() returns the error code.745*/746struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,747u32 mask);748749struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);750751int crypto_has_shash(const char *alg_name, u32 type, u32 mask);752753static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)754{755return &tfm->base;756}757758/**759* crypto_free_shash() - zeroize and free the message digest handle760* @tfm: cipher handle to be freed761*762* If @tfm is a NULL or error pointer, this function does nothing.763*/764static inline void crypto_free_shash(struct crypto_shash *tfm)765{766crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));767}768769static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)770{771return crypto_tfm_alg_name(crypto_shash_tfm(tfm));772}773774static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)775{776return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));777}778779/**780* crypto_shash_blocksize() - obtain block size for cipher781* @tfm: cipher handle782*783* The block size for the message digest cipher referenced with the cipher784* handle is returned.785*786* Return: block size of cipher787*/788static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)789{790return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));791}792793static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)794{795return container_of(alg, struct shash_alg, base);796}797798static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)799{800return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);801}802803/**804* crypto_shash_digestsize() - obtain message digest size805* @tfm: cipher handle806*807* The size for the message digest created by the message digest cipher808* referenced with the cipher handle is returned.809*810* Return: digest size of cipher811*/812static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)813{814return crypto_shash_alg(tfm)->digestsize;815}816817static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)818{819return crypto_shash_alg(tfm)->statesize;820}821822static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)823{824return crypto_tfm_get_flags(crypto_shash_tfm(tfm));825}826827static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)828{829crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);830}831832static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)833{834crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);835}836837/**838* crypto_shash_descsize() - obtain the operational state size839* @tfm: cipher handle840*841* The size of the operational state the cipher needs during operation is842* returned for the hash referenced with the cipher handle. This size is843* required to calculate the memory requirements to allow the caller allocating844* sufficient memory for operational state.845*846* The operational state is defined with struct shash_desc where the size of847* that data structure is to be calculated as848* sizeof(struct shash_desc) + crypto_shash_descsize(alg)849*850* Return: size of the operational state851*/852static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)853{854return crypto_shash_alg(tfm)->descsize;855}856857static inline void *shash_desc_ctx(struct shash_desc *desc)858{859return desc->__ctx;860}861862/**863* crypto_shash_setkey() - set key for message digest864* @tfm: cipher handle865* @key: buffer holding the key866* @keylen: length of the key in bytes867*868* The caller provided key is set for the keyed message digest cipher. The869* cipher handle must point to a keyed message digest cipher in order for this870* function to succeed.871*872* Context: Softirq or process context.873* Return: 0 if the setting of the key was successful; < 0 if an error occurred874*/875int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,876unsigned int keylen);877878/**879* crypto_shash_digest() - calculate message digest for buffer880* @desc: see crypto_shash_final()881* @data: see crypto_shash_update()882* @len: see crypto_shash_update()883* @out: see crypto_shash_final()884*885* This function is a "short-hand" for the function calls of crypto_shash_init,886* crypto_shash_update and crypto_shash_final. The parameters have the same887* meaning as discussed for those separate three functions.888*889* Context: Softirq or process context.890* Return: 0 if the message digest creation was successful; < 0 if an error891* occurred892*/893int crypto_shash_digest(struct shash_desc *desc, const u8 *data,894unsigned int len, u8 *out);895896/**897* crypto_shash_tfm_digest() - calculate message digest for buffer898* @tfm: hash transformation object899* @data: see crypto_shash_update()900* @len: see crypto_shash_update()901* @out: see crypto_shash_final()902*903* This is a simplified version of crypto_shash_digest() for users who don't904* want to allocate their own hash descriptor (shash_desc). Instead,905* crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)906* directly, and it allocates a hash descriptor on the stack internally.907* Note that this stack allocation may be fairly large.908*909* Context: Softirq or process context.910* Return: 0 on success; < 0 if an error occurred.911*/912int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,913unsigned int len, u8 *out);914915int crypto_hash_digest(struct crypto_ahash *tfm, const u8 *data,916unsigned int len, u8 *out);917918/**919* crypto_shash_export() - extract operational state for message digest920* @desc: reference to the operational state handle whose state is exported921* @out: output buffer of sufficient size that can hold the hash state922*923* This function exports the hash state of the operational state handle into the924* caller-allocated output buffer out which must have sufficient size (e.g. by925* calling crypto_shash_descsize).926*927* Context: Softirq or process context.928* Return: 0 if the export creation was successful; < 0 if an error occurred929*/930int crypto_shash_export(struct shash_desc *desc, void *out);931932/**933* crypto_shash_import() - import operational state934* @desc: reference to the operational state handle the state imported into935* @in: buffer holding the state936*937* This function imports the hash state into the operational state handle from938* the input buffer. That buffer should have been generated with the939* crypto_ahash_export function.940*941* Context: Softirq or process context.942* Return: 0 if the import was successful; < 0 if an error occurred943*/944int crypto_shash_import(struct shash_desc *desc, const void *in);945946/**947* crypto_shash_init() - (re)initialize message digest948* @desc: operational state handle that is already filled949*950* The call (re-)initializes the message digest referenced by the951* operational state handle. Any potentially existing state created by952* previous operations is discarded.953*954* Context: Softirq or process context.955* Return: 0 if the message digest initialization was successful; < 0 if an956* error occurred957*/958int crypto_shash_init(struct shash_desc *desc);959960/**961* crypto_shash_finup() - calculate message digest of buffer962* @desc: see crypto_shash_final()963* @data: see crypto_shash_update()964* @len: see crypto_shash_update()965* @out: see crypto_shash_final()966*967* This function is a "short-hand" for the function calls of968* crypto_shash_update and crypto_shash_final. The parameters have the same969* meaning as discussed for those separate functions.970*971* Context: Softirq or process context.972* Return: 0 if the message digest creation was successful; < 0 if an error973* occurred974*/975int crypto_shash_finup(struct shash_desc *desc, const u8 *data,976unsigned int len, u8 *out);977978/**979* crypto_shash_update() - add data to message digest for processing980* @desc: operational state handle that is already initialized981* @data: input data to be added to the message digest982* @len: length of the input data983*984* Updates the message digest state of the operational state handle.985*986* Context: Softirq or process context.987* Return: 0 if the message digest update was successful; < 0 if an error988* occurred989*/990static inline int crypto_shash_update(struct shash_desc *desc, const u8 *data,991unsigned int len)992{993return crypto_shash_finup(desc, data, len, NULL);994}995996/**997* crypto_shash_final() - calculate message digest998* @desc: operational state handle that is already filled with data999* @out: output buffer filled with the message digest1000*1001* Finalize the message digest operation and create the message digest1002* based on all data added to the cipher handle. The message digest is placed1003* into the output buffer. The caller must ensure that the output buffer is1004* large enough by using crypto_shash_digestsize.1005*1006* Context: Softirq or process context.1007* Return: 0 if the message digest creation was successful; < 0 if an error1008* occurred1009*/1010static inline int crypto_shash_final(struct shash_desc *desc, u8 *out)1011{1012return crypto_shash_finup(desc, NULL, 0, out);1013}10141015static inline void shash_desc_zero(struct shash_desc *desc)1016{1017memzero_explicit(desc,1018sizeof(*desc) + crypto_shash_descsize(desc->tfm));1019}10201021static inline bool ahash_is_async(struct crypto_ahash *tfm)1022{1023return crypto_tfm_is_async(&tfm->base);1024}10251026static inline struct ahash_request *ahash_request_on_stack_init(1027char *buf, struct crypto_ahash *tfm)1028{1029struct ahash_request *req = (void *)buf;10301031crypto_stack_request_init(&req->base, crypto_ahash_tfm(tfm));1032return req;1033}10341035static inline struct ahash_request *ahash_request_clone(1036struct ahash_request *req, size_t total, gfp_t gfp)1037{1038return container_of(crypto_request_clone(&req->base, total, gfp),1039struct ahash_request, base);1040}10411042#endif /* _CRYPTO_HASH_H */104310441045