summaryrefslogtreecommitdiff
path: root/usr/src/lib/libldap4/sec/cram_md5.c
blob: 948e17c24673d8f6386747e8c70d43994655bc76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 *
 * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 *
 * Comments:
 *
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <strings.h>
#include "sec.h"

/* text is the challenge, key is the password, digest is an allocated
   buffer (min 16 chars) which will contain the resulting digest */
void hmac_md5(unsigned char *text, int text_len, unsigned char *key,
	int key_len, unsigned char *digest)
{
	MD5_CTX context;
	unsigned char k_ipad[65];
	unsigned char k_opad[65];
	unsigned char tk[16];
	int i;

	if (key_len > 64){
		MD5_CTX tctx;

		(void) MD5Init(&tctx);
		(void) MD5Update(&tctx, key, key_len);
		(void) MD5Final(tk, &tctx);
		key = tk;
		key_len = 16;
	}

	bzero(k_ipad, sizeof (k_ipad));
	bzero(k_opad, sizeof (k_opad));
	bcopy(key, k_ipad, key_len);
	bcopy(key, k_opad, key_len);

	for (i=0; i<64; i++){
		k_ipad[i] ^= 0x36;
		k_opad[i] ^= 0x5c;
	}

	/* Perform inner MD5 */
	(void) MD5Init(&context);
	(void) MD5Update(&context, k_ipad, 64);
	(void) MD5Update(&context, text, text_len);
	(void) MD5Final(digest, &context);

	/* Perform outer MD5 */
	(void) MD5Init(&context);
	(void) MD5Update(&context, k_opad, 64);
	(void) MD5Update(&context, digest, 16);

	(void) MD5Final(digest, &context);

	return;
}