From: hackbard Date: Fri, 3 Oct 2003 03:28:48 +0000 (+0000) Subject: more testing X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=bda14e4049f762cebc83d71f23949c0ec44b1c79;p=my-code%2Fcrypto.git more testing --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ddd24c4 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +#!/usr/bin/make + +CC = gcc +CFLAGS = -O3 -Wall + +TARGETS = test + +all: $(TARGETS) + +test: des.o + +des.o: des.c diff --git a/des.c b/des.c index d26ae60..5874d44 100644 --- a/des.c +++ b/des.c @@ -5,9 +5,10 @@ * */ +#include #include "des.h" -u8 key_perm_table[DES_SUBKEY_LEN]={ +u8 key_perm_table[DES_INITIAL_KEY_LEN]={ 57,49,41,33,25,17,9,1,58,50,42,34,26,18, 10,2,59,51,43,35,27,18,11,3,60,52,44,36, 63,55,47,39,31,23,15,7,62,54,46,38,30,22, @@ -72,7 +73,7 @@ u8 sbox[DES_SBOXES][DES_SBOX_LEN]={ 1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2, 7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8, 2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11} -} +}; u8 pbox[DES_PBOX_LEN]={ 16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10, @@ -96,7 +97,7 @@ int rotate_right(u8 *obj,int len) { int rotate_left(u8 *obj,int len) { int i=0; u8 tmp=obj[0]; - for(i=0;i>7); + for(i=0;i>7); obj[len-1]=obj[len-1]<<1|(tmp>>7); return 1; } @@ -104,8 +105,8 @@ int rotate_left(u8 *obj,int len) { int table_trans(u8 *src,u8 *final,u8 *table,int len) { int i; memset(final,0,len/8); - for(i=0;i>((8-table[i]%8)&7))&1)<<(7-i%8)); + for(i=0;i>((8-table[i]%8)&7))&1)<<(7-i%8)); return 1; } @@ -125,7 +126,7 @@ int compute_subkeys(u8 *key0,u8 **subkey) { table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN); for(i=0;i>(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)]); + 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,j; + int i; for(i=0;i 56bit - obtain subkeys by: @@ -208,21 +207,21 @@ int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) { - do final permutation */ for(i=0;i +#include +#include +#include + +#include "des.h" + + +int main() { + u8 plain[64]; + u8 crypted[64]; + u8 key[8]; + int fd,i; + + if((fd=open("/dev/urandom",O_RDONLY))<0) { + puts("open urandom device failed!"); + return fd; + } + + printf("des crypt/decrypt test:\n"); + printf("- geberating random key ...\n"); + read(fd,key,8); + printf("key: "); + for(i=0;i<8;i++) printf("%02x ",key[i]); + puts(""); + + memset(plain,0,64); + strcpy(plain,"allyouratmels ... :)"); + + printf("encrypting '%s' (ecb mode) ...\n",plain); + memset(crypted,0,64); + des_encrypt(plain,crypted,key,64,MODE_ECB); + printf("crypted: "); + for(i=0;i<64;i++) printf("%c",crypted[i]); + puts(""); + printf("encrypting '%s' (cbc mode) ...\n",plain); + memset(crypted,0,64); + des_encrypt(plain,crypted,key,64,MODE_CBC); + printf("crypted: "); + for(i=0;i<64;i++) printf("%c",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]); + puts(""); + + puts("done :)"); + + return 1; +}