]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/blake2/blake2smac_file.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / mac / blake2 / blake2smac_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
10 #include "tomcrypt.h"
11
12 #ifdef LTC_BLAKE2SMAC
13
14 /**
15   BLAKE2S MAC a file
16   @param fname    The name of the file you wish to BLAKE2S MAC
17   @param key      The secret key
18   @param keylen   The length of the secret key
19   @param mac      [out] The BLAKE2S MAC authentication tag
20   @param maclen   [in/out]  The max size and resulting size of the authentication tag
21   @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
22 */
23 int blake2smac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen)
24 {
25 #ifdef LTC_NO_FILE
26    LTC_UNUSED_PARAM(fname);
27    LTC_UNUSED_PARAM(key);
28    LTC_UNUSED_PARAM(keylen);
29    LTC_UNUSED_PARAM(mac);
30    LTC_UNUSED_PARAM(maclen);
31    return CRYPT_NOP;
32 #else
33    blake2smac_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 = blake2smac_init(&st, *maclen, 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 = blake2smac_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 = blake2smac_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(blake2smac_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$ */