diff options
author | jsing <> | 2024-04-09 14:59:57 +0000 |
---|---|---|
committer | jsing <> | 2024-04-09 14:59:57 +0000 |
commit | bc8830ea3ee7194a6d4d4915db8a2b7ccc9aef27 (patch) | |
tree | 672725d097832c2f1d0482ef1df6015a3d8afdd3 /src | |
parent | 190be22690a7ace69ef54b351225b8b954709c2d (diff) | |
download | openbsd-bc8830ea3ee7194a6d4d4915db8a2b7ccc9aef27.tar.gz openbsd-bc8830ea3ee7194a6d4d4915db8a2b7ccc9aef27.tar.bz2 openbsd-bc8830ea3ee7194a6d4d4915db8a2b7ccc9aef27.zip |
Add initial regress for BN_set_bit(), BN_clear_bit() and BN_mask_bits().
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/Makefile | 3 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_bits.c | 227 |
2 files changed, 229 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/Makefile b/src/regress/lib/libcrypto/bn/Makefile index 6342a6142e..8f49050615 100644 --- a/src/regress/lib/libcrypto/bn/Makefile +++ b/src/regress/lib/libcrypto/bn/Makefile | |||
@@ -1,6 +1,7 @@ | |||
1 | # $OpenBSD: Makefile,v 1.37 2023/10/19 10:23:38 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.38 2024/04/09 14:59:57 jsing Exp $ |
2 | 2 | ||
3 | PROGS += bn_add_sub | 3 | PROGS += bn_add_sub |
4 | PROGS += bn_bits | ||
4 | PROGS += bn_cmp | 5 | PROGS += bn_cmp |
5 | PROGS += bn_convert | 6 | PROGS += bn_convert |
6 | PROGS += bn_gcd | 7 | PROGS += bn_gcd |
diff --git a/src/regress/lib/libcrypto/bn/bn_bits.c b/src/regress/lib/libcrypto/bn/bn_bits.c new file mode 100644 index 0000000000..4ed13e424b --- /dev/null +++ b/src/regress/lib/libcrypto/bn/bn_bits.c | |||
@@ -0,0 +1,227 @@ | |||
1 | /* $OpenBSD: bn_bits.c,v 1.1 2024/04/09 14:59:57 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2024 Joel Sing <jsing@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 <stdio.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | #include <openssl/bn.h> | ||
22 | |||
23 | static int | ||
24 | test_bn_set_bit(void) | ||
25 | { | ||
26 | BIGNUM *bn = NULL; | ||
27 | char *out_str = NULL; | ||
28 | size_t i; | ||
29 | int failed = 1; | ||
30 | |||
31 | if ((bn = BN_new()) == NULL) | ||
32 | goto failure; | ||
33 | |||
34 | for (i = 0; i < 128; i++) { | ||
35 | if (i % 2 == 0) { | ||
36 | if (!BN_set_bit(bn, i)) { | ||
37 | fprintf(stderr, "FAIL: failed to set bit\n"); | ||
38 | goto failure; | ||
39 | } | ||
40 | } | ||
41 | if (BN_is_bit_set(bn, i) != (i % 2 == 0)) { | ||
42 | fprintf(stderr, "FAIL: BN_is_bit_set() = %d, want %d\n", | ||
43 | BN_is_bit_set(bn, i), (i % 2 == 0)); | ||
44 | goto failure; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | if ((out_str = BN_bn2hex(bn)) == NULL) | ||
49 | goto failure; | ||
50 | if (strcmp(out_str, "55555555555555555555555555555555") != 0) { | ||
51 | fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str, | ||
52 | "55555555555555555555555555555555"); | ||
53 | goto failure; | ||
54 | } | ||
55 | |||
56 | failed = 0; | ||
57 | |||
58 | failure: | ||
59 | BN_free(bn); | ||
60 | free(out_str); | ||
61 | |||
62 | return failed; | ||
63 | } | ||
64 | |||
65 | static int | ||
66 | test_bn_clear_bit(void) | ||
67 | { | ||
68 | BIGNUM *bn = NULL; | ||
69 | char *out_str = NULL; | ||
70 | size_t i; | ||
71 | int failed = 1; | ||
72 | |||
73 | if ((bn = BN_new()) == NULL) | ||
74 | goto failure; | ||
75 | |||
76 | for (i = 0; i < 128; i++) { | ||
77 | if (!BN_set_bit(bn, i)) { | ||
78 | fprintf(stderr, "FAIL: failed to set bit\n"); | ||
79 | goto failure; | ||
80 | } | ||
81 | if (i % 2 == 0) { | ||
82 | if (!BN_clear_bit(bn, i)) { | ||
83 | fprintf(stderr, "FAIL: failed to clear bit\n"); | ||
84 | goto failure; | ||
85 | } | ||
86 | } | ||
87 | if (BN_is_bit_set(bn, i) != (i % 2 == 1)) { | ||
88 | fprintf(stderr, "FAIL: BN_is_bit_set() = %d, want %d\n", | ||
89 | BN_is_bit_set(bn, i), (i % 2 == 1)); | ||
90 | goto failure; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | if ((out_str = BN_bn2hex(bn)) == NULL) | ||
95 | goto failure; | ||
96 | if (strcmp(out_str, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") != 0) { | ||
97 | fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str, | ||
98 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); | ||
99 | goto failure; | ||
100 | } | ||
101 | |||
102 | /* Ensure that clearing results in non-negative zero. */ | ||
103 | if (!BN_one(bn)) | ||
104 | goto failure; | ||
105 | BN_set_negative(bn, 1); | ||
106 | if (!BN_clear_bit(bn, 0)) { | ||
107 | fprintf(stderr, "FAIL: failed to clear bit\n"); | ||
108 | goto failure; | ||
109 | } | ||
110 | if (!BN_is_zero(bn)) { | ||
111 | fprintf(stderr, "FAIL: clear bit did not result in zero\n"); | ||
112 | goto failure; | ||
113 | } | ||
114 | #if 0 | ||
115 | if (BN_is_negative(bn)) { | ||
116 | fprintf(stderr, "FAIL: clear bit resulted in -0\n"); | ||
117 | goto failure; | ||
118 | } | ||
119 | #endif | ||
120 | |||
121 | failed = 0; | ||
122 | |||
123 | failure: | ||
124 | BN_free(bn); | ||
125 | free(out_str); | ||
126 | |||
127 | return failed; | ||
128 | } | ||
129 | |||
130 | static int | ||
131 | test_bn_mask_bits(void) | ||
132 | { | ||
133 | BIGNUM *bn = NULL; | ||
134 | char *out_str = NULL; | ||
135 | size_t i; | ||
136 | int failed = 1; | ||
137 | |||
138 | if ((bn = BN_new()) == NULL) | ||
139 | goto failure; | ||
140 | |||
141 | if (BN_mask_bits(bn, 0)) { | ||
142 | fprintf(stderr, "FAIL: mask bits should have failed\n"); | ||
143 | goto failure; | ||
144 | } | ||
145 | |||
146 | for (i = 0; i < 128; i++) { | ||
147 | if (!BN_set_bit(bn, i)) { | ||
148 | fprintf(stderr, "FAIL: failed to set bit\n"); | ||
149 | goto failure; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | if ((out_str = BN_bn2hex(bn)) == NULL) | ||
154 | goto failure; | ||
155 | if (strcmp(out_str, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") != 0) { | ||
156 | fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str, | ||
157 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); | ||
158 | goto failure; | ||
159 | } | ||
160 | |||
161 | if (!BN_mask_bits(bn, 127)) { | ||
162 | fprintf(stderr, "FAIL: failed to mask bits\n"); | ||
163 | goto failure; | ||
164 | } | ||
165 | |||
166 | free(out_str); | ||
167 | if ((out_str = BN_bn2hex(bn)) == NULL) | ||
168 | goto failure; | ||
169 | if (strcmp(out_str, "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") != 0) { | ||
170 | fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str, | ||
171 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); | ||
172 | goto failure; | ||
173 | } | ||
174 | |||
175 | if (!BN_mask_bits(bn, 65)) { | ||
176 | fprintf(stderr, "FAIL: failed to mask bits\n"); | ||
177 | goto failure; | ||
178 | } | ||
179 | |||
180 | free(out_str); | ||
181 | if ((out_str = BN_bn2hex(bn)) == NULL) | ||
182 | goto failure; | ||
183 | if (strcmp(out_str, "01FFFFFFFFFFFFFFFF") != 0) { | ||
184 | fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str, | ||
185 | "01FFFFFFFFFFFFFFFF"); | ||
186 | goto failure; | ||
187 | } | ||
188 | |||
189 | /* Ensure that masking results in non-negative zero. */ | ||
190 | if (!BN_one(bn)) | ||
191 | goto failure; | ||
192 | BN_set_negative(bn, 1); | ||
193 | if (!BN_mask_bits(bn, 0)) { | ||
194 | fprintf(stderr, "FAIL: failed to mask bits\n"); | ||
195 | goto failure; | ||
196 | } | ||
197 | if (!BN_is_zero(bn)) { | ||
198 | fprintf(stderr, "FAIL: mask bits did not result in zero\n"); | ||
199 | goto failure; | ||
200 | } | ||
201 | #if 0 | ||
202 | if (BN_is_negative(bn)) { | ||
203 | fprintf(stderr, "FAIL: mask bits resulted in -0\n"); | ||
204 | goto failure; | ||
205 | } | ||
206 | #endif | ||
207 | |||
208 | failed = 0; | ||
209 | |||
210 | failure: | ||
211 | BN_free(bn); | ||
212 | free(out_str); | ||
213 | |||
214 | return failed; | ||
215 | } | ||
216 | |||
217 | int | ||
218 | main(int argc, char **argv) | ||
219 | { | ||
220 | int failed = 0; | ||
221 | |||
222 | failed |= test_bn_set_bit(); | ||
223 | failed |= test_bn_clear_bit(); | ||
224 | failed |= test_bn_mask_bits(); | ||
225 | |||
226 | return failed; | ||
227 | } | ||