1109998Smarkm/* crypto/ec/ecp_mont.c */ 2160814Ssimon/* 3160814Ssimon * Originally written by Bodo Moeller for the OpenSSL project. 4160814Ssimon */ 5109998Smarkm/* ==================================================================== 6109998Smarkm * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 7109998Smarkm * 8109998Smarkm * Redistribution and use in source and binary forms, with or without 9109998Smarkm * modification, are permitted provided that the following conditions 10109998Smarkm * are met: 11109998Smarkm * 12109998Smarkm * 1. Redistributions of source code must retain the above copyright 13280304Sjkim * notice, this list of conditions and the following disclaimer. 14109998Smarkm * 15109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 16109998Smarkm * notice, this list of conditions and the following disclaimer in 17109998Smarkm * the documentation and/or other materials provided with the 18109998Smarkm * distribution. 19109998Smarkm * 20109998Smarkm * 3. All advertising materials mentioning features or use of this 21109998Smarkm * software must display the following acknowledgment: 22109998Smarkm * "This product includes software developed by the OpenSSL Project 23109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24109998Smarkm * 25109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26109998Smarkm * endorse or promote products derived from this software without 27109998Smarkm * prior written permission. For written permission, please contact 28109998Smarkm * openssl-core@openssl.org. 29109998Smarkm * 30109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 31109998Smarkm * nor may "OpenSSL" appear in their names without prior written 32109998Smarkm * permission of the OpenSSL Project. 33109998Smarkm * 34109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 35109998Smarkm * acknowledgment: 36109998Smarkm * "This product includes software developed by the OpenSSL Project 37109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38109998Smarkm * 39109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 51109998Smarkm * ==================================================================== 52109998Smarkm * 53109998Smarkm * This product includes cryptographic software written by Eric Young 54109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 55109998Smarkm * Hudson (tjh@cryptsoft.com). 56109998Smarkm * 57109998Smarkm */ 58160814Ssimon/* ==================================================================== 59160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60160814Ssimon * Portions of this software developed by SUN MICROSYSTEMS, INC., 61160814Ssimon * and contributed to the OpenSSL project. 62160814Ssimon */ 63109998Smarkm 64109998Smarkm#include <openssl/err.h> 65109998Smarkm 66238405Sjkim#ifdef OPENSSL_FIPS 67280304Sjkim# include <openssl/fips.h> 68238405Sjkim#endif 69238405Sjkim 70109998Smarkm#include "ec_lcl.h" 71109998Smarkm 72109998Smarkmconst EC_METHOD *EC_GFp_mont_method(void) 73280304Sjkim{ 74280304Sjkim static const EC_METHOD ret = { 75280304Sjkim EC_FLAGS_DEFAULT_OCT, 76280304Sjkim NID_X9_62_prime_field, 77280304Sjkim ec_GFp_mont_group_init, 78280304Sjkim ec_GFp_mont_group_finish, 79280304Sjkim ec_GFp_mont_group_clear_finish, 80280304Sjkim ec_GFp_mont_group_copy, 81280304Sjkim ec_GFp_mont_group_set_curve, 82280304Sjkim ec_GFp_simple_group_get_curve, 83280304Sjkim ec_GFp_simple_group_get_degree, 84280304Sjkim ec_GFp_simple_group_check_discriminant, 85280304Sjkim ec_GFp_simple_point_init, 86280304Sjkim ec_GFp_simple_point_finish, 87280304Sjkim ec_GFp_simple_point_clear_finish, 88280304Sjkim ec_GFp_simple_point_copy, 89280304Sjkim ec_GFp_simple_point_set_to_infinity, 90280304Sjkim ec_GFp_simple_set_Jprojective_coordinates_GFp, 91280304Sjkim ec_GFp_simple_get_Jprojective_coordinates_GFp, 92280304Sjkim ec_GFp_simple_point_set_affine_coordinates, 93280304Sjkim ec_GFp_simple_point_get_affine_coordinates, 94280304Sjkim 0, 0, 0, 95280304Sjkim ec_GFp_simple_add, 96280304Sjkim ec_GFp_simple_dbl, 97280304Sjkim ec_GFp_simple_invert, 98280304Sjkim ec_GFp_simple_is_at_infinity, 99280304Sjkim ec_GFp_simple_is_on_curve, 100280304Sjkim ec_GFp_simple_cmp, 101280304Sjkim ec_GFp_simple_make_affine, 102280304Sjkim ec_GFp_simple_points_make_affine, 103280304Sjkim 0 /* mul */ , 104280304Sjkim 0 /* precompute_mult */ , 105280304Sjkim 0 /* have_precompute_mult */ , 106280304Sjkim ec_GFp_mont_field_mul, 107280304Sjkim ec_GFp_mont_field_sqr, 108280304Sjkim 0 /* field_div */ , 109280304Sjkim ec_GFp_mont_field_encode, 110280304Sjkim ec_GFp_mont_field_decode, 111280304Sjkim ec_GFp_mont_field_set_to_one 112280304Sjkim }; 113109998Smarkm 114273149Sjkim#ifdef OPENSSL_FIPS 115280304Sjkim if (FIPS_mode()) 116280304Sjkim return fips_ec_gfp_mont_method(); 117273149Sjkim#endif 118273149Sjkim 119280304Sjkim return &ret; 120280304Sjkim} 121109998Smarkm 122109998Smarkmint ec_GFp_mont_group_init(EC_GROUP *group) 123280304Sjkim{ 124280304Sjkim int ok; 125109998Smarkm 126280304Sjkim ok = ec_GFp_simple_group_init(group); 127280304Sjkim group->field_data1 = NULL; 128280304Sjkim group->field_data2 = NULL; 129280304Sjkim return ok; 130280304Sjkim} 131109998Smarkm 132109998Smarkmvoid ec_GFp_mont_group_finish(EC_GROUP *group) 133280304Sjkim{ 134280304Sjkim if (group->field_data1 != NULL) { 135280304Sjkim BN_MONT_CTX_free(group->field_data1); 136280304Sjkim group->field_data1 = NULL; 137280304Sjkim } 138280304Sjkim if (group->field_data2 != NULL) { 139280304Sjkim BN_free(group->field_data2); 140280304Sjkim group->field_data2 = NULL; 141280304Sjkim } 142280304Sjkim ec_GFp_simple_group_finish(group); 143280304Sjkim} 144109998Smarkm 145109998Smarkmvoid ec_GFp_mont_group_clear_finish(EC_GROUP *group) 146280304Sjkim{ 147280304Sjkim if (group->field_data1 != NULL) { 148280304Sjkim BN_MONT_CTX_free(group->field_data1); 149280304Sjkim group->field_data1 = NULL; 150280304Sjkim } 151280304Sjkim if (group->field_data2 != NULL) { 152280304Sjkim BN_clear_free(group->field_data2); 153280304Sjkim group->field_data2 = NULL; 154280304Sjkim } 155280304Sjkim ec_GFp_simple_group_clear_finish(group); 156280304Sjkim} 157109998Smarkm 158109998Smarkmint ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) 159280304Sjkim{ 160280304Sjkim if (dest->field_data1 != NULL) { 161280304Sjkim BN_MONT_CTX_free(dest->field_data1); 162280304Sjkim dest->field_data1 = NULL; 163280304Sjkim } 164280304Sjkim if (dest->field_data2 != NULL) { 165280304Sjkim BN_clear_free(dest->field_data2); 166280304Sjkim dest->field_data2 = NULL; 167280304Sjkim } 168109998Smarkm 169280304Sjkim if (!ec_GFp_simple_group_copy(dest, src)) 170280304Sjkim return 0; 171109998Smarkm 172280304Sjkim if (src->field_data1 != NULL) { 173280304Sjkim dest->field_data1 = BN_MONT_CTX_new(); 174280304Sjkim if (dest->field_data1 == NULL) 175280304Sjkim return 0; 176280304Sjkim if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) 177280304Sjkim goto err; 178280304Sjkim } 179280304Sjkim if (src->field_data2 != NULL) { 180280304Sjkim dest->field_data2 = BN_dup(src->field_data2); 181280304Sjkim if (dest->field_data2 == NULL) 182280304Sjkim goto err; 183280304Sjkim } 184109998Smarkm 185280304Sjkim return 1; 186109998Smarkm 187109998Smarkm err: 188280304Sjkim if (dest->field_data1 != NULL) { 189280304Sjkim BN_MONT_CTX_free(dest->field_data1); 190280304Sjkim dest->field_data1 = NULL; 191280304Sjkim } 192280304Sjkim return 0; 193280304Sjkim} 194109998Smarkm 195280304Sjkimint ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, 196280304Sjkim const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 197280304Sjkim{ 198280304Sjkim BN_CTX *new_ctx = NULL; 199280304Sjkim BN_MONT_CTX *mont = NULL; 200280304Sjkim BIGNUM *one = NULL; 201280304Sjkim int ret = 0; 202109998Smarkm 203280304Sjkim if (group->field_data1 != NULL) { 204280304Sjkim BN_MONT_CTX_free(group->field_data1); 205280304Sjkim group->field_data1 = NULL; 206280304Sjkim } 207280304Sjkim if (group->field_data2 != NULL) { 208280304Sjkim BN_free(group->field_data2); 209280304Sjkim group->field_data2 = NULL; 210280304Sjkim } 211160814Ssimon 212280304Sjkim if (ctx == NULL) { 213280304Sjkim ctx = new_ctx = BN_CTX_new(); 214280304Sjkim if (ctx == NULL) 215280304Sjkim return 0; 216280304Sjkim } 217160814Ssimon 218280304Sjkim mont = BN_MONT_CTX_new(); 219280304Sjkim if (mont == NULL) 220280304Sjkim goto err; 221280304Sjkim if (!BN_MONT_CTX_set(mont, p, ctx)) { 222280304Sjkim ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB); 223280304Sjkim goto err; 224280304Sjkim } 225280304Sjkim one = BN_new(); 226280304Sjkim if (one == NULL) 227280304Sjkim goto err; 228280304Sjkim if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) 229280304Sjkim goto err; 230160814Ssimon 231280304Sjkim group->field_data1 = mont; 232280304Sjkim mont = NULL; 233280304Sjkim group->field_data2 = one; 234280304Sjkim one = NULL; 235160814Ssimon 236280304Sjkim ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); 237160814Ssimon 238280304Sjkim if (!ret) { 239280304Sjkim BN_MONT_CTX_free(group->field_data1); 240280304Sjkim group->field_data1 = NULL; 241280304Sjkim BN_free(group->field_data2); 242280304Sjkim group->field_data2 = NULL; 243280304Sjkim } 244160814Ssimon 245160814Ssimon err: 246280304Sjkim if (new_ctx != NULL) 247280304Sjkim BN_CTX_free(new_ctx); 248280304Sjkim if (mont != NULL) 249280304Sjkim BN_MONT_CTX_free(mont); 250280304Sjkim return ret; 251280304Sjkim} 252160814Ssimon 253280304Sjkimint ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, 254280304Sjkim const BIGNUM *b, BN_CTX *ctx) 255280304Sjkim{ 256280304Sjkim if (group->field_data1 == NULL) { 257280304Sjkim ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED); 258280304Sjkim return 0; 259280304Sjkim } 260160814Ssimon 261280304Sjkim return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx); 262280304Sjkim} 263109998Smarkm 264280304Sjkimint ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, 265280304Sjkim BN_CTX *ctx) 266280304Sjkim{ 267280304Sjkim if (group->field_data1 == NULL) { 268280304Sjkim ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED); 269280304Sjkim return 0; 270280304Sjkim } 271109998Smarkm 272280304Sjkim return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx); 273280304Sjkim} 274109998Smarkm 275280304Sjkimint ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, 276280304Sjkim const BIGNUM *a, BN_CTX *ctx) 277280304Sjkim{ 278280304Sjkim if (group->field_data1 == NULL) { 279280304Sjkim ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED); 280280304Sjkim return 0; 281280304Sjkim } 282109998Smarkm 283280304Sjkim return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx); 284280304Sjkim} 285109998Smarkm 286280304Sjkimint ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, 287280304Sjkim const BIGNUM *a, BN_CTX *ctx) 288280304Sjkim{ 289280304Sjkim if (group->field_data1 == NULL) { 290280304Sjkim ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED); 291280304Sjkim return 0; 292280304Sjkim } 293109998Smarkm 294280304Sjkim return BN_from_montgomery(r, a, group->field_data1, ctx); 295280304Sjkim} 296109998Smarkm 297280304Sjkimint ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, 298280304Sjkim BN_CTX *ctx) 299280304Sjkim{ 300280304Sjkim if (group->field_data2 == NULL) { 301280304Sjkim ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED); 302280304Sjkim return 0; 303280304Sjkim } 304109998Smarkm 305280304Sjkim if (!BN_copy(r, group->field_data2)) 306280304Sjkim return 0; 307280304Sjkim return 1; 308280304Sjkim} 309