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