From b65c659b2c1b5af299b2ac7e5d318d8af6f97647 Mon Sep 17 00:00:00 2001
From: tb <>
Date: Tue, 6 Nov 2018 06:55:27 +0000
Subject: add a regression test for bn_rand_interval()

---
 src/regress/lib/libcrypto/bn/rand/Makefile         | 11 +++
 .../lib/libcrypto/bn/rand/bn_rand_interval.c       | 87 ++++++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 src/regress/lib/libcrypto/bn/rand/Makefile
 create mode 100644 src/regress/lib/libcrypto/bn/rand/bn_rand_interval.c

diff --git a/src/regress/lib/libcrypto/bn/rand/Makefile b/src/regress/lib/libcrypto/bn/rand/Makefile
new file mode 100644
index 0000000000..52d0835df4
--- /dev/null
+++ b/src/regress/lib/libcrypto/bn/rand/Makefile
@@ -0,0 +1,11 @@
+#	$OpenBSD: Makefile,v 1.1 2018/11/06 06:55:27 tb Exp $
+
+.include "../../Makefile.inc"
+
+PROG=	bn_rand_interval
+LDADD=	${CRYPTO_INT}
+DPADD=	${LIBCRYPTO}
+WARNINGS=	Yes
+CFLAGS+=	-Werror
+
+.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/bn/rand/bn_rand_interval.c b/src/regress/lib/libcrypto/bn/rand/bn_rand_interval.c
new file mode 100644
index 0000000000..57c55f0496
--- /dev/null
+++ b/src/regress/lib/libcrypto/bn/rand/bn_rand_interval.c
@@ -0,0 +1,87 @@
+/*	$OpenBSD: bn_rand_interval.c,v 1.1 2018/11/06 06:55:27 tb Exp $	*/
+/*
+ * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+#include <stdio.h>
+
+#include <openssl/bn.h>
+
+#define NUM_TESTS 1000000
+
+int bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_incl,
+    const BIGNUM *upper_excl);
+
+
+int
+main(int argc, char *argv[])
+{
+	BIGNUM *a, *b, *x;
+	int i, success = 1;
+
+	if ((a = BN_new()) == NULL)
+		err(1, "BN_hex2bn");
+	if ((b = BN_new()) == NULL)
+		err(1, "BN_hex2bn");
+	if ((x = BN_new()) == NULL)
+		err(1, "BN_new()");
+	
+	for (i = 0; i < NUM_TESTS; i++) {
+		if (!BN_rand(a, 256, 0, 0))
+			err(1, "BN_rand(a)");
+
+		if (bn_rand_interval(x, a, a) != 0) {
+			success = 0;
+			printf("bn_rand_interval(a == a) succeeded\n");
+		}
+
+		if (!BN_rand(b, 256, 0, 0))
+			err(1, "BN_rand(b)");
+
+		switch(BN_cmp(a, b)) {
+		case 0:		/* a == b */
+			continue;
+
+		case 1:		/* a > b */
+			BN_swap(a, b);
+			break;
+
+		default:	/* a < b */
+			break;
+		}
+
+		if (!bn_rand_interval(x, a, b))
+			err(1, "bn_rand_interval() failed");
+
+		if (BN_cmp(x, a) < 0 || BN_cmp(x, b) >= 0) {
+			printf("generated number xnot inside [a,b)\n");
+			printf("a = ");
+			BN_print_fp(stdout, a);
+			printf("\nb = ");
+			BN_print_fp(stdout, b);
+			printf("\nx = ");
+			BN_print_fp(stdout, x);
+			printf("\n");
+		}
+	}
+
+	if (success == 1)
+		printf("success\n");
+	else
+		printf("FAIL");
+
+	return 1 - success;
+}
-- 
cgit v1.2.3-55-g6feb