Annotation of src/usr.bin/ssh/mac.c, Revision 1.13
1.13 ! djm 1: /* $OpenBSD: mac.c,v 1.12 2006/08/03 03:34:42 deraadt 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:
42: struct {
43: char *name;
1.5 markus 44: const EVP_MD * (*mdfunc)(void);
1.1 markus 45: int truncatebits; /* truncate digest if != 0 */
46: } macs[] = {
47: { "hmac-sha1", EVP_sha1, 0, },
48: { "hmac-sha1-96", EVP_sha1, 96 },
49: { "hmac-md5", EVP_md5, 0 },
50: { "hmac-md5-96", EVP_md5, 96 },
51: { "hmac-ripemd160", EVP_ripemd160, 0 },
52: { "hmac-ripemd160@openssh.com", EVP_ripemd160, 0 },
1.2 markus 53: { NULL, NULL, 0 }
1.1 markus 54: };
55:
56: int
1.13 ! djm 57: mac_setup(Mac *mac, char *name)
1.1 markus 58: {
1.7 djm 59: int i, evp_len;
60:
1.1 markus 61: for (i = 0; macs[i].name; i++) {
62: if (strcmp(name, macs[i].name) == 0) {
63: if (mac != NULL) {
64: mac->md = (*macs[i].mdfunc)();
1.7 djm 65: if ((evp_len = EVP_MD_size(mac->md)) <= 0)
66: fatal("mac %s len %d", name, evp_len);
67: mac->key_len = mac->mac_len = (u_int)evp_len;
1.1 markus 68: if (macs[i].truncatebits != 0)
69: mac->mac_len = macs[i].truncatebits/8;
70: }
1.13 ! djm 71: debug2("mac_setup: found %s", name);
1.1 markus 72: return (0);
73: }
74: }
1.13 ! djm 75: debug2("mac_setup: unknown %s", name);
1.1 markus 76: return (-1);
77: }
78:
1.13 ! djm 79: void
! 80: mac_init(Mac *mac)
! 81: {
! 82: if (mac->key == NULL)
! 83: fatal("mac_init: no key");
! 84: HMAC_Init(&mac->ctx, mac->key, mac->key_len, mac->md);
! 85: }
! 86:
1.1 markus 87: u_char *
88: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
89: {
90: static u_char m[EVP_MAX_MD_SIZE];
91: u_char b[4];
92:
1.7 djm 93: if (mac->mac_len > sizeof(m))
1.1 markus 94: fatal("mac_compute: mac too long");
1.10 djm 95: put_u32(b, seqno);
1.13 ! djm 96: HMAC_Init(&mac->ctx, NULL, 0, NULL); /* reset HMAC context */
! 97: HMAC_Update(&mac->ctx, b, sizeof(b));
! 98: HMAC_Update(&mac->ctx, data, datalen);
! 99: HMAC_Final(&mac->ctx, m, NULL);
1.1 markus 100: return (m);
101: }
102:
1.13 ! djm 103: void
! 104: mac_clear(Mac *mac)
! 105: {
! 106: HMAC_cleanup(&mac->ctx);
! 107: }
! 108:
1.1 markus 109: /* XXX copied from ciphers_valid */
110: #define MAC_SEP ","
111: int
112: mac_valid(const char *names)
113: {
114: char *maclist, *cp, *p;
115:
116: if (names == NULL || strcmp(names, "") == 0)
117: return (0);
118: maclist = cp = xstrdup(names);
119: for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1.3 deraadt 120: (p = strsep(&cp, MAC_SEP))) {
1.13 ! djm 121: if (mac_setup(NULL, p) < 0) {
1.1 markus 122: debug("bad mac %s [%s]", p, names);
123: xfree(maclist);
124: return (0);
125: } else {
126: debug3("mac ok: %s [%s]", p, names);
127: }
128: }
129: debug3("macs ok: [%s]", names);
130: xfree(maclist);
131: return (1);
132: }