monotone

monotone Mtn Source Tree

Root/botan/aes.cpp

1/*************************************************
2* AES Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/aes.h>
7#include <botan/loadstor.h>
8#include <botan/bit_ops.h>
9
10namespace Botan {
11
12/*************************************************
13* AES Encryption *
14*************************************************/
15void AES::enc(const byte in[], byte out[]) const
16 {
17 const u32bit* TE0 = TE;
18 const u32bit* TE1 = TE + 256;
19 const u32bit* TE2 = TE + 512;
20 const u32bit* TE3 = TE + 768;
21
22 u32bit T0, T1, T2, T3, B0, B1, B2, B3;
23 B0 = TE0[in[ 0] ^ ME[ 0]] ^ TE1[in[ 5] ^ ME[ 5]] ^
24 TE2[in[10] ^ ME[10]] ^ TE3[in[15] ^ ME[15]] ^ EK[0];
25 B1 = TE0[in[ 4] ^ ME[ 4]] ^ TE1[in[ 9] ^ ME[ 9]] ^
26 TE2[in[14] ^ ME[14]] ^ TE3[in[ 3] ^ ME[ 3]] ^ EK[1];
27 B2 = TE0[in[ 8] ^ ME[ 8]] ^ TE1[in[13] ^ ME[13]] ^
28 TE2[in[ 2] ^ ME[ 2]] ^ TE3[in[ 7] ^ ME[ 7]] ^ EK[2];
29 B3 = TE0[in[12] ^ ME[12]] ^ TE1[in[ 1] ^ ME[ 1]] ^
30 TE2[in[ 6] ^ ME[ 6]] ^ TE3[in[11] ^ ME[11]] ^ EK[3];
31 for(u32bit j = 1; j != ROUNDS - 1; j += 2)
32 {
33 T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^
34 TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ EK[4*j+0];
35 T1 = TE0[get_byte(0, B1)] ^ TE1[get_byte(1, B2)] ^
36 TE2[get_byte(2, B3)] ^ TE3[get_byte(3, B0)] ^ EK[4*j+1];
37 T2 = TE0[get_byte(0, B2)] ^ TE1[get_byte(1, B3)] ^
38 TE2[get_byte(2, B0)] ^ TE3[get_byte(3, B1)] ^ EK[4*j+2];
39 T3 = TE0[get_byte(0, B3)] ^ TE1[get_byte(1, B0)] ^
40 TE2[get_byte(2, B1)] ^ TE3[get_byte(3, B2)] ^ EK[4*j+3];
41 B0 = TE0[get_byte(0, T0)] ^ TE1[get_byte(1, T1)] ^
42 TE2[get_byte(2, T2)] ^ TE3[get_byte(3, T3)] ^ EK[4*j+4];
43 B1 = TE0[get_byte(0, T1)] ^ TE1[get_byte(1, T2)] ^
44 TE2[get_byte(2, T3)] ^ TE3[get_byte(3, T0)] ^ EK[4*j+5];
45 B2 = TE0[get_byte(0, T2)] ^ TE1[get_byte(1, T3)] ^
46 TE2[get_byte(2, T0)] ^ TE3[get_byte(3, T1)] ^ EK[4*j+6];
47 B3 = TE0[get_byte(0, T3)] ^ TE1[get_byte(1, T0)] ^
48 TE2[get_byte(2, T1)] ^ TE3[get_byte(3, T2)] ^ EK[4*j+7];
49 }
50 out[ 0] = SE[get_byte(0, B0)] ^ ME[16];
51 out[ 1] = SE[get_byte(1, B1)] ^ ME[17];
52 out[ 2] = SE[get_byte(2, B2)] ^ ME[18];
53 out[ 3] = SE[get_byte(3, B3)] ^ ME[19];
54 out[ 4] = SE[get_byte(0, B1)] ^ ME[20];
55 out[ 5] = SE[get_byte(1, B2)] ^ ME[21];
56 out[ 6] = SE[get_byte(2, B3)] ^ ME[22];
57 out[ 7] = SE[get_byte(3, B0)] ^ ME[23];
58 out[ 8] = SE[get_byte(0, B2)] ^ ME[24];
59 out[ 9] = SE[get_byte(1, B3)] ^ ME[25];
60 out[10] = SE[get_byte(2, B0)] ^ ME[26];
61 out[11] = SE[get_byte(3, B1)] ^ ME[27];
62 out[12] = SE[get_byte(0, B3)] ^ ME[28];
63 out[13] = SE[get_byte(1, B0)] ^ ME[29];
64 out[14] = SE[get_byte(2, B1)] ^ ME[30];
65 out[15] = SE[get_byte(3, B2)] ^ ME[31];
66 }
67
68/*************************************************
69* AES Decryption *
70*************************************************/
71void AES::dec(const byte in[], byte out[]) const
72 {
73 const u32bit* TD0 = TD;
74 const u32bit* TD1 = TD + 256;
75 const u32bit* TD2 = TD + 512;
76 const u32bit* TD3 = TD + 768;
77
78 u32bit T0, T1, T2, T3, B0, B1, B2, B3;
79 B0 = TD0[in[ 0] ^ MD[ 0]] ^ TD1[in[13] ^ MD[13]] ^
80 TD2[in[10] ^ MD[10]] ^ TD3[in[ 7] ^ MD[ 7]] ^ DK[0];
81 B1 = TD0[in[ 4] ^ MD[ 4]] ^ TD1[in[ 1] ^ MD[ 1]] ^
82 TD2[in[14] ^ MD[14]] ^ TD3[in[11] ^ MD[11]] ^ DK[1];
83 B2 = TD0[in[ 8] ^ MD[ 8]] ^ TD1[in[ 5] ^ MD[ 5]] ^
84 TD2[in[ 2] ^ MD[ 2]] ^ TD3[in[15] ^ MD[15]] ^ DK[2];
85 B3 = TD0[in[12] ^ MD[12]] ^ TD1[in[ 9] ^ MD[ 9]] ^
86 TD2[in[ 6] ^ MD[ 6]] ^ TD3[in[ 3] ^ MD[ 3]] ^ DK[3];
87 for(u32bit j = 1; j != ROUNDS - 1; j += 2)
88 {
89 T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^
90 TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ DK[4*j+0];
91 T1 = TD0[get_byte(0, B1)] ^ TD1[get_byte(1, B0)] ^
92 TD2[get_byte(2, B3)] ^ TD3[get_byte(3, B2)] ^ DK[4*j+1];
93 T2 = TD0[get_byte(0, B2)] ^ TD1[get_byte(1, B1)] ^
94 TD2[get_byte(2, B0)] ^ TD3[get_byte(3, B3)] ^ DK[4*j+2];
95 T3 = TD0[get_byte(0, B3)] ^ TD1[get_byte(1, B2)] ^
96 TD2[get_byte(2, B1)] ^ TD3[get_byte(3, B0)] ^ DK[4*j+3];
97 B0 = TD0[get_byte(0, T0)] ^ TD1[get_byte(1, T3)] ^
98 TD2[get_byte(2, T2)] ^ TD3[get_byte(3, T1)] ^ DK[4*j+4];
99 B1 = TD0[get_byte(0, T1)] ^ TD1[get_byte(1, T0)] ^
100 TD2[get_byte(2, T3)] ^ TD3[get_byte(3, T2)] ^ DK[4*j+5];
101 B2 = TD0[get_byte(0, T2)] ^ TD1[get_byte(1, T1)] ^
102 TD2[get_byte(2, T0)] ^ TD3[get_byte(3, T3)] ^ DK[4*j+6];
103 B3 = TD0[get_byte(0, T3)] ^ TD1[get_byte(1, T2)] ^
104 TD2[get_byte(2, T1)] ^ TD3[get_byte(3, T0)] ^ DK[4*j+7];
105 }
106 out[ 0] = SD[get_byte(0, B0)] ^ MD[16];
107 out[ 1] = SD[get_byte(1, B3)] ^ MD[17];
108 out[ 2] = SD[get_byte(2, B2)] ^ MD[18];
109 out[ 3] = SD[get_byte(3, B1)] ^ MD[19];
110 out[ 4] = SD[get_byte(0, B1)] ^ MD[20];
111 out[ 5] = SD[get_byte(1, B0)] ^ MD[21];
112 out[ 6] = SD[get_byte(2, B3)] ^ MD[22];
113 out[ 7] = SD[get_byte(3, B2)] ^ MD[23];
114 out[ 8] = SD[get_byte(0, B2)] ^ MD[24];
115 out[ 9] = SD[get_byte(1, B1)] ^ MD[25];
116 out[10] = SD[get_byte(2, B0)] ^ MD[26];
117 out[11] = SD[get_byte(3, B3)] ^ MD[27];
118 out[12] = SD[get_byte(0, B3)] ^ MD[28];
119 out[13] = SD[get_byte(1, B2)] ^ MD[29];
120 out[14] = SD[get_byte(2, B1)] ^ MD[30];
121 out[15] = SD[get_byte(3, B0)] ^ MD[31];
122 }
123
124/*************************************************
125* AES Key Schedule *
126*************************************************/
127void AES::key(const byte key[], u32bit length)
128 {
129 static const u32bit RC[10] = {
130 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
131 0x40000000, 0x80000000, 0x1B000000, 0x36000000 };
132 ROUNDS = (length / 4) + 6;
133
134 SecureBuffer<u32bit, 64> XEK, XDK;
135
136 const u32bit X = length / 4;
137 for(u32bit j = 0; j != X; ++j)
138 XEK[j] = load_be<u32bit>(key, j);
139
140 for(u32bit j = X; j < 4*(ROUNDS+1); j += X)
141 {
142 XEK[j] = XEK[j-X] ^ S(rotate_left(XEK[j-1], 8)) ^ RC[(j-X)/X];
143 for(u32bit k = 1; k != X; ++k)
144 {
145 if(X == 8 && k == 4)
146 XEK[j+k] = XEK[j+k-X] ^ S(XEK[j+k-1]);
147 else
148 XEK[j+k] = XEK[j+k-X] ^ XEK[j+k-1];
149 }
150 }
151
152 for(u32bit j = 0; j != 4*(ROUNDS+1); j += 4)
153 {
154 XDK[j ] = XEK[4*ROUNDS-j ];
155 XDK[j+1] = XEK[4*ROUNDS-j+1];
156 XDK[j+2] = XEK[4*ROUNDS-j+2];
157 XDK[j+3] = XEK[4*ROUNDS-j+3];
158 }
159
160 for(u32bit j = 4; j != length + 24; ++j)
161 XDK[j] = TD[SE[get_byte(0, XDK[j])] + 0] ^
162 TD[SE[get_byte(1, XDK[j])] + 256] ^
163 TD[SE[get_byte(2, XDK[j])] + 512] ^
164 TD[SE[get_byte(3, XDK[j])] + 768];
165
166 for(u32bit j = 0; j != 4; ++j)
167 for(u32bit k = 0; k != 4; ++k)
168 {
169 ME[4*j+k ] = get_byte(k, XEK[j]);
170 ME[4*j+k+16] = get_byte(k, XEK[j+4*ROUNDS]);
171 MD[4*j+k ] = get_byte(k, XDK[j]);
172 MD[4*j+k+16] = get_byte(k, XEK[j]);
173 }
174
175 EK.copy(XEK + 4, length + 20);
176 DK.copy(XDK + 4, length + 20);
177 }
178
179/*************************************************
180* AES Byte Substitution *
181*************************************************/
182u32bit AES::S(u32bit input)
183 {
184 return make_u32bit(SE[get_byte(0, input)], SE[get_byte(1, input)],
185 SE[get_byte(2, input)], SE[get_byte(3, input)]);
186 }
187
188/*************************************************
189* AES Constructor *
190*************************************************/
191AES::AES(u32bit key_size) : BlockCipher(16, key_size)
192 {
193 if(key_size != 16 && key_size != 24 && key_size != 32)
194 throw Invalid_Key_Length(name(), key_size);
195 ROUNDS = (key_size / 4) + 6;
196 }
197
198/*************************************************
199* Clear memory of sensitive data *
200*************************************************/
201void AES::clear() throw()
202 {
203 EK.clear();
204 DK.clear();
205 ME.clear();
206 MD.clear();
207 }
208
209}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status