722e1cc790443797137eab823fd245fd3519cb90
[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,int len) {
91  u8 tmp=obj[len-1];
92  while(--len) obj[len]=obj[len]>>1|((obj[len-1]&1)<<7);
93  obj[0]=obj[0]>>1|((tmp&1)<<7);;
94  return 1;
95 }
96
97 int rotate_left(u8 *obj,int len) {
98  int i=0;
99  u8 tmp=obj[0];
100  for(i=0;i<len-1;i++) obj[i]=obj[i]<<1|(obj[i+1]>>7);
101  obj[len-1]=obj[len-1]<<1|(tmp>>7);
102  return 1;
103 }
104
105 int table_trans(u8 *src,u8 *final,u8 *table,int len) {
106  int i;
107  memset(final,0,len/8);
108  for(i=0;i<len;i++)
109   final[i/8]|=((((src[(table[i]-1)/8])>>((8-table[i]%8)&7))&1)<<(7-i%8));
110  return 1;
111 }
112
113 int subkey_trans(u8 *key,int round) {
114  int i;
115  for(i=0;i<rot_per_round_table[round];i++) {
116   rotate_left(key,DES_INITIAL_KEY_LEN/(8*2));
117   rotate_left(key+DES_INITIAL_KEY_LEN/(8*2),DES_INITIAL_KEY_LEN/(8*2));
118  }
119  return 1;
120 }
121
122 int compute_subkeys(u8 *key0,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8]) {
123  int i;
124  u8 key0_56[DES_INITIAL_KEY_LEN/8];
125  /* initial permutation of key */
126  table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN);
127  for(i=0;i<DES_SUBKEYS;i++) {
128   /* split and rotate key_056 */
129   subkey_trans(key0_56,i);
130   /* chose 48 bits as a subkey */
131   table_trans(key0_56,subkey[i],subkey_perm_table,DES_SUBKEY_LEN);
132  }
133  return 1;
134 }
135
136 int get_sbox_value(u8 *data,int offset,int len,int box) {
137  int tmp;
138  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))));
139  return(sbox[box][((tmp&1)|(tmp&(1<<(len-1))<<1))*16+(tmp&((1<<(len-1))-1)>>1)]); 
140 }
141
142 int data_s_and_p(u8 *data) {
143  u8 s[DES_DATA_BLOCK_LEN/(8*2)];
144  int i;
145  for(i=0;i<DES_SBOXES;i++)
146   s[i]=get_sbox_value(data,i*(DES_SUBKEY_LEN/DES_SBOXES),(DES_SUBKEY_LEN/DES_SBOXES),i);
147  table_trans(s,data,pbox,DES_PBOX_LEN);
148  return 1;
149 }
150
151 int progress_data(u8 *data,u8 *crypted,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8],u8 mode) {
152  int i,j;
153  u8 permuted_data[DES_DATA_BLOCK_LEN/8];
154  u8 left[DES_DATA_BLOCK_LEN/(2*8)];
155  u8 right[DES_DATA_BLOCK_LEN/(2*8)];
156  u8 tmp[DES_DATA_BLOCK_LEN/(2*8)];
157
158  /* initial permutation */
159  table_trans(data,permuted_data,data_perm_table,DES_DATA_BLOCK_LEN);
160  memcpy(left,permuted_data,DES_DATA_BLOCK_LEN/(2*8));
161  memcpy(right,permuted_data+DES_DATA_BLOCK_LEN/(2*8),DES_DATA_BLOCK_LEN/(2*8));
162  for(i=0;i<DES_SUBKEYS;i++){
163   /* expand right data to 48 bit */
164   table_trans(right,permuted_data,data_expansion_table,DES_SUBKEY_LEN);
165   /* xor with subkey */
166   for(j=0;j<DES_SUBKEY_LEN/8;j++) permuted_data[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
167   /* sbox substitutions and pbox permutations */
168   data_s_and_p(permuted_data);
169   /* xor with left data */
170   memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
171   for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)    
172    right[j]=permuted_data[j]^left[j];
173   /* switch left and right data - not for last round */
174   if(i!=15) memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8)); 
175  }
176  /* merge and do final permutation */
177  memcpy(permuted_data,left,DES_DATA_BLOCK_LEN/(2*8));
178  memcpy(permuted_data+DES_DATA_BLOCK_LEN/(2*8),right,DES_DATA_BLOCK_LEN/(8*2));
179  table_trans(permuted_data,crypted,data_final_perm_table,DES_DATA_BLOCK_LEN);
180  return 1;
181 }
182
183 int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) {
184  u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8];
185  int i,j;
186  /* compute the subkeys:
187     - initial key transformation: 64bit -> 56bit
188     - obtain subkeys by:
189       - split 56bit key -> 28bit - 28bit
190       - rotate these to the left (rotation depends on round)
191       - merge them again -> 56bit
192       - do permuted choice -> 56bit -> 48 bit, the subkey
193       - loop 16 times
194  */
195  compute_subkeys(key,subkey);
196  /* split data to 64bit blocks, and crypt/decrypt each block
197     depending on mode (CBC/ECB):
198     - initial data transformation
199     - crypt data:
200       - split 64bit data -> 32bit - 32bit
201       - expand right data block
202       - xor right block with subkey i (or 15-i)
203       - do sbox and pbox permutations
204       - xor result with left data block
205       - switch left and right data block (not for last cycle)
206       - loop 16 times
207     - do final permutation
208  */
209  for(i=0;i<len/8;i++) {
210   if((mode&MODE_CBC)&&(mode&MODE_CRYPT)&&(i!=0))
211    for(j=0;j<8;j++) src[i*8+j]^=final[(i-1)*8+j];
212   progress_data(src+i*8,final+i*8,subkey,mode);
213   if((mode&MODE_CBC)&&(mode&MODE_DECRYPT)&&(i!=0))
214    for(j=0;j<8;j++) final[(i*8)+j]^=src[(i-1)*8+j];
215  }  
216  return 1;
217 }
218
219 int des_encrypt(u8 *plain,u8 *crypted,u8 *key,int len,u8 mode) {
220  des_crypt_decrypt(plain,crypted,key,len,MODE_CRYPT|mode);
221  return 1;
222 }
223
224 int des_decrypt(u8 *crypted,u8 *plain,u8 *key,int len,u8 mode) {
225  des_crypt_decrypt(crypted,plain,key,len,MODE_DECRYPT|mode);
226  return 1;
227 }