xref: /netbsd-src/crypto/external/bsd/openssh/dist/mac.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: mac.c,v 1.7 2013/03/29 16:19:45 christos Exp $	*/
2 /* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen 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 "includes.h"
28 __RCSID("$NetBSD: mac.c,v 1.7 2013/03/29 16:19:45 christos Exp $");
29 #include <sys/types.h>
30 
31 #include <openssl/hmac.h>
32 
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 
45 #ifdef UMAC_HAS_BEEN_UNBROKEN
46 #include "umac.h"
47 #endif
48 
49 #define SSH_EVP		1	/* OpenSSL EVP-based MAC */
50 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
51 #define SSH_UMAC128	3
52 
53 struct {
54 	const char	*name;
55 	int		type;
56 	const EVP_MD *	(*mdfunc)(void);
57 	int		truncatebits;	/* truncate digest if != 0 */
58 	int		key_len;	/* just for UMAC */
59 	int		len;		/* just for UMAC */
60 	int		etm;		/* Encrypt-then-MAC */
61 } macs[] = {
62 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
63 	{ "hmac-sha1",				SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
64 	{ "hmac-sha1-96",			SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
65 	{ "hmac-sha2-256",			SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
66 	{ "hmac-sha2-512",			SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
67 	{ "hmac-md5",				SSH_EVP, EVP_md5, 0, 0, 0, 0 },
68 	{ "hmac-md5-96",			SSH_EVP, EVP_md5, 96, 0, 0, 0 },
69 	{ "hmac-ripemd160",			SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
70 	{ "hmac-ripemd160@openssh.com",		SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
71 #ifdef UMAC_HAS_BEEN_UNBROKEN
72 	{ "umac-64@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 0 },
73 	{ "umac-128@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 0 },
74 #endif
75 
76 	/* Encrypt-then-MAC variants */
77 	{ "hmac-sha1-etm@openssh.com",		SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
78 	{ "hmac-sha1-96-etm@openssh.com",	SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
79 	{ "hmac-sha2-256-etm@openssh.com",	SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
80 	{ "hmac-sha2-512-etm@openssh.com",	SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
81 	{ "hmac-md5-etm@openssh.com",		SSH_EVP, EVP_md5, 0, 0, 0, 1 },
82 	{ "hmac-md5-96-etm@openssh.com",	SSH_EVP, EVP_md5, 96, 0, 0, 1 },
83 	{ "hmac-ripemd160-etm@openssh.com",	SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
84 	{ "umac-64-etm@openssh.com",		SSH_UMAC, NULL, 0, 128, 64, 1 },
85 	{ "umac-128-etm@openssh.com",		SSH_UMAC128, NULL, 0, 128, 128, 1 },
86 
87 	{ NULL,					0, NULL, 0, 0, 0, 0 }
88 };
89 
90 static void
91 mac_setup_by_id(Mac *mac, int which)
92 {
93 	int evp_len;
94 	mac->type = macs[which].type;
95 	if (mac->type == SSH_EVP) {
96 		mac->evp_md = (*macs[which].mdfunc)();
97 		if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
98 			fatal("mac %s len %d", mac->name, evp_len);
99 		mac->key_len = mac->mac_len = (u_int)evp_len;
100 	} else {
101 		mac->mac_len = macs[which].len / 8;
102 		mac->key_len = macs[which].key_len / 8;
103 		mac->umac_ctx = NULL;
104 	}
105 	if (macs[which].truncatebits != 0)
106 		mac->mac_len = macs[which].truncatebits / 8;
107 	mac->etm = macs[which].etm;
108 }
109 
110 int
111 mac_setup(Mac *mac, char *name)
112 {
113 	int i;
114 
115 	for (i = 0; macs[i].name; i++) {
116 		if (strcmp(name, macs[i].name) == 0) {
117 			if (mac != NULL)
118 				mac_setup_by_id(mac, i);
119 			debug2("mac_setup: found %s", name);
120 			return (0);
121 		}
122 	}
123 	debug2("mac_setup: unknown %s", name);
124 	return (-1);
125 }
126 
127 int
128 mac_init(Mac *mac)
129 {
130 	if (mac->key == NULL)
131 		fatal("mac_init: no key");
132 	switch (mac->type) {
133 	case SSH_EVP:
134 		if (mac->evp_md == NULL)
135 			return -1;
136 		HMAC_CTX_init(&mac->evp_ctx);
137 		HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
138 		return 0;
139 #ifdef UMAC_HAS_BEEN_UNBROKEN
140 	case SSH_UMAC:
141 		mac->umac_ctx = umac_new(mac->key);
142 		return 0;
143 	case SSH_UMAC128:
144 		mac->umac_ctx = umac128_new(mac->key);
145 		return 0;
146 #endif
147 	default:
148 		return -1;
149 	}
150 }
151 
152 u_char *
153 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
154 {
155 	static u_char m[EVP_MAX_MD_SIZE];
156 	u_char b[4];
157 #ifdef UMAC_HAS_BEEN_UNBROKEN
158 	u_char nonce[8];
159 #endif
160 
161 	if (mac->mac_len > sizeof(m))
162 		fatal("mac_compute: mac too long %u %lu",
163 		    mac->mac_len, (u_long)sizeof(m));
164 
165 	switch (mac->type) {
166 	case SSH_EVP:
167 		put_u32(b, seqno);
168 		/* reset HMAC context */
169 		HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
170 		HMAC_Update(&mac->evp_ctx, b, sizeof(b));
171 		HMAC_Update(&mac->evp_ctx, data, datalen);
172 		HMAC_Final(&mac->evp_ctx, m, NULL);
173 		break;
174 #ifdef UMAC_HAS_BEEN_UNBROKEN
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 #endif
186 	default:
187 		fatal("mac_compute: unknown MAC type");
188 	}
189 	return (m);
190 }
191 
192 void
193 mac_clear(Mac *mac)
194 {
195 #ifdef UMAC_HAS_BEEN_UNBROKEN
196 	if (mac->type == SSH_UMAC) {
197 		if (mac->umac_ctx != NULL)
198 			umac_delete(mac->umac_ctx);
199 	} else if (mac->type == SSH_UMAC128) {
200 		if (mac->umac_ctx != NULL)
201 			umac128_delete(mac->umac_ctx);
202 	} else
203 #endif
204 	if (mac->evp_md != NULL)
205 		HMAC_cleanup(&mac->evp_ctx);
206 	mac->evp_md = NULL;
207 	mac->umac_ctx = NULL;
208 }
209 
210 /* XXX copied from ciphers_valid */
211 #define	MAC_SEP	","
212 int
213 mac_valid(const char *names)
214 {
215 	char *maclist, *cp, *p;
216 
217 	if (names == NULL || strcmp(names, "") == 0)
218 		return (0);
219 	maclist = cp = xstrdup(names);
220 	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
221 	    (p = strsep(&cp, MAC_SEP))) {
222 		if (mac_setup(NULL, p) < 0) {
223 			debug("bad mac %s [%s]", p, names);
224 			xfree(maclist);
225 			return (0);
226 		} else {
227 			debug3("mac ok: %s [%s]", p, names);
228 		}
229 	}
230 	debug3("macs ok: [%s]", names);
231 	xfree(maclist);
232 	return (1);
233 }
234