1 /*        $NetBSD: digest-openssl.c,v 1.9 2021/03/05 17:47:16 christos Exp $    */
2 /* $OpenBSD: digest-openssl.c,v 1.9 2020/10/29 02:52:43 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #include "includes.h"
20 __RCSID("$NetBSD: digest-openssl.c,v 1.9 2021/03/05 17:47:16 christos Exp $");
21 
22 #include <sys/types.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <openssl/evp.h>
28 
29 #include "sshbuf.h"
30 #include "digest.h"
31 #include "ssherr.h"
32 
33 struct ssh_digest_ctx {
34           int alg;
35           EVP_MD_CTX *mdctx;
36 };
37 
38 struct ssh_digest {
39           int id;
40           const char *name;
41           size_t digest_len;
42           const EVP_MD *(*mdfunc)(void);
43 };
44 
45 /* NB. Indexed directly by algorithm number */
46 const struct ssh_digest digests[] = {
47           { SSH_DIGEST_MD5,   "MD5",              16,       EVP_md5 },
48           { SSH_DIGEST_SHA1,  "SHA1",             20,       EVP_sha1 },
49           { SSH_DIGEST_SHA256,          "SHA256", 32,       EVP_sha256 },
50           { SSH_DIGEST_SHA384,          "SHA384", 48,       EVP_sha384 },
51           { SSH_DIGEST_SHA512,          "SHA512", 64,       EVP_sha512 },
52           { -1,                         NULL,               0,        NULL },
53 };
54 
55 static const struct ssh_digest *
ssh_digest_by_alg(int alg)56 ssh_digest_by_alg(int alg)
57 {
58           if (alg < 0 || alg >= SSH_DIGEST_MAX)
59                     return NULL;
60           if (digests[alg].id != alg) /* sanity */
61                     return NULL;
62           return &(digests[alg]);
63 }
64 
65 int
ssh_digest_alg_by_name(const char * name)66 ssh_digest_alg_by_name(const char *name)
67 {
68           int alg;
69 
70           for (alg = 0; digests[alg].id != -1; alg++) {
71                     if (strcasecmp(name, digests[alg].name) == 0)
72                               return digests[alg].id;
73           }
74           return -1;
75 }
76 
77 const char *
ssh_digest_alg_name(int alg)78 ssh_digest_alg_name(int alg)
79 {
80           const struct ssh_digest *digest = ssh_digest_by_alg(alg);
81 
82           return digest == NULL ? NULL : digest->name;
83 }
84 
85 size_t
ssh_digest_bytes(int alg)86 ssh_digest_bytes(int alg)
87 {
88           const struct ssh_digest *digest = ssh_digest_by_alg(alg);
89 
90           return digest == NULL ? 0 : digest->digest_len;
91 }
92 
93 size_t
ssh_digest_blocksize(struct ssh_digest_ctx * ctx)94 ssh_digest_blocksize(struct ssh_digest_ctx *ctx)
95 {
96           return EVP_MD_CTX_block_size(ctx->mdctx);
97 }
98 
99 struct ssh_digest_ctx *
ssh_digest_start(int alg)100 ssh_digest_start(int alg)
101 {
102           const struct ssh_digest *digest = ssh_digest_by_alg(alg);
103           struct ssh_digest_ctx *ret = NULL;
104 
105           if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL))
106                     return NULL;
107           ret->alg = alg;
108           if ((ret->mdctx = EVP_MD_CTX_new()) == NULL) {
109                     free(ret);
110                     return NULL;
111           }
112           if (EVP_DigestInit_ex(ret->mdctx, digest->mdfunc(), NULL) != 1) {
113                     ssh_digest_free(ret);
114                     return NULL;
115           }
116           return ret;
117 }
118 
119 int
ssh_digest_copy_state(struct ssh_digest_ctx * from,struct ssh_digest_ctx * to)120 ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to)
121 {
122           if (from->alg != to->alg)
123                     return SSH_ERR_INVALID_ARGUMENT;
124           /* we have bcopy-style order while openssl has memcpy-style */
125           if (!EVP_MD_CTX_copy_ex(to->mdctx, from->mdctx))
126                     return SSH_ERR_LIBCRYPTO_ERROR;
127           return 0;
128 }
129 
130 int
ssh_digest_update(struct ssh_digest_ctx * ctx,const void * m,size_t mlen)131 ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen)
132 {
133           if (EVP_DigestUpdate(ctx->mdctx, m, mlen) != 1)
134                     return SSH_ERR_LIBCRYPTO_ERROR;
135           return 0;
136 }
137 
138 int
ssh_digest_update_buffer(struct ssh_digest_ctx * ctx,const struct sshbuf * b)139 ssh_digest_update_buffer(struct ssh_digest_ctx *ctx, const struct sshbuf *b)
140 {
141           return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b));
142 }
143 
144 int
ssh_digest_final(struct ssh_digest_ctx * ctx,u_char * d,size_t dlen)145 ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen)
146 {
147           const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg);
148           u_int l = dlen;
149 
150           if (digest == NULL || dlen > UINT_MAX)
151                     return SSH_ERR_INVALID_ARGUMENT;
152           if (dlen < digest->digest_len) /* No truncation allowed */
153                     return SSH_ERR_INVALID_ARGUMENT;
154           if (EVP_DigestFinal_ex(ctx->mdctx, d, &l) != 1)
155                     return SSH_ERR_LIBCRYPTO_ERROR;
156           if (l != digest->digest_len) /* sanity */
157                     return SSH_ERR_INTERNAL_ERROR;
158           return 0;
159 }
160 
161 void
ssh_digest_free(struct ssh_digest_ctx * ctx)162 ssh_digest_free(struct ssh_digest_ctx *ctx)
163 {
164           if (ctx == NULL)
165                     return;
166           EVP_MD_CTX_free(ctx->mdctx);
167           freezero(ctx, sizeof(*ctx));
168 }
169 
170 int
ssh_digest_memory(int alg,const void * m,size_t mlen,u_char * d,size_t dlen)171 ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen)
172 {
173           const struct ssh_digest *digest = ssh_digest_by_alg(alg);
174           u_int mdlen;
175 
176           if (digest == NULL)
177                     return SSH_ERR_INVALID_ARGUMENT;
178           if (dlen > UINT_MAX)
179                     return SSH_ERR_INVALID_ARGUMENT;
180           if (dlen < digest->digest_len)
181                     return SSH_ERR_INVALID_ARGUMENT;
182           mdlen = dlen;
183           if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL))
184                     return SSH_ERR_LIBCRYPTO_ERROR;
185           return 0;
186 }
187 
188 int
ssh_digest_buffer(int alg,const struct sshbuf * b,u_char * d,size_t dlen)189 ssh_digest_buffer(int alg, const struct sshbuf *b, u_char *d, size_t dlen)
190 {
191           return ssh_digest_memory(alg, sshbuf_ptr(b), sshbuf_len(b), d, dlen);
192 }
193