mac.c revision 181097
1/* $OpenBSD: mac.c,v 1.12 2006/08/03 03:34:42 deraadt 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#include <openssl/hmac.h>
31
32#include <stdarg.h>
33#include <string.h>
34#include <signal.h>
35
36#include "xmalloc.h"
37#include "log.h"
38#include "cipher.h"
39#include "buffer.h"
40#include "key.h"
41#include "kex.h"
42#include "mac.h"
43#include "misc.h"
44
45struct {
46	char		*name;
47	const EVP_MD *	(*mdfunc)(void);
48	int		truncatebits;	/* truncate digest if != 0 */
49} macs[] = {
50	{ "hmac-sha1",			EVP_sha1, 0, },
51	{ "hmac-sha1-96",		EVP_sha1, 96 },
52	{ "hmac-md5",			EVP_md5, 0 },
53	{ "hmac-md5-96",		EVP_md5, 96 },
54	{ "hmac-ripemd160",		EVP_ripemd160, 0 },
55	{ "hmac-ripemd160@openssh.com",	EVP_ripemd160, 0 },
56	{ NULL,				NULL, 0 }
57};
58
59int
60mac_init(Mac *mac, char *name)
61{
62	int i, evp_len;
63
64	for (i = 0; macs[i].name; i++) {
65		if (strcmp(name, macs[i].name) == 0) {
66			if (mac != NULL) {
67				mac->md = (*macs[i].mdfunc)();
68				if ((evp_len = EVP_MD_size(mac->md)) <= 0)
69					fatal("mac %s len %d", name, evp_len);
70				mac->key_len = mac->mac_len = (u_int)evp_len;
71				if (macs[i].truncatebits != 0)
72					mac->mac_len = macs[i].truncatebits/8;
73			}
74			debug2("mac_init: found %s", name);
75			return (0);
76		}
77	}
78	debug2("mac_init: unknown %s", name);
79	return (-1);
80}
81
82u_char *
83mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
84{
85	HMAC_CTX c;
86	static u_char m[EVP_MAX_MD_SIZE];
87	u_char b[4];
88
89	if (mac->key == NULL)
90		fatal("mac_compute: no key");
91	if (mac->mac_len > sizeof(m))
92		fatal("mac_compute: mac too long");
93	HMAC_Init(&c, mac->key, mac->key_len, mac->md);
94	put_u32(b, seqno);
95	HMAC_Update(&c, b, sizeof(b));
96	HMAC_Update(&c, data, datalen);
97	HMAC_Final(&c, m, NULL);
98	HMAC_cleanup(&c);
99	return (m);
100}
101
102/* XXX copied from ciphers_valid */
103#define	MAC_SEP	","
104int
105mac_valid(const char *names)
106{
107	char *maclist, *cp, *p;
108
109	if (names == NULL || strcmp(names, "") == 0)
110		return (0);
111	maclist = cp = xstrdup(names);
112	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
113	    (p = strsep(&cp, MAC_SEP))) {
114		if (mac_init(NULL, p) < 0) {
115			debug("bad mac %s [%s]", p, names);
116			xfree(maclist);
117			return (0);
118		} else {
119			debug3("mac ok: %s [%s]", p, names);
120		}
121	}
122	debug3("macs ok: [%s]", names);
123	xfree(maclist);
124	return (1);
125}
126