masqmail-0.2

view src/md5/hmac_md5.c @ 162:52c82d755215

replaced the MD5 implementation with the one of Solar Designer Until now, the sample code of RFC 1321 was used. It had an ugly license. Now we use the implementation of Solar Designer, which is in the Public Domain. http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
author meillo@marmaro.de
date Sun, 18 Jul 2010 22:01:04 +0200
parents 26e34ae9a3e3
children b8c358b2e242
line source
1 /*
2 ** Function: hmac_md5
3 */
5 #include <string.h>
6 #include "md5.h"
7 #include "hmac_md5.h"
9 void
10 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest)
11 /* text; pointer to data stream */
12 /* text_len; length of data stream */
13 /* key; pointer to authentication key */
14 /* key_len; length of authentication key */
15 /* digest; caller digest to be filled in */
16 {
17 MD5_CTX context;
18 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
19 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
20 unsigned char tk[16];
21 int i;
22 /* if key is longer than 64 bytes reset it to key=MD5(key) */
23 if (key_len > 64) {
25 MD5_CTX tctx;
27 MD5_Init(&tctx);
28 MD5_Update(&tctx, key, key_len);
29 MD5_Final(tk, &tctx);
31 key = tk;
32 key_len = 16;
33 }
35 /*
36 * the HMAC_MD5 transform looks like:
37 *
38 * MD5(K XOR opad, MD5(K XOR ipad, text))
39 *
40 * where K is an n byte key
41 * ipad is the byte 0x36 repeated 64 times
42 * opad is the byte 0x5c repeated 64 times
43 * and text is the data being protected
44 */
46 /* start out by storing key in pads */
47 bzero(k_ipad, sizeof k_ipad);
48 bzero(k_opad, sizeof k_opad);
49 bcopy(key, k_ipad, key_len);
50 bcopy(key, k_opad, key_len);
52 /* XOR key with ipad and opad values */
53 for (i = 0; i < 64; i++) {
54 k_ipad[i] ^= 0x36;
55 k_opad[i] ^= 0x5c;
56 }
57 /*
58 * perform inner MD5
59 */
60 MD5_Init(&context); /* init context for 1st pass */
61 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
62 MD5_Update(&context, text, text_len); /* then text of datagram */
63 MD5_Final(digest, &context); /* finish up 1st pass */
64 /*
65 * perform outer MD5
66 */
67 MD5_Init(&context); /* init context for 2nd pass */
68 MD5_Update(&context, k_opad, 64); /* start with outer pad */
69 MD5_Update(&context, digest, 16); /* then results of 1st hash */
70 MD5_Final(digest, &context); /* finish up 2nd pass */
71 }