masqmail

annotate src/md5/hmac_md5.c @ 202:5cecc214afc9

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