1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <dlfcn.h> 17094332d3Sopenharmony_ci#include <hdf_log.h> 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci#include "huks_hdi_passthrough_adapter.h" 20094332d3Sopenharmony_ci#include "huks_sa_type.h" 21094332d3Sopenharmony_ci#include "huks_sa_hdi_struct.h" 22094332d3Sopenharmony_ci#include "huks_hdi_template.h" 23094332d3Sopenharmony_ci 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_citypedef struct HuksHdi *(*HalCreateHandle)(void); 26094332d3Sopenharmony_citypedef void (*HalDestroyHandle)(struct HuksHdi *); 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_cistatic struct HuksHdi *g_coreEngine = NULL; 29094332d3Sopenharmony_cistatic void *g_coreEngineHandle = NULL; 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_ciint32_t HuksHdiAdapterModuleInit(void) 32094332d3Sopenharmony_ci{ 33094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 34094332d3Sopenharmony_ci 35094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiModuleInit, HUKS_ERROR_NULL_POINTER, 36094332d3Sopenharmony_ci "Module Init function is null pointer") 37094332d3Sopenharmony_ci 38094332d3Sopenharmony_ci return g_coreEngine->HuksHdiModuleInit(); 39094332d3Sopenharmony_ci} 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_ciint32_t HuksHdiAdapterModuleDestroy(void) 42094332d3Sopenharmony_ci{ 43094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiModuleDestroy, HUKS_ERROR_NULL_POINTER, 46094332d3Sopenharmony_ci "Module Destroy function is null pointer") 47094332d3Sopenharmony_ci 48094332d3Sopenharmony_ci return g_coreEngine->HuksHdiModuleDestroy(); 49094332d3Sopenharmony_ci} 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_ciint32_t HuksHdiAdapterRefresh(void) 52094332d3Sopenharmony_ci{ 53094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 54094332d3Sopenharmony_ci 55094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiRefresh, HUKS_ERROR_NULL_POINTER, 56094332d3Sopenharmony_ci "Refresh function is null pointer") 57094332d3Sopenharmony_ci 58094332d3Sopenharmony_ci return g_coreEngine->HuksHdiRefresh(); 59094332d3Sopenharmony_ci} 60094332d3Sopenharmony_ci 61094332d3Sopenharmony_ciint32_t HuksHdiAdapterGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, 62094332d3Sopenharmony_ci const struct HksBlob *keyIn, struct HksBlob *keyOut) 63094332d3Sopenharmony_ci{ 64094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 65094332d3Sopenharmony_ci 66094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGenerateKey, HUKS_ERROR_NULL_POINTER, 67094332d3Sopenharmony_ci "GenerateKey function is null pointer") 68094332d3Sopenharmony_ci 69094332d3Sopenharmony_ci return g_coreEngine->HuksHdiGenerateKey(keyAlias, paramSetIn, keyIn, keyOut); 70094332d3Sopenharmony_ci} 71094332d3Sopenharmony_ci 72094332d3Sopenharmony_ciint32_t HuksHdiAdapterImportKey(const struct HksBlob *keyAlias, const struct HksBlob *key, 73094332d3Sopenharmony_ci const struct HksParamSet *paramSet, struct HksBlob *keyOut) 74094332d3Sopenharmony_ci{ 75094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiImportKey, HUKS_ERROR_NULL_POINTER, 78094332d3Sopenharmony_ci "ImportKey function is null pointer") 79094332d3Sopenharmony_ci 80094332d3Sopenharmony_ci return g_coreEngine->HuksHdiImportKey(keyAlias, key, paramSet, keyOut); 81094332d3Sopenharmony_ci} 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_ciint32_t HuksHdiAdapterImportWrappedKey(const struct HksBlob *wrappingKeyAlias, const struct HksBlob *wrappingKey, 84094332d3Sopenharmony_ci const struct HksBlob *wrappedKeyData, const struct HksParamSet *paramSet, struct HksBlob *keyOut) 85094332d3Sopenharmony_ci{ 86094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 87094332d3Sopenharmony_ci 88094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiImportWrappedKey, HUKS_ERROR_NULL_POINTER, 89094332d3Sopenharmony_ci "ImportWrappedKey function is null pointer") 90094332d3Sopenharmony_ci 91094332d3Sopenharmony_ci return g_coreEngine->HuksHdiImportWrappedKey(wrappingKeyAlias, wrappingKey, wrappedKeyData, paramSet, keyOut); 92094332d3Sopenharmony_ci} 93094332d3Sopenharmony_ci 94094332d3Sopenharmony_ciint32_t HuksHdiAdapterExportPublicKey(const struct HksBlob *key, const struct HksParamSet *paramSet, 95094332d3Sopenharmony_ci struct HksBlob *keyOut) 96094332d3Sopenharmony_ci{ 97094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 98094332d3Sopenharmony_ci 99094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiExportPublicKey, HUKS_ERROR_NULL_POINTER, 100094332d3Sopenharmony_ci "ExportPublicKey function is null pointer") 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci return g_coreEngine->HuksHdiExportPublicKey(key, paramSet, keyOut); 103094332d3Sopenharmony_ci} 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ciint32_t HuksHdiAdapterInit(const struct HksBlob *key, const struct HksParamSet *paramSet, 106094332d3Sopenharmony_ci struct HksBlob *handle, struct HksBlob *token) 107094332d3Sopenharmony_ci{ 108094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 109094332d3Sopenharmony_ci 110094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiInit, HUKS_ERROR_NULL_POINTER, 111094332d3Sopenharmony_ci "Init function is null pointer") 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_ci return g_coreEngine->HuksHdiInit(key, paramSet, handle, token); 114094332d3Sopenharmony_ci} 115094332d3Sopenharmony_ci 116094332d3Sopenharmony_ciint32_t HuksHdiAdapterUpdate(const struct HksBlob *handle, const struct HksParamSet *paramSet, 117094332d3Sopenharmony_ci const struct HksBlob *inData, struct HksBlob *outData) 118094332d3Sopenharmony_ci{ 119094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 120094332d3Sopenharmony_ci 121094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiUpdate, HUKS_ERROR_NULL_POINTER, 122094332d3Sopenharmony_ci "Update function is null pointer") 123094332d3Sopenharmony_ci 124094332d3Sopenharmony_ci return g_coreEngine->HuksHdiUpdate(handle, paramSet, inData, outData); 125094332d3Sopenharmony_ci} 126094332d3Sopenharmony_ci 127094332d3Sopenharmony_ciint32_t HuksHdiAdapterFinish(const struct HksBlob *handle, const struct HksParamSet *paramSet, 128094332d3Sopenharmony_ci const struct HksBlob *inData, struct HksBlob *outData) 129094332d3Sopenharmony_ci{ 130094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 131094332d3Sopenharmony_ci 132094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiFinish, HUKS_ERROR_NULL_POINTER, 133094332d3Sopenharmony_ci "Finish function is null pointer") 134094332d3Sopenharmony_ci 135094332d3Sopenharmony_ci return g_coreEngine->HuksHdiFinish(handle, paramSet, inData, outData); 136094332d3Sopenharmony_ci} 137094332d3Sopenharmony_ci 138094332d3Sopenharmony_ciint32_t HuksHdiAdapterAbort(const struct HksBlob *handle, const struct HksParamSet *paramSet) 139094332d3Sopenharmony_ci{ 140094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 141094332d3Sopenharmony_ci 142094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAbort, HUKS_ERROR_NULL_POINTER, 143094332d3Sopenharmony_ci "Abort function is null pointer") 144094332d3Sopenharmony_ci 145094332d3Sopenharmony_ci return g_coreEngine->HuksHdiAbort(handle, paramSet); 146094332d3Sopenharmony_ci} 147094332d3Sopenharmony_ci 148094332d3Sopenharmony_ciint32_t HuksHdiAdapterGetKeyProperties(const struct HksParamSet *paramSet, const struct HksBlob *key) 149094332d3Sopenharmony_ci{ 150094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 151094332d3Sopenharmony_ci 152094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGetKeyProperties, HUKS_ERROR_NULL_POINTER, 153094332d3Sopenharmony_ci "GetKeyProperties function is null pointer") 154094332d3Sopenharmony_ci 155094332d3Sopenharmony_ci return g_coreEngine->HuksHdiGetKeyProperties(paramSet, key); 156094332d3Sopenharmony_ci} 157094332d3Sopenharmony_ci 158094332d3Sopenharmony_ciint32_t HuksHdiAdapterSign(const struct HksBlob *key, const struct HksParamSet *paramSet, 159094332d3Sopenharmony_ci const struct HksBlob *srcData, struct HksBlob *signature) 160094332d3Sopenharmony_ci{ 161094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 162094332d3Sopenharmony_ci 163094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiSign, HUKS_ERROR_NULL_POINTER, 164094332d3Sopenharmony_ci "Sign function is null pointer") 165094332d3Sopenharmony_ci 166094332d3Sopenharmony_ci return g_coreEngine->HuksHdiSign(key, paramSet, srcData, signature); 167094332d3Sopenharmony_ci} 168094332d3Sopenharmony_ci 169094332d3Sopenharmony_ciint32_t HuksHdiAdapterVerify(const struct HksBlob *key, const struct HksParamSet *paramSet, 170094332d3Sopenharmony_ci const struct HksBlob *srcData, const struct HksBlob *signature) 171094332d3Sopenharmony_ci{ 172094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 173094332d3Sopenharmony_ci 174094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiVerify, HUKS_ERROR_NULL_POINTER, 175094332d3Sopenharmony_ci "Verify function is null pointer") 176094332d3Sopenharmony_ci 177094332d3Sopenharmony_ci return g_coreEngine->HuksHdiVerify(key, paramSet, srcData, signature); 178094332d3Sopenharmony_ci} 179094332d3Sopenharmony_ci 180094332d3Sopenharmony_ciint32_t HuksHdiAdapterEncrypt(const struct HksBlob *key, const struct HksParamSet *paramSet, 181094332d3Sopenharmony_ci const struct HksBlob *plainText, struct HksBlob *cipherText) 182094332d3Sopenharmony_ci{ 183094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 184094332d3Sopenharmony_ci 185094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiEncrypt, HUKS_ERROR_NULL_POINTER, 186094332d3Sopenharmony_ci "Encrypt function is null pointer") 187094332d3Sopenharmony_ci 188094332d3Sopenharmony_ci return g_coreEngine->HuksHdiEncrypt(key, paramSet, plainText, cipherText); 189094332d3Sopenharmony_ci} 190094332d3Sopenharmony_ci 191094332d3Sopenharmony_ciint32_t HuksHdiAdapterDecrypt(const struct HksBlob *key, const struct HksParamSet *paramSet, 192094332d3Sopenharmony_ci const struct HksBlob *cipherText, struct HksBlob *plainText) 193094332d3Sopenharmony_ci{ 194094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 195094332d3Sopenharmony_ci 196094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiDecrypt, HUKS_ERROR_NULL_POINTER, 197094332d3Sopenharmony_ci "Decrypt function is null pointer") 198094332d3Sopenharmony_ci 199094332d3Sopenharmony_ci return g_coreEngine->HuksHdiDecrypt(key, paramSet, cipherText, plainText); 200094332d3Sopenharmony_ci} 201094332d3Sopenharmony_ci 202094332d3Sopenharmony_ciint32_t HuksHdiAdapterAgreeKey(const struct HksParamSet *paramSet, const struct HksBlob *privateKey, 203094332d3Sopenharmony_ci const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey) 204094332d3Sopenharmony_ci{ 205094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 206094332d3Sopenharmony_ci 207094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAgreeKey, HUKS_ERROR_NULL_POINTER, 208094332d3Sopenharmony_ci "AgreeKey function is null pointer") 209094332d3Sopenharmony_ci 210094332d3Sopenharmony_ci return g_coreEngine->HuksHdiAgreeKey(paramSet, privateKey, peerPublicKey, agreedKey); 211094332d3Sopenharmony_ci} 212094332d3Sopenharmony_ci 213094332d3Sopenharmony_ciint32_t HuksHdiAdapterDeriveKey(const struct HksParamSet *paramSet, const struct HksBlob *kdfKey, 214094332d3Sopenharmony_ci struct HksBlob *derivedKey) 215094332d3Sopenharmony_ci{ 216094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 217094332d3Sopenharmony_ci 218094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiDeriveKey, HUKS_ERROR_NULL_POINTER, 219094332d3Sopenharmony_ci "DeriveKey function is null pointer") 220094332d3Sopenharmony_ci 221094332d3Sopenharmony_ci return g_coreEngine->HuksHdiDeriveKey(paramSet, kdfKey, derivedKey); 222094332d3Sopenharmony_ci} 223094332d3Sopenharmony_ci 224094332d3Sopenharmony_ciint32_t HuksHdiAdapterMac(const struct HksBlob *key, const struct HksParamSet *paramSet, 225094332d3Sopenharmony_ci const struct HksBlob *srcData, struct HksBlob *mac) 226094332d3Sopenharmony_ci{ 227094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 228094332d3Sopenharmony_ci 229094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiMac, HUKS_ERROR_NULL_POINTER, 230094332d3Sopenharmony_ci "Mac function is null pointer") 231094332d3Sopenharmony_ci 232094332d3Sopenharmony_ci return g_coreEngine->HuksHdiMac(key, paramSet, srcData, mac); 233094332d3Sopenharmony_ci} 234094332d3Sopenharmony_ci 235094332d3Sopenharmony_ciint32_t HuksHdiAdapterUpgradeKey(const struct HksBlob *oldKey, const struct HksParamSet *paramSet, 236094332d3Sopenharmony_ci struct HksBlob *newKey) 237094332d3Sopenharmony_ci{ 238094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 239094332d3Sopenharmony_ci 240094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiUpgradeKey, HUKS_ERROR_NULL_POINTER, 241094332d3Sopenharmony_ci "Change key owner function is null pointer") 242094332d3Sopenharmony_ci 243094332d3Sopenharmony_ci return g_coreEngine->HuksHdiUpgradeKey(oldKey, paramSet, newKey); 244094332d3Sopenharmony_ci} 245094332d3Sopenharmony_ci 246094332d3Sopenharmony_ciint32_t HuksHdiAdapterAttestKey(const struct HksBlob *key, const struct HksParamSet *paramSet, 247094332d3Sopenharmony_ci struct HksBlob *certChain) 248094332d3Sopenharmony_ci{ 249094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 250094332d3Sopenharmony_ci 251094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAttestKey, HUKS_ERROR_NULL_POINTER, 252094332d3Sopenharmony_ci "AttestKey function is null pointer") 253094332d3Sopenharmony_ci 254094332d3Sopenharmony_ci return g_coreEngine->HuksHdiAttestKey(key, paramSet, certChain); 255094332d3Sopenharmony_ci} 256094332d3Sopenharmony_ci 257094332d3Sopenharmony_ciint32_t HuksHdiAdapterGenerateRandom(const struct HksParamSet *paramSet, struct HksBlob *random) 258094332d3Sopenharmony_ci{ 259094332d3Sopenharmony_ci HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 260094332d3Sopenharmony_ci 261094332d3Sopenharmony_ci HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGenerateRandom, HUKS_ERROR_NULL_POINTER, 262094332d3Sopenharmony_ci "GenerateRandom function is null pointer") 263094332d3Sopenharmony_ci 264094332d3Sopenharmony_ci return g_coreEngine->HuksHdiGenerateRandom(paramSet, random); 265094332d3Sopenharmony_ci} 266094332d3Sopenharmony_ci 267094332d3Sopenharmony_ciint32_t HuksInitHuksCoreEngine(void) 268094332d3Sopenharmony_ci{ 269094332d3Sopenharmony_ci if (g_coreEngine != NULL) { 270094332d3Sopenharmony_ci return HUKS_SUCCESS; 271094332d3Sopenharmony_ci } 272094332d3Sopenharmony_ci 273094332d3Sopenharmony_ci // libhuks_engine_core_standard is a software implementation version of huks driver, built-in system image 274094332d3Sopenharmony_ci // by the source code at security_huks/services/huks_standard/huks_engine/main 275094332d3Sopenharmony_ci g_coreEngineHandle = dlopen("libhuks_engine_core_standard.z.so", RTLD_NOW); 276094332d3Sopenharmony_ci if (g_coreEngineHandle == NULL) { 277094332d3Sopenharmony_ci HDF_LOGE("HUKS dlopen failed, %{public}s!", dlerror()); 278094332d3Sopenharmony_ci return HUKS_ERROR_NULL_POINTER; 279094332d3Sopenharmony_ci } 280094332d3Sopenharmony_ci 281094332d3Sopenharmony_ci HalCreateHandle devicePtr = (HalCreateHandle)dlsym(g_coreEngineHandle, "HuksCreateHdiDevicePtr"); 282094332d3Sopenharmony_ci if (devicePtr == NULL) { 283094332d3Sopenharmony_ci HDF_LOGE("HUKS dlsym failed, %{public}s!", dlerror()); 284094332d3Sopenharmony_ci dlclose(g_coreEngineHandle); 285094332d3Sopenharmony_ci g_coreEngineHandle = NULL; 286094332d3Sopenharmony_ci return HUKS_ERROR_NULL_POINTER; 287094332d3Sopenharmony_ci } 288094332d3Sopenharmony_ci 289094332d3Sopenharmony_ci g_coreEngine = (*devicePtr)(); 290094332d3Sopenharmony_ci if (g_coreEngine == NULL) { 291094332d3Sopenharmony_ci HDF_LOGE("HUKS coreEngine is NULL!"); 292094332d3Sopenharmony_ci dlclose(g_coreEngineHandle); 293094332d3Sopenharmony_ci g_coreEngineHandle = NULL; 294094332d3Sopenharmony_ci return HUKS_ERROR_NULL_POINTER; 295094332d3Sopenharmony_ci } 296094332d3Sopenharmony_ci HDF_LOGI("HUKS HuksInitHuksCoreEngine init success!"); 297094332d3Sopenharmony_ci return HUKS_SUCCESS; 298094332d3Sopenharmony_ci} 299094332d3Sopenharmony_ci 300094332d3Sopenharmony_ciint32_t HuksReleaseCoreEngine(void) 301094332d3Sopenharmony_ci{ 302094332d3Sopenharmony_ci if (g_coreEngine == NULL) { 303094332d3Sopenharmony_ci return HUKS_SUCCESS; 304094332d3Sopenharmony_ci } 305094332d3Sopenharmony_ci 306094332d3Sopenharmony_ci if (g_coreEngineHandle == NULL) { 307094332d3Sopenharmony_ci HDF_LOGE("HUKS g_coreEngineHandle is NULL!"); 308094332d3Sopenharmony_ci return HUKS_ERROR_NULL_POINTER; 309094332d3Sopenharmony_ci } 310094332d3Sopenharmony_ci 311094332d3Sopenharmony_ci HalDestroyHandle halDestroyHandle = (HalDestroyHandle)dlsym(g_coreEngineHandle, "HuksDestoryHdiDevicePtr"); 312094332d3Sopenharmony_ci (*halDestroyHandle)(g_coreEngine); 313094332d3Sopenharmony_ci g_coreEngine = NULL; 314094332d3Sopenharmony_ci 315094332d3Sopenharmony_ci dlclose(g_coreEngineHandle); 316094332d3Sopenharmony_ci g_coreEngineHandle = NULL; 317094332d3Sopenharmony_ci return HUKS_SUCCESS; 318094332d3Sopenharmony_ci} 319094332d3Sopenharmony_ci 320094332d3Sopenharmony_cistruct HuksHdi *HuksGetCoreEngine(void) 321094332d3Sopenharmony_ci{ 322094332d3Sopenharmony_ci return g_coreEngine; 323094332d3Sopenharmony_ci} 324