@@ -76,8 +76,15 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
7676
7777 /* open the file to write */
7878 tempInFile = XFOPEN (in , "wb" );
79- XFWRITE (userInputBuffer , 1 , inputLength , tempInFile );
80- XFCLOSE (tempInFile );
79+ if (tempInFile == NULL ) {
80+ wolfCLU_LogError ("unable to open file %s" , in );
81+ XFREE (userInputBuffer , HEAP_HINT , DYNAMIC_TYPE_TMP_BUFFER );
82+ return BAD_FUNC_ARG ;
83+ }
84+ else {
85+ XFWRITE (userInputBuffer , 1 , inputLength , tempInFile );
86+ XFCLOSE (tempInFile );
87+ }
8188
8289 /* free buffer */
8390 XFREE (userInputBuffer , HEAP_HINT , DYNAMIC_TYPE_TMP_BUFFER );
@@ -121,13 +128,15 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
121128 ret = wc_RNG_GenerateBlock (& rng , iv , block );
122129
123130 if (ret != 0 ) {
131+ XFCLOSE (inFile );
124132 return ret ;
125133 }
126134
127135 /* stretches pwdKey to fit size based on wolfCLU_getAlgo() */
128136 ret = wolfCLU_genKey_PWDBASED (& rng , pwdKey , size , salt , padCounter );
129137 if (ret != WOLFCLU_SUCCESS ) {
130138 wolfCLU_LogError ("failed to set pwdKey." );
139+ XFCLOSE (inFile );
131140 return ret ;
132141 }
133142 /* move the generated pwdKey to "key" for encrypting */
@@ -140,6 +149,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
140149 outFile = XFOPEN (out , "wb" );
141150 if (outFile == NULL ) {
142151 wolfCLU_LogError ("unable to open output file %s" , out );
152+ XFCLOSE (inFile );
143153 return WOLFCLU_FATAL_ERROR ;
144154 }
145155 XFWRITE (salt , 1 , SALT_SIZE , outFile );
@@ -148,21 +158,29 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
148158
149159 /* MALLOC 1kB buffers */
150160 input = (byte * ) XMALLOC (MAX_LEN , HEAP_HINT , DYNAMIC_TYPE_TMP_BUFFER );
151- if (input == NULL )
161+ if (input == NULL ) {
162+ XFCLOSE (inFile );
152163 return MEMORY_E ;
164+ }
153165 output = (byte * ) XMALLOC (MAX_LEN , HEAP_HINT , DYNAMIC_TYPE_TMP_BUFFER );
154166 if (output == NULL ) {
167+ XFCLOSE (inFile );
155168 wolfCLU_freeBins (input , NULL , NULL , NULL , NULL );
156169 return MEMORY_E ;
157170 }
158171
159172 /* loop, encrypt 1kB at a time till length <= 0 */
160173 while (length > 0 ) {
161174 /* Read in 1kB to input[] */
162- if (inputHex == 1 )
163- ret = (int ) fread (inputString , 1 , MAX_LEN , inFile );
164- else
165- ret = (int ) fread (input , 1 , MAX_LEN , inFile );
175+ if (feof (inFile )) {
176+ ret = 0 ;
177+ }
178+ else {
179+ if (inputHex == 1 )
180+ ret = (int ) fread (inputString , 1 , MAX_LEN , inFile );
181+ else
182+ ret = (int ) fread (input , 1 , MAX_LEN , inFile );
183+ }
166184
167185 if (ret != MAX_LEN ) {
168186 /* check for end of file */
@@ -178,6 +196,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
178196 if (hexRet != WOLFCLU_SUCCESS ) {
179197 wolfCLU_LogError ("failed during conversion of input,"
180198 " ret = %d" , hexRet );
199+ XFCLOSE (inFile );
181200 return hexRet ;
182201 }
183202 }/* end hex or ascii */
@@ -191,6 +210,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
191210 }
192211 else { /* otherwise we got a file read error */
193212 wolfCLU_freeBins (input , output , NULL , NULL , NULL );
213+ XFCLOSE (inFile );
194214 return FREAD_ERROR ;
195215 }/* End feof check */
196216 }/* End fread check */
@@ -200,6 +220,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
200220 alg == WOLFCLU_CAMELLIA256CBC ) {
201221 ret = wc_CamelliaSetKey (& camellia , key , block , iv );
202222 if (ret != 0 ) {
223+ XFCLOSE (inFile );
203224 wolfCLU_LogError ("CamelliaSetKey failed." );
204225 wolfCLU_freeBins (input , output , NULL , NULL , NULL );
205226 return ret ;
@@ -208,6 +229,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
208229 wc_CamelliaCbcEncrypt (& camellia , output , input , tempMax );
209230 }
210231 else {
232+ XFCLOSE (inFile );
211233 wolfCLU_LogError ("Incompatible mode while using Camellia." );
212234 wolfCLU_freeBins (input , output , NULL , NULL , NULL );
213235 return FATAL_ERROR ;
@@ -233,14 +255,25 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
233255
234256 /* Open the outFile in append mode */
235257 outFile = XFOPEN (out , "ab" );
258+ if (outFile == NULL ) {
259+ XFCLOSE (inFile );
260+ wolfCLU_LogError ("failed to open file." );
261+ wolfCLU_freeBins (input , output , NULL , NULL , NULL );
262+ return FWRITE_ERROR ;
263+ }
264+
236265 ret = (int )XFWRITE (output , 1 , tempMax , outFile );
237266
238267 if (ferror (outFile )) {
268+ XFCLOSE (outFile );
269+ XFCLOSE (inFile );
239270 wolfCLU_LogError ("failed to write to file." );
240271 wolfCLU_freeBins (input , output , NULL , NULL , NULL );
241272 return FWRITE_ERROR ;
242273 }
243274 if (ret > MAX_LEN ) {
275+ XFCLOSE (outFile );
276+ XFCLOSE (inFile );
244277 wolfCLU_LogError ("Wrote too much to file." );
245278 wolfCLU_freeBins (input , output , NULL , NULL , NULL );
246279 return FWRITE_ERROR ;
0 commit comments