annotate src/md5/hmac_md5.c @ 210:e0938c511243

we need at least 32 bits, so let's take a long Solar Designer explained to me that it was int for better performance
author meillo@marmaro.de
date Sun, 18 Jul 2010 22:02:54 +0200
parents 10da50168dab
children 0f36c0a46f82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
1 /*
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
2 ** Function: hmac_md5
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
3 */
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
4
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
5 #include <string.h>
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
6 #include "md5.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
7 #include "hmac_md5.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
8
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
9 void
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
10 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest)
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
11 /* text; pointer to data stream */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
12 /* text_len; length of data stream */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
13 /* key; pointer to authentication key */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
14 /* key_len; length of authentication key */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
15 /* digest; caller digest to be filled in */
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
16 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
17 MD5_CTX context;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
18 unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
19 unsigned char k_opad[65]; /* outer padding - key XORd with opad */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
20 unsigned char tk[16];
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
21 int i;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
22 /* if key is longer than 64 bytes reset it to key=MD5(key) */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
23 if (key_len > 64) {
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
24
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
25 MD5_CTX tctx;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
26
209
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
27 MD5_Init(&tctx);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
28 MD5_Update(&tctx, key, key_len);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
29 MD5_Final(tk, &tctx);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
30
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
31 key = tk;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
32 key_len = 16;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
33 }
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
34
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
35 /*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
36 * the HMAC_MD5 transform looks like:
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
37 *
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
38 * MD5(K XOR opad, MD5(K XOR ipad, text))
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
39 *
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
40 * where K is an n byte key
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
41 * ipad is the byte 0x36 repeated 64 times
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
42 * opad is the byte 0x5c repeated 64 times
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
43 * and text is the data being protected
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
44 */
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
45
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
46 /* start out by storing key in pads */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
47 bzero(k_ipad, sizeof k_ipad);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
48 bzero(k_opad, sizeof k_opad);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
49 bcopy(key, k_ipad, key_len);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
50 bcopy(key, k_opad, key_len);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
51
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
52 /* XOR key with ipad and opad values */
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
53 for (i = 0; i < 64; i++) {
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
54 k_ipad[i] ^= 0x36;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
55 k_opad[i] ^= 0x5c;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
56 }
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
57 /*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
58 * perform inner MD5
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
59 */
209
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
60 MD5_Init(&context); /* init context for 1st pass */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
61 MD5_Update(&context, k_ipad, 64); /* start with inner pad */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
62 MD5_Update(&context, text, text_len); /* then text of datagram */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
63 MD5_Final(digest, &context); /* finish up 1st pass */
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
64 /*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
65 * perform outer MD5
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
66 */
209
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
67 MD5_Init(&context); /* init context for 2nd pass */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
68 MD5_Update(&context, k_opad, 64); /* start with outer pad */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
69 MD5_Update(&context, digest, 16); /* then results of 1st hash */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents: 10
diff changeset
70 MD5_Final(digest, &context); /* finish up 2nd pass */
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
71 }