1238384Sjkim/* crypto/ec/ec_lib.c */
2238384Sjkim/*
3238384Sjkim * Originally written by Bodo Moeller for the OpenSSL project.
4238384Sjkim */
5238384Sjkim/* ====================================================================
6238384Sjkim * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
7238384Sjkim *
8238384Sjkim * Redistribution and use in source and binary forms, with or without
9238384Sjkim * modification, are permitted provided that the following conditions
10238384Sjkim * are met:
11238384Sjkim *
12238384Sjkim * 1. Redistributions of source code must retain the above copyright
13296341Sdelphij *    notice, this list of conditions and the following disclaimer.
14238384Sjkim *
15238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16238384Sjkim *    notice, this list of conditions and the following disclaimer in
17238384Sjkim *    the documentation and/or other materials provided with the
18238384Sjkim *    distribution.
19238384Sjkim *
20238384Sjkim * 3. All advertising materials mentioning features or use of this
21238384Sjkim *    software must display the following acknowledgment:
22238384Sjkim *    "This product includes software developed by the OpenSSL Project
23238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24238384Sjkim *
25238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26238384Sjkim *    endorse or promote products derived from this software without
27238384Sjkim *    prior written permission. For written permission, please contact
28238384Sjkim *    openssl-core@openssl.org.
29238384Sjkim *
30238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
31238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
32238384Sjkim *    permission of the OpenSSL Project.
33238384Sjkim *
34238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
35238384Sjkim *    acknowledgment:
36238384Sjkim *    "This product includes software developed by the OpenSSL Project
37238384Sjkim *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38238384Sjkim *
39238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
51238384Sjkim * ====================================================================
52238384Sjkim *
53238384Sjkim * This product includes cryptographic software written by Eric Young
54238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
55238384Sjkim * Hudson (tjh@cryptsoft.com).
56238384Sjkim *
57238384Sjkim */
58238384Sjkim/* ====================================================================
59238384Sjkim * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60296341Sdelphij * Binary polynomial ECC support in OpenSSL originally developed by
61238384Sjkim * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62238384Sjkim */
63238384Sjkim
64238384Sjkim#include <string.h>
65238384Sjkim
66238384Sjkim#include <openssl/err.h>
67238384Sjkim#include <openssl/opensslv.h>
68238384Sjkim
69238384Sjkim#include "ec_lcl.h"
70238384Sjkim
71296341Sdelphijint EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
72296341Sdelphij                                            EC_POINT *point, const BIGNUM *x,
73296341Sdelphij                                            int y_bit, BN_CTX *ctx)
74296341Sdelphij{
75296341Sdelphij    if (group->meth->point_set_compressed_coordinates == 0
76296341Sdelphij        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
77296341Sdelphij        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
78296341Sdelphij              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
79296341Sdelphij        return 0;
80296341Sdelphij    }
81296341Sdelphij    if (group->meth != point->meth) {
82296341Sdelphij        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
83296341Sdelphij              EC_R_INCOMPATIBLE_OBJECTS);
84296341Sdelphij        return 0;
85296341Sdelphij    }
86296341Sdelphij    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
87296341Sdelphij        if (group->meth->field_type == NID_X9_62_prime_field)
88296341Sdelphij            return ec_GFp_simple_set_compressed_coordinates(group, point, x,
89296341Sdelphij                                                            y_bit, ctx);
90296341Sdelphij        else
91238384Sjkim#ifdef OPENSSL_NO_EC2M
92296341Sdelphij        {
93296341Sdelphij            ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
94296341Sdelphij                  EC_R_GF2M_NOT_SUPPORTED);
95296341Sdelphij            return 0;
96296341Sdelphij        }
97238384Sjkim#else
98296341Sdelphij            return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
99296341Sdelphij                                                             y_bit, ctx);
100238384Sjkim#endif
101296341Sdelphij    }
102296341Sdelphij    return group->meth->point_set_compressed_coordinates(group, point, x,
103296341Sdelphij                                                         y_bit, ctx);
104296341Sdelphij}
105238384Sjkim
106238384Sjkim#ifndef OPENSSL_NO_EC2M
107296341Sdelphijint EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
108296341Sdelphij                                             EC_POINT *point, const BIGNUM *x,
109296341Sdelphij                                             int y_bit, BN_CTX *ctx)
110296341Sdelphij{
111296341Sdelphij    if (group->meth->point_set_compressed_coordinates == 0
112296341Sdelphij        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
113296341Sdelphij        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
114296341Sdelphij              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
115296341Sdelphij        return 0;
116296341Sdelphij    }
117296341Sdelphij    if (group->meth != point->meth) {
118296341Sdelphij        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
119296341Sdelphij              EC_R_INCOMPATIBLE_OBJECTS);
120296341Sdelphij        return 0;
121296341Sdelphij    }
122296341Sdelphij    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
123296341Sdelphij        if (group->meth->field_type == NID_X9_62_prime_field)
124296341Sdelphij            return ec_GFp_simple_set_compressed_coordinates(group, point, x,
125296341Sdelphij                                                            y_bit, ctx);
126296341Sdelphij        else
127296341Sdelphij            return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
128296341Sdelphij                                                             y_bit, ctx);
129296341Sdelphij    }
130296341Sdelphij    return group->meth->point_set_compressed_coordinates(group, point, x,
131296341Sdelphij                                                         y_bit, ctx);
132296341Sdelphij}
133238384Sjkim#endif
134238384Sjkim
135296341Sdelphijsize_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
136296341Sdelphij                          point_conversion_form_t form, unsigned char *buf,
137296341Sdelphij                          size_t len, BN_CTX *ctx)
138296341Sdelphij{
139296341Sdelphij    if (group->meth->point2oct == 0
140296341Sdelphij        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
141296341Sdelphij        ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142296341Sdelphij        return 0;
143296341Sdelphij    }
144296341Sdelphij    if (group->meth != point->meth) {
145296341Sdelphij        ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
146296341Sdelphij        return 0;
147296341Sdelphij    }
148296341Sdelphij    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
149296341Sdelphij        if (group->meth->field_type == NID_X9_62_prime_field)
150296341Sdelphij            return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
151296341Sdelphij        else
152238384Sjkim#ifdef OPENSSL_NO_EC2M
153296341Sdelphij        {
154296341Sdelphij            ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
155296341Sdelphij            return 0;
156296341Sdelphij        }
157238384Sjkim#else
158296341Sdelphij            return ec_GF2m_simple_point2oct(group, point,
159296341Sdelphij                                            form, buf, len, ctx);
160238384Sjkim#endif
161296341Sdelphij    }
162238384Sjkim
163296341Sdelphij    return group->meth->point2oct(group, point, form, buf, len, ctx);
164296341Sdelphij}
165238384Sjkim
166238384Sjkimint EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
167296341Sdelphij                       const unsigned char *buf, size_t len, BN_CTX *ctx)
168296341Sdelphij{
169296341Sdelphij    if (group->meth->oct2point == 0
170296341Sdelphij        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
171296341Sdelphij        ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
172296341Sdelphij        return 0;
173296341Sdelphij    }
174296341Sdelphij    if (group->meth != point->meth) {
175296341Sdelphij        ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
176296341Sdelphij        return 0;
177296341Sdelphij    }
178296341Sdelphij    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
179296341Sdelphij        if (group->meth->field_type == NID_X9_62_prime_field)
180296341Sdelphij            return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
181296341Sdelphij        else
182238384Sjkim#ifdef OPENSSL_NO_EC2M
183296341Sdelphij        {
184296341Sdelphij            ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
185296341Sdelphij            return 0;
186296341Sdelphij        }
187238384Sjkim#else
188296341Sdelphij            return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
189238384Sjkim#endif
190296341Sdelphij    }
191296341Sdelphij    return group->meth->oct2point(group, point, buf, len, ctx);
192296341Sdelphij}
193