jcs's openbsd hax
openbsd
at jcs 249 lines 7.1 kB view raw
1/* $OpenBSD: mac.c,v 1.37 2025/09/05 10:01:35 dtucker 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 <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31 32#include "digest.h" 33#include "hmac.h" 34#include "umac.h" 35#include "mac.h" 36#include "misc.h" 37#include "ssherr.h" 38#include "sshbuf.h" 39 40#define SSH_DIGEST 1 /* SSH_DIGEST_XXX */ 41#define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */ 42#define SSH_UMAC128 3 43 44struct macalg { 45 char *name; 46 int type; 47 int alg; 48 int truncatebits; /* truncate digest if != 0 */ 49 int key_len; /* just for UMAC */ 50 int len; /* just for UMAC */ 51 int etm; /* Encrypt-then-MAC */ 52}; 53 54static const struct macalg macs[] = { 55 /* Encrypt-and-MAC (encrypt-and-authenticate) variants */ 56 { "hmac-sha1", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 }, 57 { "hmac-sha1-96", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 }, 58 { "hmac-sha2-256", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 }, 59 { "hmac-sha2-512", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 }, 60 { "hmac-md5", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 0 }, 61 { "hmac-md5-96", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 0 }, 62 { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 }, 63 { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 }, 64 65 /* Encrypt-then-MAC variants */ 66 { "hmac-sha1-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 }, 67 { "hmac-sha1-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 1 }, 68 { "hmac-sha2-256-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 }, 69 { "hmac-sha2-512-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 }, 70 { "hmac-md5-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 0, 0, 0, 1 }, 71 { "hmac-md5-96-etm@openssh.com", SSH_DIGEST, SSH_DIGEST_MD5, 96, 0, 0, 1 }, 72 { "umac-64-etm@openssh.com", SSH_UMAC, 0, 0, 128, 64, 1 }, 73 { "umac-128-etm@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 1 }, 74 75 { NULL, 0, 0, 0, 0, 0, 0 } 76}; 77 78/* Returns a list of supported MACs separated by the specified char. */ 79char * 80mac_alg_list(char sep) 81{ 82 char *ret = NULL; 83 const struct macalg *m; 84 char sep_str[2] = {sep, '\0'}; 85 86 for (m = macs; m->name != NULL; m++) 87 xextendf(&ret, sep_str, "%s", m->name); 88 89 return ret; 90} 91 92static int 93mac_setup_by_alg(struct sshmac *mac, const struct macalg *macalg) 94{ 95 mac->type = macalg->type; 96 if (mac->type == SSH_DIGEST) { 97 if ((mac->hmac_ctx = ssh_hmac_start(macalg->alg)) == NULL) 98 return SSH_ERR_ALLOC_FAIL; 99 mac->key_len = mac->mac_len = ssh_hmac_bytes(macalg->alg); 100 } else { 101 mac->mac_len = macalg->len / 8; 102 mac->key_len = macalg->key_len / 8; 103 mac->umac_ctx = NULL; 104 } 105 if (macalg->truncatebits != 0) 106 mac->mac_len = macalg->truncatebits / 8; 107 mac->etm = macalg->etm; 108 return 0; 109} 110 111int 112mac_setup(struct sshmac *mac, char *name) 113{ 114 const struct macalg *m; 115 116 for (m = macs; m->name != NULL; m++) { 117 if (strcmp(name, m->name) != 0) 118 continue; 119 if (mac != NULL) 120 return mac_setup_by_alg(mac, m); 121 return 0; 122 } 123 return SSH_ERR_INVALID_ARGUMENT; 124} 125 126int 127mac_init(struct sshmac *mac) 128{ 129 if (mac->key == NULL) 130 return SSH_ERR_INVALID_ARGUMENT; 131 switch (mac->type) { 132 case SSH_DIGEST: 133 if (mac->hmac_ctx == NULL || 134 ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0) 135 return SSH_ERR_INVALID_ARGUMENT; 136 return 0; 137 case SSH_UMAC: 138 if ((mac->umac_ctx = umac_new(mac->key)) == NULL) 139 return SSH_ERR_ALLOC_FAIL; 140 return 0; 141 case SSH_UMAC128: 142 if ((mac->umac_ctx = umac128_new(mac->key)) == NULL) 143 return SSH_ERR_ALLOC_FAIL; 144 return 0; 145 default: 146 return SSH_ERR_INVALID_ARGUMENT; 147 } 148} 149 150int 151mac_compute(struct sshmac *mac, u_int32_t seqno, 152 const u_char *data, int datalen, 153 u_char *digest, size_t dlen) 154{ 155 static union { 156 u_char m[SSH_DIGEST_MAX_LENGTH]; 157 u_int64_t for_align; 158 } u; 159 u_char b[4]; 160 u_char nonce[8]; 161 162 if (mac->mac_len > sizeof(u)) 163 return SSH_ERR_INTERNAL_ERROR; 164 165 switch (mac->type) { 166 case SSH_DIGEST: 167 put_u32(b, seqno); 168 /* reset HMAC context */ 169 if (ssh_hmac_init(mac->hmac_ctx, NULL, 0) < 0 || 170 ssh_hmac_update(mac->hmac_ctx, b, sizeof(b)) < 0 || 171 ssh_hmac_update(mac->hmac_ctx, data, datalen) < 0 || 172 ssh_hmac_final(mac->hmac_ctx, u.m, sizeof(u.m)) < 0) 173 return SSH_ERR_LIBCRYPTO_ERROR; 174 break; 175 case SSH_UMAC: 176 POKE_U64(nonce, seqno); 177 umac_update(mac->umac_ctx, data, datalen); 178 umac_final(mac->umac_ctx, u.m, nonce); 179 break; 180 case SSH_UMAC128: 181 put_u64(nonce, seqno); 182 umac128_update(mac->umac_ctx, data, datalen); 183 umac128_final(mac->umac_ctx, u.m, nonce); 184 break; 185 default: 186 return SSH_ERR_INVALID_ARGUMENT; 187 } 188 if (digest != NULL) { 189 if (dlen > mac->mac_len) 190 dlen = mac->mac_len; 191 memcpy(digest, u.m, dlen); 192 } 193 return 0; 194} 195 196int 197mac_check(struct sshmac *mac, u_int32_t seqno, 198 const u_char *data, size_t dlen, 199 const u_char *theirmac, size_t mlen) 200{ 201 u_char ourmac[SSH_DIGEST_MAX_LENGTH]; 202 int r; 203 204 if (mac->mac_len > mlen) 205 return SSH_ERR_INVALID_ARGUMENT; 206 if ((r = mac_compute(mac, seqno, data, dlen, 207 ourmac, sizeof(ourmac))) != 0) 208 return r; 209 if (timingsafe_bcmp(ourmac, theirmac, mac->mac_len) != 0) 210 return SSH_ERR_MAC_INVALID; 211 return 0; 212} 213 214void 215mac_clear(struct sshmac *mac) 216{ 217 if (mac->type == SSH_UMAC) { 218 if (mac->umac_ctx != NULL) 219 umac_delete(mac->umac_ctx); 220 } else if (mac->type == SSH_UMAC128) { 221 if (mac->umac_ctx != NULL) 222 umac128_delete(mac->umac_ctx); 223 } else if (mac->hmac_ctx != NULL) 224 ssh_hmac_free(mac->hmac_ctx); 225 mac->hmac_ctx = NULL; 226 mac->umac_ctx = NULL; 227} 228 229/* XXX copied from ciphers_valid */ 230#define MAC_SEP "," 231int 232mac_valid(const char *names) 233{ 234 char *maclist, *cp, *p; 235 236 if (names == NULL || strcmp(names, "") == 0) 237 return 0; 238 if ((maclist = cp = strdup(names)) == NULL) 239 return 0; 240 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; 241 (p = strsep(&cp, MAC_SEP))) { 242 if (mac_setup(NULL, p) < 0) { 243 free(maclist); 244 return 0; 245 } 246 } 247 free(maclist); 248 return 1; 249}