1 /* $OpenBSD: mac.c,v 1.21 2012/12/11 22:51:45 sthen 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 "umac.h" 43 44 #define SSH_EVP 1 /* OpenSSL EVP-based MAC */ 45 #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */ 46 #define SSH_UMAC128 3 47 48 struct { 49 char *name; 50 int type; 51 const EVP_MD * (*mdfunc)(void); 52 int truncatebits; /* truncate digest if != 0 */ 53 int key_len; /* just for UMAC */ 54 int len; /* just for UMAC */ 55 int etm; /* Encrypt-then-MAC */ 56 } macs[] = { 57 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */ 58 { "hmac-sha1", SSH_EVP, EVP_sha1, 0, 0, 0, 0 }, 59 { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, 0, 0, 0 }, 60 { "hmac-sha2-256", SSH_EVP, EVP_sha256, 0, 0, 0, 0 }, 61 { "hmac-sha2-512", SSH_EVP, EVP_sha512, 0, 0, 0, 0 }, 62 { "hmac-md5", SSH_EVP, EVP_md5, 0, 0, 0, 0 }, 63 { "hmac-md5-96", SSH_EVP, EVP_md5, 96, 0, 0, 0 }, 64 { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 }, 65 { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 }, 66 { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 0 }, 67 { "umac-128@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 0 }, 68 69 /* Encrypt-then-MAC variants */ 70 { "hmac-sha1-etm@openssh.com", SSH_EVP, EVP_sha1, 0, 0, 0, 1 }, 71 { "hmac-sha1-96-etm@openssh.com", SSH_EVP, EVP_sha1, 96, 0, 0, 1 }, 72 { "hmac-sha2-256-etm@openssh.com", SSH_EVP, EVP_sha256, 0, 0, 0, 1 }, 73 { "hmac-sha2-512-etm@openssh.com", SSH_EVP, EVP_sha512, 0, 0, 0, 1 }, 74 { "hmac-md5-etm@openssh.com", SSH_EVP, EVP_md5, 0, 0, 0, 1 }, 75 { "hmac-md5-96-etm@openssh.com", SSH_EVP, EVP_md5, 96, 0, 0, 1 }, 76 { "hmac-ripemd160-etm@openssh.com", SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 }, 77 { "umac-64-etm@openssh.com", SSH_UMAC, NULL, 0, 128, 64, 1 }, 78 { "umac-128-etm@openssh.com", SSH_UMAC128, NULL, 0, 128, 128, 1 }, 79 80 { NULL, 0, NULL, 0, 0, 0, 0 } 81 }; 82 83 static void 84 mac_setup_by_id(Mac *mac, int which) 85 { 86 int evp_len; 87 mac->type = macs[which].type; 88 if (mac->type == SSH_EVP) { 89 mac->evp_md = (*macs[which].mdfunc)(); 90 if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0) 91 fatal("mac %s len %d", mac->name, evp_len); 92 mac->key_len = mac->mac_len = (u_int)evp_len; 93 } else { 94 mac->mac_len = macs[which].len / 8; 95 mac->key_len = macs[which].key_len / 8; 96 mac->umac_ctx = NULL; 97 } 98 if (macs[which].truncatebits != 0) 99 mac->mac_len = macs[which].truncatebits / 8; 100 mac->etm = macs[which].etm; 101 } 102 103 int 104 mac_setup(Mac *mac, char *name) 105 { 106 int i; 107 108 for (i = 0; macs[i].name; i++) { 109 if (strcmp(name, macs[i].name) == 0) { 110 if (mac != NULL) 111 mac_setup_by_id(mac, i); 112 debug2("mac_setup: found %s", name); 113 return (0); 114 } 115 } 116 debug2("mac_setup: unknown %s", name); 117 return (-1); 118 } 119 120 int 121 mac_init(Mac *mac) 122 { 123 if (mac->key == NULL) 124 fatal("mac_init: no key"); 125 switch (mac->type) { 126 case SSH_EVP: 127 if (mac->evp_md == NULL) 128 return -1; 129 HMAC_CTX_init(&mac->evp_ctx); 130 HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md); 131 return 0; 132 case SSH_UMAC: 133 mac->umac_ctx = umac_new(mac->key); 134 return 0; 135 case SSH_UMAC128: 136 mac->umac_ctx = umac128_new(mac->key); 137 return 0; 138 default: 139 return -1; 140 } 141 } 142 143 u_char * 144 mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) 145 { 146 static u_char m[EVP_MAX_MD_SIZE]; 147 u_char b[4], nonce[8]; 148 149 if (mac->mac_len > sizeof(m)) 150 fatal("mac_compute: mac too long %u %lu", 151 mac->mac_len, (u_long)sizeof(m)); 152 153 switch (mac->type) { 154 case SSH_EVP: 155 put_u32(b, seqno); 156 /* reset HMAC context */ 157 HMAC_Init(&mac->evp_ctx, NULL, 0, NULL); 158 HMAC_Update(&mac->evp_ctx, b, sizeof(b)); 159 HMAC_Update(&mac->evp_ctx, data, datalen); 160 HMAC_Final(&mac->evp_ctx, m, NULL); 161 break; 162 case SSH_UMAC: 163 put_u64(nonce, seqno); 164 umac_update(mac->umac_ctx, data, datalen); 165 umac_final(mac->umac_ctx, m, nonce); 166 break; 167 case SSH_UMAC128: 168 put_u64(nonce, seqno); 169 umac128_update(mac->umac_ctx, data, datalen); 170 umac128_final(mac->umac_ctx, m, nonce); 171 break; 172 default: 173 fatal("mac_compute: unknown MAC type"); 174 } 175 return (m); 176 } 177 178 void 179 mac_clear(Mac *mac) 180 { 181 if (mac->type == SSH_UMAC) { 182 if (mac->umac_ctx != NULL) 183 umac_delete(mac->umac_ctx); 184 } else if (mac->type == SSH_UMAC128) { 185 if (mac->umac_ctx != NULL) 186 umac128_delete(mac->umac_ctx); 187 } else if (mac->evp_md != NULL) 188 HMAC_cleanup(&mac->evp_ctx); 189 mac->evp_md = NULL; 190 mac->umac_ctx = NULL; 191 } 192 193 /* XXX copied from ciphers_valid */ 194 #define MAC_SEP "," 195 int 196 mac_valid(const char *names) 197 { 198 char *maclist, *cp, *p; 199 200 if (names == NULL || strcmp(names, "") == 0) 201 return (0); 202 maclist = cp = xstrdup(names); 203 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 204 (p = strsep(&cp, MAC_SEP))) { 205 if (mac_setup(NULL, p) < 0) { 206 debug("bad mac %s [%s]", p, names); 207 xfree(maclist); 208 return (0); 209 } else { 210 debug3("mac ok: %s [%s]", p, names); 211 } 212 } 213 debug3("macs ok: [%s]", names); 214 xfree(maclist); 215 return (1); 216 } 217