masqmail

view src/md5/hmac_md5.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children 10da50168dab
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
11 hmac_md5(unsigned char *text, int text_len, 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 */
17 {
18 MD5_CTX context;
19 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
20 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
21 unsigned char tk[16];
22 int i;
23 /* if key is longer than 64 bytes reset it to key=MD5(key) */
24 if (key_len > 64) {
26 MD5_CTX tctx;
28 MD5Init(&tctx);
29 MD5Update(&tctx, key, key_len);
30 MD5Final(tk, &tctx);
32 key = tk;
33 key_len = 16;
34 }
36 /*
37 * the HMAC_MD5 transform looks like:
38 *
39 * MD5(K XOR opad, MD5(K XOR ipad, text))
40 *
41 * where K is an n byte key
42 * ipad is the byte 0x36 repeated 64 times
43 * opad is the byte 0x5c repeated 64 times
44 * and text is the data being protected
45 */
47 /* start out by storing key in pads */
48 bzero(k_ipad, sizeof k_ipad);
49 bzero(k_opad, sizeof k_opad);
50 bcopy(key, k_ipad, key_len);
51 bcopy(key, k_opad, key_len);
53 /* XOR key with ipad and opad values */
54 for (i = 0; i < 64; i++) {
55 k_ipad[i] ^= 0x36;
56 k_opad[i] ^= 0x5c;
57 }
58 /*
59 * perform inner MD5
60 */
61 MD5Init(&context); /* init context for 1st pass */
62 MD5Update(&context, k_ipad, 64); /* start with inner pad */
63 MD5Update(&context, text, text_len); /* then text of datagram */
64 MD5Final(digest, &context); /* finish up 1st pass */
65 /*
66 * perform outer MD5
67 */
68 MD5Init(&context); /* init context for 2nd pass */
69 MD5Update(&context, k_opad, 64); /* start with outer pad */
70 MD5Update(&context, digest, 16); /* then results of 1st hash */
71 MD5Final(digest, &context); /* finish up 2nd pass */
72 }