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