# AES加密算法C代码分析

0.引言

1.AES加密算法代码分析

1）数据结构

typedef struct
{
int nr;                     /*!<  number of rounds  */
uint32_t *rk;               /*!<  AES round keys    */
uint32_t buf[68];           /*!<  unaligned data    */
}
mbedtls_aes_context;

2）算法主体

 1 void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
2                           const unsigned char input[16],
3                           unsigned char output[16] )
4 {
5     int i;
6     uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
7
8     RK = ctx->rk;
9
10     GET_UINT32_LE( X0, input,  0 ); X0 ^= *RK++;
11     GET_UINT32_LE( X1, input,  4 ); X1 ^= *RK++;
12     GET_UINT32_LE( X2, input,  8 ); X2 ^= *RK++;
13     GET_UINT32_LE( X3, input, 12 ); X3 ^= *RK++;
14
15     for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
16     {
17         AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
18         AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
19     }
20
21     AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
22
23     X0 = *RK++ ^ \
24             ( (uint32_t) FSb[ ( Y0       ) & 0xFF ]       ) ^
25             ( (uint32_t) FSb[ ( Y1 >>  8 ) & 0xFF ] <<  8 ) ^
26             ( (uint32_t) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
27             ( (uint32_t) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
28
29     X1 = *RK++ ^ \
30             ( (uint32_t) FSb[ ( Y1       ) & 0xFF ]       ) ^
31             ( (uint32_t) FSb[ ( Y2 >>  8 ) & 0xFF ] <<  8 ) ^
32             ( (uint32_t) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
33             ( (uint32_t) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
34
35     X2 = *RK++ ^ \
36             ( (uint32_t) FSb[ ( Y2       ) & 0xFF ]       ) ^
37             ( (uint32_t) FSb[ ( Y3 >>  8 ) & 0xFF ] <<  8 ) ^
38             ( (uint32_t) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
39             ( (uint32_t) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
40
41     X3 = *RK++ ^ \
42             ( (uint32_t) FSb[ ( Y3       ) & 0xFF ]       ) ^
43             ( (uint32_t) FSb[ ( Y0 >>  8 ) & 0xFF ] <<  8 ) ^
44             ( (uint32_t) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
45             ( (uint32_t) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
46
47     PUT_UINT32_LE( X0, output,  0 );
48     PUT_UINT32_LE( X1, output,  4 );
49     PUT_UINT32_LE( X2, output,  8 );
50     PUT_UINT32_LE( X3, output, 12 );
51 }

AES_ROUND由宏定义得到，代码如下：

 1 #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3)     \
2 {                                               \
3     X0 = *RK++ ^ FT0[ ( Y0       ) & 0xFF ] ^   \
4                  FT1[ ( Y1 >>  8 ) & 0xFF ] ^   \
5                  FT2[ ( Y2 >> 16 ) & 0xFF ] ^   \
6                  FT3[ ( Y3 >> 24 ) & 0xFF ];    \
7                                                 \
8     X1 = *RK++ ^ FT0[ ( Y1       ) & 0xFF ] ^   \
9                  FT1[ ( Y2 >>  8 ) & 0xFF ] ^   \
10                  FT2[ ( Y3 >> 16 ) & 0xFF ] ^   \
11                  FT3[ ( Y0 >> 24 ) & 0xFF ];    \
12                                                 \
13     X2 = *RK++ ^ FT0[ ( Y2       ) & 0xFF ] ^   \
14                  FT1[ ( Y3 >>  8 ) & 0xFF ] ^   \
15                  FT2[ ( Y0 >> 16 ) & 0xFF ] ^   \
16                  FT3[ ( Y1 >> 24 ) & 0xFF ];    \
17                                                 \
18     X3 = *RK++ ^ FT0[ ( Y3       ) & 0xFF ] ^   \
19                  FT1[ ( Y0 >>  8 ) & 0xFF ] ^   \
20                  FT2[ ( Y1 >> 16 ) & 0xFF ] ^   \
21                  FT3[ ( Y2 >> 24 ) & 0xFF ];    \
22 }

mbedTLS给出了两种实现，1.ROM_TABLE的方式，所有表格直接给出，不再一一列出各种表格；2.表格预计算的方式，由于AES的设计是基于有限域的，表格预计算需要一些有限域的辅助函数，整个预计算的过程如下：

 1 #define ROTL8(x) ( ( x << 8 ) & 0xFFFFFFFF ) | ( x >> 24 )
2 #define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) )
3 #define MUL(x,y) ( ( x && y ) ? pow[(log[x]+log[y]) % 255] : 0 )
4
5 static int aes_init_done = 0;
6
7 static void aes_gen_tables( void )
8 {
9     int i, x, y, z;
10     int pow[256];
11     int log[256];
12
13     /*
14      * compute pow and log tables over GF(2^8)
15      */
16     for( i = 0, x = 1; i < 256; i++ )
17     {
18         pow[i] = x;
19         log[x] = i;
20         x = ( x ^ XTIME( x ) ) & 0xFF;
21     }
22
23     /*
24      * calculate the round constants
25      */
26     for( i = 0, x = 1; i < 10; i++ )
27     {
28         RCON[i] = (uint32_t) x;
29         x = XTIME( x ) & 0xFF;
30     }
31
32     /*
33      * generate the forward and reverse S-boxes
34      */
35     FSb[0x00] = 0x63;
36     RSb[0x63] = 0x00;
37
38     for( i = 1; i < 256; i++ )
39     {
40         x = pow[255 - log[i]];
41
42         y  = x; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
43         x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
44         x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
45         x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
46         x ^= y ^ 0x63;
47
48         FSb[i] = (unsigned char) x;
49         RSb[x] = (unsigned char) i;
50     }
51
52     /*
53      * generate the forward and reverse tables
54      */
55     for( i = 0; i < 256; i++ )
56     {
57         x = FSb[i];
58         y = XTIME( x ) & 0xFF;
59         z =  ( y ^ x ) & 0xFF;
60
61         FT0[i] = ( (uint32_t) y       ) ^
62                  ( (uint32_t) x <<  8 ) ^
63                  ( (uint32_t) x << 16 ) ^
64                  ( (uint32_t) z << 24 );
65
66         FT1[i] = ROTL8( FT0[i] );
67         FT2[i] = ROTL8( FT1[i] );
68         FT3[i] = ROTL8( FT2[i] );
69
70         x = RSb[i];
71
72         RT0[i] = ( (uint32_t) MUL( 0x0E, x )       ) ^
73                  ( (uint32_t) MUL( 0x09, x ) <<  8 ) ^
74                  ( (uint32_t) MUL( 0x0D, x ) << 16 ) ^
75                  ( (uint32_t) MUL( 0x0B, x ) << 24 );
76
77         RT1[i] = ROTL8( RT0[i] );
78         RT2[i] = ROTL8( RT1[i] );
79         RT3[i] = ROTL8( RT2[i] );
80     }
81 }

38~50行代码进行有限域求逆算法（使用对数表），再进行仿射变换，求得S盒，同时可以得到逆向S盒。AES的S盒设计为（A*x-1+b)，A为2进制矩阵，b为2进制列向量（0x63）。

2.算法简介

……

#### C语言实现AES算法加密/解密算法---以前写的，整理下

2012-11-16 00:42:13

#### C语言实现数据保护算法（2）——AES加解密

2017-09-02 10:59:57

#### AES加密解密算法的C代码实现

2011-03-21 18:18:00

#### AES加密算法的C++实现

2014-12-13 14:25:05

#### AES加密算法C语言实现，有测试代码，可直接使用

2014年09月04日 11KB 下载

#### 基于c语言aes加密算法

2015-02-09 18:49:25

#### AES加密算法（c语言）

2016-01-26 13:28:54

#### AES加密算法源代码(c语言版)

2008-05-23 13:35:00

#### C语言实现AES（标准方法）

2015-05-17 00:02:59

#### AES C代码实现

2016年01月11日 18KB 下载