]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/pmac/pmac_file.c
remove rmd hashes
[zpackage] / libtomcrypt / src / mac / pmac / pmac_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 pmac_file.c
13    PMAC implementation, process a file, by Tom St Denis
14 */
15
16 #ifdef LTC_PMAC
17
18 /**
19    PMAC 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 filename     The name of the file to send through PMAC
24    @param out          [out] Destination for the authentication tag
25    @param outlen       [in/out] 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 pmac_file(int cipher,
29               const unsigned char *key, unsigned long keylen,
30               const char *filename,
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(filename);
38    LTC_UNUSED_PARAM(out);
39    LTC_UNUSED_PARAM(outlen);
40    return CRYPT_NOP;
41 #else
42    size_t x;
43    int err;
44    pmac_state pmac;
45    FILE *in;
46    unsigned char *buf;
47
48
49    LTC_ARGCHK(key      != NULL);
50    LTC_ARGCHK(filename != NULL);
51    LTC_ARGCHK(out      != NULL);
52    LTC_ARGCHK(outlen   != NULL);
53
54    if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
55       return CRYPT_MEM;
56    }
57
58    if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
59       goto LBL_ERR;
60    }
61
62    in = fopen(filename, "rb");
63    if (in == NULL) {
64       err = CRYPT_FILE_NOTFOUND;
65       goto LBL_ERR;
66    }
67
68    do {
69       x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
70       if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) {
71          fclose(in);
72          goto LBL_CLEANBUF;
73       }
74    } while (x == LTC_FILE_READ_BUFSIZE);
75
76    if (fclose(in) != 0) {
77       err = CRYPT_ERROR;
78       goto LBL_CLEANBUF;
79    }
80
81    err = pmac_done(&pmac, out, outlen);
82
83 LBL_CLEANBUF:
84    zeromem(buf, LTC_FILE_READ_BUFSIZE);
85 LBL_ERR:
86 #ifdef LTC_CLEAN_STACK
87    zeromem(&pmac, sizeof(pmac_state));
88 #endif
89    XFREE(buf);
90    return err;
91 #endif
92 }
93
94 #endif
95
96 /* ref:         $Format:%D$ */
97 /* git commit:  $Format:%H$ */
98 /* commit time: $Format:%ai$ */