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

Annotation of src/usr.bin/ssh/hmac.c, Revision 1.13

1.13    ! djm         1: /* $OpenBSD: hmac.c,v 1.12 2015/03/24 20:03:44 markus Exp $ */
1.1       markus      2: /*
1.9       markus      3:  * Copyright (c) 2014 Markus Friedl.  All rights reserved.
1.1       markus      4:  *
1.9       markus      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       markus      8:  *
1.9       markus      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       markus     16:  */
                     17:
1.9       markus     18: #include <sys/types.h>
                     19: #include <string.h>
1.13    ! djm        20: #include <stdlib.h>
1.1       markus     21:
1.11      markus     22: #include "sshbuf.h"
1.9       markus     23: #include "digest.h"
                     24: #include "hmac.h"
1.1       markus     25:
1.9       markus     26: struct ssh_hmac_ctx {
                     27:        int                      alg;
                     28:        struct ssh_digest_ctx   *ictx;
                     29:        struct ssh_digest_ctx   *octx;
                     30:        struct ssh_digest_ctx   *digest;
                     31:        u_char                  *buf;
                     32:        size_t                   buf_len;
                     33: };
1.7       itojun     34:
1.9       markus     35: size_t
                     36: ssh_hmac_bytes(int alg)
                     37: {
                     38:        return ssh_digest_bytes(alg);
                     39: }
                     40:
                     41: struct ssh_hmac_ctx *
                     42: ssh_hmac_start(int alg)
                     43: {
                     44:        struct ssh_hmac_ctx     *ret;
                     45:
                     46:        if ((ret = calloc(1, sizeof(*ret))) == NULL)
                     47:                return NULL;
                     48:        ret->alg = alg;
                     49:        if ((ret->ictx = ssh_digest_start(alg)) == NULL ||
                     50:            (ret->octx = ssh_digest_start(alg)) == NULL ||
                     51:            (ret->digest = ssh_digest_start(alg)) == NULL)
                     52:                goto fail;
                     53:        ret->buf_len = ssh_digest_blocksize(ret->ictx);
                     54:        if ((ret->buf = calloc(1, ret->buf_len)) == NULL)
                     55:                goto fail;
                     56:        return ret;
                     57: fail:
                     58:        ssh_hmac_free(ret);
                     59:        return NULL;
                     60: }
                     61:
                     62: int
                     63: ssh_hmac_init(struct ssh_hmac_ctx *ctx, const void *key, size_t klen)
                     64: {
                     65:        size_t i;
                     66:
                     67:        /* reset ictx and octx if no is key given */
                     68:        if (key != NULL) {
                     69:                /* truncate long keys */
                     70:                if (klen <= ctx->buf_len)
                     71:                        memcpy(ctx->buf, key, klen);
                     72:                else if (ssh_digest_memory(ctx->alg, key, klen, ctx->buf,
                     73:                    ctx->buf_len) < 0)
                     74:                        return -1;
                     75:                for (i = 0; i < ctx->buf_len; i++)
                     76:                        ctx->buf[i] ^= 0x36;
                     77:                if (ssh_digest_update(ctx->ictx, ctx->buf, ctx->buf_len) < 0)
                     78:                        return -1;
                     79:                for (i = 0; i < ctx->buf_len; i++)
                     80:                        ctx->buf[i] ^= 0x36 ^ 0x5c;
                     81:                if (ssh_digest_update(ctx->octx, ctx->buf, ctx->buf_len) < 0)
                     82:                        return -1;
1.10      tedu       83:                explicit_bzero(ctx->buf, ctx->buf_len);
1.9       markus     84:        }
                     85:        /* start with ictx */
                     86:        if (ssh_digest_copy_state(ctx->ictx, ctx->digest) < 0)
                     87:                return -1;
                     88:        return 0;
                     89: }
                     90:
                     91: int
                     92: ssh_hmac_update(struct ssh_hmac_ctx *ctx, const void *m, size_t mlen)
                     93: {
                     94:        return ssh_digest_update(ctx->digest, m, mlen);
                     95: }
                     96:
                     97: int
1.11      markus     98: ssh_hmac_update_buffer(struct ssh_hmac_ctx *ctx, const struct sshbuf *b)
1.9       markus     99: {
                    100:        return ssh_digest_update_buffer(ctx->digest, b);
                    101: }
                    102:
                    103: int
                    104: ssh_hmac_final(struct ssh_hmac_ctx *ctx, u_char *d, size_t dlen)
                    105: {
                    106:        size_t len;
                    107:
                    108:        len = ssh_digest_bytes(ctx->alg);
                    109:        if (dlen < len ||
                    110:            ssh_digest_final(ctx->digest, ctx->buf, len))
                    111:                return -1;
                    112:        /* switch to octx */
                    113:        if (ssh_digest_copy_state(ctx->octx, ctx->digest) < 0 ||
                    114:            ssh_digest_update(ctx->digest, ctx->buf, len) < 0 ||
                    115:            ssh_digest_final(ctx->digest, d, dlen) < 0)
                    116:                return -1;
                    117:        return 0;
                    118: }
                    119:
                    120: void
                    121: ssh_hmac_free(struct ssh_hmac_ctx *ctx)
                    122: {
                    123:        if (ctx != NULL) {
                    124:                ssh_digest_free(ctx->ictx);
                    125:                ssh_digest_free(ctx->octx);
                    126:                ssh_digest_free(ctx->digest);
                    127:                if (ctx->buf) {
1.10      tedu      128:                        explicit_bzero(ctx->buf, ctx->buf_len);
1.9       markus    129:                        free(ctx->buf);
                    130:                }
1.10      tedu      131:                explicit_bzero(ctx, sizeof(*ctx));
1.9       markus    132:                free(ctx);
                    133:        }
                    134: }
                    135:
                    136: #ifdef TEST
                    137:
                    138: /* cc -DTEST hmac.c digest.c buffer.c cleanup.c fatal.c log.c xmalloc.c -lcrypto */
                    139: static void
                    140: hmac_test(void *key, size_t klen, void *m, size_t mlen, u_char *e, size_t elen)
                    141: {
                    142:        struct ssh_hmac_ctx     *ctx;
                    143:        size_t                   i;
                    144:        u_char                   digest[16];
                    145:
                    146:        if ((ctx = ssh_hmac_start(SSH_DIGEST_MD5)) == NULL)
                    147:                printf("ssh_hmac_start failed");
                    148:        if (ssh_hmac_init(ctx, key, klen) < 0 ||
                    149:            ssh_hmac_update(ctx, m, mlen) < 0 ||
                    150:            ssh_hmac_final(ctx, digest, sizeof(digest)) < 0)
                    151:                printf("ssh_hmac_xxx failed");
                    152:        ssh_hmac_free(ctx);
                    153:
                    154:        if (memcmp(e, digest, elen)) {
                    155:                for (i = 0; i < elen; i++)
1.12      markus    156:                        printf("[%zu] %2.2x %2.2x\n", i, e[i], digest[i]);
1.9       markus    157:                printf("mismatch\n");
                    158:        } else
                    159:                printf("ok\n");
                    160: }
                    161:
                    162: int
                    163: main(int argc, char **argv)
                    164: {
                    165:        /* try test vectors from RFC 2104 */
                    166:
                    167:        u_char key1[16] = {
                    168:            0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb,
                    169:            0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb, 0xb };
                    170:        u_char *data1 = "Hi There";
                    171:        u_char dig1[16] = {
                    172:            0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
                    173:            0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
                    174:
                    175:        u_char *key2 = "Jefe";
                    176:        u_char *data2 = "what do ya want for nothing?";
                    177:        u_char dig2[16] = {
                    178:            0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
                    179:            0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
                    180:
                    181:        u_char key3[16];
                    182:        u_char data3[50];
                    183:        u_char dig3[16] = {
                    184:            0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
                    185:            0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
                    186:        memset(key3, 0xaa, sizeof(key3));
                    187:        memset(data3, 0xdd, sizeof(data3));
                    188:
                    189:        hmac_test(key1, sizeof(key1), data1, strlen(data1), dig1, sizeof(dig1));
                    190:        hmac_test(key2, strlen(key2), data2, strlen(data2), dig2, sizeof(dig2));
                    191:        hmac_test(key3, sizeof(key3), data3, sizeof(data3), dig3, sizeof(dig3));
1.1       markus    192:
1.9       markus    193:        return 0;
1.1       markus    194: }
1.9       markus    195:
                    196: #endif