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_CBC_MAC_H 17 #define CRYPT_CBC_MAC_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CBC_MAC 21 #include <stdint.h> 22 #include "crypt_types.h" 23 #include "crypt_cmac.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif /* __cpluscplus */ 28 29 typedef struct CBC_MAC_Ctx CRYPT_CBC_MAC_Ctx; 30 31 /** 32 * @brief Create a new CBC_MAC context. 33 * @param id [IN] CBC_MAC algorithm ID 34 * @return Pointer to the CBC_MAC context 35 */ 36 CRYPT_CBC_MAC_Ctx *CRYPT_CBC_MAC_NewCtx(CRYPT_MAC_AlgId id); 37 38 /** 39 * @brief Use the key passed by the user to initialize the algorithm context. 40 * @param ctx [IN] CBC_MAC context 41 * @param key [in] symmetric algorithm key 42 * @param len [in] Key length 43 * @param param [in] param 44 * @retval #CRYPT_SUCCESS Succeeded. 45 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 46 * For other error codes, see crypt_errno.h. 47 */ 48 int32_t CRYPT_CBC_MAC_Init(CRYPT_CBC_MAC_Ctx *ctx, const uint8_t *key, uint32_t len, void *param); 49 50 /** 51 * @brief Enter the data to be calculated and update the context. 52 * @param ctx [IN] CBC_MAC context 53 * @param *in [in] Pointer to the data to be calculated 54 * @param len [in] Length of the data to be calculated 55 * @retval #CRYPT_SUCCESS Succeeded. 56 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 57 * For other error codes, see crypt_errno.h. 58 */ 59 int32_t CRYPT_CBC_MAC_Update(CRYPT_CBC_MAC_Ctx *ctx, const uint8_t *in, uint32_t len); 60 61 /** 62 * @brief Output the cmac calculation result. 63 * @param ctx [IN] CBC_MAC context 64 * @param out [OUT] Output data. Sufficient memory must be allocated to store CBC_MAC results and cannot be null. 65 * @param len [IN/OUT] Output data length 66 * @retval #CRYPT_SUCCESS Succeeded. 67 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 68 * @retval #CRYPT_EAL_BUFF_LEN_NOT_ENOUGH The length of the output buffer is insufficient. 69 * For other error codes, see crypt_errno.h. 70 */ 71 int32_t CRYPT_CBC_MAC_Final(CRYPT_CBC_MAC_Ctx *ctx, uint8_t *out, uint32_t *len); 72 73 /** 74 * @brief Re-initialize using the information retained in the ctx. Do not need to invoke the init again. 75 * This function is equivalent to the combination of deinit and init interfaces. 76 * @param ctx [IN] CBC_MAC context 77 */ 78 void CRYPT_CBC_MAC_Reinit(CRYPT_CBC_MAC_Ctx *ctx); 79 80 /** 81 * @brief Deinitialization function. 82 * If calculation is required after this function is invoked, it needs to be initialized again. 83 * @param ctx [IN] CBC_MAC context 84 */ 85 void CRYPT_CBC_MAC_Deinit(CRYPT_CBC_MAC_Ctx *ctx); 86 87 /** 88 * @brief CBC_MAC control function to set some information 89 * @param ctx [IN] CBC_MAC context 90 * @param opt [IN] option 91 * @param val [IN] value 92 * @param len [IN] the length of value 93 * @return See crypt_errno.h. 94 */ 95 int32_t CRYPT_CBC_MAC_Ctrl(CRYPT_CBC_MAC_Ctx *ctx, uint32_t opt, void *val, uint32_t len); 96 97 /** 98 * @brief Free the CBC_MAC context. 99 * @param ctx [IN] CBC_MAC context 100 */ 101 void CRYPT_CBC_MAC_FreeCtx(CRYPT_CBC_MAC_Ctx *ctx); 102 103 #ifdef __cplusplus 104 } 105 #endif /* __cpluscplus */ 106 107 #endif 108 #endif 109