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

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