1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <dlfcn.h> 17 #include <hdf_log.h> 18 19 #include "huks_hdi_passthrough_adapter.h" 20 #include "huks_sa_type.h" 21 #include "huks_sa_hdi_struct.h" 22 #include "huks_hdi_template.h" 23 24 25 typedef struct HuksHdi *(*HalCreateHandle)(void); 26 typedef void (*HalDestroyHandle)(struct HuksHdi *); 27 28 static struct HuksHdi *g_coreEngine = NULL; 29 static void *g_coreEngineHandle = NULL; 30 HuksHdiAdapterModuleInit(void)31 int32_t HuksHdiAdapterModuleInit(void) 32 { 33 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 34 35 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiModuleInit, HUKS_ERROR_NULL_POINTER, 36 "Module Init function is null pointer") 37 38 return g_coreEngine->HuksHdiModuleInit(); 39 } 40 HuksHdiAdapterModuleDestroy(void)41 int32_t HuksHdiAdapterModuleDestroy(void) 42 { 43 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 44 45 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiModuleDestroy, HUKS_ERROR_NULL_POINTER, 46 "Module Destroy function is null pointer") 47 48 return g_coreEngine->HuksHdiModuleDestroy(); 49 } 50 HuksHdiAdapterRefresh(void)51 int32_t HuksHdiAdapterRefresh(void) 52 { 53 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 54 55 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiRefresh, HUKS_ERROR_NULL_POINTER, 56 "Refresh function is null pointer") 57 58 return g_coreEngine->HuksHdiRefresh(); 59 } 60 HuksHdiAdapterGenerateKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSetIn,const struct HksBlob * keyIn,struct HksBlob * keyOut)61 int32_t HuksHdiAdapterGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, 62 const struct HksBlob *keyIn, struct HksBlob *keyOut) 63 { 64 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 65 66 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGenerateKey, HUKS_ERROR_NULL_POINTER, 67 "GenerateKey function is null pointer") 68 69 return g_coreEngine->HuksHdiGenerateKey(keyAlias, paramSetIn, keyIn, keyOut); 70 } 71 HuksHdiAdapterImportKey(const struct HksBlob * keyAlias,const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * keyOut)72 int32_t HuksHdiAdapterImportKey(const struct HksBlob *keyAlias, const struct HksBlob *key, 73 const struct HksParamSet *paramSet, struct HksBlob *keyOut) 74 { 75 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 76 77 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiImportKey, HUKS_ERROR_NULL_POINTER, 78 "ImportKey function is null pointer") 79 80 return g_coreEngine->HuksHdiImportKey(keyAlias, key, paramSet, keyOut); 81 } 82 HuksHdiAdapterImportWrappedKey(const struct HksBlob * wrappingKeyAlias,const struct HksBlob * wrappingKey,const struct HksBlob * wrappedKeyData,const struct HksParamSet * paramSet,struct HksBlob * keyOut)83 int32_t HuksHdiAdapterImportWrappedKey(const struct HksBlob *wrappingKeyAlias, const struct HksBlob *wrappingKey, 84 const struct HksBlob *wrappedKeyData, const struct HksParamSet *paramSet, struct HksBlob *keyOut) 85 { 86 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 87 88 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiImportWrappedKey, HUKS_ERROR_NULL_POINTER, 89 "ImportWrappedKey function is null pointer") 90 91 return g_coreEngine->HuksHdiImportWrappedKey(wrappingKeyAlias, wrappingKey, wrappedKeyData, paramSet, keyOut); 92 } 93 HuksHdiAdapterExportPublicKey(const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * keyOut)94 int32_t HuksHdiAdapterExportPublicKey(const struct HksBlob *key, const struct HksParamSet *paramSet, 95 struct HksBlob *keyOut) 96 { 97 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 98 99 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiExportPublicKey, HUKS_ERROR_NULL_POINTER, 100 "ExportPublicKey function is null pointer") 101 102 return g_coreEngine->HuksHdiExportPublicKey(key, paramSet, keyOut); 103 } 104 HuksHdiAdapterInit(const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * handle,struct HksBlob * token)105 int32_t HuksHdiAdapterInit(const struct HksBlob *key, const struct HksParamSet *paramSet, 106 struct HksBlob *handle, struct HksBlob *token) 107 { 108 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 109 110 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiInit, HUKS_ERROR_NULL_POINTER, 111 "Init function is null pointer") 112 113 return g_coreEngine->HuksHdiInit(key, paramSet, handle, token); 114 } 115 HuksHdiAdapterUpdate(const struct HksBlob * handle,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData)116 int32_t HuksHdiAdapterUpdate(const struct HksBlob *handle, const struct HksParamSet *paramSet, 117 const struct HksBlob *inData, struct HksBlob *outData) 118 { 119 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 120 121 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiUpdate, HUKS_ERROR_NULL_POINTER, 122 "Update function is null pointer") 123 124 return g_coreEngine->HuksHdiUpdate(handle, paramSet, inData, outData); 125 } 126 HuksHdiAdapterFinish(const struct HksBlob * handle,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData)127 int32_t HuksHdiAdapterFinish(const struct HksBlob *handle, const struct HksParamSet *paramSet, 128 const struct HksBlob *inData, struct HksBlob *outData) 129 { 130 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 131 132 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiFinish, HUKS_ERROR_NULL_POINTER, 133 "Finish function is null pointer") 134 135 return g_coreEngine->HuksHdiFinish(handle, paramSet, inData, outData); 136 } 137 HuksHdiAdapterAbort(const struct HksBlob * handle,const struct HksParamSet * paramSet)138 int32_t HuksHdiAdapterAbort(const struct HksBlob *handle, const struct HksParamSet *paramSet) 139 { 140 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 141 142 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAbort, HUKS_ERROR_NULL_POINTER, 143 "Abort function is null pointer") 144 145 return g_coreEngine->HuksHdiAbort(handle, paramSet); 146 } 147 HuksHdiAdapterGetKeyProperties(const struct HksParamSet * paramSet,const struct HksBlob * key)148 int32_t HuksHdiAdapterGetKeyProperties(const struct HksParamSet *paramSet, const struct HksBlob *key) 149 { 150 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 151 152 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGetKeyProperties, HUKS_ERROR_NULL_POINTER, 153 "GetKeyProperties function is null pointer") 154 155 return g_coreEngine->HuksHdiGetKeyProperties(paramSet, key); 156 } 157 HuksHdiAdapterSign(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * signature)158 int32_t HuksHdiAdapterSign(const struct HksBlob *key, const struct HksParamSet *paramSet, 159 const struct HksBlob *srcData, struct HksBlob *signature) 160 { 161 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 162 163 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiSign, HUKS_ERROR_NULL_POINTER, 164 "Sign function is null pointer") 165 166 return g_coreEngine->HuksHdiSign(key, paramSet, srcData, signature); 167 } 168 HuksHdiAdapterVerify(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,const struct HksBlob * signature)169 int32_t HuksHdiAdapterVerify(const struct HksBlob *key, const struct HksParamSet *paramSet, 170 const struct HksBlob *srcData, const struct HksBlob *signature) 171 { 172 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 173 174 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiVerify, HUKS_ERROR_NULL_POINTER, 175 "Verify function is null pointer") 176 177 return g_coreEngine->HuksHdiVerify(key, paramSet, srcData, signature); 178 } 179 HuksHdiAdapterEncrypt(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * plainText,struct HksBlob * cipherText)180 int32_t HuksHdiAdapterEncrypt(const struct HksBlob *key, const struct HksParamSet *paramSet, 181 const struct HksBlob *plainText, struct HksBlob *cipherText) 182 { 183 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 184 185 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiEncrypt, HUKS_ERROR_NULL_POINTER, 186 "Encrypt function is null pointer") 187 188 return g_coreEngine->HuksHdiEncrypt(key, paramSet, plainText, cipherText); 189 } 190 HuksHdiAdapterDecrypt(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * cipherText,struct HksBlob * plainText)191 int32_t HuksHdiAdapterDecrypt(const struct HksBlob *key, const struct HksParamSet *paramSet, 192 const struct HksBlob *cipherText, struct HksBlob *plainText) 193 { 194 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 195 196 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiDecrypt, HUKS_ERROR_NULL_POINTER, 197 "Decrypt function is null pointer") 198 199 return g_coreEngine->HuksHdiDecrypt(key, paramSet, cipherText, plainText); 200 } 201 HuksHdiAdapterAgreeKey(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)202 int32_t HuksHdiAdapterAgreeKey(const struct HksParamSet *paramSet, const struct HksBlob *privateKey, 203 const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey) 204 { 205 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 206 207 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAgreeKey, HUKS_ERROR_NULL_POINTER, 208 "AgreeKey function is null pointer") 209 210 return g_coreEngine->HuksHdiAgreeKey(paramSet, privateKey, peerPublicKey, agreedKey); 211 } 212 HuksHdiAdapterDeriveKey(const struct HksParamSet * paramSet,const struct HksBlob * kdfKey,struct HksBlob * derivedKey)213 int32_t HuksHdiAdapterDeriveKey(const struct HksParamSet *paramSet, const struct HksBlob *kdfKey, 214 struct HksBlob *derivedKey) 215 { 216 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 217 218 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiDeriveKey, HUKS_ERROR_NULL_POINTER, 219 "DeriveKey function is null pointer") 220 221 return g_coreEngine->HuksHdiDeriveKey(paramSet, kdfKey, derivedKey); 222 } 223 HuksHdiAdapterMac(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * mac)224 int32_t HuksHdiAdapterMac(const struct HksBlob *key, const struct HksParamSet *paramSet, 225 const struct HksBlob *srcData, struct HksBlob *mac) 226 { 227 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 228 229 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiMac, HUKS_ERROR_NULL_POINTER, 230 "Mac function is null pointer") 231 232 return g_coreEngine->HuksHdiMac(key, paramSet, srcData, mac); 233 } 234 HuksHdiAdapterUpgradeKey(const struct HksBlob * oldKey,const struct HksParamSet * paramSet,struct HksBlob * newKey)235 int32_t HuksHdiAdapterUpgradeKey(const struct HksBlob *oldKey, const struct HksParamSet *paramSet, 236 struct HksBlob *newKey) 237 { 238 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 239 240 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiUpgradeKey, HUKS_ERROR_NULL_POINTER, 241 "Change key owner function is null pointer") 242 243 return g_coreEngine->HuksHdiUpgradeKey(oldKey, paramSet, newKey); 244 } 245 HuksHdiAdapterAttestKey(const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * certChain)246 int32_t HuksHdiAdapterAttestKey(const struct HksBlob *key, const struct HksParamSet *paramSet, 247 struct HksBlob *certChain) 248 { 249 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 250 251 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiAttestKey, HUKS_ERROR_NULL_POINTER, 252 "AttestKey function is null pointer") 253 254 return g_coreEngine->HuksHdiAttestKey(key, paramSet, certChain); 255 } 256 HuksHdiAdapterGenerateRandom(const struct HksParamSet * paramSet,struct HksBlob * random)257 int32_t HuksHdiAdapterGenerateRandom(const struct HksParamSet *paramSet, struct HksBlob *random) 258 { 259 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 260 261 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiGenerateRandom, HUKS_ERROR_NULL_POINTER, 262 "GenerateRandom function is null pointer") 263 264 return g_coreEngine->HuksHdiGenerateRandom(paramSet, random); 265 } 266 HuksHdiAdapterExportChipsetPlatformPublicKey(const struct HksBlob * salt,enum HksChipsetPlatformDecryptScene scene,struct HksBlob * publicKey)267 int32_t HuksHdiAdapterExportChipsetPlatformPublicKey(const struct HksBlob *salt, 268 enum HksChipsetPlatformDecryptScene scene, struct HksBlob *publicKey) 269 { 270 HUKS_HDI_IF_NOT_SUCC_RETURN(HuksInitHuksCoreEngine(), HUKS_ERROR_NULL_POINTER) 271 272 HUKS_HDI_IF_NULL_LOGE_RETURN(g_coreEngine->HuksHdiExportChipsetPlatformPublicKey, HUKS_ERROR_NULL_POINTER, 273 "ExportChipsetPlatformPublicKey function is null pointer") 274 return g_coreEngine->HuksHdiExportChipsetPlatformPublicKey(salt, scene, publicKey); 275 } 276 HuksInitHuksCoreEngine(void)277 int32_t HuksInitHuksCoreEngine(void) 278 { 279 if (g_coreEngine != NULL) { 280 return HUKS_SUCCESS; 281 } 282 283 // libhuks_engine_core_standard is a software implementation version of huks driver, built-in system image 284 // by the source code at security_huks/services/huks_standard/huks_engine/main 285 g_coreEngineHandle = dlopen("libhuks_engine_core_standard.z.so", RTLD_NOW); 286 if (g_coreEngineHandle == NULL) { 287 HDF_LOGE("HUKS dlopen failed, %{public}s!", dlerror()); 288 return HUKS_ERROR_NULL_POINTER; 289 } 290 291 HalCreateHandle devicePtr = (HalCreateHandle)dlsym(g_coreEngineHandle, "HuksCreateHdiDevicePtr"); 292 if (devicePtr == NULL) { 293 HDF_LOGE("HUKS dlsym failed, %{public}s!", dlerror()); 294 dlclose(g_coreEngineHandle); 295 g_coreEngineHandle = NULL; 296 return HUKS_ERROR_NULL_POINTER; 297 } 298 299 g_coreEngine = (*devicePtr)(); 300 if (g_coreEngine == NULL) { 301 HDF_LOGE("HUKS coreEngine is NULL!"); 302 dlclose(g_coreEngineHandle); 303 g_coreEngineHandle = NULL; 304 return HUKS_ERROR_NULL_POINTER; 305 } 306 HDF_LOGI("HUKS HuksInitHuksCoreEngine init success!"); 307 return HUKS_SUCCESS; 308 } 309 HuksReleaseCoreEngine(void)310 int32_t HuksReleaseCoreEngine(void) 311 { 312 if (g_coreEngine == NULL) { 313 return HUKS_SUCCESS; 314 } 315 316 if (g_coreEngineHandle == NULL) { 317 HDF_LOGE("HUKS g_coreEngineHandle is NULL!"); 318 return HUKS_ERROR_NULL_POINTER; 319 } 320 321 HalDestroyHandle halDestroyHandle = (HalDestroyHandle)dlsym(g_coreEngineHandle, "HuksDestoryHdiDevicePtr"); 322 (*halDestroyHandle)(g_coreEngine); 323 g_coreEngine = NULL; 324 325 dlclose(g_coreEngineHandle); 326 g_coreEngineHandle = NULL; 327 return HUKS_SUCCESS; 328 } 329 HuksGetCoreEngine(void)330 struct HuksHdi *HuksGetCoreEngine(void) 331 { 332 return g_coreEngine; 333 } 334