2019-05-27 06:55:01 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2008-07-10 08:01:22 +00:00
|
|
|
/*
|
|
|
|
* Hash: Hash algorithms under the crypto API
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CRYPTO_HASH_H
|
|
|
|
#define _CRYPTO_HASH_H
|
|
|
|
|
|
|
|
#include <linux/crypto.h>
|
2016-01-22 15:17:58 +00:00
|
|
|
#include <linux/string.h>
|
2008-07-10 08:01:22 +00:00
|
|
|
|
2009-07-14 04:28:26 +00:00
|
|
|
struct crypto_ahash;
|
|
|
|
|
2014-11-12 04:26:03 +00:00
|
|
|
/**
|
|
|
|
* DOC: Message Digest Algorithm Definitions
|
|
|
|
*
|
|
|
|
* These data structures define modular message digest algorithm
|
|
|
|
* implementations, managed via crypto_register_ahash(),
|
|
|
|
* crypto_register_shash(), crypto_unregister_ahash() and
|
|
|
|
* crypto_unregister_shash().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct hash_alg_common - define properties of message digest
|
|
|
|
* @digestsize: Size of the result of the transformation. A buffer of this size
|
|
|
|
* must be available to the @final and @finup calls, so they can
|
|
|
|
* store the resulting hash into it. For various predefined sizes,
|
|
|
|
* search include/crypto/ using
|
|
|
|
* git grep _DIGEST_SIZE include/crypto.
|
|
|
|
* @statesize: Size of the block for partial state of the transformation. A
|
|
|
|
* buffer of this size must be passed to the @export function as it
|
|
|
|
* will save the partial state of the transformation into it. On the
|
|
|
|
* other side, the @import function will load the state from a
|
|
|
|
* buffer of this size as well.
|
2014-11-14 04:26:21 +00:00
|
|
|
* @base: Start of data structure of cipher algorithm. The common data
|
|
|
|
* structure of crypto_alg contains information common to all ciphers.
|
|
|
|
* The hash_alg_common data structure now adds the hash-specific
|
|
|
|
* information.
|
2014-11-12 04:26:03 +00:00
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
struct hash_alg_common {
|
|
|
|
unsigned int digestsize;
|
|
|
|
unsigned int statesize;
|
|
|
|
|
|
|
|
struct crypto_alg base;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ahash_request {
|
|
|
|
struct crypto_async_request base;
|
|
|
|
|
|
|
|
unsigned int nbytes;
|
|
|
|
struct scatterlist *src;
|
|
|
|
u8 *result;
|
|
|
|
|
2009-07-15 04:40:40 +00:00
|
|
|
/* This field may only be used by the ahash API code. */
|
|
|
|
void *priv;
|
|
|
|
|
2009-07-14 04:28:26 +00:00
|
|
|
void *__ctx[] CRYPTO_MINALIGN_ATTR;
|
|
|
|
};
|
|
|
|
|
2014-11-12 04:26:03 +00:00
|
|
|
/**
|
|
|
|
* struct ahash_alg - asynchronous message digest definition
|
2018-01-06 23:01:43 +00:00
|
|
|
* @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the
|
2015-06-03 15:01:21 +00:00
|
|
|
* state of the HASH transformation at the beginning. This shall fill in
|
2014-11-12 04:26:03 +00:00
|
|
|
* the internal structures used during the entire duration of the whole
|
2018-03-07 10:49:33 +00:00
|
|
|
* transformation. No data processing happens at this point. Driver code
|
|
|
|
* implementation must not use req->result.
|
2018-01-06 23:01:43 +00:00
|
|
|
* @update: **[mandatory]** Push a chunk of data into the driver for transformation. This
|
2014-11-12 04:26:03 +00:00
|
|
|
* function actually pushes blocks of data from upper layers into the
|
|
|
|
* driver, which then passes those to the hardware as seen fit. This
|
|
|
|
* function must not finalize the HASH transformation by calculating the
|
|
|
|
* final message digest as this only adds more data into the
|
|
|
|
* transformation. This function shall not modify the transformation
|
|
|
|
* context, as this function may be called in parallel with the same
|
|
|
|
* transformation object. Data processing can happen synchronously
|
2018-03-07 10:49:33 +00:00
|
|
|
* [SHASH] or asynchronously [AHASH] at this point. Driver must not use
|
|
|
|
* req->result.
|
2018-01-06 23:01:43 +00:00
|
|
|
* @final: **[mandatory]** Retrieve result from the driver. This function finalizes the
|
2014-11-12 04:26:03 +00:00
|
|
|
* transformation and retrieves the resulting hash from the driver and
|
|
|
|
* pushes it back to upper layers. No data processing happens at this
|
2017-08-17 10:11:36 +00:00
|
|
|
* point unless hardware requires it to finish the transformation
|
|
|
|
* (then the data buffered by the device driver is processed).
|
2018-01-06 23:01:43 +00:00
|
|
|
* @finup: **[optional]** Combination of @update and @final. This function is effectively a
|
2014-11-12 04:26:03 +00:00
|
|
|
* combination of @update and @final calls issued in sequence. As some
|
|
|
|
* hardware cannot do @update and @final separately, this callback was
|
|
|
|
* added to allow such hardware to be used at least by IPsec. Data
|
|
|
|
* processing can happen synchronously [SHASH] or asynchronously [AHASH]
|
|
|
|
* at this point.
|
|
|
|
* @digest: Combination of @init and @update and @final. This function
|
|
|
|
* effectively behaves as the entire chain of operations, @init,
|
|
|
|
* @update and @final issued in sequence. Just like @finup, this was
|
|
|
|
* added for hardware which cannot do even the @finup, but can only do
|
|
|
|
* the whole transformation in one run. Data processing can happen
|
|
|
|
* synchronously [SHASH] or asynchronously [AHASH] at this point.
|
|
|
|
* @setkey: Set optional key used by the hashing algorithm. Intended to push
|
|
|
|
* optional key used by the hashing algorithm from upper layers into
|
|
|
|
* the driver. This function can store the key in the transformation
|
|
|
|
* context or can outright program it into the hardware. In the former
|
|
|
|
* case, one must be careful to program the key into the hardware at
|
|
|
|
* appropriate time and one must be careful that .setkey() can be
|
|
|
|
* called multiple times during the existence of the transformation
|
|
|
|
* object. Not all hashing algorithms do implement this function as it
|
|
|
|
* is only needed for keyed message digests. SHAx/MDx/CRCx do NOT
|
|
|
|
* implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement
|
|
|
|
* this function. This function must be called before any other of the
|
|
|
|
* @init, @update, @final, @finup, @digest is called. No data
|
|
|
|
* processing happens at this point.
|
|
|
|
* @export: Export partial state of the transformation. This function dumps the
|
|
|
|
* entire state of the ongoing transformation into a provided block of
|
|
|
|
* data so it can be @import 'ed back later on. This is useful in case
|
|
|
|
* you want to save partial result of the transformation after
|
|
|
|
* processing certain amount of data and reload this partial result
|
|
|
|
* multiple times later on for multiple re-use. No data processing
|
2018-03-07 10:49:33 +00:00
|
|
|
* happens at this point. Driver must not use req->result.
|
2014-11-12 04:26:03 +00:00
|
|
|
* @import: Import partial state of the transformation. This function loads the
|
|
|
|
* entire state of the ongoing transformation from a provided block of
|
|
|
|
* data so the transformation can continue from this point onward. No
|
2018-03-07 10:49:33 +00:00
|
|
|
* data processing happens at this point. Driver must not use
|
|
|
|
* req->result.
|
2020-08-18 08:25:34 +00:00
|
|
|
* @init_tfm: Initialize the cryptographic transformation object.
|
|
|
|
* This function is called only once at the instantiation
|
|
|
|
* time, right after the transformation context was
|
|
|
|
* allocated. In case the cryptographic hardware has
|
|
|
|
* some special requirements which need to be handled
|
|
|
|
* by software, this function shall check for the precise
|
|
|
|
* requirement of the transformation and put any software
|
|
|
|
* fallbacks in place.
|
|
|
|
* @exit_tfm: Deinitialize the cryptographic transformation object.
|
|
|
|
* This is a counterpart to @init_tfm, used to remove
|
|
|
|
* various changes set in @init_tfm.
|
2014-11-14 04:26:21 +00:00
|
|
|
* @halg: see struct hash_alg_common
|
2014-11-12 04:26:03 +00:00
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
struct ahash_alg {
|
|
|
|
int (*init)(struct ahash_request *req);
|
|
|
|
int (*update)(struct ahash_request *req);
|
|
|
|
int (*final)(struct ahash_request *req);
|
|
|
|
int (*finup)(struct ahash_request *req);
|
|
|
|
int (*digest)(struct ahash_request *req);
|
|
|
|
int (*export)(struct ahash_request *req, void *out);
|
|
|
|
int (*import)(struct ahash_request *req, const void *in);
|
|
|
|
int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen);
|
2020-08-18 08:25:34 +00:00
|
|
|
int (*init_tfm)(struct crypto_ahash *tfm);
|
|
|
|
void (*exit_tfm)(struct crypto_ahash *tfm);
|
2009-07-14 04:28:26 +00:00
|
|
|
|
|
|
|
struct hash_alg_common halg;
|
|
|
|
};
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
struct shash_desc {
|
|
|
|
struct crypto_shash *tfm;
|
crypto - shash: reduce minimum alignment of shash_desc structure
Unlike many other structure types defined in the crypto API, the
'shash_desc' structure is permitted to live on the stack, which
implies its contents may not be accessed by DMA masters. (This is
due to the fact that the stack may be located in the vmalloc area,
which requires a different virtual-to-physical translation than the
one implemented by the DMA subsystem)
Our definition of CRYPTO_MINALIGN_ATTR is based on ARCH_KMALLOC_MINALIGN,
which may take DMA constraints into account on architectures that support
non-cache coherent DMA such as ARM and arm64. In this case, the value is
chosen to reflect the largest cacheline size in the system, in order to
ensure that explicit cache maintenance as required by non-coherent DMA
masters does not affect adjacent, unrelated slab allocations. On arm64,
this value is currently set at 128 bytes.
This means that applying CRYPTO_MINALIGN_ATTR to struct shash_desc is both
unnecessary (as it is never used for DMA), and undesirable, given that it
wastes stack space (on arm64, performing the alignment costs 112 bytes in
the worst case, and the hole between the 'tfm' and '__ctx' members takes
up another 120 bytes, resulting in an increased stack footprint of up to
232 bytes.) So instead, let's switch to the minimum SLAB alignment, which
does not take DMA constraints into account.
Note that this is a no-op for x86.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2021-01-13 09:11:35 +00:00
|
|
|
void *__ctx[] __aligned(ARCH_SLAB_MINALIGN);
|
2008-08-31 05:47:27 +00:00
|
|
|
};
|
|
|
|
|
2018-08-07 21:18:38 +00:00
|
|
|
#define HASH_MAX_DIGESTSIZE 64
|
2019-05-14 23:13:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc'
|
|
|
|
* containing a 'struct sha3_state'.
|
|
|
|
*/
|
|
|
|
#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360)
|
|
|
|
|
2018-08-07 21:18:38 +00:00
|
|
|
#define HASH_MAX_STATESIZE 512
|
|
|
|
|
crypto - shash: reduce minimum alignment of shash_desc structure
Unlike many other structure types defined in the crypto API, the
'shash_desc' structure is permitted to live on the stack, which
implies its contents may not be accessed by DMA masters. (This is
due to the fact that the stack may be located in the vmalloc area,
which requires a different virtual-to-physical translation than the
one implemented by the DMA subsystem)
Our definition of CRYPTO_MINALIGN_ATTR is based on ARCH_KMALLOC_MINALIGN,
which may take DMA constraints into account on architectures that support
non-cache coherent DMA such as ARM and arm64. In this case, the value is
chosen to reflect the largest cacheline size in the system, in order to
ensure that explicit cache maintenance as required by non-coherent DMA
masters does not affect adjacent, unrelated slab allocations. On arm64,
this value is currently set at 128 bytes.
This means that applying CRYPTO_MINALIGN_ATTR to struct shash_desc is both
unnecessary (as it is never used for DMA), and undesirable, given that it
wastes stack space (on arm64, performing the alignment costs 112 bytes in
the worst case, and the hole between the 'tfm' and '__ctx' members takes
up another 120 bytes, resulting in an increased stack footprint of up to
232 bytes.) So instead, let's switch to the minimum SLAB alignment, which
does not take DMA constraints into account.
Note that this is a no-op for x86.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2021-01-13 09:11:35 +00:00
|
|
|
#define SHASH_DESC_ON_STACK(shash, ctx) \
|
|
|
|
char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
|
|
|
|
__aligned(__alignof__(struct shash_desc)); \
|
2014-09-08 05:05:09 +00:00
|
|
|
struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
|
|
|
|
|
2014-11-12 04:26:03 +00:00
|
|
|
/**
|
|
|
|
* struct shash_alg - synchronous message digest definition
|
|
|
|
* @init: see struct ahash_alg
|
|
|
|
* @update: see struct ahash_alg
|
|
|
|
* @final: see struct ahash_alg
|
|
|
|
* @finup: see struct ahash_alg
|
|
|
|
* @digest: see struct ahash_alg
|
|
|
|
* @export: see struct ahash_alg
|
|
|
|
* @import: see struct ahash_alg
|
|
|
|
* @setkey: see struct ahash_alg
|
2019-12-08 05:42:51 +00:00
|
|
|
* @init_tfm: Initialize the cryptographic transformation object.
|
|
|
|
* This function is called only once at the instantiation
|
|
|
|
* time, right after the transformation context was
|
|
|
|
* allocated. In case the cryptographic hardware has
|
|
|
|
* some special requirements which need to be handled
|
|
|
|
* by software, this function shall check for the precise
|
|
|
|
* requirement of the transformation and put any software
|
|
|
|
* fallbacks in place.
|
|
|
|
* @exit_tfm: Deinitialize the cryptographic transformation object.
|
|
|
|
* This is a counterpart to @init_tfm, used to remove
|
|
|
|
* various changes set in @init_tfm.
|
2014-11-12 04:26:03 +00:00
|
|
|
* @digestsize: see struct ahash_alg
|
|
|
|
* @statesize: see struct ahash_alg
|
2014-11-14 04:26:21 +00:00
|
|
|
* @descsize: Size of the operational state for the message digest. This state
|
2014-11-12 04:26:03 +00:00
|
|
|
* size is the memory size that needs to be allocated for
|
|
|
|
* shash_desc.__ctx
|
|
|
|
* @base: internally used
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
struct shash_alg {
|
|
|
|
int (*init)(struct shash_desc *desc);
|
|
|
|
int (*update)(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len);
|
|
|
|
int (*final)(struct shash_desc *desc, u8 *out);
|
|
|
|
int (*finup)(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out);
|
|
|
|
int (*digest)(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out);
|
2009-07-09 12:30:57 +00:00
|
|
|
int (*export)(struct shash_desc *desc, void *out);
|
|
|
|
int (*import)(struct shash_desc *desc, const void *in);
|
2008-08-31 05:47:27 +00:00
|
|
|
int (*setkey)(struct crypto_shash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen);
|
2019-12-08 05:42:51 +00:00
|
|
|
int (*init_tfm)(struct crypto_shash *tfm);
|
|
|
|
void (*exit_tfm)(struct crypto_shash *tfm);
|
2008-08-31 05:47:27 +00:00
|
|
|
|
|
|
|
unsigned int descsize;
|
2009-07-14 04:28:26 +00:00
|
|
|
|
|
|
|
/* These fields must match hash_alg_common. */
|
2009-07-15 13:16:05 +00:00
|
|
|
unsigned int digestsize
|
|
|
|
__attribute__ ((aligned(__alignof__(struct hash_alg_common))));
|
2009-07-09 12:30:57 +00:00
|
|
|
unsigned int statesize;
|
2008-08-31 05:47:27 +00:00
|
|
|
|
|
|
|
struct crypto_alg base;
|
|
|
|
};
|
|
|
|
|
2008-07-10 08:01:22 +00:00
|
|
|
struct crypto_ahash {
|
2009-07-14 04:28:26 +00:00
|
|
|
int (*init)(struct ahash_request *req);
|
|
|
|
int (*update)(struct ahash_request *req);
|
|
|
|
int (*final)(struct ahash_request *req);
|
|
|
|
int (*finup)(struct ahash_request *req);
|
|
|
|
int (*digest)(struct ahash_request *req);
|
|
|
|
int (*export)(struct ahash_request *req, void *out);
|
|
|
|
int (*import)(struct ahash_request *req, const void *in);
|
|
|
|
int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen);
|
|
|
|
|
|
|
|
unsigned int reqsize;
|
2008-07-10 08:01:22 +00:00
|
|
|
struct crypto_tfm base;
|
|
|
|
};
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
struct crypto_shash {
|
2009-07-14 04:50:12 +00:00
|
|
|
unsigned int descsize;
|
2008-08-31 05:47:27 +00:00
|
|
|
struct crypto_tfm base;
|
|
|
|
};
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* DOC: Asynchronous Message Digest API
|
|
|
|
*
|
|
|
|
* The asynchronous message digest API is used with the ciphers of type
|
|
|
|
* CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
|
|
|
|
*
|
|
|
|
* The asynchronous cipher operation discussion provided for the
|
2019-11-09 17:09:53 +00:00
|
|
|
* CRYPTO_ALG_TYPE_SKCIPHER API applies here as well.
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
|
|
|
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
|
|
|
|
{
|
2009-07-14 04:28:26 +00:00
|
|
|
return container_of(tfm, struct crypto_ahash, base);
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_alloc_ahash() - allocate ahash cipher handle
|
|
|
|
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
|
|
|
|
* ahash cipher
|
|
|
|
* @type: specifies the type of the cipher
|
|
|
|
* @mask: specifies the mask for the cipher
|
|
|
|
*
|
|
|
|
* Allocate a cipher handle for an ahash. The returned struct
|
|
|
|
* crypto_ahash is the cipher handle that is required for any subsequent
|
|
|
|
* API invocation for that ahash.
|
|
|
|
*
|
|
|
|
* Return: allocated cipher handle in case of success; IS_ERR() is true in case
|
|
|
|
* of an error, PTR_ERR() returns the error code.
|
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
|
|
|
|
u32 mask);
|
2008-07-10 08:01:22 +00:00
|
|
|
|
|
|
|
static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return &tfm->base;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_free_ahash() - zeroize and free the ahash handle
|
|
|
|
* @tfm: cipher handle to be freed
|
2021-03-02 20:33:03 +00:00
|
|
|
*
|
|
|
|
* If @tfm is a NULL or error pointer, this function does nothing.
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline void crypto_free_ahash(struct crypto_ahash *tfm)
|
|
|
|
{
|
2009-07-14 04:28:26 +00:00
|
|
|
crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 05:52:40 +00:00
|
|
|
/**
|
|
|
|
* crypto_has_ahash() - Search for the availability of an ahash.
|
|
|
|
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
|
|
|
|
* ahash
|
|
|
|
* @type: specifies the type of the ahash
|
|
|
|
* @mask: specifies the mask for the ahash
|
|
|
|
*
|
|
|
|
* Return: true when the ahash is known to the kernel crypto API; false
|
|
|
|
* otherwise
|
|
|
|
*/
|
|
|
|
int crypto_has_ahash(const char *alg_name, u32 type, u32 mask);
|
|
|
|
|
2016-02-01 13:36:52 +00:00
|
|
|
static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline unsigned int crypto_ahash_alignmask(
|
|
|
|
struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2015-10-19 01:37:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_blocksize() - obtain block size for cipher
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The block size for the message digest cipher referenced with the cipher
|
|
|
|
* handle is returned.
|
|
|
|
*
|
|
|
|
* Return: block size of cipher
|
|
|
|
*/
|
|
|
|
static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2009-07-14 04:28:26 +00:00
|
|
|
static inline struct hash_alg_common *__crypto_hash_alg_common(
|
|
|
|
struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return container_of(alg, struct hash_alg_common, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct hash_alg_common *crypto_hash_alg_common(
|
|
|
|
struct crypto_ahash *tfm)
|
2008-07-10 08:01:22 +00:00
|
|
|
{
|
2009-07-14 04:28:26 +00:00
|
|
|
return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_digestsize() - obtain message digest size
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The size for the message digest created by the message digest cipher
|
|
|
|
* referenced with the cipher handle is returned.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Return: message digest size of cipher
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
|
|
|
|
{
|
2009-07-14 12:29:57 +00:00
|
|
|
return crypto_hash_alg_common(tfm)->digestsize;
|
2009-07-14 04:28:26 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:33:00 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_statesize() - obtain size of the ahash state
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* Return the size of the ahash state. With the crypto_ahash_export()
|
|
|
|
* function, the caller can export the state into a buffer whose size is
|
|
|
|
* defined with this function.
|
|
|
|
*
|
|
|
|
* Return: size of the ahash state
|
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_hash_alg_common(tfm)->statesize;
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
|
|
|
|
{
|
|
|
|
crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
|
|
|
|
{
|
|
|
|
crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_reqtfm() - obtain cipher handle from request
|
|
|
|
* @req: asynchronous request handle that contains the reference to the ahash
|
|
|
|
* cipher handle
|
|
|
|
*
|
|
|
|
* Return the ahash cipher handle that is registered with the asynchronous
|
|
|
|
* request handle ahash_request.
|
|
|
|
*
|
|
|
|
* Return: ahash cipher handle
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline struct crypto_ahash *crypto_ahash_reqtfm(
|
|
|
|
struct ahash_request *req)
|
|
|
|
{
|
|
|
|
return __crypto_ahash_cast(req->base.tfm);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_reqsize() - obtain size of the request data structure
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
2017-01-26 15:33:00 +00:00
|
|
|
* Return: size of the request data
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
|
|
|
|
{
|
2009-07-14 04:28:26 +00:00
|
|
|
return tfm->reqsize;
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-11-02 13:38:11 +00:00
|
|
|
static inline void *ahash_request_ctx(struct ahash_request *req)
|
|
|
|
{
|
|
|
|
return req->__ctx;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_setkey - set key for cipher handle
|
|
|
|
* @tfm: cipher handle
|
|
|
|
* @key: buffer holding the key
|
|
|
|
* @keylen: length of the key in bytes
|
|
|
|
*
|
|
|
|
* The caller provided key is set for the ahash cipher. The cipher
|
|
|
|
* handle must point to a keyed hash in order for this function to succeed.
|
|
|
|
*
|
|
|
|
* Return: 0 if the setting of the key was successful; < 0 if an error occurred
|
|
|
|
*/
|
2009-07-15 04:40:40 +00:00
|
|
|
int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen);
|
2014-11-12 04:26:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_ahash_finup() - update and finalize message digest
|
|
|
|
* @req: reference to the ahash_request handle that holds all information
|
|
|
|
* needed to perform the cipher operation
|
|
|
|
*
|
|
|
|
* This function is a "short-hand" for the function calls of
|
2017-08-17 10:11:36 +00:00
|
|
|
* crypto_ahash_update and crypto_ahash_final. The parameters have the same
|
2014-11-12 04:26:41 +00:00
|
|
|
* meaning as discussed for those separate functions.
|
|
|
|
*
|
2017-08-17 10:11:36 +00:00
|
|
|
* Return: see crypto_ahash_final()
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2009-07-15 04:40:40 +00:00
|
|
|
int crypto_ahash_finup(struct ahash_request *req);
|
2014-11-12 04:26:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_ahash_final() - calculate message digest
|
|
|
|
* @req: reference to the ahash_request handle that holds all information
|
|
|
|
* needed to perform the cipher operation
|
|
|
|
*
|
|
|
|
* Finalize the message digest operation and create the message digest
|
|
|
|
* based on all data added to the cipher handle. The message digest is placed
|
|
|
|
* into the output buffer registered with the ahash_request handle.
|
|
|
|
*
|
2017-08-17 10:11:36 +00:00
|
|
|
* Return:
|
|
|
|
* 0 if the message digest was successfully calculated;
|
2021-05-17 11:02:34 +00:00
|
|
|
* -EINPROGRESS if data is fed into hardware (DMA) or queued for later;
|
2017-08-17 10:11:36 +00:00
|
|
|
* -EBUSY if queue is full and request should be resubmitted later;
|
|
|
|
* other < 0 if an error occurred
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2009-07-15 04:40:40 +00:00
|
|
|
int crypto_ahash_final(struct ahash_request *req);
|
2014-11-12 04:26:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_ahash_digest() - calculate message digest for a buffer
|
|
|
|
* @req: reference to the ahash_request handle that holds all information
|
|
|
|
* needed to perform the cipher operation
|
|
|
|
*
|
|
|
|
* This function is a "short-hand" for the function calls of crypto_ahash_init,
|
|
|
|
* crypto_ahash_update and crypto_ahash_final. The parameters have the same
|
|
|
|
* meaning as discussed for those separate three functions.
|
|
|
|
*
|
2017-08-17 10:11:36 +00:00
|
|
|
* Return: see crypto_ahash_final()
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2009-07-15 04:40:40 +00:00
|
|
|
int crypto_ahash_digest(struct ahash_request *req);
|
2008-07-10 08:01:22 +00:00
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_export() - extract current message digest state
|
|
|
|
* @req: reference to the ahash_request handle whose state is exported
|
|
|
|
* @out: output buffer of sufficient size that can hold the hash state
|
|
|
|
*
|
|
|
|
* This function exports the hash state of the ahash_request handle into the
|
|
|
|
* caller-allocated output buffer out which must have sufficient size (e.g. by
|
2017-01-26 15:33:00 +00:00
|
|
|
* calling crypto_ahash_statesize()).
|
2014-11-12 04:26:41 +00:00
|
|
|
*
|
|
|
|
* Return: 0 if the export was successful; < 0 if an error occurred
|
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
static inline int crypto_ahash_export(struct ahash_request *req, void *out)
|
2008-11-02 13:38:11 +00:00
|
|
|
{
|
2009-07-14 04:28:26 +00:00
|
|
|
return crypto_ahash_reqtfm(req)->export(req, out);
|
2008-11-02 13:38:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_import() - import message digest state
|
|
|
|
* @req: reference to ahash_request handle the state is imported into
|
|
|
|
* @in: buffer holding the state
|
|
|
|
*
|
|
|
|
* This function imports the hash state into the ahash_request handle from the
|
|
|
|
* input buffer. That buffer should have been generated with the
|
|
|
|
* crypto_ahash_export function.
|
|
|
|
*
|
|
|
|
* Return: 0 if the import was successful; < 0 if an error occurred
|
|
|
|
*/
|
2009-07-14 04:28:26 +00:00
|
|
|
static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
|
|
|
|
{
|
crypto: hash - prevent using keyed hashes without setting key
Currently, almost none of the keyed hash algorithms check whether a key
has been set before proceeding. Some algorithms are okay with this and
will effectively just use a key of all 0's or some other bogus default.
However, others will severely break, as demonstrated using
"hmac(sha3-512-generic)", the unkeyed use of which causes a kernel crash
via a (potentially exploitable) stack buffer overflow.
A while ago, this problem was solved for AF_ALG by pairing each hash
transform with a 'has_key' bool. However, there are still other places
in the kernel where userspace can specify an arbitrary hash algorithm by
name, and the kernel uses it as unkeyed hash without checking whether it
is really unkeyed. Examples of this include:
- KEYCTL_DH_COMPUTE, via the KDF extension
- dm-verity
- dm-crypt, via the ESSIV support
- dm-integrity, via the "internal hash" mode with no key given
- drbd (Distributed Replicated Block Device)
This bug is especially bad for KEYCTL_DH_COMPUTE as that requires no
privileges to call.
Fix the bug for all users by adding a flag CRYPTO_TFM_NEED_KEY to the
->crt_flags of each hash transform that indicates whether the transform
still needs to be keyed or not. Then, make the hash init, import, and
digest functions return -ENOKEY if the key is still needed.
The new flag also replaces the 'has_key' bool which algif_hash was
previously using, thereby simplifying the algif_hash implementation.
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2018-01-03 19:16:27 +00:00
|
|
|
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
|
|
|
|
|
|
|
|
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
|
|
|
|
return -ENOKEY;
|
|
|
|
|
|
|
|
return tfm->import(req, in);
|
2009-07-14 04:28:26 +00:00
|
|
|
}
|
2008-11-02 13:38:11 +00:00
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_init() - (re)initialize message digest handle
|
|
|
|
* @req: ahash_request handle that already is initialized with all necessary
|
|
|
|
* data using the ahash_request_* API functions
|
|
|
|
*
|
|
|
|
* The call (re-)initializes the message digest referenced by the ahash_request
|
|
|
|
* handle. Any potentially existing state created by previous operations is
|
|
|
|
* discarded.
|
|
|
|
*
|
2017-08-17 10:11:36 +00:00
|
|
|
* Return: see crypto_ahash_final()
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2008-08-05 05:34:30 +00:00
|
|
|
static inline int crypto_ahash_init(struct ahash_request *req)
|
|
|
|
{
|
crypto: hash - prevent using keyed hashes without setting key
Currently, almost none of the keyed hash algorithms check whether a key
has been set before proceeding. Some algorithms are okay with this and
will effectively just use a key of all 0's or some other bogus default.
However, others will severely break, as demonstrated using
"hmac(sha3-512-generic)", the unkeyed use of which causes a kernel crash
via a (potentially exploitable) stack buffer overflow.
A while ago, this problem was solved for AF_ALG by pairing each hash
transform with a 'has_key' bool. However, there are still other places
in the kernel where userspace can specify an arbitrary hash algorithm by
name, and the kernel uses it as unkeyed hash without checking whether it
is really unkeyed. Examples of this include:
- KEYCTL_DH_COMPUTE, via the KDF extension
- dm-verity
- dm-crypt, via the ESSIV support
- dm-integrity, via the "internal hash" mode with no key given
- drbd (Distributed Replicated Block Device)
This bug is especially bad for KEYCTL_DH_COMPUTE as that requires no
privileges to call.
Fix the bug for all users by adding a flag CRYPTO_TFM_NEED_KEY to the
->crt_flags of each hash transform that indicates whether the transform
still needs to be keyed or not. Then, make the hash init, import, and
digest functions return -ENOKEY if the key is still needed.
The new flag also replaces the 'has_key' bool which algif_hash was
previously using, thereby simplifying the algif_hash implementation.
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2018-01-03 19:16:27 +00:00
|
|
|
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
|
|
|
|
|
|
|
|
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
|
|
|
|
return -ENOKEY;
|
|
|
|
|
|
|
|
return tfm->init(req);
|
2008-08-05 05:34:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* crypto_ahash_update() - add data to message digest for processing
|
|
|
|
* @req: ahash_request handle that was previously initialized with the
|
|
|
|
* crypto_ahash_init call.
|
|
|
|
*
|
|
|
|
* Updates the message digest state of the &ahash_request handle. The input data
|
|
|
|
* is pointed to by the scatter/gather list registered in the &ahash_request
|
|
|
|
* handle
|
|
|
|
*
|
2017-08-17 10:11:36 +00:00
|
|
|
* Return: see crypto_ahash_final()
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2008-08-05 05:34:30 +00:00
|
|
|
static inline int crypto_ahash_update(struct ahash_request *req)
|
|
|
|
{
|
2018-11-29 14:42:21 +00:00
|
|
|
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
|
|
|
|
struct crypto_alg *alg = tfm->base.__crt_alg;
|
|
|
|
unsigned int nbytes = req->nbytes;
|
2018-09-19 10:10:54 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-11-29 14:42:21 +00:00
|
|
|
crypto_stats_get(alg);
|
2018-09-19 10:10:54 +00:00
|
|
|
ret = crypto_ahash_reqtfm(req)->update(req);
|
2018-11-29 14:42:21 +00:00
|
|
|
crypto_stats_ahash_update(nbytes, ret, alg);
|
2018-09-19 10:10:54 +00:00
|
|
|
return ret;
|
2008-08-05 05:34:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* DOC: Asynchronous Hash Request Handle
|
|
|
|
*
|
|
|
|
* The &ahash_request data structure contains all pointers to data
|
|
|
|
* required for the asynchronous cipher operation. This includes the cipher
|
|
|
|
* handle (which can be used by multiple &ahash_request instances), pointer
|
|
|
|
* to plaintext and the message digest output buffer, asynchronous callback
|
|
|
|
* function, etc. It acts as a handle to the ahash_request_* API calls in a
|
|
|
|
* similar way as ahash handle to the crypto_ahash_* API calls.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ahash_request_set_tfm() - update cipher handle reference in request
|
|
|
|
* @req: request handle to be modified
|
|
|
|
* @tfm: cipher handle that shall be added to the request handle
|
|
|
|
*
|
|
|
|
* Allow the caller to replace the existing ahash handle in the request
|
|
|
|
* data structure with a different one.
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline void ahash_request_set_tfm(struct ahash_request *req,
|
|
|
|
struct crypto_ahash *tfm)
|
|
|
|
{
|
|
|
|
req->base.tfm = crypto_ahash_tfm(tfm);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* ahash_request_alloc() - allocate request data structure
|
|
|
|
* @tfm: cipher handle to be registered with the request
|
|
|
|
* @gfp: memory allocation flag that is handed to kmalloc by the API call.
|
|
|
|
*
|
|
|
|
* Allocate the request data structure that must be used with the ahash
|
|
|
|
* message digest API calls. During
|
|
|
|
* the allocation, the provided ahash handle
|
|
|
|
* is registered in the request data structure.
|
|
|
|
*
|
2016-04-02 15:54:56 +00:00
|
|
|
* Return: allocated request handle in case of success, or NULL if out of memory
|
2014-11-12 04:26:41 +00:00
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline struct ahash_request *ahash_request_alloc(
|
|
|
|
struct crypto_ahash *tfm, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct ahash_request *req;
|
|
|
|
|
|
|
|
req = kmalloc(sizeof(struct ahash_request) +
|
|
|
|
crypto_ahash_reqsize(tfm), gfp);
|
|
|
|
|
|
|
|
if (likely(req))
|
|
|
|
ahash_request_set_tfm(req, tfm);
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* ahash_request_free() - zeroize and free the request data structure
|
|
|
|
* @req: request data structure cipher handle to be freed
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline void ahash_request_free(struct ahash_request *req)
|
|
|
|
{
|
2020-08-07 06:18:13 +00:00
|
|
|
kfree_sensitive(req);
|
2008-07-10 08:01:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 15:17:58 +00:00
|
|
|
static inline void ahash_request_zero(struct ahash_request *req)
|
|
|
|
{
|
|
|
|
memzero_explicit(req, sizeof(*req) +
|
|
|
|
crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
|
|
|
|
}
|
|
|
|
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline struct ahash_request *ahash_request_cast(
|
|
|
|
struct crypto_async_request *req)
|
|
|
|
{
|
|
|
|
return container_of(req, struct ahash_request, base);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* ahash_request_set_callback() - set asynchronous callback function
|
|
|
|
* @req: request handle
|
|
|
|
* @flags: specify zero or an ORing of the flags
|
|
|
|
* CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
|
|
|
|
* increase the wait queue beyond the initial maximum size;
|
|
|
|
* CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
|
|
|
|
* @compl: callback function pointer to be registered with the request handle
|
|
|
|
* @data: The data pointer refers to memory that is not used by the kernel
|
|
|
|
* crypto API, but provided to the callback function for it to use. Here,
|
|
|
|
* the caller can provide a reference to memory the callback function can
|
|
|
|
* operate on. As the callback function is invoked asynchronously to the
|
|
|
|
* related functionality, it may need to access data structures of the
|
|
|
|
* related functionality which can be referenced using this pointer. The
|
|
|
|
* callback function can access the memory via the "data" field in the
|
|
|
|
* &crypto_async_request data structure provided to the callback function.
|
|
|
|
*
|
|
|
|
* This function allows setting the callback function that is triggered once
|
|
|
|
* the cipher operation completes.
|
|
|
|
*
|
|
|
|
* The callback function is registered with the &ahash_request handle and
|
2016-10-21 02:57:27 +00:00
|
|
|
* must comply with the following template::
|
2014-11-12 04:26:41 +00:00
|
|
|
*
|
|
|
|
* void callback_function(struct crypto_async_request *req, int error)
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline void ahash_request_set_callback(struct ahash_request *req,
|
|
|
|
u32 flags,
|
2014-07-25 09:53:38 +00:00
|
|
|
crypto_completion_t compl,
|
2008-07-10 08:01:22 +00:00
|
|
|
void *data)
|
|
|
|
{
|
2014-07-25 09:53:38 +00:00
|
|
|
req->base.complete = compl;
|
2008-07-10 08:01:22 +00:00
|
|
|
req->base.data = data;
|
|
|
|
req->base.flags = flags;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:26:41 +00:00
|
|
|
/**
|
|
|
|
* ahash_request_set_crypt() - set data buffers
|
|
|
|
* @req: ahash_request handle to be updated
|
|
|
|
* @src: source scatter/gather list
|
|
|
|
* @result: buffer that is filled with the message digest -- the caller must
|
|
|
|
* ensure that the buffer has sufficient space by, for example, calling
|
|
|
|
* crypto_ahash_digestsize()
|
|
|
|
* @nbytes: number of bytes to process from the source scatter/gather list
|
|
|
|
*
|
|
|
|
* By using this call, the caller references the source scatter/gather list.
|
|
|
|
* The source scatter/gather list points to the data the message digest is to
|
|
|
|
* be calculated for.
|
|
|
|
*/
|
2008-07-10 08:01:22 +00:00
|
|
|
static inline void ahash_request_set_crypt(struct ahash_request *req,
|
|
|
|
struct scatterlist *src, u8 *result,
|
|
|
|
unsigned int nbytes)
|
|
|
|
{
|
|
|
|
req->src = src;
|
|
|
|
req->nbytes = nbytes;
|
|
|
|
req->result = result;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* DOC: Synchronous Message Digest API
|
|
|
|
*
|
|
|
|
* The synchronous message digest API is used with the ciphers of type
|
|
|
|
* CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
|
|
|
|
*
|
|
|
|
* The message digest API is able to maintain state information for the
|
|
|
|
* caller.
|
|
|
|
*
|
2020-07-17 23:35:33 +00:00
|
|
|
* The synchronous message digest API can store user-related context in its
|
2014-11-12 04:27:16 +00:00
|
|
|
* shash_desc request data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_alloc_shash() - allocate message digest handle
|
|
|
|
* @alg_name: is the cra_name / name or cra_driver_name / driver name of the
|
|
|
|
* message digest cipher
|
|
|
|
* @type: specifies the type of the cipher
|
|
|
|
* @mask: specifies the mask for the cipher
|
|
|
|
*
|
|
|
|
* Allocate a cipher handle for a message digest. The returned &struct
|
|
|
|
* crypto_shash is the cipher handle that is required for any subsequent
|
|
|
|
* API invocation for that message digest.
|
|
|
|
*
|
|
|
|
* Return: allocated cipher handle in case of success; IS_ERR() is true in case
|
|
|
|
* of an error, PTR_ERR() returns the error code.
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
|
|
|
|
u32 mask);
|
|
|
|
|
2022-06-27 09:51:57 +00:00
|
|
|
int crypto_has_shash(const char *alg_name, u32 type, u32 mask);
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return &tfm->base;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_free_shash() - zeroize and free the message digest handle
|
|
|
|
* @tfm: cipher handle to be freed
|
2021-03-02 20:33:03 +00:00
|
|
|
*
|
|
|
|
* If @tfm is a NULL or error pointer, this function does nothing.
|
2014-11-12 04:27:16 +00:00
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline void crypto_free_shash(struct crypto_shash *tfm)
|
|
|
|
{
|
2009-02-05 05:51:25 +00:00
|
|
|
crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
|
2008-08-31 05:47:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 13:36:52 +00:00
|
|
|
static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline unsigned int crypto_shash_alignmask(
|
|
|
|
struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_blocksize() - obtain block size for cipher
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The block size for the message digest cipher referenced with the cipher
|
|
|
|
* handle is returned.
|
|
|
|
*
|
|
|
|
* Return: block size of cipher
|
|
|
|
*/
|
2009-02-03 01:47:44 +00:00
|
|
|
static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return container_of(alg, struct shash_alg, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_digestsize() - obtain message digest size
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The size for the message digest created by the message digest cipher
|
|
|
|
* referenced with the cipher handle is returned.
|
|
|
|
*
|
|
|
|
* Return: digest size of cipher
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_shash_alg(tfm)->digestsize;
|
|
|
|
}
|
|
|
|
|
2009-07-09 12:30:57 +00:00
|
|
|
static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_shash_alg(tfm)->statesize;
|
|
|
|
}
|
|
|
|
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
|
|
|
|
{
|
|
|
|
return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
|
|
|
|
{
|
|
|
|
crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
|
|
|
|
{
|
|
|
|
crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_descsize() - obtain the operational state size
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The size of the operational state the cipher needs during operation is
|
|
|
|
* returned for the hash referenced with the cipher handle. This size is
|
|
|
|
* required to calculate the memory requirements to allow the caller allocating
|
|
|
|
* sufficient memory for operational state.
|
|
|
|
*
|
|
|
|
* The operational state is defined with struct shash_desc where the size of
|
|
|
|
* that data structure is to be calculated as
|
|
|
|
* sizeof(struct shash_desc) + crypto_shash_descsize(alg)
|
|
|
|
*
|
|
|
|
* Return: size of the operational state
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
|
|
|
|
{
|
2009-07-14 04:50:12 +00:00
|
|
|
return tfm->descsize;
|
2008-08-31 05:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *shash_desc_ctx(struct shash_desc *desc)
|
|
|
|
{
|
|
|
|
return desc->__ctx;
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_setkey() - set key for message digest
|
|
|
|
* @tfm: cipher handle
|
|
|
|
* @key: buffer holding the key
|
|
|
|
* @keylen: length of the key in bytes
|
|
|
|
*
|
|
|
|
* The caller provided key is set for the keyed message digest cipher. The
|
|
|
|
* cipher handle must point to a keyed message digest cipher in order for this
|
|
|
|
* function to succeed.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the setting of the key was successful; < 0 if an error occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
|
|
|
|
unsigned int keylen);
|
2014-11-12 04:27:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_shash_digest() - calculate message digest for buffer
|
|
|
|
* @desc: see crypto_shash_final()
|
|
|
|
* @data: see crypto_shash_update()
|
|
|
|
* @len: see crypto_shash_update()
|
|
|
|
* @out: see crypto_shash_final()
|
|
|
|
*
|
|
|
|
* This function is a "short-hand" for the function calls of crypto_shash_init,
|
|
|
|
* crypto_shash_update and crypto_shash_final. The parameters have the same
|
|
|
|
* meaning as discussed for those separate three functions.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the message digest creation was successful; < 0 if an error
|
|
|
|
* occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out);
|
|
|
|
|
2020-05-02 05:31:03 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_tfm_digest() - calculate message digest for buffer
|
|
|
|
* @tfm: hash transformation object
|
|
|
|
* @data: see crypto_shash_update()
|
|
|
|
* @len: see crypto_shash_update()
|
|
|
|
* @out: see crypto_shash_final()
|
|
|
|
*
|
|
|
|
* This is a simplified version of crypto_shash_digest() for users who don't
|
|
|
|
* want to allocate their own hash descriptor (shash_desc). Instead,
|
|
|
|
* crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash)
|
|
|
|
* directly, and it allocates a hash descriptor on the stack internally.
|
|
|
|
* Note that this stack allocation may be fairly large.
|
|
|
|
*
|
|
|
|
* Context: Any context.
|
|
|
|
* Return: 0 on success; < 0 if an error occurred.
|
|
|
|
*/
|
|
|
|
int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
|
|
|
|
unsigned int len, u8 *out);
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_export() - extract operational state for message digest
|
|
|
|
* @desc: reference to the operational state handle whose state is exported
|
|
|
|
* @out: output buffer of sufficient size that can hold the hash state
|
|
|
|
*
|
|
|
|
* This function exports the hash state of the operational state handle into the
|
|
|
|
* caller-allocated output buffer out which must have sufficient size (e.g. by
|
|
|
|
* calling crypto_shash_descsize).
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the export creation was successful; < 0 if an error occurred
|
|
|
|
*/
|
2009-07-09 12:30:57 +00:00
|
|
|
static inline int crypto_shash_export(struct shash_desc *desc, void *out)
|
2008-11-02 13:38:11 +00:00
|
|
|
{
|
2009-07-09 12:30:57 +00:00
|
|
|
return crypto_shash_alg(desc->tfm)->export(desc, out);
|
2008-11-02 13:38:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_import() - import operational state
|
|
|
|
* @desc: reference to the operational state handle the state imported into
|
|
|
|
* @in: buffer holding the state
|
|
|
|
*
|
|
|
|
* This function imports the hash state into the operational state handle from
|
|
|
|
* the input buffer. That buffer should have been generated with the
|
|
|
|
* crypto_ahash_export function.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the import was successful; < 0 if an error occurred
|
|
|
|
*/
|
2009-07-09 12:30:57 +00:00
|
|
|
static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
|
|
|
|
{
|
crypto: hash - prevent using keyed hashes without setting key
Currently, almost none of the keyed hash algorithms check whether a key
has been set before proceeding. Some algorithms are okay with this and
will effectively just use a key of all 0's or some other bogus default.
However, others will severely break, as demonstrated using
"hmac(sha3-512-generic)", the unkeyed use of which causes a kernel crash
via a (potentially exploitable) stack buffer overflow.
A while ago, this problem was solved for AF_ALG by pairing each hash
transform with a 'has_key' bool. However, there are still other places
in the kernel where userspace can specify an arbitrary hash algorithm by
name, and the kernel uses it as unkeyed hash without checking whether it
is really unkeyed. Examples of this include:
- KEYCTL_DH_COMPUTE, via the KDF extension
- dm-verity
- dm-crypt, via the ESSIV support
- dm-integrity, via the "internal hash" mode with no key given
- drbd (Distributed Replicated Block Device)
This bug is especially bad for KEYCTL_DH_COMPUTE as that requires no
privileges to call.
Fix the bug for all users by adding a flag CRYPTO_TFM_NEED_KEY to the
->crt_flags of each hash transform that indicates whether the transform
still needs to be keyed or not. Then, make the hash init, import, and
digest functions return -ENOKEY if the key is still needed.
The new flag also replaces the 'has_key' bool which algif_hash was
previously using, thereby simplifying the algif_hash implementation.
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2018-01-03 19:16:27 +00:00
|
|
|
struct crypto_shash *tfm = desc->tfm;
|
|
|
|
|
|
|
|
if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
|
|
|
|
return -ENOKEY;
|
|
|
|
|
|
|
|
return crypto_shash_alg(tfm)->import(desc, in);
|
2009-07-09 12:30:57 +00:00
|
|
|
}
|
2008-11-02 13:38:11 +00:00
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_init() - (re)initialize message digest
|
|
|
|
* @desc: operational state handle that is already filled
|
|
|
|
*
|
|
|
|
* The call (re-)initializes the message digest referenced by the
|
|
|
|
* operational state handle. Any potentially existing state created by
|
|
|
|
* previous operations is discarded.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the message digest initialization was successful; < 0 if an
|
|
|
|
* error occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
static inline int crypto_shash_init(struct shash_desc *desc)
|
|
|
|
{
|
crypto: hash - prevent using keyed hashes without setting key
Currently, almost none of the keyed hash algorithms check whether a key
has been set before proceeding. Some algorithms are okay with this and
will effectively just use a key of all 0's or some other bogus default.
However, others will severely break, as demonstrated using
"hmac(sha3-512-generic)", the unkeyed use of which causes a kernel crash
via a (potentially exploitable) stack buffer overflow.
A while ago, this problem was solved for AF_ALG by pairing each hash
transform with a 'has_key' bool. However, there are still other places
in the kernel where userspace can specify an arbitrary hash algorithm by
name, and the kernel uses it as unkeyed hash without checking whether it
is really unkeyed. Examples of this include:
- KEYCTL_DH_COMPUTE, via the KDF extension
- dm-verity
- dm-crypt, via the ESSIV support
- dm-integrity, via the "internal hash" mode with no key given
- drbd (Distributed Replicated Block Device)
This bug is especially bad for KEYCTL_DH_COMPUTE as that requires no
privileges to call.
Fix the bug for all users by adding a flag CRYPTO_TFM_NEED_KEY to the
->crt_flags of each hash transform that indicates whether the transform
still needs to be keyed or not. Then, make the hash init, import, and
digest functions return -ENOKEY if the key is still needed.
The new flag also replaces the 'has_key' bool which algif_hash was
previously using, thereby simplifying the algif_hash implementation.
Reported-by: syzbot <syzkaller@googlegroups.com>
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2018-01-03 19:16:27 +00:00
|
|
|
struct crypto_shash *tfm = desc->tfm;
|
|
|
|
|
|
|
|
if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
|
|
|
|
return -ENOKEY;
|
|
|
|
|
|
|
|
return crypto_shash_alg(tfm)->init(desc);
|
2008-08-31 05:47:27 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:27:16 +00:00
|
|
|
/**
|
|
|
|
* crypto_shash_update() - add data to message digest for processing
|
|
|
|
* @desc: operational state handle that is already initialized
|
|
|
|
* @data: input data to be added to the message digest
|
|
|
|
* @len: length of the input data
|
|
|
|
*
|
|
|
|
* Updates the message digest state of the operational state handle.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the message digest update was successful; < 0 if an error
|
|
|
|
* occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
int crypto_shash_update(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len);
|
2014-11-12 04:27:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_shash_final() - calculate message digest
|
|
|
|
* @desc: operational state handle that is already filled with data
|
|
|
|
* @out: output buffer filled with the message digest
|
|
|
|
*
|
|
|
|
* Finalize the message digest operation and create the message digest
|
|
|
|
* based on all data added to the cipher handle. The message digest is placed
|
|
|
|
* into the output buffer. The caller must ensure that the output buffer is
|
|
|
|
* large enough by using crypto_shash_digestsize.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the message digest creation was successful; < 0 if an error
|
|
|
|
* occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
int crypto_shash_final(struct shash_desc *desc, u8 *out);
|
2014-11-12 04:27:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_shash_finup() - calculate message digest of buffer
|
|
|
|
* @desc: see crypto_shash_final()
|
|
|
|
* @data: see crypto_shash_update()
|
|
|
|
* @len: see crypto_shash_update()
|
|
|
|
* @out: see crypto_shash_final()
|
|
|
|
*
|
|
|
|
* This function is a "short-hand" for the function calls of
|
|
|
|
* crypto_shash_update and crypto_shash_final. The parameters have the same
|
|
|
|
* meaning as discussed for those separate functions.
|
|
|
|
*
|
crypto: shash - remove shash_desc::flags
The flags field in 'struct shash_desc' never actually does anything.
The only ostensibly supported flag is CRYPTO_TFM_REQ_MAY_SLEEP.
However, no shash algorithm ever sleeps, making this flag a no-op.
With this being the case, inevitably some users who can't sleep wrongly
pass MAY_SLEEP. These would all need to be fixed if any shash algorithm
actually started sleeping. For example, the shash_ahash_*() functions,
which wrap a shash algorithm with the ahash API, pass through MAY_SLEEP
from the ahash API to the shash API. However, the shash functions are
called under kmap_atomic(), so actually they're assumed to never sleep.
Even if it turns out that some users do need preemption points while
hashing large buffers, we could easily provide a helper function
crypto_shash_update_large() which divides the data into smaller chunks
and calls crypto_shash_update() and cond_resched() for each chunk. It's
not necessary to have a flag in 'struct shash_desc', nor is it necessary
to make individual shash algorithms aware of this at all.
Therefore, remove shash_desc::flags, and document that the
crypto_shash_*() functions can be called from any context.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-15 00:37:09 +00:00
|
|
|
* Context: Any context.
|
2014-11-12 04:27:16 +00:00
|
|
|
* Return: 0 if the message digest creation was successful; < 0 if an error
|
|
|
|
* occurred
|
|
|
|
*/
|
2008-08-31 05:47:27 +00:00
|
|
|
int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out);
|
|
|
|
|
2016-01-22 15:17:58 +00:00
|
|
|
static inline void shash_desc_zero(struct shash_desc *desc)
|
|
|
|
{
|
|
|
|
memzero_explicit(desc,
|
|
|
|
sizeof(*desc) + crypto_shash_descsize(desc->tfm));
|
|
|
|
}
|
|
|
|
|
2008-07-10 08:01:22 +00:00
|
|
|
#endif /* _CRYPTO_HASH_H */
|