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