xref: /netbsd-src/crypto/external/bsd/openssh/dist/mac.c (revision 343a8bbd306e345fe2d567b33abc9ba3aa75bfc8)
1 /*	$NetBSD: mac.c,v 1.10 2014/10/20 03:05:13 christos Exp $	*/
2 /* $OpenBSD: mac.c,v 1.30 2014/04/30 19:07:48 naddy 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.10 2014/10/20 03:05:13 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 #include "digest.h"
46 #include "hmac.h"
47 #include "umac.h"
48 
49 #define SSH_DIGEST	1	/* SSH_DIGEST_XXX */
50 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
51 #define SSH_UMAC128	3
52 
53 struct macalg {
54 	const char	*name;
55 	int		type;
56 	int		alg;
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 };
62 
63 static const struct macalg macs[] = {
64 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
65 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
66 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
67 	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
68 	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
69 	{ "hmac-md5",				SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
70 	{ "hmac-md5-96",			SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
71 	{ "hmac-ripemd160",			SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
72 	{ "hmac-ripemd160@openssh.com",		SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
73 	{ "umac-64@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 0 },
74 	{ "umac-128@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 0 },
75 
76 	/* Encrypt-then-MAC variants */
77 	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
78 	{ "hmac-sha1-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
79 	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
80 	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
81 	{ "hmac-md5-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
82 	{ "hmac-md5-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
83 	{ "hmac-ripemd160-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 1 },
84 	{ "umac-64-etm@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 1 },
85 	{ "umac-128-etm@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 1 },
86 
87 	{ NULL,					0, 0, 0, 0, 0, 0 }
88 };
89 
90 /* Returns a list of supported MACs separated by the specified char. */
91 char *
92 mac_alg_list(char sep)
93 {
94 	char *ret = NULL;
95 	size_t nlen, rlen = 0;
96 	const struct macalg *m;
97 
98 	for (m = macs; m->name != NULL; m++) {
99 		if (ret != NULL)
100 			ret[rlen++] = sep;
101 		nlen = strlen(m->name);
102 		ret = xrealloc(ret, 1, rlen + nlen + 2);
103 		memcpy(ret + rlen, m->name, nlen + 1);
104 		rlen += nlen;
105 	}
106 	return ret;
107 }
108 
109 static void
110 mac_setup_by_alg(Mac *mac, const struct macalg *macalg)
111 {
112 	mac->type = macalg->type;
113 	if (mac->type == SSH_DIGEST) {
114 		if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
115 			fatal("ssh_hmac_start(alg=%d) failed", macalg->alg);
116 		mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
117 	} else {
118 		mac->mac_len = macalg->len / 8;
119 		mac->key_len = macalg->key_len / 8;
120 		mac->umac_ctx = NULL;
121 	}
122 	if (macalg->truncatebits != 0)
123 		mac->mac_len = macalg->truncatebits / 8;
124 	mac->etm = macalg->etm;
125 }
126 
127 int
128 mac_setup(Mac *mac, char *name)
129 {
130 	const struct macalg *m;
131 
132 	for (m = macs; m->name != NULL; m++) {
133 		if (strcmp(name, m->name) != 0)
134 			continue;
135 		if (mac != NULL) {
136 			mac_setup_by_alg(mac, m);
137 			debug2("mac_setup: setup %s", name);
138 		}
139 		return (0);
140 	}
141 	debug2("mac_setup: unknown %s", name);
142 	return (-1);
143 }
144 
145 int
146 mac_init(Mac *mac)
147 {
148 	if (mac->key == NULL)
149 		fatal("%s: no key", __func__);
150 	switch (mac->type) {
151 	case SSH_DIGEST:
152 		if (mac->hmac_ctx == NULL ||
153 		    ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
154 			return -1;
155 		return 0;
156 	case SSH_UMAC:
157 		mac->umac_ctx = umac_new(mac->key);
158 		return 0;
159 	case SSH_UMAC128:
160 		mac->umac_ctx = umac128_new(mac->key);
161 		return 0;
162 	default:
163 		return -1;
164 	}
165 }
166 
167 u_char *
168 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
169 {
170 	static union {
171 		u_char m[EVP_MAX_MD_SIZE];
172 		u_int64_t for_align;
173 	} u;
174 	u_char b[4];
175 	u_char nonce[8];
176 
177 	if (mac->mac_len > sizeof(u))
178 		fatal("mac_compute: mac too long %u %zu",
179 		    mac->mac_len, sizeof(u));
180 
181 	switch (mac->type) {
182 	case SSH_DIGEST:
183 		put_u32(b, seqno);
184 		/* reset HMAC context */
185 		if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
186 		    ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
187 		    ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
188 		    ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
189 			fatal("ssh_hmac failed");
190 		break;
191 	case SSH_UMAC:
192 		put_u64(nonce, seqno);
193 		umac_update(mac->umac_ctx, data, datalen);
194 		umac_final(mac->umac_ctx, u.m, nonce);
195 		break;
196 	case SSH_UMAC128:
197 		put_u64(nonce, seqno);
198 		umac128_update(mac->umac_ctx, data, datalen);
199 		umac128_final(mac->umac_ctx, u.m, nonce);
200 		break;
201 	default:
202 		fatal("mac_compute: unknown MAC type");
203 	}
204 	return (u.m);
205 }
206 
207 void
208 mac_clear(Mac *mac)
209 {
210 	if (mac->type == SSH_UMAC) {
211 		if (mac->umac_ctx != NULL)
212 			umac_delete(mac->umac_ctx);
213 	} else if (mac->type == SSH_UMAC128) {
214 		if (mac->umac_ctx != NULL)
215 			umac128_delete(mac->umac_ctx);
216 	} else if (mac->hmac_ctx != NULL)
217 		ssh_hmac_free(mac->hmac_ctx);
218 	mac->hmac_ctx = NULL;
219 	mac->umac_ctx = NULL;
220 }
221 
222 /* XXX copied from ciphers_valid */
223 #define	MAC_SEP	","
224 int
225 mac_valid(const char *names)
226 {
227 	char *maclist, *cp, *p;
228 
229 	if (names == NULL || strcmp(names, "") == 0)
230 		return (0);
231 	maclist = cp = xstrdup(names);
232 	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
233 	    (p = strsep(&cp, MAC_SEP))) {
234 		if (mac_setup(NULL, p) < 0) {
235 			debug("bad mac %s [%s]", p, names);
236 			free(maclist);
237 			return (0);
238 		}
239 	}
240 	debug3("macs ok: [%s]", names);
241 	free(maclist);
242 	return (1);
243 }
244