2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2019-03-21 18:09:02 +08:00
|
|
|
* Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
|
2002-03-20 18:18:07 +08:00
|
|
|
*
|
2018-12-06 20:38:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* 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 18:18:07 +08:00
|
|
|
*/
|
|
|
|
|
2020-01-28 13:14:18 +08:00
|
|
|
/*
|
|
|
|
* ECDSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ec_local.h"
|
2002-03-20 18:18:07 +08:00
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2019-07-04 00:30:03 +08:00
|
|
|
int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only,
|
|
|
|
BN_CTX *ctx)
|
2019-03-21 18:09:02 +08:00
|
|
|
{
|
2019-07-04 00:30:03 +08:00
|
|
|
int nid = NID_undef;
|
|
|
|
#ifndef FIPS_MODE
|
|
|
|
BN_CTX *new_ctx = NULL;
|
2019-03-21 18:09:02 +08:00
|
|
|
|
2019-07-04 00:30:03 +08:00
|
|
|
if (ctx == NULL) {
|
|
|
|
ctx = new_ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK_NAMED_CURVE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nid = ec_curve_nid_from_params(group, ctx);
|
2019-03-21 18:09:02 +08:00
|
|
|
if (nid > 0 && nist_only && EC_curve_nid2nist(nid) == NULL)
|
2019-04-02 08:55:00 +08:00
|
|
|
nid = NID_undef;
|
2019-07-04 00:30:03 +08:00
|
|
|
|
|
|
|
#ifndef FIPS_MODE
|
|
|
|
err:
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
#endif
|
2019-03-21 18:09:02 +08:00
|
|
|
return nid;
|
|
|
|
}
|
|
|
|
|
2002-03-20 18:18:07 +08:00
|
|
|
int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2019-06-05 06:24:16 +08:00
|
|
|
#ifdef FIPS_MODE
|
|
|
|
/*
|
|
|
|
* ECC domain parameter validation.
|
|
|
|
* See SP800-56A R3 5.5.2 "Assurances of Domain-Parameter Validity" Part 1b.
|
|
|
|
*/
|
2019-07-04 00:30:03 +08:00
|
|
|
return EC_GROUP_check_named_curve(group, 1, ctx) >= 0 ? 1 : 0;
|
2019-06-05 06:24:16 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
int ret = 0;
|
2016-02-01 00:34:07 +08:00
|
|
|
const BIGNUM *order;
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_CTX *new_ctx = NULL;
|
|
|
|
EC_POINT *point = NULL;
|
2002-03-20 18:18:07 +08:00
|
|
|
|
2019-03-21 18:09:02 +08:00
|
|
|
if (group == NULL || group->meth == NULL) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 02:15:57 +08:00
|
|
|
/* Custom curves assumed to be correct */
|
|
|
|
if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
|
|
|
|
return 1;
|
|
|
|
|
2015-01-22 11:40:55 +08: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 18:18:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08: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 18:18:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* check the generator */
|
|
|
|
if (group->generator == NULL) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-06-04 21:22:00 +08:00
|
|
|
if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 18:18:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* check the order of the generator */
|
|
|
|
if ((point = EC_POINT_new(group)) == NULL)
|
|
|
|
goto err;
|
2016-02-01 00:34:07 +08:00
|
|
|
order = EC_GROUP_get0_order(group);
|
|
|
|
if (order == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (BN_is_zero(order)) {
|
|
|
|
ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-03-20 18:18:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08: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 18:18:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_CTX_free(new_ctx);
|
2015-03-26 06:35:24 +08:00
|
|
|
EC_POINT_free(point);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
2019-06-05 06:24:16 +08:00
|
|
|
#endif /* FIPS_MODE */
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|