]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/omac/omac_file.c
3f6a85d0c1a13c331ff3c17ecf4fab0df1864e6b
[zpackage] / libtomcrypt / src / mac / omac / omac_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 omac_file.c
13   OMAC1 support, process a file, Tom St Denis
14 */
15
16 #ifdef LTC_OMAC
17
18 /**
19    OMAC 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 you wish to OMAC
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 omac_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    omac_state omac;
45    FILE *in;
46    unsigned char *buf;
47
48    LTC_ARGCHK(key      != NULL);
49    LTC_ARGCHK(filename != 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 = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) {
58       goto LBL_ERR;
59    }
60
61    in = fopen(filename, "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 = omac_process(&omac, 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 = omac_done(&omac, out, outlen);
81
82 LBL_CLEANBUF:
83    zeromem(buf, LTC_FILE_READ_BUFSIZE);
84 LBL_ERR:
85 #ifdef LTC_CLEAN_STACK
86    zeromem(&omac, sizeof(omac_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$ */