1*50a69bb5SSascha Wildner /* $OpenBSD: digest-openssl.c,v 1.9 2020/10/29 02:52:43 djm Exp $ */
236e94dc5SPeter Avalos /*
336e94dc5SPeter Avalos * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
436e94dc5SPeter Avalos *
536e94dc5SPeter Avalos * Permission to use, copy, modify, and distribute this software for any
636e94dc5SPeter Avalos * purpose with or without fee is hereby granted, provided that the above
736e94dc5SPeter Avalos * copyright notice and this permission notice appear in all copies.
836e94dc5SPeter Avalos *
936e94dc5SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1036e94dc5SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1136e94dc5SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1236e94dc5SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1336e94dc5SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1436e94dc5SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1536e94dc5SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1636e94dc5SPeter Avalos */
1736e94dc5SPeter Avalos
1836e94dc5SPeter Avalos #include "includes.h"
1936e94dc5SPeter Avalos
20e9778795SPeter Avalos #ifdef WITH_OPENSSL
21e9778795SPeter Avalos
2236e94dc5SPeter Avalos #include <sys/types.h>
2336e94dc5SPeter Avalos #include <limits.h>
2436e94dc5SPeter Avalos #include <stdlib.h>
2536e94dc5SPeter Avalos #include <string.h>
2636e94dc5SPeter Avalos
2736e94dc5SPeter Avalos #include <openssl/evp.h>
2836e94dc5SPeter Avalos
2936e94dc5SPeter Avalos #include "openbsd-compat/openssl-compat.h"
3036e94dc5SPeter Avalos
3136e94dc5SPeter Avalos #include "sshbuf.h"
3236e94dc5SPeter Avalos #include "digest.h"
3336e94dc5SPeter Avalos #include "ssherr.h"
3436e94dc5SPeter Avalos
3536e94dc5SPeter Avalos #ifndef HAVE_EVP_SHA256
3636e94dc5SPeter Avalos # define EVP_sha256 NULL
370cbfa66cSDaniel Fojt #endif
380cbfa66cSDaniel Fojt #ifndef HAVE_EVP_SHA384
3936e94dc5SPeter Avalos # define EVP_sha384 NULL
400cbfa66cSDaniel Fojt #endif
410cbfa66cSDaniel Fojt #ifndef HAVE_EVP_SHA512
4236e94dc5SPeter Avalos # define EVP_sha512 NULL
430cbfa66cSDaniel Fojt #endif
4436e94dc5SPeter Avalos
4536e94dc5SPeter Avalos struct ssh_digest_ctx {
4636e94dc5SPeter Avalos int alg;
47664f4763Szrj EVP_MD_CTX *mdctx;
4836e94dc5SPeter Avalos };
4936e94dc5SPeter Avalos
5036e94dc5SPeter Avalos struct ssh_digest {
5136e94dc5SPeter Avalos int id;
5236e94dc5SPeter Avalos const char *name;
5336e94dc5SPeter Avalos size_t digest_len;
5436e94dc5SPeter Avalos const EVP_MD *(*mdfunc)(void);
5536e94dc5SPeter Avalos };
5636e94dc5SPeter Avalos
5736e94dc5SPeter Avalos /* NB. Indexed directly by algorithm number */
5836e94dc5SPeter Avalos const struct ssh_digest digests[] = {
5936e94dc5SPeter Avalos { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 },
6036e94dc5SPeter Avalos { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 },
6136e94dc5SPeter Avalos { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 },
6236e94dc5SPeter Avalos { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 },
6336e94dc5SPeter Avalos { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 },
6436e94dc5SPeter Avalos { -1, NULL, 0, NULL },
6536e94dc5SPeter Avalos };
6636e94dc5SPeter Avalos
6736e94dc5SPeter Avalos static const struct ssh_digest *
ssh_digest_by_alg(int alg)6836e94dc5SPeter Avalos ssh_digest_by_alg(int alg)
6936e94dc5SPeter Avalos {
7036e94dc5SPeter Avalos if (alg < 0 || alg >= SSH_DIGEST_MAX)
7136e94dc5SPeter Avalos return NULL;
7236e94dc5SPeter Avalos if (digests[alg].id != alg) /* sanity */
7336e94dc5SPeter Avalos return NULL;
7436e94dc5SPeter Avalos if (digests[alg].mdfunc == NULL)
7536e94dc5SPeter Avalos return NULL;
7636e94dc5SPeter Avalos return &(digests[alg]);
7736e94dc5SPeter Avalos }
7836e94dc5SPeter Avalos
79e9778795SPeter Avalos int
ssh_digest_alg_by_name(const char * name)80e9778795SPeter Avalos ssh_digest_alg_by_name(const char *name)
81e9778795SPeter Avalos {
82e9778795SPeter Avalos int alg;
83e9778795SPeter Avalos
84e9778795SPeter Avalos for (alg = 0; digests[alg].id != -1; alg++) {
85e9778795SPeter Avalos if (strcasecmp(name, digests[alg].name) == 0)
86e9778795SPeter Avalos return digests[alg].id;
87e9778795SPeter Avalos }
88e9778795SPeter Avalos return -1;
89e9778795SPeter Avalos }
90e9778795SPeter Avalos
91e9778795SPeter Avalos const char *
ssh_digest_alg_name(int alg)92e9778795SPeter Avalos ssh_digest_alg_name(int alg)
93e9778795SPeter Avalos {
94e9778795SPeter Avalos const struct ssh_digest *digest = ssh_digest_by_alg(alg);
95e9778795SPeter Avalos
96e9778795SPeter Avalos return digest == NULL ? NULL : digest->name;
97e9778795SPeter Avalos }
98e9778795SPeter Avalos
9936e94dc5SPeter Avalos size_t
ssh_digest_bytes(int alg)10036e94dc5SPeter Avalos ssh_digest_bytes(int alg)
10136e94dc5SPeter Avalos {
10236e94dc5SPeter Avalos const struct ssh_digest *digest = ssh_digest_by_alg(alg);
10336e94dc5SPeter Avalos
10436e94dc5SPeter Avalos return digest == NULL ? 0 : digest->digest_len;
10536e94dc5SPeter Avalos }
10636e94dc5SPeter Avalos
10736e94dc5SPeter Avalos size_t
ssh_digest_blocksize(struct ssh_digest_ctx * ctx)10836e94dc5SPeter Avalos ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
10936e94dc5SPeter Avalos {
110664f4763Szrj return EVP_MD_CTX_block_size(ctx->mdctx);
11136e94dc5SPeter Avalos }
11236e94dc5SPeter Avalos
11336e94dc5SPeter Avalos struct ssh_digest_ctx *
ssh_digest_start(int alg)11436e94dc5SPeter Avalos ssh_digest_start(int alg)
11536e94dc5SPeter Avalos {
11636e94dc5SPeter Avalos const struct ssh_digest *digest = ssh_digest_by_alg(alg);
11736e94dc5SPeter Avalos struct ssh_digest_ctx *ret;
11836e94dc5SPeter Avalos
11936e94dc5SPeter Avalos if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
12036e94dc5SPeter Avalos return NULL;
12136e94dc5SPeter Avalos ret->alg = alg;
122664f4763Szrj if ((ret->mdctx = EVP_MD_CTX_new()) == NULL) {
12336e94dc5SPeter Avalos free(ret);
12436e94dc5SPeter Avalos return NULL;
12536e94dc5SPeter Avalos }
126664f4763Szrj if (EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
127664f4763Szrj ssh_digest_free(ret);
128664f4763Szrj return NULL;
129664f4763Szrj }
13036e94dc5SPeter Avalos return ret;
13136e94dc5SPeter Avalos }
13236e94dc5SPeter Avalos
13336e94dc5SPeter Avalos int
ssh_digest_copy_state(struct ssh_digest_ctx * from,struct ssh_digest_ctx * to)13436e94dc5SPeter Avalos ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
13536e94dc5SPeter Avalos {
13636e94dc5SPeter Avalos if (from->alg != to->alg)
13736e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
13836e94dc5SPeter Avalos /* we have bcopy-style order while openssl has memcpy-style */
139664f4763Szrj if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
14036e94dc5SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
14136e94dc5SPeter Avalos return 0;
14236e94dc5SPeter Avalos }
14336e94dc5SPeter Avalos
14436e94dc5SPeter Avalos int
ssh_digest_update(struct ssh_digest_ctx * ctx,const void * m,size_t mlen)14536e94dc5SPeter Avalos ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
14636e94dc5SPeter Avalos {
147664f4763Szrj if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
14836e94dc5SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
14936e94dc5SPeter Avalos return 0;
15036e94dc5SPeter Avalos }
15136e94dc5SPeter Avalos
15236e94dc5SPeter Avalos int
ssh_digest_update_buffer(struct ssh_digest_ctx * ctx,const struct sshbuf * b)15336e94dc5SPeter Avalos ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
15436e94dc5SPeter Avalos {
15536e94dc5SPeter Avalos return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
15636e94dc5SPeter Avalos }
15736e94dc5SPeter Avalos
15836e94dc5SPeter Avalos int
ssh_digest_final(struct ssh_digest_ctx * ctx,u_char * d,size_t dlen)15936e94dc5SPeter Avalos ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
16036e94dc5SPeter Avalos {
16136e94dc5SPeter Avalos const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
16236e94dc5SPeter Avalos u_int l = dlen;
16336e94dc5SPeter Avalos
164ce74bacaSMatthew Dillon if (digest == NULL || dlen > UINT_MAX)
16536e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
16636e94dc5SPeter Avalos if (dlen < digest->digest_len) /* No truncation allowed */
16736e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
168664f4763Szrj if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
16936e94dc5SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
17036e94dc5SPeter Avalos if (l != digest->digest_len) /* sanity */
17136e94dc5SPeter Avalos return SSH_ERR_INTERNAL_ERROR;
17236e94dc5SPeter Avalos return 0;
17336e94dc5SPeter Avalos }
17436e94dc5SPeter Avalos
17536e94dc5SPeter Avalos void
ssh_digest_free(struct ssh_digest_ctx * ctx)17636e94dc5SPeter Avalos ssh_digest_free(struct ssh_digest_ctx *ctx)
17736e94dc5SPeter Avalos {
178664f4763Szrj if (ctx == NULL)
179664f4763Szrj return;
180664f4763Szrj EVP_MD_CTX_free(ctx->mdctx);
181664f4763Szrj freezero(ctx, sizeof(*ctx));
18236e94dc5SPeter Avalos }
18336e94dc5SPeter Avalos
18436e94dc5SPeter Avalos int
ssh_digest_memory(int alg,const void * m,size_t mlen,u_char * d,size_t dlen)18536e94dc5SPeter Avalos ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
18636e94dc5SPeter Avalos {
18736e94dc5SPeter Avalos const struct ssh_digest *digest = ssh_digest_by_alg(alg);
18836e94dc5SPeter Avalos u_int mdlen;
18936e94dc5SPeter Avalos
19036e94dc5SPeter Avalos if (digest == NULL)
19136e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
19236e94dc5SPeter Avalos if (dlen > UINT_MAX)
19336e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
19436e94dc5SPeter Avalos if (dlen < digest->digest_len)
19536e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
19636e94dc5SPeter Avalos mdlen = dlen;
19736e94dc5SPeter Avalos if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
19836e94dc5SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
19936e94dc5SPeter Avalos return 0;
20036e94dc5SPeter Avalos }
20136e94dc5SPeter Avalos
20236e94dc5SPeter Avalos int
ssh_digest_buffer(int alg,const struct sshbuf * b,u_char * d,size_t dlen)20336e94dc5SPeter Avalos ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
20436e94dc5SPeter Avalos {
20536e94dc5SPeter Avalos return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
20636e94dc5SPeter Avalos }
207e9778795SPeter Avalos #endif /* WITH_OPENSSL */
208