2016-05-17 18:51:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2002-03-20 10:18:07 +00:00
|
|
|
*
|
2016-05-17 18:51:04 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2002-03-20 10:18:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ec_lcl.h"
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2016-01-31 16:34:07 +00:00
|
|
|
const BIGNUM *order;
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_CTX *new_ctx = NULL;
|
|
|
|
EC_POINT *point = NULL;
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2016-02-01 18:15:57 +00:00
|
|
|
/* Custom curves assumed to be correct */
|
|
|
|
if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
|
|
|
|
return 1;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ctx == NULL) {
|
|
|
|
ctx = new_ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* check the discriminant */
|
|
|
|
if (!EC_GROUP_check_discriminant(group, ctx)) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* check the generator */
|
|
|
|
if (group->generator == NULL) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-06-04 13:22:00 +00:00
|
|
|
if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* check the order of the generator */
|
|
|
|
if ((point = EC_POINT_new(group)) == NULL)
|
|
|
|
goto err;
|
2016-01-31 16:34:07 +00:00
|
|
|
order = EC_GROUP_get0_order(group);
|
|
|
|
if (order == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
if (BN_is_zero(order)) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))
|
|
|
|
goto err;
|
|
|
|
if (!EC_POINT_is_at_infinity(group, point)) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 10:18:07 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_CTX_free(new_ctx);
|
2015-03-25 22:35:24 +00:00
|
|
|
EC_POINT_free(point);
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|