3
|
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 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 /* OMAC1 Support by Tom St Denis (for 64 and 128 bit block ciphers only) */ |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef OMAC |
|
15 |
|
16 int omac_file(int cipher, |
|
17 const unsigned char *key, unsigned long keylen, |
|
18 const char *filename, |
|
19 unsigned char *out, unsigned long *outlen) |
|
20 { |
|
21 #ifdef NO_FILE |
|
22 return CRYPT_NOP; |
|
23 #else |
|
24 int err, x; |
|
25 omac_state omac; |
|
26 FILE *in; |
|
27 unsigned char buf[512]; |
|
28 |
|
29 _ARGCHK(key != NULL); |
|
30 _ARGCHK(filename != NULL); |
|
31 _ARGCHK(out != NULL); |
|
32 _ARGCHK(outlen != NULL); |
|
33 |
|
34 in = fopen(filename, "rb"); |
|
35 if (in == NULL) { |
|
36 return CRYPT_FILE_NOTFOUND; |
|
37 } |
|
38 |
|
39 if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) { |
|
40 fclose(in); |
|
41 return err; |
|
42 } |
|
43 |
|
44 do { |
|
45 x = fread(buf, 1, sizeof(buf), in); |
|
46 if ((err = omac_process(&omac, buf, x)) != CRYPT_OK) { |
|
47 fclose(in); |
|
48 return err; |
|
49 } |
|
50 } while (x == sizeof(buf)); |
|
51 fclose(in); |
|
52 |
|
53 if ((err = omac_done(&omac, out, outlen)) != CRYPT_OK) { |
|
54 return err; |
|
55 } |
|
56 |
|
57 #ifdef CLEAN_STACK |
|
58 zeromem(buf, sizeof(buf)); |
|
59 #endif |
|
60 |
|
61 return CRYPT_OK; |
|
62 #endif |
|
63 } |
|
64 |
|
65 #endif |