blob: edf9b69bddf0aaafb8f5f62d5794da61c35b569d [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: mac.c,v 1.12 2006/08/03 03:34:42 deraadt Exp $ */
Ben Lindstrom06b33aa2001-02-15 03:01:59 +00002/*
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
26#include "includes.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000027
Damien Millerd7834352006-08-05 12:39:39 +100028#include <sys/types.h>
29
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000030#include <openssl/hmac.h>
31
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000035#include "xmalloc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000036#include "log.h"
37#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "buffer.h"
39#include "key.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000040#include "kex.h"
41#include "mac.h"
Damien Miller3f941882006-03-31 23:13:02 +110042#include "misc.h"
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000043
44struct {
45 char *name;
Ben Lindstrom6a246412002-06-06 19:48:16 +000046 const EVP_MD * (*mdfunc)(void);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000047 int truncatebits; /* truncate digest if != 0 */
48} macs[] = {
49 { "hmac-sha1", EVP_sha1, 0, },
50 { "hmac-sha1-96", EVP_sha1, 96 },
51 { "hmac-md5", EVP_md5, 0 },
52 { "hmac-md5-96", EVP_md5, 96 },
53 { "hmac-ripemd160", EVP_ripemd160, 0 },
54 { "hmac-ripemd160@openssh.com", EVP_ripemd160, 0 },
Ben Lindstroma3700052001-04-05 23:26:32 +000055 { NULL, NULL, 0 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000056};
57
58int
59mac_init(Mac *mac, char *name)
60{
Damien Millereccb9de2005-06-17 12:59:34 +100061 int i, evp_len;
62
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000063 for (i = 0; macs[i].name; i++) {
64 if (strcmp(name, macs[i].name) == 0) {
65 if (mac != NULL) {
66 mac->md = (*macs[i].mdfunc)();
Damien Millereccb9de2005-06-17 12:59:34 +100067 if ((evp_len = EVP_MD_size(mac->md)) <= 0)
68 fatal("mac %s len %d", name, evp_len);
69 mac->key_len = mac->mac_len = (u_int)evp_len;
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000070 if (macs[i].truncatebits != 0)
71 mac->mac_len = macs[i].truncatebits/8;
72 }
73 debug2("mac_init: found %s", name);
74 return (0);
75 }
76 }
77 debug2("mac_init: unknown %s", name);
78 return (-1);
79}
80
81u_char *
82mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
83{
84 HMAC_CTX c;
85 static u_char m[EVP_MAX_MD_SIZE];
86 u_char b[4];
87
88 if (mac->key == NULL)
89 fatal("mac_compute: no key");
Damien Millereccb9de2005-06-17 12:59:34 +100090 if (mac->mac_len > sizeof(m))
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000091 fatal("mac_compute: mac too long");
92 HMAC_Init(&c, mac->key, mac->key_len, mac->md);
Damien Miller3f941882006-03-31 23:13:02 +110093 put_u32(b, seqno);
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000094 HMAC_Update(&c, b, sizeof(b));
95 HMAC_Update(&c, data, datalen);
96 HMAC_Final(&c, m, NULL);
97 HMAC_cleanup(&c);
98 return (m);
99}
100
101/* XXX copied from ciphers_valid */
102#define MAC_SEP ","
103int
104mac_valid(const char *names)
105{
106 char *maclist, *cp, *p;
107
108 if (names == NULL || strcmp(names, "") == 0)
109 return (0);
110 maclist = cp = xstrdup(names);
111 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100112 (p = strsep(&cp, MAC_SEP))) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000113 if (mac_init(NULL, p) < 0) {
114 debug("bad mac %s [%s]", p, names);
115 xfree(maclist);
116 return (0);
117 } else {
118 debug3("mac ok: %s [%s]", p, names);
119 }
120 }
121 debug3("macs ok: [%s]", names);
122 xfree(maclist);
123 return (1);
124}