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_XTS_H 17 #define CRYPT_MODES_XTS_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_XTS 21 22 #include "crypt_types.h" 23 #include "bsl_params.h" 24 #include "crypt_modes.h" 25 #ifdef __cplusplus 26 extern "C" { 27 #endif // __cplusplus 28 29 typedef struct { 30 void *ciphCtx; /* Key defined by each algorithm */ 31 const EAL_SymMethod *ciphMeth; /* corresponding to the encrypt and decrypt in the bottom layer, operate keyctx */ 32 uint8_t iv[MODES_MAX_IV_LENGTH]; /* The length is blocksize */ 33 uint8_t tweak[MODES_MAX_IV_LENGTH]; /* The length is blocksize */ 34 uint8_t blockSize; /* Save the block size. */ 35 } MODES_CipherXTSCtx; 36 struct ModesXTSCtx { 37 int32_t algId; 38 MODES_CipherXTSCtx xtsCtx; 39 uint8_t data[EAL_MAX_BLOCK_LENGTH]; /**< last data block that may not be processed */ 40 uint8_t dataLen; /**< size of the last data block that may not be processed. */ 41 CRYPT_PaddingType pad; 42 bool enc; 43 }; 44 typedef struct ModesXTSCtx MODES_XTS_Ctx; 45 46 // XTS mode universal implementation 47 MODES_XTS_Ctx *MODES_XTS_NewCtx(int32_t algId); 48 int32_t MODES_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 49 uint32_t ivLen, bool enc); 50 51 int32_t MODES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 52 int32_t MODES_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 53 int32_t MODES_XTS_DeInitCtx(MODES_XTS_Ctx *modeCtx); 54 int32_t MODES_XTS_Ctrl(MODES_XTS_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len); 55 void MODES_XTS_FreeCtx(MODES_XTS_Ctx *modeCtx); 56 57 // XTS mode universal implementation 58 int32_t SM4_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 59 int32_t SM4_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 60 uint32_t ivLen, bool enc); 61 int32_t SM4_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 62 63 int32_t MODES_XTS_InitCtxEx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, 64 uint32_t ivLen, void *param, bool enc); 65 66 int32_t MODES_XTS_UpdateEx(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 67 68 int32_t AES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 69 70 int32_t AES_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 71 72 #ifdef __cplusplus 73 } 74 #endif // __cplusplus 75 76 #endif // HITLS_CRYPTO_XTS 77 78 #endif // CRYPT_MODES_XTS_H 79