]>
Commit | Line | Data |
---|---|---|
1b593194 | 1 | /* crypto/ec/eck_prn.c */ |
a4e75b3d DSH |
2 | /* |
3 | * Written by Nils Larsch for the OpenSSL project. | |
4 | */ | |
5 | /* ==================================================================== | |
6 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. | |
1b593194 | 7 | * |
1b593194 DSH |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
a4e75b3d DSH |
11 | * |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
a4e75b3d | 14 | * |
1b593194 | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
a4e75b3d DSH |
16 | * notice, this list of conditions and the following disclaimer in |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * openssl-core@openssl.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
1b593194 DSH |
57 | */ |
58 | /* ==================================================================== | |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 60 | * Portions originally developed by SUN MICROSYSTEMS, INC., and |
a4e75b3d | 61 | * contributed to the OpenSSL project. |
1b593194 DSH |
62 | */ |
63 | ||
64 | #include <stdio.h> | |
b39fc560 | 65 | #include "internal/cryptlib.h" |
1b593194 DSH |
66 | #include <openssl/evp.h> |
67 | #include <openssl/ec.h> | |
1e26a8ba | 68 | #include <openssl/bn.h> |
1b593194 | 69 | |
4b618848 | 70 | #ifndef OPENSSL_NO_STDIO |
1b593194 | 71 | int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) |
0f113f3e MC |
72 | { |
73 | BIO *b; | |
74 | int ret; | |
75 | ||
76 | if ((b = BIO_new(BIO_s_file())) == NULL) { | |
77 | ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB); | |
78 | return (0); | |
79 | } | |
80 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
81 | ret = ECPKParameters_print(b, x, off); | |
82 | BIO_free(b); | |
83 | return (ret); | |
84 | } | |
1b593194 DSH |
85 | |
86 | int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) | |
0f113f3e MC |
87 | { |
88 | BIO *b; | |
89 | int ret; | |
90 | ||
91 | if ((b = BIO_new(BIO_s_file())) == NULL) { | |
92 | ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); | |
93 | return (0); | |
94 | } | |
95 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
96 | ret = EC_KEY_print(b, x, off); | |
97 | BIO_free(b); | |
98 | return (ret); | |
99 | } | |
1b593194 DSH |
100 | |
101 | int ECParameters_print_fp(FILE *fp, const EC_KEY *x) | |
0f113f3e MC |
102 | { |
103 | BIO *b; | |
104 | int ret; | |
105 | ||
106 | if ((b = BIO_new(BIO_s_file())) == NULL) { | |
107 | ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); | |
108 | return (0); | |
109 | } | |
110 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
111 | ret = ECParameters_print(b, x); | |
112 | BIO_free(b); | |
113 | return (ret); | |
114 | } | |
1b593194 DSH |
115 | #endif |
116 | ||
117 | int EC_KEY_print(BIO *bp, const EC_KEY *x, int off) | |
0f113f3e MC |
118 | { |
119 | EVP_PKEY *pk; | |
120 | int ret; | |
121 | pk = EVP_PKEY_new(); | |
122 | if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) | |
123 | return 0; | |
124 | ret = EVP_PKEY_print_private(bp, pk, off, NULL); | |
125 | EVP_PKEY_free(pk); | |
126 | return ret; | |
127 | } | |
1b593194 DSH |
128 | |
129 | int ECParameters_print(BIO *bp, const EC_KEY *x) | |
0f113f3e MC |
130 | { |
131 | EVP_PKEY *pk; | |
132 | int ret; | |
133 | pk = EVP_PKEY_new(); | |
134 | if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) | |
135 | return 0; | |
136 | ret = EVP_PKEY_print_params(bp, pk, 4, NULL); | |
137 | EVP_PKEY_free(pk); | |
138 | return ret; | |
139 | } | |
1b593194 DSH |
140 | |
141 | static int print_bin(BIO *fp, const char *str, const unsigned char *num, | |
0f113f3e | 142 | size_t len, int off); |
1b593194 DSH |
143 | |
144 | int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) | |
0f113f3e MC |
145 | { |
146 | unsigned char *buffer = NULL; | |
147 | size_t buf_len = 0, i; | |
148 | int ret = 0, reason = ERR_R_BIO_LIB; | |
149 | BN_CTX *ctx = NULL; | |
150 | const EC_POINT *point = NULL; | |
151 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, | |
152 | *order = NULL, *cofactor = NULL; | |
153 | const unsigned char *seed; | |
154 | size_t seed_len = 0; | |
155 | ||
156 | static const char *gen_compressed = "Generator (compressed):"; | |
157 | static const char *gen_uncompressed = "Generator (uncompressed):"; | |
158 | static const char *gen_hybrid = "Generator (hybrid):"; | |
159 | ||
160 | if (!x) { | |
161 | reason = ERR_R_PASSED_NULL_PARAMETER; | |
162 | goto err; | |
163 | } | |
164 | ||
165 | ctx = BN_CTX_new(); | |
166 | if (ctx == NULL) { | |
167 | reason = ERR_R_MALLOC_FAILURE; | |
168 | goto err; | |
169 | } | |
170 | ||
171 | if (EC_GROUP_get_asn1_flag(x)) { | |
172 | /* the curve parameter are given by an asn1 OID */ | |
173 | int nid; | |
174 | const char *nname; | |
175 | ||
176 | if (!BIO_indent(bp, off, 128)) | |
177 | goto err; | |
178 | ||
179 | nid = EC_GROUP_get_curve_name(x); | |
180 | if (nid == 0) | |
181 | goto err; | |
182 | if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) | |
183 | goto err; | |
184 | if (BIO_printf(bp, "\n") <= 0) | |
185 | goto err; | |
186 | nname = EC_curve_nid2nist(nid); | |
187 | if (nname) { | |
188 | if (!BIO_indent(bp, off, 128)) | |
189 | goto err; | |
190 | if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0) | |
191 | goto err; | |
192 | } | |
193 | } else { | |
194 | /* explicit parameters */ | |
195 | int is_char_two = 0; | |
196 | point_conversion_form_t form; | |
197 | int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x)); | |
198 | ||
199 | if (tmp_nid == NID_X9_62_characteristic_two_field) | |
200 | is_char_two = 1; | |
201 | ||
202 | if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || | |
203 | (b = BN_new()) == NULL || (order = BN_new()) == NULL || | |
204 | (cofactor = BN_new()) == NULL) { | |
205 | reason = ERR_R_MALLOC_FAILURE; | |
206 | goto err; | |
207 | } | |
b3310161 | 208 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
209 | if (is_char_two) { |
210 | if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) { | |
211 | reason = ERR_R_EC_LIB; | |
212 | goto err; | |
213 | } | |
214 | } else /* prime field */ | |
b3310161 | 215 | #endif |
0f113f3e MC |
216 | { |
217 | if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) { | |
218 | reason = ERR_R_EC_LIB; | |
219 | goto err; | |
220 | } | |
221 | } | |
222 | ||
223 | if ((point = EC_GROUP_get0_generator(x)) == NULL) { | |
224 | reason = ERR_R_EC_LIB; | |
225 | goto err; | |
226 | } | |
227 | if (!EC_GROUP_get_order(x, order, NULL) || | |
228 | !EC_GROUP_get_cofactor(x, cofactor, NULL)) { | |
229 | reason = ERR_R_EC_LIB; | |
230 | goto err; | |
231 | } | |
232 | ||
233 | form = EC_GROUP_get_point_conversion_form(x); | |
234 | ||
235 | if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) { | |
236 | reason = ERR_R_EC_LIB; | |
237 | goto err; | |
238 | } | |
239 | ||
240 | buf_len = (size_t)BN_num_bytes(p); | |
241 | if (buf_len < (i = (size_t)BN_num_bytes(a))) | |
242 | buf_len = i; | |
243 | if (buf_len < (i = (size_t)BN_num_bytes(b))) | |
244 | buf_len = i; | |
245 | if (buf_len < (i = (size_t)BN_num_bytes(gen))) | |
246 | buf_len = i; | |
247 | if (buf_len < (i = (size_t)BN_num_bytes(order))) | |
248 | buf_len = i; | |
249 | if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) | |
250 | buf_len = i; | |
251 | ||
252 | if ((seed = EC_GROUP_get0_seed(x)) != NULL) | |
253 | seed_len = EC_GROUP_get_seed_len(x); | |
254 | ||
255 | buf_len += 10; | |
256 | if ((buffer = OPENSSL_malloc(buf_len)) == NULL) { | |
257 | reason = ERR_R_MALLOC_FAILURE; | |
258 | goto err; | |
259 | } | |
260 | ||
261 | if (!BIO_indent(bp, off, 128)) | |
262 | goto err; | |
263 | ||
264 | /* print the 'short name' of the field type */ | |
265 | if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) | |
266 | <= 0) | |
267 | goto err; | |
268 | ||
269 | if (is_char_two) { | |
270 | /* print the 'short name' of the base type OID */ | |
271 | int basis_type = EC_GROUP_get_basis_type(x); | |
272 | if (basis_type == 0) | |
273 | goto err; | |
274 | ||
275 | if (!BIO_indent(bp, off, 128)) | |
276 | goto err; | |
277 | ||
278 | if (BIO_printf(bp, "Basis Type: %s\n", | |
279 | OBJ_nid2sn(basis_type)) <= 0) | |
280 | goto err; | |
281 | ||
282 | /* print the polynomial */ | |
283 | if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer, | |
284 | off)) | |
285 | goto err; | |
286 | } else { | |
287 | if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off)) | |
288 | goto err; | |
289 | } | |
290 | if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off)) | |
291 | goto err; | |
292 | if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off)) | |
293 | goto err; | |
294 | if (form == POINT_CONVERSION_COMPRESSED) { | |
295 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, | |
296 | buffer, off)) | |
297 | goto err; | |
298 | } else if (form == POINT_CONVERSION_UNCOMPRESSED) { | |
299 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, | |
300 | buffer, off)) | |
301 | goto err; | |
302 | } else { /* form == POINT_CONVERSION_HYBRID */ | |
303 | ||
304 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, | |
305 | buffer, off)) | |
306 | goto err; | |
307 | } | |
308 | if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, | |
309 | buffer, off)) | |
310 | goto err; | |
311 | if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, | |
312 | buffer, off)) | |
313 | goto err; | |
314 | if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) | |
315 | goto err; | |
316 | } | |
317 | ret = 1; | |
318 | err: | |
319 | if (!ret) | |
320 | ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); | |
23a1d5e9 RS |
321 | BN_free(p); |
322 | BN_free(a); | |
323 | BN_free(b); | |
324 | BN_free(gen); | |
325 | BN_free(order); | |
326 | BN_free(cofactor); | |
327 | BN_CTX_free(ctx); | |
b548a1f1 | 328 | OPENSSL_free(buffer); |
0f113f3e MC |
329 | return (ret); |
330 | } | |
1b593194 DSH |
331 | |
332 | static int print_bin(BIO *fp, const char *name, const unsigned char *buf, | |
0f113f3e MC |
333 | size_t len, int off) |
334 | { | |
335 | size_t i; | |
336 | char str[128]; | |
337 | ||
338 | if (buf == NULL) | |
339 | return 1; | |
3deeeeb6 | 340 | if (off > 0) { |
0f113f3e MC |
341 | if (off > 128) |
342 | off = 128; | |
343 | memset(str, ' ', off); | |
344 | if (BIO_write(fp, str, off) <= 0) | |
345 | return 0; | |
3deeeeb6 MC |
346 | } else { |
347 | off = 0; | |
0f113f3e MC |
348 | } |
349 | ||
350 | if (BIO_printf(fp, "%s", name) <= 0) | |
351 | return 0; | |
352 | ||
353 | for (i = 0; i < len; i++) { | |
354 | if ((i % 15) == 0) { | |
355 | str[0] = '\n'; | |
356 | memset(&(str[1]), ' ', off + 4); | |
357 | if (BIO_write(fp, str, off + 1 + 4) <= 0) | |
358 | return 0; | |
359 | } | |
360 | if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= | |
361 | 0) | |
362 | return 0; | |
363 | } | |
364 | if (BIO_write(fp, "\n", 1) <= 0) | |
365 | return 0; | |
366 | ||
367 | return 1; | |
368 | } |