这个AES 的C 语言实现,是从linux 中port 而来,放在这里方便自己日后使用。 很容易把这个code 转为为C++ 的封装。
aes.h
aes.c
aes.h
a#ifndef __AES_H__
#define __AES_H__
#define AES_MIN_KEY_SIZE 16
#define AES_MAX_KEY_SIZE 32
#define AES_BLOCK_SIZE 16
typedef unsigned char u8;
typedef signed char s8;
typedef signed short s16;
typedef unsigned short u16;
typedef signed int s32;
typedef unsigned int u32;
typedef signed long long s64;
typedef unsigned long long u64;
typedef u16 __le16;
typedef u32 __le32;
#define E_KEY (&ctx->buf[0])
#define D_KEY (&ctx->buf[60])
#define le32_to_cpu
#define cpu_to_le32
struct aes_ctx
{
int key_length;
u32 buf[ 120 ];
};
void gen_tabs ( void );
int aes_set_key( struct aes_ctx * ctx, const u8 * in_key, unsigned int key_len);
void aes_encrypt( struct aes_ctx * ctx, u8 * out , const u8 * in );
void aes_decrypt( struct aes_ctx * ctx, u8 * out , const u8 * in );
#endif
#define __AES_H__
#define AES_MIN_KEY_SIZE 16
#define AES_MAX_KEY_SIZE 32
#define AES_BLOCK_SIZE 16
typedef unsigned char u8;
typedef signed char s8;
typedef signed short s16;
typedef unsigned short u16;
typedef signed int s32;
typedef unsigned int u32;
typedef signed long long s64;
typedef unsigned long long u64;
typedef u16 __le16;
typedef u32 __le32;
#define E_KEY (&ctx->buf[0])
#define D_KEY (&ctx->buf[60])
#define le32_to_cpu
#define cpu_to_le32
struct aes_ctx
{
int key_length;
u32 buf[ 120 ];
};
void gen_tabs ( void );
int aes_set_key( struct aes_ctx * ctx, const u8 * in_key, unsigned int key_len);
void aes_encrypt( struct aes_ctx * ctx, u8 * out , const u8 * in );
void aes_decrypt( struct aes_ctx * ctx, u8 * out , const u8 * in );
#endif
aes.c
#include
<
stdio.h
>
#include " aes.h "
static u8 pow_tab[ 256 ];
static u8 log_tab[ 256 ];
static u8 sbx_tab[ 256 ];
static u8 isb_tab[ 256 ];
static u32 rco_tab[ 10 ];
static u32 ft_tab[ 4 ][ 256 ];
static u32 it_tab[ 4 ][ 256 ];
static u32 fl_tab[ 4 ][ 256 ];
static u32 il_tab[ 4 ][ 256 ];
static inline u8 byte ( const u32 x , const unsigned n)
{
return x >> (n << 3 );
}
static inline u32 rol32(u32 word, unsigned int shift)
{
return (word << shift) | (word >> ( 32 - shift));
}
static inline u32 ror32(u32 word, unsigned int shift)
{
return (word >> shift) | (word << ( 32 - shift));
}
static inline u8 f_mult(u8 a , u8 b )
{
u8 aa = log_tab[a];
u8 cc = aa + log_tab[b];
return pow_tab[cc + (cc < aa ? 1 : 0 )];
}
#define ff_mult(a,b) (a && b ? f_mult(a,b) : 0 )
#define f_rn(bo, bi, n, k)
bo[n] = ft_tab[ 0 ][ byte (bi[n], 0 )] ^
ft_tab[ 1 ][ byte (bi[(n + 1 ) & 3 ], 1 )] ^
ft_tab[ 2 ][ byte (bi[(n + 2 ) & 3 ], 2 )] ^
ft_tab[ 3 ][ byte (bi[(n + 3 ) & 3 ], 3 )] ^ * (k + n)
#define i_rn(bo, bi, n, k)
bo[n] = it_tab[ 0 ][ byte (bi[n], 0 )] ^
it_tab[ 1 ][ byte (bi[(n + 3 ) & 3 ], 1 )] ^
it_tab[ 2 ][ byte (bi[(n + 2 ) & 3 ], 2 )] ^
it_tab[ 3 ][ byte (bi[(n + 1 ) & 3 ], 3 )] ^ * (k + n)
#define ls_box(x)
( fl_tab[ 0 ][ byte (x, 0 )] ^
fl_tab[ 1 ][ byte (x, 1 )] ^
fl_tab[ 2 ][ byte (x, 2 )] ^
#include " aes.h "
static u8 pow_tab[ 256 ];
static u8 log_tab[ 256 ];
static u8 sbx_tab[ 256 ];
static u8 isb_tab[ 256 ];
static u32 rco_tab[ 10 ];
static u32 ft_tab[ 4 ][ 256 ];
static u32 it_tab[ 4 ][ 256 ];
static u32 fl_tab[ 4 ][ 256 ];
static u32 il_tab[ 4 ][ 256 ];
static inline u8 byte ( const u32 x , const unsigned n)
{
return x >> (n << 3 );
}
static inline u32 rol32(u32 word, unsigned int shift)
{
return (word << shift) | (word >> ( 32 - shift));
}
static inline u32 ror32(u32 word, unsigned int shift)
{
return (word >> shift) | (word << ( 32 - shift));
}
static inline u8 f_mult(u8 a , u8 b )
{
u8 aa = log_tab[a];
u8 cc = aa + log_tab[b];
return pow_tab[cc + (cc < aa ? 1 : 0 )];
}
#define ff_mult(a,b) (a && b ? f_mult(a,b) : 0 )
#define f_rn(bo, bi, n, k)
bo[n] = ft_tab[ 0 ][ byte (bi[n], 0 )] ^
ft_tab[ 1 ][ byte (bi[(n + 1 ) & 3 ], 1 )] ^
ft_tab[ 2 ][ byte (bi[(n + 2 ) & 3 ], 2 )] ^
ft_tab[ 3 ][ byte (bi[(n + 3 ) & 3 ], 3 )] ^ * (k + n)
#define i_rn(bo, bi, n, k)
bo[n] = it_tab[ 0 ][ byte (bi[n], 0 )] ^
it_tab[ 1 ][ byte (bi[(n + 3 ) & 3 ], 1 )] ^
it_tab[ 2 ][ byte (bi[(n + 2 ) & 3 ], 2 )] ^
it_tab[ 3 ][ byte (bi[(n + 1 ) & 3 ], 3 )] ^ * (k + n)
#define ls_box(x)
( fl_tab[ 0 ][ byte (x, 0 )] ^
fl_tab[ 1 ][ byte (x, 1 )] ^
fl_tab[ 2 ][ byte (x, 2 )] ^