10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2001 Markus Friedl. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate #include "includes.h" 260Sstevel@tonic-gate RCSID("$OpenBSD: mac.c,v 1.5 2002/05/16 22:02:50 markus Exp $"); 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <openssl/hmac.h> 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include "xmalloc.h" 310Sstevel@tonic-gate #include "getput.h" 320Sstevel@tonic-gate #include "log.h" 330Sstevel@tonic-gate #include "cipher.h" 340Sstevel@tonic-gate #include "kex.h" 350Sstevel@tonic-gate #include "mac.h" 36*9486SJan.Pechanec@Sun.COM #include "misc.h" 37*9486SJan.Pechanec@Sun.COM 38*9486SJan.Pechanec@Sun.COM #define SSH_EVP 1 /* OpenSSL EVP-based MAC */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate struct { 410Sstevel@tonic-gate char *name; 42*9486SJan.Pechanec@Sun.COM int type; 430Sstevel@tonic-gate const EVP_MD * (*mdfunc)(void); 440Sstevel@tonic-gate int truncatebits; /* truncate digest if != 0 */ 45*9486SJan.Pechanec@Sun.COM int key_len; /* will be used if we have UMAC */ 460Sstevel@tonic-gate } macs[] = { 47*9486SJan.Pechanec@Sun.COM { "hmac-sha1", SSH_EVP, EVP_sha1, 0, -1 }, 48*9486SJan.Pechanec@Sun.COM { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, -1 }, 49*9486SJan.Pechanec@Sun.COM { "hmac-md5", SSH_EVP, EVP_md5, 0, -1 }, 50*9486SJan.Pechanec@Sun.COM { "hmac-md5-96", SSH_EVP, EVP_md5, 96, -1 }, 510Sstevel@tonic-gate #ifdef SOLARIS_SSH_ENABLE_RIPEMD160 52*9486SJan.Pechanec@Sun.COM { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, -1 }, 53*9486SJan.Pechanec@Sun.COM { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1 }, 540Sstevel@tonic-gate #endif /* SOLARIS_SSH_ENABLE_RIPEMD160 */ 55*9486SJan.Pechanec@Sun.COM { NULL, 0, NULL, 0, -1 } 560Sstevel@tonic-gate }; 570Sstevel@tonic-gate 58*9486SJan.Pechanec@Sun.COM static void 59*9486SJan.Pechanec@Sun.COM mac_setup_by_id(Mac *mac, int which) 60*9486SJan.Pechanec@Sun.COM { 61*9486SJan.Pechanec@Sun.COM int evp_len; 62*9486SJan.Pechanec@Sun.COM mac->type = macs[which].type; 63*9486SJan.Pechanec@Sun.COM if (mac->type == SSH_EVP) { 64*9486SJan.Pechanec@Sun.COM mac->evp_md = (*macs[which].mdfunc)(); 65*9486SJan.Pechanec@Sun.COM if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0) 66*9486SJan.Pechanec@Sun.COM fatal("mac %s len %d", mac->name, evp_len); 67*9486SJan.Pechanec@Sun.COM mac->key_len = mac->mac_len = (u_int)evp_len; 68*9486SJan.Pechanec@Sun.COM } else 69*9486SJan.Pechanec@Sun.COM fatal("wrong MAC type (%d)", mac->type); 70*9486SJan.Pechanec@Sun.COM if (macs[which].truncatebits != 0) 71*9486SJan.Pechanec@Sun.COM mac->mac_len = macs[which].truncatebits / 8; 72*9486SJan.Pechanec@Sun.COM } 73*9486SJan.Pechanec@Sun.COM 740Sstevel@tonic-gate int 75*9486SJan.Pechanec@Sun.COM mac_setup(Mac *mac, char *name) 760Sstevel@tonic-gate { 770Sstevel@tonic-gate int i; 78*9486SJan.Pechanec@Sun.COM 790Sstevel@tonic-gate for (i = 0; macs[i].name; i++) { 800Sstevel@tonic-gate if (strcmp(name, macs[i].name) == 0) { 81*9486SJan.Pechanec@Sun.COM if (mac != NULL) 82*9486SJan.Pechanec@Sun.COM mac_setup_by_id(mac, i); 83*9486SJan.Pechanec@Sun.COM debug2("mac_setup: found %s", name); 840Sstevel@tonic-gate return (0); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate } 87*9486SJan.Pechanec@Sun.COM debug2("mac_setup: unknown %s", name); 880Sstevel@tonic-gate return (-1); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 91*9486SJan.Pechanec@Sun.COM int 92*9486SJan.Pechanec@Sun.COM mac_init(Mac *mac) 93*9486SJan.Pechanec@Sun.COM { 94*9486SJan.Pechanec@Sun.COM if (mac->key == NULL) 95*9486SJan.Pechanec@Sun.COM fatal("mac_init: no key"); 96*9486SJan.Pechanec@Sun.COM switch (mac->type) { 97*9486SJan.Pechanec@Sun.COM case SSH_EVP: 98*9486SJan.Pechanec@Sun.COM if (mac->evp_md == NULL) 99*9486SJan.Pechanec@Sun.COM return -1; 100*9486SJan.Pechanec@Sun.COM HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); 101*9486SJan.Pechanec@Sun.COM return 0; 102*9486SJan.Pechanec@Sun.COM default: 103*9486SJan.Pechanec@Sun.COM return -1; 104*9486SJan.Pechanec@Sun.COM } 105*9486SJan.Pechanec@Sun.COM } 106*9486SJan.Pechanec@Sun.COM 1070Sstevel@tonic-gate u_char * 1080Sstevel@tonic-gate mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate static u_char m[EVP_MAX_MD_SIZE]; 1110Sstevel@tonic-gate u_char b[4]; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if (mac->mac_len > sizeof(m)) 114*9486SJan.Pechanec@Sun.COM fatal("mac_compute: mac too long %u %lu", 115*9486SJan.Pechanec@Sun.COM mac->mac_len, (u_long)sizeof(m)); 116*9486SJan.Pechanec@Sun.COM 117*9486SJan.Pechanec@Sun.COM switch (mac->type) { 118*9486SJan.Pechanec@Sun.COM case SSH_EVP: 119*9486SJan.Pechanec@Sun.COM put_u32(b, seqno); 120*9486SJan.Pechanec@Sun.COM /* reset HMAC context */ 121*9486SJan.Pechanec@Sun.COM HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); 122*9486SJan.Pechanec@Sun.COM HMAC_Update(&mac->evp_ctx, b, sizeof(b)); 123*9486SJan.Pechanec@Sun.COM HMAC_Update(&mac->evp_ctx, data, datalen); 124*9486SJan.Pechanec@Sun.COM HMAC_Final(&mac->evp_ctx, m, NULL); 125*9486SJan.Pechanec@Sun.COM break; 126*9486SJan.Pechanec@Sun.COM default: 127*9486SJan.Pechanec@Sun.COM fatal("mac_compute: unknown MAC type"); 128*9486SJan.Pechanec@Sun.COM } 129*9486SJan.Pechanec@Sun.COM 1300Sstevel@tonic-gate return (m); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 133*9486SJan.Pechanec@Sun.COM void 134*9486SJan.Pechanec@Sun.COM mac_clear(Mac *mac) 135*9486SJan.Pechanec@Sun.COM { 136*9486SJan.Pechanec@Sun.COM if (mac->evp_md != NULL) 137*9486SJan.Pechanec@Sun.COM HMAC_cleanup(&mac->evp_ctx); 138*9486SJan.Pechanec@Sun.COM mac->evp_md = NULL; 139*9486SJan.Pechanec@Sun.COM } 140*9486SJan.Pechanec@Sun.COM 1410Sstevel@tonic-gate /* XXX copied from ciphers_valid */ 1420Sstevel@tonic-gate #define MAC_SEP "," 1430Sstevel@tonic-gate int 1440Sstevel@tonic-gate mac_valid(const char *names) 1450Sstevel@tonic-gate { 1460Sstevel@tonic-gate char *maclist, *cp, *p; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (names == NULL || strcmp(names, "") == 0) 1490Sstevel@tonic-gate return (0); 1500Sstevel@tonic-gate maclist = cp = xstrdup(names); 1510Sstevel@tonic-gate for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 1520Sstevel@tonic-gate (p = strsep(&cp, MAC_SEP))) { 153*9486SJan.Pechanec@Sun.COM if (mac_setup(NULL, p) < 0) { 1540Sstevel@tonic-gate debug("bad mac %s [%s]", p, names); 1550Sstevel@tonic-gate xfree(maclist); 1560Sstevel@tonic-gate return (0); 1570Sstevel@tonic-gate } else { 1580Sstevel@tonic-gate debug3("mac ok: %s [%s]", p, names); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate debug3("macs ok: [%s]", names); 1620Sstevel@tonic-gate xfree(maclist); 1630Sstevel@tonic-gate return (1); 1640Sstevel@tonic-gate } 165