1109998Smarkm/* crypto/ec/ecp_nist.c */ 2160814Ssimon/* 3160814Ssimon * Written by Nils Larsch for the OpenSSL project. 4160814Ssimon */ 5109998Smarkm/* ==================================================================== 6160814Ssimon * Copyright (c) 1998-2003 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 13296341Sdelphij * 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 64160814Ssimon#include <limits.h> 65160814Ssimon 66160814Ssimon#include <openssl/err.h> 67160814Ssimon#include <openssl/obj_mac.h> 68109998Smarkm#include "ec_lcl.h" 69109998Smarkm 70238405Sjkim#ifdef OPENSSL_FIPS 71296341Sdelphij# include <openssl/fips.h> 72238405Sjkim#endif 73238405Sjkim 74109998Smarkmconst EC_METHOD *EC_GFp_nist_method(void) 75296341Sdelphij{ 76296341Sdelphij static const EC_METHOD ret = { 77296341Sdelphij EC_FLAGS_DEFAULT_OCT, 78296341Sdelphij NID_X9_62_prime_field, 79296341Sdelphij ec_GFp_simple_group_init, 80296341Sdelphij ec_GFp_simple_group_finish, 81296341Sdelphij ec_GFp_simple_group_clear_finish, 82296341Sdelphij ec_GFp_nist_group_copy, 83296341Sdelphij ec_GFp_nist_group_set_curve, 84296341Sdelphij ec_GFp_simple_group_get_curve, 85296341Sdelphij ec_GFp_simple_group_get_degree, 86296341Sdelphij ec_GFp_simple_group_check_discriminant, 87296341Sdelphij ec_GFp_simple_point_init, 88296341Sdelphij ec_GFp_simple_point_finish, 89296341Sdelphij ec_GFp_simple_point_clear_finish, 90296341Sdelphij ec_GFp_simple_point_copy, 91296341Sdelphij ec_GFp_simple_point_set_to_infinity, 92296341Sdelphij ec_GFp_simple_set_Jprojective_coordinates_GFp, 93296341Sdelphij ec_GFp_simple_get_Jprojective_coordinates_GFp, 94296341Sdelphij ec_GFp_simple_point_set_affine_coordinates, 95296341Sdelphij ec_GFp_simple_point_get_affine_coordinates, 96296341Sdelphij 0, 0, 0, 97296341Sdelphij ec_GFp_simple_add, 98296341Sdelphij ec_GFp_simple_dbl, 99296341Sdelphij ec_GFp_simple_invert, 100296341Sdelphij ec_GFp_simple_is_at_infinity, 101296341Sdelphij ec_GFp_simple_is_on_curve, 102296341Sdelphij ec_GFp_simple_cmp, 103296341Sdelphij ec_GFp_simple_make_affine, 104296341Sdelphij ec_GFp_simple_points_make_affine, 105296341Sdelphij 0 /* mul */ , 106296341Sdelphij 0 /* precompute_mult */ , 107296341Sdelphij 0 /* have_precompute_mult */ , 108296341Sdelphij ec_GFp_nist_field_mul, 109296341Sdelphij ec_GFp_nist_field_sqr, 110296341Sdelphij 0 /* field_div */ , 111296341Sdelphij 0 /* field_encode */ , 112296341Sdelphij 0 /* field_decode */ , 113296341Sdelphij 0 /* field_set_to_one */ 114296341Sdelphij }; 115109998Smarkm 116273399Sdelphij#ifdef OPENSSL_FIPS 117296341Sdelphij if (FIPS_mode()) 118296341Sdelphij return fips_ec_gfp_nist_method(); 119273399Sdelphij#endif 120273399Sdelphij 121296341Sdelphij return &ret; 122296341Sdelphij} 123160814Ssimon 124160814Ssimonint ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src) 125296341Sdelphij{ 126296341Sdelphij dest->field_mod_func = src->field_mod_func; 127109998Smarkm 128296341Sdelphij return ec_GFp_simple_group_copy(dest, src); 129296341Sdelphij} 130109998Smarkm 131160814Ssimonint ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, 132296341Sdelphij const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 133296341Sdelphij{ 134296341Sdelphij int ret = 0; 135296341Sdelphij BN_CTX *new_ctx = NULL; 136296341Sdelphij BIGNUM *tmp_bn; 137109998Smarkm 138296341Sdelphij if (ctx == NULL) 139296341Sdelphij if ((ctx = new_ctx = BN_CTX_new()) == NULL) 140296341Sdelphij return 0; 141109998Smarkm 142296341Sdelphij BN_CTX_start(ctx); 143296341Sdelphij if ((tmp_bn = BN_CTX_get(ctx)) == NULL) 144296341Sdelphij goto err; 145109998Smarkm 146296341Sdelphij if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) 147296341Sdelphij group->field_mod_func = BN_nist_mod_192; 148296341Sdelphij else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0) 149296341Sdelphij group->field_mod_func = BN_nist_mod_224; 150296341Sdelphij else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0) 151296341Sdelphij group->field_mod_func = BN_nist_mod_256; 152296341Sdelphij else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0) 153296341Sdelphij group->field_mod_func = BN_nist_mod_384; 154296341Sdelphij else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) 155296341Sdelphij group->field_mod_func = BN_nist_mod_521; 156296341Sdelphij else { 157296341Sdelphij ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME); 158296341Sdelphij goto err; 159296341Sdelphij } 160109998Smarkm 161296341Sdelphij ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); 162296341Sdelphij 163160814Ssimon err: 164296341Sdelphij BN_CTX_end(ctx); 165296341Sdelphij if (new_ctx != NULL) 166296341Sdelphij BN_CTX_free(new_ctx); 167296341Sdelphij return ret; 168296341Sdelphij} 169109998Smarkm 170160814Ssimonint ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, 171296341Sdelphij const BIGNUM *b, BN_CTX *ctx) 172296341Sdelphij{ 173296341Sdelphij int ret = 0; 174296341Sdelphij BN_CTX *ctx_new = NULL; 175109998Smarkm 176296341Sdelphij if (!group || !r || !a || !b) { 177296341Sdelphij ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER); 178296341Sdelphij goto err; 179296341Sdelphij } 180296341Sdelphij if (!ctx) 181296341Sdelphij if ((ctx_new = ctx = BN_CTX_new()) == NULL) 182296341Sdelphij goto err; 183109998Smarkm 184296341Sdelphij if (!BN_mul(r, a, b, ctx)) 185296341Sdelphij goto err; 186296341Sdelphij if (!group->field_mod_func(r, r, &group->field, ctx)) 187296341Sdelphij goto err; 188109998Smarkm 189296341Sdelphij ret = 1; 190296341Sdelphij err: 191296341Sdelphij if (ctx_new) 192296341Sdelphij BN_CTX_free(ctx_new); 193296341Sdelphij return ret; 194296341Sdelphij} 195109998Smarkm 196160814Ssimonint ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, 197296341Sdelphij BN_CTX *ctx) 198296341Sdelphij{ 199296341Sdelphij int ret = 0; 200296341Sdelphij BN_CTX *ctx_new = NULL; 201160814Ssimon 202296341Sdelphij if (!group || !r || !a) { 203296341Sdelphij ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER); 204296341Sdelphij goto err; 205296341Sdelphij } 206296341Sdelphij if (!ctx) 207296341Sdelphij if ((ctx_new = ctx = BN_CTX_new()) == NULL) 208296341Sdelphij goto err; 209160814Ssimon 210296341Sdelphij if (!BN_sqr(r, a, ctx)) 211296341Sdelphij goto err; 212296341Sdelphij if (!group->field_mod_func(r, r, &group->field, ctx)) 213296341Sdelphij goto err; 214160814Ssimon 215296341Sdelphij ret = 1; 216296341Sdelphij err: 217296341Sdelphij if (ctx_new) 218296341Sdelphij BN_CTX_free(ctx_new); 219296341Sdelphij return ret; 220296341Sdelphij} 221