All of lore.kernel.org
 help / color / mirror / Atom feed
From: George Spelvin <linux@horizon.com>
To: nhorman@tuxdriver.com, linux-crypto@vger.kernel.org
Cc: smueller@chronox.de, herbert@gondor.apana.org.au, linux@horizon.com
Subject: [PATCH v2 18/25] crypto: testmgr - Add CPRNG stutter test.
Date: Sun,  7 Dec 2014 07:26:26 -0500	[thread overview]
Message-ID: <cc79d229013770c762886e1d3098656216fb9f83.1417951990.git.linux@horizon.com> (raw)
In-Reply-To: <cover.1417951990.git.linux@horizon.com>
In-Reply-To: <cover.1417951990.git.linux@horizon.com>

This is a test for the kind of bug that caused CVE-2013-4345,
which was in the code that adapted from fixed-size cipher blocks
to arbitrary-sized reads.

This does every known answer test twice: the first time using
block-aligned reads (assuming the known answer is a full block),
and a second time using reads of size 0, 1, 2, ... 61, 0, 1, ...

A simple 32-bit hash of all of the output bytes (not just the known
ones) is compared between the two.  Any error in the bookkeeping
will result in a mismatch.

A non-cryptographic hash suffices to detect coding errors.
We just need something better than an additive checksum, because
transposition is possible.

The maximum read size (61) is chosen so the pattern repeats
after 1891 = 31 * 61 bytes.  If this is relatively prime to the
CPRNG's internal buffer size, a long enough test will eventually
explore every possible read size at every possible alignment in
the CPRNG's internal buffer.

Signed-off-by: George Spelvin <linux@horizon.com>
---
 crypto/testmgr.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 89 insertions(+), 9 deletions(-)

diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index 6bf43682..a15860ad 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -1455,11 +1455,15 @@ static int test_cprng(struct crypto_rng *tfm,
 		      const struct cprng_testvec *template, unsigned int tcount)
 {
 	const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
-	int err = 0, i, j;
-	u8 result[32];
+	int err = 0, i;
 
 	for (i = 0; i < tcount; i++) {
-		if (template[i].rlen > sizeof(result)) {
+		int j, k, bytes;
+		u32 hash1 = 0, hash2 = 0;
+		u8 result[61];		/* See below for size advice */
+		int rlen = template[i].rlen;
+
+		if (rlen > sizeof(result)) {
 			printk(KERN_CRIT "alg: cprng: Cannot test %s\n", algo);
 			err = -EOVERFLOW;
 			break;
@@ -1468,33 +1472,109 @@ static int test_cprng(struct crypto_rng *tfm,
 
 		err = crypto_rng_reset(tfm, template[i].seed, template[i].slen);
 		if (err) {
+fail_reset:
 			printk(KERN_ERR "alg: cprng: Failed to reset rng "
 			       "for %s\n", algo);
 			break;
 		}
 
 		for (j = 0; j < template[i].loops; j++) {
-			err = crypto_rng_get_bytes(tfm, result,
-						   template[i].rlen);
-			if (err != template[i].rlen) {
+			err = crypto_rng_get_bytes(tfm, result, rlen);
+			if (err != rlen) {
+fail_get:
 				printk(KERN_ERR "alg: cprng: Failed to obtain "
 				       "the correct amount of random data for "
 				       "%s (requested %d, got %d)\n", algo,
-				       template[i].rlen, err);
+				       rlen, err);
 				if (err >= 0)
 					err = -EINVAL;
 				break;
 			}
+			/* Compute simple hash for use by stutter test */
+			for (k = 0; k < rlen; k++) {
+				hash1 += result[k];
+				hash1 += hash1 << 10;
+				hash1 ^= hash1 >> 6;
+			}
 		}
 
-		err = memcmp(result, template[i].result, template[i].rlen);
+		err = memcmp(result, template[i].result, rlen);
 		if (err) {
 			printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
 			       i, algo);
-			hexdump(result, template[i].rlen);
+			hexdump(result, rlen);
 			err = -EINVAL;
 			break;
 		}
+
+		/*
+		 * Stutter test: repeat the computation, using odd-sized
+		 * reads.  This is to verify the output deblocking code,
+		 * which was the source of CVE-2013-4345.  So we read
+		 * from the RNG in 0, 1, 2, 3, 4, 5, ... byte chunks.
+		 *
+		 * When the read size reaches the size of our buffer,
+		 * the read size starts back at 0.
+		 *
+		 * For the current buffer size of 61, that happens after
+		 * 1891 = 31 * 61 bytes.  If this is relatively prime to
+		 * the CPRNG's internal state size, this will, if the test
+		 * is long enough, do every possible read size starting
+		 * at every possible offset in the CPRNG's internal state.
+		 *
+		 * The basic requirement to produce an odd total that
+		 * will be relatively prime to power-of-two state sizes
+		 * is that it be congruent to 1 or 2 mod 4.
+		 *
+		 * If you want to worry about larger factors, try:
+		 *  13:    91 =  7 * 13     73:  2701 = 37 * 73
+		 *  22:   253 = 11 * 23     82:  3403 = 41 * 83
+		 *  46:  1081 = 23 * 47    106:  5671 = 53 * 107
+		 *  58:  1711 = 29 * 59    157: 12403 = 79 * 157
+		 *  61:  1891 = 31 * 61    166: 13861 = 83 * 167
+		 *
+		 * The complete output streams are compared using a
+		 * non-cryptographic hash over the output bytes, which
+		 * is sufficient for the class of errors this is designed
+		 * to detect.
+		 */
+		err = crypto_rng_reset(tfm, template[i].seed, template[i].slen);
+		if (err)
+			goto fail_reset;
+
+		bytes = template[i].rlen * template[i].loops;
+		rlen = 0;
+		for (rlen = 0; ; rlen++) {
+			if (rlen > sizeof(result)) {
+				rlen = 0;
+			} else if (rlen > bytes) {
+				if (!bytes)
+					break;
+				rlen = bytes;
+			}
+			bytes -= rlen;
+			err = crypto_rng_get_bytes(tfm, result, rlen);
+			if (err != rlen)
+				goto fail_get;
+			/*
+			 * This is Bob Jenkins' one-at-a-time hash.
+			 * We just want something simple, byte-at-a-time,
+			 * and sensitive to transpositions, which a plain
+			 * additive checksum isn't.
+			 */
+			for (k = 0; k < rlen; k++) {
+				hash2 += result[k];
+				hash2 += hash2 << 10;
+				hash2 ^= hash2 >> 6;
+			}
+		}
+		if (hash1 != hash2) {
+			printk(KERN_ERR "alg: cprng: Stutter test %d failed "
+			       "for %s: %08x != %08x\n", i, algo, hash1, hash2);
+			err = -EINVAL;
+			break;
+		}
+		err = 0;
 	}
 
 	return err;
-- 
2.1.3

  parent reply	other threads:[~2014-12-07 12:27 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-07 12:26 [PATCH v2 00/25] Multiple changes to crypto/ansi_cprng.c George Spelvin
2014-12-07 12:26 ` [PATCH v2 01/25] crypto: ansi_cprng - unroll _get_more_prng_bytes George Spelvin
2014-12-07 12:26 ` [PATCH v2 02/25] crypto: ansi_cprng - Additional _get_more_prng_bytes cleanup George Spelvin
2014-12-07 12:26 ` [PATCH v2 03/25] crypto: ansi_cprng - Use %phN rather than print_hex_dump for debug George Spelvin
2014-12-07 12:26 ` [PATCH v2 04/25] crypto: ansi_cprng - Make debug output more like NIST test vectors George Spelvin
2014-12-07 12:26 ` [PATCH v2 05/25] crypto: ansi_cprng - Eliminate ctx->I and ctx->last_rand_data George Spelvin
2014-12-14 11:50   ` Stephan Mueller
2014-12-14 19:22     ` George Spelvin
2014-12-07 12:26 ` [PATCH v2 06/25] crypto: ansi_cprng - Make cont_test a bool George Spelvin
2014-12-07 12:26 ` [PATCH v2 07/25] crypto: ansi_cprng - Shrink context some more George Spelvin
2014-12-07 12:26 ` [PATCH v2 08/25] crypto: ansi_cprng - Don't call reset_prng_context from cprng_init George Spelvin
2014-12-07 12:26 ` [PATCH v2 09/25] crypto: ansi_cprng - Make length types consistent George Spelvin
2014-12-07 12:26 ` [PATCH v2 10/25] crypto: ansi_cprng - Use u8 data types consistently internally George Spelvin
2014-12-07 12:26 ` [PATCH v2 11/25] crypto: ansi_cprng - Eliminate unused PRNG_FIXED_SIZE flag George Spelvin
2014-12-07 12:26 ` [PATCH v2 12/25] crypto: ansi_cprng - Get rid of rdata buffer in fips_cprng_reset George Spelvin
2014-12-07 12:26 ` [PATCH v2 13/25] crypto: Add appropriate consts to RNG API George Spelvin
2014-12-14 11:39   ` Stephan Mueller
2014-12-07 12:26 ` [PATCH v2 14/25] crypto: tcrypt - Add const qualifiers all over the test code George Spelvin
2014-12-07 12:26 ` [PATCH v2 15/25] crypto: testmgr - Merge seed arrays in struct cprng_testvec George Spelvin
2014-12-07 12:26 ` [PATCH v2 16/25] crypto: testmgr - Report failure on zero-length crypto_rng_get_bytes George Spelvin
2014-12-07 12:26 ` [PATCH v2 17/25] crypto: testmgr - Don't crash if CPRNG test result is large George Spelvin
2014-12-07 12:26 ` George Spelvin [this message]
2014-12-07 12:26 ` [PATCH v2 19/25] crypto: ansi_cprng - simplify get_prng_bytes George Spelvin
2014-12-07 12:26 ` [PATCH v2 20/25] crypto: ansi_cprng - simplify xor_vectors() to xor_block() George Spelvin
2014-12-07 12:26 ` [PATCH v2 21/25] crypto: ansi_cprng - Rename rand_data_valid more sensibly George Spelvin
2014-12-07 12:26 ` [PATCH v2 22/25] crypto: ansi_cprng - Tweak comments George Spelvin
2014-12-07 12:26 ` [PATCH v2 23/25] crypto: ansi_cprng - Introduce a "union cipherblock" George Spelvin
2014-12-07 12:26 ` [PATCH v2 24/25] crypto: ansi_cprng - Introduce non-deterministic mode George Spelvin
2014-12-07 12:26 ` [PATCH v2 25/25] crypto: ansi_cprng - If non-deterministic, don't buffer old output George Spelvin
2014-12-07 22:49   ` George Spelvin
2014-12-08 14:22     ` Neil Horman
2014-12-08 16:43       ` George Spelvin
2014-12-08 18:07         ` Neil Horman
2014-12-08 20:34           ` George Spelvin
2014-12-14 12:06 ` [PATCH v2 00/25] Multiple changes to crypto/ansi_cprng.c Stephan Mueller
2014-12-14 19:47   ` George Spelvin
2014-12-15  6:18     ` Stephan Mueller
2014-12-14 20:37   ` George Spelvin
2014-12-15  6:14     ` Stephan Mueller
2014-12-15  8:42       ` George Spelvin
2014-12-15  8:50         ` Stephan Mueller
2014-12-15 10:45           ` George Spelvin
2014-12-15 11:08             ` Stephan Mueller
2014-12-15  5:53   ` George Spelvin
2014-12-15  6:27     ` Stephan Mueller
2014-12-15  8:28       ` George Spelvin
2014-12-15  8:56         ` Stephan Mueller
2014-12-15 10:21           ` George Spelvin
2014-12-15 10:46             ` Stephan Mueller
2014-12-15 11:32               ` Neil Horman
2014-12-15 22:01                 ` George Spelvin
2014-12-16  7:22                   ` Stephan Mueller
2014-12-16 11:32                   ` Neil Horman

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=cc79d229013770c762886e1d3098656216fb9f83.1417951990.git.linux@horizon.com \
    --to=linux@horizon.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=nhorman@tuxdriver.com \
    --cc=smueller@chronox.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.