10Sstevel@tonic-gate /* crypto/ec/ecp_mont.c */
2*2139Sjp161948 /*
3*2139Sjp161948 * Originally written by Bodo Moeller for the OpenSSL project.
4*2139Sjp161948 */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1998-2001 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
640Sstevel@tonic-gate #include <openssl/err.h>
650Sstevel@tonic-gate
660Sstevel@tonic-gate #include "ec_lcl.h"
670Sstevel@tonic-gate
680Sstevel@tonic-gate
EC_GFp_mont_method(void)690Sstevel@tonic-gate const EC_METHOD *EC_GFp_mont_method(void)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate static const EC_METHOD ret = {
72*2139Sjp161948 NID_X9_62_prime_field,
730Sstevel@tonic-gate ec_GFp_mont_group_init,
740Sstevel@tonic-gate ec_GFp_mont_group_finish,
750Sstevel@tonic-gate ec_GFp_mont_group_clear_finish,
760Sstevel@tonic-gate ec_GFp_mont_group_copy,
77*2139Sjp161948 ec_GFp_mont_group_set_curve,
78*2139Sjp161948 ec_GFp_simple_group_get_curve,
79*2139Sjp161948 ec_GFp_simple_group_get_degree,
80*2139Sjp161948 ec_GFp_simple_group_check_discriminant,
810Sstevel@tonic-gate ec_GFp_simple_point_init,
820Sstevel@tonic-gate ec_GFp_simple_point_finish,
830Sstevel@tonic-gate ec_GFp_simple_point_clear_finish,
840Sstevel@tonic-gate ec_GFp_simple_point_copy,
850Sstevel@tonic-gate ec_GFp_simple_point_set_to_infinity,
860Sstevel@tonic-gate ec_GFp_simple_set_Jprojective_coordinates_GFp,
870Sstevel@tonic-gate ec_GFp_simple_get_Jprojective_coordinates_GFp,
88*2139Sjp161948 ec_GFp_simple_point_set_affine_coordinates,
89*2139Sjp161948 ec_GFp_simple_point_get_affine_coordinates,
90*2139Sjp161948 ec_GFp_simple_set_compressed_coordinates,
910Sstevel@tonic-gate ec_GFp_simple_point2oct,
920Sstevel@tonic-gate ec_GFp_simple_oct2point,
930Sstevel@tonic-gate ec_GFp_simple_add,
940Sstevel@tonic-gate ec_GFp_simple_dbl,
950Sstevel@tonic-gate ec_GFp_simple_invert,
960Sstevel@tonic-gate ec_GFp_simple_is_at_infinity,
970Sstevel@tonic-gate ec_GFp_simple_is_on_curve,
980Sstevel@tonic-gate ec_GFp_simple_cmp,
990Sstevel@tonic-gate ec_GFp_simple_make_affine,
1000Sstevel@tonic-gate ec_GFp_simple_points_make_affine,
101*2139Sjp161948 0 /* mul */,
102*2139Sjp161948 0 /* precompute_mult */,
103*2139Sjp161948 0 /* have_precompute_mult */,
1040Sstevel@tonic-gate ec_GFp_mont_field_mul,
1050Sstevel@tonic-gate ec_GFp_mont_field_sqr,
106*2139Sjp161948 0 /* field_div */,
1070Sstevel@tonic-gate ec_GFp_mont_field_encode,
1080Sstevel@tonic-gate ec_GFp_mont_field_decode,
1090Sstevel@tonic-gate ec_GFp_mont_field_set_to_one };
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate return &ret;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate
ec_GFp_mont_group_init(EC_GROUP * group)1150Sstevel@tonic-gate int ec_GFp_mont_group_init(EC_GROUP *group)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate int ok;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate ok = ec_GFp_simple_group_init(group);
1200Sstevel@tonic-gate group->field_data1 = NULL;
1210Sstevel@tonic-gate group->field_data2 = NULL;
1220Sstevel@tonic-gate return ok;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate
ec_GFp_mont_group_finish(EC_GROUP * group)1260Sstevel@tonic-gate void ec_GFp_mont_group_finish(EC_GROUP *group)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate if (group->field_data1 != NULL)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate BN_MONT_CTX_free(group->field_data1);
1310Sstevel@tonic-gate group->field_data1 = NULL;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate if (group->field_data2 != NULL)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate BN_free(group->field_data2);
1360Sstevel@tonic-gate group->field_data2 = NULL;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate ec_GFp_simple_group_finish(group);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate
ec_GFp_mont_group_clear_finish(EC_GROUP * group)1420Sstevel@tonic-gate void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate if (group->field_data1 != NULL)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate BN_MONT_CTX_free(group->field_data1);
1470Sstevel@tonic-gate group->field_data1 = NULL;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate if (group->field_data2 != NULL)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate BN_clear_free(group->field_data2);
1520Sstevel@tonic-gate group->field_data2 = NULL;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate ec_GFp_simple_group_clear_finish(group);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate
ec_GFp_mont_group_copy(EC_GROUP * dest,const EC_GROUP * src)1580Sstevel@tonic-gate int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate if (dest->field_data1 != NULL)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate BN_MONT_CTX_free(dest->field_data1);
1630Sstevel@tonic-gate dest->field_data1 = NULL;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate if (dest->field_data2 != NULL)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate BN_clear_free(dest->field_data2);
1680Sstevel@tonic-gate dest->field_data2 = NULL;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (!ec_GFp_simple_group_copy(dest, src)) return 0;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (src->field_data1 != NULL)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate dest->field_data1 = BN_MONT_CTX_new();
1760Sstevel@tonic-gate if (dest->field_data1 == NULL) return 0;
1770Sstevel@tonic-gate if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) goto err;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate if (src->field_data2 != NULL)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate dest->field_data2 = BN_dup(src->field_data2);
1820Sstevel@tonic-gate if (dest->field_data2 == NULL) goto err;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate return 1;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate err:
1880Sstevel@tonic-gate if (dest->field_data1 != NULL)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate BN_MONT_CTX_free(dest->field_data1);
1910Sstevel@tonic-gate dest->field_data1 = NULL;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate return 0;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate
ec_GFp_mont_group_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)197*2139Sjp161948 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
198*2139Sjp161948 {
199*2139Sjp161948 BN_CTX *new_ctx = NULL;
200*2139Sjp161948 BN_MONT_CTX *mont = NULL;
201*2139Sjp161948 BIGNUM *one = NULL;
202*2139Sjp161948 int ret = 0;
203*2139Sjp161948
204*2139Sjp161948 if (group->field_data1 != NULL)
205*2139Sjp161948 {
206*2139Sjp161948 BN_MONT_CTX_free(group->field_data1);
207*2139Sjp161948 group->field_data1 = NULL;
208*2139Sjp161948 }
209*2139Sjp161948 if (group->field_data2 != NULL)
210*2139Sjp161948 {
211*2139Sjp161948 BN_free(group->field_data2);
212*2139Sjp161948 group->field_data2 = NULL;
213*2139Sjp161948 }
214*2139Sjp161948
215*2139Sjp161948 if (ctx == NULL)
216*2139Sjp161948 {
217*2139Sjp161948 ctx = new_ctx = BN_CTX_new();
218*2139Sjp161948 if (ctx == NULL)
219*2139Sjp161948 return 0;
220*2139Sjp161948 }
221*2139Sjp161948
222*2139Sjp161948 mont = BN_MONT_CTX_new();
223*2139Sjp161948 if (mont == NULL) goto err;
224*2139Sjp161948 if (!BN_MONT_CTX_set(mont, p, ctx))
225*2139Sjp161948 {
226*2139Sjp161948 ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
227*2139Sjp161948 goto err;
228*2139Sjp161948 }
229*2139Sjp161948 one = BN_new();
230*2139Sjp161948 if (one == NULL) goto err;
231*2139Sjp161948 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) goto err;
232*2139Sjp161948
233*2139Sjp161948 group->field_data1 = mont;
234*2139Sjp161948 mont = NULL;
235*2139Sjp161948 group->field_data2 = one;
236*2139Sjp161948 one = NULL;
237*2139Sjp161948
238*2139Sjp161948 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
239*2139Sjp161948
240*2139Sjp161948 if (!ret)
241*2139Sjp161948 {
242*2139Sjp161948 BN_MONT_CTX_free(group->field_data1);
243*2139Sjp161948 group->field_data1 = NULL;
244*2139Sjp161948 BN_free(group->field_data2);
245*2139Sjp161948 group->field_data2 = NULL;
246*2139Sjp161948 }
247*2139Sjp161948
248*2139Sjp161948 err:
249*2139Sjp161948 if (new_ctx != NULL)
250*2139Sjp161948 BN_CTX_free(new_ctx);
251*2139Sjp161948 if (mont != NULL)
252*2139Sjp161948 BN_MONT_CTX_free(mont);
253*2139Sjp161948 return ret;
254*2139Sjp161948 }
255*2139Sjp161948
256*2139Sjp161948
ec_GFp_mont_field_mul(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)2570Sstevel@tonic-gate int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate if (group->field_data1 == NULL)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
2620Sstevel@tonic-gate return 0;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate
ec_GFp_mont_field_sqr(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,BN_CTX * ctx)2690Sstevel@tonic-gate int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate if (group->field_data1 == NULL)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
2740Sstevel@tonic-gate return 0;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate
ec_GFp_mont_field_encode(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,BN_CTX * ctx)2810Sstevel@tonic-gate int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate if (group->field_data1 == NULL)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
2860Sstevel@tonic-gate return 0;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate
ec_GFp_mont_field_decode(const EC_GROUP * group,BIGNUM * r,const BIGNUM * a,BN_CTX * ctx)2930Sstevel@tonic-gate int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate if (group->field_data1 == NULL)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
2980Sstevel@tonic-gate return 0;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate return BN_from_montgomery(r, a, group->field_data1, ctx);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate
ec_GFp_mont_field_set_to_one(const EC_GROUP * group,BIGNUM * r,BN_CTX * ctx)3050Sstevel@tonic-gate int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate if (group->field_data2 == NULL)
3080Sstevel@tonic-gate {
309*2139Sjp161948 ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
3100Sstevel@tonic-gate return 0;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate if (!BN_copy(r, group->field_data2)) return 0;
3140Sstevel@tonic-gate return 1;
3150Sstevel@tonic-gate }
316