1 #ifndef __SM4_CORE_H__ 2 #define __SM4_CORE_H__ 3 4 #include <stdint.h> 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 #define ENCRYPT 1 11 #define DECRYPT 0 12 13 #define SM4_BLOCK_SIZE 16 14 15 #ifdef OPENSSL_FIPS 16 #define FIPS_AES_SIZE_T int 17 #endif 18 19 /* zhangzj: make sure u64 is 8 bytes */ 20 /* zhangzj: make sure u64 is 8 bytes */ 21 #ifndef u32 22 23 #if defined(__arch64__) 24 #define U64(C) C##UL 25 #else 26 #define U64(C) C##ULL 27 #endif 28 29 typedef int64_t i64; 30 typedef uint64_t u64; 31 typedef uint32_t u32; 32 typedef uint16_t u16; 33 typedef uint8_t u8; 34 #endif 35 36 enum{ 37 SM4_MODE_ECB = 0, 38 SM4_MODE_CBC, 39 SM4_MODE_OFB, 40 SM4_MODE_CFB, 41 SM4_MODE_CTS, 42 SM4_MODE_CTR, 43 SM4_MODE_XTS, 44 SM4_MODE_CCM, 45 SM4_MODE_GCM, 46 SM4_MODE_CMAC, 47 SM4_MODE_CBC_MAC 48 }; 49 50 struct sm4_ae_in { 51 void *key; 52 void *src; 53 void *iv; 54 void *aad; 55 int key_len; 56 unsigned int src_len; 57 unsigned int iv_len; 58 unsigned int aad_len; 59 unsigned int tag_size; 60 }; 61 62 struct sm4_ae_out { 63 void *dest; 64 void *tag; 65 unsigned int dest_len; 66 }; 67 68 /** 69 * \brief SM4 context structure 70 */ 71 typedef struct 72 { 73 int mode; /*!< encrypt/decrypt */ 74 unsigned long sk[32]; /*!< SM4 subkeys */ 75 }sm4_context; 76 77 /* 78 * SM4 key schedule (128-bit, encryption) 79 */ 80 void rk_sm4_setkey_enc( sm4_context *ctx, const unsigned char key[16] ); 81 82 /* 83 * SM4 key schedule (128-bit, decryption) 84 */ 85 void rk_sm4_setkey_dec( sm4_context *ctx, const unsigned char key[16] ); 86 87 /* 88 * SM4-ECB block encryption/decryption 89 */ 90 int rk_sm4_crypt_ecb( void *ctx, const unsigned char *input, unsigned char *output); 91 92 int rk_rk_sm4_crypt_ecb(const unsigned char *input, 93 unsigned char *output, void *ctx); 94 95 #ifndef block128_f 96 typedef int (*block128_f)( const unsigned char *input, \ 97 unsigned char *output, void *ctx); 98 #endif 99 #ifdef __cplusplus 100 } 101 #endif 102 103 #endif 104