Seeding DRBG with more entropy
Subsystems (3)
v5 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 blocking API for accessing the nonblocking pool 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 blocking API 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 asynchronous API to access the nonblocking pool to wait until the nonblocking pool is initialized. A test module for testing the asynchronous operation is given with the code below.
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 [2]).
Changes v5:
* drop patch 01 and therefore drop the creation of a kernel pool
* change patch 02 to use the nonblocking pool and block until the nonblocking pool is initialized or until the cancel operation is triggered.
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] http://www.chronox.de/jent.html
Stephan Mueller (5):
random: Async and sync API for accessing nonblocking_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 | 104 ++++++ include/crypto/drbg.h | 4 + include/linux/random.h | 20 ++ 8 files changed, 1182 insertions(+), 27 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 v5 1/5 random: Async and sync API for accessing nonblocking_pool
The added API calls provide a synchronous function call get_blocking_random_bytes where the caller is blocked until the nonblocking_pool is initialized.
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 | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/random.h | 20 ++++++++++ 2 files changed, 124 insertions(+)
drivers/char/random.c +105 -0
@@ -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 (urandom_kernel_wait);static struct fasync_struct *fasync;/**********************************************************************
@@ -661,6 +662,7 @@ retry:if (r == &nonblocking_pool) {prandom_reseed_late ();wake_up_interruptible (&urandom_init_wait); +wake_up_interruptible (&urandom_kernel_wait);pr_notice ("random: %s pool is initialized\n" , r->name); } }
@@ -1778,3 +1781,106 @@ 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 (nonblocking_pool.initialized) +return true; +return *cancel; +} + +/* +* Equivalent function to get_random_bytes with the difference that this +* function blocks the request until the nonblocking_pool is initialized. +* +* 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. +*/ +void get_blocking_random_bytes (void *buf,int nbytes,bool *cancel) +{ +if (unlikely (nonblocking_pool.initialized ==0 )) +wait_event_interruptible (urandom_kernel_wait, +get_blocking_random_bytes_term (cancel)); + +if (!cancel) +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,0 ); +} +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 (&urandom_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); + +get_blocking_random_bytes (rw->rw_buf, rw->rw_len, &rw->cancel); +if (!rw->cancel) + rw->rw_cb (rw->rw_buf, rw->rw_len, 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->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; +}; + +void get_blocking_random_bytes (void *buf,int 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
Herbert Xu
maintainerRe: [PATCH v5 1/5] random: Async and sync API for accessing nonblocking_pool
Quoted 11 lines · Stephan Mueller
The added API calls provide a synchronous function call
get_blocking_random_byteswhere the caller is blocked until thenonblocking_poolis initialized.In addition, an asynchronous API call of
get_blocking_random_bytes_cbis 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.
Quoted 2 lines
A third API call,
get_blocking_random_bytes_cancel, is provided to cancel the random number gathering operation.
There are two problems with this patch:
1) The interface is way too complicated for a once off wait used only during boot. Really there is no need for cancellations.
2) There is potential for deadlock because you schedule a work that then sits around until the entropy is available. What if whatever is generating that entropy used the same work thread in future?
So I suggest instead an interface that simply schedules a work when the entropy is available.
Cheers,
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
Re: [PATCH v5 1/5] random: Async and sync API for accessing nonblocking_pool
Am Montag, 11. Mai 2015, 14:57:14 schrieb Herbert Xu:
Hi Herbert,
Quoted 5 lines
There are two problems with this patch:
1) The interface is way too complicated for a once off wait used only during boot. Really there is no need for cancellations.
I will remove that in the next installment. But that means that the DRBG kernel module (or any other module) cannot be unloaded until the work completes.
Quoted 7 lines
2) There is potential for deadlock because you schedule a work that then sits around until the entropy is available. What if whatever is generating that entropy used the same work thread in future?
So I suggest instead an interface that simply schedules a work when the entropy is available.
Are you suggesting to only leave get_blocking_random_bytes and have the caller implementing the appropriate synchronous wait or async work?
Ciao
Stephan
Herbert Xu
maintainerRe: [PATCH v5 1/5] random: Async and sync API for accessing nonblocking_pool
Quoted 4 lines · Stephan Mueller
I will remove that in the next installment. But that means that the DRBG kernel module (or any other module) cannot be unloaded until the work completes.
I don't think that's a big deal.
Quoted 2 lines
Are you suggesting to only leave
get_blocking_random_bytesand have the caller implementing the appropriate synchronous wait or async work?
No there should just be a single interface where you feed it a function pointer and a data pointer. This function should then be called when the pool is ready.
Cheers,
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
PATCH v5 2/5 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 v5 3/5 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 v5 4/5 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 v5 5/5 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