Cryptography Reference
In-Depth Information
Listing 2-34: “aes.c” sub_bytes
static void sub_bytes( unsigned char state[ ][ 4 ] )
{
int r, c;
for ( r = 0; r < 4; r++ )
{
for ( c = 0; c < 4; c++ )
{
state[ r ][ c ] = sbox[ ( state[ r ][ c ] & 0xF0 ) >> 4 ]
[ state[ r ][ c ] & 0x0F ];
}
}
}
Row shifting is a rotation applied to each row. The fi rst row is rotated zero
places, the second one place, the third two, and the fourth three. Graphically,
this can be viewed as shown in Figure 2-13.
0
4
8
C
0
4
8
C
1
5
9
D
D
1
5
9
2
6
A
E
A
E
2
6
3
7
B
F
7
B
F
3
Figure 2-13: AES row shift
In code, this is shown in Listing 2-35.
Listing 2-35: “aes.c” shift_rows
static void shift_rows( unsigned char state[ ][ 4 ] )
{
int tmp;
tmp = state[ 1 ][ 0 ];
state[ 1 ][ 0 ] = state[ 1 ][ 1 ];
state[ 1 ][ 1 ] = state[ 1 ][ 2 ];
state[ 1 ][ 2 ] = state[ 1 ][ 3 ];
state[ 1 ][ 3 ] = tmp;
tmp = state[ 2 ][ 0 ];
state[ 2 ][ 0 ] = state[ 2 ][ 2 ];
(Continued)
Search WWH ::




Custom Search