diff options
| author | tb <> | 2023-07-09 18:27:22 +0000 |
|---|---|---|
| committer | tb <> | 2023-07-09 18:27:22 +0000 |
| commit | 4f03cd23b792effd358f8c1b24b75a754656bb43 (patch) | |
| tree | d4399f324fc45f4ddfac4af42e9f6627e51e993e /src | |
| parent | 6503fbfd862518a9088d61b700240ef33e4e3996 (diff) | |
| download | openbsd-4f03cd23b792effd358f8c1b24b75a754656bb43.tar.gz openbsd-4f03cd23b792effd358f8c1b24b75a754656bb43.tar.bz2 openbsd-4f03cd23b792effd358f8c1b24b75a754656bb43.zip | |
Refactor BN_bn2hex()
Various outputting functions are variants of BN_bn2hex(). They do not
want a sign or they display the BIGNUM at nibble granularity instead
of byte granularity. So add this functionality to an internal variant
of BN_bn2hex().
with/ok jsing
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_convert.c | 60 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_local.h | 5 |
2 files changed, 57 insertions, 8 deletions
diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c index cb0a23e010..788e90cc8d 100644 --- a/src/lib/libcrypto/bn/bn_convert.c +++ b/src/lib/libcrypto/bn/bn_convert.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_convert.c,v 1.13 2023/07/08 12:21:58 beck Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.14 2023/07/09 18:27:22 tb Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -497,20 +497,27 @@ BN_dec2bn(BIGNUM **bnp, const char *s) | |||
| 497 | } | 497 | } |
| 498 | LCRYPTO_ALIAS(BN_dec2bn); | 498 | LCRYPTO_ALIAS(BN_dec2bn); |
| 499 | 499 | ||
| 500 | char * | 500 | static int |
| 501 | BN_bn2hex(const BIGNUM *bn) | 501 | bn_bn2hex_internal(const BIGNUM *bn, int include_sign, int nibbles_only, |
| 502 | char **out, size_t *out_len) | ||
| 502 | { | 503 | { |
| 503 | int started = 0; | 504 | int started = 0; |
| 504 | uint8_t *s = NULL; | 505 | uint8_t *s = NULL; |
| 505 | size_t s_len; | 506 | size_t s_len = 0; |
| 506 | BN_ULONG v, w; | 507 | BN_ULONG v, w; |
| 507 | int i, j; | 508 | int i, j; |
| 508 | CBB cbb; | 509 | CBB cbb; |
| 510 | CBS cbs; | ||
| 511 | uint8_t nul; | ||
| 512 | int ret = 0; | ||
| 513 | |||
| 514 | *out = NULL; | ||
| 515 | *out_len = 0; | ||
| 509 | 516 | ||
| 510 | if (!CBB_init(&cbb, 0)) | 517 | if (!CBB_init(&cbb, 0)) |
| 511 | goto err; | 518 | goto err; |
| 512 | 519 | ||
| 513 | if (BN_is_negative(bn)) { | 520 | if (BN_is_negative(bn) && include_sign) { |
| 514 | if (!CBB_add_u8(&cbb, '-')) | 521 | if (!CBB_add_u8(&cbb, '-')) |
| 515 | goto err; | 522 | goto err; |
| 516 | } | 523 | } |
| @@ -524,8 +531,10 @@ BN_bn2hex(const BIGNUM *bn) | |||
| 524 | v = (w >> j) & 0xff; | 531 | v = (w >> j) & 0xff; |
| 525 | if (!started && v == 0) | 532 | if (!started && v == 0) |
| 526 | continue; | 533 | continue; |
| 527 | if (!CBB_add_u8(&cbb, hex_digits[v >> 4])) | 534 | if (started || !nibbles_only || (v >> 4) != 0) { |
| 528 | goto err; | 535 | if (!CBB_add_u8(&cbb, hex_digits[v >> 4])) |
| 536 | goto err; | ||
| 537 | } | ||
| 529 | if (!CBB_add_u8(&cbb, hex_digits[v & 0xf])) | 538 | if (!CBB_add_u8(&cbb, hex_digits[v & 0xf])) |
| 530 | goto err; | 539 | goto err; |
| 531 | started = 1; | 540 | started = 1; |
| @@ -536,8 +545,45 @@ BN_bn2hex(const BIGNUM *bn) | |||
| 536 | if (!CBB_finish(&cbb, &s, &s_len)) | 545 | if (!CBB_finish(&cbb, &s, &s_len)) |
| 537 | goto err; | 546 | goto err; |
| 538 | 547 | ||
| 548 | /* The length of a C string does not include the terminating NUL. */ | ||
| 549 | CBS_init(&cbs, s, s_len); | ||
| 550 | if (!CBS_get_last_u8(&cbs, &nul)) | ||
| 551 | goto err; | ||
| 552 | |||
| 553 | *out = (char *)CBS_data(&cbs); | ||
| 554 | *out_len = CBS_len(&cbs); | ||
| 555 | s = NULL; | ||
| 556 | s_len = 0; | ||
| 557 | |||
| 558 | ret = 1; | ||
| 559 | |||
| 539 | err: | 560 | err: |
| 540 | CBB_cleanup(&cbb); | 561 | CBB_cleanup(&cbb); |
| 562 | freezero(s, s_len); | ||
| 563 | |||
| 564 | return ret; | ||
| 565 | } | ||
| 566 | |||
| 567 | int | ||
| 568 | bn_bn2hex_nosign(const BIGNUM *bn, char **out, size_t *out_len) | ||
| 569 | { | ||
| 570 | return bn_bn2hex_internal(bn, 0, 0, out, out_len); | ||
| 571 | } | ||
| 572 | |||
| 573 | int | ||
| 574 | bn_bn2hex_nibbles(const BIGNUM *bn, char **out, size_t *out_len) | ||
| 575 | { | ||
| 576 | return bn_bn2hex_internal(bn, 1, 1, out, out_len); | ||
| 577 | } | ||
| 578 | |||
| 579 | char * | ||
| 580 | BN_bn2hex(const BIGNUM *bn) | ||
| 581 | { | ||
| 582 | char *s; | ||
| 583 | size_t s_len; | ||
| 584 | |||
| 585 | if (!bn_bn2hex_internal(bn, 1, 0, &s, &s_len)) | ||
| 586 | return NULL; | ||
| 541 | 587 | ||
| 542 | return s; | 588 | return s; |
| 543 | } | 589 | } |
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 86aa972275..a8d40fbcc8 100644 --- a/src/lib/libcrypto/bn/bn_local.h +++ b/src/lib/libcrypto/bn/bn_local.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_local.h,v 1.25 2023/07/06 14:37:39 tb Exp $ */ | 1 | /* $OpenBSD: bn_local.h,v 1.26 2023/07/09 18:27:22 tb Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -327,5 +327,8 @@ int bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) | |||
| 327 | __attribute__((__format__ (printf, 4, 5))) | 327 | __attribute__((__format__ (printf, 4, 5))) |
| 328 | __attribute__((__nonnull__ (4))); | 328 | __attribute__((__nonnull__ (4))); |
| 329 | 329 | ||
| 330 | int bn_bn2hex_nosign(const BIGNUM *bn, char **out, size_t *out_len); | ||
| 331 | int bn_bn2hex_nibbles(const BIGNUM *bn, char **out, size_t *out_len); | ||
| 332 | |||
| 330 | __END_HIDDEN_DECLS | 333 | __END_HIDDEN_DECLS |
| 331 | #endif /* !HEADER_BN_LOCAL_H */ | 334 | #endif /* !HEADER_BN_LOCAL_H */ |
