4556580 [rkeene@sledge /home/rkeene/devel/dact]$ cat -n cipher_sub.c
  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 /*
 22     Encrypt data.
 23 */
 24 #include "cipher_sub.h"
 25 #include "parse.h"
 26 #include "dact.h"
 27 #include "ui.h"
 28 #ifdef HAVE_UNISTD_H
 29 #include <unistd.h>
 30 #endif
 31 #ifdef HAVE_STDLIB_H
 32 #include <stdlib.h>
 33 #endif
 34 #ifdef HAVE_STRING_H
 35 #include <string.h>
 36 #endif
 37 #ifdef HAVE_SYS_TYPES_H
 38 #include <sys/types.h>
 39 #endif
 40 #ifdef HAVE_SYS_STAT_H
 41 #include <sys/stat.h>
 42 #endif
 43 #include <fcntl.h>
 44 #include <stdio.h>
 45 #ifndef RANDOM_DEV
 46 #ifdef TIME_WITH_SYS_TIME
 47 #include <sys/time.h>
 48 #include <time.h>
 49 #else
 50 #ifdef HAVE_SYS_TIME_H
 51 #include <sys/time.h>
 52 #else
 53 #include <time.h>
 54 #endif
 55 #endif
 56 #endif
 57 
 58 #if defined(USE_MODULES) && defined(AS_MODULE)
 59 #include "module.h"
 60 uint32_t DC_NUM=1;
 61 uint32_t DC_TYPE=DACT_MOD_TYPE_ENC;
 62 void *DC_ALGO=cipher_sub;
 63 char *DC_NAME="subst (MOD)";
 64 #endif
 65 
 66 
 67 int cipher_sub(const char *inblock, char *outblock, const int blksize, char *key, const int mode) {
 68     switch (mode) {
 69         case (DACT_MODE_CINIT+DACT_MODE_CDEC):
 70         case (DACT_MODE_CINIT+DACT_MODE_CENC):
 71         case DACT_MODE_CINIT:
 72             return(cipher_sub_init(mode,key));
 73             break;
 74         case DACT_MODE_CDEC:
 75             return(cipher_sub_decrypt(inblock, outblock, blksize, key));
 76             break;
 77         case DACT_MODE_CENC:
 78             return(cipher_sub_encrypt(inblock, outblock, blksize, key));
 79             break;
 80     }
 81     return(0);
 82 }
 83 
 84 
 85 int cipher_sub_init(const int mode, char *key) {
 86     return(cipher_sub_init_getkey(mode-DACT_MODE_CINIT,key));
 87 }
 88 
 89 int cipher_sub_init_getkey(const int mode, char *key) {
 90     char *fname;
 91     char keybuf[1024], *ptrbuf;
 92     int fd,x=257;
 93 
 94     fname=dact_ui_getuserinput("Key file: ",128,0);
 95     fd=open(fname, O_RDONLY);
 96     if (fd>=0) {
 97         x=read(fd, &keybuf, sizeof(keybuf));
 98         if (x==257) {
 99             memcpy(key,keybuf,257); /* For backward compatability with DACT 0.8.1*/
100         } else {
101             memcpy(key,ptrbuf=demime64(keybuf),257);
102             free(ptrbuf);
103         }
104         close(fd);
105         return(257);
106     }
107     if (fd<0 && mode==DACT_MODE_CENC) {
108         fd=open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0600);
109         if (fd<0) { PERROR("open"); return(0); }
110         memcpy(key,generatekey(),257);
111         memcpy(keybuf, ptrbuf=mimes64(key,&x), 400);
112         write(fd, keybuf, x);
113         write(fd, "\n", 1);
114         close(fd);
115         free(ptrbuf);
116         return(257);
117     }
118     return(-1);
119 }
120 
121 int cipher_sub_encrypt(const unsigned char *inblk, unsigned char *outblk, int blksize, unsigned char *key) {
122     int i,mod;
123     static int keyoffset=0;
124 
125     mod=(int) key[0];
126     for (i=0;i<blksize;i++) {
127         if (!(i%mod)) {
128             keyoffset=((keyoffset+1)&0xff);
129         }
130         outblk[i]=key[((((int) inblk[i])+keyoffset)&0xff)+1];
131     }
132     return(blksize);
133 }
134 
135 int cipher_sub_decrypt(const unsigned char *inblk, unsigned char *outblk, int blksize, unsigned char *key) {
136     int i,mod,x;
137     char reversekey[256];
138     static int keyoffset=0;
139 
140     mod=(int) key[0];
141     for (i=1;i<257;i++) reversekey[(int) key[i]]=(i-1);
142     for (i=0;i<blksize;i++) {
143         if (!(i%mod)) {
144             keyoffset=((keyoffset+1)&0xff);
145             for (x=0;x<256;x++) reversekey[(int) key[((x+keyoffset)&0xff)+1]]=x;
146         }
147         outblk[i]=reversekey[(int) inblk[i]];
148     }
149     return(blksize);
150 }
151 
152 char *generatekey(void) {
153     static char key[257];
154     char used[256];
155     int i,x;
156 #ifdef RANDOM_DEV
157     unsigned char buff[1];
158     int fd;
159     fd=open(RANDOM_DEV, O_RDONLY);
160     read(fd, buff, 1);
161     if (buff[0]==0) buff[0]=3;
162     key[0]=buff[0];
163 #else
164     srand(time(NULL)+rand());
165     key[0]=1+(int) (255.0*rand()/(RAND_MAX+1.0));
166 #endif
167     for (i=0;i<256;i++) used[i]=0;
168     for (i=1;i<257;i++) {
169 #ifdef RANDOM_DEV
170         read(fd, buff, 1);
171         x=buff[0];
172 #else
173         srand(time(NULL)+rand());
174         x=(int) (256.0*rand()/(RAND_MAX+1.0));
175 #endif
176         if (used[x]==0) { key[i]=x; used[x]=1; } else { i--; }
177     }
178 #ifdef RANDOM_DEV
179     close(fd);
180 #endif
181     return(key);
182 }
4556581 [rkeene@sledge /home/rkeene/devel/dact]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-04-04 07:01:49