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

View File

@ -27,7 +27,7 @@
int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
{
hash_state md;
unsigned char buf[512];
unsigned char *buf;
size_t x;
int err;
@ -35,30 +35,36 @@ int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outle
LTC_ARGCHK(outlen != NULL);
LTC_ARGCHK(in != NULL);
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
return err;
goto LBL_ERR;
}
if (*outlen < hash_descriptor[hash].hashsize) {
*outlen = hash_descriptor[hash].hashsize;
return CRYPT_BUFFER_OVERFLOW;
err = CRYPT_BUFFER_OVERFLOW;
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
return err;
goto LBL_ERR;
}
*outlen = hash_descriptor[hash].hashsize;
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
return err;
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
} while (x == LTC_FILE_READ_BUFSIZE);
err = hash_descriptor[hash].done(&md, out);
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
#endif
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
XFREE(buf);
return err;
}
#endif /* #ifndef LTC_NO_FILE */

View File

@ -22,14 +22,14 @@
@param cipher The index of the cipher desired
@param key The secret key
@param keylen The length of the secret key (octets)
@param filename The name of the file you wish to f9
@param fname The name of the file you wish to f9
@param out [out] Where the authentication tag is to be stored
@param outlen [in/out] The max size and resulting size of the authentication tag
@return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
*/
int f9_file(int cipher,
const unsigned char *key, unsigned long keylen,
const char *filename,
const char *fname,
unsigned char *out, unsigned long *outlen)
{
#ifdef LTC_NO_FILE
@ -39,41 +39,50 @@ int f9_file(int cipher,
int err;
f9_state f9;
FILE *in;
unsigned char buf[512];
unsigned char *buf;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(filename != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(fname != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
in = fopen(filename, "rb");
if (in == NULL) {
return CRYPT_FILE_NOTFOUND;
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_ERR;
}
in = fopen(fname, "rb");
if (in == NULL) {
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = f9_process(&f9, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
fclose(in);
} while (x == LTC_FILE_READ_BUFSIZE);
if ((err = f9_done(&f9, out, outlen)) != CRYPT_OK) {
return err;
if (fclose(in) != 0) {
err = CRYPT_ERROR;
goto LBL_CLEANBUF;
}
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
#endif
err = f9_done(&f9, out, outlen);
return CRYPT_OK;
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(&f9, sizeof(f9_state));
#endif
XFREE(buf);
return err;
#endif
}

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$ */

View File

@ -39,41 +39,50 @@ int omac_file(int cipher,
int err;
omac_state omac;
FILE *in;
unsigned char buf[512];
unsigned char *buf;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(filename != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
in = fopen(filename, "rb");
if (in == NULL) {
return CRYPT_FILE_NOTFOUND;
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_ERR;
}
in = fopen(filename, "rb");
if (in == NULL) {
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = omac_process(&omac, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
fclose(in);
} while (x == LTC_FILE_READ_BUFSIZE);
if ((err = omac_done(&omac, out, outlen)) != CRYPT_OK) {
return err;
if (fclose(in) != 0) {
err = CRYPT_ERROR;
goto LBL_CLEANBUF;
}
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
#endif
err = omac_done(&omac, out, outlen);
return CRYPT_OK;
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(&omac, sizeof(omac_state));
#endif
XFREE(buf);
return err;
#endif
}

View File

@ -39,7 +39,7 @@ int pmac_file(int cipher,
int err;
pmac_state pmac;
FILE *in;
unsigned char buf[512];
unsigned char *buf;
LTC_ARGCHK(key != NULL);
@ -47,34 +47,43 @@ int pmac_file(int cipher,
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
in = fopen(filename, "rb");
if (in == NULL) {
return CRYPT_FILE_NOTFOUND;
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_ERR;
}
in = fopen(filename, "rb");
if (in == NULL) {
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
fclose(in);
} while (x == LTC_FILE_READ_BUFSIZE);
if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) {
return err;
if (fclose(in) != 0) {
err = CRYPT_ERROR;
goto LBL_CLEANBUF;
}
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
#endif
err = pmac_done(&pmac, out, outlen);
return CRYPT_OK;
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(&pmac, sizeof(pmac_state));
#endif
XFREE(buf);
return err;
#endif
}

View File

@ -21,8 +21,8 @@
@param fname The name of the file you wish to POLY1305
@param key The secret key
@param keylen The length of the secret key
@param out [out] The POLY1305 authentication tag
@param outlen [in/out] The max size and resulting size of the authentication tag
@param mac [out] The POLY1305 authentication tag
@param maclen [in/out] The max size and resulting size of the authentication tag
@return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
*/
int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
@ -41,23 +41,37 @@ int poly1305_file(const char *fname, const unsigned char *key, unsigned long key
LTC_ARGCHK(mac != NULL);
LTC_ARGCHK(maclen != NULL);
if ((in = fopen(fname, "rb")) == NULL) { return CRYPT_FILE_NOTFOUND; }
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { return CRYPT_MEM; }
if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) { goto LBL_ERR; }
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) {
goto LBL_ERR;
}
in = fopen(fname, "rb");
if (in == NULL) {
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
do {
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in);
goto LBL_ERR;
goto LBL_CLEANBUF;
}
} while (x == LTC_FILE_READ_BUFSIZE);
if (fclose(in) != 0) {
if (fclose(in) != 0) {
err = CRYPT_ERROR;
goto LBL_ERR;
goto LBL_CLEANBUF;
}
err = poly1305_done(&st, mac, maclen);
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(&st, sizeof(poly1305_state));

View File

@ -39,41 +39,50 @@ int xcbc_file(int cipher,
int err;
xcbc_state xcbc;
FILE *in;
unsigned char buf[512];
unsigned char *buf;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(filename != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
in = fopen(filename, "rb");
if (in == NULL) {
return CRYPT_FILE_NOTFOUND;
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = xcbc_init(&xcbc, cipher, key, keylen)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_ERR;
}
in = fopen(filename, "rb");
if (in == NULL) {
err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
}
do {
x = fread(buf, 1, sizeof(buf), in);
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = xcbc_process(&xcbc, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in);
return err;
goto LBL_CLEANBUF;
}
} while (x == sizeof(buf));
fclose(in);
} while (x == LTC_FILE_READ_BUFSIZE);
if ((err = xcbc_done(&xcbc, out, outlen)) != CRYPT_OK) {
return err;
if (fclose(in) != 0) {
err = CRYPT_ERROR;
goto LBL_CLEANBUF;
}
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
#endif
err = xcbc_done(&xcbc, out, outlen);
return CRYPT_OK;
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(&xcbc, sizeof(xcbc_state));
#endif
XFREE(buf);
return err;
#endif
}