ec_cvt.c revision 296465
199730Sbenno/* crypto/ec/ec_cvt.c */
299730Sbenno/*
399730Sbenno * Originally written by Bodo Moeller for the OpenSSL project.
499730Sbenno */
599730Sbenno/* ====================================================================
699730Sbenno * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
799730Sbenno *
8104435Sgrehan * Redistribution and use in source and binary forms, with or without
9132069Sgrehan * modification, are permitted provided that the following conditions
10132069Sgrehan * are met:
11132069Sgrehan *
12132069Sgrehan * 1. Redistributions of source code must retain the above copyright
13132069Sgrehan *    notice, this list of conditions and the following disclaimer.
14132069Sgrehan *
15132069Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
1699730Sbenno *    notice, this list of conditions and the following disclaimer in
17132069Sgrehan *    the documentation and/or other materials provided with the
18132069Sgrehan *    distribution.
1999730Sbenno *
20132069Sgrehan * 3. All advertising materials mentioning features or use of this
21132069Sgrehan *    software must display the following acknowledgment:
22132069Sgrehan *    "This product includes software developed by the OpenSSL Project
2399730Sbenno *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2499730Sbenno *
2599730Sbenno * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2699730Sbenno *    endorse or promote products derived from this software without
2799730Sbenno *    prior written permission. For written permission, please contact
2899730Sbenno *    openssl-core@openssl.org.
2999730Sbenno *
3099730Sbenno * 5. Products derived from this software may not be called "OpenSSL"
3199730Sbenno *    nor may "OpenSSL" appear in their names without prior written
32132069Sgrehan *    permission of the OpenSSL Project.
3399730Sbenno *
34132069Sgrehan * 6. Redistributions of any form whatsoever must retain the following
35132069Sgrehan *    acknowledgment:
36132069Sgrehan *    "This product includes software developed by the OpenSSL Project
37132069Sgrehan *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3899730Sbenno *
39132069Sgrehan * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40132069Sgrehan * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41132069Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42132069Sgrehan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4399730Sbenno * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44132069Sgrehan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45132069Sgrehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46132069Sgrehan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47132069Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48132069Sgrehan * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49132069Sgrehan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50132069Sgrehan * OF THE POSSIBILITY OF SUCH DAMAGE.
5199730Sbenno * ====================================================================
52132069Sgrehan *
53132069Sgrehan * This product includes cryptographic software written by Eric Young
5499730Sbenno * (eay@cryptsoft.com).  This product includes software written by Tim
5599730Sbenno * Hudson (tjh@cryptsoft.com).
56132069Sgrehan *
57132069Sgrehan */
5899730Sbenno/* ====================================================================
59132069Sgrehan * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60132069Sgrehan *
61132069Sgrehan * Portions of the attached software ("Contribution") are developed by
62132069Sgrehan * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
6399730Sbenno *
64132069Sgrehan * The Contribution is licensed pursuant to the OpenSSL open source
65132069Sgrehan * license provided above.
66132069Sgrehan *
67132069Sgrehan * The elliptic curve binary polynomial software is originally written by
6899730Sbenno * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69132069Sgrehan *
70132069Sgrehan */
71132069Sgrehan
72132069Sgrehan#include <openssl/err.h>
73132069Sgrehan#include "ec_lcl.h"
74132069Sgrehan
75132069SgrehanEC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
7699730Sbenno                                 const BIGNUM *b, BN_CTX *ctx)
77132069Sgrehan{
78132069Sgrehan    const EC_METHOD *meth;
7999730Sbenno    EC_GROUP *ret;
8099730Sbenno
81132069Sgrehan    meth = EC_GFp_nist_method();
82132069Sgrehan
8399730Sbenno    ret = EC_GROUP_new(meth);
8499730Sbenno    if (ret == NULL)
8599730Sbenno        return NULL;
86132069Sgrehan
87132069Sgrehan    if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx)) {
88132069Sgrehan        unsigned long err;
89132069Sgrehan
9099730Sbenno        err = ERR_peek_last_error();
91132069Sgrehan
9299730Sbenno        if (!(ERR_GET_LIB(err) == ERR_LIB_EC &&
9399730Sbenno              ((ERR_GET_REASON(err) == EC_R_NOT_A_NIST_PRIME) ||
94132069Sgrehan               (ERR_GET_REASON(err) == EC_R_NOT_A_SUPPORTED_NIST_PRIME)))) {
9599730Sbenno            /* real error */
96132069Sgrehan
9799730Sbenno            EC_GROUP_clear_free(ret);
98            return NULL;
99        }
100
101        /*
102         * not an actual error, we just cannot use EC_GFp_nist_method
103         */
104
105        ERR_clear_error();
106
107        EC_GROUP_clear_free(ret);
108        meth = EC_GFp_mont_method();
109
110        ret = EC_GROUP_new(meth);
111        if (ret == NULL)
112            return NULL;
113
114        if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx)) {
115            EC_GROUP_clear_free(ret);
116            return NULL;
117        }
118    }
119
120    return ret;
121}
122
123EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
124                                  const BIGNUM *b, BN_CTX *ctx)
125{
126    const EC_METHOD *meth;
127    EC_GROUP *ret;
128
129    meth = EC_GF2m_simple_method();
130
131    ret = EC_GROUP_new(meth);
132    if (ret == NULL)
133        return NULL;
134
135    if (!EC_GROUP_set_curve_GF2m(ret, p, a, b, ctx)) {
136        EC_GROUP_clear_free(ret);
137        return NULL;
138    }
139
140    return ret;
141}
142