1 /* $NetBSD: mac.c,v 1.2 2009/06/07 22:38:46 christos Exp $ */ 2 /* $OpenBSD: mac.c,v 1.15 2008/06/13 00:51:47 dtucker Exp $ */ 3 /* 4 * Copyright (c) 2001 Markus Friedl. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "includes.h" 28 __RCSID("$NetBSD: mac.c,v 1.2 2009/06/07 22:38:46 christos Exp $"); 29 #include <sys/types.h> 30 31 #include <openssl/hmac.h> 32 33 #include <string.h> 34 #include <signal.h> 35 36 #include "xmalloc.h" 37 #include "log.h" 38 #include "cipher.h" 39 #include "buffer.h" 40 #include "key.h" 41 #include "kex.h" 42 #include "mac.h" 43 #include "misc.h" 44 45 #ifdef UMAC_HAS_BEEN_UNBROKEN 46 #include "umac.h" 47 #endif 48 49 #define SSH_EVP 1 /* OpenSSL EVP-based MAC */ 50 #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */ 51 52 struct { 53 char *name; 54 int type; 55 const EVP_MD * (*mdfunc)(void); 56 int truncatebits; /* truncate digest if != 0 */ 57 int key_len; /* just for UMAC */ 58 int len; /* just for UMAC */ 59 } macs[] = { 60 { "hmac-sha1", SSH_EVP, EVP_sha1, 0, -1, -1 }, 61 { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, -1, -1 }, 62 { "hmac-md5", SSH_EVP, EVP_md5, 0, -1, -1 }, 63 { "hmac-md5-96", SSH_EVP, EVP_md5, 96, -1, -1 }, 64 { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, -1, -1 }, 65 { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1, -1 }, 66 #ifdef UMAC_HAS_BEEN_UNBROKEN 67 { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64 }, 68 #endif 69 { NULL, 0, NULL, 0, -1, -1 } 70 }; 71 72 static void 73 mac_setup_by_id(Mac *mac, int which) 74 { 75 int evp_len; 76 mac->type = macs[which].type; 77 if (mac->type == SSH_EVP) { 78 mac->evp_md = (*macs[which].mdfunc)(); 79 if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0) 80 fatal("mac %s len %d", mac->name, evp_len); 81 mac->key_len = mac->mac_len = (u_int)evp_len; 82 } else { 83 mac->mac_len = macs[which].len / 8; 84 mac->key_len = macs[which].key_len / 8; 85 mac->umac_ctx = NULL; 86 } 87 if (macs[which].truncatebits != 0) 88 mac->mac_len = macs[which].truncatebits / 8; 89 } 90 91 int 92 mac_setup(Mac *mac, char *name) 93 { 94 int i; 95 96 for (i = 0; macs[i].name; i++) { 97 if (strcmp(name, macs[i].name) == 0) { 98 if (mac != NULL) 99 mac_setup_by_id(mac, i); 100 debug2("mac_setup: found %s", name); 101 return (0); 102 } 103 } 104 debug2("mac_setup: unknown %s", name); 105 return (-1); 106 } 107 108 int 109 mac_init(Mac *mac) 110 { 111 if (mac->key == NULL) 112 fatal("mac_init: no key"); 113 switch (mac->type) { 114 case SSH_EVP: 115 if (mac->evp_md == NULL) 116 return -1; 117 HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); 118 return 0; 119 #ifdef UMAC_HAS_BEEN_UNBROKEN 120 case SSH_UMAC: 121 mac->umac_ctx = umac_new(mac->key); 122 return 0; 123 #endif 124 default: 125 return -1; 126 } 127 } 128 129 u_char * 130 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) 131 { 132 static u_char m[EVP_MAX_MD_SIZE]; 133 u_char b[4]; 134 #ifdef UMAC_HAS_BEEN_UNBROKEN 135 u_char nonce[8]; 136 #endif 137 138 if (mac->mac_len > sizeof(m)) 139 fatal("mac_compute: mac too long %u %lu", 140 mac->mac_len, (u_long)sizeof(m)); 141 142 switch (mac->type) { 143 case SSH_EVP: 144 put_u32(b, seqno); 145 /* reset HMAC context */ 146 HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); 147 HMAC_Update(&mac->evp_ctx, b, sizeof(b)); 148 HMAC_Update(&mac->evp_ctx, data, datalen); 149 HMAC_Final(&mac->evp_ctx, m, NULL); 150 break; 151 #ifdef UMAC_HAS_BEEN_UNBROKEN 152 case SSH_UMAC: 153 put_u64(nonce, seqno); 154 umac_update(mac->umac_ctx, data, datalen); 155 umac_final(mac->umac_ctx, m, nonce); 156 break; 157 #endif 158 default: 159 fatal("mac_compute: unknown MAC type"); 160 } 161 return (m); 162 } 163 164 void 165 mac_clear(Mac *mac) 166 { 167 #ifdef UMAC_HAS_BEEN_UNBROKEN 168 if (mac->type == SSH_UMAC) { 169 if (mac->umac_ctx != NULL) 170 umac_delete(mac->umac_ctx); 171 } else 172 #endif 173 if (mac->evp_md != NULL) 174 HMAC_cleanup(&mac->evp_ctx); 175 mac->evp_md = NULL; 176 mac->umac_ctx = NULL; 177 } 178 179 /* XXX copied from ciphers_valid */ 180 #define MAC_SEP "," 181 int 182 mac_valid(const char *names) 183 { 184 char *maclist, *cp, *p; 185 186 if (names == NULL || strcmp(names, "") == 0) 187 return (0); 188 maclist = cp = xstrdup(names); 189 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 190 (p = strsep(&cp, MAC_SEP))) { 191 if (mac_setup(NULL, p) < 0) { 192 debug("bad mac %s [%s]", p, names); 193 xfree(maclist); 194 return (0); 195 } else { 196 debug3("mac ok: %s [%s]", p, names); 197 } 198 } 199 debug3("macs ok: [%s]", names); 200 xfree(maclist); 201 return (1); 202 } 203