1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* The implementation is based on:
11 * Public Domain poly1305 from Andrew Moon
12 * https://github.com/floodyberry/poly1305-donna
21 @param fname The name of the file you wish to POLY1305
22 @param key The secret key
23 @param keylen The length of the secret key
24 @param mac [out] The POLY1305 authentication tag
25 @param maclen [in/out] The max size and resulting size of the authentication tag
26 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
28 int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
31 LTC_UNUSED_PARAM(fname);
32 LTC_UNUSED_PARAM(key);
33 LTC_UNUSED_PARAM(keylen);
34 LTC_UNUSED_PARAM(mac);
35 LTC_UNUSED_PARAM(maclen);
44 LTC_ARGCHK(fname != NULL);
45 LTC_ARGCHK(key != NULL);
46 LTC_ARGCHK(mac != NULL);
47 LTC_ARGCHK(maclen != NULL);
49 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
53 if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) {
57 in = fopen(fname, "rb");
59 err = CRYPT_FILE_NOTFOUND;
64 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
65 if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
69 } while (x == LTC_FILE_READ_BUFSIZE);
71 if (fclose(in) != 0) {
76 err = poly1305_done(&st, mac, maclen);
79 zeromem(buf, LTC_FILE_READ_BUFSIZE);
81 #ifdef LTC_CLEAN_STACK
82 zeromem(&st, sizeof(poly1305_state));
91 /* ref: $Format:%D$ */
92 /* git commit: $Format:%H$ */
93 /* commit time: $Format:%ai$ */