1 /* $OpenBSD: eck_prn.c,v 1.31 2024/10/22 12:06:08 tb Exp $ */
2 /*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
5 /* ====================================================================
6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Portions originally developed by SUN MICROSYSTEMS, INC., and
61 * contributed to the OpenSSL project.
62 */
63
64 #include <stdio.h>
65 #include <string.h>
66
67 #include <openssl/bn.h>
68 #include <openssl/ec.h>
69 #include <openssl/err.h>
70 #include <openssl/evp.h>
71
72 #include "ec_local.h"
73
74 int
ECPKParameters_print_fp(FILE * fp,const EC_GROUP * x,int off)75 ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
76 {
77 BIO *b;
78 int ret;
79
80 if ((b = BIO_new(BIO_s_file())) == NULL) {
81 ECerror(ERR_R_BUF_LIB);
82 return (0);
83 }
84 BIO_set_fp(b, fp, BIO_NOCLOSE);
85 ret = ECPKParameters_print(b, x, off);
86 BIO_free(b);
87 return (ret);
88 }
89 LCRYPTO_ALIAS(ECPKParameters_print_fp);
90
91 int
EC_KEY_print_fp(FILE * fp,const EC_KEY * x,int off)92 EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
93 {
94 BIO *b;
95 int ret;
96
97 if ((b = BIO_new(BIO_s_file())) == NULL) {
98 ECerror(ERR_R_BIO_LIB);
99 return (0);
100 }
101 BIO_set_fp(b, fp, BIO_NOCLOSE);
102 ret = EC_KEY_print(b, x, off);
103 BIO_free(b);
104 return (ret);
105 }
106 LCRYPTO_ALIAS(EC_KEY_print_fp);
107
108 int
ECParameters_print_fp(FILE * fp,const EC_KEY * x)109 ECParameters_print_fp(FILE *fp, const EC_KEY *x)
110 {
111 BIO *b;
112 int ret;
113
114 if ((b = BIO_new(BIO_s_file())) == NULL) {
115 ECerror(ERR_R_BIO_LIB);
116 return (0);
117 }
118 BIO_set_fp(b, fp, BIO_NOCLOSE);
119 ret = ECParameters_print(b, x);
120 BIO_free(b);
121 return (ret);
122 }
123 LCRYPTO_ALIAS(ECParameters_print_fp);
124
125 int
EC_KEY_print(BIO * bp,const EC_KEY * x,int off)126 EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
127 {
128 EVP_PKEY *pk;
129 int ret = 0;
130
131 if ((pk = EVP_PKEY_new()) == NULL)
132 goto err;
133
134 if (!EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
135 goto err;
136
137 ret = EVP_PKEY_print_private(bp, pk, off, NULL);
138 err:
139 EVP_PKEY_free(pk);
140 return ret;
141 }
142 LCRYPTO_ALIAS(EC_KEY_print);
143
144 int
ECParameters_print(BIO * bp,const EC_KEY * x)145 ECParameters_print(BIO *bp, const EC_KEY *x)
146 {
147 EVP_PKEY *pk;
148 int ret = 0;
149
150 if ((pk = EVP_PKEY_new()) == NULL)
151 goto err;
152
153 if (!EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
154 goto err;
155
156 ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
157 err:
158 EVP_PKEY_free(pk);
159 return ret;
160 }
161 LCRYPTO_ALIAS(ECParameters_print);
162
163 static int
ecpk_print_asn1_parameters(BIO * bp,const EC_GROUP * group,int off)164 ecpk_print_asn1_parameters(BIO *bp, const EC_GROUP *group, int off)
165 {
166 const char *nist_name;
167 int nid;
168 int ret = 0;
169
170 if (!BIO_indent(bp, off, 128)) {
171 ECerror(ERR_R_BIO_LIB);
172 goto err;
173 }
174
175 if ((nid = EC_GROUP_get_curve_name(group)) == NID_undef) {
176 ECerror(ERR_R_INTERNAL_ERROR);
177 goto err;
178 }
179
180 if (BIO_printf(bp, "ASN1 OID: %s\n", OBJ_nid2sn(nid)) <= 0) {
181 ECerror(ERR_R_BIO_LIB);
182 goto err;
183 }
184
185 if ((nist_name = EC_curve_nid2nist(nid)) != NULL) {
186 if (!BIO_indent(bp, off, 128)) {
187 ECerror(ERR_R_BIO_LIB);
188 goto err;
189 }
190 if (BIO_printf(bp, "NIST CURVE: %s\n", nist_name) <= 0) {
191 ECerror(ERR_R_BIO_LIB);
192 goto err;
193 }
194 }
195
196 ret = 1;
197 err:
198
199 return ret;
200 }
201
202 static int
ecpk_print_explicit_parameters(BIO * bp,const EC_GROUP * group,int off)203 ecpk_print_explicit_parameters(BIO *bp, const EC_GROUP *group, int off)
204 {
205 BN_CTX *ctx = NULL;
206 const BIGNUM *order;
207 BIGNUM *p, *a, *b, *cofactor;
208 BIGNUM *gen = NULL;
209 const EC_POINT *generator;
210 const char *conversion_form;
211 const unsigned char *seed;
212 size_t seed_len;
213 point_conversion_form_t form;
214 int nid;
215 int ret = 0;
216
217 if ((ctx = BN_CTX_new()) == NULL) {
218 ECerror(ERR_R_MALLOC_FAILURE);
219 goto err;
220 }
221
222 BN_CTX_start(ctx);
223
224 if ((p = BN_CTX_get(ctx)) == NULL)
225 goto err;
226 if ((a = BN_CTX_get(ctx)) == NULL)
227 goto err;
228 if ((b = BN_CTX_get(ctx)) == NULL)
229 goto err;
230 if ((cofactor = BN_CTX_get(ctx)) == NULL)
231 goto err;
232 if ((gen = BN_CTX_get(ctx)) == NULL)
233 goto err;
234
235 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
236 ECerror(ERR_R_EC_LIB);
237 goto err;
238 }
239 if ((order = EC_GROUP_get0_order(group)) == NULL) {
240 ECerror(ERR_R_EC_LIB);
241 goto err;
242 }
243 if (!EC_GROUP_get_cofactor(group, cofactor, NULL)) {
244 ECerror(ERR_R_EC_LIB);
245 goto err;
246 }
247
248 if ((generator = EC_GROUP_get0_generator(group)) == NULL) {
249 ECerror(ERR_R_EC_LIB);
250 goto err;
251 }
252 form = EC_GROUP_get_point_conversion_form(group);
253 if (EC_POINT_point2bn(group, generator, form, gen, ctx) == NULL) {
254 ECerror(ERR_R_EC_LIB);
255 goto err;
256 }
257
258 if (!BIO_indent(bp, off, 128))
259 goto err;
260
261 nid = ec_group_get_field_type(group);
262 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(nid)) <= 0)
263 goto err;
264
265 if (!bn_printf(bp, p, off, "Prime:"))
266 goto err;
267 if (!bn_printf(bp, a, off, "A: "))
268 goto err;
269 if (!bn_printf(bp, b, off, "B: "))
270 goto err;
271
272 if (form == POINT_CONVERSION_COMPRESSED)
273 conversion_form = "compressed";
274 else if (form == POINT_CONVERSION_UNCOMPRESSED)
275 conversion_form = "uncompressed";
276 else if (form == POINT_CONVERSION_HYBRID)
277 conversion_form = "hybrid";
278 else
279 conversion_form = "unknown";
280 if (!bn_printf(bp, gen, off, "Generator (%s):", conversion_form))
281 goto err;
282
283 if (!bn_printf(bp, order, off, "Order: "))
284 goto err;
285 if (!bn_printf(bp, cofactor, off, "Cofactor: "))
286 goto err;
287 if ((seed = EC_GROUP_get0_seed(group)) != NULL) {
288 size_t i;
289
290 seed_len = EC_GROUP_get_seed_len(group);
291
292 /* XXX - ecx_buf_print() has a CBS version of this - dedup. */
293 if (!BIO_indent(bp, off, 128))
294 goto err;
295 if (BIO_printf(bp, "Seed:") <= 0)
296 goto err;
297
298 for (i = 0; i < seed_len; i++) {
299 const char *sep = ":";
300
301 if (i % 15 == 0) {
302 if (BIO_printf(bp, "\n") <= 0)
303 goto err;
304 if (!BIO_indent(bp, off + 4, 128))
305 goto err;
306 }
307
308 if (i + 1 == seed_len)
309 sep = "";
310 if (BIO_printf(bp, "%02x%s", seed[i], sep) <= 0)
311 goto err;
312 }
313
314 if (BIO_printf(bp, "\n") <= 0)
315 goto err;
316 }
317
318 ret = 1;
319 err:
320 BN_CTX_end(ctx);
321 BN_CTX_free(ctx);
322
323 return ret;
324 }
325
326 int
ECPKParameters_print(BIO * bp,const EC_GROUP * group,int off)327 ECPKParameters_print(BIO *bp, const EC_GROUP *group, int off)
328 {
329 if (group == NULL) {
330 ECerror(ERR_R_PASSED_NULL_PARAMETER);
331 return 0;
332 }
333
334 if (EC_GROUP_get_asn1_flag(group))
335 return ecpk_print_asn1_parameters(bp, group, off);
336
337 return ecpk_print_explicit_parameters(bp, group, off);
338 }
339 LCRYPTO_ALIAS(ECPKParameters_print);
340