Seeding DRBG with more entropy
Subsystems (3)
v6 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 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 | 140 ++++++-- crypto/jitterentropy.c | 909 +++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.c | 4 + drivers/char/random.c | 14 + include/crypto/drbg.h | 5 + include/linux/random.h | 1 + 8 files changed, 1058 insertions(+), 27 deletions(-) create mode 100644 crypto/jitterentropy.c
Signature · 1 line
2.1.0
PATCH v6 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 | 14 ++++++++++++++ include/linux/random.h | 1 + 2 files changed, 15 insertions(+)
drivers/char/random.c +14 -0
@@ -1245,6 +1245,20 @@ 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. +*/ +void get_blocking_random_bytes (void *buf,int nbytes) +{ +if (unlikely (nonblocking_pool.initialized ==0 )) +wait_event_interruptible (urandom_init_wait, + nonblocking_pool.initialized); +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,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 void 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
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
drivers/char/random.c void get_random_bytes(void *buf, int nbytes) · line 1256
Quoted 10 lines · Stephan Mueller
/*
+ * Equivalent function toget_random_byteswith the difference that this
+ * function blocks the request until thenonblocking_poolis initialized.
+ */drivers/char/random.c +6
-0+void get_blocking_random_bytes (void *buf,int nbytes) +{ +if (unlikely (nonblocking_pool.initialized ==0 )) +wait_event_interruptible (urandom_init_wait, + nonblocking_pool.initialized); +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,0 );
So what if the wait was interrupted? You are going to extract entropy from an empty pool.
Anyway, you still haven't addressed my primary concern with this model which is the potential for dead-lock. Sleeping for an open period of time like this in a work queue is bad form. It may also lead to dead-locks if whatever you're waiting for happened to use the same work thread.
That's why I think you should simply provide a function and data pointer which random.c can then stash onto a list to call 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
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Freitag, 15. Mai 2015, 14:46:26 schrieb Herbert Xu:
Hi Herbert,
Quoted 22 lines · Stephan Mueller
Review
Quoted 11 lines
/*
+ * Equivalent function to
get_random_byteswith the difference that this
+ * function blocks the request until thenonblocking_poolis initialized.
+ */(quoted diff) +6
-0+void get_blocking_random_bytes(void *buf, int nbytes) +{ + if (unlikely(nonblocking_pool.initialized == 0)) + wait_event_interruptible(urandom_init_wait, + nonblocking_pool.initialized); + extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);So what if the wait was interrupted? You are going to extract entropy from an empty pool.
Anyway, you still haven't addressed my primary concern with this model which is the potential for dead-lock. Sleeping for an open period of time like this in a work queue is bad form. It may also lead to dead-locks if whatever you're waiting for happened to use the same work thread.
Quoted 3 lines
That's why I think you should simply provide a function and data pointer which
random.ccan then stash onto a list to call when the pool is ready.
Thanks for the hint to the list. Before handing in another formal patch, may i ask for checking the following approach? I would think that this one should cover your concerns.
drivers/char/random.c +75 -0
@@ -409,6 +409,19 @@ static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);static DECLARE_WAIT_QUEUE_HEAD (urandom_init_wait);static struct fasync_struct *fasync; +static LIST_HEAD (random_wait_list); +static DEFINE_MUTEX (random_wait_list_mutex); +struct random_work { +struct list_head list; +struct work_struct rw_work; +void *rw_buf; +int rw_len; +void *rw_private; +void (*rw_cb)(void *buf,int buflen, +void *private); +}; +static void process_random_waiters (void ); +/********************************************************************** * * OS independent entropy store. Here are the functions which handle
@@ -660,6 +673,7 @@ retry: r->entropy_total =0 ;if (r == &nonblocking_pool) {prandom_reseed_late (); +process_random_waiters ();wake_up_interruptible (&urandom_init_wait);pr_notice ("random: %s pool is initialized\n" , r->name); }
@@ -1778,3 +1792,64 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,credit_entropy_bits (poolp, entropy); }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness); + +static void process_random_waiters (void ) +{ +struct random_work *rw =NULL ; + +mutex_lock (&random_wait_list_mutex); +while (!list_empty (&random_wait_list)) { + rw = list_first_entry(&random_wait_list, struct random_work, + list); +list_del (&rw->list); +schedule_work (&rw->rw_work); + } +mutex_unlock (&random_wait_list_mutex); +} + +static void get_blocking_random_bytes_work (struct work_struct *work) +{ + struct random_work *rw = container_of(work, struct random_work, + rw_work); + +get_random_bytes (rw->rw_buf, rw->rw_len); + rw->rw_cb (rw->rw_buf, rw->rw_len, rw->rw_private); +kfree (rw); +} + +/* +* 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_cb (void *buf,int nbytes,void *private, +void (*cb)(void *buf,int buflen, +void *private)) +{ +struct random_work *rw =NULL ; +int ret =0 ; + +mutex_lock (&random_wait_list_mutex); +list_for_each_entry (rw, &random_wait_list, list) +if (buf == rw->rw_buf) + goto out; + + rw =kmalloc (sizeof (struct random_work ),GFP_KERNEL ); +if (!rw) { + ret = -ENOMEM ; + goto out; + } +INIT_WORK (&rw->rw_work, get_blocking_random_bytes_work); + rw->rw_buf = buf; + rw->rw_len = nbytes; + rw->rw_private = private; + rw->rw_cb = cb; +list_add_tail (&rw->list, &random_wait_list); + +out: +mutex_unlock (&random_wait_list_mutex); +if (nonblocking_pool.initialized) +process_random_waiters (); + +return ret; +} +EXPORT_SYMBOL (get_blocking_random_bytes_cb);
include/linux/random.h +3 -0
@@ -15,6 +15,9 @@ extern void add_interrupt_randomness(int irq, int irq_flags);extern void get_random_bytes (void *buf,int nbytes);extern void get_random_bytes_arch (void *buf,int nbytes); +extern int get_blocking_random_bytes_cb (void *buf,int nbytes,void *private, +void (*cb)(void *buf,int buflen, +void *private));void generate_random_uuid (unsigned char uuid_out[16 ]);extern int random_int_secret_init (void );
Signature · 7 lines
2.1.0 -- Ciao Stephan
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 2 lines · Stephan Mueller
Thanks for the hint to the list. Before handing in another formal patch, may i ask for checking the following approach? I would think that this one should cover your concerns.
Yes this is definitely going in the right direction.
drivers/char/random.c void add_hwgenerator_randomness(const char *buffer, size_t count, · line 1826
Quoted 7 lines
drivers/char/random.c +7
-0+/* +* 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_cb (void *buf,int nbytes,void *private, +void (*cb)(void *buf,int buflen, +void *private))
You can simplify this further and get rid of buf/nbytes. All we need to know is whether the pool is ready. Everything else can come from private.
Review
Quoted 2 lines
(quoted diff) +2
-0+ struct random_work *rw = NULL; + int ret = 0;
I think this function should return 0 if the pool is ready now, -EINPROGRESS if it's not (indicating that the callback will be called when it is ready) and otherwise an error.
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 v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Montag, 18. Mai 2015, 17:21:31 schrieb Herbert Xu:
Hi Herbert,
Quoted 12 lines
drivers/char/random.c void add_hwgenerator_randomness(const char *buffer, size_t count, · line 1826
Quoted 7 lines
drivers/char/random.c +7
-0+/* +* 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_cb (void *buf,int nbytes,void *private, +void (*cb)(void *buf,int buflen, +void *private))You can simplify this further and get rid of buf/nbytes. All we need to know is whether the pool is ready. Everything else can come from private.
So, the async function is now just a notification of the caller. Sounds good with me.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ struct random_work *rw = NULL; + int ret = 0;I think this function should return 0 if the pool is ready now,
-EINPROGRESSif it's not (indicating that the callback will be called when it is ready) and otherwise an error.
Ok, will come in the next patch.
Ciao
Stephan
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Montag, 18. Mai 2015, 15:07:10 schrieb Stephan Mueller:
Hi Herbert,
Quoted 8 lines
Quoted 4 lines
You can simplify this further and get rid of buf/nbytes. All we need to know is whether the pool is ready. Everything else can come from private.
So, the async function is now just a notification of the caller. Sounds good with me.
I am just running into an interesting problem with a missing cancel operation:
a caller instantiates a DRBG handle and invokes the seeding operation. The nonblocking_pool is not initialized. Therefore, the callback is put onto the list for being processed later.
Now, the caller releases the DRBG handle *before* the callback is triggered.
The callback is triggered with a pointer that is invalid, but the pointer is non-NULL. Therefore, I am not sure how to validate the pointer in the callback function.
I would think that there is no other way than to add a cancel API call that allows removing a list entry from the wait list.
Ciao
Stephan
Theodore Ts'o
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 11 lines · Stephan Mueller
I am just running into an interesting problem with a missing cancel operation:
a caller instantiates a DRBG handle and invokes the seeding operation. Thenonblocking_poolis not initialized. Therefore, the callback is put onto the list for being processed later.Now, the caller releases the DRBG handle *before* the callback is triggered.
The callback is triggered with a pointer that is invalid, but the pointer is non-NULL. Therefore, I am not sure how to validate the pointer in the callback function.
The simplest thing to do is to put a refcount on inside the DRBG handle structure. The caller instantiates the DRBG handle, and invokes the the DRBG. The DRBG, since it is kicking off an asynchronous operation, increments the refcount.
Both the caller and the callback function, before they exit, drop the refcount, and if they see the refcount is zero, they free the DRBG handle and the memory where the random seed is to be (or has been) deposited.
This is the same pattern that the block I/O layer uses with a bio struct. In the BIO case, it's important since the callback function could have been called and returned before the caller gets control back from the bio_submit() call. Or the struct bio may contain an EOPNOTSUPP error, in which case there will be no callback function dispatched. So long as everyone handles the refcount rules, it all works out.
Regards,
- Ted
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Montag, 18. Mai 2015, 11:02:34 schrieb Theodore Ts'o:
Hi Theodore, Herbert,
Quoted 5 lines
The simplest thing to do is to put a refcount on inside the DRBG handle structure. The caller instantiates the DRBG handle, and invokes the the DRBG. The DRBG, since it is kicking off an asynchronous operation, increments the refcount.
That is a good idea. After experimenting with the refcount, I see that kernel crypto API release function of crypto_destroy_tfm unconditionally destroys the crypto handle by freeing it.
So, if a caller releases the DRBG handle, the DRBG code cannot prevent the destruction of its context with a refcount.
Herbert, do you have any ideas?
Signature · 2 lines
Ciao Stephan
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 2 lines · Stephan Mueller
Herbert, do you have any ideas?
On the /dev/random side,
1) Add a struct module argument in addition to func/data.
2) Grab module ref count when func/data is registered.
3) Drop module ref count after func returns.
On the drbg side,
1) Allocate data pointer before func/data registration, it should contain a flag indicating whether drbg is still alive.
2) In cra_exit, zap the flag in allocated data.
3) In func immediately return if flag indicates drbg is dead.
4) Free allocated data pointer when func is done.
Obviously you need to add some locking so that func doesn't race against cra_exit or any other drbg paths that it intersects.
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 v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Dienstag, 19. Mai 2015, 15:22:27 schrieb Herbert Xu:
Hi Herbert,
Quoted 11 lines · Stephan Mueller
Quoted 1 line
Herbert, do you have any ideas?
On the
/dev/randomside,1) Add a struct module argument in addition to func/data.
2) Grab module ref count when func/data is registered.
3) Drop module ref count after func returns.On the drbg side,
Quoted 8 lines
1) Allocate data pointer before func/data registration, it should contain a flag indicating whether drbg is still alive.
2) Incra_exit, zap the flag in allocated data.
3) In func immediately return if flag indicates drbg is dead.
4) Free allocated data pointer when func is done.Obviously you need to add some locking so that func doesn't race against
cra_exitor any other drbg paths that it intersects.
Thank you for the hints. I will follow your guidance.
Just for my edification: why is this (rather complex sounding) approach preferred over a simple cancel API? Other async APIs (e.g. the AIO syscalls with io_cancel) have such cancel operations.
Such cancel function would be as simple as:
void get_blocking_random_bytes_cancel(void *private)
{
struct random_work *rw, *tmp;
mutex_lock(&random_wait_list_mutex);
list_for_each_entry_safe(rw, tmp, &random_wait_list, list) {
if (private == rw->rw_private) {
list_del(&rw->list);
kfree(rw);
break;
}
}
mutex_unlock(&random_wait_list_mutex);
}Signature · 2 lines
Ciao Stephan
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 8 lines · Stephan Mueller
Thank you for the hints. I will follow your guidance.
Just for my edification: why is this (rather complex sounding) approach preferred over a simple cancel API? Other async APIs (e.g. the AIO syscalls with
io_cancel) have such cancel operations.Such cancel function would be as simple as:
You're right. The cancel function is indeed simpler. I can certainly live with that.
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
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Dienstag, 19. Mai 2015, 15:51:55 schrieb Herbert Xu:
Hi Herbert,
Quoted 2 lines
You're right. The cancel function is indeed simpler. I can certainly live with that.
Thank you. I will test my patch a bit more and then release it with the discussed changes.
Ciao
Stephan
Theodore Ts'o
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
In reply to Herbert XuQuoted 4 lines · Herbert Xu
Quoted 3 lines · Stephan Mueller
Thank you for the hints. I will follow your guidance.
Quoted 8 lines
Quoted 5 lines
Just for my edification: why is this (rather complex sounding) approach preferred over a simple cancel API? Other async APIs (e.g. the AIO syscalls with
io_cancel) have such cancel operations.Such cancel function would be as simple as:
You're right. The cancel function is indeed simpler. I can certainly live with that.
I'm not at all convinced it's simpler. I see it add a huge amount of hair to drivers/char/random.c that is only used by a single caller.
I'm also not sure I understand herbert's suggstion of adding a struct module to the /dev/random code. Why is this helpful, and what is the race that you are trying to protect against?
Finally, this is only going to block *once*, when the system is initially botting up. Why is it so important that we get the asynchronous nature of this right, and why can't we solve it simply by just simply doing the work in a workqueue, with a completion barrier getting triggered once /dev/random initializes itself, and just simply blocking the module unload until /dev/random is initialized?
- Ted
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 7 lines · Theodore Ts'o
Finally, this is only going to block *once*, when the system is initially botting up. Why is it so important that we get the asynchronous nature of this right, and why can't we solve it simply by just simply doing the work in a workqueue, with a completion barrier getting triggered once
/dev/randominitializes itself, and just simply blocking the module unload until/dev/randomis initialized?
I guess I'm still thinking of the old work queue code before Tejun's cmwq work. Yes blocking in a work queue should be fine as there is usually just one DRBG instance.
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 v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Dienstag, 19. Mai 2015, 22:18:05 schrieb Herbert Xu:
Hi Herbert,
Quoted 11 lines · Theodore Ts'o
Quoted 6 lines
Finally, this is only going to block *once*, when the system is initially botting up. Why is it so important that we get the asynchronous nature of this right, and why can't we solve it simply by just simply doing the work in a workqueue, with a completion barrier getting triggered once
/dev/randominitializes itself, and just simply blocking the module unload until/dev/randomis initialized?I guess I'm still thinking of the old work queue code before Tejun's cmwq work. Yes blocking in a work queue should be fine as there is usually just one DRBG instance.
The current modification with patch 1 to random.c is the smallest change to date. Is that then appropriate?
Herbert, based on your comment now, would the currently discussed patch with waiting in the work queue in patch 3 appropriate? Or what would you like to see changed?
Ciao
Stephan
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 7 lines · Stephan Mueller
The current modification with patch 1 to
random.cis the smallest change to date. Is that then appropriate?Herbert, based on your comment now, would the currently discussed patch with waiting in the work queue in patch 3 appropriate? Or what would you like to see changed?
Sorry but I have lost track as to which patch is which. Please post the changes to random.c and we can all take a look.
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
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Dienstag, 19. Mai 2015, 22:30:22 schrieb Herbert Xu:
Hi Herbert,
Quoted 11 lines · Stephan Mueller
Quoted 7 lines
The current modification with patch 1 to
random.cis the smallest change to date. Is that then appropriate?Herbert, based on your comment now, would the currently discussed patch with
waiting in the work queue in patch 3 appropriate? Or what would you like to see changed?Sorry but I have lost track as to which patch is which. Please post the changes to
random.cand we can all take a look.
It would be the patch from the start of the thread as follows.
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 | 14 ++++++++++++++ include/linux/random.h | 1 + 2 files changed, 15 insertions(+)
drivers/char/random.c +14 -0
@@ -1245,6 +1245,20 @@ 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. +*/ +void get_blocking_random_bytes (void *buf,int nbytes) +{ +if (unlikely (nonblocking_pool.initialized ==0 )) +wait_event_interruptible (urandom_init_wait, + nonblocking_pool.initialized); +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,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 +0 -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 void 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);
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Quoted 9 lines · Stephan Mueller
drivers/char/random.c +1
-0@@ -1245,6 +1245,20 @@ void get_random_bytes(void *buf, int nbytes)EXPORT_SYMBOL (get_random_bytes);/* +* Equivalent function to get_random_bytes with the difference that this
drivers/char/random.c void get_random_bytes(void *buf, int nbytes) · line 1258
Quoted 10 lines
drivers/char/random.c +10
-0+* function blocks the request until the nonblocking_pool is initialized. +*/ +void get_blocking_random_bytes (void *buf,int nbytes) +{ +if (unlikely (nonblocking_pool.initialized ==0 )) +wait_event_interruptible (urandom_init_wait, + nonblocking_pool.initialized); +extract_entropy (&nonblocking_pool, buf, nbytes,0 ,0 ); +} +EXPORT_SYMBOL (get_blocking_random_bytes);
You still need to handle the case where wait_event_interruptible returns an error. Otherwise this looks fine.
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
Re: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Mittwoch, 20. Mai 2015, 06:55:33 schrieb Herbert Xu:
Hi Herbert,
Quoted 3 lines
You still need to handle the case where
wait_event_interruptiblereturns an error. Otherwise this looks fine.
Thank you. I would suggest to add a while loop around the call that sets up the wait if it terminated with ERESTARTSYS. This way we can maintain a void function that is almost identical to the get_random_bytes function.
Ciao
Stephan
Herbert Xu
maintainerRe: [PATCH v6 1/5] random: Blocking API for accessing nonblocking_pool
In reply to Herbert XuQuoted 9 lines · Herbert Xu
Quoted 7 lines · Theodore Ts'o
Finally, this is only going to block *once*, when the system is initially botting up. Why is it so important that we get the asynchronous nature of this right, and why can't we solve it simply by just simply doing the work in a workqueue, with a completion barrier getting triggered once
/dev/randominitializes itself, and just simply blocking the module unload until/dev/randomis initialized?
Quoted 3 lines
I guess I'm still thinking of the old work queue code before Tejun's cmwq work. Yes blocking in a work queue should be fine as there is usually just one DRBG instance.
It looks like waiting for it in a workqueue isn't good enough after all. I just got this in a KVM machine:
INFO: task kworker/0:1:121 blocked for more than 120 seconds.
Tainted: G O 4.1.0-rc1+ #34
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
kworker/0:1 D ffff88001eb47d18 0 121 2 0x00000000
Workqueue: events drbg_async_seed [drbg]
ffff88001eb47d18 ffff88001e1bcec0 ffff88001eb84010 0000000000000246
ffff88001eb48000 0000000000000020 ffff88001d54ea50 0000000000000000
ffff88001f613080 ffff88001eb47d38 ffffffff813fe692 0000000000000020
Call Trace:
[<ffffffff813fe692>] schedule+0x32/0x80
[<ffffffff812c1415>] get_blocking_random_bytes+0x65/0xa0
[<ffffffff810825e0>] ? add_wait_queue+0x60/0x60
[<ffffffffa03dd14c>] drbg_async_seed+0x2c/0xc0 [drbg]
[<ffffffff81063369>] process_one_work+0x129/0x310
[<ffffffff810636a9>] worker_thread+0x119/0x430
[<ffffffff813fe51b>] ? __schedule+0x7fb/0x85e
[<ffffffff81063590>] ? process_scheduled_works+0x40/0x40
[<ffffffff81068ed4>] kthread+0xc4/0xe0
[<ffffffff81060000>] ? proc_cap_handler+0x180/0x1b0
[<ffffffff81068e10>] ? kthread_freezable_should_stop+0x60/0x60
[<ffffffff81401ee2>] ret_from_fork+0x42/0x70
[<ffffffff81068e10>] ? kthread_freezable_should_stop+0x60/0x60Steffen, I think we need to revisit the idea of having a list of callbacks.
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 v6 1/5] random: Blocking API for accessing nonblocking_pool
Am Freitag, 5. Juni 2015, 13:28:06 schrieb Herbert Xu:
Hi Herbert,
Quoted 3 lines
Steffen, I think we need to revisit the idea of having a list of callbacks.
Ok, I will reactivate my patch with the list.
Quoted 2 lines
Cheers,
Signature · 2 lines
Ciao Stephan
PATCH v6 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 v6 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 | 25 +++++++++++++++++++++++++ include/crypto/drbg.h | 2 ++ 2 files changed, 27 insertions(+)
crypto/drbg.c +25 -0
@@ -1056,6 +1056,24 @@ 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 =0 ; + +get_blocking_random_bytes (drbg->seed_buf, drbg->seed_buf_len); + +drbg_string_fill (&data, drbg->seed_buf, drbg->seed_buf_len); +list_add_tail (&data.list, &seedlist); +mutex_lock (&drbg->drbg_mutex); + ret =__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 +1143,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)) +schedule_work (&drbg->seed_work); + out:return ret; }
@@ -1134,6 +1156,7 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg) {if (!drbg)return ; +cancel_work_sync (&drbg->seed_work);kzfree (drbg->V); drbg->V =NULL ;kzfree (drbg->C);
@@ -1231,6 +1254,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:
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
PATCH v6 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
@@ -1104,10 +1104,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);
@@ -1132,7 +1147,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;/*
@@ -1173,6 +1188,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 ; + } }/*
@@ -1248,14 +1267,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 v6 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