2 * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/err.h>
13 int EC_GROUP_check_named_curve(const EC_GROUP
*group
, int nist_only
)
17 nid
= ec_curve_nid_from_params(group
);
18 if (nid
> 0 && nist_only
&& EC_curve_nid2nist(nid
) == NULL
)
23 int EC_GROUP_check(const EC_GROUP
*group
, BN_CTX
*ctx
)
27 BN_CTX
*new_ctx
= NULL
;
28 EC_POINT
*point
= NULL
;
30 if (group
== NULL
|| group
->meth
== NULL
) {
31 ECerr(EC_F_EC_GROUP_CHECK
, ERR_R_PASSED_NULL_PARAMETER
);
35 /* Custom curves assumed to be correct */
36 if ((group
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) != 0)
40 ctx
= new_ctx
= BN_CTX_new();
42 ECerr(EC_F_EC_GROUP_CHECK
, ERR_R_MALLOC_FAILURE
);
47 /* check the discriminant */
48 if (!EC_GROUP_check_discriminant(group
, ctx
)) {
49 ECerr(EC_F_EC_GROUP_CHECK
, EC_R_DISCRIMINANT_IS_ZERO
);
53 /* check the generator */
54 if (group
->generator
== NULL
) {
55 ECerr(EC_F_EC_GROUP_CHECK
, EC_R_UNDEFINED_GENERATOR
);
58 if (EC_POINT_is_on_curve(group
, group
->generator
, ctx
) <= 0) {
59 ECerr(EC_F_EC_GROUP_CHECK
, EC_R_POINT_IS_NOT_ON_CURVE
);
63 /* check the order of the generator */
64 if ((point
= EC_POINT_new(group
)) == NULL
)
66 order
= EC_GROUP_get0_order(group
);
69 if (BN_is_zero(order
)) {
70 ECerr(EC_F_EC_GROUP_CHECK
, EC_R_UNDEFINED_ORDER
);
74 if (!EC_POINT_mul(group
, point
, order
, NULL
, NULL
, ctx
))
76 if (!EC_POINT_is_at_infinity(group
, point
)) {
77 ECerr(EC_F_EC_GROUP_CHECK
, EC_R_INVALID_GROUP_ORDER
);