blob: 4c4a6bf5328e849a9ebffcefd4d302075befecbb [file] [log] [blame]
Damien Miller4e8d9372014-02-04 11:02:42 +11001/* $OpenBSD: mac.c,v 1.27 2014/01/27 18:58:14 markus Exp $ */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002/*
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"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000027
Damien Millerd7834352006-08-05 12:39:39 +100028#include <sys/types.h>
29
Damien Millerded319c2006-09-01 15:38:36 +100030#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000034#include "xmalloc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000035#include "log.h"
36#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "buffer.h"
38#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000039#include "kex.h"
40#include "mac.h"
Damien Miller3f941882006-03-31 23:13:02 +110041#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000042
Damien Miller4e8d9372014-02-04 11:02:42 +110043#include "digest.h"
44#include "hmac.h"
Damien Millere45796f2007-06-11 14:01:42 +100045#include "umac.h"
46
Darren Tuckere9b3ad72012-01-17 14:03:34 +110047#include "openbsd-compat/openssl-compat.h"
48
Damien Miller4e8d9372014-02-04 11:02:42 +110049#define SSH_DIGEST 1 /* SSH_DIGEST_XXX */
Damien Millere45796f2007-06-11 14:01:42 +100050#define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
Darren Tucker427e4092012-10-05 11:02:39 +100051#define SSH_UMAC128 3
Damien Millere45796f2007-06-11 14:01:42 +100052
Damien Millerea111192013-04-23 19:24:32 +100053struct macalg {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000054 char *name;
Damien Millere45796f2007-06-11 14:01:42 +100055 int type;
Damien Miller4e8d9372014-02-04 11:02:42 +110056 int alg;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000057 int truncatebits; /* truncate digest if != 0 */
Damien Millere45796f2007-06-11 14:01:42 +100058 int key_len; /* just for UMAC */
59 int len; /* just for UMAC */
Damien Miller37834af2012-12-12 11:00:37 +110060 int etm; /* Encrypt-then-MAC */
Damien Millerea111192013-04-23 19:24:32 +100061};
62
63static const struct macalg macs[] = {
Damien Milleraf43a7a2012-12-12 10:46:31 +110064 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
Damien Miller4e8d9372014-02-04 11:02:42 +110065 { "hmac-sha1", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
66 { "hmac-sha1-96", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
Tim Ricea1226822011-08-16 17:29:01 -070067#ifdef HAVE_EVP_SHA256
Damien Miller4e8d9372014-02-04 11:02:42 +110068 { "hmac-sha2-256", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
69 { "hmac-sha2-512", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
Tim Ricea1226822011-08-16 17:29:01 -070070#endif
Damien Miller4e8d9372014-02-04 11:02:42 +110071 { "hmac-md5", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
72 { "hmac-md5-96", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
73 { "hmac-ripemd160", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
74 { "hmac-ripemd160@openssh.com", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 },
75 { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 },
76 { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110077
78 /* Encrypt-then-MAC variants */
Damien Miller4e8d9372014-02-04 11:02:42 +110079 { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
80 { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110081#ifdef HAVE_EVP_SHA256
Damien Miller4e8d9372014-02-04 11:02:42 +110082 { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
83 { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110084#endif
Damien Miller4e8d9372014-02-04 11:02:42 +110085 { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
86 { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
87 { "hmac-ripemd160-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 1 },
88 { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 },
89 { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110090
Damien Miller4e8d9372014-02-04 11:02:42 +110091 { NULL, 0, 0, 0, 0, 0, 0 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000092};
93
Damien Miller690d9892013-11-08 12:16:49 +110094/* Returns a list of supported MACs separated by the specified char. */
Damien Millerea111192013-04-23 19:24:32 +100095char *
Damien Miller690d9892013-11-08 12:16:49 +110096mac_alg_list(char sep)
Damien Millerea111192013-04-23 19:24:32 +100097{
98 char *ret = NULL;
99 size_t nlen, rlen = 0;
100 const struct macalg *m;
101
102 for (m = macs; m->name != NULL; m++) {
103 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100104 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000105 nlen = strlen(m->name);
106 ret = xrealloc(ret, 1, rlen + nlen + 2);
107 memcpy(ret + rlen, m->name, nlen + 1);
108 rlen += nlen;
109 }
110 return ret;
111}
112
Damien Millere45796f2007-06-11 14:01:42 +1000113static void
Damien Millerea111192013-04-23 19:24:32 +1000114mac_setup_by_alg(Mac *mac, const struct macalg *macalg)
Damien Millere45796f2007-06-11 14:01:42 +1000115{
Damien Millerea111192013-04-23 19:24:32 +1000116 mac->type = macalg->type;
Damien Miller4e8d9372014-02-04 11:02:42 +1100117 if (mac->type == SSH_DIGEST) {
118 if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
119 fatal("ssh_hmac_start(alg=%d) failed", macalg->alg);
120 mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
Damien Millere45796f2007-06-11 14:01:42 +1000121 } else {
Damien Millerea111192013-04-23 19:24:32 +1000122 mac->mac_len = macalg->len / 8;
123 mac->key_len = macalg->key_len / 8;
Damien Millere45796f2007-06-11 14:01:42 +1000124 mac->umac_ctx = NULL;
125 }
Damien Millerea111192013-04-23 19:24:32 +1000126 if (macalg->truncatebits != 0)
127 mac->mac_len = macalg->truncatebits / 8;
128 mac->etm = macalg->etm;
Damien Millere45796f2007-06-11 14:01:42 +1000129}
130
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000131int
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000132mac_setup(Mac *mac, char *name)
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000133{
Damien Millerea111192013-04-23 19:24:32 +1000134 const struct macalg *m;
Damien Millereccb9de2005-06-17 12:59:34 +1000135
Damien Millerea111192013-04-23 19:24:32 +1000136 for (m = macs; m->name != NULL; m++) {
137 if (strcmp(name, m->name) != 0)
138 continue;
139 if (mac != NULL)
140 mac_setup_by_alg(mac, m);
141 debug2("mac_setup: found %s", name);
142 return (0);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000143 }
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000144 debug2("mac_setup: unknown %s", name);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000145 return (-1);
146}
147
Damien Millere45796f2007-06-11 14:01:42 +1000148int
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000149mac_init(Mac *mac)
150{
151 if (mac->key == NULL)
152 fatal("mac_init: no key");
Damien Millere45796f2007-06-11 14:01:42 +1000153 switch (mac->type) {
Damien Miller4e8d9372014-02-04 11:02:42 +1100154 case SSH_DIGEST:
155 if (mac->hmac_ctx == NULL ||
156 ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
Damien Millere45796f2007-06-11 14:01:42 +1000157 return -1;
Damien Millere45796f2007-06-11 14:01:42 +1000158 return 0;
159 case SSH_UMAC:
160 mac->umac_ctx = umac_new(mac->key);
161 return 0;
Darren Tucker427e4092012-10-05 11:02:39 +1000162 case SSH_UMAC128:
163 mac->umac_ctx = umac128_new(mac->key);
164 return 0;
Damien Millere45796f2007-06-11 14:01:42 +1000165 default:
166 return -1;
167 }
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000168}
169
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000170u_char *
171mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
172{
Darren Tucker4ac66af2013-06-06 08:12:37 +1000173 static union {
174 u_char m[EVP_MAX_MD_SIZE];
175 u_int64_t for_align;
176 } u;
Damien Millere45796f2007-06-11 14:01:42 +1000177 u_char b[4], nonce[8];
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000178
Darren Tucker4ac66af2013-06-06 08:12:37 +1000179 if (mac->mac_len > sizeof(u))
Damien Miller3e498532014-01-10 10:37:05 +1100180 fatal("mac_compute: mac too long %u %zu",
181 mac->mac_len, sizeof(u));
Damien Millere45796f2007-06-11 14:01:42 +1000182
183 switch (mac->type) {
Damien Miller4e8d9372014-02-04 11:02:42 +1100184 case SSH_DIGEST:
Damien Millere45796f2007-06-11 14:01:42 +1000185 put_u32(b, seqno);
186 /* reset HMAC context */
Damien Miller4e8d9372014-02-04 11:02:42 +1100187 if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
188 ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
189 ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
190 ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
191 fatal("ssh_hmac failed");
Damien Millere45796f2007-06-11 14:01:42 +1000192 break;
193 case SSH_UMAC:
194 put_u64(nonce, seqno);
195 umac_update(mac->umac_ctx, data, datalen);
Darren Tucker4ac66af2013-06-06 08:12:37 +1000196 umac_final(mac->umac_ctx, u.m, nonce);
Damien Millere45796f2007-06-11 14:01:42 +1000197 break;
Darren Tucker427e4092012-10-05 11:02:39 +1000198 case SSH_UMAC128:
199 put_u64(nonce, seqno);
200 umac128_update(mac->umac_ctx, data, datalen);
Darren Tucker4ac66af2013-06-06 08:12:37 +1000201 umac128_final(mac->umac_ctx, u.m, nonce);
Darren Tucker427e4092012-10-05 11:02:39 +1000202 break;
Damien Millere45796f2007-06-11 14:01:42 +1000203 default:
204 fatal("mac_compute: unknown MAC type");
205 }
Darren Tucker4ac66af2013-06-06 08:12:37 +1000206 return (u.m);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000207}
208
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000209void
210mac_clear(Mac *mac)
211{
Damien Millere45796f2007-06-11 14:01:42 +1000212 if (mac->type == SSH_UMAC) {
213 if (mac->umac_ctx != NULL)
214 umac_delete(mac->umac_ctx);
Darren Tucker427e4092012-10-05 11:02:39 +1000215 } else if (mac->type == SSH_UMAC128) {
216 if (mac->umac_ctx != NULL)
217 umac128_delete(mac->umac_ctx);
Damien Miller4e8d9372014-02-04 11:02:42 +1100218 } else if (mac->hmac_ctx != NULL)
219 ssh_hmac_free(mac->hmac_ctx);
220 mac->hmac_ctx = NULL;
Damien Millere45796f2007-06-11 14:01:42 +1000221 mac->umac_ctx = NULL;
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000222}
223
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000224/* XXX copied from ciphers_valid */
225#define MAC_SEP ","
226int
227mac_valid(const char *names)
228{
229 char *maclist, *cp, *p;
230
231 if (names == NULL || strcmp(names, "") == 0)
232 return (0);
233 maclist = cp = xstrdup(names);
234 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100235 (p = strsep(&cp, MAC_SEP))) {
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000236 if (mac_setup(NULL, p) < 0) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000237 debug("bad mac %s [%s]", p, names);
Darren Tuckera627d422013-06-02 07:31:17 +1000238 free(maclist);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000239 return (0);
240 } else {
241 debug3("mac ok: %s [%s]", p, names);
242 }
243 }
244 debug3("macs ok: [%s]", names);
Darren Tuckera627d422013-06-02 07:31:17 +1000245 free(maclist);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000246 return (1);
247}