Facelift of *_file functions

This commit is contained in:
Karel Miko
2017-04-21 16:11:22 +02:00
parent 513fcf35fe
commit 336c52ca5f
7 changed files with 167 additions and 108 deletions
+23 -20
View File
@@ -36,7 +36,7 @@ int hmac_file(int hash, const char *fname,
#else
hmac_state hmac;
FILE *in;
unsigned char buf[512];
unsigned char *buf;
size_t x;
int err;
@@ -45,49 +45,52 @@ int hmac_file(int hash, const char *fname,
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
if((err = hash_is_valid(hash)) != CRYPT_OK) {
return err;
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
goto LBL_ERR;
}
if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
return err;
goto LBL_ERR;
}
in = fopen(fname, "rb");
if (in == NULL) {
return CRYPT_FILE_NOTFOUND;
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
/* process the file contents */
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) {
/* we don't trap this error since we're already returning an error! */
fclose(in);
return err;
fclose(in); /* we don't trap this error since we're already returning an error! */
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
} while (x == LTC_FILE_READ_BUFSIZE);
if (fclose(in) != 0) {
return CRYPT_ERROR;
err = CRYPT_ERROR;
goto LBL_CLEANBUF;
}
/* get final hmac */
if ((err = hmac_done(&hmac, out, outlen)) != CRYPT_OK) {
return err;
}
err = hmac_done(&hmac, out, outlen);
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
/* clear memory */
zeromem(buf, sizeof(buf));
zeromem(&hmac, sizeof(hmac_state));
#endif
return CRYPT_OK;
XFREE(buf);
return err;
#endif
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */