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_CMAC_H 17 #define CRYPT_CMAC_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CMAC 21 22 #include <stdint.h> 23 #include "crypt_types.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif /* __cpluscplus */ 28 29 typedef struct Cipher_MAC_Ctx CRYPT_CMAC_Ctx; 30 31 /** 32 * @brief Create a new CMAC context. 33 * @param id [in] Symmetric encryption and decryption algorithm ID 34 * @return CMAC context 35 */ 36 CRYPT_CMAC_Ctx *CRYPT_CMAC_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] CMAC 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_CMAC_Init(CRYPT_CMAC_Ctx *ctx, const uint8_t *key, uint32_t len, void *param); 49 /** 50 * @brief Enter the data to be calculated and update the context. 51 * @param ctx [IN] CMAC context 52 * @param *in [in] Pointer to the data to be calculated 53 * @param len [in] Length of the data to be calculated 54 * @retval #CRYPT_SUCCESS Succeeded. 55 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 56 * For other error codes, see crypt_errno.h. 57 */ 58 int32_t CRYPT_CMAC_Update(CRYPT_CMAC_Ctx *ctx, const uint8_t *in, uint32_t len); 59 /** 60 * @brief Output the cmac calculation result. 61 * @param ctx [IN] CMAC context 62 * @param out [OUT] Output data. Sufficient memory must be allocated to store CMAC results and cannot be null. 63 * @param len [IN/OUT] Output data length 64 * @retval #CRYPT_SUCCESS Succeeded. 65 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 66 * @retval #CRYPT_EAL_BUFF_LEN_NOT_ENOUGH The length of the output buffer is insufficient. 67 * For other error codes, see crypt_errno.h. 68 */ 69 int32_t CRYPT_CMAC_Final(CRYPT_CMAC_Ctx *ctx, uint8_t *out, uint32_t *len); 70 /** 71 * @brief Re-initialize using the information retained in the ctx. Do not need to invoke the init again. 72 * This function is equivalent to the combination of deinit and init interfaces. 73 * @param ctx [IN] CMAC context 74 */ 75 void CRYPT_CMAC_Reinit(CRYPT_CMAC_Ctx *ctx); 76 /** 77 * @brief Deinitialization function. 78 * If calculation is required after this function is invoked, it needs to be initialized again. 79 * @param ctx [IN] CMAC context 80 */ 81 void CRYPT_CMAC_Deinit(CRYPT_CMAC_Ctx *ctx); 82 83 /** 84 * @brief Control function for CMAC. 85 * @param ctx [IN] CMAC context 86 * @param opt [IN] Control option 87 * @param val [IN]/[OUT] Control value 88 * @param len [IN] Control value length 89 * @retval #CRYPT_SUCCESS Succeeded. 90 * @retval #CRYPT_NULL_INPUT The input parameter is NULL. 91 * For other error codes, see crypt_errno.h. 92 */ 93 int32_t CRYPT_CMAC_Ctrl(CRYPT_CMAC_Ctx *ctx, uint32_t opt, void *val, uint32_t len); 94 95 /** 96 * @brief Free the CMAC context. 97 * @param ctx [IN] CMAC context 98 */ 99 void CRYPT_CMAC_FreeCtx(CRYPT_CMAC_Ctx *ctx); 100 101 #ifdef __cplusplus 102 } 103 #endif /* __cpluscplus */ 104 105 #endif 106 #endif