1 /* $OpenBSD: digest-libc.c,v 1.7 2020/02/26 13:40:09 jsg Exp $ */
2 /*
3  * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
4  * Copyright (c) 2014 Markus Friedl.  All rights reserved.
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 
19 #include "includes.h"
20 
21 #ifndef WITH_OPENSSL
22 
23 #include <sys/types.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #if 0
29 #include <md5.h>
30 #include <rmd160.h>
31 #endif
32 #ifdef HAVE_SHA1_H
33 #include <sha1.h>
34 #endif
35 #ifdef HAVE_SHA2_H
36 #include <sha2.h>
37 #endif
38 
39 #include "ssherr.h"
40 #include "sshbuf.h"
41 #include "digest.h"
42 
43 typedef void md_init_fn(void *mdctx);
44 typedef void md_update_fn(void *mdctx, const u_int8_t *m, size_t mlen);
45 typedef void md_final_fn(u_int8_t[], void *mdctx);
46 
47 struct ssh_digest_ctx {
48 	int alg;
49 	void *mdctx;
50 };
51 
52 struct ssh_digest {
53 	int id;
54 	const char *name;
55 	size_t block_len;
56 	size_t digest_len;
57 	size_t ctx_len;
58 	md_init_fn *md_init;
59 	md_update_fn *md_update;
60 	md_final_fn *md_final;
61 };
62 
63 /* NB. Indexed directly by algorithm number */
64 const struct ssh_digest digests[SSH_DIGEST_MAX] = {
65 	{
66 		SSH_DIGEST_MD5,
67 		"MD5",
68 		MD5_BLOCK_LENGTH,
69 		MD5_DIGEST_LENGTH,
70 		sizeof(MD5_CTX),
71 		(md_init_fn *) MD5Init,
72 		(md_update_fn *) MD5Update,
73 		(md_final_fn *) MD5Final
74 	},
75 	{
76 		SSH_DIGEST_SHA1,
77 		"SHA1",
78 		SHA1_BLOCK_LENGTH,
79 		SHA1_DIGEST_LENGTH,
80 		sizeof(SHA1_CTX),
81 		(md_init_fn *) SHA1Init,
82 		(md_update_fn *) SHA1Update,
83 		(md_final_fn *) SHA1Final
84 	},
85 	{
86 		SSH_DIGEST_SHA256,
87 		"SHA256",
88 		SHA256_BLOCK_LENGTH,
89 		SHA256_DIGEST_LENGTH,
90 		sizeof(SHA2_CTX),
91 		(md_init_fn *) SHA256Init,
92 		(md_update_fn *) SHA256Update,
93 		(md_final_fn *) SHA256Final
94 	},
95 	{
96 		SSH_DIGEST_SHA384,
97 		"SHA384",
98 		SHA384_BLOCK_LENGTH,
99 		SHA384_DIGEST_LENGTH,
100 		sizeof(SHA2_CTX),
101 		(md_init_fn *) SHA384Init,
102 		(md_update_fn *) SHA384Update,
103 		(md_final_fn *) SHA384Final
104 	},
105 	{
106 		SSH_DIGEST_SHA512,
107 		"SHA512",
108 		SHA512_BLOCK_LENGTH,
109 		SHA512_DIGEST_LENGTH,
110 		sizeof(SHA2_CTX),
111 		(md_init_fn *) SHA512Init,
112 		(md_update_fn *) SHA512Update,
113 		(md_final_fn *) SHA512Final
114 	}
115 };
116 
117 static const struct ssh_digest *
ssh_digest_by_alg(int alg)118 ssh_digest_by_alg(int alg)
119 {
120 	if (alg < 0 || alg >= SSH_DIGEST_MAX)
121 		return NULL;
122 	if (digests[alg].id != alg) /* sanity */
123 		return NULL;
124 	return &(digests[alg]);
125 }
126 
127 int
ssh_digest_alg_by_name(const char * name)128 ssh_digest_alg_by_name(const char *name)
129 {
130 	int alg;
131 
132 	for (alg = 0; alg < SSH_DIGEST_MAX; alg++) {
133 		if (strcasecmp(name, digests[alg].name) == 0)
134 			return digests[alg].id;
135 	}
136 	return -1;
137 }
138 
139 const char *
ssh_digest_alg_name(int alg)140 ssh_digest_alg_name(int alg)
141 {
142 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
143 
144 	return digest == NULL ? NULL : digest->name;
145 }
146 
147 size_t
ssh_digest_bytes(int alg)148 ssh_digest_bytes(int alg)
149 {
150 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
151 
152 	return digest == NULL ? 0 : digest->digest_len;
153 }
154 
155 size_t
ssh_digest_blocksize(struct ssh_digest_ctx * ctx)156 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
157 {
158 	const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
159 
160 	return digest == NULL ? 0 : digest->block_len;
161 }
162 
163 struct ssh_digest_ctx *
ssh_digest_start(int alg)164 ssh_digest_start(int alg)
165 {
166 	const struct ssh_digest *digest = ssh_digest_by_alg(alg);
167 	struct ssh_digest_ctx *ret;
168 
169 	if (digest == NULL || (ret = calloc(1, sizeof(*ret))) == NULL)
170 		return NULL;
171 	if ((ret->mdctx = calloc(1, digest->ctx_len)) == NULL) {
172 		free(ret);
173 		return NULL;
174 	}
175 	ret->alg = alg;
176 	digest->md_init(ret->mdctx);
177 	return ret;
178 }
179 
180 int
ssh_digest_copy_state(struct ssh_digest_ctx * from,struct ssh_digest_ctx * to)181 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
182 {
183 	const struct ssh_digest *digest = ssh_digest_by_alg(from->alg);
184 
185 	if (digest == NULL || from->alg != to->alg)
186 		return SSH_ERR_INVALID_ARGUMENT;
187 	memcpy(to->mdctx, from->mdctx, digest->ctx_len);
188 	return 0;
189 }
190 
191 int
ssh_digest_update(struct ssh_digest_ctx * ctx,const void * m,size_t mlen)192 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
193 {
194 	const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
195 
196 	if (digest == NULL)
197 		return SSH_ERR_INVALID_ARGUMENT;
198 	digest->md_update(ctx->mdctx, m, mlen);
199 	return 0;
200 }
201 
202 int
ssh_digest_update_buffer(struct ssh_digest_ctx * ctx,const struct sshbuf * b)203 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
204 {
205 	return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
206 }
207 
208 int
ssh_digest_final(struct ssh_digest_ctx * ctx,u_char * d,size_t dlen)209 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
210 {
211 	const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
212 
213 	if (digest == NULL)
214 		return SSH_ERR_INVALID_ARGUMENT;
215 	if (dlen > UINT_MAX)
216 		return SSH_ERR_INVALID_ARGUMENT;
217 	if (dlen < digest->digest_len) /* No truncation allowed */
218 		return SSH_ERR_INVALID_ARGUMENT;
219 	digest->md_final(d, ctx->mdctx);
220 	return 0;
221 }
222 
223 void
ssh_digest_free(struct ssh_digest_ctx * ctx)224 ssh_digest_free(struct ssh_digest_ctx *ctx)
225 {
226 	const struct ssh_digest *digest;
227 
228 	if (ctx != NULL) {
229 		digest = ssh_digest_by_alg(ctx->alg);
230 		if (digest) {
231 			explicit_bzero(ctx->mdctx, digest->ctx_len);
232 			free(ctx->mdctx);
233 			freezero(ctx, sizeof(*ctx));
234 		}
235 	}
236 }
237 
238 int
ssh_digest_memory(int alg,const void * m,size_t mlen,u_char * d,size_t dlen)239 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
240 {
241 	struct ssh_digest_ctx *ctx = ssh_digest_start(alg);
242 
243 	if (ctx == NULL)
244 		return SSH_ERR_INVALID_ARGUMENT;
245 	if (ssh_digest_update(ctx, m, mlen) != 0 ||
246 	    ssh_digest_final(ctx, d, dlen) != 0)
247 		return SSH_ERR_INVALID_ARGUMENT;
248 	ssh_digest_free(ctx);
249 	return 0;
250 }
251 
252 int
ssh_digest_buffer(int alg,const struct sshbuf * b,u_char * d,size_t dlen)253 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
254 {
255 	return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
256 }
257 #endif /* !WITH_OPENSSL */
258