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