ec_asn1.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/*
* Written by Nils Larsch for the OpenSSL project.
*/
/* ====================================================================
* Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <string.h>
#include "ec_lcl.h"
{
int i=0;
/* everything else is currently not supported */
return 0;
i++;
if (i == 4)
return NID_X9_62_ppBasis;
else if (i == 2)
return NID_X9_62_tpBasis;
else
/* everything else is currently not supported */
return 0;
}
{
return 0;
{
return 0;
}
if (k)
return 1;
}
{
return 0;
|| !((group->poly[0] != 0) && (group->poly[1] != 0) && (group->poly[2] != 0) && (group->poly[3] != 0) && (group->poly[4] == 0)))
{
return 0;
}
if (k1)
if (k2)
if (k3)
return 1;
}
/* some structures needed for the asn1 encoding */
typedef struct x9_62_pentanomial_st {
long k1;
long k2;
long k3;
typedef struct x9_62_characteristic_two_st {
long m;
union {
char *ptr;
/* NID_X9_62_onBasis */
/* NID_X9_62_tpBasis */
/* NID_X9_62_ppBasis */
/* anything else */
} p;
typedef struct x9_62_fieldid_st {
union {
char *ptr;
/* NID_X9_62_prime_field */
/* NID_X9_62_characteristic_two_field */
/* anything else */
} p;
typedef struct x9_62_curve_st {
} X9_62_CURVE;
typedef struct ec_parameters_st {
long version;
} ECPARAMETERS;
struct ecpk_parameters_st {
int type;
union {
} value;
}/* ECPKPARAMETERS */;
/* SEC1 ECPrivateKey */
typedef struct ec_privatekey_st {
long version;
/* the OpenSSL ASN.1 definitions */
ASN1_ADB(X9_62_FIELDID) = {
ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO))
ASN1_SEQUENCE(X9_62_CURVE) = {
/* some declarations of internal function */
/* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */
/* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */
/* ec_asn1_parameters2group() creates a EC_GROUP object from a
* ECPARAMETERS object */
/* ec_asn1_group2parameters() creates a ECPARAMETERS object from a
* EC_GROUP object */
/* ec_asn1_pkparameters2group() creates a EC_GROUP object from a
* ECPKPARAMETERS object */
/* ec_asn1_group2pkparameters() creates a ECPKPARAMETERS object from a
* EC_GROUP object */
ECPKPARAMETERS *);
/* the function definitions */
{
return 0;
/* clear the old values (if necessary) */
/* set OID for the field */
{
goto err;
}
if (nid == NID_X9_62_prime_field)
{
{
goto err;
}
/* the parameters are specified by the prime number p */
{
goto err;
}
/* set the prime number */
{
goto err;
}
}
else /* nid == NID_X9_62_characteristic_two_field */
{
int field_type;
{
goto err;
}
if (field_type == 0)
{
goto err;
}
/* set base type OID */
{
goto err;
}
if (field_type == NID_X9_62_tpBasis)
{
unsigned int k;
if (!EC_GROUP_get_trinomial_basis(group, &k))
goto err;
{
goto err;
}
{
goto err;
}
}
else if (field_type == NID_X9_62_ppBasis)
{
goto err;
{
goto err;
}
/* set k? values */
}
else /* field_type == NID_X9_62_onBasis */
{
/* for ONB the parameters are (asn1) NULL */
{
goto err;
}
}
}
ok = 1;
return(ok);
}
{
unsigned char char_zero = 0;
return 0;
{
goto err;
}
/* get a and b */
if (nid == NID_X9_62_prime_field)
{
{
goto err;
}
}
else /* nid == NID_X9_62_characteristic_two_field */
{
{
goto err;
}
}
if (len_1 == 0)
{
/* len_1 == 0 => a == 0 */
len_1 = 1;
}
else
{
{
goto err;
}
{
goto err;
}
}
if (len_2 == 0)
{
/* len_2 == 0 => b == 0 */
len_2 = 1;
}
else
{
{
goto err;
}
{
goto err;
}
}
/* set a and b */
{
goto err;
}
/* set the seed (optional) */
{
{
goto err;
}
{
goto err;
}
}
else
{
{
}
}
ok = 1;
if (buffer_2)
if (tmp_1)
if (tmp_2)
return(ok);
}
{
int ok=0;
{
goto err;
}
{
{
goto err;
}
}
else
/* set the version (always one) */
/* set the fieldID */
{
goto err;
}
/* set the curve */
{
goto err;
}
/* set the base point */
{
goto err;
}
if (len == 0)
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
/* set the order */
{
goto err;
}
{
goto err;
}
/* set the cofactor (optional) */
{
{
goto err;
}
}
ok = 1;
{
}
if (tmp)
if (buffer)
return(ret);
}
{
{
{
return NULL;
}
}
else
{
}
if (EC_GROUP_get_asn1_flag(group))
{
/* use the asn1 OID to describe the
* the elliptic curve parameters
*/
if (tmp)
{
ok = 0;
}
else
/* we don't kmow the nid => ERROR */
ok = 0;
}
else
{
/* use the ECPARAMETERS structure */
ok = 0;
}
if (!ok)
{
return NULL;
}
return ret;
}
{
{
goto err;
}
/* now extract the curve parameters a and b */
{
goto err;
}
if (a == NULL)
{
goto err;
}
if (b == NULL)
{
goto err;
}
/* get the field parameters */
{
{
goto err;
}
/* get the base type */
if (tmp == NID_X9_62_tpBasis)
{
long tmp_long;
{
goto err;
}
/* create the polynomial */
if (!BN_set_bit(p, (int)char_two->m))
goto err;
if (!BN_set_bit(p, (int)tmp_long))
goto err;
if (!BN_set_bit(p, 0))
goto err;
}
else if (tmp == NID_X9_62_ppBasis)
{
if (!penta)
{
goto err;
}
/* create the polynomial */
if (!BN_set_bit(p, 0)) goto err;
}
else if (tmp == NID_X9_62_onBasis)
{
goto err;
}
else /* error */
{
goto err;
}
/* create the EC_GROUP structure */
{
goto err;
}
}
else if (tmp == NID_X9_62_prime_field)
{
/* we have a curve over a prime field */
/* extract the prime number */
{
goto err;
}
if (p == NULL)
{
goto err;
}
/* create the EC_GROUP structure */
{
goto err;
}
}
/* extract seed (optional) */
{
{
goto err;
}
}
{
goto err;
}
/* set the point conversion form */
/* extract the ec point */
{
goto err;
}
/* extract the order */
{
goto err;
}
/* extract the cofactor (optional) */
{
if (b)
{
BN_free(b);
b = NULL;
}
}
else
{
goto err;
}
/* set the generator, order and cofactor (if present) */
{
goto err;
}
ok = 1;
{
if (ret)
}
if (p)
BN_free(p);
if (a)
BN_free(a);
if (b)
BN_free(b);
if (point)
return(ret);
}
{
int tmp=0;
{
return NULL;
}
{ /* the curve is given by an OID */
{
return NULL;
}
}
{ /* the parameters are given by a ECPARAMETERS
* structure */
if (!ret)
{
return NULL;
}
}
{ /* implicitlyCA */
return NULL;
}
else
{
return NULL;
}
return ret;
}
/* EC_GROUP <-> DER encoding of ECPKPARAMETERS */
{
{
return NULL;
}
{
return NULL;
}
if (a && *a)
EC_GROUP_clear_free(*a);
if (a)
*a = group;
return(group);
}
{
int ret=0;
{
return 0;
}
{
return 0;
}
return(ret);
}
/* some EC_KEY functions */
{
int ok=0;
{
return NULL;
}
{
return NULL;
}
{
{
goto err;
}
if (a)
*a = ret;
}
else
ret = *a;
if (priv_key->parameters)
{
}
{
goto err;
}
if (priv_key->privateKey)
{
{
goto err;
}
}
else
{
goto err;
}
{
const unsigned char *pub_oct;
{
goto err;
}
/* save the point conversion form */
{
goto err;
}
}
ok = 1;
err:
if (!ok)
{
if (ret)
}
if (priv_key)
return(ret);
}
{
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
{
goto err;
}
if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS))
{
{
goto err;
}
}
if (!(a->enc_flag & EC_PKEY_NO_PUBKEY))
{
{
goto err;
}
{
if (!tmp_buffer)
{
goto err;
}
buffer = tmp_buffer;
}
{
goto err;
}
buf_len))
{
goto err;
}
}
{
goto err;
}
ok=1;
err:
if (buffer)
if (priv_key)
}
{
if (a == NULL)
{
return 0;
}
}
{
{
return NULL;
}
{
{
return NULL;
}
if (a)
*a = ret;
}
else
ret = *a;
{
return NULL;
}
return ret;
}
{
{
/* sorry, but a EC_GROUP-structur is necessary
* to set the public key */
return 0;
}
ret = *a;
{
return 0;
}
{
return 0;
}
/* save the point conversion form */
return ret;
}
{
int new_buffer = 0;
if (a == NULL)
{
return 0;
}
/* out == NULL => just return the length of the octet string */
return buf_len;
{
{
return 0;
}
new_buffer = 1;
}
{
OPENSSL_free(*out);
return 0;
}
if (!new_buffer)
return buf_len;
}