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_CFB_H 17 #define CRYPT_MODES_CFB_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CFB 21 22 #include "crypt_types.h" 23 #include "crypt_modes.h" 24 #ifdef __cplusplus 25 extern "C" { 26 #endif // __cplusplus 27 28 #define DES_BLOCK_BYTE_NUM 8 29 typedef struct { 30 MODES_CipherCommonCtx modeCtx; 31 uint8_t feedbackBits; /* Save the FeedBack length. */ 32 uint8_t cipherCache[3][DES_BLOCK_BYTE_NUM]; 33 uint8_t cacheIndex; /* Used by the TDES that has 3IV. Indicate which cache is being used. */ 34 } MODES_CipherCFBCtx; 35 struct ModesCFBCtx { 36 int32_t algId; 37 MODES_CipherCFBCtx cfbCtx; 38 bool enc; 39 }; 40 typedef struct ModesCFBCtx MODES_CFB_Ctx; 41 42 // CFB mode universal implementation 43 MODES_CFB_Ctx *MODES_CFB_NewCtx(int32_t algId); 44 int32_t MODES_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 45 uint32_t ivLen, bool enc); 46 47 int32_t MODES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 48 int32_t MODES_CFB_Final(MODES_CFB_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 49 int32_t MODES_CFB_DeInitCtx(MODES_CFB_Ctx *modeCtx); 50 int32_t MODES_CFB_Ctrl(MODES_CFB_Ctx *modeCtx, int32_t opt, void *val, uint32_t len); 51 void MODES_CFB_FreeCtx(MODES_CFB_Ctx *modeCtx); 52 53 // AES CFB optimization implementation 54 int32_t AES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 55 56 // SM4 CFB optimization implementation 57 int32_t SM4_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 58 uint32_t ivLen, bool enc); 59 int32_t SM4_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 60 61 62 int32_t MODES_CFB_InitCtxEx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 63 uint32_t ivLen, void *param, bool enc); 64 int32_t MODES_CFB_UpdateEx(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 65 66 #ifdef __cplusplus 67 } 68 #endif // __cplusplus 69 70 #endif // HITLS_CRYPTO_CFB 71 72 #endif // CRYPT_MODES_CFB_H 73