summaryrefslogtreecommitdiff
path: root/digest-libc.c
diff options
context:
space:
mode:
Diffstat (limited to 'digest-libc.c')
-rw-r--r--digest-libc.c238
1 files changed, 238 insertions, 0 deletions
diff --git a/digest-libc.c b/digest-libc.c
new file mode 100644
index 000000000..e1fcda71a
--- /dev/null
+++ b/digest-libc.c
@@ -0,0 +1,238 @@
1/* $OpenBSD: digest-libc.c,v 1.1 2014/01/28 20:13:46 markus 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#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 "buffer.h"
32#include "digest.h"
33
34typedef void md_init_fn(void *mdctx);
35typedef void md_update_fn(void *mdctx, const u_int8_t *m, size_t mlen);
36typedef void md_final_fn(u_int8_t[], void *mdctx);
37
38struct ssh_digest_ctx {
39 int alg;
40 void *mdctx;
41};
42
43struct ssh_digest {
44 int id;
45 const char *name;
46 size_t block_len;
47 size_t digest_len;
48 size_t ctx_len;
49 md_init_fn *md_init;
50 md_update_fn *md_update;
51 md_final_fn *md_final;
52};
53
54/* NB. Indexed directly by algorithm number */
55const struct ssh_digest digests[SSH_DIGEST_MAX] = {
56 {
57 SSH_DIGEST_MD5,
58 "MD5",
59 MD5_BLOCK_LENGTH,
60 MD5_DIGEST_LENGTH,
61 sizeof(MD5_CTX),
62 (md_init_fn *) MD5Init,
63 (md_update_fn *) MD5Update,
64 (md_final_fn *) MD5Final
65 },
66 {
67 SSH_DIGEST_RIPEMD160,
68 "RIPEMD160",
69 RMD160_BLOCK_LENGTH,
70 RMD160_DIGEST_LENGTH,
71 sizeof(RMD160_CTX),
72 (md_init_fn *) RMD160Init,
73 (md_update_fn *) RMD160Update,
74 (md_final_fn *) RMD160Final
75 },
76 {
77 SSH_DIGEST_SHA1,
78 "SHA1",
79 SHA1_BLOCK_LENGTH,
80 SHA1_DIGEST_LENGTH,
81 sizeof(SHA1_CTX),
82 (md_init_fn *) SHA1Init,
83 (md_update_fn *) SHA1Update,
84 (md_final_fn *) SHA1Final
85 },
86 {
87 SSH_DIGEST_SHA256,
88 "SHA256",
89 SHA256_BLOCK_LENGTH,
90 SHA256_DIGEST_LENGTH,
91 sizeof(SHA2_CTX),
92 (md_init_fn *) SHA256Init,
93 (md_update_fn *) SHA256Update,
94 (md_final_fn *) SHA256Final
95 },
96 {
97 SSH_DIGEST_SHA384,
98 "SHA384",
99 SHA384_BLOCK_LENGTH,
100 SHA384_DIGEST_LENGTH,
101 sizeof(SHA2_CTX),
102 (md_init_fn *) SHA384Init,
103 (md_update_fn *) SHA384Update,
104 (md_final_fn *) SHA384Final
105 },
106 {
107 SSH_DIGEST_SHA512,
108 "SHA512",
109 SHA512_BLOCK_LENGTH,
110 SHA512_DIGEST_LENGTH,
111 sizeof(SHA2_CTX),
112 (md_init_fn *) SHA512Init,
113 (md_update_fn *) SHA512Update,
114 (md_final_fn *) SHA512Final
115 }
116};
117
118static const struct ssh_digest *
119ssh_digest_by_alg(int alg)
120{
121 if (alg < 0 || alg >= SSH_DIGEST_MAX)
122 return NULL;
123 if (digests[alg].id != alg) /* sanity */
124 return NULL;
125 return &(digests[alg]);
126}
127
128size_t
129ssh_digest_bytes(int alg)
130{
131 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
132
133 return digest == NULL ? 0 : digest->digest_len;
134}
135
136size_t
137ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
138{
139 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
140
141 return digest == NULL ? 0 : digest->block_len;
142}
143
144struct ssh_digest_ctx *
145ssh_digest_start(int alg)
146{
147 const struct ssh_digest *digest = ssh_digest_by_alg(alg);
148 struct ssh_digest_ctx *ret;
149
150 if (digest == NULL || (ret = calloc(1, sizeof(ret))) == NULL)
151 return NULL;
152 if ((ret->mdctx = calloc(1, digest->ctx_len)) == NULL) {
153 free(ret);
154 return NULL;
155 }
156 ret->alg = alg;
157 digest->md_init(ret->mdctx);
158 return ret;
159}
160
161int
162ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
163{
164 const struct ssh_digest *digest = ssh_digest_by_alg(from->alg);
165
166 if (digest == NULL || from->alg != to->alg)
167 return -1;
168 memcpy(to->mdctx, from->mdctx, digest->ctx_len);
169 return 0;
170}
171
172int
173ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
174{
175 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
176
177 if (digest == NULL)
178 return -1;
179 digest->md_update(ctx->mdctx, m, mlen);
180 return 0;
181}
182
183int
184ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const Buffer *b)
185{
186 return ssh_digest_update(ctx, buffer_ptr(b), buffer_len(b));
187}
188
189int
190ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
191{
192 const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
193
194 if (digest == NULL)
195 return -1;
196 if (dlen > UINT_MAX)
197 return -1;
198 if (dlen < digest->digest_len) /* No truncation allowed */
199 return -1;
200 digest->md_final(d, ctx->mdctx);
201 return 0;
202}
203
204void
205ssh_digest_free(struct ssh_digest_ctx *ctx)
206{
207 const struct ssh_digest *digest;
208
209 if (ctx != NULL) {
210 digest = ssh_digest_by_alg(ctx->alg);
211 if (digest) {
212 memset(ctx->mdctx, 0, digest->ctx_len);
213 free(ctx->mdctx);
214 memset(ctx, 0, sizeof(*ctx));
215 free(ctx);
216 }
217 }
218}
219
220int
221ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
222{
223 struct ssh_digest_ctx *ctx = ssh_digest_start(alg);
224
225 if (ctx == NULL)
226 return -1;
227 if (ssh_digest_update(ctx, m, mlen) != 0 ||
228 ssh_digest_final(ctx, d, dlen) != 0)
229 return -1;
230 ssh_digest_free(ctx);
231 return 0;
232}
233
234int
235ssh_digest_buffer(int alg, const Buffer *b, u_char *d, size_t dlen)
236{
237 return ssh_digest_memory(alg, buffer_ptr(b), buffer_len(b), d, dlen);
238}