xref: /openbsd-src/usr.bin/ssh/mac.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /* $OpenBSD: mac.c,v 1.33 2016/07/08 03:44:42 djm 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 <sys/types.h>
27 
28 #include <string.h>
29 #include <stdio.h>
30 
31 #include "digest.h"
32 #include "hmac.h"
33 #include "umac.h"
34 #include "mac.h"
35 #include "misc.h"
36 #include "ssherr.h"
37 #include "sshbuf.h"
38 
39 #define SSH_DIGEST	1	/* SSH_DIGEST_XXX */
40 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
41 #define SSH_UMAC128	3
42 
43 struct macalg {
44 	char		*name;
45 	int		type;
46 	int		alg;
47 	int		truncatebits;	/* truncate digest if != 0 */
48 	int		key_len;	/* just for UMAC */
49 	int		len;		/* just for UMAC */
50 	int		etm;		/* Encrypt-then-MAC */
51 };
52 
53 static const struct macalg macs[] = {
54 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
55 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
56 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
57 	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
58 	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
59 	{ "hmac-md5",				SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
60 	{ "hmac-md5-96",			SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
61 	{ "hmac-ripemd160",			SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
62 	{ "hmac-ripemd160@openssh.com",		SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
63 	{ "umac-64@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 0 },
64 	{ "umac-128@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 0 },
65 
66 	/* Encrypt-then-MAC variants */
67 	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
68 	{ "hmac-sha1-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
69 	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
70 	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
71 	{ "hmac-md5-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
72 	{ "hmac-md5-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
73 	{ "hmac-ripemd160-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 1 },
74 	{ "umac-64-etm@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 1 },
75 	{ "umac-128-etm@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 1 },
76 
77 	{ NULL,					0, 0, 0, 0, 0, 0 }
78 };
79 
80 /* Returns a list of supported MACs separated by the specified char. */
81 char *
82 mac_alg_list(char sep)
83 {
84 	char *ret = NULL, *tmp;
85 	size_t nlen, rlen = 0;
86 	const struct macalg *m;
87 
88 	for (m = macs; m->name != NULL; m++) {
89 		if (ret != NULL)
90 			ret[rlen++] = sep;
91 		nlen = strlen(m->name);
92 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
93 			free(ret);
94 			return NULL;
95 		}
96 		ret = tmp;
97 		memcpy(ret + rlen, m->name, nlen + 1);
98 		rlen += nlen;
99 	}
100 	return ret;
101 }
102 
103 static int
104 mac_setup_by_alg(struct sshmac *mac, const struct macalg *macalg)
105 {
106 	mac->type = macalg->type;
107 	if (mac->type == SSH_DIGEST) {
108 		if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
109 			return SSH_ERR_ALLOC_FAIL;
110 		mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
111 	} else {
112 		mac->mac_len = macalg->len / 8;
113 		mac->key_len = macalg->key_len / 8;
114 		mac->umac_ctx = NULL;
115 	}
116 	if (macalg->truncatebits != 0)
117 		mac->mac_len = macalg->truncatebits / 8;
118 	mac->etm = macalg->etm;
119 	return 0;
120 }
121 
122 int
123 mac_setup(struct sshmac *mac, char *name)
124 {
125 	const struct macalg *m;
126 
127 	for (m = macs; m->name != NULL; m++) {
128 		if (strcmp(name, m->name) != 0)
129 			continue;
130 		if (mac != NULL)
131 			return mac_setup_by_alg(mac, m);
132 		return 0;
133 	}
134 	return SSH_ERR_INVALID_ARGUMENT;
135 }
136 
137 int
138 mac_init(struct sshmac *mac)
139 {
140 	if (mac->key == NULL)
141 		return SSH_ERR_INVALID_ARGUMENT;
142 	switch (mac->type) {
143 	case SSH_DIGEST:
144 		if (mac->hmac_ctx == NULL ||
145 		    ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
146 			return SSH_ERR_INVALID_ARGUMENT;
147 		return 0;
148 	case SSH_UMAC:
149 		if ((mac->umac_ctx = umac_new(mac->key)) == NULL)
150 			return SSH_ERR_ALLOC_FAIL;
151 		return 0;
152 	case SSH_UMAC128:
153 		if ((mac->umac_ctx = umac128_new(mac->key)) == NULL)
154 			return SSH_ERR_ALLOC_FAIL;
155 		return 0;
156 	default:
157 		return SSH_ERR_INVALID_ARGUMENT;
158 	}
159 }
160 
161 int
162 mac_compute(struct sshmac *mac, u_int32_t seqno,
163     const u_char *data, int datalen,
164     u_char *digest, size_t dlen)
165 {
166 	static union {
167 		u_char m[SSH_DIGEST_MAX_LENGTH];
168 		u_int64_t for_align;
169 	} u;
170 	u_char b[4];
171 	u_char nonce[8];
172 
173 	if (mac->mac_len > sizeof(u))
174 		return SSH_ERR_INTERNAL_ERROR;
175 
176 	switch (mac->type) {
177 	case SSH_DIGEST:
178 		put_u32(b, seqno);
179 		/* reset HMAC context */
180 		if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
181 		    ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
182 		    ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
183 		    ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
184 			return SSH_ERR_LIBCRYPTO_ERROR;
185 		break;
186 	case SSH_UMAC:
187 		POKE_U64(nonce, seqno);
188 		umac_update(mac->umac_ctx, data, datalen);
189 		umac_final(mac->umac_ctx, u.m, nonce);
190 		break;
191 	case SSH_UMAC128:
192 		put_u64(nonce, seqno);
193 		umac128_update(mac->umac_ctx, data, datalen);
194 		umac128_final(mac->umac_ctx, u.m, nonce);
195 		break;
196 	default:
197 		return SSH_ERR_INVALID_ARGUMENT;
198 	}
199 	if (digest != NULL) {
200 		if (dlen > mac->mac_len)
201 			dlen = mac->mac_len;
202 		memcpy(digest, u.m, dlen);
203 	}
204 	return 0;
205 }
206 
207 int
208 mac_check(struct sshmac *mac, u_int32_t seqno,
209     const u_char *data, size_t dlen,
210     const u_char *theirmac, size_t mlen)
211 {
212 	u_char ourmac[SSH_DIGEST_MAX_LENGTH];
213 	int r;
214 
215 	if (mac->mac_len > mlen)
216 		return SSH_ERR_INVALID_ARGUMENT;
217 	if ((r = mac_compute(mac, seqno, data, dlen,
218 	    ourmac, sizeof(ourmac))) != 0)
219 		return r;
220 	if (timingsafe_bcmp(ourmac, theirmac, mac->mac_len) != 0)
221 		return SSH_ERR_MAC_INVALID;
222 	return 0;
223 }
224 
225 void
226 mac_clear(struct sshmac *mac)
227 {
228 	if (mac->type == SSH_UMAC) {
229 		if (mac->umac_ctx != NULL)
230 			umac_delete(mac->umac_ctx);
231 	} else if (mac->type == SSH_UMAC128) {
232 		if (mac->umac_ctx != NULL)
233 			umac128_delete(mac->umac_ctx);
234 	} else if (mac->hmac_ctx != NULL)
235 		ssh_hmac_free(mac->hmac_ctx);
236 	mac->hmac_ctx = NULL;
237 	mac->umac_ctx = NULL;
238 }
239 
240 /* XXX copied from ciphers_valid */
241 #define	MAC_SEP	","
242 int
243 mac_valid(const char *names)
244 {
245 	char *maclist, *cp, *p;
246 
247 	if (names == NULL || strcmp(names, "") == 0)
248 		return 0;
249 	if ((maclist = cp = strdup(names)) == NULL)
250 		return 0;
251 	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
252 	    (p = strsep(&cp, MAC_SEP))) {
253 		if (mac_setup(NULL, p) < 0) {
254 			free(maclist);
255 			return 0;
256 		}
257 	}
258 	free(maclist);
259 	return 1;
260 }
261