*
*/
+#include <string.h>
#include "des.h"
-u8 key_perm_table[DES_SUBKEY_LEN]={
+u8 key_perm_table[DES_INITIAL_KEY_LEN]={
57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,18,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
-}
+};
u8 pbox[DES_PBOX_LEN]={
16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
int rotate_left(u8 *obj,int len) {
int i=0;
u8 tmp=obj[0];
- for(i=0;i<len-1;i++) obj[i]=obj[i]<<1|(obj[j+1]>>7);
+ for(i=0;i<len-1;i++) obj[i]=obj[i]<<1|(obj[i+1]>>7);
obj[len-1]=obj[len-1]<<1|(tmp>>7);
return 1;
}
int table_trans(u8 *src,u8 *final,u8 *table,int len) {
int i;
memset(final,0,len/8);
- for(i=0;i<len,i++)
- final[i/8]|=((((src[(table[i]-1)/8)]>>((8-table[i]%8)&7))&1)<<(7-i%8));
+ for(i=0;i<len;i++)
+ final[i/8]|=((((src[(table[i]-1)/8])>>((8-table[i]%8)&7))&1)<<(7-i%8));
return 1;
}
table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN);
for(i=0;i<DES_SUBKEYS;i++) {
/* split and rotate key_056 */
- subkey_trans(key0_56;i);
+ subkey_trans(key0_56,i);
/* chose 48 bits as a subkey */
table_trans(key0_56,subkey[i],subkey_perm_table,DES_SUBKEY_LEN);
}
int get_sbox_value(u8 *data,int offset,int len,int box) {
int tmp;
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))));
- return(sbox[box][(tmp&1|(tmp&(1<<(len-1))<<1))*16+(tmp&((1<<(len-1))-1)>>1)]);
+ return(sbox[box][((tmp&1)|(tmp&(1<<(len-1))<<1))*16+(tmp&((1<<(len-1))-1)>>1)]);
}
int data_s_and_p(u8 *data) {
u8 s[DES_DATA_BLOCK_LEN/(8*2)];
- int i,j;
+ int i;
for(i=0;i<DES_SBOXES;i++)
s[i]=get_sbox_value(data,i*(DES_SUBKEY_LEN/DES_SBOXES),(DES_SUBKEY_LEN/DES_SBOXES),i);
table_trans(s,data,pbox,DES_PBOX_LEN);
return 1;
}
-int progress_data(u8 *data,u8 **subkey,u8 mode) {
+int progress_data(u8 *data,u8 *crypted,u8 **subkey,u8 mode) {
int i,j;
u8 permuted_data[DES_DATA_BLOCK_LEN/8];
u8 left[DES_DATA_BLOCK_LEN/(2*8)];
}
/* merge and do final permutation */
memcpy(permuted_data,left,DES_DATA_BLOCK_LEN/(2*8));
- memcpy(permuted_datad+DES_DATA_BLOCK_LEN/(2*8),right,DES_DATA_BLOCK_LEN/(8*2));
- table_trans(permuted_data,data,data_final_perm_table,DES_DATA_BLOCK_LEN);
+ memcpy(permuted_data+DES_DATA_BLOCK_LEN/(2*8),right,DES_DATA_BLOCK_LEN/(8*2));
+ table_trans(permuted_data,crypted,data_final_perm_table,DES_DATA_BLOCK_LEN);
return 1;
}
int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) {
u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8];
- u8 tmp[DES_DATA_BLOCK_LEN/8];
- int i;
-
+ int i,j;
/* compute the subkeys:
- initial key transformation: 64bit -> 56bit
- obtain subkeys by:
- do final permutation
*/
for(i=0;i<len/8;i++) {
- if(mode&MODE_DECRYPT) memcpy(tmp,src+(i*8),DES_DATA_BLOCK_LEN/8);
- progress_data(src+(i*8),subkey,mode);
- if(mode&&MODE_CBC) {
- if((i!=(len/8-1))&&(mode&MODE_CRYPT)) src[(i+1)*8]^=src[i*8];
- if((mode&MODE_DECRYPT)&&(i!=0)) /* continue here, rewrite .. :/ */
+ if((mode&MODE_CBC)&&(mode&MODE_CRYPT)&&(i!=0))
+ for(j=0;j<8;j++) src[i*8+j]^=final[(i-1)*8+j];
+ progress_data(src+i*8,final+i*8,subkey,mode);
+ if((mode&MODE_CBC)&&(mode&MODE_DECRYPT)&&(i!=0))
+ for(j=0;j<8;j++) final[(i*8)+j]^=src[(i-1)*8+j];
}
return 1;
}
-int des_crypt(u8 *plain,u8 *crypted,u8 *key,int len,MODE_CRYPT) {
-
+int des_encrypt(u8 *plain,u8 *crypted,u8 *key,int len,u8 mode) {
+ des_crypt_decrypt(plain,crypted,key,len,MODE_CRYPT|mode);
return 1;
}
-int des_decrypt(u8 *crypted,u8 *plain,u8 *key,int len,MODE_DECRYPT) {
-
+int des_decrypt(u8 *crypted,u8 *plain,u8 *key,int len,u8 mode) {
+ des_crypt_decrypt(crypted,plain,key,len,MODE_DECRYPT|mode);
return 1;
}