[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.21

1.21    ! sthen       1: /* $OpenBSD: mac.c,v 1.20 2012/12/11 22:31:18 markus Exp $ */
1.1       markus      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:
1.12      deraadt    26: #include <sys/types.h>
1.1       markus     27:
                     28: #include <openssl/hmac.h>
1.11      stevesk    29:
                     30: #include <string.h>
1.12      deraadt    31: #include <signal.h>
1.1       markus     32:
                     33: #include "xmalloc.h"
                     34: #include "log.h"
                     35: #include "cipher.h"
1.12      deraadt    36: #include "buffer.h"
                     37: #include "key.h"
1.1       markus     38: #include "kex.h"
                     39: #include "mac.h"
1.10      djm        40: #include "misc.h"
1.1       markus     41:
1.14      pvalchev   42: #include "umac.h"
                     43:
                     44: #define SSH_EVP                1       /* OpenSSL EVP-based MAC */
                     45: #define SSH_UMAC       2       /* UMAC (not integrated with OpenSSL) */
1.19      markus     46: #define SSH_UMAC128    3
1.14      pvalchev   47:
1.1       markus     48: struct {
                     49:        char            *name;
1.14      pvalchev   50:        int             type;
1.5       markus     51:        const EVP_MD *  (*mdfunc)(void);
1.1       markus     52:        int             truncatebits;   /* truncate digest if != 0 */
1.14      pvalchev   53:        int             key_len;        /* just for UMAC */
                     54:        int             len;            /* just for UMAC */
1.20      markus     55:        int             etm;            /* Encrypt-then-MAC */
1.1       markus     56: } macs[] = {
1.20      markus     57:        /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
                     58:        { "hmac-sha1",                          SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
                     59:        { "hmac-sha1-96",                       SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
                     60:        { "hmac-sha2-256",                      SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
                     61:        { "hmac-sha2-512",                      SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
                     62:        { "hmac-md5",                           SSH_EVP, EVP_md5, 0, 0, 0, 0 },
                     63:        { "hmac-md5-96",                        SSH_EVP, EVP_md5, 96, 0, 0, 0 },
                     64:        { "hmac-ripemd160",                     SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
                     65:        { "hmac-ripemd160@openssh.com",         SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
                     66:        { "umac-64@openssh.com",                SSH_UMAC, NULL, 0, 128, 64, 0 },
                     67:        { "umac-128@openssh.com",               SSH_UMAC128, NULL, 0, 128, 128, 0 },
                     68:
                     69:        /* Encrypt-then-MAC variants */
                     70:        { "hmac-sha1-etm@openssh.com",          SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
                     71:        { "hmac-sha1-96-etm@openssh.com",       SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
                     72:        { "hmac-sha2-256-etm@openssh.com",      SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
                     73:        { "hmac-sha2-512-etm@openssh.com",      SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
                     74:        { "hmac-md5-etm@openssh.com",           SSH_EVP, EVP_md5, 0, 0, 0, 1 },
                     75:        { "hmac-md5-96-etm@openssh.com",        SSH_EVP, EVP_md5, 96, 0, 0, 1 },
1.21    ! sthen      76:        { "hmac-ripemd160-etm@openssh.com",     SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
1.20      markus     77:        { "umac-64-etm@openssh.com",            SSH_UMAC, NULL, 0, 128, 64, 1 },
                     78:        { "umac-128-etm@openssh.com",           SSH_UMAC128, NULL, 0, 128, 128, 1 },
                     79:
                     80:        { NULL,                                 0, NULL, 0, 0, 0, 0 }
1.1       markus     81: };
                     82:
1.14      pvalchev   83: static void
                     84: mac_setup_by_id(Mac *mac, int which)
                     85: {
                     86:        int evp_len;
                     87:        mac->type = macs[which].type;
                     88:        if (mac->type == SSH_EVP) {
                     89:                mac->evp_md = (*macs[which].mdfunc)();
                     90:                if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
                     91:                        fatal("mac %s len %d", mac->name, evp_len);
                     92:                mac->key_len = mac->mac_len = (u_int)evp_len;
                     93:        } else {
                     94:                mac->mac_len = macs[which].len / 8;
                     95:                mac->key_len = macs[which].key_len / 8;
                     96:                mac->umac_ctx = NULL;
                     97:        }
                     98:        if (macs[which].truncatebits != 0)
                     99:                mac->mac_len = macs[which].truncatebits / 8;
1.20      markus    100:        mac->etm = macs[which].etm;
1.14      pvalchev  101: }
                    102:
1.1       markus    103: int
1.13      djm       104: mac_setup(Mac *mac, char *name)
1.1       markus    105: {
1.14      pvalchev  106:        int i;
1.7       djm       107:
1.1       markus    108:        for (i = 0; macs[i].name; i++) {
                    109:                if (strcmp(name, macs[i].name) == 0) {
1.14      pvalchev  110:                        if (mac != NULL)
                    111:                                mac_setup_by_id(mac, i);
1.13      djm       112:                        debug2("mac_setup: found %s", name);
1.1       markus    113:                        return (0);
                    114:                }
                    115:        }
1.13      djm       116:        debug2("mac_setup: unknown %s", name);
1.1       markus    117:        return (-1);
                    118: }
                    119:
1.14      pvalchev  120: int
1.13      djm       121: mac_init(Mac *mac)
                    122: {
                    123:        if (mac->key == NULL)
                    124:                fatal("mac_init: no key");
1.14      pvalchev  125:        switch (mac->type) {
                    126:        case SSH_EVP:
                    127:                if (mac->evp_md == NULL)
                    128:                        return -1;
1.17      djm       129:                HMAC_CTX_init(&mac->evp_ctx);
1.14      pvalchev  130:                HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
                    131:                return 0;
                    132:        case SSH_UMAC:
                    133:                mac->umac_ctx = umac_new(mac->key);
                    134:                return 0;
1.19      markus    135:        case SSH_UMAC128:
                    136:                mac->umac_ctx = umac128_new(mac->key);
                    137:                return 0;
1.14      pvalchev  138:        default:
                    139:                return -1;
                    140:        }
1.13      djm       141: }
                    142:
1.1       markus    143: u_char *
                    144: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
                    145: {
                    146:        static u_char m[EVP_MAX_MD_SIZE];
1.14      pvalchev  147:        u_char b[4], nonce[8];
1.1       markus    148:
1.7       djm       149:        if (mac->mac_len > sizeof(m))
1.14      pvalchev  150:                fatal("mac_compute: mac too long %u %lu",
1.15      dtucker   151:                    mac->mac_len, (u_long)sizeof(m));
1.14      pvalchev  152:
                    153:        switch (mac->type) {
                    154:        case SSH_EVP:
                    155:                put_u32(b, seqno);
                    156:                /* reset HMAC context */
                    157:                HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
                    158:                HMAC_Update(&mac->evp_ctx, b, sizeof(b));
                    159:                HMAC_Update(&mac->evp_ctx, data, datalen);
                    160:                HMAC_Final(&mac->evp_ctx, m, NULL);
                    161:                break;
                    162:        case SSH_UMAC:
                    163:                put_u64(nonce, seqno);
                    164:                umac_update(mac->umac_ctx, data, datalen);
                    165:                umac_final(mac->umac_ctx, m, nonce);
                    166:                break;
1.19      markus    167:        case SSH_UMAC128:
                    168:                put_u64(nonce, seqno);
                    169:                umac128_update(mac->umac_ctx, data, datalen);
                    170:                umac128_final(mac->umac_ctx, m, nonce);
                    171:                break;
1.14      pvalchev  172:        default:
                    173:                fatal("mac_compute: unknown MAC type");
                    174:        }
1.1       markus    175:        return (m);
                    176: }
                    177:
1.13      djm       178: void
                    179: mac_clear(Mac *mac)
                    180: {
1.14      pvalchev  181:        if (mac->type == SSH_UMAC) {
                    182:                if (mac->umac_ctx != NULL)
                    183:                        umac_delete(mac->umac_ctx);
1.19      markus    184:        } else if (mac->type == SSH_UMAC128) {
                    185:                if (mac->umac_ctx != NULL)
                    186:                        umac128_delete(mac->umac_ctx);
1.14      pvalchev  187:        } else if (mac->evp_md != NULL)
                    188:                HMAC_cleanup(&mac->evp_ctx);
                    189:        mac->evp_md = NULL;
                    190:        mac->umac_ctx = NULL;
1.13      djm       191: }
                    192:
1.1       markus    193: /* XXX copied from ciphers_valid */
                    194: #define        MAC_SEP ","
                    195: int
                    196: mac_valid(const char *names)
                    197: {
                    198:        char *maclist, *cp, *p;
                    199:
                    200:        if (names == NULL || strcmp(names, "") == 0)
                    201:                return (0);
                    202:        maclist = cp = xstrdup(names);
                    203:        for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1.3       deraadt   204:            (p = strsep(&cp, MAC_SEP))) {
1.13      djm       205:                if (mac_setup(NULL, p) < 0) {
1.1       markus    206:                        debug("bad mac %s [%s]", p, names);
                    207:                        xfree(maclist);
                    208:                        return (0);
                    209:                } else {
                    210:                        debug3("mac ok: %s [%s]", p, names);
                    211:                }
                    212:        }
                    213:        debug3("macs ok: [%s]", names);
                    214:        xfree(maclist);
                    215:        return (1);
                    216: }