blob: 2bda5a1b994928ebd6bbde55dcc3c6693190d495 [file] [log] [blame]
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00001/*
2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Damien Millereccb9de2005-06-17 12:59:34 +100026RCSID("$OpenBSD: mac.c,v 1.7 2005/06/17 02:44:32 djm Exp $");
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000027
28#include <openssl/hmac.h>
29
30#include "xmalloc.h"
31#include "getput.h"
32#include "log.h"
33#include "cipher.h"
34#include "kex.h"
35#include "mac.h"
36
37struct {
38 char *name;
Ben Lindstrom6a246412002-06-06 19:48:16 +000039 const EVP_MD * (*mdfunc)(void);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000040 int truncatebits; /* truncate digest if != 0 */
41} macs[] = {
42 { "hmac-sha1", EVP_sha1, 0, },
43 { "hmac-sha1-96", EVP_sha1, 96 },
44 { "hmac-md5", EVP_md5, 0 },
45 { "hmac-md5-96", EVP_md5, 96 },
46 { "hmac-ripemd160", EVP_ripemd160, 0 },
47 { "hmac-ripemd160@openssh.com", EVP_ripemd160, 0 },
Ben Lindstroma3700052001-04-05 23:26:32 +000048 { NULL, NULL, 0 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000049};
50
51int
52mac_init(Mac *mac, char *name)
53{
Damien Millereccb9de2005-06-17 12:59:34 +100054 int i, evp_len;
55
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000056 for (i = 0; macs[i].name; i++) {
57 if (strcmp(name, macs[i].name) == 0) {
58 if (mac != NULL) {
59 mac->md = (*macs[i].mdfunc)();
Damien Millereccb9de2005-06-17 12:59:34 +100060 if ((evp_len = EVP_MD_size(mac->md)) <= 0)
61 fatal("mac %s len %d", name, evp_len);
62 mac->key_len = mac->mac_len = (u_int)evp_len;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000063 if (macs[i].truncatebits != 0)
64 mac->mac_len = macs[i].truncatebits/8;
65 }
66 debug2("mac_init: found %s", name);
67 return (0);
68 }
69 }
70 debug2("mac_init: unknown %s", name);
71 return (-1);
72}
73
74u_char *
75mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
76{
77 HMAC_CTX c;
78 static u_char m[EVP_MAX_MD_SIZE];
79 u_char b[4];
80
81 if (mac->key == NULL)
82 fatal("mac_compute: no key");
Damien Millereccb9de2005-06-17 12:59:34 +100083 if (mac->mac_len > sizeof(m))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000084 fatal("mac_compute: mac too long");
85 HMAC_Init(&c, mac->key, mac->key_len, mac->md);
86 PUT_32BIT(b, seqno);
87 HMAC_Update(&c, b, sizeof(b));
88 HMAC_Update(&c, data, datalen);
89 HMAC_Final(&c, m, NULL);
90 HMAC_cleanup(&c);
91 return (m);
92}
93
94/* XXX copied from ciphers_valid */
95#define MAC_SEP ","
96int
97mac_valid(const char *names)
98{
99 char *maclist, *cp, *p;
100
101 if (names == NULL || strcmp(names, "") == 0)
102 return (0);
103 maclist = cp = xstrdup(names);
104 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100105 (p = strsep(&cp, MAC_SEP))) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000106 if (mac_init(NULL, p) < 0) {
107 debug("bad mac %s [%s]", p, names);
108 xfree(maclist);
109 return (0);
110 } else {
111 debug3("mac ok: %s [%s]", p, names);
112 }
113 }
114 debug3("macs ok: [%s]", names);
115 xfree(maclist);
116 return (1);
117}