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 <string.h>
17 #include "hitls_build.h"
18 #include "securec.h"
19 #include "tls_binlog_id.h"
20 #include "bsl_log_internal.h"
21 #include "bsl_log.h"
22 #include "bsl_err_internal.h"
23 #include "bsl_sal.h"
24 #include "hitls_error.h"
25 #include "transcript_hash.h"
26
VERIFY_SetHash(HITLS_Lib_Ctx * libCtx,const char * attrName,VerifyCtx * ctx,HITLS_HashAlgo hashAlgo)27 int32_t VERIFY_SetHash(HITLS_Lib_Ctx *libCtx, const char *attrName, VerifyCtx *ctx, HITLS_HashAlgo hashAlgo)
28 {
29 int32_t ret;
30 /* the value must be the same as the PRF function, use the digest algorithm with SHA-256 or higher strength */
31 HITLS_HashAlgo prfAlgo = (hashAlgo == HITLS_HASH_SHA1) ? HITLS_HASH_SHA_256 : hashAlgo;
32
33 ctx->hashCtx = SAL_CRYPT_DigestInit(libCtx, attrName, prfAlgo);
34 if (ctx->hashCtx == NULL) {
35 BSL_ERR_PUSH_ERROR(HITLS_CRYPT_ERR_DIGEST);
36 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15716, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
37 "Verify set hash error: digest init fail.", 0, 0, 0, 0);
38 return HITLS_CRYPT_ERR_DIGEST;
39 }
40
41 HsMsgCache *dataBuf = ctx->dataBuf;
42 while ((dataBuf != NULL) && (dataBuf->dataSize > 0u)) {
43 ret = SAL_CRYPT_DigestUpdate(ctx->hashCtx, dataBuf->data, dataBuf->dataSize);
44 if (ret != HITLS_SUCCESS) {
45 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15717, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
46 "Verify set hash error: digest update fail.", 0, 0, 0, 0);
47 SAL_CRYPT_DigestFree(ctx->hashCtx);
48 ctx->hashCtx = NULL;
49 return ret;
50 }
51 dataBuf = dataBuf->next;
52 }
53 ctx->hashAlgo = prfAlgo;
54 return HITLS_SUCCESS;
55 }
56
GetLastCache(HsMsgCache * dataBuf)57 static HsMsgCache *GetLastCache(HsMsgCache *dataBuf)
58 {
59 HsMsgCache *cacheBuf = dataBuf;
60 while (cacheBuf->next != NULL) {
61 cacheBuf = cacheBuf->next;
62 }
63 return cacheBuf;
64 }
65
CacheMsgData(HsMsgCache * dataBuf,const uint8_t * data,uint32_t len)66 static int32_t CacheMsgData(HsMsgCache *dataBuf, const uint8_t *data, uint32_t len)
67 {
68 HsMsgCache *lastCache = GetLastCache(dataBuf);
69
70 lastCache->next = BSL_SAL_Calloc(1u, sizeof(HsMsgCache));
71 if (lastCache->next == NULL) {
72 BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
73 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15718, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
74 "malloc HsMsgCache fail when append msg.", 0, 0, 0, 0);
75 return HITLS_MEMALLOC_FAIL;
76 }
77
78 BSL_SAL_FREE(lastCache->data);
79 lastCache->data = BSL_SAL_Dump(data, len);
80 if (lastCache->data == NULL) {
81 BSL_SAL_FREE(lastCache->next);
82 BSL_ERR_PUSH_ERROR(HITLS_MEMALLOC_FAIL);
83 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15719, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
84 "malloc HsMsgCache data fail when append msg.", 0, 0, 0, 0);
85 return HITLS_MEMALLOC_FAIL;
86 }
87 lastCache->dataSize = len;
88
89 return HITLS_SUCCESS;
90 }
91
VERIFY_Append(VerifyCtx * ctx,const uint8_t * data,uint32_t len)92 int32_t VERIFY_Append(VerifyCtx *ctx, const uint8_t *data, uint32_t len)
93 {
94 int32_t ret;
95 if (ctx->hashCtx != NULL) {
96 ret = SAL_CRYPT_DigestUpdate(ctx->hashCtx, data, len);
97 if (ret != HITLS_SUCCESS) {
98 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15720, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
99 "Verify append error: digest update fail.", 0, 0, 0, 0);
100 return ret;
101 }
102 }
103
104 if (ctx->dataBuf != NULL) {
105 return CacheMsgData(ctx->dataBuf, data, len);
106 }
107 return HITLS_SUCCESS;
108 }
109
VERIFY_CalcSessionHash(VerifyCtx * ctx,uint8_t * digest,uint32_t * digestLen)110 int32_t VERIFY_CalcSessionHash(VerifyCtx *ctx, uint8_t *digest, uint32_t *digestLen)
111 {
112 int32_t ret;
113
114 HITLS_HASH_Ctx *tmpHashCtx = SAL_CRYPT_DigestCopy(ctx->hashCtx);
115 if (tmpHashCtx == NULL) {
116 BSL_ERR_PUSH_ERROR(HITLS_CRYPT_ERR_DIGEST);
117 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15721, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
118 "Verify data calculate error: digest copy fail.", 0, 0, 0, 0);
119 return HITLS_CRYPT_ERR_DIGEST;
120 }
121
122 /* get the hash result */
123 ret = SAL_CRYPT_DigestFinal(tmpHashCtx, digest, digestLen);
124 SAL_CRYPT_DigestFree(tmpHashCtx);
125 if (ret != HITLS_SUCCESS) {
126 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15722, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
127 "Verify data calculate error: digest final fail.", 0, 0, 0, 0);
128 }
129
130 return ret;
131 }
132
VERIFY_FreeMsgCache(VerifyCtx * ctx)133 void VERIFY_FreeMsgCache(VerifyCtx *ctx)
134 {
135 HsMsgCache *nextBuf = NULL;
136 HsMsgCache *dataBuf = ctx->dataBuf;
137 while (dataBuf != NULL) {
138 nextBuf = dataBuf->next;
139 BSL_SAL_FREE(dataBuf->data);
140 BSL_SAL_FREE(dataBuf);
141 dataBuf = nextBuf;
142 }
143 ctx->dataBuf = NULL;
144 return;
145 }
146