masqmail

diff src/md5/hmac_md5.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/md5/hmac_md5.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,80 @@
     1.4 +/*
     1.5 +** Function: hmac_md5
     1.6 +*/
     1.7 +
     1.8 +#include <string.h>
     1.9 +#include "global.h"
    1.10 +#include "md5.h"
    1.11 +#include "hmac_md5.h"
    1.12 +
    1.13 +void hmac_md5(unsigned char *text, int text_len,
    1.14 +	      unsigned char* key, int key_len, unsigned char *digest)
    1.15 +     /* text;     pointer to data stream */
    1.16 +     /* text_len; length of data stream */
    1.17 +     /* key;      pointer to authentication key */
    1.18 +     /* key_len;  length of authentication key */
    1.19 +     /* digest;   caller digest to be filled in */
    1.20 +
    1.21 +{
    1.22 +  MD5_CTX context;
    1.23 +  unsigned char k_ipad[65];    /* inner padding -
    1.24 +				* key XORd with ipad
    1.25 +				*/
    1.26 +  unsigned char k_opad[65];    /* outer padding -
    1.27 +				* key XORd with opad
    1.28 +				*/
    1.29 +  unsigned char tk[16];
    1.30 +  int i;
    1.31 +  /* if key is longer than 64 bytes reset it to key=MD5(key) */
    1.32 +  if (key_len > 64) {
    1.33 +
    1.34 +    MD5_CTX      tctx;
    1.35 +
    1.36 +    MD5Init(&tctx);
    1.37 +    MD5Update(&tctx, key, key_len);
    1.38 +    MD5Final(tk, &tctx);
    1.39 +
    1.40 +    key = tk;
    1.41 +    key_len = 16;
    1.42 +  }
    1.43 +
    1.44 +  /*
    1.45 +   * the HMAC_MD5 transform looks like:
    1.46 +   *
    1.47 +   * MD5(K XOR opad, MD5(K XOR ipad, text))
    1.48 +   *
    1.49 +   * where K is an n byte key
    1.50 +   * ipad is the byte 0x36 repeated 64 times
    1.51 +   * opad is the byte 0x5c repeated 64 times
    1.52 +   * and text is the data being protected
    1.53 +   */
    1.54 +
    1.55 +  /* start out by storing key in pads */
    1.56 +  bzero( k_ipad, sizeof k_ipad);
    1.57 +  bzero( k_opad, sizeof k_opad);
    1.58 +  bcopy( key, k_ipad, key_len);
    1.59 +  bcopy( key, k_opad, key_len);
    1.60 +
    1.61 +  /* XOR key with ipad and opad values */
    1.62 +  for (i=0; i<64; i++) {
    1.63 +    k_ipad[i] ^= 0x36;
    1.64 +    k_opad[i] ^= 0x5c;
    1.65 +  }
    1.66 +  /*
    1.67 +   * perform inner MD5
    1.68 +   */
    1.69 +  MD5Init(&context);                   /* init context for 1st
    1.70 +					* pass */
    1.71 +  MD5Update(&context, k_ipad, 64);      /* start with inner pad */
    1.72 +  MD5Update(&context, text, text_len); /* then text of datagram */
    1.73 +  MD5Final(digest, &context);          /* finish up 1st pass */
    1.74 +  /*
    1.75 +   * perform outer MD5
    1.76 +   */
    1.77 +  MD5Init(&context);                   /* init context for 2nd
    1.78 +					* pass */
    1.79 +  MD5Update(&context, k_opad, 64);     /* start with outer pad */
    1.80 +  MD5Update(&context, digest, 16);     /* then results of 1st
    1.81 +					* hash */
    1.82 +  MD5Final(digest, &context);          /* finish up 2nd pass */
    1.83 +}