1 /*
2 * Copyright (C) 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 "crypto_asym_key.h"
17 #include <string.h>
18 #include <stdlib.h>
19 #include <securec.h>
20 #include "key_pair.h"
21 #include "detailed_ecc_key_params.h"
22 #include "detailed_dh_key_params.h"
23 #include "detailed_rsa_key_params.h"
24 #include "detailed_dsa_key_params.h"
25 #include "detailed_alg_25519_key_params.h"
26 #include "params_parser.h"
27 #include "ecc_key_util.h"
28 #include "dh_key_util.h"
29 #include "key_utils.h"
30 #include "memory.h"
31 #include "pub_key.h"
32 #include "pri_key.h"
33 #include "result.h"
34 #include "blob.h"
35 #include "object_base.h"
36 #include "native_common.h"
37 #include "crypto_common.h"
38 #include "big_integer.h"
39 #include "asy_key_generator.h"
40
41
42 typedef struct OH_CryptoAsymKeyGenerator {
43 HcfAsyKeyGenerator *base;
44 HcfKeyDecodingParamsSpec *decSpec;
45 } OH_CryptoAsymKeyGenerator;
46
47 typedef struct OH_CryptoKeyPair {
48 HcfObjectBase base;
49
50 HcfPriKey *priKey;
51
52 HcfPubKey *pubKey;
53 } OH_CryptoKeyPair;
54
55 typedef struct OH_CryptoPubKey {
56 HcfKey base;
57
58 HcfResult (*getAsyKeySpecBigInteger)(const HcfPubKey *self, const AsyKeySpecItem item,
59 HcfBigInteger *returnBigInteger);
60
61 HcfResult (*getAsyKeySpecString)(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString);
62
63 HcfResult (*getAsyKeySpecInt)(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt);
64
65 HcfResult (*getEncodedDer)(const HcfPubKey *self, const char *format, HcfBlob *returnBlob);
66 } OH_CryptoPubKey;
67
68 typedef struct OH_CryptoPrivKey {
69 HcfKey base;
70
71 HcfResult (*getAsyKeySpecBigInteger)(const HcfPriKey *self, const AsyKeySpecItem item,
72 HcfBigInteger *returnBigInteger);
73
74 HcfResult (*getAsyKeySpecString)(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString);
75
76 HcfResult (*getAsyKeySpecInt)(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt);
77
78 HcfResult (*getEncodedDer)(const HcfPriKey *self, const char *format, HcfBlob *returnBlob);
79
80 HcfResult (*getEncodedPem)(const HcfPriKey *self, HcfParamsSpec *params, const char *format, char **returnString);
81
82 void (*clearMem)(HcfPriKey *self);
83 } OH_CryptoPrivKey;
84
85 typedef struct OH_CryptoPrivKeyEncodingParams {
86 HcfParamsSpec base;
87
88 char *password;
89
90 char *cipher;
91 } OH_CryptoPrivKeyEncodingParams;
92
93 typedef struct OH_CryptoAsymKeySpec {
94 char *algName;
95 HcfAsyKeySpecType specType;
96 } OH_CryptoAsymKeySpec;
97
98 typedef struct OH_CryptoAsymKeyGeneratorWithSpec {
99 HcfAsyKeyGeneratorBySpec *generator;
100 HcfAsyKeySpecType specType;
101 } OH_CryptoAsymKeyGeneratorWithSpec;
102
103 typedef struct OH_CryptoEcPoint {
104 HcfPoint pointBase;
105 char *curveName;
106 } OH_CryptoEcPoint;
107
OH_CryptoAsymKeyGenerator_Create(const char * algoName,OH_CryptoAsymKeyGenerator ** ctx)108 OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx)
109 {
110 if (ctx == NULL) {
111 return CRYPTO_INVALID_PARAMS;
112 }
113 OH_CryptoAsymKeyGenerator *tmpCtx = HcfMalloc(sizeof(OH_CryptoAsymKeyGenerator), 0);
114 if (tmpCtx == NULL) {
115 return CRYPTO_MEMORY_ERROR;
116 }
117 HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, &(tmpCtx->base));
118 if (ret != HCF_SUCCESS) {
119 HcfFree(tmpCtx);
120 tmpCtx = NULL;
121 return GetOhCryptoErrCode(ret);
122 }
123 *ctx = tmpCtx;
124 return GetOhCryptoErrCode(ret);
125 }
126
OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator * ctx,OH_CryptoKeyPair ** keyCtx)127 OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx)
128 {
129 if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->generateKeyPair == NULL) || (keyCtx == NULL)) {
130 return CRYPTO_INVALID_PARAMS;
131 }
132 HcfResult ret = ctx->base->generateKeyPair((HcfAsyKeyGenerator *)(ctx->base), NULL, (HcfKeyPair **)keyCtx);
133 return GetOhCryptoErrCode(ret);
134 }
135
OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator * ctx,const unsigned char * password,uint32_t passwordLen)136 OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator *ctx, const unsigned char *password,
137 uint32_t passwordLen)
138 {
139 if ((ctx == NULL) || (password == NULL) || (passwordLen == 0)) {
140 return CRYPTO_PARAMETER_CHECK_FAILED;
141 }
142 HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0);
143 if (decSpec == NULL) {
144 return CRYPTO_MEMORY_ERROR;
145 }
146 decSpec->password = (char *)HcfMalloc(passwordLen + 1, 0);
147 if (decSpec->password == NULL) {
148 HcfFree(decSpec);
149 decSpec = NULL;
150 return CRYPTO_MEMORY_ERROR;
151 }
152 (void)memcpy_s(decSpec->password, passwordLen, password, passwordLen);
153 ctx->decSpec = decSpec;
154 return CRYPTO_SUCCESS;
155 }
156
ProcessPriKeyData(Crypto_DataBlob * priKeyData,char ** priKeyStr)157 static OH_Crypto_ErrCode ProcessPriKeyData(Crypto_DataBlob *priKeyData, char **priKeyStr)
158 {
159 if (priKeyData == NULL) {
160 return CRYPTO_SUCCESS;
161 }
162
163 *priKeyStr = (char *)HcfMalloc(priKeyData->len + 1, 0);
164 if (*priKeyStr == NULL) {
165 return CRYPTO_MEMORY_ERROR;
166 }
167 (void)memcpy_s(*priKeyStr, priKeyData->len, priKeyData->data, priKeyData->len);
168 return CRYPTO_SUCCESS;
169 }
170
ProcessPubKeyData(Crypto_DataBlob * pubKeyData,char ** pubKeyStr)171 static OH_Crypto_ErrCode ProcessPubKeyData(Crypto_DataBlob *pubKeyData, char **pubKeyStr)
172 {
173 if (pubKeyData == NULL) {
174 return CRYPTO_SUCCESS;
175 }
176
177 *pubKeyStr = (char *)HcfMalloc(pubKeyData->len + 1, 0);
178 if (*pubKeyStr == NULL) {
179 return CRYPTO_MEMORY_ERROR;
180 }
181 (void)memcpy_s(*pubKeyStr, pubKeyData->len, pubKeyData->data, pubKeyData->len);
182 return CRYPTO_SUCCESS;
183 }
184
ExecutePemConversion(OH_CryptoAsymKeyGenerator * ctx,char * pubKeyStr,char * priKeyStr,OH_CryptoKeyPair ** keyCtx)185 static HcfResult ExecutePemConversion(OH_CryptoAsymKeyGenerator *ctx, char *pubKeyStr,
186 char *priKeyStr, OH_CryptoKeyPair **keyCtx)
187 {
188 return ctx->base->convertPemKey == NULL ? HCF_INVALID_PARAMS :
189 ctx->base->convertPemKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec),
190 pubKeyStr, priKeyStr, (HcfKeyPair **)keyCtx);
191 }
192
CleanupPemMemory(char * priKeyStr,char * pubKeyStr)193 static void CleanupPemMemory(char *priKeyStr, char *pubKeyStr)
194 {
195 if (priKeyStr != NULL) {
196 (void)memset_s(priKeyStr, strlen(priKeyStr), 0, strlen(priKeyStr));
197 HcfFree(priKeyStr);
198 }
199 if (pubKeyStr != NULL) {
200 (void)memset_s(pubKeyStr, strlen(pubKeyStr), 0, strlen(pubKeyStr));
201 HcfFree(pubKeyStr);
202 }
203 }
204
HandlePemConversion(OH_CryptoAsymKeyGenerator * ctx,Crypto_DataBlob * pubKeyData,Crypto_DataBlob * priKeyData,OH_CryptoKeyPair ** keyCtx)205 static OH_Crypto_ErrCode HandlePemConversion(OH_CryptoAsymKeyGenerator *ctx, Crypto_DataBlob *pubKeyData,
206 Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx)
207 {
208 char *priKeyStr = NULL;
209 char *pubKeyStr = NULL;
210 OH_Crypto_ErrCode ret = ProcessPriKeyData(priKeyData, &priKeyStr);
211 if (ret != CRYPTO_SUCCESS) {
212 return ret;
213 }
214
215 ret = ProcessPubKeyData(pubKeyData, &pubKeyStr);
216 if (ret != CRYPTO_SUCCESS) {
217 CleanupPemMemory(priKeyStr, pubKeyStr);
218 priKeyStr = NULL;
219 pubKeyStr = NULL;
220 return ret;
221 }
222
223 HcfResult hcfRet = ExecutePemConversion(ctx, pubKeyStr, priKeyStr, keyCtx);
224 CleanupPemMemory(priKeyStr, pubKeyStr);
225 priKeyStr = NULL;
226 pubKeyStr = NULL;
227 return GetOhCryptoErrCode(hcfRet);
228 }
229
OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator * ctx,Crypto_EncodingType type,Crypto_DataBlob * pubKeyData,Crypto_DataBlob * priKeyData,OH_CryptoKeyPair ** keyCtx)230 OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type,
231 Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx)
232 {
233 if ((ctx == NULL) || (ctx->base == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) {
234 return CRYPTO_INVALID_PARAMS;
235 }
236
237 switch (type) {
238 case CRYPTO_PEM:
239 return HandlePemConversion(ctx, pubKeyData, priKeyData, keyCtx);
240 case CRYPTO_DER:
241 return GetOhCryptoErrCode(ctx->base->convertKey == NULL ? HCF_INVALID_PARAMS :
242 ctx->base->convertKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec),
243 (HcfBlob *)pubKeyData, (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx));
244 default:
245 return CRYPTO_INVALID_PARAMS;
246 }
247 }
248
OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator * ctx)249 const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx)
250 {
251 if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->getAlgoName == NULL)) {
252 return NULL;
253 }
254 return ctx->base->getAlgoName((HcfAsyKeyGenerator *)(ctx->base));
255 }
256
FreeDecParamsSpec(HcfKeyDecodingParamsSpec * decSpec)257 static void FreeDecParamsSpec(HcfKeyDecodingParamsSpec *decSpec)
258 {
259 if (decSpec == NULL) {
260 return;
261 }
262
263 if (decSpec->password != NULL) {
264 (void)memset_s(decSpec->password, strlen(decSpec->password), 0, strlen(decSpec->password));
265 HcfFree(decSpec->password);
266 decSpec->password = NULL;
267 }
268 HcfFree(decSpec);
269 }
270
OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator * ctx)271 void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx)
272 {
273 if (ctx == NULL) {
274 return;
275 }
276 HcfObjDestroy(ctx->base);
277 ctx->base = NULL;
278 FreeDecParamsSpec(ctx->decSpec);
279 ctx->decSpec = NULL;
280 HcfFree(ctx);
281 }
282
OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair * keyCtx)283 void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx)
284 {
285 if (keyCtx == NULL) {
286 return;
287 }
288 if (keyCtx->base.destroy != NULL) {
289 keyCtx->base.destroy((HcfObjectBase *)keyCtx);
290 return;
291 }
292 if ((keyCtx->priKey != NULL) && (keyCtx->priKey->base.base.destroy != NULL)) {
293 HcfObjDestroy(keyCtx->priKey);
294 keyCtx->priKey = NULL;
295 HcfFree(keyCtx);
296 return;
297 }
298 if ((keyCtx->pubKey != NULL) && (keyCtx->pubKey->base.base.destroy != NULL)) {
299 HcfObjDestroy(keyCtx->pubKey);
300 keyCtx->pubKey = NULL;
301 HcfFree(keyCtx);
302 return;
303 }
304 }
305
OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair * keyCtx)306 OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx)
307 {
308 if (keyCtx == NULL) {
309 return NULL;
310 }
311 return (OH_CryptoPubKey *)keyCtx->pubKey;
312 }
313
OH_CryptoKeyPair_GetPrivKey(OH_CryptoKeyPair * keyCtx)314 OH_CryptoPrivKey *OH_CryptoKeyPair_GetPrivKey(OH_CryptoKeyPair *keyCtx)
315 {
316 if (keyCtx == NULL) {
317 return NULL;
318 }
319 return (OH_CryptoPrivKey *)keyCtx->priKey;
320 }
321
OH_CryptoPubKey_Encode(OH_CryptoPubKey * key,Crypto_EncodingType type,const char * encodingStandard,Crypto_DataBlob * out)322 OH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingType type,
323 const char *encodingStandard, Crypto_DataBlob *out)
324 {
325 if ((key == NULL) || (out == NULL)) {
326 return CRYPTO_INVALID_PARAMS;
327 }
328 HcfResult ret = HCF_SUCCESS;
329 char *pemStr = NULL;
330 switch (type) {
331 case CRYPTO_PEM:
332 if (key->base.getEncodedPem == NULL) {
333 return CRYPTO_INVALID_PARAMS;
334 }
335 ret = key->base.getEncodedPem((HcfKey *)key, encodingStandard, &pemStr);
336 if (ret != HCF_SUCCESS) {
337 break;
338 }
339 out->data = (uint8_t *)pemStr;
340 out->len = strlen(pemStr);
341 break;
342 case CRYPTO_DER:
343 if (encodingStandard != NULL) {
344 ret = key->getEncodedDer == NULL ? HCF_INVALID_PARAMS :
345 key->getEncodedDer((HcfPubKey *)key, encodingStandard, (HcfBlob *)out);
346 break;
347 } else {
348 ret = key->base.getEncoded == NULL ? HCF_INVALID_PARAMS
349 : key->base.getEncoded((HcfKey *)key, (HcfBlob *)out);
350 break;
351 }
352 default:
353 return CRYPTO_INVALID_PARAMS;
354 }
355
356 return GetOhCryptoErrCode(ret);
357 }
358
OH_CryptoPubKey_GetParam(OH_CryptoPubKey * key,CryptoAsymKey_ParamType item,Crypto_DataBlob * value)359 OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value)
360 {
361 if ((key == NULL) || (value == NULL)) {
362 return CRYPTO_INVALID_PARAMS;
363 }
364 HcfResult ret = HCF_SUCCESS;
365 int32_t *returnInt = NULL;
366 char *returnStr = NULL;
367 HcfBigInteger bigIntValue = {0};
368 switch (item) {
369 case CRYPTO_DH_L_INT:
370 case CRYPTO_ECC_H_INT:
371 case CRYPTO_ECC_FIELD_SIZE_INT:
372 returnInt = (int32_t *)HcfMalloc(sizeof(int32_t), 0);
373 if (returnInt == NULL) {
374 ret = HCF_ERR_MALLOC;
375 break;
376 }
377 ret = key->getAsyKeySpecInt == NULL ? HCF_INVALID_PARAMS :
378 key->getAsyKeySpecInt((HcfPubKey *)key, (AsyKeySpecItem)item, returnInt);
379 if (ret != HCF_SUCCESS) {
380 HCF_FREE_PTR(returnInt);
381 break;
382 }
383 value->data = (uint8_t *)returnInt;
384 value->len = sizeof(int32_t);
385 ReverseUint8Arr(value->data, value->len);
386 break;
387 case CRYPTO_ECC_FIELD_TYPE_STR:
388 case CRYPTO_ECC_CURVE_NAME_STR:
389 ret = key->getAsyKeySpecString == NULL ? HCF_INVALID_PARAMS :
390 key->getAsyKeySpecString((HcfPubKey *)key, (AsyKeySpecItem)item, &returnStr);
391 if (ret != HCF_SUCCESS) {
392 break;
393 }
394 value->data = (uint8_t *)returnStr;
395 value->len = strlen(returnStr);
396 break;
397 default:
398 ret = key->getAsyKeySpecBigInteger == NULL ? HCF_INVALID_PARAMS :
399 key->getAsyKeySpecBigInteger((HcfPubKey *)key, (AsyKeySpecItem)item, &bigIntValue);
400 if (ret != HCF_SUCCESS) {
401 break;
402 }
403 value->data = (uint8_t *)bigIntValue.data;
404 value->len = (size_t)bigIntValue.len;
405 ReverseUint8Arr(value->data, value->len);
406 break;
407 }
408 return GetOhCryptoErrCode(ret);
409 }
410
OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncodingParams ** ctx)411 OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncodingParams **ctx)
412 {
413 if (ctx == NULL) {
414 return CRYPTO_PARAMETER_CHECK_FAILED;
415 }
416 *ctx = (OH_CryptoPrivKeyEncodingParams *)HcfMalloc(sizeof(OH_CryptoPrivKeyEncodingParams), 0);
417 if (*ctx == NULL) {
418 return CRYPTO_MEMORY_ERROR;
419 }
420 return CRYPTO_SUCCESS;
421 }
422
OH_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams * ctx,CryptoPrivKeyEncoding_ParamType type,Crypto_DataBlob * value)423 OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams *ctx,
424 CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value)
425 {
426 if ((ctx == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) {
427 return CRYPTO_PARAMETER_CHECK_FAILED;
428 }
429 char *data = (char *)HcfMalloc(value->len + 1, 0);
430 if (data == NULL) {
431 return CRYPTO_MEMORY_ERROR;
432 }
433 (void)memcpy_s(data, value->len, value->data, value->len);
434 switch (type) {
435 case CRYPTO_PRIVATE_KEY_ENCODING_PASSWORD_STR:
436 HcfFree(ctx->password);
437 ctx->password = data;
438 break;
439 case CRYPTO_PRIVATE_KEY_ENCODING_SYMMETRIC_CIPHER_STR:
440 HcfFree(ctx->cipher);
441 ctx->cipher = data;
442 break;
443 default:
444 HcfFree(data);
445 data = NULL;
446 return CRYPTO_PARAMETER_CHECK_FAILED;
447 }
448 return CRYPTO_SUCCESS;
449 }
450
OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams * ctx)451 void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx)
452 {
453 if (ctx == NULL) {
454 return;
455 }
456 HcfFree(ctx->password);
457 ctx->password = NULL;
458 HcfFree(ctx->cipher);
459 ctx->cipher = NULL;
460 HcfFree(ctx);
461 }
462
OH_CryptoPrivKey_Encode(OH_CryptoPrivKey * key,Crypto_EncodingType type,const char * encodingStandard,OH_CryptoPrivKeyEncodingParams * params,Crypto_DataBlob * out)463 OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type,
464 const char *encodingStandard, OH_CryptoPrivKeyEncodingParams *params, Crypto_DataBlob *out)
465 {
466 if ((key == NULL) || (out == NULL)) {
467 return CRYPTO_PARAMETER_CHECK_FAILED;
468 }
469 HcfResult ret = HCF_SUCCESS;
470 char *pemStr = NULL;
471 switch (type) {
472 case CRYPTO_PEM:
473 if (key->getEncodedPem == NULL) {
474 return CRYPTO_PARAMETER_CHECK_FAILED;
475 }
476 ret = key->getEncodedPem((HcfPriKey *)key, (HcfParamsSpec *)params, encodingStandard, &pemStr);
477 if (ret != HCF_SUCCESS) {
478 break;
479 }
480 out->data = (uint8_t *)pemStr;
481 out->len = strlen(pemStr);
482 break;
483 case CRYPTO_DER:
484 if (encodingStandard != NULL) {
485 ret = key->getEncodedDer == NULL ? HCF_INVALID_PARAMS :
486 key->getEncodedDer((HcfPriKey *)key, encodingStandard, (HcfBlob *)out);
487 break;
488 } else {
489 ret = key->base.getEncoded == NULL ? HCF_INVALID_PARAMS
490 : key->base.getEncoded((HcfKey *)key, (HcfBlob *)out);
491 break;
492 }
493 default:
494 return CRYPTO_PARAMETER_CHECK_FAILED;
495 }
496
497 return GetOhCryptoErrCodeNew(ret);
498 }
499
OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey * key,CryptoAsymKey_ParamType item,Crypto_DataBlob * value)500 OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item,
501 Crypto_DataBlob *value)
502 {
503 if ((key == NULL) || (value == NULL)) {
504 return CRYPTO_PARAMETER_CHECK_FAILED;
505 }
506 HcfResult ret = HCF_SUCCESS;
507 int32_t *returnInt = NULL;
508 char *returnStr = NULL;
509 HcfBigInteger bigIntValue = {0};
510 switch (item) {
511 case CRYPTO_DH_L_INT:
512 case CRYPTO_ECC_H_INT:
513 case CRYPTO_ECC_FIELD_SIZE_INT:
514 returnInt = (int32_t *)HcfMalloc(sizeof(int32_t), 0);
515 if (returnInt == NULL) {
516 ret = HCF_ERR_MALLOC;
517 break;
518 }
519 ret = key->getAsyKeySpecInt == NULL ? HCF_INVALID_PARAMS :
520 key->getAsyKeySpecInt((HcfPriKey *)key, (AsyKeySpecItem)item, returnInt);
521 if (ret != HCF_SUCCESS) {
522 HCF_FREE_PTR(returnInt);
523 break;
524 }
525 value->data = (uint8_t *)returnInt;
526 value->len = sizeof(int32_t);
527 ReverseUint8Arr(value->data, value->len);
528 break;
529 case CRYPTO_ECC_FIELD_TYPE_STR:
530 case CRYPTO_ECC_CURVE_NAME_STR:
531 ret = key->getAsyKeySpecString == NULL ? HCF_INVALID_PARAMS :
532 key->getAsyKeySpecString((HcfPriKey *)key, (AsyKeySpecItem)item, &returnStr);
533 if (ret != HCF_SUCCESS) {
534 break;
535 }
536 value->data = (uint8_t *)returnStr;
537 value->len = strlen(returnStr);
538 break;
539 default:
540 ret = key->getAsyKeySpecBigInteger == NULL ? HCF_INVALID_PARAMS :
541 key->getAsyKeySpecBigInteger((HcfPriKey *)key, (AsyKeySpecItem)item, &bigIntValue);
542 if (ret != HCF_SUCCESS) {
543 break;
544 }
545 value->data = (uint8_t *)bigIntValue.data;
546 value->len = (size_t)bigIntValue.len;
547 ReverseUint8Arr(value->data, value->len);
548 break;
549 }
550 return GetOhCryptoErrCodeNew(ret);
551 }
552
OH_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char * curveName,OH_CryptoAsymKeySpec ** spec)553 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec)
554 {
555 if ((curveName == NULL) || (spec == NULL)) {
556 return CRYPTO_PARAMETER_CHECK_FAILED;
557 }
558
559 HcfResult ret = HcfEccKeyUtilCreate(curveName, (HcfEccCommParamsSpec **)spec);
560 return GetOhCryptoErrCodeNew(ret);
561 }
562
OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen,int skLen,OH_CryptoAsymKeySpec ** spec)563 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen, OH_CryptoAsymKeySpec **spec)
564 {
565 if (spec == NULL) {
566 return CRYPTO_PARAMETER_CHECK_FAILED;
567 }
568 HcfResult ret = HcfDhKeyUtilCreate(pLen, skLen, (HcfDhCommParamsSpec **)spec);
569 return GetOhCryptoErrCodeNew(ret);
570 }
571
572 typedef struct {
573 CryptoAsymKeySpec_Type type;
574 uint32_t memSize;
575 } OH_CryptoAsymKeySpecInfo;
576
577 typedef struct {
578 HcfAlgValue algo;
579 OH_CryptoAsymKeySpecInfo *specInfo;
580 uint32_t specInfoSize;
581 } OH_CryptoAsymKeySpecInfoMap;
582
583 static OH_CryptoAsymKeySpecInfo g_rsaSpecInfo[] = {
584 {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfRsaPubKeyParamsSpec)},
585 {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfRsaKeyPairParamsSpec)},
586 };
587
588 static OH_CryptoAsymKeySpecInfo g_dsaSpecInfo[] = {
589 {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfDsaCommParamsSpec)},
590 {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfDsaPubKeyParamsSpec)},
591 {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfDsaKeyPairParamsSpec)},
592 };
593
594 static OH_CryptoAsymKeySpecInfo g_eccSpecInfo[] = {
595 {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfEccCommParamsSpec)},
596 {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfEccPriKeyParamsSpec)},
597 {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfEccPubKeyParamsSpec)},
598 {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfEccKeyPairParamsSpec)},
599 };
600
601 static OH_CryptoAsymKeySpecInfo g_dhSpecInfo[] = {
602 {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfDhCommParamsSpec)},
603 {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfDhPriKeyParamsSpec)},
604 {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfDhPubKeyParamsSpec)},
605 {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfDhKeyPairParamsSpec)},
606 };
607
608 static OH_CryptoAsymKeySpecInfo g_alg25519SpecInfo[] = {
609 {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfAlg25519PriKeyParamsSpec)},
610 {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfAlg25519PubKeyParamsSpec)},
611 {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfAlg25519KeyPairParamsSpec)},
612 };
613
614 static OH_CryptoAsymKeySpecInfoMap g_asymKeySpecInfoMap[] = {
615 {HCF_ALG_RSA, g_rsaSpecInfo, sizeof(g_rsaSpecInfo) / sizeof(g_rsaSpecInfo[0])},
616 {HCF_ALG_DSA, g_dsaSpecInfo, sizeof(g_dsaSpecInfo) / sizeof(g_dsaSpecInfo[0])},
617 {HCF_ALG_SM2, g_eccSpecInfo, sizeof(g_eccSpecInfo) / sizeof(g_eccSpecInfo[0])},
618 {HCF_ALG_ECC, g_eccSpecInfo, sizeof(g_eccSpecInfo) / sizeof(g_eccSpecInfo[0])},
619 {HCF_ALG_DH, g_dhSpecInfo, sizeof(g_dhSpecInfo) / sizeof(g_dhSpecInfo[0])},
620 {HCF_ALG_ED25519, g_alg25519SpecInfo, sizeof(g_alg25519SpecInfo) / sizeof(g_alg25519SpecInfo[0])},
621 {HCF_ALG_X25519, g_alg25519SpecInfo, sizeof(g_alg25519SpecInfo) / sizeof(g_alg25519SpecInfo[0])},
622 };
623
CreateAsymKeySpec(const char * algoName,CryptoAsymKeySpec_Type type,uint32_t memSize,OH_CryptoAsymKeySpec ** spec)624 static OH_Crypto_ErrCode CreateAsymKeySpec(const char *algoName, CryptoAsymKeySpec_Type type, uint32_t memSize,
625 OH_CryptoAsymKeySpec **spec)
626 {
627 OH_CryptoAsymKeySpec *tmpSpec = (OH_CryptoAsymKeySpec *)HcfMalloc(memSize, 0);
628 if (tmpSpec == NULL) {
629 return CRYPTO_MEMORY_ERROR;
630 }
631 char *algName = (char *)HcfMalloc(strlen(algoName) + 1, 0);
632 if (algName == NULL) {
633 HcfFree(tmpSpec);
634 tmpSpec = NULL;
635 return CRYPTO_MEMORY_ERROR;
636 }
637 (void)memcpy_s(algName, strlen(algoName), algoName, strlen(algoName));
638 tmpSpec->specType = (HcfAsyKeySpecType)type;
639 tmpSpec->algName = algName;
640 *spec = tmpSpec;
641 return CRYPTO_SUCCESS;
642 }
643
FindAsymKeySpecInfoMapByAlgoName(const char * algoName)644 static const OH_CryptoAsymKeySpecInfoMap *FindAsymKeySpecInfoMapByAlgoName(const char *algoName)
645 {
646 HcfAsyKeyGenParams params = { 0 };
647 HcfResult ret = ParseAlgNameToParams(algoName, ¶ms);
648 if (ret != HCF_SUCCESS) {
649 return NULL;
650 }
651 for (uint32_t i = 0; i < (sizeof(g_asymKeySpecInfoMap) / sizeof(OH_CryptoAsymKeySpecInfoMap)); ++i) {
652 if (g_asymKeySpecInfoMap[i].algo == params.algo) {
653 return &g_asymKeySpecInfoMap[i];
654 }
655 }
656 return NULL;
657 }
658
OH_CryptoAsymKeySpec_Create(const char * algoName,CryptoAsymKeySpec_Type type,OH_CryptoAsymKeySpec ** spec)659 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type,
660 OH_CryptoAsymKeySpec **spec)
661 {
662 if ((algoName == NULL) || (spec == NULL)) {
663 return CRYPTO_PARAMETER_CHECK_FAILED;
664 }
665 const OH_CryptoAsymKeySpecInfoMap *infoMap = FindAsymKeySpecInfoMapByAlgoName(algoName);
666 if (infoMap == NULL) {
667 return CRYPTO_PARAMETER_CHECK_FAILED;
668 }
669 for (uint32_t i = 0; i < infoMap->specInfoSize; ++i) {
670 if (infoMap->specInfo[i].type == type) {
671 return CreateAsymKeySpec(algoName, type, infoMap->specInfo[i].memSize, spec);
672 }
673 }
674 return CRYPTO_PARAMETER_CHECK_FAILED;
675 }
676
SetDataBlob(uint8_t ** dest,uint32_t * destLen,Crypto_DataBlob * value)677 static OH_Crypto_ErrCode SetDataBlob(uint8_t **dest, uint32_t *destLen, Crypto_DataBlob *value)
678 {
679 if (value == NULL || value->data == NULL || value->len == 0) {
680 return CRYPTO_PARAMETER_CHECK_FAILED;
681 }
682 uint8_t *tmp = (uint8_t *)HcfMalloc(value->len, 0);
683 if (tmp == NULL) {
684 return CRYPTO_MEMORY_ERROR;
685 }
686 (void)memcpy_s(tmp, value->len, value->data, value->len);
687 HcfFree(*dest);
688 *dest = NULL;
689 *dest = tmp;
690 *destLen = value->len;
691 ReverseUint8Arr(*dest, *destLen);
692 return CRYPTO_SUCCESS;
693 }
694
GetDataBlob(const uint8_t * src,uint32_t srcLen,Crypto_DataBlob * value)695 static OH_Crypto_ErrCode GetDataBlob(const uint8_t *src, uint32_t srcLen, Crypto_DataBlob *value)
696 {
697 if (src == NULL || srcLen == 0) {
698 return CRYPTO_PARAMETER_CHECK_FAILED;
699 }
700 value->data = (uint8_t *)HcfMalloc(srcLen, 0);
701 if (value->data == NULL) {
702 return CRYPTO_MEMORY_ERROR;
703 }
704 (void)memcpy_s(value->data, srcLen, src, srcLen);
705 value->len = srcLen;
706 ReverseUint8Arr(value->data, value->len);
707 return CRYPTO_SUCCESS;
708 }
709
SetDsaCommSpec(HcfDsaCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)710 static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type,
711 Crypto_DataBlob *value)
712 {
713 switch (type) {
714 case CRYPTO_DSA_P_DATABLOB:
715 return SetDataBlob(&(spec->p.data), &(spec->p.len), value);
716 case CRYPTO_DSA_Q_DATABLOB:
717 return SetDataBlob(&(spec->q.data), &(spec->q.len), value);
718 case CRYPTO_DSA_G_DATABLOB:
719 return SetDataBlob(&(spec->g.data), &(spec->g.len), value);
720 default:
721 return CRYPTO_PARAMETER_CHECK_FAILED;
722 }
723 }
724
SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)725 static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
726 Crypto_DataBlob *value)
727 {
728 switch (type) {
729 case CRYPTO_DSA_PK_DATABLOB:
730 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
731 default:
732 return CRYPTO_PARAMETER_CHECK_FAILED;
733 }
734 }
735
SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)736 static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
737 Crypto_DataBlob *value)
738 {
739 switch (type) {
740 case CRYPTO_DSA_SK_DATABLOB:
741 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
742 case CRYPTO_DSA_PK_DATABLOB:
743 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
744 default:
745 return CRYPTO_PARAMETER_CHECK_FAILED;
746 }
747 }
748
SetDsaSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)749 static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
750 {
751 if (SetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
752 return CRYPTO_SUCCESS;
753 }
754 switch (spec->specType) {
755 case HCF_PUBLIC_KEY_SPEC:
756 return SetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value);
757 case HCF_KEY_PAIR_SPEC:
758 return SetDsaKeyPairSpec((HcfDsaKeyPairParamsSpec *)spec, type, value);
759 default:
760 return CRYPTO_PARAMETER_CHECK_FAILED;
761 }
762 }
763
SetRsaCommSpec(HcfRsaCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)764 static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type,
765 Crypto_DataBlob *value)
766 {
767 switch (type) {
768 case CRYPTO_RSA_N_DATABLOB:
769 return SetDataBlob(&(spec->n.data), &(spec->n.len), value);
770 default:
771 return CRYPTO_PARAMETER_CHECK_FAILED;
772 }
773 }
774
SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)775 static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
776 Crypto_DataBlob *value)
777 {
778 switch (type) {
779 case CRYPTO_RSA_E_DATABLOB:
780 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
781 default:
782 return CRYPTO_PARAMETER_CHECK_FAILED;
783 }
784 }
785
SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)786 static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
787 Crypto_DataBlob *value)
788 {
789 switch (type) {
790 case CRYPTO_RSA_D_DATABLOB:
791 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
792 case CRYPTO_RSA_E_DATABLOB:
793 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
794 default:
795 return CRYPTO_PARAMETER_CHECK_FAILED;
796 }
797 }
798
SetRsaSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)799 static OH_Crypto_ErrCode SetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
800 {
801 if (SetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
802 return CRYPTO_SUCCESS;
803 }
804 switch (spec->specType) {
805 case HCF_PUBLIC_KEY_SPEC:
806 return SetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value);
807 case HCF_KEY_PAIR_SPEC:
808 return SetRsaKeyPairSpec((HcfRsaKeyPairParamsSpec *)spec, type, value);
809 default:
810 return CRYPTO_PARAMETER_CHECK_FAILED;
811 }
812 }
813
SetEccField(HcfEccCommParamsSpec * spec,Crypto_DataBlob * value)814 static OH_Crypto_ErrCode SetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob *value)
815 {
816 HcfECFieldFp *field = (HcfECFieldFp *)HcfMalloc(sizeof(HcfECFieldFp), 0);
817 if (field == NULL) {
818 return CRYPTO_MEMORY_ERROR;
819 }
820 char *fieldType = "Fp";
821 size_t fieldTypeLen = strlen(fieldType);
822 field->base.fieldType = (char *)HcfMalloc(fieldTypeLen + 1, 0);
823 if (field->base.fieldType == NULL) {
824 HcfFree(field);
825 field = NULL;
826 return CRYPTO_MEMORY_ERROR;
827 }
828 (void)memcpy_s(field->base.fieldType, fieldTypeLen, fieldType, fieldTypeLen);
829 field->p.data = (uint8_t *)HcfMalloc(value->len, 0);
830 if (field->p.data == NULL) {
831 HcfFree(field->base.fieldType);
832 field->base.fieldType = NULL;
833 HcfFree(field);
834 field = NULL;
835 return CRYPTO_MEMORY_ERROR;
836 }
837 (void)memcpy_s(field->p.data, value->len, value->data, value->len);
838 field->p.len = value->len;
839 ReverseUint8Arr(field->p.data, field->p.len);
840 spec->field = (HcfECField *)field;
841 return CRYPTO_SUCCESS;
842 }
843
SetEccCommSpec(HcfEccCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)844 static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type,
845 Crypto_DataBlob *value)
846 {
847 switch (type) {
848 case CRYPTO_ECC_FP_P_DATABLOB:
849 return SetEccField(spec, value);
850 case CRYPTO_ECC_A_DATABLOB:
851 return SetDataBlob(&(spec->a.data), &(spec->a.len), value);
852 case CRYPTO_ECC_B_DATABLOB:
853 return SetDataBlob(&(spec->b.data), &(spec->b.len), value);
854 case CRYPTO_ECC_G_X_DATABLOB:
855 return SetDataBlob(&(spec->g.x.data), &(spec->g.x.len), value);
856 case CRYPTO_ECC_G_Y_DATABLOB:
857 return SetDataBlob(&(spec->g.y.data), &(spec->g.y.len), value);
858 case CRYPTO_ECC_N_DATABLOB:
859 return SetDataBlob(&(spec->n.data), &(spec->n.len), value);
860 case CRYPTO_ECC_H_INT:
861 if (value->len != sizeof(spec->h)) {
862 return CRYPTO_PARAMETER_CHECK_FAILED;
863 }
864 uint32_t tmp = BigEndianArrToUint32(value->data, value->len);
865 if (tmp > INT32_MAX) {
866 return CRYPTO_PARAMETER_CHECK_FAILED;
867 }
868 spec->h = (int32_t)tmp;
869 break;
870 default:
871 return CRYPTO_PARAMETER_CHECK_FAILED;
872 }
873 return CRYPTO_SUCCESS;
874 }
875
SetEccPriSpec(HcfEccPriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)876 static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
877 Crypto_DataBlob *value)
878 {
879 switch (type) {
880 case CRYPTO_ECC_SK_DATABLOB:
881 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
882 default:
883 return CRYPTO_PARAMETER_CHECK_FAILED;
884 }
885 }
886
SetEccPubKeySpec(HcfEccPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)887 static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
888 Crypto_DataBlob *value)
889 {
890 switch (type) {
891 case CRYPTO_ECC_PK_X_DATABLOB:
892 return SetDataBlob(&(spec->pk.x.data), &(spec->pk.x.len), value);
893 case CRYPTO_ECC_PK_Y_DATABLOB:
894 return SetDataBlob(&(spec->pk.y.data), &(spec->pk.y.len), value);
895 default:
896 return CRYPTO_PARAMETER_CHECK_FAILED;
897 }
898 }
899
SetEccKeyPairSpec(HcfEccKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)900 static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
901 Crypto_DataBlob *value)
902 {
903 switch (type) {
904 case CRYPTO_ECC_SK_DATABLOB:
905 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
906 case CRYPTO_ECC_PK_X_DATABLOB:
907 return SetDataBlob(&(spec->pk.x.data), &(spec->pk.x.len), value);
908 case CRYPTO_ECC_PK_Y_DATABLOB:
909 return SetDataBlob(&(spec->pk.y.data), &(spec->pk.y.len), value);
910 default:
911 return CRYPTO_PARAMETER_CHECK_FAILED;
912 }
913 }
914
SetEccSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)915 static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
916 {
917 if (SetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
918 return CRYPTO_SUCCESS;
919 }
920 switch (spec->specType) {
921 case HCF_PRIVATE_KEY_SPEC:
922 return SetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value);
923 case HCF_PUBLIC_KEY_SPEC:
924 return SetEccPubKeySpec((HcfEccPubKeyParamsSpec *)spec, type, value);
925 case HCF_KEY_PAIR_SPEC:
926 return SetEccKeyPairSpec((HcfEccKeyPairParamsSpec *)spec, type, value);
927 default:
928 return CRYPTO_PARAMETER_CHECK_FAILED;
929 }
930 }
931
SetDhCommSpec(HcfDhCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)932 static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
933 {
934 switch (type) {
935 case CRYPTO_DH_P_DATABLOB:
936 return SetDataBlob(&(spec->p.data), &(spec->p.len), value);
937 case CRYPTO_DH_G_DATABLOB:
938 return SetDataBlob(&(spec->g.data), &(spec->g.len), value);
939 case CRYPTO_DH_L_INT:
940 if (value->len != sizeof(spec->length)) {
941 return CRYPTO_PARAMETER_CHECK_FAILED;
942 }
943 uint32_t tmp = BigEndianArrToUint32(value->data, value->len);
944 if (tmp > INT32_MAX) {
945 return CRYPTO_PARAMETER_CHECK_FAILED;
946 }
947 spec->length = (int)tmp;
948 break;
949 default:
950 return CRYPTO_PARAMETER_CHECK_FAILED;
951 }
952 return CRYPTO_SUCCESS;
953 }
954
SetDhPriSpec(HcfDhPriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)955 static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
956 {
957 switch (type) {
958 case CRYPTO_DH_SK_DATABLOB:
959 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
960 default:
961 return CRYPTO_PARAMETER_CHECK_FAILED;
962 }
963 }
964
SetDhPubKeySpec(HcfDhPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)965 static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
966 Crypto_DataBlob *value)
967 {
968 switch (type) {
969 case CRYPTO_DH_PK_DATABLOB:
970 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
971 default:
972 return CRYPTO_PARAMETER_CHECK_FAILED;
973 }
974 }
975
SetDhKeyPairSpec(HcfDhKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)976 static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
977 Crypto_DataBlob *value)
978 {
979 switch (type) {
980 case CRYPTO_DH_SK_DATABLOB:
981 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
982 case CRYPTO_DH_PK_DATABLOB:
983 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
984 default:
985 return CRYPTO_PARAMETER_CHECK_FAILED;
986 }
987 }
988
SetDhSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)989 static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
990 {
991 if (SetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
992 return CRYPTO_SUCCESS;
993 }
994 switch (spec->specType) {
995 case HCF_PRIVATE_KEY_SPEC:
996 return SetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value);
997 case HCF_PUBLIC_KEY_SPEC:
998 return SetDhPubKeySpec((HcfDhPubKeyParamsSpec *)spec, type, value);
999 case HCF_KEY_PAIR_SPEC:
1000 return SetDhKeyPairSpec((HcfDhKeyPairParamsSpec *)spec, type, value);
1001 default:
1002 return CRYPTO_PARAMETER_CHECK_FAILED;
1003 }
1004 }
1005
SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1006 static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1007 Crypto_DataBlob *value)
1008 {
1009 switch (type) {
1010 case CRYPTO_ED25519_SK_DATABLOB:
1011 case CRYPTO_X25519_SK_DATABLOB:
1012 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
1013 default:
1014 return CRYPTO_PARAMETER_CHECK_FAILED;
1015 }
1016 }
1017
SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1018 static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1019 Crypto_DataBlob *value)
1020 {
1021 switch (type) {
1022 case CRYPTO_ED25519_PK_DATABLOB:
1023 case CRYPTO_X25519_PK_DATABLOB:
1024 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
1025 default:
1026 return CRYPTO_PARAMETER_CHECK_FAILED;
1027 }
1028 }
1029
SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1030 static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1031 Crypto_DataBlob *value)
1032 {
1033 switch (type) {
1034 case CRYPTO_ED25519_SK_DATABLOB:
1035 case CRYPTO_X25519_SK_DATABLOB:
1036 return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value);
1037 case CRYPTO_ED25519_PK_DATABLOB:
1038 case CRYPTO_X25519_PK_DATABLOB:
1039 return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value);
1040 default:
1041 return CRYPTO_PARAMETER_CHECK_FAILED;
1042 }
1043 }
1044
SetAlg25519Spec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1045 static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type,
1046 Crypto_DataBlob *value)
1047 {
1048 switch (spec->specType) {
1049 case HCF_PRIVATE_KEY_SPEC:
1050 return SetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value);
1051 case HCF_PUBLIC_KEY_SPEC:
1052 return SetAlg25519PubKeySpec((HcfAlg25519PubKeyParamsSpec *)spec, type, value);
1053 case HCF_KEY_PAIR_SPEC:
1054 return SetAlg25519KeyPairSpec((HcfAlg25519KeyPairParamsSpec *)spec, type, value);
1055 default:
1056 return CRYPTO_PARAMETER_CHECK_FAILED;
1057 }
1058 }
1059
OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1060 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type,
1061 Crypto_DataBlob *value)
1062 {
1063 if ((spec == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) {
1064 return CRYPTO_PARAMETER_CHECK_FAILED;
1065 }
1066
1067 HcfAsyKeyGenParams params = { 0 };
1068 HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms);
1069 if (ret != HCF_SUCCESS) {
1070 return GetOhCryptoErrCodeNew(ret);
1071 }
1072
1073 switch (params.algo) {
1074 case HCF_ALG_DSA:
1075 return SetDsaSpec(spec, type, value);
1076 case HCF_ALG_RSA:
1077 return SetRsaSpec(spec, type, value);
1078 case HCF_ALG_ECC:
1079 case HCF_ALG_SM2:
1080 return SetEccSpec(spec, type, value);
1081 case HCF_ALG_DH:
1082 return SetDhSpec(spec, type, value);
1083 case HCF_ALG_ED25519:
1084 case HCF_ALG_X25519:
1085 return SetAlg25519Spec(spec, type, value);
1086 default:
1087 return CRYPTO_PARAMETER_CHECK_FAILED;
1088 }
1089 }
1090
SetDsaCommonSpec(HcfDsaCommParamsSpec * commonParamsSpec,HcfDsaCommParamsSpec * spec)1091 static OH_Crypto_ErrCode SetDsaCommonSpec(HcfDsaCommParamsSpec *commonParamsSpec, HcfDsaCommParamsSpec *spec)
1092 {
1093 spec->p.data = (unsigned char *)HcfMalloc(commonParamsSpec->p.len, 0);
1094 if (spec->p.data == NULL) {
1095 FreeDsaCommParamsSpec(spec);
1096 return CRYPTO_MEMORY_ERROR;
1097 }
1098 spec->q.data = (unsigned char *)HcfMalloc(commonParamsSpec->q.len, 0);
1099 if (spec->q.data == NULL) {
1100 FreeDsaCommParamsSpec(spec);
1101 return CRYPTO_MEMORY_ERROR;
1102 }
1103 spec->g.data = (unsigned char *)HcfMalloc(commonParamsSpec->g.len, 0);
1104 if (spec->g.data == NULL) {
1105 FreeDsaCommParamsSpec(spec);
1106 return CRYPTO_MEMORY_ERROR;
1107 }
1108 (void)memcpy_s(spec->p.data, commonParamsSpec->p.len, commonParamsSpec->p.data, commonParamsSpec->p.len);
1109 (void)memcpy_s(spec->q.data, commonParamsSpec->q.len, commonParamsSpec->q.data, commonParamsSpec->q.len);
1110 (void)memcpy_s(spec->g.data, commonParamsSpec->g.len, commonParamsSpec->g.data, commonParamsSpec->g.len);
1111 spec->p.len = commonParamsSpec->p.len;
1112 spec->q.len = commonParamsSpec->q.len;
1113 spec->g.len = commonParamsSpec->g.len;
1114 return CRYPTO_SUCCESS;
1115 }
1116
SetEccCommonSpec(HcfEccCommParamsSpec * commonParamsSpec,HcfEccCommParamsSpec * spec)1117 static OH_Crypto_ErrCode SetEccCommonSpec(HcfEccCommParamsSpec *commonParamsSpec, HcfEccCommParamsSpec *spec)
1118 {
1119 HcfEccCommParamsSpec eccCommParamsSpec = {};
1120 HcfResult ret = CopyEccCommonSpec(commonParamsSpec, &eccCommParamsSpec);
1121 if (ret != HCF_SUCCESS) {
1122 return GetOhCryptoErrCodeNew(ret);
1123 }
1124 spec->field = eccCommParamsSpec.field;
1125 spec->field->fieldType = eccCommParamsSpec.field->fieldType;
1126 ((HcfECFieldFp *)(spec->field))->p.data = ((HcfECFieldFp *)(eccCommParamsSpec.field))->p.data;
1127 ((HcfECFieldFp *)(spec->field))->p.len = ((HcfECFieldFp *)(eccCommParamsSpec.field))->p.len;
1128 spec->a.data = eccCommParamsSpec.a.data;
1129 spec->a.len = eccCommParamsSpec.a.len;
1130 spec->b.data = eccCommParamsSpec.b.data;
1131 spec->b.len = eccCommParamsSpec.b.len;
1132 spec->g.x.data = eccCommParamsSpec.g.x.data;
1133 spec->g.x.len = eccCommParamsSpec.g.x.len;
1134 spec->g.y.data = eccCommParamsSpec.g.y.data;
1135 spec->g.y.len = eccCommParamsSpec.g.y.len;
1136 spec->n.data = eccCommParamsSpec.n.data;
1137 spec->n.len = eccCommParamsSpec.n.len;
1138 spec->h = eccCommParamsSpec.h;
1139 HcfFree(eccCommParamsSpec.base.algName);
1140 eccCommParamsSpec.base.algName = NULL;
1141 return CRYPTO_SUCCESS;
1142 }
1143
SetDhCommonSpec(HcfDhCommParamsSpec * commonParamsSpec,HcfDhCommParamsSpec * spec)1144 static OH_Crypto_ErrCode SetDhCommonSpec(HcfDhCommParamsSpec *commonParamsSpec, HcfDhCommParamsSpec *spec)
1145 {
1146 HcfDhCommParamsSpec dhCommParamsSpec = {};
1147 HcfResult ret = CopyDhCommonSpec(commonParamsSpec, &dhCommParamsSpec);
1148 if (ret != HCF_SUCCESS) {
1149 return GetOhCryptoErrCodeNew(ret);
1150 }
1151 spec->p.data = dhCommParamsSpec.p.data;
1152 spec->p.len = dhCommParamsSpec.p.len;
1153 spec->g.data = dhCommParamsSpec.g.data;
1154 spec->g.len = dhCommParamsSpec.g.len;
1155 spec->length = dhCommParamsSpec.length;
1156 HcfFree(dhCommParamsSpec.base.algName);
1157 dhCommParamsSpec.base.algName = NULL;
1158 return CRYPTO_SUCCESS;
1159 }
1160
OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec * spec,OH_CryptoAsymKeySpec * commonParamsSpec)1161 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec *spec,
1162 OH_CryptoAsymKeySpec *commonParamsSpec)
1163 {
1164 if ((spec == NULL) || (commonParamsSpec == NULL) || (commonParamsSpec->specType != HCF_COMMON_PARAMS_SPEC)) {
1165 return CRYPTO_PARAMETER_CHECK_FAILED;
1166 }
1167 HcfAsyKeyGenParams params = { 0 };
1168 HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms);
1169 if (ret != HCF_SUCCESS) {
1170 return GetOhCryptoErrCodeNew(ret);
1171 }
1172
1173 switch (params.algo) {
1174 case HCF_ALG_DSA:
1175 return SetDsaCommonSpec((HcfDsaCommParamsSpec *)commonParamsSpec, (HcfDsaCommParamsSpec *)spec);
1176 case HCF_ALG_ECC:
1177 case HCF_ALG_SM2:
1178 return SetEccCommonSpec((HcfEccCommParamsSpec *)commonParamsSpec, (HcfEccCommParamsSpec *)spec);
1179 case HCF_ALG_DH:
1180 return SetDhCommonSpec((HcfDhCommParamsSpec *)commonParamsSpec, (HcfDhCommParamsSpec *)spec);
1181 default:
1182 return CRYPTO_PARAMETER_CHECK_FAILED;
1183 }
1184 }
1185
GetDsaCommSpec(HcfDsaCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1186 static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type,
1187 Crypto_DataBlob *value)
1188 {
1189 switch (type) {
1190 case CRYPTO_DSA_P_DATABLOB:
1191 return GetDataBlob(spec->p.data, spec->p.len, value);
1192 case CRYPTO_DSA_Q_DATABLOB:
1193 return GetDataBlob(spec->q.data, spec->q.len, value);
1194 case CRYPTO_DSA_G_DATABLOB:
1195 return GetDataBlob(spec->g.data, spec->g.len, value);
1196 default:
1197 return CRYPTO_PARAMETER_CHECK_FAILED;
1198 }
1199 }
1200
GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1201 static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1202 Crypto_DataBlob *value)
1203 {
1204 switch (type) {
1205 case CRYPTO_DSA_PK_DATABLOB:
1206 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1207 default:
1208 return CRYPTO_PARAMETER_CHECK_FAILED;
1209 }
1210 }
1211
GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1212 static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1213 Crypto_DataBlob *value)
1214 {
1215 switch (type) {
1216 case CRYPTO_DSA_SK_DATABLOB:
1217 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1218 case CRYPTO_DSA_PK_DATABLOB:
1219 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1220 default:
1221 return CRYPTO_PARAMETER_CHECK_FAILED;
1222 }
1223 }
1224
GetDsaSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1225 static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1226 {
1227 if (GetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
1228 return CRYPTO_SUCCESS;
1229 }
1230 switch (spec->specType) {
1231 case HCF_PUBLIC_KEY_SPEC:
1232 return GetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value);
1233 case HCF_KEY_PAIR_SPEC:
1234 return GetDsaKeyPairSpec((HcfDsaKeyPairParamsSpec *)spec, type, value);
1235 default:
1236 return CRYPTO_PARAMETER_CHECK_FAILED;
1237 }
1238 }
1239
GetRsaCommSpec(HcfRsaCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1240 static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type,
1241 Crypto_DataBlob *value)
1242 {
1243 switch (type) {
1244 case CRYPTO_RSA_N_DATABLOB:
1245 return GetDataBlob(spec->n.data, spec->n.len, value);
1246 default:
1247 return CRYPTO_PARAMETER_CHECK_FAILED;
1248 }
1249 }
1250
GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1251 static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1252 Crypto_DataBlob *value)
1253 {
1254 switch (type) {
1255 case CRYPTO_RSA_E_DATABLOB:
1256 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1257 default:
1258 return CRYPTO_PARAMETER_CHECK_FAILED;
1259 }
1260 }
1261
GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1262 static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1263 Crypto_DataBlob *value)
1264 {
1265 switch (type) {
1266 case CRYPTO_RSA_D_DATABLOB:
1267 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1268 case CRYPTO_RSA_E_DATABLOB:
1269 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1270 default:
1271 return CRYPTO_PARAMETER_CHECK_FAILED;
1272 }
1273 }
1274
GetRsaSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1275 static OH_Crypto_ErrCode GetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1276 {
1277 if (GetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
1278 return CRYPTO_SUCCESS;
1279 }
1280 switch (spec->specType) {
1281 case HCF_PUBLIC_KEY_SPEC:
1282 return GetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value);
1283 case HCF_KEY_PAIR_SPEC:
1284 return GetRsaKeyPairSpec((HcfRsaKeyPairParamsSpec *)spec, type, value);
1285 default:
1286 return CRYPTO_PARAMETER_CHECK_FAILED;
1287 }
1288 }
1289
GetEccField(HcfEccCommParamsSpec * spec,Crypto_DataBlob * value)1290 static OH_Crypto_ErrCode GetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob *value)
1291 {
1292 if ((spec->field == NULL) || (((HcfECFieldFp *)(spec->field))->p.data == NULL)) {
1293 return CRYPTO_PARAMETER_CHECK_FAILED;
1294 }
1295 return GetDataBlob(((HcfECFieldFp *)(spec->field))->p.data, ((HcfECFieldFp *)(spec->field))->p.len, value);
1296 }
1297
GetEccCommSpec(HcfEccCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1298 static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type,
1299 Crypto_DataBlob *value)
1300 {
1301 switch (type) {
1302 case CRYPTO_ECC_FP_P_DATABLOB:
1303 return GetEccField(spec, value);
1304 case CRYPTO_ECC_A_DATABLOB:
1305 return GetDataBlob(spec->a.data, spec->a.len, value);
1306 case CRYPTO_ECC_B_DATABLOB:
1307 return GetDataBlob(spec->b.data, spec->b.len, value);
1308 case CRYPTO_ECC_G_X_DATABLOB:
1309 return GetDataBlob(spec->g.x.data, spec->g.x.len, value);
1310 case CRYPTO_ECC_G_Y_DATABLOB:
1311 return GetDataBlob(spec->g.y.data, spec->g.y.len, value);
1312 case CRYPTO_ECC_N_DATABLOB:
1313 return GetDataBlob(spec->n.data, spec->n.len, value);
1314 case CRYPTO_ECC_H_INT:
1315 value->data = (uint8_t *)HcfMalloc(sizeof(spec->h), 0);
1316 if (value->data == NULL) {
1317 return CRYPTO_MEMORY_ERROR;
1318 }
1319 value->len = sizeof(spec->h);
1320 if (spec->h < 0) {
1321 HcfFree(value->data);
1322 value->data = NULL;
1323 return CRYPTO_PARAMETER_CHECK_FAILED;
1324 }
1325 uint32_t tmp = (uint32_t)spec->h;
1326 Uint32TobigEndianArr(tmp, value->data, value->len);
1327 break;
1328 default:
1329 return CRYPTO_PARAMETER_CHECK_FAILED;
1330 }
1331 return CRYPTO_SUCCESS;
1332 }
1333
GetEccPriSpec(HcfEccPriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1334 static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1335 Crypto_DataBlob *value)
1336 {
1337 switch (type) {
1338 case CRYPTO_ECC_SK_DATABLOB:
1339 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1340 default:
1341 return CRYPTO_PARAMETER_CHECK_FAILED;
1342 }
1343 }
1344
GetEccPubKeySpec(HcfEccPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1345 static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1346 Crypto_DataBlob *value)
1347 {
1348 switch (type) {
1349 case CRYPTO_ECC_PK_X_DATABLOB:
1350 return GetDataBlob(spec->pk.x.data, spec->pk.x.len, value);
1351 case CRYPTO_ECC_PK_Y_DATABLOB:
1352 return GetDataBlob(spec->pk.y.data, spec->pk.y.len, value);
1353 default:
1354 return CRYPTO_PARAMETER_CHECK_FAILED;
1355 }
1356 }
1357
GetEccKeyPairSpec(HcfEccKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1358 static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1359 Crypto_DataBlob *value)
1360 {
1361 switch (type) {
1362 case CRYPTO_ECC_SK_DATABLOB:
1363 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1364 case CRYPTO_ECC_PK_X_DATABLOB:
1365 return GetDataBlob(spec->pk.x.data, spec->pk.x.len, value);
1366 case CRYPTO_ECC_PK_Y_DATABLOB:
1367 return GetDataBlob(spec->pk.y.data, spec->pk.y.len, value);
1368 default:
1369 return CRYPTO_PARAMETER_CHECK_FAILED;
1370 }
1371 }
1372
GetEccSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1373 static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1374 {
1375 if (GetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
1376 return CRYPTO_SUCCESS;
1377 }
1378 switch (spec->specType) {
1379 case HCF_PRIVATE_KEY_SPEC:
1380 return GetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value);
1381 case HCF_PUBLIC_KEY_SPEC:
1382 return GetEccPubKeySpec((HcfEccPubKeyParamsSpec *)spec, type, value);
1383 case HCF_KEY_PAIR_SPEC:
1384 return GetEccKeyPairSpec((HcfEccKeyPairParamsSpec *)spec, type, value);
1385 default:
1386 return CRYPTO_PARAMETER_CHECK_FAILED;
1387 }
1388 }
1389
GetDhCommSpec(HcfDhCommParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1390 static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1391 {
1392 switch (type) {
1393 case CRYPTO_DH_P_DATABLOB:
1394 return GetDataBlob(spec->p.data, spec->p.len, value);
1395 case CRYPTO_DH_G_DATABLOB:
1396 return GetDataBlob(spec->g.data, spec->g.len, value);
1397 case CRYPTO_DH_L_INT:
1398 value->data = (uint8_t *)HcfMalloc(sizeof(spec->length), 0);
1399 if (value->data == NULL) {
1400 return CRYPTO_MEMORY_ERROR;
1401 }
1402 value->len = sizeof(spec->length);
1403 if (spec->length < 0) {
1404 HcfFree(value->data);
1405 value->data = NULL;
1406 return CRYPTO_PARAMETER_CHECK_FAILED;
1407 }
1408 uint32_t tmp = (uint32_t)spec->length;
1409 Uint32TobigEndianArr(tmp, value->data, value->len);
1410 break;
1411 default:
1412 return CRYPTO_PARAMETER_CHECK_FAILED;
1413 }
1414 return CRYPTO_SUCCESS;
1415 }
1416
GetDhPriSpec(HcfDhPriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1417 static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1418 {
1419 switch (type) {
1420 case CRYPTO_DH_SK_DATABLOB:
1421 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1422 default:
1423 return CRYPTO_PARAMETER_CHECK_FAILED;
1424 }
1425 }
1426
GetDhPubKeySpec(HcfDhPubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1427 static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1428 Crypto_DataBlob *value)
1429 {
1430 switch (type) {
1431 case CRYPTO_DH_PK_DATABLOB:
1432 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1433 default:
1434 return CRYPTO_PARAMETER_CHECK_FAILED;
1435 }
1436 }
1437
GetDhKeyPairSpec(HcfDhKeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1438 static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1439 Crypto_DataBlob *value)
1440 {
1441 switch (type) {
1442 case CRYPTO_DH_SK_DATABLOB:
1443 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1444 case CRYPTO_DH_PK_DATABLOB:
1445 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1446 default:
1447 return CRYPTO_PARAMETER_CHECK_FAILED;
1448 }
1449 }
1450
GetDhSpec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1451 static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value)
1452 {
1453 if (GetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) {
1454 return CRYPTO_SUCCESS;
1455 }
1456 switch (spec->specType) {
1457 case HCF_PRIVATE_KEY_SPEC:
1458 return GetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value);
1459 case HCF_PUBLIC_KEY_SPEC:
1460 return GetDhPubKeySpec((HcfDhPubKeyParamsSpec *)spec, type, value);
1461 case HCF_KEY_PAIR_SPEC:
1462 return GetDhKeyPairSpec((HcfDhKeyPairParamsSpec *)spec, type, value);
1463 default:
1464 return CRYPTO_PARAMETER_CHECK_FAILED;
1465 }
1466 }
1467
GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1468 static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1469 Crypto_DataBlob *value)
1470 {
1471 switch (type) {
1472 case CRYPTO_ED25519_SK_DATABLOB:
1473 case CRYPTO_X25519_SK_DATABLOB:
1474 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1475 default:
1476 return CRYPTO_PARAMETER_CHECK_FAILED;
1477 }
1478 }
1479
GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1480 static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type,
1481 Crypto_DataBlob *value)
1482 {
1483 switch (type) {
1484 case CRYPTO_ED25519_PK_DATABLOB:
1485 case CRYPTO_X25519_PK_DATABLOB:
1486 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1487 default:
1488 return CRYPTO_PARAMETER_CHECK_FAILED;
1489 }
1490 }
1491
GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1492 static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type,
1493 Crypto_DataBlob *value)
1494 {
1495 switch (type) {
1496 case CRYPTO_ED25519_SK_DATABLOB:
1497 case CRYPTO_X25519_SK_DATABLOB:
1498 return GetDataBlob(spec->sk.data, spec->sk.len, value);
1499 case CRYPTO_ED25519_PK_DATABLOB:
1500 case CRYPTO_X25519_PK_DATABLOB:
1501 return GetDataBlob(spec->pk.data, spec->pk.len, value);
1502 default:
1503 return CRYPTO_PARAMETER_CHECK_FAILED;
1504 }
1505 }
1506
GetAlg25519Spec(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1507 static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type,
1508 Crypto_DataBlob *value)
1509 {
1510 switch (spec->specType) {
1511 case HCF_PRIVATE_KEY_SPEC:
1512 return GetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value);
1513 case HCF_PUBLIC_KEY_SPEC:
1514 return GetAlg25519PubKeySpec((HcfAlg25519PubKeyParamsSpec *)spec, type, value);
1515 case HCF_KEY_PAIR_SPEC:
1516 return GetAlg25519KeyPairSpec((HcfAlg25519KeyPairParamsSpec *)spec, type, value);
1517 default:
1518 return CRYPTO_PARAMETER_CHECK_FAILED;
1519 }
1520 }
1521
OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec * spec,CryptoAsymKey_ParamType type,Crypto_DataBlob * value)1522 OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type,
1523 Crypto_DataBlob *value)
1524 {
1525 if ((spec == NULL) || (value == NULL)) {
1526 return CRYPTO_PARAMETER_CHECK_FAILED;
1527 }
1528
1529 HcfAsyKeyGenParams params = { 0 };
1530 HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms);
1531 if (ret != HCF_SUCCESS) {
1532 return GetOhCryptoErrCodeNew(ret);
1533 }
1534
1535 switch (params.algo) {
1536 case HCF_ALG_DSA:
1537 return GetDsaSpec(spec, type, value);
1538 case HCF_ALG_RSA:
1539 return GetRsaSpec(spec, type, value);
1540 case HCF_ALG_ECC:
1541 case HCF_ALG_SM2:
1542 return GetEccSpec(spec, type, value);
1543 case HCF_ALG_DH:
1544 return GetDhSpec(spec, type, value);
1545 case HCF_ALG_ED25519:
1546 case HCF_ALG_X25519:
1547 return GetAlg25519Spec(spec, type, value);
1548 default:
1549 return CRYPTO_PARAMETER_CHECK_FAILED;
1550 }
1551 }
1552
OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec * spec)1553 void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec)
1554 {
1555 if (spec == NULL) {
1556 return;
1557 }
1558 FreeAsyKeySpec((HcfAsyKeyParamsSpec *)spec);
1559 }
1560
OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec * keySpec,OH_CryptoAsymKeyGeneratorWithSpec ** generator)1561 OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec *keySpec,
1562 OH_CryptoAsymKeyGeneratorWithSpec **generator)
1563 {
1564 if ((keySpec == NULL) || (generator == NULL)) {
1565 return CRYPTO_PARAMETER_CHECK_FAILED;
1566 }
1567 *generator = (OH_CryptoAsymKeyGeneratorWithSpec *)HcfMalloc(sizeof(OH_CryptoAsymKeyGeneratorWithSpec), 0);
1568 if (*generator == NULL) {
1569 return CRYPTO_MEMORY_ERROR;
1570 }
1571 HcfResult ret = HcfAsyKeyGeneratorBySpecCreate((HcfAsyKeyParamsSpec *)keySpec, &((*generator)->generator));
1572 if (ret != HCF_SUCCESS) {
1573 HcfFree(*generator);
1574 *generator = NULL;
1575 return GetOhCryptoErrCodeNew(ret);
1576 }
1577 (*generator)->specType = keySpec->specType;
1578 return CRYPTO_SUCCESS;
1579 }
1580
GenPriKeyPair(HcfAsyKeyGeneratorBySpec * generator,OH_CryptoKeyPair ** keyPair)1581 static OH_Crypto_ErrCode GenPriKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair)
1582 {
1583 HcfPriKey *priKey = NULL;
1584 if (generator->generatePriKey == NULL) {
1585 return CRYPTO_NOT_SUPPORTED;
1586 }
1587 HcfResult ret = generator->generatePriKey(generator, &priKey);
1588 if (ret != HCF_SUCCESS) {
1589 return GetOhCryptoErrCodeNew(ret);
1590 }
1591 *keyPair = (OH_CryptoKeyPair *)HcfMalloc(sizeof(OH_CryptoKeyPair), 0);
1592 if (*keyPair == NULL) {
1593 HcfFree(priKey);
1594 priKey = NULL;
1595 return CRYPTO_MEMORY_ERROR;
1596 }
1597
1598 (*keyPair)->priKey = priKey;
1599 return CRYPTO_SUCCESS;
1600 }
1601
GenPubKeyPair(HcfAsyKeyGeneratorBySpec * generator,OH_CryptoKeyPair ** keyPair)1602 static OH_Crypto_ErrCode GenPubKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair)
1603 {
1604 HcfPubKey *pubKey = NULL;
1605 if (generator->generatePubKey == NULL) {
1606 return CRYPTO_NOT_SUPPORTED;
1607 }
1608 HcfResult ret = generator->generatePubKey(generator, &pubKey);
1609 if (ret != HCF_SUCCESS) {
1610 return GetOhCryptoErrCodeNew(ret);
1611 }
1612 *keyPair = (OH_CryptoKeyPair *)HcfMalloc(sizeof(OH_CryptoKeyPair), 0);
1613 if (*keyPair == NULL) {
1614 HcfFree(pubKey);
1615 pubKey = NULL;
1616 return CRYPTO_MEMORY_ERROR;
1617 }
1618 (*keyPair)->pubKey = pubKey;
1619 return CRYPTO_SUCCESS;
1620 }
1621
GenKeyPair(HcfAsyKeyGeneratorBySpec * generator,OH_CryptoKeyPair ** keyPair)1622 static OH_Crypto_ErrCode GenKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair)
1623 {
1624 if (generator->generateKeyPair == NULL) {
1625 return CRYPTO_NOT_SUPPORTED;
1626 }
1627 HcfResult ret = generator->generateKeyPair(generator, (HcfKeyPair **)keyPair);
1628 return GetOhCryptoErrCodeNew(ret);
1629 }
1630
OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec * generator,OH_CryptoKeyPair ** keyPair)1631 OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec *generator,
1632 OH_CryptoKeyPair **keyPair)
1633 {
1634 if ((generator == NULL) || (generator->generator == NULL) || (keyPair == NULL)) {
1635 return CRYPTO_PARAMETER_CHECK_FAILED;
1636 }
1637 switch (generator->specType) {
1638 case HCF_PRIVATE_KEY_SPEC:
1639 return GenPriKeyPair(generator->generator, keyPair);
1640 case HCF_PUBLIC_KEY_SPEC:
1641 return GenPubKeyPair(generator->generator, keyPair);
1642 case HCF_KEY_PAIR_SPEC:
1643 case HCF_COMMON_PARAMS_SPEC:
1644 return GenKeyPair(generator->generator, keyPair);
1645 default:
1646 return CRYPTO_PARAMETER_CHECK_FAILED;
1647 }
1648 }
1649
OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec * generator)1650 void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator)
1651 {
1652 if (generator == NULL) {
1653 return;
1654 }
1655 HcfObjDestroy(generator->generator);
1656 generator->generator = NULL;
1657 HcfFree(generator);
1658 }
1659
1660
OH_CryptoEcPoint_Create(const char * curveName,Crypto_DataBlob * ecKeyData,OH_CryptoEcPoint ** point)1661 OH_Crypto_ErrCode OH_CryptoEcPoint_Create(const char *curveName, Crypto_DataBlob *ecKeyData, OH_CryptoEcPoint **point)
1662 {
1663 if ((curveName == NULL) || (point == NULL)) {
1664 return CRYPTO_PARAMETER_CHECK_FAILED;
1665 }
1666 *point = (OH_CryptoEcPoint*)HcfMalloc(sizeof(OH_CryptoEcPoint), 0);
1667 if (*point == NULL) {
1668 return CRYPTO_MEMORY_ERROR;
1669 }
1670 (*point)->curveName = (char *)HcfMalloc(strlen(curveName) + 1, 0);
1671 if ((*point)->curveName == NULL) {
1672 HcfFree(*point);
1673 *point = NULL;
1674 return CRYPTO_MEMORY_ERROR;
1675 }
1676 (void)memcpy_s((*point)->curveName, strlen(curveName), curveName, strlen(curveName));
1677 if (ecKeyData == NULL) {
1678 return CRYPTO_SUCCESS;
1679 }
1680 HcfResult ret = HcfConvertPoint(curveName, (HcfBlob *)ecKeyData, &((*point)->pointBase));
1681 if (ret != HCF_SUCCESS) {
1682 OH_CryptoEcPoint_Destroy(*point);
1683 *point = NULL;
1684 }
1685 return GetOhCryptoErrCodeNew(ret);
1686 }
1687
OH_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint * point,Crypto_DataBlob * x,Crypto_DataBlob * y)1688 OH_Crypto_ErrCode OH_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y)
1689 {
1690 if ((point == NULL) || (x == NULL) || (y == NULL)) {
1691 return CRYPTO_PARAMETER_CHECK_FAILED;
1692 }
1693 HcfPoint dPoint = {};
1694 HcfResult ret = CopyPoint(&(point->pointBase), &dPoint);
1695 if (ret != HCF_SUCCESS) {
1696 return GetOhCryptoErrCodeNew(ret);
1697 }
1698 x->data = dPoint.x.data;
1699 y->data = dPoint.y.data;
1700 x->len = dPoint.x.len;
1701 y->len = dPoint.y.len;
1702 ReverseUint8Arr(x->data, x->len);
1703 ReverseUint8Arr(y->data, y->len);
1704 return CRYPTO_SUCCESS;
1705 }
1706
OH_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint * point,Crypto_DataBlob * x,Crypto_DataBlob * y)1707 OH_Crypto_ErrCode OH_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y)
1708 {
1709 if ((point == NULL) || (x == NULL) || (y == NULL)) {
1710 return CRYPTO_PARAMETER_CHECK_FAILED;
1711 }
1712 HcfPoint sPoint = {};
1713 sPoint.x.data = x->data;
1714 sPoint.x.len = x->len;
1715 sPoint.y.data = y->data;
1716 sPoint.y.len = y->len;
1717 HcfPoint dPoint = {};
1718 HcfResult ret = CopyPoint(&sPoint, &dPoint);
1719 if (ret != HCF_SUCCESS) {
1720 return GetOhCryptoErrCodeNew(ret);
1721 }
1722 HcfFree(point->pointBase.x.data);
1723 point->pointBase.x.data = NULL;
1724 HcfFree(point->pointBase.y.data);
1725 point->pointBase.y.data = NULL;
1726 point->pointBase.x.data = dPoint.x.data;
1727 point->pointBase.x.len = dPoint.x.len;
1728 point->pointBase.y.data = dPoint.y.data;
1729 point->pointBase.y.len = dPoint.y.len;
1730 ReverseUint8Arr(point->pointBase.x.data, point->pointBase.x.len);
1731 ReverseUint8Arr(point->pointBase.y.data, point->pointBase.y.len);
1732 return CRYPTO_SUCCESS;
1733 }
1734
OH_CryptoEcPoint_Encode(OH_CryptoEcPoint * point,const char * format,Crypto_DataBlob * out)1735 OH_Crypto_ErrCode OH_CryptoEcPoint_Encode(OH_CryptoEcPoint *point, const char *format, Crypto_DataBlob *out)
1736 {
1737 if ((point == NULL) || (format == NULL) || (out == NULL)) {
1738 return CRYPTO_PARAMETER_CHECK_FAILED;
1739 }
1740 HcfResult ret = HcfGetEncodedPoint(point->curveName, &(point->pointBase), format, (HcfBlob *)out);
1741 return GetOhCryptoErrCodeNew(ret);
1742 }
1743
OH_CryptoEcPoint_Destroy(OH_CryptoEcPoint * point)1744 void OH_CryptoEcPoint_Destroy(OH_CryptoEcPoint *point)
1745 {
1746 if (point == NULL) {
1747 return;
1748 }
1749 HcfFree(point->curveName);
1750 point->curveName = NULL;
1751 FreeEcPointMem(&(point->pointBase));
1752 HcfFree(point);
1753 }