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

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