Remove /* foo.c */ comments
[openssl.git] / crypto / ec / ec_oct.c
1 /*
2  * Originally written by Bodo Moeller for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 1998-2003 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  * Binary polynomial ECC support in OpenSSL originally developed by
60  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
61  */
62
63 #include <string.h>
64
65 #include <openssl/err.h>
66 #include <openssl/opensslv.h>
67
68 #include "ec_lcl.h"
69
70 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
71                                             EC_POINT *point, const BIGNUM *x,
72                                             int y_bit, BN_CTX *ctx)
73 {
74     if (group->meth->point_set_compressed_coordinates == 0
75         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
76         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
77               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
78         return 0;
79     }
80     if (group->meth != point->meth) {
81         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
82               EC_R_INCOMPATIBLE_OBJECTS);
83         return 0;
84     }
85     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
86         if (group->meth->field_type == NID_X9_62_prime_field)
87             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
88                                                             y_bit, ctx);
89         else
90 #ifdef OPENSSL_NO_EC2M
91         {
92             ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
93                   EC_R_GF2M_NOT_SUPPORTED);
94             return 0;
95         }
96 #else
97             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
98                                                              y_bit, ctx);
99 #endif
100     }
101     return group->meth->point_set_compressed_coordinates(group, point, x,
102                                                          y_bit, ctx);
103 }
104
105 #ifndef OPENSSL_NO_EC2M
106 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
107                                              EC_POINT *point, const BIGNUM *x,
108                                              int y_bit, BN_CTX *ctx)
109 {
110     if (group->meth->point_set_compressed_coordinates == 0
111         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
112         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
113               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
114         return 0;
115     }
116     if (group->meth != point->meth) {
117         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
118               EC_R_INCOMPATIBLE_OBJECTS);
119         return 0;
120     }
121     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
122         if (group->meth->field_type == NID_X9_62_prime_field)
123             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
124                                                             y_bit, ctx);
125         else
126             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
127                                                              y_bit, ctx);
128     }
129     return group->meth->point_set_compressed_coordinates(group, point, x,
130                                                          y_bit, ctx);
131 }
132 #endif
133
134 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
135                           point_conversion_form_t form, unsigned char *buf,
136                           size_t len, BN_CTX *ctx)
137 {
138     if (group->meth->point2oct == 0
139         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
140         ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
141         return 0;
142     }
143     if (group->meth != point->meth) {
144         ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
145         return 0;
146     }
147     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
148         if (group->meth->field_type == NID_X9_62_prime_field)
149             return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
150         else
151 #ifdef OPENSSL_NO_EC2M
152         {
153             ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
154             return 0;
155         }
156 #else
157             return ec_GF2m_simple_point2oct(group, point,
158                                             form, buf, len, ctx);
159 #endif
160     }
161
162     return group->meth->point2oct(group, point, form, buf, len, ctx);
163 }
164
165 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
166                        const unsigned char *buf, size_t len, BN_CTX *ctx)
167 {
168     if (group->meth->oct2point == 0
169         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
170         ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
171         return 0;
172     }
173     if (group->meth != point->meth) {
174         ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
175         return 0;
176     }
177     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
178         if (group->meth->field_type == NID_X9_62_prime_field)
179             return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
180         else
181 #ifdef OPENSSL_NO_EC2M
182         {
183             ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
184             return 0;
185         }
186 #else
187             return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
188 #endif
189     }
190     return group->meth->oct2point(group, point, buf, len, ctx);
191 }
192
193 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
194                           point_conversion_form_t form,
195                           unsigned char **pbuf, BN_CTX *ctx)
196 {
197     size_t len;
198     unsigned char *buf;
199     len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
200     if (len == 0)
201         return 0;
202     buf = OPENSSL_malloc(len);
203     if (buf == NULL)
204         return 0;
205     len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
206     if (len == 0) {
207         OPENSSL_free(buf);
208         return 0;
209     }
210     *pbuf = buf;
211     return len;
212 }