comparison libtomcrypt/src/mac/poly1305/poly1305_file.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
10 /* The implementation is based on:
11 * Public Domain poly1305 from Andrew Moon
12 * https://github.com/floodyberry/poly1305-donna
13 */
14
15 #include "tomcrypt.h"
16
17 #ifdef LTC_POLY1305
18
19 /**
20 POLY1305 a file
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
27 */
28 int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
29 {
30 #ifdef LTC_NO_FILE
31 return CRYPT_NOP;
32 #else
33 poly1305_state st;
34 FILE *in;
35 unsigned char *buf;
36 size_t x;
37 int err;
38
39 LTC_ARGCHK(fname != NULL);
40 LTC_ARGCHK(key != NULL);
41 LTC_ARGCHK(mac != NULL);
42 LTC_ARGCHK(maclen != NULL);
43
44 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
45 return CRYPT_MEM;
46 }
47
48 if ((err = poly1305_init(&st, key, keylen)) != CRYPT_OK) {
49 goto LBL_ERR;
50 }
51
52 in = fopen(fname, "rb");
53 if (in == NULL) {
54 err = CRYPT_FILE_NOTFOUND;
55 goto LBL_ERR;
56 }
57
58 do {
59 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
60 if ((err = poly1305_process(&st, buf, (unsigned long)x)) != CRYPT_OK) {
61 fclose(in);
62 goto LBL_CLEANBUF;
63 }
64 } while (x == LTC_FILE_READ_BUFSIZE);
65
66 if (fclose(in) != 0) {
67 err = CRYPT_ERROR;
68 goto LBL_CLEANBUF;
69 }
70
71 err = poly1305_done(&st, mac, maclen);
72
73 LBL_CLEANBUF:
74 zeromem(buf, LTC_FILE_READ_BUFSIZE);
75 LBL_ERR:
76 #ifdef LTC_CLEAN_STACK
77 zeromem(&st, sizeof(poly1305_state));
78 #endif
79 XFREE(buf);
80 return err;
81 #endif
82 }
83
84 #endif
85
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */