diff options
author | tb <> | 2023-07-06 14:37:39 +0000 |
---|---|---|
committer | tb <> | 2023-07-06 14:37:39 +0000 |
commit | 54e886eb34c014c4079f3af241bb29e0b99acf18 (patch) | |
tree | c644feed15832ce7ae4e2e4dd8a32e1f974ef087 /src/lib | |
parent | ed8e6cf151ae66733328ddd6b063a277b89f2a52 (diff) | |
download | openbsd-54e886eb34c014c4079f3af241bb29e0b99acf18.tar.gz openbsd-54e886eb34c014c4079f3af241bb29e0b99acf18.tar.bz2 openbsd-54e886eb34c014c4079f3af241bb29e0b99acf18.zip |
Add bn_printf(), a replacement for ASN1_bn_print()
ASN1_bn_print() will be removed in an upcoming bump. This adds an internal
API that covers the same functionality but doesn't require that the caller
pass in a sufficiently large scratch space that ASN1_bn_print() may or may
not use. In addition, this takes a format string, which allows us to ditch
some extra dances.
ok jsing
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/Makefile | 3 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_local.h | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_print.c | 147 |
3 files changed, 154 insertions, 2 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile index 6e41dae162..f40ef656b7 100644 --- a/src/lib/libcrypto/Makefile +++ b/src/lib/libcrypto/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.135 2023/07/05 12:31:14 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.136 2023/07/06 14:37:39 tb Exp $ |
2 | 2 | ||
3 | LIB= crypto | 3 | LIB= crypto |
4 | LIBREBUILD=y | 4 | LIBREBUILD=y |
@@ -195,6 +195,7 @@ SRCS+= bn_mont.c | |||
195 | SRCS+= bn_mul.c | 195 | SRCS+= bn_mul.c |
196 | SRCS+= bn_prime.c | 196 | SRCS+= bn_prime.c |
197 | SRCS+= bn_primitives.c | 197 | SRCS+= bn_primitives.c |
198 | SRCS+= bn_print.c | ||
198 | SRCS+= bn_rand.c | 199 | SRCS+= bn_rand.c |
199 | SRCS+= bn_recp.c | 200 | SRCS+= bn_recp.c |
200 | SRCS+= bn_shift.c | 201 | SRCS+= bn_shift.c |
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 17f5447bec..86aa972275 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.24 2023/06/24 16:01:43 jsing Exp $ */ | 1 | /* $OpenBSD: bn_local.h,v 1.25 2023/07/06 14:37:39 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 | * |
@@ -323,5 +323,9 @@ int bn_is_perfect_square(int *out_perfect, const BIGNUM *n, BN_CTX *ctx); | |||
323 | 323 | ||
324 | int bn_is_prime_bpsw(int *is_prime, const BIGNUM *n, BN_CTX *ctx, size_t rounds); | 324 | int bn_is_prime_bpsw(int *is_prime, const BIGNUM *n, BN_CTX *ctx, size_t rounds); |
325 | 325 | ||
326 | int bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) | ||
327 | __attribute__((__format__ (printf, 4, 5))) | ||
328 | __attribute__((__nonnull__ (4))); | ||
329 | |||
326 | __END_HIDDEN_DECLS | 330 | __END_HIDDEN_DECLS |
327 | #endif /* !HEADER_BN_LOCAL_H */ | 331 | #endif /* !HEADER_BN_LOCAL_H */ |
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c new file mode 100644 index 0000000000..466aeb3d64 --- /dev/null +++ b/src/lib/libcrypto/bn/bn_print.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /* $OpenBSD: bn_print.c,v 1.40 2023/07/06 14:37:39 tb Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <ctype.h> | ||
20 | #include <limits.h> | ||
21 | #include <stdarg.h> | ||
22 | #include <stdint.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | #include <openssl/bio.h> | ||
27 | #include <openssl/bn.h> | ||
28 | |||
29 | #include "bytestring.h" | ||
30 | |||
31 | static int | ||
32 | bn_print_zero(BIO *bio, const BIGNUM *bn) | ||
33 | { | ||
34 | if (!BN_is_zero(bn)) | ||
35 | return 0; | ||
36 | if (BIO_printf(bio, " 0\n") <= 0) | ||
37 | return 0; | ||
38 | return 1; | ||
39 | } | ||
40 | |||
41 | static int | ||
42 | bn_print_word(BIO *bio, const BIGNUM *bn) | ||
43 | { | ||
44 | BN_ULONG word; | ||
45 | const char *neg = ""; | ||
46 | |||
47 | if (BN_is_zero(bn) || BN_num_bytes(bn) > BN_BYTES) | ||
48 | return 0; | ||
49 | |||
50 | if (BN_is_negative(bn)) | ||
51 | neg = "-"; | ||
52 | |||
53 | word = BN_get_word(bn); | ||
54 | if (BIO_printf(bio, " %s%lu (%s0x%lx)\n", neg, word, neg, word) <= 0) | ||
55 | return 0; | ||
56 | |||
57 | return 1; | ||
58 | } | ||
59 | |||
60 | static int | ||
61 | bn_print_bignum(BIO *bio, const BIGNUM *bn, int indent) | ||
62 | { | ||
63 | CBS cbs; | ||
64 | char *hex = NULL; | ||
65 | size_t hex_len = 0; | ||
66 | size_t octets = 0; | ||
67 | uint8_t hi, lo; | ||
68 | const char *sep = ":"; | ||
69 | int ret = 0; | ||
70 | |||
71 | if (BN_num_bytes(bn) <= BN_BYTES) | ||
72 | goto err; | ||
73 | |||
74 | /* Secondary indent is 4 spaces, capped at 128. */ | ||
75 | if (indent > INT_MAX - 4) | ||
76 | goto err; | ||
77 | indent += 4; | ||
78 | if (indent > 128) | ||
79 | indent = 128; | ||
80 | if (indent < 0) | ||
81 | indent = 0; | ||
82 | |||
83 | if ((hex = BN_bn2hex(bn)) == NULL) | ||
84 | goto err; | ||
85 | hex_len = strlen(hex); | ||
86 | |||
87 | CBS_init(&cbs, hex, hex_len); | ||
88 | |||
89 | if (BN_is_negative(bn)) { | ||
90 | if (BIO_printf(bio, " (Negative)") <= 0) | ||
91 | goto err; | ||
92 | if (!CBS_skip(&cbs, 1)) | ||
93 | goto err; | ||
94 | } | ||
95 | |||
96 | while (CBS_len(&cbs) > 0) { | ||
97 | if (!CBS_get_u8(&cbs, &hi)) | ||
98 | goto err; | ||
99 | if (!CBS_get_u8(&cbs, &lo)) | ||
100 | goto err; | ||
101 | if (octets++ % 15 == 0) { | ||
102 | if (BIO_printf(bio, "\n%*s", indent, "") <= 0) | ||
103 | goto err; | ||
104 | } | ||
105 | if (CBS_len(&cbs) == 0) | ||
106 | sep = ""; | ||
107 | if (BIO_printf(bio, "%c%c%s", tolower(hi), tolower(lo), sep) <= 0) | ||
108 | goto err; | ||
109 | } | ||
110 | |||
111 | if (BIO_printf(bio, "\n") <= 0) | ||
112 | goto err; | ||
113 | |||
114 | ret = 1; | ||
115 | |||
116 | err: | ||
117 | freezero(hex, hex_len); | ||
118 | |||
119 | return ret; | ||
120 | } | ||
121 | |||
122 | int | ||
123 | bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) | ||
124 | { | ||
125 | va_list ap; | ||
126 | int rv; | ||
127 | |||
128 | if (bn == NULL) | ||
129 | return 1; | ||
130 | |||
131 | if (!BIO_indent(bio, indent, 128)) | ||
132 | return 0; | ||
133 | |||
134 | va_start(ap, fmt); | ||
135 | rv = BIO_vprintf(bio, fmt, ap); | ||
136 | va_end(ap); | ||
137 | if (rv < 0) | ||
138 | return 0; | ||
139 | |||
140 | if (BN_is_zero(bn)) | ||
141 | return bn_print_zero(bio, bn); | ||
142 | |||
143 | if (BN_num_bytes(bn) <= BN_BYTES) | ||
144 | return bn_print_word(bio, bn); | ||
145 | |||
146 | return bn_print_bignum(bio, bn, indent); | ||
147 | } | ||