10Sstevel@tonic-gate /* crypto/ec/ecp_nist.c */
2*2139Sjp161948 /*
3*2139Sjp161948 * Written by Nils Larsch for the OpenSSL project.
4*2139Sjp161948 */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948 * Copyright (c) 1998-2003 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 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
61*2139Sjp161948 * and contributed to the OpenSSL project.
62*2139Sjp161948 */
630Sstevel@tonic-gate
64*2139Sjp161948 #include <limits.h>
65*2139Sjp161948
66*2139Sjp161948 #include <openssl/err.h>
67*2139Sjp161948 #include <openssl/obj_mac.h>
680Sstevel@tonic-gate #include "ec_lcl.h"
690Sstevel@tonic-gate
EC_GFp_nist_method(void)700Sstevel@tonic-gate const EC_METHOD *EC_GFp_nist_method(void)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate static const EC_METHOD ret = {
73*2139Sjp161948 NID_X9_62_prime_field,
74*2139Sjp161948 ec_GFp_simple_group_init,
75*2139Sjp161948 ec_GFp_simple_group_finish,
76*2139Sjp161948 ec_GFp_simple_group_clear_finish,
770Sstevel@tonic-gate ec_GFp_nist_group_copy,
78*2139Sjp161948 ec_GFp_nist_group_set_curve,
79*2139Sjp161948 ec_GFp_simple_group_get_curve,
80*2139Sjp161948 ec_GFp_simple_group_get_degree,
81*2139Sjp161948 ec_GFp_simple_group_check_discriminant,
820Sstevel@tonic-gate ec_GFp_simple_point_init,
830Sstevel@tonic-gate ec_GFp_simple_point_finish,
840Sstevel@tonic-gate ec_GFp_simple_point_clear_finish,
850Sstevel@tonic-gate ec_GFp_simple_point_copy,
860Sstevel@tonic-gate ec_GFp_simple_point_set_to_infinity,
870Sstevel@tonic-gate ec_GFp_simple_set_Jprojective_coordinates_GFp,
880Sstevel@tonic-gate ec_GFp_simple_get_Jprojective_coordinates_GFp,
89*2139Sjp161948 ec_GFp_simple_point_set_affine_coordinates,
90*2139Sjp161948 ec_GFp_simple_point_get_affine_coordinates,
91*2139Sjp161948 ec_GFp_simple_set_compressed_coordinates,
920Sstevel@tonic-gate ec_GFp_simple_point2oct,
930Sstevel@tonic-gate ec_GFp_simple_oct2point,
940Sstevel@tonic-gate ec_GFp_simple_add,
950Sstevel@tonic-gate ec_GFp_simple_dbl,
960Sstevel@tonic-gate ec_GFp_simple_invert,
970Sstevel@tonic-gate ec_GFp_simple_is_at_infinity,
980Sstevel@tonic-gate ec_GFp_simple_is_on_curve,
990Sstevel@tonic-gate ec_GFp_simple_cmp,
1000Sstevel@tonic-gate ec_GFp_simple_make_affine,
1010Sstevel@tonic-gate ec_GFp_simple_points_make_affine,
102*2139Sjp161948 0 /* mul */,
103*2139Sjp161948 0 /* precompute_mult */,
104*2139Sjp161948 0 /* have_precompute_mult */,
1050Sstevel@tonic-gate ec_GFp_nist_field_mul,
1060Sstevel@tonic-gate ec_GFp_nist_field_sqr,
107*2139Sjp161948 0 /* field_div */,
1080Sstevel@tonic-gate 0 /* field_encode */,
1090Sstevel@tonic-gate 0 /* field_decode */,
1100Sstevel@tonic-gate 0 /* field_set_to_one */ };
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate return &ret;
1130Sstevel@tonic-gate }
114*2139Sjp161948
115*2139Sjp161948 #if BN_BITS2 == 64
116*2139Sjp161948 #define NO_32_BIT_TYPE
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate
ec_GFp_nist_group_copy(EC_GROUP * dest,const EC_GROUP * src)119*2139Sjp161948 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
120*2139Sjp161948 {
121*2139Sjp161948 dest->field_mod_func = src->field_mod_func;
1220Sstevel@tonic-gate
123*2139Sjp161948 return ec_GFp_simple_group_copy(dest, src);
124*2139Sjp161948 }
125*2139Sjp161948
ec_GFp_nist_group_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)126*2139Sjp161948 int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
127*2139Sjp161948 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1280Sstevel@tonic-gate {
129*2139Sjp161948 int ret = 0;
130*2139Sjp161948 BN_CTX *new_ctx = NULL;
131*2139Sjp161948 BIGNUM *tmp_bn;
132*2139Sjp161948
133*2139Sjp161948 if (ctx == NULL)
134*2139Sjp161948 if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0;
135*2139Sjp161948
136*2139Sjp161948 BN_CTX_start(ctx);
137*2139Sjp161948 if ((tmp_bn = BN_CTX_get(ctx)) == NULL) goto err;
1380Sstevel@tonic-gate
139*2139Sjp161948 if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
140*2139Sjp161948 group->field_mod_func = BN_nist_mod_192;
141*2139Sjp161948 else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
142*2139Sjp161948 {
143*2139Sjp161948 #ifndef NO_32_BIT_TYPE
144*2139Sjp161948 group->field_mod_func = BN_nist_mod_224;
145*2139Sjp161948 #else
146*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_SUPPORTED_NIST_PRIME);
147*2139Sjp161948 goto err;
148*2139Sjp161948 #endif
149*2139Sjp161948 }
150*2139Sjp161948 else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
151*2139Sjp161948 {
152*2139Sjp161948 #ifndef NO_32_BIT_TYPE
153*2139Sjp161948 group->field_mod_func = BN_nist_mod_256;
154*2139Sjp161948 #else
155*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_SUPPORTED_NIST_PRIME);
156*2139Sjp161948 goto err;
157*2139Sjp161948 #endif
158*2139Sjp161948 }
159*2139Sjp161948 else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
160*2139Sjp161948 {
161*2139Sjp161948 #ifndef NO_32_BIT_TYPE
162*2139Sjp161948 group->field_mod_func = BN_nist_mod_384;
163*2139Sjp161948 #else
164*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_SUPPORTED_NIST_PRIME);
165*2139Sjp161948 goto err;
166*2139Sjp161948 #endif
167*2139Sjp161948 }
168*2139Sjp161948 else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
169*2139Sjp161948 /* this one works in the NO_32_BIT_TYPE case */
170*2139Sjp161948 group->field_mod_func = BN_nist_mod_521;
171*2139Sjp161948 else
172*2139Sjp161948 {
173*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
174*2139Sjp161948 goto err;
175*2139Sjp161948 }
176*2139Sjp161948
177*2139Sjp161948 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
178*2139Sjp161948
179*2139Sjp161948 err:
180*2139Sjp161948 BN_CTX_end(ctx);
181*2139Sjp161948 if (new_ctx != NULL)
182*2139Sjp161948 BN_CTX_free(new_ctx);
183*2139Sjp161948 return ret;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate
ec_GFp_nist_field_mul(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)187*2139Sjp161948 int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
188*2139Sjp161948 const BIGNUM *b, BN_CTX *ctx)
189*2139Sjp161948 {
190*2139Sjp161948 int ret=0;
191*2139Sjp161948 BN_CTX *ctx_new=NULL;
1920Sstevel@tonic-gate
193*2139Sjp161948 if (!group || !r || !a || !b)
194*2139Sjp161948 {
195*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
196*2139Sjp161948 goto err;
197*2139Sjp161948 }
198*2139Sjp161948 if (!ctx)
199*2139Sjp161948 if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err;
2000Sstevel@tonic-gate
201*2139Sjp161948 if (!BN_mul(r, a, b, ctx)) goto err;
202*2139Sjp161948 if (!group->field_mod_func(r, r, &group->field, ctx))
203*2139Sjp161948 goto err;
204*2139Sjp161948
205*2139Sjp161948 ret=1;
206*2139Sjp161948 err:
207*2139Sjp161948 if (ctx_new)
208*2139Sjp161948 BN_CTX_free(ctx_new);
209*2139Sjp161948 return ret;
210*2139Sjp161948 }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate
ec_GFp_nist_field_sqr(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,BN_CTX * ctx)213*2139Sjp161948 int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
214*2139Sjp161948 BN_CTX *ctx)
215*2139Sjp161948 {
216*2139Sjp161948 int ret=0;
217*2139Sjp161948 BN_CTX *ctx_new=NULL;
2180Sstevel@tonic-gate
219*2139Sjp161948 if (!group || !r || !a)
220*2139Sjp161948 {
221*2139Sjp161948 ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
222*2139Sjp161948 goto err;
223*2139Sjp161948 }
224*2139Sjp161948 if (!ctx)
225*2139Sjp161948 if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err;
2260Sstevel@tonic-gate
227*2139Sjp161948 if (!BN_sqr(r, a, ctx)) goto err;
228*2139Sjp161948 if (!group->field_mod_func(r, r, &group->field, ctx))
229*2139Sjp161948 goto err;
2300Sstevel@tonic-gate
231*2139Sjp161948 ret=1;
232*2139Sjp161948 err:
233*2139Sjp161948 if (ctx_new)
234*2139Sjp161948 BN_CTX_free(ctx_new);
235*2139Sjp161948 return ret;
236*2139Sjp161948 }
237