|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include "ec_local.h"
|
|
Packit Service |
084de1 |
#include <openssl/err.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
const BIGNUM *order;
|
|
Packit Service |
084de1 |
BN_CTX *new_ctx = NULL;
|
|
Packit Service |
084de1 |
EC_POINT *point = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Custom curves assumed to be correct */
|
|
Packit Service |
084de1 |
if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ctx == NULL) {
|
|
Packit Service |
084de1 |
ctx = new_ctx = BN_CTX_new();
|
|
Packit Service |
084de1 |
if (ctx == NULL) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* check the discriminant */
|
|
Packit Service |
084de1 |
if (!EC_GROUP_check_discriminant(group, ctx)) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* check the generator */
|
|
Packit Service |
084de1 |
if (group->generator == NULL) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* check the order of the generator */
|
|
Packit Service |
084de1 |
if ((point = EC_POINT_new(group)) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
order = EC_GROUP_get0_order(group);
|
|
Packit Service |
084de1 |
if (order == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (BN_is_zero(order)) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!EC_POINT_is_at_infinity(group, point)) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
BN_CTX_free(new_ctx);
|
|
Packit Service |
084de1 |
EC_POINT_free(point);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|