ecb mode working, still bug in cbc mode.
[my-code/crypto.git] / des.c
1 /*
2  * des.c - data encryption standard algo
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <string.h>
9 #include "des.h"
10
11 u8 key_perm_table[DES_INITIAL_KEY_LEN]={
12  57,49,41,33,25,17,9,1,58,50,42,34,26,18,
13  10,2,59,51,43,35,27,18,11,3,60,52,44,36,
14  63,55,47,39,31,23,15,7,62,54,46,38,30,22,
15  14,6,61,53,45,37,29,21,13,5,28,20,12,4
16 };
17
18 u8 rot_per_round_table[DES_SUBKEYS]={
19  1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
20 };
21
22 u8 subkey_perm_table[DES_SUBKEY_LEN]={
23  14,17,11,24,1,5,3,28,15,6,21,10,
24  23,19,12,4,26,8,16,7,27,20,13,2,
25  41,52,31,37,47,55,30,40,51,45,33,48,
26  44,49,39,56,34,53,46,42,50,36,29,32
27 };
28
29 u8 data_perm_table[DES_DATA_BLOCK_LEN]={
30  58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
31  62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
32  57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,
33  61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7
34 };
35
36 u8 data_expansion_table[DES_SUBKEY_LEN]={
37  32,1,2,3,4,5,4,5,6,7,8,9,
38  8,9,10,11,12,13,12,13,14,15,16,17,
39  16,17,18,19,20,21,20,21,22,23,24,25,
40  24,25,26,27,28,29,28,29,30,31,32,1
41 };
42
43 u8 sbox[DES_SBOXES][DES_SBOX_LEN]={
44  {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
45   0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
46   4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
47   15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13},
48  {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
49   3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
50   0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
51   13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9},
52  {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
53   13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
54   13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
55   1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12},
56  {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
57   13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
58   10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
59   3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14},
60  {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
61   14,11,2,12,4,7,13,1,5,0,15,10,3,9.8,6,
62   4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
63   11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3},
64  {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
65   10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
66   9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
67   4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13},
68  {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
69   13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
70   1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
71   6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12},
72  {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
73   1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
74   7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
75   2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
76 };
77
78 u8 pbox[DES_PBOX_LEN]={
79  16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
80  2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25
81 };
82
83 u8 data_final_perm_table[DES_DATA_BLOCK_LEN]={
84  40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
85  38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
86  36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
87  34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25
88 };
89
90 int rotate_right(u8 *obj) {
91  u8 mid=obj[3],tmp=obj[6];
92  int i;
93  for(i=6;i>3;i++)
94   obj[i]=obj[i]>>1|((obj[i-1]&1)<<7);
95  obj[3]=(obj[3]&0xf)>>1|((tmp&1)<<3);
96  obj[3]|=(((obj[2]&1)<<7)|((mid&0xe0)>>1));
97  for(i=2;i>0;i++)
98   obj[i]=obj[i]>>1|((obj[i-1]&1)<<7);
99  obj[0]=obj[0]>>1|((mid&0x10)<<3);
100  return 1;
101 }
102
103 int rotate_left(u8 *obj) {
104  u8 mid=obj[3],tmp=obj[0];
105  int i;
106  for(i=0;i<3;i++)
107   obj[i]=obj[i]<<1|(obj[i+1]>>7);
108  obj[3]=((obj[3]&0xf0)<<1)|((tmp&0x80)>>3);
109  obj[3]|=(((mid&7)<<1)|(obj[4]>>7));
110  for(i=4;i<6;i++)
111   obj[i]=(obj[i]<<1)|(obj[i+1]>>7);
112  obj[6]=obj[6]<<1|((mid&8)>>3);
113  return 1;
114 }
115
116 int table_trans(u8 *src,u8 *final,u8 *table,int len) {
117  int i;
118  memset(final,0,len/8);
119  for(i=0;i<len;i++)
120   final[i/8]|=((((src[(table[i]-1)/8])>>((8-(table[i])%8)&7))&1)<<(7-i%8));
121   return 1;
122 }
123
124 int subkey_trans(u8 *key,int round) {
125  int i;
126  for(i=0;i<rot_per_round_table[round];i++)
127   rotate_left(key);
128  return 1;
129 }
130
131 int compute_subkeys(u8 *key0,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8]) {
132  int i;
133  u8 key0_56[DES_INITIAL_KEY_LEN/8];
134  /* initial permutation of key */
135  table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN);
136  for(i=0;i<DES_SUBKEYS;i++) {
137   /* split and rotate key_056 */
138   subkey_trans(key0_56,i);
139   /* chose 48 bits as a subkey */
140   table_trans(key0_56,subkey[i],subkey_perm_table,DES_SUBKEY_LEN);
141  }
142  return 1;
143 }
144
145 int data_s_and_p(u8 *data) {
146  u8 s[DES_DATA_BLOCK_LEN/(8*2)];
147  memset(s,0,DES_DATA_BLOCK_LEN/(8*2));
148  s[0]=sbox[0][16*(((data[0]&0x80)>>6)|((data[0]&4)>>2))+((data[0]&0x78)>>3)]<<4;
149  s[0]|=sbox[1][16*((data[0]&2)|((data[1]&0x10)>>4))+(((data[0]&1)<<3)|(data[1]&0xe0)>>5)];
150  s[1]=sbox[2][16*(((data[1]&8)>>2)|((data[2]&0x40)>>6))+(((data[1]&7)<<1)|((data[2]&0x80)>>7))]<<4;
151  s[1]|=sbox[3][16*(((data[2]&0x20)>>4)|(data[2]&1))+((data[2]&0x1e)>>1)];
152  s[2]=sbox[4][16*(((data[3]&0x80)>>6)|((data[3]&4)>>2))+((data[3]&0x78)>>3)]<<4;
153  s[2]|=sbox[5][16*((data[3]&2)|((data[4]&0x10)>>4))+(((data[3]&1)<<3)|(data[4]&0xe0)>>5)];
154  s[3]=sbox[6][16*(((data[4]&8)>>2)|((data[5]&0x40)>>6))+(((data[4]&7)<<1)|((data[5]&0x80)>>7))]<<4;
155  s[3]|=sbox[7][16*(((data[5]&0x20)>>4)|(data[5]&1))+((data[5]&0x1e)>>1)];
156  table_trans(s,data,pbox,DES_PBOX_LEN);
157  return 1;
158 }
159
160 int progress_data(u8 *data,u8 *crypted,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8],u8 mode) {
161  int i,j;
162  u8 permuted_data[DES_DATA_BLOCK_LEN/8];
163  u8 left[DES_DATA_BLOCK_LEN/(2*8)];
164  u8 right[DES_DATA_BLOCK_LEN/(2*8)];
165  u8 tmp[DES_DATA_BLOCK_LEN/(2*8)];
166
167  /* initial permutation */
168  table_trans(data,permuted_data,data_perm_table,DES_DATA_BLOCK_LEN);
169  memcpy(left,permuted_data,DES_DATA_BLOCK_LEN/(2*8));
170  memcpy(right,permuted_data+DES_DATA_BLOCK_LEN/(2*8),DES_DATA_BLOCK_LEN/(2*8));
171  for(i=0;i<DES_SUBKEYS;i++){
172   /* expand right data to 48 bit */
173   table_trans(right,permuted_data,data_expansion_table,DES_SUBKEY_LEN);
174   /* xor with subkey */
175   for(j=0;j<DES_SUBKEY_LEN/8;j++) permuted_data[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
176   /* sbox substitutions and pbox permutations */
177   data_s_and_p(permuted_data);
178   /* xor with left data */
179   if(i!=15) {
180    memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
181    for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)    
182     right[j]=permuted_data[j]^left[j];
183    /* switch left and right data - not for last round */
184    memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8));
185   }
186   else
187    for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++) left[j]^=permuted_data[j];
188  }
189  /* merge and do final permutation */
190  memcpy(permuted_data,left,DES_DATA_BLOCK_LEN/(2*8));
191  memcpy(permuted_data+DES_DATA_BLOCK_LEN/(2*8),right,DES_DATA_BLOCK_LEN/(8*2));
192  table_trans(permuted_data,crypted,data_final_perm_table,DES_DATA_BLOCK_LEN);
193  return 1;
194 }
195
196 int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) {
197  u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8];
198  int i,j;
199  /* compute the subkeys:
200     - initial key transformation: 64bit -> 56bit
201     - obtain subkeys by:
202       - split 56bit key -> 28bit - 28bit
203       - rotate these to the left (rotation depends on round)
204       - merge them again -> 56bit
205       - do permuted choice -> 56bit -> 48 bit, the subkey
206       - loop 16 times
207  */
208  compute_subkeys(key,subkey);
209  /* split data to 64bit blocks, and crypt/decrypt each block
210     depending on mode (CBC/ECB):
211     - initial data transformation
212     - crypt data:
213       - split 64bit data -> 32bit - 32bit
214       - expand right data block
215       - xor right block with subkey i (or 15-i)
216       - do sbox and pbox permutations
217       - xor result with left data block
218       - switch left and right data block (not for last cycle)
219       - loop 16 times
220     - do final permutation
221  */
222  for(i=0;i<len/8;i++) {
223   if((mode&MODE_CBC)&&(mode&MODE_CRYPT)&&(i!=0))
224    for(j=0;j<8;j++) src[i*8+j]^=final[(i-1)*8+j];
225   progress_data(src+i*8,final+i*8,subkey,mode);
226   if((mode&MODE_CBC)&&(mode&MODE_DECRYPT)&&(i!=0))
227    for(j=0;j<8;j++) final[(i*8)+j]^=src[(i-1)*8+j];
228  }  
229  return 1;
230 }
231
232 int des_encrypt(u8 *plain,u8 *crypted,u8 *key,int len,u8 mode) {
233  des_crypt_decrypt(plain,crypted,key,len,MODE_CRYPT|mode);
234  return 1;
235 }
236
237 int des_decrypt(u8 *crypted,u8 *plain,u8 *key,int len,u8 mode) {
238  des_crypt_decrypt(crypted,plain,key,len,MODE_DECRYPT|mode);
239  return 1;
240 }