2019-05-27 06:55:01 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2008-08-14 12:15:52 +00:00
|
|
|
/*
|
|
|
|
* RNG: Random Number Generator algorithms under the crypto API
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
|
2015-04-21 02:46:46 +00:00
|
|
|
* Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
|
2008-08-14 12:15:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CRYPTO_RNG_H
|
|
|
|
#define _CRYPTO_RNG_H
|
|
|
|
|
2023-02-16 10:35:23 +00:00
|
|
|
#include <linux/atomic.h>
|
|
|
|
#include <linux/container_of.h>
|
2008-08-14 12:15:52 +00:00
|
|
|
#include <linux/crypto.h>
|
|
|
|
|
2015-04-21 02:46:38 +00:00
|
|
|
struct crypto_rng;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct rng_alg - random number generator definition
|
|
|
|
*
|
|
|
|
* @generate: The function defined by this variable obtains a
|
|
|
|
* random number. The random number generator transform
|
|
|
|
* must generate the random number out of the context
|
|
|
|
* provided with this call, plus any additional data
|
|
|
|
* if provided to the call.
|
|
|
|
* @seed: Seed or reseed the random number generator. With the
|
|
|
|
* invocation of this function call, the random number
|
2015-06-03 15:01:21 +00:00
|
|
|
* generator shall become ready for generation. If the
|
2015-04-21 02:46:38 +00:00
|
|
|
* random number generator requires a seed for setting
|
|
|
|
* up a new state, the seed must be provided by the
|
|
|
|
* consumer while invoking this function. The required
|
|
|
|
* size of the seed is defined with @seedsize .
|
2015-04-21 02:46:39 +00:00
|
|
|
* @set_ent: Set entropy that would otherwise be obtained from
|
|
|
|
* entropy source. Internal use only.
|
2015-04-21 02:46:38 +00:00
|
|
|
* @seedsize: The seed size required for a random number generator
|
|
|
|
* initialization defined with this variable. Some
|
|
|
|
* random number generators does not require a seed
|
|
|
|
* as the seeding is implemented internally without
|
|
|
|
* the need of support by the consumer. In this case,
|
|
|
|
* the seed size is set to zero.
|
|
|
|
* @base: Common crypto API algorithm data structure.
|
|
|
|
*/
|
|
|
|
struct rng_alg {
|
|
|
|
int (*generate)(struct crypto_rng *tfm,
|
|
|
|
const u8 *src, unsigned int slen,
|
|
|
|
u8 *dst, unsigned int dlen);
|
|
|
|
int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
|
2015-04-21 02:46:39 +00:00
|
|
|
void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
|
|
|
|
unsigned int len);
|
2015-04-21 02:46:38 +00:00
|
|
|
|
|
|
|
unsigned int seedsize;
|
|
|
|
|
|
|
|
struct crypto_alg base;
|
|
|
|
};
|
|
|
|
|
2015-04-20 05:39:03 +00:00
|
|
|
struct crypto_rng {
|
|
|
|
struct crypto_tfm base;
|
|
|
|
};
|
|
|
|
|
2008-08-14 12:15:52 +00:00
|
|
|
extern struct crypto_rng *crypto_default_rng;
|
|
|
|
|
|
|
|
int crypto_get_default_rng(void);
|
|
|
|
void crypto_put_default_rng(void);
|
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* DOC: Random number generator API
|
|
|
|
*
|
|
|
|
* The random number generator API is used with the ciphers of type
|
|
|
|
* CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto_alloc_rng() -- allocate RNG 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 random number generator. The returned struct
|
|
|
|
* crypto_rng is the cipher handle that is required for any subsequent
|
|
|
|
* API invocation for that random number generator.
|
|
|
|
*
|
|
|
|
* For all random number generators, this call creates a new private copy of
|
|
|
|
* the random number generator that does not share a state with other
|
|
|
|
* instances. The only exception is the "krng" random number generator which
|
|
|
|
* is a kernel crypto API use case for the get_random_bytes() function of the
|
|
|
|
* /dev/random driver.
|
|
|
|
*
|
|
|
|
* Return: allocated cipher handle in case of success; IS_ERR() is true in case
|
|
|
|
* of an error, PTR_ERR() returns the error code.
|
|
|
|
*/
|
2015-04-20 05:39:03 +00:00
|
|
|
struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
|
2008-08-14 12:15:52 +00:00
|
|
|
|
|
|
|
static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
|
|
|
|
{
|
|
|
|
return &tfm->base;
|
|
|
|
}
|
|
|
|
|
2023-02-16 10:35:23 +00:00
|
|
|
static inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg)
|
|
|
|
{
|
|
|
|
return container_of(alg, struct rng_alg, base);
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* crypto_rng_alg - obtain name of RNG
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* Return the generic name (cra_name) of the initialized random number generator
|
|
|
|
*
|
|
|
|
* Return: generic name string
|
|
|
|
*/
|
2008-08-14 12:15:52 +00:00
|
|
|
static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
|
|
|
|
{
|
2023-02-16 10:35:23 +00:00
|
|
|
return __crypto_rng_alg(crypto_rng_tfm(tfm)->__crt_alg);
|
2008-08-14 12:15:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* crypto_free_rng() - zeroize and free RNG 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:25:31 +00:00
|
|
|
*/
|
2008-08-14 12:15:52 +00:00
|
|
|
static inline void crypto_free_rng(struct crypto_rng *tfm)
|
|
|
|
{
|
2015-04-20 05:39:03 +00:00
|
|
|
crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
|
2008-08-14 12:15:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 05:39:04 +00:00
|
|
|
/**
|
|
|
|
* crypto_rng_generate() - get random number
|
|
|
|
* @tfm: cipher handle
|
|
|
|
* @src: Input buffer holding additional data, may be NULL
|
|
|
|
* @slen: Length of additional data
|
|
|
|
* @dst: output buffer holding the random numbers
|
|
|
|
* @dlen: length of the output buffer
|
|
|
|
*
|
|
|
|
* This function fills the caller-allocated buffer with random
|
|
|
|
* numbers using the random number generator referenced by the
|
|
|
|
* cipher handle.
|
|
|
|
*
|
|
|
|
* Return: 0 function was successful; < 0 if an error occurred
|
|
|
|
*/
|
|
|
|
static inline int crypto_rng_generate(struct crypto_rng *tfm,
|
|
|
|
const u8 *src, unsigned int slen,
|
|
|
|
u8 *dst, unsigned int dlen)
|
|
|
|
{
|
crypto: remove CONFIG_CRYPTO_STATS
Remove support for the "Crypto usage statistics" feature
(CONFIG_CRYPTO_STATS). This feature does not appear to have ever been
used, and it is harmful because it significantly reduces performance and
is a large maintenance burden.
Covering each of these points in detail:
1. Feature is not being used
Since these generic crypto statistics are only readable using netlink,
it's fairly straightforward to look for programs that use them. I'm
unable to find any evidence that any such programs exist. For example,
Debian Code Search returns no hits except the kernel header and kernel
code itself and translations of the kernel header:
https://codesearch.debian.net/search?q=CRYPTOCFGA_STAT&literal=1&perpkg=1
The patch series that added this feature in 2018
(https://lore.kernel.org/linux-crypto/1537351855-16618-1-git-send-email-clabbe@baylibre.com/)
said "The goal is to have an ifconfig for crypto device." This doesn't
appear to have happened.
It's not clear that there is real demand for crypto statistics. Just
because the kernel provides other types of statistics such as I/O and
networking statistics and some people find those useful does not mean
that crypto statistics are useful too.
Further evidence that programs are not using CONFIG_CRYPTO_STATS is that
it was able to be disabled in RHEL and Fedora as a bug fix
(https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/2947).
Even further evidence comes from the fact that there are and have been
bugs in how the stats work, but they were never reported. For example,
before Linux v6.7 hash stats were double-counted in most cases.
There has also never been any documentation for this feature, so it
might be hard to use even if someone wanted to.
2. CONFIG_CRYPTO_STATS significantly reduces performance
Enabling CONFIG_CRYPTO_STATS significantly reduces the performance of
the crypto API, even if no program ever retrieves the statistics. This
primarily affects systems with a large number of CPUs. For example,
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/2039576 reported
that Lustre client encryption performance improved from 21.7GB/s to
48.2GB/s by disabling CONFIG_CRYPTO_STATS.
It can be argued that this means that CONFIG_CRYPTO_STATS should be
optimized with per-cpu counters similar to many of the networking
counters. But no one has done this in 5+ years. This is consistent
with the fact that the feature appears to be unused, so there seems to
be little interest in improving it as opposed to just disabling it.
It can be argued that because CONFIG_CRYPTO_STATS is off by default,
performance doesn't matter. But Linux distros tend to error on the side
of enabling options. The option is enabled in Ubuntu and Arch Linux,
and until recently was enabled in RHEL and Fedora (see above). So, even
just having the option available is harmful to users.
3. CONFIG_CRYPTO_STATS is a large maintenance burden
There are over 1000 lines of code associated with CONFIG_CRYPTO_STATS,
spread among 32 files. It significantly complicates much of the
implementation of the crypto API. After the initial submission, many
fixes and refactorings have consumed effort of multiple people to keep
this feature "working". We should be spending this effort elsewhere.
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Corentin Labbe <clabbe@baylibre.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2024-03-13 03:48:21 +00:00
|
|
|
return crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);
|
2015-04-20 05:39:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* crypto_rng_get_bytes() - get random number
|
|
|
|
* @tfm: cipher handle
|
|
|
|
* @rdata: output buffer holding the random numbers
|
|
|
|
* @dlen: length of the output buffer
|
|
|
|
*
|
|
|
|
* This function fills the caller-allocated buffer with random numbers using the
|
|
|
|
* random number generator referenced by the cipher handle.
|
|
|
|
*
|
2015-03-06 07:26:31 +00:00
|
|
|
* Return: 0 function was successful; < 0 if an error occurred
|
2014-11-12 04:25:31 +00:00
|
|
|
*/
|
2008-08-14 12:15:52 +00:00
|
|
|
static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
|
|
|
|
u8 *rdata, unsigned int dlen)
|
|
|
|
{
|
2015-04-20 05:39:04 +00:00
|
|
|
return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
|
2008-08-14 12:15:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* crypto_rng_reset() - re-initialize the RNG
|
|
|
|
* @tfm: cipher handle
|
|
|
|
* @seed: seed input data
|
|
|
|
* @slen: length of the seed input data
|
|
|
|
*
|
|
|
|
* The reset function completely re-initializes the random number generator
|
|
|
|
* referenced by the cipher handle by clearing the current state. The new state
|
|
|
|
* is initialized with the caller provided seed or automatically, depending
|
|
|
|
* on the random number generator type (the ANSI X9.31 RNG requires
|
|
|
|
* caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
|
|
|
|
* The seed is provided as a parameter to this function call. The provided seed
|
|
|
|
* should have the length of the seed size defined for the random number
|
|
|
|
* generator as defined by crypto_rng_seedsize.
|
|
|
|
*
|
|
|
|
* Return: 0 if the setting of the key was successful; < 0 if an error occurred
|
|
|
|
*/
|
2015-04-21 02:46:37 +00:00
|
|
|
int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
|
|
|
|
unsigned int slen);
|
2008-08-14 12:15:52 +00:00
|
|
|
|
2014-11-12 04:25:31 +00:00
|
|
|
/**
|
|
|
|
* crypto_rng_seedsize() - obtain seed size of RNG
|
|
|
|
* @tfm: cipher handle
|
|
|
|
*
|
|
|
|
* The function returns the seed size for the random number generator
|
|
|
|
* referenced by the cipher handle. This value may be zero if the random
|
|
|
|
* number generator does not implement or require a reseeding. For example,
|
|
|
|
* the SP800-90A DRBGs implement an automated reseeding after reaching a
|
|
|
|
* pre-defined threshold.
|
|
|
|
*
|
|
|
|
* Return: seed size for the random number generator
|
|
|
|
*/
|
2008-08-14 12:15:52 +00:00
|
|
|
static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
|
|
|
|
{
|
2015-04-21 02:46:46 +00:00
|
|
|
return crypto_rng_alg(tfm)->seedsize;
|
2008-08-14 12:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|