1*0cbfa66cSDaniel Fojt /* $OpenBSD: mac.c,v 1.35 2019/09/06 04:53:27 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos * Copyright (c) 2001 Markus Friedl. All rights reserved.
418de8d7fSPeter Avalos *
518de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without
618de8d7fSPeter Avalos * modification, are permitted provided that the following conditions
718de8d7fSPeter Avalos * are met:
818de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright
918de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer.
1018de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1118de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the
1218de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution.
1318de8d7fSPeter Avalos *
1418de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1518de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1618de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1718de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1818de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1918de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2018de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2118de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2218de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2318de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2418de8d7fSPeter Avalos */
2518de8d7fSPeter Avalos
2618de8d7fSPeter Avalos #include "includes.h"
2718de8d7fSPeter Avalos
2818de8d7fSPeter Avalos #include <sys/types.h>
2918de8d7fSPeter Avalos
30*0cbfa66cSDaniel Fojt #include <stdlib.h>
3118de8d7fSPeter Avalos #include <string.h>
32e9778795SPeter Avalos #include <stdio.h>
3318de8d7fSPeter Avalos
3436e94dc5SPeter Avalos #include "digest.h"
3536e94dc5SPeter Avalos #include "hmac.h"
3618de8d7fSPeter Avalos #include "umac.h"
37e9778795SPeter Avalos #include "mac.h"
38e9778795SPeter Avalos #include "misc.h"
39e9778795SPeter Avalos #include "ssherr.h"
40e9778795SPeter Avalos #include "sshbuf.h"
4118de8d7fSPeter Avalos
4299e85e0dSPeter Avalos #include "openbsd-compat/openssl-compat.h"
4399e85e0dSPeter Avalos
4436e94dc5SPeter Avalos #define SSH_DIGEST 1 /* SSH_DIGEST_XXX */
4518de8d7fSPeter Avalos #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
4636e94dc5SPeter Avalos #define SSH_UMAC128 3
4718de8d7fSPeter Avalos
4836e94dc5SPeter Avalos struct macalg {
4918de8d7fSPeter Avalos char *name;
5018de8d7fSPeter Avalos int type;
5136e94dc5SPeter Avalos int alg;
5218de8d7fSPeter Avalos int truncatebits; /* truncate digest if != 0 */
5318de8d7fSPeter Avalos int key_len; /* just for UMAC */
5418de8d7fSPeter Avalos int len; /* just for UMAC */
5536e94dc5SPeter Avalos int etm; /* Encrypt-then-MAC */
5618de8d7fSPeter Avalos };
5718de8d7fSPeter Avalos
5836e94dc5SPeter Avalos static const struct macalg macs[] = {
5936e94dc5SPeter Avalos /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
6036e94dc5SPeter Avalos { "hmac-sha1", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
6136e94dc5SPeter Avalos { "hmac-sha1-96", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
6236e94dc5SPeter Avalos { "hmac-sha2-256", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
6336e94dc5SPeter Avalos { "hmac-sha2-512", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
6436e94dc5SPeter Avalos { "hmac-md5", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 },
6536e94dc5SPeter Avalos { "hmac-md5-96", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 },
6636e94dc5SPeter Avalos { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 },
6736e94dc5SPeter Avalos { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 },
6836e94dc5SPeter Avalos
6936e94dc5SPeter Avalos /* Encrypt-then-MAC variants */
7036e94dc5SPeter Avalos { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
7136e94dc5SPeter Avalos { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 },
7236e94dc5SPeter Avalos { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
7336e94dc5SPeter Avalos { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
7436e94dc5SPeter Avalos { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 },
7536e94dc5SPeter Avalos { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 },
7636e94dc5SPeter Avalos { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 },
7736e94dc5SPeter Avalos { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 },
7836e94dc5SPeter Avalos
7936e94dc5SPeter Avalos { NULL, 0, 0, 0, 0, 0, 0 }
8036e94dc5SPeter Avalos };
8136e94dc5SPeter Avalos
8236e94dc5SPeter Avalos /* Returns a list of supported MACs separated by the specified char. */
8336e94dc5SPeter Avalos char *
mac_alg_list(char sep)8436e94dc5SPeter Avalos mac_alg_list(char sep)
8518de8d7fSPeter Avalos {
86e9778795SPeter Avalos char *ret = NULL, *tmp;
8736e94dc5SPeter Avalos size_t nlen, rlen = 0;
8836e94dc5SPeter Avalos const struct macalg *m;
8936e94dc5SPeter Avalos
9036e94dc5SPeter Avalos for (m = macs; m->name != NULL; m++) {
9136e94dc5SPeter Avalos if (ret != NULL)
9236e94dc5SPeter Avalos ret[rlen++] = sep;
9336e94dc5SPeter Avalos nlen = strlen(m->name);
94e9778795SPeter Avalos if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
95e9778795SPeter Avalos free(ret);
96e9778795SPeter Avalos return NULL;
97e9778795SPeter Avalos }
98e9778795SPeter Avalos ret = tmp;
9936e94dc5SPeter Avalos memcpy(ret + rlen, m->name, nlen + 1);
10036e94dc5SPeter Avalos rlen += nlen;
10136e94dc5SPeter Avalos }
10236e94dc5SPeter Avalos return ret;
10336e94dc5SPeter Avalos }
10436e94dc5SPeter Avalos
105e9778795SPeter Avalos static int
mac_setup_by_alg(struct sshmac * mac,const struct macalg * macalg)106e9778795SPeter Avalos mac_setup_by_alg(struct sshmac *mac, const struct macalg *macalg)
10736e94dc5SPeter Avalos {
10836e94dc5SPeter Avalos mac->type = macalg->type;
10936e94dc5SPeter Avalos if (mac->type == SSH_DIGEST) {
11036e94dc5SPeter Avalos if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL)
111e9778795SPeter Avalos return SSH_ERR_ALLOC_FAIL;
11236e94dc5SPeter Avalos mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg);
11318de8d7fSPeter Avalos } else {
11436e94dc5SPeter Avalos mac->mac_len = macalg->len / 8;
11536e94dc5SPeter Avalos mac->key_len = macalg->key_len / 8;
11618de8d7fSPeter Avalos mac->umac_ctx = NULL;
11718de8d7fSPeter Avalos }
11836e94dc5SPeter Avalos if (macalg->truncatebits != 0)
11936e94dc5SPeter Avalos mac->mac_len = macalg->truncatebits / 8;
12036e94dc5SPeter Avalos mac->etm = macalg->etm;
121e9778795SPeter Avalos return 0;
12218de8d7fSPeter Avalos }
12318de8d7fSPeter Avalos
12418de8d7fSPeter Avalos int
mac_setup(struct sshmac * mac,char * name)125e9778795SPeter Avalos mac_setup(struct sshmac *mac, char *name)
12618de8d7fSPeter Avalos {
12736e94dc5SPeter Avalos const struct macalg *m;
12818de8d7fSPeter Avalos
12936e94dc5SPeter Avalos for (m = macs; m->name != NULL; m++) {
13036e94dc5SPeter Avalos if (strcmp(name, m->name) != 0)
13136e94dc5SPeter Avalos continue;
132e9778795SPeter Avalos if (mac != NULL)
133e9778795SPeter Avalos return mac_setup_by_alg(mac, m);
134e9778795SPeter Avalos return 0;
13518de8d7fSPeter Avalos }
136e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
13718de8d7fSPeter Avalos }
13818de8d7fSPeter Avalos
13918de8d7fSPeter Avalos int
mac_init(struct sshmac * mac)140e9778795SPeter Avalos mac_init(struct sshmac *mac)
14118de8d7fSPeter Avalos {
14218de8d7fSPeter Avalos if (mac->key == NULL)
143e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
14418de8d7fSPeter Avalos switch (mac->type) {
14536e94dc5SPeter Avalos case SSH_DIGEST:
14636e94dc5SPeter Avalos if (mac->hmac_ctx == NULL ||
14736e94dc5SPeter Avalos ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
148e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
14918de8d7fSPeter Avalos return 0;
15018de8d7fSPeter Avalos case SSH_UMAC:
151e9778795SPeter Avalos if ((mac->umac_ctx = umac_new(mac->key)) == NULL)
152e9778795SPeter Avalos return SSH_ERR_ALLOC_FAIL;
15318de8d7fSPeter Avalos return 0;
15436e94dc5SPeter Avalos case SSH_UMAC128:
155e9778795SPeter Avalos if ((mac->umac_ctx = umac128_new(mac->key)) == NULL)
156e9778795SPeter Avalos return SSH_ERR_ALLOC_FAIL;
15736e94dc5SPeter Avalos return 0;
15818de8d7fSPeter Avalos default:
159e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
16018de8d7fSPeter Avalos }
16118de8d7fSPeter Avalos }
16218de8d7fSPeter Avalos
163e9778795SPeter Avalos int
mac_compute(struct sshmac * mac,u_int32_t seqno,const u_char * data,int datalen,u_char * digest,size_t dlen)164e9778795SPeter Avalos mac_compute(struct sshmac *mac, u_int32_t seqno,
165e9778795SPeter Avalos const u_char *data, int datalen,
166e9778795SPeter Avalos u_char *digest, size_t dlen)
16718de8d7fSPeter Avalos {
16836e94dc5SPeter Avalos static union {
169e9778795SPeter Avalos u_char m[SSH_DIGEST_MAX_LENGTH];
17036e94dc5SPeter Avalos u_int64_t for_align;
17136e94dc5SPeter Avalos } u;
17236e94dc5SPeter Avalos u_char b[4];
17336e94dc5SPeter Avalos u_char nonce[8];
17418de8d7fSPeter Avalos
17536e94dc5SPeter Avalos if (mac->mac_len > sizeof(u))
176e9778795SPeter Avalos return SSH_ERR_INTERNAL_ERROR;
17718de8d7fSPeter Avalos
17818de8d7fSPeter Avalos switch (mac->type) {
17936e94dc5SPeter Avalos case SSH_DIGEST:
18018de8d7fSPeter Avalos put_u32(b, seqno);
18118de8d7fSPeter Avalos /* reset HMAC context */
18236e94dc5SPeter Avalos if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 ||
18336e94dc5SPeter Avalos ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 ||
18436e94dc5SPeter Avalos ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 ||
18536e94dc5SPeter Avalos ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0)
186e9778795SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
18718de8d7fSPeter Avalos break;
18818de8d7fSPeter Avalos case SSH_UMAC:
189e9778795SPeter Avalos POKE_U64(nonce, seqno);
19018de8d7fSPeter Avalos umac_update(mac->umac_ctx, data, datalen);
19136e94dc5SPeter Avalos umac_final(mac->umac_ctx, u.m, nonce);
19236e94dc5SPeter Avalos break;
19336e94dc5SPeter Avalos case SSH_UMAC128:
19436e94dc5SPeter Avalos put_u64(nonce, seqno);
19536e94dc5SPeter Avalos umac128_update(mac->umac_ctx, data, datalen);
19636e94dc5SPeter Avalos umac128_final(mac->umac_ctx, u.m, nonce);
19718de8d7fSPeter Avalos break;
19818de8d7fSPeter Avalos default:
199e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
20018de8d7fSPeter Avalos }
201e9778795SPeter Avalos if (digest != NULL) {
202e9778795SPeter Avalos if (dlen > mac->mac_len)
203e9778795SPeter Avalos dlen = mac->mac_len;
204e9778795SPeter Avalos memcpy(digest, u.m, dlen);
205e9778795SPeter Avalos }
206e9778795SPeter Avalos return 0;
207e9778795SPeter Avalos }
208e9778795SPeter Avalos
209e9778795SPeter Avalos int
mac_check(struct sshmac * mac,u_int32_t seqno,const u_char * data,size_t dlen,const u_char * theirmac,size_t mlen)210e9778795SPeter Avalos mac_check(struct sshmac *mac, u_int32_t seqno,
211e9778795SPeter Avalos const u_char *data, size_t dlen,
212e9778795SPeter Avalos const u_char *theirmac, size_t mlen)
213e9778795SPeter Avalos {
214e9778795SPeter Avalos u_char ourmac[SSH_DIGEST_MAX_LENGTH];
215e9778795SPeter Avalos int r;
216e9778795SPeter Avalos
217e9778795SPeter Avalos if (mac->mac_len > mlen)
218e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
219e9778795SPeter Avalos if ((r = mac_compute(mac, seqno, data, dlen,
220e9778795SPeter Avalos ourmac, sizeof(ourmac))) != 0)
221e9778795SPeter Avalos return r;
222e9778795SPeter Avalos if (timingsafe_bcmp(ourmac, theirmac, mac->mac_len) != 0)
223e9778795SPeter Avalos return SSH_ERR_MAC_INVALID;
224e9778795SPeter Avalos return 0;
22518de8d7fSPeter Avalos }
22618de8d7fSPeter Avalos
22718de8d7fSPeter Avalos void
mac_clear(struct sshmac * mac)228e9778795SPeter Avalos mac_clear(struct sshmac *mac)
22918de8d7fSPeter Avalos {
23018de8d7fSPeter Avalos if (mac->type == SSH_UMAC) {
23118de8d7fSPeter Avalos if (mac->umac_ctx != NULL)
23218de8d7fSPeter Avalos umac_delete(mac->umac_ctx);
23336e94dc5SPeter Avalos } else if (mac->type == SSH_UMAC128) {
23436e94dc5SPeter Avalos if (mac->umac_ctx != NULL)
23536e94dc5SPeter Avalos umac128_delete(mac->umac_ctx);
23636e94dc5SPeter Avalos } else if (mac->hmac_ctx != NULL)
23736e94dc5SPeter Avalos ssh_hmac_free(mac->hmac_ctx);
23836e94dc5SPeter Avalos mac->hmac_ctx = NULL;
23918de8d7fSPeter Avalos mac->umac_ctx = NULL;
24018de8d7fSPeter Avalos }
24118de8d7fSPeter Avalos
24218de8d7fSPeter Avalos /* XXX copied from ciphers_valid */
24318de8d7fSPeter Avalos #define MAC_SEP ","
24418de8d7fSPeter Avalos int
mac_valid(const char * names)24518de8d7fSPeter Avalos mac_valid(const char *names)
24618de8d7fSPeter Avalos {
24718de8d7fSPeter Avalos char *maclist, *cp, *p;
24818de8d7fSPeter Avalos
24918de8d7fSPeter Avalos if (names == NULL || strcmp(names, "") == 0)
250e9778795SPeter Avalos return 0;
251e9778795SPeter Avalos if ((maclist = cp = strdup(names)) == NULL)
252e9778795SPeter Avalos return 0;
25318de8d7fSPeter Avalos for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
25418de8d7fSPeter Avalos (p = strsep(&cp, MAC_SEP))) {
25518de8d7fSPeter Avalos if (mac_setup(NULL, p) < 0) {
25636e94dc5SPeter Avalos free(maclist);
257e9778795SPeter Avalos return 0;
25818de8d7fSPeter Avalos }
25918de8d7fSPeter Avalos }
26036e94dc5SPeter Avalos free(maclist);
261e9778795SPeter Avalos return 1;
26218de8d7fSPeter Avalos }
263