1/* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen Exp $ */
2/*
3 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
27
28#include <sys/types.h>
29
30#ifdef __APPLE_CRYPTO__
31#include "ossl-hmac.h"
32#else
33#include <openssl/hmac.h>
34#endif
35
36#include <stdarg.h>
37#include <string.h>
38#include <signal.h>
39
40#include "xmalloc.h"
41#include "log.h"
42#include "cipher.h"
43#include "buffer.h"
44#include "key.h"
45#include "kex.h"
46#include "mac.h"
47#include "misc.h"
48
49#include "umac.h"
50
51#include "openbsd-compat/openssl-compat.h"
52
53#define SSH_EVP		1	/* OpenSSL EVP-based MAC */
54#define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
55#define SSH_UMAC128	3
56
57struct {
58	char		*name;
59	int		type;
60	const EVP_MD *	(*mdfunc)(void);
61	int		truncatebits;	/* truncate digest if != 0 */
62	int		key_len;	/* just for UMAC */
63	int		len;		/* just for UMAC */
64	int		etm;		/* Encrypt-then-MAC */
65} macs[] = {
66	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
67	{ "hmac-sha1",				SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
68	{ "hmac-sha1-96",			SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
69#ifdef HAVE_EVP_SHA256
70	{ "hmac-sha2-256",			SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
71	{ "hmac-sha2-512",			SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
72#endif
73	{ "hmac-md5",				SSH_EVP, EVP_md5, 0, 0, 0, 0 },
74	{ "hmac-md5-96",			SSH_EVP, EVP_md5, 96, 0, 0, 0 },
75	{ "hmac-ripemd160",			SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
76	{ "hmac-ripemd160@openssh.com",		SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
77	{ "umac-64@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 0 },
78	{ "umac-128@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 0 },
79
80	/* Encrypt-then-MAC variants */
81	{ "hmac-sha1-etm@openssh.com",		SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
82	{ "hmac-sha1-96-etm@openssh.com",	SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
83#ifdef HAVE_EVP_SHA256
84	{ "hmac-sha2-256-etm@openssh.com",	SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
85	{ "hmac-sha2-512-etm@openssh.com",	SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
86#endif
87	{ "hmac-md5-etm@openssh.com",		SSH_EVP, EVP_md5, 0, 0, 0, 1 },
88	{ "hmac-md5-96-etm@openssh.com",	SSH_EVP, EVP_md5, 96, 0, 0, 1 },
89	{ "hmac-ripemd160-etm@openssh.com",	SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
90	{ "umac-64-etm@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 1 },
91	{ "umac-128-etm@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 1 },
92
93	{ NULL,					0, NULL, 0, 0, 0, 0 }
94};
95
96static void
97mac_setup_by_id(Mac *mac, int which)
98{
99	int evp_len;
100	mac->type = macs[which].type;
101	if (mac->type == SSH_EVP) {
102		mac->evp_md = (*macs[which].mdfunc)();
103		if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
104			fatal("mac %s len %d", mac->name, evp_len);
105		mac->key_len = mac->mac_len = (u_int)evp_len;
106	} else {
107		mac->mac_len = macs[which].len / 8;
108		mac->key_len = macs[which].key_len / 8;
109		mac->umac_ctx = NULL;
110	}
111	if (macs[which].truncatebits != 0)
112		mac->mac_len = macs[which].truncatebits / 8;
113	mac->etm = macs[which].etm;
114}
115
116int
117mac_setup(Mac *mac, char *name)
118{
119	int i;
120
121	for (i = 0; macs[i].name; i++) {
122		if (strcmp(name, macs[i].name) == 0) {
123			if (mac != NULL)
124				mac_setup_by_id(mac, i);
125			debug2("mac_setup: found %s", name);
126			return (0);
127		}
128	}
129	debug2("mac_setup: unknown %s", name);
130	return (-1);
131}
132
133int
134mac_init(Mac *mac)
135{
136	if (mac->key == NULL)
137		fatal("mac_init: no key");
138	switch (mac->type) {
139	case SSH_EVP:
140		if (mac->evp_md == NULL)
141			return -1;
142		HMAC_CTX_init(&mac->evp_ctx);
143		HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
144		return 0;
145	case SSH_UMAC:
146		mac->umac_ctx = umac_new(mac->key);
147		return 0;
148	case SSH_UMAC128:
149		mac->umac_ctx = umac128_new(mac->key);
150		return 0;
151	default:
152		return -1;
153	}
154}
155
156u_char *
157mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
158{
159	static u_char m[EVP_MAX_MD_SIZE];
160	u_char b[4], nonce[8];
161
162	if (mac->mac_len > sizeof(m))
163		fatal("mac_compute: mac too long %u %lu",
164		    mac->mac_len, (u_long)sizeof(m));
165
166	switch (mac->type) {
167	case SSH_EVP:
168		put_u32(b, seqno);
169		/* reset HMAC context */
170		HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
171		HMAC_Update(&mac->evp_ctx, b, sizeof(b));
172		HMAC_Update(&mac->evp_ctx, data, datalen);
173		HMAC_Final(&mac->evp_ctx, m, NULL);
174		break;
175	case SSH_UMAC:
176		put_u64(nonce, seqno);
177		umac_update(mac->umac_ctx, data, datalen);
178		umac_final(mac->umac_ctx, m, nonce);
179		break;
180	case SSH_UMAC128:
181		put_u64(nonce, seqno);
182		umac128_update(mac->umac_ctx, data, datalen);
183		umac128_final(mac->umac_ctx, m, nonce);
184		break;
185	default:
186		fatal("mac_compute: unknown MAC type");
187	}
188	return (m);
189}
190
191void
192mac_clear(Mac *mac)
193{
194	if (mac->type == SSH_UMAC) {
195		if (mac->umac_ctx != NULL)
196			umac_delete(mac->umac_ctx);
197	} else if (mac->type == SSH_UMAC128) {
198		if (mac->umac_ctx != NULL)
199			umac128_delete(mac->umac_ctx);
200	} else if (mac->evp_md != NULL)
201		HMAC_cleanup(&mac->evp_ctx);
202	mac->evp_md = NULL;
203	mac->umac_ctx = NULL;
204}
205
206/* XXX copied from ciphers_valid */
207#define	MAC_SEP	","
208int
209mac_valid(const char *names)
210{
211	char *maclist, *cp, *p;
212
213	if (names == NULL || strcmp(names, "") == 0)
214		return (0);
215	maclist = cp = xstrdup(names);
216	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
217	    (p = strsep(&cp, MAC_SEP))) {
218		if (mac_setup(NULL, p) < 0) {
219			debug("bad mac %s [%s]", p, names);
220			xfree(maclist);
221			return (0);
222		} else {
223			debug3("mac ok: %s [%s]", p, names);
224		}
225	}
226	debug3("macs ok: [%s]", names);
227	xfree(maclist);
228	return (1);
229}
230