1 /* $OpenBSD: mac.c,v 1.28 2014/02/07 06:55:54 djm Exp $ */ 2 /* 3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/types.h> 27 28 #include <openssl/hmac.h> 29 30 #include <string.h> 31 #include <signal.h> 32 33 #include "xmalloc.h" 34 #include "log.h" 35 #include "cipher.h" 36 #include "buffer.h" 37 #include "key.h" 38 #include "kex.h" 39 #include "mac.h" 40 #include "misc.h" 41 42 #include "digest.h" 43 #include "hmac.h" 44 #include "umac.h" 45 46 #define SSH_DIGEST 1 /* SSH_DIGEST_XXX */ 47 #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */ 48 #define SSH_UMAC128 3 49 50 struct macalg { 51 char *name; 52 int type; 53 int alg; 54 int truncatebits; /* truncate digest if != 0 */ 55 int key_len; /* just for UMAC */ 56 int len; /* just for UMAC */ 57 int etm; /* Encrypt-then-MAC */ 58 }; 59 60 static const struct macalg macs[] = { 61 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */ 62 { "hmac-sha1", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 }, 63 { "hmac-sha1-96", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 }, 64 { "hmac-sha2-256", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 }, 65 { "hmac-sha2-512", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 }, 66 { "hmac-md5", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 }, 67 { "hmac-md5-96", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 }, 68 { "hmac-ripemd160", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 }, 69 { "hmac-ripemd160@openssh.com", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 0 }, 70 { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 }, 71 { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 }, 72 73 /* Encrypt-then-MAC variants */ 74 { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 }, 75 { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 }, 76 { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 }, 77 { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 }, 78 { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 }, 79 { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 }, 80 { "hmac-ripemd160-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_RIPEMD160, 0, 0, 0, 1 }, 81 { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 }, 82 { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 }, 83 84 { NULL, 0, 0, 0, 0, 0, 0 } 85 }; 86 87 /* Returns a list of supported MACs separated by the specified char. */ 88 char * 89 mac_alg_list(char sep) 90 { 91 char *ret = NULL; 92 size_t nlen, rlen = 0; 93 const struct macalg *m; 94 95 for (m = macs; m->name != NULL; m++) { 96 if (ret != NULL) 97 ret[rlen++] = sep; 98 nlen = strlen(m->name); 99 ret = xrealloc(ret, 1, rlen + nlen + 2); 100 memcpy(ret + rlen, m->name, nlen + 1); 101 rlen += nlen; 102 } 103 return ret; 104 } 105 106 static void 107 mac_setup_by_alg(Mac *mac, const struct macalg *macalg) 108 { 109 mac->type = macalg->type; 110 if (mac->type == SSH_DIGEST) { 111 if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL) 112 fatal("ssh_hmac_start(alg=%d) failed", macalg->alg); 113 mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg); 114 } else { 115 mac->mac_len = macalg->len / 8; 116 mac->key_len = macalg->key_len / 8; 117 mac->umac_ctx = NULL; 118 } 119 if (macalg->truncatebits != 0) 120 mac->mac_len = macalg->truncatebits / 8; 121 mac->etm = macalg->etm; 122 } 123 124 int 125 mac_setup(Mac *mac, char *name) 126 { 127 const struct macalg *m; 128 129 for (m = macs; m->name != NULL; m++) { 130 if (strcmp(name, m->name) != 0) 131 continue; 132 if (mac != NULL) { 133 mac_setup_by_alg(mac, m); 134 debug2("mac_setup: setup %s", name); 135 } 136 return (0); 137 } 138 debug2("mac_setup: unknown %s", name); 139 return (-1); 140 } 141 142 int 143 mac_init(Mac *mac) 144 { 145 if (mac->key == NULL) 146 fatal("%s: no key", __func__); 147 switch (mac->type) { 148 case SSH_DIGEST: 149 if (mac->hmac_ctx == NULL || 150 ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0) 151 return -1; 152 return 0; 153 case SSH_UMAC: 154 mac->umac_ctx = umac_new(mac->key); 155 return 0; 156 case SSH_UMAC128: 157 mac->umac_ctx = umac128_new(mac->key); 158 return 0; 159 default: 160 return -1; 161 } 162 } 163 164 u_char * 165 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) 166 { 167 static union { 168 u_char m[EVP_MAX_MD_SIZE]; 169 u_int64_t for_align; 170 } u; 171 u_char b[4], nonce[8]; 172 173 if (mac->mac_len > sizeof(u)) 174 fatal("mac_compute: mac too long %u %zu", 175 mac->mac_len, sizeof(u)); 176 177 switch (mac->type) { 178 case SSH_DIGEST: 179 put_u32(b, seqno); 180 /* reset HMAC context */ 181 if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 || 182 ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 || 183 ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 || 184 ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0) 185 fatal("ssh_hmac failed"); 186 break; 187 case SSH_UMAC: 188 put_u64(nonce, seqno); 189 umac_update(mac->umac_ctx, data, datalen); 190 umac_final(mac->umac_ctx, u.m, nonce); 191 break; 192 case SSH_UMAC128: 193 put_u64(nonce, seqno); 194 umac128_update(mac->umac_ctx, data, datalen); 195 umac128_final(mac->umac_ctx, u.m, nonce); 196 break; 197 default: 198 fatal("mac_compute: unknown MAC type"); 199 } 200 return (u.m); 201 } 202 203 void 204 mac_clear(Mac *mac) 205 { 206 if (mac->type == SSH_UMAC) { 207 if (mac->umac_ctx != NULL) 208 umac_delete(mac->umac_ctx); 209 } else if (mac->type == SSH_UMAC128) { 210 if (mac->umac_ctx != NULL) 211 umac128_delete(mac->umac_ctx); 212 } else if (mac->hmac_ctx != NULL) 213 ssh_hmac_free(mac->hmac_ctx); 214 mac->hmac_ctx = NULL; 215 mac->umac_ctx = NULL; 216 } 217 218 /* XXX copied from ciphers_valid */ 219 #define MAC_SEP "," 220 int 221 mac_valid(const char *names) 222 { 223 char *maclist, *cp, *p; 224 225 if (names == NULL || strcmp(names, "") == 0) 226 return (0); 227 maclist = cp = xstrdup(names); 228 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 229 (p = strsep(&cp, MAC_SEP))) { 230 if (mac_setup(NULL, p) < 0) { 231 debug("bad mac %s [%s]", p, names); 232 free(maclist); 233 return (0); 234 } 235 } 236 debug3("macs ok: [%s]", names); 237 free(maclist); 238 return (1); 239 } 240