xref: /openssh-portable/mac.c (revision edd1d3a6)
1*be02d7cbSdjm@openbsd.org /* $OpenBSD: mac.c,v 1.35 2019/09/06 04:53:27 djm Exp $ */
206b33aa0SBen Lindstrom /*
306b33aa0SBen Lindstrom  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
406b33aa0SBen Lindstrom  *
506b33aa0SBen Lindstrom  * Redistribution and use in source and binary forms, with or without
606b33aa0SBen Lindstrom  * modification, are permitted provided that the following conditions
706b33aa0SBen Lindstrom  * are met:
806b33aa0SBen Lindstrom  * 1. Redistributions of source code must retain the above copyright
906b33aa0SBen Lindstrom  *    notice, this list of conditions and the following disclaimer.
1006b33aa0SBen Lindstrom  * 2. Redistributions in binary form must reproduce the above copyright
1106b33aa0SBen Lindstrom  *    notice, this list of conditions and the following disclaimer in the
1206b33aa0SBen Lindstrom  *    documentation and/or other materials provided with the distribution.
1306b33aa0SBen Lindstrom  *
1406b33aa0SBen Lindstrom  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1506b33aa0SBen Lindstrom  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1606b33aa0SBen Lindstrom  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1706b33aa0SBen Lindstrom  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1806b33aa0SBen Lindstrom  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1906b33aa0SBen Lindstrom  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2006b33aa0SBen Lindstrom  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2106b33aa0SBen Lindstrom  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2206b33aa0SBen Lindstrom  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2306b33aa0SBen Lindstrom  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2406b33aa0SBen Lindstrom  */
2506b33aa0SBen Lindstrom 
2606b33aa0SBen Lindstrom #include "includes.h"
2706b33aa0SBen Lindstrom 
28d7834353SDamien Miller #include <sys/types.h>
29d7834353SDamien Miller 
302ea60312SDarren Tucker #include <stdlib.h>
31e3476ed0SDamien Miller #include <string.h>
32128343bcSmarkus@openbsd.org #include <stdio.h>
3306b33aa0SBen Lindstrom 
344e8d937aSDamien Miller #include "digest.h"
354e8d937aSDamien Miller #include "hmac.h"
36e45796f7SDamien Miller #include "umac.h"
37128343bcSmarkus@openbsd.org #include "mac.h"
38128343bcSmarkus@openbsd.org #include "misc.h"
39128343bcSmarkus@openbsd.org #include "ssherr.h"
40128343bcSmarkus@openbsd.org #include "sshbuf.h"
41e45796f7SDamien Miller 
42e9b3ad73SDarren Tucker #include "openbsd-compat/openssl-compat.h"
43e9b3ad73SDarren Tucker 
444e8d937aSDamien Miller #define SSH_DIGEST	1	/* SSH_DIGEST_XXX */
45e45796f7SDamien Miller #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
46427e409eSDarren Tucker #define SSH_UMAC128	3
47e45796f7SDamien Miller 
48ea11119eSDamien Miller struct macalg {
4906b33aa0SBen Lindstrom 	char		*name;
50e45796f7SDamien Miller 	int		type;
514e8d937aSDamien Miller 	int		alg;
5206b33aa0SBen Lindstrom 	int		truncatebits;	/* truncate digest if != 0 */
53e45796f7SDamien Miller 	int		key_len;	/* just for UMAC */
54e45796f7SDamien Miller 	int		len;		/* just for UMAC */
5537834afeSDamien Miller 	int		etm;		/* Encrypt-then-MAC */
56ea11119eSDamien Miller };
57ea11119eSDamien Miller 
58ea11119eSDamien Miller static const struct macalg macs[] = {
59af43a7acSDamien Miller 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
604e8d937aSDamien Miller 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
614e8d937aSDamien Miller 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
624e8d937aSDamien Miller 	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
634e8d937aSDamien Miller 	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
644e8d937aSDamien Miller 	{ "hmac-md5",				SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
654e8d937aSDamien Miller 	{ "hmac-md5-96",			SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
664e8d937aSDamien Miller 	{ "umac-64@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 0 },
674e8d937aSDamien Miller 	{ "umac-128@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 0 },
68af43a7acSDamien Miller 
69af43a7acSDamien Miller 	/* Encrypt-then-MAC variants */
704e8d937aSDamien Miller 	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
714e8d937aSDamien Miller 	{ "hmac-sha1-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
724e8d937aSDamien Miller 	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
734e8d937aSDamien Miller 	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
744e8d937aSDamien Miller 	{ "hmac-md5-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
754e8d937aSDamien Miller 	{ "hmac-md5-96-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
764e8d937aSDamien Miller 	{ "umac-64-etm@openssh.com",		SSH_UMAC, 0, 0, 128, 64, 1 },
774e8d937aSDamien Miller 	{ "umac-128-etm@openssh.com",		SSH_UMAC128, 0, 0, 128, 128, 1 },
78af43a7acSDamien Miller 
794e8d937aSDamien Miller 	{ NULL,					0, 0, 0, 0, 0, 0 }
8006b33aa0SBen Lindstrom };
8106b33aa0SBen Lindstrom 
82690d9890SDamien Miller /* Returns a list of supported MACs separated by the specified char. */
83ea11119eSDamien Miller char *
mac_alg_list(char sep)84690d9890SDamien Miller mac_alg_list(char sep)
85ea11119eSDamien Miller {
86128343bcSmarkus@openbsd.org 	char *ret = NULL, *tmp;
87ea11119eSDamien Miller 	size_t nlen, rlen = 0;
88ea11119eSDamien Miller 	const struct macalg *m;
89ea11119eSDamien Miller 
90ea11119eSDamien Miller 	for (m = macs; m->name != NULL; m++) {
91ea11119eSDamien Miller 		if (ret != NULL)
92690d9890SDamien Miller 			ret[rlen++] = sep;
93ea11119eSDamien Miller 		nlen = strlen(m->name);
94128343bcSmarkus@openbsd.org 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
95128343bcSmarkus@openbsd.org 			free(ret);
96128343bcSmarkus@openbsd.org 			return NULL;
97128343bcSmarkus@openbsd.org 		}
98128343bcSmarkus@openbsd.org 		ret = tmp;
99ea11119eSDamien Miller 		memcpy(ret + rlen, m->name, nlen + 1);
100ea11119eSDamien Miller 		rlen += nlen;
101ea11119eSDamien Miller 	}
102ea11119eSDamien Miller 	return ret;
103ea11119eSDamien Miller }
104ea11119eSDamien Miller 
105128343bcSmarkus@openbsd.org static int
mac_setup_by_alg(struct sshmac * mac,const struct macalg * macalg)106128343bcSmarkus@openbsd.org mac_setup_by_alg(struct sshmac *mac, const struct macalg *macalg)
107e45796f7SDamien Miller {
108ea11119eSDamien Miller 	mac->type = macalg->type;
1094e8d937aSDamien Miller 	if (mac->type == SSH_DIGEST) {
1104e8d937aSDamien Miller 		if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
111128343bcSmarkus@openbsd.org 			return SSH_ERR_ALLOC_FAIL;
1124e8d937aSDamien Miller 		mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
113e45796f7SDamien Miller 	} else {
114ea11119eSDamien Miller 		mac->mac_len = macalg->len / 8;
115ea11119eSDamien Miller 		mac->key_len = macalg->key_len / 8;
116e45796f7SDamien Miller 		mac->umac_ctx = NULL;
117e45796f7SDamien Miller 	}
118ea11119eSDamien Miller 	if (macalg->truncatebits != 0)
119ea11119eSDamien Miller 		mac->mac_len = macalg->truncatebits / 8;
120ea11119eSDamien Miller 	mac->etm = macalg->etm;
121128343bcSmarkus@openbsd.org 	return 0;
122e45796f7SDamien Miller }
123e45796f7SDamien Miller 
12406b33aa0SBen Lindstrom int
mac_setup(struct sshmac * mac,char * name)125128343bcSmarkus@openbsd.org mac_setup(struct sshmac *mac, char *name)
12606b33aa0SBen Lindstrom {
127ea11119eSDamien Miller 	const struct macalg *m;
128eccb9de7SDamien Miller 
129ea11119eSDamien Miller 	for (m = macs; m->name != NULL; m++) {
130ea11119eSDamien Miller 		if (strcmp(name, m->name) != 0)
131ea11119eSDamien Miller 			continue;
132128343bcSmarkus@openbsd.org 		if (mac != NULL)
133128343bcSmarkus@openbsd.org 			return mac_setup_by_alg(mac, m);
134128343bcSmarkus@openbsd.org 		return 0;
135d3cf67e1SDamien Miller 	}
136128343bcSmarkus@openbsd.org 	return SSH_ERR_INVALID_ARGUMENT;
13706b33aa0SBen Lindstrom }
13806b33aa0SBen Lindstrom 
139e45796f7SDamien Miller int
mac_init(struct sshmac * mac)140128343bcSmarkus@openbsd.org mac_init(struct sshmac *mac)
1415f3d5be5SDarren Tucker {
1425f3d5be5SDarren Tucker 	if (mac->key == NULL)
143128343bcSmarkus@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
144e45796f7SDamien Miller 	switch (mac->type) {
1454e8d937aSDamien Miller 	case SSH_DIGEST:
1464e8d937aSDamien Miller 		if (mac->hmac_ctx == NULL ||
1474e8d937aSDamien Miller 		    ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
148128343bcSmarkus@openbsd.org 			return SSH_ERR_INVALID_ARGUMENT;
149e45796f7SDamien Miller 		return 0;
150e45796f7SDamien Miller 	case SSH_UMAC:
151128343bcSmarkus@openbsd.org 		if ((mac->umac_ctx = umac_new(mac->key)) == NULL)
152128343bcSmarkus@openbsd.org 			return SSH_ERR_ALLOC_FAIL;
153e45796f7SDamien Miller 		return 0;
154427e409eSDarren Tucker 	case SSH_UMAC128:
155ddef9995Snaddy@openbsd.org 		if ((mac->umac_ctx = umac128_new(mac->key)) == NULL)
156ddef9995Snaddy@openbsd.org 			return SSH_ERR_ALLOC_FAIL;
157427e409eSDarren Tucker 		return 0;
158e45796f7SDamien Miller 	default:
159128343bcSmarkus@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
160e45796f7SDamien Miller 	}
1615f3d5be5SDarren Tucker }
1625f3d5be5SDarren Tucker 
163128343bcSmarkus@openbsd.org int
mac_compute(struct sshmac * mac,u_int32_t seqno,const u_char * data,int datalen,u_char * digest,size_t dlen)1646d31193dSdjm@openbsd.org mac_compute(struct sshmac *mac, u_int32_t seqno,
1656d31193dSdjm@openbsd.org     const u_char *data, int datalen,
166128343bcSmarkus@openbsd.org     u_char *digest, size_t dlen)
16706b33aa0SBen Lindstrom {
1684ac66af0SDarren Tucker 	static union {
169128343bcSmarkus@openbsd.org 		u_char m[SSH_DIGEST_MAX_LENGTH];
1704ac66af0SDarren Tucker 		u_int64_t for_align;
1714ac66af0SDarren Tucker 	} u;
1721f0311c7SDamien Miller 	u_char b[4];
1731f0311c7SDamien Miller 	u_char nonce[8];
17406b33aa0SBen Lindstrom 
1754ac66af0SDarren Tucker 	if (mac->mac_len > sizeof(u))
176128343bcSmarkus@openbsd.org 		return SSH_ERR_INTERNAL_ERROR;
177e45796f7SDamien Miller 
178e45796f7SDamien Miller 	switch (mac->type) {
1794e8d937aSDamien Miller 	case SSH_DIGEST:
1803f941889SDamien Miller 		put_u32(b, seqno);
181e45796f7SDamien Miller 		/* reset HMAC context */
1824e8d937aSDamien Miller 		if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
1834e8d937aSDamien Miller 		    ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
1844e8d937aSDamien Miller 		    ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
1854e8d937aSDamien Miller 		    ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
186128343bcSmarkus@openbsd.org 			return SSH_ERR_LIBCRYPTO_ERROR;
187e45796f7SDamien Miller 		break;
188e45796f7SDamien Miller 	case SSH_UMAC:
189128343bcSmarkus@openbsd.org 		POKE_U64(nonce, seqno);
190e45796f7SDamien Miller 		umac_update(mac->umac_ctx, data, datalen);
1914ac66af0SDarren Tucker 		umac_final(mac->umac_ctx, u.m, nonce);
192e45796f7SDamien Miller 		break;
193427e409eSDarren Tucker 	case SSH_UMAC128:
194427e409eSDarren Tucker 		put_u64(nonce, seqno);
195427e409eSDarren Tucker 		umac128_update(mac->umac_ctx, data, datalen);
1964ac66af0SDarren Tucker 		umac128_final(mac->umac_ctx, u.m, nonce);
197427e409eSDarren Tucker 		break;
198e45796f7SDamien Miller 	default:
199128343bcSmarkus@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
200e45796f7SDamien Miller 	}
201128343bcSmarkus@openbsd.org 	if (digest != NULL) {
202128343bcSmarkus@openbsd.org 		if (dlen > mac->mac_len)
203128343bcSmarkus@openbsd.org 			dlen = mac->mac_len;
204128343bcSmarkus@openbsd.org 		memcpy(digest, u.m, dlen);
205128343bcSmarkus@openbsd.org 	}
206128343bcSmarkus@openbsd.org 	return 0;
20706b33aa0SBen Lindstrom }
20806b33aa0SBen Lindstrom 
2096d31193dSdjm@openbsd.org int
mac_check(struct sshmac * mac,u_int32_t seqno,const u_char * data,size_t dlen,const u_char * theirmac,size_t mlen)2106d31193dSdjm@openbsd.org mac_check(struct sshmac *mac, u_int32_t seqno,
2116d31193dSdjm@openbsd.org     const u_char *data, size_t dlen,
2126d31193dSdjm@openbsd.org     const u_char *theirmac, size_t mlen)
2136d31193dSdjm@openbsd.org {
2146d31193dSdjm@openbsd.org 	u_char ourmac[SSH_DIGEST_MAX_LENGTH];
2156d31193dSdjm@openbsd.org 	int r;
2166d31193dSdjm@openbsd.org 
2176d31193dSdjm@openbsd.org 	if (mac->mac_len > mlen)
2186d31193dSdjm@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
2196d31193dSdjm@openbsd.org 	if ((r = mac_compute(mac, seqno, data, dlen,
2206d31193dSdjm@openbsd.org 	    ourmac, sizeof(ourmac))) != 0)
2216d31193dSdjm@openbsd.org 		return r;
2226d31193dSdjm@openbsd.org 	if (timingsafe_bcmp(ourmac, theirmac, mac->mac_len) != 0)
2236d31193dSdjm@openbsd.org 		return SSH_ERR_MAC_INVALID;
2246d31193dSdjm@openbsd.org 	return 0;
2256d31193dSdjm@openbsd.org }
2266d31193dSdjm@openbsd.org 
2275f3d5be5SDarren Tucker void
mac_clear(struct sshmac * mac)228128343bcSmarkus@openbsd.org mac_clear(struct sshmac *mac)
2295f3d5be5SDarren Tucker {
230e45796f7SDamien Miller 	if (mac->type == SSH_UMAC) {
231e45796f7SDamien Miller 		if (mac->umac_ctx != NULL)
232e45796f7SDamien Miller 			umac_delete(mac->umac_ctx);
233427e409eSDarren Tucker 	} else if (mac->type == SSH_UMAC128) {
234427e409eSDarren Tucker 		if (mac->umac_ctx != NULL)
235427e409eSDarren Tucker 			umac128_delete(mac->umac_ctx);
2364e8d937aSDamien Miller 	} else if (mac->hmac_ctx != NULL)
2374e8d937aSDamien Miller 		ssh_hmac_free(mac->hmac_ctx);
2384e8d937aSDamien Miller 	mac->hmac_ctx = NULL;
239e45796f7SDamien Miller 	mac->umac_ctx = NULL;
2405f3d5be5SDarren Tucker }
2415f3d5be5SDarren Tucker 
24206b33aa0SBen Lindstrom /* XXX copied from ciphers_valid */
24306b33aa0SBen Lindstrom #define	MAC_SEP	","
24406b33aa0SBen Lindstrom int
mac_valid(const char * names)24506b33aa0SBen Lindstrom mac_valid(const char *names)
24606b33aa0SBen Lindstrom {
24706b33aa0SBen Lindstrom 	char *maclist, *cp, *p;
24806b33aa0SBen Lindstrom 
24906b33aa0SBen Lindstrom 	if (names == NULL || strcmp(names, "") == 0)
250128343bcSmarkus@openbsd.org 		return 0;
251128343bcSmarkus@openbsd.org 	if ((maclist = cp = strdup(names)) == NULL)
252128343bcSmarkus@openbsd.org 		return 0;
25306b33aa0SBen Lindstrom 	for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
25406b33aa0SBen Lindstrom 	    (p = strsep(&cp, MAC_SEP))) {
2555f3d5be5SDarren Tucker 		if (mac_setup(NULL, p) < 0) {
256a627d42eSDarren Tucker 			free(maclist);
257128343bcSmarkus@openbsd.org 			return 0;
25806b33aa0SBen Lindstrom 		}
25906b33aa0SBen Lindstrom 	}
260a627d42eSDarren Tucker 	free(maclist);
261128343bcSmarkus@openbsd.org 	return 1;
26206b33aa0SBen Lindstrom }
263