Seeding DRBG with more entropy
Subsystems (3)
v9 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 a blocking API to access the nonblocking pool to wait until the nonblocking pool is initialized.
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 v9:
* patch 01: initialize return code
Changes v8:
* patch 01: make get_blocking_random_bytes to return the wait_event_interruptible error code
* patch 03: catch the ERESTARTSYS error code from get_blocking_random_bytes
Changes v7:
* patch 01: Catch ERESTARTSYS error for wait_event_interruptible
* patch 03: move the cancel invocation to drbg_uninstantiate to ensure that the ->fini function pointer is not yet invoked before the cancel is completed. As the cancel operation may potentially invoke crypto operations, the cipher state must be available.
* patch 04: deallocate Jitter RNG after nonblocking_pool is fully initialized. The change implies that regardless of the state of the nonblocking_pool, the Jitter RNG is used for the initial seeding in any case and only dropped for later reseeding operations.
Changes v6:
* patch 01: simplify patch by just adding a blocking API call to random.c as suggested by Herbert Xu.
* patch 03: move the async operation into this patch: the DRBG is in control of the async work.
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: Blocking 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 | 146 ++++++-- crypto/jitterentropy.c | 909 +++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.c | 4 + drivers/char/random.c | 20 ++ include/crypto/drbg.h | 5 + include/linux/random.h | 1 + 8 files changed, 1070 insertions(+), 27 deletions(-) create mode 100644 crypto/jitterentropy.c
Signature · 1 line
2.1.0
PATCH v9 1/5 random: Blocking 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.
--- drivers/char/random.c | 20 ++++++++++++++++++++ include/linux/random.h | 1 + 2 files changed, 21 insertions(+)
drivers/char/random.c +20 -0
@@ -1245,6 +1245,26 @@ void get_random_bytes(void *buf, int nbytes)EXPORT_SYMBOL (get_random_bytes);/* +* Equivalent function to get_random_bytes with the difference that this +* function blocks the request until the nonblocking_pool is initialized. +*/ +int get_blocking_random_bytes (void *buf,int nbytes) +{ +int rc =0 ; + +if (unlikely (nonblocking_pool.initialized ==0 )) + rc =wait_event_interruptible (urandom_init_wait, + nonblocking_pool.initialized); +if (rc) +return rc; + +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,0 ); + +return 0 ; +} +EXPORT_SYMBOL (get_blocking_random_bytes); + +/* * This function will use the architecture-specific hardware random * number generator if it is available. The arch-specific hw RNG will * almost certainly be faster than what we can do in software, but it
include/linux/random.h +1 -0
@@ -14,6 +14,7 @@ extern void add_input_randomness(unsigned int type, unsigned int code,extern void add_interrupt_randomness (int irq,int irq_flags);extern void get_random_bytes (void *buf,int nbytes); +extern int get_blocking_random_bytes (void *buf,int nbytes);extern void get_random_bytes_arch (void *buf,int nbytes);void generate_random_uuid (unsigned char uuid_out[16 ]);extern int random_int_secret_init (void );
Signature · 1 line
2.1.0
PATCH v9 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 v9 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 | 28 ++++++++++++++++++++++++++++ include/crypto/drbg.h | 2 ++ 2 files changed, 30 insertions(+)
crypto/drbg.c +28 -0
@@ -1056,6 +1056,27 @@ static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,return ret; } +static void drbg_async_seed (struct work_struct *work) +{ +struct drbg_string data; +LIST_HEAD (seedlist); + struct drbg_state *drbg = container_of(work, struct drbg_state, + seed_work); +int ret; + +do { + ret =get_blocking_random_bytes (drbg->seed_buf, + drbg->seed_buf_len); + }while (ret == -ERESTARTSYS ); + +drbg_string_fill (&data, drbg->seed_buf, drbg->seed_buf_len); +list_add_tail (&data.list, &seedlist); +mutex_lock (&drbg->drbg_mutex); +__drbg_seed (drbg, &seedlist, true); +memzero_explicit (drbg->seed_buf, drbg->seed_buf_len); +mutex_unlock (&drbg->drbg_mutex); +} +/* * Seeding or reseeding of the DRBG *
@@ -1125,6 +1146,10 @@ 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) && !reseed) +schedule_work (&drbg->seed_work); + out:return ret; }
@@ -1231,6 +1256,8 @@ static inline int drbg_alloc_state(struct drbg_state *drbg)if (!drbg->seed_buf) goto err; +INIT_WORK (&drbg->seed_work, drbg_async_seed); +return 0 ; err:
@@ -1487,6 +1514,7 @@ unlock:*/ static int drbg_uninstantiate (struct drbg_state *drbg) { +cancel_work_sync (&drbg->seed_work);if (drbg->d_ops) drbg->d_ops->crypto_fini (drbg);drbg_dealloc_state (drbg);
include/crypto/drbg.h +2 -0
@@ -51,6 +51,7 @@#include <linux/fips.h> #include <linux/mutex.h> #include <linux/list.h> +#include <linux/workqueue.h> /* * Concatenation Helper and string operation helper
@@ -119,6 +120,7 @@ struct drbg_state {bool fips_primed;/* Continuous test primed? */ unsigned char *prev;/* FIPS 140-2 continuous test value */ #endif +struct work_struct 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
Herbert Xu
maintainerRe: [PATCH v9 3/5] crypto: drbg - add async seeding operation
crypto/drbg.c unlock: · line 1517
Quoted 5 lines · Stephan Mueller
Review unlock:
Quoted 1 line
(quoted diff) +0
-0@@ -1487,6 +1514,7 @@ unlock:*/
static intdrbg_uninstantiate(struct drbg_state *drbg){crypto/drbg.c +1
-0+cancel_work_sync (&drbg->seed_work);
This will just block until the work is done, i.e., until the pool is ready. It's no different to an uninterruptible sleep.
So either just do an uninterruptible sleep, or allow the async seed to fail.
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 v9 3/5] crypto: drbg - add async seeding operation
Am Donnerstag, 21. Mai 2015, 05:44:08 schrieb Herbert Xu:
Hi Herbert,
Quoted 7 lines · Stephan Mueller
Quoted 6 lines
Review unlock:
Quoted 1 line
(quoted diff) +0
-0@@ -1487,6 +1514,7 @@ unlock:*/
static int
drbg_uninstantiate(struct drbg_state *drbg){
Quoted 8 lines
crypto/drbg.c unlock: · line 1517
Quoted 1 line
crypto/drbg.c +1
-0+cancel_work_sync (&drbg->seed_work);This will just block until the work is done, i.e., until the pool is ready. It's no different to an uninterruptible sleep.
So either just do an uninterruptible sleep, or allow the async seed to fail.
The cancel operation is needed as otherwise my drbg context handle will be removed by the crypto API during the sleep. That is the whole reason why wait_event_interruptible is used.
So, even when using an uninterruptible sleep, the crypto API has no knowledge about it and I have to serialize the destruction code path with the async callback.
Signature · 2 lines
Ciao Stephan
Herbert Xu
maintainerRe: [PATCH v9 3/5] crypto: drbg - add async seeding operation
Quoted 8 lines · Stephan Mueller
The cancel operation is needed as otherwise my drbg context handle will be removed by the crypto API during the sleep. That is the whole reason why
wait_event_interruptibleis used.So, even when using an uninterruptible sleep, the crypto API has no knowledge about it and I have to serialize the destruction code path with the async callback.
I understand. But if you use the uniterruptible version at least you won't have to deal with bogus early returns.
The cancel by itself will *not* cause wait_event_interruptible to return early. Somebody has to send it a signal for that to happen.
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 v9 3/5] crypto: drbg - add async seeding operation
Am Donnerstag, 21. Mai 2015, 14:36:18 schrieb Herbert Xu:
Hi Herbert,
Quoted 11 lines · Stephan Mueller
Quoted 7 lines
The cancel operation is needed as otherwise my drbg context handle will be removed by the crypto API during the sleep. That is the whole reason why
wait_event_interruptibleis used.So, even when using an uninterruptible sleep, the crypto API has no knowledge about it and I have to serialize the destruction code path with the async callback.
I understand. But if you use the uniterruptible version at least you won't have to deal with bogus early returns.
Thank you for pointing that out - I have seen that too. But the crux is that when using wait_event, the cancel function to serialize the destruction code path will *not* return at all, even when the async callback function completed successfully. Hence the choice for wait_event_interruptible
Quoted 4 lines
The cancel by itself will *not* cause
wait_event_interruptibleto return early. Somebody has to send it a signal for that to happen.
Correct. But I can only wake it up with a handle on urandom_init_wait on which the wait sleeps. That handle lives in random.c. So, to wake it up, I need another piece of code in random.c. That piece of code used to be the get_blocking_random_bytes_cancel function I offered in previous patches.
As Ted mentioned, he did not want to much code in random.c for this. In addition, if I understand you correctly in previous emails, it would be acceptable to wait until the callback was triggered (and thus causing the caller module to be not unloadable for that period of time.
Ciao
Stephan
Herbert Xu
maintainerRe: [PATCH v9 3/5] crypto: drbg - add async seeding operation
Quoted 5 lines · Stephan Mueller
Thank you for pointing that out - I have seen that too. But the crux is that when using
wait_event, the cancel function to serialize the destruction code path will *not* return at all, even when the async callback function completed successfully. Hence the choice forwait_event_interruptible
Why is that? It should return when the pool is initialised and your work completes. If it does not then that means either the pool isn't initialising or somehow your code is racy and getting stuck.
If it's racy then wait_event_interruptible is just hiding the real problem.
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 v9 3/5] crypto: drbg - add async seeding operation
In reply to Herbert XuAm Donnerstag, 21. Mai 2015, 05:44:08 schrieb Herbert Xu:
Hi Herbert,
Quoted 7 lines · Stephan Mueller
Quoted 6 lines
Review unlock:
Quoted 1 line
(quoted diff) +0
-0@@ -1487,6 +1514,7 @@ unlock:*/
static int
drbg_uninstantiate(struct drbg_state *drbg){
Quoted 7 lines
crypto/drbg.c unlock: · line 1517
Quoted 1 line
crypto/drbg.c +1
-0+cancel_work_sync (&drbg->seed_work);This will just block until the work is done, i.e., until the pool is ready. It's no different to an uninterruptible sleep.
So either just do an uninterruptible sleep, or allow the async seed to fail.
I think I found the issue: we cannot put interruptible and noninterruptible sleeps onto the same wait queue. With my initial tests, I put the uninterruptible sleep onto urandom_init_wait where also interruptible sleeps are put onto.
So, I will create a 2nd wait queue in random.c for uninterruptible waits, change the get_blocking_random_bytes back to void and use wait_event to wait for the initialization.
Quoted 2 lines
Cheers,
Signature · 2 lines
Ciao Stephan
Herbert Xu
maintainerrandom: Wake up all getrandom(2) callers when pool is ready
Quoted 4 lines · Stephan Mueller
So, I will create a 2nd wait queue in
random.cfor uninterruptible waits, change theget_blocking_random_bytesback to void and usewait_eventto wait for the initialization.
Hold your horses. You don't need a second queue, you just need to change wake_up_interruptible to wake_up.
Hmm, in fact shouldn't this be wake_up_all? Otherwise what are the other getrandom(2) callers going to do? Ted?
---8<---
If more than one application invokes getrandom(2) before the pool is ready, then all bar one will be stuck forever because we use wake_up_interruptible which wakes up a single task.
This patch replaces it with wake_up_all.
drivers/char/random.c +1 -1
@@ -660,7 +660,7 @@ retry: r->entropy_total =0 ;if (r == &nonblocking_pool) {prandom_reseed_late (); -wake_up_interruptible (&urandom_init_wait); +wake_up_all (&urandom_init_wait);pr_notice ("random: %s pool is initialized\n" , r->name); } }
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: random: Wake up all getrandom(2) callers when pool is ready
Am Donnerstag, 21. Mai 2015, 16:19:54 schrieb Herbert Xu:
Hi Herbert,
Quoted 8 lines · Stephan Mueller
Quoted 4 lines
So, I will create a 2nd wait queue in
random.cfor uninterruptible waits, change theget_blocking_random_bytesback to void and usewait_eventto wait
for the initialization.Hold your horses. You don't need a second queue, you just need to change
wake_up_interruptibletowake_up.
A simple change to wake_up is not working -- I already checked. But the proposed wake_up_all does the trick.
Ciao
Stephan
Theodore Ts'o
maintainerRe: random: Wake up all getrandom(2) callers when pool is ready
In reply to Herbert XuQuoted 11 lines · Herbert Xu
Quoted 4 lines · Stephan Mueller
So, I will create a 2nd wait queue in
random.cfor uninterruptible waits, change theget_blocking_random_bytesback to void and usewait_eventto wait for the initialization.Hold your horses. You don't need a second queue, you just need to change
wake_up_interruptibletowake_up.Hmm, in fact shouldn't this be
wake_up_all? Otherwise what are the other getrandom(2) callers going to do? Ted?
Yes, this should definitely be wake_up_all()
- Ted
Herbert Xu
maintainerRe: random: Wake up all getrandom(2) callers when pool is ready
Quoted 2 lines · Theodore Ts'o
Yes, this should definitely be
wake_up_all()
Thanks for the confirmation. Can I push this patch through the cryptodev tree since Stephan's work is probably going to depend on it?
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 v9 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 | 51 ++++++++++++++++++++++++++++++++++++++++++++------- include/crypto/drbg.h | 1 + 2 files changed, 45 insertions(+), 7 deletions(-)
crypto/drbg.c +44 -7
@@ -1072,7 +1072,11 @@ static void drbg_async_seed(struct work_struct *work)drbg_string_fill (&data, drbg->seed_buf, drbg->seed_buf_len);list_add_tail (&data.list, &seedlist);mutex_lock (&drbg->drbg_mutex); -__drbg_seed (drbg, &seedlist, true); + ret =__drbg_seed (drbg, &seedlist, true); +if (!ret && drbg->jent) { +crypto_free_rng (drbg->jent); + drbg->jent =NULL ; + }memzero_explicit (drbg->seed_buf, drbg->seed_buf_len);mutex_unlock (&drbg->drbg_mutex); }
@@ -1107,10 +1111,24 @@ 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)) { +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);
@@ -1135,7 +1153,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;/*
@@ -1175,6 +1193,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 ; + } }/*
@@ -1250,14 +1272,29 @@ 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;INIT_WORK (&drbg->seed_work, drbg_async_seed); + 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
@@ -123,6 +123,7 @@ struct drbg_state {struct work_struct 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 v9 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
@@ -1478,9 +1478,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
@@ -3106,6 +3106,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