1 /* ====================================================================
2  * Copyright (c) 2002 Johnny Shelley.  All rights reserved.
3  *
4  * Bcrypt is licensed under the BSD software license. See the file
5  * called 'LICENSE' that you should have received with this software
6  * for details
7  * ====================================================================
8  */
9 
10 #include "includes.h"
11 #include "defines.h"
12 #include "functions.h"
13 
getremain(uLong sz,int dv)14 int getremain(uLong sz, int dv) {
15   int r;
16 
17   r = sz / dv;
18   r++;
19   r = r * dv;
20   r = r - sz;
21   return(r);
22 }
23 
padInput(char ** input,uLong sz)24 uLong padInput(char **input, uLong sz) {
25   int r, j;
26 
27   j = sizeof(uInt32) * 2;
28 
29   if (sz >= j)
30     r = getremain(sz, j);
31   else
32     r = j - sz;
33 
34   if ( r < j) {
35     if ((*input = realloc(*input, sz + r + 1)) == NULL)
36       memerror();
37 
38     memset(*input+sz, 0, r + 1);
39     sz+=r;
40   }
41 
42   return(sz);
43 }
44 
attachKey(char ** input,char * key,uLong sz)45 uLong attachKey(char **input, char *key, uLong sz) {
46 
47   /* +3 so we have room for info tags at the beginning of the file */
48   if ((*input = realloc(*input, sz + MAXKEYBYTES + 3)) == NULL)
49     memerror();
50 
51   memcpy(*input+sz, key, MAXKEYBYTES);
52   sz += MAXKEYBYTES;
53 
54   return(sz);
55 }
56 
readfile(char * infile,char ** input,int type,char * key,struct stat statbuf)57 uLong readfile(char *infile, char **input, int type, char *key,
58 	struct stat statbuf) {
59   FILE *fd;
60   int readsize;
61   uLong sz = 0;
62 
63   readsize = statbuf.st_size + 1;
64 
65   fd = fopen(infile, "rb");
66   if (!fd) {
67     fprintf(stderr, "Unable to open file %s\n", infile);
68     return(-1);
69   }
70 
71   if ((*input = malloc(readsize + sz + 1)) == NULL)
72     memerror();
73 
74   memset(*input+sz, 0, readsize);
75   sz += fread(*input+sz, 1, readsize - 1, fd);
76 
77   fclose(fd);
78 
79   return(sz);
80 }
81 
writefile(char * outfile,char * output,uLong sz,BCoptions options,struct stat statbuf)82 uLong writefile(char *outfile, char *output, uLong sz,
83 	BCoptions options, struct stat statbuf) {
84   FILE *fd;
85 
86   if (options.standardout == 1)
87     fd = stdout;
88   else
89     fd = fopen(outfile, "wb");
90 
91   if (!fd) {
92     fprintf(stderr, "Unable to create file %s\n", outfile);
93     exit(1);
94   }
95 
96   if (fwrite(output, 1, sz, fd) != sz) {
97     fprintf(stderr, "Out of space while writing file %s\n", outfile);
98     exit(1);
99   }
100 
101   if (!options.standardout) {
102     fclose(fd);
103     chmod(outfile, statbuf.st_mode);
104   }
105 
106   return(0);
107 }
108 
deletefile(char * file,BCoptions options,char * key,struct stat statbuf)109 int deletefile(char *file, BCoptions options, char *key, struct stat statbuf) {
110   int lsize;
111   long g;
112   uLong j = 0, k = 0;
113   signed char i;
114   char *state, *garbage;
115   FILE *fd;
116 
117   if (options.securedelete > 0) {
118     lsize = sizeof(long);
119     k = (statbuf.st_size / lsize) + 1;
120     if ((state = malloc(257)) == NULL)
121       memerror();
122 
123     initstate((unsigned long) key, state, 256);
124     if ((garbage = malloc(lsize + 1)) == NULL)
125       memerror();
126 
127     fd = fopen(file, "r+b");
128 
129 
130     for (i = options.securedelete; i > 0; i--) {
131       fseek(fd, 0, SEEK_SET);
132 
133       for (j = 0; j < k; j += lsize) {
134         g = random();
135         memcpy(garbage, &g, lsize);
136         fwrite(garbage, lsize, 1, fd);
137       }
138       fflush(fd);
139     }
140     fclose(fd);
141   }
142 
143   if (unlink(file)) {
144     fprintf(stderr, "Error deleting file %s\n", file);
145     return(1);
146   }
147   return(0);
148 }
149