Seeding DRBG with more entropy
Subsystems (3)
v4 cover Seeding DRBG with more entropy
Hi,
as of now, the DRBG is only seeded from get_random_bytes. In various circumstances, the nonblocking_pool behind get_random_bytes may not be fully seeded from hardware events at the time the DRBG requires to be seeded. Based on the discussion in [1], the DRBG seeding is updated such that it does not completely rely on get_random_bytes any more.
The seeding approach can be characterized as follows:
1. pull buffer of size entropy + nonce from get_random_bytes
2. pull another buffer of size entropy + nonce from my Jitter RNG
3. concatenate both buffers
4. seed the DRBG with the concatenated buffer
5. trigger the async invocation of the in-kernel /dev/random with a buffer of size entropy
6. return the DRBG instance to the caller without waiting for the completion of step 5
7. at some point in time, the in-kernel /dev/random returns with a full buffer which is then used to re-seed the DRBG
This way, we will get entropy during the first initialization without blocking.
The patch set adds an in-kernel /dev/random equivalent that was discussed with Ted Ts'o last July -- see [2] and [3]. A test module for testing the asynchronous operation of the in-kernel /dev/random is given with the code below.
Ted: shall we really create and maintain a new entropy pool (the kernel_pool), or should the in-kernel /dev/random logic draw directly from the input_pool? In other words, shall we drop the first patch and update the 2nd patch to use input_pool? Also, I would not recommend using the blocking_pool as this would mix kernel and user land operation.
Note: the DRBG and Jitter RNG patches are against the current cryptodev-2.6 tree.
The new Jitter RNG is an RNG that has large set of tests and was presented on LKML some time back. After speaking with mathematicians at NIST, that Jitter RNG approach would be acceptable from their side as a noise source. Note, I personally think that the Jitter RNG has sufficient entropy in almost all circumstances (see the massive testing I conducted on all more widely used CPUs as shown in [4]).
Changes v4:
* Patch 02: Change get_blocking_random_bytes_cb to allow callers to call it multiple times without re-initializing the work data structure. Furthermore, only change the pointers to the output buffer and callback if work is not pending to avoid race conditions.
* Patch 04: No canceling of seeding during drbg_seed as the invocation of get_blocking_random_bytes_cb can now be done repeatedly without re-initializing the work data structure.
Changes v3:
* Patch 01: Correct calculation of entropy count as pointed out by Herbert Xu
* Patch 06: Correct a trivial coding issue in jent_entropy_init for checking JENT_EMINVARVAR reported by cppcheck
Changes v2:
* Use Dual BSD/GPL license in MODULE_LICENSE as suggested by
Paul Bolle <[email protected]>
* Patch 05, drbg_dealloc_state: only deallocate Jitter RNG if one was instantiated in the first place. There are two main reasons why the Jitter RNG may not be allocated: either it is not available as kernel module/in vmlinuz or during init time of the Jitter RNG, the performed testing shows that the underlying hardware is not suitable for the Jitter RNG (e.g. has a too coarse timer).
[1] http://www.mail-archive.com/[email protected]/msg13891.html
[2] https://lkml.org/lkml/2014/4/27/174
[3] http://comments.gmane.org/gmane.linux.kernel/1701117
[4] http://www.chronox.de/jent.html
Stephan Mueller (6):
random: Addition of kernel_pool
random: Async and sync API for accessing kernel_pool
crypto: drbg - prepare for async seeding
crypto: drbg - add async seeding operation
crypto: drbg - use Jitter RNG to obtain seed
crypto: add jitterentropy RNG
crypto/Kconfig | 10 + crypto/Makefile | 2 + crypto/drbg.c | 156 +++++++-- crypto/jitterentropy.c | 909 +++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.c | 4 + drivers/char/random.c | 175 +++++++++- include/crypto/drbg.h | 4 + include/linux/random.h | 20 ++ 8 files changed, 1240 insertions(+), 40 deletions(-) create mode 100644 crypto/jitterentropy.c
---
/*
* Test module for verifying the correct operation of the
* in-kernel /dev/random handling
*
* Use: compile, load into the kernel and observe dmesg
*
* Written by: Stephan Mueller <[email protected]>
* Copyright (c) 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.
*/
#include <linux/random.h>
#include <linux/module.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");MODULE_AUTHOR("Stephan Mueller <[email protected]>"); MODULE_DESCRIPTION("Test module for verifying the correct operation of async LRNG operation");
static struct random_work my_work;
static struct random_work my_work1;
static struct random_work my_work2;
static struct random_work my_work3;
static struct random_work my_work4;
static struct random_work my_work5;
static struct random_work my_work6;
static struct random_work my_work7;
static struct random_work my_work8;
static struct random_work my_work9;
#define LEN 10
static char buffer[LEN];
static char *string = "a";
static char *string2 = "b";
static char *string3 = "c";
static char *string4 = "d";
static char *string5 = "e";
static char *string6 = "f";
static char *string7 = "g";
static char *string8 = "h";
static char *string9 = "i";
void lrng_cb(void *buf, ssize_t buflen, void *private)
{
printk("received %d bytes for %s\n", (int)buflen, (char *)private);
}static void cn(struct random_work *work)
{
get_blocking_random_bytes_cancel(work);
cancel_work_sync(&work->rw_work);
}static int __init lrng_init(void)
{
get_blocking_random_bytes_cb(NULL, &my_work, buffer, LEN,
string, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work1, buffer, LEN,
string, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work2, buffer, LEN,
string2, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work3, buffer, LEN,
string3, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work4, buffer, LEN,
string4, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work5, buffer, LEN,
string5, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work6, buffer, LEN,
string6, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work7, buffer, LEN,
string7, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work8, buffer, LEN,
string8, lrng_cb);
get_blocking_random_bytes_cb(NULL, &my_work9, buffer, LEN,
string9, lrng_cb);
/*
* cancel one work to see that work can be canceled independent from
* others
*/
cn(&my_work);
/* try again to verify that double cancellation is harmless */
cn(&my_work);
return 0;
}static void __exit lrng_exit(void)
{
cn(&my_work1);
cn(&my_work2);
cn(&my_work3);
cn(&my_work4);
cn(&my_work5);
cn(&my_work6);
cn(&my_work7);
cn(&my_work8);
cn(&my_work9);
}module_init(lrng_init);module_exit(lrng_exit);
Signature · 1 line
2.1.0
PATCH v4 1/6 random: Addition of kernel_pool
The kernel pool is intended to serve kernel-internal callers only. Its purpose and usage is identical to the blocking_pool.
As the kernel_pool is not available to user space, user space cannot directly interfere with the blocking behavior when obtaining data from the kernel_pool. Thus, if entropy is present in the kernel_pool, user space can hog /dev/random and yet the kernel internal requestor of random numbers that are generated equally to the blocking_pool (i.e. with the blocking behavior) will not be affected until data is needed from the input_pool.
The patch treats the kernel_pool fully equally to the blocking and nonblocking pool with respect to the initialization and update. As now there are three output pools, the patch adds a round-robin logic for processing additional entropy when the input_pool is nearly full.
--- drivers/char/random.c | 52 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-)
drivers/char/random.c +39 -13
@@ -407,6 +407,7 @@ static struct poolinfo {static DECLARE_WAIT_QUEUE_HEAD (random_read_wait);static DECLARE_WAIT_QUEUE_HEAD (random_write_wait);static DECLARE_WAIT_QUEUE_HEAD (urandom_init_wait); +static DECLARE_WAIT_QUEUE_HEAD (random_kernel_wait);static struct fasync_struct *fasync;/**********************************************************************
@@ -442,6 +443,7 @@ static void push_to_pool(struct work_struct *work);static __u32 input_pool_data[INPUT_POOL_WORDS ];static __u32 blocking_pool_data[OUTPUT_POOL_WORDS ];static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS ]; +static __u32 kernel_pool_data[OUTPUT_POOL_WORDS ];static struct entropy_store input_pool = { .poolinfo = &poolinfo_table[0 ],
@@ -472,6 +474,17 @@ static struct entropy_store nonblocking_pool = { push_to_pool), }; +static struct entropy_store kernel_pool = { + .poolinfo = &poolinfo_table[1 ], + .name ="kernel" , + .limit =1 , + .pull = &input_pool, + .lock =__SPIN_LOCK_UNLOCKED (kernel_pool.lock), + .pool = kernel_pool_data, + .push_work =__WORK_INITIALIZER (kernel_pool.push_work, + push_to_pool), +}; +static __u32 const twist_table[8 ] = {0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 ,0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 };
@@ -674,29 +687,41 @@ retry:/* should we wake readers? */ if (entropy_bits >= random_read_wakeup_bits) { +wake_up_interruptible (&random_kernel_wait);wake_up_interruptible (&random_read_wait);kill_fasync (&fasync,SIGIO ,POLL_IN ); }/* If the input pool is getting full, send some -* entropy to the two output pools, flipping back and +* entropy to the output pools, flipping back and * forth between them, until the output pools are 75% * full. */ if (entropy_bits > random_write_wakeup_bits && r->initialized && r->entropy_total >=2 *random_read_wakeup_bits) { -static struct entropy_store *last = &blocking_pool; -struct entropy_store *other = &blocking_pool; - -if (last == &blocking_pool) - other = &nonblocking_pool; -if (other->entropy_count <= -3 * other->poolinfo->poolfracbits /4 ) - last = other; -if (last->entropy_count <= -3 * last->poolinfo->poolfracbits /4 ) { -schedule_work (&last->push_work); - r->entropy_total =0 ; +#define NUM_OUTPUT_POOLS 3 +/* as we will recalculate this variable first thing in +* the loop, it will point to the first output pool +* after the first recalculation */ +static int selected_pool = (NUM_OUTPUT_POOLS -1 ); +int i =0 ; +struct entropy_store *output_pools[NUM_OUTPUT_POOLS ] = { + &blocking_pool, + &nonblocking_pool, + &kernel_pool}; +/* select the next pool that has less than 75% fill +* rate */ +for (i =0 ;NUM_OUTPUT_POOLS > i; i++) { +struct entropy_store *p =NULL ; + selected_pool = + (selected_pool +1 ) %NUM_OUTPUT_POOLS ; + p = output_pools[selected_pool]; +if (p->entropy_count <= +3 * p->poolinfo->poolfracbits /4 ) { +schedule_work (&p->push_work); + r->entropy_total =0 ; +break ; + } } } }
@@ -1318,6 +1343,7 @@ static int rand_initialize(void)init_std_data (&input_pool);init_std_data (&blocking_pool);init_std_data (&nonblocking_pool); +init_std_data (&kernel_pool);return 0 ; }early_initcall (rand_initialize);
Signature · 1 line
2.1.0
PATCH v4 2/6 random: Async and sync API for accessing kernel_pool
In reply to Stephan MuellerThe kernel_pool is intended to be the in-kernel equivalent to the blocking_pool, i.e. requests for random data may be blocked if insufficient entropy is present.
The added API calls provide a synchronous function call get_blocking_random_bytes where the caller is blocked.
In addition, an asynchronous API call of get_blocking_random_bytes_cb is provided which returns immediately to the caller after submitting the request for random data. The caller-provided buffer that shall be filled with random data is filled up as available entropy permits. The caller may provide a callback function that is invoked once the request is completed.
A third API call, get_blocking_random_bytes_cancel, is provided to cancel the random number gathering operation.
--- drivers/char/random.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/random.h | 20 ++++++++ 2 files changed, 143 insertions(+)
drivers/char/random.c +123 -0
@@ -1804,3 +1804,126 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,credit_entropy_bits (poolp, entropy); }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness); + +static bool get_blocking_random_bytes_term (bool *cancel) +{ +if (ENTROPY_BITS (&input_pool) >= random_read_wakeup_bits) +return true; +return *cancel; +} + +/* +* Equivalent function to get_random_bytes with the difference that this +* function blocks the request in a similar fashion as random_read(), +* implementing a /dev/random device for in-kernel users. +* +* This function may sleep. +* +* @buf caller allocated buffer filled with random data +* @nbytes requested number of bytes -- buffer should be at least as big +* @cancel pointer to variable that can be used to cancel the collection +* operation. If this boolean is set to true, the collection operation +* is terminated immediately. When it is set to true during the +* collection loop, the collection is terminated immediately. +* +* return: positive value: obtained number of bytes on successful +* negative value: error code on error +*/ +ssize_t get_blocking_random_bytes (void *buf,ssize_t nbytes,bool *cancel) +{ +ssize_t ret =0 ; + +if (nbytes <=0 ) +return nbytes; +BUG_ON (!buf); + +while (ret < nbytes) { +ssize_t round =0 ; +ssize_t pull =min_t (ssize_t, (nbytes - ret),SEC_XFER_SIZE ); + +if (*cancel) +return ret; + round =extract_entropy (&kernel_pool, (buf + ret), pull,0 ,0 ); +if (0 > round) +return round; +if (0 == round) +wait_event_interruptible (random_kernel_wait, +get_blocking_random_bytes_term (cancel)); + ret += round; + } +return ret; +} +EXPORT_SYMBOL (get_blocking_random_bytes); + +/* +* Immediate canceling the collection operation for the random_work +*/ +void get_blocking_random_bytes_cancel (struct random_work *rw) +{ + rw->cancel = true; +wake_up_interruptible (&random_kernel_wait); + +} +EXPORT_SYMBOL (get_blocking_random_bytes_cancel); + +static void get_blocking_random_bytes_work (struct work_struct *work) +{ + struct random_work *rw = container_of(work, struct random_work, + rw_work); +ssize_t ret; + + ret =get_blocking_random_bytes (rw->rw_buf, rw->rw_len, &rw->cancel); +if (rw->rw_cb) + rw->rw_cb (rw->rw_buf, ret, rw->private); +} + +/* +* Asynchronous invocation of the blocking interface. The function +* queues the request in either the private work queue supplied with the +* wq argument or in the general work queue framework if wq is NULL. +* Once the request is completed or upon receiving an error, the callback +* function of cb is called, if not NULL, to inform the caller about the +* completion of its operation. +* +* Only if no work is pending, the pointers for the callback function +* or the output data can be changed. +* +* If a caller wants to cancel the work (e.g. in the module_exit function), +* simply call +* get_blocking_random_bytes_cancel(&my_random_work); +* cancel_work_sync(&my_random_work.rw_work); +* +* @wq pointer to private work queue or NULL - input +* @rw handle to the work queue frame - output +* @buf allocated buffer where random numbers are to be stored +* @nbytes size of buf and implicitly number of bytes requested +* @private pointer to data that is not processed by here, but handed to the +* callback function to allow the caller to maintain a state +* @cb callback function where +* * buf holds the pointer to buf will be supplied +* * buflen holds the length of the gathered random numbers or error code +* of the generation function. +* * private provides a reference to the private data pointer +*/ +void get_blocking_random_bytes_cb (struct workqueue_struct *wq, +struct random_work *rw, +u8 *buf,ssize_t nbytes,void *private, +void (*cb)(void *buf,ssize_t buflen, +void *private)) +{ +BUG_ON (!cb); +if (!rw->cb) +INIT_WORK (&rw->rw_work, get_blocking_random_bytes_work); +if (!work_pending (&rw->rw_work)) { + rw->rw_buf = buf; + rw->rw_len = nbytes; + rw->private = private; + rw->rw_cb = cb; + rw->cancel = false; + | +if (wq) +queue_work (wq, &rw->rw_work); +else +schedule_work (&rw->rw_work); +} +EXPORT_SYMBOL (get_blocking_random_bytes_cb);
include/linux/random.h +20 -0
@@ -7,6 +7,7 @@#define _LINUX_RANDOM_H#include <uapi/linux/random.h> +#include <linux/workqueue.h> extern void add_device_randomness (const void *,unsigned int ); extern void add_input_randomness(unsigned int type, unsigned int code,
@@ -112,4 +113,23 @@ static inline u32 next_pseudo_random32(u32 seed) return seed * 1664525 + 1013904223; } +/* API for asynchronous in-kernel operation */ +struct random_work { +struct work_struct rw_work; +u8 *rw_buf; +ssize_t rw_len; +void *private; +void (*rw_cb)(void *buf,ssize_t buflen, +void *private); +bool cancel; +}; + +ssize_t get_blocking_random_bytes (void *buf,ssize_t nbytes,bool *cancel); +void get_blocking_random_bytes_cancel (struct random_work *rw); +void get_blocking_random_bytes_cb (struct workqueue_struct *wq, +struct random_work *rw, +u8 *buf,ssize_t nbytes,void *private, +void (*cb)(void *buf,ssize_t buflen, +void *private)); +#endif /* _LINUX_RANDOM_H */
Signature · 1 line
2.1.0
Re: [PATCH v4 2/6] random: Async and sync API for accessing kernel_pool
Quoted 3 lines · Stephan Mueller
The
kernel_poolis intended to be the in-kernel equivalent to theblocking_pool, i.e. requests for random data may be blocked if insufficient entropy is present.
I cannot see any reason this would be useful, let alone necessary.
Of course /dev/random should block and it seems to me there is a good argument for making both /dev/urandom and get_random_bytes() block until there is emough entropy to seed them well. For everything else, though, a properly seeded PRNG seems adequate so there is no reason to block.
PATCH v4 3/6 crypto: drbg - prepare for async seeding
In reply to Stephan MuellerIn order to prepare for the addition of the asynchronous seeding call, the invocation of seeding the DRBG is moved out into a helper function.
In addition, a block of memory is allocated during initialization time that will be used as a scratchpad for obtaining entropy. That scratchpad is used for the initial seeding operation as well as by the asynchronous seeding call. The memory must be zeroized every time the DRBG seeding call succeeds to avoid entropy data lingering in memory.
--- crypto/drbg.c | 81 ++++++++++++++++++++++++++++++++++----------------- include/crypto/drbg.h | 2 ++ 2 files changed, 56 insertions(+), 27 deletions(-)
crypto/drbg.c +54 -27
@@ -1041,6 +1041,21 @@ static struct drbg_state_ops drbg_hash_ops = {* Functions common for DRBG implementations ******************************************************************/ +static inline int __drbg_seed (struct drbg_state *drbg,struct list_head *seed, +int reseed) +{ +int ret = drbg->d_ops->update (drbg, seed, reseed); + +if (ret) +return ret; + + drbg->seeded = true; +/* 10.1.1.2 / 10.1.1.3 step 5 */ + drbg->reseed_ctr =1 ; + +return ret; +} +/* * Seeding or reseeding of the DRBG *
@@ -1056,8 +1071,6 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, bool reseed) {int ret =0 ; -unsigned char *entropy =NULL ; -size_t entropylen =0 ;struct drbg_string data1;LIST_HEAD (seedlist);
@@ -1073,26 +1086,10 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, drbg->test_data.len);pr_devel ("DRBG: using test entropy\n" ); }else { -/* -* Gather entropy equal to the security strength of the DRBG. -* With a derivation function, a nonce is required in addition -* to the entropy. A nonce must be at least 1/2 of the security -* strength of the DRBG in size. Thus, entropy * nonce is 3/2 -* of the strength. The consideration of a nonce is only -* applicable during initial seeding. -*/ - entropylen =drbg_sec_strength (drbg->core->flags); -if (!entropylen) -return -EFAULT ; -if (!reseed) - entropylen = ((entropylen +1 ) /2 ) *3 ;pr_devel ("DRBG: (re)seeding with %zu bytes of entropy\n" , - entropylen); - entropy =kzalloc (entropylen,GFP_KERNEL ); -if (!entropy) -return -ENOMEM ; -get_random_bytes (entropy, entropylen); -drbg_string_fill (&data1, entropy, entropylen); + drbg->seed_buf_len); +get_random_bytes (drbg->seed_buf, drbg->seed_buf_len); +drbg_string_fill (&data1, drbg->seed_buf, drbg->seed_buf_len); }list_add_tail (&data1.list, &seedlist);
@@ -1111,16 +1108,24 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,memset (drbg->C,0 ,drbg_statelen (drbg)); } - ret = drbg->d_ops->update (drbg, &seedlist, reseed); + ret =__drbg_seed (drbg, &seedlist, reseed); + +/* +* Clear the initial entropy buffer as the async call may not overwrite +* that buffer for quite some time. +*/ +memzero_explicit (drbg->seed_buf, drbg->seed_buf_len);if (ret) goto out; - - drbg->seeded = true; -/* 10.1.1.2 / 10.1.1.3 step 5 */ - drbg->reseed_ctr =1 ; +/* +* For all subsequent seeding calls, we only need the seed buffer +* equal to the security strength of the DRBG. We undo the calculation +* in drbg_alloc_state. +*/ +if (!reseed) + drbg->seed_buf_len = drbg->seed_buf_len /3 *2 ; out: -kzfree (entropy);return ret; }
@@ -1143,6 +1148,8 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg) drbg->prev =NULL ; drbg->fips_primed = false;#endif +kzfree (drbg->seed_buf); + drbg->seed_buf =NULL ; }/*
@@ -1204,6 +1211,26 @@ static inline int drbg_alloc_state(struct drbg_state *drbg)if (!drbg->scratchpad) goto err; } + +/* +* Gather entropy equal to the security strength of the DRBG. +* With a derivation function, a nonce is required in addition +* to the entropy. A nonce must be at least 1/2 of the security +* strength of the DRBG in size. Thus, entropy * nonce is 3/2 +* of the strength. The consideration of a nonce is only +* applicable during initial seeding. +*/ + drbg->seed_buf_len =drbg_sec_strength (drbg->core->flags); +if (!drbg->seed_buf_len) { + ret = -EFAULT ; + goto err; + } +/* ensure we have sufficient buffer space for initial seed */ + drbg->seed_buf_len = ((drbg->seed_buf_len +1 ) /2 ) *3 ; + drbg->seed_buf =kzalloc (drbg->seed_buf_len,GFP_KERNEL ); +if (!drbg->seed_buf) + goto err; +return 0 ; err:
include/crypto/drbg.h +2 -0
@@ -119,6 +119,8 @@ struct drbg_state {bool fips_primed;/* Continuous test primed? */ unsigned char *prev;/* FIPS 140-2 continuous test value */ #endif +u8 *seed_buf;/* buffer holding the seed */ +size_t seed_buf_len;const struct drbg_state_ops *d_ops;const struct drbg_core *core;struct drbg_string test_data;
Signature · 1 line
2.1.0
PATCH v4 4/6 crypto: drbg - add async seeding operation
In reply to Stephan MuellerThe async seeding operation is triggered during initalization right after the first non-blocking seeding is completed. As required by the asynchronous operation of random.c, a callback function is provided that is triggered by random.c once entropy is available. That callback function performs the actual seeding of the DRBG.
--- crypto/drbg.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/crypto/drbg.h | 1 + 2 files changed, 42 insertions(+)
crypto/drbg.c +41 -0
@@ -1056,6 +1056,40 @@ static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,return ret; } +/* DRBG callback for obtaining data from the async Linux RNG */ +static void drbg_async_seed_cb (void *buf,ssize_t buflen,void *private) +{ +struct drbg_string data; +LIST_HEAD (seedlist); +struct drbg_state *drbg = (struct drbg_state *)private; +int ret =0 ; + +if (buflen <=0 || !buf) +return ; + +drbg_string_fill (&data, buf, buflen); +list_add_tail (&data.list, &seedlist); +/* sanity check to verify that there is still a DRBG instance */ +if (!drbg) +return ; +mutex_lock (&drbg->drbg_mutex); +/* sanity check to verify that the DRBG instance is valid */ +if (!drbg->V) { +mutex_unlock (&drbg->drbg_mutex); +return ; + } + ret =__drbg_seed (drbg, &seedlist, true); +memzero_explicit (buf, buflen); +mutex_unlock (&drbg->drbg_mutex); +} + +/* Cancel any outstanding async operation and wait for their completion */ +static inline void drbg_async_work_cancel (struct random_work *work) +{ +get_blocking_random_bytes_cancel (work); +cancel_work_sync (&work->rw_work); +} +/* * Seeding or reseeding of the DRBG *
@@ -1125,6 +1159,12 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,if (!reseed) drbg->seed_buf_len = drbg->seed_buf_len /3 *2 ; +/* Invoke asynchronous seeding unless DRBG is in test mode. */ +if (!list_empty (&drbg->test_data.list)) +get_blocking_random_bytes_cb (NULL , &drbg->seed_work, + drbg->seed_buf, drbg->seed_buf_len, + drbg, drbg_async_seed_cb); + out:return ret; }
@@ -1487,6 +1527,7 @@ unlock:*/ static int drbg_uninstantiate (struct drbg_state *drbg) { +drbg_async_work_cancel (&drbg->seed_work);if (drbg->d_ops) drbg->d_ops->crypto_fini (drbg);drbg_dealloc_state (drbg);
include/crypto/drbg.h +1 -0
@@ -119,6 +119,7 @@ struct drbg_state {bool fips_primed;/* Continuous test primed? */ unsigned char *prev;/* FIPS 140-2 continuous test value */ #endif +struct random_work seed_work;/* asynchronous seeding support */ u8 *seed_buf;/* buffer holding the seed */ size_t seed_buf_len;const struct drbg_state_ops *d_ops;
Signature · 1 line
2.1.0
PATCH v4 5/6 crypto: drbg - use Jitter RNG to obtain seed
In reply to Stephan MuellerDuring initialization, the DRBG now tries to allocate a handle of the Jitter RNG. If such a Jitter RNG is available during seeding, the DRBG pulls the required entropy/nonce string from get_random_bytes and concatenates it with a string of equal size from the Jitter RNG. That combined string is now the seed for the DRBG.
Written differently, the initial seed of the DRBG is now:
get_random_bytes(entropy/nonce) || jitterentropy (entropy/nonce)
If the Jitter RNG is not available, the DRBG only seeds from get_random_bytes.
--- crypto/drbg.c | 46 ++++++++++++++++++++++++++++++++++++++++------ include/crypto/drbg.h | 1 + 2 files changed, 41 insertions(+), 6 deletions(-)
crypto/drbg.c +40 -6
@@ -1120,10 +1120,25 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers, drbg->test_data.len);pr_devel ("DRBG: using test entropy\n" ); }else { -pr_devel ("DRBG: (re)seeding with %zu bytes of entropy\n" , - drbg->seed_buf_len); +/* Get seed from in-kernel /dev/urandom */ get_random_bytes (drbg->seed_buf, drbg->seed_buf_len); -drbg_string_fill (&data1, drbg->seed_buf, drbg->seed_buf_len); + +/* Get seed from Jitter RNG */ +if (!drbg->jent || +crypto_rng_get_bytes (drbg->jent, + drbg->seed_buf + drbg->seed_buf_len, + drbg->seed_buf_len)) { +pr_info ("DRBG: could not obtain random data from Jitter RNG\n" ); +drbg_string_fill (&data1, drbg->seed_buf, + drbg->seed_buf_len); +pr_devel ("DRBG: (re)seeding with %zu bytes of entropy\n" , + drbg->seed_buf_len); + }else { +drbg_string_fill (&data1, drbg->seed_buf, + drbg->seed_buf_len *2 ); +pr_devel ("DRBG: (re)seeding with %zu bytes of entropy\n" , + drbg->seed_buf_len *2 ); + } }list_add_tail (&data1.list, &seedlist);
@@ -1148,7 +1163,7 @@ static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,* Clear the initial entropy buffer as the async call may not overwrite * that buffer for quite some time. */ -memzero_explicit (drbg->seed_buf, drbg->seed_buf_len); +memzero_explicit (drbg->seed_buf, drbg->seed_buf_len *2 );if (ret) goto out;/*
@@ -1190,6 +1205,10 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg)#endif kzfree (drbg->seed_buf); drbg->seed_buf =NULL ; +if (drbg->jent) { +crypto_free_rng (drbg->jent); + drbg->jent =NULL ; + } }/*
@@ -1265,12 +1284,27 @@ static inline int drbg_alloc_state(struct drbg_state *drbg) ret = -EFAULT ; goto err; } -/* ensure we have sufficient buffer space for initial seed */ +/* +* Ensure we have sufficient buffer space for initial seed which +* consists of the seed from get_random_bytes and the Jitter RNG. +*/ drbg->seed_buf_len = ((drbg->seed_buf_len +1 ) /2 ) *3 ; - drbg->seed_buf =kzalloc (drbg->seed_buf_len,GFP_KERNEL ); + drbg->seed_buf =kzalloc (drbg->seed_buf_len *2 ,GFP_KERNEL );if (!drbg->seed_buf) goto err; + drbg->jent =crypto_alloc_rng ("jitterentropy_rng" ,0 ,0 ); +if (IS_ERR (drbg->jent)) + { +pr_info ("DRBG: could not allocate Jitter RNG handle for seeding\n" ); +/* +* As the Jitter RNG is a module that may not be present, we +* continue with the operation and do not fully tie the DRBG +* to the Jitter RNG. +*/ + drbg->jent =NULL ; + } +return 0 ; err:
include/crypto/drbg.h +1 -0
@@ -122,6 +122,7 @@ struct drbg_state {struct random_work seed_work;/* asynchronous seeding support */ u8 *seed_buf;/* buffer holding the seed */ size_t seed_buf_len; +struct crypto_rng *jent;const struct drbg_state_ops *d_ops;const struct drbg_core *core;struct drbg_string test_data;
Signature · 1 line
2.1.0
PATCH v4 6/6 crypto: add jitterentropy RNG
In reply to Stephan MuellerThe CPU Jitter RNG provides a source of good entropy by collecting CPU executing time jitter. The entropy in the CPU execution time jitter is magnified by the CPU Jitter Random Number Generator. The CPU Jitter Random Number Generator uses the CPU execution timing jitter to generate a bit stream which complies with different statistical measurements that determine the bit stream is random.
The CPU Jitter Random Number Generator delivers entropy which follows information theoretical requirements. Based on these studies and the implementation, the caller can assume that one bit of data extracted from the CPU Jitter Random Number Generator holds one bit of entropy.
The CPU Jitter Random Number Generator provides a decentralized source of entropy, i.e. every caller can operate on a private state of the entropy pool.
The RNG does not have any dependencies on any other service in the kernel. The RNG only needs a high-resolution time stamp.
Further design details, the cryptographic assessment and large array of test results are documented at
http://www.chronox.de/jent.html.
--- crypto/Kconfig | 10 + crypto/Makefile | 2 + crypto/jitterentropy.c | 909 +++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.c | 4 + 4 files changed, 925 insertions(+) create mode 100644 crypto/jitterentropy.c
crypto/Kconfig +10 -0
@@ -1479,9 +1479,19 @@ config CRYPTO_DRBG tristate default CRYPTO_DRBG_MENU if (CRYPTO_DRBG_HMAC || CRYPTO_DRBG_HASH || CRYPTO_DRBG_CTR) select CRYPTO_RNG + select CRYPTO_JITTERENTROPY endif # if CRYPTO_DRBG_MENU +config CRYPTO_JITTERENTROPY + tristate "Jitterentropy Non-Deterministic Random Number Generator" + help + The Jitterentropy RNG is a noise that is intended + to provide seed to another RNG. The RNG does not + perform any cryptographic whitening of the generated + random numbers. This Jitterentropy RNG registers with + the kernel crypto API and can be used by any caller. + config CRYPTO_USER_API tristate
crypto/Makefile +2 -0
@@ -94,6 +94,8 @@ obj-$(CONFIG_CRYPTO_RNG2) += rng.o obj-$(CONFIG_CRYPTO_RNG2) += krng.o obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o obj-$(CONFIG_CRYPTO_DRBG) += drbg.o +CFLAGS_jitterentropy.o = -O0 +obj-$(CONFIG_CRYPTO_JITTERENTROPY) += jitterentropy.o obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o obj-$(CONFIG_CRYPTO_GHASH) += ghash-generic.o obj-$(CONFIG_CRYPTO_USER_API) += af_alg.o
crypto/jitterentropy.c new file +909 -0
909 lines not on this page · raw message
crypto/testmgr.c +4 -0
@@ -3097,6 +3097,10 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "jitterentropy_rng", + .fips_allowed = 1, + .test = alg_test_null, + }, { .alg = "lrw(aes)", .test = alg_test_skcipher, .suite = {
Signature · 1 line
2.1.0
Theodore Ts'o
maintainerQuoted 10 lines · Stephan Mueller
The patch set adds an in-kernel
/dev/randomequivalent that was discussed with Ted Ts'o last July -- see [2] and [3]. A test module for testing the asynchronous operation of the in-kernel/dev/randomis given with the code below.Ted: shall we really create and maintain a new entropy pool (the
kernel_pool), or should the in-kernel/dev/randomlogic draw directly from theinput_pool? In other words, shall we drop the first patch and update the 2nd patch to useinput_pool? Also, I would not recommend using theblocking_poolas this would mix kernel and user land operation.
I'd drop the 3rd pool, and just simply block until the non-blocking pool has been initialized. That's now considered the best practice for userspace programs, which is to use getrandom(2), which by default will block until the nonblocking pool has been initialized with an estimated 128 bits of entropy, and after that point, all of the kernel users should be quite satisfied with cryptographic entropy.
Certainly from the perspective of the perspective of a NIST evaluator for a DRBG, using if they're happy using jitterentropy as a noise source, they should be quite happy using the non-blocking pool as a noise source, so long as it has been properly initialized.
As far as the concern of user space being able to block a kernel user "indefinitely", in practice I really don't think that's going to be an issue as far as boot-time initialization is concerned. In practice the urandom pool gets 128 bits of entropy *very* quickly, as in, before the boot process is finished.
[ 1.368585] random: systemd-udevd urandom read with 21 bits of entropy available [ 2.870497] EXT4-fs (sda3): mounted filesystem with ordered data mode. Opts: (null) [ 3.217209] EXT4-fs (sda3): re-mounted. Opts: errors=remount-ro [ 3.497191] random: nonblocking pool is initialized [ 7.705482] EXT4-fs (dm-9): mounted filesystem with ordered data mode. Opts: (null) [ 8.007435] EXT4-fs (dm-10): mounted filesystem with ordered data mode. Opts: (null) [ 8.165517] EXT4-fs (dm-0): mounted filesystem with ordered data mode. Opts: (null)
(And if the attacker is able to inject arbitrary programs running during the init sequence, they are almost certainly running as root, and you've got other problems.)
- Ted
Am Sonntag, 3. Mai 2015, 16:58:34 schrieb Theodore Ts'o:
Hi Theodore,
Quoted 6 lines · Stephan Mueller
Quoted 5 lines
The patch set adds an in-kernel
/dev/randomequivalent that was discussed with Ted Ts'o last July -- see [2] and [3]. A test module for testing the asynchronous operation of the in-kernel/dev/randomis given with the code below.
Quoted 7 lines
Quoted 5 lines
Ted: shall we really create and maintain a new entropy pool (the
kernel_pool), or should the in-kernel/dev/randomlogic draw directly from theinput_pool? In other words, shall we drop the first patch and update the 2nd patch to useinput_pool? Also, I would not recommend using theblocking_poolas this would mix kernel and user land operation.I'd drop the 3rd pool, and just simply block until the non-blocking
So, you have no concern to use the same pool that is also used by user land?
Quoted 5 lines
pool has been initialized. That's now considered the best practice for userspace programs, which is to use getrandom(2), which by default will block until the nonblocking pool has been initialized with an estimated 128 bits of entropy, and after that point, all of the kernel users should be quite satisfied with cryptographic entropy.
I am not sure that this approach is helpful, because the suggested approach implies using a seeded DRNG and the used get_random_bytes already operates as a (not always seeded) DRNG. If we have a blocking interface in the kernel, I would recommend to make it identical to /dev/random. With the suggested seeding approach for DRBG, we definitely have seed data available to start with. Therefore, re-seeding it from another seeded DRNG (i.e. the nonblocking pool after it is initialized) may not give us too much extra.
Therefore, may I propose to link the in-kernel blocking API to the blocking pool and have it behave exactly like /dev/random?
Quoted 5 lines
Certainly from the perspective of the perspective of a NIST evaluator for a DRBG, using if they're happy using jitterentropy as a noise source, they should be quite happy using the non-blocking pool as a noise source, so long as it has been properly initialized.
They should definitely be happy with that. Even more so, when it is clear that the DRBG is re-seeded once information theoretical entropy is available.
Personally, I feel that only considering the blocking_pool as a good source of entropy because it is constantly seeded with information theoretical entropy makes not much sense as that entropy is always and only used for mechanisms that only exhibit computational strength and *not* information theoretical strength. But I deviate from the topic and do not want to start another entropy discussion. :-)
Quoted 6 lines
As far as the concern of user space being able to block a kernel user "indefinitely", in practice I really don't think that's going to be an issue as far as boot-time initialization is concerned. In practice the urandom pool gets 128 bits of entropy *very* quickly, as in, before the boot process is finished.
Just as an FYI: the word "very" does not apply in all cases. I am about to draft an email about that subject in the near future. But the problem is that on SSD systems or virtual machines, the disk is no source of randomness any more starting with 3.18 where all non-rotational block devices are not used as a seed source any more. We only have interrupts which initialize the nonblocking pool much later (in my Haswell systems around the time index of 20). There are other problems that I would like to report separately.
[...]
Quoted 4 lines
(And if the attacker is able to inject arbitrary programs running during the init sequence, they are almost certainly running as root, and you've got other problems.)
No objections on the last statement :-)
Ciao
Stephan
Herbert Xu
maintainerQuoted 8 lines · Stephan Mueller
I am not sure that this approach is helpful, because the suggested approach implies using a seeded DRNG and the used
get_random_bytesalready operates as a (not always seeded) DRNG. If we have a blocking interface in the kernel, I would recommend to make it identical to/dev/random. With the suggested seeding approach for DRBG, we definitely have seed data available to start with. Therefore, re-seeding it from another seeded DRNG (i.e. the nonblocking pool after it is initialized) may not give us too much extra.
My main concern with your original approach was precisely the fact that get_random_bytes may be called before the before the kernel pool is ready. So Ted's solution solves that perfectly.
Please do what Ted says and we can all move forward.
Thanks!
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Theodore Ts'o
maintainerQuoted 3 lines · Stephan Mueller
Quoted 1 line
I'd drop the 3rd pool, and just simply block until the non-blocking
So, you have no concern to use the same pool that is also used by user land?
Nope, not really.... if you want to give me a specific articulable concern, please be explicit what _your_ concern is.
Quoted 10 lines
I am not sure that this approach is helpful, because the suggested approach implies using a seeded DRNG and the used
get_random_bytesalready operates as a (not always seeded) DRNG. If we have a blocking interface in the kernel, I would recommend to make it identical to/dev/random. With the suggested seeding approach for DRBG, we definitely have seed data available to start with. Therefore, re-seeding it from another seeded DRNG (i.e. the nonblocking pool after it is initialized) may not give us too much extra.Therefore, may I propose to link the in-kernel blocking API to the blocking pool and have it behave exactly like
/dev/random?
Why do you think we need an in-kernel block API *at* *all*? The only excuse I can think of is one where the in-kernel users want to wait for the the pool to be initialized. Why do you think it needs to behave "exactly like /dev/random"? What in-kernel user needs this?
Quoted 7 lines
Just as an FYI: the word "very" does not apply in all cases. I am about to draft an email about that subject in the near future. But the problem is that on SSD systems or virtual machines, the disk is no source of randomness any more starting with 3.18 where all non-rotational block devices are not used as a seed source any more. We only have interrupts which initialize the nonblocking pool much later (in my Haswell systems around the time index of 20). There are other problems that I would like to report separately.
The /dev/random is using the interrupt timing using versus the timestamp counter (if the architecture has such a beast). So even if the SSD or the virtual machines don't give you much entropy, we get entropy from the behavior of the caches, etc. --- which is exactly the same argument you make about the why you think jitter-randomness is sound. The other thing I'll note is that if you don't trust the timing of virtual disks on a virtual machines, I suspect the argument for why you believe these things are deterministic are very similar to the ones that could be made about the behavior of the internal CPU caches being deterministic. Furthermore, on most major cloud providers, the virtual disks are located on some kind of remote networked block device or cluster file system, which will have a fair amount of uncertainty that wouldn't be visible to an external attacker, say one sitting at NSA or BND headquarters. So it may not be as bad as you think.
Cheers,
- Ted