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
13280297Sjkim *    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.
60280297Sjkim * 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
71280297Sjkimint EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
72280297Sjkim                                            EC_POINT *point, const BIGNUM *x,
73280297Sjkim                                            int y_bit, BN_CTX *ctx)
74280297Sjkim{
75280297Sjkim    if (group->meth->point_set_compressed_coordinates == 0
76280297Sjkim        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
77280297Sjkim        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
78280297Sjkim              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
79280297Sjkim        return 0;
80280297Sjkim    }
81280297Sjkim    if (group->meth != point->meth) {
82280297Sjkim        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
83280297Sjkim              EC_R_INCOMPATIBLE_OBJECTS);
84280297Sjkim        return 0;
85280297Sjkim    }
86280297Sjkim    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
87280297Sjkim        if (group->meth->field_type == NID_X9_62_prime_field)
88280297Sjkim            return ec_GFp_simple_set_compressed_coordinates(group, point, x,
89280297Sjkim                                                            y_bit, ctx);
90280297Sjkim        else
91238384Sjkim#ifdef OPENSSL_NO_EC2M
92280297Sjkim        {
93280297Sjkim            ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
94280297Sjkim                  EC_R_GF2M_NOT_SUPPORTED);
95280297Sjkim            return 0;
96280297Sjkim        }
97238384Sjkim#else
98280297Sjkim            return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
99280297Sjkim                                                             y_bit, ctx);
100238384Sjkim#endif
101280297Sjkim    }
102280297Sjkim    return group->meth->point_set_compressed_coordinates(group, point, x,
103280297Sjkim                                                         y_bit, ctx);
104280297Sjkim}
105238384Sjkim
106238384Sjkim#ifndef OPENSSL_NO_EC2M
107280297Sjkimint EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
108280297Sjkim                                             EC_POINT *point, const BIGNUM *x,
109280297Sjkim                                             int y_bit, BN_CTX *ctx)
110280297Sjkim{
111280297Sjkim    if (group->meth->point_set_compressed_coordinates == 0
112280297Sjkim        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
113280297Sjkim        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
114280297Sjkim              ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
115280297Sjkim        return 0;
116280297Sjkim    }
117280297Sjkim    if (group->meth != point->meth) {
118280297Sjkim        ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
119280297Sjkim              EC_R_INCOMPATIBLE_OBJECTS);
120280297Sjkim        return 0;
121280297Sjkim    }
122280297Sjkim    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
123280297Sjkim        if (group->meth->field_type == NID_X9_62_prime_field)
124280297Sjkim            return ec_GFp_simple_set_compressed_coordinates(group, point, x,
125280297Sjkim                                                            y_bit, ctx);
126280297Sjkim        else
127280297Sjkim            return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
128280297Sjkim                                                             y_bit, ctx);
129280297Sjkim    }
130280297Sjkim    return group->meth->point_set_compressed_coordinates(group, point, x,
131280297Sjkim                                                         y_bit, ctx);
132280297Sjkim}
133238384Sjkim#endif
134238384Sjkim
135280297Sjkimsize_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
136280297Sjkim                          point_conversion_form_t form, unsigned char *buf,
137280297Sjkim                          size_t len, BN_CTX *ctx)
138280297Sjkim{
139280297Sjkim    if (group->meth->point2oct == 0
140280297Sjkim        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
141280297Sjkim        ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142280297Sjkim        return 0;
143280297Sjkim    }
144280297Sjkim    if (group->meth != point->meth) {
145280297Sjkim        ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
146280297Sjkim        return 0;
147280297Sjkim    }
148280297Sjkim    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
149280297Sjkim        if (group->meth->field_type == NID_X9_62_prime_field)
150280297Sjkim            return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
151280297Sjkim        else
152238384Sjkim#ifdef OPENSSL_NO_EC2M
153280297Sjkim        {
154280297Sjkim            ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
155280297Sjkim            return 0;
156280297Sjkim        }
157238384Sjkim#else
158280297Sjkim            return ec_GF2m_simple_point2oct(group, point,
159280297Sjkim                                            form, buf, len, ctx);
160238384Sjkim#endif
161280297Sjkim    }
162238384Sjkim
163280297Sjkim    return group->meth->point2oct(group, point, form, buf, len, ctx);
164280297Sjkim}
165238384Sjkim
166238384Sjkimint EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
167280297Sjkim                       const unsigned char *buf, size_t len, BN_CTX *ctx)
168280297Sjkim{
169280297Sjkim    if (group->meth->oct2point == 0
170280297Sjkim        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
171280297Sjkim        ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
172280297Sjkim        return 0;
173280297Sjkim    }
174280297Sjkim    if (group->meth != point->meth) {
175280297Sjkim        ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
176280297Sjkim        return 0;
177280297Sjkim    }
178280297Sjkim    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
179280297Sjkim        if (group->meth->field_type == NID_X9_62_prime_field)
180280297Sjkim            return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
181280297Sjkim        else
182238384Sjkim#ifdef OPENSSL_NO_EC2M
183280297Sjkim        {
184280297Sjkim            ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
185280297Sjkim            return 0;
186280297Sjkim        }
187238384Sjkim#else
188280297Sjkim            return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
189238384Sjkim#endif
190280297Sjkim    }
191280297Sjkim    return group->meth->oct2point(group, point, buf, len, ctx);
192280297Sjkim}
193