2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * Portions originally developed by SUN MICROSYSTEMS, INC., and
13 * contributed to the OpenSSL project.
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
22 #ifndef OPENSSL_NO_STDIO
23 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
28 if ((b = BIO_new(BIO_s_file())) == NULL) {
29 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
32 BIO_set_fp(b, fp, BIO_NOCLOSE);
33 ret = ECPKParameters_print(b, x, off);
38 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
43 if ((b = BIO_new(BIO_s_file())) == NULL) {
44 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
47 BIO_set_fp(b, fp, BIO_NOCLOSE);
48 ret = EC_KEY_print(b, x, off);
53 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
58 if ((b = BIO_new(BIO_s_file())) == NULL) {
59 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
62 BIO_set_fp(b, fp, BIO_NOCLOSE);
63 ret = ECParameters_print(b, x);
69 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
74 if (pk == NULL || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
76 ret = EVP_PKEY_print_private(bp, pk, off, NULL);
81 int ECParameters_print(BIO *bp, const EC_KEY *x)
86 if (pk == NULL || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
88 ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
93 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
96 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
98 int ret = 0, reason = ERR_R_BIO_LIB;
100 const EC_POINT *point = NULL;
101 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
102 const BIGNUM *order = NULL, *cofactor = NULL;
103 const unsigned char *seed;
106 static const char *gen_compressed = "Generator (compressed):";
107 static const char *gen_uncompressed = "Generator (uncompressed):";
108 static const char *gen_hybrid = "Generator (hybrid):";
111 reason = ERR_R_PASSED_NULL_PARAMETER;
117 reason = ERR_R_MALLOC_FAILURE;
121 if (EC_GROUP_get_asn1_flag(x)) {
122 /* the curve parameter are given by an asn1 OID */
126 if (!BIO_indent(bp, off, 128))
129 nid = EC_GROUP_get_curve_name(x);
132 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
134 if (BIO_printf(bp, "\n") <= 0)
136 nname = EC_curve_nid2nist(nid);
138 if (!BIO_indent(bp, off, 128))
140 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
144 /* explicit parameters */
146 point_conversion_form_t form;
147 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
149 if (tmp_nid == NID_X9_62_characteristic_two_field)
152 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
153 (b = BN_new()) == NULL) {
154 reason = ERR_R_MALLOC_FAILURE;
157 #ifndef OPENSSL_NO_EC2M
159 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
160 reason = ERR_R_EC_LIB;
163 } else /* prime field */
166 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
167 reason = ERR_R_EC_LIB;
172 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
173 reason = ERR_R_EC_LIB;
176 order = EC_GROUP_get0_order(x);
177 cofactor = EC_GROUP_get0_cofactor(x);
179 reason = ERR_R_EC_LIB;
183 form = EC_GROUP_get_point_conversion_form(x);
185 if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
186 reason = ERR_R_EC_LIB;
190 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
191 seed_len = EC_GROUP_get_seed_len(x);
193 if (!BIO_indent(bp, off, 128))
196 /* print the 'short name' of the field type */
197 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
202 /* print the 'short name' of the base type OID */
203 int basis_type = EC_GROUP_get_basis_type(x);
207 if (!BIO_indent(bp, off, 128))
210 if (BIO_printf(bp, "Basis Type: %s\n",
211 OBJ_nid2sn(basis_type)) <= 0)
214 /* print the polynomial */
215 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
219 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
222 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
224 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
226 if (form == POINT_CONVERSION_COMPRESSED) {
227 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
230 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
231 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
234 } else { /* form == POINT_CONVERSION_HYBRID */
236 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
240 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
243 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
246 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
252 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
261 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
272 memset(str, ' ', off);
273 if (BIO_write(fp, str, off) <= 0)
279 if (BIO_printf(fp, "%s", name) <= 0)
282 for (i = 0; i < len; i++) {
285 memset(&(str[1]), ' ', off + 4);
286 if (BIO_write(fp, str, off + 1 + 4) <= 0)
289 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
293 if (BIO_write(fp, "\n", 1) <= 0)