1 /* crypto/ec/eck_prn.c */
3 * Written by Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
66 #include <openssl/evp.h>
67 #include <openssl/ec.h>
68 #include <openssl/bn.h>
70 #ifndef OPENSSL_NO_FP_API
71 int ECPKParameters_print_fp(FILE *fp
, const EC_GROUP
*x
, int off
)
76 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
77 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP
, ERR_R_BUF_LIB
);
80 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
81 ret
= ECPKParameters_print(b
, x
, off
);
86 int EC_KEY_print_fp(FILE *fp
, const EC_KEY
*x
, int off
)
91 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
92 ECerr(EC_F_EC_KEY_PRINT_FP
, ERR_R_BIO_LIB
);
95 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
96 ret
= EC_KEY_print(b
, x
, off
);
101 int ECParameters_print_fp(FILE *fp
, const EC_KEY
*x
)
106 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
107 ECerr(EC_F_ECPARAMETERS_PRINT_FP
, ERR_R_BIO_LIB
);
110 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
111 ret
= ECParameters_print(b
, x
);
117 int EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
)
122 if (!pk
|| !EVP_PKEY_set1_EC_KEY(pk
, (EC_KEY
*)x
))
124 ret
= EVP_PKEY_print_private(bp
, pk
, off
, NULL
);
129 int ECParameters_print(BIO
*bp
, const EC_KEY
*x
)
134 if (!pk
|| !EVP_PKEY_set1_EC_KEY(pk
, (EC_KEY
*)x
))
136 ret
= EVP_PKEY_print_params(bp
, pk
, 4, NULL
);
141 static int print_bin(BIO
*fp
, const char *str
, const unsigned char *num
,
142 size_t len
, int off
);
144 int ECPKParameters_print(BIO
*bp
, const EC_GROUP
*x
, int off
)
146 unsigned char *buffer
= NULL
;
147 size_t buf_len
= 0, i
;
148 int ret
= 0, reason
= ERR_R_BIO_LIB
;
150 const EC_POINT
*point
= NULL
;
151 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *gen
= NULL
,
152 *order
= NULL
, *cofactor
= NULL
;
153 const unsigned char *seed
;
156 static const char *gen_compressed
= "Generator (compressed):";
157 static const char *gen_uncompressed
= "Generator (uncompressed):";
158 static const char *gen_hybrid
= "Generator (hybrid):";
161 reason
= ERR_R_PASSED_NULL_PARAMETER
;
167 reason
= ERR_R_MALLOC_FAILURE
;
171 if (EC_GROUP_get_asn1_flag(x
)) {
172 /* the curve parameter are given by an asn1 OID */
175 if (!BIO_indent(bp
, off
, 128))
178 nid
= EC_GROUP_get_curve_name(x
);
182 if (BIO_printf(bp
, "ASN1 OID: %s", OBJ_nid2sn(nid
)) <= 0)
184 if (BIO_printf(bp
, "\n") <= 0)
187 /* explicit parameters */
189 point_conversion_form_t form
;
190 int tmp_nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(x
));
192 if (tmp_nid
== NID_X9_62_characteristic_two_field
)
195 if ((p
= BN_new()) == NULL
|| (a
= BN_new()) == NULL
||
196 (b
= BN_new()) == NULL
|| (order
= BN_new()) == NULL
||
197 (cofactor
= BN_new()) == NULL
) {
198 reason
= ERR_R_MALLOC_FAILURE
;
201 #ifndef OPENSSL_NO_EC2M
203 if (!EC_GROUP_get_curve_GF2m(x
, p
, a
, b
, ctx
)) {
204 reason
= ERR_R_EC_LIB
;
207 } else /* prime field */
210 if (!EC_GROUP_get_curve_GFp(x
, p
, a
, b
, ctx
)) {
211 reason
= ERR_R_EC_LIB
;
216 if ((point
= EC_GROUP_get0_generator(x
)) == NULL
) {
217 reason
= ERR_R_EC_LIB
;
220 if (!EC_GROUP_get_order(x
, order
, NULL
) ||
221 !EC_GROUP_get_cofactor(x
, cofactor
, NULL
)) {
222 reason
= ERR_R_EC_LIB
;
226 form
= EC_GROUP_get_point_conversion_form(x
);
228 if ((gen
= EC_POINT_point2bn(x
, point
, form
, NULL
, ctx
)) == NULL
) {
229 reason
= ERR_R_EC_LIB
;
233 buf_len
= (size_t)BN_num_bytes(p
);
234 if (buf_len
< (i
= (size_t)BN_num_bytes(a
)))
236 if (buf_len
< (i
= (size_t)BN_num_bytes(b
)))
238 if (buf_len
< (i
= (size_t)BN_num_bytes(gen
)))
240 if (buf_len
< (i
= (size_t)BN_num_bytes(order
)))
242 if (buf_len
< (i
= (size_t)BN_num_bytes(cofactor
)))
245 if ((seed
= EC_GROUP_get0_seed(x
)) != NULL
)
246 seed_len
= EC_GROUP_get_seed_len(x
);
249 if ((buffer
= OPENSSL_malloc(buf_len
)) == NULL
) {
250 reason
= ERR_R_MALLOC_FAILURE
;
254 if (!BIO_indent(bp
, off
, 128))
257 /* print the 'short name' of the field type */
258 if (BIO_printf(bp
, "Field Type: %s\n", OBJ_nid2sn(tmp_nid
))
263 /* print the 'short name' of the base type OID */
264 int basis_type
= EC_GROUP_get_basis_type(x
);
268 if (!BIO_indent(bp
, off
, 128))
271 if (BIO_printf(bp
, "Basis Type: %s\n",
272 OBJ_nid2sn(basis_type
)) <= 0)
275 /* print the polynomial */
276 if ((p
!= NULL
) && !ASN1_bn_print(bp
, "Polynomial:", p
, buffer
,
280 if ((p
!= NULL
) && !ASN1_bn_print(bp
, "Prime:", p
, buffer
, off
))
283 if ((a
!= NULL
) && !ASN1_bn_print(bp
, "A: ", a
, buffer
, off
))
285 if ((b
!= NULL
) && !ASN1_bn_print(bp
, "B: ", b
, buffer
, off
))
287 if (form
== POINT_CONVERSION_COMPRESSED
) {
288 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_compressed
, gen
,
291 } else if (form
== POINT_CONVERSION_UNCOMPRESSED
) {
292 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_uncompressed
, gen
,
295 } else { /* form == POINT_CONVERSION_HYBRID */
297 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_hybrid
, gen
,
301 if ((order
!= NULL
) && !ASN1_bn_print(bp
, "Order: ", order
,
304 if ((cofactor
!= NULL
) && !ASN1_bn_print(bp
, "Cofactor: ", cofactor
,
307 if (seed
&& !print_bin(bp
, "Seed:", seed
, seed_len
, off
))
313 ECerr(EC_F_ECPKPARAMETERS_PRINT
, reason
);
329 OPENSSL_free(buffer
);
333 static int print_bin(BIO
*fp
, const char *name
, const unsigned char *buf
,
344 memset(str
, ' ', off
);
345 if (BIO_write(fp
, str
, off
) <= 0)
349 if (BIO_printf(fp
, "%s", name
) <= 0)
352 for (i
= 0; i
< len
; i
++) {
355 memset(&(str
[1]), ' ', off
+ 4);
356 if (BIO_write(fp
, str
, off
+ 1 + 4) <= 0)
359 if (BIO_printf(fp
, "%02x%s", buf
[i
], ((i
+ 1) == len
) ? "" : ":") <=
363 if (BIO_write(fp
, "\n", 1) <= 0)