diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/bn/Makefile | 14 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/bn/bn_cmp.c | 336 |
2 files changed, 347 insertions, 3 deletions
diff --git a/src/regress/lib/libcrypto/bn/Makefile b/src/regress/lib/libcrypto/bn/Makefile index ed9298575b..89c8d7861a 100644 --- a/src/regress/lib/libcrypto/bn/Makefile +++ b/src/regress/lib/libcrypto/bn/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.6 2018/11/06 06:56:17 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.7 2022/11/30 02:51:05 jsing Exp $ |
| 2 | 2 | ||
| 3 | SUBDIR= \ | 3 | SUBDIR= \ |
| 4 | addsub \ | 4 | addsub \ |
| @@ -6,7 +6,15 @@ SUBDIR= \ | |||
| 6 | mont \ | 6 | mont \ |
| 7 | rand | 7 | rand |
| 8 | 8 | ||
| 9 | PROGS+= bn_cmp | ||
| 9 | 10 | ||
| 10 | install: | 11 | LDADD= -lcrypto |
| 12 | DPADD= ${LIBCRYPTO} | ||
| 13 | WARNINGS= Yes | ||
| 14 | CFLAGS+= -Wall -Wundef -Werror | ||
| 11 | 15 | ||
| 12 | .include <bsd.subdir.mk> | 16 | REGRESS_TARGETS += run-bn_cmp |
| 17 | run-bn_cmp: bn_cmp | ||
| 18 | ./bn_cmp | ||
| 19 | |||
| 20 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libcrypto/bn/bn_cmp.c b/src/regress/lib/libcrypto/bn/bn_cmp.c new file mode 100644 index 0000000000..2f5c7c02e7 --- /dev/null +++ b/src/regress/lib/libcrypto/bn/bn_cmp.c | |||
| @@ -0,0 +1,336 @@ | |||
| 1 | /* $OpenBSD: bn_cmp.c,v 1.1 2022/11/30 02:51:05 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2022 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 | |||
| 20 | #include <openssl/bn.h> | ||
| 21 | |||
| 22 | struct bn_cmp_test { | ||
| 23 | const char *a; | ||
| 24 | const char *b; | ||
| 25 | int cmp; | ||
| 26 | int ucmp; | ||
| 27 | }; | ||
| 28 | |||
| 29 | struct bn_cmp_test bn_cmp_tests[] = { | ||
| 30 | { | ||
| 31 | .a = "0", | ||
| 32 | .b = "0", | ||
| 33 | .cmp = 0, | ||
| 34 | .ucmp = 0, | ||
| 35 | }, | ||
| 36 | { | ||
| 37 | .a = "-1", | ||
| 38 | .b = "0", | ||
| 39 | .cmp = -1, | ||
| 40 | .ucmp = 1, | ||
| 41 | }, | ||
| 42 | { | ||
| 43 | .a = "1ffffffffffffffff", | ||
| 44 | .b = "1ffffffffffffffff", | ||
| 45 | .cmp = 0, | ||
| 46 | .ucmp = 0, | ||
| 47 | }, | ||
| 48 | { | ||
| 49 | .a = "1fffffffffffffffe", | ||
| 50 | .b = "1ffffffffffffffff", | ||
| 51 | .cmp = -1, | ||
| 52 | .ucmp = -1, | ||
| 53 | }, | ||
| 54 | { | ||
| 55 | .a = "1ffffffffffffffff", | ||
| 56 | .b = "1fffffffffffffffe", | ||
| 57 | .cmp = 1, | ||
| 58 | .ucmp = 1, | ||
| 59 | }, | ||
| 60 | { | ||
| 61 | .a = "0", | ||
| 62 | .b = "1ffffffffffffffff", | ||
| 63 | .cmp = -1, | ||
| 64 | .ucmp = -1, | ||
| 65 | }, | ||
| 66 | { | ||
| 67 | .a = "1ffffffffffffffff", | ||
| 68 | .b = "0", | ||
| 69 | .cmp = 1, | ||
| 70 | .ucmp = 1, | ||
| 71 | }, | ||
| 72 | { | ||
| 73 | .a = "-1ffffffffffffffff", | ||
| 74 | .b = "0", | ||
| 75 | .cmp = -1, | ||
| 76 | .ucmp = 1, | ||
| 77 | }, | ||
| 78 | }; | ||
| 79 | |||
| 80 | #define N_BN_CMP_TESTS \ | ||
| 81 | (sizeof(bn_cmp_tests) / sizeof(*bn_cmp_tests)) | ||
| 82 | |||
| 83 | static int | ||
| 84 | test_bn_cmp(void) | ||
| 85 | { | ||
| 86 | struct bn_cmp_test *bct; | ||
| 87 | BIGNUM *a = NULL, *b = NULL; | ||
| 88 | size_t i; | ||
| 89 | int ret; | ||
| 90 | int failed = 1; | ||
| 91 | |||
| 92 | if ((a = BN_new()) == NULL) { | ||
| 93 | fprintf(stderr, "FAIL: failed to create BN\n"); | ||
| 94 | goto failure; | ||
| 95 | } | ||
| 96 | if ((b = BN_new()) == NULL) { | ||
| 97 | fprintf(stderr, "FAIL: failed to create BN\n"); | ||
| 98 | goto failure; | ||
| 99 | } | ||
| 100 | |||
| 101 | for (i = 0; i < N_BN_CMP_TESTS; i++) { | ||
| 102 | bct = &bn_cmp_tests[i]; | ||
| 103 | |||
| 104 | if (!BN_hex2bn(&a, bct->a)) { | ||
| 105 | fprintf(stderr, "FAIL: failed to set a from hex\n"); | ||
| 106 | goto failure; | ||
| 107 | } | ||
| 108 | if (!BN_hex2bn(&b, bct->b)) { | ||
| 109 | fprintf(stderr, "FAIL: failed to set b from hex\n"); | ||
| 110 | goto failure; | ||
| 111 | } | ||
| 112 | |||
| 113 | if ((ret = BN_cmp(a, b)) != bct->cmp) { | ||
| 114 | fprintf(stderr, "FAIL: BN_cmp(%s, %s) = %d, want %d\n", | ||
| 115 | bct->a, bct->b, ret, bct->cmp); | ||
| 116 | goto failure; | ||
| 117 | } | ||
| 118 | if ((ret = BN_ucmp(a, b)) != bct->ucmp) { | ||
| 119 | fprintf(stderr, "FAIL: BN_ucmp(%s, %s) = %d, want %d\n", | ||
| 120 | bct->a, bct->b, ret, bct->ucmp); | ||
| 121 | goto failure; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | failed = 0; | ||
| 126 | |||
| 127 | failure: | ||
| 128 | BN_free(a); | ||
| 129 | BN_free(b); | ||
| 130 | |||
| 131 | return failed; | ||
| 132 | } | ||
| 133 | |||
| 134 | static int | ||
| 135 | test_bn_cmp_null(void) | ||
| 136 | { | ||
| 137 | BIGNUM *a = NULL; | ||
| 138 | int ret; | ||
| 139 | int failed = 1; | ||
| 140 | |||
| 141 | if ((a = BN_new()) == NULL) { | ||
| 142 | fprintf(stderr, "FAIL: failed to create BN\n"); | ||
| 143 | goto failure; | ||
| 144 | } | ||
| 145 | |||
| 146 | /* | ||
| 147 | * Comparison to NULL. | ||
| 148 | */ | ||
| 149 | if ((ret = BN_cmp(NULL, NULL)) != 0) { | ||
| 150 | fprintf(stderr, "FAIL: BN_cmp(NULL, NULL) == %d, want 0\n", ret); | ||
| 151 | goto failure; | ||
| 152 | } | ||
| 153 | |||
| 154 | if ((ret = BN_cmp(a, NULL)) != -1) { | ||
| 155 | fprintf(stderr, "FAIL: BN_cmp(0, NULL) == %d, want -1\n", ret); | ||
| 156 | goto failure; | ||
| 157 | } | ||
| 158 | if ((ret = BN_cmp(NULL, a)) != 1) { | ||
| 159 | fprintf(stderr, "FAIL: BN_cmp(NULL, 0) == %d, want 1\n", ret); | ||
| 160 | goto failure; | ||
| 161 | } | ||
| 162 | |||
| 163 | if (!BN_set_word(a, 1)) { | ||
| 164 | fprintf(stderr, "FAIL: failed to set BN to 1\n"); | ||
| 165 | goto failure; | ||
| 166 | } | ||
| 167 | if ((ret = BN_cmp(a, NULL)) != -1) { | ||
| 168 | fprintf(stderr, "FAIL: BN_cmp(1, NULL) == %d, want -1\n", ret); | ||
| 169 | goto failure; | ||
| 170 | } | ||
| 171 | if ((ret = BN_cmp(NULL, a)) != 1) { | ||
| 172 | fprintf(stderr, "FAIL: BN_cmp(NULL, 1) == %d, want 1\n", ret); | ||
| 173 | goto failure; | ||
| 174 | } | ||
| 175 | |||
| 176 | BN_set_negative(a, 1); | ||
| 177 | if ((ret = BN_cmp(a, NULL)) != -1) { | ||
| 178 | fprintf(stderr, "FAIL: BN_cmp(-1, NULL) == %d, want -1\n", ret); | ||
| 179 | goto failure; | ||
| 180 | } | ||
| 181 | if ((ret = BN_cmp(NULL, a)) != 1) { | ||
| 182 | fprintf(stderr, "FAIL: BN_cmp(NULL, -1) == %d, want 1\n", ret); | ||
| 183 | goto failure; | ||
| 184 | } | ||
| 185 | |||
| 186 | failed = 0; | ||
| 187 | |||
| 188 | failure: | ||
| 189 | BN_free(a); | ||
| 190 | |||
| 191 | return failed; | ||
| 192 | } | ||
| 193 | |||
| 194 | struct bn_cmp_word_test { | ||
| 195 | int a; | ||
| 196 | int b; | ||
| 197 | int cmp; | ||
| 198 | int ucmp; | ||
| 199 | }; | ||
| 200 | |||
| 201 | struct bn_cmp_word_test bn_cmp_word_tests[] = { | ||
| 202 | { | ||
| 203 | .a = -1, | ||
| 204 | .b = -1, | ||
| 205 | .cmp = 0, | ||
| 206 | .ucmp = 0, | ||
| 207 | }, | ||
| 208 | { | ||
| 209 | .a = 0, | ||
| 210 | .b = 0, | ||
| 211 | .cmp = 0, | ||
| 212 | .ucmp = 0, | ||
| 213 | }, | ||
| 214 | { | ||
| 215 | .a = 1, | ||
| 216 | .b = 1, | ||
| 217 | .cmp = 0, | ||
| 218 | .ucmp = 0, | ||
| 219 | }, | ||
| 220 | { | ||
| 221 | .a = 0, | ||
| 222 | .b = 1, | ||
| 223 | .cmp = -1, | ||
| 224 | .ucmp = -1, | ||
| 225 | }, | ||
| 226 | { | ||
| 227 | .a = 1, | ||
| 228 | .b = 0, | ||
| 229 | .cmp = 1, | ||
| 230 | .ucmp = 1, | ||
| 231 | }, | ||
| 232 | { | ||
| 233 | .a = -1, | ||
| 234 | .b = 0, | ||
| 235 | .cmp = -1, | ||
| 236 | .ucmp = 1, | ||
| 237 | }, | ||
| 238 | { | ||
| 239 | .a = 0, | ||
| 240 | .b = -1, | ||
| 241 | .cmp = 1, | ||
| 242 | .ucmp = -1, | ||
| 243 | }, | ||
| 244 | { | ||
| 245 | .a = -1, | ||
| 246 | .b = 1, | ||
| 247 | .cmp = -1, | ||
| 248 | .ucmp = 0, | ||
| 249 | }, | ||
| 250 | { | ||
| 251 | .a = 1, | ||
| 252 | .b = -1, | ||
| 253 | .cmp = 1, | ||
| 254 | .ucmp = 0, | ||
| 255 | }, | ||
| 256 | }; | ||
| 257 | |||
| 258 | #define N_BN_CMP_WORD_TESTS \ | ||
| 259 | (sizeof(bn_cmp_word_tests) / sizeof(*bn_cmp_word_tests)) | ||
| 260 | |||
| 261 | static int | ||
| 262 | test_bn_cmp_word(void) | ||
| 263 | { | ||
| 264 | struct bn_cmp_word_test *bcwt; | ||
| 265 | BIGNUM *a = NULL, *b = NULL; | ||
| 266 | BN_ULONG v; | ||
| 267 | size_t i; | ||
| 268 | int ret; | ||
| 269 | int failed = 1; | ||
| 270 | |||
| 271 | if ((a = BN_new()) == NULL) { | ||
| 272 | fprintf(stderr, "FAIL: failed to create BN\n"); | ||
| 273 | goto failure; | ||
| 274 | } | ||
| 275 | if ((b = BN_new()) == NULL) { | ||
| 276 | fprintf(stderr, "FAIL: failed to create BN\n"); | ||
| 277 | goto failure; | ||
| 278 | } | ||
| 279 | |||
| 280 | for (i = 0; i < N_BN_CMP_WORD_TESTS; i++) { | ||
| 281 | bcwt = &bn_cmp_word_tests[i]; | ||
| 282 | |||
| 283 | if (bcwt->a >= 0) { | ||
| 284 | v = bcwt->a; | ||
| 285 | } else { | ||
| 286 | v = 0 - bcwt->a; | ||
| 287 | } | ||
| 288 | if (!BN_set_word(a, v)) { | ||
| 289 | fprintf(stderr, "FAIL: failed to set a\n"); | ||
| 290 | goto failure; | ||
| 291 | } | ||
| 292 | BN_set_negative(a, (bcwt->a < 0)); | ||
| 293 | |||
| 294 | if (bcwt->b >= 0) { | ||
| 295 | v = bcwt->b; | ||
| 296 | } else { | ||
| 297 | v = 0 - bcwt->b; | ||
| 298 | } | ||
| 299 | if (!BN_set_word(b, v)) { | ||
| 300 | fprintf(stderr, "FAIL: failed to set b\n"); | ||
| 301 | goto failure; | ||
| 302 | } | ||
| 303 | BN_set_negative(b, (bcwt->b < 0)); | ||
| 304 | |||
| 305 | if ((ret = BN_cmp(a, b)) != bcwt->cmp) { | ||
| 306 | fprintf(stderr, "FAIL: BN_cmp(%d, %d) = %d, want %d\n", | ||
| 307 | bcwt->a, bcwt->b, ret, bcwt->cmp); | ||
| 308 | goto failure; | ||
| 309 | } | ||
| 310 | if ((ret = BN_ucmp(a, b)) != bcwt->ucmp) { | ||
| 311 | fprintf(stderr, "FAIL: BN_ucmp(%d, %d) = %d, want %d\n", | ||
| 312 | bcwt->a, bcwt->b, ret, bcwt->ucmp); | ||
| 313 | goto failure; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | failed = 0; | ||
| 318 | |||
| 319 | failure: | ||
| 320 | BN_free(a); | ||
| 321 | BN_free(b); | ||
| 322 | |||
| 323 | return failed; | ||
| 324 | } | ||
| 325 | |||
| 326 | int | ||
| 327 | main(int argc, char **argv) | ||
| 328 | { | ||
| 329 | int failed = 0; | ||
| 330 | |||
| 331 | failed |= test_bn_cmp(); | ||
| 332 | failed |= test_bn_cmp_null(); | ||
| 333 | failed |= test_bn_cmp_word(); | ||
| 334 | |||
| 335 | return failed; | ||
| 336 | } | ||
