1 /***************************************************************************
2                           main.c  -  experimental frontend for the encrypting
3                           decrypting engine.
4                              -------------------
5     begin                : Thu Nov 16 16:59:50 EST 2000
6     copyright            : (C) 2000 by Brian Wagener and Katrina Illari
7     email                : CptanPanic@users.sourceforge.net
8 
9 
10     Patch:  Dale Amon <amon@vnl.com>
11             cryptEncode unconditionally free'd keyfile. Fixed it.
12 
13  ***************************************************************************/
14 
15 /***************************************************************************
16  *                                                                         *
17  *   This program is free software; you can redistribute it and/or modify  *
18  *   it under the terms of the GNU General Public License as published by  *
19  *   the Free Software Foundation; either version 2 of the License, or     *
20  *   (at your option) any later version.                                   *
21  *                                                                         *
22  ***************************************************************************/
23 
24 #include <stdio.h>
25 #include <globaldefs.h>
26 #include <rijndael-api-fst.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #include <crypt.h>
31 
32 #define FILENAMESIZE 100
33 
34 extern int interactive;
35 
cryptEncode(stword32 nb,stword32 nk,char * keyfile,FILE * fin,FILE * fout)36 strc cryptEncode(stword32 nb, stword32 nk, char *keyfile ,
37 		 FILE *fin, FILE *fout){
38   keyInstance key;
39   unsigned char *pKey;
40   cipherInstance cipher;
41   char inbuff[MAX_NB/8], outbuff[MAX_NB/8];
42   int rc;
43 
44 
45   pKey = malloc(nk/8*2); /* Note that the key is twice as big as the key length
46 			   because we store a binary byte using 2 ascii chars
47 			   as nibbles*/
48 
49   // Get key filename, if it wasn't read on command line,
50   // either prompt for one, or create on.
51   if( !keyfile ){
52     keyfile = malloc(sizeof(char) * FILENAMESIZE );
53     if( interactive ){
54       fprintf(stderr,"Enter key filename: ");
55       scanf("%s",keyfile);
56     } else { //For now create key filename using time().
57       sprintf(keyfile,"keyfile.%i",(int)time(NULL));
58       createKey(nk,pKey,keyfile);
59       fprintf(stderr,"The %d bit key created is %s \n",(int)nk, keyfile);
60     }
61     nk = readKey(pKey, keyfile);
62     free(keyfile);
63   } else {
64     nk = readKey(pKey, keyfile);
65   }
66 
67 
68   if( !nk) {
69     fprintf(stderr,"Error Reading in Key\n");
70     exit( EXIT_FAILURE);
71   }
72 
73   if ( !(rc = makeKey(&key, DIR_ENCRYPT, nk, pKey)) ){
74     fprintf(stderr, "Failure to initialize key with error code %d\n", rc);
75     free(pKey);
76     return FAILURE;
77   }
78   if (cipherInit(&cipher, MODE_CBC, NULL)!=TRUE){
79     fprintf(stderr, "Failure to initialize cipher\n");
80     free(pKey);
81     return FAILURE;
82   }
83 
84 
85   fprintf(fout,"%s ",keyfile);
86   fwrite(&nb, sizeof (stword32), 1, fout); // Store blocksize
87 
88 
89   while(fread(inbuff, 1, nb/8, fin)){
90     memset(outbuff, 0, sizeof(outbuff));
91     rc=blockEncrypt(&cipher, &key, inbuff, nb, outbuff);
92     if (rc != nb)
93       return FAILURE;
94     fwrite(outbuff, 1, nb/8, fout);
95     memset(inbuff, 0, sizeof(inbuff));
96 
97   }
98 
99   fclose(fout);
100   fclose(fin);
101   free(pKey);
102   return SUCCESS;
103 }
104 
cryptDecode(stword32 nk,char * keyfile,FILE * fin,FILE * fout)105 strc cryptDecode(stword32 nk, char *keyfile, FILE *fin, FILE *fout){
106   keyInstance key;
107   unsigned char *pKey;
108   cipherInstance cipher;
109   char inbuff[MAX_NB/8], outbuff[MAX_NB/8];
110   stword32 nb, realDataSize;
111   char str[FILENAMESIZE];
112 
113 
114   pKey = malloc(nk/8*2); /* Note that the key is twice as big as the key length
115 			   because we store a binary byte using 2 ascii chars
116 			   as nibbles*/
117 
118 
119 
120   if( !keyfile ){
121     keyfile = malloc(sizeof(char) * FILENAMESIZE );
122     fscanf(fin,"%s ",keyfile);
123     fprintf(stderr,"The keyname found was %s\n",keyfile);
124     nk = readKey(pKey, keyfile);
125     fprintf(stderr,"The %i bit key read in is %s \n", (int)nk, keyfile);
126     free(keyfile);
127   } else {
128     nk = readKey(pKey, keyfile);
129     fscanf(fin,"%s ",str);
130   }
131 
132 
133   if (makeKey(&key, DIR_DECRYPT, nk, pKey)!=TRUE)
134     {
135       fprintf(stderr,"SECTAR: Error initializing key\n");
136       return FAILURE;
137     }
138   if (cipherInit(&cipher, MODE_CBC, NULL)!=TRUE)
139     {
140       fprintf(stderr,"SECTAR: Error initializing cipher\n");
141       return FAILURE;
142     }
143 
144   fread(&nb, sizeof (stword32), 1, fin);
145   fprintf(stderr,"Blocksize is %d\n",(int)nb);
146 
147   while(fread(inbuff, 1, nb/8, fin)){
148     memset(outbuff,0,sizeof(outbuff));
149     if (blockDecrypt(&cipher, &key, inbuff, nb, outbuff)!= nb)
150       return (FAILURE);
151     /*    realDataSize=(filesize/(nb/8))?nb/8:filesize;
152 	  filesize-=realDataSize;
153 	  fwrite(outbuff, 1, realDataSize, fout);
154     */
155     fwrite(outbuff, 1, nb/8, fout);
156   }
157 
158   fclose(fout);
159   fclose(fin);
160   free(pKey);
161   return SUCCESS;
162 }
163 
createKey(int nk,unsigned char * key,char * keyfile)164 int createKey(int nk, unsigned char *key, char *keyfile){
165   FILE *fout = 0;
166   int i;
167   unsigned char buf;
168 
169   if ((fout = fopen (keyfile, "w")))
170     {
171       FILE *ran;
172       if ((ran=fopen ("/dev/random", "r"))==NULL){
173 	fprintf(stderr, "Could not open random device\n");
174 	return EXIT_FAILURE;
175       }
176 
177       for (i = 0; i < nk/8*2; i +=2) {
178 	char t, j;
179 
180 	if ( !fread(&buf, 1, 1, ran ) ){
181 	  fprintf(stderr, "Error in making key\n"
182 		  "Coudn't read from /dev/random\n");
183 	  return EXIT_FAILURE;
184 	}
185 
186 	t = (buf & 0xf0) >> 4;
187 	j = t + 0x30;
188 	if( t > 0x9 ) j = t + 0x37;
189 	key[i] = j;
190 
191 	t = buf & 0x0f ;
192 	j = t + 0x30;
193 	if( t > 0x9 ) j = t + 0x37;
194 	key[i+1] = j;
195       }
196 
197       fwrite(key, 1, nk/8 * 2, fout); // Write entire key to file.
198       fclose(fout);
199       fclose(ran);
200     }else{
201       fprintf(stderr,"Could not create key file\n");
202       return EXIT_FAILURE;
203     }
204   return nk;
205 }
206 
readKey(unsigned char * key,char * file)207 int readKey(unsigned char *key, char *file){
208   FILE *fin;
209   int nk, rc;
210 
211 
212   if ( (fin = fopen (file, "r"))) {
213     fseek(fin, 0, SEEK_END);  // Block gets filesize to be used in header.
214     nk = ftell(fin) ;	// This is the keysize since we are using 2 bytes to store 8 bits.
215     nk = nk * 4;
216     fseek(fin, 0, SEEK_SET);
217     rc = fread(key,1,nk/8*2,fin);
218     if( rc != nk/8 * 2){
219       fprintf(stderr, "Error: Unable to read in entire key read in %d of %d\n",rc,nk);
220       return EXIT_FAILURE;
221     } else if ( nk != 128 && nk != 198 && nk != 256 ){
222       fprintf(stderr,"Error: %d Key read in was not valid size, must be 128,192 or 256\n",nk);
223       return EXIT_FAILURE;
224     }
225     fclose(fin);
226 
227   } else {
228     fprintf(stderr,"Could not open key file\n");
229     return EXIT_FAILURE;
230   }
231   return nk;
232 }
233 
234