• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "crypto_ffi.h"
16 
17 #define MAX_MEMORY_SIZE (5 * 1024 * 1024)
18 
19 using namespace OHOS::FFI;
20 
21 namespace OHOS {
22     namespace CryptoFramework {
23         extern "C" {
24             //-------------------random
FfiOHOSCreateRandom(int32_t * errCode)25             int64_t FfiOHOSCreateRandom(int32_t* errCode)
26             {
27                 LOGD("[Random] CreateRandom start");
28                 HcfRand *randObj = nullptr;
29                 HcfResult res = HcfRandCreate(&randObj);
30                 *errCode = static_cast<int32_t>(res);
31                 if (res != HCF_SUCCESS) {
32                     LOGE("create c randObj failed.");
33                     return 0;
34                 }
35                 auto native = FFIData::Create<RandomImpl>(randObj);
36                 if (!native) {
37                     LOGE("[Random] CreateRandom failed");
38                     HcfObjDestroy(randObj);
39                     *errCode = HCF_ERR_MALLOC;
40                     return 0;
41                 }
42                 LOGD("[Randome] CreateRandom success");
43                 return native->GetID();
44             }
45 
FfiOHOSRandomGetAlgName(int64_t id,int32_t * errCode)46             const char *FfiOHOSRandomGetAlgName(int64_t id, int32_t* errCode)
47             {
48                 LOGD("[Random] GetAlgName start");
49                 auto instance = FFIData::GetData<RandomImpl>(id);
50                 if (!instance) {
51                     LOGE("[Random] instance not exist.");
52                     *errCode = HCF_ERR_MALLOC;
53                     return nullptr;
54                 }
55                 const char* res = instance->GetAlgName(errCode);
56                 LOGD("[Randome] GetAlgName success");
57                 return res;
58             }
59 
FfiOHOSGenerateRandom(int64_t id,int32_t numBytes,int32_t * errCode)60             HcfBlob FfiOHOSGenerateRandom(int64_t id, int32_t numBytes, int32_t* errCode)
61             {
62                 LOGD("[Random] GenerateRandom start");
63                 HcfBlob randBlob;
64                 auto instance = FFIData::GetData<RandomImpl>(id);
65                 if (!instance) {
66                     LOGE("[Random] instance not exist.");
67                     *errCode = HCF_ERR_MALLOC;
68                     return randBlob;
69                 }
70                 randBlob = instance->GenerateRandom(numBytes, errCode);
71                 LOGD("[Randome] GenerateRandom success");
72                 return randBlob;
73             }
74 
FfiOHOSSetSeed(int64_t id,HcfBlob * seed,int32_t * errCode)75             void FfiOHOSSetSeed(int64_t id, HcfBlob *seed, int32_t* errCode)
76             {
77                 LOGD("[Random] SetSeed start");
78                 auto instance = FFIData::GetData<RandomImpl>(id);
79                 if (!instance) {
80                     LOGE("[Random] instance not exist.");
81                     *errCode = HCF_ERR_MALLOC;
82                     return;
83                 }
84                 instance->SetSeed(seed, errCode);
85                 LOGD("[Randome] SetSeed success");
86             }
87 
88             //--------------------- md
FfiOHOSCreateMd(char * algName,int32_t * errCode)89             int64_t FfiOHOSCreateMd(char* algName, int32_t* errCode)
90             {
91                 LOGD("[Md] CreateMd start");
92                 HcfMd *mdObj = nullptr;
93                 HcfResult res = HcfMdCreate(algName, &mdObj);
94                 *errCode = static_cast<int32_t>(res);
95                 if (res != HCF_SUCCESS) {
96                     LOGE("create c mdObj failed.");
97                     return 0;
98                 }
99                 auto native = FFIData::Create<MdImpl>(mdObj);
100                 if (!native) {
101                     LOGE("[Md] CreateMd failed");
102                     HcfObjDestroy(mdObj);
103                     *errCode = HCF_ERR_MALLOC;
104                     return 0;
105                 }
106                 LOGD("[Md] CreateMd success");
107                 return native->GetID();
108             }
109 
FfiOHOSMdUpdate(int64_t id,HcfBlob * input)110             int32_t FfiOHOSMdUpdate(int64_t id, HcfBlob *input)
111             {
112                 LOGD("[Md] FfiOHOSMdUpdate start");
113                 HcfResult res = HCF_ERR_MALLOC;
114                 auto instance = FFIData::GetData<MdImpl>(id);
115                 if (!instance) {
116                     LOGE("[Md] instance not exist.");
117                     return res;
118                 }
119                 res = instance->MdUpdate(input);
120                 LOGD("[Md] FfiOHOSMdUpdate success");
121                 return res;
122             }
123 
FfiOHOSDigest(int64_t id,int32_t * errCode)124             HcfBlob FfiOHOSDigest(int64_t id, int32_t* errCode)
125             {
126                 LOGD("[Md] FfiOHOSDigest start");
127                 auto instance = FFIData::GetData<MdImpl>(id);
128                 HcfBlob blob = { .data = nullptr, .len = 0};
129                 if (!instance) {
130                     LOGE("[Md] instance not exist.");
131                     *errCode = HCF_ERR_MALLOC;
132                     return blob;
133                 }
134                 HcfResult res = instance->MdDoFinal(&blob);
135                 *errCode = static_cast<int32_t>(res);
136                 if (res != HCF_SUCCESS) {
137                     LOGE("doFinal failed!");
138                     return blob;
139                 }
140                 LOGD("[Md] FfiOHOSDigest success");
141                 return blob;
142             }
143 
FfiOHOSGetMdLength(int64_t id,int32_t * errCode)144             uint32_t FfiOHOSGetMdLength(int64_t id, int32_t* errCode)
145             {
146                 LOGD("[Md] FfiOHOSGetMdLength start");
147                 auto instance = FFIData::GetData<MdImpl>(id);
148                 uint32_t res = 0;
149                 if (!instance) {
150                     LOGE("[Md] instance not exist.");
151                     *errCode = HCF_ERR_MALLOC;
152                     return res;
153                 }
154                 res = instance->GetMdLength(errCode);
155                 LOGD("[Md] FfiOHOSGetMdLength success");
156                 return res;
157             }
158 
159             //-------------------symkeygenerator
FfiOHOSCreateSymKeyGenerator(char * algName,int32_t * errCode)160             int64_t FfiOHOSCreateSymKeyGenerator(char* algName, int32_t* errCode)
161             {
162                 LOGD("[SymKeyGenerator] CreateSymKeyGenerator start");
163                 HcfSymKeyGenerator *generator = nullptr;
164                 HcfResult res = HcfSymKeyGeneratorCreate(algName, &generator);
165                 *errCode = static_cast<int32_t>(res);
166                 if (res != HCF_SUCCESS) {
167                     LOGE("create C generator fail.");
168                     return 0;
169                 }
170                 auto native = FFIData::Create<SymKeyGeneratorImpl>(generator);
171                 if (native == nullptr) {
172                     LOGE("[SymKeyGenerator] CreateSymKeyGenerator failed");
173                     HcfObjDestroy(generator);
174                     *errCode = HCF_ERR_MALLOC;
175                     return 0;
176                 }
177                 LOGD("[SymKeyGenerator] CreateSymKeyGenerator success");
178                 return native->GetID();
179             }
180 
FfiOHOSSymKeyGeneratorGetAlgName(int64_t id,int32_t * errCode)181             const char* FfiOHOSSymKeyGeneratorGetAlgName(int64_t id, int32_t* errCode)
182             {
183                 LOGD("[SymKeyGenerator] GetAlgName start");
184                 auto instance = FFIData::GetData<SymKeyGeneratorImpl>(id);
185                 if (!instance) {
186                     LOGE("[SymKeyGenerator] instance not exist.");
187                     *errCode = HCF_ERR_MALLOC;
188                     return nullptr;
189                 }
190                 const char* res = instance->GetAlgName(errCode);
191                 LOGD("[SymKeyGenerator] GetAlgName success");
192                 return res;
193             }
194 
FfiOHOSGenerateSymKey(int64_t id,int32_t * errCode)195             int64_t FfiOHOSGenerateSymKey(int64_t id, int32_t* errCode)
196             {
197                 LOGD("[SymKeyGenerator] GenerateSymKey start");
198                 auto instance = FFIData::GetData<SymKeyGeneratorImpl>(id);
199                 if (!instance) {
200                     LOGE("[SymKeyGenerator] instance not exist.");
201                     *errCode = HCF_ERR_MALLOC;
202                     return 0;
203                 }
204                 HcfSymKey *key = nullptr;
205                 HcfResult res = instance->GenerateSymKey(&key);
206                 *errCode = static_cast<int32_t>(res);
207                 if (res != HCF_SUCCESS) {
208                     LOGE("generate sym key failed.");
209                     return 0;
210                 }
211                 auto native = FFIData::Create<SymKeyImpl>(key);
212                 if (native == nullptr) {
213                     LOGE("[SymKeyGenerator] GenerateSymKey failed");
214                     HcfObjDestroy(key);
215                     *errCode = HCF_ERR_MALLOC;
216                     return 0;
217                 }
218                 LOGD("[SymKeyGenerator] GenerateSymKey success");
219                 return native->GetID();
220             }
221 
FfiOHOSConvertKey(int64_t id,HcfBlob * key,int32_t * errCode)222             int64_t FfiOHOSConvertKey(int64_t id, HcfBlob *key, int32_t* errCode)
223             {
224                 LOGD("[SymKeyGenerator] ConvertKey start");
225                 auto instance = FFIData::GetData<SymKeyGeneratorImpl>(id);
226                 if (!instance) {
227                     LOGE("[SymKeyGenerator] instance not exist.");
228                     *errCode = HCF_ERR_MALLOC;
229                     return 0;
230                 }
231                 HcfSymKey *symkey = nullptr;
232                 HcfResult res = instance->ConvertKey(*key, &symkey);
233                 *errCode = static_cast<int32_t>(res);
234                 if (res != HCF_SUCCESS) {
235                     LOGE("generate sym key failed.");
236                     return 0;
237                 }
238                 auto native = FFIData::Create<SymKeyImpl>(symkey);
239                 if (native == nullptr) {
240                     LOGE("[SymKeyGenerator] ConvertKey failed");
241                     HcfObjDestroy(key);
242                     *errCode = HCF_ERR_MALLOC;
243                     return 0;
244                 }
245                 LOGD("[SymKeyGenerator] ConvertKey success");
246                 return native->GetID();
247             }
248 
249             //-------------------symkey
FfiOHOSSymKeyGetAlgName(int64_t id,int32_t * errCode)250             const char *FfiOHOSSymKeyGetAlgName(int64_t id, int32_t* errCode)
251             {
252                 LOGD("[SymKey] GetAlgName start");
253                 auto instance = FFIData::GetData<SymKeyImpl>(id);
254                 if (!instance) {
255                     LOGE("[SymKey] instance not exist.");
256                     *errCode = HCF_ERR_MALLOC;
257                     return nullptr;
258                 }
259                 const char* res = instance->GetAlgorithm(errCode);
260                 LOGD("[SymKey] GetAlgName success");
261                 return res;
262             }
263 
FfiOHOSSymKeyGetFormat(int64_t id,int32_t * errCode)264             const char *FfiOHOSSymKeyGetFormat(int64_t id, int32_t* errCode)
265             {
266                 LOGD("[SymKey] GetFormat start");
267                 auto instance = FFIData::GetData<SymKeyImpl>(id);
268                 if (!instance) {
269                     LOGE("[SymKey] instance not exist.");
270                     *errCode = HCF_ERR_MALLOC;
271                     return nullptr;
272                 }
273                 const char* res = instance->GetFormat(errCode);
274                 LOGD("[SymKey] GetFormat success");
275                 return res;
276             }
277 
FfiOHOSSymKeyGetEncoded(int64_t id,HcfBlob * returnBlob)278             int32_t FfiOHOSSymKeyGetEncoded(int64_t id, HcfBlob *returnBlob)
279             {
280                 LOGD("[SymKey] GetEncoded start");
281                 auto instance = FFIData::GetData<SymKeyImpl>(id);
282                 if (!instance) {
283                     LOGE("[SymKey] instance not exist.");
284                     return HCF_ERR_MALLOC;
285                 }
286                 HcfResult res = instance->GetEncoded(returnBlob);
287                 LOGD("[SymKey] GetEncoded success");
288                 return res;
289             }
290 
FfiOHOSClearMem(int64_t id)291             void FfiOHOSClearMem(int64_t id)
292             {
293                 LOGD("[SymKey] ClearMem start");
294                 auto instance = FFIData::GetData<SymKeyImpl>(id);
295                 if (!instance) {
296                     LOGE("[SymKey] instance not exist.");
297                     return;
298                 }
299                 instance->ClearMem();
300                 LOGD("[SymKey] ClearMem success");
301             }
302 
FfiOHOSSymKeyGetHcfKey(int64_t id)303             void* FfiOHOSSymKeyGetHcfKey(int64_t id)
304             {
305                 LOGD("[SymKey] GetHcfKey start");
306                 auto instance = FFIData::GetData<SymKeyImpl>(id);
307                 if (!instance) {
308                     LOGE("[SymKey] instance not exist.");
309                     return nullptr;
310                 }
311                 HcfKey *key = instance->GetHcfKey();
312                 LOGD("[SymKey] GetHcfKey success");
313                 return key;
314             }
315 
316             // cipher
317             const std::string IV_PARAMS_SPEC = "IvParamsSpec";
318             const std::string GCM_PARAMS_SPEC = "GcmParamsSpec";
319             const std::string CCM_PARAMS_SPEC = "CcmParamsSpec";
320             const size_t GCM_AUTH_TAG_LEN = 16;
321             const size_t CCM_AUTH_TAG_LEN = 12;
GetIvParamsSpecType()322             static const char *GetIvParamsSpecType()
323             {
324                 return IV_PARAMS_SPEC.c_str();
325             }
326 
GetGcmParamsSpecType()327             static const char *GetGcmParamsSpecType()
328             {
329                 return GCM_PARAMS_SPEC.c_str();
330             }
331 
GetCcmParamsSpecType()332             static const char *GetCcmParamsSpecType()
333             {
334                 return CCM_PARAMS_SPEC.c_str();
335             }
336 
HcfMalloc(uint32_t size,char val)337             void *HcfMalloc(uint32_t size, char val)
338             {
339                 if ((size == 0) || (size > MAX_MEMORY_SIZE)) {
340                     LOGE("malloc size is invalid");
341                     return nullptr;
342                 }
343                 void *addr = malloc(size);
344                 if (addr != nullptr) {
345                     (void)memset_s(addr, size, val, size);
346                 }
347                 return addr;
348             }
349 
HcfFree(void * addr)350             void HcfFree(void *addr)
351             {
352                 if (addr != nullptr) {
353                     free(addr);
354                 }
355             }
356 
FfiOHOSCreateCipher(char * transformation,int32_t * errCode)357             int64_t FfiOHOSCreateCipher(char* transformation, int32_t* errCode)
358             {
359                 LOGD("[Cipher] CreateCipher start");
360                 HcfCipher *cipher = nullptr;
361                 HcfResult res = HcfCipherCreate(transformation, &cipher);
362                 *errCode = static_cast<int32_t>(res);
363                 if (res != HCF_SUCCESS) {
364                     LOGE("create C cipher fail!");
365                     return 0;
366                 }
367                 auto native = FFIData::Create<CipherImpl>(cipher);
368                 if (native == nullptr) {
369                     LOGE("[Cipher] CreateCipher failed");
370                     HcfObjDestroy(cipher);
371                     *errCode = HCF_ERR_MALLOC;
372                     return 0;
373                 }
374                 LOGD("[Cipher] CreateCipher success");
375                 return native->GetID();
376             }
377 
FfiOHOSCipherInitByIv(int64_t id,int32_t opMode,void * key,HcfBlob blob1)378             int32_t FfiOHOSCipherInitByIv(int64_t id, int32_t opMode, void* key, HcfBlob blob1)
379             {
380                 LOGD("[Cipher] FfiOHOSCipherInitByIv start");
381                 if (key == nullptr) {
382                     LOGE("[Cipher] key can not be nullptr.");
383                     return HCF_INVALID_PARAMS;
384                 }
385                 auto instance = FFIData::GetData<CipherImpl>(id);
386                 if (!instance) {
387                     LOGE("[Cipher] instance not exist.");
388                     return HCF_ERR_MALLOC;
389                 }
390                 HcfIvParamsSpec *ivParamsSpec = reinterpret_cast<HcfIvParamsSpec *>(
391                         HcfMalloc(sizeof(HcfIvParamsSpec), 0));
392                 if (ivParamsSpec == nullptr) {
393                     LOGE("ivParamsSpec malloc failed!");
394                     return HCF_INVALID_PARAMS;
395                 }
396                 ivParamsSpec->base.getType = GetIvParamsSpecType;
397                 ivParamsSpec->iv = blob1;
398                 HcfCryptoMode mode = HcfCryptoMode(opMode);
399                 HcfParamsSpec *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ivParamsSpec);
400                 ivParamsSpec = nullptr;
401                 HcfResult res = instance->CipherInit(mode, static_cast<HcfKey*>(key), paramsSpec);
402                 HcfFree(paramsSpec);
403                 paramsSpec = nullptr;
404                 LOGD("[Cipher] FfiOHOSCipherInitByIv success");
405                 return res;
406             }
407 
FfiOHOSCipherInitByGcm(int64_t id,int32_t opMode,void * key,CParamsSpec spec)408             int32_t FfiOHOSCipherInitByGcm(int64_t id, int32_t opMode, void* key, CParamsSpec spec)
409             {
410                 LOGD("[Cipher] FfiOHOSCipherInitByGcm start");
411                 if (key == nullptr) {
412                     LOGE("[Cipher] key can not be nullptr.");
413                     return HCF_INVALID_PARAMS;
414                 }
415                 auto instance = FFIData::GetData<CipherImpl>(id);
416                 if (!instance) {
417                     LOGE("[Cipher] instance not exist.");
418                     return HCF_ERR_MALLOC;
419                 }
420                 HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast<HcfGcmParamsSpec *>(
421                                                     HcfMalloc(sizeof(HcfGcmParamsSpec), 0));
422                 if (gcmParamsSpec == nullptr) {
423                     LOGE("gcmParamsSpec malloc failed!");
424                     return HCF_INVALID_PARAMS;
425                 }
426                 HcfCryptoMode mode = HcfCryptoMode(opMode);
427                 HcfBlob authTag = {};
428                 if (mode == DECRYPT_MODE) {
429                     gcmParamsSpec->tag = spec.authTag;
430                 } else if (mode == ENCRYPT_MODE) {
431                     authTag.data = static_cast<uint8_t *>(HcfMalloc(GCM_AUTH_TAG_LEN, 0));
432                     if (authTag.data == nullptr) {
433                         HcfFree(gcmParamsSpec);
434                         return HCF_INVALID_PARAMS;
435                     }
436                     authTag.len = GCM_AUTH_TAG_LEN;
437                     gcmParamsSpec->tag = authTag;
438                 }
439                 gcmParamsSpec->base.getType = GetGcmParamsSpecType;
440                 gcmParamsSpec->iv = spec.iv;
441                 gcmParamsSpec->aad = spec.add;
442                 HcfParamsSpec *paramsSpec = reinterpret_cast<HcfParamsSpec *>(gcmParamsSpec);
443                 gcmParamsSpec = nullptr;
444                 HcfResult res = instance->CipherInit(mode, static_cast<HcfKey*>(key), paramsSpec);
445                 HcfBlobDataFree(&authTag);
446                 HcfFree(paramsSpec);
447                 paramsSpec = nullptr;
448                 LOGD("[Cipher] FfiOHOSCipherInitByGcm success");
449                 return res;
450             }
451 
FfiOHOSCipherInitByCcm(int64_t id,int32_t opMode,void * key,CParamsSpec spec)452             int32_t FfiOHOSCipherInitByCcm(int64_t id, int32_t opMode, void* key, CParamsSpec spec)
453             {
454                 LOGD("[Cipher] FfiOHOSCipherInitByCcm start");
455                 if (key == nullptr) {
456                     LOGE("[Cipher] key can not be nullptr.");
457                     return HCF_INVALID_PARAMS;
458                 }
459                 auto instance = FFIData::GetData<CipherImpl>(id);
460                 if (!instance) {
461                     LOGE("[Cipher] instance not exist.");
462                     return HCF_ERR_MALLOC;
463                 }
464                 HcfCcmParamsSpec *ccmParamsSpec = reinterpret_cast<HcfCcmParamsSpec *>(
465                                                     HcfMalloc(sizeof(HcfCcmParamsSpec), 0));
466                 if (ccmParamsSpec == nullptr) {
467                     LOGE("ccmParamsSpec malloc failed!");
468                     return HCF_INVALID_PARAMS;
469                 }
470                 HcfBlob authTag = {};
471                 HcfCryptoMode mode = HcfCryptoMode(opMode);
472                 if (mode == DECRYPT_MODE) {
473                     ccmParamsSpec->tag = spec.authTag;
474                 } else if (mode == ENCRYPT_MODE) {
475                     authTag.data = static_cast<uint8_t *>(HcfMalloc(CCM_AUTH_TAG_LEN, 0));
476                     if (authTag.data == nullptr) {
477                         HcfFree(ccmParamsSpec);
478                         return HCF_INVALID_PARAMS;
479                     }
480                     authTag.len = CCM_AUTH_TAG_LEN;
481                     ccmParamsSpec->tag = authTag;
482                 }
483                 ccmParamsSpec->base.getType = GetCcmParamsSpecType;
484                 ccmParamsSpec->iv = spec.iv;
485                 ccmParamsSpec->aad = spec.add;
486                 HcfParamsSpec *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ccmParamsSpec);
487                 ccmParamsSpec = nullptr;
488                 HcfResult res = instance->CipherInit(mode, static_cast<HcfKey*>(key), paramsSpec);
489                 HcfBlobDataFree(&authTag);
490                 HcfFree(paramsSpec);
491                 paramsSpec = nullptr;
492                 LOGD("[Cipher] FfiOHOSCipherInitByCcm success");
493                 return res;
494             }
495 
FfiOHOSCipherInitWithOutParams(int64_t id,int32_t opMode,void * key)496             int32_t FfiOHOSCipherInitWithOutParams(int64_t id, int32_t opMode, void* key)
497             {
498                 LOGD("[Cipher] FfiOHOSCipherInitWithOutParams start");
499                 if (key == nullptr) {
500                     LOGE("[Cipher] key can not be nullptr.");
501                     return HCF_INVALID_PARAMS;
502                 }
503                 auto instance = FFIData::GetData<CipherImpl>(id);
504                 if (!instance) {
505                     LOGE("[Cipher] instance not exist.");
506                     return HCF_ERR_MALLOC;
507                 }
508                 HcfParamsSpec *paramsSpec = nullptr;
509                 HcfCryptoMode mode = HcfCryptoMode(opMode);
510                 HcfResult res = instance->CipherInit(mode, static_cast<HcfKey*>(key), paramsSpec);
511                 LOGD("[Cipher] FfiOHOSCipherInitWithOutParams success");
512                 return res;
513             }
514 
FfiOHOSCipherUpdate(int64_t id,HcfBlob * input,HcfBlob * output)515             int32_t FfiOHOSCipherUpdate(int64_t id, HcfBlob *input, HcfBlob *output)
516             {
517                 LOGD("[Cipher] CipherUpdate start");
518                 auto instance = FFIData::GetData<CipherImpl>(id);
519                 if (!instance) {
520                     LOGE("[Cipher] instance not exist.");
521                     return HCF_ERR_MALLOC;
522                 }
523                 HcfResult res = instance->CipherUpdate(input, output);
524                 LOGD("[Cipher] CipherUpdate success");
525                 return res;
526             }
527 
FfiOHOSCipherDoFinal(int64_t id,HcfBlob * input,HcfBlob * output)528             int32_t FfiOHOSCipherDoFinal(int64_t id, HcfBlob *input, HcfBlob *output)
529             {
530                 LOGD("[Cipher] CipherDoFinal start");
531                 auto instance = FFIData::GetData<CipherImpl>(id);
532                 if (!instance) {
533                     LOGE("[Cipher] instance not exist.");
534                     return HCF_ERR_MALLOC;
535                 }
536                 HcfResult res = instance->CipherDoFinal(input, output);
537                 LOGD("[Cipher] CipherDoFinal success %{public}d", res);
538                 return res;
539             }
540 
FfiOHOSSetCipherSpec(int64_t id,int32_t item,HcfBlob pSource)541             int32_t FfiOHOSSetCipherSpec(int64_t id, int32_t item, HcfBlob pSource)
542             {
543                 LOGD("[Cipher] SetCipherSpec start");
544                 auto instance = FFIData::GetData<CipherImpl>(id);
545                 if (!instance) {
546                     LOGE("[Cipher] instance not exist.");
547                     return HCF_ERR_MALLOC;
548                 }
549                 CipherSpecItem csi = CipherSpecItem(item);
550                 HcfResult res = instance->SetCipherSpec(csi, pSource);
551                 LOGD("[Cipher] SetCipherSpec success");
552                 return res;
553             }
554 
FfiOHOSGetCipherSpecString(int64_t id,int32_t item,int32_t * errCode)555             char *FfiOHOSGetCipherSpecString(int64_t id, int32_t item, int32_t *errCode)
556             {
557                 LOGD("[Cipher] FfiOHOSGetCipherSpecString start");
558                 auto instance = FFIData::GetData<CipherImpl>(id);
559                 if (!instance) {
560                     LOGE("[Cipher] instance not exist.");
561                     *errCode = HCF_ERR_MALLOC;
562                     return nullptr;
563                 }
564                 CipherSpecItem specItem = CipherSpecItem(item);
565                 char *returnString = nullptr;
566                 *errCode = instance->GetCipherSpecString(specItem, &returnString);
567                 LOGD("[Cipher] FfiOHOSGetCipherSpecString success");
568                 return returnString;
569             }
570 
FfiOHOSGetCipherSpecUint8Array(int64_t id,int32_t item,HcfBlob * returnUint8Array)571             int32_t FfiOHOSGetCipherSpecUint8Array(int64_t id, int32_t item, HcfBlob *returnUint8Array)
572             {
573                 LOGD("[Cipher] GetCipherSpecUint8Array start");
574                 auto instance = FFIData::GetData<CipherImpl>(id);
575                 if (!instance) {
576                     LOGE("[Cipher] instance not exist.");
577                     return HCF_ERR_MALLOC;
578                 }
579                 CipherSpecItem csi = CipherSpecItem(item);
580                 HcfResult res = instance->GetCipherSpecUint8Array(csi, returnUint8Array);
581                 LOGD("[Cipher] GetCipherSpecUint8Array success");
582                 return res;
583             }
584 
FfiOHOSCipherGetAlgName(int64_t id,int32_t * errCode)585             const char *FfiOHOSCipherGetAlgName(int64_t id, int32_t* errCode)
586             {
587                 LOGD("[Cipher] GetAlgName start");
588                 auto instance = FFIData::GetData<CipherImpl>(id);
589                 if (!instance) {
590                     LOGE("[SymKey] instance not exist.");
591                     *errCode = HCF_ERR_MALLOC;
592                     return nullptr;
593                 }
594                 const char* res = instance->GetAlgorithm(errCode);
595                 LOGD("[Cipher] GetAlgName success");
596                 return res;
597             }
598 
599             //--------------------- mac
FFiOHOSCryptoMacConstructor(char * algName,int32_t * errCode)600             int64_t FFiOHOSCryptoMacConstructor(char* algName, int32_t* errCode)
601             {
602                 LOGD("[Mac] CreateMac start");
603                 HcfMac *macObj = nullptr;
604                 HcfHmacParamsSpec paramsSpec = {};
605                 paramsSpec.base.algName = "HMAC";
606                 paramsSpec.mdName = algName;
607                 HcfResult res = HcfMacCreate(reinterpret_cast<HcfMacParamsSpec *>(&paramsSpec), &macObj);
608                 *errCode = static_cast<int32_t>(res);
609                 if (res != HCF_SUCCESS) {
610                     LOGE("create c macObj failed.");
611                     return 0;
612                 }
613                 auto native = FFIData::Create<MacImpl>(macObj);
614                 if (native == nullptr) {
615                     LOGE("[Mac] CreateMac failed");
616                     HcfObjDestroy(macObj);
617                     *errCode = HCF_ERR_MALLOC;
618                     return 0;
619                 }
620                 LOGD("[Mac] CreateMac success");
621                 return native->GetID();
622             }
623 
FfiOHOSCryptoMacInit(int64_t id,int64_t symKeyId)624             int32_t FfiOHOSCryptoMacInit(int64_t id, int64_t symKeyId)
625             {
626                 LOGD("[MAC] FfiOHOSCryptoMacInit start");
627                 auto instance = FFIData::GetData<MacImpl>(id);
628                 if (!instance) {
629                     LOGE("[MAC] MacImpl instance not exist.");
630                     return HCF_ERR_MALLOC;
631                 }
632 
633                 auto keyInstance = FFIData::GetData<SymKeyImpl>(symKeyId);
634                 if (!keyInstance) {
635                     LOGE("[MAC] SymKeyImpl instance not exist.");
636                     return HCF_ERR_MALLOC;
637                 }
638 
639                 HcfResult res = instance->MacInit(keyInstance->GetSymKey());
640                 if (res != HCF_SUCCESS) {
641                     LOGE("[MAC] MacInit error %{public}d", res);
642                 } else {
643                     LOGD("[MAC] FfiOHOSCryptoMacInit success");
644                 }
645 
646                 return res;
647             }
648 
FfiOHOSCryptoMacUpdate(int64_t id,HcfBlob * input)649             int32_t FfiOHOSCryptoMacUpdate(int64_t id, HcfBlob *input)
650             {
651                 LOGD("[Mac] FfiOHOSCryptoMacUpdate start");
652                 HcfResult res = HCF_ERR_MALLOC;
653                 auto instance = FFIData::GetData<MacImpl>(id);
654                 if (!instance) {
655                     LOGE("[Mac] instance not exist.");
656                     return res;
657                 }
658                 res = instance->MacUpdate(input);
659                 LOGD("[Mac] FfiOHOSCryptoMacUpdate success");
660                 return res;
661             }
662 
FfiOHOSCryptoMacDoFinal(int64_t id,int32_t * errCode)663             HcfBlob FfiOHOSCryptoMacDoFinal(int64_t id, int32_t* errCode)
664             {
665                 LOGD("[Mac] FfiOHOSCryptoMacDoFinal start");
666                 auto instance = FFIData::GetData<MacImpl>(id);
667                 HcfBlob blob = { .data = nullptr, .len = 0};
668                 if (!instance) {
669                     LOGE("[Mac] instance not exist.");
670                     *errCode = HCF_ERR_MALLOC;
671                     return blob;
672                 }
673                 HcfResult res = instance->MacDoFinal(&blob);
674                 *errCode = static_cast<int32_t>(res);
675                 if (res != HCF_SUCCESS) {
676                     LOGE("doFinal failed!");
677                     return blob;
678                 }
679                 LOGD("[Mac] FfiOHOSCryptoMacDoFinal success");
680                 return blob;
681             }
682 
FfiOHOSCryptoGetMacLength(int64_t id)683             uint32_t FfiOHOSCryptoGetMacLength(int64_t id)
684             {
685                 LOGD("[Mac] FfiOHOSGCryptoGetMacLength start");
686                 auto instance = FFIData::GetData<MacImpl>(id);
687                 uint32_t res = 0;
688                 if (!instance) {
689                     LOGE("[Mac] instance not exist.");
690                     return res;
691                 }
692                 res = instance->GetMacLength();
693                 LOGD("[Mac] FfiOHOSGCryptoGetMacLength success");
694                 return res;
695             }
696 
697             //--------------------- sign
FFiOHOSCryptoSignConstructor(char * algName,int32_t * errCode)698             int64_t FFiOHOSCryptoSignConstructor(char* algName, int32_t* errCode)
699             {
700                 LOGD("[Sign] FFiOHOSCryptoSignConstructor start");
701                 HcfSign *signObj = nullptr;
702                 HcfResult res = HcfSignCreate(algName, &signObj);
703                 *errCode = static_cast<int32_t>(res);
704                 if (res != HCF_SUCCESS) {
705                     LOGE("[Sign] FFiOHOSCryptoSignConstructor create c signObj failed.");
706                     return 0;
707                 }
708                 auto native = FFIData::Create<SignImpl>(signObj);
709                 if (native == nullptr) {
710                     LOGE("[Sign] FFiOHOSCryptoSignConstructor create failed");
711                     HcfObjDestroy(signObj);
712                     *errCode = HCF_ERR_MALLOC;
713                     return 0;
714                 }
715                 LOGD("[Sign] FFiOHOSCryptoSignConstructor success");
716                 return native->GetID();
717             }
718 
FFiOHOSSignInit(int64_t sid,int64_t pid)719             int32_t FFiOHOSSignInit(int64_t sid, int64_t pid)
720             {
721                 LOGD("[Sign] FFiOHOSSignInit start");
722                 auto sign = FFIData::GetData<SignImpl>(sid);
723                 if (sign == nullptr) {
724                     LOGE("[Sign] FFiOHOSSignInit failed to get sign obj.");
725                     return HCF_INVALID_PARAMS;
726                 }
727                 auto priKeyImpl = FFIData::GetData<PriKeyImpl>(pid);
728                 if (priKeyImpl == nullptr) {
729                     LOGE("[Sign] FFiOHOSSignInit failed to get priKeyImpl obj.");
730                     return HCF_INVALID_PARAMS;
731                 }
732                 HcfPriKey *priKey = priKeyImpl->GetPriKey();
733                 if (priKey == nullptr) {
734                     LOGE("[Sign] FFiOHOSSignInit failed to get priKey obj.");
735                     return HCF_INVALID_PARAMS;
736                 }
737                 LOGD("[Sign] FFiOHOSSignInit success");
738                 return sign->Init(priKey);
739             }
740 
FFiOHOSSignUpdate(int64_t id,HcfBlob input)741             int32_t FFiOHOSSignUpdate(int64_t id, HcfBlob input)
742             {
743                 LOGD("[Sign] FFiOHOSSignUpdate start");
744                 auto sign = FFIData::GetData<SignImpl>(id);
745                 if (sign == nullptr) {
746                     LOGE("[Sign] FFiOHOSSignUpdate failed to get sign obj.");
747                     return HCF_INVALID_PARAMS;
748                 }
749                 LOGD("[Sign] FFiOHOSSignUpdate success");
750                 return sign->Update(&input);
751             }
752 
FFiOHOSSignSign(int64_t id,HcfBlob * input,HcfBlob * output)753             int32_t FFiOHOSSignSign(int64_t id, HcfBlob *input, HcfBlob *output)
754             {
755                 LOGD("[Sign] FFiOHOSSignSign start");
756                 auto sign = FFIData::GetData<SignImpl>(id);
757                 if (sign == nullptr) {
758                     LOGE("[Sign] FFiOHOSSignSign failed to get sign obj.");
759                     return HCF_INVALID_PARAMS;
760                 }
761                 LOGD("[Sign] FFiOHOSSignSign success");
762                 return sign->Sign(input, output);
763             }
764 
FFiOHOSSignSetSignSpecByNum(int64_t id,int32_t itemValue)765             int32_t FFiOHOSSignSetSignSpecByNum(int64_t id, int32_t itemValue)
766             {
767                 LOGD("[Sign] FFiOHOSSignSetSignSpecByNum start");
768                 auto sign = FFIData::GetData<SignImpl>(id);
769                 if (sign == nullptr) {
770                     LOGE("[Sign] FFiOHOSSignSetSignSpecByNum failed to get sign obj.");
771                     return HCF_INVALID_PARAMS;
772                 }
773                 LOGD("[Sign] FFiOHOSSignSetSignSpecByNum success");
774                 return sign->SetSignSpecByNum(itemValue);
775             }
776 
FFiOHOSSignSetSignSpecByArr(int64_t id,HcfBlob itemValue)777             int32_t FFiOHOSSignSetSignSpecByArr(int64_t id, HcfBlob itemValue)
778             {
779                 LOGD("[Sign] FFiOHOSSignSetSignSpecByArr start");
780                 auto sign = FFIData::GetData<SignImpl>(id);
781                 if (sign == nullptr) {
782                     LOGE("[Sign] FFiOHOSSignSetSignSpecByArr failed to get sign obj.");
783                     return HCF_INVALID_PARAMS;
784                 }
785                 LOGD("[Sign] FFiOHOSSignSetSignSpecByArr success");
786                 return sign->SetSignSpecByArr(itemValue);
787             }
788 
FFiOHOSSignGetSignSpecString(int64_t id,SignSpecItem item,int32_t * errCode)789             char *FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, int32_t *errCode)
790             {
791                 LOGD("[Sign] FFiOHOSSignGetSignSpecString start");
792                 auto sign = FFIData::GetData<SignImpl>(id);
793                 if (sign == nullptr) {
794                     LOGE("[Sign] FFiOHOSSignGetSignSpecString failed to get sign obj.");
795                     *errCode = HCF_INVALID_PARAMS;
796                     return nullptr;
797                 }
798                 char *returnString = nullptr;
799                 *errCode = sign->GetSignSpecString(item, &returnString);
800                 LOGD("[Sign] FFiOHOSSignGetSignSpecString success");
801                 return returnString;
802             }
803 
FFiOHOSSignGetSignSpecNum(int64_t id,SignSpecItem item,int32_t * itemValue)804             int32_t FFiOHOSSignGetSignSpecNum(int64_t id, SignSpecItem item, int32_t *itemValue)
805             {
806                 LOGD("[Sign] FFiOHOSSignGetSignSpecNum start");
807                 auto sign = FFIData::GetData<SignImpl>(id);
808                 if (sign == nullptr) {
809                     LOGE("[Sign] FFiOHOSSignGetSignSpecNum failed to get sign obj.");
810                     return HCF_INVALID_PARAMS;
811                 }
812                 LOGD("[Sign] FFiOHOSSignGetSignSpecNum success");
813                 return sign->GetSignSpecNum(item, itemValue);
814             }
815 
816             //--------------------- verify
FFiOHOSVerifyConstructor(char * algName,int32_t * errCode)817             int64_t FFiOHOSVerifyConstructor(char* algName, int32_t* errCode)
818             {
819                 LOGD("[Verify]FFiOHOSVerifyConstructor start");
820                 HcfVerify *verify = nullptr;
821                 HcfResult res = HcfVerifyCreate(algName, &verify);
822                 *errCode = static_cast<int32_t>(res);
823                 if (res != HCF_SUCCESS) {
824                     LOGE("[Verify] FFiOHOSVerifyConstructor create c verifyObj failed.");
825                     return 0;
826                 }
827                 auto native = FFIData::Create<VerifyImpl>(verify);
828                 if (native == nullptr) {
829                     LOGE("[Verify] FFiOHOSVerifyConstructor create failed");
830                     HcfObjDestroy(verify);
831                     *errCode = HCF_ERR_MALLOC;
832                     return 0;
833                 }
834                 LOGD("[Verify] FFiOHOSVerifyConstructor success");
835                 return native->GetID();
836             }
837 
FFiOHOSVerifyInit(int64_t sid,int64_t pid)838             int32_t FFiOHOSVerifyInit(int64_t sid, int64_t pid)
839             {
840                 LOGD("[Verify] FFiOHOSVerifyInit start");
841                 auto verify = FFIData::GetData<VerifyImpl>(sid);
842                 if (verify == nullptr) {
843                     LOGE("[Verify] FFiOHOSVerifyInit failed to get verify obj.");
844                     return HCF_INVALID_PARAMS;
845                 }
846                 auto pubKeyImpl = FFIData::GetData<PubKeyImpl>(pid);
847                 if (pubKeyImpl == nullptr) {
848                     LOGE("[Verify] FFiOHOSVerifyInit failed to get priKeyImpl obj.");
849                     return HCF_INVALID_PARAMS;
850                 }
851                 HcfPubKey *pubKey = pubKeyImpl->GetPubKey();
852                 if (pubKey == nullptr) {
853                     LOGE("[Verify] FFiOHOSVerifyInit failed to get priKey obj.");
854                     return HCF_INVALID_PARAMS;
855                 }
856                 LOGD("[Verify] FFiOHOSVerifyInit success");
857                 return verify->Init(pubKey);
858             }
859 
FFiOHOSVerifyUpdate(int64_t id,HcfBlob input)860             int32_t FFiOHOSVerifyUpdate(int64_t id, HcfBlob input)
861             {
862                 LOGD("[Verify] FFiOHOSVerifyUpdate start");
863                 auto verify = FFIData::GetData<VerifyImpl>(id);
864                 if (verify == nullptr) {
865                     LOGE("[Verify] FFiOHOSVerifyUpdate failed to get verify obj.");
866                     return HCF_INVALID_PARAMS;
867                 }
868                 LOGD("[Verify] FFiOHOSVerifyUpdate success");
869                 return verify->Update(&input);
870             }
871 
FFiOHOSVerifyVerify(int64_t id,HcfBlob * data,HcfBlob signatureData,int32_t * errCode)872             bool FFiOHOSVerifyVerify(int64_t id, HcfBlob *data, HcfBlob signatureData, int32_t* errCode)
873             {
874                 LOGD("[Verify] FFiOHOSVerifyVerify start");
875                 auto verify = FFIData::GetData<VerifyImpl>(id);
876                 if (verify == nullptr) {
877                     LOGE("[Verify] FFiOHOSVerifyVerify failed to get verify obj.");
878                     return HCF_INVALID_PARAMS;
879                 }
880                 LOGD("[Verify] FFiOHOSVerifyVerify success");
881                 return verify->Verify(data, signatureData, errCode);
882             }
883 
FFiOHOSVerifyRecover(int64_t id,HcfBlob input,HcfBlob * output)884             int32_t FFiOHOSVerifyRecover(int64_t id, HcfBlob input, HcfBlob *output)
885             {
886                 LOGD("[Verify] FFiOHOSVerifyRecover start");
887                 auto verify = FFIData::GetData<VerifyImpl>(id);
888                 if (verify == nullptr) {
889                     LOGE("[Verify] FFiOHOSVerifyVerify failed to get verify obj.");
890                     return HCF_INVALID_PARAMS;
891                 }
892                 LOGD("[Verify] FFiOHOSVerifyRecover success");
893                 return verify->Recover(input, output);
894             }
895 
896 
FFiOHOSVerifySetVerifySpecByNum(int64_t id,int32_t itemValue)897             int32_t FFiOHOSVerifySetVerifySpecByNum(int64_t id, int32_t itemValue)
898             {
899                 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByNum start");
900                 auto verify = FFIData::GetData<VerifyImpl>(id);
901                 if (verify == nullptr) {
902                     LOGE("[Verify] FFiOHOSVerifySetVerifySpecByNum failed to get verify obj.");
903                     return HCF_INVALID_PARAMS;
904                 }
905                 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByNum success");
906                 return verify->SetVerifySpecByNum(itemValue);
907             }
908 
FFiOHOSVerifySetVerifySpecByArr(int64_t id,HcfBlob itemValue)909             int32_t FFiOHOSVerifySetVerifySpecByArr(int64_t id, HcfBlob itemValue)
910             {
911                 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByArr start");
912                 auto verify = FFIData::GetData<VerifyImpl>(id);
913                 if (verify == nullptr) {
914                     LOGE("[Verify] FFiOHOSVerifySetVerifySpecByArr failed to get verify obj.");
915                     return HCF_INVALID_PARAMS;
916                 }
917                 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByArr success");
918                 return verify->SetVerifySpecByArr(itemValue);
919             }
920 
FFiOHOSVerifyGetVerifySpecString(int64_t id,SignSpecItem item,int32_t * errCode)921             char *FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, int32_t *errCode)
922             {
923                 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString start");
924                 auto verify = FFIData::GetData<VerifyImpl>(id);
925                 if (verify == nullptr) {
926                     LOGE("[Verify] FFiOHOSVerifyGetVerifySpecString failed to get verify obj.");
927                     *errCode =  HCF_INVALID_PARAMS;
928                     return nullptr;
929                 }
930                 char *returnString = nullptr;
931                 *errCode = verify->GetVerifySpecString(item, &returnString);
932                 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString success");
933                 return returnString;
934             }
935 
FFiOHOSVerifyGetVerifySpecNum(int64_t id,SignSpecItem item,int32_t * itemValue)936             int32_t FFiOHOSVerifyGetVerifySpecNum(int64_t id, SignSpecItem item, int32_t *itemValue)
937             {
938                 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecNum start");
939                 auto verify = FFIData::GetData<VerifyImpl>(id);
940                 if (verify == nullptr) {
941                     LOGE("[Verify] FFiOHOSVerifyGetVerifySpecNum failed to get verify obj.");
942                     return HCF_INVALID_PARAMS;
943                 }
944                 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecNum success");
945                 return verify->GetVerifySpecNum(item, itemValue);
946             }
947 
948             //--------------------- asykeygenerator
FFiOHOSAsyKeyGeneratorConstructor(char * algName,int32_t * errCode)949             int64_t FFiOHOSAsyKeyGeneratorConstructor(char *algName, int32_t *errCode)
950             {
951                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGeneratorConstructor start");
952                 HcfAsyKeyGenerator *generator = nullptr;
953                 *errCode = HcfAsyKeyGeneratorCreate(algName, &generator);
954                 if (*errCode != HCF_SUCCESS) {
955                     *errCode = HCF_INVALID_PARAMS;
956                     LOGE("create c generator fail.");
957                     return 0;
958                 }
959                 auto instance = FFIData::Create<AsyKeyGeneratorImpl>(generator);
960                 if (!instance) {
961                     *errCode = HCF_ERR_MALLOC;
962                     HcfObjDestroy(generator);
963                     LOGE("new asy key generator failed");
964                     return 0;
965                 }
966                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGeneratorConstructor end");
967                 return instance->GetID();
968             }
969 
FFiOHOSAsyKeyGeneratorGenerateKeyPair(int64_t id,int32_t * errCode)970             int64_t FFiOHOSAsyKeyGeneratorGenerateKeyPair(int64_t id, int32_t *errCode)
971             {
972                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGenerateKeyPair start");
973                 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id);
974                 if (!instance) {
975                     *errCode = HCF_INVALID_PARAMS;
976                     LOGE("build instance fail.");
977                     return 0;
978                 }
979                 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator();
980                 if (generator == nullptr) {
981                     *errCode = HCF_INVALID_PARAMS;
982                     LOGE("build generator fail.");
983                     return 0;
984                 }
985                 HcfKeyPair *returnKeyPair = nullptr;
986                 HcfParamsSpec *params = nullptr;
987                 *errCode = generator->generateKeyPair(generator, params, &returnKeyPair);
988                 if (*errCode != HCF_SUCCESS) {
989                     LOGD("generate key pair fail.");
990                     return 0;
991                 }
992                 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair);
993                 if (keyPair == nullptr) {
994                     *errCode = HCF_ERR_MALLOC;
995                     HcfObjDestroy(returnKeyPair);
996                     LOGE("new key pair failed");
997                     return 0;
998                 }
999                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGenerateKeyPair end");
1000                 return keyPair->GetID();
1001             }
1002 
FFiOHOSAsyKeyGeneratorConvertKey(int64_t id,HcfBlob * pubKey,HcfBlob * priKey,int32_t * errCode)1003             int64_t FFiOHOSAsyKeyGeneratorConvertKey(int64_t id, HcfBlob *pubKey, HcfBlob *priKey, int32_t *errCode)
1004             {
1005                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertKey start");
1006                 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id);
1007                 if (!instance) {
1008                     *errCode = HCF_INVALID_PARAMS;
1009                     LOGE("build instance fail.");
1010                     return 0;
1011                 }
1012                 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator();
1013                 if (generator == nullptr) {
1014                     *errCode = HCF_INVALID_PARAMS;
1015                     LOGE("build generator fail.");
1016                     return 0;
1017                 }
1018                 HcfKeyPair *returnKeyPair = nullptr;
1019                 HcfParamsSpec *params = nullptr;
1020                 *errCode = generator->convertKey(generator, params, pubKey, priKey, &returnKeyPair);
1021                 if (*errCode != HCF_SUCCESS) {
1022                     LOGD("convert key fail.");
1023                     return 0;
1024                 }
1025                 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair);
1026                 if (keyPair == nullptr) {
1027                     *errCode = HCF_ERR_MALLOC;
1028                     HcfObjDestroy(returnKeyPair);
1029                     LOGE("new key pair failed");
1030                     return 0;
1031                 }
1032                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertKey end");
1033                 return keyPair->GetID();
1034             }
1035 
FFiOHOSAsyKeyGeneratorConvertPemKey(int64_t id,char * pubKey,char * priKey,int32_t * errCode)1036             int64_t FFiOHOSAsyKeyGeneratorConvertPemKey(int64_t id, char *pubKey, char *priKey, int32_t *errCode)
1037             {
1038                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertPemKey start");
1039                 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id);
1040                 if (!instance) {
1041                     *errCode = HCF_INVALID_PARAMS;
1042                     LOGE("build instance fail.");
1043                     return 0;
1044                 }
1045                 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator();
1046                 if (generator == nullptr) {
1047                     *errCode = HCF_INVALID_PARAMS;
1048                     LOGE("build generator fail.");
1049                     return 0;
1050                 }
1051                 HcfKeyPair *returnKeyPair = nullptr;
1052                 HcfParamsSpec *params = nullptr;
1053                 *errCode = generator->convertPemKey(generator, params, pubKey, priKey, &returnKeyPair);
1054                 if (*errCode != HCF_SUCCESS) {
1055                     LOGE("ConvertPemKey fail.");
1056                     return 0;
1057                 }
1058                 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair);
1059                 if (keyPair == nullptr) {
1060                     *errCode = HCF_ERR_MALLOC;
1061                     HcfObjDestroy(returnKeyPair);
1062                     LOGE("new key pair failed");
1063                     return 0;
1064                 }
1065                 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertPemKey end");
1066                 return keyPair->GetID();
1067             }
1068 
1069             //--------------------- asykeyspecgenerator
AsyKeyGeneratorBySpecConstructor(HcfAsyKeyParamsSpec * asyKeySpec,int32_t * errCode)1070             int64_t AsyKeyGeneratorBySpecConstructor(HcfAsyKeyParamsSpec *asyKeySpec, int32_t *errCode)
1071             {
1072                 HcfAsyKeyGeneratorBySpec *generator = nullptr;
1073                 *errCode = HcfAsyKeyGeneratorBySpecCreate(asyKeySpec, &generator);
1074                 if (*errCode != HCF_SUCCESS) {
1075                     *errCode = HCF_INVALID_PARAMS;
1076                     LOGE("create C generator by sepc fail.");
1077                     return 0;
1078                 }
1079                 auto instance = FFIData::Create<AsyKeyGeneratorBySpecImpl>(generator);
1080                 if (!instance) {
1081                     *errCode = HCF_ERR_MALLOC;
1082                     HcfObjDestroy(generator);
1083                     LOGE("new asy key generator by spec failed!");
1084                     return 0;
1085                 }
1086                 return instance->GetID();
1087             }
1088 
FFiOHOSAsyKeyGeneratorByDsaCommonSpec(HcfDsaCommParamsSpec * spec,int32_t * errCode)1089             int64_t FFiOHOSAsyKeyGeneratorByDsaCommonSpec(HcfDsaCommParamsSpec *spec, int32_t *errCode)
1090             {
1091                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaCommonSpec start");
1092                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1093                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1094                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaCommonSpec end");
1095                 return id;
1096             }
1097 
FFiOHOSAsyKeyGeneratorByDsaPubKeySpec(HcfDsaPubKeyParamsSpec * spec,int32_t * errCode)1098             int64_t FFiOHOSAsyKeyGeneratorByDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, int32_t *errCode)
1099             {
1100                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaPubKeySpec start");
1101                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1102                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1103                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaPubKeySpec end");
1104                 return id;
1105             }
1106 
FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec(HcfDsaKeyPairParamsSpec * spec,int32_t * errCode)1107             int64_t FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, int32_t *errCode)
1108             {
1109                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec start");
1110                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1111                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1112                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec end");
1113                 return id;
1114             }
1115 
FFiOHOSAsyKeyGeneratorByEccCommonSpec(HcfEccCommParamsSpec * spec,int32_t * errCode)1116             int64_t FFiOHOSAsyKeyGeneratorByEccCommonSpec(HcfEccCommParamsSpec *spec, int32_t *errCode)
1117             {
1118                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccCommonSpec start");
1119                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1120                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1121                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccCommonSpec end");
1122                 return id;
1123             }
1124 
FFiOHOSAsyKeyGeneratorByEccPriKeySpec(HcfEccPriKeyParamsSpec * spec,int32_t * errCode)1125             int64_t FFiOHOSAsyKeyGeneratorByEccPriKeySpec(HcfEccPriKeyParamsSpec *spec, int32_t *errCode)
1126             {
1127                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPriKeySpec start");
1128                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1129                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1130                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPriKeySpec end");
1131                 return id;
1132             }
1133 
FFiOHOSAsyKeyGeneratorByEccPubKeySpec(HcfEccPubKeyParamsSpec * spec,int32_t * errCode)1134             int64_t FFiOHOSAsyKeyGeneratorByEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, int32_t *errCode)
1135             {
1136                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPubKeySpec start");
1137                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1138                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1139                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPubKeySpec end");
1140                 return id;
1141             }
1142 
FFiOHOSAsyKeyGeneratorByEccKeyPairSpec(HcfEccKeyPairParamsSpec * spec,int32_t * errCode)1143             int64_t FFiOHOSAsyKeyGeneratorByEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, int32_t *errCode)
1144             {
1145                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccKeyPairSpec start");
1146                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1147                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1148                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccKeyPairSpec end");
1149                 return id;
1150             }
1151 
FFiOHOSAsyKeyGeneratorByRsaPubKeySpec(HcfRsaPubKeyParamsSpec * spec,int32_t * errCode)1152             int64_t FFiOHOSAsyKeyGeneratorByRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, int32_t *errCode)
1153             {
1154                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaPubKeySpec start");
1155                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1156                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1157                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaPubKeySpec end");
1158                 return id;
1159             }
1160 
FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec(HcfRsaKeyPairParamsSpec * spec,int32_t * errCode)1161             int64_t FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, int32_t *errCode)
1162             {
1163                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec start");
1164                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1165                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1166                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec end");
1167                 return id;
1168             }
1169 
FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec * spec,int32_t * errCode)1170             int64_t FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec *spec, int32_t *errCode)
1171             {
1172                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec start");
1173                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1174                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1175                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec end");
1176                 return id;
1177             }
1178 
FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec * spec,int32_t * errCode)1179             int64_t FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, int32_t *errCode)
1180             {
1181                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec start");
1182                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1183                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1184                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec end");
1185                 return id;
1186             }
1187 
FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec * spec,int32_t * errCode)1188             int64_t FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, int32_t *errCode)
1189             {
1190                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec start");
1191                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1192                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1193                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec end");
1194                 return id;
1195             }
1196 
FFiOHOSAsyKeyGeneratorByDhPriKeySpec(HcfDhPriKeyParamsSpec * spec,int32_t * errCode)1197             int64_t FFiOHOSAsyKeyGeneratorByDhPriKeySpec(HcfDhPriKeyParamsSpec *spec, int32_t *errCode)
1198             {
1199                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPriKeySpec start");
1200                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1201                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1202                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPriKeySpec end");
1203                 return id;
1204             }
1205 
FFiOHOSAsyKeyGeneratorByDhPubKeySpec(HcfDhPubKeyParamsSpec * spec,int32_t * errCode)1206             int64_t FFiOHOSAsyKeyGeneratorByDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, int32_t *errCode)
1207             {
1208                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPubKeySpec start");
1209                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1210                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1211                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPubKeySpec end");
1212                 return id;
1213             }
1214 
FFiOHOSAsyKeyGeneratorByDhKeyPairSpec(HcfDhKeyPairParamsSpec * spec,int32_t * errCode)1215             int64_t FFiOHOSAsyKeyGeneratorByDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, int32_t *errCode)
1216             {
1217                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhKeyPairSpec start");
1218                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1219                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1220                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhKeyPairSpecc end");
1221                 return id;
1222             }
1223 
FFiOHOSAsyKeyGeneratorByDhCommonSpec(HcfDhCommParamsSpec * spec,int32_t * errCode)1224             int64_t FFiOHOSAsyKeyGeneratorByDhCommonSpec(HcfDhCommParamsSpec *spec, int32_t *errCode)
1225             {
1226                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBytDhCommonSpec start");
1227                 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1228                 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode);
1229                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBytDhCommonSpec end");
1230                 return id;
1231             }
1232 
FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair(int64_t id,int32_t * errCode)1233             int64_t FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair(int64_t id, int32_t *errCode)
1234             {
1235                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair start");
1236                 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id);
1237                 if (!instance) {
1238                     *errCode = HCF_INVALID_PARAMS;
1239                     LOGE("build instance fail.");
1240                     return 0;
1241                 }
1242                 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec();
1243                 if (generator == nullptr) {
1244                     *errCode = HCF_INVALID_PARAMS;
1245                     LOGE("build generator fail.");
1246                     return 0;
1247                 }
1248                 HcfKeyPair *returnKeyPair = nullptr;
1249                 *errCode = generator->generateKeyPair(generator, &returnKeyPair);
1250                 if (*errCode != HCF_SUCCESS) {
1251                     LOGD("generate key pair fail.");
1252                     return 0;
1253                 }
1254                 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair);
1255                 if (keyPair == nullptr) {
1256                     *errCode = HCF_ERR_MALLOC;
1257                     HcfObjDestroy(returnKeyPair);
1258                     LOGE("new key pair failed");
1259                     return 0;
1260                 }
1261                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair end");
1262                 return keyPair->GetID();
1263             }
1264 
FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey(int64_t id,int32_t * errCode)1265             int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey(int64_t id, int32_t *errCode)
1266             {
1267                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey start");
1268                 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id);
1269                 if (!instance) {
1270                     *errCode = HCF_INVALID_PARAMS;
1271                     LOGE("build instance fail.");
1272                     return 0;
1273                 }
1274                 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec();
1275                 if (generator == nullptr) {
1276                     *errCode = HCF_INVALID_PARAMS;
1277                     LOGE("build generator fail.");
1278                     return 0;
1279                 }
1280                 HcfPriKey *returnPriKey = nullptr;
1281                 *errCode = generator->generatePriKey(generator, &returnPriKey);
1282                 if (*errCode != HCF_SUCCESS) {
1283                     LOGD("generate PriKey fail.");
1284                     return 0;
1285                 }
1286                 auto priKey = FFIData::Create<PriKeyImpl>(returnPriKey);
1287                 if (priKey == nullptr) {
1288                     *errCode = HCF_ERR_MALLOC;
1289                     HcfObjDestroy(returnPriKey);
1290                     LOGE("new pri key failed");
1291                     return 0;
1292                 }
1293                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey end");
1294                 return priKey->GetID();
1295             }
1296 
FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey(int64_t id,int32_t * errCode)1297             int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey(int64_t id, int32_t *errCode)
1298             {
1299                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey start");
1300                 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id);
1301                 if (!instance) {
1302                     *errCode = HCF_INVALID_PARAMS;
1303                     LOGE("build instance fail.");
1304                     return 0;
1305                 }
1306                 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec();
1307                 if (generator == nullptr) {
1308                     *errCode = HCF_INVALID_PARAMS;
1309                     LOGE("build generator fail.");
1310                     return 0;
1311                 }
1312                 HcfPubKey *returnPubKey = nullptr;
1313                 *errCode = generator->generatePubKey(generator, &returnPubKey);
1314                 if (*errCode != HCF_SUCCESS) {
1315                     LOGD("generate PubKey fail.");
1316                     return 0;
1317                 }
1318                 auto pubKey = FFIData::Create<PubKeyImpl>(returnPubKey);
1319                 if (pubKey == nullptr) {
1320                     *errCode = HCF_ERR_MALLOC;
1321                     HcfObjDestroy(returnPubKey);
1322                     LOGE("new pub key failed");
1323                     return 0;
1324                 }
1325                 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey end");
1326                 return pubKey->GetID();
1327             }
1328 
1329             //--------------------- prikey
FFiOHOSPriKeyGetEncoded(int64_t id,int32_t * errCode)1330             HcfBlob FFiOHOSPriKeyGetEncoded(int64_t id, int32_t *errCode)
1331             {
1332                 LOGD("[PriKey] FFiOHOSPriKeyGetEncoded start");
1333                 HcfBlob ret = { .data = nullptr, .len = 0 };
1334                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1335                 if (!instance) {
1336                     LOGE("[PriKey] FFiOHOSPriKeyGetEncoded failed to unwrap private key obj!");
1337                     *errCode = HCF_INVALID_PARAMS;
1338                     return ret;
1339                 }
1340                 HcfPriKey *priKey = instance->GetPriKey();
1341                 if (priKey == nullptr) {
1342                     LOGE("[PriKey] FFiOHOSPriKeyGetEncoded failed to get private key obj!");
1343                     *errCode = HCF_INVALID_PARAMS;
1344                     return ret;
1345                 }
1346                 *errCode = priKey->base.getEncoded(&priKey->base, &ret);
1347                 LOGD("[PriKey] FFiOHOSPriKeyGetEncoded end");
1348                 return ret;
1349             }
1350 
FFiOHOSPriKeyGetEncodedDer(int64_t id,char * format,int32_t * errCode)1351             HcfBlob FFiOHOSPriKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode)
1352             {
1353                 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedDer start");
1354                 HcfBlob ret = { .data = nullptr, .len = 0 };
1355                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1356                 if (!instance) {
1357                     LOGE("[PriKey] FFiOHOSPriKeyGetEncodedDer failed to unwrap private key obj!");
1358                     *errCode = HCF_INVALID_PARAMS;
1359                     return ret;
1360                 }
1361                 HcfPriKey *priKey = instance->GetPriKey();
1362                 if (priKey == nullptr) {
1363                     LOGE("[PriKey] FFiOHOSPriKeyGetEncodedDer failed to get private key obj!");
1364                     *errCode = HCF_INVALID_PARAMS;
1365                     return ret;
1366                 }
1367                 *errCode = priKey->getEncodedDer(priKey, format, &ret);
1368                 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedDer end");
1369                 return ret;
1370             }
1371 
FFiOHOSPriKeyGetEncodedPem(int64_t id,char * format,int32_t * errCode)1372             char *FFiOHOSPriKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode)
1373             {
1374                 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedPem start");
1375                 char *ret = nullptr;
1376                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1377                 if (!instance) {
1378                     LOGE("[PriKey] FFiOHOSPriKeyGetEncodedPem failed to unwrap private key obj!");
1379                     *errCode = HCF_INVALID_PARAMS;
1380                     return ret;
1381                 }
1382                 HcfPriKey *priKey = instance->GetPriKey();
1383                 if (priKey == nullptr) {
1384                     LOGE("[PriKey] FFiOHOSPriKeyGetEncodedPem failed to get private key obj!");
1385                     *errCode = HCF_INVALID_PARAMS;
1386                     return ret;
1387                 }
1388                 HcfParamsSpec *paramsSpec = nullptr;
1389                 *errCode = priKey->getEncodedPem(priKey, paramsSpec, format, &ret);
1390                 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedPem end");
1391                 return ret;
1392             }
1393 
FFiOHOSPriKeyClearMem(int64_t id)1394             int32_t FFiOHOSPriKeyClearMem(int64_t id)
1395             {
1396                 LOGD("[PriKey] FFiOHOSPriKeyClearMem start");
1397                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1398                 if (!instance) {
1399                     LOGE("[PriKey] FFiOHOSPriKeyClearMem failed to unwrap private key obj!");
1400                     return HCF_INVALID_PARAMS;
1401                 }
1402                 HcfPriKey *priKey = instance->GetPriKey();
1403                 if (priKey == nullptr) {
1404                     LOGE("[PriKey] FFiOHOSPriKeyClearMem failed to get private key obj!");
1405                     return HCF_INVALID_PARAMS;
1406                 }
1407                 priKey->clearMem(priKey);
1408                 LOGD("[PriKey] FFiOHOSPriKeyClearMem end");
1409                 return HCF_SUCCESS;
1410             }
1411 
FFiOHOSPriKeyGetAsyKeySpecByNum(int64_t id,int32_t itemType,int32_t * errCode)1412             int FFiOHOSPriKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode)
1413             {
1414                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start");
1415                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1416                 int ret = 0;
1417                 if (!instance) {
1418                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!");
1419                     *errCode = HCF_INVALID_PARAMS;
1420                     return ret;
1421                 }
1422                 HcfPriKey *priKey = instance->GetPriKey();
1423                 if (priKey == nullptr) {
1424                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!");
1425                     *errCode = HCF_INVALID_PARAMS;
1426                     return ret;
1427                 }
1428                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1429                 *errCode = priKey->getAsyKeySpecInt(priKey, item, &ret);
1430                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end");
1431                 return ret;
1432             }
1433 
FFiOHOSPriKeyGetAsyKeySpecByStr(int64_t id,int32_t itemType,int32_t * errCode)1434             char *FFiOHOSPriKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode)
1435             {
1436                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start");
1437                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1438                 char *ret = nullptr;
1439                 if (!instance) {
1440                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!");
1441                     *errCode = HCF_INVALID_PARAMS;
1442                     return ret;
1443                 }
1444                 HcfPriKey *priKey = instance->GetPriKey();
1445                 if (priKey == nullptr) {
1446                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!");
1447                     *errCode = HCF_INVALID_PARAMS;
1448                     return ret;
1449                 }
1450                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1451                 *errCode = priKey->getAsyKeySpecString(priKey, item, &ret);
1452                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end");
1453                 return ret;
1454             }
1455 
FFiOHOSPriKeyGetAsyKeySpecByBigInt(int64_t id,int32_t itemType,int32_t * errCode)1456             HcfBigInteger FFiOHOSPriKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode)
1457             {
1458                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start");
1459                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1460                 HcfBigInteger ret = { 0 };
1461                 if (!instance) {
1462                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!");
1463                     *errCode = HCF_INVALID_PARAMS;
1464                     return ret;
1465                 }
1466                 HcfPriKey *priKey = instance->GetPriKey();
1467                 if (priKey == nullptr) {
1468                     LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!");
1469                     *errCode = HCF_INVALID_PARAMS;
1470                     return ret;
1471                 }
1472                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1473                 *errCode = priKey->getAsyKeySpecBigInteger(priKey, item, &ret);
1474                 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end");
1475                 return ret;
1476             }
1477 
FfiOHOSPriKeyGetFormat(int64_t id,int32_t * errCode)1478             const char *FfiOHOSPriKeyGetFormat(int64_t id, int32_t* errCode)
1479             {
1480                 LOGD("[PriKey] GetFormat start");
1481                 auto instance = FFIData::GetData<PriKeyImpl>(id);
1482                 if (!instance) {
1483                     LOGE("[PriKey] instance not exist.");
1484                     *errCode = HCF_ERR_MALLOC;
1485                     return nullptr;
1486                 }
1487                 const char* res = instance->GetFormat(errCode);
1488                 LOGD("[PriKey] GetFormat success");
1489                 return res;
1490             }
1491 
1492             //--------------------- pubkey
FFiOHOSPubKeyGetEncoded(int64_t id,int32_t * errCode)1493             HcfBlob FFiOHOSPubKeyGetEncoded(int64_t id, int32_t *errCode)
1494             {
1495                 LOGD("[PubKey] FFiOHOSPubKeyGetEncoded start");
1496                 HcfBlob ret = { .data = nullptr, .len = 0 };
1497                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1498                 if (!instance) {
1499                     LOGE("[PubKey] FFiOHOSPubKeyGetEncoded failed to unwrap public key obj!");
1500                     *errCode = HCF_INVALID_PARAMS;
1501                     return ret;
1502                 }
1503                 HcfPubKey *pubKey = instance->GetPubKey();
1504                 if (pubKey == nullptr) {
1505                     LOGE("[PubKey] FFiOHOSPubKeyGetEncoded failed to get public key obj!");
1506                     *errCode = HCF_INVALID_PARAMS;
1507                     return ret;
1508                 }
1509                 *errCode = pubKey->base.getEncoded(&pubKey->base, &ret);
1510                 LOGD("[PubKey] FFiOHOSPubKeyGetEncoded end");
1511                 return ret;
1512             }
1513 
FFiOHOSPubKeyGetEncodedDer(int64_t id,char * format,int32_t * errCode)1514             HcfBlob FFiOHOSPubKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode)
1515             {
1516                 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedDer start");
1517                 HcfBlob ret = { .data = nullptr, .len = 0 };
1518                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1519                 if (!instance) {
1520                     LOGE("[PubKey] FFiOHOSPubKeyGetEncodedDer failed to unwrap public key obj!");
1521                     *errCode = HCF_INVALID_PARAMS;
1522                     return ret;
1523                 }
1524                 HcfPubKey *pubKey = instance->GetPubKey();
1525                 if (pubKey == nullptr) {
1526                     LOGE("[PubKey] FFiOHOSPubKeyGetEncodedDer failed to get public key obj!");
1527                     *errCode = HCF_INVALID_PARAMS;
1528                     return ret;
1529                 }
1530                 *errCode = pubKey->getEncodedDer(pubKey, format, &ret);
1531                 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedDer end");
1532                 return ret;
1533             }
1534 
FFiOHOSPubKeyGetEncodedPem(int64_t id,char * format,int32_t * errCode)1535             char *FFiOHOSPubKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode)
1536             {
1537                 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedPem start");
1538                 char *ret = nullptr;
1539                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1540                 if (!instance) {
1541                     LOGE("[PubKey] FFiOHOSPubKeyGetEncodedPem failed to unwrap public key obj!");
1542                     *errCode = HCF_INVALID_PARAMS;
1543                     return ret;
1544                 }
1545                 HcfPubKey *pubKey = instance->GetPubKey();
1546                 if (pubKey == nullptr) {
1547                     LOGE("[PubKey] FFiOHOSPubKeyGetEncodedPem failed to get public key obj!");
1548                     *errCode = HCF_INVALID_PARAMS;
1549                     return ret;
1550                 }
1551                 *errCode = pubKey->base.getEncodedPem(&pubKey->base, format, &ret);
1552                 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedPem end");
1553                 return ret;
1554             }
1555 
FFiOHOSPubKeyGetAsyKeySpecByNum(int64_t id,int32_t itemType,int32_t * errCode)1556             int FFiOHOSPubKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode)
1557             {
1558                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start");
1559                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1560                 int ret = 0;
1561                 if (!instance) {
1562                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!");
1563                     *errCode = HCF_INVALID_PARAMS;
1564                     return ret;
1565                 }
1566                 HcfPubKey *pubKey = instance->GetPubKey();
1567                 if (pubKey == nullptr) {
1568                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!");
1569                     *errCode = HCF_INVALID_PARAMS;
1570                     return ret;
1571                 }
1572                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1573                 *errCode = pubKey->getAsyKeySpecInt(pubKey, item, &ret);
1574                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end");
1575                 return ret;
1576             }
1577 
FFiOHOSPubKeyGetAsyKeySpecByStr(int64_t id,int32_t itemType,int32_t * errCode)1578             char *FFiOHOSPubKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode)
1579             {
1580                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start");
1581                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1582                 char *ret = nullptr;
1583                 if (!instance) {
1584                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!");
1585                     *errCode = HCF_INVALID_PARAMS;
1586                     return ret;
1587                 }
1588                 HcfPubKey *pubKey = instance->GetPubKey();
1589                 if (pubKey == nullptr) {
1590                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!");
1591                     *errCode = HCF_INVALID_PARAMS;
1592                     return ret;
1593                 }
1594                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1595                 *errCode = pubKey->getAsyKeySpecString(pubKey, item, &ret);
1596                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end");
1597                 return ret;
1598             }
1599 
FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id,int32_t itemType,int32_t * errCode)1600             HcfBigInteger FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode)
1601             {
1602                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start");
1603                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1604                 HcfBigInteger ret = { 0 };
1605                 if (!instance) {
1606                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!");
1607                     *errCode = HCF_INVALID_PARAMS;
1608                     return ret;
1609                 }
1610                 HcfPubKey *pubKey = instance->GetPubKey();
1611                 if (pubKey == nullptr) {
1612                     LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!");
1613                     *errCode = HCF_INVALID_PARAMS;
1614                     return ret;
1615                 }
1616                 AsyKeySpecItem item = AsyKeySpecItem(itemType);
1617                 *errCode = pubKey->getAsyKeySpecBigInteger(pubKey, item, &ret);
1618                 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end");
1619                 return ret;
1620             }
1621 
FfiOHOSPubKeyGetFormat(int64_t id,int32_t * errCode)1622             const char *FfiOHOSPubKeyGetFormat(int64_t id, int32_t* errCode)
1623             {
1624                 LOGD("[PubKey] GetFormat start");
1625                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1626                 if (!instance) {
1627                     LOGE("[PubKey] instance not exist.");
1628                     *errCode = HCF_ERR_MALLOC;
1629                     return nullptr;
1630                 }
1631                 const char* res = instance->GetFormat(errCode);
1632                 LOGD("[PubKey] GetFormat success");
1633                 return res;
1634             }
1635 
FfiOHOSPubKeyGetRawPointer(int64_t id)1636             void *FfiOHOSPubKeyGetRawPointer(int64_t id)
1637             {
1638                 LOGD("[PubKey] FfiOHOSPubKeyGetRawPointer start");
1639                 auto instance = FFIData::GetData<PubKeyImpl>(id);
1640                 if (!instance) {
1641                     LOGE("[PubKey] instance not exist.");
1642                     return nullptr;
1643                 }
1644                 HcfPubKey *key = instance->GetPubKey();
1645                 LOGD("[PubKey] FfiOHOSPubKeyGetRawPointer success");
1646                 return key;
1647             }
1648 
FfiOHOSPubKeyFromRawPointer(void * ptr,const char ** retString,int32_t * errCode)1649             int64_t FfiOHOSPubKeyFromRawPointer(void *ptr, const char **retString, int32_t *errCode)
1650             {
1651                 LOGD("[PubKey] FfiOHOSPubKeyFromRawPointer start");
1652                 HcfPubKey *pubKey = static_cast<HcfPubKey *>(ptr);
1653                 auto pub = FFIData::Create<PubKeyImpl>(pubKey);
1654                 if (pub == nullptr) {
1655                     *errCode = HCF_ERR_MALLOC;
1656                     LOGE("new pub key failed");
1657                     return 0;
1658                 }
1659                 *retString = pubKey->base.getAlgorithm(&pubKey->base);
1660                 LOGD("[PubKey] FfiOHOSPubKeyFromRawPointer success");
1661                 return pub->GetID();
1662             }
1663 
1664             // ------------------------------------keypair
FFiOHOSKeyPairPubKey(int64_t id,int32_t * errCode)1665             int64_t FFiOHOSKeyPairPubKey(int64_t id, int32_t *errCode)
1666             {
1667                 LOGD("[KeyPair] FFiOHOSKeyPairPubKey start");
1668                 auto instance = FFIData::GetData<KeyPairImpl>(id);
1669                 if (!instance) {
1670                     *errCode = HCF_INVALID_PARAMS;
1671                     LOGE("build instance fail.");
1672                     return 0;
1673                 }
1674                 HcfKeyPair *keyPair = instance->GetHcfKeyPair();
1675                 if (keyPair == nullptr) {
1676                     *errCode = HCF_INVALID_PARAMS;
1677                     LOGE("get keyPair fail.");
1678                     return 0;
1679                 }
1680                 HcfPubKey *pubKey = keyPair->pubKey;
1681                 if (pubKey == nullptr) {
1682                     *errCode = HCF_INVALID_PARAMS;
1683                     LOGE("get pubKey fail.");
1684                     return 0;
1685                 }
1686                 auto pub = FFIData::Create<PubKeyImpl>(pubKey);
1687                 if (pub == nullptr) {
1688                     *errCode = HCF_ERR_MALLOC;
1689                     LOGE("new pub key failed");
1690                     return 0;
1691                 }
1692                 LOGD("[KeyPair] FFiOHOSKeyPairPubKey end");
1693                 return pub->GetID();
1694             }
1695 
FFiOHOSKeyPairPriKey(int64_t id,int32_t * errCode)1696             int64_t FFiOHOSKeyPairPriKey(int64_t id, int32_t *errCode)
1697             {
1698                 LOGD("[KeyPair] FFiOHOSKeyPairPriKey start");
1699                 auto instance = FFIData::GetData<KeyPairImpl>(id);
1700                 if (!instance) {
1701                     *errCode = HCF_INVALID_PARAMS;
1702                     LOGE("build instance fail.");
1703                     return 0;
1704                 }
1705                 HcfKeyPair *keyPair = instance->GetHcfKeyPair();
1706                 if (keyPair == nullptr) {
1707                     *errCode = HCF_INVALID_PARAMS;
1708                     LOGE("get keyPair fail.");
1709                     return 0;
1710                 }
1711                 HcfPriKey *priKey = keyPair->priKey;
1712                 if (priKey == nullptr) {
1713                     *errCode = HCF_INVALID_PARAMS;
1714                     LOGE("get priKey fail.");
1715                     return 0;
1716                 }
1717                 auto pri = FFIData::Create<PriKeyImpl>(priKey);
1718                 if (pri == nullptr) {
1719                     *errCode = HCF_ERR_MALLOC;
1720                     LOGE("new pri key failed");
1721                     return 0;
1722                 }
1723                 LOGD("[KeyPair] FFiOHOSKeyPairPriKey end");
1724                 return pri->GetID();
1725             }
1726 
1727             // ------------------------------------kdf
FFiOHOSKdfConstructor(char * algName,int32_t * errCode)1728             int64_t FFiOHOSKdfConstructor(char *algName, int32_t *errCode)
1729             {
1730                 LOGD("[Kdf] FFiOHOSKdfConstructor start");
1731                 HcfKdf *kdf = nullptr;
1732                 *errCode = HcfKdfCreate(algName, &kdf);
1733                 if (*errCode != HCF_SUCCESS) {
1734                     *errCode = HCF_INVALID_PARAMS;
1735                     LOGE("create c kdf fail.");
1736                     return 0;
1737                 }
1738                 auto instance = FFIData::Create<KdfImpl>(kdf);
1739                 if (!instance) {
1740                     *errCode = HCF_ERR_MALLOC;
1741                     HcfObjDestroy(kdf);
1742                     LOGE("new kdf failed!");
1743                     return 0;
1744                 }
1745                 LOGD("[Kdf] FFiOHOSKdfConstructor end");
1746                 return instance->GetID();
1747             }
1748 
FFiOHOSKdfGenerateSecretByPB(int64_t id,HcfPBKDF2ParamsSpec * params)1749             int32_t FFiOHOSKdfGenerateSecretByPB(int64_t id, HcfPBKDF2ParamsSpec *params)
1750             {
1751                 LOGD("[Kdf] FiOHOSKdfGenerateSecretByPB start");
1752                 auto instance = FFIData::GetData<KdfImpl>(id);
1753                 if (!instance) {
1754                     LOGE("[PubKey] FiOHOSKdfGenerateSecretByPB failed to get kdf impl obj!");
1755                     return HCF_INVALID_PARAMS;
1756                 }
1757                 HcfKdfParamsSpec *tmp = reinterpret_cast<HcfKdfParamsSpec *>(params);
1758                 LOGD("[Kdf] FiOHOSKdfGenerateSecretByPB end");
1759                 return instance->GenerateSecret(tmp);
1760             }
1761 
FFiOHOSKdfGenerateSecretByH(int64_t id,HcfHkdfParamsSpec * params)1762             int32_t FFiOHOSKdfGenerateSecretByH(int64_t id, HcfHkdfParamsSpec *params)
1763             {
1764                 LOGD("[Kdf] FFiOHOSKdfGenerateSecretByH start");
1765                 auto instance = FFIData::GetData<KdfImpl>(id);
1766                 if (!instance) {
1767                     LOGE("[PubKey] F FFiOHOSKdfGenerateSecretByH failed to get kdf impl obj!");
1768                     return HCF_INVALID_PARAMS;
1769                 }
1770                 HcfKdfParamsSpec *tmp = reinterpret_cast<HcfKdfParamsSpec *>(params);
1771                 LOGD("[Kdf] FFiOHOSKdfGenerateSecretByH end");
1772                 return instance->GenerateSecret(tmp);
1773             }
1774 
1775             // --------------------------ecc_key_util
FFiOHOSECCKeyUtilGenECCCommonParamsSpec(char * curveName,int32_t * errCode)1776             HcfEccCommParamsSpec *FFiOHOSECCKeyUtilGenECCCommonParamsSpec(char *curveName, int32_t *errCode)
1777             {
1778                 return ECCKeyUtilImpl::GenECCCommonParamsSpec(curveName, errCode);
1779             }
1780 
FFiOHOSECCKeyUtilConvertPoint(char * curveName,HcfBlob encodedPoint,int32_t * errCode)1781             HcfPoint FFiOHOSECCKeyUtilConvertPoint(char *curveName, HcfBlob encodedPoint, int32_t *errCode)
1782             {
1783                 return ECCKeyUtilImpl::ConvertPoint(curveName, encodedPoint, errCode);
1784             }
1785 
FFiOHOSECCKeyUtilGetEncodedPoint(char * curveName,HcfPoint point,char * format,int32_t * errCode)1786             HcfBlob FFiOHOSECCKeyUtilGetEncodedPoint(char *curveName, HcfPoint point, char *format, int32_t *errCode)
1787             {
1788                 return ECCKeyUtilImpl::GetEncodedPoint(curveName, point, format, errCode);
1789             }
1790 
1791             // ---------------------------keyagreement
FFiOHOSKeyAgreementConstructor(char * algName,int32_t * errCode)1792             int64_t FFiOHOSKeyAgreementConstructor(char *algName, int32_t *errCode)
1793             {
1794                 LOGD("[KeyAgreement] FFiOHOSKdfConstructor start");
1795                 HcfKeyAgreement *keyAgreement = nullptr;
1796                 *errCode = HcfKeyAgreementCreate(algName, &keyAgreement);
1797                 if (*errCode != HCF_SUCCESS) {
1798                     *errCode = HCF_INVALID_PARAMS;
1799                     LOGE("create c keyAgreement fail.");
1800                     return 0;
1801                 }
1802                 auto instance = FFIData::Create<KeyAgreementImpl>(keyAgreement);
1803                 if (!instance) {
1804                     *errCode = HCF_ERR_MALLOC;
1805                     HcfObjDestroy(keyAgreement);
1806                     LOGE("new key agreement failed!");
1807                     return 0;
1808                 }
1809                 LOGD("[KeyAgreement] FFiOHOSKdfConstructor end");
1810                 return instance->GetID();
1811             }
1812 
FFiOHOSKeyAgreementGenerateSecret(int64_t id,int64_t priId,int64_t pubId,int32_t * errCode)1813             HcfBlob FFiOHOSKeyAgreementGenerateSecret(int64_t id, int64_t priId, int64_t pubId, int32_t *errCode)
1814             {
1815                 LOGD("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret start");
1816                 auto instance = FFIData::GetData<KeyAgreementImpl>(id);
1817                 HcfBlob blob = { 0 };
1818                 if (!instance) {
1819                     LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get key agreement obj!");
1820                     *errCode = HCF_INVALID_PARAMS;
1821                     return blob;
1822                 }
1823                 auto priKey = FFIData::GetData<PriKeyImpl>(priId);
1824                 if (!priKey) {
1825                     LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get priKey obj!");
1826                     *errCode = HCF_INVALID_PARAMS;
1827                     return blob;
1828                 }
1829                 auto pubKey = FFIData::GetData<PubKeyImpl>(pubId);
1830                 if (!pubKey) {
1831                     LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get priKey obj!");
1832                     *errCode = HCF_INVALID_PARAMS;
1833                     return blob;
1834                 }
1835                 LOGD("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret end");
1836                 return instance->GenerateSecret(priKey->GetPriKey(), pubKey->GetPubKey(), errCode);
1837             }
1838 
1839             // dh_key_util
FFiOHOSDHKeyUtilGenDHCommonParamsSpec(int32_t pLen,int32_t skLen,int32_t * errCode)1840             HcfDhCommParamsSpec *FFiOHOSDHKeyUtilGenDHCommonParamsSpec(int32_t pLen, int32_t skLen, int32_t *errCode)
1841             {
1842                 return DHKeyUtilImpl::GenDHCommonParamsSpec(pLen, skLen, errCode);
1843             }
1844 
1845             // sm2_crypto_util
FFiOHOSSm2CryptoUtilGenCipherTextBySpec(Sm2CipherTextSpec spec,char * mode,int32_t * errCode)1846             HcfBlob FFiOHOSSm2CryptoUtilGenCipherTextBySpec(Sm2CipherTextSpec spec, char *mode, int32_t *errCode)
1847             {
1848                 return Sm2CryptoUtilImpl::GenCipherTextBySpec(spec, mode, errCode);
1849             }
1850 
FFiOHOSSm2CryptoUtilGetCipherTextSpec(HcfBlob input,char * mode,int32_t * errCode)1851             Sm2CipherTextSpec *FFiOHOSSm2CryptoUtilGetCipherTextSpec(HcfBlob input, char *mode, int32_t *errCode)
1852             {
1853                 return Sm2CryptoUtilImpl::GetCipherTextSpec(input, mode, errCode);
1854             }
1855         }
1856     } // namespace CryptoFramework
1857 } // namespace OHOS