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 13280304Sjkim * 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. 60280304Sjkim * 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 71280304Sjkimint EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, 72280304Sjkim EC_POINT *point, const BIGNUM *x, 73280304Sjkim int y_bit, BN_CTX *ctx) 74280304Sjkim{ 75280304Sjkim if (group->meth->point_set_compressed_coordinates == 0 76280304Sjkim && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { 77280304Sjkim ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, 78280304Sjkim ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 79280304Sjkim return 0; 80280304Sjkim } 81280304Sjkim if (group->meth != point->meth) { 82280304Sjkim ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, 83280304Sjkim EC_R_INCOMPATIBLE_OBJECTS); 84280304Sjkim return 0; 85280304Sjkim } 86280304Sjkim if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { 87280304Sjkim if (group->meth->field_type == NID_X9_62_prime_field) 88280304Sjkim return ec_GFp_simple_set_compressed_coordinates(group, point, x, 89280304Sjkim y_bit, ctx); 90280304Sjkim else 91238384Sjkim#ifdef OPENSSL_NO_EC2M 92280304Sjkim { 93280304Sjkim ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, 94280304Sjkim EC_R_GF2M_NOT_SUPPORTED); 95280304Sjkim return 0; 96280304Sjkim } 97238384Sjkim#else 98280304Sjkim return ec_GF2m_simple_set_compressed_coordinates(group, point, x, 99280304Sjkim y_bit, ctx); 100238384Sjkim#endif 101280304Sjkim } 102280304Sjkim return group->meth->point_set_compressed_coordinates(group, point, x, 103280304Sjkim y_bit, ctx); 104280304Sjkim} 105238384Sjkim 106238384Sjkim#ifndef OPENSSL_NO_EC2M 107280304Sjkimint EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, 108280304Sjkim EC_POINT *point, const BIGNUM *x, 109280304Sjkim int y_bit, BN_CTX *ctx) 110280304Sjkim{ 111280304Sjkim if (group->meth->point_set_compressed_coordinates == 0 112280304Sjkim && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { 113280304Sjkim ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, 114280304Sjkim ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 115280304Sjkim return 0; 116280304Sjkim } 117280304Sjkim if (group->meth != point->meth) { 118280304Sjkim ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, 119280304Sjkim EC_R_INCOMPATIBLE_OBJECTS); 120280304Sjkim return 0; 121280304Sjkim } 122280304Sjkim if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { 123280304Sjkim if (group->meth->field_type == NID_X9_62_prime_field) 124280304Sjkim return ec_GFp_simple_set_compressed_coordinates(group, point, x, 125280304Sjkim y_bit, ctx); 126280304Sjkim else 127280304Sjkim return ec_GF2m_simple_set_compressed_coordinates(group, point, x, 128280304Sjkim y_bit, ctx); 129280304Sjkim } 130280304Sjkim return group->meth->point_set_compressed_coordinates(group, point, x, 131280304Sjkim y_bit, ctx); 132280304Sjkim} 133238384Sjkim#endif 134238384Sjkim 135280304Sjkimsize_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, 136280304Sjkim point_conversion_form_t form, unsigned char *buf, 137280304Sjkim size_t len, BN_CTX *ctx) 138280304Sjkim{ 139280304Sjkim if (group->meth->point2oct == 0 140280304Sjkim && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { 141280304Sjkim ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 142280304Sjkim return 0; 143280304Sjkim } 144280304Sjkim if (group->meth != point->meth) { 145280304Sjkim ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS); 146280304Sjkim return 0; 147280304Sjkim } 148280304Sjkim if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { 149280304Sjkim if (group->meth->field_type == NID_X9_62_prime_field) 150280304Sjkim return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx); 151280304Sjkim else 152238384Sjkim#ifdef OPENSSL_NO_EC2M 153280304Sjkim { 154280304Sjkim ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED); 155280304Sjkim return 0; 156280304Sjkim } 157238384Sjkim#else 158280304Sjkim return ec_GF2m_simple_point2oct(group, point, 159280304Sjkim form, buf, len, ctx); 160238384Sjkim#endif 161280304Sjkim } 162238384Sjkim 163280304Sjkim return group->meth->point2oct(group, point, form, buf, len, ctx); 164280304Sjkim} 165238384Sjkim 166238384Sjkimint EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, 167280304Sjkim const unsigned char *buf, size_t len, BN_CTX *ctx) 168280304Sjkim{ 169280304Sjkim if (group->meth->oct2point == 0 170280304Sjkim && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { 171280304Sjkim ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 172280304Sjkim return 0; 173280304Sjkim } 174280304Sjkim if (group->meth != point->meth) { 175280304Sjkim ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS); 176280304Sjkim return 0; 177280304Sjkim } 178280304Sjkim if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { 179280304Sjkim if (group->meth->field_type == NID_X9_62_prime_field) 180280304Sjkim return ec_GFp_simple_oct2point(group, point, buf, len, ctx); 181280304Sjkim else 182238384Sjkim#ifdef OPENSSL_NO_EC2M 183280304Sjkim { 184280304Sjkim ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED); 185280304Sjkim return 0; 186280304Sjkim } 187238384Sjkim#else 188280304Sjkim return ec_GF2m_simple_oct2point(group, point, buf, len, ctx); 189238384Sjkim#endif 190280304Sjkim } 191280304Sjkim return group->meth->oct2point(group, point, buf, len, ctx); 192280304Sjkim} 193