2 * Written by Nils Larsch for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
64 #include "internal/cryptlib.h"
65 #include <openssl/evp.h>
66 #include <openssl/ec.h>
67 #include <openssl/bn.h>
69 #ifndef OPENSSL_NO_STDIO
70 int ECPKParameters_print_fp(FILE *fp
, const EC_GROUP
*x
, int off
)
75 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
76 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP
, ERR_R_BUF_LIB
);
79 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
80 ret
= ECPKParameters_print(b
, x
, off
);
85 int EC_KEY_print_fp(FILE *fp
, const EC_KEY
*x
, int off
)
90 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
91 ECerr(EC_F_EC_KEY_PRINT_FP
, ERR_R_BIO_LIB
);
94 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
95 ret
= EC_KEY_print(b
, x
, off
);
100 int ECParameters_print_fp(FILE *fp
, const EC_KEY
*x
)
105 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
106 ECerr(EC_F_ECPARAMETERS_PRINT_FP
, ERR_R_BIO_LIB
);
109 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
110 ret
= ECParameters_print(b
, x
);
116 int EC_KEY_print(BIO
*bp
, const EC_KEY
*x
, int off
)
121 if (pk
== NULL
|| !EVP_PKEY_set1_EC_KEY(pk
, (EC_KEY
*)x
))
123 ret
= EVP_PKEY_print_private(bp
, pk
, off
, NULL
);
128 int ECParameters_print(BIO
*bp
, const EC_KEY
*x
)
133 if (pk
== NULL
|| !EVP_PKEY_set1_EC_KEY(pk
, (EC_KEY
*)x
))
135 ret
= EVP_PKEY_print_params(bp
, pk
, 4, NULL
);
140 static int print_bin(BIO
*fp
, const char *str
, const unsigned char *num
,
141 size_t len
, int off
);
143 int ECPKParameters_print(BIO
*bp
, const EC_GROUP
*x
, int off
)
145 unsigned char *buffer
= NULL
;
146 size_t buf_len
= 0, i
;
147 int ret
= 0, reason
= ERR_R_BIO_LIB
;
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
;
155 static const char *gen_compressed
= "Generator (compressed):";
156 static const char *gen_uncompressed
= "Generator (uncompressed):";
157 static const char *gen_hybrid
= "Generator (hybrid):";
160 reason
= ERR_R_PASSED_NULL_PARAMETER
;
166 reason
= ERR_R_MALLOC_FAILURE
;
170 if (EC_GROUP_get_asn1_flag(x
)) {
171 /* the curve parameter are given by an asn1 OID */
175 if (!BIO_indent(bp
, off
, 128))
178 nid
= EC_GROUP_get_curve_name(x
);
181 if (BIO_printf(bp
, "ASN1 OID: %s", OBJ_nid2sn(nid
)) <= 0)
183 if (BIO_printf(bp
, "\n") <= 0)
185 nname
= EC_curve_nid2nist(nid
);
187 if (!BIO_indent(bp
, off
, 128))
189 if (BIO_printf(bp
, "NIST CURVE: %s\n", nname
) <= 0)
193 /* explicit parameters */
195 point_conversion_form_t form
;
196 int tmp_nid
= EC_METHOD_get_field_type(EC_GROUP_method_of(x
));
198 if (tmp_nid
== NID_X9_62_characteristic_two_field
)
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
;
207 #ifndef OPENSSL_NO_EC2M
209 if (!EC_GROUP_get_curve_GF2m(x
, p
, a
, b
, ctx
)) {
210 reason
= ERR_R_EC_LIB
;
213 } else /* prime field */
216 if (!EC_GROUP_get_curve_GFp(x
, p
, a
, b
, ctx
)) {
217 reason
= ERR_R_EC_LIB
;
222 if ((point
= EC_GROUP_get0_generator(x
)) == NULL
) {
223 reason
= ERR_R_EC_LIB
;
226 if (!EC_GROUP_get_order(x
, order
, NULL
) ||
227 !EC_GROUP_get_cofactor(x
, cofactor
, NULL
)) {
228 reason
= ERR_R_EC_LIB
;
232 form
= EC_GROUP_get_point_conversion_form(x
);
234 if ((gen
= EC_POINT_point2bn(x
, point
, form
, NULL
, ctx
)) == NULL
) {
235 reason
= ERR_R_EC_LIB
;
239 buf_len
= (size_t)BN_num_bytes(p
);
240 if (buf_len
< (i
= (size_t)BN_num_bytes(a
)))
242 if (buf_len
< (i
= (size_t)BN_num_bytes(b
)))
244 if (buf_len
< (i
= (size_t)BN_num_bytes(gen
)))
246 if (buf_len
< (i
= (size_t)BN_num_bytes(order
)))
248 if (buf_len
< (i
= (size_t)BN_num_bytes(cofactor
)))
251 if ((seed
= EC_GROUP_get0_seed(x
)) != NULL
)
252 seed_len
= EC_GROUP_get_seed_len(x
);
255 if ((buffer
= OPENSSL_malloc(buf_len
)) == NULL
) {
256 reason
= ERR_R_MALLOC_FAILURE
;
260 if (!BIO_indent(bp
, off
, 128))
263 /* print the 'short name' of the field type */
264 if (BIO_printf(bp
, "Field Type: %s\n", OBJ_nid2sn(tmp_nid
))
269 /* print the 'short name' of the base type OID */
270 int basis_type
= EC_GROUP_get_basis_type(x
);
274 if (!BIO_indent(bp
, off
, 128))
277 if (BIO_printf(bp
, "Basis Type: %s\n",
278 OBJ_nid2sn(basis_type
)) <= 0)
281 /* print the polynomial */
282 if ((p
!= NULL
) && !ASN1_bn_print(bp
, "Polynomial:", p
, buffer
,
286 if ((p
!= NULL
) && !ASN1_bn_print(bp
, "Prime:", p
, buffer
, off
))
289 if ((a
!= NULL
) && !ASN1_bn_print(bp
, "A: ", a
, buffer
, off
))
291 if ((b
!= NULL
) && !ASN1_bn_print(bp
, "B: ", b
, buffer
, off
))
293 if (form
== POINT_CONVERSION_COMPRESSED
) {
294 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_compressed
, gen
,
297 } else if (form
== POINT_CONVERSION_UNCOMPRESSED
) {
298 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_uncompressed
, gen
,
301 } else { /* form == POINT_CONVERSION_HYBRID */
303 if ((gen
!= NULL
) && !ASN1_bn_print(bp
, gen_hybrid
, gen
,
307 if ((order
!= NULL
) && !ASN1_bn_print(bp
, "Order: ", order
,
310 if ((cofactor
!= NULL
) && !ASN1_bn_print(bp
, "Cofactor: ", cofactor
,
313 if (seed
&& !print_bin(bp
, "Seed:", seed
, seed_len
, off
))
319 ECerr(EC_F_ECPKPARAMETERS_PRINT
, reason
);
327 OPENSSL_free(buffer
);
331 static int print_bin(BIO
*fp
, const char *name
, const unsigned char *buf
,
342 memset(str
, ' ', off
);
343 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)