2014-05-31 15:21:48 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2016-06-14 05:34:13 +00:00
|
|
|
#include <crypto/skcipher.h>
|
2014-05-31 15:21:48 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <crypto/internal/rng.h>
|
|
|
|
#include <crypto/rng.h>
|
|
|
|
#include <linux/fips.h>
|
2015-04-18 17:36:17 +00:00
|
|
|
#include <linux/mutex.h>
|
2014-06-28 19:58:24 +00:00
|
|
|
#include <linux/list.h>
|
2015-05-25 13:09:36 +00:00
|
|
|
#include <linux/workqueue.h>
|
2014-05-31 15:21:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenation Helper and string operation helper
|
|
|
|
*
|
|
|
|
* SP800-90A requires the concatenation of different data. To avoid copying
|
|
|
|
* buffers around or allocate additional memory, the following data structure
|
|
|
|
* is used to point to the original memory with its size. In addition, it
|
|
|
|
* is used to build a linked list. The linked list defines the concatenation
|
|
|
|
* of individual buffers. The order of memory block referenced in that
|
|
|
|
* linked list determines the order of concatenation.
|
|
|
|
*/
|
|
|
|
struct drbg_string {
|
|
|
|
const unsigned char *buf;
|
|
|
|
size_t len;
|
2014-06-28 19:58:24 +00:00
|
|
|
struct list_head list;
|
2014-05-31 15:21:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void drbg_string_fill(struct drbg_string *string,
|
|
|
|
const unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
string->buf = buf;
|
|
|
|
string->len = len;
|
2014-06-28 19:58:24 +00:00
|
|
|
INIT_LIST_HEAD(&string->list);
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-06-28 19:58:24 +00:00
|
|
|
int (*update)(struct drbg_state *drbg, struct list_head *seed,
|
2014-05-31 15:21:48 +00:00
|
|
|
int reseed);
|
|
|
|
int (*generate)(struct drbg_state *drbg,
|
|
|
|
unsigned char *buf, unsigned int buflen,
|
2014-07-06 00:25:36 +00:00
|
|
|
struct list_head *addtl);
|
2014-05-31 15:21:48 +00:00
|
|
|
int (*crypto_init)(struct drbg_state *drbg);
|
|
|
|
int (*crypto_fini)(struct drbg_state *drbg);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct drbg_test_data {
|
|
|
|
struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct drbg_state {
|
2015-04-18 17:36:17 +00:00
|
|
|
struct mutex drbg_mutex; /* lock around DRBG */
|
2014-05-31 15:21:48 +00:00
|
|
|
unsigned char *V; /* internal state 10.1.1.1 1a) */
|
2016-06-14 05:35:13 +00:00
|
|
|
unsigned char *Vbuf;
|
2014-05-31 15:21:48 +00:00
|
|
|
/* hash: static value 10.1.1.1 1b) hmac / ctr: key */
|
|
|
|
unsigned char *C;
|
2016-06-14 05:35:13 +00:00
|
|
|
unsigned char *Cbuf;
|
2014-05-31 15:21:48 +00:00
|
|
|
/* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
|
|
|
|
size_t reseed_ctr;
|
crypto: drbg - reseed often if seedsource is degraded
As required by SP800-90A, the DRBG implements are reseeding threshold.
This threshold is at 2**48 (64 bit) and 2**32 bit (32 bit) as
implemented in drbg_max_requests.
With the recently introduced changes, the DRBG is now always used as a
stdrng which is initialized very early in the boot cycle. To ensure that
sufficient entropy is present, the Jitter RNG is added to even provide
entropy at early boot time.
However, the 2nd seed source, the nonblocking pool, is usually
degraded at that time. Therefore, the DRBG is seeded with the Jitter RNG
(which I believe contains good entropy, which however is questioned by
others) and is seeded with a degradded nonblocking pool. This seed is
now used for quasi the lifetime of the system (2**48 requests is a lot).
The patch now changes the reseed threshold as follows: up until the time
the DRBG obtains a seed from a fully iniitialized nonblocking pool, the
reseeding threshold is lowered such that the DRBG is forced to reseed
itself resonably often. Once it obtains the seed from a fully
initialized nonblocking pool, the reseed threshold is set to the value
required by SP800-90A.
Signed-off-by: Stephan Mueller <smueller@chronox.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-06-10 01:33:37 +00:00
|
|
|
size_t reseed_threshold;
|
2014-05-31 15:21:48 +00:00
|
|
|
/* some memory the DRBG can use for its operation */
|
|
|
|
unsigned char *scratchpad;
|
2016-06-14 05:35:13 +00:00
|
|
|
unsigned char *scratchpadbuf;
|
2014-05-31 15:21:48 +00:00
|
|
|
void *priv_data; /* Cipher handle */
|
2016-06-14 05:34:13 +00:00
|
|
|
|
|
|
|
struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
|
|
|
|
struct skcipher_request *ctr_req; /* CTR mode request handle */
|
2016-11-29 08:45:04 +00:00
|
|
|
__u8 *outscratchpadbuf; /* CTR mode output scratchpad */
|
|
|
|
__u8 *outscratchpad; /* CTR mode aligned outbuf */
|
2017-10-18 07:00:41 +00:00
|
|
|
struct crypto_wait ctr_wait; /* CTR mode async wait obj */
|
2018-07-10 15:56:33 +00:00
|
|
|
struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
|
2016-06-14 05:34:13 +00:00
|
|
|
|
2014-05-31 15:21:48 +00:00
|
|
|
bool seeded; /* DRBG fully seeded? */
|
|
|
|
bool pr; /* Prediction resistance enabled? */
|
2019-05-08 14:19:24 +00:00
|
|
|
bool fips_primed; /* Continuous test primed? */
|
|
|
|
unsigned char *prev; /* FIPS 140-2 continuous test value */
|
2015-05-25 13:09:36 +00:00
|
|
|
struct work_struct seed_work; /* asynchronous seeding support */
|
2015-05-25 13:09:59 +00:00
|
|
|
struct crypto_rng *jent;
|
2014-05-31 15:21:48 +00:00
|
|
|
const struct drbg_state_ops *d_ops;
|
|
|
|
const struct drbg_core *core;
|
2015-04-21 02:46:41 +00:00
|
|
|
struct drbg_string test_data;
|
2015-06-09 13:55:38 +00:00
|
|
|
struct random_ready_callback random_ready;
|
2014-05-31 15:21:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-08-17 15:41:10 +00:00
|
|
|
/* SP800-90A requires the limit 2**19 bits, but we return bytes */
|
|
|
|
return (1 << 16);
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t drbg_max_addtl(struct drbg_state *drbg)
|
|
|
|
{
|
2014-08-17 15:41:10 +00:00
|
|
|
/* SP800-90A requires 2**35 bytes additional info str / pers str */
|
2014-08-26 08:29:45 +00:00
|
|
|
#if (__BITS_PER_LONG == 32)
|
|
|
|
/*
|
|
|
|
* SP800-90A allows smaller maximum numbers to be returned -- we
|
|
|
|
* return SIZE_MAX - 1 to allow the verification of the enforcement
|
|
|
|
* of this value in drbg_healthcheck_sanity.
|
|
|
|
*/
|
|
|
|
return (SIZE_MAX - 1);
|
|
|
|
#else
|
2014-08-17 15:41:10 +00:00
|
|
|
return (1UL<<35);
|
2014-08-26 08:29:45 +00:00
|
|
|
#endif
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t drbg_max_requests(struct drbg_state *drbg)
|
|
|
|
{
|
2014-08-17 15:41:10 +00:00
|
|
|
/* SP800-90A requires 2**48 maximum requests before reseeding */
|
2014-08-26 08:29:45 +00:00
|
|
|
#if (__BITS_PER_LONG == 32)
|
|
|
|
return SIZE_MAX;
|
|
|
|
#else
|
2014-08-17 15:41:10 +00:00
|
|
|
return (1UL<<48);
|
2014-08-26 08:29:45 +00:00
|
|
|
#endif
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a wrapper to the kernel crypto API function of
|
2015-04-21 02:46:41 +00:00
|
|
|
* crypto_rng_generate() to allow the caller to provide additional data.
|
2014-05-31 15:21:48 +00:00
|
|
|
*
|
|
|
|
* @drng DRBG handle -- see crypto_rng_get_bytes
|
|
|
|
* @outbuf output buffer -- see crypto_rng_get_bytes
|
|
|
|
* @outlen length of output buffer -- see crypto_rng_get_bytes
|
|
|
|
* @addtl_input additional information string input buffer
|
|
|
|
* @addtllen length of additional information string buffer
|
|
|
|
*
|
|
|
|
* return
|
|
|
|
* see crypto_rng_get_bytes
|
|
|
|
*/
|
|
|
|
static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
|
|
|
|
unsigned char *outbuf, unsigned int outlen,
|
|
|
|
struct drbg_string *addtl)
|
|
|
|
{
|
2015-04-21 02:46:41 +00:00
|
|
|
return crypto_rng_generate(drng, addtl->buf, addtl->len,
|
|
|
|
outbuf, outlen);
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TEST code
|
|
|
|
*
|
|
|
|
* This is a wrapper to the kernel crypto API function of
|
2015-04-21 02:46:41 +00:00
|
|
|
* crypto_rng_generate() to allow the caller to provide additional data and
|
2014-05-31 15:21:48 +00:00
|
|
|
* allow furnishing of test_data
|
|
|
|
*
|
|
|
|
* @drng DRBG handle -- see crypto_rng_get_bytes
|
|
|
|
* @outbuf output buffer -- see crypto_rng_get_bytes
|
|
|
|
* @outlen length of output buffer -- see crypto_rng_get_bytes
|
|
|
|
* @addtl_input additional information string input buffer
|
|
|
|
* @addtllen length of additional information string buffer
|
|
|
|
* @test_data filled test data
|
|
|
|
*
|
|
|
|
* return
|
|
|
|
* see crypto_rng_get_bytes
|
|
|
|
*/
|
|
|
|
static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
|
|
|
|
unsigned char *outbuf, unsigned int outlen,
|
|
|
|
struct drbg_string *addtl,
|
|
|
|
struct drbg_test_data *test_data)
|
|
|
|
{
|
2015-04-21 02:46:41 +00:00
|
|
|
crypto_rng_set_entropy(drng, test_data->testentropy->buf,
|
|
|
|
test_data->testentropy->len);
|
|
|
|
return crypto_rng_generate(drng, addtl->buf, addtl->len,
|
|
|
|
outbuf, outlen);
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TEST code
|
|
|
|
*
|
|
|
|
* This is a wrapper to the kernel crypto API function of
|
|
|
|
* crypto_rng_reset() to allow the caller to provide test_data
|
|
|
|
*
|
|
|
|
* @drng DRBG handle -- see crypto_rng_reset
|
|
|
|
* @pers personalization string input buffer
|
|
|
|
* @perslen length of additional information string buffer
|
|
|
|
* @test_data filled test data
|
|
|
|
*
|
|
|
|
* return
|
|
|
|
* see crypto_rng_reset
|
|
|
|
*/
|
|
|
|
static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
|
|
|
|
struct drbg_string *pers,
|
|
|
|
struct drbg_test_data *test_data)
|
|
|
|
{
|
2015-04-21 02:46:41 +00:00
|
|
|
crypto_rng_set_entropy(drng, test_data->testentropy->buf,
|
|
|
|
test_data->testentropy->len);
|
|
|
|
return crypto_rng_reset(drng, pers->buf, pers->len);
|
2014-05-31 15:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|