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

1.25    ! dtucker     1: /* $OpenBSD: mac.c,v 1.24 2013/06/03 00:03:18 dtucker 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.22      djm        48: struct macalg {
1.1       markus     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.22      djm        56: };
                     57:
                     58: static const struct macalg macs[] = {
1.20      markus     59:        /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
                     60:        { "hmac-sha1",                          SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
                     61:        { "hmac-sha1-96",                       SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
                     62:        { "hmac-sha2-256",                      SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
                     63:        { "hmac-sha2-512",                      SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
                     64:        { "hmac-md5",                           SSH_EVP, EVP_md5, 0, 0, 0, 0 },
                     65:        { "hmac-md5-96",                        SSH_EVP, EVP_md5, 96, 0, 0, 0 },
                     66:        { "hmac-ripemd160",                     SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
                     67:        { "hmac-ripemd160@openssh.com",         SSH_EVP, EVP_ripemd160, 0, 0, 0, 0 },
                     68:        { "umac-64@openssh.com",                SSH_UMAC, NULL, 0, 128, 64, 0 },
                     69:        { "umac-128@openssh.com",               SSH_UMAC128, NULL, 0, 128, 128, 0 },
                     70:
                     71:        /* Encrypt-then-MAC variants */
                     72:        { "hmac-sha1-etm@openssh.com",          SSH_EVP, EVP_sha1, 0, 0, 0, 1 },
                     73:        { "hmac-sha1-96-etm@openssh.com",       SSH_EVP, EVP_sha1, 96, 0, 0, 1 },
                     74:        { "hmac-sha2-256-etm@openssh.com",      SSH_EVP, EVP_sha256, 0, 0, 0, 1 },
                     75:        { "hmac-sha2-512-etm@openssh.com",      SSH_EVP, EVP_sha512, 0, 0, 0, 1 },
                     76:        { "hmac-md5-etm@openssh.com",           SSH_EVP, EVP_md5, 0, 0, 0, 1 },
                     77:        { "hmac-md5-96-etm@openssh.com",        SSH_EVP, EVP_md5, 96, 0, 0, 1 },
1.21      sthen      78:        { "hmac-ripemd160-etm@openssh.com",     SSH_EVP, EVP_ripemd160, 0, 0, 0, 1 },
1.20      markus     79:        { "umac-64-etm@openssh.com",            SSH_UMAC, NULL, 0, 128, 64, 1 },
                     80:        { "umac-128-etm@openssh.com",           SSH_UMAC128, NULL, 0, 128, 128, 1 },
                     81:
                     82:        { NULL,                                 0, NULL, 0, 0, 0, 0 }
1.1       markus     83: };
                     84:
1.25    ! dtucker    85: /* Returns a list of supported MACs separated by the specified char. */
1.22      djm        86: char *
1.25    ! dtucker    87: mac_alg_list(char sep)
1.22      djm        88: {
                     89:        char *ret = NULL;
                     90:        size_t nlen, rlen = 0;
                     91:        const struct macalg *m;
                     92:
                     93:        for (m = macs; m->name != NULL; m++) {
                     94:                if (ret != NULL)
1.25    ! dtucker    95:                        ret[rlen++] = sep;
1.22      djm        96:                nlen = strlen(m->name);
                     97:                ret = xrealloc(ret, 1, rlen + nlen + 2);
                     98:                memcpy(ret + rlen, m->name, nlen + 1);
                     99:                rlen += nlen;
                    100:        }
                    101:        return ret;
                    102: }
                    103:
1.14      pvalchev  104: static void
1.22      djm       105: mac_setup_by_alg(Mac *mac, const struct macalg *macalg)
1.14      pvalchev  106: {
                    107:        int evp_len;
1.22      djm       108:
                    109:        mac->type = macalg->type;
1.14      pvalchev  110:        if (mac->type == SSH_EVP) {
1.22      djm       111:                mac->evp_md = macalg->mdfunc();
1.14      pvalchev  112:                if ((evp_len = EVP_MD_size(mac->evp_md)) <= 0)
                    113:                        fatal("mac %s len %d", mac->name, evp_len);
                    114:                mac->key_len = mac->mac_len = (u_int)evp_len;
                    115:        } else {
1.22      djm       116:                mac->mac_len = macalg->len / 8;
                    117:                mac->key_len = macalg->key_len / 8;
1.14      pvalchev  118:                mac->umac_ctx = NULL;
                    119:        }
1.22      djm       120:        if (macalg->truncatebits != 0)
                    121:                mac->mac_len = macalg->truncatebits / 8;
                    122:        mac->etm = macalg->etm;
1.14      pvalchev  123: }
                    124:
1.1       markus    125: int
1.13      djm       126: mac_setup(Mac *mac, char *name)
1.1       markus    127: {
1.22      djm       128:        const struct macalg *m;
1.7       djm       129:
1.22      djm       130:        for (m = macs; m->name != NULL; m++) {
                    131:                if (strcmp(name, m->name) != 0)
                    132:                        continue;
                    133:                if (mac != NULL)
                    134:                        mac_setup_by_alg(mac, m);
                    135:                debug2("mac_setup: found %s", name);
                    136:                return (0);
1.1       markus    137:        }
1.13      djm       138:        debug2("mac_setup: unknown %s", name);
1.1       markus    139:        return (-1);
                    140: }
                    141:
1.14      pvalchev  142: int
1.13      djm       143: mac_init(Mac *mac)
                    144: {
                    145:        if (mac->key == NULL)
                    146:                fatal("mac_init: no key");
1.14      pvalchev  147:        switch (mac->type) {
                    148:        case SSH_EVP:
                    149:                if (mac->evp_md == NULL)
                    150:                        return -1;
1.17      djm       151:                HMAC_CTX_init(&mac->evp_ctx);
1.14      pvalchev  152:                HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
                    153:                return 0;
                    154:        case SSH_UMAC:
                    155:                mac->umac_ctx = umac_new(mac->key);
                    156:                return 0;
1.19      markus    157:        case SSH_UMAC128:
                    158:                mac->umac_ctx = umac128_new(mac->key);
                    159:                return 0;
1.14      pvalchev  160:        default:
                    161:                return -1;
                    162:        }
1.13      djm       163: }
                    164:
1.1       markus    165: u_char *
                    166: mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
                    167: {
1.24      dtucker   168:        static union {
                    169:                u_char m[EVP_MAX_MD_SIZE];
                    170:                u_int64_t for_align;
                    171:        } u;
1.14      pvalchev  172:        u_char b[4], nonce[8];
1.1       markus    173:
1.24      dtucker   174:        if (mac->mac_len > sizeof(u))
1.14      pvalchev  175:                fatal("mac_compute: mac too long %u %lu",
1.24      dtucker   176:                    mac->mac_len, (u_long)sizeof(u));
1.14      pvalchev  177:
                    178:        switch (mac->type) {
                    179:        case SSH_EVP:
                    180:                put_u32(b, seqno);
                    181:                /* reset HMAC context */
                    182:                HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
                    183:                HMAC_Update(&mac->evp_ctx, b, sizeof(b));
                    184:                HMAC_Update(&mac->evp_ctx, data, datalen);
1.24      dtucker   185:                HMAC_Final(&mac->evp_ctx, u.m, NULL);
1.14      pvalchev  186:                break;
                    187:        case SSH_UMAC:
                    188:                put_u64(nonce, seqno);
                    189:                umac_update(mac->umac_ctx, data, datalen);
1.24      dtucker   190:                umac_final(mac->umac_ctx, u.m, nonce);
1.14      pvalchev  191:                break;
1.19      markus    192:        case SSH_UMAC128:
                    193:                put_u64(nonce, seqno);
                    194:                umac128_update(mac->umac_ctx, data, datalen);
1.24      dtucker   195:                umac128_final(mac->umac_ctx, u.m, nonce);
1.19      markus    196:                break;
1.14      pvalchev  197:        default:
                    198:                fatal("mac_compute: unknown MAC type");
                    199:        }
1.24      dtucker   200:        return (u.m);
1.1       markus    201: }
                    202:
1.13      djm       203: void
                    204: mac_clear(Mac *mac)
                    205: {
1.14      pvalchev  206:        if (mac->type == SSH_UMAC) {
                    207:                if (mac->umac_ctx != NULL)
                    208:                        umac_delete(mac->umac_ctx);
1.19      markus    209:        } else if (mac->type == SSH_UMAC128) {
                    210:                if (mac->umac_ctx != NULL)
                    211:                        umac128_delete(mac->umac_ctx);
1.14      pvalchev  212:        } else if (mac->evp_md != NULL)
                    213:                HMAC_cleanup(&mac->evp_ctx);
                    214:        mac->evp_md = NULL;
                    215:        mac->umac_ctx = NULL;
1.13      djm       216: }
                    217:
1.1       markus    218: /* XXX copied from ciphers_valid */
                    219: #define        MAC_SEP ","
                    220: int
                    221: mac_valid(const char *names)
                    222: {
                    223:        char *maclist, *cp, *p;
                    224:
                    225:        if (names == NULL || strcmp(names, "") == 0)
                    226:                return (0);
                    227:        maclist = cp = xstrdup(names);
                    228:        for ((p = strsep(&cp, MAC_SEP)); p && *p != '\0';
1.3       deraadt   229:            (p = strsep(&cp, MAC_SEP))) {
1.13      djm       230:                if (mac_setup(NULL, p) < 0) {
1.1       markus    231:                        debug("bad mac %s [%s]", p, names);
1.23      djm       232:                        free(maclist);
1.1       markus    233:                        return (0);
                    234:                } else {
                    235:                        debug3("mac ok: %s [%s]", p, names);
                    236:                }
                    237:        }
                    238:        debug3("macs ok: [%s]", names);
1.23      djm       239:        free(maclist);
1.1       markus    240:        return (1);
                    241: }