diff options
author | jsing <> | 2024-04-09 14:56:21 +0000 |
---|---|---|
committer | jsing <> | 2024-04-09 14:56:21 +0000 |
commit | 72ca27ed5334f7009a47e6b3965193ac61408fe9 (patch) | |
tree | d2498e44b723eefd0f1d8633d25bca614936692c /src | |
parent | 9c19431c62c17b0cb6449afa074db6c43867bb8f (diff) | |
download | openbsd-72ca27ed5334f7009a47e6b3965193ac61408fe9.tar.gz openbsd-72ca27ed5334f7009a47e6b3965193ac61408fe9.tar.bz2 openbsd-72ca27ed5334f7009a47e6b3965193ac61408fe9.zip |
Add regress coverage for BN_bn2mpi()/BN_mpi2bn().
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_convert.c | 129 |
1 files changed, 123 insertions, 6 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_convert.c b/src/regress/lib/libcrypto/bn/bn_convert.c index 69f7da43b1..df69b78050 100644 --- a/src/regress/lib/libcrypto/bn/bn_convert.c +++ b/src/regress/lib/libcrypto/bn/bn_convert.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_convert.c,v 1.3 2023/06/23 10:50:47 tb Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.4 2024/04/09 14:56:21 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -26,7 +26,6 @@ | |||
26 | * - BN_bn2binpad() | 26 | * - BN_bn2binpad() |
27 | * - BN_bn2lebinpad() | 27 | * - BN_bn2lebinpad() |
28 | * - BN_lebin2bn() | 28 | * - BN_lebin2bn() |
29 | * - BN_bn2mpi()/BN_mpi2bn() | ||
30 | * - BN_print()/BN_print_fp() | 29 | * - BN_print()/BN_print_fp() |
31 | * | 30 | * |
32 | * - Invalid inputs to {asc,dec,hex,mpi}2bn | 31 | * - Invalid inputs to {asc,dec,hex,mpi}2bn |
@@ -238,6 +237,8 @@ struct bn_convert_test { | |||
238 | int neg; | 237 | int neg; |
239 | const char *dec; | 238 | const char *dec; |
240 | const char *hex; | 239 | const char *hex; |
240 | const uint8_t mpi[64]; | ||
241 | int mpi_len; | ||
241 | }; | 242 | }; |
242 | 243 | ||
243 | static const struct bn_convert_test bn_convert_tests[] = { | 244 | static const struct bn_convert_test bn_convert_tests[] = { |
@@ -247,6 +248,8 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
247 | .neg = 0, | 248 | .neg = 0, |
248 | .dec = "0", | 249 | .dec = "0", |
249 | .hex = "0", | 250 | .hex = "0", |
251 | .mpi = { 0x00, 0x00, 0x00, 0x00, }, | ||
252 | .mpi_len = 4, | ||
250 | }, | 253 | }, |
251 | { | 254 | { |
252 | .bin = { 0x1, }, | 255 | .bin = { 0x1, }, |
@@ -254,6 +257,17 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
254 | .neg = 0, | 257 | .neg = 0, |
255 | .dec = "1", | 258 | .dec = "1", |
256 | .hex = "01", | 259 | .hex = "01", |
260 | .mpi = { 0x00, 0x00, 0x00, 0x01, 0x01, }, | ||
261 | .mpi_len = 5, | ||
262 | }, | ||
263 | { | ||
264 | .bin = { 0x1, }, | ||
265 | .bin_len = 1, | ||
266 | .neg = 1, | ||
267 | .dec = "-1", | ||
268 | .hex = "-01", | ||
269 | .mpi = { 0x00, 0x00, 0x00, 0x01, 0x81, }, | ||
270 | .mpi_len = 5, | ||
257 | }, | 271 | }, |
258 | { | 272 | { |
259 | .bin = { 0x7f, 0xff, 0xff, }, | 273 | .bin = { 0x7f, 0xff, 0xff, }, |
@@ -261,6 +275,8 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
261 | .neg = 0, | 275 | .neg = 0, |
262 | .dec = "8388607", | 276 | .dec = "8388607", |
263 | .hex = "7FFFFF", | 277 | .hex = "7FFFFF", |
278 | .mpi = { 0x00, 0x00, 0x00, 0x03, 0x7f, 0xff, 0xff }, | ||
279 | .mpi_len = 7, | ||
264 | }, | 280 | }, |
265 | { | 281 | { |
266 | .bin = { 0x7f, 0xff, 0xff, }, | 282 | .bin = { 0x7f, 0xff, 0xff, }, |
@@ -268,6 +284,8 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
268 | .neg = 1, | 284 | .neg = 1, |
269 | .dec = "-8388607", | 285 | .dec = "-8388607", |
270 | .hex = "-7FFFFF", | 286 | .hex = "-7FFFFF", |
287 | .mpi = { 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff }, | ||
288 | .mpi_len = 7, | ||
271 | }, | 289 | }, |
272 | { | 290 | { |
273 | .bin = { 0xff, 0xff, 0xff, 0xff, }, | 291 | .bin = { 0xff, 0xff, 0xff, 0xff, }, |
@@ -275,6 +293,11 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
275 | .neg = 0, | 293 | .neg = 0, |
276 | .dec = "4294967295", | 294 | .dec = "4294967295", |
277 | .hex = "FFFFFFFF", | 295 | .hex = "FFFFFFFF", |
296 | .mpi = { | ||
297 | 0x00, 0x00, 0x00, 0x05, 0x00, 0xff, 0xff, 0xff, | ||
298 | 0xff, | ||
299 | }, | ||
300 | .mpi_len = 9, | ||
278 | }, | 301 | }, |
279 | { | 302 | { |
280 | .bin = { 0xff, 0xff, 0xff, 0xff, }, | 303 | .bin = { 0xff, 0xff, 0xff, 0xff, }, |
@@ -282,6 +305,11 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
282 | .neg = 1, | 305 | .neg = 1, |
283 | .dec = "-4294967295", | 306 | .dec = "-4294967295", |
284 | .hex = "-FFFFFFFF", | 307 | .hex = "-FFFFFFFF", |
308 | .mpi = { | ||
309 | 0x00, 0x00, 0x00, 0x05, 0x80, 0xff, 0xff, 0xff, | ||
310 | 0xff, | ||
311 | }, | ||
312 | .mpi_len = 9, | ||
285 | }, | 313 | }, |
286 | { | 314 | { |
287 | .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, | 315 | .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, |
@@ -289,6 +317,11 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
289 | .neg = 0, | 317 | .neg = 0, |
290 | .dec = "18446744069414584320", | 318 | .dec = "18446744069414584320", |
291 | .hex = "FFFFFFFF00000000", | 319 | .hex = "FFFFFFFF00000000", |
320 | .mpi = { | ||
321 | 0x00, 0x00, 0x00, 0x09, 0x00, 0xff, 0xff, 0xff, | ||
322 | 0xff, 0x00, 0x00, 0x00, 0x00, | ||
323 | }, | ||
324 | .mpi_len = 13, | ||
292 | }, | 325 | }, |
293 | { | 326 | { |
294 | .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, | 327 | .bin = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }, |
@@ -296,6 +329,11 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
296 | .neg = 1, | 329 | .neg = 1, |
297 | .dec = "-18446744069414584320", | 330 | .dec = "-18446744069414584320", |
298 | .hex = "-FFFFFFFF00000000", | 331 | .hex = "-FFFFFFFF00000000", |
332 | .mpi = { | ||
333 | 0x00, 0x00, 0x00, 0x09, 0x80, 0xff, 0xff, 0xff, | ||
334 | 0xff, 0x00, 0x00, 0x00, 0x00, | ||
335 | }, | ||
336 | .mpi_len = 13, | ||
299 | }, | 337 | }, |
300 | { | 338 | { |
301 | .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, | 339 | .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, |
@@ -303,6 +341,11 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
303 | .neg = 0, | 341 | .neg = 0, |
304 | .dec = "9223794255762391041", | 342 | .dec = "9223794255762391041", |
305 | .hex = "8001800180018001", | 343 | .hex = "8001800180018001", |
344 | .mpi = { | ||
345 | 0x00, 0x00, 0x00, 0x09, 0x00, 0x80, 0x01, 0x80, | ||
346 | 0x01, 0x80, 0x01, 0x80, 0x01, | ||
347 | }, | ||
348 | .mpi_len = 13, | ||
306 | }, | 349 | }, |
307 | { | 350 | { |
308 | .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, | 351 | .bin = { 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, |
@@ -310,20 +353,41 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
310 | .neg = 1, | 353 | .neg = 1, |
311 | .dec = "-9223794255762391041", | 354 | .dec = "-9223794255762391041", |
312 | .hex = "-8001800180018001", | 355 | .hex = "-8001800180018001", |
356 | .mpi = { | ||
357 | 0x00, 0x00, 0x00, 0x09, 0x80, 0x80, 0x01, 0x80, | ||
358 | 0x01, 0x80, 0x01, 0x80, 0x01, | ||
359 | }, | ||
360 | .mpi_len = 13, | ||
313 | }, | 361 | }, |
314 | { | 362 | { |
315 | .bin = { 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, | 363 | .bin = { |
364 | 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, | ||
365 | 0x01, | ||
366 | }, | ||
316 | .bin_len = 9, | 367 | .bin_len = 9, |
317 | .neg = 0, | 368 | .neg = 0, |
318 | .dec = "27670538329471942657", | 369 | .dec = "27670538329471942657", |
319 | .hex = "018001800180018001", | 370 | .hex = "018001800180018001", |
371 | .mpi = { | ||
372 | 0x00, 0x00, 0x00, 0x09, 0x01, 0x80, 0x01, 0x80, | ||
373 | 0x01, 0x80, 0x01, 0x80, 0x01, | ||
374 | }, | ||
375 | .mpi_len = 13, | ||
320 | }, | 376 | }, |
321 | { | 377 | { |
322 | .bin = { 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, }, | 378 | .bin = { |
379 | 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, | ||
380 | 0x01, | ||
381 | }, | ||
323 | .bin_len = 9, | 382 | .bin_len = 9, |
324 | .neg = 1, | 383 | .neg = 1, |
325 | .dec = "-27670538329471942657", | 384 | .dec = "-27670538329471942657", |
326 | .hex = "-018001800180018001", | 385 | .hex = "-018001800180018001", |
386 | .mpi = { | ||
387 | 0x00, 0x00, 0x00, 0x09, 0x81, 0x80, 0x01, 0x80, | ||
388 | 0x01, 0x80, 0x01, 0x80, 0x01, | ||
389 | }, | ||
390 | .mpi_len = 13, | ||
327 | }, | 391 | }, |
328 | { | 392 | { |
329 | .bin = { | 393 | .bin = { |
@@ -336,6 +400,14 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
336 | .neg = 0, | 400 | .neg = 0, |
337 | .dec = "57895161181645529494837117048595051142566530671229791132691030063130991362047", | 401 | .dec = "57895161181645529494837117048595051142566530671229791132691030063130991362047", |
338 | .hex = "7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", | 402 | .hex = "7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", |
403 | .mpi = { | ||
404 | 0x00, 0x00, 0x00, 0x20, 0x7f, 0xff, 0x7f, 0xff, | ||
405 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
406 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
407 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
408 | 0x7f, 0xff, 0x7f, 0xff, | ||
409 | }, | ||
410 | .mpi_len = 36, | ||
339 | }, | 411 | }, |
340 | { | 412 | { |
341 | .bin = { | 413 | .bin = { |
@@ -348,6 +420,14 @@ static const struct bn_convert_test bn_convert_tests[] = { | |||
348 | .neg = 1, | 420 | .neg = 1, |
349 | .dec = "-57895161181645529494837117048595051142566530671229791132691030063130991362047", | 421 | .dec = "-57895161181645529494837117048595051142566530671229791132691030063130991362047", |
350 | .hex = "-7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", | 422 | .hex = "-7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF", |
423 | .mpi = { | ||
424 | 0x00, 0x00, 0x00, 0x20, 0xff, 0xff, 0x7f, 0xff, | ||
425 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
426 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
427 | 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, | ||
428 | 0x7f, 0xff, 0x7f, 0xff, | ||
429 | }, | ||
430 | .mpi_len = 36, | ||
351 | }, | 431 | }, |
352 | }; | 432 | }; |
353 | 433 | ||
@@ -358,8 +438,10 @@ static int | |||
358 | test_bn_convert(void) | 438 | test_bn_convert(void) |
359 | { | 439 | { |
360 | const struct bn_convert_test *bct; | 440 | const struct bn_convert_test *bct; |
441 | uint8_t *mpi_out = NULL; | ||
361 | char *out_str = NULL; | 442 | char *out_str = NULL; |
362 | BIGNUM *bn = NULL; | 443 | BIGNUM *bn = NULL; |
444 | int mpi_len; | ||
363 | size_t i; | 445 | size_t i; |
364 | int failed = 1; | 446 | int failed = 1; |
365 | 447 | ||
@@ -399,6 +481,29 @@ test_bn_convert(void) | |||
399 | goto failure; | 481 | goto failure; |
400 | } | 482 | } |
401 | 483 | ||
484 | free(mpi_out); | ||
485 | |||
486 | if ((mpi_len = BN_bn2mpi(bn, NULL)) != bct->mpi_len) { | ||
487 | fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() returned " | ||
488 | "%d, want %d", i, mpi_len, bct->mpi_len); | ||
489 | goto failure; | ||
490 | } | ||
491 | if ((mpi_out = calloc(1, bct->mpi_len)) == NULL) | ||
492 | goto failure; | ||
493 | if ((mpi_len = BN_bn2mpi(bn, mpi_out)) != bct->mpi_len) { | ||
494 | fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() returned " | ||
495 | "%d, want %d", i, mpi_len, bct->mpi_len); | ||
496 | goto failure; | ||
497 | } | ||
498 | if (memcmp(mpi_out, bct->mpi, bct->mpi_len) != 0) { | ||
499 | fprintf(stderr, "FAIL: Test %zu - BN_bn2mpi() " | ||
500 | "generated:\n", i); | ||
501 | hexdump(mpi_out, bct->mpi_len); | ||
502 | fprintf(stderr, "Want:\n"); | ||
503 | hexdump(bct->mpi, bct->mpi_len); | ||
504 | goto failure; | ||
505 | } | ||
506 | |||
402 | if (BN_dec2bn(&bn, bct->dec) != (int)strlen(bct->dec)) { | 507 | if (BN_dec2bn(&bn, bct->dec) != (int)strlen(bct->dec)) { |
403 | fprintf(stderr, "FAIL: BN_dec2bn() failed\n"); | 508 | fprintf(stderr, "FAIL: BN_dec2bn() failed\n"); |
404 | goto failure; | 509 | goto failure; |
@@ -409,7 +514,6 @@ test_bn_convert(void) | |||
409 | bct->neg); | 514 | bct->neg); |
410 | goto failure; | 515 | goto failure; |
411 | } | 516 | } |
412 | |||
413 | if (check_bin_output(i, "BN_dec2bn()", bct->bin, bct->bin_len, | 517 | if (check_bin_output(i, "BN_dec2bn()", bct->bin, bct->bin_len, |
414 | bn) != 0) | 518 | bn) != 0) |
415 | goto failure; | 519 | goto failure; |
@@ -424,10 +528,23 @@ test_bn_convert(void) | |||
424 | bct->neg); | 528 | bct->neg); |
425 | goto failure; | 529 | goto failure; |
426 | } | 530 | } |
427 | |||
428 | if (check_bin_output(i, "BN_hex2bn()", bct->bin, bct->bin_len, | 531 | if (check_bin_output(i, "BN_hex2bn()", bct->bin, bct->bin_len, |
429 | bn) != 0) | 532 | bn) != 0) |
430 | goto failure; | 533 | goto failure; |
534 | |||
535 | if (BN_mpi2bn(bct->mpi, bct->mpi_len, bn) == NULL) { | ||
536 | fprintf(stderr, "FAIL: BN_mpi2bn() failed\n"); | ||
537 | goto failure; | ||
538 | } | ||
539 | if (BN_is_negative(bn) != bct->neg) { | ||
540 | fprintf(stderr, "FAIL: Test %zu - BN_mpi2bn() resulted " | ||
541 | "in negative %d, want %d", i, BN_is_negative(bn), | ||
542 | bct->neg); | ||
543 | goto failure; | ||
544 | } | ||
545 | if (check_bin_output(i, "BN_mpi2bn()", bct->bin, bct->bin_len, | ||
546 | bn) != 0) | ||
547 | goto failure; | ||
431 | } | 548 | } |
432 | 549 | ||
433 | failed = 0; | 550 | failed = 0; |