blob: a216e784e2ae67997d7bb4fa68e6f90fd6cd0f35 [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: digest-libc.c,v 1.4 2014/12/21 22:27:56 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 {
72 SSH_DIGEST_RIPEMD160,
73 "RIPEMD160",
74 RMD160_BLOCK_LENGTH,
75 RMD160_DIGEST_LENGTH,
76 sizeof(RMD160_CTX),
77 (md_init_fn *) RMD160Init,
78 (md_update_fn *) RMD160Update,
79 (md_final_fn *) RMD160Final
80 },
81 {
82 SSH_DIGEST_SHA1,
83 "SHA1",
84 SHA1_BLOCK_LENGTH,
85 SHA1_DIGEST_LENGTH,
86 sizeof(SHA1_CTX),
87 (md_init_fn *) SHA1Init,
88 (md_update_fn *) SHA1Update,
89 (md_final_fn *) SHA1Final
90 },
91 {
92 SSH_DIGEST_SHA256,
93 "SHA256",
94 SHA256_BLOCK_LENGTH,
95 SHA256_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +110096 sizeof(SHA256_CTX),
97 (md_init_fn *) SHA256_Init,
98 (md_update_fn *) SHA256_Update,
99 (md_final_fn *) SHA256_Final
Damien Millerec93d152014-02-04 11:07:13 +1100100 },
101 {
102 SSH_DIGEST_SHA384,
103 "SHA384",
104 SHA384_BLOCK_LENGTH,
105 SHA384_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +1100106 sizeof(SHA384_CTX),
107 (md_init_fn *) SHA384_Init,
108 (md_update_fn *) SHA384_Update,
109 (md_final_fn *) SHA384_Final
Damien Millerec93d152014-02-04 11:07:13 +1100110 },
111 {
112 SSH_DIGEST_SHA512,
113 "SHA512",
114 SHA512_BLOCK_LENGTH,
115 SHA512_DIGEST_LENGTH,
Damien Miller72ef7c12015-01-15 02:21:31 +1100116 sizeof(SHA512_CTX),
117 (md_init_fn *) SHA512_Init,
118 (md_update_fn *) SHA512_Update,
119 (md_final_fn *) SHA512_Final
Damien Millerec93d152014-02-04 11:07:13 +1100120 }
121};
122
123static const struct ssh_digest *
124ssh_digest_by_alg(int alg)
125{
126 if (alg < 0 || alg >= SSH_DIGEST_MAX)
127 return NULL;
128 if (digests[alg].id != alg) /* sanity */
129 return NULL;
130 return &(digests[alg]);
131}
132
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000133int
134ssh_digest_alg_by_name(const char *name)
135{
136 int alg;
137
138 for (alg = 0; alg < SSH_DIGEST_MAX; alg++) {
139 if (strcasecmp(name, digests[alg].name) == 0)
140 return digests[alg].id;
141 }
142 return -1;
143}
144
145const char *
146ssh_digest_alg_name(int alg)
147{
148 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
149
150 return digest == NULL ? NULL : digest->name;
151}
152
Damien Millerec93d152014-02-04 11:07:13 +1100153size_t
154ssh_digest_bytes(int alg)
155{
156 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
157
158 return digest == NULL ? 0 : digest->digest_len;
159}
160
161size_t
162ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
163{
164 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
165
166 return digest == NULL ? 0 : digest->block_len;
167}
168
169struct ssh_digest_ctx *
170ssh_digest_start(int alg)
171{
172 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
173 struct ssh_digest_ctx *ret;
174
175 if (digest == NULL || (ret = calloc(1, sizeof(ret))) == NULL)
176 return NULL;
177 if ((ret->mdctx = calloc(1, digest->ctx_len)) == NULL) {
178 free(ret);
179 return NULL;
180 }
181 ret->alg = alg;
182 digest->md_init(ret->mdctx);
183 return ret;
184}
185
186int
187ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
188{
189 const struct ssh_digest *digest = ssh_digest_by_alg(from->alg);
190
191 if (digest == NULL || from->alg != to->alg)
Damien Miller86687062014-07-02 15:28:02 +1000192 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100193 memcpy(to->mdctx, from->mdctx, digest->ctx_len);
194 return 0;
195}
196
197int
198ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
199{
200 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
201
202 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000203 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100204 digest->md_update(ctx->mdctx, m, mlen);
205 return 0;
206}
207
208int
Damien Miller86687062014-07-02 15:28:02 +1000209ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
Damien Millerec93d152014-02-04 11:07:13 +1100210{
Damien Miller86687062014-07-02 15:28:02 +1000211 return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
Damien Millerec93d152014-02-04 11:07:13 +1100212}
213
214int
215ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
216{
217 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
218
219 if (digest == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000220 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100221 if (dlen > UINT_MAX)
Damien Miller86687062014-07-02 15:28:02 +1000222 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100223 if (dlen < digest->digest_len) /* No truncation allowed */
Damien Miller86687062014-07-02 15:28:02 +1000224 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100225 digest->md_final(d, ctx->mdctx);
226 return 0;
227}
228
229void
230ssh_digest_free(struct ssh_digest_ctx *ctx)
231{
232 const struct ssh_digest *digest;
233
234 if (ctx != NULL) {
235 digest = ssh_digest_by_alg(ctx->alg);
236 if (digest) {
Damien Millerdb3c5952014-02-04 11:25:45 +1100237 explicit_bzero(ctx->mdctx, digest->ctx_len);
Damien Millerec93d152014-02-04 11:07:13 +1100238 free(ctx->mdctx);
Damien Millerdb3c5952014-02-04 11:25:45 +1100239 explicit_bzero(ctx, sizeof(*ctx));
Damien Millerec93d152014-02-04 11:07:13 +1100240 free(ctx);
241 }
242 }
243}
244
245int
246ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
247{
248 struct ssh_digest_ctx *ctx = ssh_digest_start(alg);
249
250 if (ctx == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000251 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100252 if (ssh_digest_update(ctx, m, mlen) != 0 ||
253 ssh_digest_final(ctx, d, dlen) != 0)
Damien Miller86687062014-07-02 15:28:02 +1000254 return SSH_ERR_INVALID_ARGUMENT;
Damien Millerec93d152014-02-04 11:07:13 +1100255 ssh_digest_free(ctx);
256 return 0;
257}
258
259int
Damien Miller86687062014-07-02 15:28:02 +1000260ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
Damien Millerec93d152014-02-04 11:07:13 +1100261{
Damien Miller86687062014-07-02 15:28:02 +1000262 return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
Damien Millerec93d152014-02-04 11:07:13 +1100263}
Damien Miller72ef7c12015-01-15 02:21:31 +1100264#endif /* !WITH_OPENSSL */