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) int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen)
{ {
hash_state md; hash_state md;
unsigned char buf[512]; unsigned char *buf;
size_t x; size_t x;
int err; 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(outlen != NULL);
LTC_ARGCHK(in != NULL); LTC_ARGCHK(in != NULL);
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return CRYPT_MEM;
}
if ((err = hash_is_valid(hash)) != CRYPT_OK) { if ((err = hash_is_valid(hash)) != CRYPT_OK) {
return err; goto LBL_ERR;
} }
if (*outlen < hash_descriptor[hash].hashsize) { if (*outlen < hash_descriptor[hash].hashsize) {
*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) { if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
return err; goto LBL_ERR;
} }
*outlen = hash_descriptor[hash].hashsize; *outlen = hash_descriptor[hash].hashsize;
do { 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) { 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); err = hash_descriptor[hash].done(&md, out);
#ifdef LTC_CLEAN_STACK LBL_CLEANBUF:
zeromem(buf, sizeof(buf)); zeromem(buf, LTC_FILE_READ_BUFSIZE);
#endif LBL_ERR:
XFREE(buf);
return err; return err;
} }
#endif /* #ifndef LTC_NO_FILE */ #endif /* #ifndef LTC_NO_FILE */

View File

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

View File

@ -36,7 +36,7 @@ int hmac_file(int hash, const char *fname,
#else #else
hmac_state hmac; hmac_state hmac;
FILE *in; FILE *in;
unsigned char buf[512]; unsigned char *buf;
size_t x; size_t x;
int err; int err;
@ -45,49 +45,52 @@ int hmac_file(int hash, const char *fname,
LTC_ARGCHK(out != NULL); LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(outlen != NULL);
if((err = hash_is_valid(hash)) != CRYPT_OK) { if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
return err; return CRYPT_MEM;
}
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
goto LBL_ERR;
} }
if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) {
return err; goto LBL_ERR;
} }
in = fopen(fname, "rb"); in = fopen(fname, "rb");
if (in == NULL) { if (in == NULL) {
return CRYPT_FILE_NOTFOUND; err = CRYPT_FILE_NOTFOUND;
goto LBL_ERR;
} }
/* process the file contents */
do { 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) { 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); /* we don't trap this error since we're already returning an error! */
fclose(in); goto LBL_CLEANBUF;
return err;
} }
} while (x == sizeof(buf)); } while (x == LTC_FILE_READ_BUFSIZE);
if (fclose(in) != 0) { if (fclose(in) != 0) {
return CRYPT_ERROR; err = CRYPT_ERROR;
goto LBL_CLEANBUF;
} }
/* get final hmac */ err = hmac_done(&hmac, out, outlen);
if ((err = hmac_done(&hmac, out, outlen)) != CRYPT_OK) {
return err;
}
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK #ifdef LTC_CLEAN_STACK
/* clear memory */ zeromem(&hmac, sizeof(hmac_state));
zeromem(buf, sizeof(buf));
#endif #endif
return CRYPT_OK; XFREE(buf);
return err;
#endif #endif
} }
#endif #endif
/* $Source$ */ /* $Source$ */
/* $Revision$ */ /* $Revision$ */
/* $Date$ */ /* $Date$ */

View File

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

View File

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

View File

@ -21,8 +21,8 @@
@param fname The name of the file you wish to POLY1305 @param fname The name of the file you wish to POLY1305
@param key The secret key @param key The secret key
@param keylen The length of the secret key @param keylen The length of the secret key
@param out [out] The POLY1305 authentication tag @param mac [out] The POLY1305 authentication tag
@param outlen [in/out] The max size and resulting size of the 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 @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) 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(mac != NULL);
LTC_ARGCHK(maclen != NULL); LTC_ARGCHK(maclen != NULL);
if ((in = fopen(fname, "rb")) == NULL) { return CRYPT_FILE_NOTFOUND; } if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { return CRYPT_MEM; } return CRYPT_MEM;
if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } }
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 { do {
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in); x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) { if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
fclose(in); fclose(in);
goto LBL_ERR; goto LBL_CLEANBUF;
} }
} while (x == LTC_FILE_READ_BUFSIZE); } while (x == LTC_FILE_READ_BUFSIZE);
if (fclose(in) != 0) {
if (fclose(in) != 0) {
err = CRYPT_ERROR; err = CRYPT_ERROR;
goto LBL_ERR; goto LBL_CLEANBUF;
} }
err = poly1305_done(&st, mac, maclen); err = poly1305_done(&st, mac, maclen);
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR: LBL_ERR:
#ifdef LTC_CLEAN_STACK #ifdef LTC_CLEAN_STACK
zeromem(&st, sizeof(poly1305_state)); zeromem(&st, sizeof(poly1305_state));

View File

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