Skip to content

Commit

Permalink
Merge pull request #184 from libtom/pr/file-related-tests
Browse files Browse the repository at this point in the history
Facelift of *_file functions
  • Loading branch information
karel-m committed Apr 21, 2017
2 parents 513fcf3 + dae70f9 commit 65ace1a
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 109 deletions.
1 change: 1 addition & 0 deletions demos/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static const struct {
LTC_TEST_FN(ecc_tests),
LTC_TEST_FN(dsa_test),
LTC_TEST_FN(katja_test),
LTC_TEST_FN(file_test),
};

int main(int argc, char **argv)
Expand Down
26 changes: 16 additions & 10 deletions src/hashes/helper/hash_filehandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,44 @@
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;

LTC_ARGCHK(out != NULL);
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 */
Expand Down
51 changes: 30 additions & 21 deletions src/mac/f9/f9_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

err = f9_done(&f9, out, outlen);

LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
zeromem(&f9, sizeof(f9_state));
#endif

return CRYPT_OK;
XFREE(buf);
return err;
#endif
}

Expand Down
43 changes: 23 additions & 20 deletions src/mac/hmac/hmac_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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$ */
39 changes: 24 additions & 15 deletions src/mac/omac/omac_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

err = omac_done(&omac, out, outlen);

LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
zeromem(&omac, sizeof(omac_state));
#endif

return CRYPT_OK;
XFREE(buf);
return err;
#endif
}

Expand Down
39 changes: 24 additions & 15 deletions src/mac/pmac/pmac_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,51 @@ int pmac_file(int cipher,
int err;
pmac_state pmac;
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 = 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;
}

err = pmac_done(&pmac, out, outlen);

LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(buf, sizeof(buf));
zeromem(&pmac, sizeof(pmac_state));
#endif

return CRYPT_OK;
XFREE(buf);
return err;
#endif
}

Expand Down

0 comments on commit 65ace1a

Please sign in to comment.