return 1;
}
-key_perm(u8 *key) {
+int table_trans(u8 *src,u8 *final,u8 *table,int len) {
int i;
- u8 new[DES_INITIAL_KEY_LEN/8];
- memset(new,0,DES_INITIAL_KEY_LEN/8];
- for(i=0;i<DES_INITIAL_KEY_LEN;i++)
- new[i/8]|=((((key[(key_perm_table[i]-1)/8)]>>((8-key_perm_table[i]%8)&7))&1)<<(7-i%8));
- memcpy(key,new,DES_INITIAL_KEY_LEN);
+ 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));
return 1;
}
return 1;
}
-int subkey_perm(u8 *key,u8 *pc) {
- int i;
- memset(pc,0,DES_SUBKEY_LEN/8);
- for(i=0;i<DES_SUBKEY_LEN;i++)
- pc[i/8]|=((((key[subkey_perm_table[i]-1)/8]>>((8-subkey_perm_table[i]%8)&7))&1)<<(7-i%8));
- return 1;
-}
-
int compute_subkeys(u8 *key0,u8 **subkey) {
int i;
- key_perm(key0);
+ u8 key0_56[DES_INITIAL_KEY_LEN/8];
+ /* initial permutation of key */
+ table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN);
for(i=0;i<DES_SUBKEYS;i++) {
- subkey_trans(key0;i);
- subkey_perm(key0,subkey[i]);
+ /* split and rotate key_056 */
+ subkey_trans(key0_56;i);
+ /* chose 48 bits as a subkey */
+ table_trans(key0_56,subkey[i],subkey_perm_table,DES_SUBKEY_LEN);
}
return 1;
}
-int data_perm(u8 *plain,u8 *pd) {
- int i;
- memset(pd,0,DES_DATA_BLOCK_LEN/8);
- for(i=0;i<DES_DATA_BLOCK_LEN;i++)
- pd[i/8]|=((((plain[data_perm_table[i]-1)/8]>>((8-data_perm_table[i]%8)&7))&1)<<(7-i%8));
- return 1;
-}
-
-int data_expansion_perm(u8 *right,u8 expanded) {
- int i;
- 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;
-}
-
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)]);
}
-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/(8*2)];
- u8 p[DES_DATA_BLOCK_LEN/(8*2)];
+ u8 s[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);
- data_pbox_perm(s,p);
- memcpy(data,p,DES_DATA_BLOCK_LEN/(8*2));
+ table_trans(s,data,pbox,DES_PBOX_LEN);
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/8];
+ u8 permuted_data[DES_DATA_BLOCK_LEN/8];
u8 left[DES_DATA_BLOCK_LEN/(2*8)];
u8 right[DES_DATA_BLOCK_LEN/(2*8)];
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));
+ /* initial permutation */
+ table_trans(data,permuted_data,data_perm_table,DES_DATA_BLOCK_LEN);
+ memcpy(left,permuted_data,DES_DATA_BLOCK_LEN/(2*8));
+ memcpy(right,permuted_data+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/8;j++) expanded[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
- data_s_and_p(expanded);
+ /* expand right data to 48 bit */
+ table_trans(right,permuted_data,data_expansion_table,DES_SUBKEY_LEN);
+ /* xor with subkey */
+ for(j=0;j<DES_SUBKEY_LEN/8;j++) permuted_data[j]^=subkey[mode&MODE_CRYPT?i:DES_SUBKEYS-1-i][j];
+ /* sbox substitutions and pbox permutations */
+ data_s_and_p(permuted_data);
+ /* xor with left data */
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];
+ right[j]=permuted_data[j]^left[j];
+ /* switch left and right data - not for last round */
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);
+ /* 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);
return 1;
}