2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/ec.h>
15 #include <openssl/bn.h>
17 #ifndef OPENSSL_NO_STDIO
18 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
23 if ((b = BIO_new(BIO_s_file())) == NULL) {
24 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
27 BIO_set_fp(b, fp, BIO_NOCLOSE);
28 ret = ECPKParameters_print(b, x, off);
33 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
38 if ((b = BIO_new(BIO_s_file())) == NULL) {
39 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
42 BIO_set_fp(b, fp, BIO_NOCLOSE);
43 ret = EC_KEY_print(b, x, off);
48 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
53 if ((b = BIO_new(BIO_s_file())) == NULL) {
54 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
57 BIO_set_fp(b, fp, BIO_NOCLOSE);
58 ret = ECParameters_print(b, x);
64 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
67 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
69 int ret = 0, reason = ERR_R_BIO_LIB;
71 const EC_POINT *point = NULL;
72 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
73 const BIGNUM *order = NULL, *cofactor = NULL;
74 const unsigned char *seed;
77 static const char *gen_compressed = "Generator (compressed):";
78 static const char *gen_uncompressed = "Generator (uncompressed):";
79 static const char *gen_hybrid = "Generator (hybrid):";
82 reason = ERR_R_PASSED_NULL_PARAMETER;
88 reason = ERR_R_MALLOC_FAILURE;
92 if (EC_GROUP_get_asn1_flag(x)) {
93 /* the curve parameter are given by an asn1 OID */
97 if (!BIO_indent(bp, off, 128))
100 nid = EC_GROUP_get_curve_name(x);
103 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
105 if (BIO_printf(bp, "\n") <= 0)
107 nname = EC_curve_nid2nist(nid);
109 if (!BIO_indent(bp, off, 128))
111 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
115 /* explicit parameters */
117 point_conversion_form_t form;
118 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
120 if (tmp_nid == NID_X9_62_characteristic_two_field)
123 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
124 (b = BN_new()) == NULL) {
125 reason = ERR_R_MALLOC_FAILURE;
128 #ifndef OPENSSL_NO_EC2M
130 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
131 reason = ERR_R_EC_LIB;
134 } else /* prime field */
137 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
138 reason = ERR_R_EC_LIB;
143 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
144 reason = ERR_R_EC_LIB;
147 order = EC_GROUP_get0_order(x);
148 cofactor = EC_GROUP_get0_cofactor(x);
150 reason = ERR_R_EC_LIB;
154 form = EC_GROUP_get_point_conversion_form(x);
156 if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
157 reason = ERR_R_EC_LIB;
161 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
162 seed_len = EC_GROUP_get_seed_len(x);
164 if (!BIO_indent(bp, off, 128))
167 /* print the 'short name' of the field type */
168 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
173 /* print the 'short name' of the base type OID */
174 int basis_type = EC_GROUP_get_basis_type(x);
178 if (!BIO_indent(bp, off, 128))
181 if (BIO_printf(bp, "Basis Type: %s\n",
182 OBJ_nid2sn(basis_type)) <= 0)
185 /* print the polynomial */
186 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
190 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
193 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
195 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
197 if (form == POINT_CONVERSION_COMPRESSED) {
198 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
201 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
202 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
205 } else { /* form == POINT_CONVERSION_HYBRID */
207 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
211 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
214 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
217 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
223 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
232 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
236 char str[128 + 1 + 4];
243 memset(str, ' ', off);
244 if (BIO_write(fp, str, off) <= 0)
250 if (BIO_printf(fp, "%s", name) <= 0)
253 for (i = 0; i < len; i++) {
256 memset(&(str[1]), ' ', off + 4);
257 if (BIO_write(fp, str, off + 1 + 4) <= 0)
260 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
264 if (BIO_write(fp, "\n", 1) <= 0)