2 /* $OpenBSD: mac.c,v 1.15 2008/06/13 00:51:47 dtucker Exp $ */
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 __RCSID("$NetBSD: mac.c,v 1.13 2009/02/16 20:53:54 christos Exp $");
29 #include <sys/types.h>
31 #include <openssl/hmac.h>
45 #ifdef UMAC_HAS_BEEN_UNBROKEN
49 #define SSH_EVP 1 /* OpenSSL EVP-based MAC */
50 #define SSH_UMAC 2 /* UMAC (not integrated with OpenSSL) */
55 const EVP_MD
* (*mdfunc
)(void);
56 int truncatebits
; /* truncate digest if != 0 */
57 int key_len
; /* just for UMAC */
58 int len
; /* just for UMAC */
60 { "hmac-sha1", SSH_EVP
, EVP_sha1
, 0, -1, -1 },
61 { "hmac-sha1-96", SSH_EVP
, EVP_sha1
, 96, -1, -1 },
62 { "hmac-md5", SSH_EVP
, EVP_md5
, 0, -1, -1 },
63 { "hmac-md5-96", SSH_EVP
, EVP_md5
, 96, -1, -1 },
64 { "hmac-ripemd160", SSH_EVP
, EVP_ripemd160
, 0, -1, -1 },
65 { "hmac-ripemd160@openssh.com", SSH_EVP
, EVP_ripemd160
, 0, -1, -1 },
66 #ifdef UMAC_HAS_BEEN_UNBROKEN
67 { "umac-64@openssh.com", SSH_UMAC
, NULL
, 0, 128, 64 },
69 { NULL
, 0, NULL
, 0, -1, -1 }
73 mac_setup_by_id(Mac
*mac
, int which
)
76 mac
->type
= macs
[which
].type
;
77 if (mac
->type
== SSH_EVP
) {
78 mac
->evp_md
= (*macs
[which
].mdfunc
)();
79 if ((evp_len
= EVP_MD_size(mac
->evp_md
)) <= 0)
80 fatal("mac %s len %d", mac
->name
, evp_len
);
81 mac
->key_len
= mac
->mac_len
= (u_int
)evp_len
;
83 mac
->mac_len
= macs
[which
].len
/ 8;
84 mac
->key_len
= macs
[which
].key_len
/ 8;
87 if (macs
[which
].truncatebits
!= 0)
88 mac
->mac_len
= macs
[which
].truncatebits
/ 8;
92 mac_setup(Mac
*mac
, char *name
)
96 for (i
= 0; macs
[i
].name
; i
++) {
97 if (strcmp(name
, macs
[i
].name
) == 0) {
99 mac_setup_by_id(mac
, i
);
100 debug2("mac_setup: found %s", name
);
104 debug2("mac_setup: unknown %s", name
);
111 if (mac
->key
== NULL
)
112 fatal("mac_init: no key");
115 if (mac
->evp_md
== NULL
)
117 HMAC_Init(&mac
->evp_ctx
, mac
->key
, mac
->key_len
, mac
->evp_md
);
119 #ifdef UMAC_HAS_BEEN_UNBROKEN
121 mac
->umac_ctx
= umac_new(mac
->key
);
130 mac_compute(Mac
*mac
, u_int32_t seqno
, u_char
*data
, int datalen
)
132 static u_char m
[EVP_MAX_MD_SIZE
];
134 #ifdef UMAC_HAS_BEEN_UNBROKEN
138 if (mac
->mac_len
> sizeof(m
))
139 fatal("mac_compute: mac too long %u %lu",
140 mac
->mac_len
, (u_long
)sizeof(m
));
145 /* reset HMAC context */
146 HMAC_Init(&mac
->evp_ctx
, NULL
, 0, NULL
);
147 HMAC_Update(&mac
->evp_ctx
, b
, sizeof(b
));
148 HMAC_Update(&mac
->evp_ctx
, data
, datalen
);
149 HMAC_Final(&mac
->evp_ctx
, m
, NULL
);
151 #ifdef UMAC_HAS_BEEN_UNBROKEN
153 put_u64(nonce
, seqno
);
154 umac_update(mac
->umac_ctx
, data
, datalen
);
155 umac_final(mac
->umac_ctx
, m
, nonce
);
159 fatal("mac_compute: unknown MAC type");
167 #ifdef UMAC_HAS_BEEN_UNBROKEN
168 if (mac
->type
== SSH_UMAC
) {
169 if (mac
->umac_ctx
!= NULL
)
170 umac_delete(mac
->umac_ctx
);
173 if (mac
->evp_md
!= NULL
)
174 HMAC_cleanup(&mac
->evp_ctx
);
176 mac
->umac_ctx
= NULL
;
179 /* XXX copied from ciphers_valid */
182 mac_valid(const char *names
)
184 char *maclist
, *cp
, *p
;
186 if (names
== NULL
|| strcmp(names
, "") == 0)
188 maclist
= cp
= xstrdup(names
);
189 for ((p
= strsep(&cp
, MAC_SEP
)); p
&& *p
!= '\0';
190 (p
= strsep(&cp
, MAC_SEP
))) {
191 if (mac_setup(NULL
, p
) < 0) {
192 debug("bad mac %s [%s]", p
, names
);
196 debug3("mac ok: %s [%s]", p
, names
);
199 debug3("macs ok: [%s]", names
);