aboutsummaryrefslogtreecommitdiffstats
path: root/src/block/aes/aes.cpp
blob: 6119281bd18307df8651bc1d24db7b2f1336d7e1 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* AES Source File
* (C) 1999-2007 Jack Lloyd
*/

#include <botan/aes.h>
#include <botan/loadstor.h>

namespace Botan {

/**
* AES Encryption
*/
void AES::enc(const byte in[], byte out[]) const
   {
   const u32bit* TE0 = TE;
   const u32bit* TE1 = TE + 256;
   const u32bit* TE2 = TE + 512;
   const u32bit* TE3 = TE + 768;

   u32bit T0 = load_be<u32bit>(in, 0) ^ EK[0];
   u32bit T1 = load_be<u32bit>(in, 1) ^ EK[1];
   u32bit T2 = load_be<u32bit>(in, 2) ^ EK[2];
   u32bit T3 = load_be<u32bit>(in, 3) ^ EK[3];

   u32bit B0, B1, B2, B3;
   B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^
        TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ EK[4];
   B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^
        TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ EK[5];
   B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^
        TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ EK[6];
   B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^
        TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ EK[7];

   for(u32bit j = 2; j != ROUNDS; j += 2)
      {
      const u32bit K0 = EK[4*j];
      const u32bit K1 = EK[4*j+1];
      const u32bit K2 = EK[4*j+2];
      const u32bit K3 = EK[4*j+3];

      T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^
           TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ K0;
      T1 = TE0[get_byte(0, B1)] ^ TE1[get_byte(1, B2)] ^
           TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ K1;
      T2 = TE0[get_byte(0, B2)] ^ TE1[get_byte(1, B3)] ^
           TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ K2;
      T3 = TE0[get_byte(0, B3)] ^ TE1[get_byte(1, B0)] ^
           TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ K3;

      const u32bit K4 = EK[4*(j+1)+0];
      const u32bit K5 = EK[4*(j+1)+1];
      const u32bit K6 = EK[4*(j+1)+2];
      const u32bit K7 = EK[4*(j+1)+3];

      B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^
           TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ K4;
      B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^
           TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ K5;
      B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^
           TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ K6;
      B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^
           TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ K7;
      }

   out[ 0] = SE[get_byte(0, B0)] ^ ME[0];
   out[ 1] = SE[get_byte(1, B1)] ^ ME[1];
   out[ 2] = SE[get_byte(2, B2)] ^ ME[2];
   out[ 3] = SE[get_byte(3, B3)] ^ ME[3];
   out[ 4] = SE[get_byte(0, B1)] ^ ME[4];
   out[ 5] = SE[get_byte(1, B2)] ^ ME[5];
   out[ 6] = SE[get_byte(2, B3)] ^ ME[6];
   out[ 7] = SE[get_byte(3, B0)] ^ ME[7];
   out[ 8] = SE[get_byte(0, B2)] ^ ME[8];
   out[ 9] = SE[get_byte(1, B3)] ^ ME[9];
   out[10] = SE[get_byte(2, B0)] ^ ME[10];
   out[11] = SE[get_byte(3, B1)] ^ ME[11];
   out[12] = SE[get_byte(0, B3)] ^ ME[12];
   out[13] = SE[get_byte(1, B0)] ^ ME[13];
   out[14] = SE[get_byte(2, B1)] ^ ME[14];
   out[15] = SE[get_byte(3, B2)] ^ ME[15];
   }

/**
* AES Decryption
*/
void AES::dec(const byte in[], byte out[]) const
   {
   const u32bit* TD0 = TD;
   const u32bit* TD1 = TD + 256;
   const u32bit* TD2 = TD + 512;
   const u32bit* TD3 = TD + 768;

   u32bit B0 = TD0[in[ 0] ^ MD[ 0]] ^ TD1[in[13] ^ MD[13]] ^
               TD2[in[10] ^ MD[10]] ^ TD3[in[ 7] ^ MD[ 7]] ^ DK[0];
   u32bit B1 = TD0[in[ 4] ^ MD[ 4]] ^ TD1[in[ 1] ^ MD[ 1]] ^
               TD2[in[14] ^ MD[14]] ^ TD3[in[11] ^ MD[11]] ^ DK[1];
   u32bit B2 = TD0[in[ 8] ^ MD[ 8]] ^ TD1[in[ 5] ^ MD[ 5]] ^
               TD2[in[ 2] ^ MD[ 2]] ^ TD3[in[15] ^ MD[15]] ^ DK[2];
   u32bit B3 = TD0[in[12] ^ MD[12]] ^ TD1[in[ 9] ^ MD[ 9]] ^
               TD2[in[ 6] ^ MD[ 6]] ^ TD3[in[ 3] ^ MD[ 3]] ^ DK[3];

   for(u32bit j = 1; j != ROUNDS - 1; j += 2)
      {
      const u32bit K0 = DK[4*j+0];
      const u32bit K1 = DK[4*j+1];
      const u32bit K2 = DK[4*j+2];
      const u32bit K3 = DK[4*j+3];
      const u32bit K4 = DK[4*j+4];
      const u32bit K5 = DK[4*j+5];
      const u32bit K6 = DK[4*j+6];
      const u32bit K7 = DK[4*j+7];

      u32bit T0, T1, T2, T3;

      T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^
           TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ K0;
      T1 = TD0[get_byte(0, B1)] ^ TD1[get_byte(1, B0)] ^
           TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ K1;
      T2 = TD0[get_byte(0, B2)] ^ TD1[get_byte(1, B1)] ^
           TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ K2;
      T3 = TD0[get_byte(0, B3)] ^ TD1[get_byte(1, B2)] ^
           TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ K3;

      B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^
           TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ K4;
      B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^
           TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ K5;
      B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^
           TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ K6;
      B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^
           TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ K7;
      }

   out[ 0] = SD[get_byte(0, B0)] ^ MD[16];
   out[ 1] = SD[get_byte(1, B3)] ^ MD[17];
   out[ 2] = SD[get_byte(2, B2)] ^ MD[18];
   out[ 3] = SD[get_byte(3, B1)] ^ MD[19];
   out[ 4] = SD[get_byte(0, B1)] ^ MD[20];
   out[ 5] = SD[get_byte(1, B0)] ^ MD[21];
   out[ 6] = SD[get_byte(2, B3)] ^ MD[22];
   out[ 7] = SD[get_byte(3, B2)] ^ MD[23];
   out[ 8] = SD[get_byte(0, B2)] ^ MD[24];
   out[ 9] = SD[get_byte(1, B1)] ^ MD[25];
   out[10] = SD[get_byte(2, B0)] ^ MD[26];
   out[11] = SD[get_byte(3, B3)] ^ MD[27];
   out[12] = SD[get_byte(0, B3)] ^ MD[28];
   out[13] = SD[get_byte(1, B2)] ^ MD[29];
   out[14] = SD[get_byte(2, B1)] ^ MD[30];
   out[15] = SD[get_byte(3, B0)] ^ MD[31];
   }

/**
* AES Key Schedule
*/
void AES::key_schedule(const byte key[], u32bit length)
   {
   static const u32bit RC[10] = {
      0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
      0x40000000, 0x80000000, 0x1B000000, 0x36000000 };
   ROUNDS = (length / 4) + 6;

   SecureBuffer<u32bit, 64> XEK, XDK;

   const u32bit X = length / 4;
   for(u32bit j = 0; j != X; ++j)
      XEK[j] = load_be<u32bit>(key, j);

   for(u32bit j = X; j < 4*(ROUNDS+1); j += X)
      {
      XEK[j] = XEK[j-X] ^ S(rotate_left(XEK[j-1], 8)) ^ RC[(j-X)/X];
      for(u32bit k = 1; k != X; ++k)
         {
         if(X == 8 && k == 4)
            XEK[j+k] = XEK[j+k-X] ^ S(XEK[j+k-1]);
         else
            XEK[j+k] = XEK[j+k-X] ^ XEK[j+k-1];
         }
      }

   for(u32bit j = 0; j != 4*(ROUNDS+1); j += 4)
      {
      XDK[j  ] = XEK[4*ROUNDS-j  ];
      XDK[j+1] = XEK[4*ROUNDS-j+1];
      XDK[j+2] = XEK[4*ROUNDS-j+2];
      XDK[j+3] = XEK[4*ROUNDS-j+3];
      }

   for(u32bit j = 4; j != length + 24; ++j)
      XDK[j] = TD[SE[get_byte(0, XDK[j])] +   0] ^
               TD[SE[get_byte(1, XDK[j])] + 256] ^
               TD[SE[get_byte(2, XDK[j])] + 512] ^
               TD[SE[get_byte(3, XDK[j])] + 768];

   for(u32bit j = 0; j != 4; ++j)
      for(u32bit k = 0; k != 4; ++k)
         {
         ME[4*j+k   ] = get_byte(k, XEK[j+4*ROUNDS]);
         MD[4*j+k   ] = get_byte(k, XDK[j]);
         MD[4*j+k+16] = get_byte(k, XEK[j]);
         }

   EK.copy(XEK, length + 24);
   DK.copy(XDK + 4, length + 20);
   }

/**
* AES Byte Substitution
*/
u32bit AES::S(u32bit input)
   {
   return make_u32bit(SE[get_byte(0, input)], SE[get_byte(1, input)],
                      SE[get_byte(2, input)], SE[get_byte(3, input)]);
   }

/**
* AES Constructor
*/
AES::AES(u32bit key_size) : BlockCipher(16, key_size)
   {
   if(key_size != 16 && key_size != 24 && key_size != 32)
      throw Invalid_Key_Length(name(), key_size);
   ROUNDS = (key_size / 4) + 6;
   }

/**
* Clear memory of sensitive data
*/
void AES::clear() throw()
   {
   EK.clear();
   DK.clear();
   ME.clear();
   MD.clear();
   }

}