• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <stdbool.h>
20 #include "securec.h"
21 #include "bsl_sal.h"
22 #include "crypt_eal_pkey.h"
23 #include "crypt_eal_md.h"
24 #include "crypt_errno.h"
25 #include "eal_md_local.h"
26 #include "eal_pkey_local.h"
27 #include "crypt_eal_rand.h"
28 #include "crypt_algid.h"
29 #include "bsl_err_internal.h"
30 #include "eal_common.h"
31 #include "crypt_utils.h"
32 
CRYPT_EAL_PkeySignData(const CRYPT_EAL_PkeyCtx * pkey,const uint8_t * hash,uint32_t hashLen,uint8_t * sign,uint32_t * signLen)33 int32_t CRYPT_EAL_PkeySignData(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *hash,
34     uint32_t hashLen, uint8_t *sign, uint32_t *signLen)
35 {
36     if (pkey == NULL) {
37         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
38         return CRYPT_NULL_INPUT;
39     }
40     if (pkey->method == NULL || pkey->method->signData == NULL) {
41         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
42         return CRYPT_EAL_ALG_NOT_SUPPORT;
43     }
44 
45     if ((hash == NULL && hashLen != 0) || (hash != NULL && hashLen == 0)) {
46         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_INVALID_ARG);
47         return CRYPT_INVALID_ARG;
48     }
49 
50     int32_t ret = pkey->method->signData(pkey->key, hash, hashLen, sign, signLen);
51     EAL_EventReport((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_SIGN : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
52     return ret;
53 }
54 
CRYPT_EAL_PkeySign(const CRYPT_EAL_PkeyCtx * pkey,CRYPT_MD_AlgId id,const uint8_t * data,uint32_t dataLen,uint8_t * sign,uint32_t * signLen)55 int32_t CRYPT_EAL_PkeySign(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id,
56     const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen)
57 {
58     // 1. Check the input parameter
59     if (pkey == NULL) {
60         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
61         return CRYPT_NULL_INPUT;
62     }
63     if (pkey->method == NULL || pkey->method->sign == NULL) {
64         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
65         return CRYPT_EAL_ALG_NOT_SUPPORT;
66     }
67 
68     int32_t ret = pkey->method->sign(pkey->key, id, data, dataLen, sign, signLen);
69     EAL_EventReport((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_SIGN : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
70     return ret;
71 }
72 
73 
CRYPT_EAL_PkeyVerify(const CRYPT_EAL_PkeyCtx * pkey,CRYPT_MD_AlgId id,const uint8_t * data,uint32_t dataLen,const uint8_t * sign,uint32_t signLen)74 int32_t CRYPT_EAL_PkeyVerify(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id,
75     const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen)
76 {
77     // 1. Check the input parameter
78     if (pkey == NULL) {
79         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
80         return CRYPT_NULL_INPUT;
81     }
82     if (pkey->method == NULL || pkey->method->verify == NULL) {
83         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
84         return CRYPT_EAL_ALG_NOT_SUPPORT;
85     }
86 
87     // 2. Hash the plaintext data and verify the hash value.
88     int32_t ret = pkey->method->verify(pkey->key, id, data, dataLen, sign, signLen);
89     if (ret != CRYPT_SUCCESS) {
90         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
91         return ret;
92     }
93     EAL_EventReport(CRYPT_EVENT_VERIFY, CRYPT_ALGO_PKEY, pkey->id, ret);
94     return ret;
95 }
96 
CRYPT_EAL_PkeyVerifyData(const CRYPT_EAL_PkeyCtx * pkey,const uint8_t * hash,uint32_t hashLen,const uint8_t * sign,uint32_t signLen)97 int32_t CRYPT_EAL_PkeyVerifyData(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *hash,
98     uint32_t hashLen, const uint8_t *sign, uint32_t signLen)
99 {
100     if (pkey == NULL) {
101         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
102         return CRYPT_NULL_INPUT;
103     }
104     if (pkey->method == NULL || pkey->method->verifyData == NULL) {
105         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
106         return CRYPT_EAL_ALG_NOT_SUPPORT;
107     }
108 
109     if ((hash == NULL && hashLen != 0) || (hash != NULL && hashLen == 0)) {
110         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_INVALID_ARG);
111         return CRYPT_INVALID_ARG;
112     }
113     int32_t ret = pkey->method->verifyData(pkey->key, hash, hashLen, sign, signLen);
114     EAL_EventReport((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_VERIFY : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
115     return ret;
116 }
117 
CRYPT_EAL_PkeyBlind(CRYPT_EAL_PkeyCtx * pkey,CRYPT_MD_AlgId id,const uint8_t * input,uint32_t inputLen,uint8_t * out,uint32_t * outLen)118 int32_t CRYPT_EAL_PkeyBlind(CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id, const uint8_t *input, uint32_t inputLen,
119     uint8_t *out, uint32_t *outLen)
120 {
121     if (pkey == NULL) {
122         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
123         return CRYPT_NULL_INPUT;
124     }
125     if (pkey->method == NULL || pkey->method->blind == NULL) {
126         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
127         return CRYPT_EAL_ALG_NOT_SUPPORT;
128     }
129     int32_t ret = pkey->method->blind(pkey->key, id, input, inputLen, out, outLen);
130     EAL_EventReport((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_BLIND : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
131     return ret;
132 }
133 
CRYPT_EAL_PkeyUnBlind(CRYPT_EAL_PkeyCtx * pkey,const uint8_t * input,uint32_t inputLen,uint8_t * out,uint32_t * outLen)134 int32_t CRYPT_EAL_PkeyUnBlind(CRYPT_EAL_PkeyCtx *pkey, const uint8_t *input, uint32_t inputLen,
135     uint8_t *out, uint32_t *outLen)
136 {
137     if (pkey == NULL) {
138         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, CRYPT_PKEY_MAX, CRYPT_NULL_INPUT);
139         return CRYPT_NULL_INPUT;
140     }
141     if (pkey->method == NULL || pkey->method->unBlind == NULL) {
142         EAL_ERR_REPORT(CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, CRYPT_EAL_ALG_NOT_SUPPORT);
143         return CRYPT_EAL_ALG_NOT_SUPPORT;
144     }
145     int32_t ret = pkey->method->unBlind(pkey->key, input, inputLen, out, outLen);
146     EAL_EventReport((ret == CRYPT_SUCCESS) ? CRYPT_EVENT_UNBLIND : CRYPT_EVENT_ERR, CRYPT_ALGO_PKEY, pkey->id, ret);
147     return ret;
148 }
149 
150 #endif
151