blob: c2b0b2403d1171402cfa3c7d7ad46d7fdb30fde2 [file] [log] [blame]
djm@openbsd.org7bdb2ee2017-05-08 22:57:38 +00001/* $OpenBSD: digest-libc.c,v 1.6 2017/05/08 22:57:38 djm Exp $ */
Damien Millerec93d152014-02-04 11:07:13 +11002/*
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
Damien Miller72ef7c12015-01-15 02:21:31 +110021#ifndef WITH_OPENSSL
22
Damien Millerec93d152014-02-04 11:07:13 +110023#include <sys/types.h>
24#include <limits.h>
25#include <stdlib.h>
26#include <string.h>
27
Damien Miller72ef7c12015-01-15 02:21:31 +110028#if 0
Damien Millerec93d152014-02-04 11:07:13 +110029#include <md5.h>
30#include <rmd160.h>
31#include <sha1.h>
32#include <sha2.h>
Damien Miller72ef7c12015-01-15 02:21:31 +110033#endif
Damien Millerec93d152014-02-04 11:07:13 +110034
Damien Miller86687062014-07-02 15:28:02 +100035#include "ssherr.h"
36#include "sshbuf.h"
Damien Millerec93d152014-02-04 11:07:13 +110037#include "digest.h"
38
39typedef void md_init_fn(void *mdctx);
40typedef void md_update_fn(void *mdctx, const u_int8_t *m, size_t mlen);
41typedef void md_final_fn(u_int8_t[], void *mdctx);
42
43struct ssh_digest_ctx {
44 int alg;
45 void *mdctx;
46};
47
48struct ssh_digest {
49 int id;
50 const char *name;
51 size_t block_len;
52 size_t digest_len;
53 size_t ctx_len;
54 md_init_fn *md_init;
55 md_update_fn *md_update;
56 md_final_fn *md_final;
57};
58
59/* NB. Indexed directly by algorithm number */
60const struct ssh_digest digests[SSH_DIGEST_MAX] = {
61 {
62 SSH_DIGEST_MD5,
63 "MD5",
64 MD5_BLOCK_LENGTH,
65 MD5_DIGEST_LENGTH,
66 sizeof(MD5_CTX),
67 (md_init_fn *) MD5Init,
68 (md_update_fn *) MD5Update,
69 (md_final_fn *) MD5Final
70 },
71 {
Damien Millerec93d152014-02-04 11:07:13 +110072 SSH_DIGEST_SHA1,
73 "SHA1",
74 SHA1_BLOCK_LENGTH,
75 SHA1_DIGEST_LENGTH,
76 sizeof(SHA1_CTX),
77 (md_init_fn *) SHA1Init,
78 (md_update_fn *) SHA1Update,
79 (md_final_fn *) SHA1Final
80 },
81 {
82 SSH_DIGEST_SHA256,
83 "SHA256",
84 SHA256_BLOCK_LENGTH,
85 SHA256_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +110086 sizeof(SHA256_CTX),
87 (md_init_fn *) SHA256_Init,
88 (md_update_fn *) SHA256_Update,
89 (md_final_fn *) SHA256_Final
Damien Millerec93d152014-02-04 11:07:13 +110090 },
91 {
92 SSH_DIGEST_SHA384,
93 "SHA384",
94 SHA384_BLOCK_LENGTH,
95 SHA384_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +110096 sizeof(SHA384_CTX),
97 (md_init_fn *) SHA384_Init,
98 (md_update_fn *) SHA384_Update,
99 (md_final_fn *) SHA384_Final
Damien Millerec93d152014-02-04 11:07:13 +1100100 },
101 {
102 SSH_DIGEST_SHA512,
103 "SHA512",
104 SHA512_BLOCK_LENGTH,
105 SHA512_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +1100106 sizeof(SHA512_CTX),
107 (md_init_fn *) SHA512_Init,
108 (md_update_fn *) SHA512_Update,
109 (md_final_fn *) SHA512_Final
Damien Millerec93d152014-02-04 11:07:13 +1100110 }
111};
112
113static const struct ssh_digest *
114ssh_digest_by_alg(int alg)
115{
116 if (alg < 0 || alg >= SSH_DIGEST_MAX)
117 return NULL;
118 if (digests[alg].id != alg) /* sanity */
119 return NULL;
120 return &(digests[alg]);
121}
122
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000123int
124ssh_digest_alg_by_name(const char *name)
125{
126 int alg;
127
128 for (alg = 0; alg < SSH_DIGEST_MAX; alg++) {
129 if (strcasecmp(name, digests[alg].name) == 0)
130 return digests[alg].id;
131 }
132 return -1;
133}
134
135const char *
136ssh_digest_alg_name(int alg)
137{
138 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
139
140 return digest == NULL ? NULL : digest->name;
141}
142
Damien Millerec93d152014-02-04 11:07:13 +1100143size_t
144ssh_digest_bytes(int alg)
145{
146 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
147
148 return digest == NULL ? 0 : digest->digest_len;
149}
150
151size_t
152ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
153{
154 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
155
156 return digest == NULL ? 0 : digest->block_len;
157}
158
159struct ssh_digest_ctx *
160ssh_digest_start(int alg)
161{
162 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
163 struct ssh_digest_ctx *ret;
164
jsg@openbsd.orgd5b15072015-05-05 02:48:17 +0000165 if (digest == NULL || (ret = calloc(1, sizeof(*ret))) == NULL)
Damien Millerec93d152014-02-04 11:07:13 +1100166 return NULL;
167 if ((ret->mdctx = calloc(1, digest->ctx_len)) == NULL) {
168 free(ret);
169 return NULL;
170 }
171 ret->alg = alg;
172 digest->md_init(ret->mdctx);
173 return ret;
174}
175
176int
177ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
178{
179 const struct ssh_digest *digest = ssh_digest_by_alg(from->alg);
180
181 if (digest == NULL || from->alg != to->alg)
Damien Miller86687062014-07-02 15:28:02 +1000182 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100183 memcpy(to->mdctx, from->mdctx, digest->ctx_len);
184 return 0;
185}
186
187int
188ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
189{
190 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
191
192 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000193 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100194 digest->md_update(ctx->mdctx, m, mlen);
195 return 0;
196}
197
198int
Damien Miller86687062014-07-02 15:28:02 +1000199ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
Damien Millerec93d152014-02-04 11:07:13 +1100200{
Damien Miller86687062014-07-02 15:28:02 +1000201 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
Damien Millerec93d152014-02-04 11:07:13 +1100202}
203
204int
205ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
206{
207 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
208
209 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000210 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100211 if (dlen > UINT_MAX)
Damien Miller86687062014-07-02 15:28:02 +1000212 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100213 if (dlen < digest->digest_len) /* No truncation allowed */
Damien Miller86687062014-07-02 15:28:02 +1000214 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100215 digest->md_final(d, ctx->mdctx);
216 return 0;
217}
218
219void
220ssh_digest_free(struct ssh_digest_ctx *ctx)
221{
222 const struct ssh_digest *digest;
223
224 if (ctx != NULL) {
225 digest = ssh_digest_by_alg(ctx->alg);
226 if (digest) {
Damien Millerdb3c5952014-02-04 11:25:45 +1100227 explicit_bzero(ctx->mdctx, digest->ctx_len);
Damien Millerec93d152014-02-04 11:07:13 +1100228 free(ctx->mdctx);
Damien Millerdb3c5952014-02-04 11:25:45 +1100229 explicit_bzero(ctx, sizeof(*ctx));
Damien Millerec93d152014-02-04 11:07:13 +1100230 free(ctx);
231 }
232 }
233}
234
235int
236ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
237{
238 struct ssh_digest_ctx *ctx = ssh_digest_start(alg);
239
240 if (ctx == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000241 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100242 if (ssh_digest_update(ctx, m, mlen) != 0 ||
243 ssh_digest_final(ctx, d, dlen) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000244 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100245 ssh_digest_free(ctx);
246 return 0;
247}
248
249int
Damien Miller86687062014-07-02 15:28:02 +1000250ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
Damien Millerec93d152014-02-04 11:07:13 +1100251{
Damien Miller86687062014-07-02 15:28:02 +1000252 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
Damien Millerec93d152014-02-04 11:07:13 +1100253}
Damien Miller72ef7c12015-01-15 02:21:31 +1100254#endif /* !WITH_OPENSSL */