mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stephan Mueller <smueller@chronox.de>
To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org
Cc: aquini@redhat.com, jeremy.wayne.powell@gmail.com
Subject: Re: [PATCH v2 1/6] SP800-90A Deterministic Random Bit Generator
Date: Wed, 19 Mar 2014 08:51:45 +0100	[thread overview]
Message-ID: <34882876.HnRfi0MJtt@myon.chronox.de> (raw)
In-Reply-To: <3662681.npTzbSq3ye@myon.chronox.de>

Am Montag, 17. März 2014, 08:34:06 schrieb Stephan Mueller:

> +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;
> +	struct drbg_string *data2;
> +
> +	/* 9.1 / 9.2 / 9.3.1 step 3 */
> +	if (pers && pers->len > (drbg_max_addtl(drbg)))
> +		return -EINVAL;
> +
> +	if (drbg->test_data) {
> +		data1.buf = drbg->test_data->testentropy->buf;
> +		data1.len = drbg->test_data->testentropy->len;
> +		data1.next = NULL;
> +	} 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) / 8);

drbg_sec_strength returns the strength in bytes, thus the division by 8 must 
be removed

> +		if (!entropylen)
> +			return -EFAULT;
> +		if (!reseed)
> +			/* make sure we round up strength/2 in
> +			 * case it is not divisible by 2 */
> +			entropylen = ((entropylen + 1) / 2) * 3;
> +
> +		entropy = kzalloc(entropylen, GFP_KERNEL);
> +		if (!entropy)
> +			return -ENOMEM;
> +		get_random_bytes(entropy, entropylen);
> +		drbg_string_fill(&data1, entropy, entropylen);
> +	}
> +
> +	/* concatenation of entropy with personalization str / addtl input) */
> +	if (pers && 0 < pers->len) {
> +		data2 = pers;
> +		data2->next = NULL;
> +		data1.next = data2;
> +	}
> +
> +	ret = drbg->d_ops->update(drbg, &data1, reseed);
> +	if (ret)
> +		goto out;
> +
> +	drbg->seeded = true;
> +	/* 10.1.1.2 / 10.1.1.3 step 5 */
> +	drbg->reseed_ctr = 1;
> +
> +out:
> +	if (entropy)
> +		kzfree(entropy);
> +	return ret;
> +}
> +

[...] 
> +static unsigned int drbg_generate(struct drbg_state *drbg,
> +				  unsigned char *buf, unsigned int buflen,
> +				  struct drbg_string *addtl)
> +{
> +	unsigned int len = 0;
> +	struct drbg_state *shadow = NULL;
> +
> +	if (0 == buflen || !buf)
> +		return 0;
> +	if (addtl && NULL == addtl->buf && 0 < addtl->len)
> +		return 0;
> +
> +	if (drbg_make_shadow(drbg, &shadow))
> +		return 0;
> +	/* 9.3.1 step 2 */
> +	if (buflen > (drbg_max_request_bytes(shadow)))
> +		goto err;
> +	/* 9.3.1 step 3 is implicit with the chosen DRBG */
> +	/* 9.3.1 step 4 */
> +	if (addtl && addtl->len > (drbg_max_addtl(shadow)))
> +		goto err;
> +	/* 9.3.1 step 5 is implicit with the chosen DRBG */
> +	/* 9.3.1 step 6 and 9 supplemented by 9.3.2 step c -- the spec is a
> +	 * bit convoluted here, we make it simpler */
> +	if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
> +		shadow->seeded = false;
> +
> +	/* allocate cipher handle */
> +	if (shadow->d_ops->crypto_init && shadow->d_ops->crypto_init(shadow))
> +		goto err;
> +
> +	if (shadow->pr || !shadow->seeded) {
> +		/* 9.3.1 steps 7.1 through 7.3 */
> +		if (drbg_seed(shadow, addtl, true))
> +			goto err;
> +		/* 9.3.1 step 7.4 */
> +		addtl = NULL;
> +	}
> +	/* 9.3.1 step 8 and 10 */
> +	len = drbg->d_ops->generate(shadow, buf, buflen, addtl);

This needs to be shadow->d_ops
> +
> +	/* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
> +	shadow->reseed_ctr++;
> +
> +err:
> +	if (shadow->d_ops->crypto_fini)
> +		shadow->d_ops->crypto_fini(shadow);
> +	drbg_restore_shadow(drbg, &shadow);
> +	return len;
> +}

Ciao
Stephan

  parent reply	other threads:[~2014-03-19  7:51 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-08 23:43 [PATCH 0/6] " Stephan Mueller
2014-03-08 23:46 ` [PATCH 1/6] " Stephan Mueller
2014-03-08 23:46   ` [PATCH 2/6] header file for DRBG Stephan Mueller
2014-03-08 23:47     ` [PATCH 3/6] DRBG kernel configuration options Stephan Mueller
2014-03-08 23:48       ` [PATCH 4/6] compile the DRBG code Stephan Mueller
2014-03-08 23:49         ` [PATCH 5/6] DRBG testmgr test vectors Stephan Mueller
2014-03-08 23:50           ` [PATCH 6/6] Add DRBG test code to testmgr Stephan Mueller
2014-03-10 13:56     ` [PATCH 2/6] header file for DRBG Rafael Aquini
2014-03-10 13:36   ` [PATCH 1/6] SP800-90A Deterministic Random Bit Generator Rafael Aquini
2014-03-17  7:34   ` [PATCH v2 " Stephan Mueller
2014-03-17  7:35     ` [PATCH v2 2/6] header file for DRBG Stephan Mueller
2014-03-17  7:35       ` [PATCH v2 3/6] DRBG kernel configuration options Stephan Mueller
2014-03-17  7:37         ` [PATCH v2 4/6] compile the DRBG code Stephan Mueller
2014-03-17  7:38           ` [PATCH v2 5/6] DRBG testmgr test vectors Stephan Mueller
2014-03-17  7:39             ` [PATCH v2 6/6] Add DRBG test code to testmgr Stephan Mueller
2014-04-11 18:07       ` [PATCH v4 2/6] header file for DRBG Stephan Mueller
2014-03-19  7:51     ` Stephan Mueller [this message]
2014-03-20  8:12     ` [PATCH v2 1/6] SP800-90A Deterministic Random Bit Generator Clemens Ladisch
2014-03-20 13:30       ` Stephan Mueller
2014-03-27 19:53     ` [PATCH v3 " Stephan Mueller
2014-03-27 19:56     ` Stephan Mueller
2014-04-11 18:07       ` [PATCH v4 " Stephan Mueller
2014-04-11 18:20         ` Joe Perches
2014-04-11 19:24           ` Stephan Mueller
2014-04-15  5:35         ` [PATCH v5 " Stephan Mueller
2014-04-15  5:51           ` Joe Perches
2014-04-15  6:08             ` Stephan Mueller
2014-04-26 20:13           ` [PATCH v6 " Stephan Mueller
2014-05-20 21:32             ` Rafael Aquini

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=34882876.HnRfi0MJtt@myon.chronox.de \
    --to=smueller@chronox.de \
    --cc=aquini@redhat.com \
    --cc=jeremy.wayne.powell@gmail.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®