Damien Miller | c174a3b | 2014-07-03 21:23:24 +1000 | [diff] [blame] | 1 | /* $OpenBSD: digest-openssl.c,v 1.4 2014/07/03 03:26:43 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 | |
| 20 | #include <sys/types.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 25 | #include <openssl/evp.h> |
| 26 | |
Darren Tucker | d23a91f | 2014-01-17 17:32:30 +1100 | [diff] [blame] | 27 | #include "openbsd-compat/openssl-compat.h" |
| 28 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 29 | #include "sshbuf.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 30 | #include "digest.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 31 | #include "ssherr.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 32 | |
Damien Miller | f6293a0 | 2014-07-17 09:01:25 +1000 | [diff] [blame] | 33 | #ifndef HAVE_EVP_RIPEMD160 |
| 34 | # define EVP_ripemd160 NULL |
| 35 | #endif /* HAVE_EVP_RIPEMD160 */ |
| 36 | #ifndef HAVE_EVP_SHA256 |
| 37 | # define EVP_sha256 NULL |
| 38 | # define EVP_sha384 NULL |
| 39 | # define EVP_sha512 NULL |
| 40 | #endif /* HAVE_EVP_SHA256 */ |
| 41 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 42 | struct ssh_digest_ctx { |
| 43 | int alg; |
| 44 | EVP_MD_CTX mdctx; |
| 45 | }; |
| 46 | |
| 47 | struct ssh_digest { |
| 48 | int id; |
| 49 | const char *name; |
| 50 | size_t digest_len; |
| 51 | const EVP_MD *(*mdfunc)(void); |
| 52 | }; |
| 53 | |
| 54 | /* NB. Indexed directly by algorithm number */ |
| 55 | const struct ssh_digest digests[] = { |
| 56 | { SSH_DIGEST_MD5, "MD5", 16, EVP_md5 }, |
| 57 | { SSH_DIGEST_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 }, |
| 58 | { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 }, |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 59 | { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 }, |
| 60 | { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 }, |
| 61 | { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 }, |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 62 | { -1, NULL, 0, NULL }, |
| 63 | }; |
| 64 | |
| 65 | static const struct ssh_digest * |
| 66 | ssh_digest_by_alg(int alg) |
| 67 | { |
| 68 | if (alg < 0 || alg >= SSH_DIGEST_MAX) |
| 69 | return NULL; |
| 70 | if (digests[alg].id != alg) /* sanity */ |
| 71 | return NULL; |
Damien Miller | f6293a0 | 2014-07-17 09:01:25 +1000 | [diff] [blame] | 72 | if (digests[alg].mdfunc == NULL) |
| 73 | return NULL; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 74 | return &(digests[alg]); |
| 75 | } |
| 76 | |
| 77 | size_t |
| 78 | ssh_digest_bytes(int alg) |
| 79 | { |
| 80 | const struct ssh_digest *digest = ssh_digest_by_alg(alg); |
| 81 | |
| 82 | return digest == NULL ? 0 : digest->digest_len; |
| 83 | } |
| 84 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 85 | size_t |
| 86 | ssh_digest_blocksize(struct ssh_digest_ctx *ctx) |
| 87 | { |
| 88 | return EVP_MD_CTX_block_size(&ctx->mdctx); |
| 89 | } |
| 90 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 91 | struct ssh_digest_ctx * |
| 92 | ssh_digest_start(int alg) |
| 93 | { |
| 94 | const struct ssh_digest *digest = ssh_digest_by_alg(alg); |
| 95 | struct ssh_digest_ctx *ret; |
| 96 | |
| 97 | if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL)) |
| 98 | return NULL; |
| 99 | ret->alg = alg; |
| 100 | EVP_MD_CTX_init(&ret->mdctx); |
| 101 | if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) { |
| 102 | free(ret); |
| 103 | return NULL; |
| 104 | } |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | int |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 109 | ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) |
| 110 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 111 | if (from->alg != to->alg) |
| 112 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 113 | /* we have bcopy-style order while openssl has memcpy-style */ |
| 114 | if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 115 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 120 | ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) |
| 121 | { |
| 122 | if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 123 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 128 | 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] | 129 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 130 | return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int |
| 134 | ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) |
| 135 | { |
| 136 | const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); |
| 137 | u_int l = dlen; |
| 138 | |
| 139 | if (dlen > UINT_MAX) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 140 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 141 | if (dlen < digest->digest_len) /* No truncation allowed */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 142 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 143 | if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 144 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 145 | if (l != digest->digest_len) /* sanity */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 146 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | ssh_digest_free(struct ssh_digest_ctx *ctx) |
| 152 | { |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 153 | if (ctx != NULL) { |
| 154 | EVP_MD_CTX_cleanup(&ctx->mdctx); |
Damien Miller | db3c595 | 2014-02-04 11:25:45 +1100 | [diff] [blame] | 155 | explicit_bzero(ctx, sizeof(*ctx)); |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 156 | free(ctx); |
| 157 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int |
| 161 | ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) |
| 162 | { |
Damien Miller | c174a3b | 2014-07-03 21:23:24 +1000 | [diff] [blame] | 163 | const struct ssh_digest *digest = ssh_digest_by_alg(alg); |
| 164 | u_int mdlen; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 165 | |
Damien Miller | c174a3b | 2014-07-03 21:23:24 +1000 | [diff] [blame] | 166 | if (digest == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 167 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | c174a3b | 2014-07-03 21:23:24 +1000 | [diff] [blame] | 168 | if (dlen > UINT_MAX) |
| 169 | return SSH_ERR_INVALID_ARGUMENT; |
| 170 | if (dlen < digest->digest_len) |
| 171 | return SSH_ERR_INVALID_ARGUMENT; |
| 172 | mdlen = dlen; |
| 173 | if (!EVP_Digest(m, mlen, d, &mdlen, digest->mdfunc(), NULL)) |
| 174 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 179 | 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] | 180 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 181 | 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] | 182 | } |