11 #include <botan/des.h>
12 #include <botan/loadstor.h>
21 void des_key_schedule(uint32_t round_key[32],
const uint8_t key[8])
23 static const uint8_t ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
24 1, 2, 2, 2, 2, 2, 2, 1 };
26 uint32_t C = ((key[7] & 0x80) << 20) | ((key[6] & 0x80) << 19) |
27 ((key[5] & 0x80) << 18) | ((key[4] & 0x80) << 17) |
28 ((key[3] & 0x80) << 16) | ((key[2] & 0x80) << 15) |
29 ((key[1] & 0x80) << 14) | ((key[0] & 0x80) << 13) |
30 ((key[7] & 0x40) << 13) | ((key[6] & 0x40) << 12) |
31 ((key[5] & 0x40) << 11) | ((key[4] & 0x40) << 10) |
32 ((key[3] & 0x40) << 9) | ((key[2] & 0x40) << 8) |
33 ((key[1] & 0x40) << 7) | ((key[0] & 0x40) << 6) |
34 ((key[7] & 0x20) << 6) | ((key[6] & 0x20) << 5) |
35 ((key[5] & 0x20) << 4) | ((key[4] & 0x20) << 3) |
36 ((key[3] & 0x20) << 2) | ((key[2] & 0x20) << 1) |
37 ((key[1] & 0x20) ) | ((key[0] & 0x20) >> 1) |
38 ((key[7] & 0x10) >> 1) | ((key[6] & 0x10) >> 2) |
39 ((key[5] & 0x10) >> 3) | ((key[4] & 0x10) >> 4);
40 uint32_t D = ((key[7] & 0x02) << 26) | ((key[6] & 0x02) << 25) |
41 ((key[5] & 0x02) << 24) | ((key[4] & 0x02) << 23) |
42 ((key[3] & 0x02) << 22) | ((key[2] & 0x02) << 21) |
43 ((key[1] & 0x02) << 20) | ((key[0] & 0x02) << 19) |
44 ((key[7] & 0x04) << 17) | ((key[6] & 0x04) << 16) |
45 ((key[5] & 0x04) << 15) | ((key[4] & 0x04) << 14) |
46 ((key[3] & 0x04) << 13) | ((key[2] & 0x04) << 12) |
47 ((key[1] & 0x04) << 11) | ((key[0] & 0x04) << 10) |
48 ((key[7] & 0x08) << 8) | ((key[6] & 0x08) << 7) |
49 ((key[5] & 0x08) << 6) | ((key[4] & 0x08) << 5) |
50 ((key[3] & 0x08) << 4) | ((key[2] & 0x08) << 3) |
51 ((key[1] & 0x08) << 2) | ((key[0] & 0x08) << 1) |
52 ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) |
53 ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4);
55 for(
size_t i = 0; i != 16; ++i)
57 C = ((C << ROT[i]) | (C >> (28-ROT[i]))) & 0x0FFFFFFF;
58 D = ((D << ROT[i]) | (D >> (28-ROT[i]))) & 0x0FFFFFFF;
59 round_key[2*i ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) |
60 ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) |
61 ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) |
62 ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) |
63 ((C & 0x00010000) << 3) | ((C & 0x08000000) >> 2) |
64 ((C & 0x00800000) << 1) | ((D & 0x00000010) << 8) |
65 ((D & 0x00000002) << 7) | ((D & 0x00000001) << 2) |
66 ((D & 0x00000200) ) | ((D & 0x00008000) >> 2) |
67 ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) |
68 ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) |
69 ((D & 0x00400000) >> 21);
70 round_key[2*i+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) |
71 ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) |
72 ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) |
73 ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) |
74 ((C & 0x00200000) >> 1) | ((C & 0x04000000) >> 10) |
75 ((D & 0x00000020) << 6) | ((D & 0x00000100) ) |
76 ((D & 0x00000800) >> 1) | ((D & 0x00000040) >> 3) |
77 ((D & 0x00010000) >> 4) | ((D & 0x00000400) >> 5) |
78 ((D & 0x00004000) >> 10) | ((D & 0x04000000) >> 13) |
79 ((D & 0x00800000) >> 14) | ((D & 0x00100000) >> 18) |
80 ((D & 0x01000000) >> 24) | ((D & 0x08000000) >> 26);
87 void des_encrypt(uint32_t& L, uint32_t& R,
88 const uint32_t round_key[32])
90 for(
size_t i = 0; i != 16; i += 2)
95 T1 = R ^ round_key[2*i + 1];
103 T1 = L ^ round_key[2*i + 3];
115 void des_decrypt(uint32_t& L, uint32_t& R,
116 const uint32_t round_key[32])
118 for(
size_t i = 16; i != 0; i -= 2)
123 T1 = R ^ round_key[2*i - 1];
131 T1 = L ^ round_key[2*i - 3];
147 for(
size_t i = 0; i < blocks; ++i)
154 uint32_t L =
static_cast<uint32_t
>(T >> 32);
155 uint32_t R =
static_cast<uint32_t
>(T);
157 des_encrypt(L, R, m_round_key.data());
174 for(
size_t i = 0; i < blocks; ++i)
181 uint32_t L =
static_cast<uint32_t
>(T >> 32);
182 uint32_t R =
static_cast<uint32_t
>(T);
184 des_decrypt(L, R, m_round_key.data());
200 void DES::key_schedule(
const uint8_t key[],
size_t)
202 m_round_key.resize(32);
203 des_key_schedule(m_round_key.data(), key);
216 for(
size_t i = 0; i != blocks; ++i)
223 uint32_t L =
static_cast<uint32_t
>(T >> 32);
224 uint32_t R =
static_cast<uint32_t
>(T);
226 des_encrypt(L, R, &m_round_key[0]);
227 des_decrypt(R, L, &m_round_key[32]);
228 des_encrypt(L, R, &m_round_key[64]);
249 for(
size_t i = 0; i != blocks; ++i)
256 uint32_t L =
static_cast<uint32_t
>(T >> 32);
257 uint32_t R =
static_cast<uint32_t
>(T);
259 des_decrypt(L, R, &m_round_key[64]);
260 des_encrypt(R, L, &m_round_key[32]);
261 des_decrypt(L, R, &m_round_key[0]);
280 void TripleDES::key_schedule(
const uint8_t key[],
size_t length)
282 m_round_key.resize(3*32);
283 des_key_schedule(&m_round_key[0], key);
284 des_key_schedule(&m_round_key[32], key + 8);
287 des_key_schedule(&m_round_key[64], key + 16);
289 copy_mem(&m_round_key[64], &m_round_key[0], 32);
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
const uint64_t DES_IPTAB2[256]
const uint32_t DES_SPBOX6[256]
const uint32_t DES_SPBOX7[256]
void zap(std::vector< T, Alloc > &vec)
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
void store_be(uint16_t in, uint8_t out[2])
const uint64_t DES_IPTAB1[256]
const uint32_t DES_SPBOX4[256]
T rotate_left(T input, size_t rot)
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
const uint32_t DES_SPBOX3[256]
const uint32_t DES_SPBOX8[256]
T rotate_right(T input, size_t rot)
const uint32_t DES_SPBOX1[256]
void copy_mem(T *out, const T *in, size_t n)
const uint32_t DES_SPBOX5[256]
const uint32_t DES_SPBOX2[256]
uint8_t get_byte(size_t byte_num, T input)
const uint64_t DES_FPTAB1[256]
const uint64_t DES_FPTAB2[256]