xref: /onnv-gate/usr/src/lib/libldap4/sec/cram_md5.c (revision 3857:21b9b714e4ab)
1 /*
2  *
3  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  *
7  * Comments:
8  *
9  */
10 
11 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include <sys/types.h>
14 #include <strings.h>
15 #include "sec.h"
16 
17 /* text is the challenge, key is the password, digest is an allocated
18    buffer (min 16 chars) which will contain the resulting digest */
hmac_md5(unsigned char * text,int text_len,unsigned char * key,int key_len,unsigned char * digest)19 void hmac_md5(unsigned char *text, int text_len, unsigned char *key,
20 	int key_len, unsigned char *digest)
21 {
22 	MD5_CTX context;
23 	unsigned char k_ipad[65];
24 	unsigned char k_opad[65];
25 	unsigned char tk[16];
26 	int i;
27 
28 	if (key_len > 64){
29 		MD5_CTX tctx;
30 
31 		(void) MD5Init(&tctx);
32 		(void) MD5Update(&tctx, key, key_len);
33 		(void) MD5Final(tk, &tctx);
34 		key = tk;
35 		key_len = 16;
36 	}
37 
38 	bzero(k_ipad, sizeof (k_ipad));
39 	bzero(k_opad, sizeof (k_opad));
40 	bcopy(key, k_ipad, key_len);
41 	bcopy(key, k_opad, key_len);
42 
43 	for (i=0; i<64; i++){
44 		k_ipad[i] ^= 0x36;
45 		k_opad[i] ^= 0x5c;
46 	}
47 
48 	/* Perform inner MD5 */
49 	(void) MD5Init(&context);
50 	(void) MD5Update(&context, k_ipad, 64);
51 	(void) MD5Update(&context, text, text_len);
52 	(void) MD5Final(digest, &context);
53 
54 	/* Perform outer MD5 */
55 	(void) MD5Init(&context);
56 	(void) MD5Update(&context, k_opad, 64);
57 	(void) MD5Update(&context, digest, 16);
58 
59 	(void) MD5Final(digest, &context);
60 
61 	return;
62 }
63