diff options
author | tb <> | 2018-11-06 06:55:27 +0000 |
---|---|---|
committer | tb <> | 2018-11-06 06:55:27 +0000 |
commit | b65c659b2c1b5af299b2ac7e5d318d8af6f97647 (patch) | |
tree | 6c2c47cabe0b175c005165f2032734a752f00464 /src | |
parent | df94debe54462b28ed0cf3c46f72c9bfc375b619 (diff) | |
download | openbsd-b65c659b2c1b5af299b2ac7e5d318d8af6f97647.tar.gz openbsd-b65c659b2c1b5af299b2ac7e5d318d8af6f97647.tar.bz2 openbsd-b65c659b2c1b5af299b2ac7e5d318d8af6f97647.zip |
add a regression test for bn_rand_interval()
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/rand/Makefile | 11 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/rand/bn_rand_interval.c | 87 |
2 files changed, 98 insertions, 0 deletions
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 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2018/11/06 06:55:27 tb Exp $ | ||
2 | |||
3 | .include "../../Makefile.inc" | ||
4 | |||
5 | PROG= bn_rand_interval | ||
6 | LDADD= ${CRYPTO_INT} | ||
7 | DPADD= ${LIBCRYPTO} | ||
8 | WARNINGS= Yes | ||
9 | CFLAGS+= -Werror | ||
10 | |||
11 | .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 @@ | |||
1 | /* $OpenBSD: bn_rand_interval.c,v 1.1 2018/11/06 06:55:27 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <err.h> | ||
19 | #include <stdio.h> | ||
20 | |||
21 | #include <openssl/bn.h> | ||
22 | |||
23 | #define NUM_TESTS 1000000 | ||
24 | |||
25 | int bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_incl, | ||
26 | const BIGNUM *upper_excl); | ||
27 | |||
28 | |||
29 | int | ||
30 | main(int argc, char *argv[]) | ||
31 | { | ||
32 | BIGNUM *a, *b, *x; | ||
33 | int i, success = 1; | ||
34 | |||
35 | if ((a = BN_new()) == NULL) | ||
36 | err(1, "BN_hex2bn"); | ||
37 | if ((b = BN_new()) == NULL) | ||
38 | err(1, "BN_hex2bn"); | ||
39 | if ((x = BN_new()) == NULL) | ||
40 | err(1, "BN_new()"); | ||
41 | |||
42 | for (i = 0; i < NUM_TESTS; i++) { | ||
43 | if (!BN_rand(a, 256, 0, 0)) | ||
44 | err(1, "BN_rand(a)"); | ||
45 | |||
46 | if (bn_rand_interval(x, a, a) != 0) { | ||
47 | success = 0; | ||
48 | printf("bn_rand_interval(a == a) succeeded\n"); | ||
49 | } | ||
50 | |||
51 | if (!BN_rand(b, 256, 0, 0)) | ||
52 | err(1, "BN_rand(b)"); | ||
53 | |||
54 | switch(BN_cmp(a, b)) { | ||
55 | case 0: /* a == b */ | ||
56 | continue; | ||
57 | |||
58 | case 1: /* a > b */ | ||
59 | BN_swap(a, b); | ||
60 | break; | ||
61 | |||
62 | default: /* a < b */ | ||
63 | break; | ||
64 | } | ||
65 | |||
66 | if (!bn_rand_interval(x, a, b)) | ||
67 | err(1, "bn_rand_interval() failed"); | ||
68 | |||
69 | if (BN_cmp(x, a) < 0 || BN_cmp(x, b) >= 0) { | ||
70 | printf("generated number xnot inside [a,b)\n"); | ||
71 | printf("a = "); | ||
72 | BN_print_fp(stdout, a); | ||
73 | printf("\nb = "); | ||
74 | BN_print_fp(stdout, b); | ||
75 | printf("\nx = "); | ||
76 | BN_print_fp(stdout, x); | ||
77 | printf("\n"); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | if (success == 1) | ||
82 | printf("success\n"); | ||
83 | else | ||
84 | printf("FAIL"); | ||
85 | |||
86 | return 1 - success; | ||
87 | } | ||