Mercurial > masqmail-0.2
annotate 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 |
rev | line source |
---|---|
0 | 1 /* |
2 ** Function: hmac_md5 | |
3 */ | |
4 | |
5 #include <string.h> | |
6 #include "md5.h" | |
7 #include "hmac_md5.h" | |
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 | 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 | 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 | 26 |
162
52c82d755215
replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
10
diff
changeset
|
27 MD5_Init(&tctx); |
52c82d755215
replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
10
diff
changeset
|
28 MD5_Update(&tctx, key, key_len); |
52c82d755215
replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
10
diff
changeset
|
29 MD5_Final(tk, &tctx); |
0 | 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 | 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 | 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 | 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 */ |
162
52c82d755215
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 */ |
52c82d755215
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 */ |
52c82d755215
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 */ |
52c82d755215
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 */ |
162
52c82d755215
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 */ |
52c82d755215
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 */ |
52c82d755215
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 */ |
52c82d755215
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 | 71 } |