Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Hash: Hash algorithms under the crypto API
0004  * 
0005  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
0006  */
0007 
0008 #ifndef _CRYPTO_HASH_H
0009 #define _CRYPTO_HASH_H
0010 
0011 #include <linux/crypto.h>
0012 #include <linux/string.h>
0013 
0014 struct crypto_ahash;
0015 
0016 /**
0017  * DOC: Message Digest Algorithm Definitions
0018  *
0019  * These data structures define modular message digest algorithm
0020  * implementations, managed via crypto_register_ahash(),
0021  * crypto_register_shash(), crypto_unregister_ahash() and
0022  * crypto_unregister_shash().
0023  */
0024 
0025 /**
0026  * struct hash_alg_common - define properties of message digest
0027  * @digestsize: Size of the result of the transformation. A buffer of this size
0028  *          must be available to the @final and @finup calls, so they can
0029  *          store the resulting hash into it. For various predefined sizes,
0030  *          search include/crypto/ using
0031  *          git grep _DIGEST_SIZE include/crypto.
0032  * @statesize: Size of the block for partial state of the transformation. A
0033  *         buffer of this size must be passed to the @export function as it
0034  *         will save the partial state of the transformation into it. On the
0035  *         other side, the @import function will load the state from a
0036  *         buffer of this size as well.
0037  * @base: Start of data structure of cipher algorithm. The common data
0038  *    structure of crypto_alg contains information common to all ciphers.
0039  *    The hash_alg_common data structure now adds the hash-specific
0040  *    information.
0041  */
0042 struct hash_alg_common {
0043     unsigned int digestsize;
0044     unsigned int statesize;
0045 
0046     struct crypto_alg base;
0047 };
0048 
0049 struct ahash_request {
0050     struct crypto_async_request base;
0051 
0052     unsigned int nbytes;
0053     struct scatterlist *src;
0054     u8 *result;
0055 
0056     /* This field may only be used by the ahash API code. */
0057     void *priv;
0058 
0059     void *__ctx[] CRYPTO_MINALIGN_ATTR;
0060 };
0061 
0062 /**
0063  * struct ahash_alg - asynchronous message digest definition
0064  * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the
0065  *    state of the HASH transformation at the beginning. This shall fill in
0066  *    the internal structures used during the entire duration of the whole
0067  *    transformation. No data processing happens at this point. Driver code
0068  *    implementation must not use req->result.
0069  * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This
0070  *     function actually pushes blocks of data from upper layers into the
0071  *     driver, which then passes those to the hardware as seen fit. This
0072  *     function must not finalize the HASH transformation by calculating the
0073  *     final message digest as this only adds more data into the
0074  *     transformation. This function shall not modify the transformation
0075  *     context, as this function may be called in parallel with the same
0076  *     transformation object. Data processing can happen synchronously
0077  *     [SHASH] or asynchronously [AHASH] at this point. Driver must not use
0078  *     req->result.
0079  * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the
0080  *     transformation and retrieves the resulting hash from the driver and
0081  *     pushes it back to upper layers. No data processing happens at this
0082  *     point unless hardware requires it to finish the transformation
0083  *     (then the data buffered by the device driver is processed).
0084  * @finup: **[optional]** Combination of @update and @final. This function is effectively a
0085  *     combination of @update and @final calls issued in sequence. As some
0086  *     hardware cannot do @update and @final separately, this callback was
0087  *     added to allow such hardware to be used at least by IPsec. Data
0088  *     processing can happen synchronously [SHASH] or asynchronously [AHASH]
0089  *     at this point.
0090  * @digest: Combination of @init and @update and @final. This function
0091  *      effectively behaves as the entire chain of operations, @init,
0092  *      @update and @final issued in sequence. Just like @finup, this was
0093  *      added for hardware which cannot do even the @finup, but can only do
0094  *      the whole transformation in one run. Data processing can happen
0095  *      synchronously [SHASH] or asynchronously [AHASH] at this point.
0096  * @setkey: Set optional key used by the hashing algorithm. Intended to push
0097  *      optional key used by the hashing algorithm from upper layers into
0098  *      the driver. This function can store the key in the transformation
0099  *      context or can outright program it into the hardware. In the former
0100  *      case, one must be careful to program the key into the hardware at
0101  *      appropriate time and one must be careful that .setkey() can be
0102  *      called multiple times during the existence of the transformation
0103  *      object. Not  all hashing algorithms do implement this function as it
0104  *      is only needed for keyed message digests. SHAx/MDx/CRCx do NOT
0105  *      implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement
0106  *      this function. This function must be called before any other of the
0107  *      @init, @update, @final, @finup, @digest is called. No data
0108  *      processing happens at this point.
0109  * @export: Export partial state of the transformation. This function dumps the
0110  *      entire state of the ongoing transformation into a provided block of
0111  *      data so it can be @import 'ed back later on. This is useful in case
0112  *      you want to save partial result of the transformation after
0113  *      processing certain amount of data and reload this partial result
0114  *      multiple times later on for multiple re-use. No data processing
0115  *      happens at this point. Driver must not use req->result.
0116  * @import: Import partial state of the transformation. This function loads the
0117  *      entire state of the ongoing transformation from a provided block of
0118  *      data so the transformation can continue from this point onward. No
0119  *      data processing happens at this point. Driver must not use
0120  *      req->result.
0121  * @init_tfm: Initialize the cryptographic transformation object.
0122  *        This function is called only once at the instantiation
0123  *        time, right after the transformation context was
0124  *        allocated. In case the cryptographic hardware has
0125  *        some special requirements which need to be handled
0126  *        by software, this function shall check for the precise
0127  *        requirement of the transformation and put any software
0128  *        fallbacks in place.
0129  * @exit_tfm: Deinitialize the cryptographic transformation object.
0130  *        This is a counterpart to @init_tfm, used to remove
0131  *        various changes set in @init_tfm.
0132  * @halg: see struct hash_alg_common
0133  */
0134 struct ahash_alg {
0135     int (*init)(struct ahash_request *req);
0136     int (*update)(struct ahash_request *req);
0137     int (*final)(struct ahash_request *req);
0138     int (*finup)(struct ahash_request *req);
0139     int (*digest)(struct ahash_request *req);
0140     int (*export)(struct ahash_request *req, void *out);
0141     int (*import)(struct ahash_request *req, const void *in);
0142     int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
0143               unsigned int keylen);
0144     int (*init_tfm)(struct crypto_ahash *tfm);
0145     void (*exit_tfm)(struct crypto_ahash *tfm);
0146 
0147     struct hash_alg_common halg;
0148 };
0149 
0150 struct shash_desc {
0151     struct crypto_shash *tfm;
0152     void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
0153 };
0154 
0155 #define HASH_MAX_DIGESTSIZE  64
0156 
0157 /*
0158  * Worst case is hmac(sha3-224-generic).  Its context is a nested 'shash_desc'
0159  * containing a 'struct sha3_state'.
0160  */
0161 #define HASH_MAX_DESCSIZE   (sizeof(struct shash_desc) + 360)
0162 
0163 #define HASH_MAX_STATESIZE  512
0164 
0165 #define SHASH_DESC_ON_STACK(shash, ctx)                      \
0166     char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
0167         __aligned(__alignof__(struct shash_desc));           \
0168     struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
0169 
0170 /**
0171  * struct shash_alg - synchronous message digest definition
0172  * @init: see struct ahash_alg
0173  * @update: see struct ahash_alg
0174  * @final: see struct ahash_alg
0175  * @finup: see struct ahash_alg
0176  * @digest: see struct ahash_alg
0177  * @export: see struct ahash_alg
0178  * @import: see struct ahash_alg
0179  * @setkey: see struct ahash_alg
0180  * @init_tfm: Initialize the cryptographic transformation object.
0181  *        This function is called only once at the instantiation
0182  *        time, right after the transformation context was
0183  *        allocated. In case the cryptographic hardware has
0184  *        some special requirements which need to be handled
0185  *        by software, this function shall check for the precise
0186  *        requirement of the transformation and put any software
0187  *        fallbacks in place.
0188  * @exit_tfm: Deinitialize the cryptographic transformation object.
0189  *        This is a counterpart to @init_tfm, used to remove
0190  *        various changes set in @init_tfm.
0191  * @digestsize: see struct ahash_alg
0192  * @statesize: see struct ahash_alg
0193  * @descsize: Size of the operational state for the message digest. This state
0194  *        size is the memory size that needs to be allocated for
0195  *        shash_desc.__ctx
0196  * @base: internally used
0197  */
0198 struct shash_alg {
0199     int (*init)(struct shash_desc *desc);
0200     int (*update)(struct shash_desc *desc, const u8 *data,
0201               unsigned int len);
0202     int (*final)(struct shash_desc *desc, u8 *out);
0203     int (*finup)(struct shash_desc *desc, const u8 *data,
0204              unsigned int len, u8 *out);
0205     int (*digest)(struct shash_desc *desc, const u8 *data,
0206               unsigned int len, u8 *out);
0207     int (*export)(struct shash_desc *desc, void *out);
0208     int (*import)(struct shash_desc *desc, const void *in);
0209     int (*setkey)(struct crypto_shash *tfm, const u8 *key,
0210               unsigned int keylen);
0211     int (*init_tfm)(struct crypto_shash *tfm);
0212     void (*exit_tfm)(struct crypto_shash *tfm);
0213 
0214     unsigned int descsize;
0215 
0216     /* These fields must match hash_alg_common. */
0217     unsigned int digestsize
0218         __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
0219     unsigned int statesize;
0220 
0221     struct crypto_alg base;
0222 };
0223 
0224 struct crypto_ahash {
0225     int (*init)(struct ahash_request *req);
0226     int (*update)(struct ahash_request *req);
0227     int (*final)(struct ahash_request *req);
0228     int (*finup)(struct ahash_request *req);
0229     int (*digest)(struct ahash_request *req);
0230     int (*export)(struct ahash_request *req, void *out);
0231     int (*import)(struct ahash_request *req, const void *in);
0232     int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
0233               unsigned int keylen);
0234 
0235     unsigned int reqsize;
0236     struct crypto_tfm base;
0237 };
0238 
0239 struct crypto_shash {
0240     unsigned int descsize;
0241     struct crypto_tfm base;
0242 };
0243 
0244 /**
0245  * DOC: Asynchronous Message Digest API
0246  *
0247  * The asynchronous message digest API is used with the ciphers of type
0248  * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
0249  *
0250  * The asynchronous cipher operation discussion provided for the
0251  * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
0252  */
0253 
0254 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
0255 {
0256     return container_of(tfm, struct crypto_ahash, base);
0257 }
0258 
0259 /**
0260  * crypto_alloc_ahash() - allocate ahash cipher handle
0261  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
0262  *        ahash cipher
0263  * @type: specifies the type of the cipher
0264  * @mask: specifies the mask for the cipher
0265  *
0266  * Allocate a cipher handle for an ahash. The returned struct
0267  * crypto_ahash is the cipher handle that is required for any subsequent
0268  * API invocation for that ahash.
0269  *
0270  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
0271  *     of an error, PTR_ERR() returns the error code.
0272  */
0273 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
0274                     u32 mask);
0275 
0276 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
0277 {
0278     return &tfm->base;
0279 }
0280 
0281 /**
0282  * crypto_free_ahash() - zeroize and free the ahash handle
0283  * @tfm: cipher handle to be freed
0284  *
0285  * If @tfm is a NULL or error pointer, this function does nothing.
0286  */
0287 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
0288 {
0289     crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
0290 }
0291 
0292 /**
0293  * crypto_has_ahash() - Search for the availability of an ahash.
0294  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
0295  *        ahash
0296  * @type: specifies the type of the ahash
0297  * @mask: specifies the mask for the ahash
0298  *
0299  * Return: true when the ahash is known to the kernel crypto API; false
0300  *     otherwise
0301  */
0302 int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
0303 
0304 static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
0305 {
0306     return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
0307 }
0308 
0309 static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
0310 {
0311     return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
0312 }
0313 
0314 static inline unsigned int crypto_ahash_alignmask(
0315     struct crypto_ahash *tfm)
0316 {
0317     return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
0318 }
0319 
0320 /**
0321  * crypto_ahash_blocksize() - obtain block size for cipher
0322  * @tfm: cipher handle
0323  *
0324  * The block size for the message digest cipher referenced with the cipher
0325  * handle is returned.
0326  *
0327  * Return: block size of cipher
0328  */
0329 static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
0330 {
0331     return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
0332 }
0333 
0334 static inline struct hash_alg_common *__crypto_hash_alg_common(
0335     struct crypto_alg *alg)
0336 {
0337     return container_of(alg, struct hash_alg_common, base);
0338 }
0339 
0340 static inline struct hash_alg_common *crypto_hash_alg_common(
0341     struct crypto_ahash *tfm)
0342 {
0343     return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
0344 }
0345 
0346 /**
0347  * crypto_ahash_digestsize() - obtain message digest size
0348  * @tfm: cipher handle
0349  *
0350  * The size for the message digest created by the message digest cipher
0351  * referenced with the cipher handle is returned.
0352  *
0353  *
0354  * Return: message digest size of cipher
0355  */
0356 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
0357 {
0358     return crypto_hash_alg_common(tfm)->digestsize;
0359 }
0360 
0361 /**
0362  * crypto_ahash_statesize() - obtain size of the ahash state
0363  * @tfm: cipher handle
0364  *
0365  * Return the size of the ahash state. With the crypto_ahash_export()
0366  * function, the caller can export the state into a buffer whose size is
0367  * defined with this function.
0368  *
0369  * Return: size of the ahash state
0370  */
0371 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
0372 {
0373     return crypto_hash_alg_common(tfm)->statesize;
0374 }
0375 
0376 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
0377 {
0378     return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
0379 }
0380 
0381 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
0382 {
0383     crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
0384 }
0385 
0386 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
0387 {
0388     crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
0389 }
0390 
0391 /**
0392  * crypto_ahash_reqtfm() - obtain cipher handle from request
0393  * @req: asynchronous request handle that contains the reference to the ahash
0394  *   cipher handle
0395  *
0396  * Return the ahash cipher handle that is registered with the asynchronous
0397  * request handle ahash_request.
0398  *
0399  * Return: ahash cipher handle
0400  */
0401 static inline struct crypto_ahash *crypto_ahash_reqtfm(
0402     struct ahash_request *req)
0403 {
0404     return __crypto_ahash_cast(req->base.tfm);
0405 }
0406 
0407 /**
0408  * crypto_ahash_reqsize() - obtain size of the request data structure
0409  * @tfm: cipher handle
0410  *
0411  * Return: size of the request data
0412  */
0413 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
0414 {
0415     return tfm->reqsize;
0416 }
0417 
0418 static inline void *ahash_request_ctx(struct ahash_request *req)
0419 {
0420     return req->__ctx;
0421 }
0422 
0423 /**
0424  * crypto_ahash_setkey - set key for cipher handle
0425  * @tfm: cipher handle
0426  * @key: buffer holding the key
0427  * @keylen: length of the key in bytes
0428  *
0429  * The caller provided key is set for the ahash cipher. The cipher
0430  * handle must point to a keyed hash in order for this function to succeed.
0431  *
0432  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
0433  */
0434 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
0435             unsigned int keylen);
0436 
0437 /**
0438  * crypto_ahash_finup() - update and finalize message digest
0439  * @req: reference to the ahash_request handle that holds all information
0440  *   needed to perform the cipher operation
0441  *
0442  * This function is a "short-hand" for the function calls of
0443  * crypto_ahash_update and crypto_ahash_final. The parameters have the same
0444  * meaning as discussed for those separate functions.
0445  *
0446  * Return: see crypto_ahash_final()
0447  */
0448 int crypto_ahash_finup(struct ahash_request *req);
0449 
0450 /**
0451  * crypto_ahash_final() - calculate message digest
0452  * @req: reference to the ahash_request handle that holds all information
0453  *   needed to perform the cipher operation
0454  *
0455  * Finalize the message digest operation and create the message digest
0456  * based on all data added to the cipher handle. The message digest is placed
0457  * into the output buffer registered with the ahash_request handle.
0458  *
0459  * Return:
0460  * 0        if the message digest was successfully calculated;
0461  * -EINPROGRESS if data is fed into hardware (DMA) or queued for later;
0462  * -EBUSY   if queue is full and request should be resubmitted later;
0463  * other < 0    if an error occurred
0464  */
0465 int crypto_ahash_final(struct ahash_request *req);
0466 
0467 /**
0468  * crypto_ahash_digest() - calculate message digest for a buffer
0469  * @req: reference to the ahash_request handle that holds all information
0470  *   needed to perform the cipher operation
0471  *
0472  * This function is a "short-hand" for the function calls of crypto_ahash_init,
0473  * crypto_ahash_update and crypto_ahash_final. The parameters have the same
0474  * meaning as discussed for those separate three functions.
0475  *
0476  * Return: see crypto_ahash_final()
0477  */
0478 int crypto_ahash_digest(struct ahash_request *req);
0479 
0480 /**
0481  * crypto_ahash_export() - extract current message digest state
0482  * @req: reference to the ahash_request handle whose state is exported
0483  * @out: output buffer of sufficient size that can hold the hash state
0484  *
0485  * This function exports the hash state of the ahash_request handle into the
0486  * caller-allocated output buffer out which must have sufficient size (e.g. by
0487  * calling crypto_ahash_statesize()).
0488  *
0489  * Return: 0 if the export was successful; < 0 if an error occurred
0490  */
0491 static inline int crypto_ahash_export(struct ahash_request *req, void *out)
0492 {
0493     return crypto_ahash_reqtfm(req)->export(req, out);
0494 }
0495 
0496 /**
0497  * crypto_ahash_import() - import message digest state
0498  * @req: reference to ahash_request handle the state is imported into
0499  * @in: buffer holding the state
0500  *
0501  * This function imports the hash state into the ahash_request handle from the
0502  * input buffer. That buffer should have been generated with the
0503  * crypto_ahash_export function.
0504  *
0505  * Return: 0 if the import was successful; < 0 if an error occurred
0506  */
0507 static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
0508 {
0509     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0510 
0511     if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0512         return -ENOKEY;
0513 
0514     return tfm->import(req, in);
0515 }
0516 
0517 /**
0518  * crypto_ahash_init() - (re)initialize message digest handle
0519  * @req: ahash_request handle that already is initialized with all necessary
0520  *   data using the ahash_request_* API functions
0521  *
0522  * The call (re-)initializes the message digest referenced by the ahash_request
0523  * handle. Any potentially existing state created by previous operations is
0524  * discarded.
0525  *
0526  * Return: see crypto_ahash_final()
0527  */
0528 static inline int crypto_ahash_init(struct ahash_request *req)
0529 {
0530     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0531 
0532     if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0533         return -ENOKEY;
0534 
0535     return tfm->init(req);
0536 }
0537 
0538 /**
0539  * crypto_ahash_update() - add data to message digest for processing
0540  * @req: ahash_request handle that was previously initialized with the
0541  *   crypto_ahash_init call.
0542  *
0543  * Updates the message digest state of the &ahash_request handle. The input data
0544  * is pointed to by the scatter/gather list registered in the &ahash_request
0545  * handle
0546  *
0547  * Return: see crypto_ahash_final()
0548  */
0549 static inline int crypto_ahash_update(struct ahash_request *req)
0550 {
0551     struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
0552     struct crypto_alg *alg = tfm->base.__crt_alg;
0553     unsigned int nbytes = req->nbytes;
0554     int ret;
0555 
0556     crypto_stats_get(alg);
0557     ret = crypto_ahash_reqtfm(req)->update(req);
0558     crypto_stats_ahash_update(nbytes, ret, alg);
0559     return ret;
0560 }
0561 
0562 /**
0563  * DOC: Asynchronous Hash Request Handle
0564  *
0565  * The &ahash_request data structure contains all pointers to data
0566  * required for the asynchronous cipher operation. This includes the cipher
0567  * handle (which can be used by multiple &ahash_request instances), pointer
0568  * to plaintext and the message digest output buffer, asynchronous callback
0569  * function, etc. It acts as a handle to the ahash_request_* API calls in a
0570  * similar way as ahash handle to the crypto_ahash_* API calls.
0571  */
0572 
0573 /**
0574  * ahash_request_set_tfm() - update cipher handle reference in request
0575  * @req: request handle to be modified
0576  * @tfm: cipher handle that shall be added to the request handle
0577  *
0578  * Allow the caller to replace the existing ahash handle in the request
0579  * data structure with a different one.
0580  */
0581 static inline void ahash_request_set_tfm(struct ahash_request *req,
0582                      struct crypto_ahash *tfm)
0583 {
0584     req->base.tfm = crypto_ahash_tfm(tfm);
0585 }
0586 
0587 /**
0588  * ahash_request_alloc() - allocate request data structure
0589  * @tfm: cipher handle to be registered with the request
0590  * @gfp: memory allocation flag that is handed to kmalloc by the API call.
0591  *
0592  * Allocate the request data structure that must be used with the ahash
0593  * message digest API calls. During
0594  * the allocation, the provided ahash handle
0595  * is registered in the request data structure.
0596  *
0597  * Return: allocated request handle in case of success, or NULL if out of memory
0598  */
0599 static inline struct ahash_request *ahash_request_alloc(
0600     struct crypto_ahash *tfm, gfp_t gfp)
0601 {
0602     struct ahash_request *req;
0603 
0604     req = kmalloc(sizeof(struct ahash_request) +
0605               crypto_ahash_reqsize(tfm), gfp);
0606 
0607     if (likely(req))
0608         ahash_request_set_tfm(req, tfm);
0609 
0610     return req;
0611 }
0612 
0613 /**
0614  * ahash_request_free() - zeroize and free the request data structure
0615  * @req: request data structure cipher handle to be freed
0616  */
0617 static inline void ahash_request_free(struct ahash_request *req)
0618 {
0619     kfree_sensitive(req);
0620 }
0621 
0622 static inline void ahash_request_zero(struct ahash_request *req)
0623 {
0624     memzero_explicit(req, sizeof(*req) +
0625                   crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
0626 }
0627 
0628 static inline struct ahash_request *ahash_request_cast(
0629     struct crypto_async_request *req)
0630 {
0631     return container_of(req, struct ahash_request, base);
0632 }
0633 
0634 /**
0635  * ahash_request_set_callback() - set asynchronous callback function
0636  * @req: request handle
0637  * @flags: specify zero or an ORing of the flags
0638  *     CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
0639  *     increase the wait queue beyond the initial maximum size;
0640  *     CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
0641  * @compl: callback function pointer to be registered with the request handle
0642  * @data: The data pointer refers to memory that is not used by the kernel
0643  *    crypto API, but provided to the callback function for it to use. Here,
0644  *    the caller can provide a reference to memory the callback function can
0645  *    operate on. As the callback function is invoked asynchronously to the
0646  *    related functionality, it may need to access data structures of the
0647  *    related functionality which can be referenced using this pointer. The
0648  *    callback function can access the memory via the "data" field in the
0649  *    &crypto_async_request data structure provided to the callback function.
0650  *
0651  * This function allows setting the callback function that is triggered once
0652  * the cipher operation completes.
0653  *
0654  * The callback function is registered with the &ahash_request handle and
0655  * must comply with the following template::
0656  *
0657  *  void callback_function(struct crypto_async_request *req, int error)
0658  */
0659 static inline void ahash_request_set_callback(struct ahash_request *req,
0660                           u32 flags,
0661                           crypto_completion_t compl,
0662                           void *data)
0663 {
0664     req->base.complete = compl;
0665     req->base.data = data;
0666     req->base.flags = flags;
0667 }
0668 
0669 /**
0670  * ahash_request_set_crypt() - set data buffers
0671  * @req: ahash_request handle to be updated
0672  * @src: source scatter/gather list
0673  * @result: buffer that is filled with the message digest -- the caller must
0674  *      ensure that the buffer has sufficient space by, for example, calling
0675  *      crypto_ahash_digestsize()
0676  * @nbytes: number of bytes to process from the source scatter/gather list
0677  *
0678  * By using this call, the caller references the source scatter/gather list.
0679  * The source scatter/gather list points to the data the message digest is to
0680  * be calculated for.
0681  */
0682 static inline void ahash_request_set_crypt(struct ahash_request *req,
0683                        struct scatterlist *src, u8 *result,
0684                        unsigned int nbytes)
0685 {
0686     req->src = src;
0687     req->nbytes = nbytes;
0688     req->result = result;
0689 }
0690 
0691 /**
0692  * DOC: Synchronous Message Digest API
0693  *
0694  * The synchronous message digest API is used with the ciphers of type
0695  * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
0696  *
0697  * The message digest API is able to maintain state information for the
0698  * caller.
0699  *
0700  * The synchronous message digest API can store user-related context in its
0701  * shash_desc request data structure.
0702  */
0703 
0704 /**
0705  * crypto_alloc_shash() - allocate message digest handle
0706  * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
0707  *        message digest cipher
0708  * @type: specifies the type of the cipher
0709  * @mask: specifies the mask for the cipher
0710  *
0711  * Allocate a cipher handle for a message digest. The returned &struct
0712  * crypto_shash is the cipher handle that is required for any subsequent
0713  * API invocation for that message digest.
0714  *
0715  * Return: allocated cipher handle in case of success; IS_ERR() is true in case
0716  *     of an error, PTR_ERR() returns the error code.
0717  */
0718 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
0719                     u32 mask);
0720 
0721 int crypto_has_shash(const char *alg_name, u32 type, u32 mask);
0722 
0723 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
0724 {
0725     return &tfm->base;
0726 }
0727 
0728 /**
0729  * crypto_free_shash() - zeroize and free the message digest handle
0730  * @tfm: cipher handle to be freed
0731  *
0732  * If @tfm is a NULL or error pointer, this function does nothing.
0733  */
0734 static inline void crypto_free_shash(struct crypto_shash *tfm)
0735 {
0736     crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
0737 }
0738 
0739 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
0740 {
0741     return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
0742 }
0743 
0744 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
0745 {
0746     return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
0747 }
0748 
0749 static inline unsigned int crypto_shash_alignmask(
0750     struct crypto_shash *tfm)
0751 {
0752     return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
0753 }
0754 
0755 /**
0756  * crypto_shash_blocksize() - obtain block size for cipher
0757  * @tfm: cipher handle
0758  *
0759  * The block size for the message digest cipher referenced with the cipher
0760  * handle is returned.
0761  *
0762  * Return: block size of cipher
0763  */
0764 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
0765 {
0766     return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
0767 }
0768 
0769 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
0770 {
0771     return container_of(alg, struct shash_alg, base);
0772 }
0773 
0774 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
0775 {
0776     return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
0777 }
0778 
0779 /**
0780  * crypto_shash_digestsize() - obtain message digest size
0781  * @tfm: cipher handle
0782  *
0783  * The size for the message digest created by the message digest cipher
0784  * referenced with the cipher handle is returned.
0785  *
0786  * Return: digest size of cipher
0787  */
0788 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
0789 {
0790     return crypto_shash_alg(tfm)->digestsize;
0791 }
0792 
0793 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
0794 {
0795     return crypto_shash_alg(tfm)->statesize;
0796 }
0797 
0798 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
0799 {
0800     return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
0801 }
0802 
0803 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
0804 {
0805     crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
0806 }
0807 
0808 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
0809 {
0810     crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
0811 }
0812 
0813 /**
0814  * crypto_shash_descsize() - obtain the operational state size
0815  * @tfm: cipher handle
0816  *
0817  * The size of the operational state the cipher needs during operation is
0818  * returned for the hash referenced with the cipher handle. This size is
0819  * required to calculate the memory requirements to allow the caller allocating
0820  * sufficient memory for operational state.
0821  *
0822  * The operational state is defined with struct shash_desc where the size of
0823  * that data structure is to be calculated as
0824  * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
0825  *
0826  * Return: size of the operational state
0827  */
0828 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
0829 {
0830     return tfm->descsize;
0831 }
0832 
0833 static inline void *shash_desc_ctx(struct shash_desc *desc)
0834 {
0835     return desc->__ctx;
0836 }
0837 
0838 /**
0839  * crypto_shash_setkey() - set key for message digest
0840  * @tfm: cipher handle
0841  * @key: buffer holding the key
0842  * @keylen: length of the key in bytes
0843  *
0844  * The caller provided key is set for the keyed message digest cipher. The
0845  * cipher handle must point to a keyed message digest cipher in order for this
0846  * function to succeed.
0847  *
0848  * Context: Any context.
0849  * Return: 0 if the setting of the key was successful; < 0 if an error occurred
0850  */
0851 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
0852             unsigned int keylen);
0853 
0854 /**
0855  * crypto_shash_digest() - calculate message digest for buffer
0856  * @desc: see crypto_shash_final()
0857  * @data: see crypto_shash_update()
0858  * @len: see crypto_shash_update()
0859  * @out: see crypto_shash_final()
0860  *
0861  * This function is a "short-hand" for the function calls of crypto_shash_init,
0862  * crypto_shash_update and crypto_shash_final. The parameters have the same
0863  * meaning as discussed for those separate three functions.
0864  *
0865  * Context: Any context.
0866  * Return: 0 if the message digest creation was successful; < 0 if an error
0867  *     occurred
0868  */
0869 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
0870             unsigned int len, u8 *out);
0871 
0872 /**
0873  * crypto_shash_tfm_digest() - calculate message digest for buffer
0874  * @tfm: hash transformation object
0875  * @data: see crypto_shash_update()
0876  * @len: see crypto_shash_update()
0877  * @out: see crypto_shash_final()
0878  *
0879  * This is a simplified version of crypto_shash_digest() for users who don't
0880  * want to allocate their own hash descriptor (shash_desc).  Instead,
0881  * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)
0882  * directly, and it allocates a hash descriptor on the stack internally.
0883  * Note that this stack allocation may be fairly large.
0884  *
0885  * Context: Any context.
0886  * Return: 0 on success; < 0 if an error occurred.
0887  */
0888 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
0889                 unsigned int len, u8 *out);
0890 
0891 /**
0892  * crypto_shash_export() - extract operational state for message digest
0893  * @desc: reference to the operational state handle whose state is exported
0894  * @out: output buffer of sufficient size that can hold the hash state
0895  *
0896  * This function exports the hash state of the operational state handle into the
0897  * caller-allocated output buffer out which must have sufficient size (e.g. by
0898  * calling crypto_shash_descsize).
0899  *
0900  * Context: Any context.
0901  * Return: 0 if the export creation was successful; < 0 if an error occurred
0902  */
0903 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
0904 {
0905     return crypto_shash_alg(desc->tfm)->export(desc, out);
0906 }
0907 
0908 /**
0909  * crypto_shash_import() - import operational state
0910  * @desc: reference to the operational state handle the state imported into
0911  * @in: buffer holding the state
0912  *
0913  * This function imports the hash state into the operational state handle from
0914  * the input buffer. That buffer should have been generated with the
0915  * crypto_ahash_export function.
0916  *
0917  * Context: Any context.
0918  * Return: 0 if the import was successful; < 0 if an error occurred
0919  */
0920 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
0921 {
0922     struct crypto_shash *tfm = desc->tfm;
0923 
0924     if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0925         return -ENOKEY;
0926 
0927     return crypto_shash_alg(tfm)->import(desc, in);
0928 }
0929 
0930 /**
0931  * crypto_shash_init() - (re)initialize message digest
0932  * @desc: operational state handle that is already filled
0933  *
0934  * The call (re-)initializes the message digest referenced by the
0935  * operational state handle. Any potentially existing state created by
0936  * previous operations is discarded.
0937  *
0938  * Context: Any context.
0939  * Return: 0 if the message digest initialization was successful; < 0 if an
0940  *     error occurred
0941  */
0942 static inline int crypto_shash_init(struct shash_desc *desc)
0943 {
0944     struct crypto_shash *tfm = desc->tfm;
0945 
0946     if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0947         return -ENOKEY;
0948 
0949     return crypto_shash_alg(tfm)->init(desc);
0950 }
0951 
0952 /**
0953  * crypto_shash_update() - add data to message digest for processing
0954  * @desc: operational state handle that is already initialized
0955  * @data: input data to be added to the message digest
0956  * @len: length of the input data
0957  *
0958  * Updates the message digest state of the operational state handle.
0959  *
0960  * Context: Any context.
0961  * Return: 0 if the message digest update was successful; < 0 if an error
0962  *     occurred
0963  */
0964 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
0965             unsigned int len);
0966 
0967 /**
0968  * crypto_shash_final() - calculate message digest
0969  * @desc: operational state handle that is already filled with data
0970  * @out: output buffer filled with the message digest
0971  *
0972  * Finalize the message digest operation and create the message digest
0973  * based on all data added to the cipher handle. The message digest is placed
0974  * into the output buffer. The caller must ensure that the output buffer is
0975  * large enough by using crypto_shash_digestsize.
0976  *
0977  * Context: Any context.
0978  * Return: 0 if the message digest creation was successful; < 0 if an error
0979  *     occurred
0980  */
0981 int crypto_shash_final(struct shash_desc *desc, u8 *out);
0982 
0983 /**
0984  * crypto_shash_finup() - calculate message digest of buffer
0985  * @desc: see crypto_shash_final()
0986  * @data: see crypto_shash_update()
0987  * @len: see crypto_shash_update()
0988  * @out: see crypto_shash_final()
0989  *
0990  * This function is a "short-hand" for the function calls of
0991  * crypto_shash_update and crypto_shash_final. The parameters have the same
0992  * meaning as discussed for those separate functions.
0993  *
0994  * Context: Any context.
0995  * Return: 0 if the message digest creation was successful; < 0 if an error
0996  *     occurred
0997  */
0998 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
0999                unsigned int len, u8 *out);
1000 
1001 static inline void shash_desc_zero(struct shash_desc *desc)
1002 {
1003     memzero_explicit(desc,
1004              sizeof(*desc) + crypto_shash_descsize(desc->tfm));
1005 }
1006 
1007 #endif  /* _CRYPTO_HASH_H */