1 /*
2 * Copyright (C) 2023-2024 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 <openssl/param_build.h>
26 #include "utils.h"
27
28 #define OPENSSL_ECC_KEY_GENERATOR_CLASS "OPENSSL.ECC.KEY_GENERATOR_CLASS"
29 #define OPENSSL_ECC_ALGORITHM "EC"
30 #define OPENSSL_ECC_PUB_KEY_FORMAT "X.509"
31 #define OPENSSL_ECC_PRI_KEY_FORMAT "PKCS#8"
32 #define OPENSSL_ECC160_BITS 160
33 #define OPENSSL_ECC192_BITS 192
34 #define OPENSSL_ECC224_BITS 224
35 #define OPENSSL_ECC256_BITS 256
36 #define OPENSSL_ECC320_BITS 320
37 #define OPENSSL_ECC384_BITS 384
38 #define OPENSSL_ECC512_BITS 512
39 #define OPENSSL_ECC521_BITS 521
40
41 #define UNCOMPRESSED_FORMAT "UNCOMPRESSED"
42 #define COMPRESSED_FORMAT "COMPRESSED"
43
44 static const char *g_eccGenerateFieldType = "Fp";
45
46 typedef struct {
47 BIGNUM *p;
48 BIGNUM *b;
49 BIGNUM *x;
50 BIGNUM *y;
51 }HcfBigIntegerParams;
52
53 typedef struct {
54 HcfAsyKeyGeneratorSpi base;
55
56 int32_t curveId;
57 } HcfAsyKeyGeneratorSpiOpensslEccImpl;
58
CheckEc224CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)59 static HcfResult CheckEc224CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
60 {
61 BIGNUM *pStd = NULL;
62 BIGNUM *bStd = NULL;
63 BIGNUM *xStd = NULL;
64 BIGNUM *yStd = NULL;
65 pStd = OpensslBin2Bn(g_ecc224CorrectBigP, NID_secp224r1_len, NULL);
66 bStd = OpensslBin2Bn(g_ecc224CorrectBigB, NID_secp224r1_len, NULL);
67 xStd = OpensslBin2Bn(g_ecc224CorrectBigGX, NID_secp224r1_len, NULL);
68 yStd = OpensslBin2Bn(g_ecc224CorrectBigGY, NID_secp224r1_len, NULL);
69 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
70 LOGD("[error] EC 224 Curve convert to BN fail");
71 FreeCurveBigNum(pStd, bStd, xStd, yStd);
72 return HCF_ERR_CRYPTO_OPERATION;
73 }
74 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
75 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
76 FreeCurveBigNum(pStd, bStd, xStd, yStd);
77 return HCF_SUCCESS;
78 }
79 LOGD("[error] EC 224 compare fail");
80 FreeCurveBigNum(pStd, bStd, xStd, yStd);
81 return HCF_INVALID_PARAMS;
82 }
83
CheckEc256CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)84 static HcfResult CheckEc256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
85 {
86 BIGNUM *pStd = NULL;
87 BIGNUM *bStd = NULL;
88 BIGNUM *xStd = NULL;
89 BIGNUM *yStd = NULL;
90 pStd = OpensslBin2Bn(g_ecc256CorrectBigP, NID_X9_62_prime256v1_len, NULL);
91 bStd = OpensslBin2Bn(g_ecc256CorrectBigB, NID_X9_62_prime256v1_len, NULL);
92 xStd = OpensslBin2Bn(g_ecc256CorrectBigGX, NID_X9_62_prime256v1_len, NULL);
93 yStd = OpensslBin2Bn(g_ecc256CorrectBigGY, NID_X9_62_prime256v1_len, NULL);
94 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
95 LOGD("[error] EC 256 Curve convert to BN fail");
96 FreeCurveBigNum(pStd, bStd, xStd, yStd);
97 return HCF_ERR_CRYPTO_OPERATION;
98 }
99 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
100 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
101 FreeCurveBigNum(pStd, bStd, xStd, yStd);
102 return HCF_SUCCESS;
103 }
104 LOGD("[error] EC 256 compare fail");
105 FreeCurveBigNum(pStd, bStd, xStd, yStd);
106 return HCF_INVALID_PARAMS;
107 }
108
CheckEc384CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)109 static HcfResult CheckEc384CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
110 {
111 BIGNUM *pStd = NULL;
112 BIGNUM *bStd = NULL;
113 BIGNUM *xStd = NULL;
114 BIGNUM *yStd = NULL;
115 pStd = OpensslBin2Bn(g_ecc384CorrectBigP, NID_secp384r1_len, NULL);
116 bStd = OpensslBin2Bn(g_ecc384CorrectBigB, NID_secp384r1_len, NULL);
117 xStd = OpensslBin2Bn(g_ecc384CorrectBigGX, NID_secp384r1_len, NULL);
118 yStd = OpensslBin2Bn(g_ecc384CorrectBigGY, NID_secp384r1_len, NULL);
119 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
120 LOGD("[error] EC 384 Curve convert to BN fail");
121 FreeCurveBigNum(pStd, bStd, xStd, yStd);
122 return HCF_ERR_CRYPTO_OPERATION;
123 }
124 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
125 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
126 FreeCurveBigNum(pStd, bStd, xStd, yStd);
127 return HCF_SUCCESS;
128 }
129 LOGD("[error] EC 384 compare fail");
130 FreeCurveBigNum(pStd, bStd, xStd, yStd);
131 return HCF_INVALID_PARAMS;
132 }
133
CheckEc521CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)134 static HcfResult CheckEc521CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
135 {
136 BIGNUM *pStd = NULL;
137 BIGNUM *bStd = NULL;
138 BIGNUM *xStd = NULL;
139 BIGNUM *yStd = NULL;
140 pStd = OpensslBin2Bn(g_ecc521CorrectBigP, NID_secp521r1_len, NULL);
141 bStd = OpensslBin2Bn(g_ecc521CorrectBigB, NID_secp521r1_len, NULL);
142 xStd = OpensslBin2Bn(g_ecc521CorrectBigGX, NID_secp521r1_len, NULL);
143 yStd = OpensslBin2Bn(g_ecc521CorrectBigGY, NID_secp521r1_len, NULL);
144 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
145 LOGD("[error] EC 521 Curve convert to BN fail");
146 FreeCurveBigNum(pStd, bStd, xStd, yStd);
147 return HCF_ERR_CRYPTO_OPERATION;
148 }
149 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
150 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
151 FreeCurveBigNum(pStd, bStd, xStd, yStd);
152 return HCF_SUCCESS;
153 }
154 LOGD("[error] EC 521 compare fail");
155 FreeCurveBigNum(pStd, bStd, xStd, yStd);
156 return HCF_INVALID_PARAMS;
157 }
158
CheckBP160r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)159 static HcfResult CheckBP160r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
160 {
161 BIGNUM *pStd = NULL;
162 BIGNUM *bStd = NULL;
163 BIGNUM *xStd = NULL;
164 BIGNUM *yStd = NULL;
165 pStd = OpensslBin2Bn(g_bp160r1CorrectBigP, NID_brainpoolP160r1_len, NULL);
166 bStd = OpensslBin2Bn(g_bp160r1CorrectBigB, NID_brainpoolP160r1_len, NULL);
167 xStd = OpensslBin2Bn(g_bp160r1CorrectBigGX, NID_brainpoolP160r1_len, NULL);
168 yStd = OpensslBin2Bn(g_bp160r1CorrectBigGY, NID_brainpoolP160r1_len, NULL);
169 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
170 LOGD("[error] BP 160r1 Curve convert to BN fail");
171 FreeCurveBigNum(pStd, bStd, xStd, yStd);
172 return HCF_ERR_CRYPTO_OPERATION;
173 }
174 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
175 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
176 FreeCurveBigNum(pStd, bStd, xStd, yStd);
177 return HCF_SUCCESS;
178 }
179 LOGD("[error] BP 160r1 compare fail");
180 FreeCurveBigNum(pStd, bStd, xStd, yStd);
181 return HCF_INVALID_PARAMS;
182 }
183
CheckBP160t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)184 static HcfResult CheckBP160t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
185 {
186 BIGNUM *pStd = NULL;
187 BIGNUM *bStd = NULL;
188 BIGNUM *xStd = NULL;
189 BIGNUM *yStd = NULL;
190 pStd = OpensslBin2Bn(g_bp160t1CorrectBigP, NID_brainpoolP160t1_len, NULL);
191 bStd = OpensslBin2Bn(g_bp160t1CorrectBigB, NID_brainpoolP160t1_len, NULL);
192 xStd = OpensslBin2Bn(g_bp160t1CorrectBigGX, NID_brainpoolP160t1_len, NULL);
193 yStd = OpensslBin2Bn(g_bp160t1CorrectBigGY, NID_brainpoolP160t1_len, NULL);
194 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
195 LOGD("[error] BP 160t1 Curve convert to BN fail");
196 FreeCurveBigNum(pStd, bStd, xStd, yStd);
197 return HCF_ERR_CRYPTO_OPERATION;
198 }
199 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
200 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
201 FreeCurveBigNum(pStd, bStd, xStd, yStd);
202 return HCF_SUCCESS;
203 }
204 LOGD("[error] BP 160t1 compare fail");
205 FreeCurveBigNum(pStd, bStd, xStd, yStd);
206 return HCF_INVALID_PARAMS;
207 }
208
CheckBP192r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)209 static HcfResult CheckBP192r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
210 {
211 BIGNUM *pStd = NULL;
212 BIGNUM *bStd = NULL;
213 BIGNUM *xStd = NULL;
214 BIGNUM *yStd = NULL;
215 pStd = OpensslBin2Bn(g_bp192r1CorrectBigP, NID_brainpoolP192r1_len, NULL);
216 bStd = OpensslBin2Bn(g_bp192r1CorrectBigB, NID_brainpoolP192r1_len, NULL);
217 xStd = OpensslBin2Bn(g_bp192r1CorrectBigGX, NID_brainpoolP192r1_len, NULL);
218 yStd = OpensslBin2Bn(g_bp192r1CorrectBigGY, NID_brainpoolP192r1_len, NULL);
219 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
220 LOGD("[error] BP 192r1 Curve convert to BN fail");
221 FreeCurveBigNum(pStd, bStd, xStd, yStd);
222 return HCF_ERR_CRYPTO_OPERATION;
223 }
224 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
225 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
226 FreeCurveBigNum(pStd, bStd, xStd, yStd);
227 return HCF_SUCCESS;
228 }
229 LOGD("[error] BP 192r1 compare fail");
230 FreeCurveBigNum(pStd, bStd, xStd, yStd);
231 return HCF_INVALID_PARAMS;
232 }
233
CheckBP192t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)234 static HcfResult CheckBP192t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
235 {
236 BIGNUM *pStd = NULL;
237 BIGNUM *bStd = NULL;
238 BIGNUM *xStd = NULL;
239 BIGNUM *yStd = NULL;
240 pStd = OpensslBin2Bn(g_bp192t1CorrectBigP, NID_brainpoolP192t1_len, NULL);
241 bStd = OpensslBin2Bn(g_bp192t1CorrectBigB, NID_brainpoolP192t1_len, NULL);
242 xStd = OpensslBin2Bn(g_bp192t1CorrectBigGX, NID_brainpoolP192t1_len, NULL);
243 yStd = OpensslBin2Bn(g_bp192t1CorrectBigGY, NID_brainpoolP192t1_len, NULL);
244 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
245 LOGD("[error] BP 192t1 Curve convert to BN fail");
246 FreeCurveBigNum(pStd, bStd, xStd, yStd);
247 return HCF_ERR_CRYPTO_OPERATION;
248 }
249 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
250 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
251 FreeCurveBigNum(pStd, bStd, xStd, yStd);
252 return HCF_SUCCESS;
253 }
254 LOGD("[error] BP 192t1 compare fail");
255 FreeCurveBigNum(pStd, bStd, xStd, yStd);
256 return HCF_INVALID_PARAMS;
257 }
258
CheckBP224r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)259 static HcfResult CheckBP224r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
260 {
261 BIGNUM *pStd = NULL;
262 BIGNUM *bStd = NULL;
263 BIGNUM *xStd = NULL;
264 BIGNUM *yStd = NULL;
265 pStd = OpensslBin2Bn(g_bp224r1CorrectBigP, NID_brainpoolP224r1_len, NULL);
266 bStd = OpensslBin2Bn(g_bp224r1CorrectBigB, NID_brainpoolP224r1_len, NULL);
267 xStd = OpensslBin2Bn(g_bp224r1CorrectBigGX, NID_brainpoolP224r1_len, NULL);
268 yStd = OpensslBin2Bn(g_bp224r1CorrectBigGY, NID_brainpoolP224r1_len, NULL);
269 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
270 LOGD("[error] BP 224r1 Curve convert to BN fail");
271 FreeCurveBigNum(pStd, bStd, xStd, yStd);
272 return HCF_ERR_CRYPTO_OPERATION;
273 }
274 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
275 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
276 FreeCurveBigNum(pStd, bStd, xStd, yStd);
277 return HCF_SUCCESS;
278 }
279 LOGD("[error] BP 224r1 compare fail");
280 FreeCurveBigNum(pStd, bStd, xStd, yStd);
281 return HCF_INVALID_PARAMS;
282 }
283
CheckBP224t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)284 static HcfResult CheckBP224t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
285 {
286 BIGNUM *pStd = NULL;
287 BIGNUM *bStd = NULL;
288 BIGNUM *xStd = NULL;
289 BIGNUM *yStd = NULL;
290 pStd = OpensslBin2Bn(g_bp224t1CorrectBigP, NID_brainpoolP224t1_len, NULL);
291 bStd = OpensslBin2Bn(g_bp224t1CorrectBigB, NID_brainpoolP224t1_len, NULL);
292 xStd = OpensslBin2Bn(g_bp224t1CorrectBigGX, NID_brainpoolP224t1_len, NULL);
293 yStd = OpensslBin2Bn(g_bp224t1CorrectBigGY, NID_brainpoolP224t1_len, NULL);
294 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
295 LOGD("[error] BP 224t1 Curve convert to BN fail");
296 FreeCurveBigNum(pStd, bStd, xStd, yStd);
297 return HCF_ERR_CRYPTO_OPERATION;
298 }
299 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
300 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
301 FreeCurveBigNum(pStd, bStd, xStd, yStd);
302 return HCF_SUCCESS;
303 }
304 LOGD("[error] BP 224t1 compare fail");
305 FreeCurveBigNum(pStd, bStd, xStd, yStd);
306 return HCF_INVALID_PARAMS;
307 }
308
CheckBP256r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)309 static HcfResult CheckBP256r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
310 {
311 BIGNUM *pStd = NULL;
312 BIGNUM *bStd = NULL;
313 BIGNUM *xStd = NULL;
314 BIGNUM *yStd = NULL;
315 pStd = OpensslBin2Bn(g_bp256r1CorrectBigP, NID_brainpoolP256r1_len, NULL);
316 bStd = OpensslBin2Bn(g_bp256r1CorrectBigB, NID_brainpoolP256r1_len, NULL);
317 xStd = OpensslBin2Bn(g_bp256r1CorrectBigGX, NID_brainpoolP256r1_len, NULL);
318 yStd = OpensslBin2Bn(g_bp256r1CorrectBigGY, NID_brainpoolP256r1_len, NULL);
319 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
320 LOGD("[error] BP 256r1 Curve convert to BN fail");
321 FreeCurveBigNum(pStd, bStd, xStd, yStd);
322 return HCF_ERR_CRYPTO_OPERATION;
323 }
324 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
325 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
326 FreeCurveBigNum(pStd, bStd, xStd, yStd);
327 return HCF_SUCCESS;
328 }
329 LOGD("[error] BP 256r1 compare fail");
330 FreeCurveBigNum(pStd, bStd, xStd, yStd);
331 return HCF_INVALID_PARAMS;
332 }
333
CheckBP256t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)334 static HcfResult CheckBP256t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
335 {
336 BIGNUM *pStd = NULL;
337 BIGNUM *bStd = NULL;
338 BIGNUM *xStd = NULL;
339 BIGNUM *yStd = NULL;
340 pStd = OpensslBin2Bn(g_bp256t1CorrectBigP, NID_brainpoolP256t1_len, NULL);
341 bStd = OpensslBin2Bn(g_bp256t1CorrectBigB, NID_brainpoolP256t1_len, NULL);
342 xStd = OpensslBin2Bn(g_bp256t1CorrectBigGX, NID_brainpoolP256t1_len, NULL);
343 yStd = OpensslBin2Bn(g_bp256t1CorrectBigGY, NID_brainpoolP256t1_len, NULL);
344 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
345 LOGD("[error] BP 256t1 Curve convert to BN fail");
346 FreeCurveBigNum(pStd, bStd, xStd, yStd);
347 return HCF_ERR_CRYPTO_OPERATION;
348 }
349 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
350 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
351 FreeCurveBigNum(pStd, bStd, xStd, yStd);
352 return HCF_SUCCESS;
353 }
354 FreeCurveBigNum(pStd, bStd, xStd, yStd);
355 return HCF_INVALID_PARAMS;
356 }
357
CheckBP320r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)358 static HcfResult CheckBP320r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
359 {
360 BIGNUM *pStd = NULL;
361 BIGNUM *bStd = NULL;
362 BIGNUM *xStd = NULL;
363 BIGNUM *yStd = NULL;
364 pStd = OpensslBin2Bn(g_bp320r1CorrectBigP, NID_brainpoolP320r1_len, NULL);
365 bStd = OpensslBin2Bn(g_bp320r1CorrectBigB, NID_brainpoolP320r1_len, NULL);
366 xStd = OpensslBin2Bn(g_bp320r1CorrectBigGX, NID_brainpoolP320r1_len, NULL);
367 yStd = OpensslBin2Bn(g_bp320r1CorrectBigGY, NID_brainpoolP320r1_len, NULL);
368 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
369 LOGD("[error] BP 320r1 Curve convert to BN fail");
370 FreeCurveBigNum(pStd, bStd, xStd, yStd);
371 return HCF_ERR_CRYPTO_OPERATION;
372 }
373 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
374 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
375 FreeCurveBigNum(pStd, bStd, xStd, yStd);
376 return HCF_SUCCESS;
377 }
378 FreeCurveBigNum(pStd, bStd, xStd, yStd);
379 return HCF_INVALID_PARAMS;
380 }
381
CheckBP320t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)382 static HcfResult CheckBP320t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
383 {
384 BIGNUM *pStd = NULL;
385 BIGNUM *bStd = NULL;
386 BIGNUM *xStd = NULL;
387 BIGNUM *yStd = NULL;
388 pStd = OpensslBin2Bn(g_bp320t1CorrectBigP, NID_brainpoolP320t1_len, NULL);
389 bStd = OpensslBin2Bn(g_bp320t1CorrectBigB, NID_brainpoolP320t1_len, NULL);
390 xStd = OpensslBin2Bn(g_bp320t1CorrectBigGX, NID_brainpoolP320t1_len, NULL);
391 yStd = OpensslBin2Bn(g_bp320t1CorrectBigGY, NID_brainpoolP320t1_len, NULL);
392 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
393 LOGD("[error] BP 320t1 Curve convert to BN fail");
394 FreeCurveBigNum(pStd, bStd, xStd, yStd);
395 return HCF_ERR_CRYPTO_OPERATION;
396 }
397 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
398 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
399 FreeCurveBigNum(pStd, bStd, xStd, yStd);
400 return HCF_SUCCESS;
401 }
402 FreeCurveBigNum(pStd, bStd, xStd, yStd);
403 return HCF_INVALID_PARAMS;
404 }
405
CheckBP384r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)406 static HcfResult CheckBP384r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
407 {
408 BIGNUM *pStd = NULL;
409 BIGNUM *bStd = NULL;
410 BIGNUM *xStd = NULL;
411 BIGNUM *yStd = NULL;
412 pStd = OpensslBin2Bn(g_bp384r1CorrectBigP, NID_brainpoolP384r1_len, NULL);
413 bStd = OpensslBin2Bn(g_bp384r1CorrectBigB, NID_brainpoolP384r1_len, NULL);
414 xStd = OpensslBin2Bn(g_bp384r1CorrectBigGX, NID_brainpoolP384r1_len, NULL);
415 yStd = OpensslBin2Bn(g_bp384r1CorrectBigGY, NID_brainpoolP384r1_len, NULL);
416 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
417 LOGD("[error] BP 384r1 Curve convert to BN fail");
418 FreeCurveBigNum(pStd, bStd, xStd, yStd);
419 return HCF_ERR_CRYPTO_OPERATION;
420 }
421 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
422 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
423 FreeCurveBigNum(pStd, bStd, xStd, yStd);
424 return HCF_SUCCESS;
425 }
426 FreeCurveBigNum(pStd, bStd, xStd, yStd);
427 return HCF_INVALID_PARAMS;
428 }
429
CheckBP384t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)430 static HcfResult CheckBP384t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
431 {
432 BIGNUM *pStd = NULL;
433 BIGNUM *bStd = NULL;
434 BIGNUM *xStd = NULL;
435 BIGNUM *yStd = NULL;
436 pStd = OpensslBin2Bn(g_bp384t1CorrectBigP, NID_brainpoolP384t1_len, NULL);
437 bStd = OpensslBin2Bn(g_bp384t1CorrectBigB, NID_brainpoolP384t1_len, NULL);
438 xStd = OpensslBin2Bn(g_bp384t1CorrectBigGX, NID_brainpoolP384t1_len, NULL);
439 yStd = OpensslBin2Bn(g_bp384t1CorrectBigGY, NID_brainpoolP384t1_len, NULL);
440 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
441 LOGD("[error] BP 384t1 Curve convert to BN fail");
442 FreeCurveBigNum(pStd, bStd, xStd, yStd);
443 return HCF_ERR_CRYPTO_OPERATION;
444 }
445 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
446 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
447 FreeCurveBigNum(pStd, bStd, xStd, yStd);
448 return HCF_SUCCESS;
449 }
450 FreeCurveBigNum(pStd, bStd, xStd, yStd);
451 return HCF_INVALID_PARAMS;
452 }
453
CheckBP512r1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)454 static HcfResult CheckBP512r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
455 {
456 BIGNUM *pStd = NULL;
457 BIGNUM *bStd = NULL;
458 BIGNUM *xStd = NULL;
459 BIGNUM *yStd = NULL;
460 pStd = OpensslBin2Bn(g_bp512r1CorrectBigP, NID_brainpoolP512r1_len, NULL);
461 bStd = OpensslBin2Bn(g_bp512r1CorrectBigB, NID_brainpoolP512r1_len, NULL);
462 xStd = OpensslBin2Bn(g_bp512r1CorrectBigGX, NID_brainpoolP512r1_len, NULL);
463 yStd = OpensslBin2Bn(g_bp512r1CorrectBigGY, NID_brainpoolP512r1_len, NULL);
464 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
465 LOGD("[error] BP 512r1 Curve convert to BN fail");
466 FreeCurveBigNum(pStd, bStd, xStd, yStd);
467 return HCF_ERR_CRYPTO_OPERATION;
468 }
469 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
470 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
471 FreeCurveBigNum(pStd, bStd, xStd, yStd);
472 return HCF_SUCCESS;
473 }
474 FreeCurveBigNum(pStd, bStd, xStd, yStd);
475 return HCF_INVALID_PARAMS;
476 }
477
CheckBP512t1CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)478 static HcfResult CheckBP512t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
479 {
480 BIGNUM *pStd = NULL;
481 BIGNUM *bStd = NULL;
482 BIGNUM *xStd = NULL;
483 BIGNUM *yStd = NULL;
484 pStd = OpensslBin2Bn(g_bp512t1CorrectBigP, NID_brainpoolP512t1_len, NULL);
485 bStd = OpensslBin2Bn(g_bp512t1CorrectBigB, NID_brainpoolP512t1_len, NULL);
486 xStd = OpensslBin2Bn(g_bp512t1CorrectBigGX, NID_brainpoolP512t1_len, NULL);
487 yStd = OpensslBin2Bn(g_bp512t1CorrectBigGY, NID_brainpoolP512t1_len, NULL);
488 if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
489 LOGD("[error] BP 512t1 Curve convert to BN fail");
490 FreeCurveBigNum(pStd, bStd, xStd, yStd);
491 return HCF_ERR_CRYPTO_OPERATION;
492 }
493 if (OpensslBnCmp(p, pStd) == 0 && OpensslBnCmp(b, bStd) == 0 &&
494 OpensslBnCmp(x, xStd) == 0 && OpensslBnCmp(y, yStd) == 0) {
495 FreeCurveBigNum(pStd, bStd, xStd, yStd);
496 return HCF_SUCCESS;
497 }
498 FreeCurveBigNum(pStd, bStd, xStd, yStd);
499 return HCF_INVALID_PARAMS;
500 }
501
CompareOpenssl160BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)502 static HcfResult CompareOpenssl160BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
503 HcfBigIntegerParams *bigIntegerParams)
504 {
505 if (CheckBP160r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
506 bigIntegerParams->y) == HCF_SUCCESS) {
507 *curveId = NID_brainpoolP160r1;
508 return HCF_SUCCESS;
509 } else if (CheckBP160t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
510 bigIntegerParams->y) == HCF_SUCCESS) {
511 *curveId = NID_brainpoolP160t1;
512 return HCF_SUCCESS;
513 }
514 return HCF_NOT_SUPPORT;
515 }
516
CompareOpenssl192BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)517 static HcfResult CompareOpenssl192BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
518 HcfBigIntegerParams *bigIntegerParams)
519 {
520 if (CheckBP192r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
521 bigIntegerParams->y) == HCF_SUCCESS) {
522 *curveId = NID_brainpoolP192r1;
523 return HCF_SUCCESS;
524 } else if (CheckBP192t1CurveId(bigIntegerParams->p, bigIntegerParams->b,
525 bigIntegerParams->x, bigIntegerParams->y) == HCF_SUCCESS) {
526 *curveId = NID_brainpoolP192t1;
527 return HCF_SUCCESS;
528 }
529 return HCF_NOT_SUPPORT;
530 }
531
CompareOpenssl224BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)532 static HcfResult CompareOpenssl224BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
533 HcfBigIntegerParams *bigIntegerParams)
534 {
535 HcfResult res = HCF_INVALID_PARAMS;
536 res = CheckEc224CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
537 if (res == HCF_SUCCESS) {
538 *curveId = NID_secp224r1;
539 return res;
540 } else if (CheckBP224r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
541 bigIntegerParams->y) == HCF_SUCCESS) {
542 *curveId = NID_brainpoolP224r1;
543 return HCF_SUCCESS;
544 } else if (CheckBP224t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
545 bigIntegerParams->y) == HCF_SUCCESS) {
546 *curveId = NID_brainpoolP224t1;
547 return HCF_SUCCESS;
548 }
549 return HCF_NOT_SUPPORT;
550 }
551
CompareOpenssl256BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)552 static HcfResult CompareOpenssl256BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
553 HcfBigIntegerParams *bigIntegerParams)
554 {
555 HcfResult res = HCF_INVALID_PARAMS;
556 res = CheckEc256CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
557 if (res == HCF_SUCCESS) {
558 *curveId = NID_X9_62_prime256v1;
559 return res;
560 } else if (CheckBP256r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
561 bigIntegerParams->y) == HCF_SUCCESS) {
562 *curveId = NID_brainpoolP256r1;
563 return HCF_SUCCESS;
564 } else if (CheckBP256t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
565 bigIntegerParams->y) == HCF_SUCCESS) {
566 *curveId = NID_brainpoolP256t1;
567 return HCF_SUCCESS;
568 }
569 return HCF_NOT_SUPPORT;
570 }
571
CompareOpenssl320BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)572 static HcfResult CompareOpenssl320BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
573 HcfBigIntegerParams *bigIntegerParams)
574 {
575 if (CheckBP320r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
576 bigIntegerParams->y) == HCF_SUCCESS) {
577 *curveId = NID_brainpoolP320r1;
578 return HCF_SUCCESS;
579 } else if (CheckBP320t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
580 bigIntegerParams->y) == HCF_SUCCESS) {
581 *curveId = NID_brainpoolP320t1;
582 return HCF_SUCCESS;
583 }
584 return HCF_NOT_SUPPORT;
585 }
586
CompareOpenssl384BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)587 static HcfResult CompareOpenssl384BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
588 HcfBigIntegerParams *bigIntegerParams)
589 {
590 HcfResult res = HCF_INVALID_PARAMS;
591 res = CheckBP384r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x, bigIntegerParams->y);
592 if (res == HCF_SUCCESS) {
593 *curveId = NID_brainpoolP384r1;
594 return res;
595 } else if (CheckEc384CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
596 bigIntegerParams->y) == HCF_SUCCESS) {
597 *curveId = NID_secp384r1;
598 return HCF_SUCCESS;
599 } else if (CheckBP384t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
600 bigIntegerParams->y) == HCF_SUCCESS) {
601 *curveId = NID_brainpoolP384t1;
602 return HCF_SUCCESS;
603 }
604 return HCF_NOT_SUPPORT;
605 }
606
CompareOpenssl512BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)607 static HcfResult CompareOpenssl512BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
608 HcfBigIntegerParams *bigIntegerParams)
609 {
610 if (CheckBP512r1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
611 bigIntegerParams->y) == HCF_SUCCESS) {
612 *curveId = NID_brainpoolP512r1;
613 return HCF_SUCCESS;
614 } else if (CheckBP512t1CurveId(bigIntegerParams->p, bigIntegerParams->b, bigIntegerParams->x,
615 bigIntegerParams->y) == HCF_SUCCESS) {
616 *curveId = NID_brainpoolP512t1;
617 return HCF_SUCCESS;
618 }
619 return HCF_NOT_SUPPORT;
620 }
621
CompareOpenssl521BitsType(const HcfEccCommParamsSpec * ecParams,int32_t * curveId,HcfBigIntegerParams * bigIntegerParams)622 static HcfResult CompareOpenssl521BitsType(const HcfEccCommParamsSpec *ecParams, int32_t *curveId,
623 HcfBigIntegerParams *bigIntegerParams)
624 {
625 HcfResult res = CheckEc521CurveId(bigIntegerParams->p, bigIntegerParams->b,
626 bigIntegerParams->x, bigIntegerParams->y);
627 if (res == HCF_SUCCESS) {
628 *curveId = NID_secp521r1;
629 return res;
630 }
631 return HCF_NOT_SUPPORT;
632 }
633
CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec * ecParams,int32_t * curveId)634 static HcfResult CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec *ecParams, int32_t *curveId)
635 {
636 HcfBigIntegerParams bigIntegerParams;
637 bigIntegerParams.p = NULL;
638 bigIntegerParams.b = NULL;
639 bigIntegerParams.x = NULL;
640 bigIntegerParams.y = NULL;
641 HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field);
642 if (BigIntegerToBigNum(&(field->p), &(bigIntegerParams.p)) != HCF_SUCCESS ||
643 BigIntegerToBigNum(&(ecParams->b), &(bigIntegerParams.b)) != HCF_SUCCESS ||
644 BigIntegerToBigNum(&(ecParams->g.x), &(bigIntegerParams.x)) != HCF_SUCCESS ||
645 BigIntegerToBigNum(&(ecParams->g.y), &(bigIntegerParams.y)) != HCF_SUCCESS) {
646 LOGD("[error] BigIntegerToBigNum failed.");
647 FreeCurveBigNum(bigIntegerParams.p, bigIntegerParams.b, bigIntegerParams.x, bigIntegerParams.y);
648 return HCF_ERR_CRYPTO_OPERATION;
649 }
650
651 int32_t bitLenP = (int32_t)OpensslBnNumBits(bigIntegerParams.p);
652 HcfResult res = HCF_INVALID_PARAMS;
653 switch (bitLenP) {
654 case OPENSSL_ECC160_BITS:
655 res = CompareOpenssl160BitsType(ecParams, curveId, &bigIntegerParams);
656 break;
657 case OPENSSL_ECC192_BITS:
658 res = CompareOpenssl192BitsType(ecParams, curveId, &bigIntegerParams);
659 break;
660 case OPENSSL_ECC224_BITS:
661 res = CompareOpenssl224BitsType(ecParams, curveId, &bigIntegerParams);
662 break;
663 case OPENSSL_ECC256_BITS:
664 res = CompareOpenssl256BitsType(ecParams, curveId, &bigIntegerParams);
665 break;
666 case OPENSSL_ECC320_BITS:
667 res = CompareOpenssl320BitsType(ecParams, curveId, &bigIntegerParams);
668 break;
669 case OPENSSL_ECC384_BITS:
670 res = CompareOpenssl384BitsType(ecParams, curveId, &bigIntegerParams);
671 break;
672 case OPENSSL_ECC512_BITS:
673 res = CompareOpenssl512BitsType(ecParams, curveId, &bigIntegerParams);
674 break;
675 case OPENSSL_ECC521_BITS:
676 res = CompareOpenssl521BitsType(ecParams, curveId, &bigIntegerParams);
677 break;
678 default:
679 LOGE("Find no bit len:%d", bitLenP);
680 break;
681 }
682 FreeCurveBigNum(bigIntegerParams.p, bigIntegerParams.b, bigIntegerParams.x, bigIntegerParams.y);
683 return res;
684 }
685
GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnKey)686 static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnKey)
687 {
688 if (ecParams == NULL || returnKey == NULL) {
689 LOGE("Invalid input parameters.");
690 return HCF_INVALID_PARAMS;
691 }
692 EC_KEY *ecKey = NULL;
693 int32_t curveId = 0;
694 HcfResult ret = CheckParamsSpecToGetCurveId(ecParams, &curveId);
695 if (ret == HCF_SUCCESS && curveId != 0) {
696 ecKey = OpensslEcKeyNewByCurveName(curveId);
697 LOGD("generate EC_KEY by curve name");
698 if (ecKey == NULL) {
699 LOGD("[error] new ec key failed.");
700 return HCF_ERR_CRYPTO_OPERATION;
701 }
702 } else {
703 EC_GROUP *group = NULL;
704 ret = GenerateEcGroupWithParamsSpec(ecParams, &group);
705 if (ret != HCF_SUCCESS) {
706 LOGD("[error] GenerateEcGroupWithParamsSpec failed.");
707 return ret;
708 }
709 ecKey = OpensslEcKeyNew();
710 if (ecKey == NULL) {
711 LOGD("[error] OpensslEcKeyNew failed.");
712 OpensslEcGroupFree(group);
713 return HCF_ERR_CRYPTO_OPERATION;
714 }
715 if (OpensslEcKeySetGroup(ecKey, group) != HCF_OPENSSL_SUCCESS) {
716 LOGD("[error] OpensslEcKeySetGroup failed.");
717 OpensslEcGroupFree(group);
718 OpensslEcKeyFree(ecKey);
719 return HCF_ERR_CRYPTO_OPERATION;
720 }
721 OpensslEcGroupFree(group);
722 LOGD("generate EC_KEY by group spec parmas");
723 }
724 // all exceptions have been returned above.
725 *returnKey = ecKey;
726 return HCF_SUCCESS;
727 }
728
NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnEckey)729 static HcfResult NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnEckey)
730 {
731 if (ecParams == NULL || returnEckey == NULL) {
732 LOGE("Invalid input parameters.");
733 return HCF_INVALID_PARAMS;
734 }
735 EC_KEY *ecKey = NULL;
736 HcfResult ret = GenerateEcKeyWithParamsSpec(ecParams, &ecKey);
737 if (ret != HCF_SUCCESS) {
738 LOGE("generate EC key fails");
739 return ret;
740 }
741 if (OpensslEcKeyGenerateKey(ecKey) != HCF_OPENSSL_SUCCESS) {
742 LOGD("[error] OpensslEcKeyGenerateKey failed.");
743 OpensslEcKeyFree(ecKey);
744 return HCF_ERR_CRYPTO_OPERATION;
745 }
746
747 if (OpensslEcKeyCheckKey(ecKey) <= 0) {
748 LOGD("[error] Check key fail.");
749 OpensslEcKeyFree(ecKey);
750 return HCF_ERR_CRYPTO_OPERATION;
751 }
752 *returnEckey = ecKey;
753 return ret;
754 }
755
NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)756 static HcfResult NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
757 {
758 if (ecParams == NULL || returnEcKey == NULL) {
759 LOGE("Invalid input parameters.");
760 return HCF_INVALID_PARAMS;
761 }
762 EC_KEY *ecKey = NULL;
763 HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
764 if (ret != HCF_SUCCESS) {
765 LOGE("generate EC key fails");
766 return ret;
767 }
768 ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
769 if (ret != HCF_SUCCESS) {
770 LOGD("[error] Set pub ecKey failed.");
771 OpensslEcKeyFree(ecKey);
772 return HCF_ERR_CRYPTO_OPERATION;
773 }
774
775 if (OpensslEcKeyCheckKey(ecKey) <= 0) {
776 LOGD("[error] Check key fail.");
777 OpensslEcKeyFree(ecKey);
778 return HCF_ERR_CRYPTO_OPERATION;
779 }
780 *returnEcKey = ecKey;
781 return ret;
782 }
783
NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)784 static HcfResult NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
785 {
786 if (ecParams == NULL || returnEcKey == NULL) {
787 LOGE("Invalid input parameters.");
788 return HCF_INVALID_PARAMS;
789 }
790 EC_KEY *ecKey = NULL;
791 HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
792 if (ret != HCF_SUCCESS) {
793 LOGE("generate EC key fails");
794 return ret;
795 }
796 ret = SetEcKey(NULL, &(ecParams->sk), ecKey);
797 if (ret != HCF_SUCCESS) {
798 LOGD("[error] Set pri ecKey failed.");
799 OpensslEcKeyFree(ecKey);
800 return HCF_ERR_CRYPTO_OPERATION;
801 }
802
803 if (OpensslEcKeyCheckKey(ecKey) <= 0) {
804 LOGD("[error] Check key fail.");
805 OpensslEcKeyFree(ecKey);
806 return HCF_ERR_CRYPTO_OPERATION;
807 }
808 *returnEcKey = ecKey;
809 return ret;
810 }
811
NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec * ecParams,EC_KEY ** returnEcKey,bool needPrivate)812 static HcfResult NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParams, EC_KEY **returnEcKey,
813 bool needPrivate)
814 {
815 if (ecParams == NULL || returnEcKey == NULL) {
816 LOGE("Invalid input parameters.");
817 return HCF_INVALID_PARAMS;
818 }
819 EC_KEY *ecKey = NULL;
820 HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
821 if (ret != HCF_SUCCESS) {
822 LOGE("generate EC key fails");
823 return ret;
824 }
825 if (needPrivate) {
826 ret = SetEcKey(&(ecParams->pk), &(ecParams->sk), ecKey);
827 } else {
828 ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
829 }
830 if (ret != HCF_SUCCESS) {
831 LOGD("[error] SetEcKey failed.");
832 OpensslEcKeyFree(ecKey);
833 return HCF_ERR_CRYPTO_OPERATION;
834 }
835
836 if (OpensslEcKeyCheckKey(ecKey) <= 0) {
837 LOGD("[error] Check key fail.");
838 OpensslEcKeyFree(ecKey);
839 return HCF_ERR_CRYPTO_OPERATION;
840 }
841 *returnEcKey = ecKey;
842 return ret;
843 }
844
GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)845 static HcfResult GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
846 {
847 HcfResult ret = HCF_INVALID_PARAMS;
848 switch (params->specType) {
849 case HCF_COMMON_PARAMS_SPEC:
850 ret = NewEcKeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey);
851 break;
852 case HCF_KEY_PAIR_SPEC:
853 ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
854 break;
855 default:
856 LOGE("Invaild input spec to gen key pair.");
857 break;
858 }
859 return ret;
860 }
861
GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)862 static HcfResult GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
863 {
864 HcfResult ret = HCF_INVALID_PARAMS;
865 switch (params->specType) {
866 case HCF_PUBLIC_KEY_SPEC:
867 ret = NewEcPubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey);
868 break;
869 case HCF_KEY_PAIR_SPEC:
870 ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false);
871 break;
872 default:
873 LOGE("Invaild input spec to gen pub key");
874 break;
875 }
876 return ret;
877 }
878
GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)879 static HcfResult GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
880 {
881 HcfResult ret = HCF_INVALID_PARAMS;
882 switch (params->specType) {
883 case HCF_PRIVATE_KEY_SPEC:
884 ret = NewEcPriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey);
885 break;
886 case HCF_KEY_PAIR_SPEC:
887 ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
888 break;
889 default:
890 LOGE("Invaild input spec to gen pri key");
891 break;
892 }
893 return ret;
894 }
895
GetEccKeyPairGeneratorClass(void)896 static const char *GetEccKeyPairGeneratorClass(void)
897 {
898 return OPENSSL_ECC_KEY_GENERATOR_CLASS;
899 }
900
GetEccKeyPairClass(void)901 static const char *GetEccKeyPairClass(void)
902 {
903 return HCF_OPENSSL_ECC_KEY_PAIR_CLASS;
904 }
905
GetEccPubKeyClass(void)906 static const char *GetEccPubKeyClass(void)
907 {
908 return HCF_OPENSSL_ECC_PUB_KEY_CLASS;
909 }
910
GetEccPriKeyClass(void)911 static const char *GetEccPriKeyClass(void)
912 {
913 return HCF_OPENSSL_ECC_PRI_KEY_CLASS;
914 }
915
DestroyEccKeyPairGenerator(HcfObjectBase * self)916 static void DestroyEccKeyPairGenerator(HcfObjectBase *self)
917 {
918 if (self == NULL) {
919 return;
920 }
921 if (!HcfIsClassMatch(self, GetEccKeyPairGeneratorClass())) {
922 return;
923 }
924 HcfFree(self);
925 }
926
DestroyEccPubKey(HcfObjectBase * self)927 static void DestroyEccPubKey(HcfObjectBase *self)
928 {
929 if (self == NULL) {
930 return;
931 }
932 if (!HcfIsClassMatch(self, GetEccPubKeyClass())) {
933 return;
934 }
935 HcfOpensslEccPubKey *impl = (HcfOpensslEccPubKey *)self;
936 OpensslEcKeyFree(impl->ecKey);
937 impl->ecKey = NULL;
938 HcfFree(impl->fieldType);
939 impl->fieldType = NULL;
940 HcfFree(impl);
941 }
942
DestroyEccPriKey(HcfObjectBase * self)943 static void DestroyEccPriKey(HcfObjectBase *self)
944 {
945 if (self == NULL) {
946 return;
947 }
948 if (!HcfIsClassMatch(self, GetEccPriKeyClass())) {
949 return;
950 }
951 HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
952 OpensslEcKeyFree(impl->ecKey);
953 impl->ecKey = NULL;
954 HcfFree(impl->fieldType);
955 impl->fieldType = NULL;
956 HcfFree(impl);
957 }
958
DestroyEccKeyPair(HcfObjectBase * self)959 static void DestroyEccKeyPair(HcfObjectBase *self)
960 {
961 if (self == NULL) {
962 return;
963 }
964 if (!HcfIsClassMatch(self, GetEccKeyPairClass())) {
965 return;
966 }
967 HcfOpensslEccKeyPair *impl = (HcfOpensslEccKeyPair *)self;
968 if (impl->base.pubKey != NULL) {
969 DestroyEccPubKey((HcfObjectBase *)impl->base.pubKey);
970 impl->base.pubKey = NULL;
971 }
972 if (impl->base.priKey != NULL) {
973 DestroyEccPriKey((HcfObjectBase *)impl->base.priKey);
974 impl->base.priKey = NULL;
975 }
976 HcfFree(impl);
977 }
978
GetEccPubKeyAlgorithm(HcfKey * self)979 static const char *GetEccPubKeyAlgorithm(HcfKey *self)
980 {
981 if (self == NULL) {
982 LOGE("Invalid input parameter.");
983 return NULL;
984 }
985 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
986 return NULL;
987 }
988 return OPENSSL_ECC_ALGORITHM;
989 }
990
GetEccPriKeyAlgorithm(HcfKey * self)991 static const char *GetEccPriKeyAlgorithm(HcfKey *self)
992 {
993 if (self == NULL) {
994 LOGE("Invalid input parameter.");
995 return NULL;
996 }
997 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
998 return NULL;
999 }
1000 return OPENSSL_ECC_ALGORITHM;
1001 }
1002
GetEccPubKeyFormat(HcfKey * self)1003 static const char *GetEccPubKeyFormat(HcfKey *self)
1004 {
1005 if (self == NULL) {
1006 LOGE("Invalid input parameter.");
1007 return NULL;
1008 }
1009 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
1010 return NULL;
1011 }
1012 return OPENSSL_ECC_PUB_KEY_FORMAT;
1013 }
1014
GetEccPriKeyFormat(HcfKey * self)1015 static const char *GetEccPriKeyFormat(HcfKey *self)
1016 {
1017 if (self == NULL) {
1018 LOGE("Invalid input parameter.");
1019 return NULL;
1020 }
1021 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
1022 return NULL;
1023 }
1024 return OPENSSL_ECC_PRI_KEY_FORMAT;
1025 }
1026
CheckAndUpdateEccPubKeyFormat(const char ** format)1027 static HcfResult CheckAndUpdateEccPubKeyFormat(const char **format)
1028 {
1029 if (format == NULL || *format == NULL) {
1030 LOGE("Invalid format parameter");
1031 return HCF_INVALID_PARAMS;
1032 }
1033
1034 const char *x509Str = "X509|";
1035
1036 if (strncmp(*format, x509Str, HcfStrlen(x509Str)) != 0) {
1037 LOGE("Invalid x509Str parameter");
1038 return HCF_INVALID_PARAMS;
1039 }
1040
1041 const char *formatPtr = *format + HcfStrlen(x509Str);
1042
1043 if (strcmp(formatPtr, UNCOMPRESSED_FORMAT) == 0 || strcmp(formatPtr, COMPRESSED_FORMAT) == 0) {
1044 *format = formatPtr;
1045 return HCF_SUCCESS;
1046 } else {
1047 LOGE("Invalid formatPtr parameter");
1048 return HCF_INVALID_PARAMS;
1049 }
1050 }
1051
ConvertHcfBlobToOsslParams(const char * groupName,HcfBlob * pointBlob,const char * format)1052 static OSSL_PARAM *ConvertHcfBlobToOsslParams(const char *groupName, HcfBlob *pointBlob, const char *format)
1053 {
1054 OSSL_PARAM_BLD *paramBld = OpensslOsslParamBldNew();
1055 if (paramBld == NULL) {
1056 LOGE("paramBld is null");
1057 return NULL;
1058 }
1059 if (OpensslOsslParamBldPushUtf8String(paramBld, "group", groupName, 0) != HCF_OPENSSL_SUCCESS) {
1060 LOGE("Invalid groupName parameter.");
1061 OpensslOsslParamBldFree(paramBld);
1062 return NULL;
1063 }
1064 if (OpensslOsslParamBldPushUtf8String(paramBld, "point-format", format, 0) != HCF_OPENSSL_SUCCESS) {
1065 LOGE("Invalid format parameter.");
1066 OpensslOsslParamBldFree(paramBld);
1067 return NULL;
1068 }
1069 if (OpensslOsslParamBldPushOctetString(paramBld, "pub", pointBlob->data, pointBlob->len) != HCF_OPENSSL_SUCCESS) {
1070 LOGE("Invalid pointBlob parameter.");
1071 OpensslOsslParamBldFree(paramBld);
1072 return NULL;
1073 }
1074 OSSL_PARAM *params = OpensslOsslParamBldToParam(paramBld);
1075 if (params == NULL) {
1076 LOGE("Failed to convert OSSL_PARAM_BLD to OSSL_PARAM");
1077 HcfPrintOpensslError();
1078 OpensslOsslParamBldFree(paramBld);
1079 return NULL;
1080 }
1081 OpensslOsslParamBldFree(paramBld);
1082 return params;
1083 }
1084
ConvertOsslParamsToEccPubKey(const char * groupName,int32_t curveId,HcfBlob * pointBlob,const char * format)1085 static EC_KEY *ConvertOsslParamsToEccPubKey(const char *groupName, int32_t curveId,
1086 HcfBlob *pointBlob, const char *format)
1087 {
1088 OSSL_PARAM *params = ConvertHcfBlobToOsslParams(groupName, pointBlob, format);
1089 if (params == NULL) {
1090 LOGE("Failed to convert OSSL_PARAM_BLD to OSSL_PARAM");
1091 return NULL;
1092 }
1093 EVP_PKEY_CTX *ctx = NULL;
1094 EVP_PKEY *pkey = NULL;
1095 EC_KEY *returnKey = NULL;
1096 do {
1097 ctx = OpensslEvpPkeyCtxNewId(EVP_PKEY_EC, NULL);
1098 if (ctx == NULL) {
1099 LOGE("Failed to create EVP_PKEY_CTX");
1100 break;
1101 }
1102 if (OpensslEvpPkeyParamGenInit(ctx) <= 0) {
1103 LOGE("Create EVP_PKEY_CTX by curveId fail, curveId is %d", curveId);
1104 break;
1105 }
1106 if (OpensslEvpPkeyCtxSetEcParamgenCurveNid(ctx, curveId) <= 0) {
1107 LOGE("EVP init curveId fail");
1108 HcfPrintOpensslError();
1109 break;
1110 }
1111 if (OpensslEvpPkeyFromDataInit(ctx) != HCF_OPENSSL_SUCCESS) {
1112 LOGE("EVP init fail");
1113 break;
1114 }
1115 if (OpensslEvpPkeyFromData(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) != HCF_OPENSSL_SUCCESS) {
1116 LOGE("EVP get pkey fail");
1117 HcfPrintOpensslError();
1118 break;
1119 }
1120 returnKey = OpensslEvpPkeyGet1EcKey(pkey);
1121 if (returnKey == NULL) {
1122 LOGE("Return key is NULL");
1123 break;
1124 }
1125 } while (0);
1126 OpensslEvpPkeyFree(pkey);
1127 OpensslEvpPkeyCtxFree(ctx);
1128 OpensslOsslParamFree(params);
1129 return returnKey;
1130 }
1131
GetCompressedEccPointEncoded(HcfOpensslEccPubKey * impl,HcfBlob * returnBlob)1132 static HcfResult GetCompressedEccPointEncoded(HcfOpensslEccPubKey *impl, HcfBlob *returnBlob)
1133 {
1134 EC_KEY *ecKey = impl->ecKey;
1135 if (ecKey == NULL) {
1136 LOGE("EcKey is NULL.");
1137 return HCF_INVALID_PARAMS;
1138 }
1139 const EC_GROUP *group = OpensslEcKeyGet0Group(ecKey);
1140 if (group == NULL) {
1141 LOGE("Failed to get group.");
1142 return HCF_ERR_CRYPTO_OPERATION;
1143 }
1144 const EC_POINT *point = OpensslEcKeyGet0PublicKey(ecKey);
1145 if (point == NULL) {
1146 LOGE("Failed to get point.");
1147 return HCF_ERR_CRYPTO_OPERATION;
1148 }
1149 size_t returnDataLen = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, NULL, 0, NULL);
1150 if (returnDataLen == 0) {
1151 LOGE("Failed to get compressed key length.");
1152 HcfPrintOpensslError();
1153 return HCF_ERR_CRYPTO_OPERATION;
1154 }
1155 uint8_t *returnData = (uint8_t *)HcfMalloc(returnDataLen, 0);
1156 if (returnData == NULL) {
1157 LOGE("Failed to allocate memory for returnBlob data.");
1158 return HCF_ERR_MALLOC;
1159 }
1160 size_t result = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
1161 returnData, returnDataLen, NULL);
1162 if (result != returnDataLen) {
1163 LOGE("Failed to convert public key to compressed format.");
1164 HcfPrintOpensslError();
1165 HcfFree(returnData);
1166 return HCF_ERR_CRYPTO_OPERATION;
1167 }
1168 returnBlob->data = returnData;
1169 returnBlob->len = returnDataLen;
1170 return HCF_SUCCESS;
1171 }
1172
GetDerEccPubKeyEncoded(EC_KEY * ecKey,HcfBlob * returnBlob)1173 static HcfResult GetDerEccPubKeyEncoded(EC_KEY *ecKey, HcfBlob *returnBlob)
1174 {
1175 unsigned char *returnData = NULL;
1176 int returnDataLen = OpensslI2dEcPubKey(ecKey, &returnData);
1177 if (returnDataLen <= 0) {
1178 LOGE("i2d_EC_PUBKEY fail");
1179 HcfPrintOpensslError();
1180 return HCF_ERR_CRYPTO_OPERATION;
1181 }
1182 returnBlob->data = returnData;
1183 returnBlob->len = returnDataLen;
1184 return HCF_SUCCESS;
1185 }
1186
SetEccKeyAsn1Flag(HcfOpensslEccPubKey * impl)1187 static void SetEccKeyAsn1Flag(HcfOpensslEccPubKey *impl)
1188 {
1189 if (impl->curveId != 0) {
1190 LOGD("have a curveId");
1191 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
1192 } else {
1193 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
1194 }
1195 }
1196
GetEccPubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)1197 static HcfResult GetEccPubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
1198 {
1199 if ((self == NULL) || (returnBlob == NULL)) {
1200 LOGE("Invalid input parameter.");
1201 return HCF_INVALID_PARAMS;
1202 }
1203
1204 if (CheckAndUpdateEccPubKeyFormat(&format) != HCF_SUCCESS) {
1205 LOGE("Invalid format.");
1206 return HCF_INVALID_PARAMS;
1207 }
1208
1209 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
1210 LOGE("Invalid input parameter type.");
1211 return HCF_INVALID_PARAMS;
1212 }
1213 HcfOpensslEccPubKey *impl = (HcfOpensslEccPubKey *)self;
1214 SetEccKeyAsn1Flag(impl);
1215
1216 char *groupName = NULL;
1217 HcfResult ret = GetGroupNameByNid(impl->curveId, &groupName);
1218 if (ret != HCF_SUCCESS) {
1219 LOGE("Failed to get group name.");
1220 return ret;
1221 }
1222 HcfBlob tmpBlob = { .data = NULL, .len = 0 };
1223 ret = GetCompressedEccPointEncoded(impl, &tmpBlob);
1224 if (ret != HCF_SUCCESS) {
1225 LOGE("Invalid input parameter.");
1226 return ret;
1227 }
1228 EC_KEY *tmpEcKey = ConvertOsslParamsToEccPubKey(groupName, impl->curveId, &tmpBlob, format);
1229 if (tmpEcKey == NULL) {
1230 LOGE("Failed to convert ECC parameters to EC public key.");
1231 HcfBlobDataFree(&tmpBlob);
1232 return HCF_ERR_CRYPTO_OPERATION;
1233 }
1234 ret = GetDerEccPubKeyEncoded(tmpEcKey, returnBlob);
1235 OpensslEcKeyFree(tmpEcKey);
1236 HcfBlobDataFree(&tmpBlob);
1237 return ret;
1238 }
1239
GetEccPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)1240 static HcfResult GetEccPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
1241 {
1242 if ((self == NULL) || (returnBlob == NULL)) {
1243 LOGE("Invalid input parameter.");
1244 return HCF_INVALID_PARAMS;
1245 }
1246 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PUB_KEY_CLASS)) {
1247 return HCF_INVALID_PARAMS;
1248 }
1249
1250 HcfOpensslEccPubKey *impl = (HcfOpensslEccPubKey *)self;
1251 SetEccKeyAsn1Flag(impl);
1252
1253 unsigned char *returnData = NULL;
1254 int returnDataLen = OpensslI2dEcPubKey(impl->ecKey, &returnData);
1255 if (returnDataLen <= 0) {
1256 LOGD("[error] i2d_EC_PUBKEY fail");
1257 HcfPrintOpensslError();
1258 return HCF_ERR_CRYPTO_OPERATION;
1259 }
1260 LOGD("ECC pubKey i2d success");
1261 returnBlob->data = returnData;
1262 returnBlob->len = returnDataLen;
1263 return HCF_SUCCESS;
1264 }
1265
GetEccPubKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)1266 static HcfResult GetEccPubKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
1267 {
1268 (void)self;
1269 (void)format;
1270 (void)returnString;
1271 return HCF_INVALID_PARAMS;
1272 }
1273
GetEccPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)1274 static HcfResult GetEccPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
1275 {
1276 if ((self == NULL) || (returnBlob == NULL)) {
1277 LOGE("Invalid input parameter.");
1278 return HCF_INVALID_PARAMS;
1279 }
1280 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
1281 return HCF_INVALID_PARAMS;
1282 }
1283
1284 HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
1285 if (impl->curveId != 0) {
1286 LOGD("have a curveId");
1287 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
1288 } else {
1289 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
1290 }
1291 // keep consistence of 3.2
1292 OpensslEcKeySetEncFlags(impl->ecKey, EC_PKEY_NO_PUBKEY);
1293 // if the convert key has no pubKey, it will generate pub key automatically,
1294 // and set the no pubKey flag to ensure the consistency of blob.
1295 unsigned char *returnData = NULL;
1296 int returnDataLen = OpensslI2dEcPrivateKey(impl->ecKey, &returnData);
1297 if (returnDataLen <= 0) {
1298 LOGD("[error] i2d_ECPrivateKey fail.");
1299 HcfPrintOpensslError();
1300 return HCF_ERR_CRYPTO_OPERATION;
1301 }
1302 LOGD("ECC priKey i2d success");
1303 returnBlob->data = returnData;
1304 returnBlob->len = returnDataLen;
1305 return HCF_SUCCESS;
1306 }
1307
GetEccPriKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)1308 static HcfResult GetEccPriKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
1309 {
1310 (void)self;
1311 (void)format;
1312 (void)returnString;
1313 return HCF_INVALID_PARAMS;
1314 }
1315
ParamCheck(const HcfPriKey * self,const char * format,const HcfBlob * returnBlob)1316 static HcfResult ParamCheck(const HcfPriKey *self, const char *format, const HcfBlob *returnBlob)
1317 {
1318 if ((self == NULL) || (format == NULL) || (returnBlob == NULL)) {
1319 LOGE("Invalid input parameter.");
1320 return HCF_INVALID_PARAMS;
1321 }
1322 if (!HcfIsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) {
1323 LOGE("Invalid ecc params.");
1324 return HCF_INVALID_PARAMS;
1325 }
1326 if (strcmp(format, "PKCS8") != 0) {
1327 LOGE("Invalid point format.");
1328 return HCF_INVALID_PARAMS;
1329 }
1330 return HCF_SUCCESS;
1331 }
1332
CopyMemFromBIO(BIO * bio,HcfBlob * returnBlob)1333 static HcfResult CopyMemFromBIO(BIO *bio, HcfBlob *returnBlob)
1334 {
1335 int len = BIO_pending(bio);
1336 if (len <= 0) {
1337 LOGE("Bio len less than 0.");
1338 return HCF_INVALID_PARAMS;
1339 }
1340 HcfBlob tmpBlob;
1341 tmpBlob.len = len;
1342 tmpBlob.data = (uint8_t *)HcfMalloc(sizeof(uint8_t) * len, 0);
1343 if (tmpBlob.data == NULL) {
1344 LOGE("Malloc mem for blob fail.");
1345 return HCF_ERR_MALLOC;
1346 }
1347 if (OpensslBioRead(bio, tmpBlob.data, tmpBlob.len) <= 0) {
1348 LOGE("Bio read fail");
1349 HcfPrintOpensslError();
1350 HcfFree(tmpBlob.data);
1351 return HCF_ERR_CRYPTO_OPERATION;
1352 }
1353 returnBlob->len = tmpBlob.len;
1354 returnBlob->data = tmpBlob.data;
1355 return HCF_SUCCESS;
1356 }
1357
GetECPriKeyEncodedDer(const HcfPriKey * self,const char * format,HcfBlob * returnBlob)1358 static HcfResult GetECPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob)
1359 {
1360 HcfResult ret = ParamCheck(self, format, returnBlob);
1361 if (ret != HCF_SUCCESS) {
1362 return ret;
1363 }
1364 HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
1365 if (impl->curveId != 0) {
1366 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
1367 } else {
1368 OpensslEcKeySetAsn1Flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
1369 }
1370 // keep consistence of 3.2
1371 OpensslEcKeySetEncFlags(impl->ecKey, EC_PKEY_NO_PUBKEY);
1372 EVP_PKEY *pkey = OpensslEvpPkeyNew();
1373 if (pkey == NULL) {
1374 HcfPrintOpensslError();
1375 LOGE("New pKey failed.");
1376 return HCF_ERR_CRYPTO_OPERATION;
1377 }
1378 if (OpensslEvpPkeySet1EcKey(pkey, impl->ecKey) != HCF_OPENSSL_SUCCESS) {
1379 OpensslEvpPkeyFree(pkey);
1380 HcfPrintOpensslError();
1381 LOGE("set ec key failed.");
1382 return HCF_ERR_CRYPTO_OPERATION;
1383 }
1384 BIO *bio = OpensslBioNew(OpensslBioSMem());
1385 if (bio == NULL) {
1386 LOGE("BIO new fail.");
1387 HcfPrintOpensslError();
1388 ret = HCF_ERR_CRYPTO_OPERATION;
1389 goto ERR2;
1390 }
1391 if (i2d_PKCS8PrivateKey_bio(bio, pkey, NULL, NULL, 0, NULL, NULL) != HCF_OPENSSL_SUCCESS) {
1392 LOGE("i2d privateKey bio fail.");
1393 HcfPrintOpensslError();
1394 ret = HCF_ERR_CRYPTO_OPERATION;
1395 goto ERR1;
1396 }
1397 ret = CopyMemFromBIO(bio, returnBlob);
1398 if (ret != HCF_SUCCESS) {
1399 LOGE("Copy mem from BIO fail.");
1400 }
1401 ERR1:
1402 OpensslBioFreeAll(bio);
1403 ERR2:
1404 OpensslEvpPkeyFree(pkey);
1405 return ret;
1406 }
1407
EccPriKeyClearMem(HcfPriKey * self)1408 static void EccPriKeyClearMem(HcfPriKey *self)
1409 {
1410 if (self == NULL) {
1411 return;
1412 }
1413 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccPriKeyClass())) {
1414 return;
1415 }
1416 HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self;
1417 OpensslEcKeyFree(impl->ecKey);
1418 impl->ecKey = NULL;
1419 }
1420
GetCurveName(const HcfKey * self,const bool isPriavte,char ** returnString)1421 static HcfResult GetCurveName(const HcfKey *self, const bool isPriavte, char **returnString)
1422 {
1423 int32_t curveId = 0;
1424 if (isPriavte) {
1425 curveId = ((HcfOpensslEccPriKey *)self)->curveId;
1426 } else {
1427 curveId = ((HcfOpensslEccPubKey *)self)->curveId;
1428 }
1429
1430 char *tmp = NULL;
1431 if (GetCurveNameByCurveId(curveId, &tmp) != HCF_SUCCESS) {
1432 LOGE("get vurveName by curveId failed.");
1433 return HCF_INVALID_PARAMS;
1434 }
1435
1436 if (tmp == NULL) {
1437 LOGE("tmp is null.");
1438 return HCF_INVALID_PARAMS;
1439 }
1440 size_t len = HcfStrlen(tmp);
1441 if (len == 0) {
1442 LOGE("fieldType is empty!");
1443 return HCF_INVALID_PARAMS;
1444 }
1445
1446 *returnString = (char *)HcfMalloc(len + 1, 0);
1447 if (*returnString == NULL) {
1448 LOGE("Alloc returnString memory failed.");
1449 return HCF_ERR_MALLOC;
1450 }
1451 (void)memcpy_s(*returnString, len, tmp, len);
1452 return HCF_SUCCESS;
1453 }
1454
CheckEcKeySelf(const HcfKey * self,bool * isPrivate)1455 static HcfResult CheckEcKeySelf(const HcfKey *self, bool *isPrivate)
1456 {
1457 if (HcfIsClassMatch((HcfObjectBase *)self, GetEccPubKeyClass())) {
1458 *isPrivate = false;
1459 return HCF_SUCCESS;
1460 } else if (HcfIsClassMatch((HcfObjectBase *)self, GetEccPriKeyClass())) {
1461 if (((HcfOpensslEccPriKey *)self)->ecKey == NULL) {
1462 LOGE("Cannot use priKey after free");
1463 return HCF_INVALID_PARAMS;
1464 }
1465 *isPrivate = true;
1466 return HCF_SUCCESS;
1467 } else {
1468 return HCF_INVALID_PARAMS;
1469 }
1470 }
1471
GetEcKeySpecBigInteger(const HcfKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1472 static HcfResult GetEcKeySpecBigInteger(const HcfKey *self, const AsyKeySpecItem item,
1473 HcfBigInteger *returnBigInteger)
1474 {
1475 if (self == NULL || returnBigInteger == NULL) {
1476 LOGE("Invalid input parameter.");
1477 return HCF_INVALID_PARAMS;
1478 }
1479 bool isPrivate = false;
1480 HcfResult res = CheckEcKeySelf(self, &isPrivate);
1481 if (res != HCF_SUCCESS) {
1482 LOGE("Invalid input key");
1483 return HCF_INVALID_PARAMS;
1484 }
1485 const EC_GROUP *group = NULL;
1486 if (isPrivate) {
1487 group = OpensslEcKeyGet0Group(((HcfOpensslEccPriKey *)self)->ecKey);
1488 } else {
1489 group = OpensslEcKeyGet0Group(((HcfOpensslEccPubKey *)self)->ecKey);
1490 }
1491 switch (item) {
1492 case ECC_FP_P_BN:
1493 case ECC_A_BN:
1494 case ECC_B_BN:
1495 res = GetCurveGFp(group, item, returnBigInteger);
1496 break;
1497 case ECC_G_X_BN:
1498 case ECC_G_Y_BN:
1499 res = GetGenerator(group, item, returnBigInteger);
1500 break;
1501 case ECC_N_BN:
1502 res = GetOrder(group, returnBigInteger);
1503 break;
1504 case ECC_SK_BN:
1505 case ECC_PK_X_BN:
1506 case ECC_PK_Y_BN:
1507 res = GetPkSkBigInteger(self, isPrivate, item, returnBigInteger);
1508 break;
1509 default:
1510 LOGE("Invalid ecc key big number spec!");
1511 res = HCF_INVALID_PARAMS;
1512 break;
1513 }
1514 return res;
1515 }
1516
GetEcKeySpecString(const HcfKey * self,const AsyKeySpecItem item,char ** returnString)1517 static HcfResult GetEcKeySpecString(const HcfKey *self, const AsyKeySpecItem item, char **returnString)
1518 {
1519 if (self == NULL || returnString == NULL) {
1520 LOGE("Invalid input parameter.");
1521 return HCF_INVALID_PARAMS;
1522 }
1523 bool isPrivate = false;
1524 HcfResult res = CheckEcKeySelf(self, &isPrivate);
1525 if (res != HCF_SUCCESS) {
1526 LOGE("Invalid input key");
1527 return HCF_INVALID_PARAMS;
1528 }
1529
1530 switch (item) {
1531 case ECC_FIELD_TYPE_STR:
1532 res = GetFieldType(self, isPrivate, returnString);
1533 break;
1534 case ECC_CURVE_NAME_STR:
1535 res = GetCurveName(self, isPrivate, returnString);
1536 break;
1537 default:
1538 res = HCF_INVALID_PARAMS;
1539 LOGE("Invalid spec of ec string");
1540 break;
1541 }
1542 return res;
1543 }
1544
GetEcKeySpecInt(const HcfKey * self,const AsyKeySpecItem item,int * returnInt)1545 static HcfResult GetEcKeySpecInt(const HcfKey *self, const AsyKeySpecItem item, int *returnInt)
1546 {
1547 if (self == NULL || returnInt == NULL) {
1548 LOGE("Invalid input parameter.");
1549 return HCF_INVALID_PARAMS;
1550 }
1551 bool isPrivate = false;
1552 HcfResult res = CheckEcKeySelf(self, &isPrivate);
1553 if (res != HCF_SUCCESS) {
1554 LOGE("Invalid input key");
1555 return HCF_INVALID_PARAMS;
1556 }
1557 const EC_GROUP *group = NULL;
1558 if (isPrivate) {
1559 group = OpensslEcKeyGet0Group(((HcfOpensslEccPriKey *)self)->ecKey);
1560 } else {
1561 group = OpensslEcKeyGet0Group(((HcfOpensslEccPubKey *)self)->ecKey);
1562 }
1563 switch (item) {
1564 case ECC_H_INT:
1565 res = GetCofactor(group, returnInt);
1566 break;
1567 case ECC_FIELD_SIZE_INT:
1568 res = GetFieldSize(group, returnInt);
1569 break;
1570 default:
1571 res = HCF_INVALID_PARAMS;
1572 LOGE("invalid ec key int spec");
1573 break;
1574 }
1575 return res;
1576 }
1577
GetECPubKeySpecBigInteger(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1578 static HcfResult GetECPubKeySpecBigInteger(const HcfPubKey *self, const AsyKeySpecItem item,
1579 HcfBigInteger *returnBigInteger)
1580 {
1581 return GetEcKeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
1582 }
1583
GetECPubKeySpecString(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)1584 static HcfResult GetECPubKeySpecString(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
1585 {
1586 return GetEcKeySpecString((HcfKey *)self, item, returnString);
1587 }
1588
GetECPubKeySpecInt(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)1589 static HcfResult GetECPubKeySpecInt(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
1590 {
1591 return GetEcKeySpecInt((HcfKey *)self, item, returnInt);
1592 }
1593
GetECPriKeySpecBigInteger(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)1594 static HcfResult GetECPriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item,
1595 HcfBigInteger *returnBigInteger)
1596 {
1597 return GetEcKeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
1598 }
1599
GetECPriKeySpecString(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)1600 static HcfResult GetECPriKeySpecString(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
1601 {
1602 return GetEcKeySpecString((HcfKey *)self, item, returnString);
1603 }
1604
GetECPriKeySpecInt(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)1605 static HcfResult GetECPriKeySpecInt(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
1606 {
1607 return GetEcKeySpecInt((HcfKey *)self, item, returnInt);
1608 }
1609
PackEccPubKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslEccPubKey ** returnObj)1610 static HcfResult PackEccPubKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
1611 HcfOpensslEccPubKey **returnObj)
1612 {
1613 HcfOpensslEccPubKey *returnPubKey = (HcfOpensslEccPubKey *)HcfMalloc(sizeof(HcfOpensslEccPubKey), 0);
1614 if (returnPubKey == NULL) {
1615 LOGE("Failed to allocate returnPubKey memory!");
1616 return HCF_ERR_MALLOC;
1617 }
1618 char *tmpFieldType = NULL;
1619 if (fieldType != NULL) {
1620 size_t len = HcfStrlen(fieldType);
1621 if (len == 0) {
1622 LOGE("fieldType is empty!");
1623 HcfFree(returnPubKey);
1624 return HCF_INVALID_PARAMS;
1625 }
1626 tmpFieldType = (char *)HcfMalloc(len + 1, 0);
1627 if (tmpFieldType == NULL) {
1628 LOGE("Alloc tmpFieldType memory failed.");
1629 HcfFree(returnPubKey);
1630 return HCF_ERR_MALLOC;
1631 }
1632 (void)memcpy_s(tmpFieldType, len, fieldType, len);
1633 }
1634
1635 returnPubKey->base.base.base.destroy = DestroyEccPubKey;
1636 returnPubKey->base.base.base.getClass = GetEccPubKeyClass;
1637 returnPubKey->base.base.getAlgorithm = GetEccPubKeyAlgorithm;
1638 returnPubKey->base.base.getEncoded = GetEccPubKeyEncoded;
1639 returnPubKey->base.base.getEncodedPem = GetEccPubKeyEncodedPem;
1640 returnPubKey->base.base.getFormat = GetEccPubKeyFormat;
1641 returnPubKey->base.getAsyKeySpecBigInteger = GetECPubKeySpecBigInteger;
1642 returnPubKey->base.getAsyKeySpecString = GetECPubKeySpecString;
1643 returnPubKey->base.getAsyKeySpecInt = GetECPubKeySpecInt;
1644 returnPubKey->base.getEncodedDer = GetEccPubKeyEncodedDer;
1645 returnPubKey->curveId = curveId;
1646 returnPubKey->ecKey = ecKey;
1647 returnPubKey->fieldType = tmpFieldType;
1648
1649 *returnObj = returnPubKey;
1650 return HCF_SUCCESS;
1651 }
1652
PackEccPriKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslEccPriKey ** returnObj)1653 static HcfResult PackEccPriKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
1654 HcfOpensslEccPriKey **returnObj)
1655 {
1656 HcfOpensslEccPriKey *returnPriKey = (HcfOpensslEccPriKey *)HcfMalloc(sizeof(HcfOpensslEccPriKey), 0);
1657 if (returnPriKey == NULL) {
1658 LOGE("Failed to allocate returnPriKey memory!");
1659 return HCF_ERR_MALLOC;
1660 }
1661 char *tmpFieldType = NULL;
1662 if (fieldType != NULL) {
1663 size_t len = HcfStrlen(fieldType);
1664 if (len == 0) {
1665 LOGE("fieldType is empty!");
1666 HcfFree(returnPriKey);
1667 return HCF_INVALID_PARAMS;
1668 }
1669 tmpFieldType = (char *)HcfMalloc(len + 1, 0);
1670 if (tmpFieldType == NULL) {
1671 LOGE("Alloc tmpFieldType memory failed.");
1672 HcfFree(returnPriKey);
1673 return HCF_ERR_MALLOC;
1674 }
1675 (void)memcpy_s(tmpFieldType, len, fieldType, len);
1676 }
1677
1678 returnPriKey->base.base.base.destroy = DestroyEccPriKey;
1679 returnPriKey->base.base.base.getClass = GetEccPriKeyClass;
1680 returnPriKey->base.base.getAlgorithm = GetEccPriKeyAlgorithm;
1681 returnPriKey->base.base.getEncoded = GetEccPriKeyEncoded;
1682 returnPriKey->base.base.getEncodedPem = GetEccPriKeyEncodedPem;
1683 returnPriKey->base.base.getFormat = GetEccPriKeyFormat;
1684 returnPriKey->base.clearMem = EccPriKeyClearMem;
1685 returnPriKey->base.getAsyKeySpecBigInteger = GetECPriKeySpecBigInteger;
1686 returnPriKey->base.getAsyKeySpecString = GetECPriKeySpecString;
1687 returnPriKey->base.getAsyKeySpecInt = GetECPriKeySpecInt;
1688 returnPriKey->base.getEncodedDer = GetECPriKeyEncodedDer;
1689 returnPriKey->curveId = curveId;
1690 returnPriKey->ecKey = ecKey;
1691 returnPriKey->fieldType = tmpFieldType;
1692
1693 *returnObj = returnPriKey;
1694 return HCF_SUCCESS;
1695 }
1696
PackEccKeyPair(HcfOpensslEccPubKey * pubKey,HcfOpensslEccPriKey * priKey,HcfOpensslEccKeyPair ** returnObj)1697 static HcfResult PackEccKeyPair(HcfOpensslEccPubKey *pubKey, HcfOpensslEccPriKey *priKey,
1698 HcfOpensslEccKeyPair **returnObj)
1699 {
1700 HcfOpensslEccKeyPair *returnKeyPair = (HcfOpensslEccKeyPair *)HcfMalloc(sizeof(HcfOpensslEccKeyPair), 0);
1701 if (returnKeyPair == NULL) {
1702 LOGE("Failed to allocate returnKeyPair memory!");
1703 return HCF_ERR_MALLOC;
1704 }
1705 returnKeyPair->base.base.getClass = GetEccKeyPairClass;
1706 returnKeyPair->base.base.destroy = DestroyEccKeyPair;
1707 returnKeyPair->base.pubKey = (HcfPubKey *)pubKey;
1708 returnKeyPair->base.priKey = (HcfPriKey *)priKey;
1709
1710 *returnObj = returnKeyPair;
1711 return HCF_SUCCESS;
1712 }
1713
ConvertEcPubKey(int32_t curveId,HcfBlob * pubKeyBlob,HcfOpensslEccPubKey ** returnPubKey)1714 static HcfResult ConvertEcPubKey(int32_t curveId, HcfBlob *pubKeyBlob, HcfOpensslEccPubKey **returnPubKey)
1715 {
1716 const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
1717 EC_KEY *ecKey = OpensslD2iEcPubKey(NULL, &tmpData, pubKeyBlob->len);
1718 if (ecKey == NULL) {
1719 LOGE("d2i_EC_PUBKEY fail.");
1720 HcfPrintOpensslError();
1721 return HCF_ERR_CRYPTO_OPERATION;
1722 }
1723 HcfResult res = PackEccPubKey(curveId, ecKey, g_eccGenerateFieldType, returnPubKey);
1724 if (res != HCF_SUCCESS) {
1725 LOGE("PackEccPubKey failed.");
1726 OpensslEcKeyFree(ecKey);
1727 return res;
1728 }
1729 return HCF_SUCCESS;
1730 }
1731
ConvertPriFromEncoded(EC_KEY ** eckey,HcfBlob * priKeyBlob)1732 static HcfResult ConvertPriFromEncoded(EC_KEY **eckey, HcfBlob *priKeyBlob)
1733 {
1734 const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
1735 EVP_PKEY *pkey = OpensslD2iPrivateKey(EVP_PKEY_EC, NULL, &tmpData, priKeyBlob->len);
1736 if (pkey == NULL) {
1737 HcfPrintOpensslError();
1738 LOGE("d2i pri key failed.");
1739 return HCF_ERR_CRYPTO_OPERATION;
1740 }
1741 *eckey = EVP_PKEY_get1_EC_KEY(pkey);
1742 OpensslEvpPkeyFree(pkey);
1743 if (*eckey == NULL) {
1744 LOGE("Get eckey failed");
1745 HcfPrintOpensslError();
1746 return HCF_ERR_CRYPTO_OPERATION;
1747 }
1748 return HCF_SUCCESS;
1749 }
1750
ConvertEcPriKey(int32_t curveId,HcfBlob * priKeyBlob,HcfOpensslEccPriKey ** returnPriKey)1751 static HcfResult ConvertEcPriKey(int32_t curveId, HcfBlob *priKeyBlob, HcfOpensslEccPriKey **returnPriKey)
1752 {
1753 EC_KEY *ecKey = NULL;
1754 HcfResult res = ConvertPriFromEncoded(&ecKey, priKeyBlob);
1755 if (res != HCF_SUCCESS) {
1756 LOGE("i2d for private key failed");
1757 HcfPrintOpensslError();
1758 return HCF_ERR_CRYPTO_OPERATION;
1759 }
1760 if (ecKey == NULL) {
1761 LOGE("d2i ec private key fail");
1762 HcfPrintOpensslError();
1763 return HCF_ERR_CRYPTO_OPERATION;
1764 }
1765 res = PackEccPriKey(curveId, ecKey, g_eccGenerateFieldType, returnPriKey);
1766 if (res != HCF_SUCCESS) {
1767 LOGE("Pack ec pri key failed.");
1768 OpensslEcKeyFree(ecKey);
1769 return res;
1770 }
1771 return HCF_SUCCESS;
1772 }
1773
EngineConvertEccKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)1774 static HcfResult EngineConvertEccKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
1775 HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
1776 {
1777 (void)params;
1778 if ((self == NULL) || (returnKeyPair == NULL)) {
1779 LOGE("Invalid input parameter.");
1780 return HCF_INVALID_PARAMS;
1781 }
1782 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1783 return HCF_INVALID_PARAMS;
1784 }
1785 bool pubKeyValid = HcfIsBlobValid(pubKeyBlob);
1786 bool priKeyValid = HcfIsBlobValid(priKeyBlob);
1787 if ((!pubKeyValid) && (!priKeyValid)) {
1788 LOGE("The private key and public key cannot both be NULL.");
1789 return HCF_INVALID_PARAMS;
1790 }
1791
1792 HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1793 HcfResult res = HCF_SUCCESS;
1794 HcfOpensslEccPubKey *pubKey = NULL;
1795 HcfOpensslEccPriKey *priKey = NULL;
1796 HcfOpensslEccKeyPair *keyPair = NULL;
1797 do {
1798 if (pubKeyValid) {
1799 res = ConvertEcPubKey(impl->curveId, pubKeyBlob, &pubKey);
1800 if (res != HCF_SUCCESS) {
1801 break;
1802 }
1803 }
1804 if (priKeyValid) {
1805 res = ConvertEcPriKey(impl->curveId, priKeyBlob, &priKey);
1806 if (res != HCF_SUCCESS) {
1807 break;
1808 }
1809 }
1810 res = PackEccKeyPair(pubKey, priKey, &keyPair);
1811 } while (0);
1812 if (res != HCF_SUCCESS) {
1813 HcfObjDestroy(pubKey);
1814 HcfObjDestroy(priKey);
1815 return res;
1816 }
1817
1818 *returnKeyPair = (HcfKeyPair *)keyPair;
1819 return HCF_SUCCESS;
1820 }
1821
PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfPubKey ** returnObj)1822 static HcfResult PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1823 EC_KEY *ecKey, HcfPubKey **returnObj)
1824 {
1825 HcfOpensslEccPubKey *pubKey = NULL;
1826 HcfResult res = PackEccPubKey(impl->curveId, ecKey, fieldType, &pubKey);
1827 if (res != HCF_SUCCESS) {
1828 return res;
1829 }
1830 *returnObj = (HcfPubKey *)pubKey;
1831 return HCF_SUCCESS;
1832 }
1833
PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfPriKey ** returnObj)1834 static HcfResult PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1835 EC_KEY *ecKey, HcfPriKey **returnObj)
1836 {
1837 HcfOpensslEccPriKey *priKey = NULL;
1838 HcfResult res = PackEccPriKey(impl->curveId, ecKey, fieldType, &priKey);
1839 if (res != HCF_SUCCESS) {
1840 return res;
1841 }
1842 *returnObj = (HcfPriKey *)priKey;
1843 return HCF_SUCCESS;
1844 }
1845
CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslEccImpl * impl,const char * fieldType,EC_KEY * ecKey,HcfKeyPair ** returnObj)1846 static HcfResult CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslEccImpl *impl, const char *fieldType,
1847 EC_KEY *ecKey, HcfKeyPair **returnObj)
1848 {
1849 EC_KEY *ecPriKey = EC_KEY_dup(ecKey);
1850 if (ecPriKey == NULL) {
1851 LOGD("[error] copy ecKey fail.");
1852 return HCF_ERR_CRYPTO_OPERATION;
1853 }
1854 HcfOpensslEccPriKey *priKey = NULL;
1855 HcfResult res = PackEccPriKey(impl->curveId, ecPriKey, fieldType, &priKey);
1856 if (res != HCF_SUCCESS) {
1857 OpensslEcKeyFree(ecPriKey);
1858 return res;
1859 }
1860 HcfOpensslEccPubKey *pubKey = NULL;
1861 EC_KEY *ecPubKey = EC_KEY_dup(ecKey);
1862 if (ecPubKey == NULL) {
1863 LOGD("[error] copy ecKey fail.");
1864 HcfObjDestroy(priKey);
1865 return HCF_ERR_CRYPTO_OPERATION;
1866 }
1867 res = PackEccPubKey(impl->curveId, ecPubKey, fieldType, &pubKey);
1868 if (res != HCF_SUCCESS) {
1869 HcfObjDestroy(priKey);
1870 OpensslEcKeyFree(ecPubKey);
1871 return res;
1872 }
1873
1874 HcfOpensslEccKeyPair *returnKeyPair = (HcfOpensslEccKeyPair *)HcfMalloc(sizeof(HcfOpensslEccKeyPair), 0);
1875 if (returnKeyPair == NULL) {
1876 LOGE("Failed to allocate returnKeyPair memory!");
1877 HcfObjDestroy(pubKey);
1878 HcfObjDestroy(priKey);
1879 return HCF_ERR_MALLOC;
1880 }
1881 returnKeyPair->base.base.getClass = GetEccKeyPairClass;
1882 returnKeyPair->base.base.destroy = DestroyEccKeyPair;
1883 returnKeyPair->base.pubKey = (HcfPubKey *)pubKey;
1884 returnKeyPair->base.priKey = (HcfPriKey *)priKey;
1885
1886 *returnObj = (HcfKeyPair *)returnKeyPair;
1887 return HCF_SUCCESS;
1888 }
1889
EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnObj)1890 static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnObj)
1891 {
1892 if ((self == NULL) || (returnObj == NULL)) {
1893 LOGE("Invalid input parameter.");
1894 return HCF_INVALID_PARAMS;
1895 }
1896 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1897 return HCF_INVALID_PARAMS;
1898 }
1899
1900 HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1901 EC_KEY *ecKey = NULL;
1902 HcfResult res = NewEcKeyPair(impl->curveId, &ecKey);
1903 if (res != HCF_SUCCESS) {
1904 return res;
1905 }
1906 res = CreateAndAssignKeyPair(impl, g_eccGenerateFieldType, ecKey, returnObj);
1907 OpensslEcKeyFree(ecKey);
1908 if (res != HCF_SUCCESS) {
1909 LOGE("CreateAndAssignKeyPair failed.");
1910 return res;
1911 }
1912 return HCF_SUCCESS;
1913 }
1914
EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfKeyPair ** returnKeyPair)1915 static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1916 HcfKeyPair **returnKeyPair)
1917 {
1918 if ((self == NULL) || (returnKeyPair == NULL) || (params == NULL) ||
1919 (((HcfEccCommParamsSpec *)params)->field == NULL)) {
1920 LOGE("Invalid input parameter.");
1921 return HCF_INVALID_PARAMS;
1922 }
1923 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1924 return HCF_INVALID_PARAMS;
1925 }
1926
1927 HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1928 EC_KEY *ecKey = NULL;
1929 HcfResult res = GenKeyPairEcKeyBySpec(params, &ecKey);
1930 if (res != HCF_SUCCESS) {
1931 LOGE("Gen ec key pair with spec failed.");
1932 return res;
1933 }
1934
1935 // curveId == 0 means no curve to match.
1936 int32_t curveId = (int32_t)OpensslEcGroupGetCurveName(OpensslEcKeyGet0Group(ecKey));
1937 if (curveId != 0) {
1938 impl->curveId = curveId;
1939 }
1940 // deep copy of ecKey, free ecKey whether it succeed or failed.
1941 res = CreateAndAssignKeyPair(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnKeyPair);
1942 OpensslEcKeyFree(ecKey);
1943 if (res != HCF_SUCCESS) {
1944 LOGE("CreateAndAssignKeyPair failed.");
1945 return res;
1946 }
1947
1948 return HCF_SUCCESS;
1949 }
1950
EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPubKey ** returnPubKey)1951 static HcfResult EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1952 HcfPubKey **returnPubKey)
1953 {
1954 if ((self == NULL) || (returnPubKey == NULL) || (params == NULL) ||
1955 (((HcfEccCommParamsSpec *)params)->field == NULL)) {
1956 LOGE("Invalid input parameter.");
1957 return HCF_INVALID_PARAMS;
1958 }
1959 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1960 return HCF_INVALID_PARAMS;
1961 }
1962
1963 HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1964 EC_KEY *ecKey = NULL;
1965 HcfResult res = GenPubKeyEcKeyBySpec(params, &ecKey);
1966 if (res != HCF_SUCCESS) {
1967 LOGE("Gen ec pubKey with spec failed.");
1968 return res;
1969 }
1970 int32_t curveId = (int32_t)OpensslEcGroupGetCurveName(OpensslEcKeyGet0Group(ecKey));
1971 if (curveId != 0) {
1972 impl->curveId = curveId;
1973 }
1974 res = PackAndAssignPubKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPubKey);
1975 if (res != HCF_SUCCESS) {
1976 LOGE("PackAndAssignPubKey failed.");
1977 OpensslEcKeyFree(ecKey);
1978 return res;
1979 }
1980
1981 return HCF_SUCCESS;
1982 }
1983
EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPriKey ** returnPriKey)1984 static HcfResult EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1985 HcfPriKey **returnPriKey)
1986 {
1987 if ((self == NULL) || (returnPriKey == NULL) || (params == NULL) ||
1988 (((HcfEccCommParamsSpec *)params)->field == NULL)) {
1989 LOGE("Invalid input parameter.");
1990 return HCF_INVALID_PARAMS;
1991 }
1992 if (!HcfIsClassMatch((HcfObjectBase *)self, GetEccKeyPairGeneratorClass())) {
1993 return HCF_INVALID_PARAMS;
1994 }
1995
1996 HcfAsyKeyGeneratorSpiOpensslEccImpl *impl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)self;
1997 EC_KEY *ecKey = NULL;
1998 HcfResult res = GenPriKeyEcKeyBySpec(params, &ecKey);
1999 if (res != HCF_SUCCESS) {
2000 LOGE("Gen ec pubKey with spec failed.");
2001 return res;
2002 }
2003
2004 int32_t curveId = (int32_t)OpensslEcGroupGetCurveName(OpensslEcKeyGet0Group(ecKey));
2005 if (curveId != 0) {
2006 impl->curveId = curveId;
2007 }
2008
2009 res = PackAndAssignPriKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPriKey);
2010 if (res != HCF_SUCCESS) {
2011 LOGE("PackAndAssignPriKey failed.");
2012 OpensslEcKeyFree(ecKey);
2013 return res;
2014 }
2015
2016 return HCF_SUCCESS;
2017 }
2018
HcfAsyKeyGeneratorSpiEccCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)2019 HcfResult HcfAsyKeyGeneratorSpiEccCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
2020 {
2021 if (params == NULL || returnObj == NULL) {
2022 LOGE("Invalid input parameter.");
2023 return HCF_INVALID_PARAMS;
2024 }
2025 int32_t curveId = 0;
2026 if (params->bits != 0) {
2027 if (GetOpensslCurveId(params->bits, &curveId) != HCF_SUCCESS) {
2028 return HCF_INVALID_PARAMS;
2029 }
2030 }
2031
2032 HcfAsyKeyGeneratorSpiOpensslEccImpl *returnImpl = (HcfAsyKeyGeneratorSpiOpensslEccImpl *)HcfMalloc(
2033 sizeof(HcfAsyKeyGeneratorSpiOpensslEccImpl), 0);
2034 if (returnImpl == NULL) {
2035 LOGE("Failed to allocate returnImpl memroy!");
2036 return HCF_ERR_MALLOC;
2037 }
2038 returnImpl->base.base.getClass = GetEccKeyPairGeneratorClass;
2039 returnImpl->base.base.destroy = DestroyEccKeyPairGenerator;
2040 returnImpl->base.engineConvertKey = EngineConvertEccKey;
2041 returnImpl->base.engineGenerateKeyPair = EngineGenerateKeyPair;
2042 returnImpl->base.engineGenerateKeyPairBySpec = EngineGenerateKeyPairBySpec;
2043 returnImpl->base.engineGeneratePubKeyBySpec = EngineGeneratePubKeyBySpec;
2044 returnImpl->base.engineGeneratePriKeyBySpec = EngineGeneratePriKeyBySpec;
2045 returnImpl->curveId = curveId;
2046
2047 *returnObj = (HcfAsyKeyGeneratorSpi *)returnImpl;
2048 return HCF_SUCCESS;
2049 }
2050