freebsd-nq/digest-openssl.c

208 lines
4.9 KiB
C
Raw Normal View History

2018-05-06 12:24:45 +00:00
/* $OpenBSD: digest-openssl.c,v 1.7 2017/05/08 22:57:38 djm Exp $ */
2014-01-30 10:56:49 +00:00
/*
* Copyright (c) 2013 Damien Miller <djm@mindrot.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "includes.h"
2015-07-02 13:15:34 +00:00
#ifdef WITH_OPENSSL
2014-01-30 10:56:49 +00:00
#include <sys/types.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include "openbsd-compat/openssl-compat.h"
2015-01-05 16:09:55 +00:00
#include "sshbuf.h"
2014-01-30 10:56:49 +00:00
#include "digest.h"
2015-01-05 16:09:55 +00:00
#include "ssherr.h"
#ifndef HAVE_EVP_SHA256
# define EVP_sha256 NULL
2021-02-14 21:00:25 +00:00
#endif
#ifndef HAVE_EVP_SHA384
2015-01-05 16:09:55 +00:00
# define EVP_sha384 NULL
2021-02-14 21:00:25 +00:00
#endif
#ifndef HAVE_EVP_SHA512
2015-01-05 16:09:55 +00:00
# define EVP_sha512 NULL
2021-02-14 21:00:25 +00:00
#endif
2014-01-30 10:56:49 +00:00
struct ssh_digest_ctx {
int alg;
EVP_MD_CTX *mdctx;
2014-01-30 10:56:49 +00:00
};
struct ssh_digest {
int id;
const char *name;
size_t digest_len;
const EVP_MD *(*mdfunc)(void);
};
/* NB. Indexed directly by algorithm number */
const struct ssh_digest digests[] = {
{ SSH_DIGEST_MD5, "MD5", 16, EVP_md5 },
{ SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 },
{ SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 },
{ SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 },
{ SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 },
{ -1, NULL, 0, NULL },
};
static const struct ssh_digest *
ssh_digest_by_alg(int alg)
{
if (alg < 0 || alg >= SSH_DIGEST_MAX)
return NULL;
if (digests[alg].id != alg) /* sanity */
return NULL;
2015-01-05 16:09:55 +00:00
if (digests[alg].mdfunc == NULL)
return NULL;
2014-01-30 10:56:49 +00:00
return &(digests[alg]);
}
2015-07-02 13:15:34 +00:00
int
ssh_digest_alg_by_name(const char *name)
{
int alg;
for (alg = 0; digests[alg].id != -1; alg++) {
if (strcasecmp(name, digests[alg].name) == 0)
return digests[alg].id;
}
return -1;
}
const char *
ssh_digest_alg_name(int alg)
{
const struct ssh_digest *digest = ssh_digest_by_alg(alg);
return digest == NULL ? NULL : digest->name;
}
2014-01-30 10:56:49 +00:00
size_t
ssh_digest_bytes(int alg)
{
const struct ssh_digest *digest = ssh_digest_by_alg(alg);
return digest == NULL ? 0 : digest->digest_len;
}
2014-03-22 15:23:38 +00:00
size_t
ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
{
return EVP_MD_CTX_block_size(ctx->mdctx);
2014-03-22 15:23:38 +00:00
}
2014-01-30 10:56:49 +00:00
struct ssh_digest_ctx *
ssh_digest_start(int alg)
{
const struct ssh_digest *digest = ssh_digest_by_alg(alg);
struct ssh_digest_ctx *ret;
if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
return NULL;
ret->alg = alg;
if ((ret->mdctx = EVP_MD_CTX_new()) == NULL) {
2014-01-30 10:56:49 +00:00
free(ret);
return NULL;
}
if (EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
ssh_digest_free(ret);
return NULL;
}
2014-01-30 10:56:49 +00:00
return ret;
}
2014-03-22 15:23:38 +00:00
int
ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
{
2015-01-05 16:09:55 +00:00
if (from->alg != to->alg)
return SSH_ERR_INVALID_ARGUMENT;
2014-03-22 15:23:38 +00:00
/* we have bcopy-style order while openssl has memcpy-style */
if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
2015-01-05 16:09:55 +00:00
return SSH_ERR_LIBCRYPTO_ERROR;
2014-03-22 15:23:38 +00:00
return 0;
}
2014-01-30 10:56:49 +00:00
int
ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
{
if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
2015-01-05 16:09:55 +00:00
return SSH_ERR_LIBCRYPTO_ERROR;
2014-01-30 10:56:49 +00:00
return 0;
}
int
2015-01-05 16:09:55 +00:00
ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
2014-01-30 10:56:49 +00:00
{
2015-01-05 16:09:55 +00:00
return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
2014-01-30 10:56:49 +00:00
}
int
ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
{
const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
u_int l = dlen;
2017-08-03 10:10:20 +00:00
if (digest == NULL || dlen > UINT_MAX)
2015-01-05 16:09:55 +00:00
return SSH_ERR_INVALID_ARGUMENT;
2014-01-30 10:56:49 +00:00
if (dlen < digest->digest_len) /* No truncation allowed */
2015-01-05 16:09:55 +00:00
return SSH_ERR_INVALID_ARGUMENT;
if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
2015-01-05 16:09:55 +00:00
return SSH_ERR_LIBCRYPTO_ERROR;
2014-01-30 10:56:49 +00:00
if (l != digest->digest_len) /* sanity */
2015-01-05 16:09:55 +00:00
return SSH_ERR_INTERNAL_ERROR;
2014-01-30 10:56:49 +00:00
return 0;
}
void
ssh_digest_free(struct ssh_digest_ctx *ctx)
{
if (ctx == NULL)
return;
EVP_MD_CTX_free(ctx->mdctx);
freezero(ctx, sizeof(*ctx));
2014-01-30 10:56:49 +00:00
}
int
ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
{
2015-01-05 16:09:55 +00:00
const struct ssh_digest *digest = ssh_digest_by_alg(alg);
u_int mdlen;
if (digest == NULL)
return SSH_ERR_INVALID_ARGUMENT;
if (dlen > UINT_MAX)
return SSH_ERR_INVALID_ARGUMENT;
if (dlen < digest->digest_len)
return SSH_ERR_INVALID_ARGUMENT;
mdlen = dlen;
if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
return SSH_ERR_LIBCRYPTO_ERROR;
2014-01-30 10:56:49 +00:00
return 0;
}
int
2015-01-05 16:09:55 +00:00
ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
2014-01-30 10:56:49 +00:00
{
2015-01-05 16:09:55 +00:00
return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
2014-01-30 10:56:49 +00:00
}
2015-07-02 13:15:34 +00:00
#endif /* WITH_OPENSSL */