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