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_H 17 #define CRYPT_MODES_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_MODES 21 22 #include <stdint.h> 23 #include "crypt_local_types.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif // __cplusplus 28 29 #define MODES_MAX_IV_LENGTH 24 30 #define MODES_MAX_BUF_LENGTH 24 31 #define MODES_IV_LENGTH 16 32 #define EAL_MAX_BLOCK_LENGTH 32 33 typedef struct { 34 void *ciphCtx; /* Context defined by each algorithm */ 35 const EAL_SymMethod *ciphMeth; /* Corresponding to the related methods for each symmetric algorithm */ 36 uint8_t iv[MODES_MAX_IV_LENGTH]; /* IV information */ 37 uint8_t buf[MODES_MAX_BUF_LENGTH]; /* Cache the information of the previous block. */ 38 uint8_t blockSize; /* Save the block size. */ 39 uint8_t offset; 40 uint8_t flag3Iv; /* Indicates whether three IVs are used. */ 41 uint32_t ivIndex; /* Indicates the sequence number of the IV block to be used. TDES may have three IV. */ 42 } MODES_CipherCommonCtx; 43 44 struct ModesCipherCtx { 45 MODES_CipherCommonCtx commonCtx; 46 int32_t algId; 47 uint8_t data[EAL_MAX_BLOCK_LENGTH]; /**< last data block that may not be processed */ 48 uint8_t dataLen; /**< size of the last data block that may not be processed. */ 49 CRYPT_PaddingType pad; /**< padding type */ 50 bool enc; 51 }; 52 typedef struct ModesCipherCtx MODES_CipherCtx; 53 54 typedef struct { 55 const uint8_t *in; 56 uint8_t *out; 57 const uint8_t *ctr; 58 uint8_t *tag; 59 } XorCryptData; 60 void MODES_Clean(MODES_CipherCommonCtx *ctx); 61 int32_t MODES_SetIv(MODES_CipherCommonCtx *ctx, const uint8_t *val, uint32_t len); 62 int32_t MODES_GetIv(MODES_CipherCommonCtx *ctx, uint8_t *val, uint32_t len); 63 #ifdef __cplusplus 64 } 65 #endif // __cplusplus 66 67 #endif // HITLS_CRYPTO_MODES 68 69 #endif // CRYPT_MODES_H 70