xref: /onnv-gate/usr/src/lib/libldap4/sec/cram_md5.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  *
3*3857Sstevel  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4*3857Sstevel  * Use is subject to license terms.
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Comments:
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  */
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
120Sstevel@tonic-gate 
130Sstevel@tonic-gate #include <sys/types.h>
140Sstevel@tonic-gate #include <strings.h>
150Sstevel@tonic-gate #include "sec.h"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate /* text is the challenge, key is the password, digest is an allocated
180Sstevel@tonic-gate    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)190Sstevel@tonic-gate void hmac_md5(unsigned char *text, int text_len, unsigned char *key,
200Sstevel@tonic-gate 	int key_len, unsigned char *digest)
210Sstevel@tonic-gate {
220Sstevel@tonic-gate 	MD5_CTX context;
230Sstevel@tonic-gate 	unsigned char k_ipad[65];
240Sstevel@tonic-gate 	unsigned char k_opad[65];
250Sstevel@tonic-gate 	unsigned char tk[16];
260Sstevel@tonic-gate 	int i;
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 	if (key_len > 64){
290Sstevel@tonic-gate 		MD5_CTX tctx;
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 		(void) MD5Init(&tctx);
320Sstevel@tonic-gate 		(void) MD5Update(&tctx, key, key_len);
330Sstevel@tonic-gate 		(void) MD5Final(tk, &tctx);
340Sstevel@tonic-gate 		key = tk;
350Sstevel@tonic-gate 		key_len = 16;
360Sstevel@tonic-gate 	}
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 	bzero(k_ipad, sizeof (k_ipad));
390Sstevel@tonic-gate 	bzero(k_opad, sizeof (k_opad));
400Sstevel@tonic-gate 	bcopy(key, k_ipad, key_len);
410Sstevel@tonic-gate 	bcopy(key, k_opad, key_len);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	for (i=0; i<64; i++){
440Sstevel@tonic-gate 		k_ipad[i] ^= 0x36;
450Sstevel@tonic-gate 		k_opad[i] ^= 0x5c;
460Sstevel@tonic-gate 	}
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	/* Perform inner MD5 */
490Sstevel@tonic-gate 	(void) MD5Init(&context);
500Sstevel@tonic-gate 	(void) MD5Update(&context, k_ipad, 64);
510Sstevel@tonic-gate 	(void) MD5Update(&context, text, text_len);
520Sstevel@tonic-gate 	(void) MD5Final(digest, &context);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	/* Perform outer MD5 */
550Sstevel@tonic-gate 	(void) MD5Init(&context);
560Sstevel@tonic-gate 	(void) MD5Update(&context, k_opad, 64);
570Sstevel@tonic-gate 	(void) MD5Update(&context, digest, 16);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	(void) MD5Final(digest, &context);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	return;
620Sstevel@tonic-gate }
63