1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 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  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
12*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
13*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
14*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
15*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*0Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*0Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*0Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*0Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*0Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "includes.h"
30*0Sstevel@tonic-gate RCSID("$OpenBSD: mac.c,v 1.5 2002/05/16 22:02:50 markus Exp $");
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <openssl/hmac.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "xmalloc.h"
37*0Sstevel@tonic-gate #include "getput.h"
38*0Sstevel@tonic-gate #include "log.h"
39*0Sstevel@tonic-gate #include "cipher.h"
40*0Sstevel@tonic-gate #include "kex.h"
41*0Sstevel@tonic-gate #include "mac.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate struct {
44*0Sstevel@tonic-gate 	char		*name;
45*0Sstevel@tonic-gate 	const EVP_MD *	(*mdfunc)(void);
46*0Sstevel@tonic-gate 	int		truncatebits;	/* truncate digest if != 0 */
47*0Sstevel@tonic-gate } macs[] = {
48*0Sstevel@tonic-gate 	{ "hmac-sha1",			EVP_sha1, 0 },
49*0Sstevel@tonic-gate 	{ "hmac-sha1-96",		EVP_sha1, 96 },
50*0Sstevel@tonic-gate 	{ "hmac-md5",			EVP_md5, 0 },
51*0Sstevel@tonic-gate 	{ "hmac-md5-96",		EVP_md5, 96 },
52*0Sstevel@tonic-gate #ifdef SOLARIS_SSH_ENABLE_RIPEMD160
53*0Sstevel@tonic-gate 	{ "hmac-ripemd160",		EVP_ripemd160, 0 },
54*0Sstevel@tonic-gate 	{ "hmac-ripemd160@openssh.com",	EVP_ripemd160, 0 },
55*0Sstevel@tonic-gate #endif /* SOLARIS_SSH_ENABLE_RIPEMD160 */
56*0Sstevel@tonic-gate 	{ NULL,				NULL, 0 }
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate int
60*0Sstevel@tonic-gate mac_init(Mac *mac, char *name)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	int i;
63*0Sstevel@tonic-gate 	for (i = 0; macs[i].name; i++) {
64*0Sstevel@tonic-gate 		if (strcmp(name, macs[i].name) == 0) {
65*0Sstevel@tonic-gate 			if (mac != NULL) {
66*0Sstevel@tonic-gate 				mac->md = (*macs[i].mdfunc)();
67*0Sstevel@tonic-gate 				mac->key_len = mac->mac_len = EVP_MD_size(mac->md);
68*0Sstevel@tonic-gate 				if (macs[i].truncatebits != 0)
69*0Sstevel@tonic-gate 					mac->mac_len = macs[i].truncatebits/8;
70*0Sstevel@tonic-gate 			}
71*0Sstevel@tonic-gate 			debug2("mac_init: found %s", name);
72*0Sstevel@tonic-gate 			return (0);
73*0Sstevel@tonic-gate 		}
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	debug2("mac_init: unknown %s", name);
76*0Sstevel@tonic-gate 	return (-1);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate u_char *
80*0Sstevel@tonic-gate mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	HMAC_CTX c;
83*0Sstevel@tonic-gate 	static u_char m[EVP_MAX_MD_SIZE];
84*0Sstevel@tonic-gate 	u_char b[4];
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (mac->key == NULL)
87*0Sstevel@tonic-gate 		fatal("mac_compute: no key");
88*0Sstevel@tonic-gate 	if (mac->mac_len > sizeof(m))
89*0Sstevel@tonic-gate 		fatal("mac_compute: mac too long");
90*0Sstevel@tonic-gate 	HMAC_Init(&c, mac->key, mac->key_len, mac->md);
91*0Sstevel@tonic-gate 	PUT_32BIT(b, seqno);
92*0Sstevel@tonic-gate 	HMAC_Update(&c, b, sizeof(b));
93*0Sstevel@tonic-gate 	HMAC_Update(&c, data, datalen);
94*0Sstevel@tonic-gate 	HMAC_Final(&c, m, NULL);
95*0Sstevel@tonic-gate 	HMAC_cleanup(&c);
96*0Sstevel@tonic-gate 	return (m);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* XXX copied from ciphers_valid */
100*0Sstevel@tonic-gate #define	MAC_SEP	","
101*0Sstevel@tonic-gate int
102*0Sstevel@tonic-gate mac_valid(const char *names)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	char *maclist, *cp, *p;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	if (names == NULL || strcmp(names, "") == 0)
107*0Sstevel@tonic-gate 		return (0);
108*0Sstevel@tonic-gate 	maclist = cp = xstrdup(names);
109*0Sstevel@tonic-gate 	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
110*0Sstevel@tonic-gate 	    (p = strsep(&cp, MAC_SEP))) {
111*0Sstevel@tonic-gate 		if (mac_init(NULL, p) < 0) {
112*0Sstevel@tonic-gate 			debug("bad mac %s [%s]", p, names);
113*0Sstevel@tonic-gate 			xfree(maclist);
114*0Sstevel@tonic-gate 			return (0);
115*0Sstevel@tonic-gate 		} else {
116*0Sstevel@tonic-gate 			debug3("mac ok: %s [%s]", p, names);
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 	debug3("macs ok: [%s]", names);
120*0Sstevel@tonic-gate 	xfree(maclist);
121*0Sstevel@tonic-gate 	return (1);
122*0Sstevel@tonic-gate }
123