ecb mode working, still bug in cbc mode.
[my-code/crypto.git] / test.c
1 /*
2  * test.c - test ciphers ...
3  *
4  * hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "des.h"
15
16 #define LEN 32
17
18 int main() {
19  u8 plain[LEN];
20  u8 crypted[LEN];
21  u8 plain_new[LEN];
22  u8 key[8];
23  int fd,i;
24
25  if((fd=open("/dev/urandom",O_RDONLY))<0) {
26   puts("open urandom device failed!");
27   return fd;
28  }
29
30  printf("des crypt/decrypt test:\n");
31  printf("- generating simple/random key ...\n");
32  read(fd,key,8);
33  printf("key: ");
34  for(i=0;i<8;i++) printf("%02x ",key[i]);
35  puts("");
36
37  memset(plain,0,LEN);
38  strcpy(plain,"allyourbase");
39  printf("encrypting '%s' \n",plain);
40  
41  printf("ecb mode:\n");
42  memset(crypted,0,LEN);
43  des_encrypt(plain,crypted,key,LEN,MODE_ECB);
44  printf("plain: ");
45  for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
46  puts("");
47  printf("crypted: ");
48  for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
49  puts("");
50  des_decrypt(crypted,plain_new,key,LEN,MODE_ECB);
51  printf("new plain: ");
52  for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
53  puts("");
54  if(!strcmp(plain,plain_new)) printf("looks good!\n");
55
56  printf("cbc mode:\n");
57  memset(crypted,0,LEN);
58  memset(plain_new,0,LEN);
59  printf("plain: ");
60  for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
61  puts("");
62  des_encrypt(plain,crypted,key,LEN,MODE_CBC);
63
64  /* test */
65  printf("plain: ");
66  for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
67  puts("");
68  /* test end */
69
70  printf("crypted: ");
71  for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
72  puts("");
73  des_decrypt(crypted,plain_new,key,LEN,MODE_CBC);
74  printf("new plain: ");
75  for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
76  puts("");
77  if(!strcmp(plain,plain_new)) printf("looks good!\n");
78  
79  close(fd);
80
81  puts("done :)");
82
83  return 1;
84 }