c 语言aes算法,AES 加密算法的 C 语言实现

  • Post author:
  • Post category:其他


#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

)]

^

fl_tab[

3

][

byte

(x,

3

)] )

#define

f_rl(bo, bi, n, k)

bo[n]

=

fl_tab[

0

][

byte

(bi[n],

0

)]

^

fl_tab[

1

][

byte

(bi[(n

+

1

)

&

3

],

1

)]

^

fl_tab[

2

][

byte

(bi[(n

+

2

)

&

3

],

2

)]

^

fl_tab[

3

][

byte

(bi[(n

+

3

)

&

3

],

3

)]

^

*

(k

+

n)

#define

i_rl(bo, bi, n, k)

bo[n]

=

il_tab[

0

][

byte

(bi[