mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-07-22 02:17:36 -04:00
FIPS 140-2 required that a continuous test for repeated outputs be done on both "Approved RNGs" and "Non-Approved RNGs". That's apparently why crypto/drbg.c does such a test on the bytes it pulls from get_random_bytes(), despite get_random_bytes() being a "Non-Approved RNG" that is credited with zero entropy for FIPS purposes. (From FIPS's point of view, the "Approved RNG" is jitterentropy.) FIPS 140-3 "modernized" the continuous RNG test requirements. They're now a bit more sophisticated, requiring both an "Adaptive Proportion Test" and a "Repetition Count Test". At the same time, FIPS 140-3 doesn't require continuous RNG tests on "Non-Approved RNGs" if a "vetted conditioning component" is used. The SP800-90A DRBGs are exactly such a vetted conditioning component, by their design. (In the case of HASH_DRBG and CTR_DRBG, the derivation function does have to be implemented. But the kernel does that.) In other words: from FIPS 140-3's point of view, get_random_bytes() still produces zero entropy, but the way the DRBG combines those bytes with the jitterentropy bytes preserves all the "approved" entropy from jitterentropy. Thus no test for get_random_bytes() is required. Seeing as FIPS 140-2 certificates stopped being issued in 2021 in favor of FIPS 140-3, this means this code is obsolete. Remove it. Signed-off-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
182 lines
5.9 KiB
C
182 lines
5.9 KiB
C
/*
|
|
* DRBG based on NIST SP800-90A
|
|
*
|
|
* Copyright Stephan Mueller <smueller@chronox.de>, 2014
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, and the entire permission notice in its entirety,
|
|
* including the disclaimer of warranties.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
* products derived from this software without specific prior
|
|
* written permission.
|
|
*
|
|
* ALTERNATIVELY, this product may be distributed under the terms of
|
|
* the GNU General Public License, in which case the provisions of the GPL are
|
|
* required INSTEAD OF the above restrictions. (This clause is
|
|
* necessary due to a potential bad interaction between the GPL and
|
|
* the restrictions contained in a BSD-style copyright.)
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
|
|
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
|
|
* DAMAGE.
|
|
*/
|
|
|
|
#ifndef _DRBG_H
|
|
#define _DRBG_H
|
|
|
|
|
|
#include <linux/random.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <crypto/hash.h>
|
|
#include <crypto/skcipher.h>
|
|
#include <linux/module.h>
|
|
#include <linux/crypto.h>
|
|
#include <linux/slab.h>
|
|
#include <crypto/internal/drbg.h>
|
|
#include <crypto/internal/rng.h>
|
|
#include <crypto/rng.h>
|
|
#include <linux/fips.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/list.h>
|
|
#include <linux/workqueue.h>
|
|
|
|
struct drbg_state;
|
|
typedef uint32_t drbg_flag_t;
|
|
|
|
struct drbg_core {
|
|
drbg_flag_t flags; /* flags for the cipher */
|
|
__u8 statelen; /* maximum state length */
|
|
__u8 blocklen_bytes; /* block size of output in bytes */
|
|
char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
|
|
/* kernel crypto API backend cipher name */
|
|
char backend_cra_name[CRYPTO_MAX_ALG_NAME];
|
|
};
|
|
|
|
struct drbg_state_ops {
|
|
int (*update)(struct drbg_state *drbg, struct list_head *seed,
|
|
int reseed);
|
|
int (*generate)(struct drbg_state *drbg,
|
|
unsigned char *buf, unsigned int buflen,
|
|
struct list_head *addtl);
|
|
int (*crypto_init)(struct drbg_state *drbg);
|
|
int (*crypto_fini)(struct drbg_state *drbg);
|
|
|
|
};
|
|
|
|
enum drbg_seed_state {
|
|
DRBG_SEED_STATE_UNSEEDED,
|
|
DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */
|
|
DRBG_SEED_STATE_FULL,
|
|
};
|
|
|
|
struct drbg_state {
|
|
struct mutex drbg_mutex; /* lock around DRBG */
|
|
unsigned char *V; /* internal state 10.1.1.1 1a) */
|
|
unsigned char *Vbuf;
|
|
/* hash: static value 10.1.1.1 1b) hmac / ctr: key */
|
|
unsigned char *C;
|
|
unsigned char *Cbuf;
|
|
/* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
|
|
size_t reseed_ctr;
|
|
size_t reseed_threshold;
|
|
/* some memory the DRBG can use for its operation */
|
|
unsigned char *scratchpad;
|
|
unsigned char *scratchpadbuf;
|
|
void *priv_data; /* Cipher handle */
|
|
|
|
struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
|
|
struct skcipher_request *ctr_req; /* CTR mode request handle */
|
|
__u8 *outscratchpadbuf; /* CTR mode output scratchpad */
|
|
__u8 *outscratchpad; /* CTR mode aligned outbuf */
|
|
struct crypto_wait ctr_wait; /* CTR mode async wait obj */
|
|
struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
|
|
|
|
enum drbg_seed_state seeded; /* DRBG fully seeded? */
|
|
unsigned long last_seed_time;
|
|
bool pr; /* Prediction resistance enabled? */
|
|
struct crypto_rng *jent;
|
|
const struct drbg_state_ops *d_ops;
|
|
const struct drbg_core *core;
|
|
struct drbg_string test_data;
|
|
};
|
|
|
|
static inline __u8 drbg_statelen(struct drbg_state *drbg)
|
|
{
|
|
if (drbg && drbg->core)
|
|
return drbg->core->statelen;
|
|
return 0;
|
|
}
|
|
|
|
static inline __u8 drbg_blocklen(struct drbg_state *drbg)
|
|
{
|
|
if (drbg && drbg->core)
|
|
return drbg->core->blocklen_bytes;
|
|
return 0;
|
|
}
|
|
|
|
static inline __u8 drbg_keylen(struct drbg_state *drbg)
|
|
{
|
|
if (drbg && drbg->core)
|
|
return (drbg->core->statelen - drbg->core->blocklen_bytes);
|
|
return 0;
|
|
}
|
|
|
|
static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
|
|
{
|
|
/* SP800-90A requires the limit 2**19 bits, but we return bytes */
|
|
return (1 << 16);
|
|
}
|
|
|
|
/*
|
|
* SP800-90A allows implementations to support additional info / personalization
|
|
* strings of up to 2**35 bits. Implementations can have a smaller maximum. We
|
|
* use 2**35 - 16 bits == U32_MAX - 1 bytes so that the max + 1 always fits in a
|
|
* size_t, allowing drbg_healthcheck_sanity() to verify its enforcement.
|
|
*/
|
|
static inline size_t drbg_max_addtl(struct drbg_state *drbg)
|
|
{
|
|
return U32_MAX - 1;
|
|
}
|
|
|
|
static inline size_t drbg_max_requests(struct drbg_state *drbg)
|
|
{
|
|
/* SP800-90A requires 2**48 maximum requests before reseeding */
|
|
return (1<<20);
|
|
}
|
|
|
|
/* DRBG type flags */
|
|
#define DRBG_CTR ((drbg_flag_t)1<<0)
|
|
#define DRBG_HMAC ((drbg_flag_t)1<<1)
|
|
#define DRBG_HASH ((drbg_flag_t)1<<2)
|
|
#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
|
|
/* DRBG strength flags */
|
|
#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
|
|
#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
|
|
#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
|
|
#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
|
|
DRBG_STRENGTH256)
|
|
|
|
enum drbg_prefixes {
|
|
DRBG_PREFIX0 = 0x00,
|
|
DRBG_PREFIX1,
|
|
DRBG_PREFIX2,
|
|
DRBG_PREFIX3
|
|
};
|
|
|
|
#endif /* _DRBG_H */
|