Mercurial > dropbear
comparison hmac_file.c @ 0:d7da3b1e1540 libtomcrypt
put back the 0.95 makefile which was inadvertently merged over
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:21:40 +0000 |
parents | |
children | 5d99163f7e32 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7da3b1e1540 |
---|---|
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 /* Submited by Dobes Vandermeer ([email protected]) */ | |
12 | |
13 #include "mycrypt.h" | |
14 | |
15 /* | |
16 (1) append zeros to the end of K to create a B byte string | |
17 (e.g., if K is of length 20 bytes and B=64, then K will be | |
18 appended with 44 zero bytes 0x00) | |
19 (2) XOR (bitwise exclusive-OR) the B byte string computed in step | |
20 (1) with ipad (ipad = the byte 0x36 repeated B times) | |
21 (3) append the stream of data 'text' to the B byte string resulting | |
22 from step (2) | |
23 (4) apply H to the stream generated in step (3) | |
24 (5) XOR (bitwise exclusive-OR) the B byte string computed in | |
25 step (1) with opad (opad = the byte 0x5C repeated B times.) | |
26 (6) append the H result from step (4) to the B byte string | |
27 resulting from step (5) | |
28 (7) apply H to the stream generated in step (6) and output | |
29 the result | |
30 */ | |
31 | |
32 #ifdef HMAC | |
33 | |
34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize | |
35 | |
36 /* hmac_file added by Tom St Denis */ | |
37 int hmac_file(int hash, const char *fname, | |
38 const unsigned char *key, unsigned long keylen, | |
39 unsigned char *dst, unsigned long *dstlen) | |
40 { | |
41 #ifdef NO_FILE | |
42 return CRYPT_NOP; | |
43 #else | |
44 hmac_state hmac; | |
45 FILE *in; | |
46 unsigned char buf[512]; | |
47 size_t x; | |
48 int err; | |
49 | |
50 _ARGCHK(fname != NULL); | |
51 _ARGCHK(key != NULL); | |
52 _ARGCHK(dst != NULL); | |
53 _ARGCHK(dstlen != NULL); | |
54 | |
55 if((err = hash_is_valid(hash)) != CRYPT_OK) { | |
56 return err; | |
57 } | |
58 | |
59 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { | |
60 return err; | |
61 } | |
62 | |
63 in = fopen(fname, "rb"); | |
64 if (in == NULL) { | |
65 return CRYPT_FILE_NOTFOUND; | |
66 } | |
67 | |
68 /* process the file contents */ | |
69 do { | |
70 x = fread(buf, 1, sizeof(buf), in); | |
71 if ((err = hmac_process(&hmac, buf, (unsigned long)x)) != CRYPT_OK) { | |
72 /* we don't trap this error since we're already returning an error! */ | |
73 fclose(in); | |
74 return err; | |
75 } | |
76 } while (x == sizeof(buf)); | |
77 | |
78 if (fclose(in) != 0) { | |
79 return CRYPT_ERROR; | |
80 } | |
81 | |
82 /* get final hmac */ | |
83 if ((err = hmac_done(&hmac, dst, dstlen)) != CRYPT_OK) { | |
84 return err; | |
85 } | |
86 | |
87 #ifdef CLEAN_STACK | |
88 /* clear memory */ | |
89 zeromem(buf, sizeof(buf)); | |
90 #endif | |
91 return CRYPT_OK; | |
92 #endif | |
93 } | |
94 | |
95 #endif | |
96 |