masqmail-0.2

annotate src/md5/hmac_md5.c @ 2:653aef34b225

removed distribution specific data
author meillo@marmaro.de
date Fri, 26 Sep 2008 21:26:36 +0200
parents
children 26e34ae9a3e3
rev   line source
meillo@0 1 /*
meillo@0 2 ** Function: hmac_md5
meillo@0 3 */
meillo@0 4
meillo@0 5 #include <string.h>
meillo@0 6 #include "global.h"
meillo@0 7 #include "md5.h"
meillo@0 8 #include "hmac_md5.h"
meillo@0 9
meillo@0 10 void hmac_md5(unsigned char *text, int text_len,
meillo@0 11 unsigned char* key, int key_len, unsigned char *digest)
meillo@0 12 /* text; pointer to data stream */
meillo@0 13 /* text_len; length of data stream */
meillo@0 14 /* key; pointer to authentication key */
meillo@0 15 /* key_len; length of authentication key */
meillo@0 16 /* digest; caller digest to be filled in */
meillo@0 17
meillo@0 18 {
meillo@0 19 MD5_CTX context;
meillo@0 20 unsigned char k_ipad[65]; /* inner padding -
meillo@0 21 * key XORd with ipad
meillo@0 22 */
meillo@0 23 unsigned char k_opad[65]; /* outer padding -
meillo@0 24 * key XORd with opad
meillo@0 25 */
meillo@0 26 unsigned char tk[16];
meillo@0 27 int i;
meillo@0 28 /* if key is longer than 64 bytes reset it to key=MD5(key) */
meillo@0 29 if (key_len > 64) {
meillo@0 30
meillo@0 31 MD5_CTX tctx;
meillo@0 32
meillo@0 33 MD5Init(&tctx);
meillo@0 34 MD5Update(&tctx, key, key_len);
meillo@0 35 MD5Final(tk, &tctx);
meillo@0 36
meillo@0 37 key = tk;
meillo@0 38 key_len = 16;
meillo@0 39 }
meillo@0 40
meillo@0 41 /*
meillo@0 42 * the HMAC_MD5 transform looks like:
meillo@0 43 *
meillo@0 44 * MD5(K XOR opad, MD5(K XOR ipad, text))
meillo@0 45 *
meillo@0 46 * where K is an n byte key
meillo@0 47 * ipad is the byte 0x36 repeated 64 times
meillo@0 48 * opad is the byte 0x5c repeated 64 times
meillo@0 49 * and text is the data being protected
meillo@0 50 */
meillo@0 51
meillo@0 52 /* start out by storing key in pads */
meillo@0 53 bzero( k_ipad, sizeof k_ipad);
meillo@0 54 bzero( k_opad, sizeof k_opad);
meillo@0 55 bcopy( key, k_ipad, key_len);
meillo@0 56 bcopy( key, k_opad, key_len);
meillo@0 57
meillo@0 58 /* XOR key with ipad and opad values */
meillo@0 59 for (i=0; i<64; i++) {
meillo@0 60 k_ipad[i] ^= 0x36;
meillo@0 61 k_opad[i] ^= 0x5c;
meillo@0 62 }
meillo@0 63 /*
meillo@0 64 * perform inner MD5
meillo@0 65 */
meillo@0 66 MD5Init(&context); /* init context for 1st
meillo@0 67 * pass */
meillo@0 68 MD5Update(&context, k_ipad, 64); /* start with inner pad */
meillo@0 69 MD5Update(&context, text, text_len); /* then text of datagram */
meillo@0 70 MD5Final(digest, &context); /* finish up 1st pass */
meillo@0 71 /*
meillo@0 72 * perform outer MD5
meillo@0 73 */
meillo@0 74 MD5Init(&context); /* init context for 2nd
meillo@0 75 * pass */
meillo@0 76 MD5Update(&context, k_opad, 64); /* start with outer pad */
meillo@0 77 MD5Update(&context, digest, 16); /* then results of 1st
meillo@0 78 * hash */
meillo@0 79 MD5Final(digest, &context); /* finish up 2nd pass */
meillo@0 80 }