monotone

monotone Mtn Source Tree

Root/botan/aes.cpp

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

Archive Download this file

Branches

Tags

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