xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ec_cvt.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ec/ec_cvt.c */
2*2139Sjp161948 /*
3*2139Sjp161948  * Originally written by Bodo Moeller for the OpenSSL project.
4*2139Sjp161948  */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  *
61*2139Sjp161948  * Portions of the attached software ("Contribution") are developed by
62*2139Sjp161948  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63*2139Sjp161948  *
64*2139Sjp161948  * The Contribution is licensed pursuant to the OpenSSL open source
65*2139Sjp161948  * license provided above.
66*2139Sjp161948  *
67*2139Sjp161948  * The elliptic curve binary polynomial software is originally written by
68*2139Sjp161948  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69*2139Sjp161948  *
70*2139Sjp161948  */
710Sstevel@tonic-gate 
72*2139Sjp161948 #include <openssl/err.h>
730Sstevel@tonic-gate #include "ec_lcl.h"
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
EC_GROUP_new_curve_GFp(const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)760Sstevel@tonic-gate EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
770Sstevel@tonic-gate 	{
780Sstevel@tonic-gate 	const EC_METHOD *meth;
790Sstevel@tonic-gate 	EC_GROUP *ret;
80*2139Sjp161948 
81*2139Sjp161948 	meth = EC_GFp_nist_method();
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	ret = EC_GROUP_new(meth);
840Sstevel@tonic-gate 	if (ret == NULL)
850Sstevel@tonic-gate 		return NULL;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx))
880Sstevel@tonic-gate 		{
89*2139Sjp161948 		unsigned long err;
90*2139Sjp161948 
91*2139Sjp161948 		err = ERR_peek_last_error();
92*2139Sjp161948 
93*2139Sjp161948 		if (!(ERR_GET_LIB(err) == ERR_LIB_EC &&
94*2139Sjp161948 			((ERR_GET_REASON(err) == EC_R_NOT_A_NIST_PRIME) ||
95*2139Sjp161948 			 (ERR_GET_REASON(err) == EC_R_NOT_A_SUPPORTED_NIST_PRIME))))
96*2139Sjp161948 			{
97*2139Sjp161948 			/* real error */
98*2139Sjp161948 
99*2139Sjp161948 			EC_GROUP_clear_free(ret);
100*2139Sjp161948 			return NULL;
101*2139Sjp161948 			}
102*2139Sjp161948 
103*2139Sjp161948 
104*2139Sjp161948 		/* not an actual error, we just cannot use EC_GFp_nist_method */
105*2139Sjp161948 
106*2139Sjp161948 		ERR_clear_error();
107*2139Sjp161948 
108*2139Sjp161948 		EC_GROUP_clear_free(ret);
109*2139Sjp161948 		meth = EC_GFp_mont_method();
110*2139Sjp161948 
111*2139Sjp161948 		ret = EC_GROUP_new(meth);
112*2139Sjp161948 		if (ret == NULL)
113*2139Sjp161948 			return NULL;
114*2139Sjp161948 
115*2139Sjp161948 		if (!EC_GROUP_set_curve_GFp(ret, p, a, b, ctx))
116*2139Sjp161948 			{
117*2139Sjp161948 			EC_GROUP_clear_free(ret);
118*2139Sjp161948 			return NULL;
119*2139Sjp161948 			}
120*2139Sjp161948 		}
121*2139Sjp161948 
122*2139Sjp161948 	return ret;
123*2139Sjp161948 	}
124*2139Sjp161948 
125*2139Sjp161948 
EC_GROUP_new_curve_GF2m(const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)126*2139Sjp161948 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
127*2139Sjp161948 	{
128*2139Sjp161948 	const EC_METHOD *meth;
129*2139Sjp161948 	EC_GROUP *ret;
130*2139Sjp161948 
131*2139Sjp161948 	meth = EC_GF2m_simple_method();
132*2139Sjp161948 
133*2139Sjp161948 	ret = EC_GROUP_new(meth);
134*2139Sjp161948 	if (ret == NULL)
135*2139Sjp161948 		return NULL;
136*2139Sjp161948 
137*2139Sjp161948 	if (!EC_GROUP_set_curve_GF2m(ret, p, a, b, ctx))
138*2139Sjp161948 		{
1390Sstevel@tonic-gate 		EC_GROUP_clear_free(ret);
1400Sstevel@tonic-gate 		return NULL;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	return ret;
1440Sstevel@tonic-gate 	}
145