-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaes.c
139 lines (111 loc) · 3.29 KB
/
aes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "aes.h"
void initialize_aes_sbox(u8 sbox[256]) {
u8 p = 1, q = 1;
/* loop invariant: p * q == 1 in the Galois field */
do {
/* multiply p by 3 */
p = p ^ (p << 1) ^ (p & 0x80 ? 0x1B : 0);
/* divide q by 3 (equals multiplication by 0xf6) */
q ^= q << 1;
q ^= q << 2;
q ^= q << 4;
q ^= q & 0x80 ? 0x09 : 0;
/* compute the affine transformation */
u8 xformed = q ^ ROTL8(q, 1) ^ ROTL8(q, 2) ^ ROTL8(q, 3) ^ ROTL8(q, 4);
sbox[p] = xformed ^ 0x63;
} while (p != 1);
/* 0 is a special case since it has no inverse */
sbox[0] = 0x63;
}
void printState (u8 in[16]) {
for(int i=0; i < 4; i++) {
printf("%.2X %.2X %.2X %.2X\n", in[i], in[i+4], in[i+8], in[i+12]);
}
printf("\n");
}
void subBytes (u8 state[16]) {
for (int i = 0; i < 16; i++)
state[i] = SBox[state[i]];
}
void shiftRows (u8 state[16]) {
u8 out[16];
int shiftTab[16] = {0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11};
for (int i = 0; i < 16; i++) {
out[i] = state[shiftTab[i]];
}
memcpy(state, out, sizeof(out));
}
void addRoundKey (u8 state[16], u8 roundKey[16]) {
for (int i = 0; i < 16; i++)
state[i] ^= roundKey[i];
}
u8 gMul (u8 a, u8 b) {
u8 p = 0;
u8 hi_bit_set;
for (int i = 0; i < 8; i++) {
if ((b & 1) == 1)
p ^= a;
hi_bit_set = (a & 0x80);
a <<= 1;
if (hi_bit_set == 0x80)
a ^= 0x1b;
b >>= 1;
}
return p;
}
void mixColumns (u8 state[16]) {
u8 out[16];
for (int i = 0; i < 4; i++) {
out[4*i] = gMul(2, state[4*i]) ^ gMul(3, state[4*i + 1]) ^ state[4*i + 2] ^ state[4*i + 3];
out[4*i + 1] = state[4*i] ^ gMul(2, state[4*i + 1]) ^ gMul(3, state[4*i + 2]) ^ state[4*i + 3];
out[4*i + 2] = state[4*i] ^ state[4*i + 1] ^ gMul(2, state[4*i + 2]) ^ gMul(3, state[4*i + 3]);
out[4*i + 3] = gMul(3, state[4*i]) ^ state[4*i+1] ^ state[4*i + 2] ^ gMul(2, state[4*i + 3]);
}
memcpy(state, out, sizeof(out));
}
void expandKey (u8 key[16], u8 expandedKey[176]) {
u8 tmp[4];
int i = 0;
for (i = 0; i < 4; i++) {
expandedKey[4*i] = key[4*i];
expandedKey[4*i + 1] = key[4*i + 1];
expandedKey[4*i + 2] = key[4*i + 2];
expandedKey[4*i + 3] = key[4*i + 3];
}
for (i = 4; i < 44; i++) {
tmp[0] = expandedKey[4*(i-1)];
tmp[1] = expandedKey[4*(i-1) + 1];
tmp[2] = expandedKey[4*(i-1) + 2];
tmp[3] = expandedKey[4*(i-1) + 3];
if (i % 4 == 0) {
int k = tmp[0];
tmp[0] = SBox[tmp[1]] ^ rCon[i/4];
tmp[1] = SBox[tmp[2]];
tmp[2] = SBox[tmp[3]];
tmp[3] = SBox[k];
}
expandedKey[4*i] = expandedKey[4*(i-4)] ^ tmp[0];
expandedKey[4*i + 1] = expandedKey[4*(i-4) + 1] ^ tmp[1];
expandedKey[4*i + 2] = expandedKey[4*(i-4) + 2] ^ tmp[2];
expandedKey[4*i + 3] = expandedKey[4*(i-4) + 3] ^ tmp[3];
}
}
void aes_128_encrypt (u8 input[16], u8 output[16]) {
u8 expandedKey[176];
u8 key[16] = {0};
expandKey (key, expandedKey);
for (int i = 0; i < 9; i++) {
shiftRows (input);
shiftRows (expandedKey+16*i);
addRoundKey (input, expandedKey + 16*i);
subBytes (input);
mixColumns (input);
}
shiftRows (input);
shiftRows (expandedKey + 144);
addRoundKey (input, expandedKey + 144);
subBytes (input);
addRoundKey (input, expandedKey + 160);
for (int i = 0; i < 16; i++)
output[i] = input[i];
}