2 * Originally written by Bodo Moeller for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
65 #include <openssl/err.h>
66 #include <openssl/opensslv.h>
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)
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);
80 if (group->meth != point->meth) {
81 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
82 EC_R_INCOMPATIBLE_OBJECTS);
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,
90 #ifdef OPENSSL_NO_EC2M
92 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
93 EC_R_GF2M_NOT_SUPPORTED);
97 return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
101 return group->meth->point_set_compressed_coordinates(group, point, x,
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)
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);
116 if (group->meth != point->meth) {
117 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
118 EC_R_INCOMPATIBLE_OBJECTS);
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,
126 return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
129 return group->meth->point_set_compressed_coordinates(group, point, x,
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)
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);
143 if (group->meth != point->meth) {
144 ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
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);
151 #ifdef OPENSSL_NO_EC2M
153 ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
157 return ec_GF2m_simple_point2oct(group, point,
158 form, buf, len, ctx);
162 return group->meth->point2oct(group, point, form, buf, len, ctx);
165 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
166 const unsigned char *buf, size_t len, BN_CTX *ctx)
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);
173 if (group->meth != point->meth) {
174 ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
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);
181 #ifdef OPENSSL_NO_EC2M
183 ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
187 return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
190 return group->meth->oct2point(group, point, buf, len, ctx);
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)
199 len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
202 buf = OPENSSL_malloc(len);
205 len = EC_POINT_point2oct(group, point, form, buf, len, ctx);