blob: bb58ff2260636f6aa410016445d2f994d42bfd6c [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: digest-openssl.c,v 1.5 2014/12/21 22:27:56 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
Damien Millerf6293a02014-07-17 09:01:25 +100033#ifndef HAVE_EVP_RIPEMD160
34# define EVP_ripemd160 NULL
35#endif /* HAVE_EVP_RIPEMD160 */
36#ifndef HAVE_EVP_SHA256
37# define EVP_sha256 NULL
38# define EVP_sha384 NULL
39# define EVP_sha512 NULL
40#endif /* HAVE_EVP_SHA256 */
41
Damien Millerb3051d02014-01-10 10:58:53 +110042struct ssh_digest_ctx {
43 int alg;
44 EVP_MD_CTX mdctx;
45};
46
47struct ssh_digest {
48 int id;
49 const char *name;
50 size_t digest_len;
51 const EVP_MD *(*mdfunc)(void);
52};
53
54/* NB. Indexed directly by algorithm number */
55const struct ssh_digest digests[] = {
56 { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 },
57 { SSH_DIGEST_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 },
58 { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 },
Damien Millerb3051d02014-01-10 10:58:53 +110059 { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 },
60 { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 },
61 { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 },
Damien Millerb3051d02014-01-10 10:58:53 +110062 { -1, NULL, 0, NULL },
63};
64
65static const struct ssh_digest *
66ssh_digest_by_alg(int alg)
67{
68 if (alg < 0 || alg >= SSH_DIGEST_MAX)
69 return NULL;
70 if (digests[alg].id != alg) /* sanity */
71 return NULL;
Damien Millerf6293a02014-07-17 09:01:25 +100072 if (digests[alg].mdfunc == NULL)
73 return NULL;
Damien Millerb3051d02014-01-10 10:58:53 +110074 return &(digests[alg]);
75}
76
djm@openbsd.org56d1c832014-12-21 22:27:55 +000077int
78ssh_digest_alg_by_name(const char *name)
79{
80 int alg;
81
82 for (alg = 0; digests[alg].id != -1; alg++) {
83 if (strcasecmp(name, digests[alg].name) == 0)
84 return digests[alg].id;
85 }
86 return -1;
87}
88
89const char *
90ssh_digest_alg_name(int alg)
91{
92 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
93
94 return digest == NULL ? NULL : digest->name;
95}
96
Damien Millerb3051d02014-01-10 10:58:53 +110097size_t
98ssh_digest_bytes(int alg)
99{
100 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
101
102 return digest == NULL ? 0 : digest->digest_len;
103}
104
Damien Miller4e8d9372014-02-04 11:02:42 +1100105size_t
106ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
107{
108 return EVP_MD_CTX_block_size(&ctx->mdctx);
109}
110
Damien Millerb3051d02014-01-10 10:58:53 +1100111struct ssh_digest_ctx *
112ssh_digest_start(int alg)
113{
114 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
115 struct ssh_digest_ctx *ret;
116
117 if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
118 return NULL;
119 ret->alg = alg;
120 EVP_MD_CTX_init(&ret->mdctx);
121 if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) {
122 free(ret);
123 return NULL;
124 }
125 return ret;
126}
127
128int
Damien Miller4e8d9372014-02-04 11:02:42 +1100129ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
130{
Damien Miller86687062014-07-02 15:28:02 +1000131 if (from->alg != to->alg)
132 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller4e8d9372014-02-04 11:02:42 +1100133 /* we have bcopy-style order while openssl has memcpy-style */
134 if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx))
Damien Miller86687062014-07-02 15:28:02 +1000135 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller4e8d9372014-02-04 11:02:42 +1100136 return 0;
137}
138
139int
Damien Millerb3051d02014-01-10 10:58:53 +1100140ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
141{
142 if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1)
Damien Miller86687062014-07-02 15:28:02 +1000143 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100144 return 0;
145}
146
147int
Damien Miller86687062014-07-02 15:28:02 +1000148ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
Damien Millerb3051d02014-01-10 10:58:53 +1100149{
Damien Miller86687062014-07-02 15:28:02 +1000150 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
Damien Millerb3051d02014-01-10 10:58:53 +1100151}
152
153int
154ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
155{
156 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
157 u_int l = dlen;
158
159 if (dlen > UINT_MAX)
Damien Miller86687062014-07-02 15:28:02 +1000160 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerb3051d02014-01-10 10:58:53 +1100161 if (dlen < digest->digest_len) /* No truncation allowed */
Damien Miller86687062014-07-02 15:28:02 +1000162 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerb3051d02014-01-10 10:58:53 +1100163 if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1)
Damien Miller86687062014-07-02 15:28:02 +1000164 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100165 if (l != digest->digest_len) /* sanity */
Damien Miller86687062014-07-02 15:28:02 +1000166 return SSH_ERR_INTERNAL_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100167 return 0;
168}
169
170void
171ssh_digest_free(struct ssh_digest_ctx *ctx)
172{
Damien Miller4e8d9372014-02-04 11:02:42 +1100173 if (ctx != NULL) {
174 EVP_MD_CTX_cleanup(&ctx->mdctx);
Damien Millerdb3c5952014-02-04 11:25:45 +1100175 explicit_bzero(ctx, sizeof(*ctx));
Damien Miller4e8d9372014-02-04 11:02:42 +1100176 free(ctx);
177 }
Damien Millerb3051d02014-01-10 10:58:53 +1100178}
179
180int
181ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
182{
Damien Millerc174a3b2014-07-03 21:23:24 +1000183 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
184 u_int mdlen;
Damien Millerb3051d02014-01-10 10:58:53 +1100185
Damien Millerc174a3b2014-07-03 21:23:24 +1000186 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000187 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerc174a3b2014-07-03 21:23:24 +1000188 if (dlen > UINT_MAX)
189 return SSH_ERR_INVALID_ARGUMENT;
190 if (dlen < digest->digest_len)
191 return SSH_ERR_INVALID_ARGUMENT;
192 mdlen = dlen;
193 if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
194 return SSH_ERR_LIBCRYPTO_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100195 return 0;
196}
197
198int
Damien Miller86687062014-07-02 15:28:02 +1000199ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
Damien Millerb3051d02014-01-10 10:58:53 +1100200{
Damien Miller86687062014-07-02 15:28:02 +1000201 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
Damien Millerb3051d02014-01-10 10:58:53 +1100202}