• 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 #ifndef CRYPT_SIPHASH_H
17 #define CRYPT_SIPHASH_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_SIPHASH
21 
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include "crypt_local_types.h"
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cpluscplus */
30 
31 #define SIPHASH_KEY_SIZE 16 // 128 bit
32 #define SIPHASH_WORD_SIZE 8 // 64 bit
33 #define DEFAULT_COMPRESSION_ROUND 2
34 #define DEFAULT_FINALIZATION_ROUND 4
35 // The siphash has only two output lengths: 8-byte and 16-byte.
36 #define SIPHASH_MIN_DIGEST_SIZE 8
37 #define SIPHASH_MAX_DIGEST_SIZE 16
38 
39 typedef struct SIPHASH_Ctx CRYPT_SIPHASH_Ctx;
40 
41 /**
42  * @brief Create a new siphash context.
43  * @param id [IN] MAC algorithm id
44  * @retval Pointer to the created siphash context.
45  */
46 CRYPT_SIPHASH_Ctx *CRYPT_SIPHASH_NewCtx(CRYPT_MAC_AlgId id);
47 
48 /**
49  * @brief Initialize the siphash context by using the key passed by the user.
50  * @param ctx [IN] siphash context
51  * @param key [IN] MAC symmetric key
52  * @param len [IN] Key length. The length of the siphash key is fixed to 128 bits.
53  * @param param [IN] param, reserved.
54  * @retval #CRYPT_SUCCESS       Succeeded.
55  * @retval #CRYPT_NULL_INPUT    The input parameter is NULL.
56  *         #CRYPT_INVALID_ARG   invalid input parameter. For example, the input key length is not 128 bits.
57  */
58 int32_t CRYPT_SIPHASH_Init(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *key, uint32_t keyLen, void *param);
59 
60 /**
61  * @brief siphash update, supporting streaming update
62  * @param ctx [IN] siphash context
63  * @param in [IN] Point to the data buffer for MAC calculation.
64  * @param inlen [IN] Length of the data to be calculated
65  * @retval #CRYPT_SUCCESS                           Succeeded.
66  * @retval #CRYPT_NULL_INPUT                        The input parameter is NULL.
67  */
68 int32_t CRYPT_SIPHASH_Update(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *in, uint32_t inlen);
69 
70 /**
71  * @brief siphash closeout calculation
72  * @param ctx [IN] siphash context
73  * @param out [OUT] Output data. Sufficient memory must be allocated to store CMAC results and cannot be null.
74  * @param outlen [IN/OUT] Output data length
75  * @retval #CRYPT_SUCCESS                            Succeeded.
76  * @retval #CRYPT_NULL_INPUT                         The input parameter is NULL.
77  * @retval #CRYPT_SIPHASH_OUT_BUFF_LEN_NOT_ENOUGH    The output buffer is insufficient.
78  */
79 int32_t CRYPT_SIPHASH_Final(CRYPT_SIPHASH_Ctx *ctx, uint8_t *out, uint32_t *outlen);
80 
81 /**
82  * @brief Re-initialize the siphash context
83  * @param ctx [IN]  siphash context
84  */
85 void CRYPT_SIPHASH_Reinit(CRYPT_SIPHASH_Ctx *ctx);
86 
87 /**
88  * @brief   siphash de-initialization
89  * @param ctx [IN]  siphash context
90  */
91 void CRYPT_SIPHASH_Deinit(CRYPT_SIPHASH_Ctx *ctx);
92 
93 /**
94  * @brief   siphash control
95  * @param ctx [IN]  siphash context
96  * @param opt [IN]  control option
97  * @param val [IN]/[OUT] Control value
98  * @param len [IN]  control value length
99  * @retval #CRYPT_SUCCESS                           Succeeded.
100  * @retval #CRYPT_NULL_INPUT                        The input parameter is NULL.
101  *          For other error codes, see crypt_errno.h.
102  */
103 int32_t CRYPT_SIPHASH_Ctrl(CRYPT_SIPHASH_Ctx *ctx, uint32_t opt, void *val, uint32_t len);
104 
105 /**
106  * @brief   siphash free context
107  * @param ctx [IN]  siphash context
108  */
109 void CRYPT_SIPHASH_FreeCtx(CRYPT_SIPHASH_Ctx *ctx);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* HITLS_CRYPTO_SIPHASH */
116 
117 #endif
118