]>
Commit | Line | Data |
---|---|---|
1b593194 DSH |
1 | /* crypto/ec/eck_prn.c */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | |
3 | * All rights reserved. | |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
8 | * | |
9 | * This library is free for commercial and non-commercial use as long as | |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
15 | * | |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
22 | * | |
23 | * Redistribution and use in source and binary forms, with or without | |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
38 | * the apps directory (application code) you must include an acknowledgement: | |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
53 | * The licence and distribution terms for any publically available version or | |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
58 | /* ==================================================================== | |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
60 | * Binary polynomial ECC support in OpenSSL originally developed by | |
61 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. | |
62 | */ | |
63 | ||
64 | #include <stdio.h> | |
65 | #include "cryptlib.h" | |
66 | #include <openssl/evp.h> | |
67 | #include <openssl/ec.h> | |
68 | ||
69 | #ifndef OPENSSL_NO_FP_API | |
70 | int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) | |
71 | { | |
72 | BIO *b; | |
73 | int ret; | |
74 | ||
75 | if ((b=BIO_new(BIO_s_file())) == NULL) | |
76 | { | |
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 | } | |
85 | ||
86 | int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) | |
87 | { | |
88 | BIO *b; | |
89 | int ret; | |
90 | ||
91 | if ((b=BIO_new(BIO_s_file())) == NULL) | |
92 | { | |
93 | ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); | |
94 | return(0); | |
95 | } | |
96 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
97 | ret = EC_KEY_print(b, x, off); | |
98 | BIO_free(b); | |
99 | return(ret); | |
100 | } | |
101 | ||
102 | int ECParameters_print_fp(FILE *fp, const EC_KEY *x) | |
103 | { | |
104 | BIO *b; | |
105 | int ret; | |
106 | ||
107 | if ((b=BIO_new(BIO_s_file())) == NULL) | |
108 | { | |
109 | ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); | |
110 | return(0); | |
111 | } | |
112 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
113 | ret = ECParameters_print(b, x); | |
114 | BIO_free(b); | |
115 | return(ret); | |
116 | } | |
117 | #endif | |
118 | ||
119 | int EC_KEY_print(BIO *bp, const EC_KEY *x, int off) | |
120 | { | |
121 | EVP_PKEY *pk; | |
122 | int ret; | |
123 | pk = EVP_PKEY_new(); | |
124 | if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) | |
125 | return 0; | |
126 | ret = EVP_PKEY_print_private(bp, pk, off, NULL); | |
127 | EVP_PKEY_free(pk); | |
128 | return ret; | |
129 | } | |
130 | ||
131 | int ECParameters_print(BIO *bp, const EC_KEY *x) | |
132 | { | |
133 | EVP_PKEY *pk; | |
134 | int ret; | |
135 | pk = EVP_PKEY_new(); | |
136 | if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x)) | |
137 | return 0; | |
138 | ret = EVP_PKEY_print_params(bp, pk, 4, NULL); | |
139 | EVP_PKEY_free(pk); | |
140 | return ret; | |
141 | } | |
142 | ||
143 | static int print_bin(BIO *fp, const char *str, const unsigned char *num, | |
144 | size_t len, int off); | |
145 | ||
146 | int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) | |
147 | { | |
148 | unsigned char *buffer=NULL; | |
149 | size_t buf_len=0, i; | |
150 | int ret=0, reason=ERR_R_BIO_LIB; | |
151 | BN_CTX *ctx=NULL; | |
152 | const EC_POINT *point=NULL; | |
153 | BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL, | |
154 | *order=NULL, *cofactor=NULL; | |
155 | const unsigned char *seed; | |
156 | size_t seed_len=0; | |
157 | ||
158 | static const char *gen_compressed = "Generator (compressed):"; | |
159 | static const char *gen_uncompressed = "Generator (uncompressed):"; | |
160 | static const char *gen_hybrid = "Generator (hybrid):"; | |
161 | ||
162 | if (!x) | |
163 | { | |
164 | reason = ERR_R_PASSED_NULL_PARAMETER; | |
165 | goto err; | |
166 | } | |
167 | ||
168 | ctx = BN_CTX_new(); | |
169 | if (ctx == NULL) | |
170 | { | |
171 | reason = ERR_R_MALLOC_FAILURE; | |
172 | goto err; | |
173 | } | |
174 | ||
175 | if (EC_GROUP_get_asn1_flag(x)) | |
176 | { | |
177 | /* the curve parameter are given by an asn1 OID */ | |
178 | int nid; | |
179 | ||
180 | if (!BIO_indent(bp, off, 128)) | |
181 | goto err; | |
182 | ||
183 | nid = EC_GROUP_get_curve_name(x); | |
184 | if (nid == 0) | |
185 | goto err; | |
186 | ||
187 | if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) | |
188 | goto err; | |
189 | if (BIO_printf(bp, "\n") <= 0) | |
190 | goto err; | |
191 | } | |
192 | else | |
193 | { | |
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 | { | |
206 | reason = ERR_R_MALLOC_FAILURE; | |
207 | goto err; | |
208 | } | |
209 | ||
210 | if (is_char_two) | |
211 | { | |
212 | if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) | |
213 | { | |
214 | reason = ERR_R_EC_LIB; | |
215 | goto err; | |
216 | } | |
217 | } | |
218 | else /* prime field */ | |
219 | { | |
220 | if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) | |
221 | { | |
222 | reason = ERR_R_EC_LIB; | |
223 | goto err; | |
224 | } | |
225 | } | |
226 | ||
227 | if ((point = EC_GROUP_get0_generator(x)) == NULL) | |
228 | { | |
229 | reason = ERR_R_EC_LIB; | |
230 | goto err; | |
231 | } | |
232 | if (!EC_GROUP_get_order(x, order, NULL) || | |
233 | !EC_GROUP_get_cofactor(x, cofactor, NULL)) | |
234 | { | |
235 | reason = ERR_R_EC_LIB; | |
236 | goto err; | |
237 | } | |
238 | ||
239 | form = EC_GROUP_get_point_conversion_form(x); | |
240 | ||
241 | if ((gen = EC_POINT_point2bn(x, point, | |
242 | form, NULL, ctx)) == NULL) | |
243 | { | |
244 | reason = ERR_R_EC_LIB; | |
245 | goto err; | |
246 | } | |
247 | ||
248 | buf_len = (size_t)BN_num_bytes(p); | |
249 | if (buf_len < (i = (size_t)BN_num_bytes(a))) | |
250 | buf_len = i; | |
251 | if (buf_len < (i = (size_t)BN_num_bytes(b))) | |
252 | buf_len = i; | |
253 | if (buf_len < (i = (size_t)BN_num_bytes(gen))) | |
254 | buf_len = i; | |
255 | if (buf_len < (i = (size_t)BN_num_bytes(order))) | |
256 | buf_len = i; | |
257 | if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) | |
258 | buf_len = i; | |
259 | ||
260 | if ((seed = EC_GROUP_get0_seed(x)) != NULL) | |
261 | seed_len = EC_GROUP_get_seed_len(x); | |
262 | ||
263 | buf_len += 10; | |
264 | if ((buffer = OPENSSL_malloc(buf_len)) == NULL) | |
265 | { | |
266 | reason = ERR_R_MALLOC_FAILURE; | |
267 | goto err; | |
268 | } | |
269 | ||
270 | if (!BIO_indent(bp, off, 128)) | |
271 | goto err; | |
272 | ||
273 | /* print the 'short name' of the field type */ | |
274 | if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) | |
275 | <= 0) | |
276 | goto err; | |
277 | ||
278 | if (is_char_two) | |
279 | { | |
280 | /* print the 'short name' of the base type OID */ | |
281 | int basis_type = EC_GROUP_get_basis_type(x); | |
282 | if (basis_type == 0) | |
283 | goto err; | |
284 | ||
285 | if (!BIO_indent(bp, off, 128)) | |
286 | goto err; | |
287 | ||
288 | if (BIO_printf(bp, "Basis Type: %s\n", | |
289 | OBJ_nid2sn(basis_type)) <= 0) | |
290 | goto err; | |
291 | ||
292 | /* print the polynomial */ | |
293 | if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer, | |
294 | off)) | |
295 | goto err; | |
296 | } | |
297 | else | |
298 | { | |
299 | if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off)) | |
300 | goto err; | |
301 | } | |
302 | if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off)) | |
303 | goto err; | |
304 | if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off)) | |
305 | goto err; | |
306 | if (form == POINT_CONVERSION_COMPRESSED) | |
307 | { | |
308 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, | |
309 | buffer, off)) | |
310 | goto err; | |
311 | } | |
312 | else if (form == POINT_CONVERSION_UNCOMPRESSED) | |
313 | { | |
314 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, | |
315 | buffer, off)) | |
316 | goto err; | |
317 | } | |
318 | else /* form == POINT_CONVERSION_HYBRID */ | |
319 | { | |
320 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, | |
321 | buffer, off)) | |
322 | goto err; | |
323 | } | |
324 | if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, | |
325 | buffer, off)) goto err; | |
326 | if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, | |
327 | buffer, off)) goto err; | |
328 | if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) | |
329 | goto err; | |
330 | } | |
331 | ret=1; | |
332 | err: | |
333 | if (!ret) | |
334 | ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); | |
335 | if (p) | |
336 | BN_free(p); | |
337 | if (a) | |
338 | BN_free(a); | |
339 | if (b) | |
340 | BN_free(b); | |
341 | if (gen) | |
342 | BN_free(gen); | |
343 | if (order) | |
344 | BN_free(order); | |
345 | if (cofactor) | |
346 | BN_free(cofactor); | |
347 | if (ctx) | |
348 | BN_CTX_free(ctx); | |
349 | if (buffer != NULL) | |
350 | OPENSSL_free(buffer); | |
351 | return(ret); | |
352 | } | |
353 | ||
354 | static int print_bin(BIO *fp, const char *name, const unsigned char *buf, | |
355 | size_t len, int off) | |
356 | { | |
357 | size_t i; | |
358 | char str[128]; | |
359 | ||
360 | if (buf == NULL) | |
361 | return 1; | |
362 | if (off) | |
363 | { | |
364 | if (off > 128) | |
365 | off=128; | |
366 | memset(str,' ',off); | |
367 | if (BIO_write(fp, str, off) <= 0) | |
368 | return 0; | |
369 | } | |
370 | ||
371 | if (BIO_printf(fp,"%s", name) <= 0) | |
372 | return 0; | |
373 | ||
374 | for (i=0; i<len; i++) | |
375 | { | |
376 | if ((i%15) == 0) | |
377 | { | |
378 | str[0]='\n'; | |
379 | memset(&(str[1]),' ',off+4); | |
380 | if (BIO_write(fp, str, off+1+4) <= 0) | |
381 | return 0; | |
382 | } | |
383 | if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0) | |
384 | return 0; | |
385 | } | |
386 | if (BIO_write(fp,"\n",1) <= 0) | |
387 | return 0; | |
388 | ||
389 | return 1; | |
390 | } |