/* 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/*180* The size of a core hash state and a partial block. The final byte181* is the length of the partial block.182*/183#define HASH_STATE_AND_BLOCK(state, block) ((state) + (block) + 1)184185186/* Worst case is sha3-224. */187#define HASH_MAX_STATESIZE HASH_STATE_AND_BLOCK(200, 144)188189/* This needs to match arch/s390/crypto/sha.h. */190#define S390_SHA_CTX_SIZE 216191192/*193* Worst case is hmac(sha3-224-s390). Its context is a nested 'shash_desc'194* containing a 'struct s390_sha_ctx'.195*/196#define SHA3_224_S390_DESCSIZE HASH_STATE_AND_BLOCK(S390_SHA_CTX_SIZE, 144)197#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + \198SHA3_224_S390_DESCSIZE)199#define MAX_SYNC_HASH_REQSIZE (sizeof(struct ahash_request) + \200HASH_MAX_DESCSIZE)201202#define SHASH_DESC_ON_STACK(shash, ctx) \203char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \204__aligned(__alignof__(struct shash_desc)); \205struct shash_desc *shash = (struct shash_desc *)__##shash##_desc206207#define HASH_REQUEST_ON_STACK(name, _tfm) \208char __##name##_req[sizeof(struct ahash_request) + \209MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \210struct ahash_request *name = \211ahash_request_on_stack_init(__##name##_req, (_tfm))212213#define HASH_REQUEST_CLONE(name, gfp) \214hash_request_clone(name, sizeof(__##name##_req), gfp)215216#define CRYPTO_HASH_STATESIZE(coresize, blocksize) (coresize + blocksize + 1)217218/**219* struct shash_alg - synchronous message digest definition220* @init: see struct ahash_alg221* @update: see struct ahash_alg222* @final: see struct ahash_alg223* @finup: see struct ahash_alg224* @digest: see struct ahash_alg225* @export: see struct ahash_alg226* @import: see struct ahash_alg227* @export_core: see struct ahash_alg228* @import_core: see struct ahash_alg229* @setkey: see struct ahash_alg230* @init_tfm: Initialize the cryptographic transformation object.231* This function is called only once at the instantiation232* time, right after the transformation context was233* allocated. In case the cryptographic hardware has234* some special requirements which need to be handled235* by software, this function shall check for the precise236* requirement of the transformation and put any software237* fallbacks in place.238* @exit_tfm: Deinitialize the cryptographic transformation object.239* This is a counterpart to @init_tfm, used to remove240* various changes set in @init_tfm.241* @clone_tfm: Copy transform into new object, may allocate memory.242* @descsize: Size of the operational state for the message digest. This state243* size is the memory size that needs to be allocated for244* shash_desc.__ctx245* @halg: see struct hash_alg_common246* @HASH_ALG_COMMON: see struct hash_alg_common247*/248struct shash_alg {249int (*init)(struct shash_desc *desc);250int (*update)(struct shash_desc *desc, const u8 *data,251unsigned int len);252int (*final)(struct shash_desc *desc, u8 *out);253int (*finup)(struct shash_desc *desc, const u8 *data,254unsigned int len, u8 *out);255int (*digest)(struct shash_desc *desc, const u8 *data,256unsigned int len, u8 *out);257int (*export)(struct shash_desc *desc, void *out);258int (*import)(struct shash_desc *desc, const void *in);259int (*export_core)(struct shash_desc *desc, void *out);260int (*import_core)(struct shash_desc *desc, const void *in);261int (*setkey)(struct crypto_shash *tfm, const u8 *key,262unsigned int keylen);263int (*init_tfm)(struct crypto_shash *tfm);264void (*exit_tfm)(struct crypto_shash *tfm);265int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src);266267unsigned int descsize;268269union {270struct HASH_ALG_COMMON;271struct hash_alg_common halg;272};273};274#undef HASH_ALG_COMMON275276struct crypto_ahash {277bool using_shash; /* Underlying algorithm is shash, not ahash */278unsigned int statesize;279unsigned int reqsize;280struct crypto_tfm base;281};282283struct crypto_shash {284struct crypto_tfm base;285};286287/**288* DOC: Asynchronous Message Digest API289*290* The asynchronous message digest API is used with the ciphers of type291* CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)292*293* The asynchronous cipher operation discussion provided for the294* CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.295*/296297static inline bool ahash_req_on_stack(struct ahash_request *req)298{299return crypto_req_on_stack(&req->base);300}301302static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)303{304return container_of(tfm, struct crypto_ahash, base);305}306307/**308* crypto_alloc_ahash() - allocate ahash cipher handle309* @alg_name: is the cra_name / name or cra_driver_name / driver name of the310* ahash cipher311* @type: specifies the type of the cipher312* @mask: specifies the mask for the cipher313*314* Allocate a cipher handle for an ahash. The returned struct315* crypto_ahash is the cipher handle that is required for any subsequent316* API invocation for that ahash.317*318* Return: allocated cipher handle in case of success; IS_ERR() is true in case319* of an error, PTR_ERR() returns the error code.320*/321struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,322u32 mask);323324struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm);325326static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)327{328return &tfm->base;329}330331/**332* crypto_free_ahash() - zeroize and free the ahash handle333* @tfm: cipher handle to be freed334*335* If @tfm is a NULL or error pointer, this function does nothing.336*/337static inline void crypto_free_ahash(struct crypto_ahash *tfm)338{339crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));340}341342/**343* crypto_has_ahash() - Search for the availability of an ahash.344* @alg_name: is the cra_name / name or cra_driver_name / driver name of the345* ahash346* @type: specifies the type of the ahash347* @mask: specifies the mask for the ahash348*349* Return: true when the ahash is known to the kernel crypto API; false350* otherwise351*/352int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);353354static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)355{356return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));357}358359static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)360{361return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));362}363364/**365* crypto_ahash_blocksize() - obtain block size for cipher366* @tfm: cipher handle367*368* The block size for the message digest cipher referenced with the cipher369* handle is returned.370*371* Return: block size of cipher372*/373static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)374{375return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));376}377378static inline struct hash_alg_common *__crypto_hash_alg_common(379struct crypto_alg *alg)380{381return container_of(alg, struct hash_alg_common, base);382}383384static inline struct hash_alg_common *crypto_hash_alg_common(385struct crypto_ahash *tfm)386{387return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);388}389390/**391* crypto_ahash_digestsize() - obtain message digest size392* @tfm: cipher handle393*394* The size for the message digest created by the message digest cipher395* referenced with the cipher handle is returned.396*397*398* Return: message digest size of cipher399*/400static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)401{402return crypto_hash_alg_common(tfm)->digestsize;403}404405/**406* crypto_ahash_statesize() - obtain size of the ahash state407* @tfm: cipher handle408*409* Return the size of the ahash state. With the crypto_ahash_export()410* function, the caller can export the state into a buffer whose size is411* defined with this function.412*413* Return: size of the ahash state414*/415static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)416{417return tfm->statesize;418}419420static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)421{422return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));423}424425static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)426{427crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);428}429430static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)431{432crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);433}434435/**436* crypto_ahash_reqtfm() - obtain cipher handle from request437* @req: asynchronous request handle that contains the reference to the ahash438* cipher handle439*440* Return the ahash cipher handle that is registered with the asynchronous441* request handle ahash_request.442*443* Return: ahash cipher handle444*/445static inline struct crypto_ahash *crypto_ahash_reqtfm(446struct ahash_request *req)447{448return __crypto_ahash_cast(req->base.tfm);449}450451/**452* crypto_ahash_reqsize() - obtain size of the request data structure453* @tfm: cipher handle454*455* Return: size of the request data456*/457static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)458{459return tfm->reqsize;460}461462static inline void *ahash_request_ctx(struct ahash_request *req)463{464return req->__ctx;465}466467/**468* crypto_ahash_setkey - set key for cipher handle469* @tfm: cipher handle470* @key: buffer holding the key471* @keylen: length of the key in bytes472*473* The caller provided key is set for the ahash cipher. The cipher474* handle must point to a keyed hash in order for this function to succeed.475*476* Return: 0 if the setting of the key was successful; < 0 if an error occurred477*/478int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,479unsigned int keylen);480481/**482* crypto_ahash_finup() - update and finalize message digest483* @req: reference to the ahash_request handle that holds all information484* needed to perform the cipher operation485*486* This function is a "short-hand" for the function calls of487* crypto_ahash_update and crypto_ahash_final. The parameters have the same488* meaning as discussed for those separate functions.489*490* Return: see crypto_ahash_final()491*/492int crypto_ahash_finup(struct ahash_request *req);493494/**495* crypto_ahash_final() - calculate message digest496* @req: reference to the ahash_request handle that holds all information497* needed to perform the cipher operation498*499* Finalize the message digest operation and create the message digest500* based on all data added to the cipher handle. The message digest is placed501* into the output buffer registered with the ahash_request handle.502*503* Return:504* 0 if the message digest was successfully calculated;505* -EINPROGRESS if data is fed into hardware (DMA) or queued for later;506* -EBUSY if queue is full and request should be resubmitted later;507* other < 0 if an error occurred508*/509static inline int crypto_ahash_final(struct ahash_request *req)510{511req->nbytes = 0;512return crypto_ahash_finup(req);513}514515/**516* crypto_ahash_digest() - calculate message digest for a buffer517* @req: reference to the ahash_request handle that holds all information518* needed to perform the cipher operation519*520* This function is a "short-hand" for the function calls of crypto_ahash_init,521* crypto_ahash_update and crypto_ahash_final. The parameters have the same522* meaning as discussed for those separate three functions.523*524* Return: see crypto_ahash_final()525*/526int crypto_ahash_digest(struct ahash_request *req);527528/**529* crypto_ahash_export() - extract current message digest state530* @req: reference to the ahash_request handle whose state is exported531* @out: output buffer of sufficient size that can hold the hash state532*533* This function exports the hash state of the ahash_request handle into the534* caller-allocated output buffer out which must have sufficient size (e.g. by535* calling crypto_ahash_statesize()).536*537* Return: 0 if the export was successful; < 0 if an error occurred538*/539int crypto_ahash_export(struct ahash_request *req, void *out);540541/**542* crypto_ahash_import() - import message digest state543* @req: reference to ahash_request handle the state is imported into544* @in: buffer holding the state545*546* This function imports the hash state into the ahash_request handle from the547* input buffer. That buffer should have been generated with the548* crypto_ahash_export function.549*550* Return: 0 if the import was successful; < 0 if an error occurred551*/552int crypto_ahash_import(struct ahash_request *req, const void *in);553554/**555* crypto_ahash_init() - (re)initialize message digest handle556* @req: ahash_request handle that already is initialized with all necessary557* data using the ahash_request_* API functions558*559* The call (re-)initializes the message digest referenced by the ahash_request560* handle. Any potentially existing state created by previous operations is561* discarded.562*563* Return: see crypto_ahash_final()564*/565int crypto_ahash_init(struct ahash_request *req);566567/**568* crypto_ahash_update() - add data to message digest for processing569* @req: ahash_request handle that was previously initialized with the570* crypto_ahash_init call.571*572* Updates the message digest state of the &ahash_request handle. The input data573* is pointed to by the scatter/gather list registered in the &ahash_request574* handle575*576* Return: see crypto_ahash_final()577*/578int crypto_ahash_update(struct ahash_request *req);579580/**581* DOC: Asynchronous Hash Request Handle582*583* The &ahash_request data structure contains all pointers to data584* required for the asynchronous cipher operation. This includes the cipher585* handle (which can be used by multiple &ahash_request instances), pointer586* to plaintext and the message digest output buffer, asynchronous callback587* function, etc. It acts as a handle to the ahash_request_* API calls in a588* similar way as ahash handle to the crypto_ahash_* API calls.589*/590591/**592* ahash_request_set_tfm() - update cipher handle reference in request593* @req: request handle to be modified594* @tfm: cipher handle that shall be added to the request handle595*596* Allow the caller to replace the existing ahash handle in the request597* data structure with a different one.598*/599static inline void ahash_request_set_tfm(struct ahash_request *req,600struct crypto_ahash *tfm)601{602crypto_request_set_tfm(&req->base, crypto_ahash_tfm(tfm));603}604605/**606* ahash_request_alloc() - allocate request data structure607* @tfm: cipher handle to be registered with the request608* @gfp: memory allocation flag that is handed to kmalloc by the API call.609*610* Allocate the request data structure that must be used with the ahash611* message digest API calls. During612* the allocation, the provided ahash handle613* is registered in the request data structure.614*615* Return: allocated request handle in case of success, or NULL if out of memory616*/617static inline struct ahash_request *ahash_request_alloc_noprof(618struct crypto_ahash *tfm, gfp_t gfp)619{620struct ahash_request *req;621622req = kmalloc_noprof(sizeof(struct ahash_request) +623crypto_ahash_reqsize(tfm), gfp);624625if (likely(req))626ahash_request_set_tfm(req, tfm);627628return req;629}630#define ahash_request_alloc(...) alloc_hooks(ahash_request_alloc_noprof(__VA_ARGS__))631632/**633* ahash_request_free() - zeroize and free the request data structure634* @req: request data structure cipher handle to be freed635*/636void ahash_request_free(struct ahash_request *req);637638static inline void ahash_request_zero(struct ahash_request *req)639{640memzero_explicit(req, sizeof(*req) +641crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));642}643644static inline struct ahash_request *ahash_request_cast(645struct crypto_async_request *req)646{647return container_of(req, struct ahash_request, base);648}649650/**651* ahash_request_set_callback() - set asynchronous callback function652* @req: request handle653* @flags: specify zero or an ORing of the flags654* CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and655* increase the wait queue beyond the initial maximum size;656* CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep657* @compl: callback function pointer to be registered with the request handle658* @data: The data pointer refers to memory that is not used by the kernel659* crypto API, but provided to the callback function for it to use. Here,660* the caller can provide a reference to memory the callback function can661* operate on. As the callback function is invoked asynchronously to the662* related functionality, it may need to access data structures of the663* related functionality which can be referenced using this pointer. The664* callback function can access the memory via the "data" field in the665* &crypto_async_request data structure provided to the callback function.666*667* This function allows setting the callback function that is triggered once668* the cipher operation completes.669*670* The callback function is registered with the &ahash_request handle and671* must comply with the following template::672*673* void callback_function(struct crypto_async_request *req, int error)674*/675static inline void ahash_request_set_callback(struct ahash_request *req,676u32 flags,677crypto_completion_t compl,678void *data)679{680flags &= ~CRYPTO_AHASH_REQ_PRIVATE;681flags |= req->base.flags & CRYPTO_AHASH_REQ_PRIVATE;682crypto_request_set_callback(&req->base, flags, compl, data);683}684685/**686* ahash_request_set_crypt() - set data buffers687* @req: ahash_request handle to be updated688* @src: source scatter/gather list689* @result: buffer that is filled with the message digest -- the caller must690* ensure that the buffer has sufficient space by, for example, calling691* crypto_ahash_digestsize()692* @nbytes: number of bytes to process from the source scatter/gather list693*694* By using this call, the caller references the source scatter/gather list.695* The source scatter/gather list points to the data the message digest is to696* be calculated for.697*/698static inline void ahash_request_set_crypt(struct ahash_request *req,699struct scatterlist *src, u8 *result,700unsigned int nbytes)701{702req->src = src;703req->nbytes = nbytes;704req->result = result;705req->base.flags &= ~CRYPTO_AHASH_REQ_VIRT;706}707708/**709* ahash_request_set_virt() - set virtual address data buffers710* @req: ahash_request handle to be updated711* @src: source virtual address712* @result: buffer that is filled with the message digest -- the caller must713* ensure that the buffer has sufficient space by, for example, calling714* crypto_ahash_digestsize()715* @nbytes: number of bytes to process from the source virtual address716*717* By using this call, the caller references the source virtual address.718* The source virtual address points to the data the message digest is to719* be calculated for.720*/721static inline void ahash_request_set_virt(struct ahash_request *req,722const u8 *src, u8 *result,723unsigned int nbytes)724{725req->svirt = src;726req->nbytes = nbytes;727req->result = result;728req->base.flags |= CRYPTO_AHASH_REQ_VIRT;729}730731/**732* DOC: Synchronous Message Digest API733*734* The synchronous message digest API is used with the ciphers of type735* CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)736*737* The message digest API is able to maintain state information for the738* caller.739*740* The synchronous message digest API can store user-related context in its741* shash_desc request data structure.742*/743744/**745* crypto_alloc_shash() - allocate message digest handle746* @alg_name: is the cra_name / name or cra_driver_name / driver name of the747* message digest cipher748* @type: specifies the type of the cipher749* @mask: specifies the mask for the cipher750*751* Allocate a cipher handle for a message digest. The returned &struct752* crypto_shash is the cipher handle that is required for any subsequent753* API invocation for that message digest.754*755* Return: allocated cipher handle in case of success; IS_ERR() is true in case756* of an error, PTR_ERR() returns the error code.757*/758struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,759u32 mask);760761struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm);762763int crypto_has_shash(const char *alg_name, u32 type, u32 mask);764765static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)766{767return &tfm->base;768}769770/**771* crypto_free_shash() - zeroize and free the message digest handle772* @tfm: cipher handle to be freed773*774* If @tfm is a NULL or error pointer, this function does nothing.775*/776static inline void crypto_free_shash(struct crypto_shash *tfm)777{778crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));779}780781static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)782{783return crypto_tfm_alg_name(crypto_shash_tfm(tfm));784}785786static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)787{788return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));789}790791/**792* crypto_shash_blocksize() - obtain block size for cipher793* @tfm: cipher handle794*795* The block size for the message digest cipher referenced with the cipher796* handle is returned.797*798* Return: block size of cipher799*/800static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)801{802return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));803}804805static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)806{807return container_of(alg, struct shash_alg, base);808}809810static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)811{812return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);813}814815/**816* crypto_shash_digestsize() - obtain message digest size817* @tfm: cipher handle818*819* The size for the message digest created by the message digest cipher820* referenced with the cipher handle is returned.821*822* Return: digest size of cipher823*/824static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)825{826return crypto_shash_alg(tfm)->digestsize;827}828829static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)830{831return crypto_shash_alg(tfm)->statesize;832}833834static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)835{836return crypto_tfm_get_flags(crypto_shash_tfm(tfm));837}838839static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)840{841crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);842}843844static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)845{846crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);847}848849/**850* crypto_shash_descsize() - obtain the operational state size851* @tfm: cipher handle852*853* The size of the operational state the cipher needs during operation is854* returned for the hash referenced with the cipher handle. This size is855* required to calculate the memory requirements to allow the caller allocating856* sufficient memory for operational state.857*858* The operational state is defined with struct shash_desc where the size of859* that data structure is to be calculated as860* sizeof(struct shash_desc) + crypto_shash_descsize(alg)861*862* Return: size of the operational state863*/864static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)865{866return crypto_shash_alg(tfm)->descsize;867}868869static inline void *shash_desc_ctx(struct shash_desc *desc)870{871return desc->__ctx;872}873874/**875* crypto_shash_setkey() - set key for message digest876* @tfm: cipher handle877* @key: buffer holding the key878* @keylen: length of the key in bytes879*880* The caller provided key is set for the keyed message digest cipher. The881* cipher handle must point to a keyed message digest cipher in order for this882* function to succeed.883*884* Context: Softirq or process context.885* Return: 0 if the setting of the key was successful; < 0 if an error occurred886*/887int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,888unsigned int keylen);889890/**891* crypto_shash_digest() - calculate message digest for buffer892* @desc: see crypto_shash_final()893* @data: see crypto_shash_update()894* @len: see crypto_shash_update()895* @out: see crypto_shash_final()896*897* This function is a "short-hand" for the function calls of crypto_shash_init,898* crypto_shash_update and crypto_shash_final. The parameters have the same899* meaning as discussed for those separate three functions.900*901* Context: Softirq or process context.902* Return: 0 if the message digest creation was successful; < 0 if an error903* occurred904*/905int crypto_shash_digest(struct shash_desc *desc, const u8 *data,906unsigned int len, u8 *out);907908/**909* crypto_shash_tfm_digest() - calculate message digest for buffer910* @tfm: hash transformation object911* @data: see crypto_shash_update()912* @len: see crypto_shash_update()913* @out: see crypto_shash_final()914*915* This is a simplified version of crypto_shash_digest() for users who don't916* want to allocate their own hash descriptor (shash_desc). Instead,917* crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)918* directly, and it allocates a hash descriptor on the stack internally.919* Note that this stack allocation may be fairly large.920*921* Context: Softirq or process context.922* Return: 0 on success; < 0 if an error occurred.923*/924int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,925unsigned int len, u8 *out);926927int crypto_hash_digest(struct crypto_ahash *tfm, const u8 *data,928unsigned int len, u8 *out);929930/**931* crypto_shash_export() - extract operational state for message digest932* @desc: reference to the operational state handle whose state is exported933* @out: output buffer of sufficient size that can hold the hash state934*935* This function exports the hash state of the operational state handle into the936* caller-allocated output buffer out which must have sufficient size (e.g. by937* calling crypto_shash_descsize).938*939* Context: Softirq or process context.940* Return: 0 if the export creation was successful; < 0 if an error occurred941*/942int crypto_shash_export(struct shash_desc *desc, void *out);943944/**945* crypto_shash_import() - import operational state946* @desc: reference to the operational state handle the state imported into947* @in: buffer holding the state948*949* This function imports the hash state into the operational state handle from950* the input buffer. That buffer should have been generated with the951* crypto_ahash_export function.952*953* Context: Softirq or process context.954* Return: 0 if the import was successful; < 0 if an error occurred955*/956int crypto_shash_import(struct shash_desc *desc, const void *in);957958/**959* crypto_shash_init() - (re)initialize message digest960* @desc: operational state handle that is already filled961*962* The call (re-)initializes the message digest referenced by the963* operational state handle. Any potentially existing state created by964* previous operations is discarded.965*966* Context: Softirq or process context.967* Return: 0 if the message digest initialization was successful; < 0 if an968* error occurred969*/970int crypto_shash_init(struct shash_desc *desc);971972/**973* crypto_shash_finup() - calculate message digest of buffer974* @desc: see crypto_shash_final()975* @data: see crypto_shash_update()976* @len: see crypto_shash_update()977* @out: see crypto_shash_final()978*979* This function is a "short-hand" for the function calls of980* crypto_shash_update and crypto_shash_final. The parameters have the same981* meaning as discussed for those separate functions.982*983* Context: Softirq or process context.984* Return: 0 if the message digest creation was successful; < 0 if an error985* occurred986*/987int crypto_shash_finup(struct shash_desc *desc, const u8 *data,988unsigned int len, u8 *out);989990/**991* crypto_shash_update() - add data to message digest for processing992* @desc: operational state handle that is already initialized993* @data: input data to be added to the message digest994* @len: length of the input data995*996* Updates the message digest state of the operational state handle.997*998* Context: Softirq or process context.999* Return: 0 if the message digest update was successful; < 0 if an error1000* occurred1001*/1002static inline int crypto_shash_update(struct shash_desc *desc, const u8 *data,1003unsigned int len)1004{1005return crypto_shash_finup(desc, data, len, NULL);1006}10071008/**1009* crypto_shash_final() - calculate message digest1010* @desc: operational state handle that is already filled with data1011* @out: output buffer filled with the message digest1012*1013* Finalize the message digest operation and create the message digest1014* based on all data added to the cipher handle. The message digest is placed1015* into the output buffer. The caller must ensure that the output buffer is1016* large enough by using crypto_shash_digestsize.1017*1018* Context: Softirq or process context.1019* Return: 0 if the message digest creation was successful; < 0 if an error1020* occurred1021*/1022static inline int crypto_shash_final(struct shash_desc *desc, u8 *out)1023{1024return crypto_shash_finup(desc, NULL, 0, out);1025}10261027static inline void shash_desc_zero(struct shash_desc *desc)1028{1029memzero_explicit(desc,1030sizeof(*desc) + crypto_shash_descsize(desc->tfm));1031}10321033static inline bool ahash_is_async(struct crypto_ahash *tfm)1034{1035return crypto_tfm_is_async(&tfm->base);1036}10371038static inline struct ahash_request *ahash_request_on_stack_init(1039char *buf, struct crypto_ahash *tfm)1040{1041struct ahash_request *req = (void *)buf;10421043crypto_stack_request_init(&req->base, crypto_ahash_tfm(tfm));1044return req;1045}10461047static inline struct ahash_request *ahash_request_clone(1048struct ahash_request *req, size_t total, gfp_t gfp)1049{1050return container_of(crypto_request_clone(&req->base, total, gfp),1051struct ahash_request, base);1052}10531054#endif /* _CRYPTO_HASH_H */105510561057