1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #if defined(HITLS_CRYPTO_EAL) && defined(HITLS_CRYPTO_PKEY)
18 #include "securec.h"
19 #include "eal_pkey_local.h"
20 #include "crypt_eal_pkey.h"
21 #include "crypt_errno.h"
22 #include "bsl_sal.h"
23 #include "bsl_err_internal.h"
24 #include "bsl_params.h"
25 #include "eal_common.h"
26 #include "crypt_eal_md.h"
27
CRYPT_EAL_PkeyEncapsInit(CRYPT_EAL_PkeyCtx * pkey,BSL_Param * params)28 int32_t CRYPT_EAL_PkeyEncapsInit(CRYPT_EAL_PkeyCtx *pkey, BSL_Param *params)
29 {
30 if (pkey == NULL) {
31 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
32 return CRYPT_NULL_INPUT;
33 }
34 int32_t ret = CRYPT_SUCCESS;
35 #ifdef HITLS_CRYPTO_PROVIDER
36 if (pkey->isProvider && pkey->method != NULL && pkey->method->encapsInit != NULL) {
37 ret = pkey->method->encapsInit(pkey->key, params);
38 if (ret != CRYPT_SUCCESS) {
39 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
40 }
41 }
42 #else
43 (void)params;
44 #endif
45 return ret;
46 }
47
CRYPT_EAL_PkeyDecapsInit(CRYPT_EAL_PkeyCtx * pkey,BSL_Param * params)48 int32_t CRYPT_EAL_PkeyDecapsInit(CRYPT_EAL_PkeyCtx *pkey, BSL_Param *params)
49 {
50 if (pkey == NULL) {
51 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
52 return CRYPT_NULL_INPUT;
53 }
54 int32_t ret = CRYPT_SUCCESS;
55 #ifdef HITLS_CRYPTO_PROVIDER
56 if (pkey->isProvider && pkey->method != NULL && pkey->method->decapsInit != NULL) {
57 ret = pkey->method->decapsInit(pkey->key, params);
58 if (ret != CRYPT_SUCCESS) {
59 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
60 }
61 }
62 #else
63 (void)params;
64 #endif
65 return ret;
66 }
67
CRYPT_EAL_PkeyEncaps(const CRYPT_EAL_PkeyCtx * pkey,uint8_t * cipher,uint32_t * cipherLen,uint8_t * sharekey,uint32_t * shareKeyLen)68 int32_t CRYPT_EAL_PkeyEncaps(const CRYPT_EAL_PkeyCtx *pkey, uint8_t *cipher, uint32_t *cipherLen, uint8_t *sharekey,
69 uint32_t *shareKeyLen)
70 {
71 if (pkey == NULL) {
72 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
73 return CRYPT_NULL_INPUT;
74 }
75 if (pkey->method == NULL || pkey->method->encaps == NULL) {
76 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
77 return CRYPT_EAL_ALG_NOT_SUPPORT;
78 }
79 int32_t ret = pkey->method->encaps(pkey->key, cipher, cipherLen, sharekey, shareKeyLen);
80 EAL_ERR_REPORT((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_ENCAPS : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
81 return ret;
82 }
83
CRYPT_EAL_PkeyDecaps(const CRYPT_EAL_PkeyCtx * pkey,uint8_t * cipher,uint32_t cipherLen,uint8_t * sharekey,uint32_t * shareKeyLen)84 int32_t CRYPT_EAL_PkeyDecaps(const CRYPT_EAL_PkeyCtx *pkey, uint8_t *cipher, uint32_t cipherLen, uint8_t *sharekey,
85 uint32_t *shareKeyLen)
86 {
87 if (pkey == NULL) {
88 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
89 return CRYPT_NULL_INPUT;
90 }
91 if (pkey->method == NULL || pkey->method->decaps == NULL) {
92 EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
93 return CRYPT_EAL_ALG_NOT_SUPPORT;
94 }
95 int32_t ret = pkey->method->decaps(pkey->key, cipher, cipherLen, sharekey, shareKeyLen);
96 EAL_ERR_REPORT((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_DECAPS : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
97 return ret;
98 }
99
100 #endif
101