blob: fc2bd427687e8e31fc1a63864d92b7b3e6a72594 [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: mac.c,v 1.29 2014/04/29 18:01:49 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 },
Damien Miller1f0311c2014-05-15 14:24:09 +100075#ifdef WITH_OPENSSL
Damien Miller4e8d9372014-02-04 11:02:42 +110076 { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 },
77 { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 },
Damien Miller1f0311c2014-05-15 14:24:09 +100078#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +110079
80 /* Encrypt-then-MAC variants */
Damien Miller4e8d9372014-02-04 11:02:42 +110081 { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
82 { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110083#ifdef HAVE_EVP_SHA256
Damien Miller4e8d9372014-02-04 11:02:42 +110084 { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
85 { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
Damien Milleraf43a7a2012-12-12 10:46:31 +110086#endif
Damien Miller4e8d9372014-02-04 11:02:42 +110087 { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
88 { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
89 { "hmac-ripemd160-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 1 },
Damien Miller1f0311c2014-05-15 14:24:09 +100090#ifdef WITH_OPENSSL
Damien Miller4e8d9372014-02-04 11:02:42 +110091 { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 },
92 { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 },
Damien Miller1f0311c2014-05-15 14:24:09 +100093#endif
Damien Milleraf43a7a2012-12-12 10:46:31 +110094
Damien Miller4e8d9372014-02-04 11:02:42 +110095 { NULL, 0, 0, 0, 0, 0, 0 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000096};
97
Damien Miller690d9892013-11-08 12:16:49 +110098/* Returns a list of supported MACs separated by the specified char. */
Damien Millerea111192013-04-23 19:24:32 +100099char *
Damien Miller690d9892013-11-08 12:16:49 +1100100mac_alg_list(char sep)
Damien Millerea111192013-04-23 19:24:32 +1000101{
102 char *ret = NULL;
103 size_t nlen, rlen = 0;
104 const struct macalg *m;
105
106 for (m = macs; m->name != NULL; m++) {
107 if (ret != NULL)
Damien Miller690d9892013-11-08 12:16:49 +1100108 ret[rlen++] = sep;
Damien Millerea111192013-04-23 19:24:32 +1000109 nlen = strlen(m->name);
110 ret = xrealloc(ret, 1, rlen + nlen + 2);
111 memcpy(ret + rlen, m->name, nlen + 1);
112 rlen += nlen;
113 }
114 return ret;
115}
116
Damien Millere45796f2007-06-11 14:01:42 +1000117static void
Damien Millerea111192013-04-23 19:24:32 +1000118mac_setup_by_alg(Mac *mac, const struct macalg *macalg)
Damien Millere45796f2007-06-11 14:01:42 +1000119{
Damien Millerea111192013-04-23 19:24:32 +1000120 mac->type = macalg->type;
Damien Miller4e8d9372014-02-04 11:02:42 +1100121 if (mac->type == SSH_DIGEST) {
122 if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
123 fatal("ssh_hmac_start(alg=%d) failed", macalg->alg);
124 mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
Damien Millere45796f2007-06-11 14:01:42 +1000125 } else {
Damien Miller1f0311c2014-05-15 14:24:09 +1000126#ifdef WITH_OPENSSL
Damien Millerea111192013-04-23 19:24:32 +1000127 mac->mac_len = macalg->len / 8;
128 mac->key_len = macalg->key_len / 8;
Damien Millere45796f2007-06-11 14:01:42 +1000129 mac->umac_ctx = NULL;
Damien Miller1f0311c2014-05-15 14:24:09 +1000130#endif
Damien Millere45796f2007-06-11 14:01:42 +1000131 }
Damien Millerea111192013-04-23 19:24:32 +1000132 if (macalg->truncatebits != 0)
133 mac->mac_len = macalg->truncatebits / 8;
134 mac->etm = macalg->etm;
Damien Millere45796f2007-06-11 14:01:42 +1000135}
136
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000137int
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000138mac_setup(Mac *mac, char *name)
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000139{
Damien Millerea111192013-04-23 19:24:32 +1000140 const struct macalg *m;
Damien Millereccb9de2005-06-17 12:59:34 +1000141
Damien Millerea111192013-04-23 19:24:32 +1000142 for (m = macs; m->name != NULL; m++) {
143 if (strcmp(name, m->name) != 0)
144 continue;
Damien Millerd3cf67e2014-02-24 15:55:36 +1100145 if (mac != NULL) {
Damien Millerea111192013-04-23 19:24:32 +1000146 mac_setup_by_alg(mac, m);
Damien Millerd3cf67e2014-02-24 15:55:36 +1100147 debug2("mac_setup: setup %s", name);
148 }
Damien Millerea111192013-04-23 19:24:32 +1000149 return (0);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000150 }
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000151 debug2("mac_setup: unknown %s", name);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000152 return (-1);
153}
154
Damien Millere45796f2007-06-11 14:01:42 +1000155int
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000156mac_init(Mac *mac)
157{
158 if (mac->key == NULL)
Damien Millerd3cf67e2014-02-24 15:55:36 +1100159 fatal("%s: no key", __func__);
Damien Millere45796f2007-06-11 14:01:42 +1000160 switch (mac->type) {
Damien Miller4e8d9372014-02-04 11:02:42 +1100161 case SSH_DIGEST:
162 if (mac->hmac_ctx == NULL ||
163 ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
Damien Millere45796f2007-06-11 14:01:42 +1000164 return -1;
Damien Millere45796f2007-06-11 14:01:42 +1000165 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000166#ifdef WITH_OPENSSL
Damien Millere45796f2007-06-11 14:01:42 +1000167 case SSH_UMAC:
168 mac->umac_ctx = umac_new(mac->key);
169 return 0;
Darren Tucker427e4092012-10-05 11:02:39 +1000170 case SSH_UMAC128:
171 mac->umac_ctx = umac128_new(mac->key);
172 return 0;
Damien Miller1f0311c2014-05-15 14:24:09 +1000173#endif
Damien Millere45796f2007-06-11 14:01:42 +1000174 default:
175 return -1;
176 }
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000177}
178
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000179u_char *
180mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
181{
Darren Tucker4ac66af2013-06-06 08:12:37 +1000182 static union {
183 u_char m[EVP_MAX_MD_SIZE];
184 u_int64_t for_align;
185 } u;
Damien Miller1f0311c2014-05-15 14:24:09 +1000186 u_char b[4];
187#ifdef WITH_OPENSSL
188 u_char nonce[8];
189#endif
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000190
Darren Tucker4ac66af2013-06-06 08:12:37 +1000191 if (mac->mac_len > sizeof(u))
Damien Miller3e498532014-01-10 10:37:05 +1100192 fatal("mac_compute: mac too long %u %zu",
193 mac->mac_len, sizeof(u));
Damien Millere45796f2007-06-11 14:01:42 +1000194
195 switch (mac->type) {
Damien Miller4e8d9372014-02-04 11:02:42 +1100196 case SSH_DIGEST:
Damien Millere45796f2007-06-11 14:01:42 +1000197 put_u32(b, seqno);
198 /* reset HMAC context */
Damien Miller4e8d9372014-02-04 11:02:42 +1100199 if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
200 ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
201 ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
202 ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
203 fatal("ssh_hmac failed");
Damien Millere45796f2007-06-11 14:01:42 +1000204 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000205#ifdef WITH_OPENSSL
Damien Millere45796f2007-06-11 14:01:42 +1000206 case SSH_UMAC:
207 put_u64(nonce, seqno);
208 umac_update(mac->umac_ctx, data, datalen);
Darren Tucker4ac66af2013-06-06 08:12:37 +1000209 umac_final(mac->umac_ctx, u.m, nonce);
Damien Millere45796f2007-06-11 14:01:42 +1000210 break;
Darren Tucker427e4092012-10-05 11:02:39 +1000211 case SSH_UMAC128:
212 put_u64(nonce, seqno);
213 umac128_update(mac->umac_ctx, data, datalen);
Darren Tucker4ac66af2013-06-06 08:12:37 +1000214 umac128_final(mac->umac_ctx, u.m, nonce);
Darren Tucker427e4092012-10-05 11:02:39 +1000215 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#endif
Damien Millere45796f2007-06-11 14:01:42 +1000217 default:
218 fatal("mac_compute: unknown MAC type");
219 }
Darren Tucker4ac66af2013-06-06 08:12:37 +1000220 return (u.m);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000221}
222
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000223void
224mac_clear(Mac *mac)
225{
Damien Miller1f0311c2014-05-15 14:24:09 +1000226#ifdef WITH_OPENSSL
Damien Millere45796f2007-06-11 14:01:42 +1000227 if (mac->type == SSH_UMAC) {
228 if (mac->umac_ctx != NULL)
229 umac_delete(mac->umac_ctx);
Darren Tucker427e4092012-10-05 11:02:39 +1000230 } else if (mac->type == SSH_UMAC128) {
231 if (mac->umac_ctx != NULL)
232 umac128_delete(mac->umac_ctx);
Damien Miller4e8d9372014-02-04 11:02:42 +1100233 } else if (mac->hmac_ctx != NULL)
Damien Miller1f0311c2014-05-15 14:24:09 +1000234#endif
Damien Miller4e8d9372014-02-04 11:02:42 +1100235 ssh_hmac_free(mac->hmac_ctx);
236 mac->hmac_ctx = NULL;
Damien Millere45796f2007-06-11 14:01:42 +1000237 mac->umac_ctx = NULL;
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000238}
239
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000240/* XXX copied from ciphers_valid */
241#define MAC_SEP ","
242int
243mac_valid(const char *names)
244{
245 char *maclist, *cp, *p;
246
247 if (names == NULL || strcmp(names, "") == 0)
248 return (0);
249 maclist = cp = xstrdup(names);
250 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100251 (p = strsep(&cp, MAC_SEP))) {
Darren Tucker5f3d5be2007-06-05 18:30:18 +1000252 if (mac_setup(NULL, p) < 0) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000253 debug("bad mac %s [%s]", p, names);
Darren Tuckera627d422013-06-02 07:31:17 +1000254 free(maclist);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000255 return (0);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000256 }
257 }
258 debug3("macs ok: [%s]", names);
Darren Tuckera627d422013-06-02 07:31:17 +1000259 free(maclist);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000260 return (1);
261}