From: hackbard Date: Thu, 2 Oct 2003 20:11:30 +0000 (+0000) Subject: small clean ups X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=967ba6ad8f3dbdf974e29e92badcd25af47baf6f;p=my-code%2Fcrypto.git small clean ups --- diff --git a/des.c b/des.c index d71317d..d26ae60 100644 --- a/des.c +++ b/des.c @@ -101,13 +101,11 @@ int rotate_left(u8 *obj,int len) { 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>((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>((8-table[i]%8)&7))&1)<<(7-i%8)); return 1; } @@ -120,96 +118,64 @@ int subkey_trans(u8 *key,int round) { return 1; } -int subkey_perm(u8 *key,u8 *pc) { - int i; - memset(pc,0,DES_SUBKEY_LEN/8); - for(i=0;i>((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>((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>((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>((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>((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