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

Annotation of src/usr.bin/ssh/digest-openssl.c, Revision 1.6

1.6     ! dtucker     1: /* $OpenBSD: digest-openssl.c,v 1.5 2014/12/21 22:27:56 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
                      4:  *
                      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.
                      8:  *
                      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.
                     16:  */
                     17:
                     18: #include <sys/types.h>
                     19: #include <limits.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
                     23: #include <openssl/evp.h>
                     24:
1.3       djm        25: #include "sshbuf.h"
1.1       markus     26: #include "digest.h"
1.3       djm        27: #include "ssherr.h"
1.1       markus     28:
                     29: struct ssh_digest_ctx {
                     30:        int alg;
                     31:        EVP_MD_CTX mdctx;
                     32: };
                     33:
                     34: struct ssh_digest {
                     35:        int id;
                     36:        const char *name;
                     37:        size_t digest_len;
                     38:        const EVP_MD *(*mdfunc)(void);
                     39: };
                     40:
                     41: /* NB. Indexed directly by algorithm number */
                     42: const struct ssh_digest digests[] = {
                     43:        { SSH_DIGEST_MD5,       "MD5",          16,     EVP_md5 },
                     44:        { SSH_DIGEST_RIPEMD160, "RIPEMD160",    20,     EVP_ripemd160 },
                     45:        { SSH_DIGEST_SHA1,      "SHA1",         20,     EVP_sha1 },
                     46:        { SSH_DIGEST_SHA256,    "SHA256",       32,     EVP_sha256 },
                     47:        { SSH_DIGEST_SHA384,    "SHA384",       48,     EVP_sha384 },
                     48:        { SSH_DIGEST_SHA512,    "SHA512",       64,     EVP_sha512 },
                     49:        { -1,                   NULL,           0,      NULL },
                     50: };
                     51:
                     52: static const struct ssh_digest *
                     53: ssh_digest_by_alg(int alg)
                     54: {
                     55:        if (alg < 0 || alg >= SSH_DIGEST_MAX)
                     56:                return NULL;
                     57:        if (digests[alg].id != alg) /* sanity */
                     58:                return NULL;
                     59:        return &(digests[alg]);
1.5       djm        60: }
                     61:
                     62: int
                     63: ssh_digest_alg_by_name(const char *name)
                     64: {
                     65:        int alg;
                     66:
                     67:        for (alg = 0; digests[alg].id != -1; alg++) {
                     68:                if (strcasecmp(name, digests[alg].name) == 0)
                     69:                        return digests[alg].id;
                     70:        }
                     71:        return -1;
                     72: }
                     73:
                     74: const char *
                     75: ssh_digest_alg_name(int alg)
                     76: {
                     77:        const struct ssh_digest *digest = ssh_digest_by_alg(alg);
                     78:
                     79:        return digest == NULL ? NULL : digest->name;
1.1       markus     80: }
                     81:
                     82: size_t
                     83: ssh_digest_bytes(int alg)
                     84: {
                     85:        const struct ssh_digest *digest = ssh_digest_by_alg(alg);
                     86:
                     87:        return digest == NULL ? 0 : digest->digest_len;
                     88: }
                     89:
                     90: size_t
                     91: ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
                     92: {
                     93:        return EVP_MD_CTX_block_size(&ctx->mdctx);
                     94: }
                     95:
                     96: struct ssh_digest_ctx *
                     97: ssh_digest_start(int alg)
                     98: {
                     99:        const struct ssh_digest *digest = ssh_digest_by_alg(alg);
                    100:        struct ssh_digest_ctx *ret;
                    101:
                    102:        if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
                    103:                return NULL;
                    104:        ret->alg = alg;
                    105:        EVP_MD_CTX_init(&ret->mdctx);
                    106:        if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
                    107:                free(ret);
                    108:                return NULL;
                    109:        }
                    110:        return ret;
                    111: }
                    112:
                    113: int
                    114: ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
                    115: {
1.3       djm       116:        if (from->alg != to->alg)
                    117:                return SSH_ERR_INVALID_ARGUMENT;
1.1       markus    118:        /* we have bcopy-style order while openssl has memcpy-style */
                    119:        if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
1.3       djm       120:                return SSH_ERR_LIBCRYPTO_ERROR;
1.1       markus    121:        return 0;
                    122: }
                    123:
                    124: int
                    125: ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
                    126: {
                    127:        if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
1.3       djm       128:                return SSH_ERR_LIBCRYPTO_ERROR;
1.1       markus    129:        return 0;
                    130: }
                    131:
                    132: int
1.3       djm       133: ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
1.1       markus    134: {
1.3       djm       135:        return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
1.1       markus    136: }
                    137:
                    138: int
                    139: ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
                    140: {
                    141:        const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
                    142:        u_int l = dlen;
                    143:
1.6     ! dtucker   144:        if (digest == NULL || dlen > UINT_MAX)
1.3       djm       145:                return SSH_ERR_INVALID_ARGUMENT;
1.1       markus    146:        if (dlen < digest->digest_len) /* No truncation allowed */
1.3       djm       147:                return SSH_ERR_INVALID_ARGUMENT;
1.1       markus    148:        if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
1.3       djm       149:                return SSH_ERR_LIBCRYPTO_ERROR;
1.1       markus    150:        if (l != digest->digest_len) /* sanity */
1.3       djm       151:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    152:        return 0;
                    153: }
                    154:
                    155: void
                    156: ssh_digest_free(struct ssh_digest_ctx *ctx)
                    157: {
                    158:        if (ctx != NULL) {
                    159:                EVP_MD_CTX_cleanup(&ctx->mdctx);
1.2       djm       160:                explicit_bzero(ctx, sizeof(*ctx));
1.1       markus    161:                free(ctx);
                    162:        }
                    163: }
                    164:
                    165: int
                    166: ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
                    167: {
1.4       djm       168:        const struct ssh_digest *digest = ssh_digest_by_alg(alg);
                    169:        u_int mdlen;
1.1       markus    170:
1.4       djm       171:        if (digest == NULL)
                    172:                return SSH_ERR_INVALID_ARGUMENT;
                    173:        if (dlen > UINT_MAX)
1.3       djm       174:                return SSH_ERR_INVALID_ARGUMENT;
1.4       djm       175:        if (dlen < digest->digest_len)
                    176:                return SSH_ERR_INVALID_ARGUMENT;
                    177:        mdlen = dlen;
                    178:        if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
                    179:                return SSH_ERR_LIBCRYPTO_ERROR;
1.1       markus    180:        return 0;
                    181: }
                    182:
                    183: int
1.3       djm       184: ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
1.1       markus    185: {
1.3       djm       186:        return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
1.1       markus    187: }