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