Annotation of src/usr.bin/ssh/mac.c, Revision 1.28
1.28 ! djm 1: /* $OpenBSD: mac.c,v 1.27 2014/01/27 18:58:14 markus 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 },
70: { "umac-64@openssh.com", SSH_UMAC, 0, 0, 128, 64, 0 },
71: { "umac-128@openssh.com", SSH_UMAC128, 0, 0, 128, 128, 0 },
1.20 markus 72:
73: /* Encrypt-then-MAC variants */
1.27 markus 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 },
1.20 markus 83:
1.27 markus 84: { NULL, 0, 0, 0, 0, 0, 0 }
1.1 markus 85: };
86:
1.25 dtucker 87: /* Returns a list of supported MACs separated by the specified char. */
1.22 djm 88: char *
1.25 dtucker 89: mac_alg_list(char sep)
1.22 djm 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)
1.25 dtucker 97: ret[rlen++] = sep;
1.22 djm 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:
1.14 pvalchev 106: static void
1.22 djm 107: mac_setup_by_alg(Mac *mac, const struct macalg *macalg)
1.14 pvalchev 108: {
1.22 djm 109: mac->type = macalg->type;
1.27 markus 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);
1.14 pvalchev 114: } else {
1.22 djm 115: mac->mac_len = macalg->len / 8;
116: mac->key_len = macalg->key_len / 8;
1.14 pvalchev 117: mac->umac_ctx = NULL;
118: }
1.22 djm 119: if (macalg->truncatebits != 0)
120: mac->mac_len = macalg->truncatebits / 8;
121: mac->etm = macalg->etm;
1.14 pvalchev 122: }
123:
1.1 markus 124: int
1.13 djm 125: mac_setup(Mac *mac, char *name)
1.1 markus 126: {
1.22 djm 127: const struct macalg *m;
1.7 djm 128:
1.22 djm 129: for (m = macs; m->name != NULL; m++) {
130: if (strcmp(name, m->name) != 0)
131: continue;
1.28 ! djm 132: if (mac != NULL) {
1.22 djm 133: mac_setup_by_alg(mac, m);
1.28 ! djm 134: debug2("mac_setup: setup %s", name);
! 135: }
1.22 djm 136: return (0);
1.1 markus 137: }
1.13 djm 138: debug2("mac_setup: unknown %s", name);
1.1 markus 139: return (-1);
140: }
141:
1.14 pvalchev 142: int
1.13 djm 143: mac_init(Mac *mac)
144: {
145: if (mac->key == NULL)
1.28 ! djm 146: fatal("%s: no key", __func__);
1.14 pvalchev 147: switch (mac->type) {
1.27 markus 148: case SSH_DIGEST:
149: if (mac->hmac_ctx == NULL ||
150: ssh_hmac_init(mac->hmac_ctx, mac->key, mac->key_len) < 0)
1.14 pvalchev 151: return -1;
152: return 0;
153: case SSH_UMAC:
154: mac->umac_ctx = umac_new(mac->key);
155: return 0;
1.19 markus 156: case SSH_UMAC128:
157: mac->umac_ctx = umac128_new(mac->key);
158: return 0;
1.14 pvalchev 159: default:
160: return -1;
161: }
1.13 djm 162: }
163:
1.1 markus 164: u_char *
165: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
166: {
1.24 dtucker 167: static union {
168: u_char m[EVP_MAX_MD_SIZE];
169: u_int64_t for_align;
170: } u;
1.14 pvalchev 171: u_char b[4], nonce[8];
1.1 markus 172:
1.24 dtucker 173: if (mac->mac_len > sizeof(u))
1.26 tedu 174: fatal("mac_compute: mac too long %u %zu",
175: mac->mac_len, sizeof(u));
1.14 pvalchev 176:
177: switch (mac->type) {
1.27 markus 178: case SSH_DIGEST:
1.14 pvalchev 179: put_u32(b, seqno);
180: /* reset HMAC context */
1.27 markus 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");
1.14 pvalchev 186: break;
187: case SSH_UMAC:
188: put_u64(nonce, seqno);
189: umac_update(mac->umac_ctx, data, datalen);
1.24 dtucker 190: umac_final(mac->umac_ctx, u.m, nonce);
1.14 pvalchev 191: break;
1.19 markus 192: case SSH_UMAC128:
193: put_u64(nonce, seqno);
194: umac128_update(mac->umac_ctx, data, datalen);
1.24 dtucker 195: umac128_final(mac->umac_ctx, u.m, nonce);
1.19 markus 196: break;
1.14 pvalchev 197: default:
198: fatal("mac_compute: unknown MAC type");
199: }
1.24 dtucker 200: return (u.m);
1.1 markus 201: }
202:
1.13 djm 203: void
204: mac_clear(Mac *mac)
205: {
1.14 pvalchev 206: if (mac->type == SSH_UMAC) {
207: if (mac->umac_ctx != NULL)
208: umac_delete(mac->umac_ctx);
1.19 markus 209: } else if (mac->type == SSH_UMAC128) {
210: if (mac->umac_ctx != NULL)
211: umac128_delete(mac->umac_ctx);
1.27 markus 212: } else if (mac->hmac_ctx != NULL)
213: ssh_hmac_free(mac->hmac_ctx);
214: mac->hmac_ctx = NULL;
1.14 pvalchev 215: mac->umac_ctx = NULL;
1.13 djm 216: }
217:
1.1 markus 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';
1.3 deraadt 229: (p = strsep(&cp, MAC_SEP))) {
1.13 djm 230: if (mac_setup(NULL, p) < 0) {
1.1 markus 231: debug("bad mac %s [%s]", p, names);
1.23 djm 232: free(maclist);
1.1 markus 233: return (0);
234: }
235: }
236: debug3("macs ok: [%s]", names);
1.23 djm 237: free(maclist);
1.1 markus 238: return (1);
239: }