mac.c revision 262566
15897Sjmz/* $OpenBSD: mac.c,v 1.26 2014/01/04 17:50:55 tedu Exp $ */
25897Sjmz/*
35897Sjmz * Copyright (c) 2001 Markus Friedl.  All rights reserved.
45897Sjmz *
55897Sjmz * Redistribution and use in source and binary forms, with or without
65897Sjmz * modification, are permitted provided that the following conditions
75897Sjmz * are met:
85897Sjmz * 1. Redistributions of source code must retain the above copyright
95897Sjmz *    notice, this list of conditions and the following disclaimer.
105897Sjmz * 2. Redistributions in binary form must reproduce the above copyright
115897Sjmz *    notice, this list of conditions and the following disclaimer in the
125897Sjmz *    documentation and/or other materials provided with the distribution.
135897Sjmz *
145897Sjmz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
155897Sjmz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
165897Sjmz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175897Sjmz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
185897Sjmz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
195897Sjmz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205897Sjmz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
215897Sjmz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225897Sjmz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
235897Sjmz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245897Sjmz */
255897Sjmz
265897Sjmz#include "includes.h"
275897Sjmz
285897Sjmz#include <sys/types.h>
295897Sjmz
305897Sjmz#include <openssl/hmac.h>
315897Sjmz
325897Sjmz#include <stdarg.h>
335897Sjmz#include <string.h>
346734Sbde#include <signal.h>
357430Sbde
367430Sbde#include "xmalloc.h"
376734Sbde#include "log.h"
386734Sbde#include "cipher.h"
396734Sbde#include "buffer.h"
405897Sjmz#include "key.h"
416734Sbde#include "kex.h"
425897Sjmz#include "mac.h"
435897Sjmz#include "misc.h"
445897Sjmz
455897Sjmz#include "umac.h"
465897Sjmz
475897Sjmz#include "openbsd-compat/openssl-compat.h"
485897Sjmz
495897Sjmz#define SSH_EVP		1	/* OpenSSL EVP-based MAC */
505897Sjmz#define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
515897Sjmz#define SSH_UMAC128	3
525897Sjmz
535897Sjmzstruct macalg {
545897Sjmz	char		*name;
555993Sjmz	int		type;
565993Sjmz	const EVP_MD *	(*mdfunc)(void);
575998Sjmz	int		truncatebits;	/* truncate digest if != 0 */
585998Sjmz	int		key_len;	/* just for UMAC */
595897Sjmz	int		len;		/* just for UMAC */
605897Sjmz	int		etm;		/* Encrypt-then-MAC */
615897Sjmz};
625897Sjmz
635897Sjmzstatic const struct macalg macs[] = {
645897Sjmz	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
655897Sjmz	{ "hmac-sha1",				SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
665897Sjmz	{ "hmac-sha1-96",			SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
675897Sjmz#ifdef HAVE_EVP_SHA256
685897Sjmz	{ "hmac-sha2-256",			SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
695897Sjmz	{ "hmac-sha2-512",			SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
705897Sjmz#endif
715897Sjmz	{ "hmac-md5",				SSH_EVP, EVP_md5, 0, 0, 0, 0 },
725897Sjmz	{ "hmac-md5-96",			SSH_EVP, EVP_md5, 96, 0, 0, 0 },
735897Sjmz	{ "hmac-ripemd160",			SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
745993Sjmz	{ "hmac-ripemd160@openssh.com",		SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
755897Sjmz	{ "umac-64@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 0 },
765897Sjmz	{ "umac-128@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 0 },
775897Sjmz
785897Sjmz	/* Encrypt-then-MAC variants */
795897Sjmz	{ "hmac-sha1-etm@openssh.com",		SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
805897Sjmz	{ "hmac-sha1-96-etm@openssh.com",	SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
815897Sjmz#ifdef HAVE_EVP_SHA256
825897Sjmz	{ "hmac-sha2-256-etm@openssh.com",	SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
835897Sjmz	{ "hmac-sha2-512-etm@openssh.com",	SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
845897Sjmz#endif
855897Sjmz	{ "hmac-md5-etm@openssh.com",		SSH_EVP, EVP_md5, 0, 0, 0, 1 },
865897Sjmz	{ "hmac-md5-96-etm@openssh.com",	SSH_EVP, EVP_md5, 96, 0, 0, 1 },
875897Sjmz	{ "hmac-ripemd160-etm@openssh.com",	SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
885897Sjmz	{ "umac-64-etm@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 1 },
895897Sjmz	{ "umac-128-etm@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 1 },
905897Sjmz
915897Sjmz	{ NULL,					0, NULL, 0, 0, 0, 0 }
925897Sjmz};
935897Sjmz
945897Sjmz/* Returns a list of supported MACs separated by the specified char. */
955897Sjmzchar *
965897Sjmzmac_alg_list(char sep)
975897Sjmz{
985897Sjmz	char *ret = NULL;
995897Sjmz	size_t nlen, rlen = 0;
1005897Sjmz	const struct macalg *m;
1015897Sjmz
1025897Sjmz	for (m = macs; m->name != NULL; m++) {
1035897Sjmz		if (ret != NULL)
1045897Sjmz			ret[rlen++] = sep;
1055897Sjmz		nlen = strlen(m->name);
1065897Sjmz		ret = xrealloc(ret, 1, rlen + nlen + 2);
1075897Sjmz		memcpy(ret + rlen, m->name, nlen + 1);
1085897Sjmz		rlen += nlen;
1095897Sjmz	}
1105897Sjmz	return ret;
1115897Sjmz}
1125897Sjmz
1135897Sjmzstatic void
1145897Sjmzmac_setup_by_alg(Mac *mac, const struct macalg *macalg)
1155897Sjmz{
1165897Sjmz	int evp_len;
1175897Sjmz
1185897Sjmz	mac->type = macalg->type;
1195897Sjmz	if (mac->type == SSH_EVP) {
1205897Sjmz		mac->evp_md = macalg->mdfunc();
1215897Sjmz		if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
1225897Sjmz			fatal("mac %s len %d", mac->name, evp_len);
1235897Sjmz		mac->key_len = mac->mac_len = (u_int)evp_len;
1245897Sjmz	} else {
1255897Sjmz		mac->mac_len = macalg->len / 8;
1265897Sjmz		mac->key_len = macalg->key_len / 8;
1275897Sjmz		mac->umac_ctx = NULL;
1285897Sjmz	}
1295897Sjmz	if (macalg->truncatebits != 0)
1305897Sjmz		mac->mac_len = macalg->truncatebits / 8;
1315897Sjmz	mac->etm = macalg->etm;
1325897Sjmz}
1335897Sjmz
1346644Sjmzint
1355897Sjmzmac_setup(Mac *mac, char *name)
1365897Sjmz{
1375897Sjmz	const struct macalg *m;
1385897Sjmz
1395897Sjmz	for (m = macs; m->name != NULL; m++) {
1405897Sjmz		if (strcmp(name, m->name) != 0)
1415897Sjmz			continue;
1425897Sjmz		if (mac != NULL)
1435897Sjmz			mac_setup_by_alg(mac, m);
1445897Sjmz		debug2("mac_setup: found %s", name);
1455897Sjmz		return (0);
1465897Sjmz	}
1475993Sjmz	debug2("mac_setup: unknown %s", name);
1485897Sjmz	return (-1);
1495897Sjmz}
1505897Sjmz
1515897Sjmzint
1525897Sjmzmac_init(Mac *mac)
1535897Sjmz{
1545897Sjmz	if (mac->key == NULL)
1555897Sjmz		fatal("mac_init: no key");
1566644Sjmz	switch (mac->type) {
1576644Sjmz	case SSH_EVP:
1585897Sjmz		if (mac->evp_md == NULL)
1596644Sjmz			return -1;
1606644Sjmz		HMAC_CTX_init(&mac->evp_ctx);
1617430Sbde		HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
1625897Sjmz		return 0;
1635897Sjmz	case SSH_UMAC:
1645897Sjmz		mac->umac_ctx = umac_new(mac->key);
1655897Sjmz		return 0;
1665897Sjmz	case SSH_UMAC128:
1675897Sjmz		mac->umac_ctx = umac128_new(mac->key);
1685897Sjmz		return 0;
1695897Sjmz	default:
1705897Sjmz		return -1;
1715897Sjmz	}
1725897Sjmz}
1735897Sjmz
1745897Sjmzu_char *
1755897Sjmzmac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
1765897Sjmz{
1775897Sjmz	static union {
1785897Sjmz		u_char m[EVP_MAX_MD_SIZE];
1795897Sjmz		u_int64_t for_align;
1805897Sjmz	} u;
1815897Sjmz	u_char b[4], nonce[8];
1825897Sjmz
1835897Sjmz	if (mac->mac_len > sizeof(u))
1845897Sjmz		fatal("mac_compute: mac too long %u %zu",
1855897Sjmz		    mac->mac_len, sizeof(u));
1865897Sjmz
1875897Sjmz	switch (mac->type) {
1885897Sjmz	case SSH_EVP:
1895897Sjmz		put_u32(b, seqno);
1905897Sjmz		/* reset HMAC context */
1915897Sjmz		HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
1925897Sjmz		HMAC_Update(&mac->evp_ctx, b, sizeof(b));
1935897Sjmz		HMAC_Update(&mac->evp_ctx, data, datalen);
1945897Sjmz		HMAC_Final(&mac->evp_ctx, u.m, NULL);
1955897Sjmz		break;
1965897Sjmz	case SSH_UMAC:
1975897Sjmz		put_u64(nonce, seqno);
1985897Sjmz		umac_update(mac->umac_ctx, data, datalen);
1995897Sjmz		umac_final(mac->umac_ctx, u.m, nonce);
2005897Sjmz		break;
2015897Sjmz	case SSH_UMAC128:
2025897Sjmz		put_u64(nonce, seqno);
2035897Sjmz		umac128_update(mac->umac_ctx, data, datalen);
2045897Sjmz		umac128_final(mac->umac_ctx, u.m, nonce);
2055897Sjmz		break;
2065897Sjmz	default:
2075897Sjmz		fatal("mac_compute: unknown MAC type");
2085897Sjmz	}
209	return (u.m);
210}
211
212void
213mac_clear(Mac *mac)
214{
215	if (mac->type == SSH_UMAC) {
216		if (mac->umac_ctx != NULL)
217			umac_delete(mac->umac_ctx);
218	} else if (mac->type == SSH_UMAC128) {
219		if (mac->umac_ctx != NULL)
220			umac128_delete(mac->umac_ctx);
221	} else if (mac->evp_md != NULL)
222		HMAC_cleanup(&mac->evp_ctx);
223	mac->evp_md = NULL;
224	mac->umac_ctx = NULL;
225}
226
227/* XXX copied from ciphers_valid */
228#define	MAC_SEP	","
229int
230mac_valid(const char *names)
231{
232	char *maclist, *cp, *p;
233
234	if (names == NULL || strcmp(names, "") == 0)
235		return (0);
236	maclist = cp = xstrdup(names);
237	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
238	    (p = strsep(&cp, MAC_SEP))) {
239		if (mac_setup(NULL, p) < 0) {
240			debug("bad mac %s [%s]", p, names);
241			free(maclist);
242			return (0);
243		} else {
244			debug3("mac ok: %s [%s]", p, names);
245		}
246	}
247	debug3("macs ok: [%s]", names);
248	free(maclist);
249	return (1);
250}
251