1 /*
2  * Copyright (C) 2001, 2002, and 2003  Roy Keene
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  *      email: dact@rkeene.org
19  */
20 
21 #include "dact.h"
22 #include <math.h>
23 #include <stdio.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_STAT_H
37 #include <sys/stat.h>
38 #endif
39 #include <fcntl.h>
40 #include "ui.h"
41 #include "cipher_chaos.h"
42 
43 
44 #if defined(USE_MODULES) && defined(AS_MODULE)
45 #include "module.h"
46 uint32_t DC_NUM=0;
47 uint32_t DC_TYPE=DACT_MOD_TYPE_ENC;
48 void *DC_ALGO=cipher_chaos;
49 char *DC_NAME="chaos (MOD)";
50 #endif
51 
52 
cipher_chaos(const char * inblock,char * outblock,const int blksize,char * key,const int mode)53 int cipher_chaos(const char *inblock, char *outblock, const int blksize, char *key, const int mode) {
54 	switch (mode) {
55 		case (DACT_MODE_CINIT+DACT_MODE_CDEC):
56 		case (DACT_MODE_CINIT+DACT_MODE_CENC):
57 		case DACT_MODE_CINIT:
58 			return(cipher_chaos_init(mode,key));
59 			break;
60 		case DACT_MODE_CDEC:
61 			return(cipher_chaos_encdec(inblock, outblock, blksize, key));
62 			break;
63 		case DACT_MODE_CENC:
64 #ifdef DACT_CIPHER_CHAOS
65 			return(cipher_chaos_encdec(inblock, outblock, blksize, key));
66 #else
67 			dact_ui_status(DACT_UI_LVL_GEN, "The chaos cipher is no longer supported.");
68 			return(-1);
69 #endif
70 			break;
71 	}
72 	return(0);
73 }
74 
cipher_chaos_init(const int mode,char * key)75 int cipher_chaos_init(const int mode, char *key) {
76 	return(cipher_chaos_init_getkey(mode-DACT_MODE_CINIT,key));
77 }
78 
cipher_chaos_init_getkey(const int mode,char * key)79 int cipher_chaos_init_getkey(const int mode, char *key) {
80 	strcpy(key,dact_ui_getuserinput("File Identification Number: ", 128, 1));
81 	return(1);
82 }
83 
cipher_chaos_encdec(const char * blk,char * outblk,const int blksize,char * key)84 int cipher_chaos_encdec(const char *blk, char *outblk, const int blksize, char *key) {
85 	double fkey;
86 	int y=0,i;
87 
88 	fkey=(double) atoi(key);
89 	for (i=0;i<blksize;i++) {
90 		outblk[i]=blk[i]^cipher_chaos_getbyte(&fkey,y);
91 	}
92 	return(blksize);
93 }
94 
cipher_chaos_getbyte(double * x,int y)95 unsigned char cipher_chaos_getbyte(double *x, int y) {
96 	static int i=0;
97 
98 	NORM(*x);
99 	FIX(*x);
100 	BEST(*x,y);
101 	*x=(double) (R(*x)*(1-*x));
102 	i++;
103 SPOTVAR_NUM(y);
104 	return((unsigned char) y);
105 }
106