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