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_CHACHA20POLY1305_H 17 #define CRYPT_MODES_CHACHA20POLY1305_H 18 19 #include "hitls_build.h" 20 #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) 21 22 #include "crypt_types.h" 23 #include "crypt_modes.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif // __cplusplus 28 29 typedef struct { 30 uint32_t acc[6]; // The intermediate data of the acc, must be greater than 130 bits. 31 uint32_t r[4]; // Key information r, 16 bytes, that is, 4 * sizeof(uint32_t) 32 uint32_t s[4]; // Key information s, 16 bytes, that is, 4 * sizeof(uint32_t) 33 uint32_t table[36]; // Indicates the table used to accelerate the assembly calculation. 34 uint8_t last[16]; // A block 16 bytes are cached for the last unprocessed data. 35 uint32_t lastLen; // Indicates the remaining length of the last data. 36 uint32_t flag; // Used to save the assembly status information. 37 } Poly1305Ctx; 38 typedef struct { 39 void *key; // Handle for the method. 40 const EAL_SymMethod *method; // algorithm method 41 Poly1305Ctx polyCtx; 42 uint64_t aadLen; // Status, indicating whether identification data is set. 43 uint64_t cipherTextLen; // status, indicating whether the identification data is set. 44 } MODES_CipherChaChaPolyCtx; 45 struct ModesChaChaCtx { 46 int32_t algId; 47 MODES_CipherChaChaPolyCtx chachaCtx; 48 bool enc; 49 }; 50 typedef struct ModesChaChaCtx MODES_CHACHAPOLY_Ctx; 51 52 MODES_CHACHAPOLY_Ctx *MODES_CHACHA20POLY1305_NewCtx(int32_t algId); 53 int32_t MODES_CHACHA20POLY1305_InitCtx(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *key, 54 uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); 55 56 int32_t MODES_CHACHA20POLY1305_Update(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, 57 uint8_t *out, uint32_t *outLen); 58 int32_t MODES_CHACHA20POLY1305_Final(MODES_CHACHAPOLY_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); 59 int32_t MODES_CHACHA20POLY1305_DeInitCtx(MODES_CHACHAPOLY_Ctx *modeCtx); 60 int32_t MODES_CHACHA20POLY1305_Ctrl(MODES_CHACHAPOLY_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len); 61 void MODES_CHACHA20POLY1305_FreeCtx(MODES_CHACHAPOLY_Ctx *modeCtx); 62 63 #ifdef __cplusplus 64 } 65 #endif // __cplusplus 66 67 #endif // HITLS_CRYPTO_CHACHA20POLY1305 68 69 #endif // CRYPT_MODES_CHACHA20POLY1305_H 70