1370b324cSopenharmony_ci/* Aes.h -- AES encryption / decryption 2370b324cSopenharmony_ci2023-04-02 : Igor Pavlov : Public domain */ 3370b324cSopenharmony_ci 4370b324cSopenharmony_ci#ifndef ZIP7_INC_AES_H 5370b324cSopenharmony_ci#define ZIP7_INC_AES_H 6370b324cSopenharmony_ci 7370b324cSopenharmony_ci#include "7zTypes.h" 8370b324cSopenharmony_ci 9370b324cSopenharmony_ciEXTERN_C_BEGIN 10370b324cSopenharmony_ci 11370b324cSopenharmony_ci#define AES_BLOCK_SIZE 16 12370b324cSopenharmony_ci 13370b324cSopenharmony_ci/* Call AesGenTables one time before other AES functions */ 14370b324cSopenharmony_civoid AesGenTables(void); 15370b324cSopenharmony_ci 16370b324cSopenharmony_ci/* UInt32 pointers must be 16-byte aligned */ 17370b324cSopenharmony_ci 18370b324cSopenharmony_ci/* 16-byte (4 * 32-bit words) blocks: 1 (IV) + 1 (keyMode) + 15 (AES-256 roundKeys) */ 19370b324cSopenharmony_ci#define AES_NUM_IVMRK_WORDS ((1 + 1 + 15) * 4) 20370b324cSopenharmony_ci 21370b324cSopenharmony_ci/* aes - 16-byte aligned pointer to keyMode+roundKeys sequence */ 22370b324cSopenharmony_ci/* keySize = 16 or 24 or 32 (bytes) */ 23370b324cSopenharmony_citypedef void (Z7_FASTCALL *AES_SET_KEY_FUNC)(UInt32 *aes, const Byte *key, unsigned keySize); 24370b324cSopenharmony_civoid Z7_FASTCALL Aes_SetKey_Enc(UInt32 *aes, const Byte *key, unsigned keySize); 25370b324cSopenharmony_civoid Z7_FASTCALL Aes_SetKey_Dec(UInt32 *aes, const Byte *key, unsigned keySize); 26370b324cSopenharmony_ci 27370b324cSopenharmony_ci/* ivAes - 16-byte aligned pointer to iv+keyMode+roundKeys sequence: UInt32[AES_NUM_IVMRK_WORDS] */ 28370b324cSopenharmony_civoid AesCbc_Init(UInt32 *ivAes, const Byte *iv); /* iv size is AES_BLOCK_SIZE */ 29370b324cSopenharmony_ci 30370b324cSopenharmony_ci/* data - 16-byte aligned pointer to data */ 31370b324cSopenharmony_ci/* numBlocks - the number of 16-byte blocks in data array */ 32370b324cSopenharmony_citypedef void (Z7_FASTCALL *AES_CODE_FUNC)(UInt32 *ivAes, Byte *data, size_t numBlocks); 33370b324cSopenharmony_ci 34370b324cSopenharmony_ciextern AES_CODE_FUNC g_AesCbc_Decode; 35370b324cSopenharmony_ci#ifndef Z7_SFX 36370b324cSopenharmony_ciextern AES_CODE_FUNC g_AesCbc_Encode; 37370b324cSopenharmony_ciextern AES_CODE_FUNC g_AesCtr_Code; 38370b324cSopenharmony_ci#define k_Aes_SupportedFunctions_HW (1 << 2) 39370b324cSopenharmony_ci#define k_Aes_SupportedFunctions_HW_256 (1 << 3) 40370b324cSopenharmony_ciextern UInt32 g_Aes_SupportedFunctions_Flags; 41370b324cSopenharmony_ci#endif 42370b324cSopenharmony_ci 43370b324cSopenharmony_ci 44370b324cSopenharmony_ci#define Z7_DECLARE_AES_CODE_FUNC(funcName) \ 45370b324cSopenharmony_ci void Z7_FASTCALL funcName(UInt32 *ivAes, Byte *data, size_t numBlocks); 46370b324cSopenharmony_ci 47370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCbc_Encode) 48370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCbc_Decode) 49370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCtr_Code) 50370b324cSopenharmony_ci 51370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCbc_Encode_HW) 52370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCbc_Decode_HW) 53370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCtr_Code_HW) 54370b324cSopenharmony_ci 55370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCbc_Decode_HW_256) 56370b324cSopenharmony_ciZ7_DECLARE_AES_CODE_FUNC (AesCtr_Code_HW_256) 57370b324cSopenharmony_ci 58370b324cSopenharmony_ciEXTERN_C_END 59370b324cSopenharmony_ci 60370b324cSopenharmony_ci#endif 61