xref: /netbsd-src/crypto/external/bsd/openssh/dist/digest-openssl.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: digest-openssl.c,v 1.8 2019/04/20 17:16:40 christos Exp $	*/
2 /* $OpenBSD: digest-openssl.c,v 1.8 2018/09/13 02:08:33 djm Exp $ */
3 /*
4  * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include "includes.h"
19 __RCSID("$NetBSD: digest-openssl.c,v 1.8 2019/04/20 17:16:40 christos Exp $");
20 
21 #include <sys/types.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <openssl/evp.h>
27 
28 #include "sshbuf.h"
29 #include "digest.h"
30 #include "ssherr.h"
31 
32 struct ssh_digest_ctx {
33 	int alg;
34 	EVP_MD_CTX *mdctx;
35 };
36 
37 struct ssh_digest {
38 	int id;
39 	const char *name;
40 	size_t digest_len;
41 	const EVP_MD *(*mdfunc)(void);
42 };
43 
44 /* NB. Indexed directly by algorithm number */
45 const struct ssh_digest digests[] = {
46 	{ SSH_DIGEST_MD5,	"MD5",	 	16,	EVP_md5 },
47 	{ SSH_DIGEST_SHA1,	"SHA1",	 	20,	EVP_sha1 },
48 	{ SSH_DIGEST_SHA256,	"SHA256", 	32,	EVP_sha256 },
49 	{ SSH_DIGEST_SHA384,	"SHA384",	48,	EVP_sha384 },
50 	{ SSH_DIGEST_SHA512,	"SHA512", 	64,	EVP_sha512 },
51 	{ -1,			NULL,		0,	NULL },
52 };
53 
54 static const struct ssh_digest *
55 ssh_digest_by_alg(int alg)
56 {
57 	if (alg < 0 || alg >= SSH_DIGEST_MAX)
58 		return NULL;
59 	if (digests[alg].id != alg) /* sanity */
60 		return NULL;
61 	return &(digests[alg]);
62 }
63 
64 int
65 ssh_digest_alg_by_name(const char *name)
66 {
67 	int alg;
68 
69 	for (alg = 0; digests[alg].id != -1; alg++) {
70 		if (strcasecmp(name, digests[alg].name) == 0)
71 			return digests[alg].id;
72 	}
73 	return -1;
74 }
75 
76 const char *
77 ssh_digest_alg_name(int alg)
78 {
79 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
80 
81 	return digest == NULL ? NULL : digest->name;
82 }
83 
84 size_t
85 ssh_digest_bytes(int alg)
86 {
87 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
88 
89 	return digest == NULL ? 0 : digest->digest_len;
90 }
91 
92 size_t
93 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
94 {
95 	return EVP_MD_CTX_block_size(ctx->mdctx);
96 }
97 
98 struct ssh_digest_ctx *
99 ssh_digest_start(int alg)
100 {
101 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
102 	struct ssh_digest_ctx *ret = NULL;
103 
104 	if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
105 		return NULL;
106 	ret->alg = alg;
107 	if ((ret->mdctx = EVP_MD_CTX_new()) == NULL) {
108 		free(ret);
109 		return NULL;
110 	}
111 	if (EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
112 		ssh_digest_free(ret);
113 		return NULL;
114 	}
115 	return ret;
116 }
117 
118 int
119 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
120 {
121 	if (from->alg != to->alg)
122 		return SSH_ERR_INVALID_ARGUMENT;
123 	/* we have bcopy-style order while openssl has memcpy-style */
124 	if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
125 		return SSH_ERR_LIBCRYPTO_ERROR;
126 	return 0;
127 }
128 
129 int
130 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
131 {
132 	if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
133 		return SSH_ERR_LIBCRYPTO_ERROR;
134 	return 0;
135 }
136 
137 int
138 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
139 {
140 	return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
141 }
142 
143 int
144 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
145 {
146 	const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
147 	u_int l = dlen;
148 
149 	if (digest == NULL || dlen > UINT_MAX)
150 		return SSH_ERR_INVALID_ARGUMENT;
151 	if (dlen < digest->digest_len) /* No truncation allowed */
152 		return SSH_ERR_INVALID_ARGUMENT;
153 	if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
154 		return SSH_ERR_LIBCRYPTO_ERROR;
155 	if (l != digest->digest_len) /* sanity */
156 		return SSH_ERR_INTERNAL_ERROR;
157 	return 0;
158 }
159 
160 void
161 ssh_digest_free(struct ssh_digest_ctx *ctx)
162 {
163 	if (ctx == NULL)
164 		return;
165 	EVP_MD_CTX_free(ctx->mdctx);
166 	freezero(ctx, sizeof(*ctx));
167 }
168 
169 int
170 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
171 {
172 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
173 	u_int mdlen;
174 
175 	if (digest == NULL)
176 		return SSH_ERR_INVALID_ARGUMENT;
177 	if (dlen > UINT_MAX)
178 		return SSH_ERR_INVALID_ARGUMENT;
179 	if (dlen < digest->digest_len)
180 		return SSH_ERR_INVALID_ARGUMENT;
181 	mdlen = dlen;
182 	if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
183 		return SSH_ERR_LIBCRYPTO_ERROR;
184 	return 0;
185 }
186 
187 int
188 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
189 {
190 	return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
191 }
192