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