1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #ifdef HMAC_MD5
9*0Sstevel@tonic-gate #ifndef LINT
10*0Sstevel@tonic-gate static const char rcsid[] = "$Header: /proj/cvs/isc/bind8/src/lib/dst/hmac_link.c,v 1.9 2001/05/29 05:48:10 marka Exp $";
11*0Sstevel@tonic-gate #endif
12*0Sstevel@tonic-gate /*
13*0Sstevel@tonic-gate  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * Permission to use, copy modify, and distribute this software for any
16*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
17*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
20*0Sstevel@tonic-gate  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
21*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
22*0Sstevel@tonic-gate  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
23*0Sstevel@tonic-gate  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
24*0Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
25*0Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
26*0Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file contains an implementation of the HMAC-MD5 algorithm.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include "port_before.h"
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <memory.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <sys/time.h>
41*0Sstevel@tonic-gate #include <netinet/in.h>
42*0Sstevel@tonic-gate #include <arpa/nameser.h>
43*0Sstevel@tonic-gate #include <resolv.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "dst_internal.h"
46*0Sstevel@tonic-gate #ifdef USE_MD5
47*0Sstevel@tonic-gate #ifndef	SUNW_LIBMD5
48*0Sstevel@tonic-gate # include "md5.h"
49*0Sstevel@tonic-gate #else
50*0Sstevel@tonic-gate #include <sys/md5.h>
51*0Sstevel@tonic-gate #endif
52*0Sstevel@tonic-gate # ifndef _MD5_H_
53*0Sstevel@tonic-gate #  define _MD5_H_ 1	/* make sure we do not include rsaref md5.h file */
54*0Sstevel@tonic-gate # endif
55*0Sstevel@tonic-gate #endif
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #include "port_after.h"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define HMAC_LEN	64
61*0Sstevel@tonic-gate #define HMAC_IPAD	0x36
62*0Sstevel@tonic-gate #define HMAC_OPAD	0x5c
63*0Sstevel@tonic-gate #define MD5_LEN		16
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate typedef struct hmackey {
67*0Sstevel@tonic-gate 	u_char hk_ipad[64], hk_opad[64];
68*0Sstevel@tonic-gate } HMAC_Key;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /**************************************************************************
72*0Sstevel@tonic-gate  * dst_hmac_md5_sign
73*0Sstevel@tonic-gate  *     Call HMAC signing functions to sign a block of data.
74*0Sstevel@tonic-gate  *     There are three steps to signing, INIT (initialize structures),
75*0Sstevel@tonic-gate  *     UPDATE (hash (more) data), FINAL (generate a signature).  This
76*0Sstevel@tonic-gate  *     routine performs one or more of these steps.
77*0Sstevel@tonic-gate  * Parameters
78*0Sstevel@tonic-gate  *     mode	SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
79*0Sstevel@tonic-gate  *     priv_key    key to use for signing.
80*0Sstevel@tonic-gate  *     context   the context to be used in this digest
81*0Sstevel@tonic-gate  *     data	data to be signed.
82*0Sstevel@tonic-gate  *     len	 length in bytes of data.
83*0Sstevel@tonic-gate  *     signature   location to store signature.
84*0Sstevel@tonic-gate  *     sig_len     size of the signature location
85*0Sstevel@tonic-gate  * returns
86*0Sstevel@tonic-gate  *	N  Success on SIG_MODE_FINAL = returns signature length in bytes
87*0Sstevel@tonic-gate  *	0  Success on SIG_MODE_INIT  and UPDATE
88*0Sstevel@tonic-gate  *	 <0  Failure
89*0Sstevel@tonic-gate  */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static int
92*0Sstevel@tonic-gate dst_hmac_md5_sign(const int mode, DST_KEY *d_key, void **context,
93*0Sstevel@tonic-gate 		  const u_char *data, const int len,
94*0Sstevel@tonic-gate 		  u_char *signature, const int sig_len)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	HMAC_Key *key;
97*0Sstevel@tonic-gate 	int sign_len = 0;
98*0Sstevel@tonic-gate 	MD5_CTX *ctx = NULL;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if (mode & SIG_MODE_INIT)
101*0Sstevel@tonic-gate 		ctx = (MD5_CTX *) malloc(sizeof(*ctx));
102*0Sstevel@tonic-gate 	else if (context)
103*0Sstevel@tonic-gate 		ctx = (MD5_CTX *) *context;
104*0Sstevel@tonic-gate 	if (ctx == NULL)
105*0Sstevel@tonic-gate 		return (-1);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (d_key == NULL || d_key->dk_KEY_struct == NULL)
108*0Sstevel@tonic-gate 		return (-1);
109*0Sstevel@tonic-gate 	key = (HMAC_Key *) d_key->dk_KEY_struct;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (mode & SIG_MODE_INIT) {
112*0Sstevel@tonic-gate 		MD5Init(ctx);
113*0Sstevel@tonic-gate 		MD5Update(ctx, key->hk_ipad, HMAC_LEN);
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
117*0Sstevel@tonic-gate 		MD5Update(ctx, data, len);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (mode & SIG_MODE_FINAL) {
120*0Sstevel@tonic-gate 		if (signature == NULL || sig_len < MD5_LEN)
121*0Sstevel@tonic-gate 			return (SIGN_FINAL_FAILURE);
122*0Sstevel@tonic-gate 		MD5Final(signature, ctx);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		/* perform outer MD5 */
125*0Sstevel@tonic-gate 		MD5Init(ctx);
126*0Sstevel@tonic-gate 		MD5Update(ctx, key->hk_opad, HMAC_LEN);
127*0Sstevel@tonic-gate 		MD5Update(ctx, signature, MD5_LEN);
128*0Sstevel@tonic-gate 		MD5Final(signature, ctx);
129*0Sstevel@tonic-gate 		sign_len = MD5_LEN;
130*0Sstevel@tonic-gate 		SAFE_FREE(ctx);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	else {
133*0Sstevel@tonic-gate 		if (context == NULL)
134*0Sstevel@tonic-gate 			return (-1);
135*0Sstevel@tonic-gate 		*context = (void *) ctx;
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 	return (sign_len);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /**************************************************************************
142*0Sstevel@tonic-gate  * dst_hmac_md5_verify()
143*0Sstevel@tonic-gate  *     Calls HMAC verification routines.  There are three steps to
144*0Sstevel@tonic-gate  *     verification, INIT (initialize structures), UPDATE (hash (more) data),
145*0Sstevel@tonic-gate  *     FINAL (generate a signature).  This routine performs one or more of
146*0Sstevel@tonic-gate  *     these steps.
147*0Sstevel@tonic-gate  * Parameters
148*0Sstevel@tonic-gate  *     mode	SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
149*0Sstevel@tonic-gate  *     dkey	key to use for verify.
150*0Sstevel@tonic-gate  *     data	data signed.
151*0Sstevel@tonic-gate  *     len	 length in bytes of data.
152*0Sstevel@tonic-gate  *     signature   signature.
153*0Sstevel@tonic-gate  *     sig_len     length in bytes of signature.
154*0Sstevel@tonic-gate  * returns
155*0Sstevel@tonic-gate  *     0  Success
156*0Sstevel@tonic-gate  *    <0  Failure
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate static int
160*0Sstevel@tonic-gate dst_hmac_md5_verify(const int mode, DST_KEY *d_key, void **context,
161*0Sstevel@tonic-gate 		const u_char *data, const int len,
162*0Sstevel@tonic-gate 		const u_char *signature, const int sig_len)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	HMAC_Key *key;
165*0Sstevel@tonic-gate 	MD5_CTX *ctx = NULL;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (mode & SIG_MODE_INIT)
168*0Sstevel@tonic-gate 		ctx = (MD5_CTX *) malloc(sizeof(*ctx));
169*0Sstevel@tonic-gate 	else if (context)
170*0Sstevel@tonic-gate 		ctx = (MD5_CTX *) *context;
171*0Sstevel@tonic-gate 	if (ctx == NULL)
172*0Sstevel@tonic-gate 		return (-1);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (d_key == NULL || d_key->dk_KEY_struct == NULL)
175*0Sstevel@tonic-gate 		return (-1);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	key = (HMAC_Key *) d_key->dk_KEY_struct;
178*0Sstevel@tonic-gate 	if (mode & SIG_MODE_INIT) {
179*0Sstevel@tonic-gate 		MD5Init(ctx);
180*0Sstevel@tonic-gate 		MD5Update(ctx, key->hk_ipad, HMAC_LEN);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
183*0Sstevel@tonic-gate 		MD5Update(ctx, data, len);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (mode & SIG_MODE_FINAL) {
186*0Sstevel@tonic-gate 		u_char digest[MD5_LEN];
187*0Sstevel@tonic-gate 		if (signature == NULL || key == NULL || sig_len != MD5_LEN)
188*0Sstevel@tonic-gate 			return (VERIFY_FINAL_FAILURE);
189*0Sstevel@tonic-gate 		MD5Final(digest, ctx);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		/* perform outer MD5 */
192*0Sstevel@tonic-gate 		MD5Init(ctx);
193*0Sstevel@tonic-gate 		MD5Update(ctx, key->hk_opad, HMAC_LEN);
194*0Sstevel@tonic-gate 		MD5Update(ctx, digest, MD5_LEN);
195*0Sstevel@tonic-gate 		MD5Final(digest, ctx);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		SAFE_FREE(ctx);
198*0Sstevel@tonic-gate 		if (memcmp(digest, signature, MD5_LEN) != 0)
199*0Sstevel@tonic-gate 			return (VERIFY_FINAL_FAILURE);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 	else {
202*0Sstevel@tonic-gate 		if (context == NULL)
203*0Sstevel@tonic-gate 			return (-1);
204*0Sstevel@tonic-gate 		*context = (void *) ctx;
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 	return (0);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /**************************************************************************
211*0Sstevel@tonic-gate  * dst_buffer_to_hmac_md5
212*0Sstevel@tonic-gate  *     Converts key from raw data to an HMAC Key
213*0Sstevel@tonic-gate  *     This function gets in a pointer to the data
214*0Sstevel@tonic-gate  * Parameters
215*0Sstevel@tonic-gate  *     hkey	the HMAC key to be filled in
216*0Sstevel@tonic-gate  *     key	the key in raw format
217*0Sstevel@tonic-gate  *     keylen	the length of the key
218*0Sstevel@tonic-gate  * Return
219*0Sstevel@tonic-gate  *	0	Success
220*0Sstevel@tonic-gate  *	<0	Failure
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate static int
223*0Sstevel@tonic-gate dst_buffer_to_hmac_md5(DST_KEY *dkey, const u_char *key, const int keylen)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	int i;
226*0Sstevel@tonic-gate 	HMAC_Key *hkey = NULL;
227*0Sstevel@tonic-gate 	MD5_CTX ctx;
228*0Sstevel@tonic-gate 	int local_keylen = keylen;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	if (dkey == NULL || key == NULL || keylen < 0)
231*0Sstevel@tonic-gate 		return (-1);
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
234*0Sstevel@tonic-gate 		  return (-2);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
237*0Sstevel@tonic-gate 	memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
240*0Sstevel@tonic-gate 	if (keylen > HMAC_LEN) {
241*0Sstevel@tonic-gate 		u_char tk[MD5_LEN];
242*0Sstevel@tonic-gate 		MD5Init(&ctx);
243*0Sstevel@tonic-gate 		MD5Update(&ctx, key, keylen);
244*0Sstevel@tonic-gate 		MD5Final(tk, &ctx);
245*0Sstevel@tonic-gate 		memset((void *) &ctx, 0, sizeof(ctx));
246*0Sstevel@tonic-gate 		key = tk;
247*0Sstevel@tonic-gate 		local_keylen = MD5_LEN;
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 	/* start out by storing key in pads */
250*0Sstevel@tonic-gate 	memcpy(hkey->hk_ipad, key, local_keylen);
251*0Sstevel@tonic-gate 	memcpy(hkey->hk_opad, key, local_keylen);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	/* XOR key with hk_ipad and opad values */
254*0Sstevel@tonic-gate 	for (i = 0; i < HMAC_LEN; i++) {
255*0Sstevel@tonic-gate 		hkey->hk_ipad[i] ^= HMAC_IPAD;
256*0Sstevel@tonic-gate 		hkey->hk_opad[i] ^= HMAC_OPAD;
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 	dkey->dk_key_size = local_keylen;
259*0Sstevel@tonic-gate 	dkey->dk_KEY_struct = (void *) hkey;
260*0Sstevel@tonic-gate 	return (1);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /**************************************************************************
265*0Sstevel@tonic-gate  *  dst_hmac_md5_key_to_file_format
266*0Sstevel@tonic-gate  *	Encodes an HMAC Key into the portable file format.
267*0Sstevel@tonic-gate  *  Parameters
268*0Sstevel@tonic-gate  *	hkey      HMAC KEY structure
269*0Sstevel@tonic-gate  *	buff      output buffer
270*0Sstevel@tonic-gate  *	buff_len  size of output buffer
271*0Sstevel@tonic-gate  *  Return
272*0Sstevel@tonic-gate  *	0  Failure - null input hkey
273*0Sstevel@tonic-gate  *     -1  Failure - not enough space in output area
274*0Sstevel@tonic-gate  *	N  Success - Length of data returned in buff
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate static int
278*0Sstevel@tonic-gate dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
279*0Sstevel@tonic-gate 			    const int buff_len)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	char *bp;
282*0Sstevel@tonic-gate 	int len, b_len, i, key_len;
283*0Sstevel@tonic-gate 	u_char key[HMAC_LEN];
284*0Sstevel@tonic-gate 	HMAC_Key *hkey;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	if (dkey == NULL || dkey->dk_KEY_struct == NULL)
287*0Sstevel@tonic-gate 		return (0);
288*0Sstevel@tonic-gate 	if (buff == NULL || buff_len <= (int) strlen(key_file_fmt_str))
289*0Sstevel@tonic-gate 		return (-1);	/* no OR not enough space in output area */
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	hkey = (HMAC_Key *) dkey->dk_KEY_struct;
292*0Sstevel@tonic-gate 	memset(buff, 0, buff_len);	/* just in case */
293*0Sstevel@tonic-gate 	/* write file header */
294*0Sstevel@tonic-gate 	sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC");
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	bp = (char *) strchr(buff, '\0');
297*0Sstevel@tonic-gate 	b_len = buff_len - (bp - buff);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	memset(key, 0, HMAC_LEN);
300*0Sstevel@tonic-gate 	for (i = 0; i < HMAC_LEN; i++)
301*0Sstevel@tonic-gate 		key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
302*0Sstevel@tonic-gate 	for (i = HMAC_LEN - 1; i >= 0; i--)
303*0Sstevel@tonic-gate 		if (key[i] != 0)
304*0Sstevel@tonic-gate 			break;
305*0Sstevel@tonic-gate 	key_len = i + 1;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	strcat(bp, "Key: ");
308*0Sstevel@tonic-gate 	bp += strlen("Key: ");
309*0Sstevel@tonic-gate 	b_len = buff_len - (bp - buff);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	len = b64_ntop(key, key_len, bp, b_len);
312*0Sstevel@tonic-gate 	if (len < 0)
313*0Sstevel@tonic-gate 		return (-1);
314*0Sstevel@tonic-gate 	bp += len;
315*0Sstevel@tonic-gate 	*(bp++) = '\n';
316*0Sstevel@tonic-gate 	*bp = '\0';
317*0Sstevel@tonic-gate 	b_len = buff_len - (bp - buff);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	return (buff_len - b_len);
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate /**************************************************************************
324*0Sstevel@tonic-gate  * dst_hmac_md5_key_from_file_format
325*0Sstevel@tonic-gate  *     Converts contents of a key file into an HMAC key.
326*0Sstevel@tonic-gate  * Parameters
327*0Sstevel@tonic-gate  *     hkey    structure to put key into
328*0Sstevel@tonic-gate  *     buff       buffer containing the encoded key
329*0Sstevel@tonic-gate  *     buff_len   the length of the buffer
330*0Sstevel@tonic-gate  * Return
331*0Sstevel@tonic-gate  *     n >= 0 Foot print of the key converted
332*0Sstevel@tonic-gate  *     n <  0 Error in conversion
333*0Sstevel@tonic-gate  */
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate static int
336*0Sstevel@tonic-gate dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
337*0Sstevel@tonic-gate 			      const int buff_len)
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	const char *p = buff, *eol;
340*0Sstevel@tonic-gate 	u_char key[HMAC_LEN+1];	/* b64_pton needs more than 64 bytes do decode
341*0Sstevel@tonic-gate 							 * it should probably be fixed rather than doing
342*0Sstevel@tonic-gate 							 * this
343*0Sstevel@tonic-gate 							 */
344*0Sstevel@tonic-gate 	u_char *tmp;
345*0Sstevel@tonic-gate 	int key_len, len;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	if (dkey == NULL)
348*0Sstevel@tonic-gate 		return (-2);
349*0Sstevel@tonic-gate 	if (buff == NULL || buff_len < 0)
350*0Sstevel@tonic-gate 		return (-1);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	memset(key, 0, sizeof(key));
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (!dst_s_verify_str(&p, "Key: "))
355*0Sstevel@tonic-gate 		return (-3);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	eol = strchr(p, '\n');
358*0Sstevel@tonic-gate 	if (eol == NULL)
359*0Sstevel@tonic-gate 		return (-4);
360*0Sstevel@tonic-gate 	len = eol - p;
361*0Sstevel@tonic-gate 	tmp = malloc(len + 2);
362*0Sstevel@tonic-gate 	memcpy(tmp, p, len);
363*0Sstevel@tonic-gate 	*(tmp + len) = 0x0;
364*0Sstevel@tonic-gate 	key_len = b64_pton((char *)tmp, key, HMAC_LEN+1);	/* see above */
365*0Sstevel@tonic-gate 	SAFE_FREE2(tmp, len + 2);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
368*0Sstevel@tonic-gate 		return (-6);
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 	return (0);
371*0Sstevel@tonic-gate }
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate /*
374*0Sstevel@tonic-gate  * dst_hmac_md5_to_dns_key()
375*0Sstevel@tonic-gate  *         function to extract hmac key from DST_KEY structure
376*0Sstevel@tonic-gate  * intput:
377*0Sstevel@tonic-gate  *      in_key:  HMAC-MD5 key
378*0Sstevel@tonic-gate  * output:
379*0Sstevel@tonic-gate  *	out_str: buffer to write ot
380*0Sstevel@tonic-gate  *      out_len: size of output buffer
381*0Sstevel@tonic-gate  * returns:
382*0Sstevel@tonic-gate  *      number of bytes written to output buffer
383*0Sstevel@tonic-gate  */
384*0Sstevel@tonic-gate static int
385*0Sstevel@tonic-gate dst_hmac_md5_to_dns_key(const DST_KEY *in_key, u_char *out_str,
386*0Sstevel@tonic-gate 			const int out_len)
387*0Sstevel@tonic-gate {
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	HMAC_Key *hkey;
390*0Sstevel@tonic-gate 	int i;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	if (in_key == NULL || in_key->dk_KEY_struct == NULL ||
393*0Sstevel@tonic-gate 	    out_len <= in_key->dk_key_size || out_str == NULL)
394*0Sstevel@tonic-gate 		return (-1);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	hkey = (HMAC_Key *) in_key->dk_KEY_struct;
397*0Sstevel@tonic-gate 	for (i = 0; i < in_key->dk_key_size; i++)
398*0Sstevel@tonic-gate 		out_str[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
399*0Sstevel@tonic-gate 	return (i);
400*0Sstevel@tonic-gate }
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate /**************************************************************************
403*0Sstevel@tonic-gate  *  dst_hmac_md5_compare_keys
404*0Sstevel@tonic-gate  *	Compare two keys for equality.
405*0Sstevel@tonic-gate  *  Return
406*0Sstevel@tonic-gate  *	0	  The keys are equal
407*0Sstevel@tonic-gate  *	NON-ZERO   The keys are not equal
408*0Sstevel@tonic-gate  */
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate static int
411*0Sstevel@tonic-gate dst_hmac_md5_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
412*0Sstevel@tonic-gate {
413*0Sstevel@tonic-gate 	HMAC_Key *hkey1 = (HMAC_Key *) key1->dk_KEY_struct;
414*0Sstevel@tonic-gate 	HMAC_Key *hkey2 = (HMAC_Key *) key2->dk_KEY_struct;
415*0Sstevel@tonic-gate 	return memcmp(hkey1->hk_ipad, hkey2->hk_ipad, HMAC_LEN);
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate /**************************************************************************
419*0Sstevel@tonic-gate  * dst_hmac_md5_free_key_structure
420*0Sstevel@tonic-gate  *     Frees all (none) dynamically allocated structures in hkey
421*0Sstevel@tonic-gate  */
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate static void *
424*0Sstevel@tonic-gate dst_hmac_md5_free_key_structure(void *key)
425*0Sstevel@tonic-gate {
426*0Sstevel@tonic-gate 	HMAC_Key *hkey = key;
427*0Sstevel@tonic-gate 	SAFE_FREE(hkey);
428*0Sstevel@tonic-gate 	return (NULL);
429*0Sstevel@tonic-gate }
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate /***************************************************************************
433*0Sstevel@tonic-gate  * dst_hmac_md5_generate_key
434*0Sstevel@tonic-gate  *     Creates a HMAC key of size size with a maximum size of 63 bytes
435*0Sstevel@tonic-gate  *     generating a HMAC key larger than 63 bytes makes no sense as that key
436*0Sstevel@tonic-gate  *     is digested before use.
437*0Sstevel@tonic-gate  */
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate static int
440*0Sstevel@tonic-gate dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
441*0Sstevel@tonic-gate {
442*0Sstevel@tonic-gate 	u_char *buff;
443*0Sstevel@tonic-gate 	int i, n, size;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	i = nothing;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	if (key == NULL || key->dk_alg != KEY_HMAC_MD5)
448*0Sstevel@tonic-gate 		return (0);
449*0Sstevel@tonic-gate 	size = (key->dk_key_size + 7) / 8; /* convert to bytes */
450*0Sstevel@tonic-gate 	if (size <= 0)
451*0Sstevel@tonic-gate 		return(0);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	i = size > 64 ? 64 : size;
454*0Sstevel@tonic-gate 	buff = malloc(i+8);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	n = dst_random(DST_RAND_SEMI, i, buff);
457*0Sstevel@tonic-gate 	n += dst_random(DST_RAND_KEY, i, buff);
458*0Sstevel@tonic-gate 	if (n <= i) {	/* failed getting anything */
459*0Sstevel@tonic-gate 		SAFE_FREE2(buff, i);
460*0Sstevel@tonic-gate 		return (-1);
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 	n = dst_buffer_to_hmac_md5(key, buff, i);
463*0Sstevel@tonic-gate 	SAFE_FREE2(buff, i);
464*0Sstevel@tonic-gate 	if (n <= 0)
465*0Sstevel@tonic-gate 		return (n);
466*0Sstevel@tonic-gate 	return (1);
467*0Sstevel@tonic-gate }
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate /*
470*0Sstevel@tonic-gate  * dst_hmac_md5_init()  Function to answer set up function pointers for HMAC
471*0Sstevel@tonic-gate  *	   related functions
472*0Sstevel@tonic-gate  */
473*0Sstevel@tonic-gate int
474*0Sstevel@tonic-gate #ifdef	ORIGINAL_ISC_CODE
475*0Sstevel@tonic-gate dst_hmac_md5_init()
476*0Sstevel@tonic-gate #else
477*0Sstevel@tonic-gate dst_md5_hmac_init()
478*0Sstevel@tonic-gate #endif
479*0Sstevel@tonic-gate {
480*0Sstevel@tonic-gate 	if (dst_t_func[KEY_HMAC_MD5] != NULL)
481*0Sstevel@tonic-gate 		return (1);
482*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
483*0Sstevel@tonic-gate 	if (dst_t_func[KEY_HMAC_MD5] == NULL)
484*0Sstevel@tonic-gate 		return (0);
485*0Sstevel@tonic-gate 	memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
486*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->sign = dst_hmac_md5_sign;
487*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->verify = dst_hmac_md5_verify;
488*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->compare = dst_hmac_md5_compare_keys;
489*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->generate = dst_hmac_md5_generate_key;
490*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->destroy = dst_hmac_md5_free_key_structure;
491*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->to_dns_key = dst_hmac_md5_to_dns_key;
492*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->from_dns_key = dst_buffer_to_hmac_md5;
493*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->to_file_fmt = dst_hmac_md5_key_to_file_format;
494*0Sstevel@tonic-gate 	dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
495*0Sstevel@tonic-gate 	return (1);
496*0Sstevel@tonic-gate }
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate #else
499*0Sstevel@tonic-gate int
500*0Sstevel@tonic-gate dst_hmac_md5_init(){
501*0Sstevel@tonic-gate 	return (0);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate #endif
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 
511