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,char * returnString)555 int32_t FfiOHOSGetCipherSpecString(int64_t id, int32_t item, char *returnString) 556 { 557 LOGD("[Cipher] GetCipherSpecString start"); 558 auto instance = FFIData::GetData<CipherImpl>(id); 559 if (!instance) { 560 LOGE("[Cipher] instance not exist."); 561 return HCF_ERR_MALLOC; 562 } 563 CipherSpecItem csi = CipherSpecItem(item); 564 HcfResult res = instance->GetCipherSpecString(csi, returnString); 565 LOGD("[Cipher] GetCipherSpecString success"); 566 return res; 567 } 568 FfiOHOSGetCipherSpecUint8Array(int64_t id,int32_t item,HcfBlob * returnUint8Array)569 int32_t FfiOHOSGetCipherSpecUint8Array(int64_t id, int32_t item, HcfBlob *returnUint8Array) 570 { 571 LOGD("[Cipher] GetCipherSpecUint8Array start"); 572 auto instance = FFIData::GetData<CipherImpl>(id); 573 if (!instance) { 574 LOGE("[Cipher] instance not exist."); 575 return HCF_ERR_MALLOC; 576 } 577 CipherSpecItem csi = CipherSpecItem(item); 578 HcfResult res = instance->GetCipherSpecUint8Array(csi, returnUint8Array); 579 LOGD("[Cipher] GetCipherSpecUint8Array success"); 580 return res; 581 } 582 FfiOHOSCipherGetAlgName(int64_t id,int32_t * errCode)583 const char *FfiOHOSCipherGetAlgName(int64_t id, int32_t* errCode) 584 { 585 LOGD("[Cipher] GetAlgName start"); 586 auto instance = FFIData::GetData<CipherImpl>(id); 587 if (!instance) { 588 LOGE("[SymKey] instance not exist."); 589 *errCode = HCF_ERR_MALLOC; 590 return nullptr; 591 } 592 const char* res = instance->GetAlgorithm(errCode); 593 LOGD("[Cipher] GetAlgName success"); 594 return res; 595 } 596 597 //--------------------- mac FFiOHOSCryptoMacConstructor(char * algName,int32_t * errCode)598 int64_t FFiOHOSCryptoMacConstructor(char* algName, int32_t* errCode) 599 { 600 LOGD("[Mac] CreateMac start"); 601 HcfMac *macObj = nullptr; 602 HcfResult res = HcfMacCreate(algName, &macObj); 603 *errCode = static_cast<int32_t>(res); 604 if (res != HCF_SUCCESS) { 605 LOGE("create c macObj failed."); 606 return 0; 607 } 608 auto native = FFIData::Create<MacImpl>(macObj); 609 if (native == nullptr) { 610 LOGE("[Mac] CreateMac failed"); 611 HcfObjDestroy(macObj); 612 *errCode = HCF_ERR_MALLOC; 613 return 0; 614 } 615 LOGD("[Mac] CreateMac success"); 616 return native->GetID(); 617 } 618 FfiOHOSCryptoMacInit(int64_t id,int64_t symKeyId)619 int32_t FfiOHOSCryptoMacInit(int64_t id, int64_t symKeyId) 620 { 621 LOGD("[MAC] FfiOHOSCryptoMacInit start"); 622 auto instance = FFIData::GetData<MacImpl>(id); 623 if (!instance) { 624 LOGE("[MAC] MacImpl instance not exist."); 625 return HCF_ERR_MALLOC; 626 } 627 628 auto keyInstance = FFIData::GetData<SymKeyImpl>(symKeyId); 629 if (!instance) { 630 LOGE("[MAC] SymKeyImpl instance not exist."); 631 return HCF_ERR_MALLOC; 632 } 633 634 HcfResult res = instance->MacInit(keyInstance->GetSymKey()); 635 if (res != HCF_SUCCESS) { 636 LOGE("[MAC] MacInit error %{public}d", res); 637 } else { 638 LOGD("[MAC] FfiOHOSCryptoMacInit success"); 639 } 640 641 return res; 642 } 643 FfiOHOSCryptoMacUpdate(int64_t id,HcfBlob * input)644 int32_t FfiOHOSCryptoMacUpdate(int64_t id, HcfBlob *input) 645 { 646 LOGD("[Mac] FfiOHOSCryptoMacUpdate start"); 647 HcfResult res = HCF_ERR_MALLOC; 648 auto instance = FFIData::GetData<MacImpl>(id); 649 if (!instance) { 650 LOGE("[Mac] instance not exist."); 651 return res; 652 } 653 res = instance->MacUpdate(input); 654 LOGD("[Mac] FfiOHOSCryptoMacUpdate success"); 655 return res; 656 } 657 FfiOHOSCryptoMacDoFinal(int64_t id,int32_t * errCode)658 HcfBlob FfiOHOSCryptoMacDoFinal(int64_t id, int32_t* errCode) 659 { 660 LOGD("[Mac] FfiOHOSCryptoMacDoFinal start"); 661 auto instance = FFIData::GetData<MacImpl>(id); 662 HcfBlob blob = { .data = nullptr, .len = 0}; 663 if (!instance) { 664 LOGE("[Mac] instance not exist."); 665 *errCode = HCF_ERR_MALLOC; 666 return blob; 667 } 668 HcfResult res = instance->MacDoFinal(&blob); 669 *errCode = static_cast<int32_t>(res); 670 if (res != HCF_SUCCESS) { 671 LOGE("doFinal failed!"); 672 return blob; 673 } 674 LOGD("[Mac] FfiOHOSCryptoMacDoFinal success"); 675 return blob; 676 } 677 FfiOHOSCryptoGetMacLength(int64_t id)678 uint32_t FfiOHOSCryptoGetMacLength(int64_t id) 679 { 680 LOGD("[Mac] FfiOHOSGCryptoGetMacLength start"); 681 auto instance = FFIData::GetData<MacImpl>(id); 682 uint32_t res = 0; 683 if (!instance) { 684 LOGE("[Mac] instance not exist."); 685 return res; 686 } 687 res = instance->GetMacLength(); 688 LOGD("[Mac] FfiOHOSGCryptoGetMacLength success"); 689 return res; 690 } 691 692 //--------------------- sign FFiOHOSCryptoSignConstructor(char * algName,int32_t * errCode)693 int64_t FFiOHOSCryptoSignConstructor(char* algName, int32_t* errCode) 694 { 695 LOGD("[Sign] FFiOHOSCryptoSignConstructor start"); 696 HcfSign *signObj = nullptr; 697 HcfResult res = HcfSignCreate(algName, &signObj); 698 *errCode = static_cast<int32_t>(res); 699 if (res != HCF_SUCCESS) { 700 LOGE("[Sign] FFiOHOSCryptoSignConstructor create c signObj failed."); 701 return 0; 702 } 703 auto native = FFIData::Create<SignImpl>(signObj); 704 if (native == nullptr) { 705 LOGE("[Sign] FFiOHOSCryptoSignConstructor create failed"); 706 HcfObjDestroy(signObj); 707 *errCode = HCF_ERR_MALLOC; 708 return 0; 709 } 710 LOGD("[Sign] FFiOHOSCryptoSignConstructor success"); 711 return native->GetID(); 712 } 713 FFiOHOSSignInit(int64_t sid,int64_t pid)714 int32_t FFiOHOSSignInit(int64_t sid, int64_t pid) 715 { 716 LOGD("[Sign] FFiOHOSSignInit start"); 717 auto sign = FFIData::GetData<SignImpl>(sid); 718 if (sign == nullptr) { 719 LOGE("[Sign] FFiOHOSSignInit failed to get sign obj."); 720 return HCF_INVALID_PARAMS; 721 } 722 auto priKeyImpl = FFIData::GetData<PriKeyImpl>(pid); 723 if (priKeyImpl == nullptr) { 724 LOGE("[Sign] FFiOHOSSignInit failed to get priKeyImpl obj."); 725 return HCF_INVALID_PARAMS; 726 } 727 HcfPriKey *priKey = priKeyImpl->GetPriKey(); 728 if (priKey == nullptr) { 729 LOGE("[Sign] FFiOHOSSignInit failed to get priKey obj."); 730 return HCF_INVALID_PARAMS; 731 } 732 LOGD("[Sign] FFiOHOSSignInit success"); 733 return sign->Init(priKey); 734 } 735 FFiOHOSSignUpdate(int64_t id,HcfBlob input)736 int32_t FFiOHOSSignUpdate(int64_t id, HcfBlob input) 737 { 738 LOGD("[Sign] FFiOHOSSignUpdate start"); 739 auto sign = FFIData::GetData<SignImpl>(id); 740 if (sign == nullptr) { 741 LOGE("[Sign] FFiOHOSSignUpdate failed to get sign obj."); 742 return HCF_INVALID_PARAMS; 743 } 744 LOGD("[Sign] FFiOHOSSignUpdate success"); 745 return sign->Update(&input); 746 } 747 FFiOHOSSignSign(int64_t id,HcfBlob * input,HcfBlob * output)748 int32_t FFiOHOSSignSign(int64_t id, HcfBlob *input, HcfBlob *output) 749 { 750 LOGD("[Sign] FFiOHOSSignSign start"); 751 auto sign = FFIData::GetData<SignImpl>(id); 752 if (sign == nullptr) { 753 LOGE("[Sign] FFiOHOSSignSign failed to get sign obj."); 754 return HCF_INVALID_PARAMS; 755 } 756 LOGD("[Sign] FFiOHOSSignSign success"); 757 return sign->Sign(input, output); 758 } 759 FFiOHOSSignSetSignSpecByNum(int64_t id,int32_t itemValue)760 int32_t FFiOHOSSignSetSignSpecByNum(int64_t id, int32_t itemValue) 761 { 762 LOGD("[Sign] FFiOHOSSignSetSignSpecByNum start"); 763 auto sign = FFIData::GetData<SignImpl>(id); 764 if (sign == nullptr) { 765 LOGE("[Sign] FFiOHOSSignSetSignSpecByNum failed to get sign obj."); 766 return HCF_INVALID_PARAMS; 767 } 768 LOGD("[Sign] FFiOHOSSignSetSignSpecByNum success"); 769 return sign->SetSignSpecByNum(itemValue); 770 } 771 FFiOHOSSignSetSignSpecByArr(int64_t id,HcfBlob itemValue)772 int32_t FFiOHOSSignSetSignSpecByArr(int64_t id, HcfBlob itemValue) 773 { 774 LOGD("[Sign] FFiOHOSSignSetSignSpecByArr start"); 775 auto sign = FFIData::GetData<SignImpl>(id); 776 if (sign == nullptr) { 777 LOGE("[Sign] FFiOHOSSignSetSignSpecByArr failed to get sign obj."); 778 return HCF_INVALID_PARAMS; 779 } 780 LOGD("[Sign] FFiOHOSSignSetSignSpecByArr success"); 781 return sign->SetSignSpecByArr(itemValue); 782 } 783 FFiOHOSSignGetSignSpecString(int64_t id,SignSpecItem item,char * itemValue)784 int32_t FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, char *itemValue) 785 { 786 LOGD("[Sign] FFiOHOSSignGetSignSpecString start"); 787 auto sign = FFIData::GetData<SignImpl>(id); 788 if (sign == nullptr) { 789 LOGE("[Sign] FFiOHOSSignGetSignSpecString failed to get sign obj."); 790 return HCF_INVALID_PARAMS; 791 } 792 LOGD("[Sign] FFiOHOSSignGetSignSpecString success"); 793 return sign->GetSignSpecString(item, itemValue); 794 } 795 FFiOHOSSignGetSignSpecNum(int64_t id,SignSpecItem item,int32_t * itemValue)796 int32_t FFiOHOSSignGetSignSpecNum(int64_t id, SignSpecItem item, int32_t *itemValue) 797 { 798 LOGD("[Sign] FFiOHOSSignGetSignSpecNum start"); 799 auto sign = FFIData::GetData<SignImpl>(id); 800 if (sign == nullptr) { 801 LOGE("[Sign] FFiOHOSSignGetSignSpecNum failed to get sign obj."); 802 return HCF_INVALID_PARAMS; 803 } 804 LOGD("[Sign] FFiOHOSSignGetSignSpecNum success"); 805 return sign->GetSignSpecNum(item, itemValue); 806 } 807 808 //--------------------- verify FFiOHOSVerifyConstructor(char * algName,int32_t * errCode)809 int64_t FFiOHOSVerifyConstructor(char* algName, int32_t* errCode) 810 { 811 LOGD("[Verify]FFiOHOSVerifyConstructor start"); 812 HcfVerify *verify = nullptr; 813 HcfResult res = HcfVerifyCreate(algName, &verify); 814 *errCode = static_cast<int32_t>(res); 815 if (res != HCF_SUCCESS) { 816 LOGE("[Verify] FFiOHOSVerifyConstructor create c verifyObj failed."); 817 return 0; 818 } 819 auto native = FFIData::Create<VerifyImpl>(verify); 820 if (native == nullptr) { 821 LOGE("[Verify] FFiOHOSVerifyConstructor create failed"); 822 HcfObjDestroy(verify); 823 *errCode = HCF_ERR_MALLOC; 824 return 0; 825 } 826 LOGD("[Verify] FFiOHOSVerifyConstructor success"); 827 return native->GetID(); 828 } 829 FFiOHOSVerifyInit(int64_t sid,int64_t pid)830 int32_t FFiOHOSVerifyInit(int64_t sid, int64_t pid) 831 { 832 LOGD("[Verify] FFiOHOSVerifyInit start"); 833 auto verify = FFIData::GetData<VerifyImpl>(sid); 834 if (verify == nullptr) { 835 LOGE("[Verify] FFiOHOSVerifyInit failed to get verify obj."); 836 return HCF_INVALID_PARAMS; 837 } 838 auto pubKeyImpl = FFIData::GetData<PubKeyImpl>(pid); 839 if (pubKeyImpl == nullptr) { 840 LOGE("[Verify] FFiOHOSVerifyInit failed to get priKeyImpl obj."); 841 return HCF_INVALID_PARAMS; 842 } 843 HcfPubKey *pubKey = pubKeyImpl->GetPubKey(); 844 if (pubKey == nullptr) { 845 LOGE("[Verify] FFiOHOSVerifyInit failed to get priKey obj."); 846 return HCF_INVALID_PARAMS; 847 } 848 LOGD("[Verify] FFiOHOSVerifyInit success"); 849 return verify->Init(pubKey); 850 } 851 FFiOHOSVerifyUpdate(int64_t id,HcfBlob input)852 int32_t FFiOHOSVerifyUpdate(int64_t id, HcfBlob input) 853 { 854 LOGD("[Verify] FFiOHOSVerifyUpdate start"); 855 auto verify = FFIData::GetData<VerifyImpl>(id); 856 if (verify == nullptr) { 857 LOGE("[Verify] FFiOHOSVerifyUpdate failed to get verify obj."); 858 return HCF_INVALID_PARAMS; 859 } 860 LOGD("[Verify] FFiOHOSVerifyUpdate success"); 861 return verify->Update(&input); 862 } 863 FFiOHOSVerifyVerify(int64_t id,HcfBlob * data,HcfBlob signatureData,int32_t * errCode)864 bool FFiOHOSVerifyVerify(int64_t id, HcfBlob *data, HcfBlob signatureData, int32_t* errCode) 865 { 866 LOGD("[Verify] FFiOHOSVerifyVerify start"); 867 auto verify = FFIData::GetData<VerifyImpl>(id); 868 if (verify == nullptr) { 869 LOGE("[Verify] FFiOHOSVerifyVerify failed to get verify obj."); 870 return HCF_INVALID_PARAMS; 871 } 872 LOGD("[Verify] FFiOHOSVerifyVerify success"); 873 return verify->Verify(data, signatureData, errCode); 874 } 875 FFiOHOSVerifyRecover(int64_t id,HcfBlob input,HcfBlob * output)876 int32_t FFiOHOSVerifyRecover(int64_t id, HcfBlob input, HcfBlob *output) 877 { 878 LOGD("[Verify] FFiOHOSVerifyRecover start"); 879 auto verify = FFIData::GetData<VerifyImpl>(id); 880 if (verify == nullptr) { 881 LOGE("[Verify] FFiOHOSVerifyVerify failed to get verify obj."); 882 return HCF_INVALID_PARAMS; 883 } 884 LOGD("[Verify] FFiOHOSVerifyRecover success"); 885 return verify->Recover(input, output); 886 } 887 888 FFiOHOSVerifySetVerifySpecByNum(int64_t id,int32_t itemValue)889 int32_t FFiOHOSVerifySetVerifySpecByNum(int64_t id, int32_t itemValue) 890 { 891 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByNum start"); 892 auto verify = FFIData::GetData<VerifyImpl>(id); 893 if (verify == nullptr) { 894 LOGE("[Verify] FFiOHOSVerifySetVerifySpecByNum failed to get verify obj."); 895 return HCF_INVALID_PARAMS; 896 } 897 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByNum success"); 898 return verify->SetVerifySpecByNum(itemValue); 899 } 900 FFiOHOSVerifySetVerifySpecByArr(int64_t id,HcfBlob itemValue)901 int32_t FFiOHOSVerifySetVerifySpecByArr(int64_t id, HcfBlob itemValue) 902 { 903 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByArr start"); 904 auto verify = FFIData::GetData<VerifyImpl>(id); 905 if (verify == nullptr) { 906 LOGE("[Verify] FFiOHOSVerifySetVerifySpecByArr failed to get verify obj."); 907 return HCF_INVALID_PARAMS; 908 } 909 LOGD("[Verify] FFiOHOSVerifySetVerifySpecByArr success"); 910 return verify->SetVerifySpecByArr(itemValue); 911 } 912 FFiOHOSVerifyGetVerifySpecString(int64_t id,SignSpecItem item,char * itemValue)913 int32_t FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, char *itemValue) 914 { 915 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString start"); 916 auto verify = FFIData::GetData<VerifyImpl>(id); 917 if (verify == nullptr) { 918 LOGE("[Verify] FFiOHOSVerifyGetVerifySpecString failed to get verify obj."); 919 return HCF_INVALID_PARAMS; 920 } 921 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString success"); 922 return verify->GetVerifySpecString(item, itemValue); 923 } 924 FFiOHOSVerifyGetVerifySpecNum(int64_t id,SignSpecItem item,int32_t * itemValue)925 int32_t FFiOHOSVerifyGetVerifySpecNum(int64_t id, SignSpecItem item, int32_t *itemValue) 926 { 927 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecNum start"); 928 auto verify = FFIData::GetData<VerifyImpl>(id); 929 if (verify == nullptr) { 930 LOGE("[Verify] FFiOHOSVerifyGetVerifySpecNum failed to get verify obj."); 931 return HCF_INVALID_PARAMS; 932 } 933 LOGD("[Verify] FFiOHOSVerifyGetVerifySpecNum success"); 934 return verify->GetVerifySpecNum(item, itemValue); 935 } 936 937 //--------------------- asykeygenerator FFiOHOSAsyKeyGeneratorConstructor(char * algName,int32_t * errCode)938 int64_t FFiOHOSAsyKeyGeneratorConstructor(char *algName, int32_t *errCode) 939 { 940 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGeneratorConstructor start"); 941 HcfAsyKeyGenerator *generator = nullptr; 942 *errCode = HcfAsyKeyGeneratorCreate(algName, &generator); 943 if (*errCode != HCF_SUCCESS) { 944 *errCode = HCF_INVALID_PARAMS; 945 LOGE("create c generator fail."); 946 return 0; 947 } 948 auto instance = FFIData::Create<AsyKeyGeneratorImpl>(generator); 949 if (!instance) { 950 *errCode = HCF_ERR_MALLOC; 951 HcfObjDestroy(generator); 952 LOGE("new asy key generator failed"); 953 return 0; 954 } 955 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGeneratorConstructor end"); 956 return instance->GetID(); 957 } 958 FFiOHOSAsyKeyGeneratorGenerateKeyPair(int64_t id,int32_t * errCode)959 int64_t FFiOHOSAsyKeyGeneratorGenerateKeyPair(int64_t id, int32_t *errCode) 960 { 961 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGenerateKeyPair start"); 962 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id); 963 if (!instance) { 964 *errCode = HCF_INVALID_PARAMS; 965 LOGE("build instance fail."); 966 return 0; 967 } 968 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator(); 969 if (generator == nullptr) { 970 *errCode = HCF_INVALID_PARAMS; 971 LOGE("build generator fail."); 972 return 0; 973 } 974 HcfKeyPair *returnKeyPair = nullptr; 975 HcfParamsSpec *params = nullptr; 976 *errCode = generator->generateKeyPair(generator, params, &returnKeyPair); 977 if (*errCode != HCF_SUCCESS) { 978 LOGD("generate key pair fail."); 979 return 0; 980 } 981 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair); 982 if (keyPair == nullptr) { 983 *errCode = HCF_ERR_MALLOC; 984 HcfObjDestroy(returnKeyPair); 985 LOGE("new key pair failed"); 986 return 0; 987 } 988 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyGenerateKeyPair end"); 989 return keyPair->GetID(); 990 } 991 FFiOHOSAsyKeyGeneratorConvertKey(int64_t id,HcfBlob * pubKey,HcfBlob * priKey,int32_t * errCode)992 int64_t FFiOHOSAsyKeyGeneratorConvertKey(int64_t id, HcfBlob *pubKey, HcfBlob *priKey, int32_t *errCode) 993 { 994 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertKey start"); 995 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id); 996 if (!instance) { 997 *errCode = HCF_INVALID_PARAMS; 998 LOGE("build instance fail."); 999 return 0; 1000 } 1001 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator(); 1002 if (generator == nullptr) { 1003 *errCode = HCF_INVALID_PARAMS; 1004 LOGE("build generator fail."); 1005 return 0; 1006 } 1007 HcfKeyPair *returnKeyPair = nullptr; 1008 HcfParamsSpec *params = nullptr; 1009 *errCode = generator->convertKey(generator, params, pubKey, priKey, &returnKeyPair); 1010 if (*errCode != HCF_SUCCESS) { 1011 LOGD("convert key fail."); 1012 return 0; 1013 } 1014 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair); 1015 if (keyPair == nullptr) { 1016 *errCode = HCF_ERR_MALLOC; 1017 HcfObjDestroy(returnKeyPair); 1018 LOGE("new key pair failed"); 1019 return 0; 1020 } 1021 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertKey end"); 1022 return keyPair->GetID(); 1023 } 1024 FFiOHOSAsyKeyGeneratorConvertPemKey(int64_t id,char * pubKey,char * priKey,int32_t * errCode)1025 int64_t FFiOHOSAsyKeyGeneratorConvertPemKey(int64_t id, char *pubKey, char *priKey, int32_t *errCode) 1026 { 1027 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertPemKey start"); 1028 auto instance = FFIData::GetData<AsyKeyGeneratorImpl>(id); 1029 if (!instance) { 1030 *errCode = HCF_INVALID_PARAMS; 1031 LOGE("build instance fail."); 1032 return 0; 1033 } 1034 HcfAsyKeyGenerator *generator = instance->GetAsyKeyGenerator(); 1035 if (generator == nullptr) { 1036 *errCode = HCF_INVALID_PARAMS; 1037 LOGE("build generator fail."); 1038 return 0; 1039 } 1040 HcfKeyPair *returnKeyPair = nullptr; 1041 HcfParamsSpec *params = nullptr; 1042 *errCode = generator->convertPemKey(generator, params, pubKey, priKey, &returnKeyPair); 1043 if (*errCode != HCF_SUCCESS) { 1044 LOGE("ConvertPemKey fail."); 1045 return 0; 1046 } 1047 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair); 1048 if (keyPair == nullptr) { 1049 *errCode = HCF_ERR_MALLOC; 1050 HcfObjDestroy(returnKeyPair); 1051 LOGE("new key pair failed"); 1052 return 0; 1053 } 1054 LOGD("[AsyKeyGenerator] FFiOHOSAsyKeyConvertPemKey end"); 1055 return keyPair->GetID(); 1056 } 1057 1058 //--------------------- asykeyspecgenerator AsyKeyGeneratorBySpecConstructor(HcfAsyKeyParamsSpec * asyKeySpec,int32_t * errCode)1059 int64_t AsyKeyGeneratorBySpecConstructor(HcfAsyKeyParamsSpec *asyKeySpec, int32_t *errCode) 1060 { 1061 HcfAsyKeyGeneratorBySpec *generator = nullptr; 1062 *errCode = HcfAsyKeyGeneratorBySpecCreate(asyKeySpec, &generator); 1063 if (*errCode != HCF_SUCCESS) { 1064 *errCode = HCF_INVALID_PARAMS; 1065 LOGE("create C generator by sepc fail."); 1066 return 0; 1067 } 1068 auto instance = FFIData::Create<AsyKeyGeneratorBySpecImpl>(generator); 1069 if (!instance) { 1070 *errCode = HCF_ERR_MALLOC; 1071 HcfObjDestroy(generator); 1072 LOGE("new asy key generator by spec failed!"); 1073 return 0; 1074 } 1075 return instance->GetID(); 1076 } 1077 FFiOHOSAsyKeyGeneratorByDsaCommonSpec(HcfDsaCommParamsSpec * spec,int32_t * errCode)1078 int64_t FFiOHOSAsyKeyGeneratorByDsaCommonSpec(HcfDsaCommParamsSpec *spec, int32_t *errCode) 1079 { 1080 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaCommonSpec start"); 1081 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1082 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1083 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaCommonSpec end"); 1084 return id; 1085 } 1086 FFiOHOSAsyKeyGeneratorByDsaPubKeySpec(HcfDsaPubKeyParamsSpec * spec,int32_t * errCode)1087 int64_t FFiOHOSAsyKeyGeneratorByDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, int32_t *errCode) 1088 { 1089 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaPubKeySpec start"); 1090 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1091 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1092 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaPubKeySpec end"); 1093 return id; 1094 } 1095 FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec(HcfDsaKeyPairParamsSpec * spec,int32_t * errCode)1096 int64_t FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, int32_t *errCode) 1097 { 1098 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec start"); 1099 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1100 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1101 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec end"); 1102 return id; 1103 } 1104 FFiOHOSAsyKeyGeneratorByEccCommonSpec(HcfEccCommParamsSpec * spec,int32_t * errCode)1105 int64_t FFiOHOSAsyKeyGeneratorByEccCommonSpec(HcfEccCommParamsSpec *spec, int32_t *errCode) 1106 { 1107 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccCommonSpec start"); 1108 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1109 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1110 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccCommonSpec end"); 1111 return id; 1112 } 1113 FFiOHOSAsyKeyGeneratorByEccPriKeySpec(HcfEccPriKeyParamsSpec * spec,int32_t * errCode)1114 int64_t FFiOHOSAsyKeyGeneratorByEccPriKeySpec(HcfEccPriKeyParamsSpec *spec, int32_t *errCode) 1115 { 1116 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPriKeySpec start"); 1117 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1118 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1119 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPriKeySpec end"); 1120 return id; 1121 } 1122 FFiOHOSAsyKeyGeneratorByEccPubKeySpec(HcfEccPubKeyParamsSpec * spec,int32_t * errCode)1123 int64_t FFiOHOSAsyKeyGeneratorByEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, int32_t *errCode) 1124 { 1125 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPubKeySpec start"); 1126 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1127 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1128 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccPubKeySpec end"); 1129 return id; 1130 } 1131 FFiOHOSAsyKeyGeneratorByEccKeyPairSpec(HcfEccKeyPairParamsSpec * spec,int32_t * errCode)1132 int64_t FFiOHOSAsyKeyGeneratorByEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, int32_t *errCode) 1133 { 1134 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccKeyPairSpec start"); 1135 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1136 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1137 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByEccKeyPairSpec end"); 1138 return id; 1139 } 1140 FFiOHOSAsyKeyGeneratorByRsaPubKeySpec(HcfRsaPubKeyParamsSpec * spec,int32_t * errCode)1141 int64_t FFiOHOSAsyKeyGeneratorByRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, int32_t *errCode) 1142 { 1143 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaPubKeySpec start"); 1144 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1145 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1146 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaPubKeySpec end"); 1147 return id; 1148 } 1149 FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec(HcfRsaKeyPairParamsSpec * spec,int32_t * errCode)1150 int64_t FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, int32_t *errCode) 1151 { 1152 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec start"); 1153 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1154 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1155 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec end"); 1156 return id; 1157 } 1158 FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec * spec,int32_t * errCode)1159 int64_t FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec *spec, int32_t *errCode) 1160 { 1161 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec start"); 1162 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1163 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1164 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec end"); 1165 return id; 1166 } 1167 FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec * spec,int32_t * errCode)1168 int64_t FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, int32_t *errCode) 1169 { 1170 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec start"); 1171 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1172 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1173 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec end"); 1174 return id; 1175 } 1176 FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec * spec,int32_t * errCode)1177 int64_t FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, int32_t *errCode) 1178 { 1179 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec start"); 1180 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1181 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1182 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec end"); 1183 return id; 1184 } 1185 FFiOHOSAsyKeyGeneratorByDhPriKeySpec(HcfDhPriKeyParamsSpec * spec,int32_t * errCode)1186 int64_t FFiOHOSAsyKeyGeneratorByDhPriKeySpec(HcfDhPriKeyParamsSpec *spec, int32_t *errCode) 1187 { 1188 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPriKeySpec start"); 1189 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1190 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1191 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPriKeySpec end"); 1192 return id; 1193 } 1194 FFiOHOSAsyKeyGeneratorByDhPubKeySpec(HcfDhPubKeyParamsSpec * spec,int32_t * errCode)1195 int64_t FFiOHOSAsyKeyGeneratorByDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, int32_t *errCode) 1196 { 1197 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPubKeySpec start"); 1198 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1199 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1200 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhPubKeySpec end"); 1201 return id; 1202 } 1203 FFiOHOSAsyKeyGeneratorByDhKeyPairSpec(HcfDhKeyPairParamsSpec * spec,int32_t * errCode)1204 int64_t FFiOHOSAsyKeyGeneratorByDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, int32_t *errCode) 1205 { 1206 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhKeyPairSpec start"); 1207 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1208 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1209 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorByDhKeyPairSpecc end"); 1210 return id; 1211 } 1212 FFiOHOSAsyKeyGeneratorByDhCommonSpec(HcfDhCommParamsSpec * spec,int32_t * errCode)1213 int64_t FFiOHOSAsyKeyGeneratorByDhCommonSpec(HcfDhCommParamsSpec *spec, int32_t *errCode) 1214 { 1215 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBytDhCommonSpec start"); 1216 HcfAsyKeyParamsSpec *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec); 1217 int64_t id = AsyKeyGeneratorBySpecConstructor(asyKeySpec, errCode); 1218 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBytDhCommonSpec end"); 1219 return id; 1220 } 1221 FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair(int64_t id,int32_t * errCode)1222 int64_t FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair(int64_t id, int32_t *errCode) 1223 { 1224 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair start"); 1225 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id); 1226 if (!instance) { 1227 *errCode = HCF_INVALID_PARAMS; 1228 LOGE("build instance fail."); 1229 return 0; 1230 } 1231 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec(); 1232 if (generator == nullptr) { 1233 *errCode = HCF_INVALID_PARAMS; 1234 LOGE("build generator fail."); 1235 return 0; 1236 } 1237 HcfKeyPair *returnKeyPair = nullptr; 1238 *errCode = generator->generateKeyPair(generator, &returnKeyPair); 1239 if (*errCode != HCF_SUCCESS) { 1240 LOGD("generate key pair fail."); 1241 return 0; 1242 } 1243 auto keyPair = FFIData::Create<KeyPairImpl>(returnKeyPair); 1244 if (keyPair == nullptr) { 1245 *errCode = HCF_ERR_MALLOC; 1246 HcfObjDestroy(returnKeyPair); 1247 LOGE("new key pair failed"); 1248 return 0; 1249 } 1250 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair end"); 1251 return keyPair->GetID(); 1252 } 1253 FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey(int64_t id,int32_t * errCode)1254 int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey(int64_t id, int32_t *errCode) 1255 { 1256 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey start"); 1257 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id); 1258 if (!instance) { 1259 *errCode = HCF_INVALID_PARAMS; 1260 LOGE("build instance fail."); 1261 return 0; 1262 } 1263 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec(); 1264 if (generator == nullptr) { 1265 *errCode = HCF_INVALID_PARAMS; 1266 LOGE("build generator fail."); 1267 return 0; 1268 } 1269 HcfPriKey *returnPriKey = nullptr; 1270 *errCode = generator->generatePriKey(generator, &returnPriKey); 1271 if (*errCode != HCF_SUCCESS) { 1272 LOGD("generate PriKey fail."); 1273 return 0; 1274 } 1275 auto priKey = FFIData::Create<PriKeyImpl>(returnPriKey); 1276 if (priKey == nullptr) { 1277 *errCode = HCF_ERR_MALLOC; 1278 HcfObjDestroy(returnPriKey); 1279 LOGE("new pri key failed"); 1280 return 0; 1281 } 1282 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey end"); 1283 return priKey->GetID(); 1284 } 1285 FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey(int64_t id,int32_t * errCode)1286 int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey(int64_t id, int32_t *errCode) 1287 { 1288 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey start"); 1289 auto instance = FFIData::GetData<AsyKeyGeneratorBySpecImpl>(id); 1290 if (!instance) { 1291 *errCode = HCF_INVALID_PARAMS; 1292 LOGE("build instance fail."); 1293 return 0; 1294 } 1295 HcfAsyKeyGeneratorBySpec *generator = instance->GetAsyKeyGeneratorBySpec(); 1296 if (generator == nullptr) { 1297 *errCode = HCF_INVALID_PARAMS; 1298 LOGE("build generator fail."); 1299 return 0; 1300 } 1301 HcfPubKey *returnPubKey = nullptr; 1302 *errCode = generator->generatePubKey(generator, &returnPubKey); 1303 if (*errCode != HCF_SUCCESS) { 1304 LOGD("generate PubKey fail."); 1305 return 0; 1306 } 1307 auto pubKey = FFIData::Create<PubKeyImpl>(returnPubKey); 1308 if (pubKey == nullptr) { 1309 *errCode = HCF_ERR_MALLOC; 1310 HcfObjDestroy(returnPubKey); 1311 LOGE("new pub key failed"); 1312 return 0; 1313 } 1314 LOGD("[AsyKeyGeneratorBySpec] FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey end"); 1315 return pubKey->GetID(); 1316 } 1317 1318 //--------------------- prikey FFiOHOSPriKeyGetEncoded(int64_t id,int32_t * errCode)1319 HcfBlob FFiOHOSPriKeyGetEncoded(int64_t id, int32_t *errCode) 1320 { 1321 LOGD("[PriKey] FFiOHOSPriKeyGetEncoded start"); 1322 HcfBlob ret = { .data = nullptr, .len = 0 }; 1323 auto instance = FFIData::GetData<PriKeyImpl>(id); 1324 if (!instance) { 1325 LOGE("[PriKey] FFiOHOSPriKeyGetEncoded failed to unwrap private key obj!"); 1326 *errCode = HCF_INVALID_PARAMS; 1327 return ret; 1328 } 1329 HcfPriKey *priKey = instance->GetPriKey(); 1330 if (priKey == nullptr) { 1331 LOGE("[PriKey] FFiOHOSPriKeyGetEncoded failed to get private key obj!"); 1332 *errCode = HCF_INVALID_PARAMS; 1333 return ret; 1334 } 1335 *errCode = priKey->base.getEncoded(&priKey->base, &ret); 1336 LOGD("[PriKey] FFiOHOSPriKeyGetEncoded end"); 1337 return ret; 1338 } 1339 FFiOHOSPriKeyGetEncodedDer(int64_t id,char * format,int32_t * errCode)1340 HcfBlob FFiOHOSPriKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode) 1341 { 1342 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedDer start"); 1343 HcfBlob ret = { .data = nullptr, .len = 0 }; 1344 auto instance = FFIData::GetData<PriKeyImpl>(id); 1345 if (!instance) { 1346 LOGE("[PriKey] FFiOHOSPriKeyGetEncodedDer failed to unwrap private key obj!"); 1347 *errCode = HCF_INVALID_PARAMS; 1348 return ret; 1349 } 1350 HcfPriKey *priKey = instance->GetPriKey(); 1351 if (priKey == nullptr) { 1352 LOGE("[PriKey] FFiOHOSPriKeyGetEncodedDer failed to get private key obj!"); 1353 *errCode = HCF_INVALID_PARAMS; 1354 return ret; 1355 } 1356 *errCode = priKey->getEncodedDer(priKey, format, &ret); 1357 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedDer end"); 1358 return ret; 1359 } 1360 FFiOHOSPriKeyGetEncodedPem(int64_t id,char * format,int32_t * errCode)1361 char *FFiOHOSPriKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode) 1362 { 1363 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedPem start"); 1364 char *ret = nullptr; 1365 auto instance = FFIData::GetData<PriKeyImpl>(id); 1366 if (!instance) { 1367 LOGE("[PriKey] FFiOHOSPriKeyGetEncodedPem failed to unwrap private key obj!"); 1368 *errCode = HCF_INVALID_PARAMS; 1369 return ret; 1370 } 1371 HcfPriKey *priKey = instance->GetPriKey(); 1372 if (priKey == nullptr) { 1373 LOGE("[PriKey] FFiOHOSPriKeyGetEncodedPem failed to get private key obj!"); 1374 *errCode = HCF_INVALID_PARAMS; 1375 return ret; 1376 } 1377 *errCode = priKey->base.getEncodedPem(&priKey->base, format, &ret); 1378 LOGD("[PriKey] FFiOHOSPriKeyGetEncodedPem end"); 1379 return ret; 1380 } 1381 FFiOHOSPriKeyClearMem(int64_t id)1382 int32_t FFiOHOSPriKeyClearMem(int64_t id) 1383 { 1384 LOGD("[PriKey] FFiOHOSPriKeyClearMem start"); 1385 auto instance = FFIData::GetData<PriKeyImpl>(id); 1386 if (!instance) { 1387 LOGE("[PriKey] FFiOHOSPriKeyClearMem failed to unwrap private key obj!"); 1388 return HCF_INVALID_PARAMS; 1389 } 1390 HcfPriKey *priKey = instance->GetPriKey(); 1391 if (priKey == nullptr) { 1392 LOGE("[PriKey] FFiOHOSPriKeyClearMem failed to get private key obj!"); 1393 return HCF_INVALID_PARAMS; 1394 } 1395 priKey->clearMem(priKey); 1396 LOGD("[PriKey] FFiOHOSPriKeyClearMem end"); 1397 return HCF_SUCCESS; 1398 } 1399 FFiOHOSPriKeyGetAsyKeySpecByNum(int64_t id,int32_t itemType,int32_t * errCode)1400 int FFiOHOSPriKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode) 1401 { 1402 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start"); 1403 auto instance = FFIData::GetData<PriKeyImpl>(id); 1404 int ret = 0; 1405 if (!instance) { 1406 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!"); 1407 *errCode = HCF_INVALID_PARAMS; 1408 return ret; 1409 } 1410 HcfPriKey *priKey = instance->GetPriKey(); 1411 if (priKey == nullptr) { 1412 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!"); 1413 *errCode = HCF_INVALID_PARAMS; 1414 return ret; 1415 } 1416 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1417 *errCode = priKey->getAsyKeySpecInt(priKey, item, &ret); 1418 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end"); 1419 return ret; 1420 } 1421 FFiOHOSPriKeyGetAsyKeySpecByStr(int64_t id,int32_t itemType,int32_t * errCode)1422 char *FFiOHOSPriKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode) 1423 { 1424 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start"); 1425 auto instance = FFIData::GetData<PriKeyImpl>(id); 1426 char *ret = nullptr; 1427 if (!instance) { 1428 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!"); 1429 *errCode = HCF_INVALID_PARAMS; 1430 return ret; 1431 } 1432 HcfPriKey *priKey = instance->GetPriKey(); 1433 if (priKey == nullptr) { 1434 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!"); 1435 *errCode = HCF_INVALID_PARAMS; 1436 return ret; 1437 } 1438 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1439 *errCode = priKey->getAsyKeySpecString(priKey, item, &ret); 1440 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end"); 1441 return ret; 1442 } 1443 FFiOHOSPriKeyGetAsyKeySpecByBigInt(int64_t id,int32_t itemType,int32_t * errCode)1444 HcfBigInteger FFiOHOSPriKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode) 1445 { 1446 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec start"); 1447 auto instance = FFIData::GetData<PriKeyImpl>(id); 1448 HcfBigInteger ret = { 0 }; 1449 if (!instance) { 1450 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to unwrap private key obj!"); 1451 *errCode = HCF_INVALID_PARAMS; 1452 return ret; 1453 } 1454 HcfPriKey *priKey = instance->GetPriKey(); 1455 if (priKey == nullptr) { 1456 LOGE("[PriKey] FFiOHOSPriKeyGetAsyKeySpec failed to get private key obj!"); 1457 *errCode = HCF_INVALID_PARAMS; 1458 return ret; 1459 } 1460 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1461 *errCode = priKey->getAsyKeySpecBigInteger(priKey, item, &ret); 1462 LOGD("[PriKey] FFiOHOSPriKeyGetAsyKeySpec end"); 1463 return ret; 1464 } 1465 FfiOHOSPriKeyGetFormat(int64_t id,int32_t * errCode)1466 const char *FfiOHOSPriKeyGetFormat(int64_t id, int32_t* errCode) 1467 { 1468 LOGD("[PriKey] GetFormat start"); 1469 auto instance = FFIData::GetData<PriKeyImpl>(id); 1470 if (!instance) { 1471 LOGE("[PriKey] instance not exist."); 1472 *errCode = HCF_ERR_MALLOC; 1473 return nullptr; 1474 } 1475 const char* res = instance->GetFormat(errCode); 1476 LOGD("[PriKey] GetFormat success"); 1477 return res; 1478 } 1479 1480 //--------------------- pubkey FFiOHOSPubKeyGetEncoded(int64_t id,int32_t * errCode)1481 HcfBlob FFiOHOSPubKeyGetEncoded(int64_t id, int32_t *errCode) 1482 { 1483 LOGD("[PubKey] FFiOHOSPubKeyGetEncoded start"); 1484 HcfBlob ret = { .data = nullptr, .len = 0 }; 1485 auto instance = FFIData::GetData<PubKeyImpl>(id); 1486 if (!instance) { 1487 LOGE("[PubKey] FFiOHOSPubKeyGetEncoded failed to unwrap public key obj!"); 1488 *errCode = HCF_INVALID_PARAMS; 1489 return ret; 1490 } 1491 HcfPubKey *pubKey = instance->GetPubKey(); 1492 if (pubKey == nullptr) { 1493 LOGE("[PubKey] FFiOHOSPubKeyGetEncoded failed to get public key obj!"); 1494 *errCode = HCF_INVALID_PARAMS; 1495 return ret; 1496 } 1497 *errCode = pubKey->base.getEncoded(&pubKey->base, &ret); 1498 LOGD("[PubKey] FFiOHOSPubKeyGetEncoded end"); 1499 return ret; 1500 } 1501 FFiOHOSPubKeyGetEncodedDer(int64_t id,char * format,int32_t * errCode)1502 HcfBlob FFiOHOSPubKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode) 1503 { 1504 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedDer start"); 1505 HcfBlob ret = { .data = nullptr, .len = 0 }; 1506 auto instance = FFIData::GetData<PubKeyImpl>(id); 1507 if (!instance) { 1508 LOGE("[PubKey] FFiOHOSPubKeyGetEncodedDer failed to unwrap public key obj!"); 1509 *errCode = HCF_INVALID_PARAMS; 1510 return ret; 1511 } 1512 HcfPubKey *pubKey = instance->GetPubKey(); 1513 if (pubKey == nullptr) { 1514 LOGE("[PubKey] FFiOHOSPubKeyGetEncodedDer failed to get public key obj!"); 1515 *errCode = HCF_INVALID_PARAMS; 1516 return ret; 1517 } 1518 *errCode = pubKey->getEncodedDer(pubKey, format, &ret); 1519 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedDer end"); 1520 return ret; 1521 } 1522 FFiOHOSPubKeyGetEncodedPem(int64_t id,char * format,int32_t * errCode)1523 char *FFiOHOSPubKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode) 1524 { 1525 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedPem start"); 1526 char *ret = nullptr; 1527 auto instance = FFIData::GetData<PubKeyImpl>(id); 1528 if (!instance) { 1529 LOGE("[PubKey] FFiOHOSPubKeyGetEncodedPem failed to unwrap public key obj!"); 1530 *errCode = HCF_INVALID_PARAMS; 1531 return ret; 1532 } 1533 HcfPubKey *pubKey = instance->GetPubKey(); 1534 if (pubKey == nullptr) { 1535 LOGE("[PubKey] FFiOHOSPubKeyGetEncodedPem failed to get public key obj!"); 1536 *errCode = HCF_INVALID_PARAMS; 1537 return ret; 1538 } 1539 *errCode = pubKey->base.getEncodedPem(&pubKey->base, format, &ret); 1540 LOGD("[PubKey] FFiOHOSPubKeyGetEncodedPem end"); 1541 return ret; 1542 } 1543 FFiOHOSPubKeyGetAsyKeySpecByNum(int64_t id,int32_t itemType,int32_t * errCode)1544 int FFiOHOSPubKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode) 1545 { 1546 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start"); 1547 auto instance = FFIData::GetData<PubKeyImpl>(id); 1548 int ret = 0; 1549 if (!instance) { 1550 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!"); 1551 *errCode = HCF_INVALID_PARAMS; 1552 return ret; 1553 } 1554 HcfPubKey *pubKey = instance->GetPubKey(); 1555 if (pubKey == nullptr) { 1556 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!"); 1557 *errCode = HCF_INVALID_PARAMS; 1558 return ret; 1559 } 1560 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1561 *errCode = pubKey->getAsyKeySpecInt(pubKey, item, &ret); 1562 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end"); 1563 return ret; 1564 } 1565 FFiOHOSPubKeyGetAsyKeySpecByStr(int64_t id,int32_t itemType,int32_t * errCode)1566 char *FFiOHOSPubKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode) 1567 { 1568 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start"); 1569 auto instance = FFIData::GetData<PubKeyImpl>(id); 1570 char *ret = nullptr; 1571 if (!instance) { 1572 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!"); 1573 *errCode = HCF_INVALID_PARAMS; 1574 return ret; 1575 } 1576 HcfPubKey *pubKey = instance->GetPubKey(); 1577 if (pubKey == nullptr) { 1578 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!"); 1579 *errCode = HCF_INVALID_PARAMS; 1580 return ret; 1581 } 1582 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1583 *errCode = pubKey->getAsyKeySpecString(pubKey, item, &ret); 1584 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end"); 1585 return ret; 1586 } 1587 FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id,int32_t itemType,int32_t * errCode)1588 HcfBigInteger FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode) 1589 { 1590 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec start"); 1591 auto instance = FFIData::GetData<PubKeyImpl>(id); 1592 HcfBigInteger ret = { 0 }; 1593 if (!instance) { 1594 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to unwrap public key obj!"); 1595 *errCode = HCF_INVALID_PARAMS; 1596 return ret; 1597 } 1598 HcfPubKey *pubKey = instance->GetPubKey(); 1599 if (pubKey == nullptr) { 1600 LOGE("[PubKey] FFiOHOSPubKeyGetAsyKeySpec failed to get public key obj!"); 1601 *errCode = HCF_INVALID_PARAMS; 1602 return ret; 1603 } 1604 AsyKeySpecItem item = AsyKeySpecItem(itemType); 1605 *errCode = pubKey->getAsyKeySpecBigInteger(pubKey, item, &ret); 1606 LOGD("[PubKey] FFiOHOSPubKeyGetAsyKeySpec end"); 1607 return ret; 1608 } 1609 FfiOHOSPubKeyGetFormat(int64_t id,int32_t * errCode)1610 const char *FfiOHOSPubKeyGetFormat(int64_t id, int32_t* errCode) 1611 { 1612 LOGD("[PubKey] GetFormat start"); 1613 auto instance = FFIData::GetData<PubKeyImpl>(id); 1614 if (!instance) { 1615 LOGE("[PubKey] instance not exist."); 1616 *errCode = HCF_ERR_MALLOC; 1617 return nullptr; 1618 } 1619 const char* res = instance->GetFormat(errCode); 1620 LOGD("[PubKey] GetFormat success"); 1621 return res; 1622 } 1623 1624 // ------------------------------------keypair FFiOHOSKeyPairPubKey(int64_t id,int32_t * errCode)1625 int64_t FFiOHOSKeyPairPubKey(int64_t id, int32_t *errCode) 1626 { 1627 LOGD("[KeyPair] FFiOHOSKeyPairPubKey start"); 1628 auto instance = FFIData::GetData<KeyPairImpl>(id); 1629 if (!instance) { 1630 *errCode = HCF_INVALID_PARAMS; 1631 LOGE("build instance fail."); 1632 return 0; 1633 } 1634 HcfKeyPair *keyPair = instance->GetHcfKeyPair(); 1635 if (keyPair == nullptr) { 1636 *errCode = HCF_INVALID_PARAMS; 1637 LOGE("get keyPair fail."); 1638 return 0; 1639 } 1640 HcfPubKey *pubKey = keyPair->pubKey; 1641 if (pubKey == nullptr) { 1642 *errCode = HCF_INVALID_PARAMS; 1643 LOGE("get pubKey fail."); 1644 return 0; 1645 } 1646 auto pub = FFIData::Create<PubKeyImpl>(pubKey); 1647 if (pub == nullptr) { 1648 *errCode = HCF_ERR_MALLOC; 1649 LOGE("new pub key failed"); 1650 return 0; 1651 } 1652 LOGD("[KeyPair] FFiOHOSKeyPairPubKey end"); 1653 return pub->GetID(); 1654 } 1655 FFiOHOSKeyPairPriKey(int64_t id,int32_t * errCode)1656 int64_t FFiOHOSKeyPairPriKey(int64_t id, int32_t *errCode) 1657 { 1658 LOGD("[KeyPair] FFiOHOSKeyPairPriKey start"); 1659 auto instance = FFIData::GetData<KeyPairImpl>(id); 1660 if (!instance) { 1661 *errCode = HCF_INVALID_PARAMS; 1662 LOGE("build instance fail."); 1663 return 0; 1664 } 1665 HcfKeyPair *keyPair = instance->GetHcfKeyPair(); 1666 if (keyPair == nullptr) { 1667 *errCode = HCF_INVALID_PARAMS; 1668 LOGE("get keyPair fail."); 1669 return 0; 1670 } 1671 HcfPriKey *priKey = keyPair->priKey; 1672 if (priKey == nullptr) { 1673 *errCode = HCF_INVALID_PARAMS; 1674 LOGE("get priKey fail."); 1675 return 0; 1676 } 1677 auto pri = FFIData::Create<PriKeyImpl>(priKey); 1678 if (pri == nullptr) { 1679 *errCode = HCF_ERR_MALLOC; 1680 LOGE("new pri key failed"); 1681 return 0; 1682 } 1683 LOGD("[KeyPair] FFiOHOSKeyPairPriKey end"); 1684 return pri->GetID(); 1685 } 1686 1687 // ------------------------------------kdf FFiOHOSKdfConstructor(char * algName,int32_t * errCode)1688 int64_t FFiOHOSKdfConstructor(char *algName, int32_t *errCode) 1689 { 1690 LOGD("[Kdf] FFiOHOSKdfConstructor start"); 1691 HcfKdf *kdf = nullptr; 1692 *errCode = HcfKdfCreate(algName, &kdf); 1693 if (*errCode != HCF_SUCCESS) { 1694 *errCode = HCF_INVALID_PARAMS; 1695 LOGE("create c kdf fail."); 1696 return 0; 1697 } 1698 auto instance = FFIData::Create<KdfImpl>(kdf); 1699 if (!instance) { 1700 *errCode = HCF_ERR_MALLOC; 1701 HcfObjDestroy(kdf); 1702 LOGE("new kdf failed!"); 1703 return 0; 1704 } 1705 LOGD("[Kdf] FFiOHOSKdfConstructor end"); 1706 return instance->GetID(); 1707 } 1708 FFiOHOSKdfGenerateSecretByPB(int64_t id,HcfPBKDF2ParamsSpec * params)1709 int32_t FFiOHOSKdfGenerateSecretByPB(int64_t id, HcfPBKDF2ParamsSpec *params) 1710 { 1711 LOGD("[Kdf] FiOHOSKdfGenerateSecretByPB start"); 1712 auto instance = FFIData::GetData<KdfImpl>(id); 1713 if (!instance) { 1714 LOGE("[PubKey] FiOHOSKdfGenerateSecretByPB failed to get kdf impl obj!"); 1715 return HCF_INVALID_PARAMS; 1716 } 1717 HcfKdfParamsSpec *tmp = reinterpret_cast<HcfKdfParamsSpec *>(params); 1718 LOGD("[Kdf] FiOHOSKdfGenerateSecretByPB end"); 1719 return instance->GenerateSecret(tmp); 1720 } 1721 FFiOHOSKdfGenerateSecretByH(int64_t id,HcfHkdfParamsSpec * params)1722 int32_t FFiOHOSKdfGenerateSecretByH(int64_t id, HcfHkdfParamsSpec *params) 1723 { 1724 LOGD("[Kdf] FFiOHOSKdfGenerateSecretByH start"); 1725 auto instance = FFIData::GetData<KdfImpl>(id); 1726 if (!instance) { 1727 LOGE("[PubKey] F FFiOHOSKdfGenerateSecretByH failed to get kdf impl obj!"); 1728 return HCF_INVALID_PARAMS; 1729 } 1730 HcfKdfParamsSpec *tmp = reinterpret_cast<HcfKdfParamsSpec *>(params); 1731 LOGD("[Kdf] FFiOHOSKdfGenerateSecretByH end"); 1732 return instance->GenerateSecret(tmp); 1733 } 1734 1735 // --------------------------ecc_key_util FFiOHOSECCKeyUtilGenECCCommonParamsSpec(char * curveName,int32_t * errCode)1736 HcfEccCommParamsSpec *FFiOHOSECCKeyUtilGenECCCommonParamsSpec(char *curveName, int32_t *errCode) 1737 { 1738 return ECCKeyUtilImpl::GenECCCommonParamsSpec(curveName, errCode); 1739 } 1740 FFiOHOSECCKeyUtilConvertPoint(char * curveName,HcfBlob encodedPoint,int32_t * errCode)1741 HcfPoint FFiOHOSECCKeyUtilConvertPoint(char *curveName, HcfBlob encodedPoint, int32_t *errCode) 1742 { 1743 return ECCKeyUtilImpl::ConvertPoint(curveName, encodedPoint, errCode); 1744 } 1745 FFiOHOSECCKeyUtilGetEncodedPoint(char * curveName,HcfPoint point,char * format,int32_t * errCode)1746 HcfBlob FFiOHOSECCKeyUtilGetEncodedPoint(char *curveName, HcfPoint point, char *format, int32_t *errCode) 1747 { 1748 return ECCKeyUtilImpl::GetEncodedPoint(curveName, point, format, errCode); 1749 } 1750 1751 // ---------------------------keyagreement FFiOHOSKeyAgreementConstructor(char * algName,int32_t * errCode)1752 int64_t FFiOHOSKeyAgreementConstructor(char *algName, int32_t *errCode) 1753 { 1754 LOGD("[KeyAgreement] FFiOHOSKdfConstructor start"); 1755 HcfKeyAgreement *keyAgreement = nullptr; 1756 *errCode = HcfKeyAgreementCreate(algName, &keyAgreement); 1757 if (*errCode != HCF_SUCCESS) { 1758 *errCode = HCF_INVALID_PARAMS; 1759 LOGE("create c keyAgreement fail."); 1760 return 0; 1761 } 1762 auto instance = FFIData::Create<KeyAgreementImpl>(keyAgreement); 1763 if (!instance) { 1764 *errCode = HCF_ERR_MALLOC; 1765 HcfObjDestroy(keyAgreement); 1766 LOGE("new key agreement failed!"); 1767 return 0; 1768 } 1769 LOGD("[KeyAgreement] FFiOHOSKdfConstructor end"); 1770 return instance->GetID(); 1771 } 1772 FFiOHOSKeyAgreementGenerateSecret(int64_t id,int64_t priId,int64_t pubId,int32_t * errCode)1773 HcfBlob FFiOHOSKeyAgreementGenerateSecret(int64_t id, int64_t priId, int64_t pubId, int32_t *errCode) 1774 { 1775 LOGD("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret start"); 1776 auto instance = FFIData::GetData<KeyAgreementImpl>(id); 1777 HcfBlob blob = { 0 }; 1778 if (!instance) { 1779 LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get key agreement obj!"); 1780 *errCode = HCF_INVALID_PARAMS; 1781 return blob; 1782 } 1783 auto priKey = FFIData::GetData<PriKeyImpl>(priId); 1784 if (!priKey) { 1785 LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get priKey obj!"); 1786 *errCode = HCF_INVALID_PARAMS; 1787 return blob; 1788 } 1789 auto pubKey = FFIData::GetData<PubKeyImpl>(pubId); 1790 if (!pubKey) { 1791 LOGE("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret failed to get priKey obj!"); 1792 *errCode = HCF_INVALID_PARAMS; 1793 return blob; 1794 } 1795 LOGD("[KeyAgreement] FFiOHOSKeyAgreementGenerateSecret end"); 1796 return instance->GenerateSecret(priKey->GetPriKey(), pubKey->GetPubKey(), errCode); 1797 } 1798 1799 // dh_key_util FFiOHOSDHKeyUtilGenDHCommonParamsSpec(int32_t pLen,int32_t skLen,int32_t * errCode)1800 HcfDhCommParamsSpec *FFiOHOSDHKeyUtilGenDHCommonParamsSpec(int32_t pLen, int32_t skLen, int32_t *errCode) 1801 { 1802 return DHKeyUtilImpl::GenDHCommonParamsSpec(pLen, skLen, errCode); 1803 } 1804 1805 // sm2_crypto_util FFiOHOSSm2CryptoUtilGenCipherTextBySpec(Sm2CipherTextSpec spec,char * mode,int32_t * errCode)1806 HcfBlob FFiOHOSSm2CryptoUtilGenCipherTextBySpec(Sm2CipherTextSpec spec, char *mode, int32_t *errCode) 1807 { 1808 return Sm2CryptoUtilImpl::GenCipherTextBySpec(spec, mode, errCode); 1809 } 1810 FFiOHOSSm2CryptoUtilGetCipherTextSpec(HcfBlob input,char * mode,int32_t * errCode)1811 Sm2CipherTextSpec *FFiOHOSSm2CryptoUtilGetCipherTextSpec(HcfBlob input, char *mode, int32_t *errCode) 1812 { 1813 return Sm2CryptoUtilImpl::GetCipherTextSpec(input, mode, errCode); 1814 } 1815 } 1816 } // namespace CryptoFramework 1817 } // namespace OHOS