masqmail

view src/md5/hmac_md5.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line source
1 /*
2 ** Function: hmac_md5
3 */
5 #include <string.h>
6 #include "global.h"
7 #include "md5.h"
8 #include "hmac_md5.h"
10 void hmac_md5(unsigned char *text, int text_len,
11 unsigned char* key, int key_len, unsigned char *digest)
12 /* text; pointer to data stream */
13 /* text_len; length of data stream */
14 /* key; pointer to authentication key */
15 /* key_len; length of authentication key */
16 /* digest; caller digest to be filled in */
18 {
19 MD5_CTX context;
20 unsigned char k_ipad[65]; /* inner padding -
21 * key XORd with ipad
22 */
23 unsigned char k_opad[65]; /* outer padding -
24 * key XORd with opad
25 */
26 unsigned char tk[16];
27 int i;
28 /* if key is longer than 64 bytes reset it to key=MD5(key) */
29 if (key_len > 64) {
31 MD5_CTX tctx;
33 MD5Init(&tctx);
34 MD5Update(&tctx, key, key_len);
35 MD5Final(tk, &tctx);
37 key = tk;
38 key_len = 16;
39 }
41 /*
42 * the HMAC_MD5 transform looks like:
43 *
44 * MD5(K XOR opad, MD5(K XOR ipad, text))
45 *
46 * where K is an n byte key
47 * ipad is the byte 0x36 repeated 64 times
48 * opad is the byte 0x5c repeated 64 times
49 * and text is the data being protected
50 */
52 /* start out by storing key in pads */
53 bzero( k_ipad, sizeof k_ipad);
54 bzero( k_opad, sizeof k_opad);
55 bcopy( key, k_ipad, key_len);
56 bcopy( key, k_opad, key_len);
58 /* XOR key with ipad and opad values */
59 for (i=0; i<64; i++) {
60 k_ipad[i] ^= 0x36;
61 k_opad[i] ^= 0x5c;
62 }
63 /*
64 * perform inner MD5
65 */
66 MD5Init(&context); /* init context for 1st
67 * pass */
68 MD5Update(&context, k_ipad, 64); /* start with inner pad */
69 MD5Update(&context, text, text_len); /* then text of datagram */
70 MD5Final(digest, &context); /* finish up 1st pass */
71 /*
72 * perform outer MD5
73 */
74 MD5Init(&context); /* init context for 2nd
75 * pass */
76 MD5Update(&context, k_opad, 64); /* start with outer pad */
77 MD5Update(&context, digest, 16); /* then results of 1st
78 * hash */
79 MD5Final(digest, &context); /* finish up 2nd pass */
80 }