Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Stephan Mueller <smueller@chronox.de>
To: herbert@gondor.apana.org.au
Cc: pebolle@tiscali.nl, andreas.steffen@strongswan.org,
	tytso@mit.edu, sandyinchina@gmail.com,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v9 3/5] crypto: drbg - add async seeding operation
Date: Wed, 20 May 2015 22:03:45 +0200	[thread overview]
Message-ID: <12904468.qCSNRrkb6t@tachyon.chronox.de> (raw)
In-Reply-To: <3340545.QDDPvU7BuN@tachyon.chronox.de>

The 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.

CC: Andreas Steffen <andreas.steffen@strongswan.org>
CC: Theodore Ts'o <tytso@mit.edu>
CC: Sandy Harris <sandyinchina@gmail.com>
Signed-off-by: Stephan Mueller <smueller@chronox.de>
---
 crypto/drbg.c         | 28 ++++++++++++++++++++++++++++
 include/crypto/drbg.h |  2 ++
 2 files changed, 30 insertions(+)

diff --git a/crypto/drbg.c b/crypto/drbg.c
index 36dfece..563e5e9 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -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);
diff --git a/include/crypto/drbg.h b/include/crypto/drbg.h
index b052698..46994b2 100644
--- a/include/crypto/drbg.h
+++ b/include/crypto/drbg.h
@@ -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;
-- 
2.1.0

  parent reply	other threads:[~2015-05-20 20:03 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-20 20:02 [PATCH v9 0/5] Seeding DRBG with more entropy Stephan Mueller
2015-05-20 20:02 ` [PATCH v9 1/5] random: Blocking API for accessing nonblocking_pool Stephan Mueller
2015-05-20 20:03 ` [PATCH v9 2/5] crypto: drbg - prepare for async seeding Stephan Mueller
2015-05-20 20:03 ` Stephan Mueller [this message]
2015-05-20 21:44   ` [PATCH v9 3/5] crypto: drbg - add async seeding operation Herbert Xu
2015-05-21  6:10     ` Stephan Mueller
2015-05-21  6:36       ` Herbert Xu
2015-05-21  6:53         ` Stephan Mueller
2015-05-21  6:56           ` Herbert Xu
2015-05-21  7:55     ` Stephan Mueller
2015-05-21  8:19       ` random: Wake up all getrandom(2) callers when pool is ready Herbert Xu
2015-05-21  8:36         ` Stephan Mueller
2015-05-21 19:17         ` Theodore Ts'o
2015-05-22  0:23           ` Herbert Xu
2015-05-20 20:04 ` [PATCH v9 4/5] crypto: drbg - use Jitter RNG to obtain seed Stephan Mueller
2015-05-20 20:04 ` [PATCH v9 5/5] crypto: add jitterentropy RNG Stephan Mueller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12904468.qCSNRrkb6t@tachyon.chronox.de \
    --to=smueller@chronox.de \
    --cc=andreas.steffen@strongswan.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pebolle@tiscali.nl \
    --cc=sandyinchina@gmail.com \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox