-
Notifications
You must be signed in to change notification settings - Fork 0
/
aesinit.m
167 lines (151 loc) · 4.38 KB
/
aesinit.m
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
function s = aesinit(key)
% AESINIT Generate structure with s-boxes, expanded key, etc.
% Usage: s = aesinit([23 34 168 ... 39])
% key: 16 (AES-128), 24 (AES-192), and 32 (AES-256)
% items array with bytes of key
% s: AES structure for AES parameters and tables
% Stepan Matejka, 2011, matejka[at]feld.cvut.cz
% $Revision: 1.1.0 $ $Date: 2011/10/12 $
validateattributes(key,...
{'numeric'},...
{'real', 'vector', '>=', 0, '<=', 255});
key = key(:);
lengthkey = length(key);
switch (lengthkey)
case 16
rounds = 10;
case 24
rounds = 12;
case 32
rounds = 14;
otherwise
error('Only AES-128, AES-192, and AES-256 are supported.');
end
% fill s structure
s = {};
s.key = key;
s.bytes = lengthkey;
s.length = lengthkey * 8;
s.rounds = rounds;
% irreducible polynomial for multiplication in a finite field 0x11b
% bin2dec('100011011');
s.mod_pol = 283;
% first build logarithm lookup table and it's inverse
aes_logt = zeros(1,256);
aes_ilogt = zeros(1,256);
gen = 1;
for i = 0:255
aes_logt(gen + 1) = i;
aes_ilogt(i + 1) = gen;
gen = poly_mult(gen, 3, s.mod_pol);
end
% store log tables
s.aes_logt = aes_logt;
s.aes_ilogt = aes_ilogt;
% build s-box and it's inverse
s_box = zeros(1,256);
loctable = [1 2 4 8 16 32 64 128 1 2 4 8 16 32 64 128];
for i = 0:255
if (i == 0)
inv = 0;
else
inv = aes_ilogt(255 - aes_logt(i + 1) + 1);
end
temp = 0;
for bi = 0:7
temp2 = sign(bitand(inv, loctable(bi + 1)));
temp2 = temp2 + sign(bitand(inv, loctable(bi + 4 + 1)));
temp2 = temp2 + sign(bitand(inv, loctable(bi + 5 + 1)));
temp2 = temp2 + sign(bitand(inv, loctable(bi + 6 + 1)));
temp2 = temp2 + sign(bitand(inv, loctable(bi + 7 + 1)));
temp2 = temp2 + sign(bitand(99, loctable(bi + 1)));
if (rem(temp2,2))
temp = bitor(temp, loctable(bi + 1));
end
end
s_box(i + 1) = temp;
end
inv_s_box(s_box(1:256) + 1) = (0:255);
% table correction (must be)
s_box(1 + 1) = 124;
inv_s_box(124 + 1) = 1;
inv_s_box(99 + 1) = 0;
s.s_box = s_box;
s.inv_s_box = inv_s_box;
% tables for fast MixColumns
mix_col2 = zeros(1,256);
mix_col3 = mix_col2;
mix_col9 = mix_col2;
mix_col11 = mix_col2;
mix_col13 = mix_col2;
mix_col14 = mix_col2;
for i = 1:256
mix_col2(i) = poly_mult(2, i - 1, s.mod_pol);
mix_col3(i) = poly_mult(3, i - 1, s.mod_pol);
mix_col9(i) = poly_mult(9, i - 1, s.mod_pol);
mix_col11(i) = poly_mult(11, i - 1, s.mod_pol);
mix_col13(i) = poly_mult(13, i - 1, s.mod_pol);
mix_col14(i) = poly_mult(14, i - 1, s.mod_pol);
end
s.mix_col2 = mix_col2;
s.mix_col3 = mix_col3;
s.mix_col9 = mix_col9;
s.mix_col11 = mix_col11;
s.mix_col13 = mix_col13;
s.mix_col14 = mix_col14;
% expanded key
s.keyexp = key_expansion(s.key, s.s_box, s.rounds, s.mod_pol, s.aes_logt, s.aes_ilogt);
% poly & invpoly
s.poly_mat = [...
2 3 1 1;...
1 2 3 1;...
1 1 2 3;...
3 1 1 2];
s.inv_poly_mat =[...
14 11 13 9;...
9 14 11 13;...
13 9 14 11;...
11 13 9 14];
% end of aesinit.m
% ------------------------------------------------------------------------
function p = poly_mult(a, b, mod_pol)
% Multiplication in a finite field
% For loop multiplication - slower than log/ilog tables
% but must be used for log/ilog tables generation
p = 0;
for counter = 1 : 8
if (rem(b, 2))
p = bitxor(p, a);
b = (b - 1)/2;
else
b = b/2;
end
a = 2*a;
if (a > 255)
a = bitxor(a, mod_pol);
end
end
function expkey = key_expansion(key, s_box, rounds, mod_pol, aes_logt, aes_ilogt)
% Expansion of key
rcon = 1;
kcol = length(key)/4;
expkey = (reshape(key,4,kcol))';
for i = kcol:(4*(rounds + 1) - 1)
% copy the previous row of the expanded key into a buffer
temp = expkey(i, :);
% each kcol row
if (mod(i, kcol) == 0)
% rotate
temp = temp([2 3 4 1]);
% s-box
temp = s_box(temp + 1);
% xor
temp(1) = bitxor(temp(1), rcon);
rcon = poly_mult(rcon, 2, mod_pol);
else
if ((kcol > 6) && (mod(i, kcol) == 4))
temp = s_box(temp + 1);
end
end
expkey(i + 1, :) = bitxor(expkey(i - kcol + 1, :), temp);
end