1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include <dlfcn.h>
13 #include <securec.h>
14 #include <tee_log.h>
15 #include <tee_crypto_hal.h>
16 #include <crypto_hal_hmac.h>
17 #include <crypto_hal_hash.h>
18 #include <oemkey.h>
19 #include "crypto_inner_interface.h"
20
get_derived_key(uint8_t * tmp_key,uint8_t * priv,uint32_t priv_len)21 static int32_t get_derived_key(uint8_t *tmp_key, uint8_t *priv, uint32_t priv_len)
22 {
23 struct memref_t data_in = {0};
24 struct memref_t data_out = {0};
25 uint8_t derived_key[SHA256_LEN] = {0};
26 data_in.buffer = (uint64_t)(uintptr_t)tmp_key;
27 data_in.size = OEM_KEY_LEN;
28 data_out.buffer = (uint64_t)(uintptr_t)derived_key;
29 data_out.size = SHA256_LEN;
30 (void)tee_crypto_hash(CRYPTO_TYPE_DIGEST_SHA256, &data_in, &data_out, SOFT_CRYPTO);
31
32 errno_t ret_s = memcpy_s(priv, priv_len, derived_key, SHA256_LEN);
33 (void)memset_s(derived_key, SHA256_LEN, 0, SHA256_LEN);
34 if (ret_s != EOK)
35 return -1;
36
37 return 0;
38 }
39
get_class_ecc_key(uint8_t * priv,uint32_t priv_len)40 int32_t get_class_ecc_key(uint8_t *priv, uint32_t priv_len)
41 {
42 uint32_t ret;
43 uint8_t tmp_key[OEM_KEY_LEN] = {0};
44 bool check = (priv == NULL || priv_len != SHA256_LEN);
45 if (check) {
46 tloge("invalid parameters\n");
47 return -1;
48 }
49
50 ret = tee_hal_get_provision_key(tmp_key, OEM_KEY_LEN);
51 if (ret) {
52 tloge("get provision data failed\n");
53 return -1;
54 }
55
56 ret = (uint32_t)get_derived_key(tmp_key, priv, priv_len);
57 (void)memset_s(tmp_key, OEM_KEY_LEN, 0, OEM_KEY_LEN);
58 return (int32_t)ret;
59 }
60