]> andersk Git - openssh.git/blame - mac.c
- markus@cvs.openbsd.org 2005/06/16 08:00:00
[openssh.git] / mac.c
CommitLineData
b2552997 1/*
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"
bb92e5cc 26RCSID("$OpenBSD: mac.c,v 1.6 2003/09/18 13:02:21 miod Exp $");
b2552997 27
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;
7b5edc2b 39 const EVP_MD * (*mdfunc)(void);
b2552997 40 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 },
cd332296 48 { NULL, NULL, 0 }
b2552997 49};
50
51int
52mac_init(Mac *mac, char *name)
53{
54 int i;
55 for (i = 0; macs[i].name; i++) {
56 if (strcmp(name, macs[i].name) == 0) {
57 if (mac != NULL) {
58 mac->md = (*macs[i].mdfunc)();
a209a158 59 mac->key_len = mac->mac_len = EVP_MD_size(mac->md);
b2552997 60 if (macs[i].truncatebits != 0)
61 mac->mac_len = macs[i].truncatebits/8;
62 }
63 debug2("mac_init: found %s", name);
64 return (0);
65 }
66 }
67 debug2("mac_init: unknown %s", name);
68 return (-1);
69}
70
71u_char *
72mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
73{
74 HMAC_CTX c;
75 static u_char m[EVP_MAX_MD_SIZE];
76 u_char b[4];
77
78 if (mac->key == NULL)
79 fatal("mac_compute: no key");
bb92e5cc 80 if ((u_int)mac->mac_len > sizeof(m))
b2552997 81 fatal("mac_compute: mac too long");
82 HMAC_Init(&c, mac->key, mac->key_len, mac->md);
83 PUT_32BIT(b, seqno);
84 HMAC_Update(&c, b, sizeof(b));
85 HMAC_Update(&c, data, datalen);
86 HMAC_Final(&c, m, NULL);
87 HMAC_cleanup(&c);
88 return (m);
89}
90
91/* XXX copied from ciphers_valid */
92#define MAC_SEP ","
93int
94mac_valid(const char *names)
95{
96 char *maclist, *cp, *p;
97
98 if (names == NULL || strcmp(names, "") == 0)
99 return (0);
100 maclist = cp = xstrdup(names);
101 for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
184eed6a 102 (p = strsep(&cp, MAC_SEP))) {
b2552997 103 if (mac_init(NULL, p) < 0) {
104 debug("bad mac %s [%s]", p, names);
105 xfree(maclist);
106 return (0);
107 } else {
108 debug3("mac ok: %s [%s]", p, names);
109 }
110 }
111 debug3("macs ok: [%s]", names);
112 xfree(maclist);
113 return (1);
114}
This page took 0.163784 seconds and 5 git commands to generate.