blob: 6e8bf15d3590b9e9bbd1db069b09b4ab07980769 [file] [log] [blame]
Damien Millerc174a3b2014-07-03 21:23:24 +10001/* $OpenBSD: digest-openssl.c,v 1.4 2014/07/03 03:26:43 djm Exp $ */
Damien Millerb3051d02014-01-10 10:58:53 +11002/*
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 "includes.h"
19
20#include <sys/types.h>
21#include <limits.h>
22#include <stdlib.h>
23#include <string.h>
24
Damien Millerb3051d02014-01-10 10:58:53 +110025#include <openssl/evp.h>
26
Darren Tuckerd23a91f2014-01-17 17:32:30 +110027#include "openbsd-compat/openssl-compat.h"
28
Damien Miller86687062014-07-02 15:28:02 +100029#include "sshbuf.h"
Damien Millerb3051d02014-01-10 10:58:53 +110030#include "digest.h"
Damien Miller86687062014-07-02 15:28:02 +100031#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110032
33struct ssh_digest_ctx {
34 int alg;
35 EVP_MD_CTX mdctx;
36};
37
38struct ssh_digest {
39 int id;
40 const char *name;
41 size_t digest_len;
42 const EVP_MD *(*mdfunc)(void);
43};
44
45/* NB. Indexed directly by algorithm number */
46const struct ssh_digest digests[] = {
47 { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 },
Damien Miller8da0fa22014-07-03 11:54:19 +100048#ifdef HAVE_EVP_RIPEMD160 /* XXX replace with local if missing */
Damien Millerb3051d02014-01-10 10:58:53 +110049 { SSH_DIGEST_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 },
Damien Miller8da0fa22014-07-03 11:54:19 +100050#endif
Damien Millerb3051d02014-01-10 10:58:53 +110051 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 },
52#ifdef HAVE_EVP_SHA256 /* XXX replace with local if missing */
53 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 },
54 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 },
55 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 },
56#endif
57 { -1, NULL, 0, NULL },
58};
59
60static const struct ssh_digest *
61ssh_digest_by_alg(int alg)
62{
63 if (alg < 0 || alg >= SSH_DIGEST_MAX)
64 return NULL;
65 if (digests[alg].id != alg) /* sanity */
66 return NULL;
67 return &(digests[alg]);
68}
69
70size_t
71ssh_digest_bytes(int alg)
72{
73 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
74
75 return digest == NULL ? 0 : digest->digest_len;
76}
77
Damien Miller4e8d9372014-02-04 11:02:42 +110078size_t
79ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
80{
81 return EVP_MD_CTX_block_size(&ctx->mdctx);
82}
83
Damien Millerb3051d02014-01-10 10:58:53 +110084struct ssh_digest_ctx *
85ssh_digest_start(int alg)
86{
87 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
88 struct ssh_digest_ctx *ret;
89
90 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
91 return NULL;
92 ret->alg = alg;
93 EVP_MD_CTX_init(&ret->mdctx);
94 if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
95 free(ret);
96 return NULL;
97 }
98 return ret;
99}
100
101int
Damien Miller4e8d9372014-02-04 11:02:42 +1100102ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
103{
Damien Miller86687062014-07-02 15:28:02 +1000104 if (from->alg != to->alg)
105 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller4e8d9372014-02-04 11:02:42 +1100106 /* we have bcopy-style order while openssl has memcpy-style */
107 if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
Damien Miller86687062014-07-02 15:28:02 +1000108 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller4e8d9372014-02-04 11:02:42 +1100109 return 0;
110}
111
112int
Damien Millerb3051d02014-01-10 10:58:53 +1100113ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
114{
115 if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
Damien Miller86687062014-07-02 15:28:02 +1000116 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100117 return 0;
118}
119
120int
Damien Miller86687062014-07-02 15:28:02 +1000121ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
Damien Millerb3051d02014-01-10 10:58:53 +1100122{
Damien Miller86687062014-07-02 15:28:02 +1000123 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
Damien Millerb3051d02014-01-10 10:58:53 +1100124}
125
126int
127ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
128{
129 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
130 u_int l = dlen;
131
132 if (dlen > UINT_MAX)
Damien Miller86687062014-07-02 15:28:02 +1000133 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerb3051d02014-01-10 10:58:53 +1100134 if (dlen < digest->digest_len) /* No truncation allowed */
Damien Miller86687062014-07-02 15:28:02 +1000135 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerb3051d02014-01-10 10:58:53 +1100136 if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
Damien Miller86687062014-07-02 15:28:02 +1000137 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100138 if (l != digest->digest_len) /* sanity */
Damien Miller86687062014-07-02 15:28:02 +1000139 return SSH_ERR_INTERNAL_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100140 return 0;
141}
142
143void
144ssh_digest_free(struct ssh_digest_ctx *ctx)
145{
Damien Miller4e8d9372014-02-04 11:02:42 +1100146 if (ctx != NULL) {
147 EVP_MD_CTX_cleanup(&ctx->mdctx);
Damien Millerdb3c5952014-02-04 11:25:45 +1100148 explicit_bzero(ctx, sizeof(*ctx));
Damien Miller4e8d9372014-02-04 11:02:42 +1100149 free(ctx);
150 }
Damien Millerb3051d02014-01-10 10:58:53 +1100151}
152
153int
154ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
155{
Damien Millerc174a3b2014-07-03 21:23:24 +1000156 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
157 u_int mdlen;
Damien Millerb3051d02014-01-10 10:58:53 +1100158
Damien Millerc174a3b2014-07-03 21:23:24 +1000159 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000160 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerc174a3b2014-07-03 21:23:24 +1000161 if (dlen > UINT_MAX)
162 return SSH_ERR_INVALID_ARGUMENT;
163 if (dlen < digest->digest_len)
164 return SSH_ERR_INVALID_ARGUMENT;
165 mdlen = dlen;
166 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
167 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100168 return 0;
169}
170
171int
Damien Miller86687062014-07-02 15:28:02 +1000172ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
Damien Millerb3051d02014-01-10 10:58:53 +1100173{
Damien Miller86687062014-07-02 15:28:02 +1000174 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
Damien Millerb3051d02014-01-10 10:58:53 +1100175}