• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecc_asy_key_generator_openssl.h"
17 
18 #include "securec.h"
19 
20 #include "ecc_openssl_common.h"
21 #include "ecc_openssl_common_param_spec.h"
22 #include "log.h"
23 #include "memory.h"
24 #include "openssl_adapter.h"
25 #include "utils.h"
26 
27 #define OPENSSL_ECC_KEY_GENERATOR_CLASS "OPENSSL.ECC.KEY_GENERATOR_CLASS"
28 #define OPENSSL_ECC_ALGORITHM "EC"
29 #define OPENSSL_ECC_PUB_KEY_FORMAT "X.509"
30 #define OPENSSL_ECC_PRI_KEY_FORMAT "PKCS#8"
31 #define OPENSSL_ECC160_BITS 160
32 #define OPENSSL_ECC192_BITS 192
33 #define OPENSSL_ECC224_BITS 224
34 #define OPENSSL_ECC256_BITS 256
35 #define OPENSSL_ECC320_BITS 320
36 #define OPENSSL_ECC384_BITS 384
37 #define OPENSSL_ECC512_BITS 512
38 #define OPENSSL_ECC521_BITS 521
39 
40 static const char *g_eccGenerateFieldType = "Fp";
41 
42 typedef struct {
43     BIGNUM *p;
44     BIGNUM *b;
45     BIGNUM *x;
46     BIGNUM *y;
47 }HcfBigIntegerParams;
48 
49 typedef struct {
50     HcfAsyKeyGeneratorSpi base;
51 
52     int32_t curveId;
53 } HcfAsyKeyGeneratorSpiOpensslEccImpl;
54 
CheckEc224CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)55 static HcfResult CheckEc224CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
56 {
57     BIGNUM *pStd = NULL;
58     BIGNUM *bStd = NULL;
59     BIGNUM *xStd = NULL;
60     BIGNUM *yStd = NULL;
61     pStd = Openssl_BN_bin2bn(g_ecc224CorrectBigP, NID_secp224r1_len, NULL);
62     bStd = Openssl_BN_bin2bn(g_ecc224CorrectBigB, NID_secp224r1_len, NULL);
63     xStd = Openssl_BN_bin2bn(g_ecc224CorrectBigGX, NID_secp224r1_len, NULL);
64     yStd = Openssl_BN_bin2bn(g_ecc224CorrectBigGY, NID_secp224r1_len, NULL);
65     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
66         LOGD("[error] EC 224 Curve convert to BN fail");
67         FreeCurveBigNum(pStd, bStd, xStd, yStd);
68         return HCF_ERR_CRYPTO_OPERATION;
69     }
70     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
71         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
72         FreeCurveBigNum(pStd, bStd, xStd, yStd);
73         return HCF_SUCCESS;
74     }
75     LOGD("[error] EC 224 compare fail");
76     FreeCurveBigNum(pStd, bStd, xStd, yStd);
77     return HCF_INVALID_PARAMS;
78 }
79 
CheckEc256CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)80 static HcfResult CheckEc256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
81 {
82     BIGNUM *pStd = NULL;
83     BIGNUM *bStd = NULL;
84     BIGNUM *xStd = NULL;
85     BIGNUM *yStd = NULL;
86     pStd = Openssl_BN_bin2bn(g_ecc256CorrectBigP, NID_X9_62_prime256v1_len, NULL);
87     bStd = Openssl_BN_bin2bn(g_ecc256CorrectBigB, NID_X9_62_prime256v1_len, NULL);
88     xStd = Openssl_BN_bin2bn(g_ecc256CorrectBigGX, NID_X9_62_prime256v1_len, NULL);
89     yStd = Openssl_BN_bin2bn(g_ecc256CorrectBigGY, NID_X9_62_prime256v1_len, NULL);
90     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
91         LOGD("[error] EC 256 Curve convert to BN fail");
92         FreeCurveBigNum(pStd, bStd, xStd, yStd);
93         return HCF_ERR_CRYPTO_OPERATION;
94     }
95     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
96         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
97         FreeCurveBigNum(pStd, bStd, xStd, yStd);
98         return HCF_SUCCESS;
99     }
100     LOGD("[error] EC 256 compare fail");
101     FreeCurveBigNum(pStd, bStd, xStd, yStd);
102     return HCF_INVALID_PARAMS;
103 }
104 
CheckEc384CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)105 static HcfResult CheckEc384CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
106 {
107     BIGNUM *pStd = NULL;
108     BIGNUM *bStd = NULL;
109     BIGNUM *xStd = NULL;
110     BIGNUM *yStd = NULL;
111     pStd = Openssl_BN_bin2bn(g_ecc384CorrectBigP, NID_secp384r1_len, NULL);
112     bStd = Openssl_BN_bin2bn(g_ecc384CorrectBigB, NID_secp384r1_len, NULL);
113     xStd = Openssl_BN_bin2bn(g_ecc384CorrectBigGX, NID_secp384r1_len, NULL);
114     yStd = Openssl_BN_bin2bn(g_ecc384CorrectBigGY, NID_secp384r1_len, NULL);
115     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
116         LOGD("[error] EC 384 Curve convert to BN fail");
117         FreeCurveBigNum(pStd, bStd, xStd, yStd);
118         return HCF_ERR_CRYPTO_OPERATION;
119     }
120     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
121         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
122         FreeCurveBigNum(pStd, bStd, xStd, yStd);
123         return HCF_SUCCESS;
124     }
125     LOGD("[error] EC 384 compare fail");
126     FreeCurveBigNum(pStd, bStd, xStd, yStd);
127     return HCF_INVALID_PARAMS;
128 }
129 
CheckEc521CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)130 static HcfResult CheckEc521CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
131 {
132     BIGNUM *pStd = NULL;
133     BIGNUM *bStd = NULL;
134     BIGNUM *xStd = NULL;
135     BIGNUM *yStd = NULL;
136     pStd = Openssl_BN_bin2bn(g_ecc521CorrectBigP, NID_secp521r1_len, NULL);
137     bStd = Openssl_BN_bin2bn(g_ecc521CorrectBigB, NID_secp521r1_len, NULL);
138     xStd = Openssl_BN_bin2bn(g_ecc521CorrectBigGX, NID_secp521r1_len, NULL);
139     yStd = Openssl_BN_bin2bn(g_ecc521CorrectBigGY, NID_secp521r1_len, NULL);
140     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
141         LOGD("[error] EC 521 Curve convert to BN fail");
142         FreeCurveBigNum(pStd, bStd, xStd, yStd);
143         return HCF_ERR_CRYPTO_OPERATION;
144     }
145     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
146         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
147         FreeCurveBigNum(pStd, bStd, xStd, yStd);
148         return HCF_SUCCESS;
149     }
150     LOGD("[error] EC 521 compare fail");
151     FreeCurveBigNum(pStd, bStd, xStd, yStd);
152     return HCF_INVALID_PARAMS;
153 }
154 
CheckBP160r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)155 static HcfResult CheckBP160r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
156 {
157     BIGNUM *pStd = NULL;
158     BIGNUM *bStd = NULL;
159     BIGNUM *xStd = NULL;
160     BIGNUM *yStd = NULL;
161     pStd = Openssl_BN_bin2bn(g_bp160r1CorrectBigP, NID_brainpoolP160r1_len, NULL);
162     bStd = Openssl_BN_bin2bn(g_bp160r1CorrectBigB, NID_brainpoolP160r1_len, NULL);
163     xStd = Openssl_BN_bin2bn(g_bp160r1CorrectBigGX, NID_brainpoolP160r1_len, NULL);
164     yStd = Openssl_BN_bin2bn(g_bp160r1CorrectBigGY, NID_brainpoolP160r1_len, NULL);
165     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
166         LOGD("[error] BP 160r1 Curve convert to BN fail");
167         FreeCurveBigNum(pStd, bStd, xStd, yStd);
168         return HCF_ERR_CRYPTO_OPERATION;
169     }
170     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
171         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
172         FreeCurveBigNum(pStd, bStd, xStd, yStd);
173         return HCF_SUCCESS;
174     }
175     LOGD("[error] BP 160r1 compare fail");
176     FreeCurveBigNum(pStd, bStd, xStd, yStd);
177     return HCF_INVALID_PARAMS;
178 }
179 
CheckBP160t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)180 static HcfResult CheckBP160t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
181 {
182     BIGNUM *pStd = NULL;
183     BIGNUM *bStd = NULL;
184     BIGNUM *xStd = NULL;
185     BIGNUM *yStd = NULL;
186     pStd = Openssl_BN_bin2bn(g_bp160t1CorrectBigP, NID_brainpoolP160t1_len, NULL);
187     bStd = Openssl_BN_bin2bn(g_bp160t1CorrectBigB, NID_brainpoolP160t1_len, NULL);
188     xStd = Openssl_BN_bin2bn(g_bp160t1CorrectBigGX, NID_brainpoolP160t1_len, NULL);
189     yStd = Openssl_BN_bin2bn(g_bp160t1CorrectBigGY, NID_brainpoolP160t1_len, NULL);
190     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
191         LOGD("[error] BP 160t1 Curve convert to BN fail");
192         FreeCurveBigNum(pStd, bStd, xStd, yStd);
193         return HCF_ERR_CRYPTO_OPERATION;
194     }
195     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
196         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
197         FreeCurveBigNum(pStd, bStd, xStd, yStd);
198         return HCF_SUCCESS;
199     }
200     LOGD("[error] BP 160t1 compare fail");
201     FreeCurveBigNum(pStd, bStd, xStd, yStd);
202     return HCF_INVALID_PARAMS;
203 }
204 
CheckBP192r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)205 static HcfResult CheckBP192r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
206 {
207     BIGNUM *pStd = NULL;
208     BIGNUM *bStd = NULL;
209     BIGNUM *xStd = NULL;
210     BIGNUM *yStd = NULL;
211     pStd = Openssl_BN_bin2bn(g_bp192r1CorrectBigP, NID_brainpoolP192r1_len, NULL);
212     bStd = Openssl_BN_bin2bn(g_bp192r1CorrectBigB, NID_brainpoolP192r1_len, NULL);
213     xStd = Openssl_BN_bin2bn(g_bp192r1CorrectBigGX, NID_brainpoolP192r1_len, NULL);
214     yStd = Openssl_BN_bin2bn(g_bp192r1CorrectBigGY, NID_brainpoolP192r1_len, NULL);
215     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
216         LOGD("[error] BP 192r1 Curve convert to BN fail");
217         FreeCurveBigNum(pStd, bStd, xStd, yStd);
218         return HCF_ERR_CRYPTO_OPERATION;
219     }
220     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
221         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
222         FreeCurveBigNum(pStd, bStd, xStd, yStd);
223         return HCF_SUCCESS;
224     }
225     LOGD("[error] BP 192r1 compare fail");
226     FreeCurveBigNum(pStd, bStd, xStd, yStd);
227     return HCF_INVALID_PARAMS;
228 }
229 
CheckBP192t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)230 static HcfResult CheckBP192t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
231 {
232     BIGNUM *pStd = NULL;
233     BIGNUM *bStd = NULL;
234     BIGNUM *xStd = NULL;
235     BIGNUM *yStd = NULL;
236     pStd = Openssl_BN_bin2bn(g_bp192t1CorrectBigP, NID_brainpoolP192t1_len, NULL);
237     bStd = Openssl_BN_bin2bn(g_bp192t1CorrectBigB, NID_brainpoolP192t1_len, NULL);
238     xStd = Openssl_BN_bin2bn(g_bp192t1CorrectBigGX, NID_brainpoolP192t1_len, NULL);
239     yStd = Openssl_BN_bin2bn(g_bp192t1CorrectBigGY, NID_brainpoolP192t1_len, NULL);
240     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
241         LOGD("[error] BP 192t1 Curve convert to BN fail");
242         FreeCurveBigNum(pStd, bStd, xStd, yStd);
243         return HCF_ERR_CRYPTO_OPERATION;
244     }
245     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
246         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
247         FreeCurveBigNum(pStd, bStd, xStd, yStd);
248         return HCF_SUCCESS;
249     }
250     LOGD("[error] BP 192t1 compare fail");
251     FreeCurveBigNum(pStd, bStd, xStd, yStd);
252     return HCF_INVALID_PARAMS;
253 }
254 
CheckBP224r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)255 static HcfResult CheckBP224r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
256 {
257     BIGNUM *pStd = NULL;
258     BIGNUM *bStd = NULL;
259     BIGNUM *xStd = NULL;
260     BIGNUM *yStd = NULL;
261     pStd = Openssl_BN_bin2bn(g_bp224r1CorrectBigP, NID_brainpoolP224r1_len, NULL);
262     bStd = Openssl_BN_bin2bn(g_bp224r1CorrectBigB, NID_brainpoolP224r1_len, NULL);
263     xStd = Openssl_BN_bin2bn(g_bp224r1CorrectBigGX, NID_brainpoolP224r1_len, NULL);
264     yStd = Openssl_BN_bin2bn(g_bp224r1CorrectBigGY, NID_brainpoolP224r1_len, NULL);
265     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
266         LOGD("[error] BP 224r1 Curve convert to BN fail");
267         FreeCurveBigNum(pStd, bStd, xStd, yStd);
268         return HCF_ERR_CRYPTO_OPERATION;
269     }
270     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
271         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
272         FreeCurveBigNum(pStd, bStd, xStd, yStd);
273         return HCF_SUCCESS;
274     }
275     LOGD("[error] BP 224r1 compare fail");
276     FreeCurveBigNum(pStd, bStd, xStd, yStd);
277     return HCF_INVALID_PARAMS;
278 }
279 
CheckBP224t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)280 static HcfResult CheckBP224t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
281 {
282     BIGNUM *pStd = NULL;
283     BIGNUM *bStd = NULL;
284     BIGNUM *xStd = NULL;
285     BIGNUM *yStd = NULL;
286     pStd = Openssl_BN_bin2bn(g_bp224t1CorrectBigP, NID_brainpoolP224t1_len, NULL);
287     bStd = Openssl_BN_bin2bn(g_bp224t1CorrectBigB, NID_brainpoolP224t1_len, NULL);
288     xStd = Openssl_BN_bin2bn(g_bp224t1CorrectBigGX, NID_brainpoolP224t1_len, NULL);
289     yStd = Openssl_BN_bin2bn(g_bp224t1CorrectBigGY, NID_brainpoolP224t1_len, NULL);
290     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
291         LOGD("[error] BP 224t1 Curve convert to BN fail");
292         FreeCurveBigNum(pStd, bStd, xStd, yStd);
293         return HCF_ERR_CRYPTO_OPERATION;
294     }
295     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
296         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
297         FreeCurveBigNum(pStd, bStd, xStd, yStd);
298         return HCF_SUCCESS;
299     }
300     LOGD("[error] BP 224t1 compare fail");
301     FreeCurveBigNum(pStd, bStd, xStd, yStd);
302     return HCF_INVALID_PARAMS;
303 }
304 
CheckBP256r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)305 static HcfResult CheckBP256r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
306 {
307     BIGNUM *pStd = NULL;
308     BIGNUM *bStd = NULL;
309     BIGNUM *xStd = NULL;
310     BIGNUM *yStd = NULL;
311     pStd = Openssl_BN_bin2bn(g_bp256r1CorrectBigP, NID_brainpoolP256r1_len, NULL);
312     bStd = Openssl_BN_bin2bn(g_bp256r1CorrectBigB, NID_brainpoolP256r1_len, NULL);
313     xStd = Openssl_BN_bin2bn(g_bp256r1CorrectBigGX, NID_brainpoolP256r1_len, NULL);
314     yStd = Openssl_BN_bin2bn(g_bp256r1CorrectBigGY, NID_brainpoolP256r1_len, NULL);
315     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
316         LOGD("[error] BP 256r1 Curve convert to BN fail");
317         FreeCurveBigNum(pStd, bStd, xStd, yStd);
318         return HCF_ERR_CRYPTO_OPERATION;
319     }
320     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
321         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
322         FreeCurveBigNum(pStd, bStd, xStd, yStd);
323         return HCF_SUCCESS;
324     }
325     LOGD("[error] BP 256r1 compare fail");
326     FreeCurveBigNum(pStd, bStd, xStd, yStd);
327     return HCF_INVALID_PARAMS;
328 }
329 
CheckBP256t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)330 static HcfResult CheckBP256t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
331 {
332     BIGNUM *pStd = NULL;
333     BIGNUM *bStd = NULL;
334     BIGNUM *xStd = NULL;
335     BIGNUM *yStd = NULL;
336     pStd = Openssl_BN_bin2bn(g_bp256t1CorrectBigP, NID_brainpoolP256t1_len, NULL);
337     bStd = Openssl_BN_bin2bn(g_bp256t1CorrectBigB, NID_brainpoolP256t1_len, NULL);
338     xStd = Openssl_BN_bin2bn(g_bp256t1CorrectBigGX, NID_brainpoolP256t1_len, NULL);
339     yStd = Openssl_BN_bin2bn(g_bp256t1CorrectBigGY, NID_brainpoolP256t1_len, NULL);
340     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
341         LOGD("[error] BP 256t1 Curve convert to BN fail");
342         FreeCurveBigNum(pStd, bStd, xStd, yStd);
343         return HCF_ERR_CRYPTO_OPERATION;
344     }
345     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
346         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
347         FreeCurveBigNum(pStd, bStd, xStd, yStd);
348         return HCF_SUCCESS;
349     }
350     FreeCurveBigNum(pStd, bStd, xStd, yStd);
351     return HCF_INVALID_PARAMS;
352 }
353 
CheckBP320r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)354 static HcfResult CheckBP320r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
355 {
356     BIGNUM *pStd = NULL;
357     BIGNUM *bStd = NULL;
358     BIGNUM *xStd = NULL;
359     BIGNUM *yStd = NULL;
360     pStd = Openssl_BN_bin2bn(g_bp320r1CorrectBigP, NID_brainpoolP320r1_len, NULL);
361     bStd = Openssl_BN_bin2bn(g_bp320r1CorrectBigB, NID_brainpoolP320r1_len, NULL);
362     xStd = Openssl_BN_bin2bn(g_bp320r1CorrectBigGX, NID_brainpoolP320r1_len, NULL);
363     yStd = Openssl_BN_bin2bn(g_bp320r1CorrectBigGY, NID_brainpoolP320r1_len, NULL);
364     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
365         LOGD("[error] BP 320r1 Curve convert to BN fail");
366         FreeCurveBigNum(pStd, bStd, xStd, yStd);
367         return HCF_ERR_CRYPTO_OPERATION;
368     }
369     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
370         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
371         FreeCurveBigNum(pStd, bStd, xStd, yStd);
372         return HCF_SUCCESS;
373     }
374     FreeCurveBigNum(pStd, bStd, xStd, yStd);
375     return HCF_INVALID_PARAMS;
376 }
377 
CheckBP320t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)378 static HcfResult CheckBP320t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
379 {
380     BIGNUM *pStd = NULL;
381     BIGNUM *bStd = NULL;
382     BIGNUM *xStd = NULL;
383     BIGNUM *yStd = NULL;
384     pStd = Openssl_BN_bin2bn(g_bp320t1CorrectBigP, NID_brainpoolP320t1_len, NULL);
385     bStd = Openssl_BN_bin2bn(g_bp320t1CorrectBigB, NID_brainpoolP320t1_len, NULL);
386     xStd = Openssl_BN_bin2bn(g_bp320t1CorrectBigGX, NID_brainpoolP320t1_len, NULL);
387     yStd = Openssl_BN_bin2bn(g_bp320t1CorrectBigGY, NID_brainpoolP320t1_len, NULL);
388     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
389         LOGD("[error] BP 320t1 Curve convert to BN fail");
390         FreeCurveBigNum(pStd, bStd, xStd, yStd);
391         return HCF_ERR_CRYPTO_OPERATION;
392     }
393     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
394         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
395         FreeCurveBigNum(pStd, bStd, xStd, yStd);
396         return HCF_SUCCESS;
397     }
398     FreeCurveBigNum(pStd, bStd, xStd, yStd);
399     return HCF_INVALID_PARAMS;
400 }
401 
CheckBP384r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)402 static HcfResult CheckBP384r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
403 {
404     BIGNUM *pStd = NULL;
405     BIGNUM *bStd = NULL;
406     BIGNUM *xStd = NULL;
407     BIGNUM *yStd = NULL;
408     pStd = Openssl_BN_bin2bn(g_bp384r1CorrectBigP, NID_brainpoolP384r1_len, NULL);
409     bStd = Openssl_BN_bin2bn(g_bp384r1CorrectBigB, NID_brainpoolP384r1_len, NULL);
410     xStd = Openssl_BN_bin2bn(g_bp384r1CorrectBigGX, NID_brainpoolP384r1_len, NULL);
411     yStd = Openssl_BN_bin2bn(g_bp384r1CorrectBigGY, NID_brainpoolP384r1_len, NULL);
412     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
413         LOGD("[error] BP 384r1 Curve convert to BN fail");
414         FreeCurveBigNum(pStd, bStd, xStd, yStd);
415         return HCF_ERR_CRYPTO_OPERATION;
416     }
417     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
418         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
419         FreeCurveBigNum(pStd, bStd, xStd, yStd);
420         return HCF_SUCCESS;
421     }
422     FreeCurveBigNum(pStd, bStd, xStd, yStd);
423     return HCF_INVALID_PARAMS;
424 }
425 
CheckBP384t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)426 static HcfResult CheckBP384t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
427 {
428     BIGNUM *pStd = NULL;
429     BIGNUM *bStd = NULL;
430     BIGNUM *xStd = NULL;
431     BIGNUM *yStd = NULL;
432     pStd = Openssl_BN_bin2bn(g_bp384t1CorrectBigP, NID_brainpoolP384t1_len, NULL);
433     bStd = Openssl_BN_bin2bn(g_bp384t1CorrectBigB, NID_brainpoolP384t1_len, NULL);
434     xStd = Openssl_BN_bin2bn(g_bp384t1CorrectBigGX, NID_brainpoolP384t1_len, NULL);
435     yStd = Openssl_BN_bin2bn(g_bp384t1CorrectBigGY, NID_brainpoolP384t1_len, NULL);
436     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
437         LOGD("[error] BP 384t1 Curve convert to BN fail");
438         FreeCurveBigNum(pStd, bStd, xStd, yStd);
439         return HCF_ERR_CRYPTO_OPERATION;
440     }
441     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
442         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
443         FreeCurveBigNum(pStd, bStd, xStd, yStd);
444         return HCF_SUCCESS;
445     }
446     FreeCurveBigNum(pStd, bStd, xStd, yStd);
447     return HCF_INVALID_PARAMS;
448 }
449 
CheckBP512r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)450 static HcfResult CheckBP512r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
451 {
452     BIGNUM *pStd = NULL;
453     BIGNUM *bStd = NULL;
454     BIGNUM *xStd = NULL;
455     BIGNUM *yStd = NULL;
456     pStd = Openssl_BN_bin2bn(g_bp512r1CorrectBigP, NID_brainpoolP512r1_len, NULL);
457     bStd = Openssl_BN_bin2bn(g_bp512r1CorrectBigB, NID_brainpoolP512r1_len, NULL);
458     xStd = Openssl_BN_bin2bn(g_bp512r1CorrectBigGX, NID_brainpoolP512r1_len, NULL);
459     yStd = Openssl_BN_bin2bn(g_bp512r1CorrectBigGY, NID_brainpoolP512r1_len, NULL);
460     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
461         LOGD("[error] BP 512r1 Curve convert to BN fail");
462         FreeCurveBigNum(pStd, bStd, xStd, yStd);
463         return HCF_ERR_CRYPTO_OPERATION;
464     }
465     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
466         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
467         FreeCurveBigNum(pStd, bStd, xStd, yStd);
468         return HCF_SUCCESS;
469     }
470     FreeCurveBigNum(pStd, bStd, xStd, yStd);
471     return HCF_INVALID_PARAMS;
472 }
473 
CheckBP512t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)474 static HcfResult CheckBP512t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
475 {
476     BIGNUM *pStd = NULL;
477     BIGNUM *bStd = NULL;
478     BIGNUM *xStd = NULL;
479     BIGNUM *yStd = NULL;
480     pStd = Openssl_BN_bin2bn(g_bp512t1CorrectBigP, NID_brainpoolP512t1_len, NULL);
481     bStd = Openssl_BN_bin2bn(g_bp512t1CorrectBigB, NID_brainpoolP512t1_len, NULL);
482     xStd = Openssl_BN_bin2bn(g_bp512t1CorrectBigGX, NID_brainpoolP512t1_len, NULL);
483     yStd = Openssl_BN_bin2bn(g_bp512t1CorrectBigGY, NID_brainpoolP512t1_len, NULL);
484     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
485         LOGD("[error] BP 512t1 Curve convert to BN fail");
486         FreeCurveBigNum(pStd, bStd, xStd, yStd);
487         return HCF_ERR_CRYPTO_OPERATION;
488     }
489     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
490         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
491         FreeCurveBigNum(pStd, bStd, xStd, yStd);
492         return HCF_SUCCESS;
493     }
494     FreeCurveBigNum(pStd, bStd, xStd, yStd);
495     return HCF_INVALID_PARAMS;
496 }
497 
CompareOpenssl160BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)498 static HcfResult CompareOpenssl160BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
499     HcfBigIntegerParams *bigIntegerParams)
500 {
501     if (CheckBP160r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
502         bigIntegerParams->y) == HCF_SUCCESS) {
503         *curveId = NID_brainpoolP160r1;
504         return HCF_SUCCESS;
505     } else if (CheckBP160t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
506         bigIntegerParams->y) == HCF_SUCCESS) {
507         *curveId = NID_brainpoolP160t1;
508         return HCF_SUCCESS;
509     }
510     return HCF_NOT_SUPPORT;
511 }
512 
CompareOpenssl192BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)513 static HcfResult CompareOpenssl192BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
514     HcfBigIntegerParams *bigIntegerParams)
515 {
516     if (CheckBP192r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
517         bigIntegerParams->y) == HCF_SUCCESS) {
518         *curveId = NID_brainpoolP192r1;
519         return HCF_SUCCESS;
520     } else if (CheckBP192t1CurveId(bigIntegerParams->p, bigIntegerParams->b,
521         bigIntegerParams->x, bigIntegerParams->y) == HCF_SUCCESS) {
522         *curveId = NID_brainpoolP192t1;
523         return HCF_SUCCESS;
524     }
525     return HCF_NOT_SUPPORT;
526 }
527 
CompareOpenssl224BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)528 static HcfResult CompareOpenssl224BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
529     HcfBigIntegerParams *bigIntegerParams)
530 {
531     HcfResult res = HCF_INVALID_PARAMS;
532     res = CheckEc224CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
533     if (res == HCF_SUCCESS) {
534         *curveId = NID_secp224r1;
535         return res;
536     } else if (CheckBP224r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
537         bigIntegerParams->y) == HCF_SUCCESS) {
538         *curveId = NID_brainpoolP224r1;
539         return HCF_SUCCESS;
540     } else if (CheckBP224t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
541         bigIntegerParams->y) == HCF_SUCCESS) {
542         *curveId = NID_brainpoolP224t1;
543         return HCF_SUCCESS;
544     }
545     return HCF_NOT_SUPPORT;
546 }
547 
CompareOpenssl256BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)548 static HcfResult CompareOpenssl256BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
549     HcfBigIntegerParams *bigIntegerParams)
550 {
551     HcfResult res = HCF_INVALID_PARAMS;
552     res = CheckEc256CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
553     if (res == HCF_SUCCESS) {
554         *curveId = NID_X9_62_prime256v1;
555         return res;
556     } else if (CheckBP256r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
557         bigIntegerParams->y) == HCF_SUCCESS) {
558         *curveId = NID_brainpoolP256r1;
559         return HCF_SUCCESS;
560     } else if (CheckBP256t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
561         bigIntegerParams->y) == HCF_SUCCESS) {
562         *curveId = NID_brainpoolP256t1;
563         return HCF_SUCCESS;
564     }
565     return HCF_NOT_SUPPORT;
566 }
567 
CompareOpenssl320BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)568 static HcfResult CompareOpenssl320BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
569     HcfBigIntegerParams *bigIntegerParams)
570 {
571     if (CheckBP320r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
572         bigIntegerParams->y) == HCF_SUCCESS) {
573         *curveId = NID_brainpoolP320r1;
574         return HCF_SUCCESS;
575     } else if (CheckBP320t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
576         bigIntegerParams->y) == HCF_SUCCESS) {
577         *curveId = NID_brainpoolP320t1;
578         return HCF_SUCCESS;
579     }
580     return HCF_NOT_SUPPORT;
581 }
582 
CompareOpenssl384BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)583 static HcfResult CompareOpenssl384BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
584     HcfBigIntegerParams *bigIntegerParams)
585 {
586     HcfResult res = HCF_INVALID_PARAMS;
587     res = CheckBP384r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
588     if (res == HCF_SUCCESS) {
589         *curveId = NID_brainpoolP384r1;
590         return res;
591     } else if (CheckEc384CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
592         bigIntegerParams->y) == HCF_SUCCESS) {
593         *curveId = NID_secp384r1;
594         return HCF_SUCCESS;
595     } else if (CheckBP384t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
596         bigIntegerParams->y) == HCF_SUCCESS) {
597         *curveId = NID_brainpoolP384t1;
598         return HCF_SUCCESS;
599     }
600     return HCF_NOT_SUPPORT;
601 }
602 
CompareOpenssl512BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)603 static HcfResult CompareOpenssl512BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
604     HcfBigIntegerParams *bigIntegerParams)
605 {
606     if (CheckBP512r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
607         bigIntegerParams->y) == HCF_SUCCESS) {
608         *curveId = NID_brainpoolP512r1;
609         return HCF_SUCCESS;
610     } else if (CheckBP512t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
611         bigIntegerParams->y) == HCF_SUCCESS) {
612         *curveId = NID_brainpoolP512t1;
613         return HCF_SUCCESS;
614     }
615     return HCF_NOT_SUPPORT;
616 }
617 
CompareOpenssl521BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)618 static HcfResult CompareOpenssl521BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
619     HcfBigIntegerParams *bigIntegerParams)
620 {
621     HcfResult res = CheckEc521CurveId(bigIntegerParams->p, bigIntegerParams->b,
622         bigIntegerParams->x, bigIntegerParams->y);
623     if (res == HCF_SUCCESS) {
624         *curveId = NID_secp521r1;
625         return res;
626     }
627     return HCF_NOT_SUPPORT;
628 }
629 
CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec * ecParams,int32_t * curveId)630 static HcfResult CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec *ecParams, int32_t *curveId)
631 {
632     HcfBigIntegerParams bigIntegerParams;
633     bigIntegerParams.p = NULL;
634     bigIntegerParams.b = NULL;
635     bigIntegerParams.x = NULL;
636     bigIntegerParams.y = NULL;
637     HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field);
638     if (BigIntegerToBigNum(&(field->p), &(bigIntegerParams.p)) != HCF_SUCCESS ||
639         BigIntegerToBigNum(&(ecParams->b), &(bigIntegerParams.b)) != HCF_SUCCESS ||
640         BigIntegerToBigNum(&(ecParams->g.x), &(bigIntegerParams.x)) != HCF_SUCCESS ||
641         BigIntegerToBigNum(&(ecParams->g.y), &(bigIntegerParams.y)) != HCF_SUCCESS) {
642         LOGD("[error] BigIntegerToBigNum failed.");
643         FreeCurveBigNum(bigIntegerParams.p, bigIntegerParams.b, bigIntegerParams.x, bigIntegerParams.y);
644         return HCF_ERR_CRYPTO_OPERATION;
645     }
646 
647     int32_t bitLenP = (int32_t)Openssl_BN_num_bits(bigIntegerParams.p);
648     HcfResult res = HCF_INVALID_PARAMS;
649     switch (bitLenP) {
650         case OPENSSL_ECC160_BITS:
651             res = CompareOpenssl160BitsType(ecParams, curveId, &bigIntegerParams);
652             break;
653         case OPENSSL_ECC192_BITS:
654             res = CompareOpenssl192BitsType(ecParams, curveId, &bigIntegerParams);
655             break;
656         case OPENSSL_ECC224_BITS:
657             res = CompareOpenssl224BitsType(ecParams, curveId, &bigIntegerParams);
658             break;
659         case OPENSSL_ECC256_BITS:
660             res = CompareOpenssl256BitsType(ecParams, curveId, &bigIntegerParams);
661             break;
662         case OPENSSL_ECC320_BITS:
663             res = CompareOpenssl320BitsType(ecParams, curveId, &bigIntegerParams);
664             break;
665         case OPENSSL_ECC384_BITS:
666             res = CompareOpenssl384BitsType(ecParams, curveId, &bigIntegerParams);
667             break;
668         case OPENSSL_ECC512_BITS:
669             res = CompareOpenssl512BitsType(ecParams, curveId, &bigIntegerParams);
670             break;
671         case OPENSSL_ECC521_BITS:
672             res = CompareOpenssl521BitsType(ecParams, curveId, &bigIntegerParams);
673             break;
674         default:
675             LOGE("Find no bit len:%d", bitLenP);
676             break;
677     }
678     FreeCurveBigNum(bigIntegerParams.p, bigIntegerParams.b, bigIntegerParams.x, bigIntegerParams.y);
679     return res;
680 }
681 
GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnKey)682 static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnKey)
683 {
684     if (ecParams == NULL || returnKey == NULL) {
685         LOGE("Invalid input parameters.");
686         return HCF_INVALID_PARAMS;
687     }
688     EC_KEY *ecKey = NULL;
689     int32_t curveId = 0;
690     HcfResult ret = CheckParamsSpecToGetCurveId(ecParams, &curveId);
691     if (ret == HCF_SUCCESS && curveId != 0) {
692         ecKey = Openssl_EC_KEY_new_by_curve_name(curveId);
693         LOGD("generate EC_KEY by curve name");
694         if (ecKey == NULL) {
695             LOGD("[error] new ec key failed.");
696             return HCF_ERR_CRYPTO_OPERATION;
697         }
698     } else {
699         EC_GROUP *group = NULL;
700         ret = GenerateEcGroupWithParamsSpec(ecParams, &group);
701         if (ret != HCF_SUCCESS) {
702             LOGD("[error] GenerateEcGroupWithParamsSpec failed.");
703             return ret;
704         }
705         ecKey = Openssl_EC_KEY_new();
706         if (ecKey == NULL) {
707             LOGD("[error] Openssl_EC_KEY_new failed.");
708             Openssl_EC_GROUP_free(group);
709             return HCF_ERR_CRYPTO_OPERATION;
710         }
711         if (Openssl_EC_KEY_set_group(ecKey, group) != HCF_OPENSSL_SUCCESS) {
712             LOGD("[error] Openssl_EC_KEY_set_group failed.");
713             Openssl_EC_GROUP_free(group);
714             Openssl_EC_KEY_free(ecKey);
715             return HCF_ERR_CRYPTO_OPERATION;
716         }
717         Openssl_EC_GROUP_free(group);
718         LOGD("generate EC_KEY by group spec parmas");
719     }
720     // all exceptions have been returned above.
721     *returnKey = ecKey;
722     return HCF_SUCCESS;
723 }
724 
NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnEckey)725 static HcfResult NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnEckey)
726 {
727     if (ecParams == NULL || returnEckey == NULL) {
728         LOGE("Invalid input parameters.");
729         return HCF_INVALID_PARAMS;
730     }
731     EC_KEY *ecKey = NULL;
732     HcfResult ret = GenerateEcKeyWithParamsSpec(ecParams, &ecKey);
733     if (ret != HCF_SUCCESS) {
734         LOGE("generate EC key fails");
735         return ret;
736     }
737     if (Openssl_EC_KEY_generate_key(ecKey) != HCF_OPENSSL_SUCCESS) {
738         LOGD("[error] Openssl_EC_KEY_generate_key failed.");
739         Openssl_EC_KEY_free(ecKey);
740         return HCF_ERR_CRYPTO_OPERATION;
741     }
742 
743     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
744         LOGD("[error] Check key fail.");
745         Openssl_EC_KEY_free(ecKey);
746         return HCF_ERR_CRYPTO_OPERATION;
747     }
748     *returnEckey = ecKey;
749     return ret;
750 }
751 
NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)752 static HcfResult NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
753 {
754     if (ecParams == NULL || returnEcKey == NULL) {
755         LOGE("Invalid input parameters.");
756         return HCF_INVALID_PARAMS;
757     }
758     EC_KEY *ecKey = NULL;
759     HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
760     if (ret != HCF_SUCCESS) {
761         LOGE("generate EC key fails");
762         return ret;
763     }
764     ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
765     if (ret != HCF_SUCCESS) {
766         LOGD("[error] Set pub ecKey failed.");
767         Openssl_EC_KEY_free(ecKey);
768         return HCF_ERR_CRYPTO_OPERATION;
769     }
770 
771     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
772         LOGD("[error] Check key fail.");
773         Openssl_EC_KEY_free(ecKey);
774         return HCF_ERR_CRYPTO_OPERATION;
775     }
776     *returnEcKey = ecKey;
777     return ret;
778 }
779 
NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)780 static HcfResult NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
781 {
782     if (ecParams == NULL || returnEcKey == NULL) {
783         LOGE("Invalid input parameters.");
784         return HCF_INVALID_PARAMS;
785     }
786     EC_KEY *ecKey = NULL;
787     HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
788     if (ret != HCF_SUCCESS) {
789         LOGE("generate EC key fails");
790         return ret;
791     }
792     ret = SetEcKey(NULL, &(ecParams->sk), ecKey);
793     if (ret != HCF_SUCCESS) {
794         LOGD("[error] Set pri ecKey failed.");
795         Openssl_EC_KEY_free(ecKey);
796         return HCF_ERR_CRYPTO_OPERATION;
797     }
798 
799     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
800         LOGD("[error] Check key fail.");
801         Openssl_EC_KEY_free(ecKey);
802         return HCF_ERR_CRYPTO_OPERATION;
803     }
804     *returnEcKey = ecKey;
805     return ret;
806 }
807 
NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec * ecParams,EC_KEY ** returnEcKey,bool needPrivate)808 static HcfResult NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParams, EC_KEY **returnEcKey,
809     bool needPrivate)
810 {
811     if (ecParams == NULL || returnEcKey == NULL) {
812         LOGE("Invalid input parameters.");
813         return HCF_INVALID_PARAMS;
814     }
815     EC_KEY *ecKey = NULL;
816     HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
817     if (ret != HCF_SUCCESS) {
818         LOGE("generate EC key fails");
819         return ret;
820     }
821     if (needPrivate) {
822         ret = SetEcKey(&(ecParams->pk), &(ecParams->sk), ecKey);
823     } else {
824         ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
825     }
826     if (ret != HCF_SUCCESS) {
827         LOGD("[error] SetEcKey failed.");
828         Openssl_EC_KEY_free(ecKey);
829         return HCF_ERR_CRYPTO_OPERATION;
830     }
831 
832     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
833         LOGD("[error] Check key fail.");
834         Openssl_EC_KEY_free(ecKey);
835         return HCF_ERR_CRYPTO_OPERATION;
836     }
837     *returnEcKey = ecKey;
838     return ret;
839 }
840 
GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)841 static HcfResult GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
842 {
843     HcfResult ret = HCF_INVALID_PARAMS;
844     switch (params->specType) {
845         case HCF_COMMON_PARAMS_SPEC:
846             ret = NewEcKeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey);
847             break;
848         case HCF_KEY_PAIR_SPEC:
849             ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
850             break;
851         default:
852             LOGE("Invaild input spec to gen key pair.");
853             break;
854     }
855     return ret;
856 }
857 
GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)858 static HcfResult GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
859 {
860     HcfResult ret = HCF_INVALID_PARAMS;
861     switch (params->specType) {
862         case HCF_PUBLIC_KEY_SPEC:
863             ret = NewEcPubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey);
864             break;
865         case HCF_KEY_PAIR_SPEC:
866             ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false);
867             break;
868         default:
869             LOGE("Invaild input spec to gen pub key");
870             break;
871     }
872     return ret;
873 }
874 
GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)875 static HcfResult GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
876 {
877     HcfResult ret = HCF_INVALID_PARAMS;
878     switch (params->specType) {
879         case HCF_PRIVATE_KEY_SPEC:
880             ret = NewEcPriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey);
881             break;
882         case HCF_KEY_PAIR_SPEC:
883             ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
884             break;
885         default:
886             LOGE("Invaild input spec to gen pri key");
887             break;
888     }
889     return ret;
890 }
891 
GetEccKeyPairGeneratorClass(void)892 static const char *GetEccKeyPairGeneratorClass(void)
893 {
894     return OPENSSL_ECC_KEY_GENERATOR_CLASS;
895 }
896 
GetEccKeyPairClass(void)897 static const char *GetEccKeyPairClass(void)
898 {
899     return HCF_OPENSSL_ECC_KEY_PAIR_CLASS;
900 }
901 
GetEccPubKeyClass(void)902 static const char *GetEccPubKeyClass(void)
903 {
904     return HCF_OPENSSL_ECC_PUB_KEY_CLASS;
905 }
906 
GetEccPriKeyClass(void)907 static const char *GetEccPriKeyClass(void)
908 {
909     return HCF_OPENSSL_ECC_PRI_KEY_CLASS;
910 }
911 
DestroyEccKeyPairGenerator(HcfObjectBase * self)912 static void DestroyEccKeyPairGenerator(HcfObjectBase *self)
913 {
914     if (self == NULL) {
915         return;
916     }
917     if (!IsClassMatch(self, GetEccKeyPairGeneratorClass())) {
918         return;
919     }
920     HcfFree(self);
921 }
922 
DestroyEccPubKey(HcfObjectBase * self)923 static void DestroyEccPubKey(HcfObjectBase *self)
924 {
925     if (self == NULL) {
926         return;
927     }
928     if (!IsClassMatch(self, GetEccPubKeyClass())) {
929         return;
930     }
931     HcfOpensslEccPubKey *impl = (HcfOpensslEccPubKey *)self;
932     Openssl_EC_KEY_free(impl->ecKey);
933     impl->ecKey = NULL;
934     HcfFree(impl->fieldType);
935     impl->fieldType = NULL;
936     HcfFree(impl);
937 }
938 
DestroyEccPriKey(HcfObjectBase * self)939 static void DestroyEccPriKey(HcfObjectBase *self)
940 {
941     if (self == NULL) {
942         return;
943     }
944     if (!IsClassMatch(self, GetEccPriKeyClass())) {
945         return;
946     }
947     HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
948     Openssl_EC_KEY_free(impl->ecKey);
949     impl->ecKey = NULL;
950     HcfFree(impl->fieldType);
951     impl->fieldType = NULL;
952     HcfFree(impl);
953 }
954 
DestroyEccKeyPair(HcfObjectBase * self)955 static void DestroyEccKeyPair(HcfObjectBase *self)
956 {
957     if (self == NULL) {
958         return;
959     }
960     if (!IsClassMatch(self, GetEccKeyPairClass())) {
961         return;
962     }
963     HcfOpensslEccKeyPair *impl = (HcfOpensslEccKeyPair *)self;
964     if (impl->base.pubKey != NULL) {
965         DestroyEccPubKey((HcfObjectBase *)impl->base.pubKey);
966         impl->base.pubKey = NULL;
967     }
968     if (impl->base.priKey != NULL) {
969         DestroyEccPriKey((HcfObjectBase *)impl->base.priKey);
970         impl->base.priKey = NULL;
971     }
972     HcfFree(impl);
973 }
974 
GetEccPubKeyAlgorithm(HcfKey * self)975 static const char *GetEccPubKeyAlgorithm(HcfKey *self)
976 {
977     if (self == NULL) {
978         LOGE("Invalid input parameter.");
979         return NULL;
980     }
981     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
982         return NULL;
983     }
984     return OPENSSL_ECC_ALGORITHM;
985 }
986 
GetEccPriKeyAlgorithm(HcfKey * self)987 static const char *GetEccPriKeyAlgorithm(HcfKey *self)
988 {
989     if (self == NULL) {
990         LOGE("Invalid input parameter.");
991         return NULL;
992     }
993     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
994         return NULL;
995     }
996     return OPENSSL_ECC_ALGORITHM;
997 }
998 
GetEccPubKeyFormat(HcfKey * self)999 static const char *GetEccPubKeyFormat(HcfKey *self)
1000 {
1001     if (self == NULL) {
1002         LOGE("Invalid input parameter.");
1003         return NULL;
1004     }
1005     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
1006         return NULL;
1007     }
1008     return OPENSSL_ECC_PUB_KEY_FORMAT;
1009 }
1010 
GetEccPriKeyFormat(HcfKey * self)1011 static const char *GetEccPriKeyFormat(HcfKey *self)
1012 {
1013     if (self == NULL) {
1014         LOGE("Invalid input parameter.");
1015         return NULL;
1016     }
1017     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
1018         return NULL;
1019     }
1020     return OPENSSL_ECC_PRI_KEY_FORMAT;
1021 }
1022 
GetEccPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)1023 static HcfResult GetEccPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
1024 {
1025     if ((self == NULL) || (returnBlob == NULL)) {
1026         LOGE("Invalid input parameter.");
1027         return HCF_INVALID_PARAMS;
1028     }
1029     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
1030         return HCF_INVALID_PARAMS;
1031     }
1032 
1033     HcfOpensslEccPubKey *impl = (HcfOpensslEccPubKey *)self;
1034     if (impl->curveId != 0) {
1035         LOGD("have a curveId");
1036         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
1037     } else {
1038         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
1039     }
1040 
1041     unsigned char *returnData = NULL;
1042     int returnDataLen = Openssl_i2d_EC_PUBKEY(impl->ecKey, &returnData);
1043     if (returnDataLen <= 0) {
1044         LOGD("[error] i2d_EC_PUBKEY fail");
1045         HcfPrintOpensslError();
1046         return HCF_ERR_CRYPTO_OPERATION;
1047     }
1048     LOGD("ECC pubKey i2d success");
1049     returnBlob->data = returnData;
1050     returnBlob->len = returnDataLen;
1051     return HCF_SUCCESS;
1052 }
1053 
GetEccPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)1054 static HcfResult GetEccPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
1055 {
1056     if ((self == NULL) || (returnBlob == NULL)) {
1057         LOGE("Invalid input parameter.");
1058         return HCF_INVALID_PARAMS;
1059     }
1060     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
1061         return HCF_INVALID_PARAMS;
1062     }
1063 
1064     HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
1065     if (impl->curveId != 0) {
1066         LOGD("have a curveId");
1067         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
1068     } else {
1069         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
1070     }
1071     // keep consistence of 3.2
1072     Openssl_EC_KEY_set_enc_flags(impl->ecKey, EC_PKEY_NO_PUBKEY);
1073     // if the convert key has no pubKey, it will generate pub key automatically,
1074     // and set the no pubKey flag to ensure the consistency of blob.
1075     unsigned char *returnData = NULL;
1076     int returnDataLen = Openssl_i2d_ECPrivateKey(impl->ecKey, &returnData);
1077     if (returnDataLen <= 0) {
1078         LOGD("[error] i2d_ECPrivateKey fail.");
1079         HcfPrintOpensslError();
1080         return HCF_ERR_CRYPTO_OPERATION;
1081     }
1082     LOGD("ECC priKey i2d success");
1083     returnBlob->data = returnData;
1084     returnBlob->len = returnDataLen;
1085     return HCF_SUCCESS;
1086 }
1087 
EccPriKeyClearMem(HcfPriKey * self)1088 static void EccPriKeyClearMem(HcfPriKey *self)
1089 {
1090     if (self == NULL) {
1091         return;
1092     }
1093     if (!IsClassMatch((HcfObjectBase *)self, GetEccPriKeyClass())) {
1094         return;
1095     }
1096     HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
1097     Openssl_EC_KEY_free(impl->ecKey);
1098     impl->ecKey = NULL;
1099 }
1100 
GetCurveName(const HcfKey * self,const bool isPriavte,char ** returnString)1101 static HcfResult GetCurveName(const HcfKey *self, const bool isPriavte, char **returnString)
1102 {
1103     int32_t curveId = 0;
1104     if (isPriavte) {
1105         curveId = ((HcfOpensslEccPriKey *)self)->curveId;
1106     } else {
1107         curveId = ((HcfOpensslEccPubKey *)self)->curveId;
1108     }
1109 
1110     char *tmp = NULL;
1111     if (GetCurveNameByCurveId(curveId, &tmp) != HCF_SUCCESS) {
1112         LOGE("get vurveName by curveId failed.");
1113         return HCF_INVALID_PARAMS;
1114     }
1115 
1116     if (tmp == NULL) {
1117         LOGE("tmp is null.");
1118         return HCF_INVALID_PARAMS;
1119     }
1120     size_t len = HcfStrlen(tmp);
1121     if (len == 0) {
1122         LOGE("fieldType is empty!");
1123         return HCF_INVALID_PARAMS;
1124     }
1125 
1126     *returnString = (char *)HcfMalloc(len + 1, 0);
1127     if (*returnString == NULL) {
1128         LOGE("Alloc returnString memory failed.");
1129         return HCF_ERR_MALLOC;
1130     }
1131     (void)memcpy_s(*returnString, len, tmp, len);
1132     return HCF_SUCCESS;
1133 }
1134 
CheckEcKeySelf(const HcfKey * self,bool * isPrivate)1135 static HcfResult CheckEcKeySelf(const HcfKey *self, bool *isPrivate)
1136 {
1137     if (IsClassMatch((HcfObjectBase *)self, GetEccPubKeyClass())) {
1138         *isPrivate = false;
1139         return HCF_SUCCESS;
1140     } else if (IsClassMatch((HcfObjectBase *)self, GetEccPriKeyClass())) {
1141         if (((HcfOpensslEccPriKey *)self)->ecKey == NULL) {
1142             LOGE("Cannot use priKey after free");
1143             return HCF_INVALID_PARAMS;
1144         }
1145         *isPrivate = true;
1146         return HCF_SUCCESS;
1147     } else {
1148         return HCF_INVALID_PARAMS;
1149     }
1150 }
1151 
GetEcKeySpecBigInteger(const HcfKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1152 static HcfResult GetEcKeySpecBigInteger(const HcfKey *self, const AsyKeySpecItem item,
1153     HcfBigInteger *returnBigInteger)
1154 {
1155     if (self == NULL || returnBigInteger == NULL) {
1156         LOGE("Invalid input parameter.");
1157         return HCF_INVALID_PARAMS;
1158     }
1159     bool isPrivate;
1160     HcfResult res = CheckEcKeySelf(self, &isPrivate);
1161     if (res != HCF_SUCCESS) {
1162         LOGE("Invalid input key");
1163         return HCF_INVALID_PARAMS;
1164     }
1165     const EC_GROUP *group = NULL;
1166     if (isPrivate) {
1167         group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPriKey *)self)->ecKey);
1168     } else {
1169         group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey);
1170     }
1171     switch (item) {
1172         case ECC_FP_P_BN:
1173         case ECC_A_BN:
1174         case ECC_B_BN:
1175             res = GetCurveGFp(group, item, returnBigInteger);
1176             break;
1177         case ECC_G_X_BN:
1178         case ECC_G_Y_BN:
1179             res = GetGenerator(group, item, returnBigInteger);
1180             break;
1181         case ECC_N_BN:
1182             res = GetOrder(group, returnBigInteger);
1183             break;
1184         case ECC_SK_BN:
1185         case ECC_PK_X_BN:
1186         case ECC_PK_Y_BN:
1187             res = GetPkSkBigInteger(self, isPrivate, item, returnBigInteger);
1188             break;
1189         default:
1190             LOGE("Invalid ecc key big number spec!");
1191             res = HCF_INVALID_PARAMS;
1192             break;
1193     }
1194     return res;
1195 }
1196 
GetEcKeySpecString(const HcfKey * self,const AsyKeySpecItem item,char ** returnString)1197 static HcfResult GetEcKeySpecString(const HcfKey *self, const AsyKeySpecItem item, char **returnString)
1198 {
1199     if (self == NULL || returnString == NULL) {
1200         LOGE("Invalid input parameter.");
1201         return HCF_INVALID_PARAMS;
1202     }
1203     bool isPrivate;
1204     HcfResult res = CheckEcKeySelf(self, &isPrivate);
1205     if (res != HCF_SUCCESS) {
1206         LOGE("Invalid input key");
1207         return HCF_INVALID_PARAMS;
1208     }
1209 
1210     switch (item) {
1211         case ECC_FIELD_TYPE_STR:
1212             res = GetFieldType(self, isPrivate, returnString);
1213             break;
1214         case ECC_CURVE_NAME_STR:
1215             res = GetCurveName(self, isPrivate, returnString);
1216             break;
1217         default:
1218             res = HCF_INVALID_PARAMS;
1219             LOGE("Invalid spec of ec string");
1220             break;
1221     }
1222     return res;
1223 }
1224 
GetEcKeySpecInt(const HcfKey * self,const AsyKeySpecItem item,int * returnInt)1225 static HcfResult GetEcKeySpecInt(const HcfKey *self, const AsyKeySpecItem item, int *returnInt)
1226 {
1227     if (self == NULL || returnInt == NULL) {
1228         LOGE("Invalid input parameter.");
1229         return HCF_INVALID_PARAMS;
1230     }
1231     bool isPrivate;
1232     HcfResult res = CheckEcKeySelf(self, &isPrivate);
1233     if (res != HCF_SUCCESS) {
1234         LOGE("Invalid input key");
1235         return HCF_INVALID_PARAMS;
1236     }
1237     const EC_GROUP *group = NULL;
1238     if (isPrivate) {
1239         group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPriKey *)self)->ecKey);
1240     } else {
1241         group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey);
1242     }
1243     switch (item) {
1244         case ECC_H_INT:
1245             res = GetCofactor(group, returnInt);
1246             break;
1247         case ECC_FIELD_SIZE_INT:
1248             res = GetFieldSize(group, returnInt);
1249             break;
1250         default:
1251             res = HCF_INVALID_PARAMS;
1252             LOGE("invalid ec key int spec");
1253             break;
1254     }
1255     return res;
1256 }
1257 
GetECPubKeySpecBigInteger(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1258 static HcfResult GetECPubKeySpecBigInteger(const HcfPubKey *self, const AsyKeySpecItem item,
1259     HcfBigInteger *returnBigInteger)
1260 {
1261     return GetEcKeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
1262 }
1263 
GetECPubKeySpecString(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)1264 static HcfResult GetECPubKeySpecString(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
1265 {
1266     return GetEcKeySpecString((HcfKey *)self, item, returnString);
1267 }
1268 
GetECPubKeySpecInt(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)1269 static HcfResult GetECPubKeySpecInt(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
1270 {
1271     return GetEcKeySpecInt((HcfKey *)self, item, returnInt);
1272 }
1273 
GetECPriKeySpecBigInteger(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1274 static HcfResult GetECPriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item,
1275     HcfBigInteger *returnBigInteger)
1276 {
1277     return GetEcKeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
1278 }
1279 
GetECPriKeySpecString(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)1280 static HcfResult GetECPriKeySpecString(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
1281 {
1282     return GetEcKeySpecString((HcfKey *)self, item, returnString);
1283 }
1284 
GetECPriKeySpecInt(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)1285 static HcfResult GetECPriKeySpecInt(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
1286 {
1287     return GetEcKeySpecInt((HcfKey *)self, item, returnInt);
1288 }
1289 
PackEccPubKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslEccPubKey ** returnObj)1290 static HcfResult PackEccPubKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
1291     HcfOpensslEccPubKey **returnObj)
1292 {
1293     HcfOpensslEccPubKey *returnPubKey = (HcfOpensslEccPubKey *)HcfMalloc(sizeof(HcfOpensslEccPubKey), 0);
1294     if (returnPubKey == NULL) {
1295         LOGE("Failed to allocate returnPubKey memory!");
1296         return HCF_ERR_MALLOC;
1297     }
1298     char *tmpFieldType = NULL;
1299     if (fieldType != NULL) {
1300         size_t len = HcfStrlen(fieldType);
1301         if (len == 0) {
1302             LOGE("fieldType is empty!");
1303             HcfFree(returnPubKey);
1304             return HCF_INVALID_PARAMS;
1305         }
1306         tmpFieldType = (char *)HcfMalloc(len + 1, 0);
1307         if (tmpFieldType == NULL) {
1308             LOGE("Alloc tmpFieldType memory failed.");
1309             HcfFree(returnPubKey);
1310             return HCF_ERR_MALLOC;
1311         }
1312         (void)memcpy_s(tmpFieldType, len, fieldType, len);
1313     }
1314 
1315     returnPubKey->base.base.base.destroy = DestroyEccPubKey;
1316     returnPubKey->base.base.base.getClass = GetEccPubKeyClass;
1317     returnPubKey->base.base.getAlgorithm = GetEccPubKeyAlgorithm;
1318     returnPubKey->base.base.getEncoded = GetEccPubKeyEncoded;
1319     returnPubKey->base.base.getFormat = GetEccPubKeyFormat;
1320     returnPubKey->base.getAsyKeySpecBigInteger = GetECPubKeySpecBigInteger;
1321     returnPubKey->base.getAsyKeySpecString = GetECPubKeySpecString;
1322     returnPubKey->base.getAsyKeySpecInt = GetECPubKeySpecInt;
1323     returnPubKey->curveId = curveId;
1324     returnPubKey->ecKey = ecKey;
1325     returnPubKey->fieldType = tmpFieldType;
1326 
1327     *returnObj = returnPubKey;
1328     return HCF_SUCCESS;
1329 }
1330 
PackEccPriKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslEccPriKey ** returnObj)1331 static HcfResult PackEccPriKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
1332     HcfOpensslEccPriKey **returnObj)
1333 {
1334     HcfOpensslEccPriKey *returnPriKey = (HcfOpensslEccPriKey *)HcfMalloc(sizeof(HcfOpensslEccPriKey), 0);
1335     if (returnPriKey == NULL) {
1336         LOGE("Failed to allocate returnPriKey memory!");
1337         return HCF_ERR_MALLOC;
1338     }
1339     char *tmpFieldType = NULL;
1340     if (fieldType != NULL) {
1341         size_t len = HcfStrlen(fieldType);
1342         if (len == 0) {
1343             LOGE("fieldType is empty!");
1344             HcfFree(returnPriKey);
1345             return HCF_INVALID_PARAMS;
1346         }
1347         tmpFieldType = (char *)HcfMalloc(len + 1, 0);
1348         if (tmpFieldType == NULL) {
1349             LOGE("Alloc tmpFieldType memory failed.");
1350             HcfFree(returnPriKey);
1351             return HCF_ERR_MALLOC;
1352         }
1353         (void)memcpy_s(tmpFieldType, len, fieldType, len);
1354     }
1355 
1356     returnPriKey->base.base.base.destroy = DestroyEccPriKey;
1357     returnPriKey->base.base.base.getClass = GetEccPriKeyClass;
1358     returnPriKey->base.base.getAlgorithm = GetEccPriKeyAlgorithm;
1359     returnPriKey->base.base.getEncoded = GetEccPriKeyEncoded;
1360     returnPriKey->base.base.getFormat = GetEccPriKeyFormat;
1361     returnPriKey->base.clearMem = EccPriKeyClearMem;
1362     returnPriKey->base.getAsyKeySpecBigInteger = GetECPriKeySpecBigInteger;
1363     returnPriKey->base.getAsyKeySpecString = GetECPriKeySpecString;
1364     returnPriKey->base.getAsyKeySpecInt = GetECPriKeySpecInt;
1365     returnPriKey->curveId = curveId;
1366     returnPriKey->ecKey = ecKey;
1367     returnPriKey->fieldType = tmpFieldType;
1368 
1369     *returnObj = returnPriKey;
1370     return HCF_SUCCESS;
1371 }
1372 
PackEccKeyPair(HcfOpensslEccPubKey * pubKey,HcfOpensslEccPriKey * priKey,HcfOpensslEccKeyPair ** returnObj)1373 static HcfResult PackEccKeyPair(HcfOpensslEccPubKey *pubKey, HcfOpensslEccPriKey *priKey,
1374     HcfOpensslEccKeyPair **returnObj)
1375 {
1376     HcfOpensslEccKeyPair *returnKeyPair = (HcfOpensslEccKeyPair *)HcfMalloc(sizeof(HcfOpensslEccKeyPair), 0);
1377     if (returnKeyPair == NULL) {
1378         LOGE("Failed to allocate returnKeyPair memory!");
1379         return HCF_ERR_MALLOC;
1380     }
1381     returnKeyPair->base.base.getClass = GetEccKeyPairClass;
1382     returnKeyPair->base.base.destroy = DestroyEccKeyPair;
1383     returnKeyPair->base.pubKey = (HcfPubKey *)pubKey;
1384     returnKeyPair->base.priKey = (HcfPriKey *)priKey;
1385 
1386     *returnObj = returnKeyPair;
1387     return HCF_SUCCESS;
1388 }
1389 
ConvertEcPubKey(int32_t curveId,HcfBlob * pubKeyBlob,HcfOpensslEccPubKey ** returnPubKey)1390 static HcfResult ConvertEcPubKey(int32_t curveId, HcfBlob *pubKeyBlob, HcfOpensslEccPubKey **returnPubKey)
1391 {
1392     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
1393     EC_KEY *ecKey = Openssl_d2i_EC_PUBKEY(NULL, &tmpData, pubKeyBlob->len);
1394     if (ecKey == NULL) {
1395         LOGD("[error] d2i_EC_PUBKEY fail.");
1396         HcfPrintOpensslError();
1397         return HCF_ERR_CRYPTO_OPERATION;
1398     }
1399     HcfResult res = PackEccPubKey(curveId, ecKey, g_eccGenerateFieldType, returnPubKey);
1400     if (res != HCF_SUCCESS) {
1401         LOGD("[error] PackEccPubKey failed.");
1402         Openssl_EC_KEY_free(ecKey);
1403         return res;
1404     }
1405     return HCF_SUCCESS;
1406 }
1407 
ConvertEcPriKey(int32_t curveId,HcfBlob * priKeyBlob,HcfOpensslEccPriKey ** returnPriKey)1408 static HcfResult ConvertEcPriKey(int32_t curveId, HcfBlob *priKeyBlob, HcfOpensslEccPriKey **returnPriKey)
1409 {
1410     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
1411     EC_KEY *ecKey = Openssl_d2i_ECPrivateKey(NULL, &tmpData, priKeyBlob->len);
1412     if (ecKey == NULL) {
1413         LOGD("[error] d2i_ECPrivateKey fail");
1414         HcfPrintOpensslError();
1415         return HCF_ERR_CRYPTO_OPERATION;
1416     }
1417     HcfResult res = PackEccPriKey(curveId, ecKey, g_eccGenerateFieldType, returnPriKey);
1418     if (res != HCF_SUCCESS) {
1419         LOGD("[error] PackEccPriKey failed.");
1420         Openssl_EC_KEY_free(ecKey);
1421         return res;
1422     }
1423     return HCF_SUCCESS;
1424 }
1425 
EngineConvertEccKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)1426 static HcfResult EngineConvertEccKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
1427     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
1428 {
1429     (void)params;
1430     if ((self == NULL) || (returnKeyPair == NULL)) {
1431         LOGE("Invalid input parameter.");
1432         return HCF_INVALID_PARAMS;
1433     }
1434     if (!IsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1435         return HCF_INVALID_PARAMS;
1436     }
1437     bool pubKeyValid = IsBlobValid(pubKeyBlob);
1438     bool priKeyValid = IsBlobValid(priKeyBlob);
1439     if ((!pubKeyValid) && (!priKeyValid)) {
1440         LOGE("The private key and public key cannot both be NULL.");
1441         return HCF_INVALID_PARAMS;
1442     }
1443 
1444     HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1445     HcfResult res = HCF_SUCCESS;
1446     HcfOpensslEccPubKey *pubKey = NULL;
1447     HcfOpensslEccPriKey *priKey = NULL;
1448     HcfOpensslEccKeyPair *keyPair = NULL;
1449     do {
1450         if (pubKeyValid) {
1451             res = ConvertEcPubKey(impl->curveId, pubKeyBlob, &pubKey);
1452             if (res != HCF_SUCCESS) {
1453                 break;
1454             }
1455         }
1456         if (priKeyValid) {
1457             res = ConvertEcPriKey(impl->curveId, priKeyBlob, &priKey);
1458             if (res != HCF_SUCCESS) {
1459                 break;
1460             }
1461         }
1462         res = PackEccKeyPair(pubKey, priKey, &keyPair);
1463     } while (0);
1464     if (res != HCF_SUCCESS) {
1465         HcfObjDestroy(pubKey);
1466         HcfObjDestroy(priKey);
1467         return res;
1468     }
1469 
1470     *returnKeyPair = (HcfKeyPair *)keyPair;
1471     return HCF_SUCCESS;
1472 }
1473 
PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfPubKey ** returnObj)1474 static HcfResult PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1475     EC_KEY *ecKey, HcfPubKey **returnObj)
1476 {
1477     HcfOpensslEccPubKey *pubKey = NULL;
1478     HcfResult res = PackEccPubKey(impl->curveId, ecKey, fieldType, &pubKey);
1479     if (res != HCF_SUCCESS) {
1480         return res;
1481     }
1482     *returnObj = (HcfPubKey *)pubKey;
1483     return HCF_SUCCESS;
1484 }
1485 
PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfPriKey ** returnObj)1486 static HcfResult PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1487     EC_KEY *ecKey, HcfPriKey **returnObj)
1488 {
1489     HcfOpensslEccPriKey *priKey = NULL;
1490     HcfResult res = PackEccPriKey(impl->curveId, ecKey, fieldType, &priKey);
1491     if (res != HCF_SUCCESS) {
1492         return res;
1493     }
1494     *returnObj = (HcfPriKey *)priKey;
1495     return HCF_SUCCESS;
1496 }
1497 
CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfKeyPair ** returnObj)1498 static HcfResult CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1499     EC_KEY *ecKey, HcfKeyPair **returnObj)
1500 {
1501     EC_KEY *ecPriKey = EC_KEY_dup(ecKey);
1502     if (ecPriKey == NULL) {
1503         LOGD("[error] copy ecKey fail.");
1504         return HCF_ERR_CRYPTO_OPERATION;
1505     }
1506     HcfOpensslEccPriKey *priKey = NULL;
1507     HcfResult res = PackEccPriKey(impl->curveId, ecPriKey, fieldType, &priKey);
1508     if (res != HCF_SUCCESS) {
1509         Openssl_EC_KEY_free(ecPriKey);
1510         return res;
1511     }
1512     HcfOpensslEccPubKey *pubKey = NULL;
1513     EC_KEY *ecPubKey = EC_KEY_dup(ecKey);
1514     if (ecPubKey == NULL) {
1515         LOGD("[error] copy ecKey fail.");
1516         HcfObjDestroy(priKey);
1517         return HCF_ERR_CRYPTO_OPERATION;
1518     }
1519     res = PackEccPubKey(impl->curveId, ecPubKey, fieldType, &pubKey);
1520     if (res != HCF_SUCCESS) {
1521         HcfObjDestroy(priKey);
1522         Openssl_EC_KEY_free(ecPubKey);
1523         return res;
1524     }
1525 
1526     HcfOpensslEccKeyPair *returnKeyPair = (HcfOpensslEccKeyPair *)HcfMalloc(sizeof(HcfOpensslEccKeyPair), 0);
1527     if (returnKeyPair == NULL) {
1528         LOGE("Failed to allocate returnKeyPair memory!");
1529         HcfObjDestroy(pubKey);
1530         HcfObjDestroy(priKey);
1531         return HCF_ERR_MALLOC;
1532     }
1533     returnKeyPair->base.base.getClass = GetEccKeyPairClass;
1534     returnKeyPair->base.base.destroy = DestroyEccKeyPair;
1535     returnKeyPair->base.pubKey = (HcfPubKey *)pubKey;
1536     returnKeyPair->base.priKey = (HcfPriKey *)priKey;
1537 
1538     *returnObj = (HcfKeyPair *)returnKeyPair;
1539     return HCF_SUCCESS;
1540 }
1541 
EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnObj)1542 static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnObj)
1543 {
1544     if ((self == NULL) || (returnObj == NULL)) {
1545         LOGE("Invalid input parameter.");
1546         return HCF_INVALID_PARAMS;
1547     }
1548     if (!IsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1549         return HCF_INVALID_PARAMS;
1550     }
1551 
1552     HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1553     EC_KEY *ecKey = NULL;
1554     HcfResult res = NewEcKeyPair(impl->curveId, &ecKey);
1555     if (res != HCF_SUCCESS) {
1556         return res;
1557     }
1558     res = CreateAndAssignKeyPair(impl, g_eccGenerateFieldType, ecKey, returnObj);
1559     Openssl_EC_KEY_free(ecKey);
1560     if (res != HCF_SUCCESS) {
1561         LOGE("CreateAndAssignKeyPair failed.");
1562         return res;
1563     }
1564     return HCF_SUCCESS;
1565 }
1566 
EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfKeyPair ** returnKeyPair)1567 static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1568     HcfKeyPair **returnKeyPair)
1569 {
1570     if ((self == NULL) || (returnKeyPair == NULL)) {
1571         LOGE("Invalid input parameter.");
1572         return HCF_INVALID_PARAMS;
1573     }
1574     if (!IsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1575         return HCF_INVALID_PARAMS;
1576     }
1577 
1578     HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1579     EC_KEY *ecKey = NULL;
1580     HcfResult res = GenKeyPairEcKeyBySpec(params, &ecKey);
1581     if (res != HCF_SUCCESS) {
1582         LOGE("Gen ec key pair with spec failed.");
1583         return res;
1584     }
1585 
1586     // curveId == 0 means no curve to match.
1587     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1588     if (curveId != 0) {
1589         impl->curveId = curveId;
1590     }
1591     // deep copy of ecKey, free ecKey whether it succeed or failed.
1592     res = CreateAndAssignKeyPair(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnKeyPair);
1593     Openssl_EC_KEY_free(ecKey);
1594     if (res != HCF_SUCCESS) {
1595         LOGE("CreateAndAssignKeyPair failed.");
1596         return res;
1597     }
1598 
1599     return HCF_SUCCESS;
1600 }
1601 
EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPubKey ** returnPubKey)1602 static HcfResult EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1603     HcfPubKey **returnPubKey)
1604 {
1605     if ((self == NULL) || (returnPubKey == NULL)) {
1606         LOGE("Invalid input parameter.");
1607         return HCF_INVALID_PARAMS;
1608     }
1609     if (!IsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1610         return HCF_INVALID_PARAMS;
1611     }
1612 
1613     HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1614     EC_KEY *ecKey = NULL;
1615     HcfResult res = GenPubKeyEcKeyBySpec(params, &ecKey);
1616     if (res != HCF_SUCCESS) {
1617         LOGE("Gen ec pubKey with spec failed.");
1618         return res;
1619     }
1620     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1621     if (curveId != 0) {
1622         impl->curveId = curveId;
1623     }
1624     res = PackAndAssignPubKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPubKey);
1625     if (res != HCF_SUCCESS) {
1626         LOGE("PackAndAssignPubKey failed.");
1627         Openssl_EC_KEY_free(ecKey);
1628         return res;
1629     }
1630 
1631     return HCF_SUCCESS;
1632 }
1633 
EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPriKey ** returnPriKey)1634 static HcfResult EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1635     HcfPriKey **returnPriKey)
1636 {
1637     if ((self == NULL) || (returnPriKey == NULL)) {
1638         LOGE("Invalid input parameter.");
1639         return HCF_INVALID_PARAMS;
1640     }
1641     if (!IsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1642         return HCF_INVALID_PARAMS;
1643     }
1644 
1645     HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1646     EC_KEY *ecKey = NULL;
1647     HcfResult res = GenPriKeyEcKeyBySpec(params, &ecKey);
1648     if (res != HCF_SUCCESS) {
1649         LOGE("Gen ec pubKey with spec failed.");
1650         return res;
1651     }
1652 
1653     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1654     if (curveId != 0) {
1655         impl->curveId = curveId;
1656     }
1657 
1658     res = PackAndAssignPriKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPriKey);
1659     if (res != HCF_SUCCESS) {
1660         LOGE("PackAndAssignPriKey failed.");
1661         Openssl_EC_KEY_free(ecKey);
1662         return res;
1663     }
1664 
1665     return HCF_SUCCESS;
1666 }
1667 
HcfAsyKeyGeneratorSpiEccCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1668 HcfResult HcfAsyKeyGeneratorSpiEccCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1669 {
1670     if (params == NULL || returnObj == NULL) {
1671         LOGE("Invalid input parameter.");
1672         return HCF_INVALID_PARAMS;
1673     }
1674     int32_t curveId = 0;
1675     if (params->bits != 0) {
1676         if (GetOpensslCurveId(params->bits, &curveId) != HCF_SUCCESS) {
1677             return HCF_INVALID_PARAMS;
1678         }
1679     }
1680 
1681     HcfAsyKeyGeneratorSpiOpensslEccImpl *returnImpl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)HcfMalloc(
1682         sizeof(HcfAsyKeyGeneratorSpiOpensslEccImpl), 0);
1683     if (returnImpl == NULL) {
1684         LOGE("Failed to allocate returnImpl memroy!");
1685         return HCF_ERR_MALLOC;
1686     }
1687     returnImpl->base.base.getClass = GetEccKeyPairGeneratorClass;
1688     returnImpl->base.base.destroy = DestroyEccKeyPairGenerator;
1689     returnImpl->base.engineConvertKey = EngineConvertEccKey;
1690     returnImpl->base.engineGenerateKeyPair = EngineGenerateKeyPair;
1691     returnImpl->base.engineGenerateKeyPairBySpec = EngineGenerateKeyPairBySpec;
1692     returnImpl->base.engineGeneratePubKeyBySpec = EngineGeneratePubKeyBySpec;
1693     returnImpl->base.engineGeneratePriKeyBySpec = EngineGeneratePriKeyBySpec;
1694     returnImpl->curveId = curveId;
1695 
1696     *returnObj = (HcfAsyKeyGeneratorSpi *)returnImpl;
1697     return HCF_SUCCESS;
1698 }
1699