diff options
author | Theo Buehler <tb@cvs.openbsd.org> | 2018-11-06 06:55:28 +0000 |
---|---|---|
committer | Theo Buehler <tb@cvs.openbsd.org> | 2018-11-06 06:55:28 +0000 |
commit | 46c0ea62e5bfc07f32003f8381f527068e6a026d (patch) | |
tree | 79c985c12bcaaf1bf189f109b0cf6f7604c6f29d /regress/lib/libcrypto/bn | |
parent | 495bfc32706b241ee11a1bc92f4870ce13cd5fea (diff) |
add a regression test for bn_rand_interval()
Diffstat (limited to 'regress/lib/libcrypto/bn')
-rw-r--r-- | regress/lib/libcrypto/bn/rand/Makefile | 11 | ||||
-rw-r--r-- | regress/lib/libcrypto/bn/rand/bn_rand_interval.c | 87 |
2 files changed, 98 insertions, 0 deletions
diff --git a/regress/lib/libcrypto/bn/rand/Makefile b/regress/lib/libcrypto/bn/rand/Makefile new file mode 100644 index 00000000000..52d0835df41 --- /dev/null +++ b/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/regress/lib/libcrypto/bn/rand/bn_rand_interval.c b/regress/lib/libcrypto/bn/rand/bn_rand_interval.c new file mode 100644 index 00000000000..57c55f04963 --- /dev/null +++ b/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; +} |