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