API参考
载入中...
搜索中...
未找到
rec_conn.h
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#ifndef REC_CONN_H
17#define REC_CONN_H
18
19#include <stdint.h>
20#include <stddef.h>
21#include "rec.h"
22#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
23#include "rec_anti_replay.h"
24#endif /* HITLS_TLS_PROTO_DTLS12 && HITLS_BSL_UIO_UDP */
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#define REC_MAX_MAC_KEY_LEN 64
31#define REC_MAX_KEY_LENGTH 64
32#define REC_MAX_IV_LENGTH 16
33#define REC_MAX_KEY_BLOCK_LEN (REC_MAX_MAC_KEY_LEN * 2 + REC_MAX_KEY_LENGTH * 2 + REC_MAX_IV_LENGTH * 2)
34#define MAX_SHA1_SIZE 20
35#define MAX_MD5_SIZE 16
36
37#define REC_CONN_SEQ_SIZE 8u /* Sequence number size */
38
39/*
40 * Cipher suite information, which is required for local encryption and decryption
41 * For details, see RFC5246 6.1
42 */
43typedef struct {
44 HITLS_MacAlgo macAlg; /* MAC algorithm */
45 HITLS_CipherAlgo cipherAlg; /* symmetric encryption algorithm */
46 HITLS_CipherType cipherType; /* encryption algorithm type */
47 HITLS_Cipher_Ctx *ctx; /* cipher context handle, only for record layer encryption and decryption */
48 HITLS_HMAC_Ctx *macCtx; /* mac context handle, only for record layer mac */
49
50 uint8_t macKey[REC_MAX_MAC_KEY_LEN];
51 uint8_t key[REC_MAX_KEY_LENGTH];
52 uint8_t iv[REC_MAX_IV_LENGTH];
53 bool isExportIV; /* Used by the TTO feature. The IV does not need to be randomly
54 generated during CBC encryption If it is set by user */
55 /* key length */
56 uint8_t macKeyLen; /* Length of the MAC key. The length of the MAC key is 0 in AEAD algorithm */
57 uint8_t encKeyLen; /* Length of the symmetric key */
58 uint8_t fixedIvLength; /* iv length. It is the implicit IV length in AEAD algorithm */
59
60 /* result length */
61 uint8_t blockLength; /* If the block length is not zero, the alignment should be handled */
62 uint8_t recordIvLength; /* The explicit IV needs to be sent to the peer */
63 uint8_t macLen; /* Add the length of the MAC. Or the tag length in AEAD */
65
66/* connection state */
67typedef struct {
68 RecConnSuitInfo *suiteInfo; /* Cipher suite information */
69 uint64_t seq; /* tls: 8 byte sequence number or dtls: 6 byte seq */
70 bool isWrapped; /* tls: Check whether the sequence number is wrapped */
71
72 uint16_t epoch; /* dtls: 2 byte epoch */
73#if defined(HITLS_TLS_PROTO_DTLS12) && defined(HITLS_BSL_UIO_UDP)
74 uint16_t reserve; /* Four-byte alignment is reserved */
75 RecSlidWindow window; /* dtls record sliding window (for anti-replay) */
76#endif
78
79/* see TLSPlaintext structure definition in rfc */
80typedef struct {
81 uint8_t type; // ccs(20), alert(21), hs(22), app data(23), (255)
82#ifdef HITLS_TLS_FEATURE_ETM
83 bool isEncryptThenMac;
84#endif
85 uint8_t reverse[2];
86
87 uint16_t version;
88 uint16_t negotiatedVersion;
89
90 uint8_t seq[REC_CONN_SEQ_SIZE]; /* 1. tls: sequence number 2.dtls: epoch + sequence */
91
92 uint32_t textLen;
93 const uint8_t *text; // fragment
95
99RecConnState *RecConnStateNew(void);
100
104void RecConnStateFree(RecConnState *state);
105
113uint64_t RecConnGetSeqNum(const RecConnState *state);
114
123void RecConnSetSeqNum(RecConnState *state, uint64_t seq);
124
125#ifdef HITLS_TLS_PROTO_DTLS12
135uint16_t RecConnGetEpoch(const RecConnState *state);
136
145void RecConnSetEpoch(RecConnState *state, uint16_t epoch);
146
147#endif
148
159int32_t RecConnStateSetCipherInfo(RecConnState *state, RecConnSuitInfo *suitInfo);
160
161
177int32_t RecConnEncrypt(TLS_Ctx *ctx,
178 RecConnState *state, const REC_TextInput *plainMsg, uint8_t *cipherText, uint32_t cipherTextLen);
179
193int32_t RecConnDecrypt(TLS_Ctx *ctx, RecConnState *state,
194 const REC_TextInput *cryptMsg, uint8_t *data, uint32_t *dataLen);
195
209int32_t RecConnKeyBlockGen(HITLS_Lib_Ctx *libCtx, const char *attrName,
210 const REC_SecParameters *param, RecConnSuitInfo *client, RecConnSuitInfo *server);
225int32_t RecTLS13ConnKeyBlockGen(HITLS_Lib_Ctx *libCtx, const char *attrName,
226 const REC_SecParameters *param, RecConnSuitInfo *suitInfo);
227
228/*
229 * @brief check the mac
230 *
231 * @param ctx [IN] tls Context
232 * @param suiteInfo [IN] ciphersuiteInfo
233 * @param cryptMsg [IN] text info
234 * @param text [IN] fragment
235 * @param textLen [IN] fragment len
236 * @retval HITLS_SUCCESS
237 * @retval Reference hitls_error.h
238 */
239int32_t RecConnCheckMac(TLS_Ctx *ctx, RecConnSuitInfo *suiteInfo, const REC_TextInput *cryptMsg,
240 const uint8_t *text, uint32_t textLen);
241
242/*
243 * @brief generate the mac
244 *
245 * @param libCtx [IN] library context for provider
246 * @param attrName [IN] attribute name of the provider, maybe NULL
247 * @param suiteInfo [IN] ciphersuiteInfo
248 * @param plainMsg [IN] text info
249 * @param mac [OUT] mac buffer
250 * @param macLen [OUT] mac buffer len
251 * @retval HITLS_SUCCESS
252 * @retval Reference hitls_error.h
253 */
254int32_t RecConnGenerateMac(HITLS_Lib_Ctx *libCtx, const char *attrName,
255 RecConnSuitInfo *suiteInfo, const REC_TextInput *plainMsg,
256 uint8_t *mac, uint32_t *macLen);
257
258/*
259 * @brief check the mac
260 *
261 * @param in [IN] plaintext info
262 * @param text [IN] plaintext buf
263 * @param textLen [IN] plaintext buf len
264 * @param out [IN] mac info
265 * @retval HITLS_SUCCESS
266 * @retval Reference hitls_error.h
267 */
268void RecConnInitGenerateMacInput(const REC_TextInput *in, const uint8_t *text, uint32_t textLen,
269 REC_TextInput *out);
270
271#ifdef HITLS_TLS_SUITE_CIPHER_CBC
272uint32_t RecGetHashAlgoFromMacAlgo(HITLS_MacAlgo macAlgo);
273#endif
274#ifdef __cplusplus
275}
276#endif
277
278#endif /* REC_CONN_H */
定义 rec.h:55
定义 rec_conn.h:80
定义 rec_conn.h:67
定义 rec_conn.h:43
定义 rec_anti_replay.h:42