12#define uchar unsigned char
13#define uint unsigned int
18#define BITNUM(a,b,c) (((a[(b)/8] >> (7 - (b%8))) & 0x01) << (c))
19#define BITNUMINTR(a,b,c) ((((a) >> (31 - (b))) & 0x00000001) << (c))
20#define BITNUMINTL(a,b,c) ((((a) << (b)) & 0x80000000) >> (c))
23#define SBOXBIT(a) (((a) & 0x20) | (((a) & 0x1f) >> 1) | (((a) & 0x01) << 4))
26 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
27 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
28 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
29 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
32 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
33 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
34 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
35 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
38 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
39 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
40 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
41 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
44 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
45 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
46 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
47 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
50 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
51 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
52 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
53 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
56 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
57 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
58 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
59 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
62 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
63 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
64 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
65 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
68 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
69 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
70 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
71 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
78 key_rnd_shift[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1},
79 key_perm_c[28]={56,48,40,32,24,16,8,0,57,49,41,33,25,17,
80 9,1,58,50,42,34,26,18,10,2,59,51,43,35},
81 key_perm_d[28]={62,54,46,38,30,22,14,6,61,53,45,37,29,21,
82 13,5,60,52,44,36,28,20,12,4,27,19,11,3},
83 key_compression[48]={13,16,10,23,0,4,2,27,14,5,20,9,
84 22,18,11,3,25,7,15,6,26,19,12,1,
85 40,51,30,36,46,54,29,39,50,44,32,47,
86 43,48,38,55,33,52,45,41,49,35,28,31};
89 for (i = 0, j = 31, C = 0; i < 28; ++i, --j)
90 C |=
BITNUM(key,key_perm_c[i],j);
91 for (i = 0, j = 31, D = 0; i < 28; ++i, --j)
92 D |=
BITNUM(key,key_perm_d[i],j);
95 for (i = 0; i < 16; ++i) {
96 C = ((C << key_rnd_shift[i]) | (C >> (28-key_rnd_shift[i]))) & 0xfffffff0;
97 D = ((D << key_rnd_shift[i]) | (D >> (28-key_rnd_shift[i]))) & 0xfffffff0;
106 for (j = 0; j < 6; ++j)
107 schedule[to_gen][j] = 0;
108 for (j = 0; j < 24; ++j)
109 schedule[to_gen][j/8] |=
BITNUMINTR(C,key_compression[j],7 - (j%8));
111 schedule[to_gen][j/8] |=
BITNUMINTR(D,key_compression[j] - 28,7 - (j%8));
188 lrgstate[0] = (t1 >> 24) & 0x000000ff;
189 lrgstate[1] = (t1 >> 16) & 0x000000ff;
190 lrgstate[2] = (t1 >> 8) & 0x000000ff;
191 lrgstate[3] = (t2 >> 24) & 0x000000ff;
192 lrgstate[4] = (t2 >> 16) & 0x000000ff;
193 lrgstate[5] = (t2 >> 8) & 0x000000ff;
196 lrgstate[0] ^= key[0];
197 lrgstate[1] ^= key[1];
198 lrgstate[2] ^= key[2];
199 lrgstate[3] ^= key[3];
200 lrgstate[4] ^= key[4];
201 lrgstate[5] ^= key[5];
205 (
sbox2[
SBOXBIT(((lrgstate[0] & 0x03) << 4) | (lrgstate[1] >> 4))] << 24) |
206 (
sbox3[
SBOXBIT(((lrgstate[1] & 0x0f) << 2) | (lrgstate[2] >> 6))] << 20) |
209 (
sbox6[
SBOXBIT(((lrgstate[3] & 0x03) << 4) | (lrgstate[4] >> 4))] << 8) |
210 (
sbox7[
SBOXBIT(((lrgstate[4] & 0x0f) << 2) | (lrgstate[5] >> 6))] << 4) |
237 for (idx=0; idx < 15; ++idx) {
239 state[1] =
f(state[1],key[idx]) ^ state[0];
242 state[0] =
f(state[1],key[15]) ^ state[0];
void three_des_key_schedule(uchar key[], uchar schedule[][16][6], uint mode)
#define BITNUMINTL(a, b, c)
void key_schedule(uchar key[], uchar schedule[][6], uint mode)
#define BITNUMINTR(a, b, c)
void InvIP(uint state[], uchar in[])
void des_crypt(uchar in[], uchar out[], uchar key[][6])
uint f(uint state, uchar key[])
void IP(uint state[], uchar in[])
void three_des_crypt(uchar in[], uchar out[], uchar key[][16][6])