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