Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 1 | /* $OpenBSD: mac.c,v 1.11 2006/07/22 20:48:23 stevesk Exp $ */ |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 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 | |
| 26 | #include "includes.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 27 | |
| 28 | #include <openssl/hmac.h> |
| 29 | |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 32 | #include "xmalloc.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 33 | #include "log.h" |
| 34 | #include "cipher.h" |
| 35 | #include "kex.h" |
| 36 | #include "mac.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 37 | #include "misc.h" |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 38 | |
| 39 | struct { |
| 40 | char *name; |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 41 | const EVP_MD * (*mdfunc)(void); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 42 | int truncatebits; /* truncate digest if != 0 */ |
| 43 | } macs[] = { |
| 44 | { "hmac-sha1", EVP_sha1, 0, }, |
| 45 | { "hmac-sha1-96", EVP_sha1, 96 }, |
| 46 | { "hmac-md5", EVP_md5, 0 }, |
| 47 | { "hmac-md5-96", EVP_md5, 96 }, |
| 48 | { "hmac-ripemd160", EVP_ripemd160, 0 }, |
| 49 | { "hmac-ripemd160@openssh.com", EVP_ripemd160, 0 }, |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 50 | { NULL, NULL, 0 } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | int |
| 54 | mac_init(Mac *mac, char *name) |
| 55 | { |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 56 | int i, evp_len; |
| 57 | |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 58 | for (i = 0; macs[i].name; i++) { |
| 59 | if (strcmp(name, macs[i].name) == 0) { |
| 60 | if (mac != NULL) { |
| 61 | mac->md = (*macs[i].mdfunc)(); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 62 | if ((evp_len = EVP_MD_size(mac->md)) <= 0) |
| 63 | fatal("mac %s len %d", name, evp_len); |
| 64 | mac->key_len = mac->mac_len = (u_int)evp_len; |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 65 | if (macs[i].truncatebits != 0) |
| 66 | mac->mac_len = macs[i].truncatebits/8; |
| 67 | } |
| 68 | debug2("mac_init: found %s", name); |
| 69 | return (0); |
| 70 | } |
| 71 | } |
| 72 | debug2("mac_init: unknown %s", name); |
| 73 | return (-1); |
| 74 | } |
| 75 | |
| 76 | u_char * |
| 77 | mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen) |
| 78 | { |
| 79 | HMAC_CTX c; |
| 80 | static u_char m[EVP_MAX_MD_SIZE]; |
| 81 | u_char b[4]; |
| 82 | |
| 83 | if (mac->key == NULL) |
| 84 | fatal("mac_compute: no key"); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 85 | if (mac->mac_len > sizeof(m)) |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 86 | fatal("mac_compute: mac too long"); |
| 87 | HMAC_Init(&c, mac->key, mac->key_len, mac->md); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 88 | put_u32(b, seqno); |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 89 | HMAC_Update(&c, b, sizeof(b)); |
| 90 | HMAC_Update(&c, data, datalen); |
| 91 | HMAC_Final(&c, m, NULL); |
| 92 | HMAC_cleanup(&c); |
| 93 | return (m); |
| 94 | } |
| 95 | |
| 96 | /* XXX copied from ciphers_valid */ |
| 97 | #define MAC_SEP "," |
| 98 | int |
| 99 | mac_valid(const char *names) |
| 100 | { |
| 101 | char *maclist, *cp, *p; |
| 102 | |
| 103 | if (names == NULL || strcmp(names, "") == 0) |
| 104 | return (0); |
| 105 | maclist = cp = xstrdup(names); |
| 106 | for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 107 | (p = strsep(&cp, MAC_SEP))) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 108 | if (mac_init(NULL, p) < 0) { |
| 109 | debug("bad mac %s [%s]", p, names); |
| 110 | xfree(maclist); |
| 111 | return (0); |
| 112 | } else { |
| 113 | debug3("mac ok: %s [%s]", p, names); |
| 114 | } |
| 115 | } |
| 116 | debug3("macs ok: [%s]", names); |
| 117 | xfree(maclist); |
| 118 | return (1); |
| 119 | } |