masqmail-0.2

diff 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 52c82d755215
line diff
     1.1 --- a/src/md5/hmac_md5.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/md5/hmac_md5.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -7,74 +7,66 @@
     1.4  #include "md5.h"
     1.5  #include "hmac_md5.h"
     1.6  
     1.7 -void hmac_md5(unsigned char *text, int text_len,
     1.8 -	      unsigned char* key, int key_len, unsigned char *digest)
     1.9 -     /* text;     pointer to data stream */
    1.10 -     /* text_len; length of data stream */
    1.11 -     /* key;      pointer to authentication key */
    1.12 -     /* key_len;  length of authentication key */
    1.13 -     /* digest;   caller digest to be filled in */
    1.14 +void
    1.15 +hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest)
    1.16 +	 /* text;     pointer to data stream */
    1.17 +	 /* text_len; length of data stream */
    1.18 +	 /* key;      pointer to authentication key */
    1.19 +	 /* key_len;  length of authentication key */
    1.20 +	 /* digest;   caller digest to be filled in */
    1.21 +{
    1.22 +	MD5_CTX context;
    1.23 +	unsigned char k_ipad[65];  /* inner padding - key XORd with ipad */
    1.24 +	unsigned char k_opad[65];  /* outer padding - key XORd with opad */
    1.25 +	unsigned char tk[16];
    1.26 +	int i;
    1.27 +	/* if key is longer than 64 bytes reset it to key=MD5(key) */
    1.28 +	if (key_len > 64) {
    1.29  
    1.30 -{
    1.31 -  MD5_CTX context;
    1.32 -  unsigned char k_ipad[65];    /* inner padding -
    1.33 -				* key XORd with ipad
    1.34 -				*/
    1.35 -  unsigned char k_opad[65];    /* outer padding -
    1.36 -				* key XORd with opad
    1.37 -				*/
    1.38 -  unsigned char tk[16];
    1.39 -  int i;
    1.40 -  /* if key is longer than 64 bytes reset it to key=MD5(key) */
    1.41 -  if (key_len > 64) {
    1.42 +		MD5_CTX tctx;
    1.43  
    1.44 -    MD5_CTX      tctx;
    1.45 +		MD5Init(&tctx);
    1.46 +		MD5Update(&tctx, key, key_len);
    1.47 +		MD5Final(tk, &tctx);
    1.48  
    1.49 -    MD5Init(&tctx);
    1.50 -    MD5Update(&tctx, key, key_len);
    1.51 -    MD5Final(tk, &tctx);
    1.52 +		key = tk;
    1.53 +		key_len = 16;
    1.54 +	}
    1.55  
    1.56 -    key = tk;
    1.57 -    key_len = 16;
    1.58 -  }
    1.59 +	/*
    1.60 +	 * the HMAC_MD5 transform looks like:
    1.61 +	 *
    1.62 +	 * MD5(K XOR opad, MD5(K XOR ipad, text))
    1.63 +	 *
    1.64 +	 * where K is an n byte key
    1.65 +	 * ipad is the byte 0x36 repeated 64 times
    1.66 +	 * opad is the byte 0x5c repeated 64 times
    1.67 +	 * and text is the data being protected
    1.68 +	 */
    1.69  
    1.70 -  /*
    1.71 -   * the HMAC_MD5 transform looks like:
    1.72 -   *
    1.73 -   * MD5(K XOR opad, MD5(K XOR ipad, text))
    1.74 -   *
    1.75 -   * where K is an n byte key
    1.76 -   * ipad is the byte 0x36 repeated 64 times
    1.77 -   * opad is the byte 0x5c repeated 64 times
    1.78 -   * and text is the data being protected
    1.79 -   */
    1.80 +	/* start out by storing key in pads */
    1.81 +	bzero(k_ipad, sizeof k_ipad);
    1.82 +	bzero(k_opad, sizeof k_opad);
    1.83 +	bcopy(key, k_ipad, key_len);
    1.84 +	bcopy(key, k_opad, key_len);
    1.85  
    1.86 -  /* start out by storing key in pads */
    1.87 -  bzero( k_ipad, sizeof k_ipad);
    1.88 -  bzero( k_opad, sizeof k_opad);
    1.89 -  bcopy( key, k_ipad, key_len);
    1.90 -  bcopy( key, k_opad, key_len);
    1.91 -
    1.92 -  /* XOR key with ipad and opad values */
    1.93 -  for (i=0; i<64; i++) {
    1.94 -    k_ipad[i] ^= 0x36;
    1.95 -    k_opad[i] ^= 0x5c;
    1.96 -  }
    1.97 -  /*
    1.98 -   * perform inner MD5
    1.99 -   */
   1.100 -  MD5Init(&context);                   /* init context for 1st
   1.101 -					* pass */
   1.102 -  MD5Update(&context, k_ipad, 64);      /* start with inner pad */
   1.103 -  MD5Update(&context, text, text_len); /* then text of datagram */
   1.104 -  MD5Final(digest, &context);          /* finish up 1st pass */
   1.105 -  /*
   1.106 -   * perform outer MD5
   1.107 -   */
   1.108 -  MD5Init(&context);                   /* init context for 2nd
   1.109 -					* pass */
   1.110 -  MD5Update(&context, k_opad, 64);     /* start with outer pad */
   1.111 -  MD5Update(&context, digest, 16);     /* then results of 1st
   1.112 -					* hash */
   1.113 -  MD5Final(digest, &context);          /* finish up 2nd pass */
   1.114 +	/* XOR key with ipad and opad values */
   1.115 +	for (i = 0; i < 64; i++) {
   1.116 +		k_ipad[i] ^= 0x36;
   1.117 +		k_opad[i] ^= 0x5c;
   1.118 +	}
   1.119 +	/*
   1.120 +	 * perform inner MD5
   1.121 +	 */
   1.122 +	MD5Init(&context);  /* init context for 1st pass */
   1.123 +	MD5Update(&context, k_ipad, 64);  /* start with inner pad */
   1.124 +	MD5Update(&context, text, text_len);  /* then text of datagram */
   1.125 +	MD5Final(digest, &context);  /* finish up 1st pass */
   1.126 +	/*
   1.127 +	 * perform outer MD5
   1.128 +	 */
   1.129 +	MD5Init(&context);  /* init context for 2nd pass */
   1.130 +	MD5Update(&context, k_opad, 64);  /* start with outer pad */
   1.131 +	MD5Update(&context, digest, 16);  /* then results of 1st hash */
   1.132 +	MD5Final(digest, &context); /* finish up 2nd pass */
   1.133  }