/src/openssl/crypto/ec/ec_check.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
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  | 
8  |  |  */  | 
9  |  |  | 
10  |  | /*  | 
11  |  |  * ECDSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include "ec_local.h"  | 
17  |  | #include <openssl/err.h>  | 
18  |  |  | 
19  |  | int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only,  | 
20  |  |                                BN_CTX *ctx)  | 
21  | 0  | { | 
22  | 0  |     int nid;  | 
23  | 0  |     BN_CTX *new_ctx = NULL;  | 
24  |  | 
  | 
25  | 0  |     if (group == NULL) { | 
26  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);  | 
27  | 0  |         return NID_undef;  | 
28  | 0  |     }  | 
29  |  |  | 
30  | 0  |     if (ctx == NULL) { | 
31  | 0  |         ctx = new_ctx = BN_CTX_new_ex(NULL);  | 
32  | 0  |         if (ctx == NULL) { | 
33  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);  | 
34  | 0  |             return NID_undef;  | 
35  | 0  |         }  | 
36  | 0  |     }  | 
37  |  |  | 
38  | 0  |     nid = ossl_ec_curve_nid_from_params(group, ctx);  | 
39  | 0  |     if (nid > 0 && nist_only && EC_curve_nid2nist(nid) == NULL)  | 
40  | 0  |         nid = NID_undef;  | 
41  |  | 
  | 
42  | 0  |     BN_CTX_free(new_ctx);  | 
43  | 0  |     return nid;  | 
44  | 0  | }  | 
45  |  |  | 
46  |  | int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)  | 
47  | 0  | { | 
48  |  | #ifdef FIPS_MODULE  | 
49  |  |     /*  | 
50  |  |     * ECC domain parameter validation.  | 
51  |  |     * See SP800-56A R3 5.5.2 "Assurances of Domain-Parameter Validity" Part 1b.  | 
52  |  |     */  | 
53  |  |     return EC_GROUP_check_named_curve(group, 1, ctx) >= 0 ? 1 : 0;  | 
54  |  | #else  | 
55  | 0  |     int ret = 0;  | 
56  | 0  |     const BIGNUM *order;  | 
57  | 0  |     BN_CTX *new_ctx = NULL;  | 
58  | 0  |     EC_POINT *point = NULL;  | 
59  |  | 
  | 
60  | 0  |     if (group == NULL || group->meth == NULL) { | 
61  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);  | 
62  | 0  |         return 0;  | 
63  | 0  |     }  | 
64  |  |  | 
65  |  |     /* Custom curves assumed to be correct */  | 
66  | 0  |     if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)  | 
67  | 0  |         return 1;  | 
68  |  |  | 
69  | 0  |     if (ctx == NULL) { | 
70  | 0  |         ctx = new_ctx = BN_CTX_new();  | 
71  | 0  |         if (ctx == NULL) { | 
72  | 0  |             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);  | 
73  | 0  |             goto err;  | 
74  | 0  |         }  | 
75  | 0  |     }  | 
76  |  |  | 
77  |  |     /* check the discriminant */  | 
78  | 0  |     if (!EC_GROUP_check_discriminant(group, ctx)) { | 
79  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_DISCRIMINANT_IS_ZERO);  | 
80  | 0  |         goto err;  | 
81  | 0  |     }  | 
82  |  |  | 
83  |  |     /* check the generator */  | 
84  | 0  |     if (group->generator == NULL) { | 
85  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);  | 
86  | 0  |         goto err;  | 
87  | 0  |     }  | 
88  | 0  |     if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) { | 
89  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);  | 
90  | 0  |         goto err;  | 
91  | 0  |     }  | 
92  |  |  | 
93  |  |     /* check the order of the generator */  | 
94  | 0  |     if ((point = EC_POINT_new(group)) == NULL)  | 
95  | 0  |         goto err;  | 
96  | 0  |     order = EC_GROUP_get0_order(group);  | 
97  | 0  |     if (order == NULL)  | 
98  | 0  |         goto err;  | 
99  | 0  |     if (BN_is_zero(order)) { | 
100  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_ORDER);  | 
101  | 0  |         goto err;  | 
102  | 0  |     }  | 
103  |  |  | 
104  | 0  |     if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))  | 
105  | 0  |         goto err;  | 
106  | 0  |     if (!EC_POINT_is_at_infinity(group, point)) { | 
107  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);  | 
108  | 0  |         goto err;  | 
109  | 0  |     }  | 
110  |  |  | 
111  | 0  |     ret = 1;  | 
112  |  | 
  | 
113  | 0  |  err:  | 
114  | 0  |     BN_CTX_free(new_ctx);  | 
115  | 0  |     EC_POINT_free(point);  | 
116  | 0  |     return ret;  | 
117  | 0  | #endif /* FIPS_MODULE */  | 
118  | 0  | }  |