*
*/
+#include <stdio.h>
#include <string.h>
#include "des.h"
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];
- while(--len) obj[len]=obj[len]>>1|((obj[len-1]&1)<<7);
- obj[0]=obj[0]>>1|((tmp&1)<<7);;
+int rotate_right(u8 *obj) {
+ u8 mid=obj[3],tmp=obj[6];
+ int i;
+ for(i=6;i>3;i++)
+ obj[i]=obj[i]>>1|((obj[i-1]&1)<<7);
+ obj[3]=(obj[3]&0xf)>>1|((tmp&1)<<3);
+ obj[3]|=(((obj[2]&1)<<7)|((mid&0xe0)>>1));
+ for(i=2;i>0;i++)
+ obj[i]=obj[i]>>1|((obj[i-1]&1)<<7);
+ obj[0]=obj[0]>>1|((mid&0x10)<<3);
return 1;
}
-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[i+1]>>7);
- obj[len-1]=obj[len-1]<<1|(tmp>>7);
+int rotate_left(u8 *obj) {
+ u8 mid=obj[3],tmp=obj[0];
+ int i;
+ for(i=0;i<3;i++)
+ obj[i]=obj[i]<<1|(obj[i+1]>>7);
+ obj[3]=((obj[3]&0xf0)<<1)|((tmp&0x80)>>3);
+ obj[3]|=(((mid&7)<<1)|(obj[4]>>7));
+ for(i=4;i<6;i++)
+ obj[i]=(obj[i]<<1)|(obj[i+1]>>7);
+ obj[6]=obj[6]<<1|((mid&8)>>3);
+ printf("debug: ");
+ for(i=0;i<7;i++) printf("%02x ",obj[i]);
+ printf("\n");
return 1;
}
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));
- return 1;
+ final[i/8]|=((((src[(table[i]-1)/8])>>((8-(table[i])%8)&7))&1)<<(7-i%8));
+ return 1;
}
int subkey_trans(u8 *key,int round) {
int i;
- for(i=0;i<rot_per_round_table[round];i++) {
- rotate_left(key,DES_INITIAL_KEY_LEN/(8*2));
- rotate_left(key+DES_INITIAL_KEY_LEN/(8*2),DES_INITIAL_KEY_LEN/(8*2));
- }
+ for(i=0;i<rot_per_round_table[round];i++)
+ rotate_left(key);
return 1;
}
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<7;i++) printf("%02x ",key0_56[i]);
+ printf("\n");
for(i=0;i<DES_SUBKEYS;i++) {
/* split and rotate key_056 */
subkey_trans(key0_56,i);
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_s_and_p(u8 *data) {
u8 s[DES_DATA_BLOCK_LEN/(8*2)];
- 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);
+ memset(s,0,DES_DATA_BLOCK_LEN/(8*2));
+ s[0]=sbox[0][16*(((data[0]&0x80)>>6)|((data[0]&4)>>2))+((data[0]&0x78)>>3)]<<4;
+ s[0]|=sbox[1][16*((data[0]&2)|((data[1]&0x10)>>4))+(((data[0]&1)<<3)|(data[1]&0xe0)>>5)];
+ s[1]=sbox[2][16*(((data[1]&8)>>2)|((data[2]&0x40)>>6))+(((data[1]&7)<<1)|((data[2]&0x80)>>7))]<<4;
+ s[1]|=sbox[3][16*(((data[2]&0x20)>>4)|(data[2]&1))+((data[2]&0x1e)>>1)];
+ s[2]=sbox[4][16*(((data[3]&0x80)>>6)|((data[3]&4)>>2))+((data[3]&0x78)>>3)]<<4;
+ s[2]|=sbox[5][16*((data[3]&2)|((data[4]&0x10)>>4))+(((data[3]&1)<<3)|(data[4]&0xe0)>>5)];
+ s[3]=sbox[6][16*(((data[4]&8)>>2)|((data[5]&0x40)>>6))+(((data[4]&7)<<1)|((data[5]&0x80)>>7))]<<4;
+ s[3]|=sbox[7][16*(((data[5]&0x20)>>4)|(data[5]&1))+((data[5]&0x1e)>>1)];
table_trans(s,data,pbox,DES_PBOX_LEN);
return 1;
}
- loop 16 times
*/
compute_subkeys(key,subkey);
+ printf("debug:\n");
+ for(i=0;i<16;i++) {
+ printf("subkey %d: ",i);
+ for(j=0;j<6;j++) printf("%02x ",subkey[i][j]);
+ printf("\n");
+ }
/* split data to 64bit blocks, and crypt/decrypt each block
depending on mode (CBC/ECB):
- initial data transformation
int main() {
- u8 plain[64];
- u8 crypted[64];
+ u8 plain[32];
+ u8 crypted[32];
u8 key[8];
int fd,i;
}
printf("des crypt/decrypt test:\n");
- printf("- geberating random key ...\n");
- read(fd,key,8);
+ printf("- generating simple/random key ...\n");
+ // read(fd,key,8);
+ memset(key,0x90,8);
printf("key: ");
for(i=0;i<8;i++) printf("%02x ",key[i]);
puts("");
- memset(plain,0,64);
- strcpy(plain,"allyouratmels ... :)");
+ memset(plain,0,32);
+ // strcpy(plain,"allyouratmels ... :)");
+ printf("encrypting '");
+ for(i=0;i<32;i++) printf("%02x%c",plain[i],i==31?'\n':' ');
- printf("encrypting '%s' (ecb mode) ...\n",plain);
- memset(crypted,0,64);
- des_encrypt(plain,crypted,key,64,MODE_ECB);
+ printf("encrypting (ecb mode) ...\n");
+ memset(crypted,0,32);
+ des_encrypt(plain,crypted,key,32,MODE_ECB);
+ printf("plain: ");
+ for(i=0;i<32;i++) printf("%02x ",plain[i]);
+ puts("");
printf("crypted: ");
- for(i=0;i<64;i++) printf("%c",crypted[i]);
+ for(i=0;i<32;i++) printf("%02x ",crypted[i]);
puts("");
- printf("encrypting '%s' (cbc mode) ...\n",plain);
- memset(crypted,0,64);
- des_encrypt(plain,crypted,key,64,MODE_CBC);
+ printf("decrypting (ecb mode) ...\n");
+ memset(plain,0,32);
+ des_decrypt(crypted,plain,key,32,MODE_ECB);
printf("crypted: ");
- for(i=0;i<64;i++) printf("%c",crypted[i]);
+ for(i=0;i<32;i++) printf("%02x ",crypted[i]);
puts("");
-
- printf("--\n");
- printf("verify:\n");
- des_decrypt(crypted,plain,key,64,MODE_CBC);
- printf("plain text: ");
- for(i=0;i<64;i++) printf("%c",plain[i]);
+ printf("plain: ");
+ for(i=0;i<32;i++) printf("%02x ",plain[i]);
puts("");
+
+ close(fd);
puts("done :)");