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