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_MODES_CCM_H 17 #define CRYPT_MODES_CCM_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CCM 21 22 #include "crypt_types.h" 23 #include "crypt_modes.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif // __cplusplus 28 29 #define CCM_BLOCKSIZE 16 30 typedef struct { 31 void *ciphCtx; /* Context defined by each algorithm */ 32 const EAL_SymMethod *ciphMeth; /* Corresponding to the related methods for each symmetric algorithm */ 33 34 uint8_t nonce[CCM_BLOCKSIZE]; /* Data nonce, ctr encrypted data */ 35 uint8_t tag[CCM_BLOCKSIZE]; /* Data tag, intermediate data encrypted by the CBC */ 36 uint8_t last[CCM_BLOCKSIZE]; /* Previous data block in ctr mode */ 37 uint64_t msgLen; /* The message length */ 38 uint8_t lastLen; /* Unused data length of the previous data block in ctr mode. */ 39 uint8_t tagLen; /* The length of the tag is 16 by default. The tag is reset each time the key is set. */ 40 uint8_t tagInit; /* Indicate whether the tag is initialized. */ 41 } MODES_CipherCCMCtx; 42 struct ModesCcmCtx { 43 int32_t algId; 44 MODES_CipherCCMCtx ccmCtx; 45 bool enc; 46 }; 47 typedef struct ModesCcmCtx MODES_CCM_Ctx; 48 // CCM mode universal implementation 49 MODES_CCM_Ctx *MODES_CCM_NewCtx(int32_t algId); 50 int32_t MODES_CCM_InitCtx(MODES_CCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 51 uint32_t ivLen, void *param, bool enc); 52 53 int32_t MODES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 54 int32_t MODES_CCM_Final(MODES_CCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 55 int32_t MODES_CCM_DeInitCtx(MODES_CCM_Ctx *modeCtx); 56 int32_t MODES_CCM_Ctrl(MODES_CCM_Ctx *modeCtx, int32_t opt, void *val, uint32_t len); 57 void MODES_CCM_FreeCtx(MODES_CCM_Ctx *modeCtx); 58 59 // AES CCM optimization implementation 60 int32_t AES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 61 62 int32_t MODES_CCM_UpdateEx(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 63 64 #ifdef __cplusplus 65 } 66 #endif // __cplusplus 67 68 #endif // HITLS_CRYPTO_CCM 69 70 #endif // CRYPT_MODES_CCM_H 71