17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate * are met:
77c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include "includes.h"
267c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: mac.c,v 1.5 2002/05/16 22:02:50 markus Exp $");
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <openssl/hmac.h>
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include "xmalloc.h"
317c478bd9Sstevel@tonic-gate #include "getput.h"
327c478bd9Sstevel@tonic-gate #include "log.h"
337c478bd9Sstevel@tonic-gate #include "cipher.h"
347c478bd9Sstevel@tonic-gate #include "kex.h"
357c478bd9Sstevel@tonic-gate #include "mac.h"
36*8caf082fSJan Pechanec #include "misc.h"
37*8caf082fSJan Pechanec
38*8caf082fSJan Pechanec #define SSH_EVP 1 /* OpenSSL EVP-based MAC */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate struct {
417c478bd9Sstevel@tonic-gate char *name;
42*8caf082fSJan Pechanec int type;
437c478bd9Sstevel@tonic-gate const EVP_MD * (*mdfunc)(void);
447c478bd9Sstevel@tonic-gate int truncatebits; /* truncate digest if != 0 */
45*8caf082fSJan Pechanec int key_len; /* will be used if we have UMAC */
467c478bd9Sstevel@tonic-gate } macs[] = {
47*8caf082fSJan Pechanec { "hmac-sha1", SSH_EVP, EVP_sha1, 0, -1 },
48*8caf082fSJan Pechanec { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, -1 },
49*8caf082fSJan Pechanec { "hmac-md5", SSH_EVP, EVP_md5, 0, -1 },
50*8caf082fSJan Pechanec { "hmac-md5-96", SSH_EVP, EVP_md5, 96, -1 },
517c478bd9Sstevel@tonic-gate #ifdef SOLARIS_SSH_ENABLE_RIPEMD160
52*8caf082fSJan Pechanec { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, -1 },
53*8caf082fSJan Pechanec { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1 },
547c478bd9Sstevel@tonic-gate #endif /* SOLARIS_SSH_ENABLE_RIPEMD160 */
55*8caf082fSJan Pechanec { NULL, 0, NULL, 0, -1 }
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate
58*8caf082fSJan Pechanec static void
mac_setup_by_id(Mac * mac,int which)59*8caf082fSJan Pechanec mac_setup_by_id(Mac *mac, int which)
60*8caf082fSJan Pechanec {
61*8caf082fSJan Pechanec int evp_len;
62*8caf082fSJan Pechanec mac->type = macs[which].type;
63*8caf082fSJan Pechanec if (mac->type == SSH_EVP) {
64*8caf082fSJan Pechanec mac->evp_md = (*macs[which].mdfunc)();
65*8caf082fSJan Pechanec if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
66*8caf082fSJan Pechanec fatal("mac %s len %d", mac->name, evp_len);
67*8caf082fSJan Pechanec mac->key_len = mac->mac_len = (u_int)evp_len;
68*8caf082fSJan Pechanec } else
69*8caf082fSJan Pechanec fatal("wrong MAC type (%d)", mac->type);
70*8caf082fSJan Pechanec if (macs[which].truncatebits != 0)
71*8caf082fSJan Pechanec mac->mac_len = macs[which].truncatebits / 8;
72*8caf082fSJan Pechanec }
73*8caf082fSJan Pechanec
747c478bd9Sstevel@tonic-gate int
mac_setup(Mac * mac,char * name)75*8caf082fSJan Pechanec mac_setup(Mac *mac, char *name)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate int i;
78*8caf082fSJan Pechanec
797c478bd9Sstevel@tonic-gate for (i = 0; macs[i].name; i++) {
807c478bd9Sstevel@tonic-gate if (strcmp(name, macs[i].name) == 0) {
81*8caf082fSJan Pechanec if (mac != NULL)
82*8caf082fSJan Pechanec mac_setup_by_id(mac, i);
83*8caf082fSJan Pechanec debug2("mac_setup: found %s", name);
847c478bd9Sstevel@tonic-gate return (0);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate }
87*8caf082fSJan Pechanec debug2("mac_setup: unknown %s", name);
887c478bd9Sstevel@tonic-gate return (-1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
91*8caf082fSJan Pechanec int
mac_init(Mac * mac)92*8caf082fSJan Pechanec mac_init(Mac *mac)
93*8caf082fSJan Pechanec {
94*8caf082fSJan Pechanec if (mac->key == NULL)
95*8caf082fSJan Pechanec fatal("mac_init: no key");
96*8caf082fSJan Pechanec switch (mac->type) {
97*8caf082fSJan Pechanec case SSH_EVP:
98*8caf082fSJan Pechanec if (mac->evp_md == NULL)
99*8caf082fSJan Pechanec return -1;
100*8caf082fSJan Pechanec HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
101*8caf082fSJan Pechanec return 0;
102*8caf082fSJan Pechanec default:
103*8caf082fSJan Pechanec return -1;
104*8caf082fSJan Pechanec }
105*8caf082fSJan Pechanec }
106*8caf082fSJan Pechanec
1077c478bd9Sstevel@tonic-gate u_char *
mac_compute(Mac * mac,u_int32_t seqno,u_char * data,int datalen)1087c478bd9Sstevel@tonic-gate mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate static u_char m[EVP_MAX_MD_SIZE];
1117c478bd9Sstevel@tonic-gate u_char b[4];
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (mac->mac_len > sizeof(m))
114*8caf082fSJan Pechanec fatal("mac_compute: mac too long %u %lu",
115*8caf082fSJan Pechanec mac->mac_len, (u_long)sizeof(m));
116*8caf082fSJan Pechanec
117*8caf082fSJan Pechanec switch (mac->type) {
118*8caf082fSJan Pechanec case SSH_EVP:
119*8caf082fSJan Pechanec put_u32(b, seqno);
120*8caf082fSJan Pechanec /* reset HMAC context */
121*8caf082fSJan Pechanec HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
122*8caf082fSJan Pechanec HMAC_Update(&mac->evp_ctx, b, sizeof(b));
123*8caf082fSJan Pechanec HMAC_Update(&mac->evp_ctx, data, datalen);
124*8caf082fSJan Pechanec HMAC_Final(&mac->evp_ctx, m, NULL);
125*8caf082fSJan Pechanec break;
126*8caf082fSJan Pechanec default:
127*8caf082fSJan Pechanec fatal("mac_compute: unknown MAC type");
128*8caf082fSJan Pechanec }
129*8caf082fSJan Pechanec
1307c478bd9Sstevel@tonic-gate return (m);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
133*8caf082fSJan Pechanec void
mac_clear(Mac * mac)134*8caf082fSJan Pechanec mac_clear(Mac *mac)
135*8caf082fSJan Pechanec {
136*8caf082fSJan Pechanec if (mac->evp_md != NULL)
137*8caf082fSJan Pechanec HMAC_cleanup(&mac->evp_ctx);
138*8caf082fSJan Pechanec mac->evp_md = NULL;
139*8caf082fSJan Pechanec }
140*8caf082fSJan Pechanec
1417c478bd9Sstevel@tonic-gate /* XXX copied from ciphers_valid */
1427c478bd9Sstevel@tonic-gate #define MAC_SEP ","
1437c478bd9Sstevel@tonic-gate int
mac_valid(const char * names)1447c478bd9Sstevel@tonic-gate mac_valid(const char *names)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate char *maclist, *cp, *p;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (names == NULL || strcmp(names, "") == 0)
1497c478bd9Sstevel@tonic-gate return (0);
1507c478bd9Sstevel@tonic-gate maclist = cp = xstrdup(names);
1517c478bd9Sstevel@tonic-gate for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1527c478bd9Sstevel@tonic-gate (p = strsep(&cp, MAC_SEP))) {
153*8caf082fSJan Pechanec if (mac_setup(NULL, p) < 0) {
1547c478bd9Sstevel@tonic-gate debug("bad mac %s [%s]", p, names);
1557c478bd9Sstevel@tonic-gate xfree(maclist);
1567c478bd9Sstevel@tonic-gate return (0);
1577c478bd9Sstevel@tonic-gate } else {
1587c478bd9Sstevel@tonic-gate debug3("mac ok: %s [%s]", p, names);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate debug3("macs ok: [%s]", names);
1627c478bd9Sstevel@tonic-gate xfree(maclist);
1637c478bd9Sstevel@tonic-gate return (1);
1647c478bd9Sstevel@tonic-gate }
165