Annotation of src/usr.bin/ssh/mac.c, Revision 1.15
1.15 ! dtucker 1: /* $OpenBSD: mac.c,v 1.14 2007/06/07 19:37:34 pvalchev 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.14 pvalchev 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:
1.1 markus 47: struct {
48: char *name;
1.14 pvalchev 49: int type;
1.5 markus 50: const EVP_MD * (*mdfunc)(void);
1.1 markus 51: int truncatebits; /* truncate digest if != 0 */
1.14 pvalchev 52: int key_len; /* just for UMAC */
53: int len; /* just for UMAC */
1.1 markus 54: } macs[] = {
1.14 pvalchev 55: { "hmac-sha1", SSH_EVP, EVP_sha1, 0, -1, -1 },
56: { "hmac-sha1-96", SSH_EVP, EVP_sha1, 96, -1, -1 },
57: { "hmac-md5", SSH_EVP, EVP_md5, 0, -1, -1 },
58: { "hmac-md5-96", SSH_EVP, EVP_md5, 96, -1, -1 },
59: { "hmac-ripemd160", SSH_EVP, EVP_ripemd160, 0, -1, -1 },
60: { "hmac-ripemd160@openssh.com", SSH_EVP, EVP_ripemd160, 0, -1, -1 },
61: { "umac-64@openssh.com", SSH_UMAC, NULL, 0, 128, 64 },
62: { NULL, 0, NULL, 0, -1, -1 }
1.1 markus 63: };
64:
1.14 pvalchev 65: static void
66: mac_setup_by_id(Mac *mac, int which)
67: {
68: int evp_len;
69: mac->type = macs[which].type;
70: if (mac->type == SSH_EVP) {
71: mac->evp_md = (*macs[which].mdfunc)();
72: if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
73: fatal("mac %s len %d", mac->name, evp_len);
74: mac->key_len = mac->mac_len = (u_int)evp_len;
75: } else {
76: mac->mac_len = macs[which].len / 8;
77: mac->key_len = macs[which].key_len / 8;
78: mac->umac_ctx = NULL;
79: }
80: if (macs[which].truncatebits != 0)
81: mac->mac_len = macs[which].truncatebits / 8;
82: }
83:
1.1 markus 84: int
1.13 djm 85: mac_setup(Mac *mac, char *name)
1.1 markus 86: {
1.14 pvalchev 87: int i;
1.7 djm 88:
1.1 markus 89: for (i = 0; macs[i].name; i++) {
90: if (strcmp(name, macs[i].name) == 0) {
1.14 pvalchev 91: if (mac != NULL)
92: mac_setup_by_id(mac, i);
1.13 djm 93: debug2("mac_setup: found %s", name);
1.1 markus 94: return (0);
95: }
96: }
1.13 djm 97: debug2("mac_setup: unknown %s", name);
1.1 markus 98: return (-1);
99: }
100:
1.14 pvalchev 101: int
1.13 djm 102: mac_init(Mac *mac)
103: {
104: if (mac->key == NULL)
105: fatal("mac_init: no key");
1.14 pvalchev 106: switch (mac->type) {
107: case SSH_EVP:
108: if (mac->evp_md == NULL)
109: return -1;
110: HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
111: return 0;
112: case SSH_UMAC:
113: mac->umac_ctx = umac_new(mac->key);
114: return 0;
115: default:
116: return -1;
117: }
1.13 djm 118: }
119:
1.1 markus 120: u_char *
121: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
122: {
123: static u_char m[EVP_MAX_MD_SIZE];
1.14 pvalchev 124: u_char b[4], nonce[8];
1.1 markus 125:
1.7 djm 126: if (mac->mac_len > sizeof(m))
1.14 pvalchev 127: fatal("mac_compute: mac too long %u %lu",
1.15 ! dtucker 128: mac->mac_len, (u_long)sizeof(m));
1.14 pvalchev 129:
130: switch (mac->type) {
131: case SSH_EVP:
132: put_u32(b, seqno);
133: /* reset HMAC context */
134: HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
135: HMAC_Update(&mac->evp_ctx, b, sizeof(b));
136: HMAC_Update(&mac->evp_ctx, data, datalen);
137: HMAC_Final(&mac->evp_ctx, m, NULL);
138: break;
139: case SSH_UMAC:
140: put_u64(nonce, seqno);
141: umac_update(mac->umac_ctx, data, datalen);
142: umac_final(mac->umac_ctx, m, nonce);
143: break;
144: default:
145: fatal("mac_compute: unknown MAC type");
146: }
1.1 markus 147: return (m);
148: }
149:
1.13 djm 150: void
151: mac_clear(Mac *mac)
152: {
1.14 pvalchev 153: if (mac->type == SSH_UMAC) {
154: if (mac->umac_ctx != NULL)
155: umac_delete(mac->umac_ctx);
156: } else if (mac->evp_md != NULL)
157: HMAC_cleanup(&mac->evp_ctx);
158: mac->evp_md = NULL;
159: mac->umac_ctx = NULL;
1.13 djm 160: }
161:
1.1 markus 162: /* XXX copied from ciphers_valid */
163: #define MAC_SEP ","
164: int
165: mac_valid(const char *names)
166: {
167: char *maclist, *cp, *p;
168:
169: if (names == NULL || strcmp(names, "") == 0)
170: return (0);
171: maclist = cp = xstrdup(names);
172: for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1.3 deraadt 173: (p = strsep(&cp, MAC_SEP))) {
1.13 djm 174: if (mac_setup(NULL, p) < 0) {
1.1 markus 175: debug("bad mac %s [%s]", p, names);
176: xfree(maclist);
177: return (0);
178: } else {
179: debug3("mac ok: %s [%s]", p, names);
180: }
181: }
182: debug3("macs ok: [%s]", names);
183: xfree(maclist);
184: return (1);
185: }