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 "huks_adapter_diff_impl.h"
17 #include "hal_error.h"
18 #include "hc_dev_info.h"
19 #include "hc_file.h"
20 #include "hc_log.h"
21 #include "hks_api.h"
22 #include "hks_type.h"
23 #include "mbedtls_ec_adapter.h"
24
InitHks(void)25 int32_t InitHks(void)
26 {
27 LOGI("[HUKS]: HksInitialize enter.");
28 int32_t res = HksInitialize();
29 LOGI("[HUKS]: HksInitialize quit. [Res]: %" LOG_PUB "d.", res);
30 if (res == HKS_SUCCESS) {
31 return HAL_SUCCESS;
32 }
33
34 if ((res != HKS_ERROR_INVALID_KEY_FILE) && (res != HKS_ERROR_CRYPTO_ENGINE_ERROR) &&
35 (res != HKS_ERROR_UPDATE_ROOT_KEY_MATERIAL_FAIL)) {
36 LOGE("[HUKS]: HksInitialize fail. [Res]: %" LOG_PUB "d", res);
37 return HAL_ERR_INIT_FAILED;
38 }
39
40 LOGI("Start to delete local database file!");
41 HcFileRemove(GetStoragePath());
42 LOGI("Delete local database file successfully!");
43 LOGI("[HUKS]: HksRefreshKeyInfo enter.");
44 res = HksRefreshKeyInfo();
45 LOGI("[HUKS]: HksRefreshKeyInfo quit. [Res]: %" LOG_PUB "d", res);
46 if (res != HKS_SUCCESS) {
47 LOGE("[HUKS]: HksRefreshKeyInfo failed, res: %" LOG_PUB "d", res);
48 return HAL_ERR_INIT_FAILED;
49 }
50 LOGI("[HUKS]: HksInitialize enter.");
51 res = HksInitialize();
52 LOGI("[HUKS]: HksInitialize quit. [Res]: %" LOG_PUB "d", res);
53 if (res != HKS_SUCCESS) {
54 LOGE("[HUKS]: HksInitialize fail. [Res]: %" LOG_PUB "d", res);
55 return HAL_ERR_INIT_FAILED;
56 }
57
58 return HAL_SUCCESS;
59 }
60
HashToPointX25519(const Uint8Buff * hash,Uint8Buff * outEcPoint)61 int32_t HashToPointX25519(const Uint8Buff *hash, Uint8Buff *outEcPoint)
62 {
63 int32_t res = MbedtlsHashToPoint25519(hash, outEcPoint);
64 if (res != 0) {
65 LOGE("hashToPoint for x25519 failed, res: %" LOG_PUB "d", res);
66 return HAL_FAILED;
67 }
68
69 return HAL_SUCCESS;
70 }