diff --git a/src/hashes/helper/hash_filehandle.c b/src/hashes/helper/hash_filehandle.c index 41832fd..e1d037e 100644 --- a/src/hashes/helper/hash_filehandle.c +++ b/src/hashes/helper/hash_filehandle.c @@ -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 */ diff --git a/src/mac/f9/f9_file.c b/src/mac/f9/f9_file.c index 5d885fb..c99d7a3 100644 --- a/src/mac/f9/f9_file.c +++ b/src/mac/f9/f9_file.c @@ -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 } diff --git a/src/mac/hmac/hmac_file.c b/src/mac/hmac/hmac_file.c index 8558c2a..f74505c 100644 --- a/src/mac/hmac/hmac_file.c +++ b/src/mac/hmac/hmac_file.c @@ -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$ */ diff --git a/src/mac/omac/omac_file.c b/src/mac/omac/omac_file.c index bbff793..51c67b7 100644 --- a/src/mac/omac/omac_file.c +++ b/src/mac/omac/omac_file.c @@ -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 } diff --git a/src/mac/pmac/pmac_file.c b/src/mac/pmac/pmac_file.c index b484400..c7d9877 100644 --- a/src/mac/pmac/pmac_file.c +++ b/src/mac/pmac/pmac_file.c @@ -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 } diff --git a/src/mac/poly1305/poly1305_file.c b/src/mac/poly1305/poly1305_file.c index 1c3e61e..42afdc3 100644 --- a/src/mac/poly1305/poly1305_file.c +++ b/src/mac/poly1305/poly1305_file.c @@ -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)); diff --git a/src/mac/xcbc/xcbc_file.c b/src/mac/xcbc/xcbc_file.c index bba0d48..c8119f9 100644 --- a/src/mac/xcbc/xcbc_file.c +++ b/src/mac/xcbc/xcbc_file.c @@ -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 }