1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file pmac_file.c
14 PMAC implementation, process a file, by Tom St Denis
15 */
16
17 #ifdef LTC_PMAC
18
19 /**
20 PMAC a file
21 @param cipher The index of the cipher desired
22 @param key The secret key
23 @param keylen The length of the secret key (octets)
24 @param filename The name of the file to send through PMAC
25 @param out [out] Destination for the authentication tag
26 @param outlen [in/out] Max size and resulting size of the authentication tag
27 @return CRYPT_OK if successful, CRYPT_NOP if file support has been disabled
28 */
pmac_file(int cipher,const unsigned char * key,unsigned long keylen,const char * filename,unsigned char * out,unsigned long * outlen)29 int pmac_file(int cipher,
30 const unsigned char *key, unsigned long keylen,
31 const char *filename,
32 unsigned char *out, unsigned long *outlen)
33 {
34 #ifdef LTC_NO_FILE
35 LTC_UNUSED_PARAM(cipher);
36 LTC_UNUSED_PARAM(key);
37 LTC_UNUSED_PARAM(keylen);
38 LTC_UNUSED_PARAM(filename);
39 LTC_UNUSED_PARAM(out);
40 LTC_UNUSED_PARAM(outlen);
41 return CRYPT_NOP;
42 #else
43 size_t x;
44 int err;
45 pmac_state pmac;
46 FILE *in;
47 unsigned char *buf;
48
49
50 LTC_ARGCHK(key != NULL);
51 LTC_ARGCHK(filename != NULL);
52 LTC_ARGCHK(out != NULL);
53 LTC_ARGCHK(outlen != NULL);
54
55 if ((buf = XMALLOC(LTC_FILE_READ_BUFSIZE)) == NULL) {
56 return CRYPT_MEM;
57 }
58
59 if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) {
60 goto LBL_ERR;
61 }
62
63 in = fopen(filename, "rb");
64 if (in == NULL) {
65 err = CRYPT_FILE_NOTFOUND;
66 goto LBL_ERR;
67 }
68
69 do {
70 x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
71 if ((err = pmac_process(&pmac, buf, (unsigned long)x)) != CRYPT_OK) {
72 fclose(in);
73 goto LBL_CLEANBUF;
74 }
75 } while (x == LTC_FILE_READ_BUFSIZE);
76
77 if (fclose(in) != 0) {
78 err = CRYPT_ERROR;
79 goto LBL_CLEANBUF;
80 }
81
82 err = pmac_done(&pmac, out, outlen);
83
84 LBL_CLEANBUF:
85 zeromem(buf, LTC_FILE_READ_BUFSIZE);
86 LBL_ERR:
87 #ifdef LTC_CLEAN_STACK
88 zeromem(&pmac, sizeof(pmac_state));
89 #endif
90 XFREE(buf);
91 return err;
92 #endif
93 }
94
95 #endif
96
97 /* ref: $Format:%D$ */
98 /* git commit: $Format:%H$ */
99 /* commit time: $Format:%ai$ */
100