11 #include <botan/des.h>
12 #include <botan/loadstor.h>
13 #include <botan/rotate.h>
22 void des_key_schedule(
u32bit round_key[32],
const byte key[8])
24 static const byte ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
25 1, 2, 2, 2, 2, 2, 2, 1 };
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);
56 for(
size_t i = 0; i != 16; ++i)
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);
89 const u32bit round_key[32])
91 for(
size_t i = 0; i != 16; i += 2)
96 T1 = R ^ round_key[2*i + 1];
104 T1 = L ^ round_key[2*i + 3];
117 const u32bit round_key[32])
119 for(
size_t i = 16; i != 0; i -= 2)
124 T1 = R ^ round_key[2*i - 1];
132 T1 = L ^ round_key[2*i - 3];
148 for(
size_t i = 0; i != blocks; ++i)
158 des_encrypt(L, R, &round_key[0]);
178 for(
size_t i = 0; i != blocks; ++i)
188 des_decrypt(L, R, &round_key[0]);
207 void DES::key_schedule(
const byte key[],
size_t)
209 des_key_schedule(&round_key[0], key);
217 for(
size_t i = 0; i != blocks; ++i)
227 des_encrypt(L, R, &round_key[0]);
228 des_decrypt(R, L, &round_key[32]);
229 des_encrypt(L, R, &round_key[64]);
250 for(
size_t i = 0; i != blocks; ++i)
260 des_decrypt(L, R, &round_key[64]);
261 des_encrypt(R, L, &round_key[32]);
262 des_decrypt(L, R, &round_key[0]);
281 void TripleDES::key_schedule(
const byte key[],
size_t length)
283 des_key_schedule(&round_key[0], key);
284 des_key_schedule(&round_key[32], key + 8);
287 des_key_schedule(&round_key[64], key + 16);
289 copy_mem(&round_key[64], &round_key[0], 32);
const u32bit DES_SPBOX4[256]
const u64bit DES_IPTAB1[256]
T rotate_left(T input, size_t rot)
const u32bit DES_SPBOX6[256]
const u32bit DES_SPBOX7[256]
byte get_byte(size_t byte_num, T input)
const u32bit DES_SPBOX3[256]
const u32bit DES_SPBOX5[256]
unsigned long long u64bit
T rotate_right(T input, size_t rot)
const u32bit DES_SPBOX1[256]
void decrypt_n(const byte in[], byte out[], size_t blocks) const
const u32bit DES_SPBOX8[256]
const u64bit DES_IPTAB2[256]
void copy_mem(T *out, const T *in, size_t n)
void encrypt_n(const byte in[], byte out[], size_t blocks) const
const u64bit DES_FPTAB2[256]
void encrypt_n(const byte in[], byte out[], size_t blocks) const
void store_be(u16bit in, byte out[2])
const u64bit DES_FPTAB1[256]
const u32bit DES_SPBOX2[256]
void decrypt_n(const byte in[], byte out[], size_t blocks) const