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 25typedef struct HuksHdi *(*HalCreateHandle)(void); 26typedef void (*HalDestroyHandle)(struct HuksHdi *); 27 28static struct HuksHdi *g_coreEngine = NULL; 29static void *g_coreEngineHandle = NULL; 30 31int32_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 41int32_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 51int32_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 61int32_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 72int32_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 83int32_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 94int32_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 105int32_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 116int32_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 127int32_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 138int32_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 148int32_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 158int32_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 169int32_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 180int32_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 191int32_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 202int32_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 213int32_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 224int32_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 235int32_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 246int32_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 257int32_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 267int32_t HuksInitHuksCoreEngine(void) 268{ 269 if (g_coreEngine != NULL) { 270 return HUKS_SUCCESS; 271 } 272 273 // libhuks_engine_core_standard is a software implementation version of huks driver, built-in system image 274 // by the source code at security_huks/services/huks_standard/huks_engine/main 275 g_coreEngineHandle = dlopen("libhuks_engine_core_standard.z.so", RTLD_NOW); 276 if (g_coreEngineHandle == NULL) { 277 HDF_LOGE("HUKS dlopen failed, %{public}s!", dlerror()); 278 return HUKS_ERROR_NULL_POINTER; 279 } 280 281 HalCreateHandle devicePtr = (HalCreateHandle)dlsym(g_coreEngineHandle, "HuksCreateHdiDevicePtr"); 282 if (devicePtr == NULL) { 283 HDF_LOGE("HUKS dlsym failed, %{public}s!", dlerror()); 284 dlclose(g_coreEngineHandle); 285 g_coreEngineHandle = NULL; 286 return HUKS_ERROR_NULL_POINTER; 287 } 288 289 g_coreEngine = (*devicePtr)(); 290 if (g_coreEngine == NULL) { 291 HDF_LOGE("HUKS coreEngine is NULL!"); 292 dlclose(g_coreEngineHandle); 293 g_coreEngineHandle = NULL; 294 return HUKS_ERROR_NULL_POINTER; 295 } 296 HDF_LOGI("HUKS HuksInitHuksCoreEngine init success!"); 297 return HUKS_SUCCESS; 298} 299 300int32_t HuksReleaseCoreEngine(void) 301{ 302 if (g_coreEngine == NULL) { 303 return HUKS_SUCCESS; 304 } 305 306 if (g_coreEngineHandle == NULL) { 307 HDF_LOGE("HUKS g_coreEngineHandle is NULL!"); 308 return HUKS_ERROR_NULL_POINTER; 309 } 310 311 HalDestroyHandle halDestroyHandle = (HalDestroyHandle)dlsym(g_coreEngineHandle, "HuksDestoryHdiDevicePtr"); 312 (*halDestroyHandle)(g_coreEngine); 313 g_coreEngine = NULL; 314 315 dlclose(g_coreEngineHandle); 316 g_coreEngineHandle = NULL; 317 return HUKS_SUCCESS; 318} 319 320struct HuksHdi *HuksGetCoreEngine(void) 321{ 322 return g_coreEngine; 323} 324