ec_oct.c revision 238384
11553Srgrimes/* crypto/ec/ec_lib.c */
21553Srgrimes/*
31553Srgrimes * Originally written by Bodo Moeller for the OpenSSL project.
41553Srgrimes */
51553Srgrimes/* ====================================================================
61553Srgrimes * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
71553Srgrimes *
81553Srgrimes * Redistribution and use in source and binary forms, with or without
91553Srgrimes * modification, are permitted provided that the following conditions
101553Srgrimes * are met:
111553Srgrimes *
121553Srgrimes * 1. Redistributions of source code must retain the above copyright
131553Srgrimes *    notice, this list of conditions and the following disclaimer.
141553Srgrimes *
151553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161553Srgrimes *    notice, this list of conditions and the following disclaimer in
171553Srgrimes *    the documentation and/or other materials provided with the
181553Srgrimes *    distribution.
191553Srgrimes *
201553Srgrimes * 3. All advertising materials mentioning features or use of this
211553Srgrimes *    software must display the following acknowledgment:
221553Srgrimes *    "This product includes software developed by the OpenSSL Project
231553Srgrimes *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
241553Srgrimes *
251553Srgrimes * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
261553Srgrimes *    endorse or promote products derived from this software without
271553Srgrimes *    prior written permission. For written permission, please contact
281553Srgrimes *    openssl-core@openssl.org.
291553Srgrimes *
301553Srgrimes * 5. Products derived from this software may not be called "OpenSSL"
311553Srgrimes *    nor may "OpenSSL" appear in their names without prior written
321553Srgrimes *    permission of the OpenSSL Project.
331553Srgrimes *
341553Srgrimes * 6. Redistributions of any form whatsoever must retain the following
351553Srgrimes *    acknowledgment:
3629780Scharnier *    "This product includes software developed by the OpenSSL Project
371553Srgrimes *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
381553Srgrimes *
391553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
401553Srgrimes * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
411553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4229780Scharnier * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4315637Sjoerg * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4429780Scharnier * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4529780Scharnier * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4650479Speter * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
471553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
481553Srgrimes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
491553Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
501553Srgrimes * OF THE POSSIBILITY OF SUCH DAMAGE.
5129780Scharnier * ====================================================================
521553Srgrimes *
5331492Swollman * This product includes cryptographic software written by Eric Young
5429780Scharnier * (eay@cryptsoft.com).  This product includes software written by Tim
5529780Scharnier * Hudson (tjh@cryptsoft.com).
561553Srgrimes *
5729780Scharnier */
5829780Scharnier/* ====================================================================
591553Srgrimes * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
6029780Scharnier * Binary polynomial ECC support in OpenSSL originally developed by
611553Srgrimes * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
6250039Smdodd */
6331492Swollman
641553Srgrimes#include <string.h>
651553Srgrimes
661553Srgrimes#include <openssl/err.h>
671553Srgrimes#include <openssl/opensslv.h>
6815637Sjoerg
6915637Sjoerg#include "ec_lcl.h"
7015637Sjoerg
7115637Sjoergint EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
721553Srgrimes	const BIGNUM *x, int y_bit, BN_CTX *ctx)
731553Srgrimes	{
741553Srgrimes	if (group->meth->point_set_compressed_coordinates == 0
751553Srgrimes		&& !(group->meth->flags & EC_FLAGS_DEFAULT_OCT))
7627618Simp		{
7727618Simp		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
7839084Swollman		return 0;
791553Srgrimes		}
8039084Swollman	if (group->meth != point->meth)
8139084Swollman		{
8239084Swollman		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
8339084Swollman		return 0;
841553Srgrimes		}
8578146Sgad	if(group->meth->flags & EC_FLAGS_DEFAULT_OCT)
8678146Sgad		{
8778146Sgad		if (group->meth->field_type == NID_X9_62_prime_field)
8878146Sgad			return ec_GFp_simple_set_compressed_coordinates(
8978146Sgad					group, point, x, y_bit, ctx);
9078146Sgad		else
911553Srgrimes#ifdef OPENSSL_NO_EC2M
921553Srgrimes			{
9378146Sgad			ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, EC_R_GF2M_NOT_SUPPORTED);
941553Srgrimes			return 0;
951553Srgrimes			}
961553Srgrimes#else
9727618Simp			return ec_GF2m_simple_set_compressed_coordinates(
9827618Simp					group, point, x, y_bit, ctx);
9927618Simp#endif
1001553Srgrimes		}
1011553Srgrimes	return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
1021553Srgrimes	}
1031553Srgrimes
1041553Srgrimes#ifndef OPENSSL_NO_EC2M
1051553Srgrimesint EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
1061553Srgrimes	const BIGNUM *x, int y_bit, BN_CTX *ctx)
1071553Srgrimes	{
1081553Srgrimes	if (group->meth->point_set_compressed_coordinates == 0
1091553Srgrimes		&& !(group->meth->flags & EC_FLAGS_DEFAULT_OCT))
1101553Srgrimes		{
1111553Srgrimes		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1121553Srgrimes		return 0;
11315637Sjoerg		}
1141553Srgrimes	if (group->meth != point->meth)
1151553Srgrimes		{
1161553Srgrimes		ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
11731492Swollman		return 0;
11831492Swollman		}
11931492Swollman	if(group->meth->flags & EC_FLAGS_DEFAULT_OCT)
12031492Swollman		{
1211553Srgrimes		if (group->meth->field_type == NID_X9_62_prime_field)
1221553Srgrimes			return ec_GFp_simple_set_compressed_coordinates(
1231553Srgrimes					group, point, x, y_bit, ctx);
12450039Smdodd		else
1251553Srgrimes			return ec_GF2m_simple_set_compressed_coordinates(
1261553Srgrimes					group, point, x, y_bit, ctx);
12750039Smdodd		}
1281553Srgrimes	return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
1291553Srgrimes	}
1301553Srgrimes#endif
1311553Srgrimes
13278146Sgadsize_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
1331553Srgrimes        unsigned char *buf, size_t len, BN_CTX *ctx)
13478146Sgad	{
13550039Smdodd	if (group->meth->point2oct == 0
1361553Srgrimes		&& !(group->meth->flags & EC_FLAGS_DEFAULT_OCT))
1371553Srgrimes		{
13878146Sgad		ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
13968400Sgad		return 0;
14050039Smdodd		}
14168400Sgad	if (group->meth != point->meth)
14250039Smdodd		{
14350039Smdodd		ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
1441553Srgrimes		return 0;
1451553Srgrimes		}
1461553Srgrimes	if(group->meth->flags & EC_FLAGS_DEFAULT_OCT)
1471553Srgrimes		{
14878146Sgad		if (group->meth->field_type == NID_X9_62_prime_field)
1491553Srgrimes			return ec_GFp_simple_point2oct(group, point,
1501553Srgrimes							form, buf, len, ctx);
15150039Smdodd		else
15250039Smdodd#ifdef OPENSSL_NO_EC2M
15350039Smdodd			{
15450071Smdodd			ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
15550039Smdodd			return 0;
1561553Srgrimes			}
1571553Srgrimes#else
1581553Srgrimes			return ec_GF2m_simple_point2oct(group, point,
15950039Smdodd							form, buf, len, ctx);
16050039Smdodd#endif
16150039Smdodd		}
16250039Smdodd
16350039Smdodd	return group->meth->point2oct(group, point, form, buf, len, ctx);
16450039Smdodd	}
16550039Smdodd
16650039Smdodd
16750042Smdoddint EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
16850039Smdodd        const unsigned char *buf, size_t len, BN_CTX *ctx)
16950039Smdodd	{
17062294Smph	if (group->meth->oct2point == 0
17150039Smdodd		&& !(group->meth->flags & EC_FLAGS_DEFAULT_OCT))
17250077Smdodd		{
17350071Smdodd		ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
17450071Smdodd		return 0;
17550039Smdodd		}
17650039Smdodd	if (group->meth != point->meth)
17750039Smdodd		{
17850039Smdodd		ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
17950039Smdodd		return 0;
18050039Smdodd		}
18150039Smdodd	if(group->meth->flags & EC_FLAGS_DEFAULT_OCT)
1821553Srgrimes		{
18350039Smdodd		if (group->meth->field_type == NID_X9_62_prime_field)
1841553Srgrimes			return ec_GFp_simple_oct2point(group, point,
18550039Smdodd							buf, len, ctx);
18650039Smdodd		else
18750039Smdodd#ifdef OPENSSL_NO_EC2M
18850039Smdodd			{
18950039Smdodd			ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
1901553Srgrimes			return 0;
1911553Srgrimes			}
1921553Srgrimes#else
1931553Srgrimes			return ec_GF2m_simple_oct2point(group, point,
1941553Srgrimes							buf, len, ctx);
1951553Srgrimes#endif
1961553Srgrimes		}
1971553Srgrimes	return group->meth->oct2point(group, point, buf, len, ctx);
1981553Srgrimes	}
19915637Sjoerg
2001553Srgrimes