]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/eck_prn.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / ec / eck_prn.c
1 /*
2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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.
14 */
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/evp.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21
22 #ifndef OPENSSL_NO_STDIO
23 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
24 {
25 BIO *b;
26 int ret;
27
28 if ((b = BIO_new(BIO_s_file())) == NULL) {
29 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
30 return (0);
31 }
32 BIO_set_fp(b, fp, BIO_NOCLOSE);
33 ret = ECPKParameters_print(b, x, off);
34 BIO_free(b);
35 return (ret);
36 }
37
38 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
39 {
40 BIO *b;
41 int ret;
42
43 if ((b = BIO_new(BIO_s_file())) == NULL) {
44 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
45 return (0);
46 }
47 BIO_set_fp(b, fp, BIO_NOCLOSE);
48 ret = EC_KEY_print(b, x, off);
49 BIO_free(b);
50 return (ret);
51 }
52
53 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
54 {
55 BIO *b;
56 int ret;
57
58 if ((b = BIO_new(BIO_s_file())) == NULL) {
59 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
60 return (0);
61 }
62 BIO_set_fp(b, fp, BIO_NOCLOSE);
63 ret = ECParameters_print(b, x);
64 BIO_free(b);
65 return (ret);
66 }
67 #endif
68
69 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
70 {
71 EVP_PKEY *pk;
72 int ret;
73 pk = EVP_PKEY_new();
74 if (pk == NULL || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
75 return 0;
76 ret = EVP_PKEY_print_private(bp, pk, off, NULL);
77 EVP_PKEY_free(pk);
78 return ret;
79 }
80
81 int ECParameters_print(BIO *bp, const EC_KEY *x)
82 {
83 EVP_PKEY *pk;
84 int ret;
85 pk = EVP_PKEY_new();
86 if (pk == NULL || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
87 return 0;
88 ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
89 EVP_PKEY_free(pk);
90 return ret;
91 }
92
93 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
94 size_t len, int off);
95
96 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
97 {
98 int ret = 0, reason = ERR_R_BIO_LIB;
99 BN_CTX *ctx = NULL;
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;
104 size_t seed_len = 0;
105
106 static const char *gen_compressed = "Generator (compressed):";
107 static const char *gen_uncompressed = "Generator (uncompressed):";
108 static const char *gen_hybrid = "Generator (hybrid):";
109
110 if (!x) {
111 reason = ERR_R_PASSED_NULL_PARAMETER;
112 goto err;
113 }
114
115 ctx = BN_CTX_new();
116 if (ctx == NULL) {
117 reason = ERR_R_MALLOC_FAILURE;
118 goto err;
119 }
120
121 if (EC_GROUP_get_asn1_flag(x)) {
122 /* the curve parameter are given by an asn1 OID */
123 int nid;
124 const char *nname;
125
126 if (!BIO_indent(bp, off, 128))
127 goto err;
128
129 nid = EC_GROUP_get_curve_name(x);
130 if (nid == 0)
131 goto err;
132 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
133 goto err;
134 if (BIO_printf(bp, "\n") <= 0)
135 goto err;
136 nname = EC_curve_nid2nist(nid);
137 if (nname) {
138 if (!BIO_indent(bp, off, 128))
139 goto err;
140 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
141 goto err;
142 }
143 } else {
144 /* explicit parameters */
145 int is_char_two = 0;
146 point_conversion_form_t form;
147 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
148
149 if (tmp_nid == NID_X9_62_characteristic_two_field)
150 is_char_two = 1;
151
152 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
153 (b = BN_new()) == NULL) {
154 reason = ERR_R_MALLOC_FAILURE;
155 goto err;
156 }
157 #ifndef OPENSSL_NO_EC2M
158 if (is_char_two) {
159 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) {
160 reason = ERR_R_EC_LIB;
161 goto err;
162 }
163 } else /* prime field */
164 #endif
165 {
166 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) {
167 reason = ERR_R_EC_LIB;
168 goto err;
169 }
170 }
171
172 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
173 reason = ERR_R_EC_LIB;
174 goto err;
175 }
176 order = EC_GROUP_get0_order(x);
177 cofactor = EC_GROUP_get0_cofactor(x);
178 if (order == NULL) {
179 reason = ERR_R_EC_LIB;
180 goto err;
181 }
182
183 form = EC_GROUP_get_point_conversion_form(x);
184
185 if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
186 reason = ERR_R_EC_LIB;
187 goto err;
188 }
189
190 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
191 seed_len = EC_GROUP_get_seed_len(x);
192
193 if (!BIO_indent(bp, off, 128))
194 goto err;
195
196 /* print the 'short name' of the field type */
197 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
198 <= 0)
199 goto err;
200
201 if (is_char_two) {
202 /* print the 'short name' of the base type OID */
203 int basis_type = EC_GROUP_get_basis_type(x);
204 if (basis_type == 0)
205 goto err;
206
207 if (!BIO_indent(bp, off, 128))
208 goto err;
209
210 if (BIO_printf(bp, "Basis Type: %s\n",
211 OBJ_nid2sn(basis_type)) <= 0)
212 goto err;
213
214 /* print the polynomial */
215 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
216 off))
217 goto err;
218 } else {
219 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
220 goto err;
221 }
222 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
223 goto err;
224 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
225 goto err;
226 if (form == POINT_CONVERSION_COMPRESSED) {
227 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
228 NULL, off))
229 goto err;
230 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
231 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
232 NULL, off))
233 goto err;
234 } else { /* form == POINT_CONVERSION_HYBRID */
235
236 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
237 NULL, off))
238 goto err;
239 }
240 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
241 NULL, off))
242 goto err;
243 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
244 NULL, off))
245 goto err;
246 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
247 goto err;
248 }
249 ret = 1;
250 err:
251 if (!ret)
252 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
253 BN_free(p);
254 BN_free(a);
255 BN_free(b);
256 BN_free(gen);
257 BN_CTX_free(ctx);
258 return (ret);
259 }
260
261 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
262 size_t len, int off)
263 {
264 size_t i;
265 char str[128];
266
267 if (buf == NULL)
268 return 1;
269 if (off > 0) {
270 if (off > 128)
271 off = 128;
272 memset(str, ' ', off);
273 if (BIO_write(fp, str, off) <= 0)
274 return 0;
275 } else {
276 off = 0;
277 }
278
279 if (BIO_printf(fp, "%s", name) <= 0)
280 return 0;
281
282 for (i = 0; i < len; i++) {
283 if ((i % 15) == 0) {
284 str[0] = '\n';
285 memset(&(str[1]), ' ', off + 4);
286 if (BIO_write(fp, str, off + 1 + 4) <= 0)
287 return 0;
288 }
289 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
290 0)
291 return 0;
292 }
293 if (BIO_write(fp, "\n", 1) <= 0)
294 return 0;
295
296 return 1;
297 }