comparison 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
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
5 #include <string.h> 5 #include <string.h>
6 #include "global.h" 6 #include "global.h"
7 #include "md5.h" 7 #include "md5.h"
8 #include "hmac_md5.h" 8 #include "hmac_md5.h"
9 9
10 void hmac_md5(unsigned char *text, int text_len, 10 void
11 unsigned char* key, int key_len, unsigned char *digest) 11 hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest)
12 /* text; pointer to data stream */ 12 /* text; pointer to data stream */
13 /* text_len; length of data stream */ 13 /* text_len; length of data stream */
14 /* key; pointer to authentication key */ 14 /* key; pointer to authentication key */
15 /* key_len; length of authentication key */ 15 /* key_len; length of authentication key */
16 /* digest; caller digest to be filled in */ 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) {
17 25
18 { 26 MD5_CTX tctx;
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) {
30 27
31 MD5_CTX tctx; 28 MD5Init(&tctx);
29 MD5Update(&tctx, key, key_len);
30 MD5Final(tk, &tctx);
32 31
33 MD5Init(&tctx); 32 key = tk;
34 MD5Update(&tctx, key, key_len); 33 key_len = 16;
35 MD5Final(tk, &tctx); 34 }
36 35
37 key = tk; 36 /*
38 key_len = 16; 37 * the HMAC_MD5 transform looks like:
39 } 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 */
40 46
41 /* 47 /* start out by storing key in pads */
42 * the HMAC_MD5 transform looks like: 48 bzero(k_ipad, sizeof k_ipad);
43 * 49 bzero(k_opad, sizeof k_opad);
44 * MD5(K XOR opad, MD5(K XOR ipad, text)) 50 bcopy(key, k_ipad, key_len);
45 * 51 bcopy(key, k_opad, key_len);
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 */
51 52
52 /* start out by storing key in pads */ 53 /* XOR key with ipad and opad values */
53 bzero( k_ipad, sizeof k_ipad); 54 for (i = 0; i < 64; i++) {
54 bzero( k_opad, sizeof k_opad); 55 k_ipad[i] ^= 0x36;
55 bcopy( key, k_ipad, key_len); 56 k_opad[i] ^= 0x5c;
56 bcopy( key, k_opad, key_len); 57 }
57 58 /*
58 /* XOR key with ipad and opad values */ 59 * perform inner MD5
59 for (i=0; i<64; i++) { 60 */
60 k_ipad[i] ^= 0x36; 61 MD5Init(&context); /* init context for 1st pass */
61 k_opad[i] ^= 0x5c; 62 MD5Update(&context, k_ipad, 64); /* start with inner pad */
62 } 63 MD5Update(&context, text, text_len); /* then text of datagram */
63 /* 64 MD5Final(digest, &context); /* finish up 1st pass */
64 * perform inner MD5 65 /*
65 */ 66 * perform outer MD5
66 MD5Init(&context); /* init context for 1st 67 */
67 * pass */ 68 MD5Init(&context); /* init context for 2nd pass */
68 MD5Update(&context, k_ipad, 64); /* start with inner pad */ 69 MD5Update(&context, k_opad, 64); /* start with outer pad */
69 MD5Update(&context, text, text_len); /* then text of datagram */ 70 MD5Update(&context, digest, 16); /* then results of 1st hash */
70 MD5Final(digest, &context); /* finish up 1st pass */ 71 MD5Final(digest, &context); /* finish up 2nd 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 } 72 }