Botan  1.10.9
des.cpp
Go to the documentation of this file.
1 /*
2 * DES
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Based on a public domain implemenation by Phil Karn (who in turn
6 * credited Richard Outerbridge and Jim Gillogly)
7 *
8 * Distributed under the terms of the Botan license
9 */
10 
11 #include <botan/des.h>
12 #include <botan/loadstor.h>
13 #include <botan/rotate.h>
14 
15 namespace Botan {
16 
17 namespace {
18 
19 /*
20 * DES Key Schedule
21 */
22 void des_key_schedule(u32bit round_key[32], const byte key[8])
23  {
24  static const byte ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
25  1, 2, 2, 2, 2, 2, 2, 1 };
26 
27  u32bit C = ((key[7] & 0x80) << 20) | ((key[6] & 0x80) << 19) |
28  ((key[5] & 0x80) << 18) | ((key[4] & 0x80) << 17) |
29  ((key[3] & 0x80) << 16) | ((key[2] & 0x80) << 15) |
30  ((key[1] & 0x80) << 14) | ((key[0] & 0x80) << 13) |
31  ((key[7] & 0x40) << 13) | ((key[6] & 0x40) << 12) |
32  ((key[5] & 0x40) << 11) | ((key[4] & 0x40) << 10) |
33  ((key[3] & 0x40) << 9) | ((key[2] & 0x40) << 8) |
34  ((key[1] & 0x40) << 7) | ((key[0] & 0x40) << 6) |
35  ((key[7] & 0x20) << 6) | ((key[6] & 0x20) << 5) |
36  ((key[5] & 0x20) << 4) | ((key[4] & 0x20) << 3) |
37  ((key[3] & 0x20) << 2) | ((key[2] & 0x20) << 1) |
38  ((key[1] & 0x20) ) | ((key[0] & 0x20) >> 1) |
39  ((key[7] & 0x10) >> 1) | ((key[6] & 0x10) >> 2) |
40  ((key[5] & 0x10) >> 3) | ((key[4] & 0x10) >> 4);
41  u32bit D = ((key[7] & 0x02) << 26) | ((key[6] & 0x02) << 25) |
42  ((key[5] & 0x02) << 24) | ((key[4] & 0x02) << 23) |
43  ((key[3] & 0x02) << 22) | ((key[2] & 0x02) << 21) |
44  ((key[1] & 0x02) << 20) | ((key[0] & 0x02) << 19) |
45  ((key[7] & 0x04) << 17) | ((key[6] & 0x04) << 16) |
46  ((key[5] & 0x04) << 15) | ((key[4] & 0x04) << 14) |
47  ((key[3] & 0x04) << 13) | ((key[2] & 0x04) << 12) |
48  ((key[1] & 0x04) << 11) | ((key[0] & 0x04) << 10) |
49  ((key[7] & 0x08) << 8) | ((key[6] & 0x08) << 7) |
50  ((key[5] & 0x08) << 6) | ((key[4] & 0x08) << 5) |
51  ((key[3] & 0x08) << 4) | ((key[2] & 0x08) << 3) |
52  ((key[1] & 0x08) << 2) | ((key[0] & 0x08) << 1) |
53  ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) |
54  ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4);
55 
56  for(size_t i = 0; i != 16; ++i)
57  {
58  C = ((C << ROT[i]) | (C >> (28-ROT[i]))) & 0x0FFFFFFF;
59  D = ((D << ROT[i]) | (D >> (28-ROT[i]))) & 0x0FFFFFFF;
60  round_key[2*i ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) |
61  ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) |
62  ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) |
63  ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) |
64  ((C & 0x00010000) << 3) | ((C & 0x08000000) >> 2) |
65  ((C & 0x00800000) << 1) | ((D & 0x00000010) << 8) |
66  ((D & 0x00000002) << 7) | ((D & 0x00000001) << 2) |
67  ((D & 0x00000200) ) | ((D & 0x00008000) >> 2) |
68  ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) |
69  ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) |
70  ((D & 0x00400000) >> 21);
71  round_key[2*i+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) |
72  ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) |
73  ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) |
74  ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) |
75  ((C & 0x00200000) >> 1) | ((C & 0x04000000) >> 10) |
76  ((D & 0x00000020) << 6) | ((D & 0x00000100) ) |
77  ((D & 0x00000800) >> 1) | ((D & 0x00000040) >> 3) |
78  ((D & 0x00010000) >> 4) | ((D & 0x00000400) >> 5) |
79  ((D & 0x00004000) >> 10) | ((D & 0x04000000) >> 13) |
80  ((D & 0x00800000) >> 14) | ((D & 0x00100000) >> 18) |
81  ((D & 0x01000000) >> 24) | ((D & 0x08000000) >> 26);
82  }
83  }
84 
85 /*
86 * DES Encryption
87 */
88 void des_encrypt(u32bit& L, u32bit& R,
89  const u32bit round_key[32])
90  {
91  for(size_t i = 0; i != 16; i += 2)
92  {
93  u32bit T0, T1;
94 
95  T0 = rotate_right(R, 4) ^ round_key[2*i];
96  T1 = R ^ round_key[2*i + 1];
97 
98  L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^
99  DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^
100  DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^
101  DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)];
102 
103  T0 = rotate_right(L, 4) ^ round_key[2*i + 2];
104  T1 = L ^ round_key[2*i + 3];
105 
106  R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^
107  DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^
108  DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^
109  DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)];
110  }
111  }
112 
113 /*
114 * DES Decryption
115 */
116 void des_decrypt(u32bit& L, u32bit& R,
117  const u32bit round_key[32])
118  {
119  for(size_t i = 16; i != 0; i -= 2)
120  {
121  u32bit T0, T1;
122 
123  T0 = rotate_right(R, 4) ^ round_key[2*i - 2];
124  T1 = R ^ round_key[2*i - 1];
125 
126  L ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^
127  DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^
128  DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^
129  DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)];
130 
131  T0 = rotate_right(L, 4) ^ round_key[2*i - 4];
132  T1 = L ^ round_key[2*i - 3];
133 
134  R ^= DES_SPBOX1[get_byte(0, T0)] ^ DES_SPBOX2[get_byte(0, T1)] ^
135  DES_SPBOX3[get_byte(1, T0)] ^ DES_SPBOX4[get_byte(1, T1)] ^
136  DES_SPBOX5[get_byte(2, T0)] ^ DES_SPBOX6[get_byte(2, T1)] ^
137  DES_SPBOX7[get_byte(3, T0)] ^ DES_SPBOX8[get_byte(3, T1)];
138  }
139  }
140 
141 }
142 
143 /*
144 * DES Encryption
145 */
146 void DES::encrypt_n(const byte in[], byte out[], size_t blocks) const
147  {
148  for(size_t i = 0; i != blocks; ++i)
149  {
150  u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) |
151  (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) |
152  (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) |
153  (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] );
154 
155  u32bit L = static_cast<u32bit>(T >> 32);
156  u32bit R = static_cast<u32bit>(T);
157 
158  des_encrypt(L, R, &round_key[0]);
159 
160  T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) |
161  (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) |
162  (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) |
163  (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] );
164  T = rotate_left(T, 32);
165 
166  store_be(T, out);
167 
168  in += BLOCK_SIZE;
169  out += BLOCK_SIZE;
170  }
171  }
172 
173 /*
174 * DES Decryption
175 */
176 void DES::decrypt_n(const byte in[], byte out[], size_t blocks) const
177  {
178  for(size_t i = 0; i != blocks; ++i)
179  {
180  u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) |
181  (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) |
182  (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) |
183  (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] );
184 
185  u32bit L = static_cast<u32bit>(T >> 32);
186  u32bit R = static_cast<u32bit>(T);
187 
188  des_decrypt(L, R, &round_key[0]);
189 
190  T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) |
191  (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) |
192  (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) |
193  (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] );
194 
195  T = rotate_left(T, 32);
196 
197  store_be(T, out);
198 
199  in += BLOCK_SIZE;
200  out += BLOCK_SIZE;
201  }
202  }
203 
204 /*
205 * DES Key Schedule
206 */
207 void DES::key_schedule(const byte key[], size_t)
208  {
209  des_key_schedule(&round_key[0], key);
210  }
211 
212 /*
213 * TripleDES Encryption
214 */
215 void TripleDES::encrypt_n(const byte in[], byte out[], size_t blocks) const
216  {
217  for(size_t i = 0; i != blocks; ++i)
218  {
219  u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) |
220  (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) |
221  (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) |
222  (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] );
223 
224  u32bit L = static_cast<u32bit>(T >> 32);
225  u32bit R = static_cast<u32bit>(T);
226 
227  des_encrypt(L, R, &round_key[0]);
228  des_decrypt(R, L, &round_key[32]);
229  des_encrypt(L, R, &round_key[64]);
230 
231  T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) |
232  (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) |
233  (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) |
234  (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] );
235 
236  T = rotate_left(T, 32);
237 
238  store_be(T, out);
239 
240  in += BLOCK_SIZE;
241  out += BLOCK_SIZE;
242  }
243  }
244 
245 /*
246 * TripleDES Decryption
247 */
248 void TripleDES::decrypt_n(const byte in[], byte out[], size_t blocks) const
249  {
250  for(size_t i = 0; i != blocks; ++i)
251  {
252  u64bit T = (DES_IPTAB1[in[0]] ) | (DES_IPTAB1[in[1]] << 1) |
253  (DES_IPTAB1[in[2]] << 2) | (DES_IPTAB1[in[3]] << 3) |
254  (DES_IPTAB1[in[4]] << 4) | (DES_IPTAB1[in[5]] << 5) |
255  (DES_IPTAB1[in[6]] << 6) | (DES_IPTAB2[in[7]] );
256 
257  u32bit L = static_cast<u32bit>(T >> 32);
258  u32bit R = static_cast<u32bit>(T);
259 
260  des_decrypt(L, R, &round_key[64]);
261  des_encrypt(R, L, &round_key[32]);
262  des_decrypt(L, R, &round_key[0]);
263 
264  T = (DES_FPTAB1[get_byte(0, L)] << 5) | (DES_FPTAB1[get_byte(1, L)] << 3) |
265  (DES_FPTAB1[get_byte(2, L)] << 1) | (DES_FPTAB2[get_byte(3, L)] << 1) |
266  (DES_FPTAB1[get_byte(0, R)] << 4) | (DES_FPTAB1[get_byte(1, R)] << 2) |
267  (DES_FPTAB1[get_byte(2, R)] ) | (DES_FPTAB2[get_byte(3, R)] );
268 
269  T = rotate_left(T, 32);
270 
271  store_be(T, out);
272 
273  in += BLOCK_SIZE;
274  out += BLOCK_SIZE;
275  }
276  }
277 
278 /*
279 * TripleDES Key Schedule
280 */
281 void TripleDES::key_schedule(const byte key[], size_t length)
282  {
283  des_key_schedule(&round_key[0], key);
284  des_key_schedule(&round_key[32], key + 8);
285 
286  if(length == 24)
287  des_key_schedule(&round_key[64], key + 16);
288  else
289  copy_mem(&round_key[64], &round_key[0], 32);
290  }
291 
292 }
const u32bit DES_SPBOX4[256]
Definition: des_tab.cpp:147
const u64bit DES_IPTAB1[256]
Definition: des_tab.cpp:372
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
const u32bit DES_SPBOX6[256]
Definition: des_tab.cpp:237
const u32bit DES_SPBOX7[256]
Definition: des_tab.cpp:282
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
const u32bit DES_SPBOX3[256]
Definition: des_tab.cpp:102
const u32bit DES_SPBOX5[256]
Definition: des_tab.cpp:192
unsigned char byte
Definition: types.h:22
unsigned long long u64bit
Definition: types.h:49
T rotate_right(T input, size_t rot)
Definition: rotate.h:34
const u32bit DES_SPBOX1[256]
Definition: des_tab.cpp:12
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: des.cpp:248
const u32bit DES_SPBOX8[256]
Definition: des_tab.cpp:327
const u64bit DES_IPTAB2[256]
Definition: des_tab.cpp:438
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: des.cpp:215
const u64bit DES_FPTAB2[256]
Definition: des_tab.cpp:570
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: des.cpp:146
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
const u64bit DES_FPTAB1[256]
Definition: des_tab.cpp:504
const u32bit DES_SPBOX2[256]
Definition: des_tab.cpp:57
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: des.cpp:176
unsigned int u32bit
Definition: types.h:32