Mercurial > dropbear
comparison libtomcrypt/src/mac/blake2/blake2bmac_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 #include "tomcrypt.h" | |
11 | |
12 #ifdef LTC_BLAKE2BMAC | |
13 | |
14 /** | |
15 BLAKE2B MAC a file | |
16 @param fname The name of the file you wish to BLAKE2B MAC | |
17 @param key The secret key | |
18 @param keylen The length of the secret key | |
19 @param mac [out] The BLAKE2B 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 blake2bmac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen) | |
24 { | |
25 #ifdef LTC_NO_FILE | |
26 return CRYPT_NOP; | |
27 #else | |
28 blake2bmac_state st; | |
29 FILE *in; | |
30 unsigned char *buf; | |
31 size_t x; | |
32 int err; | |
33 | |
34 LTC_ARGCHK(fname != NULL); | |
35 LTC_ARGCHK(key != NULL); | |
36 LTC_ARGCHK(mac != NULL); | |
37 LTC_ARGCHK(maclen != NULL); | |
38 | |
39 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) { | |
40 return CRYPT_MEM; | |
41 } | |
42 | |
43 if ((err = blake2bmac_init(&st, *maclen, key, keylen)) != CRYPT_OK) { | |
44 goto LBL_ERR; | |
45 } | |
46 | |
47 in = fopen(fname, "rb"); | |
48 if (in == NULL) { | |
49 err = CRYPT_FILE_NOTFOUND; | |
50 goto LBL_ERR; | |
51 } | |
52 | |
53 do { | |
54 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in); | |
55 if ((err = blake2bmac_process(&st, buf, (unsigned long)x)) != CRYPT_OK) { | |
56 fclose(in); | |
57 goto LBL_CLEANBUF; | |
58 } | |
59 } while (x == LTC_FILE_READ_BUFSIZE); | |
60 | |
61 if (fclose(in) != 0) { | |
62 err = CRYPT_ERROR; | |
63 goto LBL_CLEANBUF; | |
64 } | |
65 | |
66 err = blake2bmac_done(&st, mac, maclen); | |
67 | |
68 LBL_CLEANBUF: | |
69 zeromem(buf, LTC_FILE_READ_BUFSIZE); | |
70 LBL_ERR: | |
71 #ifdef LTC_CLEAN_STACK | |
72 zeromem(&st, sizeof(blake2bmac_state)); | |
73 #endif | |
74 XFREE(buf); | |
75 return err; | |
76 #endif | |
77 } | |
78 | |
79 #endif | |
80 | |
81 /* ref: $Format:%D$ */ | |
82 /* git commit: $Format:%H$ */ | |
83 /* commit time: $Format:%ai$ */ |