Seeding DRBG with more entropy
Subsystems (3)
PATCH v10 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 | 24 ++++++++++++++++++++++++ include/crypto/drbg.h | 2 ++ 2 files changed, 26 insertions(+)
crypto/drbg.c +24 -0
@@ -1056,6 +1056,23 @@ 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); + +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); +__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 +1142,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 +1252,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 +1510,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
PATCH v10 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 | 52 ++++++++++++++++++++++++++++++++++++++++++++------- include/crypto/drbg.h | 1 + 2 files changed, 46 insertions(+), 7 deletions(-)
crypto/drbg.c +45 -7
@@ -1062,13 +1062,18 @@ static void drbg_async_seed(struct work_struct *work)LIST_HEAD (seedlist); struct drbg_state *drbg = container_of(work, struct drbg_state, seed_work); +int ret;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); -__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); }
@@ -1103,10 +1108,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);
@@ -1131,7 +1150,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;/*
@@ -1171,6 +1190,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 ; + } }/*
@@ -1246,14 +1269,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 v10 1/5 random: Blocking API for accessing nonblocking_pool
In reply to Stephan MuellerThe 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 | 12 ++++++++++++ include/linux/random.h | 1 + 2 files changed, 13 insertions(+)
drivers/char/random.c +12 -0
@@ -1245,6 +1245,18 @@ 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 (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
PATCH v10 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 v10 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
v10 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 v10:
* patch 01/03: use uninterruptible sleep -- this patch only works with the change from wake_up_interruptible to wake_up_all for drivers/char/random.c proposed by Herbert Xu
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 | 143 ++++++-- crypto/jitterentropy.c | 909 +++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.c | 4 + drivers/char/random.c | 12 + include/crypto/drbg.h | 5 + include/linux/random.h | 1 + 8 files changed, 1059 insertions(+), 27 deletions(-) create mode 100644 crypto/jitterentropy.c
Signature · 1 line
2.1.0
Herbert Xu
maintainerQuoted 7 lines · Stephan Mueller
Hi,
as of now, the DRBG is only seeded from
get_random_bytes. In various circumstances, thenonblocking_poolbehindget_random_bytesmay 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 onget_random_bytesany more.
All applied to cryptodev. Thanks a lot!
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