[BACK]Return to mac.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/mac.c, Revision 1.8

1.1       markus      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"
                     26:
                     27: #include <openssl/hmac.h>
                     28:
                     29: #include "xmalloc.h"
                     30: #include "getput.h"
                     31: #include "log.h"
                     32: #include "cipher.h"
                     33: #include "kex.h"
                     34: #include "mac.h"
                     35:
                     36: struct {
                     37:        char            *name;
1.5       markus     38:        const EVP_MD *  (*mdfunc)(void);
1.1       markus     39:        int             truncatebits;   /* truncate digest if != 0 */
                     40: } macs[] = {
                     41:        { "hmac-sha1",                  EVP_sha1, 0, },
                     42:        { "hmac-sha1-96",               EVP_sha1, 96 },
                     43:        { "hmac-md5",                   EVP_md5, 0 },
                     44:        { "hmac-md5-96",                EVP_md5, 96 },
                     45:        { "hmac-ripemd160",             EVP_ripemd160, 0 },
                     46:        { "hmac-ripemd160@openssh.com", EVP_ripemd160, 0 },
1.2       markus     47:        { NULL,                         NULL, 0 }
1.1       markus     48: };
                     49:
                     50: int
                     51: mac_init(Mac *mac, char *name)
                     52: {
1.7       djm        53:        int i, evp_len;
                     54:
1.1       markus     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)();
1.7       djm        59:                                if ((evp_len = EVP_MD_size(mac->md)) <= 0)
                     60:                                        fatal("mac %s len %d", name, evp_len);
                     61:                                mac->key_len = mac->mac_len = (u_int)evp_len;
1.1       markus     62:                                if (macs[i].truncatebits != 0)
                     63:                                        mac->mac_len = macs[i].truncatebits/8;
                     64:                        }
                     65:                        debug2("mac_init: found %s", name);
                     66:                        return (0);
                     67:                }
                     68:        }
                     69:        debug2("mac_init: unknown %s", name);
                     70:        return (-1);
                     71: }
                     72:
                     73: u_char *
                     74: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
                     75: {
                     76:        HMAC_CTX c;
                     77:        static u_char m[EVP_MAX_MD_SIZE];
                     78:        u_char b[4];
                     79:
                     80:        if (mac->key == NULL)
                     81:                fatal("mac_compute: no key");
1.7       djm        82:        if (mac->mac_len > sizeof(m))
1.1       markus     83:                fatal("mac_compute: mac too long");
                     84:        HMAC_Init(&c, mac->key, mac->key_len, mac->md);
                     85:        PUT_32BIT(b, seqno);
                     86:        HMAC_Update(&c, b, sizeof(b));
                     87:        HMAC_Update(&c, data, datalen);
                     88:        HMAC_Final(&c, m, NULL);
                     89:        HMAC_cleanup(&c);
                     90:        return (m);
                     91: }
                     92:
                     93: /* XXX copied from ciphers_valid */
                     94: #define        MAC_SEP ","
                     95: int
                     96: mac_valid(const char *names)
                     97: {
                     98:        char *maclist, *cp, *p;
                     99:
                    100:        if (names == NULL || strcmp(names, "") == 0)
                    101:                return (0);
                    102:        maclist = cp = xstrdup(names);
                    103:        for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1.3       deraadt   104:            (p = strsep(&cp, MAC_SEP))) {
1.1       markus    105:                if (mac_init(NULL, p) < 0) {
                    106:                        debug("bad mac %s [%s]", p, names);
                    107:                        xfree(maclist);
                    108:                        return (0);
                    109:                } else {
                    110:                        debug3("mac ok: %s [%s]", p, names);
                    111:                }
                    112:        }
                    113:        debug3("macs ok: [%s]", names);
                    114:        xfree(maclist);
                    115:        return (1);
                    116: }