u8 pbox[DES_PBOX_LEN]={
16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25
-};
+};
+
+u8 data_final_perm_table[DES_DATA_BLOCK_LEN]={
+ 40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
+ 38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
+ 36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
+ 34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25
+};
int rotate_right(u8 *obj,int len) {
u8 tmp=obj[len-1];
int data_expansion_perm(u8 *right,u8 expanded) {
int i;
- memset(expanded,0,DES_SUBKEY_LEN);
+ memset(expanded,0,DES_SUBKEY_LEN/8);
for(i=0;i<DES_SUBKEY_LEN;i++)
expanded[i/8]|=((((right[data_expansion_table[i]-1)/8]>>((8-data_expansion_table[i]%8)&7))&1)<<(7-i%8));
return 1;
return(sbox[box][(tmp&1|(tmp&(1<<(len-1))<<1))*16+(tmp&((1<<(len-1))-1)>>1)]);
}
+int data_pbox_perm(u8 *s,u8 *p) {
+ int i;
+ for(i=0;i<DES_PBOX_LEN;i++)
+ memset(p,0,DES_PBOX_LEN/8);
+ p[i/8]|=((((s[pbox[i]-1)/8]>>((8-pbox[i]%8)&7))&1)<<(7-i%8));
+ return 1;
+}
+
int data_s_and_p(u8 *data) {
- u8 s[DES_DATA_BLOCK_LEN/2];
- u8 p[DES_DATA_BLOCK_LEN/2];
+ u8 s[DES_DATA_BLOCK_LEN/(8*2)];
+ u8 p[DES_DATA_BLOCK_LEN/(8*2)];
int i,j;
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);
-
+ s[i]=get_sbox_value(data,i*(DES_SUBKEY_LEN/DES_SBOXES),(DES_SUBKEY_LEN/DES_SBOXES),i);
+ data_pbox_perm(s,p);
+ memcpy(data,p,DES_DATA_BLOCK_LEN/(8*2));
return 1;
}
+int data_final_perm(u8 *final,u8 *src) {
+ int i;
+ memset(final,0,DES_DATA_BLOCK_LEN/8);
+ for(i=0;i<DES_DATA_BLOCK_LEN;i++)
+ final[i/8]|=((((src[data_final_perm_table[i]-1)/8]>>((8-data_final_perm_table[i]%8)&7))&1)<<(7-i%8));
+ return 1;
+}
+
int progress_data(u8 *data,u8 **subkey,u8 mode) {
int i,j;
- u8 pd[DES_DATA_BLOCK_LEN];
+ u8 pd[DES_DATA_BLOCK_LEN/8];
u8 left[DES_DATA_BLOCK_LEN/(2*8)];
u8 right[DES_DATA_BLOCK_LEN/(2*8)];
- u8 expanded[DES_SUBKEY_LEN];
+ u8 tmp[DES_DATA_BLOCK_LEN/(2*8)];
+ u8 expanded[DES_SUBKEY_LEN/8];
data_perm(data,pd);
memcpy(left,pd,DES_DATA_BLOCK_LEN/(2*8));
memcpy(right,pd+DES_DATA_BLOCK_LEN/(2*8),DES_DATA_BLOCK_LEN/(2*8));
for(i=0;i<DES_SUBKEYS;i++){
data_expansion_perm(right,expanded);
- for(j=0;j<DES_SUBKEY_LEN;j++) expanded[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
+ for(j=0;j<DES_SUBKEY_LEN/8;j++) expanded[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
data_s_and_p(expanded);
-
+ memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
+ for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)
+ right[j]=expanded[j]^left[j];
+ if(i!=15) memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8));
}
-
+ memcpy(pd,left,DES_DATA_BLOCK_LEN/(2*8));
+ memcpy(pd+DES_DATA_BLOCK_LEN/(2*8),right,DES_DATA_BLOCK_LEN/(8*2));
+ data_final_perm(data,pd);
return 1;
}
-int des_crypt_decrypt(u8 *plain,u8 *crypted,u8 *key,int len,u8 mode) {
+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;
/* compute the subkeys:
- loop 16 times
- do final permutation
*/
-
- /* for all data do: (not yet ready) */
- progress_data(plain,subkey,mode);
-
+ 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 .. :/ */
+ }
return 1;
}