Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: digest-openssl.c,v 1.3 2014/06/24 01:13:21 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 | |
| 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_RIPEMD160, "RIPEMD160", 20, EVP_ripemd160 }, |
| 49 | { SSH_DIGEST_SHA1, "SHA1", 20, EVP_sha1 }, |
| 50 | #ifdef HAVE_EVP_SHA256 /* XXX replace with local if missing */ |
| 51 | { SSH_DIGEST_SHA256, "SHA256", 32, EVP_sha256 }, |
| 52 | { SSH_DIGEST_SHA384, "SHA384", 48, EVP_sha384 }, |
| 53 | { SSH_DIGEST_SHA512, "SHA512", 64, EVP_sha512 }, |
| 54 | #endif |
| 55 | { -1, NULL, 0, NULL }, |
| 56 | }; |
| 57 | |
| 58 | static const struct ssh_digest * |
| 59 | ssh_digest_by_alg(int alg) |
| 60 | { |
| 61 | if (alg < 0 || alg >= SSH_DIGEST_MAX) |
| 62 | return NULL; |
| 63 | if (digests[alg].id != alg) /* sanity */ |
| 64 | return NULL; |
| 65 | return &(digests[alg]); |
| 66 | } |
| 67 | |
| 68 | size_t |
| 69 | ssh_digest_bytes(int alg) |
| 70 | { |
| 71 | const struct ssh_digest *digest = ssh_digest_by_alg(alg); |
| 72 | |
| 73 | return digest == NULL ? 0 : digest->digest_len; |
| 74 | } |
| 75 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 76 | size_t |
| 77 | ssh_digest_blocksize(struct ssh_digest_ctx *ctx) |
| 78 | { |
| 79 | return EVP_MD_CTX_block_size(&ctx->mdctx); |
| 80 | } |
| 81 | |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 82 | struct ssh_digest_ctx * |
| 83 | ssh_digest_start(int alg) |
| 84 | { |
| 85 | const struct ssh_digest *digest = ssh_digest_by_alg(alg); |
| 86 | struct ssh_digest_ctx *ret; |
| 87 | |
| 88 | if (digest == NULL || ((ret = calloc(1, sizeof(*ret))) == NULL)) |
| 89 | return NULL; |
| 90 | ret->alg = alg; |
| 91 | EVP_MD_CTX_init(&ret->mdctx); |
| 92 | if (EVP_DigestInit_ex(&ret->mdctx, digest->mdfunc(), NULL) != 1) { |
| 93 | free(ret); |
| 94 | return NULL; |
| 95 | } |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 100 | ssh_digest_copy_state(struct ssh_digest_ctx *from, struct ssh_digest_ctx *to) |
| 101 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 102 | if (from->alg != to->alg) |
| 103 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 104 | /* we have bcopy-style order while openssl has memcpy-style */ |
| 105 | if (!EVP_MD_CTX_copy_ex(&to->mdctx, &from->mdctx)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 106 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 111 | ssh_digest_update(struct ssh_digest_ctx *ctx, const void *m, size_t mlen) |
| 112 | { |
| 113 | if (EVP_DigestUpdate(&ctx->mdctx, m, mlen) != 1) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 114 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 119 | 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] | 120 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 121 | return ssh_digest_update(ctx, sshbuf_ptr(b), sshbuf_len(b)); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int |
| 125 | ssh_digest_final(struct ssh_digest_ctx *ctx, u_char *d, size_t dlen) |
| 126 | { |
| 127 | const struct ssh_digest *digest = ssh_digest_by_alg(ctx->alg); |
| 128 | u_int l = dlen; |
| 129 | |
| 130 | if (dlen > UINT_MAX) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 131 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 132 | if (dlen < digest->digest_len) /* No truncation allowed */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 133 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 134 | if (EVP_DigestFinal_ex(&ctx->mdctx, d, &l) != 1) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 135 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 136 | if (l != digest->digest_len) /* sanity */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 137 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | ssh_digest_free(struct ssh_digest_ctx *ctx) |
| 143 | { |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 144 | if (ctx != NULL) { |
| 145 | EVP_MD_CTX_cleanup(&ctx->mdctx); |
Damien Miller | db3c595 | 2014-02-04 11:25:45 +1100 | [diff] [blame] | 146 | explicit_bzero(ctx, sizeof(*ctx)); |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 147 | free(ctx); |
| 148 | } |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int |
| 152 | ssh_digest_memory(int alg, const void *m, size_t mlen, u_char *d, size_t dlen) |
| 153 | { |
| 154 | struct ssh_digest_ctx *ctx = ssh_digest_start(alg); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 155 | int r; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 156 | |
| 157 | if (ctx == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 158 | return SSH_ERR_INVALID_ARGUMENT; |
| 159 | if ((r = ssh_digest_update(ctx, m, mlen) != 0) || |
| 160 | (r = ssh_digest_final(ctx, d, dlen) != 0)) |
| 161 | return r; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 162 | ssh_digest_free(ctx); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 167 | 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] | 168 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame^] | 169 | 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] | 170 | } |