1160814Ssimon/* crypto/ec/ec_check.c */ 2160814Ssimon/* ==================================================================== 3160814Ssimon * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 4160814Ssimon * 5160814Ssimon * Redistribution and use in source and binary forms, with or without 6160814Ssimon * modification, are permitted provided that the following conditions 7160814Ssimon * are met: 8160814Ssimon * 9160814Ssimon * 1. Redistributions of source code must retain the above copyright 10280297Sjkim * notice, this list of conditions and the following disclaimer. 11160814Ssimon * 12160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 13160814Ssimon * notice, this list of conditions and the following disclaimer in 14160814Ssimon * the documentation and/or other materials provided with the 15160814Ssimon * distribution. 16160814Ssimon * 17160814Ssimon * 3. All advertising materials mentioning features or use of this 18160814Ssimon * software must display the following acknowledgment: 19160814Ssimon * "This product includes software developed by the OpenSSL Project 20160814Ssimon * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21160814Ssimon * 22160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23160814Ssimon * endorse or promote products derived from this software without 24160814Ssimon * prior written permission. For written permission, please contact 25160814Ssimon * openssl-core@openssl.org. 26160814Ssimon * 27160814Ssimon * 5. Products derived from this software may not be called "OpenSSL" 28160814Ssimon * nor may "OpenSSL" appear in their names without prior written 29160814Ssimon * permission of the OpenSSL Project. 30160814Ssimon * 31160814Ssimon * 6. Redistributions of any form whatsoever must retain the following 32160814Ssimon * acknowledgment: 33160814Ssimon * "This product includes software developed by the OpenSSL Project 34160814Ssimon * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35160814Ssimon * 36160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39160814Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 48160814Ssimon * ==================================================================== 49160814Ssimon * 50160814Ssimon * This product includes cryptographic software written by Eric Young 51160814Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 52160814Ssimon * Hudson (tjh@cryptsoft.com). 53160814Ssimon * 54160814Ssimon */ 55160814Ssimon 56160814Ssimon#include "ec_lcl.h" 57160814Ssimon#include <openssl/err.h> 58160814Ssimon 59160814Ssimonint EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx) 60280297Sjkim{ 61280297Sjkim int ret = 0; 62280297Sjkim BIGNUM *order; 63280297Sjkim BN_CTX *new_ctx = NULL; 64280297Sjkim EC_POINT *point = NULL; 65160814Ssimon 66280297Sjkim if (ctx == NULL) { 67280297Sjkim ctx = new_ctx = BN_CTX_new(); 68280297Sjkim if (ctx == NULL) { 69280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE); 70280297Sjkim goto err; 71280297Sjkim } 72280297Sjkim } 73280297Sjkim BN_CTX_start(ctx); 74280297Sjkim if ((order = BN_CTX_get(ctx)) == NULL) 75280297Sjkim goto err; 76160814Ssimon 77280297Sjkim /* check the discriminant */ 78280297Sjkim if (!EC_GROUP_check_discriminant(group, ctx)) { 79280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO); 80280297Sjkim goto err; 81280297Sjkim } 82160814Ssimon 83280297Sjkim /* check the generator */ 84280297Sjkim if (group->generator == NULL) { 85280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR); 86280297Sjkim goto err; 87280297Sjkim } 88284283Sjkim if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) { 89280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE); 90280297Sjkim goto err; 91280297Sjkim } 92160814Ssimon 93280297Sjkim /* check the order of the generator */ 94280297Sjkim if ((point = EC_POINT_new(group)) == NULL) 95280297Sjkim goto err; 96280297Sjkim if (!EC_GROUP_get_order(group, order, ctx)) 97280297Sjkim goto err; 98280297Sjkim if (BN_is_zero(order)) { 99280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER); 100280297Sjkim goto err; 101280297Sjkim } 102160814Ssimon 103280297Sjkim if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx)) 104280297Sjkim goto err; 105280297Sjkim if (!EC_POINT_is_at_infinity(group, point)) { 106280297Sjkim ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER); 107280297Sjkim goto err; 108280297Sjkim } 109160814Ssimon 110280297Sjkim ret = 1; 111280297Sjkim 112280297Sjkim err: 113280297Sjkim if (ctx != NULL) 114280297Sjkim BN_CTX_end(ctx); 115280297Sjkim if (new_ctx != NULL) 116280297Sjkim BN_CTX_free(new_ctx); 117280297Sjkim if (point) 118280297Sjkim EC_POINT_free(point); 119280297Sjkim return ret; 120280297Sjkim} 121