mac.c revision 248619
177957Sbenno/* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen Exp $ */
277957Sbenno/*
377957Sbenno * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4139825Simp *
577957Sbenno * Redistribution and use in source and binary forms, with or without
677957Sbenno * modification, are permitted provided that the following conditions
777957Sbenno * are met:
877957Sbenno * 1. Redistributions of source code must retain the above copyright
977957Sbenno *    notice, this list of conditions and the following disclaimer.
1077957Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1177957Sbenno *    notice, this list of conditions and the following disclaimer in the
1277957Sbenno *    documentation and/or other materials provided with the distribution.
1377957Sbenno *
1477957Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1577957Sbenno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1677957Sbenno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1777957Sbenno * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1877957Sbenno * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1977957Sbenno * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2077957Sbenno * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2177957Sbenno * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2277957Sbenno * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2377957Sbenno * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2477957Sbenno */
2577957Sbenno
2677957Sbenno#include "includes.h"
2777957Sbenno
28139825Simp#include <sys/types.h>
2977957Sbenno
3077957Sbenno#include <openssl/hmac.h>
3177957Sbenno
3277957Sbenno#include <stdarg.h>
3377957Sbenno#include <string.h>
3477957Sbenno#include <signal.h>
3577957Sbenno
3677957Sbenno#include "xmalloc.h"
3777957Sbenno#include "log.h"
3877957Sbenno#include "cipher.h"
3977957Sbenno#include "buffer.h"
4077957Sbenno#include "key.h"
4177957Sbenno#include "kex.h"
4277957Sbenno#include "mac.h"
4377957Sbenno#include "misc.h"
4477957Sbenno
4577957Sbenno#include "umac.h"
4677957Sbenno
4777957Sbenno#include "openbsd-compat/openssl-compat.h"
4877957Sbenno
4977957Sbenno#define SSH_EVP		1	/* OpenSSL EVP-based MAC */
5077957Sbenno#define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
5177957Sbenno#define SSH_UMAC128	3
5277957Sbenno
5377957Sbennostruct {
5477957Sbenno	char		*name;
5577957Sbenno	int		type;
5677957Sbenno	const EVP_MD *	(*mdfunc)(void);
5777957Sbenno	int		truncatebits;	/* truncate digest if != 0 */
5877957Sbenno	int		key_len;	/* just for UMAC */
5977957Sbenno	int		len;		/* just for UMAC */
60198723Snwhitehorn	int		etm;		/* Encrypt-then-MAC */
6177957Sbenno} macs[] = {
6277957Sbenno	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
6377957Sbenno	{ "hmac-sha1",				SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
6477957Sbenno	{ "hmac-sha1-96",			SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
6577957Sbenno#ifdef HAVE_EVP_SHA256
6677957Sbenno	{ "hmac-sha2-256",			SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
6777957Sbenno	{ "hmac-sha2-512",			SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
6877957Sbenno#endif
69178628Smarcel	{ "hmac-md5",				SSH_EVP, EVP_md5, 0, 0, 0, 0 },
70178628Smarcel	{ "hmac-md5-96",			SSH_EVP, EVP_md5, 96, 0, 0, 0 },
71231019Sandreast	{ "hmac-ripemd160",			SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
72223485Snwhitehorn	{ "hmac-ripemd160@openssh.com",		SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
73178628Smarcel	{ "umac-64@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 0 },
74178628Smarcel	{ "umac-128@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 0 },
75178628Smarcel
76172887Sgrehan	/* Encrypt-then-MAC variants */
77172887Sgrehan	{ "hmac-sha1-etm@openssh.com",		SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
78172887Sgrehan	{ "hmac-sha1-96-etm@openssh.com",	SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
79118893Sgrehan#ifdef HAVE_EVP_SHA256
80118893Sgrehan	{ "hmac-sha2-256-etm@openssh.com",	SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
8177957Sbenno	{ "hmac-sha2-512-etm@openssh.com",	SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
82231019Sandreast#endif
83209975Snwhitehorn	{ "hmac-md5-etm@openssh.com",		SSH_EVP, EVP_md5, 0, 0, 0, 1 },
84209975Snwhitehorn	{ "hmac-md5-96-etm@openssh.com",	SSH_EVP, EVP_md5, 96, 0, 0, 1 },
85118893Sgrehan	{ "hmac-ripemd160-etm@openssh.com",	SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
86209975Snwhitehorn	{ "umac-64-etm@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 1 },
87209975Snwhitehorn	{ "umac-128-etm@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 1 },
88209975Snwhitehorn
89209975Snwhitehorn	{ NULL,					0, NULL, 0, 0, 0, 0 }
90209975Snwhitehorn};
91209975Snwhitehorn
92209975Snwhitehornstatic void
93209975Snwhitehornmac_setup_by_id(Mac *mac, int which)
94209975Snwhitehorn{
95209975Snwhitehorn	int evp_len;
96209975Snwhitehorn	mac->type = macs[which].type;
97209975Snwhitehorn	if (mac->type == SSH_EVP) {
98209975Snwhitehorn		mac->evp_md = (*macs[which].mdfunc)();
99209975Snwhitehorn		if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
100209975Snwhitehorn			fatal("mac %s len %d", mac->name, evp_len);
101209975Snwhitehorn		mac->key_len = mac->mac_len = (u_int)evp_len;
102209975Snwhitehorn	} else {
103209975Snwhitehorn		mac->mac_len = macs[which].len / 8;
104118893Sgrehan		mac->key_len = macs[which].key_len / 8;
10591486Sbenno		mac->umac_ctx = NULL;
106209975Snwhitehorn	}
10791486Sbenno	if (macs[which].truncatebits != 0)
108209975Snwhitehorn		mac->mac_len = macs[which].truncatebits / 8;
109209975Snwhitehorn	mac->etm = macs[which].etm;
110209975Snwhitehorn}
111209975Snwhitehorn
112118893Sgrehanint
113223485Snwhitehornmac_setup(Mac *mac, char *name)
114198723Snwhitehorn{
115198731Snwhitehorn	int i;
116118893Sgrehan
117209975Snwhitehorn	for (i = 0; macs[i].name; i++) {
118209975Snwhitehorn		if (strcmp(name, macs[i].name) == 0) {
119198731Snwhitehorn			if (mac != NULL)
120178628Smarcel				mac_setup_by_id(mac, i);
121198731Snwhitehorn			debug2("mac_setup: found %s", name);
12286066Smp			return (0);
123218824Snwhitehorn		}
124209975Snwhitehorn	}
125188860Snwhitehorn	debug2("mac_setup: unknown %s", name);
126188860Snwhitehorn	return (-1);
127198731Snwhitehorn}
128198731Snwhitehorn
129188860Snwhitehornint
130198731Snwhitehornmac_init(Mac *mac)
131188860Snwhitehorn{
132218824Snwhitehorn	if (mac->key == NULL)
133209975Snwhitehorn		fatal("mac_init: no key");
134188860Snwhitehorn	switch (mac->type) {
135188860Snwhitehorn	case SSH_EVP:
136118893Sgrehan		if (mac->evp_md == NULL)
137218824Snwhitehorn			return -1;
138209975Snwhitehorn		HMAC_CTX_init(&mac->evp_ctx);
13991486Sbenno		HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
140209975Snwhitehorn		return 0;
141198723Snwhitehorn	case SSH_UMAC:
142234517Snwhitehorn		mac->umac_ctx = umac_new(mac->key);
143209975Snwhitehorn		return 0;
144209975Snwhitehorn	case SSH_UMAC128:
145178628Smarcel		mac->umac_ctx = umac128_new(mac->key);
146198723Snwhitehorn		return 0;
147198723Snwhitehorn	default:
148198723Snwhitehorn		return -1;
149198723Snwhitehorn	}
150198723Snwhitehorn}
151223485Snwhitehorn
152209975Snwhitehornu_char *
153235013Snwhitehornmac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
154235013Snwhitehorn{
155198723Snwhitehorn	static u_char m[EVP_MAX_MD_SIZE];
156198723Snwhitehorn	u_char b[4], nonce[8];
157118893Sgrehan
158223485Snwhitehorn	if (mac->mac_len > sizeof(m))
159223485Snwhitehorn		fatal("mac_compute: mac too long %u %lu",
160209975Snwhitehorn		    mac->mac_len, (u_long)sizeof(m));
16177957Sbenno
162209975Snwhitehorn	switch (mac->type) {
163209975Snwhitehorn	case SSH_EVP:
164223485Snwhitehorn		put_u32(b, seqno);
165218824Snwhitehorn		/* reset HMAC context */
166209975Snwhitehorn		HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
167183088Smarcel		HMAC_Update(&mac->evp_ctx, b, sizeof(b));
168178628Smarcel		HMAC_Update(&mac->evp_ctx, data, datalen);
169178628Smarcel		HMAC_Final(&mac->evp_ctx, m, NULL);
170118893Sgrehan		break;
171188860Snwhitehorn	case SSH_UMAC:
172223485Snwhitehorn		put_u64(nonce, seqno);
173218824Snwhitehorn		umac_update(mac->umac_ctx, data, datalen);
174209975Snwhitehorn		umac_final(mac->umac_ctx, m, nonce);
17586066Smp		break;
176188860Snwhitehorn	case SSH_UMAC128:
177188860Snwhitehorn		put_u64(nonce, seqno);
178188860Snwhitehorn		umac128_update(mac->umac_ctx, data, datalen);
179188860Snwhitehorn		umac128_final(mac->umac_ctx, m, nonce);
180188860Snwhitehorn		break;
181223485Snwhitehorn	default:
182218824Snwhitehorn		fatal("mac_compute: unknown MAC type");
183209975Snwhitehorn	}
184188860Snwhitehorn	return (m);
18599036Sbenno}
186188860Snwhitehorn
187209975Snwhitehornvoid
188118893Sgrehanmac_clear(Mac *mac)
189209975Snwhitehorn{
190209975Snwhitehorn	if (mac->type == SSH_UMAC) {
191209975Snwhitehorn		if (mac->umac_ctx != NULL)
192209975Snwhitehorn			umac_delete(mac->umac_ctx);
193209975Snwhitehorn	} else if (mac->type == SSH_UMAC128) {
194209975Snwhitehorn		if (mac->umac_ctx != NULL)
195209975Snwhitehorn			umac128_delete(mac->umac_ctx);
196209975Snwhitehorn	} else if (mac->evp_md != NULL)
197209975Snwhitehorn		HMAC_cleanup(&mac->evp_ctx);
198209975Snwhitehorn	mac->evp_md = NULL;
199209975Snwhitehorn	mac->umac_ctx = NULL;
200209975Snwhitehorn}
201209975Snwhitehorn
202209975Snwhitehorn/* XXX copied from ciphers_valid */
203209975Snwhitehorn#define	MAC_SEP	","
204209975Snwhitehornint
205209975Snwhitehornmac_valid(const char *names)
206209975Snwhitehorn{
207209975Snwhitehorn	char *maclist, *cp, *p;
208209975Snwhitehorn
20984945Smp	if (names == NULL || strcmp(names, "") == 0)
210209975Snwhitehorn		return (0);
21191486Sbenno	maclist = cp = xstrdup(names);
212209975Snwhitehorn	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
213209975Snwhitehorn	    (p = strsep(&cp, MAC_SEP))) {
214209975Snwhitehorn		if (mac_setup(NULL, p) < 0) {
215214574Snwhitehorn			debug("bad mac %s [%s]", p, names);
216209975Snwhitehorn			xfree(maclist);
217209975Snwhitehorn			return (0);
218209975Snwhitehorn		} else {
219214607Snwhitehorn			debug3("mac ok: %s [%s]", p, names);
220209975Snwhitehorn		}
221214574Snwhitehorn	}
222214574Snwhitehorn	debug3("macs ok: [%s]", names);
223214574Snwhitehorn	xfree(maclist);
224214574Snwhitehorn	return (1);
225214574Snwhitehorn}
226209975Snwhitehorn