]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/f9/f9_file.c
04d509bfc9ad273b0239711befee5c892dcb3e33
[zpackage] / libtomcrypt / src / mac / f9 / f9_file.c
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 #include "tomcrypt.h"
10
11 /**
12   @file f9_file.c
13   f9 support, process a file, Tom St Denis
14 */
15
16 #ifdef LTC_F9_MODE
17
18 /**
19    f9 a file
20    @param cipher   The index of the cipher desired
21    @param key      The secret key
22    @param keylen   The length of the secret key (octets)
23    @param fname    The name of the file you wish to f9
24    @param out      [out] Where the authentication tag is to be stored
25    @param outlen   [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 f9_file(int cipher,
29               const unsigned char *key, unsigned long keylen,
30               const char *fname,
31                     unsigned char *out, unsigned long *outlen)
32 {
33 #ifdef LTC_NO_FILE
34    LTC_UNUSED_PARAM(cipher);
35    LTC_UNUSED_PARAM(key);
36    LTC_UNUSED_PARAM(keylen);
37    LTC_UNUSED_PARAM(fname);
38    LTC_UNUSED_PARAM(out);
39    LTC_UNUSED_PARAM(outlen);
40    return CRYPT_NOP;
41 #else
42    size_t x;
43    int err;
44    f9_state f9;
45    FILE *in;
46    unsigned char *buf;
47
48    LTC_ARGCHK(key    != NULL);
49    LTC_ARGCHK(fname  != NULL);
50    LTC_ARGCHK(out    != NULL);
51    LTC_ARGCHK(outlen != NULL);
52
53    if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
54       return CRYPT_MEM;
55    }
56
57    if ((err = f9_init(&f9, cipher, key, keylen)) != CRYPT_OK) {
58       goto LBL_ERR;
59    }
60
61    in = fopen(fname, "rb");
62    if (in == NULL) {
63       err = CRYPT_FILE_NOTFOUND;
64       goto LBL_ERR;
65    }
66
67    do {
68       x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
69       if ((err = f9_process(&f9, buf, (unsigned long)x)) != CRYPT_OK) {
70          fclose(in);
71          goto LBL_CLEANBUF;
72       }
73    } while (x == LTC_FILE_READ_BUFSIZE);
74
75    if (fclose(in) != 0) {
76       err = CRYPT_ERROR;
77       goto LBL_CLEANBUF;
78    }
79
80    err = f9_done(&f9, out, outlen);
81
82 LBL_CLEANBUF:
83    zeromem(buf, LTC_FILE_READ_BUFSIZE);
84 LBL_ERR:
85 #ifdef LTC_CLEAN_STACK
86    zeromem(&f9, sizeof(f9_state));
87 #endif
88    XFREE(buf);
89    return err;
90 #endif
91 }
92
93 #endif
94
95 /* ref:         $Format:%D$ */
96 /* git commit:  $Format:%H$ */
97 /* commit time: $Format:%ai$ */