Cleanup OPENSSL_NO_xxx, part 1
[openssl.git] / crypto / ec / eck_prn.c
1 /* crypto/ec/eck_prn.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
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  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * Portions originally developed by SUN MICROSYSTEMS, INC., and 
61  * 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 #include <openssl/bn.h>
69
70 #ifndef OPENSSL_NO_STDIO
71 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
72         {
73         BIO *b;
74         int ret;
75
76         if ((b=BIO_new(BIO_s_file())) == NULL)
77                 {
78                 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
79                 return(0);
80                 }
81         BIO_set_fp(b, fp, BIO_NOCLOSE);
82         ret = ECPKParameters_print(b, x, off);
83         BIO_free(b);
84         return(ret);
85         }
86
87 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
88         {
89         BIO *b;
90         int ret;
91  
92         if ((b=BIO_new(BIO_s_file())) == NULL)
93                 {
94                 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
95                 return(0);
96                 }
97         BIO_set_fp(b, fp, BIO_NOCLOSE);
98         ret = EC_KEY_print(b, x, off);
99         BIO_free(b);
100         return(ret);
101         }
102
103 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
104         {
105         BIO *b;
106         int ret;
107  
108         if ((b=BIO_new(BIO_s_file())) == NULL)
109                 {
110                 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
111                 return(0);
112                 }
113         BIO_set_fp(b, fp, BIO_NOCLOSE);
114         ret = ECParameters_print(b, x);
115         BIO_free(b);
116         return(ret);
117         }
118 #endif
119
120 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
121         {
122         EVP_PKEY *pk;
123         int ret;
124         pk = EVP_PKEY_new();
125         if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
126                 return 0;
127         ret = EVP_PKEY_print_private(bp, pk, off, NULL);
128         EVP_PKEY_free(pk);
129         return ret;
130         }
131
132 int ECParameters_print(BIO *bp, const EC_KEY *x)
133         {
134         EVP_PKEY *pk;
135         int ret;
136         pk = EVP_PKEY_new();
137         if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *)x))
138                 return 0;
139         ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
140         EVP_PKEY_free(pk);
141         return ret;
142         }
143
144 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
145                 size_t len, int off);
146
147 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
148         {
149         unsigned char *buffer=NULL;
150         size_t  buf_len=0, i;
151         int     ret=0, reason=ERR_R_BIO_LIB;
152         BN_CTX  *ctx=NULL;
153         const EC_POINT *point=NULL;
154         BIGNUM  *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
155                 *order=NULL, *cofactor=NULL;
156         const unsigned char *seed;
157         size_t  seed_len=0;
158         
159         static const char *gen_compressed = "Generator (compressed):";
160         static const char *gen_uncompressed = "Generator (uncompressed):";
161         static const char *gen_hybrid = "Generator (hybrid):";
162  
163         if (!x)
164                 {
165                 reason = ERR_R_PASSED_NULL_PARAMETER;
166                 goto err;
167                 }
168
169         ctx = BN_CTX_new();
170         if (ctx == NULL)
171                 {
172                 reason = ERR_R_MALLOC_FAILURE;
173                 goto err;
174                 }
175
176         if (EC_GROUP_get_asn1_flag(x))
177                 {
178                 /* the curve parameter are given by an asn1 OID */
179                 int nid;
180                 const char *nname;
181
182                 if (!BIO_indent(bp, off, 128))
183                         goto err;
184
185                 nid = EC_GROUP_get_curve_name(x);
186                 if (nid == 0)
187                         goto err;
188                 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
189                         goto err;
190                 if (BIO_printf(bp, "\n") <= 0)
191                         goto err;
192                 nname = EC_curve_nid2nist(nid);
193                 if (nname)
194                         {
195                         if (!BIO_indent(bp, off, 128))
196                                 goto err;
197                         if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
198                                 goto err;
199                         }
200                 }
201         else
202                 {
203                 /* explicit parameters */
204                 int is_char_two = 0;
205                 point_conversion_form_t form;
206                 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
207
208                 if (tmp_nid == NID_X9_62_characteristic_two_field)
209                         is_char_two = 1;
210
211                 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
212                         (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
213                         (cofactor = BN_new()) == NULL)
214                         {
215                         reason = ERR_R_MALLOC_FAILURE;
216                         goto err;
217                         }
218 #ifndef OPENSSL_NO_EC2M
219                 if (is_char_two)
220                         {
221                         if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
222                                 {
223                                 reason = ERR_R_EC_LIB;
224                                 goto err;
225                                 }
226                         }
227                 else /* prime field */
228 #endif
229                         {
230                         if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
231                                 {
232                                 reason = ERR_R_EC_LIB;
233                                 goto err;
234                                 }
235                         }
236
237                 if ((point = EC_GROUP_get0_generator(x)) == NULL)
238                         {
239                         reason = ERR_R_EC_LIB;
240                         goto err;
241                         }
242                 if (!EC_GROUP_get_order(x, order, NULL) || 
243                         !EC_GROUP_get_cofactor(x, cofactor, NULL))
244                         {
245                         reason = ERR_R_EC_LIB;
246                         goto err;
247                         }
248                 
249                 form = EC_GROUP_get_point_conversion_form(x);
250
251                 if ((gen = EC_POINT_point2bn(x, point, 
252                                 form, NULL, ctx)) == NULL)
253                         {
254                         reason = ERR_R_EC_LIB;
255                         goto err;
256                         }
257
258                 buf_len = (size_t)BN_num_bytes(p);
259                 if (buf_len < (i = (size_t)BN_num_bytes(a)))
260                         buf_len = i;
261                 if (buf_len < (i = (size_t)BN_num_bytes(b)))
262                         buf_len = i;
263                 if (buf_len < (i = (size_t)BN_num_bytes(gen)))
264                         buf_len = i;
265                 if (buf_len < (i = (size_t)BN_num_bytes(order)))
266                         buf_len = i;
267                 if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) 
268                         buf_len = i;
269
270                 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
271                         seed_len = EC_GROUP_get_seed_len(x);
272
273                 buf_len += 10;
274                 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
275                         {
276                         reason = ERR_R_MALLOC_FAILURE;
277                         goto err;
278                         }
279
280                 if (!BIO_indent(bp, off, 128))
281                         goto err;
282
283                 /* print the 'short name' of the field type */
284                 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
285                         <= 0)
286                         goto err;  
287
288                 if (is_char_two)
289                         {
290                         /* print the 'short name' of the base type OID */
291                         int basis_type = EC_GROUP_get_basis_type(x);
292                         if (basis_type == 0)
293                                 goto err;
294
295                         if (!BIO_indent(bp, off, 128))
296                                 goto err;
297
298                         if (BIO_printf(bp, "Basis Type: %s\n", 
299                                 OBJ_nid2sn(basis_type)) <= 0)
300                                 goto err;
301
302                         /* print the polynomial */
303                         if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
304                                 off))
305                                 goto err;
306                         }
307                 else
308                         {
309                         if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer,off))
310                                 goto err;
311                         }
312                 if ((a != NULL) && !ASN1_bn_print(bp, "A:   ", a, buffer, off)) 
313                         goto err;
314                 if ((b != NULL) && !ASN1_bn_print(bp, "B:   ", b, buffer, off))
315                         goto err;
316                 if (form == POINT_CONVERSION_COMPRESSED)
317                         {
318                         if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
319                                 buffer, off))
320                                 goto err;
321                         }
322                 else if (form == POINT_CONVERSION_UNCOMPRESSED)
323                         {
324                         if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
325                                 buffer, off))
326                                 goto err;
327                         }
328                 else /* form == POINT_CONVERSION_HYBRID */
329                         {
330                         if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
331                                 buffer, off))
332                                 goto err;
333                         }
334                 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, 
335                         buffer, off)) goto err;
336                 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, 
337                         buffer, off)) goto err;
338                 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
339                         goto err;
340                 }
341         ret=1;
342 err:
343         if (!ret)
344                 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
345         if (p) 
346                 BN_free(p);
347         if (a) 
348                 BN_free(a);
349         if (b)
350                 BN_free(b);
351         if (gen)
352                 BN_free(gen);
353         if (order)
354                 BN_free(order);
355         if (cofactor)
356                 BN_free(cofactor);
357         if (ctx)
358                 BN_CTX_free(ctx);
359         if (buffer != NULL) 
360                 OPENSSL_free(buffer);
361         return(ret);    
362         }
363
364 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
365                 size_t len, int off)
366         {
367         size_t i;
368         char str[128];
369
370         if (buf == NULL)
371                 return 1;
372         if (off)
373                 {
374                 if (off > 128)
375                         off=128;
376                 memset(str,' ',off);
377                 if (BIO_write(fp, str, off) <= 0)
378                         return 0;
379                 }
380
381         if (BIO_printf(fp,"%s", name) <= 0)
382                 return 0;
383
384         for (i=0; i<len; i++)
385                 {
386                 if ((i%15) == 0)
387                         {
388                         str[0]='\n';
389                         memset(&(str[1]),' ',off+4);
390                         if (BIO_write(fp, str, off+1+4) <= 0)
391                                 return 0;
392                         }
393                 if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
394                         return 0;
395                 }
396         if (BIO_write(fp,"\n",1) <= 0)
397                 return 0;
398
399         return 1;
400         }