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 int ret = 0, reason = ERR_R_BIO_LIB;
147 const EC_POINT *point = NULL;
148 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
149 const BIGNUM *order = NULL, *cofactor = NULL;
150 const unsigned char *seed;
153 static const char *gen_compressed = "Generator (compressed):";
154 static const char *gen_uncompressed = "Generator (uncompressed):";
155 static const char *gen_hybrid = "Generator (hybrid):";
158 reason = ERR_R_PASSED_NULL_PARAMETER;
164 reason = ERR_R_MALLOC_FAILURE;
168 if (EC_GROUP_get_asn1_flag(x)) {
169 /* the curve parameter are given by an asn1 OID */
173 if (!BIO_indent(bp, off, 128))
176 nid = EC_GROUP_get_curve_name(x);
179 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
181 if (BIO_printf(bp, "\n") <= 0)
183 nname = EC_curve_nid2nist(nid);
185 if (!BIO_indent(bp, off, 128))
187 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
191 /* explicit parameters */
193 point_conversion_form_t form;
194 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
196 if (tmp_nid == NID_X9_62_characteristic_two_field)
199 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
200 (b = BN_new()) == NULL) {
201 reason = ERR_R_MALLOC_FAILURE;
204 #ifndef OPENSSL_NO_EC2M
206 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
207 reason = ERR_R_EC_LIB;
210 } else /* prime field */
213 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
214 reason = ERR_R_EC_LIB;
219 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
220 reason = ERR_R_EC_LIB;
223 order = EC_GROUP_get0_order(x);
224 cofactor = EC_GROUP_get0_cofactor(x);
226 reason = ERR_R_EC_LIB;
230 form = EC_GROUP_get_point_conversion_form(x);
232 if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
233 reason = ERR_R_EC_LIB;
237 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
238 seed_len = EC_GROUP_get_seed_len(x);
240 if (!BIO_indent(bp, off, 128))
243 /* print the 'short name' of the field type */
244 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
249 /* print the 'short name' of the base type OID */
250 int basis_type = EC_GROUP_get_basis_type(x);
254 if (!BIO_indent(bp, off, 128))
257 if (BIO_printf(bp, "Basis Type: %s\n",
258 OBJ_nid2sn(basis_type)) <= 0)
261 /* print the polynomial */
262 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
266 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
269 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
271 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
273 if (form == POINT_CONVERSION_COMPRESSED) {
274 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
277 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
278 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
281 } else { /* form == POINT_CONVERSION_HYBRID */
283 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
287 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
290 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
293 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
299 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
308 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
319 memset(str, ' ', off);
320 if (BIO_write(fp, str, off) <= 0)
326 if (BIO_printf(fp, "%s", name) <= 0)
329 for (i = 0; i < len; i++) {
332 memset(&(str[1]), ' ', off + 4);
333 if (BIO_write(fp, str, off + 1 + 4) <= 0)
336 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
340 if (BIO_write(fp, "\n", 1) <= 0)