ecb mode working, still bug in cbc mode. master origin
authorhackbard <hackbard>
Sun, 5 Oct 2003 22:32:48 +0000 (22:32 +0000)
committerhackbard <hackbard>
Sun, 5 Oct 2003 22:32:48 +0000 (22:32 +0000)
Makefile
des.c
test.c

index ddd24c4..ca582f1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,3 +10,8 @@ all: $(TARGETS)
 test: des.o
 
 des.o: des.c
+
+clean:
+       rm -f *.o test
+
+remake: clean all
diff --git a/des.c b/des.c
index 2101a95..a001060 100644 (file)
--- a/des.c
+++ b/des.c
@@ -5,7 +5,6 @@
  *
  */
 
-#include <stdio.h>
 #include <string.h>
 #include "des.h"
 
@@ -111,9 +110,6 @@ int rotate_left(u8 *obj) {
  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;
 }
 
@@ -137,8 +133,6 @@ int compute_subkeys(u8 *key0,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8]) {
  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);
@@ -182,11 +176,15 @@ int progress_data(u8 *data,u8 *crypted,u8 subkey[DES_SUBKEYS][DES_SUBKEY_LEN/8],
   /* 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]=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)); 
+  if(i!=15) {
+   memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
+   for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)    
+    right[j]=permuted_data[j]^left[j];
+   /* switch left and right data - not for last round */
+   memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8));
+  }
+  else
+   for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++) left[j]^=permuted_data[j];
  }
  /* merge and do final permutation */
  memcpy(permuted_data,left,DES_DATA_BLOCK_LEN/(2*8));
@@ -208,12 +206,6 @@ int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) {
       - 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
diff --git a/test.c b/test.c
index 35b75a8..98d1d6b 100644 (file)
--- a/test.c
+++ b/test.c
 #include <string.h>
 #include "des.h"
 
+#define LEN 32
 
 int main() {
- u8 plain[32];
- u8 crypted[32];
+ u8 plain[LEN];
+ u8 crypted[LEN];
+ u8 plain_new[LEN];
  u8 key[8];
  int fd,i;
 
@@ -27,35 +29,52 @@ int main() {
 
  printf("des crypt/decrypt test:\n");
  printf("- generating simple/random key ...\n");
- // read(fd,key,8);
- memset(key,0x90,8);
+ read(fd,key,8);
  printf("key: ");
  for(i=0;i<8;i++) printf("%02x ",key[i]);
  puts("");
 
- memset(plain,0,32);
- // strcpy(plain,"allyouratmels ... :)");
- printf("encrypting '");
- for(i=0;i<32;i++) printf("%02x%c",plain[i],i==31?'\n':' ');
+ memset(plain,0,LEN);
+ strcpy(plain,"allyourbase");
+ printf("encrypting '%s' \n",plain);
  
- printf("encrypting (ecb mode) ...\n");
- memset(crypted,0,32);
- des_encrypt(plain,crypted,key,32,MODE_ECB);
+ printf("ecb mode:\n");
+ memset(crypted,0,LEN);
+ des_encrypt(plain,crypted,key,LEN,MODE_ECB);
  printf("plain: ");
- for(i=0;i<32;i++) printf("%02x ",plain[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
  puts("");
  printf("crypted: ");
- for(i=0;i<32;i++) printf("%02x ",crypted[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
  puts("");
- printf("decrypting (ecb mode) ...\n");
- memset(plain,0,32);
- des_decrypt(crypted,plain,key,32,MODE_ECB);
- printf("crypted: ");
- for(i=0;i<32;i++) printf("%02x ",crypted[i]);
+ des_decrypt(crypted,plain_new,key,LEN,MODE_ECB);
+ printf("new plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
  puts("");
+ if(!strcmp(plain,plain_new)) printf("looks good!\n");
+
+ printf("cbc mode:\n");
+ memset(crypted,0,LEN);
+ memset(plain_new,0,LEN);
+ printf("plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
+ puts("");
+ des_encrypt(plain,crypted,key,LEN,MODE_CBC);
+
+ /* test */
  printf("plain: ");
- for(i=0;i<32;i++) printf("%02x ",plain[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
+ puts("");
+ /* test end */
+
+ printf("crypted: ");
+ for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
+ puts("");
+ des_decrypt(crypted,plain_new,key,LEN,MODE_CBC);
+ printf("new plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
  puts("");
+ if(!strcmp(plain,plain_new)) printf("looks good!\n");
  
  close(fd);