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
13 #include "crypto_hal_derive_key.h"
14 #include <securec.h>
15 #include <tee_log.h>
16 #include <tee_crypto_hal.h>
17 #include "crypto_manager.h"
18 #include "soft_derive_key_api.h"
19
tee_crypto_dh_generate_key(const struct dh_key_t * dh_generate_key_data,struct memref_t * pub_key,struct memref_t * priv_key,uint32_t engine)20 int32_t tee_crypto_dh_generate_key(const struct dh_key_t *dh_generate_key_data,
21 struct memref_t *pub_key, struct memref_t *priv_key, uint32_t engine)
22 {
23 if ((dh_generate_key_data == NULL) || (pub_key == NULL) || (priv_key == NULL)) {
24 tloge("Invalid params\n");
25 return CRYPTO_BAD_PARAMETERS;
26 }
27 return crypto_driver_dh_generate_key(dh_generate_key_data, pub_key, priv_key, engine);
28 }
29
tee_crypto_dh_derive_key(const struct dh_key_t * dh_derive_key_data,struct memref_t * secret,uint32_t engine)30 int32_t tee_crypto_dh_derive_key(const struct dh_key_t *dh_derive_key_data, struct memref_t *secret, uint32_t engine)
31 {
32 if ((dh_derive_key_data == NULL) || (secret == NULL)) {
33 tloge("Invalid params\n");
34 return CRYPTO_BAD_PARAMETERS;
35 }
36 return crypto_driver_dh_derive_key(dh_derive_key_data, secret, engine);
37 }
38
tee_crypto_ecdh_derive_key(uint32_t alg_type,const struct ecc_pub_key_t * client_key,const struct ecc_priv_key_t * server_key,const struct asymmetric_params_t * ec_params,struct memref_t * secret,uint32_t engine)39 int32_t tee_crypto_ecdh_derive_key(uint32_t alg_type, const struct ecc_pub_key_t *client_key,
40 const struct ecc_priv_key_t *server_key, const struct asymmetric_params_t *ec_params,
41 struct memref_t *secret, uint32_t engine)
42 {
43 if ((client_key == NULL) || (server_key == NULL) || (secret == NULL)) {
44 tloge("Invalid params\n");
45 return CRYPTO_BAD_PARAMETERS;
46 }
47 if (engine == SOFT_CRYPTO)
48 return soft_crypto_ecdh_derive_key(alg_type, client_key, server_key, ec_params, secret);
49 return crypto_driver_ecdh_derive_key(alg_type, client_key, server_key, ec_params, secret, engine);
50 }
51
tee_crypto_derive_root_key(uint32_t derive_type,const struct memref_t * data_in,struct memref_t * data_out,uint32_t iter_num)52 int32_t tee_crypto_derive_root_key(uint32_t derive_type, const struct memref_t *data_in,
53 struct memref_t *data_out, uint32_t iter_num)
54 {
55 if ((data_in == NULL) || (data_out == NULL) || (iter_num == 0)) {
56 tloge("Invalid params\n");
57 return CRYPTO_BAD_PARAMETERS;
58 }
59 #if defined (CONFIG_NO_PLAT_ROOT_KEY)
60 (void)derive_type;
61 if (data_out->buffer == 0) {
62 tloge("data_out Invalid params\n");
63 return CRYPTO_BAD_PARAMETERS;
64 }
65 (void)memset_s((void *)(uintptr_t)data_out->buffer, data_out->size, 0xFF, data_out->size);
66 return CRYPTO_SUCCESS;
67 #else
68 return crypto_driver_derive_root_key(derive_type, data_in, data_out, iter_num);
69 #endif
70 }
71
tee_crypto_pbkdf2_derive_key(const struct memref_t * password,const struct memref_t * salt,uint32_t iterations,uint32_t digest_type,struct memref_t * data_out,uint32_t engine)72 int32_t tee_crypto_pbkdf2_derive_key(const struct memref_t *password, const struct memref_t *salt,
73 uint32_t iterations, uint32_t digest_type, struct memref_t *data_out, uint32_t engine)
74 {
75 if ((password == NULL) || (salt == NULL) || (data_out == NULL)) {
76 tloge("Invalid params\n");
77 return CRYPTO_BAD_PARAMETERS;
78 }
79 if (engine == SOFT_CRYPTO)
80 return soft_crypto_pbkdf2(password, salt, iterations, digest_type, data_out);
81 return crypto_driver_pbkdf2(password, salt, iterations, digest_type, data_out, engine);
82 }
83