rev |
line source |
meillo@0
|
1 /*
|
meillo@0
|
2 ** Function: hmac_md5
|
meillo@0
|
3 */
|
meillo@0
|
4
|
meillo@0
|
5 #include <string.h>
|
meillo@0
|
6 #include "md5.h"
|
meillo@0
|
7 #include "hmac_md5.h"
|
meillo@0
|
8
|
meillo@10
|
9 void
|
meillo@10
|
10 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest)
|
meillo@10
|
11 /* text; pointer to data stream */
|
meillo@10
|
12 /* text_len; length of data stream */
|
meillo@10
|
13 /* key; pointer to authentication key */
|
meillo@10
|
14 /* key_len; length of authentication key */
|
meillo@10
|
15 /* digest; caller digest to be filled in */
|
meillo@10
|
16 {
|
meillo@10
|
17 MD5_CTX context;
|
meillo@10
|
18 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
|
meillo@10
|
19 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
|
meillo@10
|
20 unsigned char tk[16];
|
meillo@10
|
21 int i;
|
meillo@10
|
22 /* if key is longer than 64 bytes reset it to key=MD5(key) */
|
meillo@10
|
23 if (key_len > 64) {
|
meillo@0
|
24
|
meillo@10
|
25 MD5_CTX tctx;
|
meillo@0
|
26
|
meillo@209
|
27 MD5_Init(&tctx);
|
meillo@209
|
28 MD5_Update(&tctx, key, key_len);
|
meillo@209
|
29 MD5_Final(tk, &tctx);
|
meillo@0
|
30
|
meillo@10
|
31 key = tk;
|
meillo@10
|
32 key_len = 16;
|
meillo@10
|
33 }
|
meillo@0
|
34
|
meillo@10
|
35 /*
|
meillo@10
|
36 * the HMAC_MD5 transform looks like:
|
meillo@10
|
37 *
|
meillo@10
|
38 * MD5(K XOR opad, MD5(K XOR ipad, text))
|
meillo@10
|
39 *
|
meillo@10
|
40 * where K is an n byte key
|
meillo@10
|
41 * ipad is the byte 0x36 repeated 64 times
|
meillo@10
|
42 * opad is the byte 0x5c repeated 64 times
|
meillo@10
|
43 * and text is the data being protected
|
meillo@10
|
44 */
|
meillo@0
|
45
|
meillo@10
|
46 /* start out by storing key in pads */
|
meillo@10
|
47 bzero(k_ipad, sizeof k_ipad);
|
meillo@10
|
48 bzero(k_opad, sizeof k_opad);
|
meillo@10
|
49 bcopy(key, k_ipad, key_len);
|
meillo@10
|
50 bcopy(key, k_opad, key_len);
|
meillo@0
|
51
|
meillo@10
|
52 /* XOR key with ipad and opad values */
|
meillo@10
|
53 for (i = 0; i < 64; i++) {
|
meillo@10
|
54 k_ipad[i] ^= 0x36;
|
meillo@10
|
55 k_opad[i] ^= 0x5c;
|
meillo@10
|
56 }
|
meillo@10
|
57 /*
|
meillo@10
|
58 * perform inner MD5
|
meillo@10
|
59 */
|
meillo@209
|
60 MD5_Init(&context); /* init context for 1st pass */
|
meillo@209
|
61 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
|
meillo@209
|
62 MD5_Update(&context, text, text_len); /* then text of datagram */
|
meillo@209
|
63 MD5_Final(digest, &context); /* finish up 1st pass */
|
meillo@10
|
64 /*
|
meillo@10
|
65 * perform outer MD5
|
meillo@10
|
66 */
|
meillo@209
|
67 MD5_Init(&context); /* init context for 2nd pass */
|
meillo@209
|
68 MD5_Update(&context, k_opad, 64); /* start with outer pad */
|
meillo@209
|
69 MD5_Update(&context, digest, 16); /* then results of 1st hash */
|
meillo@209
|
70 MD5_Final(digest, &context); /* finish up 2nd pass */
|
meillo@0
|
71 }
|