1 /* $NetBSD: mac.c,v 1.8 2013/11/08 19:18:25 christos Exp $ */ 2 /* $OpenBSD: mac.c,v 1.24 2013/06/03 00:03:18 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.8 2013/11/08 19:18:25 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 #define SSH_UMAC128 3 52 53 struct macalg { 54 const char *name; 55 int type; 56 const EVP_MD * (*mdfunc)(void); 57 int truncatebits; /* truncate digest if != 0 */ 58 int key_len; /* just for UMAC */ 59 int len; /* just for UMAC */ 60 int etm; /* Encrypt-then-MAC */ 61 }; 62 63 static const struct macalg macs[] = { 64 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */ 65 { "hmac-sha1", SSH_EVP, EVP_sha1, 0, 0, 0, 0 }, 66 { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, 0, 0, 0 }, 67 { "hmac-sha2-256", SSH_EVP, EVP_sha256, 0, 0, 0, 0 }, 68 { "hmac-sha2-512", SSH_EVP, EVP_sha512, 0, 0, 0, 0 }, 69 { "hmac-md5", SSH_EVP, EVP_md5, 0, 0, 0, 0 }, 70 { "hmac-md5-96", SSH_EVP, EVP_md5, 96, 0, 0, 0 }, 71 { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 }, 72 { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 }, 73 #ifdef UMAC_HAS_BEEN_UNBROKEN 74 { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 0 }, 75 { "umac-128@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 0 }, 76 #endif 77 78 /* Encrypt-then-MAC variants */ 79 { "hmac-sha1-etm@openssh.com", SSH_EVP, EVP_sha1, 0, 0, 0, 1 }, 80 { "hmac-sha1-96-etm@openssh.com", SSH_EVP, EVP_sha1, 96, 0, 0, 1 }, 81 { "hmac-sha2-256-etm@openssh.com", SSH_EVP, EVP_sha256, 0, 0, 0, 1 }, 82 { "hmac-sha2-512-etm@openssh.com", SSH_EVP, EVP_sha512, 0, 0, 0, 1 }, 83 { "hmac-md5-etm@openssh.com", SSH_EVP, EVP_md5, 0, 0, 0, 1 }, 84 { "hmac-md5-96-etm@openssh.com", SSH_EVP, EVP_md5, 96, 0, 0, 1 }, 85 { "hmac-ripemd160-etm@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 }, 86 { "umac-64-etm@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 1 }, 87 { "umac-128-etm@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 1 }, 88 89 { NULL, 0, NULL, 0, 0, 0, 0 } 90 }; 91 92 /* Returns a comma-separated list of supported MACs. */ 93 char * 94 mac_alg_list(void) 95 { 96 char *ret = NULL; 97 size_t nlen, rlen = 0; 98 const struct macalg *m; 99 100 for (m = macs; m->name != NULL; m++) { 101 if (ret != NULL) 102 ret[rlen++] = '\n'; 103 nlen = strlen(m->name); 104 ret = xrealloc(ret, 1, rlen + nlen + 2); 105 memcpy(ret + rlen, m->name, nlen + 1); 106 rlen += nlen; 107 } 108 return ret; 109 } 110 111 static void 112 mac_setup_by_alg(Mac *mac, const struct macalg *macalg) 113 { 114 int evp_len; 115 116 mac->type = macalg->type; 117 if (mac->type == SSH_EVP) { 118 mac->evp_md = macalg->mdfunc(); 119 if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0) 120 fatal("mac %s len %d", mac->name, evp_len); 121 mac->key_len = mac->mac_len = (u_int)evp_len; 122 } else { 123 mac->mac_len = macalg->len / 8; 124 mac->key_len = macalg->key_len / 8; 125 mac->umac_ctx = NULL; 126 } 127 if (macalg->truncatebits != 0) 128 mac->mac_len = macalg->truncatebits / 8; 129 mac->etm = macalg->etm; 130 } 131 132 int 133 mac_setup(Mac *mac, char *name) 134 { 135 const struct macalg *m; 136 137 for (m = macs; m->name != NULL; m++) { 138 if (strcmp(name, m->name) != 0) 139 continue; 140 if (mac != NULL) 141 mac_setup_by_alg(mac, m); 142 debug2("mac_setup: found %s", name); 143 return (0); 144 } 145 debug2("mac_setup: unknown %s", name); 146 return (-1); 147 } 148 149 int 150 mac_init(Mac *mac) 151 { 152 if (mac->key == NULL) 153 fatal("mac_init: no key"); 154 switch (mac->type) { 155 case SSH_EVP: 156 if (mac->evp_md == NULL) 157 return -1; 158 HMAC_CTX_init(&mac->evp_ctx); 159 HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); 160 return 0; 161 #ifdef UMAC_HAS_BEEN_UNBROKEN 162 case SSH_UMAC: 163 mac->umac_ctx = umac_new(mac->key); 164 return 0; 165 case SSH_UMAC128: 166 mac->umac_ctx = umac128_new(mac->key); 167 return 0; 168 #endif 169 default: 170 return -1; 171 } 172 } 173 174 u_char * 175 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) 176 { 177 static union { 178 u_char m[EVP_MAX_MD_SIZE]; 179 u_int64_t for_align; 180 } u; 181 u_char b[4]; 182 #ifdef UMAC_HAS_BEEN_UNBROKEN 183 u_char nonce[8]; 184 #endif 185 186 if (mac->mac_len > sizeof(u)) 187 fatal("mac_compute: mac too long %u %lu", 188 mac->mac_len, (u_long)sizeof(u)); 189 190 switch (mac->type) { 191 case SSH_EVP: 192 put_u32(b, seqno); 193 /* reset HMAC context */ 194 HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); 195 HMAC_Update(&mac->evp_ctx, b, sizeof(b)); 196 HMAC_Update(&mac->evp_ctx, data, datalen); 197 HMAC_Final(&mac->evp_ctx, u.m, NULL); 198 break; 199 #ifdef UMAC_HAS_BEEN_UNBROKEN 200 case SSH_UMAC: 201 put_u64(nonce, seqno); 202 umac_update(mac->umac_ctx, data, datalen); 203 umac_final(mac->umac_ctx, u.m, nonce); 204 break; 205 case SSH_UMAC128: 206 put_u64(nonce, seqno); 207 umac128_update(mac->umac_ctx, data, datalen); 208 umac128_final(mac->umac_ctx, u.m, nonce); 209 break; 210 #endif 211 default: 212 fatal("mac_compute: unknown MAC type"); 213 } 214 return (u.m); 215 } 216 217 void 218 mac_clear(Mac *mac) 219 { 220 #ifdef UMAC_HAS_BEEN_UNBROKEN 221 if (mac->type == SSH_UMAC) { 222 if (mac->umac_ctx != NULL) 223 umac_delete(mac->umac_ctx); 224 } else if (mac->type == SSH_UMAC128) { 225 if (mac->umac_ctx != NULL) 226 umac128_delete(mac->umac_ctx); 227 } else 228 #endif 229 if (mac->evp_md != NULL) 230 HMAC_cleanup(&mac->evp_ctx); 231 mac->evp_md = NULL; 232 mac->umac_ctx = NULL; 233 } 234 235 /* XXX copied from ciphers_valid */ 236 #define MAC_SEP "," 237 int 238 mac_valid(const char *names) 239 { 240 char *maclist, *cp, *p; 241 242 if (names == NULL || strcmp(names, "") == 0) 243 return (0); 244 maclist = cp = xstrdup(names); 245 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 246 (p = strsep(&cp, MAC_SEP))) { 247 if (mac_setup(NULL, p) < 0) { 248 debug("bad mac %s [%s]", p, names); 249 free(maclist); 250 return (0); 251 } else { 252 debug3("mac ok: %s [%s]", p, names); 253 } 254 } 255 debug3("macs ok: [%s]", names); 256 free(maclist); 257 return (1); 258 } 259