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